Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
libtrx: tidy json+bson typing
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Sep 24, 2024
1 parent 26b3c66 commit 2b29714
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
10 changes: 4 additions & 6 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@
#include <libtrx/config/config_file.h>
#include <libtrx/filesystem.h>

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);
}
Expand Down
40 changes: 17 additions & 23 deletions src/game/gameflow/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -129,29 +123,29 @@ 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);
result &= M_LoadScriptLevels(root_obj, gf);

end:
if (root) {
json_value_free(root);
JSON_ValueFree(root);
root = NULL;
}

Expand Down

0 comments on commit 2b29714

Please sign in to comment.