-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.cpp
397 lines (318 loc) · 12.1 KB
/
main.cpp
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
386
387
388
389
390
391
392
393
394
395
396
397
#include <ws2spi.h>
#include <windows.h>
#include <stdio.h>
#include <Include\d3d8.h> // Set the include path like this as it conflicts with windows 10 sdk unless using visual studio and manually adjusting include orders
#include "detours.h"
extern "C" __declspec(dllexport) void dummyExport()
{
printf("You are calling the dummy export\n");
return;
}
#define WINDOWNAME "Valhalla v28"
#define CONFIGNAME "valhalla.ini"
#define CREATEDEVICE_VTI 15
PDWORD IDirect3D8_vtable = NULL;
D3DDISPLAYMODE display_mode;
HWND window = NULL;
struct config
{
BOOL debug_print;
BOOL multiclient;
BOOL windowed;
int width;
int height;
BOOL screen_centre;
};
config settings = {};
BOOL parse_config()
{
char path[MAX_PATH];
GetModuleFileNameA(NULL, path, MAX_PATH);
for (int i = MAX_PATH - 1; i >= 0; --i)
{
if (path[i] == '\\')
{
++i;
if (i + strlen(CONFIGNAME) >= MAX_PATH)
{
MessageBoxA(NULL, "You have placed your maplestory install in a folder path that is too long.", "Error", MB_OK | MB_ICONERROR);
return FALSE;
}
for (int j = 0; j < strlen(CONFIGNAME); ++j)
{
path[i + j] = CONFIGNAME[j];
}
path[i + strlen(CONFIGNAME)] = '\0';
break;
}
}
HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBoxA(NULL, "Could not load settings file, please make sure it is named properly.", "Error", MB_OK | MB_ICONERROR);
return FALSE;
}
CloseHandle(hFile);
settings.debug_print = GetPrivateProfileInt("Debug", "print", 0, path);
settings.multiclient = GetPrivateProfileInt("Debug", "multiclient", 0, path);
settings.windowed = GetPrivateProfileInt("Display", "window_mode", 0, path);
settings.width = GetPrivateProfileInt("Display", "resolution_x", 800, path);
settings.height = GetPrivateProfileInt("Display", "resolution_y", 700, path);
settings.screen_centre = GetPrivateProfileInt("Display", "screen_centre", 0, path);
return TRUE;
}
typedef HWND (WINAPI* create_window_ex_ptr)(DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);
create_window_ex_ptr CreateWindowEx_original;
typedef BOOL (WINAPI* set_window_pos_ptr)(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags);
set_window_pos_ptr SetWindowPos_original;
typedef LONG (WINAPI* set_window_long_a_ptr)(HWND hWnd, int nIndex, LONG dwNewLong);
set_window_long_a_ptr SetWindowLongA_original;
typedef HANDLE (WINAPI* create_mutex_a_ptr)(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCSTR lpName);
create_mutex_a_ptr CreateMutexA_original;
typedef int (WINAPI* wsp_startup_ptr)(WORD wVersionRequested, LPWSPDATA lpWSPData, LPWSAPROTOCOL_INFOW lpProtocolInfo, WSPUPCALLTABLE UpcallTable, LPWSPPROC_TABLE lpProcTable);
wsp_startup_ptr WSPStartup_original;
typedef HRESULT (WINAPI* create_device_ptr)(IDirect3D8* Direct3D_Object, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DDevice8** ppReturnedDeviceInterface);
create_device_ptr CreateDevice_original;
HWND WINAPI CreateWindowEx_hook(DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
{
printf("Creating window with name: %s\n", lpClassName);
if (!strcmp((char*)lpClassName, "NexonADBallon") || !strcmp((char*)lpClassName, "NexonADBrowser"))
{
return NULL;
}
if (!strcmp((char*)lpClassName, "MapleStoryClass"))
{
window = CreateWindowEx_original(dwExStyle, lpClassName, (LPCTSTR)WINDOWNAME, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
return window;
}
return CreateWindowEx_original(dwExStyle, lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
}
BOOL WINAPI SetWindowPos_hook(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags)
{
printf("SetWindowPos: %i %i %i %i %i\n", X, Y, cx, cy, uFlags);
if (settings.windowed && hWnd == window)
{
cx = settings.width;
cy = settings.height;
if (settings.screen_centre)
{
X = (GetSystemMetrics(SM_CXSCREEN) / 2) - (settings.width / 2);
Y = (GetSystemMetrics(SM_CYSCREEN) / 2) - (settings.height / 2);
}
uFlags = SWP_DRAWFRAME;
hWndInsertAfter = HWND_NOTOPMOST;
RECT rect = {};
rect.left = X;
rect.right = X + cx;
rect.top = Y;
rect.bottom = Y + cy;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
X = rect.left;
cx = rect.right - X;
Y = rect.top;
cy = rect.bottom - Y;
}
return SetWindowPos_original(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags);
}
LONG WINAPI SetWindowLongA_hook(HWND hWnd, int nIndex, LONG dwNewLong)
{
printf("SetWindowLongA: %i, 0x%.8X\n", nIndex, dwNewLong);
if (settings.windowed && hWnd == window)
{
dwNewLong = WS_OVERLAPPEDWINDOW;
}
return SetWindowLongA_original(hWnd, nIndex, dwNewLong);;
}
HANDLE WINAPI CreateMutexA_hook(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCSTR lpName)
{
printf("CreateMutexA: %s\n", lpName);
if (settings.multiclient)
{
lpName = NULL; // If this causes issues, only null the named mutex: WvsClientMtx
}
return CreateMutexA_original(lpMutexAttributes, bInitialOwner, lpName);
}
int WINAPI WSPStartup_hook(WORD wVersionRequested, LPWSPDATA lpWSPData, LPWSAPROTOCOL_INFOW lpProtocolInfo, WSPUPCALLTABLE UpcallTable, LPWSPPROC_TABLE lpProcTable)
{
printf("WSPStartup\n");
// Detouring/Gateway the following functions should allow you to re-direct any client to any ip/domain (IPv4 only?)
// https://docs.microsoft.com/en-us/windows/win32/api/ws2spi/ns-ws2spi-wspproc_table
// lpProcTable->lpWSPConnect;
// lpProcTable->lpWSPGetPeerName;
return WSPStartup_original(wVersionRequested, lpWSPData, lpProtocolInfo, UpcallTable, lpProcTable);
}
HRESULT WINAPI CreateDevice_hook(IDirect3D8* Direct3D_Object, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DDevice8** ppReturnedDeviceInterface)
{
if (settings.windowed)
{
pPresentationParameters->Windowed = TRUE;
pPresentationParameters->SwapEffect = D3DSWAPEFFECT::D3DSWAPEFFECT_DISCARD; // D3DSWAPEFFECT_FLIP for fullscreen
pPresentationParameters->FullScreen_RefreshRateInHz = 0; // 60 for fullscreen
pPresentationParameters->BackBufferFormat = display_mode.Format;
pPresentationParameters->BackBufferWidth = 0;
pPresentationParameters->BackBufferHeight = 0;
}
HRESULT result = CreateDevice_original(Direct3D_Object, Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface);
DWORD protectFlag;
if (VirtualProtect(&IDirect3D8_vtable[CREATEDEVICE_VTI], sizeof(DWORD), PAGE_READWRITE, &protectFlag))
{
IDirect3D8_vtable[CREATEDEVICE_VTI] = (DWORD)CreateDevice_original;
if (!VirtualProtect(&IDirect3D8_vtable[CREATEDEVICE_VTI], sizeof(DWORD), protectFlag, &protectFlag))
{
printf("VirtualProtect apply on CreateDevice vtable entry failed\n");
return D3DERR_INVALIDCALL;
}
printf("Reset CreateDevice vtable\n");
}
else
{
printf("VirtualProtect remove on CreateDevice vtable entry failed\n");
return D3DERR_INVALIDCALL;
}
if (result == D3D_OK)
{
IDirect3D8_vtable = (DWORD*)*(DWORD*)*ppReturnedDeviceInterface;
// patch in functions of interest in vtable (endscene?), need to check if ms ever resets these pointers at any point and if so start thread to periodically patch
}
return result;
}
BOOL apply_hook(__inout PVOID* ppvTarget, __in PVOID pvDetour)
{
if (DetourTransactionBegin() != NO_ERROR)
{
printf("Failed to being detour\n");
return FALSE;
}
if (DetourUpdateThread(GetCurrentThread()) != NO_ERROR)
{
printf("Detour thread update failed\n");
return FALSE;
}
if (DetourAttach(ppvTarget, pvDetour) != NO_ERROR)
{
printf("Failed detour attach\n");
return FALSE;
}
if (DetourTransactionCommit() != NO_ERROR)
{
printf("Failed detour transaction commit\n");
DetourTransactionAbort();
return FALSE;
}
return TRUE;
}
BOOL hook()
{
CreateWindowEx_original = &CreateWindowExA;
if (!apply_hook((PVOID*)&CreateWindowEx_original, (PVOID)CreateWindowEx_hook))
{
printf("CreateWindowEx hook failed\n");
return FALSE;
}
else
{
printf("CreateWindowEx hooked\n");
}
SetWindowLongA_original = &SetWindowLongA;
if (!apply_hook((PVOID*)&SetWindowLongA_original, (PVOID)SetWindowLongA_hook))
{
printf("SetWindowLongA hook failed\n");
return FALSE;
}
else
{
printf("SetWindowLongA hooked\n");
}
SetWindowPos_original = &SetWindowPos;
if (!apply_hook((PVOID*)&SetWindowPos_original, (PVOID)SetWindowPos_hook))
{
printf("SetWindowPos hook failed\n");
return FALSE;
}
else
{
printf("SetWindowPos hooked\n");
}
CreateMutexA_original = &CreateMutexA;
if (!apply_hook((PVOID*)&CreateMutexA_original, (PVOID)CreateMutexA_hook))
{
printf("CreateMutexA hook failed\n");
return FALSE;
}
else
{
printf("CreateMutexA hooked\n");
}
HMODULE module = LoadLibrary("MSWSOCK");
if (!module)
{
printf("Could not load MSWSOCK\n");
return FALSE;
}
DWORD address = (DWORD)GetProcAddress(module, "WSPStartup");
if (!address)
{
printf("Unable to find address of WSPStartup\n");
return FALSE;
}
wsp_startup_ptr WSPStartup = (wsp_startup_ptr)address;
WSPStartup_original = WSPStartup;
if (!apply_hook((PVOID*)&WSPStartup_original, (PVOID)WSPStartup_hook))
{
printf("WSPStartup hook failed\n");
return FALSE;
}
else
{
printf("WSPStartup hooked\n");
}
IDirect3D8* d3d_device = Direct3DCreate8(D3D_SDK_VERSION);
if (!d3d_device)
{
printf("Failed to create d3d device\n");
return FALSE;
}
IDirect3D8_vtable = (DWORD*)*(DWORD*)d3d_device;
d3d_device->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &display_mode);
d3d_device->Release();
DWORD protectFlag;
if (VirtualProtect(&IDirect3D8_vtable[CREATEDEVICE_VTI], sizeof(DWORD), PAGE_READWRITE, &protectFlag))
{
CreateDevice_original = (create_device_ptr)IDirect3D8_vtable[CREATEDEVICE_VTI];
IDirect3D8_vtable[CREATEDEVICE_VTI] = (DWORD)CreateDevice_hook;
if (!VirtualProtect(&IDirect3D8_vtable[CREATEDEVICE_VTI], sizeof(DWORD), protectFlag, &protectFlag))
{
printf("VirtualProtect apply on CreateDevice vtable entry failed\n");
return FALSE;
}
printf("CreateDevice hooked\n");
}
else
{
printf("VirtualProtect remove on CreateDevice vtable entry failed\n");
return FALSE;
}
return TRUE;
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
{
if (!parse_config())
{
return FALSE;
}
if (settings.debug_print)
{
AllocConsole();
AttachConsole(GetCurrentProcessId());
freopen("CON", "w", stdout);
char title[128];
sprintf_s(title, "Attached to: %i", GetCurrentProcessId());
SetConsoleTitleA(title);
}
DisableThreadLibraryCalls(hinstDLL);
return hook();
}
return TRUE;
}