From ad1a35aa84216219bdd1312b7dc5fb75c85295c0 Mon Sep 17 00:00:00 2001 From: FunkyFr3sh Date: Wed, 2 Aug 2023 17:09:01 +0200 Subject: [PATCH] remove dinputhook setting --- inc/directinput.h | 5 +++ inc/hook.h | 3 +- src/config.c | 16 -------- src/dd.c | 2 +- src/directinput.c | 98 ++++++++++++++++++++++++++++++++++++++++++++- src/dllmain.c | 4 +- src/hook.c | 99 ++-------------------------------------------- src/winapi_hooks.c | 38 ++++++++++++++++-- src/wndproc.c | 3 +- 9 files changed, 146 insertions(+), 122 deletions(-) diff --git a/inc/directinput.h b/inc/directinput.h index 0bd822c..cc0f25a 100644 --- a/inc/directinput.h +++ b/inc/directinput.h @@ -14,6 +14,8 @@ typedef HRESULT(WINAPI* DIDSETCOOPERATIVELEVELPROC)(IDirectInputDeviceA*, HWND, typedef HRESULT(WINAPI* DIDGETDEVICEDATAPROC)(IDirectInputDeviceA*, DWORD, LPDIDEVICEOBJECTDATA, LPDWORD, DWORD); typedef HRESULT(WINAPI* DIDGETDEVICESTATEPROC)(IDirectInputDeviceA*, DWORD, LPVOID); +extern BOOL g_dinput_hook_active; + extern DIRECTINPUTCREATEAPROC real_DirectInputCreateA; extern DIRECTINPUTCREATEWPROC real_DirectInputCreateW; extern DIRECTINPUTCREATEEXPROC real_DirectInputCreateEx; @@ -24,4 +26,7 @@ HRESULT WINAPI fake_DirectInputCreateW(HINSTANCE hinst, DWORD dwVersion, LPDIREC HRESULT WINAPI fake_DirectInputCreateEx(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPDIRECTINPUT7A* ppvOut, LPUNKNOWN punkOuter); HRESULT WINAPI fake_DirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPDIRECTINPUT8* ppvOut, LPUNKNOWN punkOuter); +void dinput_hook_init(); +void dinput_hook_exit(); + #endif diff --git a/inc/hook.h b/inc/hook.h index d42bef9..e5025c6 100644 --- a/inc/hook.h +++ b/inc/hook.h @@ -99,10 +99,9 @@ extern COCREATEINSTANCEPROC real_CoCreateInstance; extern SETUNHANDLEDEXCEPTIONFILTERPROC real_SetUnhandledExceptionFilter; extern int g_hook_method; -extern BOOL g_hook_dinput; extern BOOL g_hook_active; -void hook_init(); +void hook_init(BOOL initial_hook); void hook_exit(); void hook_patch_iat(HMODULE hmod, BOOL unhook, char* module_name, char* function_name, PROC new_function); void hook_patch_iat_list(HMODULE hmod, BOOL unhook, HOOKLIST* hooks); diff --git a/src/config.c b/src/config.c index 4272f84..9b785a0 100644 --- a/src/config.c +++ b/src/config.c @@ -76,8 +76,6 @@ void cfg_load() g_config.save_settings = cfg_get_int("savesettings", 1); - g_hook_dinput = cfg_get_bool("dinputhook", FALSE); - g_ddraw->render.maxfps = cfg_get_int("maxfps", -1); g_ddraw->render.minfps = cfg_get_int("minfps", 0); @@ -351,7 +349,6 @@ static void cfg_create_ini() "lock_surfaces=false\n" "releasealt=false\n" "allow_wmactivate=false\n" - "dinputhook=false\n" "flipclear=false\n" "fixmousehook=false\n" "rgb555=false\n" @@ -735,14 +732,6 @@ static void cfg_create_ini() "maxgameticks=60\n" "fixnotresponding=true\n" "\n" - "; Fallout\n" - "[falloutw]\n" - "dinputhook=true\n" - "\n" - "; Fallout 2\n" - "[FALLOUT2]\n" - "dinputhook=true\n" - "\n" "; Fairy Tale About Father Frost, Ivan and Nastya\n" "[mrazik]\n" "guard_lines=0\n" @@ -878,13 +867,8 @@ static void cfg_create_ini() "[Mech3]\n" "nonexclusive=true\n" "\n" - "; Moorhuhn\n" - "[Moorhuhn]\n" - "dinputhook=true\n" - "\n" "; Moorhuhn 2\n" "[Moorhuhn2]\n" - "dinputhook=true\n" "releasealt=true\n" "\n" "; New Robinson\n" diff --git a/src/dd.c b/src/dd.c index c7eab42..2df6c39 100644 --- a/src/dd.c +++ b/src/dd.c @@ -924,7 +924,7 @@ HRESULT dd_SetCooperativeLevel(HWND hwnd, DWORD dwFlags) if (!g_ddraw->wndproc) { - hook_init(); + hook_init(FALSE); g_ddraw->wndproc = (WNDPROC)real_SetWindowLongA(g_ddraw->hwnd, GWL_WNDPROC, (LONG)fake_WndProc); g_ddraw->gui_thread_id = GetWindowThreadProcessId(g_ddraw->hwnd, NULL); diff --git a/src/directinput.c b/src/directinput.c index 047a2f1..4cee4b4 100644 --- a/src/directinput.c +++ b/src/directinput.c @@ -1,4 +1,3 @@ - #include #include #include "directinput.h" @@ -7,6 +6,11 @@ #include "dd.h" #include "mouse.h" +#ifdef _MSC_VER +#include "detours.h" +#endif + +BOOL g_dinput_hook_active; DIRECTINPUTCREATEAPROC real_DirectInputCreateA; DIRECTINPUTCREATEWPROC real_DirectInputCreateW; @@ -317,3 +321,95 @@ HRESULT WINAPI fake_DirectInput8Create( return result; } + +void dinput_hook_init() +{ +#ifdef _MSC_VER + if (!g_dinput_hook_active) + { + g_dinput_hook_active = TRUE; + + real_DirectInputCreateA = (void*)GetProcAddress(LoadLibraryA("dinput.dll"), "DirectInputCreateA"); + + if (real_DirectInputCreateA && real_DirectInputCreateA != fake_DirectInputCreateA) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourAttach((PVOID*)&real_DirectInputCreateA, (PVOID)fake_DirectInputCreateA); + DetourTransactionCommit(); + } + + real_DirectInputCreateW = (void*)GetProcAddress(LoadLibraryA("dinput.dll"), "DirectInputCreateW"); + + if (real_DirectInputCreateW && real_DirectInputCreateW != fake_DirectInputCreateW) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourAttach((PVOID*)&real_DirectInputCreateW, (PVOID)fake_DirectInputCreateW); + DetourTransactionCommit(); + } + + real_DirectInputCreateEx = (void*)GetProcAddress(LoadLibraryA("dinput.dll"), "DirectInputCreateEx"); + + if (real_DirectInputCreateEx && real_DirectInputCreateEx != fake_DirectInputCreateEx) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourAttach((PVOID*)&real_DirectInputCreateEx, (PVOID)fake_DirectInputCreateEx); + DetourTransactionCommit(); + } + + real_DirectInput8Create = (void*)GetProcAddress(LoadLibraryA("dinput8.dll"), "DirectInput8Create"); + + if (real_DirectInput8Create && real_DirectInput8Create != fake_DirectInput8Create) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourAttach((PVOID*)&real_DirectInput8Create, (PVOID)fake_DirectInput8Create); + DetourTransactionCommit(); + } + } +#endif +} + +void dinput_hook_exit() +{ +#ifdef _MSC_VER + if (g_dinput_hook_active) + { + if (real_DirectInputCreateA) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourDetach((PVOID*)&real_DirectInputCreateA, (PVOID)fake_DirectInputCreateA); + DetourTransactionCommit(); + } + + if (real_DirectInputCreateW) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourDetach((PVOID*)&real_DirectInputCreateW, (PVOID)fake_DirectInputCreateW); + DetourTransactionCommit(); + } + + if (real_DirectInputCreateEx) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourDetach((PVOID*)&real_DirectInputCreateEx, (PVOID)fake_DirectInputCreateEx); + DetourTransactionCommit(); + } + + if (real_DirectInput8Create) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourDetach((PVOID*)&real_DirectInput8Create, (PVOID)fake_DirectInput8Create); + DetourTransactionCommit(); + } + + g_dinput_hook_active = FALSE; + } +#endif +} diff --git a/src/dllmain.c b/src/dllmain.c index b084ecc..13832a1 100644 --- a/src/dllmain.c +++ b/src/dllmain.c @@ -2,6 +2,7 @@ #include "ddraw.h" #include #include "dllmain.h" +#include "directinput.h" #include "IDirectDraw.h" #include "dd.h" #include "ddclipper.h" @@ -102,7 +103,7 @@ BOOL WINAPI DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved) timeBeginPeriod(1); g_hook_method = cfg_get_int("hook", 4); - hook_init(); + hook_init(TRUE); break; } case DLL_PROCESS_DETACH: @@ -112,6 +113,7 @@ BOOL WINAPI DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved) cfg_save(); timeEndPeriod(1); + dinput_hook_exit(); hook_exit(); break; } diff --git a/src/hook.c b/src/hook.c index a8d4e37..8add6b2 100644 --- a/src/hook.c +++ b/src/hook.c @@ -15,7 +15,6 @@ #endif BOOL g_hook_active; -BOOL g_hook_dinput; int g_hook_method = 1; GETCURSORPOSPROC real_GetCursorPos = GetCursorPos; @@ -536,66 +535,13 @@ void hook_revert(HOOKLIST* hooks) } } -void hook_init() +void hook_init(BOOL initial_hook) { if (!g_hook_active || g_hook_method == 3 || g_hook_method == 4) { - BOOL initial_hook = !g_hook_active; - -#ifdef _MSC_VER - if (initial_hook && cfg_get_bool("dinputhook", FALSE)) - { - real_DirectInputCreateA = - (DIRECTINPUTCREATEAPROC)GetProcAddress(LoadLibraryA("dinput.dll"), "DirectInputCreateA"); - - if (real_DirectInputCreateA) - { - DetourTransactionBegin(); - DetourUpdateThread(GetCurrentThread()); - DetourAttach((PVOID*)&real_DirectInputCreateA, (PVOID)fake_DirectInputCreateA); - DetourTransactionCommit(); - } - - real_DirectInputCreateW = - (DIRECTINPUTCREATEWPROC)GetProcAddress(LoadLibraryA("dinput.dll"), "DirectInputCreateW"); - - if (real_DirectInputCreateW) - { - DetourTransactionBegin(); - DetourUpdateThread(GetCurrentThread()); - DetourAttach((PVOID*)&real_DirectInputCreateW, (PVOID)fake_DirectInputCreateW); - DetourTransactionCommit(); - } - - real_DirectInputCreateEx = - (DIRECTINPUTCREATEEXPROC)GetProcAddress(LoadLibraryA("dinput.dll"), "DirectInputCreateEx"); - - if (real_DirectInputCreateEx) - { - DetourTransactionBegin(); - DetourUpdateThread(GetCurrentThread()); - DetourAttach((PVOID*)&real_DirectInputCreateEx, (PVOID)fake_DirectInputCreateEx); - DetourTransactionCommit(); - } - - real_DirectInput8Create = - (DIRECTINPUT8CREATEPROC)GetProcAddress(LoadLibraryA("dinput8.dll"), "DirectInput8Create"); - - if (real_DirectInput8Create) - { - DetourTransactionBegin(); - DetourUpdateThread(GetCurrentThread()); - DetourAttach((PVOID*)&real_DirectInput8Create, (PVOID)fake_DirectInput8Create); - DetourTransactionCommit(); - } - } -#endif - #if defined(_DEBUG) && defined(_MSC_VER) if (initial_hook) { - hook_patch_iat(GetModuleHandle(NULL), FALSE, "kernel32.dll", "SetUnhandledExceptionFilter", (PROC)fake_SetUnhandledExceptionFilter); - DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); DetourAttach((PVOID*)&real_SetUnhandledExceptionFilter, (PVOID)fake_SetUnhandledExceptionFilter); @@ -608,9 +554,9 @@ void hook_init() hook_patch_iat(GetModuleHandle("AcGenral"), FALSE, "user32.dll", "SetWindowsHookExA", (PROC)fake_SetWindowsHookExA); } - g_hook_active = TRUE; - hook_create((HOOKLIST*)&g_hooks, initial_hook); + + g_hook_active = TRUE; } } @@ -620,43 +566,6 @@ void hook_exit() { g_hook_active = FALSE; -#ifdef _MSC_VER - if (cfg_get_bool("dinputhook", FALSE)) - { - if (real_DirectInputCreateA) - { - DetourTransactionBegin(); - DetourUpdateThread(GetCurrentThread()); - DetourDetach((PVOID*)&real_DirectInputCreateA, (PVOID)fake_DirectInputCreateA); - DetourTransactionCommit(); - } - - if (real_DirectInputCreateW) - { - DetourTransactionBegin(); - DetourUpdateThread(GetCurrentThread()); - DetourDetach((PVOID*)&real_DirectInputCreateW, (PVOID)fake_DirectInputCreateW); - DetourTransactionCommit(); - } - - if (real_DirectInputCreateEx) - { - DetourTransactionBegin(); - DetourUpdateThread(GetCurrentThread()); - DetourDetach((PVOID*)&real_DirectInputCreateEx, (PVOID)fake_DirectInputCreateEx); - DetourTransactionCommit(); - } - - if (real_DirectInput8Create) - { - DetourTransactionBegin(); - DetourUpdateThread(GetCurrentThread()); - DetourDetach((PVOID*)&real_DirectInput8Create, (PVOID)fake_DirectInput8Create); - DetourTransactionCommit(); - } - } -#endif - hook_revert((HOOKLIST*)&g_hooks); } @@ -666,8 +575,6 @@ void hook_exit() DetourDetach((PVOID*)&real_SetUnhandledExceptionFilter, (PVOID)fake_SetUnhandledExceptionFilter); DetourTransactionCommit(); - hook_patch_iat(GetModuleHandle(NULL), TRUE, "kernel32.dll", "SetUnhandledExceptionFilter", (PROC)fake_SetUnhandledExceptionFilter); - real_SetUnhandledExceptionFilter(g_dbg_exception_filter); #endif diff --git a/src/winapi_hooks.c b/src/winapi_hooks.c index f3dfb38..3bc9a92 100644 --- a/src/winapi_hooks.c +++ b/src/winapi_hooks.c @@ -11,7 +11,9 @@ #include "mouse.h" #include "wndproc.h" #include "render_gdi.h" +#include "directinput.h" #include "ddsurface.h" +#include "dllmain.h" BOOL WINAPI fake_GetCursorPos(LPPOINT lpPoint) @@ -911,7 +913,14 @@ HMODULE WINAPI fake_LoadLibraryA(LPCSTR lpLibFileName) } #endif - hook_init(); + if (hmod && hmod != g_ddraw_module && lpLibFileName && + (_strcmpi(lpLibFileName, "dinput.dll") == 0 || _strcmpi(lpLibFileName, "dinput") == 0 || + _strcmpi(lpLibFileName, "dinput8.dll") == 0 || _strcmpi(lpLibFileName, "dinput8") == 0)) + { + dinput_hook_init(); + } + + hook_init(FALSE); return hmod; } @@ -928,7 +937,14 @@ HMODULE WINAPI fake_LoadLibraryW(LPCWSTR lpLibFileName) } #endif - hook_init(); + if (hmod && hmod != g_ddraw_module && lpLibFileName && + (_wcsicmp(lpLibFileName, L"dinput.dll") == 0 || _wcsicmp(lpLibFileName, L"dinput") == 0 || + _wcsicmp(lpLibFileName, L"dinput8.dll") == 0 || _wcsicmp(lpLibFileName, L"dinput8") == 0)) + { + dinput_hook_init(); + } + + hook_init(FALSE); return hmod; } @@ -945,7 +961,14 @@ HMODULE WINAPI fake_LoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwF } #endif - hook_init(); + if (hmod && hmod != g_ddraw_module && lpLibFileName && + (_strcmpi(lpLibFileName, "dinput.dll") == 0 || _strcmpi(lpLibFileName, "dinput") == 0 || + _strcmpi(lpLibFileName, "dinput8.dll") == 0 || _strcmpi(lpLibFileName, "dinput8") == 0)) + { + dinput_hook_init(); + } + + hook_init(FALSE); return hmod; } @@ -962,7 +985,14 @@ HMODULE WINAPI fake_LoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dw } #endif - hook_init(); + if (hmod && hmod != g_ddraw_module && lpLibFileName && + (_wcsicmp(lpLibFileName, L"dinput.dll") == 0 || _wcsicmp(lpLibFileName, L"dinput") == 0 || + _wcsicmp(lpLibFileName, L"dinput8.dll") == 0 || _wcsicmp(lpLibFileName, L"dinput8") == 0)) + { + dinput_hook_init(); + } + + hook_init(FALSE); return hmod; } diff --git a/src/wndproc.c b/src/wndproc.c index 00e1514..413cd5e 100644 --- a/src/wndproc.c +++ b/src/wndproc.c @@ -9,6 +9,7 @@ #include "config.h" #include "screenshot.h" #include "winapi_hooks.h" +#include "directinput.h" #include "wndproc.h" #include "utils.h" #include "debug.h" @@ -560,7 +561,7 @@ LRESULT CALLBACK fake_WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ip.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1, &ip, sizeof(ip)); - if (g_hook_dinput) + if (g_dinput_hook_active) { ip.type = INPUT_KEYBOARD; ip.ki.wScan = 56; // LeftAlt