-
Notifications
You must be signed in to change notification settings - Fork 0
/
idLauncher.cpp
562 lines (462 loc) · 17.8 KB
/
idLauncher.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// internal includes
#include "idLauncher.h"
namespace fs = boost::filesystem;
const char MainModuleName[] = "DOOMEternalx64vk.exe";
INIReader reader(CONFIG_INI);
void Success() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << "Success!" << endl;
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
void Failed() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << "Failed!" << endl;
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
char* ScanIn(const char* pattern, const char* mask, char* begin, unsigned int size)
{
unsigned int patternLength = strlen(mask);
for (unsigned int i = 0; i < size - patternLength; i++)
{
bool found = true;
for (unsigned int j = 0; j < patternLength; j++)
{
if (mask[j] != '?' && pattern[j] != *(begin + i + j))
{
found = false;
break;
}
}
if (found)
{
return (begin + i);
}
}
return nullptr;
}
char* ScanEx(const char* pattern, const char* mask, char* begin, char* end, HANDLE hProc)
{
char* currentChunk = begin;
char* match = nullptr;
SIZE_T bytesRead;
while (currentChunk < end)
{
MEMORY_BASIC_INFORMATION mbi;
//return nullptr if VirtualQuery fails
if (!VirtualQueryEx(hProc, currentChunk, &mbi, sizeof(mbi)))
{
return nullptr;
}
char* buffer = new char[mbi.RegionSize];
if (mbi.State == MEM_COMMIT && mbi.Protect != PAGE_NOACCESS)
{
DWORD oldprotect;
if (VirtualProtectEx(hProc, mbi.BaseAddress, mbi.RegionSize, PAGE_EXECUTE_READWRITE, &oldprotect))
{
ReadProcessMemory(hProc, mbi.BaseAddress, buffer, mbi.RegionSize, &bytesRead);
VirtualProtectEx(hProc, mbi.BaseAddress, mbi.RegionSize, oldprotect, &oldprotect);
char* internalAddress = ScanIn(pattern, mask, buffer, bytesRead);
if (internalAddress != nullptr)
{
//calculate from internal to external
uintptr_t offsetFromBuffer = internalAddress - buffer;
match = currentChunk + offsetFromBuffer;
delete[] buffer;
break;
}
}
}
currentChunk = currentChunk + mbi.RegionSize;
delete[] buffer;
}
return match;
}
HMODULE GetMainModuleHandle(HANDLE hProcess, const char* modName, LPMODULEINFO mi) {
// get process modules
HMODULE hMods[2048];
DWORD cbNeeded;
DWORD modCnt = 0;
while (true) {
EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded);
modCnt = cbNeeded / sizeof(HMODULE);
if (modCnt > 0)
break;
Sleep(10);
}
// iterate through each module
// MODULEINFO mi;
for (unsigned int i = 0; i < modCnt; i++) {
char szModName[MAX_PATH];
if (!GetModuleFileNameExA(hProcess, hMods[i], szModName, sizeof(szModName) / sizeof(TCHAR))) {
cout << "GetModuleFileNameExA failed!" << endl;
CloseHandle(hProcess);
return NULL;
}
if (strstr(szModName, modName) == NULL)
continue;
// get module information
if (!GetModuleInformation(hProcess, hMods[i], mi, sizeof(MODULEINFO))) {
cout << "GetModuleInformation failed!" << endl;
CloseHandle(hProcess);
return NULL;
}
return hMods[i];
}
return NULL;
}
BOOL PatchAddressEx(HANDLE hProcess, const char* pattern, const char* patch, const char* mask, char* start, DWORD size) {
SIZE_T bw;
PBYTE pbAddr = (PBYTE)ScanEx(pattern, mask, start, start + size, hProcess);
if (pbAddr == NULL)
return FALSE;
if (!WriteProcessMemory(hProcess, pbAddr, patch, strlen(mask), &bw))
return FALSE;
if (bw != strlen(mask))
return FALSE;
return TRUE;
}
HANDLE GetProcessByName(const char* procName, LPMODULEINFO mi, PDWORD procId) {
DWORD aProcesses[1024];
DWORD cbNeeded;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded)) {
cout << "EnumProcesses failed!" << endl;
}
DWORD cntProcesses = cbNeeded / sizeof(DWORD);
for (DWORD i = 0; i < cntProcesses; i++) {
DWORD procIdTmp = aProcesses[i];
if (procIdTmp == 0)
continue;
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procIdTmp);
if (hProcess == NULL)
continue;
HMODULE hMod;
DWORD cbNeeded;
char szProcessName[MAX_PATH];
if (!EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
{
CloseHandle(hProcess);
continue;
}
if (GetModuleBaseNameA(hProcess, hMod, szProcessName, MAX_PATH) == 0) {
CloseHandle(hMod);
CloseHandle(hProcess);
continue;
}
if (strstr(szProcessName, procName) == NULL) {
CloseHandle(hMod);
CloseHandle(hProcess);
continue;
}
if (!GetModuleInformation(hProcess, hMod, mi, sizeof(MODULEINFO))) {
cout << "GetModuleInformation failed!" << endl;
CloseHandle(hMod);
CloseHandle(hProcess);
continue;
}
CloseHandle(hMod);
*procId = procIdTmp;
return hProcess;
}
return NULL;
}
void SetProcessState(DWORD procId, ProcessState ps) {
// put all threads to sleep or wake them up
HANDLE hThreadSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
THREADENTRY32 threadEntry;
threadEntry.dwSize = sizeof(THREADENTRY32);
Thread32First(hThreadSnapshot, &threadEntry);
do
{
if (threadEntry.th32OwnerProcessID == procId) {
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, threadEntry.th32ThreadID);
if (ps == PROCESS_STATE_SLEEP) {
SuspendThread(hThread);
} else if (ps == PROCESS_STATE_WAKE) {
ResumeThread(hThread);
}
CloseHandle(hThread);
}
} while (Thread32Next(hThreadSnapshot, &threadEntry));
CloseHandle(hThreadSnapshot);
}
vector<string> GetFileList(fs::path path)
{
vector<string> m_file_list;
fs::directory_iterator end;
for (fs::directory_iterator i(path); i != end; ++i)
{
const fs::path cp = (*i);
m_file_list.push_back(cp.string());
}
return m_file_list;
}
BOOL IsSteam(fs::path path) {
vector<string> files = GetFileList(path);
int i = 0;
for (auto it = files.begin(); it != files.end(); it++, i++) {
if ((*it).find_first_of("steam_api64.dll") != string::npos)
return TRUE;
}
return FALSE;
}
/* char* GetDoomArgs(int argc, char* argv[]) {
// argument to start on
int startArg = 2;
// calculate argument size
int argSize = 0;
for (int i = startArg; i < argc; i++) {
argSize += strlen(argv[i]);
}
// calculate the allocation size for all arguments plus spaces
int allocSize = (argSize + (argc - startArg)) - 1;
// allocate a buffer for all the arguments plus the spaces
char* argComb = (char*)malloc(allocSize);
// set all bytes to a ' ' character
memset(argComb, 0x20, allocSize);
// add null terminator
argComb[allocSize] = 0;
// index into the argComb buffer
int idx = 0;
for (int i = startArg; i < argc; i++) {
// copy the arguments into the argComb buffer then skip a character
memcpy(argComb + idx, argv[i], strlen(argv[i]));
// argument size plus skip one for the space
idx += strlen(argv[i]) + 1;
}
// return it
return argComb;
} */
int Launch(const char* filename, const char* args = NULL) {
STARTUPINFOA si;
PROCESS_INFORMATION pi;
MODULEINFO mi;
DWORD procId;
HANDLE hProcess = 0;
HANDLE hThread = 0;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// create the process
fs::path p(filename);
p = fs::absolute(p);
// check if this is a Steam installation
BOOL steam = IsSteam(p.parent_path());
// get the working directory
string dir = p.parent_path().string();
// steam://rungameid/782330
// recreate the arguments
if (args == NULL) {
cout << "Startup directory: \"" << dir << "\"" << endl;
if (!CreateProcessA(NULL, /* (char*)p.string().c_str() */ (char*)STEAM_LAUNCH_URI, NULL, NULL, FALSE, 0, NULL, dir.c_str(), &si, &pi)) {
cout << "CreateProcess failed!" << endl;
return 1;
}
} else {
string fullPath = p.string();
fullPath += " ";
fullPath += args;
cout << "Startup directory: \"" << dir << "\"" << endl;
if (!CreateProcessA(NULL, (char*)fullPath.c_str(), NULL, NULL, FALSE, 0, NULL, dir.c_str(), &si, &pi)) {
cout << "CreateProcess failed!" << endl;
return 1;
}
}
if (steam) {
cout << "Steam detected!" << endl;
WaitForSingleObject(pi.hProcess, INFINITE);
cout << "Process restarted under Steam..." << endl;
cout << "Waiting on process to start back under a different process ID..." << endl;
while (true) {
hProcess = GetProcessByName(MainModuleName, &mi, &procId);
if (hProcess != NULL && procId != pi.dwProcessId)
break;
// Sleep(40);
}
cout << "Found new process with ID " << procId << "!" << endl;
} else {
cout << "no-DRM or Bethesda.net detected!" << endl;
hProcess = pi.hProcess;
hThread = pi.hThread;
procId = pi.dwProcessId;
HMODULE hMainMod = GetMainModuleHandle(hProcess, MainModuleName, &mi);
if (hMainMod == NULL) {
cout << "GetMainModuleHandle failed!" << endl;
TerminateProcess(hProcess, 1);
return 1;
}
CloseHandle(hMainMod);
}
// suspend while patching
cout << "Suspending..." << endl;
SetProcessState(procId, PROCESS_STATE_SLEEP);
DWORD bw;
if (reader.ParseError() != 0) {
cout << "Generating default \"" << CONFIG_INI << "\"" << endl;
HANDLE hFile = CreateFileA(CONFIG_INI, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(hFile, DEFAULT_INI, strlen(DEFAULT_INI), &bw, NULL);
CloseHandle(hFile);
INIReader reader(CONFIG_INI);
}
cout << "Reading \"" << CONFIG_INI << "\"..." << endl;
cout << "Patching..." << endl;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// unsigned build-manifest.bin patch
if (reader.GetBoolean("patches", "UnsignedManifest", true)) {
cout << "Applying unsigned manifest patch..." << endl;
if(PatchAddressEx(hProcess, "\x48\x83\xEC\x28\x49\x8B", "\xB8\x01\x00\x00\x00\xC3", "xxxxxx", (char*)mi.lpBaseOfDll, mi.SizeOfImage))
Success();
else
Failed();
}
// skip data checksum checks
/* if (reader.GetBoolean("patches", "ChecksumChecks", true)) {
cout << "Applying checksum check patch..." << endl;
if (PatchAddressEx(hProcess, "\x74\x1E\x8B\x53\x48\x41\xB8\xEF\xBE\xAD\xDE", "\xEB\x1E\x8B\x53\x48\x41\xB8\xEF\xBE\xAD\xDE", "xxxxxxxxxxx", (char*)mi.lpBaseOfDll, mi.SizeOfImage)) {
Success();
} else {
Failed();
}
} */
// skip checking against hashes inside build-manifest.bin
if (reader.GetBoolean("patches", "ManifestHashes", true)) {
cout << "Applying manifest hashes patch..." << endl;
if(PatchAddressEx(hProcess, "\x74\x20\x48\x8B\x07\x48\x8B\xCF\xFF\x50", "\xEB\x20\x48\x8B\x07\x48\x8B\xCF\xFF\x50", "xxxxxxxxxx", (char*)mi.lpBaseOfDll, mi.SizeOfImage))
Success();
else
Failed();
}
// skip checking against filesizes inside build-manifest.bin
if (reader.GetBoolean("patches", "ManifestSizes", true)) {
cout << "Applying manifest sizes patch..." << endl;
if (PatchAddressEx(hProcess, "\xFF\x50\x68\x48\x3B\xC5\x74\x43", "\xFF\x50\x68\x48\x89\xC5\xEB\x43", "xxxxxxxx", (char*)mi.lpBaseOfDll, mi.SizeOfImage))
Success();
else
Failed();
}
/*
local gameProcess = "DOOMEternalx64vk.exe"
local gameModule = getAddress( gameProcess )
local t = aobscanex( "4C8B0EBA01000000488BCE448BF041FF51??8D", nil, nil, nil, gameModule, gameModule + getModuleSize( gameProcess ) )
t = tonumber( t[0], 16 ) + 0x3
unregisterSymbol( "bRestricted_ConsoleType" )
registerSymbol( "bRestricted_ConsoleType", t, true )
t = aobscanex( "4C8B0FBA01000000488BCF448BF041FF51??4C6BC507", nil, nil, nil, gameModule, gameModule + getModuleSize( gameProcess ) )
t = tonumber( t[0], 16 ) + 0x3
unregisterSymbol( "bRestricted_KeyPress" )
registerSymbol( "bRestricted_KeyPress", t, true )
*/
// unrestrict binds #1 and #2
if (reader.GetBoolean("patches", "UnrestrictCvarsAndBinds", true)) {
cout << "Applying unrestrict binds patches..." << endl;
BOOL ret0 = PatchAddressEx(hProcess,
"\x4C\x8B\x0E\xBA\x01\x00\x00\x00\x48\x8B\xCE\x44\x8B\xF0\x41\xFF\x51\x00\x8D",
"\x4C\x8B\x0E\xBA\x00\x00\x00\x00\x48\x8B\xCE\x44\x8B\xF0\x41\xFF\x51\x00\x8D",
"xxxxxxxxxxxxx?xx", (char*)mi.lpBaseOfDll, mi.SizeOfImage);
BOOL ret1 = PatchAddressEx(hProcess,
"\x4C\x8B\x0F\xBA\x01\x00\x00\x00\x48\x8B\xCF\x44\x8B\xF0\x41\xFF\x51\x00\x4C\x6B\xC5\x07",
"\x4C\x8B\x0F\xBA\x00\x00\x00\x00\x48\x8B\xCF\x44\x8B\xF0\x41\xFF\x51\x00\x4C\x6B\xC5\x07",
"xxxxxxxxxxxxxxxxx?xxxx", (char*)mi.lpBaseOfDll, mi.SizeOfImage);
if (ret0)
Success();
else
Failed();
if (ret1)
Success();
else
Failed();
}
// block HTTP requests
/* if (reader.GetBoolean("patches", "BlockHTTP", true)) {
cout << "Applying block HTTP patch..." << endl;
if(PatchAddressEx(hProcess, "\xE8\x62\xFE\xFF\xFF\x0F\xB6\xC0", "\xB0\x01\x48\x83\xC4\x20\x5B\xC3", "xxxxxxxx", (char*)mi.lpBaseOfDll, mi.SizeOfImage)) {
Success();
} else {
Failed();
}
} */
// resource_loadMostRecent
if (reader.GetBoolean("patches", "resource_loadMostRecent", true)) {
cout << "Applying resource_loadMostRecent patch..." << endl;
BOOL ret0 = PatchAddressEx(hProcess, "\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\x40\x53\x48\x83\xEC\x50\x48\x8B\x84\x24\x88\x00\x00\x00\x48\x8B", "\x41\x83\xC9\x10\x53\xEB\x03\x90\xEB\xF6\x48\x83\xEC\x50\x48\x8B\x84\x24\x88\x00\x00\x00\x48\x8B", "xxxxxxxxxxxxxxxxxxxxxxxx", (char*)mi.lpBaseOfDll, mi.SizeOfImage);
BOOL ret1 = PatchAddressEx(hProcess, "\x4C\x8D\x05\x3A\xE8\x37\x02", "\x4C\x8D\x05\x56\xEE\x37\x02", "xxxxxxx", (char*)mi.lpBaseOfDll, mi.SizeOfImage);
if(ret0)
Success();
else
Failed();
if(ret1)
Success();
else
Failed();
}
cout << "Resuming..." << endl;
SetProcessState(procId, PROCESS_STATE_WAKE);
cout << "Cleaning up..." << endl;
// close process handles
CloseHandle(hThread);
CloseHandle(hProcess);
// wait on user input
if (!reader.GetBoolean("config", "AutoExit", true)) {
cout << "Press \"ENTER\" to exit..." << endl;
cin.get();
}
return 0;
}
auto ReadFile(string_view path) -> string {
constexpr auto read_size = size_t(4096);
auto stream = fs::ifstream(path.data());
stream.exceptions(ios_base::badbit);
auto out = string();
auto buf = string(read_size, '\0');
while (stream.read(&buf[0], read_size)) {
out.append(buf, 0, stream.gcount());
}
out.append(buf, 0, stream.gcount());
return out;
}
// "D:\Program Files (x86)\Steam\steamapps\common\DOOMEternal\DOOMEternalx64vk.exe" "+com_skipIntroVideo 1 +g_infiniteAmmo 1 +crucible_CanUseInfiniteAmmoCheat 1 +p_infiniteBloodPunch 1"
int main(int argc, char* argv[])
{
/* if (argc == 1) { // without executable path and arguments
fs::path p(argv[0]);
cout << "Usage: " << p.filename().string() << " <executable path> [arguments]" << endl;
return 0;
} else if (argc == 2) { // executable path without arguments
cout << "Launching without arguments..." << endl;
return Launch(argv[1]);
} else if (argc >= 3) { // executable path with arguments
char* doomArgs = GetDoomArgs(argc, argv);
cout << "Launching with arguments: \"" << doomArgs << "\"..." << endl;
return Launch(argv[1], doomArgs);
} */
po::options_description desc("Options");
desc.add_options()
("help,h", "Display the help message")
// ("path", po::value<string>()->required(), "Doom Eternal executable path")
("argfile", po::value<string>()->zero_tokens()->composing(), "Doom Eternal argument file");
po::positional_options_description pos_desc;
// pos_desc.add("path", 1);
pos_desc.add("argfile", 1);
po::command_line_parser parser{ argc, argv };
parser.options(desc).positional(pos_desc).allow_unregistered();
po::parsed_options parsed_options = parser.run();
po::variables_map vm;
po::store(parsed_options, vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << endl;
return 0;
}
string path = reader.Get("config", "Path", "");
if (path != "") {
if (vm.count("argfile")) { // arguments
string doomArgs = ReadFile(vm["argfile"].as<string>());
boost::replace_all(doomArgs, "\r\n", " ");
boost::replace_all(doomArgs, "\n", " ");
Launch(path.c_str(), doomArgs.c_str());
} else { // no arguments
Launch(path.c_str());
}
}
}