From 0aec5592a078b9548549a455d7023d2b99a47646 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 29 Oct 2014 07:50:06 +0100 Subject: [PATCH] allow json config to contain a list of config files (includes). --- src/kernel/jsonconf.c | 36 +++++++++++++++++++--- src/kernel/jsonconf.test.c | 62 +++++++++++++++++++++++++------------- 2 files changed, 73 insertions(+), 25 deletions(-) diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index d8a0e59e6..2ba555e9e 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -627,15 +627,40 @@ static void json_keywords(cJSON *json) { static void json_races(cJSON *json) { cJSON *child; - if (json->type!=cJSON_Object) { + if (json->type != cJSON_Object) { log_error("races is not a json object: %d", json->type); return; } - for (child=json->child;child;child=child->next) { + for (child = json->child; child; child = child->next) { json_race(child, rc_get_or_create(child->string)); } } +static void json_configs(cJSON *json) { + cJSON *child; + if (json->type != cJSON_Array) { + log_error("config is not a json array: %d", json->type); + return; + } + for (child = json->child; child; child = child->next) { + cJSON *config; + char *data; + FILE *F = fopen(child->valuestring, "rt"); + if (F) { + size_t sz; + fseek(F, 0, SEEK_END); + sz = ftell(F); + rewind(F); + data = malloc(sz); + fread(data, 1, sz, F); + fclose(F); + config = cJSON_Parse(data); + free(data); + json_config(config); + } + } +} + void json_config(cJSON *json) { cJSON *child; if (json->type!=cJSON_Object) { @@ -646,10 +671,13 @@ void json_config(cJSON *json) { if (strcmp(child->string, "races")==0) { json_races(child); } - else if (strcmp(child->string, "items")==0) { + else if (strcmp(child->string, "items") == 0) { json_items(child); } - else if (strcmp(child->string, "ships")==0) { + else if (strcmp(child->string, "config") == 0) { + json_configs(child); + } + else if (strcmp(child->string, "ships") == 0) { json_ships(child); } else if (strcmp(child->string, "strings")==0) { diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 42505f0bb..5ddba65ce 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -228,29 +228,29 @@ static void test_spells(CuTest * tc) CuAssertPtrEquals(tc, 0, find_spell("fireball")); } +static const char * building_data = "{\"buildings\": { " +"\"house\" : { " +"\"maintenance\" : " +"{ \"type\" : \"iron\", \"amount\" : 1, \"flags\" : [ \"required\", \"variable\" ] }" +"," +"\"construction\" : {" +"\"maxsize\" : 20," +"\"reqsize\" : 10," +"\"minskill\" : 1," +"\"materials\" : {" +"\"stone\" : 2," +"\"iron\" : 1" +"}}}," +"\"shed\" : {" +"\"maintenance\" : [" +"{ \"type\" : \"iron\", \"amount\" : 1 }," +"{ \"type\" : \"stone\", \"amount\" : 2 }" +"]}" +"}}"; + static void test_buildings(CuTest * tc) { - const char * data = "{\"buildings\": { " - "\"house\" : { " - "\"maintenance\" : " - "{ \"type\" : \"iron\", \"amount\" : 1, \"flags\" : [ \"required\", \"variable\" ] }" - "," - "\"construction\" : {" - "\"maxsize\" : 20," - "\"reqsize\" : 10," - "\"minskill\" : 1," - "\"materials\" : {" - "\"stone\" : 2," - "\"iron\" : 1" - "}}}," - "\"shed\" : {" - "\"maintenance\" : [" - "{ \"type\" : \"iron\", \"amount\" : 1 }," - "{ \"type\" : \"stone\", \"amount\" : 2 }" - "]}" - "}}"; - - cJSON *json = cJSON_Parse(data); + cJSON *json = cJSON_Parse(building_data); const building_type *bt; test_cleanup(); @@ -292,6 +292,25 @@ static void test_buildings(CuTest * tc) test_cleanup(); } +static void test_configs(CuTest * tc) +{ + const char * data = "{\"config\": [ \"test.json\" ] }"; + FILE *F; + cJSON *json = cJSON_Parse(data); + + test_cleanup(); + + F = fopen("test.json", "wt"); + fwrite(building_data, 1, strlen(building_data), F); + fclose(F); + CuAssertPtrNotNull(tc, json); + CuAssertPtrEquals(tc, 0, buildingtypes); + json_config(json); + CuAssertPtrNotNull(tc, buildingtypes); + unlink("test.json"); + test_cleanup(); +} + static void test_terrains(CuTest * tc) { const char * data = "{\"terrains\": { \"plain\" : { \"flags\" : [ \"land\", \"fly\", \"walk\" ] } }}"; @@ -404,6 +423,7 @@ CuSuite *get_jsonconf_suite(void) SUITE_ADD_TEST(suite, test_items); SUITE_ADD_TEST(suite, test_ships); SUITE_ADD_TEST(suite, test_buildings); + SUITE_ADD_TEST(suite, test_configs); SUITE_ADD_TEST(suite, test_castles); SUITE_ADD_TEST(suite, test_terrains); SUITE_ADD_TEST(suite, test_races);