From bb294677fe7ef5287d688911802aeac5e8763c45 Mon Sep 17 00:00:00 2001 From: Alexander Yee Date: Sun, 9 Aug 2026 21:27:38 -0700 Subject: [PATCH 1/4] Migrate more Mac stuff in GlobalAutoPaths away from Qt. --- Common/Cpp/Filesystem/Filesystem.h | 3 +- Common/Cpp/Filesystem/Filesystem_Linux.h | 5 +- Common/Cpp/Filesystem/Filesystem_Mac.h | 55 ++++++++++++++++++- Common/Cpp/Filesystem/Filesystem_Qt.h | 5 +- Common/Cpp/Filesystem/Filesystem_Windows.h | 5 +- .../CommonFramework/GlobalAutoPaths.cpp | 21 +------ .../Source/CommonFramework/Main.cpp | 2 +- 7 files changed, 70 insertions(+), 26 deletions(-) diff --git a/Common/Cpp/Filesystem/Filesystem.h b/Common/Cpp/Filesystem/Filesystem.h index 2396d86ea0..63b7f3f2c2 100644 --- a/Common/Cpp/Filesystem/Filesystem.h +++ b/Common/Cpp/Filesystem/Filesystem.h @@ -61,7 +61,8 @@ void rename(const Path& old_path, const Path& new_path, std::error_code& ec); Path current_path(); -Path application_path(); +Path application_binary_path(); +Path application_working_path(); diff --git a/Common/Cpp/Filesystem/Filesystem_Linux.h b/Common/Cpp/Filesystem/Filesystem_Linux.h index 51d3109835..f2264c22ef 100644 --- a/Common/Cpp/Filesystem/Filesystem_Linux.h +++ b/Common/Cpp/Filesystem/Filesystem_Linux.h @@ -12,7 +12,7 @@ namespace PokemonAutomation{ namespace Filesystem{ -Path application_path(){ +Path application_binary_path(){ char buffer[PATH_MAX]; ssize_t count = readlink("/proc/self/exe", buffer, PATH_MAX); if (count != -1) { @@ -20,6 +20,9 @@ Path application_path(){ } return Path(); } +Path application_working_path(){ + return application_binary_path(); +} diff --git a/Common/Cpp/Filesystem/Filesystem_Mac.h b/Common/Cpp/Filesystem/Filesystem_Mac.h index bf9b26abfd..9a0eb130e4 100644 --- a/Common/Cpp/Filesystem/Filesystem_Mac.h +++ b/Common/Cpp/Filesystem/Filesystem_Mac.h @@ -4,15 +4,17 @@ * */ -#include #include +#include +#include +#include #include "FilePath.h" namespace PokemonAutomation{ namespace Filesystem{ -Path application_path(){ +Path application_binary_path(){ char buffer[PATH_MAX]; uint32_t size = sizeof(buffer); if (_NSGetExecutablePath(buffer, &size) == 0) { @@ -23,5 +25,54 @@ Path application_path(){ +std::filesystem::path CFString_to_path(CFStringRef cfString) { + if (!cfString) { + return {}; + } + + // 1. Get the exact buffer size needed for the file system representation + CFIndex maxLength = CFStringGetMaximumSizeOfFileSystemRepresentation(cfString); + + // 2. Allocate a buffer to store the raw file system bytes + std::vector buffer(maxLength); + + // 3. Extract the file system representation directly into the buffer + if (CFStringGetFileSystemRepresentation(cfString, buffer.data(), buffer.size())) { + return std::filesystem::path(buffer.data()); + } + + // Fallback: If file-system specific translation failed, try standard UTF-8 text extraction + CFIndex length = CFStringGetLength(cfString); + CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1; + std::vector fallbackBuffer(maxSize); + + if (CFStringGetCString(cfString, fallbackBuffer.data(), fallbackBuffer.size(), kCFStringEncodingUTF8)) { + return std::filesystem::path(fallbackBuffer.data()); + } + + return {}; +} +Path application_working_path(){ + // Use CFBundle to find the .app bundle path. + // Change working directory to the folder that hosts the .app bundle. + CFURLRef bundleURL = CFBundleCopyBundleURL(CFBundleGetMainBundle()); + if (bundleURL){ + CFStringRef cfPath = CFURLCopyFileSystemPath(bundleURL, kCFURLPOSIXPathStyle); + CFRelease(bundleURL); + if (cfPath){ + std::filesystem::path bundlePath = CFString_to_path(cfPath); + CFRelease(cfPath); + if (bundlePath.extension() == ".app"){ + return std::filesystem::absolute(bundlePath).lexically_normal(); + } + } + } + + // Fallback + return application_binary_path(); +} + + + } } diff --git a/Common/Cpp/Filesystem/Filesystem_Qt.h b/Common/Cpp/Filesystem/Filesystem_Qt.h index 5a6aa70358..c9e3e7b0f1 100644 --- a/Common/Cpp/Filesystem/Filesystem_Qt.h +++ b/Common/Cpp/Filesystem/Filesystem_Qt.h @@ -11,10 +11,13 @@ namespace PokemonAutomation{ namespace Filesystem{ -Path application_path(){ +Path application_binary_path(){ QString application_dir_path = qApp->applicationDirPath(); return Path(application_dir_path.toStdString()); } +Path application_working_path(){ + return application_binary_path(); +} diff --git a/Common/Cpp/Filesystem/Filesystem_Windows.h b/Common/Cpp/Filesystem/Filesystem_Windows.h index 264876dd68..ddfbdb576e 100644 --- a/Common/Cpp/Filesystem/Filesystem_Windows.h +++ b/Common/Cpp/Filesystem/Filesystem_Windows.h @@ -11,11 +11,14 @@ namespace PokemonAutomation{ namespace Filesystem{ -Path application_path(){ +Path application_binary_path(){ wchar_t buffer[MAX_PATH]; GetModuleFileNameW(nullptr, buffer, MAX_PATH); return std::filesystem::path(buffer).parent_path(); } +Path application_working_path(){ + return application_binary_path(); +} diff --git a/SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp b/SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp index 185be4f0f0..1866fdb829 100644 --- a/SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp +++ b/SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp @@ -11,9 +11,6 @@ #include #include #include -#if defined(__APPLE__) -#include -#endif #include "Common/Cpp/Filesystem/Filesystem.h" #include "GlobalAutoPaths.h" @@ -58,21 +55,7 @@ namespace{ Filesystem::Path get_application_base_dir_path(){ -#if defined(__APPLE__) - // Use CFBundle to find the .app bundle path. Change working directory to the folder that hosts the .app bundle. - CFURLRef bundleURL = CFBundleCopyBundleURL(CFBundleGetMainBundle()); - if (bundleURL){ - CFStringRef cfPath = CFURLCopyFileSystemPath(bundleURL, kCFURLPOSIXPathStyle); - CFRelease(bundleURL); - if (cfPath){ - QString bundlePath = QDir::cleanPath(QString::fromCFString(cfPath)); - CFRelease(cfPath); - if (bundlePath.endsWith(".app")){ - return QFileInfo(bundlePath).dir().absolutePath().toStdString(); - } - } - } -#elif defined(__linux__) +#if defined(__linux__) // Check for AppImage environment variables to find the root directory, if running as an AppImage. // PA_APPIMAGE_DIR is set by Azure via a patched AppRun script. QByteArray dir = qgetenv("PA_APPIMAGE_DIR"); @@ -108,7 +91,7 @@ Filesystem::Path get_application_base_dir_path(){ } } #endif - return Filesystem::application_path(); + return Filesystem::application_binary_path(); } std::string get_resource_path(){ // Find the resource directory. diff --git a/SerialPrograms/Source/CommonFramework/Main.cpp b/SerialPrograms/Source/CommonFramework/Main.cpp index 61aa9eb129..744f84f9b2 100644 --- a/SerialPrograms/Source/CommonFramework/Main.cpp +++ b/SerialPrograms/Source/CommonFramework/Main.cpp @@ -109,7 +109,7 @@ int run_program(int argc, char *argv[]){ logger.log("================================================================================"); logger.log("Starting Program..."); logger.log("Current path: " + Filesystem::current_path().string_slash_normalized()); - logger.log("Executable path: " + Filesystem::application_path().string_slash_normalized()); + logger.log("Executable path: " + Filesystem::application_binary_path().string_slash_normalized()); logger.log("Program setting folder: " + SETTINGS_PATH()); logger.log("Program resources folder: " + RESOURCE_PATH()); From b54e17c9aa374db52d3ec0a08d6588e63ec21991 Mon Sep 17 00:00:00 2001 From: Alexander Yee Date: Sun, 9 Aug 2026 22:23:13 -0700 Subject: [PATCH 2/4] fix --- Common/Cpp/Filesystem/Filesystem_Mac.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Common/Cpp/Filesystem/Filesystem_Mac.h b/Common/Cpp/Filesystem/Filesystem_Mac.h index 9a0eb130e4..8ef3caec10 100644 --- a/Common/Cpp/Filesystem/Filesystem_Mac.h +++ b/Common/Cpp/Filesystem/Filesystem_Mac.h @@ -5,6 +5,7 @@ */ #include +#include #include #include #include From de7471dc9cf5e2b0939c5ef17e8b600db51bab19 Mon Sep 17 00:00:00 2001 From: Alexander Yee Date: Sun, 9 Aug 2026 22:45:34 -0700 Subject: [PATCH 3/4] fix --- Common/Cpp/Filesystem/Filesystem_Mac.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Common/Cpp/Filesystem/Filesystem_Mac.h b/Common/Cpp/Filesystem/Filesystem_Mac.h index 8ef3caec10..95636fc1cb 100644 --- a/Common/Cpp/Filesystem/Filesystem_Mac.h +++ b/Common/Cpp/Filesystem/Filesystem_Mac.h @@ -64,7 +64,7 @@ Path application_working_path(){ std::filesystem::path bundlePath = CFString_to_path(cfPath); CFRelease(cfPath); if (bundlePath.extension() == ".app"){ - return std::filesystem::absolute(bundlePath).lexically_normal(); + return std::filesystem::absolute(bundlePath).parent_path().lexically_normal(); } } } From ad3c19d6422eeb6be53d47c770f41a04de9e01af Mon Sep 17 00:00:00 2001 From: Alexander Yee Date: Mon, 10 Aug 2026 00:02:51 -0700 Subject: [PATCH 4/4] fix --- SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp b/SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp index 1866fdb829..844f0de064 100644 --- a/SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp +++ b/SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp @@ -91,7 +91,7 @@ Filesystem::Path get_application_base_dir_path(){ } } #endif - return Filesystem::application_binary_path(); + return Filesystem::application_working_path(); } std::string get_resource_path(){ // Find the resource directory.