Skip to content

Commit

Permalink
nall: Account for macOS app bundles in program path. (#1373)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Steveice10 authored Jan 22, 2024
1 parent d823641 commit d6b2ca5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion desktop-ui/desktop-ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions desktop-ui/settings/paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion mia/mia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 24 additions & 0 deletions nall/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#if defined(PLATFORM_WINDOWS)
#include <shlobj.h>
#elif defined(PLATFORM_MACOS)
#include <CoreFoundation/CFBundle.h>
#endif

namespace nall::Path {
Expand All @@ -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<UInt8*>(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<UInt8*>(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"";
Expand Down
4 changes: 4 additions & 0 deletions nall/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d6b2ca5

Please sign in to comment.