From 93f423ae0eb3c2749b3b88eec6f5419fbab66592 Mon Sep 17 00:00:00 2001 From: IliasHDZ Date: Wed, 21 Aug 2024 00:48:14 +0500 Subject: [PATCH 1/3] add fix currect working directory --- loader/src/platform/windows/main.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/loader/src/platform/windows/main.cpp b/loader/src/platform/windows/main.cpp index dd02a901..ac5f306c 100644 --- a/loader/src/platform/windows/main.cpp +++ b/loader/src/platform/windows/main.cpp @@ -138,11 +138,30 @@ void* mainTrampolineAddr; #include "gdTimestampMap.hpp" unsigned int gdTimestamp = 0; +// In case the game is launched from a different directory through command line +// this function will set the current working directory to the game's directory +// to avoid the game crashing due to not being able to find the resources +static void fixCWD() { + char cwd[1024]; + DWORD size = GetModuleFileNameA(NULL, cwd, sizeof(cwd)); + if (size == sizeof(cwd)) return; + for (int i = size - 1; i >= 0; i--) { + if (cwd[i] == '\\') { + cwd[i] = '\0'; + break; + } + } + console::messageBox("Geode", fmt::format("Hello from gdMainHook!\Setting CWD: {}", cwd)); + SetCurrentDirectoryA(cwd); +} + int WINAPI gdMainHook(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { // MessageBoxA(NULL, "Hello from gdMainHook!", "Hi", 0); updateGeode(); + fixCWD(); + if (versionToTimestamp(GEODE_STR(GEODE_GD_VERSION)) > gdTimestamp) { console::messageBox( "Unable to Load Geode!", From 4ce4fd972b4c797a26f4df5ba275a0482d27a572 Mon Sep 17 00:00:00 2001 From: IliasHDZ Date: Wed, 21 Aug 2024 00:52:39 +0500 Subject: [PATCH 2/3] oops --- loader/src/platform/windows/main.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/loader/src/platform/windows/main.cpp b/loader/src/platform/windows/main.cpp index ac5f306c..1bc8e251 100644 --- a/loader/src/platform/windows/main.cpp +++ b/loader/src/platform/windows/main.cpp @@ -151,7 +151,6 @@ static void fixCWD() { break; } } - console::messageBox("Geode", fmt::format("Hello from gdMainHook!\Setting CWD: {}", cwd)); SetCurrentDirectoryA(cwd); } From e222313f228601911695dba1b0bda1cf332b96b4 Mon Sep 17 00:00:00 2001 From: IliasHDZ Date: Wed, 21 Aug 2024 11:29:40 +0500 Subject: [PATCH 3/3] change to wchar and use max_path --- loader/src/platform/windows/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/loader/src/platform/windows/main.cpp b/loader/src/platform/windows/main.cpp index 1bc8e251..817447ba 100644 --- a/loader/src/platform/windows/main.cpp +++ b/loader/src/platform/windows/main.cpp @@ -142,8 +142,8 @@ unsigned int gdTimestamp = 0; // this function will set the current working directory to the game's directory // to avoid the game crashing due to not being able to find the resources static void fixCWD() { - char cwd[1024]; - DWORD size = GetModuleFileNameA(NULL, cwd, sizeof(cwd)); + WCHAR cwd[MAX_PATH]; + DWORD size = GetModuleFileNameW(NULL, cwd, sizeof(cwd)); if (size == sizeof(cwd)) return; for (int i = size - 1; i >= 0; i--) { if (cwd[i] == '\\') { @@ -151,7 +151,7 @@ static void fixCWD() { break; } } - SetCurrentDirectoryA(cwd); + SetCurrentDirectoryW(cwd); } int WINAPI gdMainHook(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {