From 98a86b6eaed74693f89f13a11f2601a44386fbf8 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Mon, 14 Oct 2019 04:48:42 -0400 Subject: [PATCH] Add proper app bundle detection on Mac. --- src/asset.cpp | 19 +++++++++++-------- src/main.cpp | 3 +++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/asset.cpp b/src/asset.cpp index 4b1d2915..ca194aef 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -7,6 +7,7 @@ #if defined ARCH_MAC #include + #include #include #endif @@ -38,18 +39,20 @@ void init() { CFBundleRef bundle = CFBundleGetMainBundle(); assert(bundle); + // Check if we're running as a command-line program or an app bundle. CFURLRef bundleUrl = CFBundleCopyBundleURL(bundle); - char bundleBuf[PATH_MAX]; - Boolean success = CFURLGetFileSystemRepresentation(bundleUrl, TRUE, (UInt8*) bundleBuf, sizeof(bundleBuf)); - assert(success); - bundlePath = bundleBuf; - // If the bundle path doesn't end with ".app", assume it's a fake app bundle run from the command line. - if (string::filenameExtension(string::filename(bundlePath)) != "app") - bundlePath = ""; + // Thanks Ken Thomases! https://stackoverflow.com/a/58369256/272642 + CFStringRef uti; + if (CFURLCopyResourcePropertyForKey(bundleUrl, kCFURLTypeIdentifierKey, &uti, NULL) && uti && UTTypeConformsTo(uti, kUTTypeApplicationBundle)) { + char bundleBuf[PATH_MAX]; + Boolean success = CFURLGetFileSystemRepresentation(bundleUrl, TRUE, (UInt8*) bundleBuf, sizeof(bundleBuf)); + assert(success); + bundlePath = bundleBuf; + } CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle); char resourcesBuf[PATH_MAX]; - success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf)); + Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf)); assert(success); CFRelease(resourcesUrl); systemDir = resourcesBuf; diff --git a/src/main.cpp b/src/main.cpp index 7d1ec764..7379f31a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -127,6 +127,9 @@ int main(int argc, char* argv[]) { INFO("Development mode"); INFO("System directory: %s", asset::systemDir.c_str()); INFO("User directory: %s", asset::userDir.c_str()); +#if defined ARCH_MAC + INFO("Bundle path: %s", asset::bundlePath.c_str()); +#endif // Load settings try {