-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbink2w64.cpp
More file actions
345 lines (313 loc) · 8.31 KB
/
bink2w64.cpp
File metadata and controls
345 lines (313 loc) · 8.31 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
#include <Windows.h>
#include <DbgHelp.h>
#include <vector>
#include <fstream>
#include "ReplaceImport.h"
std::vector<const char *> sImportFunctionNames =
{
"BinkAllocateFrameBuffers",
"BinkClose",
"BinkCloseTrack",
"BinkControlBackgroundIO",
"BinkCopyToBuffer",
"BinkCopyToBufferRect",
"BinkDoFrame",
"BinkDoFrameAsync",
"BinkDoFrameAsyncMulti",
"BinkDoFrameAsyncWait",
"BinkDoFramePlane",
"BinkFreeGlobals",
"BinkGetError",
"BinkGetFrameBuffersInfo",
"BinkGetGPUDataBuffersInfo",
"BinkGetKeyFrame",
"BinkGetPlatformInfo",
"BinkGetRealtime",
"BinkGetRects",
"BinkGetSummary",
"BinkGetTrackData",
"BinkGetTrackID",
"BinkGetTrackMaxSize",
"BinkGetTrackType",
"BinkGoto",
"BinkLogoAddress",
"BinkNextFrame",
"BinkOpen",
"BinkOpenDirectSound",
"BinkOpenMiles",
"BinkOpenTrack",
"BinkOpenWaveOut",
"BinkOpenWithOptions",
"BinkOpenXAudio2",
"BinkOpenXAudio27",
"BinkOpenXAudio28",
"BinkPause",
"BinkRegisterFrameBuffers",
"BinkRegisterGPUDataBuffers",
"BinkRequestStopAsyncThread",
"BinkRequestStopAsyncThreadsMulti",
"BinkService",
"BinkSetError",
"BinkSetFileOffset",
"BinkSetFrameRate",
"BinkSetIO",
"BinkSetIOSize",
"BinkSetMemory",
"BinkSetOSFileCallbacks",
"BinkSetPan",
"BinkSetSimulate",
"BinkSetSoundOnOff",
"BinkSetSoundSystem",
"BinkSetSoundSystem2",
"BinkSetSoundTrack",
"BinkSetSpeakerVolumes",
"BinkSetVideoOnOff",
"BinkSetVolume",
"BinkSetWillLoop",
"BinkShouldSkip",
"BinkStartAsyncThread",
"BinkUtilCPUs",
"BinkUtilFree",
"BinkUtilMalloc",
"BinkUtilMutexCreate",
"BinkUtilMutexDestroy",
"BinkUtilMutexLock",
"BinkUtilMutexLockTimeOut",
"BinkUtilMutexUnlock",
"BinkWait",
"BinkWaitStopAsyncThread",
"BinkWaitStopAsyncThreadsMulti",
"RADTimerRead"
};
extern "C" uintptr_t iImportFunctions[73] = { 0 };
HINSTANCE pOriginalHinst = nullptr;
HINSTANCE pWarpperHinst = nullptr;
std::vector<HINSTANCE> loadedPlugins;
PROC Init_Original = nullptr;
std::string GetPluginsDirectory()
{
return "NativeMods\\";
}
void Error(const char * msg)
{
MessageBoxA(nullptr, msg, "NativeModLoader", 0);
}
int LoadDLLPlugin(const char * path)
{
int state = -1;
__try
{
HINSTANCE plugin = LoadLibraryA(path);
if (!plugin)
return 0;
int ok = 1;
FARPROC fnInit = GetProcAddress(plugin, "Init");
if (fnInit != nullptr)
{
state = -2;
((void(__cdecl *)())fnInit)();
ok = 2;
}
loadedPlugins.push_back(plugin);
return ok;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
}
return state;
}
void LoadLib()
{
static bool isLoaded = false;
if (!isLoaded)
{
isLoaded = true;
std::ofstream fLog = std::ofstream("NativeModLoader.log");
WIN32_FIND_DATA wfd;
std::string dir = GetPluginsDirectory();
std::string search_dir = dir + "*.dll";
HANDLE hFind = FindFirstFileA(search_dir.c_str(), &wfd);
if (hFind != INVALID_HANDLE_VALUE)
{
int i_error = 0;
do
{
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
continue;
std::string name = wfd.cFileName;
name = dir + name;
if (fLog.good())
fLog << "Checking \"" << name.c_str() << "\" ... ";
int result = LoadDLLPlugin(name.c_str());
switch (result)
{
case 2:
{
if (fLog.good())
fLog << "OK - loaded and called Init().\n";
break;
}
case 1:
{
if (fLog.good())
fLog << "OK - loaded.\n";
break;
}
case 0:
{
if (fLog.good())
fLog << "LoadLibrary failed!\n";
i_error = 1;
std::string err = "LoadLibrary failed on ";
err = err + name;
Error(err.c_str());
break;
}
case -1:
{
if (fLog.good())
fLog << "LoadLibrary crashed! This means there's a problem in the plugin DLL file.\n";
i_error = 1;
std::string err = "LoadLibrary crashed on ";
err = err + name;
err = err + ". This means there's a problem in the plugin DLL file. Contact the author of that plugin.";
Error(err.c_str());
break;
}
case -2:
{
if (fLog.good())
fLog << "Init() crashed! This means there's a problem in the plugin DLL file.\n";
i_error = 1;
std::string err = "Init() crashed on ";
err = err + name;
err = err + ". This means there's a problem in the plugin DLL file. Contact the author of that plugin.";
Error(err.c_str());
break;
}
}
} while (i_error == 0 && FindNextFileA(hFind, &wfd));
FindClose(hFind);
}
else
{
if (fLog.good())
fLog << "Failed to get search handle to \"" << search_dir.c_str() << "\"!\n";
}
}
}
PVOID Init_Hook(PVOID arg1, PVOID arg2)
{
LoadLib();
return ((PVOID(*)(PVOID, PVOID))Init_Original)(arg1, arg2);
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
pWarpperHinst = hinstDLL;
if (fdwReason == DLL_PROCESS_ATTACH)
{
pOriginalHinst = LoadLibraryA("bink2w64_original.dll");
if (!pOriginalHinst)
{
Error("Failed to load bink2w64_.dll!");
return FALSE;
}
for (size_t i = 0; i < sImportFunctionNames.size(); i++)
iImportFunctions[i] = reinterpret_cast<uintptr_t>(GetProcAddress(pOriginalHinst, sImportFunctionNames[i]));
int result = ReplaceImport::Replace("api-ms-win-crt-runtime-l1-1-0.dll", "_initterm_e", (PROC)Init_Hook, &Init_Original);
switch (result)
{
case 0: break;
case 1: Error("Failed to get handle to main module!"); break;
case 2: Error("Failed to find import table in executable!"); break;
case 3: Error("Failed to change protection flags on memory page!"); break;
case 4: Error("Failed to find API function in module!"); break;
case 5: Error("Failed to find module!"); break;
default: Error("Unknown error occurred!"); break;
}
}
else if (fdwReason == DLL_PROCESS_DETACH)
{
FreeLibrary(pOriginalHinst);
if (!loadedPlugins.empty())
{
for(auto plugin : loadedPlugins)
FreeLibrary(plugin);
loadedPlugins.clear();
}
}
return TRUE;
}
extern "C" void BinkAllocateFrameBuffers();
extern "C" void BinkClose();
extern "C" void BinkCloseTrack();
extern "C" void BinkControlBackgroundIO();
extern "C" void BinkCopyToBuffer();
extern "C" void BinkCopyToBufferRect();
extern "C" void BinkDoFrame();
extern "C" void BinkDoFrameAsync();
extern "C" void BinkDoFrameAsyncMulti();
extern "C" void BinkDoFrameAsyncWait();
extern "C" void BinkDoFramePlane();
extern "C" void BinkFreeGlobals();
extern "C" void BinkGetError();
extern "C" void BinkGetFrameBuffersInfo();
extern "C" void BinkGetGPUDataBuffersInfo();
extern "C" void BinkGetKeyFrame();
extern "C" void BinkGetPlatformInfo();
extern "C" void BinkGetRealtime();
extern "C" void BinkGetRects();
extern "C" void BinkGetSummary();
extern "C" void BinkGetTrackData();
extern "C" void BinkGetTrackID();
extern "C" void BinkGetTrackMaxSize();
extern "C" void BinkGetTrackType();
extern "C" void BinkGoto();
extern "C" void BinkLogoAddress();
extern "C" void BinkNextFrame();
extern "C" void BinkOpen();
extern "C" void BinkOpenDirectSound();
extern "C" void BinkOpenMiles();
extern "C" void BinkOpenTrack();
extern "C" void BinkOpenWaveOut();
extern "C" void BinkOpenWithOptions();
extern "C" void BinkOpenXAudio2();
extern "C" void BinkOpenXAudio27();
extern "C" void BinkOpenXAudio28();
extern "C" void BinkPause();
extern "C" void BinkRegisterFrameBuffers();
extern "C" void BinkRegisterGPUDataBuffers();
extern "C" void BinkRequestStopAsyncThread();
extern "C" void BinkRequestStopAsyncThreadsMulti();
extern "C" void BinkService();
extern "C" void BinkSetError();
extern "C" void BinkSetFileOffset();
extern "C" void BinkSetFrameRate();
extern "C" void BinkSetIO();
extern "C" void BinkSetIOSize();
extern "C" void BinkSetMemory();
extern "C" void BinkSetOSFileCallbacks();
extern "C" void BinkSetPan();
extern "C" void BinkSetSimulate();
extern "C" void BinkSetSoundOnOff();
extern "C" void BinkSetSoundSystem();
extern "C" void BinkSetSoundSystem2();
extern "C" void BinkSetSoundTrack();
extern "C" void BinkSetSpeakerVolumes();
extern "C" void BinkSetVideoOnOff();
extern "C" void BinkSetVolume();
extern "C" void BinkSetWillLoop();
extern "C" void BinkShouldSkip();
extern "C" void BinkStartAsyncThread();
extern "C" void BinkUtilCPUs();
extern "C" void BinkUtilFree();
extern "C" void BinkUtilMalloc();
extern "C" void BinkUtilMutexCreate();
extern "C" void BinkUtilMutexDestroy();
extern "C" void BinkUtilMutexLock();
extern "C" void BinkUtilMutexLockTimeOut();
extern "C" void BinkUtilMutexUnlock();
extern "C" void BinkWait();
extern "C" void BinkWaitStopAsyncThread();
extern "C" void BinkWaitStopAsyncThreadsMulti();
extern "C" void RADTimerRead();