diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index 0e36ad8f7..f888ab88e 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -345,6 +345,35 @@ static void json_buildings(cJSON *json) { } } +static void json_spells(cJSON *json) { + cJSON *child; + if (json->type != cJSON_Object) { + log_error_n("spells is not a json object: %d", json->type); + return; + } + for (child = json->child; child; child = child->next) { + if (child->type == cJSON_Object) { + spell *sp; + cJSON * item = cJSON_GetObjectItem(child, "index"); + sp = create_spell(child->string, item ? item->valueint : 0); + for (item = child->child; item; item = item->next) { + if (strcmp(item->string, "index") == 0) { + continue; + } + else if (strcmp(item->string, "cast") == 0) { + sp->cast = (spell_f)get_function(item->valuestring); + } + else if (strcmp(item->string, "fumble") == 0) { + sp->fumble = (fumble_f)get_function(item->valuestring); + } + else if (strcmp(item->string, "syntax") == 0) { + sp->syntax = _strdup(item->valuestring); + } + } + } + } +} + static void json_items(cJSON *json) { cJSON *child; if (json->type!=cJSON_Object) { @@ -560,10 +589,13 @@ void json_config(cJSON *json) { else if (strcmp(child->string, "skills")==0) { json_skills(child); } - else if (strcmp(child->string, "buildings")==0) { + else if (strcmp(child->string, "buildings") == 0) { json_buildings(child); } - else if (strcmp(child->string, "terrains")==0) { + else if (strcmp(child->string, "spells") == 0) { + json_spells(child); + } + else if (strcmp(child->string, "terrains") == 0) { json_terrains(child); } else { log_error_n("config contains unknown attribute %s", child->string); diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index e422d4e63..f128acf4a 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -8,6 +8,7 @@ #include "keyword.h" #include "race.h" #include "ship.h" +#include "spell.h" #include "terrain.h" #include "util/language.h" #include @@ -177,6 +178,26 @@ static void test_castles(CuTest *tc) { CuAssertPtrEquals(tc, 0, bt->construction->improvement->improvement); } +static void test_spells(CuTest * tc) +{ + const char * data = "{\"spells\": { \"fireball\" : { \"syntax\" : \"u+\" } } }"; + + cJSON *json = cJSON_Parse(data); + const spell *sp; + + test_cleanup(); + CuAssertPtrNotNull(tc, json); + CuAssertPtrEquals(tc, 0, find_spell("fireball")); + + json_config(json); + sp = find_spell("fireball"); + CuAssertPtrNotNull(tc, sp); + CuAssertStrEquals(tc, "u+", sp->syntax); + + test_cleanup(); + CuAssertPtrEquals(tc, 0, find_spell("fireball")); +} + static void test_buildings(CuTest * tc) { const char * data = "{\"buildings\": { \"house\" : { " @@ -331,6 +352,7 @@ CuSuite *get_jsonconf_suite(void) SUITE_ADD_TEST(suite, test_terrains); SUITE_ADD_TEST(suite, test_races); SUITE_ADD_TEST(suite, test_strings); + SUITE_ADD_TEST(suite, test_spells); SUITE_ADD_TEST(suite, test_flags); return suite; }