From 44eb3a4f584f68825eb999f7bcb5ab467403d641 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 12 Sep 2015 17:06:47 +0200 Subject: [PATCH 1/2] disable keywords through JSON configuration --- conf/e2/config.json | 3 +++ src/kernel/jsonconf.c | 14 ++++++++++++++ src/kernel/jsonconf.test.c | 20 ++++++++++++++++++++ src/kernel/xmlreader.c | 11 +---------- src/keyword.c | 19 +++++++++++++++++-- src/keyword.h | 1 + src/tests.c | 5 +++++ 7 files changed, 61 insertions(+), 12 deletions(-) diff --git a/conf/e2/config.json b/conf/e2/config.json index 946ddd1dc..86d3949aa 100644 --- a/conf/e2/config.json +++ b/conf/e2/config.json @@ -4,6 +4,9 @@ "prefixes.json", "e2/terrains.json" ], + "disable": [ + "pay" + ], "settings": { "game.id": 2, "game.name": "Eressea", diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index 5fdd82e65..0f1a81dc0 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -501,6 +501,17 @@ static void json_prefixes(cJSON *json) { } } +static void json_disable_keywords(cJSON *json) { + cJSON *child; + if (json->type != cJSON_Array) { + log_error("disabled is not a json array: %d", json->type); + return; + } + for (child = json->child; child; child = child->next) { + disable_keyword_str(child->valuestring); + } +} + static void json_terrains(cJSON *json) { cJSON *child; if (json->type != cJSON_Object) { @@ -854,6 +865,9 @@ void json_config(cJSON *json) { else if (strcmp(child->string, "prefixes") == 0) { json_prefixes(child); } + else if (strcmp(child->string, "disable") == 0) { + json_disable_keywords(child); + } else if (strcmp(child->string, "terrains") == 0) { json_terrains(child); init_terrains(); diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 1547fde45..7b64f25a9 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -99,6 +99,25 @@ static void test_prefixes(CuTest * tc) test_cleanup(); } +static void test_disable(CuTest * tc) +{ + const char * data = "{\"disable\": [ " + "\"pay\"," + "\"besiege\"" + "]}"; + cJSON *json = cJSON_Parse(data); + + test_cleanup(); + CuAssertTrue(tc, !keyword_disabled(K_BANNER)); + CuAssertTrue(tc, !keyword_disabled(K_PAY)); + CuAssertTrue(tc, !keyword_disabled(K_BESIEGE)); + json_config(json); + CuAssertTrue(tc, !keyword_disabled(K_BANNER)); + CuAssertTrue(tc, keyword_disabled(K_PAY)); + CuAssertTrue(tc, keyword_disabled(K_BESIEGE)); + test_cleanup(); +} + static void test_races(CuTest * tc) { const char * data = "{\"races\": { \"orc\" : { " @@ -576,6 +595,7 @@ CuSuite *get_jsonconf_suite(void) SUITE_ADD_TEST(suite, test_flags); SUITE_ADD_TEST(suite, test_settings); SUITE_ADD_TEST(suite, test_prefixes); + SUITE_ADD_TEST(suite, test_disable); SUITE_ADD_TEST(suite, test_infinitive_from_config); return suite; } diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index c2114641d..5aa40e6e1 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -2074,16 +2074,7 @@ static int parse_main(xmlDocPtr doc) xmlChar *propName = xmlGetProp(node, BAD_CAST "name"); if (xml_bvalue(node, "disable", false)) { - int k; - for (k = 0; k != MAXKEYWORDS; ++k) { - if (strcmp(keywords[k], (const char *)propName) == 0) { - enable_keyword(k, false); - break; - } - } - if (k == MAXKEYWORDS) { - log_error("trying to disable unknown command %s\n", (const char *)propName); - } + disable_keyword_str((const char *)propName); } xmlFree(propName); } diff --git a/src/keyword.c b/src/keyword.c index 683b1d2c2..7dd31b55a 100644 --- a/src/keyword.c +++ b/src/keyword.c @@ -2,8 +2,9 @@ #include #include "keyword.h" -#include "util/language.h" -#include "util/umlaut.h" +#include +#include +#include #include @@ -73,6 +74,20 @@ keyword_t get_keyword(const char *s, const struct locale *lang) { static bool disabled_kwd[MAXKEYWORDS]; +void disable_keyword_str(const char *str) { + // FIXME: this is slower than balls. + int k; + for (k = 0; k != MAXKEYWORDS; ++k) { + if (strcmp(keywords[k], str) == 0) { + enable_keyword(k, false); + break; + } + } + if (k == MAXKEYWORDS) { + log_error("trying to disable unknown command %s\n", str); + } +} + void enable_keyword(keyword_t kwd, bool enabled) { assert(kwd < MAXKEYWORDS); disabled_kwd[kwd] = !enabled; diff --git a/src/keyword.h b/src/keyword.h index 9d5e20f64..43918320d 100644 --- a/src/keyword.h +++ b/src/keyword.h @@ -81,6 +81,7 @@ extern "C" void init_keyword(const struct locale *lang, keyword_t kwd, const char *str); bool keyword_disabled(keyword_t kwd); void enable_keyword(keyword_t kwd, bool enabled); + void disable_keyword_str(const char *str); const char *keyword(keyword_t kwd); // #define keyword(kwd) mkname("keyword", keywords[kwd]) diff --git a/src/tests.c b/src/tests.c index e336c192a..3389aff01 100644 --- a/src/tests.c +++ b/src/tests.c @@ -71,6 +71,8 @@ struct unit *test_create_unit(struct faction *f, struct region *r) void test_cleanup(void) { + int i; + free_terrains(); free_resources(); global.functions.maintenance = NULL; @@ -87,6 +89,9 @@ void test_cleanup(void) free_seen(); free_prefixes(); mt_clear(); + for (i = 0; i != MAXKEYWORDS; ++i) { + enable_keyword(i, true); + } if (!mt_find("missing_message")) { mt_register(mt_new_va("missing_message", "name:string", 0)); mt_register(mt_new_va("missing_feedback", "unit:unit", "region:region", "command:order", "name:string", 0)); From c76b118fb8e0b4da17ccbf60b9a2374a7ce950f6 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 12 Sep 2015 17:12:02 +0200 Subject: [PATCH 2/2] eliminate game/order from XML data --- conf/e2/config.xml | 4 ---- conf/e3/config.json | 11 +++++++++++ conf/e3/config.xml | 11 ----------- conf/e4/config.json | 11 +++++++++++ conf/e4/config.xml | 10 ---------- src/kernel/jsonconf.c | 2 +- src/kernel/xmlreader.c | 15 --------------- 7 files changed, 23 insertions(+), 41 deletions(-) diff --git a/conf/e2/config.xml b/conf/e2/config.xml index 98c118088..2c9ac7022 100644 --- a/conf/e2/config.xml +++ b/conf/e2/config.xml @@ -54,10 +54,6 @@ - - - - diff --git a/conf/e3/config.json b/conf/e3/config.json index d831a0b3e..58804f1f0 100644 --- a/conf/e3/config.json +++ b/conf/e3/config.json @@ -4,6 +4,17 @@ "prefixes.json", "e3/terrains.json" ], + "disable": [ + "besiege", + "steal", + "buy", + "teach", + "sabotage", + "spy", + "tax", + "entertain", + "sell" + ], "settings": { "game.id": 3, "game.name": "E3", diff --git a/conf/e3/config.xml b/conf/e3/config.xml index 6a32c95da..68e61b7d1 100644 --- a/conf/e3/config.xml +++ b/conf/e3/config.xml @@ -43,17 +43,6 @@ - - - - - - - - - - - diff --git a/conf/e4/config.json b/conf/e4/config.json index 831b973a8..5a58e7021 100644 --- a/conf/e4/config.json +++ b/conf/e4/config.json @@ -4,6 +4,17 @@ "prefixes.json", "e3/terrains.json" ], + "disable": [ + "besiege", + "steal", + "buy", + "teach", + "sabotage", + "spy", + "tax", + "entertain", + "sell" + ], "settings": { "game.id": 4, "game.name": "Deveron", diff --git a/conf/e4/config.xml b/conf/e4/config.xml index bbf712c09..0d6ea5704 100644 --- a/conf/e4/config.xml +++ b/conf/e4/config.xml @@ -43,16 +43,6 @@ - - - - - - - - - - diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index 0f1a81dc0..be6a931d9 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -504,7 +504,7 @@ static void json_prefixes(cJSON *json) { static void json_disable_keywords(cJSON *json) { cJSON *child; if (json->type != cJSON_Array) { - log_error("disabled is not a json array: %d", json->type); + log_error("disable is not a json array: %d", json->type); return; } for (child = json->child; child; child = child->next) { diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 5aa40e6e1..1e87401a7 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -2066,21 +2066,6 @@ static int parse_main(xmlDocPtr doc) xmlXPathFreeObject(result); xpath->node = node; - /* reading eressea/game/order */ - result = xmlXPathEvalExpression(BAD_CAST "order", xpath); - nodes = result->nodesetval; - for (i = 0; i != nodes->nodeNr; ++i) { - xmlNodePtr node = nodes->nodeTab[i]; - xmlChar *propName = xmlGetProp(node, BAD_CAST "name"); - - if (xml_bvalue(node, "disable", false)) { - disable_keyword_str((const char *)propName); - } - xmlFree(propName); - } - - xmlXPathFreeObject(result); - /* reading eressea/game/skill */ result = xmlXPathEvalExpression(BAD_CAST "skill", xpath); nodes = result->nodesetval;