r5sdk/r5launcher/main.cpp

109 lines
3.2 KiB
C++
Raw Normal View History

2021-04-13 04:45:22 -07:00
#include <stdio.h>
#include <Windows.h>
#include <detours.h>
void PrintLastError()
{
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
return;
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
printf("ERROR: %s\n", messageBuffer);
LocalFree(messageBuffer);
}
// "C:\\Program Files (x86)\\Origin Games\\Apex"
bool LaunchR5Apex()
{
FILE* sLaunchParams;
CHAR sArgumentSize[1024];
CHAR sCommandDirectory[MAX_PATH];
LPSTR sCommandLine = sCommandDirectory;
#pragma warning(suppress : 4996) // Temp since fopen_s() does not parse the arguments over for some reason.
sLaunchParams = fopen("launchparams.txt", "r"); // "+exec autoexec -dev -fnf -noplatform"
2021-04-13 04:45:22 -07:00
BOOL result;
CHAR sDevDll[MAX_PATH];
CHAR sGameExe[MAX_PATH];
CHAR sGameDirectory[MAX_PATH];
STARTUPINFO StartupInfo = { 0 };
PROCESS_INFORMATION ProcInfo = { 0 };
// Initialize the startup info structure.
StartupInfo.cb = sizeof(STARTUPINFO);
// Load command line arguments from a file on the disk.
if (sLaunchParams)
{
while (fgets(sArgumentSize, 1024, sLaunchParams) != NULL)
fclose(sLaunchParams);
}
2021-04-13 04:45:22 -07:00
// Format the file paths for the game exe and dll.
GetCurrentDirectory(MAX_PATH, sGameDirectory);
snprintf(sGameExe, sizeof(sGameExe), "%s\\r5apex.exe", sGameDirectory);
snprintf(sDevDll, sizeof(sDevDll), "%s\\r5dev.dll", sGameDirectory);
snprintf(sCommandLine, sizeof(sCommandDirectory), "%s\\r5apex.exe %s", sGameDirectory, sArgumentSize);
2021-04-13 04:45:22 -07:00
printf("Launching Apex Dev...\n");
printf(" - CWD: %s\n", sGameDirectory);
printf(" - EXE: %s\n", sGameExe);
printf(" - DLL: %s\n", sDevDll);
printf(" - CLI: %s\n", sCommandLine);
// Build our list of dlls to inject.
LPCSTR DllsToInject[1] =
{
sDevDll
};
// Create the game process in a suspended state with our dll.
result = DetourCreateProcessWithDllsA(
sGameExe, // lpApplicationName
sCommandLine, // lpCommandLine
NULL, // lpProcessAttributes
NULL, // lpThreadAttributes
FALSE, // bInheritHandles
CREATE_SUSPENDED, // dwCreationFlags
NULL, // lpEnvironment
sGameDirectory, // lpCurrentDirectory
&StartupInfo, // lpStartupInfo
&ProcInfo, // lpProcessInformation
1, // nDlls
DllsToInject, // rlpDlls
NULL // pfCreateProcessA
);
// Failed to create the game process.
if (!result)
{
PrintLastError();
return false;
}
// Resume the process.
ResumeThread(ProcInfo.hThread);
// Close the process and thread handles.
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
return true;
}
int main(int argc, char* argv[], char* envp[])
{
LaunchR5Apex();
return 0;
}