From cf8e50eca15cdd73829d2498719839b4d254a415 Mon Sep 17 00:00:00 2001 From: TheMorc Date: Wed, 23 Aug 2023 03:32:22 +0200 Subject: [PATCH] add hackish macOS detection for home folder, resources and cache --- src/platform.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/platform.cpp b/src/platform.cpp index 0117e176..0ca0918b 100644 --- a/src/platform.cpp +++ b/src/platform.cpp @@ -151,6 +151,78 @@ std::string Platform::FindStateCacheFolder() { return "."; } +#elif defined(__APPLE__) +#include +#include +#include +#include +std::string Platform::FindResourceFolder() { + static std::string found_path; + static bool found = false; + if (found) return found_path; + + CFURLRef resourceURL = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle()); + char resourcePath[PATH_MAX]; + if (CFURLGetFileSystemRepresentation(resourceURL, true, (UInt8 *)resourcePath, PATH_MAX)) { + if (resourceURL != NULL) { + CFRelease(resourceURL); + } + found_path = resourcePath; + found = true; + return found_path; + } + + puts("cant find a resources folder, will try to load from cwd"); + found_path = "."; + found = true; + return found_path; +} + +std::string Platform::FindConfigFile() { + const auto cfg = std::getenv("ABADDON_CONFIG"); + if (cfg != nullptr) return cfg; + + struct passwd *home = getpwuid(getuid()); + const char *homeDir = home->pw_dir; + + char appSupportPath[PATH_MAX]; + snprintf(appSupportPath, sizeof(appSupportPath), "%s/Library/Application Support", homeDir); + + char homefolder_path[PATH_MAX]; + snprintf(homefolder_path, sizeof(homefolder_path), "%s/%s", appSupportPath, "com.uowuo.abaddon"); + + if (mkdir(homefolder_path, 0755) == 0) { + puts("created Application Support dir"); + } + + char home_path[PATH_MAX]; + snprintf(home_path, sizeof(home_path), "%s/%s", homefolder_path, "/abaddon.ini"); + + return home_path; + + // fallback to cwd if cant find + cant make in ~/.config + puts("can't find configuration file!"); + return "./abaddon.ini"; +} + +std::string Platform::FindStateCacheFolder() { + + struct passwd *home = getpwuid(getuid()); + const char *homeDir = home->pw_dir; + + char appSupportPath[PATH_MAX]; + snprintf(appSupportPath, sizeof(appSupportPath), "%s/Library/Application Support", homeDir); + + char home_path[PATH_MAX]; + snprintf(home_path, sizeof(home_path), "%s/%s", appSupportPath, "com.uowuo.abaddon"); + + return home_path; + + puts("can't find cache folder!"); + return "."; +} + + #else std::string Platform::FindResourceFolder() { puts("unknown OS, trying to load resources from cwd");