Repository navigation
Expand file tree
/
Copy pathhook.cpp
More file actions
159 lines (143 loc) · 4.95 KB
/
Copy pathhook.cpp
File metadata and controls
159 lines (143 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "hook.h"
#include "log_shader_count.h"
#include "patch_memory.h"
#include "load_shader_hook.h"
#include "check_key.h"
#include "print_stacktrace.h"
HMODULE g_whgame_base = NULL;
BOOL g_logging_enabled = FALSE;
static HANDLE g_timer_thread = NULL;
static BOOL g_running = TRUE;
static BOOL g_patch_applied = FALSE;
static char g_log_path[MAX_PATH];
void LogMessage(const char* fmt, ...) {
if (!g_logging_enabled) {
return;
}
FILE* file = fopen(g_log_path, "a");
if (file) {
SYSTEMTIME st;
GetLocalTime(&st);
// Print timestamp first
fprintf(file, "[%04d-%02d-%02d %02d:%02d:%02d] ",
st.wYear, st.wMonth, st.wDay,
st.wHour, st.wMinute, st.wSecond);
// Handle variable arguments
va_list args;
va_start(args, fmt);
vfprintf(file, fmt, args); // safely print formatted string
va_end(args);
fprintf(file, "\n"); // newline at the end
fclose(file);
}
}
HMODULE GetWHGameBase() {
HMODULE modules[1024];
DWORD needed;
char module_name[MAX_PATH];
if (EnumProcessModules(GetCurrentProcess(), modules, sizeof(modules), &needed)) {
for (int i = 0; i < (needed / sizeof(HMODULE)); i++) {
if (GetModuleBaseNameA(GetCurrentProcess(), modules[i], module_name, sizeof(module_name))) {
if (_stricmp(module_name, "WHGame.dll") == 0) {
return modules[i];
}
}
}
}
return NULL;
}
// Initialize logging
BOOL InitLogging() {
char user_folder[MAX_PATH];
// Try SHGetFolderPathA first
if (SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, user_folder) != S_OK) {
// Fallback to environment variable
DWORD result = GetEnvironmentVariableA("USERPROFILE", user_folder, MAX_PATH);
if (result == 0 || result >= MAX_PATH) {
// Final fallback - try HOMEDRIVE + HOMEPATH
char homeDrive[MAX_PATH] = {0};
char homePath[MAX_PATH] = {0};
GetEnvironmentVariableA("HOMEDRIVE", homeDrive, MAX_PATH);
GetEnvironmentVariableA("HOMEPATH", homePath, MAX_PATH);
snprintf(user_folder, MAX_PATH, "%s%s", homeDrive, homePath);
}
}
snprintf(g_log_path, sizeof(g_log_path), "%s\\KCD_patcher.txt", user_folder);
FILE* file = fopen(g_log_path, "w+");
if (file) {
SYSTEMTIME st;
GetLocalTime(&st);
fprintf(file, "\n=== KCD2 mac fix Started [%04d-%02d-%02d %02d:%02d:%02d] ===\n",
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
fclose(file);
snprintf(user_folder, sizeof(user_folder), "%s\\ghidra_symbols.csv", user_folder);
if (!InitializeSymbolsWithCSV(user_folder)) {
LogMessage("Warning: Failed to load csv file: %s", user_folder);
}
LogMessage("Csv file loaded at: %s", user_folder);
return TRUE;
}
return FALSE;
}
// Timer thread function
DWORD WINAPI TimerThread(LPVOID param) {
while (g_running) {
if (!g_whgame_base) {
LogMessage("Finding WHGame.dll");
g_whgame_base = GetWHGameBase();
if (!g_whgame_base) {
LogMessage("Could not found WHGame.dll");
Sleep(1000);
continue;
}
else {
LogMessage("WHGame.dll found at 0x%p", g_whgame_base);
}
}
if (!g_patch_applied && ApplyMemoryPatch()) {
g_patch_applied = TRUE;
InitializeShaderCacheLog();
LogMessage("*** PATCH SUCCESSFULLY APPLIED! ***");
if (InstallShaderLoadHook()) {
LogMessage("Allocator hook installed successfully");
} else {
LogMessage("Failed to install allocator hook");
}
}
// LogGlobalVar();
Sleep(1000); // 1 second
}
return 0;
}
// DLL entry point
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hModule);
char value[32];
GetEnvironmentVariableA("KCD_PATCH_LOG", value, sizeof(value));
if (strcmp(value, "1") == 0) {
g_logging_enabled = TRUE;
if (InitLogging())
g_timer_thread = CreateThread(NULL, 0, TimerThread, NULL, 0, NULL);
}
else {
g_timer_thread = CreateThread(NULL, 0, TimerThread, NULL, 0, NULL);
char msg [] = "KCD2 mac fix has been applied successfully \nPlease report this issues in github.com/AlvinHV/KCD2_mac_fix";
MessageBoxA(NULL, msg, "KCD2 patch", MB_OK | MB_ICONINFORMATION);
}
break;
case DLL_PROCESS_DETACH:
g_running = FALSE;
RemoveShaderLoadHook();
CleanupF9Hotkey();
if (g_timer_thread) {
WaitForSingleObject(g_timer_thread, 1050);
CloseHandle(g_timer_thread);
}
// Log exit
LogMessage("KCD2 mac fix Stopped");
break;
}
return TRUE;
}