Merge pull request #297 from ennorehling/feature/json-disable-keywords

disable keywords in JSON, not XML
This commit is contained in:
Enno Rehling 2015-09-12 17:23:08 +02:00
commit e80f53d35e
12 changed files with 82 additions and 51 deletions

View File

@ -4,6 +4,9 @@
"prefixes.json",
"e2/terrains.json"
],
"disable": [
"pay"
],
"settings": {
"game.id": 2,
"game.name": "Eressea",

View File

@ -54,10 +54,6 @@
<xi:include href="config://default/names-dragons.xml"/>
<game>
<!-- Game specific settings -->
<order name="pay" disable="yes"/>
<skill name="alchemy" enable="true"/>
<skill name="crossbow" enable="true"/>
<skill name="mining" enable="true"/>

View File

@ -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",

View File

@ -43,17 +43,6 @@
<xi:include href="config://default/names-dragons.xml"/>
<game>
<!-- Game specific settings -->
<order name="besiege" disable="yes"/>
<order name="steal" disable="yes"/>
<order name="buy" disable="yes"/>
<order name="teach" disable="yes"/>
<order name="sabotage" disable="yes"/>
<order name="spy" disable="yes"/>
<order name="tax" disable="yes"/>
<order name="entertain" disable="yes"/>
<order name="sell" disable="yes"/>
<skill name="armorer" enable="true"/>
<skill name="bow" enable="true"/>
<skill name="building" enable="true"/>

View File

@ -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",

View File

@ -43,16 +43,6 @@
<xi:include href="config://default/names-dragons.xml"/>
<game>
<order name="besiege" disable="yes"/>
<order name="steal" disable="yes"/>
<order name="buy" disable="yes"/>
<order name="teach" disable="yes"/>
<order name="sabotage" disable="yes"/>
<order name="spy" disable="yes"/>
<order name="tax" disable="yes"/>
<order name="entertain" disable="yes"/>
<order name="sell" disable="yes"/>
<skill name="armorer" enable="true"/>
<skill name="bow" enable="true"/>
<skill name="building" enable="true"/>

View File

@ -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("disable 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();

View File

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

View File

@ -2066,30 +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)) {
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);
}
}
xmlFree(propName);
}
xmlXPathFreeObject(result);
/* reading eressea/game/skill */
result = xmlXPathEvalExpression(BAD_CAST "skill", xpath);
nodes = result->nodesetval;

View File

@ -2,8 +2,9 @@
#include <kernel/config.h>
#include "keyword.h"
#include "util/language.h"
#include "util/umlaut.h"
#include <util/language.h>
#include <util/umlaut.h>
#include <util/log.h>
#include <critbit.h>
@ -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;

View File

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

View File

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