From 1969c165d3d1e621c72895dea99b13f816c601e9 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 10 Sep 2015 23:16:17 +0200 Subject: [PATCH] allow settings in json configuration file move E2 settings from XML to JSON --- conf/e2/config.json | 31 ++++++++++++++++++++++++++++++- conf/e2/config.xml | 28 ---------------------------- src/kernel/jsonconf.c | 21 +++++++++++++++++++++ src/kernel/jsonconf.test.c | 17 +++++++++++++++++ 4 files changed, 68 insertions(+), 29 deletions(-) diff --git a/conf/e2/config.json b/conf/e2/config.json index b2acffcb3..67ae85bce 100644 --- a/conf/e2/config.json +++ b/conf/e2/config.json @@ -1,5 +1,34 @@ { "include": [ "keywords.json" - ] + ], + "settings": { + "NewbieImmunity": 8, + "modules.wormholes": 1, + "entertain.base": 0, + "entertain.perlevel": 20, + "nmr.timeout": 5, + "nmr.removenewbie": 0, + "GiveRestriction": 3, + "hunger.long": 1, + "init_spells": 0, + "world.era": 2, + "seed.population.min": 8, + "seed.population.max": 8, + "rules.ship.damage_drift": 0.00, + "rules.reserve.twophase": 1, + "rules.give.max_men": "-1", + "rules.check_overload": 0, + "rules.limit.faction": 2500, + "rules.maxskills.magic": 5, + "rules.guard.base_stop_prob": 0.30, + "rules.guard.skill_stop_prob": 0.05, + "rules.guard.amulet_stop_prob": 0.10, + "rules.guard.guard_number_stop_prob": 0.001, + "rules.guard.castle_stop_prob": 0.05, + "rules.guard.region_type_stop_prob": 0.05, + "rules.economy.repopulate_maximum": 500, + "game.id": 2, + "game.name": "Eressea" + } } diff --git a/conf/e2/config.xml b/conf/e2/config.xml index 6263e6095..367f43c29 100644 --- a/conf/e2/config.xml +++ b/conf/e2/config.xml @@ -90,34 +90,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index f9a025432..465478775 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -638,6 +638,24 @@ static void json_keywords(cJSON *json) { } } +static void json_settings(cJSON *json) { + cJSON *child; + if (json->type != cJSON_Object) { + log_error("settings is not a json object: %d", json->type); + return; + } + for (child = json->child; child; child = child->next) { + if (child->valuestring) { + set_param(&global.parameters, child->string, child->valuestring); + } + else { + char value[32]; + _snprintf(value, sizeof(value), "%lf", child->valuedouble); + set_param(&global.parameters, child->string, value); + } + } +} + static void json_races(cJSON *json) { cJSON *child; if (json->type != cJSON_Object) { @@ -714,6 +732,9 @@ void json_config(cJSON *json) { else if (strcmp(child->string, "keywords") == 0) { json_keywords(child); } + else if (strcmp(child->string, "settings") == 0) { + json_settings(child); + } else if (strcmp(child->string, "skills") == 0) { json_skills(child); } diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 50cba3891..38fd41ae9 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -57,6 +57,22 @@ static void test_flags(CuTest *tc) { test_cleanup(); } +static void test_settings(CuTest * tc) +{ + const char * data = "{\"settings\": { " + "\"string\" : \"1d4\"," + "\"integer\" : 14," + "\"float\" : 0.5 }}"; + cJSON *json = cJSON_Parse(data); + + test_cleanup(); + json_config(json); + CuAssertStrEquals(tc, "1d4", get_param(global.parameters, "string")); + CuAssertIntEquals(tc, 14, get_param_int(global.parameters, "integer", 0)); + CuAssertDblEquals(tc, 0.5f, get_param_flt(global.parameters, "float", 0), 0.01); + test_cleanup(); +} + static void test_races(CuTest * tc) { const char * data = "{\"races\": { \"orc\" : { " @@ -510,6 +526,7 @@ CuSuite *get_jsonconf_suite(void) SUITE_ADD_TEST(suite, test_strings); SUITE_ADD_TEST(suite, test_spells); SUITE_ADD_TEST(suite, test_flags); + SUITE_ADD_TEST(suite, test_settings); SUITE_ADD_TEST(suite, test_infinitive_from_config); return suite; }