From f75be76ee145f7fd838a9a866e9aecdf88e36ed3 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 11 Sep 2016 12:15:53 +0200 Subject: [PATCH] created a new configuration caching API (config_changed). applied this to some rules. --- src/kernel/config.c | 24 ++++++++++++++++++++++-- src/kernel/config.h | 1 + src/kernel/config.test.c | 23 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index 89d816f0f..481a68220 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -887,14 +887,23 @@ int rule_blessed_harvest(void) int rule_alliance_limit(void) { - int rule = config_get_int("rules.limit.alliance", 0); + static int cache_token; + static int rule = 0; + + if (config_changed(&cache_token)) { + rule = config_get_int("rules.limit.alliance", 0); + } assert(rule >= 0); return rule; } int rule_faction_limit(void) { - int rule = config_get_int("rules.limit.faction", 0); + static int cache_token; + static int rule = 0; + if (config_changed(&cache_token)) { + rule = config_get_int("rules.limit.faction", 0); + } assert(rule >= 0); return rule; } @@ -1053,8 +1062,19 @@ bool markets_module(void) } static struct param *configuration; +static int config_cache_key = 1; + +bool config_changed(int *cache_key) { + assert(cache_key); + if (config_cache_key != *cache_key) { + *cache_key = config_cache_key; + return true; + } + return false; +} void config_set(const char *key, const char *value) { + ++config_cache_key; set_param(&configuration, key, value); } diff --git a/src/kernel/config.h b/src/kernel/config.h index 081a96e61..8e0bd72e5 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -184,6 +184,7 @@ struct param; int config_get_int(const char *key, int def); double config_get_flt(const char *key, double def); bool config_token(const char *key, const char *tok); + bool config_changed(int *cache_key); char * join_path(const char *p1, const char *p2, char *dst, size_t len); bool ExpensiveMigrants(void); diff --git a/src/kernel/config.test.c b/src/kernel/config.test.c index 6bc193bbf..a5bad30c7 100644 --- a/src/kernel/config.test.c +++ b/src/kernel/config.test.c @@ -190,9 +190,31 @@ static void test_default_order(CuTest *tc) { test_cleanup(); } +static void test_config_cache(CuTest *tc) { + int key = 0; + + test_setup(); + CuAssertTrue(tc, config_changed(&key)); + config_set("hodor", "0"); + CuAssertTrue(tc, config_changed(&key)); + CuAssertTrue(tc, !config_changed(&key)); + test_cleanup(); +} + +static void test_rules(CuTest *tc) { + CuAssertIntEquals(tc, 0, rule_alliance_limit()); + config_set("rules.limit.alliance", "1"); + CuAssertIntEquals(tc, 1, rule_alliance_limit()); + + CuAssertIntEquals(tc, 0, rule_faction_limit()); + config_set("rules.limit.faction", "1000"); + CuAssertIntEquals(tc, 1000, rule_faction_limit()); +} + CuSuite *get_config_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_config_cache); SUITE_ADD_TEST(suite, test_get_set_param); SUITE_ADD_TEST(suite, test_param_int); SUITE_ADD_TEST(suite, test_param_flt); @@ -200,5 +222,6 @@ CuSuite *get_config_suite(void) SUITE_ADD_TEST(suite, test_getunit); SUITE_ADD_TEST(suite, test_read_unitid); SUITE_ADD_TEST(suite, test_default_order); + SUITE_ADD_TEST(suite, test_rules); return suite; }