From d298f3ff5746f1d60d5f72190f580269b1dcdf9c Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 8 Sep 2018 14:35:07 +1000 Subject: [PATCH] C client: command line args read on linux, dropping map onto client works too --- src/Constants.h | 1 + src/Game.c | 2 +- src/Platform.c | 60 ++++++++++++++++++++++++++++++++++++------------- src/Platform.h | 2 +- src/Program.c | 14 ++++++------ 5 files changed, 54 insertions(+), 25 deletions(-) diff --git a/src/Constants.h b/src/Constants.h index c9d1ac32a..49a0aa947 100644 --- a/src/Constants.h +++ b/src/Constants.h @@ -4,6 +4,7 @@ Copyright 2017 ClassicalSharp | Licensed under BSD-3 */ +#define PROGRAM_MAX_CMDARGS 5 #define PROGRAM_APP_NAME "ClassiCube 0.99.9.2" #define USE16_BIT FALSE diff --git a/src/Game.c b/src/Game.c index edee77beb..7ccc17689 100644 --- a/src/Game.c +++ b/src/Game.c @@ -42,7 +42,7 @@ Int32 Game_ComponentsCount; struct ScheduledTask Game_Tasks[6]; Int32 Game_TasksCount, entTaskI; -char Game_UsernameBuffer[STRING_SIZE]; +char Game_UsernameBuffer[FILENAME_SIZE]; String Game_Username = String_FromArray(Game_UsernameBuffer); char Game_MppassBuffer[STRING_SIZE]; String Game_Mppass = String_FromArray(Game_MppassBuffer); diff --git a/src/Platform.c b/src/Platform.c index 2a19586c5..14bc0ea6c 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -1283,25 +1283,43 @@ ReturnCode Platform_StartShell(STRING_PURE String* args) { return instance > 32 ? 0 : (ReturnCode)instance; } -STRING_PURE String Platform_GetCommandLineArgs(void) { - String args = String_FromReadonly(GetCommandLineA()); +static String Platform_NextArg(STRING_REF String* args) { + /* get rid of leading spaces before arg */ + while (args->length && args->buffer[0] == ' ') { + *args = String_UNSAFE_SubstringAt(args, 1); + } - Int32 argsStart; - if (args.buffer[0] == '"') { - /* Handle path argument in full "path" form, which can include spaces */ - argsStart = String_IndexOf(&args, '"', 1) + 1; + Int32 end; + if (args->length && args->buffer[0] == '"') { + /* "xy za" is used for arg with spaces */ + *args = String_UNSAFE_SubstringAt(args, 1); + end = String_IndexOf(args, '"', 0); } else { - argsStart = String_IndexOf(&args, ' ', 0) + 1; + end = String_IndexOf(args, ' ', 0); } - if (argsStart == 0) argsStart = args.length; - args = String_UNSAFE_SubstringAt(&args, argsStart); - - /* get rid of duplicate leading spaces before first arg */ - while (args.length && args.buffer[0] == ' ') { - args = String_UNSAFE_SubstringAt(&args, 1); + String arg; + if (end == -1) { + arg = *args; + args->length = 0; + } else { + arg = String_UNSAFE_Substring(args, 0, end); + *args = String_UNSAFE_SubstringAt(args, end + 1); } - return args; + return arg; +} + +Int32 Platform_GetCommandLineArgs(int argc, char** argv, STRING_TRANSIENT String* args) { + String cmdArgs = String_FromReadonly(GetCommandLineA()); + Platform_NextArg(&cmdArgs); /* skip exe path */ + + Int32 count; + for (count = 0; count < PROGRAM_MAX_CMDARGS; count++) { + args[count] = Platform_NextArg(&cmdArgs); + + if (!args[count].length) break; + } + return count; } #elif CC_BUILD_NIX void Platform_ConvertString(void* dstPtr, STRING_PURE String* src) { @@ -1326,9 +1344,9 @@ static void Platform_InitDisplay(void) { /* TODO: Use Xinerama and XRandR for querying these */ struct DisplayDevice device = { 0 }; - device.Bounds.Width = DisplayWidth(display, screen); + device.Bounds.Width = DisplayWidth(display, screen); device.Bounds.Height = DisplayHeight(display, screen); - device.BitsPerPixel = DefaultDepth(display, screen); + device.BitsPerPixel = DefaultDepth(display, screen); DisplayDevice_Default = device; DisplayDevice_Meta[0] = display; @@ -1370,4 +1388,14 @@ ReturnCode Platform_StartShell(STRING_PURE String* args) { if (!fp) return errno; return Nix_Return(pclose(fp)); } + +Int32 Platform_GetCommandLineArgs(int argc, char** argv, STRING_TRANSIENT String* args) { + argc--; /* skip path argument*/ + Int32 i, count = min(argc, PROGRAM_MAX_CMDARGS); + + for (i = 0; i < count; i++) { + args[i] = String_FromReadonly(argv[i]); + } + return count; +} #endif diff --git a/src/Platform.h b/src/Platform.h index f8544463f..0fb5d44b2 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -32,7 +32,7 @@ void Platform_Init(void); void Platform_Free(void); void Platform_SetWorkingDir(void); void Platform_Exit(ReturnCode code); -STRING_PURE String Platform_GetCommandLineArgs(void); +Int32 Platform_GetCommandLineArgs(int argc, char** argv, STRING_TRANSIENT String* args); ReturnCode Platform_StartShell(STRING_PURE String* args); NOINLINE_ void* Mem_Alloc(UInt32 numElems, UInt32 elemsSize, const char* place); diff --git a/src/Program.c b/src/Program.c index 28ae51f4b..073fe7a03 100644 --- a/src/Program.c +++ b/src/Program.c @@ -39,7 +39,7 @@ int main_imdct() { } #endif -int main(void) { +int main(int argc, char** argv) { Platform_SetWorkingDir(); ErrorHandler_Init("client.log"); Platform_Init(); @@ -61,13 +61,13 @@ int main(void) { Platform_Exit(1); return 1; } - String title = String_FromConst(PROGRAM_APP_NAME); - String rawArgs = Platform_GetCommandLineArgs(); - /* NOTE: Make sure to comment this out before pushing a commit */ - //rawArgs = String_FromReadonly("UnknownShadow200 fff 127.0.0.1 25565"); + String args[PROGRAM_MAX_CMDARGS]; + String title = String_FromConst(PROGRAM_APP_NAME); + Int32 argsCount = Platform_GetCommandLineArgs(argc, argv, args); - String args[5]; Int32 argsCount = Array_Elems(args); - String_UNSAFE_Split(&rawArgs, ' ', args, &argsCount); + /* NOTE: Make sure to comment this out before pushing a commit */ + // String rawArgs = String_FromConst("UnknownShadow200 fff 127.0.0.1 25565"); + // argsCount = 4; String_UNSAFE_Split(&rawArgs, ' ', args, &argsCount); if (argsCount == 1) { String name = args[0];