From 43c0f313f255329020762cddc27b24e3c93d7e0c Mon Sep 17 00:00:00 2001 From: squidbus <175574877+squidbus@users.noreply.github.com> Date: Sat, 20 Jul 2024 03:03:09 -0700 Subject: [PATCH] Fix user data path on macOS. --- src/common/path_util.cpp | 35 +++++++++++++++++++++++++++++++++++ src/qt_gui/main.cpp | 13 +++---------- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/common/path_util.cpp b/src/common/path_util.cpp index 243bc15b..429fe2a5 100644 --- a/src/common/path_util.cpp +++ b/src/common/path_util.cpp @@ -4,6 +4,12 @@ #include #include "common/logging/log.h" #include "common/path_util.h" +#include "common/scope_exit.h" + +#ifdef __APPLE__ +#include +#include +#endif #ifndef MAX_PATH #ifdef _WIN32 @@ -19,7 +25,36 @@ namespace Common::FS { namespace fs = std::filesystem; +#ifdef __APPLE__ +static std::filesystem::path GetBundleParentDirectory() { + if (CFBundleRef bundle_ref = CFBundleGetMainBundle()) { + if (CFURLRef bundle_url_ref = CFBundleCopyBundleURL(bundle_ref)) { + SCOPE_EXIT { + CFRelease(bundle_url_ref); + }; + if (CFStringRef bundle_path_ref = + CFURLCopyFileSystemPath(bundle_url_ref, kCFURLPOSIXPathStyle)) { + SCOPE_EXIT { + CFRelease(bundle_path_ref); + }; + char app_bundle_path[MAXPATHLEN]; + if (CFStringGetFileSystemRepresentation(bundle_path_ref, app_bundle_path, + sizeof(app_bundle_path))) { + std::filesystem::path bundle_path{app_bundle_path}; + return bundle_path.parent_path(); + } + } + } + } + return std::filesystem::current_path(); +} +#endif + static auto UserPaths = [] { +#ifdef __APPLE__ + std::filesystem::current_path(GetBundleParentDirectory()); +#endif + std::unordered_map paths; const auto user_dir = std::filesystem::current_path() / PORTABLE_DIR; diff --git a/src/qt_gui/main.cpp b/src/qt_gui/main.cpp index 08c363b3..ea3f27f8 100644 --- a/src/qt_gui/main.cpp +++ b/src/qt_gui/main.cpp @@ -18,16 +18,9 @@ int main(int argc, char* argv[]) { QApplication a(argc, argv); // Load configurations and initialize Qt application - const auto config_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir); - Config::load(config_dir / "config.toml"); - QString gameDataPath = QDir::currentPath() + "/user/game_data/"; - std::string stdStr = gameDataPath.toStdString(); - std::filesystem::path path(stdStr); -#ifdef _WIN64 - std::wstring wstdStr = gameDataPath.toStdWString(); - path = std::filesystem::path(wstdStr); -#endif - std::filesystem::create_directory(path); + const auto user_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir); + Config::load(user_dir / "config.toml"); + std::filesystem::create_directory(user_dir / "game_data"); // Check if the game install directory is set if (Config::getGameInstallDir() == "") {