From dc96da012df994fefce0e9c3ed85459f81794800 Mon Sep 17 00:00:00 2001
From: altalk23 <45172705+altalk23@users.noreply.github.com>
Date: Mon, 1 May 2023 14:47:25 +0300
Subject: [PATCH] don't rely on filesystem current_path

---
 loader/launcher/windows/Bootstrapper.cpp |  6 +++++-
 loader/src/loader/Dirs.cpp               |  2 +-
 loader/src/platform/mac/util.mm          | 15 ++++++---------
 loader/src/platform/windows/util.cpp     |  7 ++++++-
 4 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/loader/launcher/windows/Bootstrapper.cpp b/loader/launcher/windows/Bootstrapper.cpp
index 84b75896..dac0e63a 100644
--- a/loader/launcher/windows/Bootstrapper.cpp
+++ b/loader/launcher/windows/Bootstrapper.cpp
@@ -17,7 +17,11 @@ int loadGeode(PVOID module) {
 }
 
 DWORD WINAPI load(PVOID module) {
-	auto workingDir = ghc::filesystem::current_path();
+	std::array<TCHAR, MAX_PATH> szFileName;
+    GetModuleFileName(NULL, szFileName.data(), MAX_PATH);
+
+    ghc::filesystem::path path(szFileName);
+    auto workingDir = path.parent_path();
 	auto updatesDir = workingDir / "geode" / "update";
 	auto resourcesDir = workingDir / "geode" / "resources";
 
diff --git a/loader/src/loader/Dirs.cpp b/loader/src/loader/Dirs.cpp
index 530f21bc..133f69c2 100644
--- a/loader/src/loader/Dirs.cpp
+++ b/loader/src/loader/Dirs.cpp
@@ -19,7 +19,7 @@ namespace {
 }
 
 ghc::filesystem::path dirs::getGameDir() {
-    return weaklyCanonical(CCFileUtils::sharedFileUtils()->getWritablePath2().c_str());
+    return utils::file::current_path();
 }
 
 ghc::filesystem::path dirs::getSaveDir() {
diff --git a/loader/src/platform/mac/util.mm b/loader/src/platform/mac/util.mm
index e7d11f71..364186d5 100644
--- a/loader/src/platform/mac/util.mm
+++ b/loader/src/platform/mac/util.mm
@@ -149,17 +149,14 @@ CCPoint cocos::getMousePos() {
 }
 
 ghc::filesystem::path utils::file::current_path() {
-    // if it's just a slash because for some reason it is
-    if (ghc::filesystem::current_path().string().size() < 2) {
-        std::array<char, PATH_MAX> gddir;
+    std::array<char, PATH_MAX> gddir;
 
-        uint32_t out = PATH_MAX;
-        _NSGetExecutablePath(gddir.data(), &out);
+    uint32_t out = PATH_MAX;
+    _NSGetExecutablePath(gddir.data(), &out);
 
-        ghc::filesystem::path gdpath = gddir.data();
-        ghc::filesystem::current_path(gdpath.parent_path().parent_path());
-    }
-    return ghc::filesystem::current_path();
+    ghc::filesystem::path gdpath = gddir.data();
+    auto currentPath = gdpath.parent_path().parent_path();    
+    return currentPath;
 }
 
 #endif
diff --git a/loader/src/platform/windows/util.cpp b/loader/src/platform/windows/util.cpp
index 82bb7d78..bd2accb0 100644
--- a/loader/src/platform/windows/util.cpp
+++ b/loader/src/platform/windows/util.cpp
@@ -116,7 +116,12 @@ CCPoint cocos::getMousePos() {
 }
 
 ghc::filesystem::path utils::file::current_path() {
-    return ghc::filesystem::current_path();
+    std::array<TCHAR, MAX_PATH> szFileName;
+    GetModuleFileName(NULL, szFileName.data(), MAX_PATH);
+
+    ghc::filesystem::path path(szFileName);
+    auto currentPath = path.parent_path();
+    return currentPath;
 }
 
 #endif