diff --git a/game_payload/blob/core.o b/game_payload/blob/core.o index 4c441e0..4bd58b6 100644 Binary files a/game_payload/blob/core.o and b/game_payload/blob/core.o differ diff --git a/game_payload/include/utils.h b/game_payload/include/utils.h index af0bbbe..90155f7 100644 --- a/game_payload/include/utils.h +++ b/game_payload/include/utils.h @@ -1,9 +1,19 @@ #pragma once +#include #include #define UTILS_COUNT(arr) (sizeof(arr) / sizeof(*arr)) +struct file_mapping { + HANDLE file; + HANDLE mapping; + unsigned char *data; +}; + +void utils_map_file(const wchar_t *path, struct file_mapping *map); +void utils_unmap_file(struct file_mapping *map); + int utils_path_exists(const wchar_t *filePath); uint32_t utils_file_crc32c(const wchar_t *filePath); diff --git a/game_payload/src/tx.c b/game_payload/src/tx.c index af88377..3b444d2 100644 --- a/game_payload/src/tx.c +++ b/game_payload/src/tx.c @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -15,30 +16,23 @@ void tx_table_file(struct game_data *game, wchar_t *buf) { GetTempPathW(MAX_PATH, tempDir); // Memorymap the base module - HANDLE baseFile = CreateFileA(game->base_module_name, FILE_READ_ACCESS, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - if (!baseFile) { - msg_err_a("Could not open file: %s", game->base_module_name); - } + wchar_t baseModuleW[MAX_PATH]; + MultiByteToWideChar(CP_UTF8, 0, game->base_module_name, strlen(game->base_module_name) + 1, baseModuleW, MAX_PATH); - HANDLE hBaseMap = CreateFileMappingA(baseFile, NULL, PAGE_READONLY, 0, 0, NULL); - char *baseMap = MapViewOfFile(hBaseMap, FILE_MAP_READ, 0, 0, 0); - if (!baseMap) { - msg_err_a("Could not create file mapping for %s", game->base_module_name); - } + struct file_mapping map; + utils_map_file(baseModuleW, &map); // Checksum the TXS section - IMAGE_SECTION_HEADER *txsSection = pe_find_section(baseMap, game->txs_section_name); + IMAGE_SECTION_HEADER *txsSection = pe_find_section(map.data, game->txs_section_name); if (!txsSection) { msg_err_a("Could not find %s in %s. " ISSUE_SUFFIX, game->txs_section_name, game->base_module_name); } - uint32_t txsChecksum = crc32c(0, baseMap + txsSection->PointerToRawData, txsSection->SizeOfRawData); + uint32_t txsChecksum = crc32c(0, map.data + txsSection->PointerToRawData, txsSection->SizeOfRawData); // Format the path wsprintfW(buf, L"%sjadeite\\" JADEITE_VERSION "\\%hs.%x.dat", tempDir, game->base_module_name, txsChecksum); // Cleanup - UnmapViewOfFile(baseMap); - CloseHandle(hBaseMap); - CloseHandle(baseFile); + utils_unmap_file(&map); } diff --git a/game_payload/src/utils.c b/game_payload/src/utils.c index 3cf7211..c312cb6 100644 --- a/game_payload/src/utils.c +++ b/game_payload/src/utils.c @@ -5,31 +5,39 @@ #include +void utils_map_file(const wchar_t *path, struct file_mapping *map) { + map->file = CreateFileW(path, FILE_READ_ACCESS, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (map->file == INVALID_HANDLE_VALUE) { + msg_err_w(L"Could not open file: %ls", path); + } + + map->mapping = CreateFileMappingA(map->file, NULL, PAGE_READONLY, 0, 0, NULL); + map->data = MapViewOfFile(map->mapping, FILE_MAP_READ, 0, 0, 0); + if (!map->data) { + msg_err_w(L"Could not map view of file %ls", path); + } +} + +void utils_unmap_file(struct file_mapping *map) { + UnmapViewOfFile(map->data); + CloseHandle(map->mapping); + CloseHandle(map->file); +} + int utils_path_exists(const wchar_t *filePath) { return GetFileAttributesW(filePath) != INVALID_FILE_ATTRIBUTES; } uint32_t utils_file_crc32c(const wchar_t *filePath) { - HANDLE file = CreateFileW(filePath, FILE_READ_ACCESS, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - if (!file) { - msg_err_w(L"Could not open file: %ls", filePath); - } + struct file_mapping map; + utils_map_file(filePath, &map); LARGE_INTEGER fileSize; - GetFileSizeEx(file, &fileSize); + GetFileSizeEx(map.file, &fileSize); - HANDLE hMap = CreateFileMappingA(file, NULL, PAGE_READONLY, 0, 0, NULL); - char *map = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0); - if (!map) { - msg_err_w(L"Could not create file mapping for %ls", filePath); - } - - uint32_t crc = crc32c(0, map, fileSize.QuadPart); - - UnmapViewOfFile(map); - CloseHandle(hMap); - CloseHandle(file); + uint32_t crc = crc32c(0, map.data, fileSize.QuadPart); + utils_unmap_file(&map); return crc; } @@ -51,8 +59,8 @@ void utils_create_parent_dirs(const wchar_t *path) { 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); + if (file == INVALID_HANDLE_VALUE) { + msg_err_w(L"Could not create file: %ls", filePath); } WriteFile(file, buf, length, NULL, FALSE);