From f05ccf01287d972f9902388f65adbecb187451af Mon Sep 17 00:00:00 2001 From: dakriy Date: Sun, 20 Oct 2019 02:11:01 -0700 Subject: [PATCH] Only make the major modifications to the platform files --- src/Platform.c | 29 ++++++++++++++++++++++++++--- src/Platform.h | 4 ++++ src/Program.c | 2 +- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Platform.c b/src/Platform.c index e9f661cb3..fef4d0101 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -65,6 +65,7 @@ const cc_result ReturnCode_SocketWouldBlock = WSAEWOULDBLOCK; #define Socket__Error() errno #define NATIVE_STR_LEN 600 +static const char *Platform_DefaultDirectory = NULL; const cc_result ReturnCode_FileShareViolation = 1000000000; /* TODO: not used apparently */ const cc_result ReturnCode_FileNotFound = ENOENT; const cc_result ReturnCode_SocketInProgess = EINPROGRESS; @@ -1131,7 +1132,7 @@ static cc_result Process_RawGetExePath(char* path, int* len); cc_result Process_StartGame(const String* args) { char path[NATIVE_STR_LEN], raw[NATIVE_STR_LEN]; int i, j, len = 0; - char* argv[15]; + const char* argv[15]; cc_result res = Process_RawGetExePath(path, &len); if (res) return res; @@ -1148,6 +1149,9 @@ cc_result Process_StartGame(const String* args) { raw[i] = '\0'; argv[j++] = &raw[i + 1]; } + if (Platform_DefaultDirectory) { + argv[j++] = Platform_DefaultDirectory; + } argv[j] = NULL; return Process_RawStart(path, argv); } @@ -1768,13 +1772,32 @@ int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, String* args) #endif count = min(argc, GAME_MAX_CMDARGS); - for (i = 0; i < count; i++) { args[i] = String_FromReadonly(argv[i]); } + for (i = 0; i < count; i++) { + if (strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == 'd') { + --count; + continue; + } + args[i] = String_FromReadonly(argv[i]); + } return count; } -cc_result Platform_SetDefaultCurrentDirectory(void) { + +cc_result Platform_SetDefaultCurrentDirectory(int argc, char **argv) { char path[NATIVE_STR_LEN]; int i, len = 0; + + for (i = 1; i < argc; ++i) { + if (strlen(argv[i]) > 2 && argv[i][0] == '-' && argv[i][1] == 'd') { + Platform_DefaultDirectory = argv[i]; + break; + } + } + + if (Platform_DefaultDirectory) { + return chdir(Platform_DefaultDirectory + 2) == -1 ? errno : 0; + } + cc_result res = Process_RawGetExePath(path, &len); if (res) return res; diff --git a/src/Platform.h b/src/Platform.h index ca9e10cd3..7dc0dcbc0 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -41,7 +41,11 @@ void Platform_Init(void); /* Frees the platform specific state. */ void Platform_Free(void); /* Sets the appropriate default current/working directory. */ +#if defined CC_BUILD_POSIX +cc_result Platform_SetDefaultCurrentDirectory(int argc, char **argv); +#else cc_result Platform_SetDefaultCurrentDirectory(void); +#endif /* Gets the command line arguments passed to the program. */ int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, String* args); diff --git a/src/Program.c b/src/Program.c index 09bb11d67..260e342e0 100644 --- a/src/Program.c +++ b/src/Program.c @@ -169,7 +169,7 @@ int main(int argc, char** argv) { Platform_Init(); Window_Init(); - res = Platform_SetDefaultCurrentDirectory(); + res = Platform_SetDefaultCurrentDirectory(argc, argv); if (res) Logger_Warn(res, "setting current directory"); #ifdef CC_TEST_VORBIS main_imdct();