-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_shader_hook.cpp
More file actions
149 lines (125 loc) · 4.64 KB
/
load_shader_hook.cpp
File metadata and controls
149 lines (125 loc) · 4.64 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
#include "minhook/include/MinHook.h"
#include "WHGame.dll.h"
#include "hook.h"
#include "log_shader_count.h"
#include "patch_memory.h"
#include "minwindef.h"
#include "winnt.h"
#include "check_key.h"
#include <cstdint>
#include <cstring>
static BOOL g_detailed_logging = FALSE;
static CRITICAL_SECTION g_terrain_critical_section;
static BOOL g_critical_section_initialized = FALSE;
typedef void* (*mfCreateShaderResources_t)(void* self, SInputShaderResources* Res, bool bShare);
// Original function pointers
static mfCreateShaderResources_t g_original_mfCreateShaderResources = NULL;
void* Hook_mfCreateShaderResources(void* self, SInputShaderResources* Res, bool bShare) {
if (g_detailed_logging) {
LogMessage("mfCreateShaderResources called:");
LogMessage(" self: %p, Res: %p, bShare: %c",
self, Res, bShare ? '1' : '0');
LogMessage(" m_shaderParms.size(): %i", *(int *)((long long)Res->m_ShaderParams + -4));
// if (IsDebuggerPresent()) {
// LogMessage("Attached debugger Detected. Debugger will stop");
// DebugBreak();
// }
}
void* result = g_original_mfCreateShaderResources(self, Res, bShare);
if (g_detailed_logging) {
}
LogGlobalVar();
return result;
}
void trigger_shaderlog(bool _i){
g_detailed_logging = !g_detailed_logging;
}
// Install all hooks
BOOL InstallShaderLoadHook() {
if (!g_whgame_base) {
LogMessage("ERROR: g_whgame_base is NULL!");
return FALSE;
}
// Initialize critical section
if (!g_critical_section_initialized) {
InitializeCriticalSection(&g_terrain_critical_section);
g_critical_section_initialized = TRUE;
}
// Initialize MinHook
static BOOL mh_initialized = FALSE;
if (!mh_initialized) {
MH_STATUS status = MH_Initialize();
if (status != MH_OK) {
LogMessage("ERROR: MH_Initialize failed with status: %d", status);
return FALSE;
}
mh_initialized = TRUE;
}
BOOL success = TRUE;
// Hook 2: mfCreateShaderResources
unsigned char pattern[] = {
0x48, 0x89, 0x5C, 0x24, 0x08,
0x48, 0x89, 0x6C, 0x24, 0x18,
0x48, 0x89, 0x74, 0x24, 0x20,
0x57, 0x41, 0x54, 0x41, 0x55,
0x41, 0x56, 0x41, 0x57,
0x48, 0x81, 0xEC, 0x90, 0x08, 0x00, 0x00
};
void* pattern_addr = FindPattern(pattern, sizeof(pattern));
if (!pattern_addr) {
LogMessage("Pattern not found");
char error_msg[512];
snprintf(error_msg, sizeof(error_msg),
"Failed to find mfCreateShaderResources pattern!\n\n"
"WHGame.dll base: 0x%p\n\n"
"The game engine may have been updated.\n"
"Please report this issue in github.com/AlvinHV/KCD2_mac_fix",
g_whgame_base);
MessageBoxA(NULL, error_msg, "Pattern Search Failed", MB_OK | MB_ICONERROR);
return false;
}
void* target_mfCreateShaderResources = (void*)pattern_addr;
if (target_mfCreateShaderResources) {
if (MH_CreateHook(target_mfCreateShaderResources, (LPVOID)&Hook_mfCreateShaderResources, (LPVOID*)&g_original_mfCreateShaderResources) != MH_OK) {
LogMessage("Failed to create hook for mfCreateShaderResources");
} else {
LogMessage("Created hook for mfCreateShaderResources at 0x%p", target_mfCreateShaderResources);
}
}
success = FALSE;
MH_STATUS status = MH_EnableHook(MH_ALL_HOOKS);
switch (status) {
case MH_OK:
LogMessage("Hook created successfully");
success = TRUE;
break;
case MH_ERROR_ALREADY_CREATED:
LogMessage("ERROR: Hook already exists for this target");
return FALSE;
case MH_ERROR_NOT_EXECUTABLE:
LogMessage("ERROR: Target is not executable");
return FALSE;
case MH_ERROR_UNSUPPORTED_FUNCTION:
LogMessage("ERROR: Function cannot be hooked (too short or problematic)");
return FALSE;
case MH_ERROR_MEMORY_ALLOC:
LogMessage("ERROR: Memory allocation failed");
return FALSE;
default:
LogMessage("ERROR: Unknown MinHook error: %d", status);
return FALSE;
}
if (g_logging_enabled)
InitializeF9Hotkey(trigger_shaderlog);
return success;
}
// Cleanup function
void RemoveShaderLoadHook() {
MH_DisableHook(MH_ALL_HOOKS);
MH_RemoveHook(MH_ALL_HOOKS);
if (g_critical_section_initialized) {
DeleteCriticalSection(&g_terrain_critical_section);
g_critical_section_initialized = FALSE;
}
LogMessage("All shader hooks removed");
}