Files
jadeite/game_payload/src/main.c

107 lines
2.5 KiB
C
Raw Normal View History

2023-06-06 00:23:08 +03:00
#include <windows.h>
#include <ntdll.h>
#include <ace.h>
#include <game.h>
2023-07-28 01:51:04 +03:00
#include <core.h>
2023-06-06 00:23:08 +03:00
#include <utils.h>
2023-08-04 15:35:29 +03:00
#include <msg.h>
2023-08-04 22:17:31 +03:00
#include <tx.h>
2023-06-06 00:23:08 +03:00
2023-06-21 14:25:22 +03:00
#include <main.h>
HMODULE this_module;
size_t unload_ctr = 0;
void unload_ctr_inc() {
unload_ctr++;
}
void unload_ctr_dec() {
unload_ctr--;
if (unload_ctr == 0) {
void *pFreeLibrary = GetProcAddress(GetModuleHandleA("kernel32.dll"), "FreeLibrary");
CreateThread(NULL, 0, pFreeLibrary, this_module, 0, NULL);
}
}
2023-08-04 15:35:29 +03:00
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);
}
2023-08-04 22:17:31 +03:00
static void _run_game(struct game_data *game) {
// Create fake ACE driver files
ace_fake_driver_files();
// Load both ACE modules
HMODULE baseModule = ace_load_base_module(game);
ace_load_driver_module();
// ...magic
core_setup_patcher(game, baseModule);
// Load the UnityPlayer module and invoke the callback
HMODULE unityModule = LoadLibraryA("UnityPlayer.dll");
INVOKE_CALLBACK(game->unityplayer_callback, unityModule);
}
static void _run_tx(struct game_data *game, wchar_t *tableFile) {
2023-08-04 22:55:10 +03:00
// Load unpatched base module
HMODULE baseModule = LoadLibraryA(game->base_module_name);
if (!baseModule) {
msg_err_a("Failed to load base module: %s", game->base_module_name);
}
// ...more magic
size_t tableSize;
void *table = core_perform_tx(&tableSize);
2023-08-04 22:17:31 +03:00
2023-08-04 22:55:10 +03:00
// Save to file
utils_create_dir_recursively(tableFile);
utils_save_to_file(tableFile, table, tableSize);
2023-08-04 22:17:31 +03:00
2023-08-04 22:55:10 +03:00
// Cleanup
free(table);
// The file should now exist: restart and launch the game
2023-08-04 22:17:31 +03:00
request_restart();
exit(0);
}
2023-06-06 00:23:08 +03:00
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) {
// Only listen to attach
if (reason != DLL_PROCESS_ATTACH) {
return TRUE;
}
2023-06-21 14:25:22 +03:00
this_module = instance;
2023-06-06 00:23:08 +03:00
// Dynamically link functions from ntdll
ntdll_link();
// Detect which game the user is trying to run
struct game_data game;
game_detect(&game);
2023-08-04 22:17:31 +03:00
// Get required table file path
wchar_t tableFile[MAX_PATH];
tx_table_file(&game, tableFile);
2023-06-06 00:23:08 +03:00
2023-08-04 22:55:10 +03:00
if (utils_path_exists(tableFile)) {
2023-08-04 22:17:31 +03:00
_run_game(&game);
} else {
_run_tx(&game, tableFile);
}
2023-06-06 00:23:08 +03:00
return TRUE;
}