From 54f13de92a058b9a54eb37f95be7d574cc58d4ce Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sun, 26 Jun 2022 14:53:08 +0800 Subject: [PATCH] Put repo config in global config Ensure every config-backed subsystem can serialize/deserialize config in a single consistent manner. --- src/packages/net/IZ_net_client.c | 4 +-- src/packages/net/IZ_net_client.h | 4 +-- src/packages/net/IZ_net_server.c | 3 ++- src/packages/net/IZ_net_server.h | 2 +- src/packages/server/db/IZ_repo.c | 45 ++++++++++++++++++++------------ src/packages/server/db/IZ_repo.h | 2 ++ 6 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/packages/net/IZ_net_client.c b/src/packages/net/IZ_net_client.c index 7c4230d..33d6a1d 100644 --- a/src/packages/net/IZ_net_client.c +++ b/src/packages/net/IZ_net_client.c @@ -140,8 +140,8 @@ void IZ_NetClientConnect(IZ_NetClientState* state, IZ_WSClientInitializeParams p state->retries = 0; state->params = params; - state->client_thread = SDL_CreateThread(state->callback, "networking", state->binding.user_data); - SDL_DetachThread(state->client_thread); + state->thread = SDL_CreateThread(state->callback, "networking", state->binding.user_data); + SDL_DetachThread(state->thread); } void IZ_NetClientDisconnect(IZ_NetClientState* state) { diff --git a/src/packages/net/IZ_net_client.h b/src/packages/net/IZ_net_client.h index 080d920..f194ac1 100644 --- a/src/packages/net/IZ_net_client.h +++ b/src/packages/net/IZ_net_client.h @@ -23,7 +23,7 @@ typedef struct { } IZ_NetClientConfig; typedef struct { - SDL_Thread* client_thread; + void* thread; IZ_NetClientConfig config; IZ_NetBinding binding; IZ_NetInitializeParams params; @@ -35,7 +35,7 @@ typedef struct { } IZ_NetClientState; static IZ_NetClientState IZ_NET_CLIENT_DEFAULT_STATE = { - .client_thread = NULL, + .thread = NULL, .config = { .packet_interval_ms = 200, .max_reconnect_retries = 3, diff --git a/src/packages/net/IZ_net_server.c b/src/packages/net/IZ_net_server.c index 48cb7b7..223f1ff 100644 --- a/src/packages/net/IZ_net_server.c +++ b/src/packages/net/IZ_net_server.c @@ -46,7 +46,8 @@ static IZ_ConfigItem net_server_config_items[] = { .deserialize = NULL, }, NULL, - } + }, + IZ_CONFIG_ITEM_NULL, }; void IZ_NetServerBindStateToConfig(IZ_NetServerState* state, IZ_ConfigItem config_items[]) { diff --git a/src/packages/net/IZ_net_server.h b/src/packages/net/IZ_net_server.h index 3fdf6e2..89ae301 100644 --- a/src/packages/net/IZ_net_server.h +++ b/src/packages/net/IZ_net_server.h @@ -34,7 +34,7 @@ static IZ_NetServerState IZ_NET_SERVER_DEFAULT_STATE = { }, }; -IZ_ProcedureResult IZ_NetServerInitialize(IZ_NetServerState *state, void *user_data, const char *config_path, u8 argc, const char **argv); +IZ_ProcedureResult IZ_NetServerInitialize(IZ_NetServerState*, void*, const char*, u8, const char*[]); IZ_ProcedureResult IZ_NetSaveConfig(IZ_NetServerState*, const char*); diff --git a/src/packages/server/db/IZ_repo.c b/src/packages/server/db/IZ_repo.c index 74dde07..fbbc968 100644 --- a/src/packages/server/db/IZ_repo.c +++ b/src/packages/server/db/IZ_repo.c @@ -1,34 +1,45 @@ #include "IZ_repo.h" -void IZ_RepoLoadConfig(IZ_RepoState* state, const char* config_path) { - char buffer[64]; +static IZ_ConfigItem repo_config_items[] = { + { + IZ_CONFIG_TYPE_STRING, + sizeof(char) * 64, + "Database", + "Path", + "-d", + &IZ_REPO_DEFAULT_STATE.config.path, + NULL, + { + .serialize = NULL, + .deserialize = NULL, + }, + NULL, + }, + IZ_CONFIG_ITEM_NULL, +}; - ini_gets("Database", "Path", IZ_REPO_DEFAULT_STATE.config.path, buffer, 64, config_path); - memcpy_s(state->config.path, 64, buffer, 64); +void IZ_RepoBindStateToConfig(IZ_RepoState* state, IZ_ConfigItem config_items[]) { + config_items[0].dest = &state->config.path; } IZ_ProcedureResult IZ_RepoSaveConfig(IZ_RepoState* state, const char* config_path) { - if (!ini_puts("Database", "Path", state->config.path, config_path)) { - return -1; - } - - return 0; + IZ_RepoBindStateToConfig(state, repo_config_items); + return IZ_ConfigSave(repo_config_items, config_path); } -void IZ_RepoOverrideConfig(IZ_RepoState* state, u8 argc, const char* argv[]) { - const char* cmdline_buffer; - if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-d"))) { - memcpy_s(state->config.path, 64, cmdline_buffer, 64); +IZ_ProcedureResult IZ_RepoInitializeConfig(IZ_RepoState* state, const char* config_path, u8 argc, const char* argv[]) { + IZ_RepoBindStateToConfig(state, repo_config_items); + if (IZ_ConfigInitialize(repo_config_items, config_path, argc, argv) < 0) { + return -1; } + return 0; } IZ_ProcedureResult IZ_RepoInitialize(IZ_RepoState* state, const char* config_path, u8 argc, const char* argv[]) { memcpy_s(state, sizeof(IZ_RepoState), &IZ_REPO_DEFAULT_STATE, sizeof(IZ_RepoState)); - IZ_RepoLoadConfig(state, config_path); - if (IZ_RepoSaveConfig(state, config_path)) { - return -1; + if (IZ_RepoInitializeConfig(state, config_path, argc, argv) < 0) { + return -2; } - IZ_RepoOverrideConfig(state, argc, argv); sqlite3_open_v2(state->config.path, &state->db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); return 0; } diff --git a/src/packages/server/db/IZ_repo.h b/src/packages/server/db/IZ_repo.h index 995a8fe..69c234f 100644 --- a/src/packages/server/db/IZ_repo.h +++ b/src/packages/server/db/IZ_repo.h @@ -23,6 +23,8 @@ static IZ_RepoState IZ_REPO_DEFAULT_STATE = { .db = NULL, }; +IZ_ProcedureResult IZ_RepoSaveConfig(IZ_RepoState*, const char*); + IZ_ProcedureResult IZ_RepoInitialize(IZ_RepoState*, const char*, u8, const char*[]); void IZ_RepoTeardown(IZ_RepoState*);