122 lines
3.8 KiB
C
122 lines
3.8 KiB
C
#include <stdio.h>
|
|
|
|
#include <inject.h>
|
|
#include <envs.h>
|
|
|
|
#include <game_p.h>
|
|
|
|
typedef char *(*wgufn_t)(wchar_t* path); // wine_get_unix_file_name
|
|
|
|
const wchar_t *J_MB_TITLE = L"Jadeite Launcher Payload";
|
|
|
|
BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) {
|
|
// Only listen for attach
|
|
if (reason != DLL_PROCESS_ATTACH) {
|
|
return TRUE;
|
|
}
|
|
|
|
// Get target EXE path
|
|
wchar_t targetExe[MAX_PATH];
|
|
GetEnvironmentVariableW(ENV_EXE_PATH, targetExe, MAX_PATH);
|
|
|
|
// Get the path of the DLL to inject
|
|
wchar_t injectDll[MAX_PATH];
|
|
GetEnvironmentVariableW(ENV_DLL_PATH, injectDll, MAX_PATH);
|
|
|
|
// Get game commandline
|
|
wchar_t cmdline[8192];
|
|
GetEnvironmentVariableW(ENV_PROC_CMD, cmdline, sizeof(cmdline) / sizeof(wchar_t));
|
|
|
|
// Compute the working directory path
|
|
wchar_t workdir[MAX_PATH];
|
|
wcscpy(workdir, targetExe);
|
|
*(wcsrchr(workdir, L'\\')) = L'\0';
|
|
|
|
// SAFETY: verify that the injector is not inside the game directory
|
|
HMODULE kernel32 = GetModuleHandleA("kernel32.dll");
|
|
wgufn_t wine_get_unix_file_name = (wgufn_t)GetProcAddress(kernel32, "wine_get_unix_file_name");
|
|
|
|
if (wine_get_unix_file_name) {
|
|
char *unixInjectDll = wine_get_unix_file_name(injectDll);
|
|
char *unixWorkdir = wine_get_unix_file_name(workdir);
|
|
|
|
char *i = unixInjectDll, *w = unixWorkdir;
|
|
char startsWith = 0;
|
|
while (*i && *w) {
|
|
startsWith = *i == *w;
|
|
if (!startsWith) break;
|
|
|
|
i++, w++;
|
|
}
|
|
|
|
HANDLE heap = GetProcessHeap();
|
|
HeapFree(heap, 0, unixInjectDll);
|
|
HeapFree(heap, 0, unixWorkdir);
|
|
|
|
if (startsWith) {
|
|
MessageBoxW(NULL, L"Putting the patcher (or any other foreign PE binaries) inside the game directory is dangerous! Please move it elsewhere.", J_MB_TITLE, MB_OK | MB_ICONERROR);
|
|
exit(1);
|
|
}
|
|
} else {
|
|
MessageBoxW(NULL, L"Could not find wine_get_unix_file_name! Wine version too old?", J_MB_TITLE, MB_OK | MB_ICONWARNING);
|
|
}
|
|
|
|
// Get restart flag file path
|
|
wchar_t restartFlagFile[MAX_PATH];
|
|
GetTempPathW(MAX_PATH, restartFlagFile);
|
|
wcscat(restartFlagFile, L"jadeite\\restart_flag");
|
|
|
|
do {
|
|
// Start the game
|
|
STARTUPINFOW si;
|
|
ZeroMemory(&si, sizeof(si));
|
|
si.cb = sizeof(si);
|
|
|
|
PROCESS_INFORMATION pi;
|
|
ZeroMemory(&pi, sizeof(pi));
|
|
|
|
if (!CreateProcessW(
|
|
NULL,
|
|
cmdline,
|
|
NULL,
|
|
NULL,
|
|
FALSE,
|
|
CREATE_SUSPENDED,
|
|
NULL,
|
|
workdir,
|
|
&si,
|
|
&pi
|
|
)) {
|
|
wchar_t message[1024];
|
|
wsprintfW(message, L"Failed to start game process: %ld\nGame executable path: '%ls'", GetLastError(), targetExe);
|
|
MessageBoxW(NULL, message, J_MB_TITLE, MB_OK | MB_ICONERROR);
|
|
|
|
exit(1);
|
|
}
|
|
|
|
// Inject
|
|
void *payloadStart = &_binary_game_p_o_p_game_p_bin_start;
|
|
size_t payloadSize = (size_t)&_binary_game_p_o_p_game_p_bin_size;
|
|
inject(pi.hProcess, payloadStart, payloadSize, injectDll);
|
|
|
|
// Remove the restart flag file
|
|
DeleteFileW(restartFlagFile);
|
|
|
|
// Optional: wait for user input before resuming (useful for debugging)
|
|
char *waitEnabled = getenv("WAIT_BEFORE_RESUME");
|
|
if (waitEnabled && *waitEnabled) {
|
|
wchar_t message[64];
|
|
wsprintfW(message, L"PID: %ld. Press OK to continue", pi.dwProcessId);
|
|
MessageBoxW(NULL, message, J_MB_TITLE, MB_OK | MB_ICONINFORMATION);
|
|
}
|
|
|
|
// Resume the process
|
|
ResumeThread(pi.hThread);
|
|
|
|
// The launcher process should now hang untill the game terminates
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
|
} while (GetFileAttributesW(restartFlagFile) != INVALID_FILE_ATTRIBUTES);
|
|
|
|
return TRUE;
|
|
}
|