-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.cpp
390 lines (314 loc) · 13.2 KB
/
patch.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
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#include <ws2tcpip.h>
#include <winsock2.h>
#include <windows.h>
#include <commctrl.h>
#include <iphlpapi.h>
#include <winnls.h>
#include <pathcch.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <locale>
#include <codecvt>
#include <string>
#include <vector>
#include <thread>
#include "discord/cpp/discord.h"
constexpr unsigned int patchOneStart = 0x4C14E0;
constexpr unsigned char patchOne[] = {
0x30, 0x0F, 0x84, 0xDE, 0x01, 0x00, 0x00, 0x53, 0x57, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0xBD, 0xC0, 0xA8, 0x01, 0x06, 0xBA, 0xFF, 0xFF, 0xFF, 0x00, 0x89,
0x6C, 0x24, 0x20, 0x89, 0x54, 0x24, 0x30, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x8B, 0x4C, 0x24, 0x38, 0x51, 0xFF, 0x15, 0xE0, 0xD3, 0x5A, 0x00,
};
constexpr unsigned int patchOneIpLocation = 0x4C1556;
constexpr unsigned int patchOneNetMaskLocation = 0x4C155B;
constexpr unsigned int patchTwoStart = 0x4c1380;
constexpr unsigned char patchTwo[] = {
0x9C, 0x24, 0x98, 0x02, 0x00, 0x00, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0xB8, 0xC0, 0xA8, 0x01, 0x06, 0x68, 0x81, 0x00, 0x00, 0x00, 0x50,
};
constexpr unsigned int patchTwoIpLocation = 0x4C13B6;
char *level = (char*)0x8B83A1;
IP_ADAPTER_ADDRESSES *pAddresses;
int selectedIndex = 0;
std::filesystem::path configFilePath;
discord::Core* core{};
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void OpenAdapterSelectDialog(HINSTANCE hInstDll);
bool LoadPrior();
void LoadAdapters();
void Patch(PIP_ADAPTER_UNICAST_ADDRESS_LH addr);
void Discord();
const char* LevelToName();
auto exiting = false;
extern __declspec(dllexport) INT APIENTRY DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
{
wchar_t path[MAX_PATH];
const auto getModuleError = GetModuleFileNameW(nullptr, path, MAX_PATH);
configFilePath = std::filesystem::path(path);
configFilePath.remove_filename();
configFilePath.append("arenaPatch.cfg");
LoadAdapters();
if(!LoadPrior())
OpenAdapterSelectDialog(hInstDll);
delete(pAddresses);
auto hThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Discord, 0, 0, 0);
break;
}
case DLL_PROCESS_DETACH:
exiting = true;
break;
}
return TRUE;
}
void Discord()
{
discord::ClientId CLIENT_ID(983113098980364359);
auto result = discord::Core::Create(CLIENT_ID, EDiscordCreateFlags::DiscordCreateFlags_NoRequireDiscord, &core);
if(core == nullptr) {
return;
}
discord::Activity activity{};
activity.SetType(discord::ActivityType::Playing);
activity.GetAssets().SetLargeImage("raymanmarena");
activity.GetAssets().SetLargeText("Join the community: https://discord.gg/qp5S83ZhgW");
activity.GetAssets().SetSmallText("Join the community: https://discord.gg/qp5S83ZhgW");
activity.GetTimestamps().SetStart(std::time(nullptr));
core->ActivityManager().UpdateActivity(activity, [](discord::Result result) {
});
while (!exiting)
{
activity.SetDetails(LevelToName());
core->ActivityManager().UpdateActivity(activity, [](discord::Result result) {
});
core->RunCallbacks();
const auto delay = 5000;
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
}
}
const char* LevelToName()
{
if(strcmp(level, "1shadow") == 0) return "Shadow Plain";
if(strcmp(level, "1shrine") == 0) return "Rise & Shrine";
if(strcmp(level, "1sunset") == 0) return "Sunset Coast";
if(strcmp(level, "2haunted") == 0) return "Haunted Yard";
if(strcmp(level, "2palm") == 0) return "Palm Beach";
if(strcmp(level, "2timber") == 0) return "Timber Wood";
if(strcmp(level, "3coconut") == 0) return "Coconut Island";
if(strcmp(level, "3ly") == 0) return "Ly's Palace";
if(strcmp(level, "3spell") == 0) return "Spellbound Forest";
if(strcmp(level, "4forgot") == 0) return "Forgotten Dungeon";
if(strcmp(level, "4gem") == 0) return "Gemstone Temple";
if(strcmp(level, "4ghastly") == 0) return "Ghastly Trees";
if(strcmp(level, "crypt1") == 0) return "First Ruins";
if(strcmp(level, "crypt2") == 0) return "Nebulous Tower";
if(strcmp(level, "crypt3") == 0) return "Dark Sewer";
if(strcmp(level, "factory1") == 0) return "Pipe Maze";
if(strcmp(level, "factory2") == 0) return "Lava Factory";
if(strcmp(level, "factory3") == 0) return "Electric Final";
if(strcmp(level, "lagoon1") == 0) return "Dawn Sand";
if(strcmp(level, "lagoon2") == 0) return "Water Canyon";
if(strcmp(level, "lagoon3") == 0) return "Thousand Waterfalls";
if(strcmp(level, "pirate1") == 0) return "Forest Jump";
if(strcmp(level, "pirate2") == 0) return "Zenith Harbour";
if(strcmp(level, "pirate3") == 0) return "Treasure Ship";
return "Menu";
}
bool LoadPrior()
{
std::wifstream settings(configFilePath);
settings.imbue(std::locale(settings.getloc(), new std::codecvt_utf8_utf16<wchar_t, 0x10FFFF, std::consume_header>));
std::wstring line;
std::getline(settings, line);
settings.close();
const auto normalizedSize = NormalizeString(NORM_FORM::NormalizationC, line.c_str(), -1, nullptr, 0);
std::vector<wchar_t> normalizedString(normalizedSize);
const auto error = NormalizeString(NORM_FORM::NormalizationC, line.c_str(), -1, normalizedString.data(), normalizedSize);
std::wstring adapterName(normalizedString.data());
auto pCurrAddresses = pAddresses;
while (pCurrAddresses)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter;
//convert friendlyname to utf8.
//windows does not usually validate utf16 (for example with file paths), I assume it doesn't for the FriendlyName.
//roundtrip the string to utf8 and back to utf16 with default char generated for bad surrogates and other garbage.
//that way it will match what is saved in the file
//though, I'm not sure how the codecvt handles bad chars, and it's deprecated
//should have used the windows os converter functions
const auto utf8 = converter.to_bytes(pCurrAddresses->FriendlyName);
//convert back to utf16
const auto utf16 = converter.from_bytes(utf8);
//normalize, so comparison will work.
//If other programs edit the text, there is no guarantee the normalization will match what was saved in the friendly name
const auto normalizedFriendlyNameSize = NormalizeString(NORM_FORM::NormalizationC, utf16.c_str(), -1, nullptr, 0);
std::vector<wchar_t> normalizedFriendlynameString(normalizedFriendlyNameSize);
const auto friendlyNameError = NormalizeString(NORM_FORM::NormalizationC, utf16.c_str(), -1, normalizedFriendlynameString.data(), normalizedFriendlyNameSize);
std::wstring friendlyName(normalizedFriendlynameString.data());
//== is just going to compare ordinals/wchar_ts/code units, so normalization needed first
if(adapterName == friendlyName)
{
Patch(pCurrAddresses->FirstUnicastAddress);
return true;
}
pCurrAddresses = pCurrAddresses->Next;
}
return false;
}
void LoadAdapters()
{
unsigned long size = 0;
const auto statusGetSize = GetAdaptersAddresses(AF_INET, 0, nullptr, nullptr, &size);
pAddresses = new IP_ADAPTER_ADDRESSES[size];
const auto status = GetAdaptersAddresses(AF_INET, 0, nullptr, pAddresses, &size);
}
void OpenAdapterSelectDialog(const HINSTANCE hInstDll)
{
const auto CLASS_NAME = L"Adapter Select";
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstDll;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
auto hwnd = CreateWindowEx(
0,
CLASS_NAME,
L"Select Adapter",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
400,
400,
nullptr,
nullptr,
hInstDll,
nullptr
);
ShowWindow(hwnd, SW_SHOW);
int xpos = 30;
int ypos = 30;
int nwidth = 340;
int nheight = 300;
auto hWndListBox = CreateWindowEx(
0,
WC_LISTBOX,
TEXT(""),
WS_VISIBLE|WS_CHILD|WS_VSCROLL|LBS_NOTIFY,
xpos, ypos, nwidth, nheight,
hwnd,
nullptr,
hInstDll,
nullptr);
//keep the start of the list in pAddresses
auto pCurrAddresses = pAddresses;
while (pCurrAddresses)
{
SendMessageW(hWndListBox,(UINT) LB_ADDSTRING,(WPARAM) 0,(LPARAM) pCurrAddresses->FriendlyName);
pCurrAddresses = pCurrAddresses->Next;
}
SendMessageW(hWndListBox, LB_SETCURSEL, (WPARAM)0, (LPARAM)0);
auto hwndButton = CreateWindowExW(
0,
L"BUTTON",
L"OK",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
163,
320,
75,
23,
hwnd,
NULL,
hInstDll,
NULL);
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//https://github.com/ianpatt/f4se/blob/34dd7e92227e2c027e3910631ac7b7478c3fe6c5/f4se_common/SafeWrite.cpp
void SafeWriteBuf(const uintptr_t addr, const void * data, const size_t len)
{
DWORD oldProtect;
VirtualProtect((void *)addr, len, PAGE_EXECUTE_READWRITE, &oldProtect);
memcpy((void *)addr, data, len);
VirtualProtect((void *)addr, len, oldProtect, &oldProtect);
}
void Patch(const PIP_ADAPTER_UNICAST_ADDRESS_LH addr)
{
SafeWriteBuf(patchOneStart, patchOne, sizeof(patchOne));
SafeWriteBuf(patchTwoStart, patchTwo, sizeof(patchTwo));
auto ipAddrArray = (unsigned char *)addr->Address.lpSockaddr->sa_data;
unsigned int octal0 = ipAddrArray[5] << 24;
unsigned int octal1 = ipAddrArray[4] << 16;
unsigned int octal2 = ipAddrArray[3] << 8;
unsigned int octal3 = ipAddrArray[2] << 0;
auto ipAddress = octal0 | octal1 | octal2 | octal3;
unsigned long netmask;
ConvertLengthToIpv4Mask(addr->OnLinkPrefixLength, &netmask);
SafeWriteBuf(patchOneIpLocation, &ipAddress, sizeof(ipAddress));
SafeWriteBuf(patchOneNetMaskLocation, &netmask, sizeof(netmask));
SafeWriteBuf(patchTwoIpLocation, &ipAddress, sizeof(ipAddress));
}
LRESULT CALLBACK WindowProc(const HWND hwnd, const UINT uMsg, const WPARAM wParam, const LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_COMMAND:
auto command = HIWORD(wParam);
auto childHWnd = (HWND)(lParam);
switch (command)
{
case(LBN_SELCHANGE):
selectedIndex = SendMessage(childHWnd, (UINT) LB_GETCURSEL, (WPARAM) 0, (LPARAM) 0);
break;
case(BN_CLICKED):
auto addr = pAddresses;
for(int i = 0; i < selectedIndex; i++){
addr = addr->Next;
}
Patch(addr->FirstUnicastAddress);
std::wofstream settings(configFilePath);
settings.imbue(std::locale(settings.getloc(), new std::codecvt_utf8_utf16<wchar_t, 0x10FFFF, std::consume_header>));
settings << addr->FriendlyName << std::endl;
settings.close();
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
}
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}