Implement table saving functionality

This commit is contained in:
mkrsym1 2023-08-04 22:55:10 +03:00
parent 7eac309372
commit 400729a3dc
6 changed files with 56 additions and 7 deletions

Binary file not shown.

View File

@ -5,3 +5,5 @@
#include <game.h> #include <game.h>
void core_setup_patcher(struct game_data *game, HMODULE baseModule); void core_setup_patcher(struct game_data *game, HMODULE baseModule);
void *core_perform_tx(size_t *outLength);

View File

@ -2,7 +2,11 @@
#include <stdint.h> #include <stdint.h>
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); 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); char utils_env_enabled(const char *env);

View File

@ -55,8 +55,24 @@ static void _run_game(struct game_data *game) {
} }
static void _run_tx(struct game_data *game, wchar_t *tableFile) { 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(); request_restart();
exit(0); exit(0);
} }
@ -80,10 +96,7 @@ BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) {
wchar_t tableFile[MAX_PATH]; wchar_t tableFile[MAX_PATH];
tx_table_file(&game, tableFile); tx_table_file(&game, tableFile);
// Remove this if (utils_path_exists(tableFile)) {
msg_err_w(tableFile);
if (1) {
_run_game(&game); _run_game(&game);
} else { } else {
_run_tx(&game, tableFile); _run_tx(&game, tableFile);

View File

@ -37,4 +37,4 @@ void tx_table_file(struct game_data *game, wchar_t *buf) {
UnmapViewOfFile(baseMap); UnmapViewOfFile(baseMap);
CloseHandle(hBaseMap); CloseHandle(hBaseMap);
CloseHandle(baseFile); CloseHandle(baseFile);
} }

View File

@ -5,7 +5,7 @@
#include <utils.h> #include <utils.h>
int utils_file_exists(const wchar_t *filePath) { int utils_path_exists(const wchar_t *filePath) {
return GetFileAttributesW(filePath) != INVALID_FILE_ATTRIBUTES; return GetFileAttributesW(filePath) != INVALID_FILE_ATTRIBUTES;
} }
@ -33,6 +33,36 @@ uint32_t utils_file_crc32c(const wchar_t *filePath) {
return crc; 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 utils_env_enabled(const char *env) {
char *envText = getenv(env); char *envText = getenv(env);
return envText && *envText; return envText && *envText;