disable keywords through JSON configuration

This commit is contained in:
Enno Rehling 2015-09-12 17:06:47 +02:00
parent 32ff7c89bc
commit 44eb3a4f58
7 changed files with 61 additions and 12 deletions

View File

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

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

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

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

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