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

@ -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) {

View File

@ -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);