1
0
mirror of https://github.com/FunkyFr3sh/cnc-ddraw.git synced 2025-03-24 17:49:52 +01:00

make Windows API Hooking optional

This commit is contained in:
FunkyFr3sh 2019-04-01 22:54:36 +02:00
parent 9bd26df82c
commit 9e1397d0f6
4 changed files with 34 additions and 29 deletions

View File

@ -46,6 +46,6 @@ extern BOOL Hook_Active;
void Hook_Init(); void Hook_Init();
void Hook_PatchIAT(HMODULE hMod, char *moduleName, char *functionName, PROC newFunction); void Hook_PatchIAT(HMODULE hMod, char *moduleName, char *functionName, PROC newFunction);
PROC Hook_HotPatch(PROC function, PROC newFunction); PROC Hook_HotPatch(PROC function, PROC newFunction);
void Hook_TryHotPatch(char *moduleName, char *functionName, PROC newFunction, PROC *function); void Hook_Create(char *moduleName, char *functionName, PROC newFunction, PROC *function);
#endif #endif

View File

@ -124,7 +124,7 @@ typedef struct IDirectDrawImpl
BOOL altenter; BOOL altenter;
BOOL hidecursor; BOOL hidecursor;
BOOL accurateTimers; BOOL accurateTimers;
BOOL hotPatch; BOOL hook;
SpeedLimiter ticksLimiter; SpeedLimiter ticksLimiter;
SpeedLimiter flipLimiter; SpeedLimiter flipLimiter;
SpeedLimiter fpsLimiter; SpeedLimiter fpsLimiter;

View File

