From a01955e06ae6ee06841912784cbca150c980d5fa Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 12 Sep 2015 17:37:29 +0200 Subject: [PATCH 1/4] disable features by name that are not keywords --- conf/e2/config.json | 5 +++-- conf/e3/config.json | 5 +++-- conf/e4/config.json | 5 +++-- scripts/eressea/jsreport.lua | 2 +- src/jsreport.c | 2 +- src/kernel/jsonconf.c | 34 +++++++++++++++++++++++++++++----- src/kernel/jsonconf.test.c | 7 +++++-- src/keyword.c | 14 -------------- src/keyword.h | 1 - 9 files changed, 45 insertions(+), 30 deletions(-) diff --git a/conf/e2/config.json b/conf/e2/config.json index 86d3949aa..6b11dceff 100644 --- a/conf/e2/config.json +++ b/conf/e2/config.json @@ -4,8 +4,9 @@ "prefixes.json", "e2/terrains.json" ], - "disable": [ - "pay" + "disabled": [ + "pay", + "jsreport" ], "settings": { "game.id": 2, diff --git a/conf/e3/config.json b/conf/e3/config.json index 58804f1f0..871b131a7 100644 --- a/conf/e3/config.json +++ b/conf/e3/config.json @@ -4,7 +4,7 @@ "prefixes.json", "e3/terrains.json" ], - "disable": [ + "disabled": [ "besiege", "steal", "buy", @@ -13,7 +13,8 @@ "spy", "tax", "entertain", - "sell" + "sell", + "jsreport" ], "settings": { "game.id": 3, diff --git a/conf/e4/config.json b/conf/e4/config.json index 5a58e7021..2aaa1e998 100644 --- a/conf/e4/config.json +++ b/conf/e4/config.json @@ -4,7 +4,7 @@ "prefixes.json", "e3/terrains.json" ], - "disable": [ + "disabled": [ "besiege", "steal", "buy", @@ -13,7 +13,8 @@ "spy", "tax", "entertain", - "sell" + "sell", + "jsreport" ], "settings": { "game.id": 4, diff --git a/scripts/eressea/jsreport.lua b/scripts/eressea/jsreport.lua index 0351efd12..b22f1acf2 100644 --- a/scripts/eressea/jsreport.lua +++ b/scripts/eressea/jsreport.lua @@ -1,7 +1,7 @@ local pkg = {} function pkg.init() - eressea.settings.set("feature.jsreport.enable", "1") + eressea.settings.set("jsreport.enabled", "1") end function pkg.update() diff --git a/src/jsreport.c b/src/jsreport.c index 64d6d4e2d..44b9ea252 100644 --- a/src/jsreport.c +++ b/src/jsreport.c @@ -25,7 +25,7 @@ static void coor_from_tiled(int *x, int *y) { static int report_json(const char *filename, report_context * ctx, const char *charset) { - if (get_param_int(global.parameters, "feature.jsreport.enable", 0) != 0) { + if (get_param_int(global.parameters, "jsreport.enabled", 0) != 0) { FILE * F = fopen(filename, "w"); if (F) { int x, y, minx = INT_MAX, maxx = INT_MIN, miny = INT_MAX, maxy = INT_MIN; diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index be6a931d9..23486c8aa 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -501,14 +501,38 @@ static void json_prefixes(cJSON *json) { } } -static void json_disable_keywords(cJSON *json) { +/** disable a feature. + * features are identified by eone of: + * 1. the keyword for their orders, + * 2. the name of the skill they use, + * 3. a "module.enabled" flag in the settings + */ +static void disable_feature(const char *str) { + // FIXME: this is slower than balls. + int k; + for (k = 0; k != MAXKEYWORDS; ++k) { + if (strcmp(keywords[k], str) == 0) { + log_info("disable keyword %s\n", str); + enable_keyword(k, false); + break; + } + } + if (k == MAXKEYWORDS) { + char name[32]; + _snprintf(name, sizeof(name), "%s.enabled", str); + log_info("disable feature %s\n", name); + set_param(&global.parameters, name, "0"); + } +} + +static void json_disable_features(cJSON *json) { cJSON *child; if (json->type != cJSON_Array) { - log_error("disable is not a json array: %d", json->type); + 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); + disable_feature(child->valuestring); } } @@ -865,8 +889,8 @@ 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, "disabled") == 0) { + json_disable_features(child); } else if (strcmp(child->string, "terrains") == 0) { json_terrains(child); diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 7b64f25a9..0474b99a6 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -101,9 +101,10 @@ static void test_prefixes(CuTest * tc) static void test_disable(CuTest * tc) { - const char * data = "{\"disable\": [ " + const char * data = "{\"disabled\": [ " "\"pay\"," - "\"besiege\"" + "\"besiege\"," + "\"module\"" "]}"; cJSON *json = cJSON_Parse(data); @@ -111,10 +112,12 @@ static void test_disable(CuTest * tc) CuAssertTrue(tc, !keyword_disabled(K_BANNER)); CuAssertTrue(tc, !keyword_disabled(K_PAY)); CuAssertTrue(tc, !keyword_disabled(K_BESIEGE)); + CuAssertIntEquals(tc, 1, get_param_int(global.parameters, "module.enabled", 1)); json_config(json); CuAssertTrue(tc, !keyword_disabled(K_BANNER)); CuAssertTrue(tc, keyword_disabled(K_PAY)); CuAssertTrue(tc, keyword_disabled(K_BESIEGE)); + CuAssertIntEquals(tc, 0, get_param_int(global.parameters, "module.enabled", 1)); test_cleanup(); } diff --git a/src/keyword.c b/src/keyword.c index 7dd31b55a..0bd699836 100644 --- a/src/keyword.c +++ b/src/keyword.c @@ -74,20 +74,6 @@ 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 43918320d..9d5e20f64 100644 --- a/src/keyword.h +++ b/src/keyword.h @@ -81,7 +81,6 @@ 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]) From 1e75255d51edcf22915dce7ead7ee2dcdfd4946b Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 12 Sep 2015 17:43:31 +0200 Subject: [PATCH 2/4] allow disabling skills through json --- src/kernel/jsonconf.c | 20 ++++++++++++-------- src/kernel/jsonconf.test.c | 3 +++ src/tests.c | 3 +++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index 23486c8aa..64ae06477 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -508,21 +508,25 @@ static void json_prefixes(cJSON *json) { * 3. a "module.enabled" flag in the settings */ static void disable_feature(const char *str) { - // FIXME: this is slower than balls. + char name[32]; int k; + skill_t sk; + sk = findskill(str); + if (sk != NOSKILL) { + enable_skill(sk, false); + return; + } for (k = 0; k != MAXKEYWORDS; ++k) { + // FIXME: this loop is slow as balls. if (strcmp(keywords[k], str) == 0) { log_info("disable keyword %s\n", str); enable_keyword(k, false); - break; + return; } } - if (k == MAXKEYWORDS) { - char name[32]; - _snprintf(name, sizeof(name), "%s.enabled", str); - log_info("disable feature %s\n", name); - set_param(&global.parameters, name, "0"); - } + _snprintf(name, sizeof(name), "%s.enabled", str); + log_info("disable feature %s\n", name); + set_param(&global.parameters, name, "0"); } static void json_disable_features(cJSON *json) { diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 0474b99a6..d7a4ad0cd 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -102,6 +102,7 @@ static void test_prefixes(CuTest * tc) static void test_disable(CuTest * tc) { const char * data = "{\"disabled\": [ " + "\"alchemy\"," "\"pay\"," "\"besiege\"," "\"module\"" @@ -109,11 +110,13 @@ static void test_disable(CuTest * tc) cJSON *json = cJSON_Parse(data); test_cleanup(); + CuAssertTrue(tc, skill_enabled(SK_ALCHEMY)); CuAssertTrue(tc, !keyword_disabled(K_BANNER)); CuAssertTrue(tc, !keyword_disabled(K_PAY)); CuAssertTrue(tc, !keyword_disabled(K_BESIEGE)); CuAssertIntEquals(tc, 1, get_param_int(global.parameters, "module.enabled", 1)); json_config(json); + CuAssertTrue(tc, !skill_enabled(SK_ALCHEMY)); CuAssertTrue(tc, !keyword_disabled(K_BANNER)); CuAssertTrue(tc, keyword_disabled(K_PAY)); CuAssertTrue(tc, keyword_disabled(K_BESIEGE)); diff --git a/src/tests.c b/src/tests.c index 3389aff01..596d7b02f 100644 --- a/src/tests.c +++ b/src/tests.c @@ -89,6 +89,9 @@ void test_cleanup(void) free_seen(); free_prefixes(); mt_clear(); + for (i = 0; i != MAXSKILLS; ++i) { + enable_skill(i, true); + } for (i = 0; i != MAXKEYWORDS; ++i) { enable_keyword(i, true); } From 0ed365e539942b7cb0debb95fcfbacb29cde5577 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 12 Sep 2015 18:15:36 +0200 Subject: [PATCH 3/4] remove the game section from XML config --- conf/e2/config.xml | 32 -------------------------------- conf/e3/config.json | 8 ++++++++ conf/e3/config.xml | 34 ---------------------------------- conf/e4/config.json | 8 ++++++++ conf/e4/config.xml | 33 --------------------------------- 5 files changed, 16 insertions(+), 99 deletions(-) diff --git a/conf/e2/config.xml b/conf/e2/config.xml index 2c9ac7022..d1eac12d8 100644 --- a/conf/e2/config.xml +++ b/conf/e2/config.xml @@ -52,38 +52,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - eressea-server@eressea.de diff --git a/conf/e3/config.json b/conf/e3/config.json index 871b131a7..236001e1d 100644 --- a/conf/e3/config.json +++ b/conf/e3/config.json @@ -5,6 +5,14 @@ "e3/terrains.json" ], "disabled": [ + "herbalism", + "alchemy", + "entertainment", + "espionage", + "perception", + "stealth", + "taxation", + "trade", "besiege", "steal", "buy", diff --git a/conf/e3/config.xml b/conf/e3/config.xml index 68e61b7d1..1a1f5f26f 100644 --- a/conf/e3/config.xml +++ b/conf/e3/config.xml @@ -41,40 +41,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/conf/e4/config.json b/conf/e4/config.json index 2aaa1e998..ffa11443c 100644 --- a/conf/e4/config.json +++ b/conf/e4/config.json @@ -5,6 +5,14 @@ "e3/terrains.json" ], "disabled": [ + "herbalism", + "alchemy", + "entertainment", + "espionage", + "perception", + "stealth", + "taxation", + "trade", "besiege", "steal", "buy", diff --git a/conf/e4/config.xml b/conf/e4/config.xml index 0d6ea5704..ffa1c5df8 100644 --- a/conf/e4/config.xml +++ b/conf/e4/config.xml @@ -42,39 +42,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From 7259a4b45e96c980f8f031f6b1b48c70ced1538d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 12 Sep 2015 18:16:23 +0200 Subject: [PATCH 4/4] remove parse_main from xmlreader, everything superseeded by jsonconf --- src/kernel/xmlreader.c | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 1e87401a7..4f6dbf0c0 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -2052,44 +2052,8 @@ static int parse_strings(xmlDocPtr doc) return 0; } -static int parse_main(xmlDocPtr doc) -{ - xmlXPathContextPtr xpath = xmlXPathNewContext(doc); - xmlXPathObjectPtr result = - xmlXPathEvalExpression(BAD_CAST "/eressea/game", xpath); - xmlNodeSetPtr nodes = result->nodesetval; - int i; - - if (nodes->nodeNr > 0) { - xmlNodePtr node = nodes->nodeTab[0]; - - xmlXPathFreeObject(result); - - xpath->node = node; - /* reading eressea/game/skill */ - result = xmlXPathEvalExpression(BAD_CAST "skill", xpath); - nodes = result->nodesetval; - for (i = 0; i != nodes->nodeNr; ++i) { - xmlNodePtr node = nodes->nodeTab[i]; - xmlChar *propName = xmlGetProp(node, BAD_CAST "name"); - skill_t sk = findskill((const char *)propName); - if (sk != NOSKILL) { - bool enable = xml_bvalue(node, "enable", true); - enable_skill(sk, enable); - } - xmlFree(propName); - } - } - xmlXPathFreeObject(result); - - xmlXPathFreeContext(xpath); - return 0; -} - void register_xmlreader(void) { - xml_register_callback(parse_main); - xml_register_callback(parse_strings); xml_register_callback(parse_messages); xml_register_callback(parse_resources);