Skip to content

Commit

Permalink
fix sometimes d3d11 hook is being created before the game even loads …
Browse files Browse the repository at this point in the history
…the dll
  • Loading branch information
0x-FADED committed Aug 20, 2024
1 parent 71801f1 commit dfb34d1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
13 changes: 10 additions & 3 deletions IHHook/D3D11Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ D3D11Hook::~D3D11Hook() {
bool D3D11Hook::hook() {
spdlog::info("Hooking D3D11");

auto pD3D11CreateDeviceAndSwapChain = (decltype(&::D3D11CreateDeviceAndSwapChain)) GetProcAddress(GetModuleHandleW(L"d3d11.dll"), "D3D11CreateDeviceAndSwapChain");

if (pD3D11CreateDeviceAndSwapChain == nullptr){
spdlog::error("d3d11.dll is not loaded can't hook d3d11 functions");

return m_hooked = false;
}
g_d3d11_hook = this;

HWND h_wnd = GetDesktopWindow();
IDXGISwapChain* swap_chain = nullptr;
ID3D11Device* device = nullptr;
D3D_FEATURE_LEVEL device_max_feature_level = D3D_FEATURE_LEVEL_9_1;
D3D_FEATURE_LEVEL device_max_feature_level;
ID3D11DeviceContext* context = nullptr;

D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_11_0;
D3D_FEATURE_LEVEL feature_level[] = {D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_11_0};
DXGI_SWAP_CHAIN_DESC swap_chain_desc;

ZeroMemory(&swap_chain_desc, sizeof(swap_chain_desc));
Expand All @@ -39,7 +46,7 @@ bool D3D11Hook::hook() {
swap_chain_desc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
spdlog::info("Creating dummy D3D11 device.");
HRESULT hr = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_NULL, nullptr, 0, &feature_level, 1, D3D11_SDK_VERSION, &swap_chain_desc, &swap_chain, &device, &device_max_feature_level, &context);
HRESULT hr = pD3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_NULL, nullptr, 0, feature_level, 1, D3D11_SDK_VERSION, &swap_chain_desc, &swap_chain, &device, &device_max_feature_level, &context);
if (FAILED(hr)) {
spdlog::error("Failed to create dummy D3D11 device. HRESULT={0:x} max_feature={1:x}", hr, device_max_feature_level);
return false;
Expand Down
33 changes: 32 additions & 1 deletion IHHook/IHHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ namespace IHHook {
}
}//InitCursorHook

typedef HMODULE(WINAPI* LoadLibraryAFn)(LPCSTR);
LoadLibraryAFn g_LoadLibraryA = NULL;

bool IHH::isD3D11Loaded = false;
HMODULE WINAPI LoadLibraryA_Hook(LPCSTR lpFileName)
{
if (strcmp(lpFileName, "d3d11.dll") == 0)
{
spdlog::info("LoadLibraryA_Hook: {:} is loaded", lpFileName);
g_ihhook->isD3D11Loaded = true;
}

return g_LoadLibraryA(lpFileName);
}

void InitLoadLibraryAHook()
{
auto log = spdlog::get("ihhook");

if (MH_CreateHook(&LoadLibraryA, &LoadLibraryA_Hook, reinterpret_cast<LPVOID*>(&g_LoadLibraryA)) != MH_OK)
{
log->error("Couldn't create hook for LoadLibraryA.");
}

if (MH_EnableHook(&LoadLibraryA) != MH_OK)
{
log->error("Couldn't enable LoadLibraryA hook.");
}
}

void Shutdown() {
spdlog::debug("IHHook DLL_PROCESS_DETACH");
doShutDown = true;
Expand Down Expand Up @@ -303,7 +333,7 @@ namespace IHHook {

auto tend = std::chrono::high_resolution_clock::now();
auto durationShort = std::chrono::duration_cast<std::chrono::microseconds>(tend - tstart).count();
spdlog::debug("IHHook::CreateHooks total time(microseconds): {}µs", durationShort);
spdlog::debug("IHHook::CreateHooks total time(microseconds): {}�s", durationShort);
}//if doHooks

PipeServer::StartPipeServer();
Expand Down Expand Up @@ -868,6 +898,7 @@ namespace IHHook {
}//RebaseAddresses

void IHH::CreateAllHooks() {
InitLoadLibraryAHook();
Hooks_CityHash::CreateHooks(RealBaseAddr);//TODO: rebase/convert to same style as rest, so don't have to pass in realbaseaddr
Hooks_FNVHash::CreateHooks();
Hooks_Lua::CreateHooks();
Expand Down
1 change: 1 addition & 0 deletions IHHook/IHHook.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ namespace IHHook {
void OnReset();

bool OnMessage(HWND wnd, UINT message, WPARAM w_param, LPARAM l_param);
static bool isD3D11Loaded;
private:
void SetupLog();
std::string GetLangVersion();
Expand Down
5 changes: 5 additions & 0 deletions IHHook/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ HMODULE g_thisModule;
extern HMODULE origDll;//dinputproxy

DWORD WINAPI InitThread(LPVOID lpParameter) {

if (g_ihhook->isD3D11Loaded == true){

g_ihhook->Initialize();

}

return 0;
}//InitThread
Expand Down

0 comments on commit dfb34d1

Please sign in to comment.