#include "Hook.hpp"
#ifdef _DEBUG
#include <cstdio>
#define DEBUG_PRINT(format, ...) printf(format, __VA_ARGS__)
#else
#define DEBUG_PRINT(format, ...) (void)0
#endif
#include "minhook/include/MinHook.h"
#include "FindPattern/FindPattern.h"
#include "Models.hpp"
namespace Hook
{
// Safely releases a COM object and sets the pointer to nullptr
inline void SafeRelease(IUnknown* dxObject)
{
if (dxObject)
{
dxObject->Release();
dxObject = nullptr;
}
}
// Safely releases an array of shader resource views
inline void SafeReleaseSRVs(ID3D11ShaderResourceView* SRVs[])
{
for (size_t i = 0; i < D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; ++i)
{
if (SRVs[i] == nullptr)
{
break;
}
SafeRelease(SRVs[i]);
}
}
// Counts the number of valid shader resource views in the array
inline size_t CountSRVs(ID3D11ShaderResourceView* SRVs[])
{
size_t count = 0;
while (count < D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT && SRVs[count] != nullptr)
{
++count;
}
return count;
}
// Initialization function to set up hooks
DWORD WINAPI Init()
{
// Initialize MinHook
MH_STATUS mhStatus = MH_Initialize();
if (mhStatus != MH_OK)
{
DEBUG_PRINT("Error: Failed to initialize MinHook! Status: %d\n", mhStatus);
return FALSE;
}
// Get the module handle for d3d11.dll
HMODULE d3d11Module = GetModuleHandleW(L"d3d11.dll");
if (!d3d11Module)
{
DEBUG_PRINT("Error: Failed to get d3d11.dll module handle. LastError: %lu\n", GetLastError());
return FALSE;
}
DEBUG_PRINT("d3d11.dll module handle: %p\n", static_cast<void*>(d3d11Module));
// Define search offsets and ranges
constexpr size_t SEARCH_OFFSET = 0x100000;
constexpr size_t SEARCH_RANGE = 0x200000;
uintptr_t moduleBase = reinterpret_cast<uintptr_t>(d3d11Module);
uintptr_t searchStart = moduleBase + SEARCH_OFFSET;
size_t searchLength = SEARCH_RANGE - SEARCH_OFFSET;
// Attempt to find the DrawIndexedInstancedIndirect function using pattern scanning
uintptr_t functionAddress = IgroWidgets::FindPattern(
searchStart,
searchLength,
reinterpret_cast<const unsigned char*>("\x48\x89\x5C\x24\x08\x48\x89\x74\x24\x10\x57\x48\x83\xEC\x00\x41\x8B\xF0\x48\x8B\xDA\x48\x8D\xB9\x00\x00\x00\xFF\x48\x8B\xCF\xE8\x00\x00\x00\x00\x84\xC0\x74\x00\x48\x85\xDB\x74\x00\x8B\x8B\x00\x00\x00\x00\x8B\x93"),
"xxxxxxxxxxxxxx?xxxxxxxxx???xxxxx????xxx?xxxx?xx????xx"
);
if (functionAddress == 0)
{
DEBUG_PRINT("Couldn't find DrawIndexedInstancedIndirect with Windows 11 pattern, trying with Windows 10...\n");
functionAddress = IgroWidgets::FindPattern(
searchStart,
searchLength,
reinterpret_cast<const unsigned char*>("\x48\x83\xEC\x00\x4C\x8B\xD1\x48\x85\xD2\x74\x00\x8B\x82"),
"xxx?xxxxxxx?xx"
);
}
// Assign the found function address
if (functionAddress == 0)
{
DEBUG_PRINT("Error: Couldn't find DrawIndexedInstancedIndirect function!\n");
return FALSE;
}
DEBUG_PRINT("Found DrawIndexedInstancedIndirect function at: 0x%llX\n", functionAddress);
// Cast the function address to LPVOID
LPVOID targetFunction = reinterpret_cast<LPVOID>(functionAddress);
DrawIndexedInstancedIndirectAddress = targetFunction;
// Create the hook for DrawIndexedInstancedIndirect
mhStatus = MH_CreateHook(
DrawIndexedInstancedIndirectAddress,
&DetourDrawIndexedInstancedIndirect,
reinterpret_cast<LPVOID*>(&OriginalDrawIndexedInstancedIndirect)
);
if (mhStatus != MH_OK)
{
DEBUG_PRINT("Error: Failed MH_CreateHook for DrawIndexedInstancedIndirect! Status: %d\n", mhStatus);
return FALSE;
}
// Enable the hook
mhStatus = MH_EnableHook(DrawIndexedInstancedIndirectAddress);
if (mhStatus != MH_OK)
{
DEBUG_PRINT("Error: Failed MH_EnableHook for DrawIndexedInstancedIndirect! Status: %d\n", mhStatus);
return FALSE;
}
DEBUG_PRINT("DrawIndexedInstancedIndirect hook enabled\n");
return TRUE;
}
// Cleanup function to disable hooks and release resources
void Unload()
{
if (DrawIndexedInstancedIndirectAddress)
{
MH_DisableHook(DrawIndexedInstancedIndirectAddress);
}
SafeRelease(wallHackDSS);
SafeRelease(redSRV);
SafeRelease(greenSRV);
}
// Creates a shader resource view (SRV) with a solid color
void CreateColorSRV(ID3D11Device* device, const RGBA& color, ID3D11ShaderResourceView** shaderResourceView)
{
if (!device || !shaderResourceView)
return;
D3D11_SUBRESOURCE_DATA initData = {};
initData.pSysMem = &color;
initData.SysMemPitch = sizeof(color);
initData.SysMemSlicePitch = 0;
ID3D11Texture2D* texture = nullptr;
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = 1;
desc.Height = 1;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
HRESULT hr = device->CreateTexture2D(&desc, &initData, &texture);
if (SUCCEEDED(hr) && texture)
{
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
SRVDesc.Format = desc.Format;
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
SRVDesc.Texture2D.MipLevels = 1;
hr = device->CreateShaderResourceView(texture, &SRVDesc, shaderResourceView);
if (FAILED(hr))
{
DEBUG_PRINT("Error: Failed to create shader resource view. HRESULT: 0x%X\n", hr);
}
SafeRelease(texture);
}
else
{
DEBUG_PRINT("Error: Failed to create texture2D. HRESULT: 0x%X\n", hr);
}
}
// Creates red and green SRVs if they haven't been created yet
void CreateColorSRVs(ID3D11Device* device)
{
if (!device || (greenSRV && redSRV))
return;
const RGBA red(255, 0, 0, 255);
const RGBA green(0, 255, 0, 255);
CreateColorSRV(device, red, &redSRV);
CreateColorSRV(device, green, &greenSRV);
}
// Creates a Depth Stencil State (DSS) for wall hacks
void CreateDepthStencilStates(ID3D11Device* device)
{
if (!device || wallHackDSS)
return;
D3D11_DEPTH_STENCIL_DESC depthStencilDesc = {};
depthStencilDesc.DepthEnable = FALSE;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_GREATER_EQUAL;
depthStencilDesc.StencilEnable = FALSE;
depthStencilDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK;
depthStencilDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK;
// Front face stencil operations
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
// Back face stencil operations
depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
HRESULT hr = device->CreateDepthStencilState(&depthStencilDesc, &wallHackDSS);
if (FAILED(hr))
{
DEBUG_PRINT("Error: Failed to create DepthStencilState. HRESULT: 0x%X\n", hr);
}
}
// Draws chams (colored models) based on visibility
bool DrawChams(
ID3D11DeviceContext* pContext,
ID3D11Buffer* pBufferForArgs,
UINT AlignedByteOffsetForArgs,
ID3D11ShaderResourceView* SRVs[],
ID3D11ShaderResourceView* srvWhenInvisible,
ID3D11ShaderResourceView* srvWhenVisible
)
{
if (!pContext || !pBufferForArgs || !SRVs)
return false;
const size_t srvCount = CountSRVs(SRVs);
if (srvWhenInvisible)
{
ID3D11DepthStencilState* originalDSS = nullptr;
UINT originalStencilRef = 0;
pContext->OMGetDepthStencilState(&originalDSS, &originalStencilRef);
for (size_t i = 0; i < srvCount; ++i)
{
pContext->PSSetShaderResources(static_cast<UINT>(i), 1, &srvWhenInvisible);
}
pContext->OMSetDepthStencilState(wallHackDSS, originalStencilRef);
OriginalDrawIndexedInstancedIndirect(pContext, pBufferForArgs, AlignedByteOffsetForArgs);
pContext->OMSetDepthStencilState(originalDSS, originalStencilRef);
SafeRelease(originalDSS);
}
if (srvWhenVisible)
{
for (size_t i = 0; i < srvCount; ++i)
{
pContext->PSSetShaderResources(static_cast<UINT>(i), 1, &srvWhenVisible);
}
OriginalDrawIndexedInstancedIndirect(pContext, pBufferForArgs, AlignedByteOffsetForArgs);
return true;
}
return false;
}
// Detour function for DrawIndexedInstancedIndirect
void __stdcall DetourDrawIndexedInstancedIndirect(
ID3D11DeviceContext* pContext,
ID3D11Buffer* pBufferForArgs,
UINT AlignedByteOffsetForArgs
)
{
#ifdef _DEBUG
static bool printOnce = true;
if (printOnce)
{
DEBUG_PRINT("DetourDrawIndexedInstancedIndirect was called, confirmation that the right function was hooked\n");
printOnce = false;
}
#endif
if (!pContext)
{
OriginalDrawIndexedInstancedIndirect(pContext, pBufferForArgs, AlignedByteOffsetForArgs);
return;
}
ID3D11Device* device = nullptr;
pContext->GetDevice(&device);
if (!device)
{
OriginalDrawIndexedInstancedIndirect(pContext, pBufferForArgs, AlignedByteOffsetForArgs);
return;
}
CreateColorSRVs(device);
CreateDepthStencilStates(device);
SafeRelease(device);
ID3D11Buffer* veBuffer = nullptr;
UINT stride = 0;
UINT veBufferOffset = 0;
pContext->IAGetVertexBuffers(0, 1, &veBuffer, &stride, &veBufferOffset);
SafeRelease(veBuffer);
ID3D11Buffer* pscBuffer = nullptr;
D3D11_BUFFER_DESC pscDesc = {};
UINT pscWidth = 0;
pContext->PSGetConstantBuffers(0, 1, &pscBuffer);
if (pscBuffer)
{
pscBuffer->GetDesc(&pscDesc);
pscWidth = pscDesc.ByteWidth;
SafeRelease(pscBuffer);
}
ID3D11Buffer* vscBuffer = nullptr;
D3D11_BUFFER_DESC vscDesc = {};
UINT vscWidth = 0;
pContext->VSGetConstantBuffers(0, 1, &vscBuffer);
if (vscBuffer)
{
vscBuffer->GetDesc(&vscDesc);
vscWidth = vscDesc.ByteWidth;
SafeRelease(vscBuffer);
}
const UINT compVscWidth = vscWidth / 10;
const UINT compPscWidth = pscWidth / 10;
ID3D11ShaderResourceView* originalSRVs[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT] = {};
pContext->PSGetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, originalSRVs);
bool isModel =
Models::Body1.IsModel(stride, compVscWidth, compPscWidth, originalSRVs) ||
Models::Body2.IsModel(stride, compVscWidth, compPscWidth, originalSRVs) ||
Models::Skin1.IsModel(stride, compVscWidth, compPscWidth, originalSRVs) ||
Models::Skin2.IsModel(stride, compVscWidth, compPscWidth, originalSRVs);
bool isWorldOverlay =
Models::DroneWorldOverlay.IsModel(stride, compVscWidth, compPscWidth, originalSRVs) ||
Models::WorldOverlay1.IsModel(stride, compVscWidth, compPscWidth, originalSRVs) ||
Models::WorldOverlay2.IsModel(stride, compVscWidth, compPscWidth, originalSRVs) ||
Models::WorldOverlay3.IsModel(stride, compVscWidth, compPscWidth, originalSRVs) ||
(Models::WorldOverlay4.IsModel(stride, compVscWidth, compPscWidth, originalSRVs) &&
!Models::Scope.IsModel(stride, compVscWidth, compPscWidth, originalSRVs));
if (isModel)
{
if (DrawChams(pContext, pBufferForArgs, AlignedByteOffsetForArgs, originalSRVs, redSRV, greenSRV))
{
SafeReleaseSRVs(originalSRVs);
return;
}
}
else if (isWorldOverlay)
{
SafeReleaseSRVs(originalSRVs);
return;
}
SafeReleaseSRVs(originalSRVs);
OriginalDrawIndexedInstancedIndirect(pContext, pBufferForArgs, AlignedByteOffsetForArgs);
}
}