From 97a81b67b54874a82569354886d690f50277abce Mon Sep 17 00:00:00 2001 From: FunkyFr3sh Date: Sun, 13 Aug 2023 20:31:26 +0200 Subject: [PATCH] try to use hook=3 by default --- src/config.c | 11 +---------- src/dllmain.c | 1 - src/hook.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/config.c b/src/config.c index f110d54..284d445 100644 --- a/src/config.c +++ b/src/config.c @@ -323,11 +323,6 @@ static void cfg_create_ini() "; Note: Usually one of the following values will work: 60 / 30 / 25 / 20 / 15 (lower value = slower game speed)\n" "maxgameticks=0\n" "\n" - "; Windows API Hooking, Possible values: 0 = disabled, 1 = IAT Hooking, 2 = Microsoft Detours, 3 = IAT+GetProcAddress hook (All Modules), 4 = IAT Hooking (All Modules)\n" - "; Note: Change this value if windowed mode or upscaling isn't working properly\n" - "; Note: 'hook=3' will usually work for problematic games, if it doesn't then try 'hook=2' + 'renderer=gdi'\n" - "hook=4\n" - "\n" "; Force minimum FPS, possible values: 0 = disabled, -1 = use 'maxfps=' value, -2 = same as -1 but force full redraw, 1-1000 = custom FPS\n" "; Note: Set this to a low value such as 5 or 10 if some parts of the game are not being displayed (e.g. menus or loading screens)\n" "minfps=0\n" @@ -356,6 +351,7 @@ static void cfg_create_ini() "releasealt=false\n" "game_handles_close=false\n" "fixnotresponding=false\n" + "hook=4\n" "guard_lines=200\n" "max_resolutions=0\n" "limit_bltfast=false\n" @@ -866,7 +862,6 @@ static void cfg_create_ini() "; KKND2: Krossfire\n" "[KKND2]\n" "noactivateapp=true\n" - "hook=3\n" "\n" "; Lionheart\n" "[Lionheart]\n" @@ -1064,10 +1059,6 @@ static void cfg_create_ini() "height=0\n" "resizable=false\n" "\n" - "; Wizards and Warriors\n" - "[deep6]\n" - "hook=3\n" - "\n" "; War Wind\n" "[WW]\n" "renderer=opengl\n" diff --git a/src/dllmain.c b/src/dllmain.c index da9035d..92e6d2d 100644 --- a/src/dllmain.c +++ b/src/dllmain.c @@ -105,7 +105,6 @@ BOOL WINAPI DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved) } timeBeginPeriod(1); - g_hook_method = cfg_get_int("hook", 4); hook_init(TRUE); break; } diff --git a/src/hook.c b/src/hook.c index 0762cf5..b7a06df 100644 --- a/src/hook.c +++ b/src/hook.c @@ -364,6 +364,45 @@ void hook_patch_iat_list(HMODULE hmod, BOOL unhook, HOOKLIST* hooks) } } +BOOL hook_got_ddraw_import() +{ + __try + { + HMODULE hmod = GetModuleHandleA(NULL); + + PIMAGE_DOS_HEADER dos_header = (PIMAGE_DOS_HEADER)hmod; + if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) + return FALSE; + + PIMAGE_NT_HEADERS nt_headers = (PIMAGE_NT_HEADERS)((DWORD)dos_header + (DWORD)dos_header->e_lfanew); + if (nt_headers->Signature != IMAGE_NT_SIGNATURE) + return FALSE; + + PIMAGE_IMPORT_DESCRIPTOR import_desc = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD)dos_header + + (DWORD)(nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress)); + + if (import_desc == (PIMAGE_IMPORT_DESCRIPTOR)nt_headers) + return FALSE; + + while (import_desc->FirstThunk) + { + char* imp_module_name = (char*)((DWORD)dos_header + (DWORD)(import_desc->Name)); + + if (_stricmp(imp_module_name, "ddraw.dll") == 0) + { + return TRUE; + } + + import_desc++; + } + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + } + + return FALSE; +} + void hook_create(HOOKLIST* hooks, BOOL initial_hook) { #ifdef _MSC_VER @@ -525,6 +564,17 @@ void hook_revert(HOOKLIST* hooks) void hook_init(BOOL initial_hook) { + if (initial_hook) + { + g_hook_method = cfg_get_int("hook", 4); + + if (g_hook_method == 4 && hook_got_ddraw_import()) + { + /* Switch to 3 if we can be sure that ddraw.dll will not be unloaded from the process */ + g_hook_method = 3; + } + } + if (!g_hook_active || g_hook_method == 3 || g_hook_method == 4) { #if defined(_DEBUG) && defined(_MSC_VER)