From 1d93a62cca3f3ce0b3e2830f03b7caecd55d94ca Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 11 Sep 2019 07:28:47 +1000 Subject: [PATCH] Detect if .exe is in a bundle on OSX, and if so, set current directory to folder containing bundle instead of contents/macos folder --- src/Platform.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Platform.c b/src/Platform.c index 0010c2a8b..79d2345e1 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -1720,8 +1720,8 @@ ReturnCode Platform_SetDefaultCurrentDirectory(void) { int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, String* args) { int i, count; argc--; argv++; /* skip executable path argument */ - count = min(argc, GAME_MAX_CMDARGS); + count = min(argc, GAME_MAX_CMDARGS); for (i = 0; i < count; i++) { args[i] = String_FromReadonly(argv[i]); } return count; } @@ -1751,13 +1751,13 @@ int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, String* args) #ifdef CC_BUILD_OSX if (argc) { - String arg0 = String_FromReadonly(argv[0]); - String psn = String_FromConst("-psn_0_"); + static const String psn = String_FromConst("-psn_0_"); + String arg0 = String_FromReadonly(argv[0]); if (String_CaselessStarts(&arg0, &psn)) { argc--; argv++; } } #endif - count = min(argc, GAME_MAX_CMDARGS); + count = min(argc, GAME_MAX_CMDARGS); for (i = 0; i < count; i++) { args[i] = String_FromReadonly(argv[i]); } return count; } @@ -1773,6 +1773,19 @@ ReturnCode Platform_SetDefaultCurrentDirectory(void) { if (path[i] == '/') break; } +#ifdef CC_BUILD_OSX + static const String bundle = String_FromConst(".app/Contents/MacOS/"); + String raw = String_Init(path, len, 0); + + if (String_CaselessEnds(&raw, &bundle)) { + len -= bundle.length; + + for (i = len - 1; i >= 0; i--, len--) { + if (path[i] == '/') break; + } + } +#endif + path[len] = '\0'; return chdir(path) == -1 ? errno : 0; }