Repository navigation
Expand file tree
/
Copy pathLauncher.cpp
More file actions
283 lines (245 loc) · 10 KB
/
Copy pathLauncher.cpp
File metadata and controls
283 lines (245 loc) · 10 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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tlhelp32.h>
#include <string.h>
typedef unsigned long long u64;
typedef unsigned char u8;
#define MAX_PATTERN_BYTES 128
#define MAX_AOB_RESULTS 100
struct Patch {
u64 offset;
u8 expect;
u8 bytes;
};
struct PatchAOB {
u8 pattern[MAX_PATTERN_BYTES];
SIZE_T pattern_len;
SIZE_T offset;
u8 expect;
u8 bytes;
};
struct Version {
const Patch* patches;
SIZE_T patch_count;
};
BOOL GetModuleInfo(DWORD pid, const wchar_t* moduleName, u64* base, SIZE_T* size) {
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (snap == INVALID_HANDLE_VALUE) return FALSE;
MODULEENTRY32W me; me.dwSize = sizeof(me);
BOOL found = FALSE;
if (Module32FirstW(snap, &me)) {
do {
if (_wcsicmp(me.szModule, moduleName) == 0) {
if (base) *base = (u64)me.modBaseAddr;
if (size) *size = me.modBaseSize;
found = TRUE;
break;
}
} while (Module32NextW(snap, &me));
}
CloseHandle(snap);
return found;
}
BOOL ReadAndCompareByte(HANDLE hProc, u64 addr, u8 expected) {
u8 buf = 0; SIZE_T rd = 0;
if (!ReadProcessMemory(hProc, (LPCVOID)addr, &buf, 1, &rd)) return FALSE;
return rd == 1 && buf == expected;
}
BOOL WriteByte(HANDLE hProc, u64 addr, u8 data) {
DWORD oldProt = 0;
if (!VirtualProtectEx(hProc, (LPVOID)addr, 1, PAGE_EXECUTE_READWRITE, &oldProt))
return FALSE;
SIZE_T written = 0;
BOOL ok = WriteProcessMemory(hProc, (LPVOID)addr, &data, 1, &written);
VirtualProtectEx(hProc, (LPVOID)addr, 1, oldProt, &oldProt);
return ok && written == 1;
}
BOOL PatchDirectAddresses(HANDLE hProc, u64 moduleBase, const Version* ver) {
if (!ver) return FALSE;
BOOL allAlreadyApplied = TRUE;
for (SIZE_T i = 0; i < ver->patch_count; ++i) {
const Patch* p = &ver->patches[i];
if (!ReadAndCompareByte(hProc, moduleBase + p->offset, p->bytes)) {
allAlreadyApplied = FALSE;
break;
}
}
if (allAlreadyApplied) return TRUE;
for (SIZE_T i = 0; i < ver->patch_count; ++i) {
const Patch* p = &ver->patches[i];
if (!ReadAndCompareByte(hProc, moduleBase + p->offset, p->expect)) return FALSE;
}
for (SIZE_T i = 0; i < ver->patch_count; ++i) {
const Patch* p = &ver->patches[i];
if (!WriteByte(hProc, moduleBase + p->offset, p->bytes)) return FALSE;
}
return TRUE;
}
struct AOBResults {
u64* results;
SIZE_T count;
};
AOBResults AOBScanBuffer(const u8* buffer, SIZE_T size, u64 base,
const u8* pattern, SIZE_T pattern_len) {
AOBResults results = {0};
if (!buffer || size < pattern_len || pattern_len == 0 || pattern_len > MAX_PATTERN_BYTES)
return results;
SIZE_T cap = 4;
results.results = (u64*)LocalAlloc(LPTR, cap * sizeof(u64));
if (!results.results) return results;
const u8* bufEnd = buffer + size;
const u8* lastStart = bufEnd - pattern_len;
const u8 first = pattern[0];
const u8* p = buffer;
while (p <= lastStart) {
const u8* hit = (const u8*)memchr(p, first, (SIZE_T)(lastStart - p) + 1);
if (!hit) break;
BOOL match = TRUE;
for (SIZE_T j = 1; j < pattern_len; j++) {
if (pattern[j] != 0xCC && hit[j] != pattern[j]) { match = FALSE; break; }
}
if (match) {
if (results.count == cap) {
if (cap >= MAX_AOB_RESULTS) break;
SIZE_T newCap = cap * 2;
if (newCap > MAX_AOB_RESULTS) newCap = MAX_AOB_RESULTS;
u64* grown = (u64*)LocalAlloc(LPTR, newCap * sizeof(u64));
if (!grown) break;
RtlCopyMemory(grown, results.results, results.count * sizeof(u64));
LocalFree(results.results);
results.results = grown;
cap = newCap;
}
results.results[results.count++] = base + (u64)(hit - buffer);
}
p = hit + 1;
}
if (results.count == 0) { LocalFree(results.results); results.results = NULL; }
return results;
}
void FreeAOBResults(AOBResults* results) {
if (results && results->results) {
LocalFree(results->results);
results->results = NULL;
results->count = 0;
}
}
static void ToggleProcessThreads(HANDLE hProcess, BOOL suspend) {
DWORD pid = GetProcessId(hProcess);
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hSnap == INVALID_HANDLE_VALUE) return;
THREADENTRY32 te; te.dwSize = sizeof(te);
if (Thread32First(hSnap, &te)) {
do {
if (te.th32OwnerProcessID == pid) {
HANDLE hThread = OpenThread(THREAD_SUSPEND_RESUME, FALSE, te.th32ThreadID);
if (hThread) {
if (suspend) SuspendThread(hThread);
else ResumeThread(hThread);
CloseHandle(hThread);
}
}
} while (Thread32Next(hSnap, &te));
}
CloseHandle(hSnap);
}
void SuspendProcess(HANDLE hProcess) { ToggleProcessThreads(hProcess, TRUE); }
void ResumeProcess(HANDLE hProcess) { ToggleProcessThreads(hProcess, FALSE); }
static const u8 aob_pattern[] = {
0x48, 0x8B, 0x0D, 0xCC, 0xCC, 0xCC, 0xCC, 0x83, 0xB9, 0xE4, 0x00, 0x00, 0x00, 0x00, 0x75, 0x0C,
0xE8, 0xCC, 0xCC, 0xCC, 0xCC, 0x48, 0x8B, 0x0D, 0xCC, 0xCC, 0xCC, 0xCC, 0x48, 0x8B, 0x81, 0xB8,
0x00, 0x00, 0x00, 0x83, 0x38, 0x01, 0x74, 0x1E, 0x83, 0xB9, 0xE4, 0x00, 0x00, 0x00, 0x00, 0x75,
0x05, 0xE8, 0xCC, 0xCC, 0xCC, 0xCC, 0x33, 0xD2, 0xB9, 0xCC, 0xCC, 0xCC, 0xCC, 0x48, 0x83, 0xC4,
0x28, 0xE9, 0xCC, 0xCC, 0xCC, 0xCC, 0x48, 0x83, 0xC4, 0x28, 0xC3
};
static const SIZE_T aob_pattern_len = sizeof(aob_pattern);
//filled below, to avoid duplication, pattern_len, offset 0x39 (decimal 57), expect, bytes (patch)
static const PatchAOB aob_patch = { {0}, 0, 0x39, 0x01, 0x08 };
void FallbackAOB(HANDLE hProc, u64 moduleBase, SIZE_T moduleSize) {
if (!moduleSize) return;
SuspendProcess(hProc);
u8* buffer = (u8*)LocalAlloc(LMEM_FIXED, moduleSize);
if (buffer) {
SIZE_T bytesRead = 0;
if (ReadProcessMemory(hProc, (LPCVOID)moduleBase, buffer, moduleSize, &bytesRead)
&& bytesRead >= aob_pattern_len) {
AOBResults results = AOBScanBuffer(buffer, bytesRead, moduleBase,
aob_pattern, aob_pattern_len);
for (SIZE_T j = 0; j < results.count; j++) {
u64 addr = results.results[j] + aob_patch.offset;
if (ReadAndCompareByte(hProc, addr, aob_patch.bytes)) continue;
if (ReadAndCompareByte(hProc, addr, aob_patch.expect)) {
WriteByte(hProc, addr, aob_patch.bytes);
}
}
FreeAOBResults(&results);
}
LocalFree(buffer);
}
ResumeProcess(hProc);
}
static const Patch patches_v0_0_10[] = { { 0x54494EULL, 0x01, 0x08 } };
static const Patch patches_v0_0_11[] = { { 0x67B9EEULL, 0x01, 0x08 } };
static const Patch patches_v0_0_12[] = { { 0x6C863EULL, 0x01, 0x08 } };
static const Patch patches_v0_0_13[] = { { 0x6D28CEULL, 0x01, 0x08 } };
static const Version versions[] = {
{ patches_v0_0_10, sizeof(patches_v0_0_10) / sizeof(patches_v0_0_10[0]) },
{ patches_v0_0_11, sizeof(patches_v0_0_11) / sizeof(patches_v0_0_11[0]) },
{ patches_v0_0_12, sizeof(patches_v0_0_12) / sizeof(patches_v0_0_12[0]) },
{ patches_v0_0_13, sizeof(patches_v0_0_13) / sizeof(patches_v0_0_13[0]) }
};
static const SIZE_T versions_count = sizeof(versions) / sizeof(versions[0]);
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
_In_ PWSTR lpCmdLine, _In_ int nShowCmd) {
WCHAR selfDir[MAX_PATH] = {};
DWORD len = GetModuleFileNameW(NULL, selfDir, MAX_PATH);
if (len == 0 || len >= MAX_PATH) return 1;
WCHAR* lastSlash = wcsrchr(selfDir, L'\\');
if (!lastSlash) return 1;
*lastSlash = L'\0';
const WCHAR* exeName = L"Swords & Slippers Patreon.exe";
WCHAR gamePath[MAX_PATH] = {};
const WCHAR* sep = L"\\";
if (lstrlenW(selfDir) + lstrlenW(sep) + lstrlenW(exeName) >= MAX_PATH)
return 1;
lstrcpyW(gamePath, selfDir);
lstrcatW(gamePath, sep);
lstrcatW(gamePath, exeName);
STARTUPINFOW si = { sizeof(si) };
PROCESS_INFORMATION pi = {};
if (!CreateProcessW(gamePath, NULL, NULL, NULL, FALSE, 0, NULL, selfDir, &si, &pi)) {
WCHAR msg[256];
wsprintfW(msg, L"Failed to start %ls", exeName);
MessageBoxW(NULL, msg, L"Launcher", MB_OK | MB_ICONERROR);
return 1;
}
DWORD pid = GetProcessId(pi.hProcess);
u64 base = 0;
SIZE_T moduleSize = 0;
int waited = 0;
const int maxWait = 30000, step = 250;
while (waited < maxWait) {
if (WaitForSingleObject(pi.hProcess, 0) == WAIT_OBJECT_0) break;
if (GetModuleInfo(pid, L"GameAssembly.dll", &base, &moduleSize)) break;
Sleep(step); waited += step;
}
if (!base) { CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return 1; }
HANDLE hProc = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION | PROCESS_QUERY_INFORMATION, FALSE, pid);
if (!hProc) {
MessageBoxW(NULL, L"Failed to access game process", L"Launcher", MB_OK | MB_ICONERROR);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 1;
}
BOOL applied = FALSE;
for (SIZE_T i = 0; i < versions_count; ++i) {
if (PatchDirectAddresses(hProc, base, &versions[i])) { applied = TRUE; break; }
}
if (!applied) {
FallbackAOB(hProc, base, moduleSize);
}
CloseHandle(hProc);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}