forked from gamebooster/witcher3-booster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
385 lines (354 loc) · 11.7 KB
/
main.cc
File metadata and controls
385 lines (354 loc) · 11.7 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <windows.h>
#include <fstream>
#include "vtable/vmthooks.h"
#include "witcher3-classes.h"
#include <iostream>
HANDLE thread = nullptr;
utils::VtableHook* game_hook = nullptr;
void** global_debug_console = nullptr;
CRTTISystem* rtti_system = nullptr;
void* native_globals_function_map = nullptr;
CGame** global_game = nullptr;
// void DumpEnums() {
// std::wofstream log_file;
// log_file.open("global_enums_log.txt");
//
// TDynArray enums;
// enums.base_pointer = &enums.begin;
// enums.count = 0;
// enums.max = 0;
// rtti_system->EnumEnums(enums);
//
// for (size_t i = 0; i < enums.count; i++) {
// auto enum_ = *(CEnum**)(enums.base_pointer + i);
// if (!enum_) continue; //|| !enum_->source_script) continue;
//
// log_file << "enum " << enum_->name.AsChar() << " // " << std::dec <<
// enum_->names_count << " " << std::dec << enum_->flags << " " <<
// enum_->source_script << std::endl;
// log_file << "{" << std::endl;
// for (size_t i = 0; i < enum_->names_count; i++) {
// CName name;
// enum_->FindName(i, name);
// if (name.pool_index) {
// int value;
// enum_->FindValue(name, value);
// log_file << " " << name.AsChar() << " = " << value << "," <<
// std::endl;
// }
// }
// log_file << std::endl << "}" << std::endl;
// }
//
// log_file.close();
//}
//
// std::wostream& operator<<(std::wostream& stream, const wchar_t* string) {
// if (string == nullptr) return stream;
//
// std::wstring test(string);
//
// size_t pos = test.find(L"handle:");
// if (pos != -1) {
// test.replace(pos, std::wstring(L"handle:").length(), L"");
// }
//
// pos = test.find(L"ptr:");
// if (pos != -1) {
// test.replace(pos, std::wstring(L"ptr:").length(), L"*");
// }
//
// pos = test.find(L"array:2,0,array:2,0,");
// if (pos != -1) {
// test.replace(pos, std::wstring(L"array:2,0,array:2,0,").length(),
// L"C2dArray");
// }
//
// pos = test.find(L"array:2,0,");
// if (pos != -1) {
// test.replace(pos, std::wstring(L"array:2,0,").length(), L"array<");
// test += L">";
// }
//
// stream << test;
// return stream;
//}
//
// void DumpGlobalFunctions() {
// std::wofstream log_file;
// log_file.open("global_functions_log.txt");
//
// TDynArray functions;
// functions.base_pointer = &functions.begin;
// functions.count = 0;
// functions.max = 0;
// rtti_system->EnumFunctions(functions);
//
// CName* last_class = nullptr;
//
// for (size_t i = 0; i < functions.count; i++) {
// auto function = *(CFunction**)(functions.base_pointer + i);
// if (!function) continue; // || !(function->flags & (1 << 8))) continue;
//
// if (last_class && (!function->parent_class ||
// (function->parent_class->GetName().pool_index != last_class->pool_index)))
// {
// log_file << std::endl << "}" << std::endl;
// last_class = nullptr;
// }
//
// if (function->parent_class && !last_class) {
// last_class = &function->parent_class->GetName();
// log_file << "import class " << last_class->AsChar();
// if (function->parent_class->parent) {
// log_file << " extends " <<
// function->parent_class->parent->GetName().AsChar();
// }
// log_file << " {" << std::endl;
//
// for (size_t i = 0; i < function->parent_class->property_count; i++) {
// auto property = (CProperty*)function->parent_class->property_array[i];
// //if (!(property->flags & PropFlag_Compatible)) continue;
// if (!(property->flags & PropFlag_Import)) continue;
//
// log_file << " import var " << property->name.AsChar() << " : " <<
// property->class_type->GetName().AsChar() << ";" << " // " <<
// property->flags << std::endl;
// }
// log_file << std::endl;
//
// }
//
// if (function->parent_class) log_file << " ";
//
// if (function->flags & FuncFlag_Exported) {
// log_file << "import ";
// }
//
// if (function->flags & FuncFlag_Public) {
// log_file << "public ";
// }
//
// if (function->flags & FuncFlag_Private) {
// log_file << "private ";
// }
//
// if (function->flags & FuncFlag_Protected) {
// log_file << "protected ";
// }
//
// if (function->flags & FuncFlag_Event) {
// log_file << "event ";
// }
//
// if (function->flags & FuncFlag_Latent) {
// log_file << "latent ";
// }
//
// if (function->flags & FuncFlag_Cleanup) {
// log_file << "cleanup ";
// }
//
// if (function->flags & FuncFlag_Final) {
// log_file << "final ";
// }
//
// log_file << "function " << function->name.AsChar() << "(";
// for (size_t i = 0; i < function->argument_count; i++) {
// auto property = (CProperty*)function->property_array[i];
//
// if (property->flags & PropFlag_Optional) {
// log_file << " optional ";
// }
//
// if (property->flags & PropFlag_Out) {
// log_file << " out ";
// }
//
// log_file << " " << property->name.AsChar() << " : " <<
// property->class_type->GetName().AsChar();
// if (i + 1 != function->argument_count)
// {
// log_file << ", ";
// } else {
// }
// }
// log_file << ")";
// if (function->return_type) {
// log_file << " : " <<
// function->return_type->class_type->GetName().AsChar();
// }
// log_file << "; " << " // flags: " << std::dec << function->flags << " "<<
// std::hex << function << std::endl;
// }
//
//
//
// log_file.close();
//}
//
typedef bool (*OnViewportInputType)(void* thisptr,
void* viewport,
EInputKey input_key,
EInputAction input_action,
float tick);
OnViewportInputType OnViewportInputDebugConsole = nullptr;
bool OnViewportInputDebugAlwaysHook(void* thisptr,
void* viewport,
EInputKey input_key,
EInputAction input_action,
float tick) {
if ((*global_game)->ProcessFreeCameraInput(input_key, input_action, tick))
return true;
if (input_key == IK_F2 && input_action == IACT_Release) {
input_key = IK_Tilde;
input_action = IACT_Press;
}
return OnViewportInputDebugConsole(*global_debug_console, viewport, input_key,
input_action, tick);
}
//
// void funcLogHook(struct CObject *a1, struct CScriptStackFrame *a2, void *a3)
// {
// OutputDebugStringW(L"test log");
//}
//
// uint64_t* GetFunctionAddress(CFunction* function) {
// return (uint64_t*)((uint64_t)native_globals_function_map + (8 *
// function->registration_offset));
//}
//
// void ReplaceFunction(const char* name, uint64_t address) {
// auto function = rtti_system->FindGlobalFunction(CName(name));
//
// if (function) {
// auto function_address = GetFunctionAddress(function);
//
// std::wstringstream message;
// message << "ReplaceFunction: " << name << " " << std::hex << function << "
// " << " " << std::hex << function_address;
// OutputDebugStringW(message.str().c_str());
//
// *function_address = address;
// }
//}
//
// static void ScriptWarn(void* thisptr, CScriptFileContext* file_context,
// TString* message) {
// std::wofstream log_file;
// log_file.open("script_compilation.log", std::ios_base::app);
// log_file << "warn " << file_context->file_name.buffer_address << ":" <<
// file_context->line_number << " " << message->buffer_address << std::endl;
// log_file.close();
//
// std::wcout << "warn " << file_context->file_name.buffer_address << ":" <<
// file_context->line_number << " " << message->buffer_address << std::endl;
//}
//
// static void ScriptError(void* thisptr, CScriptFileContext* file_context,
// TString* message) {
// std::wofstream log_file;
// log_file.open("script_compilation.log", std::ios_base::app);
// log_file << "ERROR " << file_context->file_name.buffer_address << ":" <<
// file_context->line_number << " " << message->buffer_address << std::endl;
// log_file.close();
//
// std::wcerr << "ERROR " << file_context->file_name.buffer_address << ":" <<
// file_context->line_number << " " << message->buffer_address << std::endl;
//}
std::wstring GetExecutablePath() {
wchar_t buffer[MAX_PATH];
GetModuleFileName(NULL, buffer, MAX_PATH);
std::wstring::size_type pos = std::wstring(buffer).find_last_of(L"\\/");
return std::wstring(buffer).substr(0, pos);
}
std::vector<std::wstring> GetAllFileNamesFromFolder(std::wstring folder) {
std::vector<std::wstring> names;
wchar_t search_path[200];
wsprintf(search_path, L"%s*.*", folder.c_str());
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(search_path, &fd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
names.push_back(fd.cFileName);
}
} while (::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
return names;
}
static bool(__thiscall* OrginalBaseEngine_InitializeScripts)(void*);
static bool BaseEngine_InitializeScripts(void* rcx, void* rdx) {
std::wcout << "BaseEngine_InitializeScripts" << std::endl;
// CRTTISerializer serializer;
// TString path(L"cookedfinal.redscripts");
// if (serializer.LoadScriptData(&path, false)) {
// std::wcout << L"Loaded scriptplugin: cookedfinal.redscripts" << std::endl;
//};
return OrginalBaseEngine_InitializeScripts(rcx);
}
DWORD WINAPI InitializeHook(void* arguments) {
hook::set_base();
HookFunction::RunAll();
global_game = hook::pattern("48 8B 05 ? ? ? ? 48 8D 4C 24 ? C6 44 24")
.count(1)
.get(0)
.extract<CGame**>(3);
global_debug_console =
hook::pattern("48 89 05 ? ? ? ? EB 07 48 89 35 ? ? ? ? 48 8B 97")
.count(1)
.get(0)
.extract<void**>(3);
while (*global_game == nullptr || *global_debug_console == nullptr) {
Sleep(500);
}
OnViewportInputDebugConsole =
hook::pattern("48 83 EC 28 48 8B 05 ? ? ? ? 0F B6 90")
.count(1)
.get(0)
.get<OnViewportInputType>(0);
rtti_system = *hook::pattern("48 8B 0D ? ? ? ? 48 8B 5C 24 ? 48 83 C4 30")
.count(1)
.get(0)
.extract<CRTTISystem**>(3);
// native_globals_function_map = hook::pattern("4C 8D 0D ? ? ? ? 49 89 14
// C1").count(1).get(0).extract<void*>(3);
// FileManager* file_manager = hook::pattern("48 8B 0D ? ? ? ? 48 8D 55 A0 41
// B8").count(1).get(0).extract<FileManager*>(3);
game_hook = new utils::VtableHook(*global_game);
game_hook->HookMethod(OnViewportInputDebugAlwaysHook, 133);
// auto exe_path = GetExecutablePath();
// exe_path += L"\\scriptplugins\\";
// auto plugin_names = GetAllFileNamesFromFolder(exe_path);
// for (auto name : plugin_names) {
// if (name.find(L".redscriptsplugin") == std::string::npos) continue;
// auto full_path = (L"..\\..\\bin\\x64\\scriptplugins\\" + name);
// CRTTISerializer serializer;
// TString path(full_path.c_str());
// if (serializer.LoadScriptData(&path, false)) {
// OutputDebugStringW((L"Loaded scriptplugin: " + name).c_str());
// };
//}
// DumpGlobalFunctions();
// DumpEnums();
// ReplaceFunction("Log", (uint64_t)&funcLogHook);
// ReplaceFunction("LogChannel", (uint64_t)&funcLogHook);
// ReplaceFunction("Trace", (uint64_t)&funcLogHook);
// ReplaceFunction("DebugBreak", (uint64_t)&funcLogHook);
return 1;
}
void FinalizeHook() {
if (game_hook)
delete game_hook;
}
int WINAPI DllMain(HINSTANCE instance, DWORD reason, PVOID reserved) {
if (reason == DLL_PROCESS_ATTACH) {
thread = CreateThread(nullptr, 0, InitializeHook, 0, 0, nullptr);
} else if (reason == DLL_PROCESS_DETACH) {
FinalizeHook();
WaitForSingleObject(thread, INFINITE);
CloseHandle(thread);
}
return 1;
}