@ -133,17 +133,22 @@ PROC Hook_HotPatch(PROC function, PROC newFunction)
return result; return result;
} }
void Hook_TryHotPatch(char *moduleName, char *functionName, PROC newFunction, PROC *function) void Hook_Create(char *moduleName, char *functionName, PROC newFunction, PROC *function)
{ {
FARPROC org = GetProcAddress(GetModuleHandle(moduleName), functionName); if (!ddraw->hook)
if (ddraw->hotPatch && org) return;
{
*function = Hook_HotPatch(org, newFunction);
if (*function == org) // hotpatch failed... if (ddraw->hook >= 2)
{
FARPROC org = GetProcAddress(GetModuleHandle(moduleName), functionName);
if (org)
*function = Hook_HotPatch(org, newFunction);
if ((!org || *function == org) && ddraw->hook == 3) // hotpatch failed...
Hook_PatchIAT(GetModuleHandle(NULL), moduleName, functionName, newFunction); Hook_PatchIAT(GetModuleHandle(NULL), moduleName, functionName, newFunction);
} }
else else
{ {
Hook_PatchIAT(GetModuleHandle(NULL), moduleName, functionName, newFunction); Hook_PatchIAT(GetModuleHandle(NULL), moduleName, functionName, newFunction);
} }
@ -155,23 +160,23 @@ void Hook_Init()
{ {
Hook_Active = TRUE; Hook_Active = TRUE;
Hook_TryHotPatch("user32.dll", "GetCursorPos", (PROC)fake_GetCursorPos, (PROC *)&real_GetCursorPos); Hook_Create("user32.dll", "GetCursorPos", (PROC)fake_GetCursorPos, (PROC *)&real_GetCursorPos);
Hook_TryHotPatch("user32.dll", "ClipCursor", (PROC)fake_ClipCursor, (PROC *)&real_ClipCursor); Hook_Create("user32.dll", "ClipCursor", (PROC)fake_ClipCursor, (PROC *)&real_ClipCursor);
Hook_TryHotPatch("user32.dll", "ShowCursor", (PROC)fake_ShowCursor, (PROC *)&real_ShowCursor); Hook_Create("user32.dll", "ShowCursor", (PROC)fake_ShowCursor, (PROC *)&real_ShowCursor);
Hook_TryHotPatch("user32.dll", "SetCursor", (PROC)fake_SetCursor, (PROC *)&real_SetCursor); Hook_Create("user32.dll", "SetCursor", (PROC)fake_SetCursor, (PROC *)&real_SetCursor);
Hook_TryHotPatch("user32.dll", "GetWindowRect", (PROC)fake_GetWindowRect, (PROC *)&real_GetWindowRect); Hook_Create("user32.dll", "GetWindowRect", (PROC)fake_GetWindowRect, (PROC *)&real_GetWindowRect);
Hook_TryHotPatch("user32.dll", "GetClientRect", (PROC)fake_GetClientRect, (PROC *)&real_GetClientRect); Hook_Create("user32.dll", "GetClientRect", (PROC)fake_GetClientRect, (PROC *)&real_GetClientRect);
Hook_TryHotPatch("user32.dll", "ClientToScreen", (PROC)fake_ClientToScreen, (PROC *)&real_ClientToScreen); Hook_Create("user32.dll", "ClientToScreen", (PROC)fake_ClientToScreen, (PROC *)&real_ClientToScreen);
Hook_TryHotPatch("user32.dll", "ScreenToClient", (PROC)fake_ScreenToClient, (PROC *)&real_ScreenToClient); Hook_Create("user32.dll", "ScreenToClient", (PROC)fake_ScreenToClient, (PROC *)&real_ScreenToClient);
Hook_TryHotPatch("user32.dll", "SetCursorPos", (PROC)fake_SetCursorPos, (PROC *)&real_SetCursorPos); Hook_Create("user32.dll", "SetCursorPos", (PROC)fake_SetCursorPos, (PROC *)&real_SetCursorPos);
Hook_TryHotPatch("user32.dll", "GetClipCursor", (PROC)fake_GetClipCursor, (PROC *)&real_GetClipCursor); Hook_Create("user32.dll", "GetClipCursor", (PROC)fake_GetClipCursor, (PROC *)&real_GetClipCursor);
Hook_TryHotPatch("user32.dll", "WindowFromPoint", (PROC)fake_WindowFromPoint, (PROC *)&real_WindowFromPoint); Hook_Create("user32.dll", "WindowFromPoint", (PROC)fake_WindowFromPoint, (PROC *)&real_WindowFromPoint);
Hook_TryHotPatch("user32.dll", "GetCursorInfo", (PROC)fake_GetCursorInfo, (PROC *)&real_GetCursorInfo); Hook_Create("user32.dll", "GetCursorInfo", (PROC)fake_GetCursorInfo, (PROC *)&real_GetCursorInfo);
Hook_TryHotPatch("user32.dll", "GetSystemMetrics", (PROC)fake_GetSystemMetrics, (PROC *)&real_GetSystemMetrics); Hook_Create("user32.dll", "GetSystemMetrics", (PROC)fake_GetSystemMetrics, (PROC *)&real_GetSystemMetrics);
Hook_TryHotPatch("user32.dll", "SetWindowPos", (PROC)fake_SetWindowPos, (PROC *)&real_SetWindowPos); Hook_Create("user32.dll", "SetWindowPos", (PROC)fake_SetWindowPos, (PROC *)&real_SetWindowPos);
Hook_TryHotPatch("user32.dll", "MoveWindow", (PROC)fake_MoveWindow, (PROC *)&real_MoveWindow); Hook_Create("user32.dll", "MoveWindow", (PROC)fake_MoveWindow, (PROC *)&real_MoveWindow);
Hook_TryHotPatch("user32.dll", "SendMessageA", (PROC)fake_SendMessageA, (PROC *)&real_SendMessageA); Hook_Create("user32.dll", "SendMessageA", (PROC)fake_SendMessageA, (PROC *)&real_SendMessageA);
Hook_TryHotPatch("user32.dll", "SetWindowLongA", (PROC)fake_SetWindowLongA, (PROC *)&real_SetWindowLongA); Hook_Create("user32.dll", "SetWindowLongA", (PROC)fake_SetWindowLongA, (PROC *)&real_SetWindowLongA);
//Hook_PatchIAT(GetModuleHandle(NULL), "user32.dll", "GetCursorPos", (PROC)fake_GetCursorPos); //Hook_PatchIAT(GetModuleHandle(NULL), "user32.dll", "GetCursorPos", (PROC)fake_GetCursorPos);
} }

View File

@ -41,13 +41,13 @@ void Settings_Load()
ddraw->noactivateapp = GetBool("noactivateapp", FALSE); ddraw->noactivateapp = GetBool("noactivateapp", FALSE);
ddraw->vhack = GetBool("vhack", FALSE); ddraw->vhack = GetBool("vhack", FALSE);
ddraw->accurateTimers = GetBool("accuratetimers", FALSE); ddraw->accurateTimers = GetBool("accuratetimers", FALSE);
ddraw->hotPatch = GetBool("hotpatch", FALSE);
WindowRect.right = GetInt("width", 0); WindowRect.right = GetInt("width", 0);
WindowRect.bottom = GetInt("height", 0); WindowRect.bottom = GetInt("height", 0);
WindowRect.left = GetInt("posX", -32000); WindowRect.left = GetInt("posX", -32000);
WindowRect.top = GetInt("posY", -32000); WindowRect.top = GetInt("posY", -32000);
ddraw->hook = GetInt("hook", 1);
ddraw->render.maxfps = GetInt("maxfps", 125); ddraw->render.maxfps = GetInt("maxfps", 125);
if (ddraw->accurateTimers || ddraw->vsync) if (ddraw->accurateTimers || ddraw->vsync)
@ -275,9 +275,9 @@ static void CreateSettingsIni()
"; Force CPU0 affinity, avoids crashes/freezing, *might* have a performance impact\n" "; Force CPU0 affinity, avoids crashes/freezing, *might* have a performance impact\n"
"singlecpu=true\n" "singlecpu=true\n"
"\n" "\n"
"; Use hotpatching rather than IAT hooking\n" "; Windows API Hooking, Possible values: 0 = disabled, 1 = IAT Hooking, 2 = HotPatch, 3 = Try HotPatch / fallback = IAT Hooking\n"
"; Note: Can be used to fix issues related to new features added by cnc-ddraw such as windowed mode or stretching\n" "; Note: Can be used to fix issues related to new features added by cnc-ddraw such as windowed mode or stretching\n"
"hotpatch=false\n" "hook=1\n"
"\n" "\n"
"\n" "\n"
"\n" "\n"