From ff77a230567da10e13458bf44b139eb1a20c69cc Mon Sep 17 00:00:00 2001 From: Joel Davis Date: Sat, 29 Apr 2017 14:43:30 -0700 Subject: [PATCH] Use resource bundle path for root: on MacOS and iOS --- code/Modules/LocalFS/LocalFileSystem.cc | 2 +- code/Modules/LocalFS/posix/posixFSWrapper.cc | 26 +++++++++++++++++++- code/Modules/LocalFS/posix/posixFSWrapper.h | 4 +-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/code/Modules/LocalFS/LocalFileSystem.cc b/code/Modules/LocalFS/LocalFileSystem.cc index dacc93c58..20709be8e 100644 --- a/code/Modules/LocalFS/LocalFileSystem.cc +++ b/code/Modules/LocalFS/LocalFileSystem.cc @@ -18,7 +18,7 @@ LocalFileSystem::init(const StringAtom& scheme_) { StringBuilder strBuilder; // setup the root assign - strBuilder.Format(4096, "%s:///%s", this->scheme.AsCStr(), fsWrapper::getExecutableDir().AsCStr()); + strBuilder.Format(4096, "%s:///%s", this->scheme.AsCStr(), fsWrapper::getRootDir().AsCStr()); IO::SetAssign("root:", strBuilder.GetString()); // setup the cwd assign diff --git a/code/Modules/LocalFS/posix/posixFSWrapper.cc b/code/Modules/LocalFS/posix/posixFSWrapper.cc index 4bdac84c2..4933e125d 100644 --- a/code/Modules/LocalFS/posix/posixFSWrapper.cc +++ b/code/Modules/LocalFS/posix/posixFSWrapper.cc @@ -11,6 +11,9 @@ #else #include #endif +#if ORYOL_MACOS || ORYOL_IOS +#include +#endif namespace Oryol { namespace _priv { @@ -75,7 +78,27 @@ posixFSWrapper::close(handle h) { //------------------------------------------------------------------------------ String -posixFSWrapper::getExecutableDir() { +posixFSWrapper::getRootDir() { +#if ORYOL_MACOS || ORYOL_IOS + // Get the main bundle for the app + char bundlePath[4096]; + CFBundleRef mainBundle = CFBundleGetMainBundle(); + CFURLRef mainBundleURL = CFBundleCopyBundleURL( mainBundle); + CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); + CFStringGetCString( cfStringRef, bundlePath, 4096, kCFStringEncodingASCII); + + // Append the resources directory + StringBuilder strBuilder(bundlePath, 0, (int)CFStringGetLength(cfStringRef)); +#if ORYOL_MACOS + // On iOS, bundled files live at the toplevel app bundle. On MacOS, they live + // in the resources directory beneath that. + strBuilder.Append( "/Contents/Resources"); +#endif + strBuilder.Append( "/"); + return strBuilder.GetString(); + +#else + // Anything else (such as windows) use the executable path char buf[4096]; int length = wai_getExecutablePath(buf, sizeof(buf), nullptr); if (length > 0) { @@ -89,6 +112,7 @@ posixFSWrapper::getExecutableDir() { else { return String(); } + #endif } //------------------------------------------------------------------------------ diff --git a/code/Modules/LocalFS/posix/posixFSWrapper.h b/code/Modules/LocalFS/posix/posixFSWrapper.h index 91923feb7..8063d005e 100644 --- a/code/Modules/LocalFS/posix/posixFSWrapper.h +++ b/code/Modules/LocalFS/posix/posixFSWrapper.h @@ -33,8 +33,8 @@ class posixFSWrapper { /// close file static void close(handle f); - /// get path to own executable - static String getExecutableDir(); + /// get path to where application resources are typically stored + static String getRootDir(); /// get current directory static String getCwd(); };