diff --git a/game_payload/blob/core.o b/game_payload/blob/core.o index f9c35f5..2389228 100644 Binary files a/game_payload/blob/core.o and b/game_payload/blob/core.o differ diff --git a/game_payload/include/core.h b/game_payload/include/core.h index 9389281..d0d868b 100644 --- a/game_payload/include/core.h +++ b/game_payload/include/core.h @@ -5,3 +5,5 @@ #include void core_setup_patcher(struct game_data *game, HMODULE baseModule); + +void *core_perform_tx(size_t *outLength); diff --git a/game_payload/include/utils.h b/game_payload/include/utils.h index 237c3e5..d9c4099 100644 --- a/game_payload/include/utils.h +++ b/game_payload/include/utils.h @@ -2,7 +2,11 @@ #include -int utils_file_exists(const wchar_t *filePath); +int utils_path_exists(const wchar_t *filePath); uint32_t utils_file_crc32c(const wchar_t *filePath); +void utils_create_dir_recursively(const wchar_t *path); + +void utils_save_to_file(const wchar_t *filePath, const void *buf, size_t length); + char utils_env_enabled(const char *env); diff --git a/game_payload/src/main.c b/game_payload/src/main.c index 9ca76f0..718e7a4 100644 --- a/game_payload/src/main.c +++ b/game_payload/src/main.c @@ -55,8 +55,24 @@ static void _run_game(struct game_data *game) { } static void _run_tx(struct game_data *game, wchar_t *tableFile) { + // 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); + // Save to file + utils_create_dir_recursively(tableFile); + utils_save_to_file(tableFile, table, tableSize); + + // Cleanup + free(table); + + // The file should now exist: restart and launch the game request_restart(); exit(0); } @@ -80,10 +96,7 @@ BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) { wchar_t tableFile[MAX_PATH]; tx_table_file(&game, tableFile); - // Remove this - msg_err_w(tableFile); - - if (1) { + if (utils_path_exists(tableFile)) { _run_game(&game); } else { _run_tx(&game, tableFile); diff --git a/game_payload/src/tx.c b/game_payload/src/tx.c index 42a9a23..e96e4ff 100644 --- a/game_payload/src/tx.c +++ b/game_payload/src/tx.c @@ -37,4 +37,4 @@ void tx_table_file(struct game_data *game, wchar_t *buf) { UnmapViewOfFile(baseMap); CloseHandle(hBaseMap); CloseHandle(baseFile); -} \ No newline at end of file +} diff --git a/game_payload/src/utils.c b/game_payload/src/utils.c index 1656eab..ef97869 100644 --- a/game_payload/src/utils.c +++ b/game_payload/src/utils.c @@ -5,7 +5,7 @@ #include -int utils_file_exists(const wchar_t *filePath) { +int utils_path_exists(const wchar_t *filePath) { return GetFileAttributesW(filePath) != INVALID_FILE_ATTRIBUTES; } @@ -33,6 +33,36 @@ uint32_t utils_file_crc32c(const wchar_t *filePath) { return crc; } +// https://stackoverflow.com/a/16719260 +void utils_create_dir_recursively(const wchar_t *path) { + wchar_t dir[MAX_PATH]; + ZeroMemory(dir, MAX_PATH * sizeof(wchar_t)); + + wchar_t *end = wcschr(path, L'\\'); + + while(end != NULL) + { + wcsncpy(dir, path, end - path + 1); + + if (!utils_path_exists(dir) && !CreateDirectoryW(dir, NULL)) { + msg_err_w(L"Failed to create directory: %ls", dir); + } + + end = wcschr(++end, L'\\'); + } +} + +void utils_save_to_file(const wchar_t *filePath, const void *buf, size_t length) { + HANDLE file = CreateFileW(filePath, FILE_WRITE_ACCESS, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); + if (!file) { + msg_err_w(L"Could not open file: %ls", filePath); + } + + WriteFile(file, buf, length, NULL, FALSE); + + CloseHandle(file); +} + char utils_env_enabled(const char *env) { char *envText = getenv(env); return envText && *envText;