From d1530e0ae0cbcfe9b512784e7654121a5889aa02 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Mon, 20 Jun 2022 03:40:15 +0800 Subject: [PATCH] Add help Make sure to have help messages for both the game and the server. --- CMakeLists.txt | 8 ++++- src/packages/game/IZ_app.c | 44 ++++++++++++++++++----- src/packages/game/net/IZ_net.c | 7 +++- src/packages/game/output/video/IZ_video.c | 7 +++- src/packages/server/IZ_app.c | 27 ++++++++++++++ src/packages/server/db/IZ_repo.c | 2 +- src/packages/server/net/IZ_net.c | 7 +++- 7 files changed, 88 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2cbd906..4666ecf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,13 @@ if (WIN32) endif () endif () -add_definitions(-DIZ_APP_NAME="Izanagi" -DIZ_PLAYERS=1) +add_definitions( + -DIZ_APP_NAME="Izanagi" + -DIZ_APP_DESCRIPTION="Run and gun game" + -DIZ_APP_SERVER_DESCRIPTION="Dedicated server" + -DIZ_PLAYERS=1 +) + if (${CMAKE_BUILD_TYPE} STREQUAL "Debug") add_definitions(-DIZ_DEBUG) endif() diff --git a/src/packages/game/IZ_app.c b/src/packages/game/IZ_app.c index 9d6dc76..3088ca8 100644 --- a/src/packages/game/IZ_app.c +++ b/src/packages/game/IZ_app.c @@ -18,6 +18,16 @@ IZ_InputState* IZ_AppGetInputState(struct IZ_App* app) { IZ_ProcedureResult IZ_AppInitialize(struct IZ_App* app, u8 argc, const char* argv[]) { memset(app, 0, sizeof(struct IZ_App)); + + const char* cmdline_buffer; + char config_path[128]; + // TODO abstract command line args parsing + if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-c"))) { + memcpy_s(config_path, 128, cmdline_buffer, 128); + } else { + IZ_ConfigGetDefaultPath(config_path, 128); + } + u32 flags = ( SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER @@ -29,15 +39,6 @@ IZ_ProcedureResult IZ_AppInitialize(struct IZ_App* app, u8 argc, const char* arg return IZ_APP_RUN_SDL_INIT_ERROR; } - const char* cmdline_buffer; - char config_path[128]; - // TODO abstract command line args parsing - if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-c"))) { - memcpy_s(config_path, 128, cmdline_buffer, 128); - } else { - IZ_ConfigGetDefaultPath(config_path, 128); - } - if (IZ_VideoInitialize(&app->video_state, app, config_path, argc, argv)) { return IZ_APP_RUN_VIDEO_INIT_ERROR; } @@ -65,6 +66,31 @@ void IZ_AppTeardown(struct IZ_App* app) { } IZ_ProcedureResult IZ_AppRun(struct IZ_App* app, u8 argc, const char* argv[]) { + if (IZ_ConfigGetCommandlineOption(argc, argv, "-h")) { + printf( + ( + "\n" + "%s - %s\n" + "\n" + "Usage:\n" + "\n" + " %s [options]\n" + "\n" + "Options:\n" + "\n" + " -c Specifies the path to the config file. (default: \"./config-game.ini\")\n" + " -f Specifies the frames per second. (default: 30)\n" + " -h Displays this help file.\n" + " -i Specifies the interval of sending packets (default: 200)\n" + " in milliseconds.\n" + ), + IZ_APP_NAME, + IZ_APP_DESCRIPTION, + "game.exe" + ); + return IZ_APP_RUN_RESULT_OK; + } + IZ_ProcedureResult init_result = IZ_AppInitialize(app, argc, argv); if (init_result) { return init_result; diff --git a/src/packages/game/net/IZ_net.c b/src/packages/game/net/IZ_net.c index f34486c..ed10f54 100644 --- a/src/packages/game/net/IZ_net.c +++ b/src/packages/game/net/IZ_net.c @@ -44,8 +44,13 @@ IZ_ProcedureResult IZ_NetSaveConfig(IZ_NetState* state, const char* config_path) void IZ_NetOverrideConfig(IZ_NetState* state, u8 argc, const char* argv[]) { const char* cmdline_buffer; + char* rest_of_string; + u16 packet_interval_ms; if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-i"))) { - state->config.packet_interval_ms = atoi(cmdline_buffer); + packet_interval_ms = strtol(cmdline_buffer, &rest_of_string, 10); + if (strcmp(cmdline_buffer, rest_of_string) != 0) { + state->config.packet_interval_ms = packet_interval_ms; + } } } diff --git a/src/packages/game/output/video/IZ_video.c b/src/packages/game/output/video/IZ_video.c index 5a332b7..ff7b051 100644 --- a/src/packages/game/output/video/IZ_video.c +++ b/src/packages/game/output/video/IZ_video.c @@ -22,8 +22,13 @@ void IZ_VideoLoadConfig(IZ_VideoState* state, const char* config_path) { void IZ_VideoOverrideConfig(IZ_VideoState* state, u8 argc, const char* argv[]) { const char* cmdline_buffer; + char* rest_of_string; + u8 max_fps; if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-f"))) { - state->config.max_fps = atoi(cmdline_buffer); + max_fps = strtol(cmdline_buffer, &rest_of_string, 10); + if (strcmp(cmdline_buffer, rest_of_string) != 0) { + state->config.max_fps = max_fps; + } } } diff --git a/src/packages/server/IZ_app.c b/src/packages/server/IZ_app.c index a60851e..32f83cc 100644 --- a/src/packages/server/IZ_app.c +++ b/src/packages/server/IZ_app.c @@ -42,6 +42,33 @@ void IZ_AppTeardown(IZ_App* app) { } IZ_ProcedureResult IZ_AppRun(IZ_App *app, u8 argc, const char **argv) { + if (IZ_ConfigGetCommandlineOption(argc, argv, "-h")) { + printf( + ( + "\n" + "%s - %s\n" + "\n" + "Usage:\n" + "\n" + " %s [options]\n" + "\n" + "Options:\n" + "\n" + " -c Specifies the path to the config file. (default: \"./config-server.ini\")\n" + " -d Specifies the path to the database. (default: \"./db.sqlite\")\n" + " -h Displays this help file.\n" + " -m Specifies the message of the day. (default: \"\")\n" + " -n Specifies the name of the server. (default: \"%s\")\n" + " -p Specifies the port where the server runs. (default: 42069)\n" + ), + IZ_APP_NAME, + IZ_APP_SERVER_DESCRIPTION, + "server.exe", + IZ_APP_NAME + ); + return 0; + } + if (IZ_AppInitialize(app, argc, argv)) { return -1; } diff --git a/src/packages/server/db/IZ_repo.c b/src/packages/server/db/IZ_repo.c index 9211c38..74dde07 100644 --- a/src/packages/server/db/IZ_repo.c +++ b/src/packages/server/db/IZ_repo.c @@ -17,7 +17,7 @@ IZ_ProcedureResult IZ_RepoSaveConfig(IZ_RepoState* state, const char* config_pat void IZ_RepoOverrideConfig(IZ_RepoState* state, u8 argc, const char* argv[]) { const char* cmdline_buffer; - if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-n"))) { + if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-d"))) { memcpy_s(state->config.path, 64, cmdline_buffer, 64); } } diff --git a/src/packages/server/net/IZ_net.c b/src/packages/server/net/IZ_net.c index 553bfbb..20c9503 100644 --- a/src/packages/server/net/IZ_net.c +++ b/src/packages/server/net/IZ_net.c @@ -30,8 +30,13 @@ IZ_ProcedureResult IZ_NetSaveConfig(IZ_NetState* state, const char* config_path) void IZ_NetOverrideConfig(IZ_NetState* state, u8 argc, const char* argv[]) { const char* cmdline_buffer; + char* rest_of_string; + u16 port; if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-p"))) { - state->config.port = atoi(cmdline_buffer); + port = strtol(cmdline_buffer, &rest_of_string, 10); + if (strcmp(cmdline_buffer, rest_of_string) != 0) { + state->config.port = port; + } } if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-n"))) {