From 9d4e8d1e2cbe2d6da8840e32f0129ecb51c82ea6 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 26 Feb 2022 09:58:37 +1100 Subject: [PATCH] Also support space in game process command line arguments on unix/android/iOS platform backends --- src/Platform_Android.c | 25 +++++++++++++++---------- src/Platform_Posix.c | 19 +++++++------------ src/_GraphicsBase.h | 2 +- src/interop_ios.m | 25 ++++++++++++++++--------- 4 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/Platform_Android.c b/src/Platform_Android.c index 690419a88..482ca64ff 100644 --- a/src/Platform_Android.c +++ b/src/Platform_Android.c @@ -27,11 +27,15 @@ void Platform_Log(const char* msg, int len) { /*########################################################################################################################* *-----------------------------------------------------Process/Module------------------------------------------------------* *#########################################################################################################################*/ -static char gameArgsBuffer[512]; -static cc_string gameArgs = String_FromArray(gameArgsBuffer); +static char gameArgs[GAME_MAX_CMDARGS][STRING_SIZE]; +static int gameNumArgs; -cc_result Process_StartGame(const cc_string* args) { - String_Copy(&gameArgs, args); +cc_result Process_StartGame2(const cc_string* args, int numArgs) { + for (int i = 0; i < numArgs; i++) { + String_CopyToRawArray(gameArgs[i], &args[i]); + } + + gameNumArgs = numArgs; return 0; } @@ -94,13 +98,14 @@ void Platform_ShareScreenshot(const cc_string* filename) { *-----------------------------------------------------Configuration-------------------------------------------------------* *#########################################################################################################################*/ int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, cc_string* args) { - int count = 0; - if (gameArgs.length) { - count = String_UNSAFE_Split(&gameArgs, ' ', args, GAME_MAX_CMDARGS); - /* clear arguments so after game is closed, launcher is started */ - gameArgs.length = 0; + int count = gameNumArgs; + for (int i = 0; i < count; i++) { + args[i] = String_FromRawArray(gameArgs[i]); } - return count; + + // clear arguments so after game is closed, launcher is started + gameNumArgs = 0; + return count; } cc_result Platform_SetDefaultCurrentDirectory(int argc, char **argv) { diff --git a/src/Platform_Posix.c b/src/Platform_Posix.c index 66eaf599f..ff13cf217 100644 --- a/src/Platform_Posix.c +++ b/src/Platform_Posix.c @@ -622,25 +622,20 @@ static cc_result Process_RawStart(const char* path, char** argv) { static cc_result Process_RawGetExePath(char* path, int* len); -cc_result Process_StartGame(const cc_string* args) { - char path[NATIVE_STR_LEN], raw[NATIVE_STR_LEN]; +cc_result Process_StartGame2(const cc_string* args, int numArgs) { + char raw[GAME_MAX_CMDARGS][NATIVE_STR_LEN]; + char path[NATIVE_STR_LEN]; int i, j, len = 0; char* argv[15]; cc_result res = Process_RawGetExePath(path, &len); if (res) return res; path[len] = '\0'; + argv[0] = path; - Platform_EncodeUtf8(raw, args); - argv[0] = path; argv[1] = raw; - - /* need to null-terminate multiple arguments */ - for (i = 0, j = 2; raw[i] && i < Array_Elems(raw); i++) { - if (raw[i] != ' ') continue; - - /* null terminate previous argument */ - raw[i] = '\0'; - argv[j++] = &raw[i + 1]; + for (i = 0, j = 1; i < numArgs; i++, j++) { + Platform_EncodeUtf8(raw[i], &args[i]); + argv[j] = raw[i]; } if (defaultDirectory) { argv[j++] = defaultDirectory; } diff --git a/src/_GraphicsBase.h b/src/_GraphicsBase.h index b5ec63987..7923dac1a 100644 --- a/src/_GraphicsBase.h +++ b/src/_GraphicsBase.h @@ -85,7 +85,7 @@ static void LimitFPS(void) { float cooldown = gfx_targetTime - gfx_actualTime; Thread_Sleep((int)(cooldown + 0.5f)); - /* also accumulate Thread_Sleep duration, as actual sleep + /* also accumulate Thread_Sleep duration, as actual sleep */ /* duration can significantly deviate from requested time */ /* (e.g. requested 4ms, but actually slept for 8ms) */ cc_uint64 sleepEnd = Stopwatch_Measure(); diff --git a/src/interop_ios.m b/src/interop_ios.m index f14cea482..c8f86679b 100644 --- a/src/interop_ios.m +++ b/src/interop_ios.m @@ -406,6 +406,9 @@ cc_result Updater_SetNewBuildTime(cc_uint64 t) { return ERR_NOT_SUPPORTED; } /*########################################################################################################################* *--------------------------------------------------------Platform--------------------------------------------------------* *#########################################################################################################################*/ +static char gameArgs[GAME_MAX_CMDARGS][STRING_SIZE]; +static int gameNumArgs; + cc_result Process_StartOpen(const cc_string* args) { char raw[NATIVE_STR_LEN]; NSURL* url; @@ -418,19 +421,23 @@ cc_result Process_StartOpen(const cc_string* args) { return 0; } -static char gameArgsBuffer[512]; -static cc_string gameArgs = String_FromArray(gameArgsBuffer); -cc_result Process_StartGame(const cc_string* args) { - String_Copy(&gameArgs, args); +cc_result Process_StartGame2(const cc_string* args, int numArgs) { + for (int i = 0; i < numArgs; i++) { + String_CopyToRawArray(gameArgs[i], &args[i]); + } + + gameNumArgs = numArgs; return 0; } int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, cc_string* args) { - if (!gameArgs.length) return 0; - - int count = String_UNSAFE_Split(&gameArgs, ' ', args, GAME_MAX_CMDARGS); - // clear arguments so after game is closed, launcher is started again - gameArgs.length = 0; + int count = gameNumArgs; + for (int i = 0; i < count; i++) { + args[i] = String_FromRawArray(gameArgs[i]); + } + + // clear arguments so after game is closed, launcher is started + gameNumArgs = 0; return count; }