allow json config to contain a list of config files (includes).

This commit is contained in:
Enno Rehling 2014-10-29 07:50:06 +01:00
parent 0a1209d031
commit 0aec5592a0
2 changed files with 73 additions and 25 deletions

View File

@ -636,6 +636,31 @@ static void json_races(cJSON *json) {
} }
} }
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) { void json_config(cJSON *json) {
cJSON *child; cJSON *child;
if (json->type!=cJSON_Object) { if (json->type!=cJSON_Object) {
@ -649,6 +674,9 @@ void json_config(cJSON *json) {
else if (strcmp(child->string, "items") == 0) { else if (strcmp(child->string, "items") == 0) {
json_items(child); json_items(child);
} }
else if (strcmp(child->string, "config") == 0) {
json_configs(child);
}
else if (strcmp(child->string, "ships") == 0) { else if (strcmp(child->string, "ships") == 0) {
json_ships(child); json_ships(child);
} }

View File

@ -228,9 +228,7 @@ static void test_spells(CuTest * tc)
CuAssertPtrEquals(tc, 0, find_spell("fireball")); CuAssertPtrEquals(tc, 0, find_spell("fireball"));
} }
static void test_buildings(CuTest * tc) static const char * building_data = "{\"buildings\": { "
{
const char * data = "{\"buildings\": { "
"\"house\" : { " "\"house\" : { "
"\"maintenance\" : " "\"maintenance\" : "
"{ \"type\" : \"iron\", \"amount\" : 1, \"flags\" : [ \"required\", \"variable\" ] }" "{ \"type\" : \"iron\", \"amount\" : 1, \"flags\" : [ \"required\", \"variable\" ] }"
@ -250,7 +248,9 @@ static void test_buildings(CuTest * tc)
"]}" "]}"
"}}"; "}}";
cJSON *json = cJSON_Parse(data); static void test_buildings(CuTest * tc)
{
cJSON *json = cJSON_Parse(building_data);
const building_type *bt; const building_type *bt;
test_cleanup(); test_cleanup();
@ -292,6 +292,25 @@ static void test_buildings(CuTest * tc)
test_cleanup(); 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) static void test_terrains(CuTest * tc)
{ {
const char * data = "{\"terrains\": { \"plain\" : { \"flags\" : [ \"land\", \"fly\", \"walk\" ] } }}"; 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_items);
SUITE_ADD_TEST(suite, test_ships); SUITE_ADD_TEST(suite, test_ships);
SUITE_ADD_TEST(suite, test_buildings); SUITE_ADD_TEST(suite, test_buildings);
SUITE_ADD_TEST(suite, test_configs);
SUITE_ADD_TEST(suite, test_castles); SUITE_ADD_TEST(suite, test_castles);
SUITE_ADD_TEST(suite, test_terrains); SUITE_ADD_TEST(suite, test_terrains);
SUITE_ADD_TEST(suite, test_races); SUITE_ADD_TEST(suite, test_races);