-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathMain.cpp
488 lines (385 loc) · 13.4 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
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
/*
* This file is part of the CitizenFX project - http://citizen.re/
*
* See LICENSE and MENTIONS in the root of the source tree for information
* regarding licensing.
*/
#include "StdInc.h"
#include "CitizenGame.h"
#include <io.h>
#include <fcntl.h>
#include <CfxState.h>
#include <HostSharedData.h>
#include <array>
#include <shellscalingapi.h>
#include <shobjidl.h>
extern "C" BOOL WINAPI _CRT_INIT(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved);
void InitializeDummies();
void EnsureGamePath();
bool InitializeExceptionHandler();
std::map<std::string, std::string> UpdateGameCache();
#pragma comment(lib, "version.lib")
std::map<std::string, std::string> g_redirectionData;
extern "C" int wmainCRTStartup();
void DoPreLaunchTasks();
void NVSP_DisableOnStartup();
bool ExecutablePreload_Init();
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
bool toolMode = false;
if (getenv("CitizenFX_ToolMode"))
{
toolMode = true;
}
if (!toolMode)
{
// bootstrap the game
if (Bootstrap_RunInit())
{
return 0;
}
}
// delete any old .exe.new file
_unlink("CitizenFX.exe.new");
// path environment appending of our primary directories
static wchar_t pathBuf[32768];
GetEnvironmentVariable(L"PATH", pathBuf, sizeof(pathBuf));
std::wstring newPath = MakeRelativeCitPath(L"bin") + L";" + MakeRelativeCitPath(L"") + L";" + std::wstring(pathBuf);
SetEnvironmentVariable(L"PATH", newPath.c_str());
SetDllDirectory(MakeRelativeCitPath(L"bin").c_str()); // to prevent a) current directory DLL search being disabled and b) xlive.dll being taken from system if not overridden
if (!toolMode)
{
SetCurrentDirectory(MakeRelativeCitPath(L"").c_str());
}
auto addDllDirectory = (decltype(&AddDllDirectory))GetProcAddress(GetModuleHandle(L"kernel32.dll"), "AddDllDirectory");
auto setDefaultDllDirectories = (decltype(&SetDefaultDllDirectories))GetProcAddress(GetModuleHandle(L"kernel32.dll"), "SetDefaultDllDirectories");
// don't set DLL directories for ros:legit under Windows 7
// see https://connect.microsoft.com/VisualStudio/feedback/details/2281687/setdefaultdlldirectories-results-in-exception-during-opening-a-winform-on-win7
if (!IsWindows8OrGreater() && toolMode && wcsstr(GetCommandLineW(), L"ros:legit"))
{
setDefaultDllDirectories = nullptr;
}
if (addDllDirectory && setDefaultDllDirectories)
{
setDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_USER_DIRS);
addDllDirectory(MakeRelativeCitPath(L"").c_str());
addDllDirectory(MakeRelativeCitPath(L"bin").c_str());
}
// determine dev mode and do updating
wchar_t exeName[512];
GetModuleFileName(GetModuleHandle(NULL), exeName, sizeof(exeName) / 2);
wchar_t* exeBaseName = wcsrchr(exeName, L'\\');
exeBaseName[0] = L'\0';
exeBaseName++;
bool devMode = toolMode;
if (GetFileAttributes(va(L"%s.formaldev", exeBaseName)) != INVALID_FILE_ATTRIBUTES)
{
devMode = true;
}
// don't allow running a subprocess executable directly
if (MakeRelativeCitPath(L"").find(L"cache\\subprocess") != std::string::npos)
{
return 0;
}
SetCurrentProcessExplicitAppUserModelID(L"CitizenFX.FiveM.Client");
static HostSharedData<CfxState> initState("CfxInitState");
// check if the master process still lives
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, initState->initialPid);
if (hProcess == nullptr)
{
initState->initialPid = GetCurrentProcessId();
}
else
{
DWORD exitCode = STILL_ACTIVE;
GetExitCodeProcess(hProcess, &exitCode);
if (exitCode != STILL_ACTIVE)
{
initState->initialPid = GetCurrentProcessId();
}
CloseHandle(hProcess);
}
}
// if not the master process, force devmode
if (!devMode)
{
devMode = !initState->IsMasterProcess();
}
if (!devMode)
{
if (!Bootstrap_DoBootstrap())
{
return 0;
}
}
if (InitializeExceptionHandler())
{
return 0;
}
// load global dinput8.dll over any that might exist in the game directory
{
wchar_t systemPath[512];
GetSystemDirectory(systemPath, _countof(systemPath));
wcscat_s(systemPath, L"\\dinput8.dll");
LoadLibrary(systemPath);
}
LoadLibrary(MakeRelativeCitPath(L"dinput8.dll").c_str());
LoadLibrary(MakeRelativeCitPath(L"steam_api64.dll").c_str());
// laod V8 DLLs in case end users have these in a 'weird' directory
LoadLibrary(MakeRelativeCitPath(L"v8_libplatform.dll").c_str());
LoadLibrary(MakeRelativeCitPath(L"v8_libbase.dll").c_str());
LoadLibrary(MakeRelativeCitPath(L"v8.dll").c_str());
// assign us to a job object
if (initState->IsMasterProcess())
{
// set DPI-aware
HMODULE shCore = LoadLibrary(L"shcore.dll");
if (shCore)
{
auto SetProcessDpiAwareness = (decltype(&::SetProcessDpiAwareness))GetProcAddress(shCore, "SetProcessDpiAwareness");
if (SetProcessDpiAwareness)
{
SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
}
}
// delete crashometry
_wunlink(MakeRelativeCitPath(L"cache\\crashometry").c_str());
if (GetFileAttributesW(MakeRelativeCitPath(L"permalauncher").c_str()) == INVALID_FILE_ATTRIBUTES)
{
// create job
HANDLE hJob = CreateJobObject(nullptr, nullptr);
if (hJob)
{
if (AssignProcessToJobObject(hJob, GetCurrentProcess()))
{
JOBOBJECT_EXTENDED_LIMIT_INFORMATION info = {};
info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
if (SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, &info, sizeof(info)))
{
initState->inJobObject = true;
}
}
}
}
}
// exit if not in a job object
if (initState->inJobObject)
{
BOOL result;
IsProcessInJob(GetCurrentProcess(), nullptr, &result);
if (!result)
{
// and if this isn't a subprocess
wchar_t fxApplicationName[MAX_PATH];
GetModuleFileName(GetModuleHandle(nullptr), fxApplicationName, _countof(fxApplicationName));
if (wcsstr(fxApplicationName, L"subprocess") == nullptr)
{
// and not a fivem:// protocol handler
if (wcsstr(GetCommandLineW(), L"fivem://") == nullptr)
{
return 0;
}
}
}
}
if (initState->IsMasterProcess())
{
DoPreLaunchTasks();
}
// make sure the game path exists
EnsureGamePath();
if (addDllDirectory)
{
addDllDirectory(MakeRelativeGamePath(L"").c_str());
}
if (!toolMode)
{
if (OpenMutex(SYNCHRONIZE, FALSE, L"CitizenFX_LogMutex") == nullptr)
{
// create the mutex
CreateMutex(nullptr, TRUE, L"CitizenFX_LogMutex");
// rotate any CitizenFX.log files cleanly
const int MaxLogs = 10;
auto makeLogName = [] (int idx)
{
return MakeRelativeCitPath(va(L"CitizenFX.log%s", (idx == 0) ? L"" : va(L".%d", idx)));
};
for (int i = (MaxLogs - 1); i >= 0; i--)
{
std::wstring logPath = makeLogName(i);
std::wstring newLogPath = makeLogName(i + 1);
if ((i + 1) == MaxLogs)
{
_wunlink(logPath.c_str());
}
else
{
_wrename(logPath.c_str(), newLogPath.c_str());
}
}
// also do checks here to complain at BAD USERS
if (!GetProcAddress(GetModuleHandle(L"kernel32.dll"), "SetThreadDescription")) // kernel32 forwarder only got this export in 1703, kernelbase.dll got this in 1607.
{
std::wstring fpath = MakeRelativeCitPath(L"CitizenFX.ini");
bool showOSWarning = true;
if (GetFileAttributes(fpath.c_str()) != INVALID_FILE_ATTRIBUTES)
{
showOSWarning = (GetPrivateProfileInt(L"Game", L"DisableOSVersionCheck", 0, fpath.c_str()) != 1);
}
if (showOSWarning)
{
MessageBox(nullptr, L"You are currently using an outdated version of Windows. This may lead to issues using the FiveM client. Please update to Windows 10 version 1703 (\"Creators Update\") or higher in case you are experiencing "
L"any issues. The game will continue to start now.", L"FiveM", MB_OK | MB_ICONWARNING);
}
}
#ifndef _DEBUG
{
HANDLE hToken;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
{
TOKEN_ELEVATION_TYPE elevationData;
DWORD size;
if (GetTokenInformation(hToken, TokenElevationType, &elevationData, sizeof(elevationData), &size))
{
if (elevationData == TokenElevationTypeFull)
{
const wchar_t* elevationComplaint = L"FiveM does not support running under elevated privileges. Please change your Windows settings to not run FiveM as administrator.\nThat won't fix anything. The game will exit now.";
auto result = MessageBox(nullptr, elevationComplaint, L"FiveM", MB_ABORTRETRYIGNORE | MB_ICONERROR);
if (result == IDIGNORE)
{
MessageBox(nullptr, L"No, you can't ignore this. The game will exit now.", L"FiveM", MB_OK | MB_ICONINFORMATION);
}
else if (result == IDRETRY)
{
MessageBox(nullptr, elevationComplaint, L"FiveM", MB_OK | MB_ICONWARNING);
}
return 0;
}
}
CloseHandle(hToken);
}
}
#endif
{
HANDLE hFile = CreateFile(MakeRelativeCitPath(L"writable_test").c_str(), GENERIC_WRITE, FILE_SHARE_DELETE, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_ACCESS_DENIED)
{
MessageBox(nullptr, L"FiveM could not create a file in the folder it is placed in. Please move your installation out of Program Files or another protected folder.", L"Error", MB_OK | MB_ICONSTOP);
return 0;
}
}
else
{
CloseHandle(hFile);
}
}
}
}
NVSP_DisableOnStartup();
// readd the game path into the PATH
newPath = MakeRelativeCitPath(L"bin\\crt") + L";" + MakeRelativeCitPath(L"bin") + L";" + MakeRelativeCitPath(L"") + L";" + MakeRelativeGamePath(L"") + L"; " + std::wstring(pathBuf);
SetEnvironmentVariable(L"PATH", newPath.c_str());
if (!toolMode)
{
SetCurrentDirectory(MakeRelativeGamePath(L"").c_str());
}
#ifdef GTA_NY
// initialize TLS variable so we get a TLS directory
InitializeDummies();
#endif
// check stuff regarding the game executable
std::wstring gameExecutable = MakeRelativeGamePath(GAME_EXECUTABLE);
if (GetFileAttributes(gameExecutable.c_str()) == INVALID_FILE_ATTRIBUTES)
{
MessageBox(nullptr, L"Could not find the game executable (" GAME_EXECUTABLE L") at the configured path. Please check your CitizenFX.ini file.", PRODUCT_NAME, MB_OK | MB_ICONERROR);
return 0;
}
#ifdef GTA_FIVE
if (!ExecutablePreload_Init())
{
return 0;
}
// ensure game cache is up-to-date, and obtain redirection metadata from the game cache
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter;
auto redirectionData = UpdateGameCache();
g_redirectionData = redirectionData;
gameExecutable = converter.from_bytes(redirectionData["GTA5.exe"]);
{
DWORD versionInfoSize = GetFileVersionInfoSize(gameExecutable.c_str(), nullptr);
if (versionInfoSize)
{
std::vector<uint8_t> versionInfo(versionInfoSize);
if (GetFileVersionInfo(gameExecutable.c_str(), 0, versionInfo.size(), &versionInfo[0]))
{
void* fixedInfoBuffer;
UINT fixedInfoSize;
VerQueryValue(&versionInfo[0], L"\\", &fixedInfoBuffer, &fixedInfoSize);
VS_FIXEDFILEINFO* fixedInfo = reinterpret_cast<VS_FIXEDFILEINFO*>(fixedInfoBuffer);
if ((fixedInfo->dwFileVersionLS >> 16) != 1604)
{
MessageBox(nullptr, va(L"The found GTA executable (%s) has version %d.%d.%d.%d, but only 1.0.1604.0 is currently supported. Please obtain this version, and try again.",
gameExecutable.c_str(),
(fixedInfo->dwFileVersionMS >> 16),
(fixedInfo->dwFileVersionMS & 0xFFFF),
(fixedInfo->dwFileVersionLS >> 16),
(fixedInfo->dwFileVersionLS & 0xFFFF)), PRODUCT_NAME, MB_OK | MB_ICONERROR);
return 0;
}
}
}
}
#endif
if (!toolMode)
{
// game launcher initialization
CitizenGame::Launch(gameExecutable, true);
}
else
{
HMODULE coreRT = LoadLibrary(MakeRelativeCitPath(L"CoreRT.dll").c_str());
if (coreRT)
{
auto toolProc = (void(*)())GetProcAddress(coreRT, "ToolMode_Init");
if (toolProc)
{
auto gameFunctionProc = (void(*)(void(*)(const wchar_t*)))GetProcAddress(coreRT, "ToolMode_SetGameFunction");
if (gameFunctionProc)
{
static auto gameExecutableStr = gameExecutable;
gameFunctionProc([] (const wchar_t* customExecutable)
{
if (customExecutable == nullptr)
{
SetCurrentDirectory(MakeRelativeGamePath(L"").c_str());
if (OpenMutex(SYNCHRONIZE, FALSE, L"CitizenFX_GameMutex") == nullptr)
{
CreateMutex(nullptr, TRUE, L"CitizenFX_GameMutex");
CitizenGame::Launch(gameExecutableStr);
}
}
else
{
CitizenGame::Launch(customExecutable);
}
});
}
CitizenGame::SetCoreMapping();
toolProc();
}
else
{
printf("Could not find ToolMode_Init in CoreRT.dll.\n");
}
}
else
{
printf("Could not initialize CoreRT.dll.\n");
}
}
return 0;
}
extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 1;
extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 1;