Copy implementation from lws.feature/data-structs
@@ -81,7 +81,7 @@ IZ_ProcedureResult IZ_AppInitialize(IZ_App* app, u8 argc, const char* argv[]) { | |||||
const char* cmdline_buffer; | const char* cmdline_buffer; | ||||
char config_path[128]; | char config_path[128]; | ||||
// TODO abstract command line args parsing | // TODO abstract command line args parsing | ||||
if ((cmdline_buffer = lws_cmdline_option(argc, argv, "-c"))) { | |||||
if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-c"))) { | |||||
memcpy_s(config_path, 128, cmdline_buffer, 128); | memcpy_s(config_path, 128, cmdline_buffer, 128); | ||||
} else { | } else { | ||||
IZ_ConfigGetDefaultPath(config_path, 128); | IZ_ConfigGetDefaultPath(config_path, 128); | ||||
@@ -6,3 +6,26 @@ void IZ_ConfigGetDefaultPath(const char* config_path, size_t string_size) { | |||||
memcpy_s(config_path, string_size, config_path_dir, 128); | memcpy_s(config_path, string_size, config_path_dir, 128); | ||||
strcat_s(config_path, string_size, "config-game.ini"); | strcat_s(config_path, string_size, "config-game.ini"); | ||||
} | } | ||||
const char* IZ_ConfigGetCommandlineOption(u8 argc, const char* argv[], const char* val) { | |||||
size_t n = strlen(val); | |||||
int c = argc; | |||||
while (--c > 0) { | |||||
if (!strncmp(argv[c], val, n)) { | |||||
if (!*(argv[c] + n) && c < argc - 1) { | |||||
/* coverity treats unchecked argv as "tainted" */ | |||||
if (!argv[c + 1] || strlen(argv[c + 1]) > 1024) | |||||
return NULL; | |||||
return argv[c + 1]; | |||||
} | |||||
if (argv[c][n] == '=') | |||||
return &argv[c][n + 1]; | |||||
return argv[c] + n; | |||||
} | |||||
} | |||||
return NULL; | |||||
} |
@@ -3,8 +3,11 @@ | |||||
#include <SDL_filesystem.h> | #include <SDL_filesystem.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include "IZ_common.h" | |||||
// TODO unify loading of config from cmdline and config file | // TODO unify loading of config from cmdline and config file | ||||
void IZ_ConfigGetDefaultPath(const char*, size_t); | void IZ_ConfigGetDefaultPath(const char*, size_t); | ||||
const char* IZ_ConfigGetCommandlineOption(u8, const char**, const char*); | |||||
#endif | #endif |
@@ -20,6 +20,13 @@ void IZ_VideoLoadConfig(IZ_VideoState* state, const char* config_path) { | |||||
state->config.max_fps = ini_getl("Video", "MaxFps", IZ_DEFAULT_VIDEO_STATE.config.max_fps, config_path); | state->config.max_fps = ini_getl("Video", "MaxFps", IZ_DEFAULT_VIDEO_STATE.config.max_fps, config_path); | ||||
} | } | ||||
void IZ_VideoOverrideConfig(IZ_VideoState* state, u8 argc, const char* argv[]) { | |||||
const char* cmdline_buffer; | |||||
if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-f"))) { | |||||
state->config.max_fps = atoi(cmdline_buffer); | |||||
} | |||||
} | |||||
IZ_ProcedureResult IZ_VideoInitialize(IZ_VideoState* state, const char* config_path, u8 argc, const char* argv[]) { | IZ_ProcedureResult IZ_VideoInitialize(IZ_VideoState* state, const char* config_path, u8 argc, const char* argv[]) { | ||||
SDL_memcpy(state, &IZ_DEFAULT_VIDEO_STATE, sizeof(IZ_VideoState)); | SDL_memcpy(state, &IZ_DEFAULT_VIDEO_STATE, sizeof(IZ_VideoState)); | ||||
@@ -27,6 +34,7 @@ IZ_ProcedureResult IZ_VideoInitialize(IZ_VideoState* state, const char* config_p | |||||
if (IZ_VideoSaveConfig(state, config_path)) { | if (IZ_VideoSaveConfig(state, config_path)) { | ||||
// fprintf_s(stderr, "Error committing video config.\n"); | // fprintf_s(stderr, "Error committing video config.\n"); | ||||
} | } | ||||
IZ_VideoOverrideConfig(state, argc, argv); | |||||
state->last_update_at = 0u; | state->last_update_at = 0u; | ||||
SDL_Window* window = SDL_CreateWindow( | SDL_Window* window = SDL_CreateWindow( | ||||
@@ -18,7 +18,7 @@ IZ_ProcedureResult IZ_AppInitialize(IZ_App *app, u8 argc, const char **argv) { | |||||
const char* cmdline_buffer; | const char* cmdline_buffer; | ||||
char config_path[128]; | char config_path[128]; | ||||
// TODO abstract command line args parsing | // TODO abstract command line args parsing | ||||
if ((cmdline_buffer = lws_cmdline_option(argc, argv, "-c"))) { | |||||
if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-c"))) { | |||||
memcpy_s(config_path, 128, cmdline_buffer, 128); | memcpy_s(config_path, 128, cmdline_buffer, 128); | ||||
} else { | } else { | ||||
IZ_ConfigGetDefaultPath(config_path, 128); | IZ_ConfigGetDefaultPath(config_path, 128); | ||||
@@ -6,3 +6,26 @@ void IZ_ConfigGetDefaultPath(const char* config_path, size_t string_size) { | |||||
memcpy_s(config_path, string_size, config_path_dir, 128); | memcpy_s(config_path, string_size, config_path_dir, 128); | ||||
strcat_s(config_path, string_size, "config-server.ini"); | strcat_s(config_path, string_size, "config-server.ini"); | ||||
} | } | ||||
const char* IZ_ConfigGetCommandlineOption(u8 argc, const char* argv[], const char* val) { | |||||
size_t n = strlen(val); | |||||
int c = argc; | |||||
while (--c > 0) { | |||||
if (!strncmp(argv[c], val, n)) { | |||||
if (!*(argv[c] + n) && c < argc - 1) { | |||||
/* coverity treats unchecked argv as "tainted" */ | |||||
if (!argv[c + 1] || strlen(argv[c + 1]) > 1024) | |||||
return NULL; | |||||
return argv[c + 1]; | |||||
} | |||||
if (argv[c][n] == '=') | |||||
return &argv[c][n + 1]; | |||||
return argv[c] + n; | |||||
} | |||||
} | |||||
return NULL; | |||||
} |
@@ -3,8 +3,11 @@ | |||||
#include <SDL_filesystem.h> | #include <SDL_filesystem.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include "IZ_common.h" | |||||
// TODO unify loading of config from cmdline and config file | // TODO unify loading of config from cmdline and config file | ||||
void IZ_ConfigGetDefaultPath(const char*, size_t); | void IZ_ConfigGetDefaultPath(const char*, size_t); | ||||
const char* IZ_ConfigGetCommandlineOption(u8, const char**, const char*); | |||||
#endif | #endif |
@@ -1,6 +1,6 @@ | |||||
#include "IZ_app.h" | #include "IZ_app.h" | ||||
IZ_ProcedureResult main(i32 argc, const char *argv[]) { | |||||
IZ_ProcedureResult main(i32 argc, char* argv[]) { | |||||
IZ_App app; | IZ_App app; | ||||
return IZ_AppRun(&app, argc, argv); | |||||
return IZ_AppRun(&app, argc, (const char**) argv); | |||||
} | } |
@@ -39,7 +39,7 @@ void IZ_WSServerLoadConfig(IZ_WSServerState* state, const char* config_path, u8 | |||||
// state->config.log_level = atoi(cmdline_buffer); | // state->config.log_level = atoi(cmdline_buffer); | ||||
// } | // } | ||||
if ((cmdline_buffer = lws_cmdline_option(argc, argv, "-p"))) { | |||||
if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-p"))) { | |||||
state->config.port = atoi(cmdline_buffer); | state->config.port = atoi(cmdline_buffer); | ||||
} | } | ||||
} | } | ||||
@@ -4,6 +4,7 @@ | |||||
#include "libwebsockets.h" | #include "libwebsockets.h" | ||||
#include <string.h> | #include <string.h> | ||||
#include "../IZ_common.h" | #include "../IZ_common.h" | ||||
#include "../IZ_config.h" | |||||
#include "IZ_websocket.h" | #include "IZ_websocket.h" | ||||
/* one of these is created for each client connecting to us */ | /* one of these is created for each client connecting to us */ | ||||