From 4911f8d9037a736d5c2dd1baa6976719d1467317 Mon Sep 17 00:00:00 2001 From: mkrsym1 Date: Fri, 4 Aug 2023 15:35:29 +0300 Subject: [PATCH] Implement restart flag --- game_payload/include/main.h | 2 + game_payload/src/main.c | 14 ++++++ injector/src/dll.c | 91 +++++++++++++++++++++---------------- 3 files changed, 68 insertions(+), 39 deletions(-) diff --git a/game_payload/include/main.h b/game_payload/include/main.h index 84ac1d2..89bb790 100644 --- a/game_payload/include/main.h +++ b/game_payload/include/main.h @@ -2,3 +2,5 @@ void unload_ctr_inc(); void unload_ctr_dec(); + +void request_restart(); diff --git a/game_payload/src/main.c b/game_payload/src/main.c index 0c4718d..0dc47de 100644 --- a/game_payload/src/main.c +++ b/game_payload/src/main.c @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -23,6 +24,19 @@ void unload_ctr_dec() { } } +void request_restart() { + HANDLE hRestartFlag = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, "Global\\JadeiteRestartFlag"); + int *restartFlag = MapViewOfFile(hRestartFlag, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(int)); + if (!restartFlag) { + msg_err_a("Could not map shared memory to set restart flag"); + } + + *restartFlag = 1; + + UnmapViewOfFile(restartFlag); + CloseHandle(hRestartFlag); +} + BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) { // Only listen to attach if (reason != DLL_PROCESS_ATTACH) { diff --git a/injector/src/dll.c b/injector/src/dll.c index 1b69181..c8b4a6e 100644 --- a/injector/src/dll.c +++ b/injector/src/dll.c @@ -61,51 +61,64 @@ BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) { MessageBoxW(NULL, L"Could not find wine_get_unix_file_name! Wine version too old?", J_MB_TITLE, MB_OK | MB_ICONWARNING); } - // Start the game - STARTUPINFOW si; - ZeroMemory(&si, sizeof(si)); - - PROCESS_INFORMATION pi; - si.cb = sizeof(si); - 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", GetLastError()); - MessageBoxW(NULL, message, J_MB_TITLE, MB_OK | MB_ICONERROR); - + // Create shared memory for the restart flag + HANDLE hRestartFlag = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(int), "Global\\JadeiteRestartFlag"); + int *restartFlag = MapViewOfFile(hRestartFlag, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(int)); + if (!restartFlag) { + MessageBoxW(NULL, L"Failed to create shared memory!", 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); + do { + // Start the game + STARTUPINFOW si; + ZeroMemory(&si, sizeof(si)); - // 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); - } + PROCESS_INFORMATION pi; + si.cb = sizeof(si); + ZeroMemory(&pi, sizeof(pi)); - // Resume the process - ResumeThread(pi.hThread); + 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", GetLastError()); + MessageBoxW(NULL, message, J_MB_TITLE, MB_OK | MB_ICONERROR); - // The launcher process should now hang untill the game terminates - WaitForSingleObject(pi.hProcess, INFINITE); + 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); + + // Clear the restart flag + *restartFlag = 0; + + // 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 (*restartFlag); return TRUE; }