feat: support external game launchers

This commit is contained in:
2024-03-24 17:14:00 +07:00
parent 7aa7047ccd
commit 9099d50ba8
3 changed files with 122 additions and 9 deletions

View File

@ -75,6 +75,11 @@ BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) {
return TRUE;
}
LPWSTR targetExe = malloc(MAX_PATH);
GetModuleFileNameW(NULL, targetExe, 0);
SetCurrentDirectoryW(targetExe);
free(targetExe);
this_module = instance;
// Dynamically link functions from ntdll

View File

@ -6,16 +6,26 @@
#include <utils.h>
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);
wchar_t* final_path = malloc(MAX_PATH);
if (wcsstr(path, L"C:\\") == NULL) {
wchar_t* tmp = malloc(MAX_PATH);
GetEnvironmentVariableW(L"GAME_PATH", tmp, MAX_PATH);
swprintf(final_path, MAX_PATH, L"%ls\\%ls", tmp, path);
free(tmp);
} else {
swprintf(final_path, MAX_PATH, L"%ls", path);
}
map->file = CreateFileW(final_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);
msg_err_w(L"Could not open file: %ls", final_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);
msg_err_w(L"Could not map view of file %ls", final_path);
}
free(final_path);
}
void utils_unmap_file(struct file_mapping *map) {
@ -30,6 +40,10 @@ int utils_path_exists(const wchar_t *filePath) {
uint32_t utils_file_crc32c(const wchar_t *filePath) {
struct file_mapping map;
// LPWSTR cwd = malloc(MAX_PATH);
// GetCurrentDirectoryW(MAX_PATH, cwd);
// msg_info_w(L"File %ls %ls", filePath, cwd);
// free(cwd);
utils_map_file(filePath, &map);
LARGE_INTEGER fileSize;