From d6b2ca53e2f02a1e77b84c70a24450475e915b49 Mon Sep 17 00:00:00 2001 From: Steveice10 <1269164+Steveice10@users.noreply.github.com> Date: Mon, 22 Jan 2024 06:20:14 -0800 Subject: [PATCH] nall: Account for macOS app bundles in program path. (#1373) Contains three main changes: * In `Path::program()`, account for the macOS app bundle, so that portable config support works with the app bundle. * Create a new function `Path::resources()` for the resources path, since `Path::program()` can no longer trivially be used to construct the bundle resources path (no guarantee what the .app is named). On macOS this returns the path to "ares.app/Contents/Resources", on other platforms (in case they use it in the future) it just returns the same as `Path::program()`. * Fix an issue where, if Ares is stored in a sub-directory of the desktop, the path settings UI will display paths next to the program as relative to the desktop instead of relative to the program directory. --- desktop-ui/desktop-ui.cpp | 2 +- desktop-ui/settings/paths.cpp | 8 ++++---- mia/mia.cpp | 2 +- nall/path.cpp | 24 ++++++++++++++++++++++++ nall/path.hpp | 4 ++++ 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/desktop-ui/desktop-ui.cpp b/desktop-ui/desktop-ui.cpp index 1c00138ed..5742a80bf 100644 --- a/desktop-ui/desktop-ui.cpp +++ b/desktop-ui/desktop-ui.cpp @@ -14,7 +14,7 @@ auto locate(const string& name) -> string { // On macOS, also check the AppBundle Resource path #if defined(PLATFORM_MACOS) - location = {Path::program(), "../Resources/", name}; + location = {Path::resources(), name}; if(inode::exists(location)) return location; #endif diff --git a/desktop-ui/settings/paths.cpp b/desktop-ui/settings/paths.cpp index c2e594e99..39de6496e 100644 --- a/desktop-ui/settings/paths.cpp +++ b/desktop-ui/settings/paths.cpp @@ -118,14 +118,14 @@ auto PathSettings::construct() -> void { auto PathSettings::refresh() -> void { //simplifies pathnames by abbreviating the home folder and trailing slash auto pathname = [](string name) -> string { - if(name.beginsWith(Path::user())) { - name.trimLeft(Path::user(), 1L); - name.prepend("~/"); - } if(name.beginsWith(Path::program())) { name.trimLeft(Path::program(), 1L); name.prepend("./"); } + if(name.beginsWith(Path::user())) { + name.trimLeft(Path::user(), 1L); + name.prepend("~/"); + } if(name != "/") name.trimRight("/", 1L); return name; }; diff --git a/mia/mia.cpp b/mia/mia.cpp index a5b24530a..11dd8cbe8 100644 --- a/mia/mia.cpp +++ b/mia/mia.cpp @@ -16,7 +16,7 @@ auto locate(const string& name) -> string { // On macOS, also check the AppBundle Resource path #if defined(PLATFORM_MACOS) - location = {Path::program(), "../Resources/", name}; + location = {Path::resources(), name}; if(inode::exists(location)) return location; #endif diff --git a/nall/path.cpp b/nall/path.cpp index 02a12b9f3..265cf9fe3 100644 --- a/nall/path.cpp +++ b/nall/path.cpp @@ -2,6 +2,8 @@ #if defined(PLATFORM_WINDOWS) #include +#elif defined(PLATFORM_MACOS) + #include #endif namespace nall::Path { @@ -14,12 +16,34 @@ NALL_HEADER_INLINE auto program() -> string { result.transform("\\", "/"); return Path::real(result); #else + #if defined(PLATFORM_MACOS) + if (CFBundleRef bundle = CFBundleGetMainBundle()) { + char path[PATH_MAX] = ""; + CFURLRef url = CFBundleCopyBundleURL(bundle); + CFURLGetFileSystemRepresentation(url, true, reinterpret_cast(path), sizeof(path)); + CFRelease(url); + return Path::real(path); + } + #endif Dl_info info; dladdr((void*)&program, &info); return Path::real(info.dli_fname); #endif } +NALL_HEADER_INLINE auto resources() -> string { + #if defined(PLATFORM_MACOS) + if (CFBundleRef bundle = CFBundleGetMainBundle()) { + char path[PATH_MAX] = ""; + CFURLRef url = CFBundleCopyBundleURL(bundle); + CFURLGetFileSystemRepresentation(url, true, reinterpret_cast(path), sizeof(path)); + CFRelease(url); + return string(path).append("/Contents/Resources/"); + } + #endif + return program(); +} + NALL_HEADER_INLINE auto root() -> string { #if defined(PLATFORM_WINDOWS) wchar_t path[PATH_MAX] = L""; diff --git a/nall/path.hpp b/nall/path.hpp index b9b602a68..621811005 100644 --- a/nall/path.hpp +++ b/nall/path.hpp @@ -26,6 +26,10 @@ inline auto real(string_view name) -> string { auto program() -> string; +// program() +// ./ares.app/Contents/Resources/ +auto resources() -> string; + // / // c:/ auto root() -> string;