From 1febaab9c78895a1ad8e784d45bf1959ab20028d Mon Sep 17 00:00:00 2001 From: squidbus <175574877+squidbus@users.noreply.github.com> Date: Sun, 23 Mar 2025 11:00:26 -0700 Subject: [PATCH] Use Application Support directory on macOS. --- include/zelda_support.h | 2 ++ src/game/config.cpp | 7 +++++++ src/main/support_apple.mm | 9 +++++++++ 3 files changed, 18 insertions(+) diff --git a/include/zelda_support.h b/include/zelda_support.h index 9751e9a..70dfe6f 100644 --- a/include/zelda_support.h +++ b/include/zelda_support.h @@ -3,6 +3,7 @@ #include #include +#include namespace zelda64 { std::filesystem::path get_asset_path(const char* asset); @@ -12,6 +13,7 @@ namespace zelda64 { // Apple specific methods that usually require Objective-C. Implemented in support_apple.mm. #ifdef __APPLE__ void dispatch_on_ui_thread(std::function func); + std::optional get_application_support_directory(); std::filesystem::path get_bundle_resource_directory(); std::filesystem::path get_bundle_directory(); #endif diff --git a/src/game/config.cpp b/src/game/config.cpp index 7f6c614..17ff9e4 100644 --- a/src/game/config.cpp +++ b/src/game/config.cpp @@ -162,6 +162,13 @@ std::filesystem::path zelda64::get_app_folder_path() { return std::filesystem::path{getenv("APP_FOLDER_PATH")}; } +#if defined(__APPLE__) + const auto supportdir = zelda64::get_application_support_directory(); + if (supportdir) { + return *supportdir / zelda64::program_id; + } +#endif + const char *homedir; if ((homedir = getenv("HOME")) == nullptr) { diff --git a/src/main/support_apple.mm b/src/main/support_apple.mm index 604a998..60a851e 100644 --- a/src/main/support_apple.mm +++ b/src/main/support_apple.mm @@ -12,6 +12,15 @@ namespace zelda64 { }); } + std::optional get_application_support_directory() { + NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); + if ([dirs count] > 0) { + NSString *dir = [dirs firstObject]; + return std::filesystem::path([dir UTF8String]); + } + return std::nullopt; + } + std::filesystem::path get_bundle_resource_directory() { NSString *bundlePath = [[NSBundle mainBundle] resourcePath]; return std::filesystem::path([bundlePath UTF8String]);