From 2b297147bfea219b8a45ad2e23639624e1ffdb33 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 24 Sep 2024 01:31:26 +0200 Subject: [PATCH] libtrx: tidy json+bson typing --- src/config.c | 10 ++++------ src/game/gameflow/reader.c | 40 ++++++++++++++++---------------------- subprojects/libtrx | 2 +- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/config.c b/src/config.c index 1f04f57a..fa90ba9d 100644 --- a/src/config.c +++ b/src/config.c @@ -5,23 +5,21 @@ #include #include -struct json_object_s; - CONFIG g_Config = { 0 }; #define DIR_CONFIG "cfg" static const char *m_ConfigPath = DIR_CONFIG "/TR2X.json5"; -static void M_Load(struct json_object_s *root_obj); -static void M_Dump(struct json_object_s *root_obj); +static void M_Load(JSON_OBJECT *root_obj); +static void M_Dump(JSON_OBJECT *root_obj); -static void M_Load(struct json_object_s *root_obj) +static void M_Load(JSON_OBJECT *root_obj) { ConfigFile_LoadOptions(root_obj, g_ConfigOptionMap); } -static void M_Dump(struct json_object_s *root_obj) +static void M_Dump(JSON_OBJECT *root_obj) { ConfigFile_DumpOptions(root_obj, g_ConfigOptionMap); } diff --git a/src/game/gameflow/reader.c b/src/game/gameflow/reader.c index c48c2237..80113d55 100644 --- a/src/game/gameflow/reader.c +++ b/src/game/gameflow/reader.c @@ -10,9 +10,8 @@ static void M_StringTableShutdown(GAMEFLOW_NEW_STRING_ENTRY *dest); static bool M_LoadStringTable( - struct json_object_s *root_obj, const char *key, - GAMEFLOW_NEW_STRING_ENTRY **dest); -static bool M_LoadScriptLevels(struct json_object_s *obj, GAMEFLOW_NEW *gf); + JSON_OBJECT *root_obj, const char *key, GAMEFLOW_NEW_STRING_ENTRY **dest); +static bool M_LoadScriptLevels(JSON_OBJECT *obj, GAMEFLOW_NEW *gf); static void M_StringTableShutdown(GAMEFLOW_NEW_STRING_ENTRY *const dest) { @@ -29,18 +28,16 @@ static void M_StringTableShutdown(GAMEFLOW_NEW_STRING_ENTRY *const dest) } static bool M_LoadStringTable( - struct json_object_s *const root_obj, const char *const key, + JSON_OBJECT *const root_obj, const char *const key, GAMEFLOW_NEW_STRING_ENTRY **dest) { - struct json_value_s *const strings_value = - json_object_get_value(root_obj, key); + JSON_VALUE *const strings_value = JSON_ObjectGetValue(root_obj, key); if (strings_value == NULL) { // key is missing - rely on default strings return true; } - struct json_object_s *const strings_obj = - json_value_as_object(strings_value); + JSON_OBJECT *const strings_obj = JSON_ValueAsObject(strings_value); if (strings_obj == NULL) { LOG_ERROR("'%s' must be a dictionary", key); return false; @@ -50,11 +47,10 @@ static bool M_LoadStringTable( sizeof(GAMEFLOW_NEW_STRING_ENTRY) * (strings_obj->length + 1)); GAMEFLOW_NEW_STRING_ENTRY *cur = *dest; - struct json_object_element_s *strings_elem = strings_obj->start; + JSON_OBJECT_ELEMENT *strings_elem = strings_obj->start; for (size_t i = 0; i < strings_obj->length; i++, strings_elem = strings_elem->next) { - struct json_string_s *const value = - json_value_as_string(strings_elem->value); + JSON_STRING *const value = JSON_ValueAsString(strings_elem->value); if (value == NULL) { LOG_ERROR("invalid string key %s", strings_elem->name->string); return NULL; @@ -69,12 +65,11 @@ static bool M_LoadStringTable( return true; } -static bool M_LoadScriptLevels( - struct json_object_s *obj, GAMEFLOW_NEW *const gf) +static bool M_LoadScriptLevels(JSON_OBJECT *obj, GAMEFLOW_NEW *const gf) { bool result = true; - struct json_array_s *const jlvl_arr = json_object_get_array(obj, "levels"); + JSON_ARRAY *const jlvl_arr = JSON_ObjectGetArray(obj, "levels"); if (jlvl_arr == NULL) { LOG_ERROR("'levels' must be a list"); result = false; @@ -94,12 +89,11 @@ static bool M_LoadScriptLevels( gf->level_count = level_count; gf->levels = Memory_Alloc(sizeof(GAMEFLOW_NEW_LEVEL) * level_count); - struct json_array_element_s *jlvl_elem = jlvl_arr->start; + JSON_ARRAY_ELEMENT *jlvl_elem = jlvl_arr->start; for (size_t i = 0; i < jlvl_arr->length; i++, jlvl_elem = jlvl_elem->next) { GAMEFLOW_NEW_LEVEL *const level = &gf->levels[i]; - struct json_object_s *const jlvl_obj = - json_value_as_object(jlvl_elem->value); + JSON_OBJECT *const jlvl_obj = JSON_ValueAsObject(jlvl_elem->value); if (jlvl_obj == NULL) { LOG_ERROR("'levels' elements must be dictionaries"); result = false; @@ -129,21 +123,21 @@ bool GF_N_Load(const char *const path) goto end; } - struct json_parse_result_s parse_result; - struct json_value_s *root = json_parse_ex( - script_data, strlen(script_data), json_parse_flags_allow_json5, NULL, + JSON_PARSE_RESULT parse_result; + JSON_VALUE *root = JSON_ParseEx( + script_data, strlen(script_data), JSON_PARSE_FLAGS_ALLOW_JSON5, NULL, NULL, &parse_result); if (root == NULL) { LOG_ERROR( "failed to parse script file: %s in line %d, char %d", - json_get_error_description(parse_result.error), + JSON_GetErrorDescription(parse_result.error), parse_result.error_line_no, parse_result.error_row_no, script_data); result = false; goto end; } GAMEFLOW_NEW *const gf = &g_GameflowNew; - struct json_object_s *root_obj = json_value_as_object(root); + JSON_OBJECT *root_obj = JSON_ValueAsObject(root); result &= M_LoadStringTable(root_obj, "object_strings", &gf->object_strings); result &= M_LoadStringTable(root_obj, "game_strings", &gf->game_strings); @@ -151,7 +145,7 @@ bool GF_N_Load(const char *const path) end: if (root) { - json_value_free(root); + JSON_ValueFree(root); root = NULL; } diff --git a/subprojects/libtrx b/subprojects/libtrx index 08e1c6d6..198f1036 160000 --- a/subprojects/libtrx +++ b/subprojects/libtrx @@ -1 +1 @@ -Subproject commit 08e1c6d6be485648dda839e95d8ded3de89de74e +Subproject commit 198f10361bc0994678deccbd281393750f39ab69