Merge pull request #934 from UnknownShadow200/GameArgsFix

Support spaces in command line arguments when starting game from launcher
This commit is contained in:
UnknownShadow200 2022-02-26 11:15:30 +11:00 committed by GitHub
commit 4f79864d5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 65 additions and 44 deletions

View File

@ -86,7 +86,7 @@ static CC_NOINLINE void InitFramebuffer(void) {
*#########################################################################################################################*/ *#########################################################################################################################*/
static cc_uint64 lastJoin; static cc_uint64 lastJoin;
cc_bool Launcher_StartGame(const cc_string* user, const cc_string* mppass, const cc_string* ip, const cc_string* port, const cc_string* server) { cc_bool Launcher_StartGame(const cc_string* user, const cc_string* mppass, const cc_string* ip, const cc_string* port, const cc_string* server) {
cc_string args; char argsBuffer[512]; cc_string args[4]; int numArgs;
TimeMS now; TimeMS now;
cc_result res; cc_result res;
@ -106,11 +106,16 @@ cc_bool Launcher_StartGame(const cc_string* user, const cc_string* mppass, const
/* Otherwise can get 'file already in use' errors on startup */ /* Otherwise can get 'file already in use' errors on startup */
Options_SaveIfChanged(); Options_SaveIfChanged();
String_InitArray(args, argsBuffer); args[0] = *user;
String_AppendString(&args, user); numArgs = 1;
if (mppass->length) String_Format3(&args, " %s %s %s", mppass, ip, port); if (mppass->length) {
args[1] = *mppass;
args[2] = *ip;
args[3] = *port;
numArgs = 4;
}
res = Process_StartGame(&args); res = Process_StartGame2(args, numArgs);
if (res) { Logger_SysWarn(res, "starting game"); return false; } if (res) { Logger_SysWarn(res, "starting game"); return false; }
#ifdef CC_BUILD_MOBILE #ifdef CC_BUILD_MOBILE

View File

@ -63,7 +63,7 @@ describe exists (e.g. Http_DescribeError), that should be preferred. */
cc_bool Platform_DescribeError(cc_result res, cc_string* dst); cc_bool Platform_DescribeError(cc_result res, cc_string* dst);
/* Starts the game with the given arguments. */ /* Starts the game with the given arguments. */
CC_API cc_result Process_StartGame(const cc_string* args); CC_API cc_result Process_StartGame2(const cc_string* args, int numArgs);
/* Terminates the process with the given return code. */ /* Terminates the process with the given return code. */
CC_API void Process_Exit(cc_result code); CC_API void Process_Exit(cc_result code);
/* Starts the platform-specific program to open the given url or filename. */ /* Starts the platform-specific program to open the given url or filename. */

View File

@ -27,11 +27,15 @@ void Platform_Log(const char* msg, int len) {
/*########################################################################################################################* /*########################################################################################################################*
*-----------------------------------------------------Process/Module------------------------------------------------------* *-----------------------------------------------------Process/Module------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
static char gameArgsBuffer[512]; static char gameArgs[GAME_MAX_CMDARGS][STRING_SIZE];
static cc_string gameArgs = String_FromArray(gameArgsBuffer); static int gameNumArgs;
cc_result Process_StartGame(const cc_string* args) { cc_result Process_StartGame2(const cc_string* args, int numArgs) {
String_Copy(&gameArgs, args); for (int i = 0; i < numArgs; i++) {
String_CopyToRawArray(gameArgs[i], &args[i]);
}
gameNumArgs = numArgs;
return 0; return 0;
} }
@ -94,13 +98,14 @@ void Platform_ShareScreenshot(const cc_string* filename) {
*-----------------------------------------------------Configuration-------------------------------------------------------* *-----------------------------------------------------Configuration-------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, cc_string* args) { int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, cc_string* args) {
int count = 0; int count = gameNumArgs;
if (gameArgs.length) { for (int i = 0; i < count; i++) {
count = String_UNSAFE_Split(&gameArgs, ' ', args, GAME_MAX_CMDARGS); args[i] = String_FromRawArray(gameArgs[i]);
/* clear arguments so after game is closed, launcher is started */
gameArgs.length = 0;
} }
return count;
// clear arguments so after game is closed, launcher is started
gameNumArgs = 0;
return count;
} }
cc_result Platform_SetDefaultCurrentDirectory(int argc, char **argv) { cc_result Platform_SetDefaultCurrentDirectory(int argc, char **argv) {

View File

@ -622,25 +622,20 @@ static cc_result Process_RawStart(const char* path, char** argv) {
static cc_result Process_RawGetExePath(char* path, int* len); static cc_result Process_RawGetExePath(char* path, int* len);
cc_result Process_StartGame(const cc_string* args) { cc_result Process_StartGame2(const cc_string* args, int numArgs) {
char path[NATIVE_STR_LEN], raw[NATIVE_STR_LEN]; char raw[GAME_MAX_CMDARGS][NATIVE_STR_LEN];
char path[NATIVE_STR_LEN];
int i, j, len = 0; int i, j, len = 0;
char* argv[15]; char* argv[15];
cc_result res = Process_RawGetExePath(path, &len); cc_result res = Process_RawGetExePath(path, &len);
if (res) return res; if (res) return res;
path[len] = '\0'; path[len] = '\0';
argv[0] = path;
Platform_EncodeUtf8(raw, args); for (i = 0, j = 1; i < numArgs; i++, j++) {
argv[0] = path; argv[1] = raw; Platform_EncodeUtf8(raw[i], &args[i]);
argv[j] = raw[i];
/* 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];
} }
if (defaultDirectory) { argv[j++] = defaultDirectory; } if (defaultDirectory) { argv[j++] = defaultDirectory; }

View File

@ -357,7 +357,9 @@ cc_result Socket_Poll(cc_socket s, int mode, cc_bool* success) {
/*########################################################################################################################* /*########################################################################################################################*
*-----------------------------------------------------Process/Module------------------------------------------------------* *-----------------------------------------------------Process/Module------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
cc_result Process_StartGame(const cc_string* args) { return ERR_NOT_SUPPORTED; } cc_result Process_StartGame(const cc_string* args, int numArgs) {
return ERR_NOT_SUPPORTED;
}
void Process_Exit(cc_result code) { void Process_Exit(cc_result code) {
/* Window isn't implicitly closed when process is exited */ /* Window isn't implicitly closed when process is exited */
if (code) Window_Close(); if (code) Window_Close();

View File

@ -547,13 +547,13 @@ static cc_result Process_RawGetExePath(WCHAR* path, int* len) {
return *len ? 0 : GetLastError(); return *len ? 0 : GetLastError();
} }
cc_result Process_StartGame(const cc_string* args) { cc_result Process_StartGame2(const cc_string* args, int numArgs) {
WCHAR path[NATIVE_STR_LEN + 1], raw[NATIVE_STR_LEN]; WCHAR path[NATIVE_STR_LEN + 1], raw[NATIVE_STR_LEN];
cc_string argv; char argvBuffer[NATIVE_STR_LEN]; cc_string argv; char argvBuffer[NATIVE_STR_LEN];
STARTUPINFOW si = { 0 }; STARTUPINFOW si = { 0 };
PROCESS_INFORMATION pi = { 0 }; PROCESS_INFORMATION pi = { 0 };
cc_result res; cc_result res;
int len; int len, i;
Process_RawGetExePath(path, &len); Process_RawGetExePath(path, &len);
path[len] = '\0'; path[len] = '\0';
@ -561,8 +561,15 @@ cc_result Process_StartGame(const cc_string* args) {
String_InitArray(argv, argvBuffer); String_InitArray(argv, argvBuffer);
/* Game doesn't actually care about argv[0] */ /* Game doesn't actually care about argv[0] */
String_Format1(&argv, "cc %s", args); String_AppendConst(&argv, "cc");
String_UNSAFE_TrimEnd(&argv); for (i = 0; i < numArgs; i++)
{
if (String_IndexOf(&args[i], ' ') >= 0) {
String_Format1(&argv, " \"%s\"", &args[i]);
} else {
String_Format1(&argv, " %s", &args[i]);
}
}
Platform_EncodeUtf16(raw, &argv); Platform_EncodeUtf16(raw, &argv);
if (CreateProcessW(path, raw, NULL, NULL, if (CreateProcessW(path, raw, NULL, NULL,
@ -636,7 +643,7 @@ cc_result Updater_Start(const char** action) {
if (!MoveFileExW(UPDATE_SRC, path, MOVEFILE_REPLACE_EXISTING)) return GetLastError(); if (!MoveFileExW(UPDATE_SRC, path, MOVEFILE_REPLACE_EXISTING)) return GetLastError();
*action = "Restarting game"; *action = "Restarting game";
return Process_StartGame(&String_Empty); return Process_StartGame2(NULL, 0);
} }
cc_result Updater_GetBuildTime(cc_uint64* timestamp) { cc_result Updater_GetBuildTime(cc_uint64* timestamp) {

View File

@ -85,7 +85,7 @@ static void LimitFPS(void) {
float cooldown = gfx_targetTime - gfx_actualTime; float cooldown = gfx_targetTime - gfx_actualTime;
Thread_Sleep((int)(cooldown + 0.5f)); 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 */ /* duration can significantly deviate from requested time */
/* (e.g. requested 4ms, but actually slept for 8ms) */ /* (e.g. requested 4ms, but actually slept for 8ms) */
cc_uint64 sleepEnd = Stopwatch_Measure(); cc_uint64 sleepEnd = Stopwatch_Measure();

View File

@ -406,6 +406,9 @@ cc_result Updater_SetNewBuildTime(cc_uint64 t) { return ERR_NOT_SUPPORTED; }
/*########################################################################################################################* /*########################################################################################################################*
*--------------------------------------------------------Platform--------------------------------------------------------* *--------------------------------------------------------Platform--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
static char gameArgs[GAME_MAX_CMDARGS][STRING_SIZE];
static int gameNumArgs;
cc_result Process_StartOpen(const cc_string* args) { cc_result Process_StartOpen(const cc_string* args) {
char raw[NATIVE_STR_LEN]; char raw[NATIVE_STR_LEN];
NSURL* url; NSURL* url;
@ -418,19 +421,23 @@ cc_result Process_StartOpen(const cc_string* args) {
return 0; return 0;
} }
static char gameArgsBuffer[512]; cc_result Process_StartGame2(const cc_string* args, int numArgs) {
static cc_string gameArgs = String_FromArray(gameArgsBuffer); for (int i = 0; i < numArgs; i++) {
cc_result Process_StartGame(const cc_string* args) { String_CopyToRawArray(gameArgs[i], &args[i]);
String_Copy(&gameArgs, args); }
gameNumArgs = numArgs;
return 0; return 0;
} }
int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, cc_string* args) { int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, cc_string* args) {
if (!gameArgs.length) return 0; int count = gameNumArgs;
for (int i = 0; i < count; i++) {
int count = String_UNSAFE_Split(&gameArgs, ' ', args, GAME_MAX_CMDARGS); args[i] = String_FromRawArray(gameArgs[i]);
// clear arguments so after game is closed, launcher is started again }
gameArgs.length = 0;
// clear arguments so after game is closed, launcher is started
gameNumArgs = 0;
return count; return count;
} }