Browse Source

Add proper app bundle detection on Mac.

tags/v1.1.6
Andrew Belt 5 years ago
parent
commit
98a86b6eae
2 changed files with 14 additions and 8 deletions
  1. +11
    -8
      src/asset.cpp
  2. +3
    -0
      src/main.cpp

+ 11
- 8
src/asset.cpp View File

@@ -7,6 +7,7 @@


#if defined ARCH_MAC #if defined ARCH_MAC
#include <CoreFoundation/CoreFoundation.h> #include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#include <pwd.h> #include <pwd.h>
#endif #endif


@@ -38,18 +39,20 @@ void init() {
CFBundleRef bundle = CFBundleGetMainBundle(); CFBundleRef bundle = CFBundleGetMainBundle();
assert(bundle); assert(bundle);


// Check if we're running as a command-line program or an app bundle.
CFURLRef bundleUrl = CFBundleCopyBundleURL(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); CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle);
char resourcesBuf[PATH_MAX]; char resourcesBuf[PATH_MAX];
success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf));
Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf));
assert(success); assert(success);
CFRelease(resourcesUrl); CFRelease(resourcesUrl);
systemDir = resourcesBuf; systemDir = resourcesBuf;


+ 3
- 0
src/main.cpp View File

@@ -127,6 +127,9 @@ int main(int argc, char* argv[]) {
INFO("Development mode"); INFO("Development mode");
INFO("System directory: %s", asset::systemDir.c_str()); INFO("System directory: %s", asset::systemDir.c_str());
INFO("User directory: %s", asset::userDir.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 // Load settings
try { try {


Loading…
Cancel
Save