From 7c558ee47eed631271f8689dd10e1228c0a5da27 Mon Sep 17 00:00:00 2001 From: IliasHDZ <11091568+iliasHDZ@users.noreply.github.com> Date: Tue, 10 Sep 2024 18:07:09 +0100 Subject: [PATCH] Fixing current working directory on startup (#1047) * add fix currect working directory * oops * change to wchar and use max_path * using `std::array` * Make the cwd fix use std filesystem path --------- Co-authored-by: IliasHDZ Co-authored-by: alk <45172705+altalk23@users.noreply.github.com> --- loader/src/platform/windows/main.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/loader/src/platform/windows/main.cpp b/loader/src/platform/windows/main.cpp index dd02a9019..6f6c7b823 100644 --- a/loader/src/platform/windows/main.cpp +++ b/loader/src/platform/windows/main.cpp @@ -138,11 +138,25 @@ 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 +void fixCurrentWorkingDirectory() { + std::array cwd; + + auto size = GetModuleFileNameW(nullptr, cwd.data(), cwd.size()); + if (size == cwd.size()) return; + + SetCurrentDirectoryW(std::filesystem::path(cwd.data()).parent_path().wstring().c_str()); +} + int WINAPI gdMainHook(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { // MessageBoxA(NULL, "Hello from gdMainHook!", "Hi", 0); updateGeode(); + fixCurrentWorkingDirectory(); + if (versionToTimestamp(GEODE_STR(GEODE_GD_VERSION)) > gdTimestamp) { console::messageBox( "Unable to Load Geode!",