1
0
mirror of https://github.com/FunkyFr3sh/cnc-ddraw.git synced 2025-03-20 16:09:12 +01:00
cnc-ddraw/src/hook.c

637 lines
25 KiB
C
Raw Normal View History

2020-10-13 09:20:52 +02:00
#define WIN32_LEAN_AND_MEAN
2019-03-19 06:57:49 +01:00
#include <windows.h>
#include <stdio.h>
2021-05-10 01:00:55 +02:00
#include <psapi.h>
2021-05-11 21:45:38 +02:00
#include "directinput.h"
2020-10-13 09:20:52 +02:00
#include "dd.h"
#include "winapi_hooks.h"
2019-03-19 06:57:49 +01:00
#include "hook.h"
2021-01-23 14:55:22 +01:00
#include "debug.h"
2020-10-13 09:20:52 +02:00
#include "dllmain.h"
2019-03-19 06:57:49 +01:00
2019-08-07 12:45:40 +02:00
#ifdef _MSC_VER
#include "detours.h"
#endif
2020-10-13 09:20:52 +02:00
BOOL g_hook_active;
2021-08-11 15:45:01 +02:00
BOOL g_hook_dinput;
2020-10-13 09:20:52 +02:00
int g_hook_method = 1;
2019-03-19 06:57:49 +01:00
GETCURSORPOSPROC real_GetCursorPos = GetCursorPos;
CLIPCURSORPROC real_ClipCursor = ClipCursor;
SHOWCURSORPROC real_ShowCursor = ShowCursor;
SETCURSORPROC real_SetCursor = SetCursor;
GETWINDOWRECTPROC real_GetWindowRect = GetWindowRect;
GETCLIENTRECTPROC real_GetClientRect = GetClientRect;
CLIENTTOSCREENPROC real_ClientToScreen = ClientToScreen;
SCREENTOCLIENTPROC real_ScreenToClient = ScreenToClient;
SETCURSORPOSPROC real_SetCursorPos = SetCursorPos;
WINDOWFROMPOINTPROC real_WindowFromPoint = WindowFromPoint;
GETCLIPCURSORPROC real_GetClipCursor = GetClipCursor;
GETCURSORINFOPROC real_GetCursorInfo = GetCursorInfo;
GETSYSTEMMETRICSPROC real_GetSystemMetrics = GetSystemMetrics;
SETWINDOWPOSPROC real_SetWindowPos = SetWindowPos;
MOVEWINDOWPROC real_MoveWindow = MoveWindow;
SENDMESSAGEAPROC real_SendMessageA = SendMessageA;
SETWINDOWLONGAPROC real_SetWindowLongA = SetWindowLongA;
GETWINDOWLONGAPROC real_GetWindowLongA = GetWindowLongA;
2019-08-06 04:37:06 +02:00
ENABLEWINDOWPROC real_EnableWindow = EnableWindow;
CREATEWINDOWEXAPROC real_CreateWindowExA = CreateWindowExA;
DESTROYWINDOWPROC real_DestroyWindow = DestroyWindow;
2021-05-29 21:40:21 +02:00
MAPWINDOWPOINTSPROC real_MapWindowPoints = MapWindowPoints;
2021-08-04 15:24:16 +02:00
SHOWWINDOWPROC real_ShowWindow = ShowWindow;
2022-10-02 18:41:06 +02:00
STRETCHBLTPROC real_StretchBlt = StretchBlt;
2022-10-03 12:19:15 +02:00
SETDIBITSTODEVICEPROC real_SetDIBitsToDevice = SetDIBitsToDevice;
2022-10-05 22:33:53 +02:00
STRETCHDIBITSPROC real_StretchDIBits = StretchDIBits;
2021-06-06 19:03:38 +02:00
SETWINDOWSHOOKEXAPROC real_SetWindowsHookExA = SetWindowsHookExA;
GETDEVICECAPSPROC real_GetDeviceCaps = GetDeviceCaps;
LOADLIBRARYAPROC real_LoadLibraryA = LoadLibraryA;
LOADLIBRARYWPROC real_LoadLibraryW = LoadLibraryW;
LOADLIBRARYEXAPROC real_LoadLibraryExA = LoadLibraryExA;
2020-09-19 11:23:06 +02:00
LOADLIBRARYEXWPROC real_LoadLibraryExW = LoadLibraryExW;
COCREATEINSTANCEPROC real_CoCreateInstance = CoCreateInstance;
2019-08-06 04:37:06 +02:00
2021-06-11 20:30:43 +02:00
static HOOKLIST g_hooks[] =
2020-10-19 16:37:12 +02:00
{
{
"user32.dll",
{
2021-08-06 02:07:50 +02:00
{ "GetCursorPos", (PROC)fake_GetCursorPos, (PROC*)&real_GetCursorPos, 0 },
{ "ClipCursor", (PROC)fake_ClipCursor, (PROC*)&real_ClipCursor, 0 },
{ "ShowCursor", (PROC)fake_ShowCursor, (PROC*)&real_ShowCursor, 0 },
{ "SetCursor", (PROC)fake_SetCursor, (PROC*)&real_SetCursor, 0 },
{ "GetWindowRect", (PROC)fake_GetWindowRect, (PROC*)&real_GetWindowRect, SKIP_HOOK3 },
{ "GetClientRect", (PROC)fake_GetClientRect, (PROC*)&real_GetClientRect, SKIP_HOOK3 },
{ "ClientToScreen", (PROC)fake_ClientToScreen, (PROC*)&real_ClientToScreen, 0 },
{ "ScreenToClient", (PROC)fake_ScreenToClient, (PROC*)&real_ScreenToClient, 0 },
{ "SetCursorPos", (PROC)fake_SetCursorPos, (PROC*)&real_SetCursorPos, 0 },
{ "GetClipCursor", (PROC)fake_GetClipCursor, (PROC*)&real_GetClipCursor, 0 },
{ "WindowFromPoint", (PROC)fake_WindowFromPoint, (PROC*)&real_WindowFromPoint, 0 },
{ "GetCursorInfo", (PROC)fake_GetCursorInfo, (PROC*)&real_GetCursorInfo, 0 },
{ "GetSystemMetrics", (PROC)fake_GetSystemMetrics, (PROC*)&real_GetSystemMetrics, 0 },
{ "SetWindowPos", (PROC)fake_SetWindowPos, (PROC*)&real_SetWindowPos, 0 },
{ "MoveWindow", (PROC)fake_MoveWindow, (PROC*)&real_MoveWindow, 0 },
{ "SendMessageA", (PROC)fake_SendMessageA, (PROC*)&real_SendMessageA, 0 },
{ "SetWindowLongA", (PROC)fake_SetWindowLongA, (PROC*)&real_SetWindowLongA, 0 },
{ "GetWindowLongA", (PROC)fake_GetWindowLongA, (PROC*)&real_GetWindowLongA, 0 },
{ "EnableWindow", (PROC)fake_EnableWindow, (PROC*)&real_EnableWindow, 0 },
{ "CreateWindowExA", (PROC)fake_CreateWindowExA, (PROC*)&real_CreateWindowExA, 0 },
{ "DestroyWindow", (PROC)fake_DestroyWindow, (PROC*)&real_DestroyWindow, 0 },
{ "MapWindowPoints", (PROC)fake_MapWindowPoints, (PROC*)&real_MapWindowPoints, 0 },
{ "ShowWindow", (PROC)fake_ShowWindow, (PROC*)&real_ShowWindow, 0 },
{ "", NULL, NULL, 0 }
2020-10-19 16:37:12 +02:00
}
},
{
"gdi32.dll",
{
2022-10-02 18:41:06 +02:00
{ "StretchBlt", (PROC)fake_StretchBlt, (PROC*)&real_StretchBlt, SKIP_HOOK2 | SKIP_HOOK3 },
2022-10-03 12:19:15 +02:00
{ "SetDIBitsToDevice", (PROC)fake_SetDIBitsToDevice, (PROC*)&real_SetDIBitsToDevice, SKIP_HOOK2 | SKIP_HOOK3 },
2022-10-05 22:33:53 +02:00
{ "StretchDIBits", (PROC)fake_StretchDIBits, (PROC*)&real_StretchDIBits, SKIP_HOOK2 | SKIP_HOOK3 },
2021-08-06 02:07:50 +02:00
{ "GetDeviceCaps", (PROC)fake_GetDeviceCaps, (PROC*)&real_GetDeviceCaps, SKIP_HOOK3 },
{ "", NULL, NULL, 0 }
2020-10-19 16:37:12 +02:00
}
},
{
"kernel32.dll",
{
2021-08-06 02:07:50 +02:00
{ "LoadLibraryA", (PROC)fake_LoadLibraryA, (PROC*)&real_LoadLibraryA, SKIP_HOOK2 | SKIP_HOOK3 },
{ "LoadLibraryW", (PROC)fake_LoadLibraryW, (PROC*)&real_LoadLibraryW, SKIP_HOOK2 | SKIP_HOOK3 },
{ "LoadLibraryExA", (PROC)fake_LoadLibraryExA, (PROC*)&real_LoadLibraryExA, SKIP_HOOK2 | SKIP_HOOK3 },
{ "LoadLibraryExW", (PROC)fake_LoadLibraryExW, (PROC*)&real_LoadLibraryExW, SKIP_HOOK2 | SKIP_HOOK3 },
{ "", NULL, NULL, 0 }
2020-10-19 16:37:12 +02:00
}
},
{
"",
{
2021-08-06 02:07:50 +02:00
{ "", NULL, NULL, 0 }
2020-10-19 16:37:12 +02:00
}
}
};
2019-03-19 06:57:49 +01:00
2020-10-20 05:59:41 +02:00
void hook_patch_iat(HMODULE hmod, BOOL unhook, char* module_name, char* function_name, PROC new_function)
2019-03-19 06:57:49 +01:00
{
2021-06-11 20:30:43 +02:00
HOOKLIST hooks[2];
2020-10-20 05:59:41 +02:00
memset(&hooks, 0, sizeof(hooks));
hooks[0].data[0].new_function = new_function;
2021-06-11 20:30:43 +02:00
strncpy(hooks[0].module_name, module_name, sizeof(hooks[0].module_name) - 1);
2020-10-20 05:59:41 +02:00
strncpy(hooks[0].data[0].function_name, function_name, sizeof(hooks[0].data[0].function_name) - 1);
2021-06-11 20:30:43 +02:00
hook_patch_iat_list(hmod, unhook, (HOOKLIST*)&hooks);
2020-10-20 05:59:41 +02:00
}
2021-09-04 06:24:43 +02:00
void hook_patch_obfuscated_iat_list(HMODULE hmod, BOOL unhook, HOOKLIST* hooks)
{
if (!hmod || hmod == INVALID_HANDLE_VALUE || !hooks)
return;
#ifdef _MSC_VER
__try
{
#endif
PIMAGE_DOS_HEADER dos_header = (PIMAGE_DOS_HEADER)hmod;
if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
return;
PIMAGE_NT_HEADERS nt_headers = (PIMAGE_NT_HEADERS)((DWORD)dos_header + (DWORD)dos_header->e_lfanew);
if (nt_headers->Signature != IMAGE_NT_SIGNATURE)
return;
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;
while (import_desc->FirstThunk)
{
for (int i = 0; hooks[i].module_name[0]; i++)
{
char* imp_module_name = (char*)((DWORD)dos_header + (DWORD)(import_desc->Name));
if (_stricmp(imp_module_name, hooks[i].module_name) == 0)
{
2021-09-06 01:13:17 +02:00
HMODULE cur_mod = GetModuleHandle(hooks[i].module_name);
2021-09-04 06:24:43 +02:00
PIMAGE_THUNK_DATA first_thunk =
(PIMAGE_THUNK_DATA)((DWORD)dos_header + (DWORD)import_desc->FirstThunk);
PIMAGE_THUNK_DATA original_first_thunk =
(PIMAGE_THUNK_DATA)((DWORD)dos_header + (DWORD)import_desc->OriginalFirstThunk);
while (first_thunk->u1.Function)
{
for (int x = 0; hooks[i].data[x].function_name[0]; x++)
{
DWORD org_function =
(DWORD)GetProcAddress(
2021-09-06 01:13:17 +02:00
cur_mod,
2021-09-04 06:24:43 +02:00
hooks[i].data[x].function_name);
2021-09-04 19:34:31 +02:00
if (!hooks[i].data[x].new_function || !org_function)
2021-09-04 06:24:43 +02:00
continue;
if (unhook)
{
if (first_thunk->u1.Function == (DWORD)hooks[i].data[x].new_function)
{
DWORD op;
if (VirtualProtect(
&first_thunk->u1.Function,
sizeof(DWORD),
PAGE_READWRITE,
&op))
{
first_thunk->u1.Function = org_function;
VirtualProtect(&first_thunk->u1.Function, sizeof(DWORD), op, &op);
}
break;
}
}
else
{
if (first_thunk->u1.Function == org_function)
{
DWORD op;
if (VirtualProtect(
&first_thunk->u1.Function,
sizeof(DWORD),
PAGE_READWRITE,
&op))
{
first_thunk->u1.Function = (DWORD)hooks[i].data[x].new_function;
VirtualProtect(&first_thunk->u1.Function, sizeof(DWORD), op, &op);
}
break;
}
}
}
first_thunk++;
original_first_thunk++;
}
}
}
import_desc++;
}
#ifdef _MSC_VER
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
}
#endif
}
2021-06-11 20:30:43 +02:00
void hook_patch_iat_list(HMODULE hmod, BOOL unhook, HOOKLIST* hooks)
2020-10-20 05:59:41 +02:00
{
2021-09-04 06:24:43 +02:00
hook_patch_obfuscated_iat_list(hmod, unhook, hooks);
2020-10-20 05:59:41 +02:00
if (!hmod || hmod == INVALID_HANDLE_VALUE || !hooks)
2019-03-19 06:57:49 +01:00
return;
2020-09-22 14:04:49 +02:00
#ifdef _MSC_VER
__try
{
#endif
2020-10-13 09:20:52 +02:00
PIMAGE_DOS_HEADER dos_header = (PIMAGE_DOS_HEADER)hmod;
if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
2020-09-22 14:04:49 +02:00
return;
2019-03-19 06:57:49 +01:00
2020-10-13 09:20:52 +02:00
PIMAGE_NT_HEADERS nt_headers = (PIMAGE_NT_HEADERS)((DWORD)dos_header + (DWORD)dos_header->e_lfanew);
if (nt_headers->Signature != IMAGE_NT_SIGNATURE)
2020-09-22 14:04:49 +02:00
return;
2019-03-19 06:57:49 +01:00
2020-10-13 09:20:52 +02:00
PIMAGE_IMPORT_DESCRIPTOR import_desc = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD)dos_header +
(DWORD)(nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress));
2019-03-19 06:57:49 +01:00
2020-10-13 09:20:52 +02:00
if (import_desc == (PIMAGE_IMPORT_DESCRIPTOR)nt_headers)
2020-09-22 14:04:49 +02:00
return;
2019-03-19 06:57:49 +01:00
2020-10-13 09:20:52 +02:00
while (import_desc->FirstThunk)
2019-03-19 06:57:49 +01:00
{
2020-10-20 05:59:41 +02:00
for (int i = 0; hooks[i].module_name[0]; i++)
2019-03-19 06:57:49 +01:00
{
2020-10-20 05:59:41 +02:00
char* imp_module_name = (char*)((DWORD)dos_header + (DWORD)(import_desc->Name));
2020-09-22 14:04:49 +02:00
2020-10-20 05:59:41 +02:00
if (_stricmp(imp_module_name, hooks[i].module_name) == 0)
2019-03-19 06:57:49 +01:00
{
2020-10-20 05:59:41 +02:00
PIMAGE_THUNK_DATA first_thunk =
(PIMAGE_THUNK_DATA)((DWORD)dos_header + (DWORD)import_desc->FirstThunk);
PIMAGE_THUNK_DATA original_first_thunk =
(PIMAGE_THUNK_DATA)((DWORD)dos_header + (DWORD)import_desc->OriginalFirstThunk);
2019-03-19 06:57:49 +01:00
2020-10-20 05:59:41 +02:00
while (first_thunk->u1.Function && original_first_thunk->u1.AddressOfData)
2019-03-19 06:57:49 +01:00
{
2020-10-20 05:59:41 +02:00
PIMAGE_IMPORT_BY_NAME import =
(PIMAGE_IMPORT_BY_NAME)((DWORD)dos_header + original_first_thunk->u1.AddressOfData);
2020-09-22 14:04:49 +02:00
2020-10-20 05:59:41 +02:00
if ((original_first_thunk->u1.Ordinal & IMAGE_ORDINAL_FLAG) == 0)
2020-09-22 14:04:49 +02:00
{
2020-10-20 05:59:41 +02:00
for (int x = 0; hooks[i].data[x].function_name[0]; x++)
{
if (!unhook && !hooks[i].data[x].new_function)
continue;
if (_stricmp((const char*)import->Name, hooks[i].data[x].function_name) == 0)
{
2021-09-04 06:24:43 +02:00
DWORD op;
2020-10-20 05:59:41 +02:00
if (VirtualProtect(
2021-09-04 06:24:43 +02:00
&first_thunk->u1.Function,
sizeof(DWORD),
PAGE_READWRITE,
&op))
2020-10-20 05:59:41 +02:00
{
if (unhook)
{
2021-06-11 20:30:43 +02:00
DWORD org =
2020-10-20 05:59:41 +02:00
(DWORD)GetProcAddress(
2021-06-11 20:30:43 +02:00
GetModuleHandle(hooks[i].module_name),
2020-10-20 05:59:41 +02:00
hooks[i].data[x].function_name);
2021-09-06 01:13:17 +02:00
if (org && first_thunk->u1.Function != org)
2020-10-20 05:59:41 +02:00
{
first_thunk->u1.Function = org;
}
}
else
{
2021-09-06 01:13:17 +02:00
if (first_thunk->u1.Function != (DWORD)hooks[i].data[x].new_function)
first_thunk->u1.Function = (DWORD)hooks[i].data[x].new_function;
2020-10-20 05:59:41 +02:00
}
2021-09-04 06:24:43 +02:00
VirtualProtect(&first_thunk->u1.Function, sizeof(DWORD), op, &op);
2020-10-20 05:59:41 +02:00
}
break;
}
}
2020-09-22 14:04:49 +02:00
}
2020-10-20 05:59:41 +02:00
first_thunk++;
original_first_thunk++;
2019-03-19 06:57:49 +01:00
}
}
}
2020-10-13 09:20:52 +02:00
import_desc++;
2020-09-22 14:04:49 +02:00
}
#ifdef _MSC_VER
}
2021-06-11 20:30:43 +02:00
__except (EXCEPTION_EXECUTE_HANDLER)
2020-09-22 14:04:49 +02:00
{
2019-03-19 06:57:49 +01:00
}
2020-09-22 14:04:49 +02:00
#endif
2019-03-19 06:57:49 +01:00
}
2021-08-06 02:07:50 +02:00
void hook_create(HOOKLIST* hooks, BOOL initial_hook)
2019-03-19 06:57:49 +01:00
{
2019-08-07 12:45:40 +02:00
#ifdef _MSC_VER
2021-08-06 02:07:50 +02:00
if ((g_hook_method == 2 || g_hook_method == 3) && initial_hook)
2019-03-20 05:07:28 +01:00
{
2020-10-19 16:37:12 +02:00
for (int i = 0; hooks[i].module_name[0]; i++)
{
for (int x = 0; hooks[i].data[x].function_name[0]; x++)
{
2021-08-06 02:07:50 +02:00
if (g_hook_method == 2 && (hooks[i].data[x].flags & SKIP_HOOK2))
continue;
if (g_hook_method == 3 && (hooks[i].data[x].flags & SKIP_HOOK3))
continue;
2020-10-19 16:37:12 +02:00
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)hooks[i].data[x].function, (PVOID)hooks[i].data[x].new_function);
DetourTransactionCommit();
}
}
2019-03-20 05:07:28 +01:00
}
2021-05-10 01:00:55 +02:00
#endif
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
if (g_hook_method == 3 || g_hook_method == 4)
2020-09-19 11:23:06 +02:00
{
2021-01-23 14:55:22 +01:00
char game_exe_path[MAX_PATH] = { 0 };
char game_dir[MAX_PATH] = { 0 };
2020-09-19 11:23:06 +02:00
2021-01-23 14:55:22 +01:00
if (GetModuleFileNameA(NULL, game_exe_path, MAX_PATH))
2020-09-19 11:23:06 +02:00
{
2021-01-23 14:55:22 +01:00
_splitpath(game_exe_path, NULL, game_dir, NULL, NULL);
2020-09-19 11:23:06 +02:00
2021-01-23 14:55:22 +01:00
char mod_path[MAX_PATH] = { 0 };
char mod_dir[MAX_PATH] = { 0 };
2021-01-30 16:45:27 +01:00
char mod_filename[MAX_PATH] = { 0 };
2020-10-13 09:20:52 +02:00
HMODULE hmod = NULL;
2021-05-10 01:00:55 +02:00
HANDLE process = NULL;
#ifndef _MSC_VER
HMODULE mods[512];
memset(mods, 0, sizeof(mods));
process = GetCurrentProcess();
EnumProcessModules(process, mods, sizeof(mods) - sizeof(mods[0]), NULL);
for (int i = 0; i < sizeof(mods) / sizeof(mods[0]) && (hmod = mods[i]); i++)
#else
2020-10-13 09:20:52 +02:00
while (hmod = DetourEnumerateModules(hmod))
2021-05-10 01:00:55 +02:00
#endif
2020-09-19 11:23:06 +02:00
{
2020-10-13 09:20:52 +02:00
if (hmod == g_ddraw_module)
2020-09-19 11:23:06 +02:00
continue;
2021-01-23 14:55:22 +01:00
if (GetModuleFileNameA(hmod, mod_path, MAX_PATH))
2020-09-19 11:23:06 +02:00
{
2021-06-11 20:30:43 +02:00
TRACE_EXT("Module %s = %p\n", mod_path, hmod);
2020-09-19 11:23:06 +02:00
2021-01-30 16:45:27 +01:00
_splitpath(mod_path, NULL, mod_dir, mod_filename, NULL);
/* Don't hook reshade/swiftshader/mesa3d */
2021-06-11 20:30:43 +02:00
if (_strcmpi(mod_filename, "opengl32") == 0 ||
_strcmpi(mod_filename, "d3d9") == 0 ||
2021-05-26 19:34:45 +02:00
_strcmpi(mod_filename, "Shw32") == 0)
2021-01-30 16:45:27 +01:00
continue;
2021-01-23 14:55:22 +01:00
2021-08-11 01:36:12 +02:00
if (_strnicmp(game_dir, mod_dir, strlen(game_dir)) == 0 ||
2022-10-02 18:41:06 +02:00
_strcmpi(mod_filename, "MSVFW32") == 0 ||
2021-08-11 01:36:12 +02:00
_strcmpi(mod_filename, "quartz") == 0 ||
_strcmpi(mod_filename, "winmm") == 0)
2020-09-19 11:23:06 +02:00
{
2020-10-20 05:59:41 +02:00
hook_patch_iat_list(hmod, FALSE, hooks);
2020-09-19 11:23:06 +02:00
}
}
}
2021-05-10 01:00:55 +02:00
if (process)
CloseHandle(process);
2020-09-19 11:23:06 +02:00
}
}
2019-03-20 05:07:28 +01:00
2020-10-13 09:20:52 +02:00
if (g_hook_method == 1)
2019-08-07 12:59:23 +02:00
{
2020-10-20 05:59:41 +02:00
hook_patch_iat_list(GetModuleHandle(NULL), FALSE, hooks);
2019-08-07 12:59:23 +02:00
}
2019-03-19 06:57:49 +01:00
}
2021-06-11 20:30:43 +02:00
void hook_revert(HOOKLIST* hooks)
2019-03-19 06:57:49 +01:00
{
2019-08-07 12:45:40 +02:00
#ifdef _MSC_VER
2021-08-06 02:07:50 +02:00
if (g_hook_method == 2 || g_hook_method == 3)
2019-03-19 06:57:49 +01:00
{
2020-10-19 16:37:12 +02:00
for (int i = 0; hooks[i].module_name[0]; i++)
{
for (int x = 0; hooks[i].data[x].function_name[0]; x++)
{
2021-08-06 02:07:50 +02:00
if (g_hook_method == 2 && (hooks[i].data[x].flags & SKIP_HOOK2))
continue;
if (g_hook_method == 3 && (hooks[i].data[x].flags & SKIP_HOOK3))
continue;
2020-10-19 16:37:12 +02:00
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach((PVOID*)hooks[i].data[x].function, (PVOID)hooks[i].data[x].new_function);
DetourTransactionCommit();
}
}
2019-03-19 09:43:47 +01:00
}
2021-05-10 01:00:55 +02:00
#endif
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
if (g_hook_method == 3 || g_hook_method == 4)
2020-09-19 11:23:06 +02:00
{
2021-01-23 14:55:22 +01:00
char game_exe_path[MAX_PATH] = { 0 };
char game_dir[MAX_PATH] = { 0 };
2020-09-19 11:23:06 +02:00
2021-01-23 14:55:22 +01:00
if (GetModuleFileNameA(NULL, game_exe_path, MAX_PATH))
2020-09-19 11:23:06 +02:00
{
2021-01-23 14:55:22 +01:00
_splitpath(game_exe_path, NULL, game_dir, NULL, NULL);
2020-09-19 11:23:06 +02:00
2021-01-23 14:55:22 +01:00
char mod_path[MAX_PATH] = { 0 };
char mod_dir[MAX_PATH] = { 0 };
2021-08-11 01:36:12 +02:00
char mod_filename[MAX_PATH] = { 0 };
2020-10-13 09:20:52 +02:00
HMODULE hmod = NULL;
2021-05-10 01:00:55 +02:00
HANDLE process = NULL;
#ifndef _MSC_VER
HMODULE mods[512];
memset(mods, 0, sizeof(mods));
process = GetCurrentProcess();
EnumProcessModules(process, mods, sizeof(mods) - sizeof(mods[0]), NULL);
for (int i = 0; i < sizeof(mods) / sizeof(mods[0]) && (hmod = mods[i]); i++)
#else
2020-10-13 09:20:52 +02:00
while (hmod = DetourEnumerateModules(hmod))
2021-05-10 01:00:55 +02:00
#endif
2020-09-19 11:23:06 +02:00
{
2020-10-13 09:20:52 +02:00
if (hmod == g_ddraw_module)
2020-09-19 11:23:06 +02:00
continue;
2021-01-23 14:55:22 +01:00
if (GetModuleFileNameA(hmod, mod_path, MAX_PATH))
2020-09-19 11:23:06 +02:00
{
2021-08-11 01:36:12 +02:00
_splitpath(mod_path, NULL, mod_dir, mod_filename, NULL);
2020-09-19 11:23:06 +02:00
2021-08-11 01:36:12 +02:00
if (_strnicmp(game_dir, mod_dir, strlen(game_dir)) == 0 ||
2022-10-02 18:41:06 +02:00
_strcmpi(mod_filename, "MSVFW32") == 0 ||
2021-08-11 01:36:12 +02:00
_strcmpi(mod_filename, "quartz") == 0 ||
_strcmpi(mod_filename, "winmm") == 0)
2020-09-19 11:23:06 +02:00
{
2020-10-20 05:59:41 +02:00
hook_patch_iat_list(hmod, TRUE, hooks);
2020-09-19 11:23:06 +02:00
}
}
}
2021-05-10 01:00:55 +02:00
if (process)
CloseHandle(process);
2020-09-19 11:23:06 +02:00
}
}
2019-08-06 04:37:06 +02:00
2020-10-13 09:20:52 +02:00
if (g_hook_method == 1)
2019-08-07 12:45:40 +02:00
{
2020-10-20 05:59:41 +02:00
hook_patch_iat_list(GetModuleHandle(NULL), TRUE, hooks);
2019-03-19 06:57:49 +01:00
}
}
2020-10-13 09:20:52 +02:00
void hook_init()
2019-03-19 06:57:49 +01:00
{
2020-10-13 09:20:52 +02:00
if (!g_hook_active || g_hook_method == 3 || g_hook_method == 4)
2019-03-19 06:57:49 +01:00
{
2021-08-06 02:07:50 +02:00
BOOL initial_hook = !g_hook_active;
2020-09-22 14:04:49 +02:00
#ifdef _MSC_VER
2021-08-11 15:45:01 +02:00
if (initial_hook && g_hook_dinput)
2020-09-19 11:23:06 +02:00
{
2021-06-11 20:30:43 +02:00
real_DirectInputCreateA =
(DIRECTINPUTCREATEAPROC)GetProcAddress(LoadLibraryA("dinput.dll"), "DirectInputCreateA");
2020-09-19 12:05:49 +02:00
2021-05-11 21:45:38 +02:00
if (real_DirectInputCreateA)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)&real_DirectInputCreateA, (PVOID)fake_DirectInputCreateA);
DetourTransactionCommit();
}
2021-06-11 20:30:43 +02:00
real_DirectInputCreateW =
(DIRECTINPUTCREATEWPROC)GetProcAddress(LoadLibraryA("dinput.dll"), "DirectInputCreateW");
2021-05-23 14:55:36 +02:00
if (real_DirectInputCreateW)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)&real_DirectInputCreateW, (PVOID)fake_DirectInputCreateW);
DetourTransactionCommit();
}
2021-06-11 20:30:43 +02:00
real_DirectInputCreateEx =
(DIRECTINPUTCREATEEXPROC)GetProcAddress(LoadLibraryA("dinput.dll"), "DirectInputCreateEx");
2021-05-23 14:55:36 +02:00
if (real_DirectInputCreateEx)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)&real_DirectInputCreateEx, (PVOID)fake_DirectInputCreateEx);
DetourTransactionCommit();
}
2021-06-11 20:30:43 +02:00
real_DirectInput8Create =
(DIRECTINPUT8CREATEPROC)GetProcAddress(LoadLibraryA("dinput8.dll"), "DirectInput8Create");
2020-09-19 12:05:49 +02:00
2021-05-11 21:45:38 +02:00
if (real_DirectInput8Create)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)&real_DirectInput8Create, (PVOID)fake_DirectInput8Create);
DetourTransactionCommit();
}
2020-09-19 11:23:06 +02:00
}
2020-09-22 14:04:49 +02:00
#endif
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
g_hook_active = TRUE;
2021-08-06 02:07:50 +02:00
hook_create((HOOKLIST*)&g_hooks, initial_hook);
2019-08-07 12:45:40 +02:00
}
}
2021-05-14 01:24:46 +02:00
void hook_early_init()
{
/*
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)&real_CoCreateInstance, (PVOID)fake_CoCreateInstance);
DetourTransactionCommit();
*/
hook_patch_iat(GetModuleHandle(NULL), FALSE, "ole32.dll", "CoCreateInstance", (PROC)fake_CoCreateInstance);
2021-05-14 01:24:46 +02:00
hook_patch_iat(GetModuleHandle(NULL), FALSE, "dinput.dll", "DirectInputCreateA", (PROC)fake_DirectInputCreateA);
2021-05-23 14:55:36 +02:00
hook_patch_iat(GetModuleHandle(NULL), FALSE, "dinput.dll", "DirectInputCreateW", (PROC)fake_DirectInputCreateW);
hook_patch_iat(GetModuleHandle(NULL), FALSE, "dinput.dll", "DirectInputCreateEx", (PROC)fake_DirectInputCreateEx);
2021-05-14 01:24:46 +02:00
hook_patch_iat(GetModuleHandle(NULL), FALSE, "dinput8.dll", "DirectInput8Create", (PROC)fake_DirectInput8Create);
hook_patch_iat(GetModuleHandle(NULL), FALSE, "user32.dll", "GetClientRect", (PROC)fake_GetClientRect); //anno 1602
2022-09-23 00:26:43 +02:00
hook_patch_iat(GetModuleHandle(NULL), FALSE, "user32.dll", "ClipCursor", (PROC)fake_ClipCursor); //NexusTK
2021-06-06 19:03:38 +02:00
hook_patch_iat(GetModuleHandle("AcGenral"), FALSE, "user32.dll", "SetWindowsHookExA", (PROC)fake_SetWindowsHookExA);
hook_patch_iat(GetModuleHandle(NULL), FALSE, "user32.dll", "SetWindowsHookExA", (PROC)fake_SetWindowsHookExA);
2021-05-14 01:24:46 +02:00
}
2020-10-13 09:20:52 +02:00
void hook_exit()
2019-08-07 12:45:40 +02:00
{
2020-10-13 09:20:52 +02:00
if (g_hook_active)
2019-08-07 12:45:40 +02:00
{
2020-10-13 09:20:52 +02:00
g_hook_active = FALSE;
2019-08-07 12:45:40 +02:00
2020-09-22 14:04:49 +02:00
#ifdef _MSC_VER
2021-08-11 15:45:01 +02:00
if (g_hook_dinput)
2020-09-19 11:23:06 +02:00
{
2021-05-11 21:45:38 +02:00
if (real_DirectInputCreateA)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach((PVOID*)&real_DirectInputCreateA, (PVOID)fake_DirectInputCreateA);
DetourTransactionCommit();
}
2021-05-23 14:55:36 +02:00
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();
}
2021-05-11 21:45:38 +02:00
if (real_DirectInput8Create)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach((PVOID*)&real_DirectInput8Create, (PVOID)fake_DirectInput8Create);
DetourTransactionCommit();
}
2020-09-19 11:23:06 +02:00
}
2020-09-22 14:04:49 +02:00
#endif
2020-09-19 11:23:06 +02:00
2021-06-11 20:30:43 +02:00
hook_revert((HOOKLIST*)&g_hooks);
2019-03-19 06:57:49 +01:00
}
2021-05-14 01:24:46 +02:00
hook_patch_iat(GetModuleHandle(NULL), TRUE, "ole32.dll", "CoCreateInstance", (PROC)fake_CoCreateInstance);
2021-05-14 01:24:46 +02:00
hook_patch_iat(GetModuleHandle(NULL), TRUE, "dinput.dll", "DirectInputCreateA", (PROC)fake_DirectInputCreateA);
2021-05-23 14:55:36 +02:00
hook_patch_iat(GetModuleHandle(NULL), TRUE, "dinput.dll", "DirectInputCreateW", (PROC)fake_DirectInputCreateW);
hook_patch_iat(GetModuleHandle(NULL), TRUE, "dinput.dll", "DirectInputCreateEx", (PROC)fake_DirectInputCreateEx);
2021-05-14 01:24:46 +02:00
hook_patch_iat(GetModuleHandle(NULL), TRUE, "dinput8.dll", "DirectInput8Create", (PROC)fake_DirectInput8Create);
hook_patch_iat(GetModuleHandle(NULL), TRUE, "user32.dll", "GetClientRect", (PROC)fake_GetClientRect); //anno 1602
2022-09-23 00:26:43 +02:00
hook_patch_iat(GetModuleHandle(NULL), TRUE, "user32.dll", "ClipCursor", (PROC)fake_ClipCursor); //NexusTK
2021-06-06 19:03:38 +02:00
hook_patch_iat(GetModuleHandle("AcGenral"), TRUE, "user32.dll", "SetWindowsHookExA", (PROC)fake_SetWindowsHookExA);
hook_patch_iat(GetModuleHandle(NULL), TRUE, "user32.dll", "SetWindowsHookExA", (PROC)fake_SetWindowsHookExA);
2019-03-19 06:57:49 +01:00
}