diff --git a/s/setup b/s/setup index d8ffa74ad..848175ddc 100755 --- a/s/setup +++ b/s/setup @@ -96,6 +96,7 @@ touch eressea.ini ini_start ini_sec game ini_add game locales de,en +ini_add id $game ini_sec lua ini_add lua install $SOURCE ini_add lua paths $SOURCE/scripts:$SOURCE/lunit diff --git a/src/bindings.c b/src/bindings.c index f17ccc3a1..94e78fb70 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -978,7 +978,7 @@ static int tolua_report_unit(lua_State * L) return 1; } -static void parse_inifile(lua_State * L, dictionary * d, const char *section) +static void parse_inifile(lua_State * L, const dictionary * d, const char *section) { int i; const char *arg; @@ -1018,7 +1018,7 @@ static void parse_inifile(lua_State * L, dictionary * d, const char *section) void tolua_bind_open(lua_State * L); -int tolua_bindings_open(lua_State * L) +int tolua_bindings_open(lua_State * L, const dictionary *inifile) { tolua_open(L); @@ -1072,7 +1072,7 @@ int tolua_bindings_open(lua_State * L) tolua_module(L, TOLUA_CAST "config", 1); tolua_beginmodule(L, TOLUA_CAST "config"); { - parse_inifile(L, global.inifile, "lua"); + parse_inifile(L, inifile, "lua"); tolua_variable(L, TOLUA_CAST "locales", &config_get_locales, 0); tolua_function(L, TOLUA_CAST "get_resource", &config_get_resource); tolua_variable(L, TOLUA_CAST "buildings", &config_get_buildings, 0); @@ -1142,12 +1142,12 @@ void lua_done(lua_State * L) { lua_close(L); } -lua_State *lua_init(void) { +lua_State *lua_init(const dictionary *inifile) { lua_State *L = luaL_newstate(); openlibs(L); register_tolua_helpers(); - tolua_bindings_open(L); + tolua_bindings_open(L, inifile); tolua_eressea_open(L); #ifdef USE_SQLITE tolua_sqlite_open(L); diff --git a/src/bindings.h b/src/bindings.h index 6c71ecf94..2db83248d 100755 --- a/src/bindings.h +++ b/src/bindings.h @@ -16,10 +16,10 @@ extern "C" { struct lua_State; struct quicklist; + struct _dictionary_; int tolua_sqlite_open(struct lua_State *L); - int tolua_bindings_open(struct lua_State *L); - int tolua_spelllist_next(struct lua_State *L); + int tolua_bindings_open(struct lua_State *L, const struct _dictionary_ *d); int tolua_itemlist_next(struct lua_State *L); int tolua_orderlist_next(struct lua_State *L); int tolua_quicklist_push(struct lua_State *L, const char *list_type, @@ -28,7 +28,7 @@ extern "C" { int log_lua_error(struct lua_State *L); void lua_done(struct lua_State *L); - struct lua_State *lua_init(void); + struct lua_State *lua_init(const struct _dictionary_ *d); int eressea_run(struct lua_State *L, const char *luafile); #ifdef __cplusplus diff --git a/src/kernel/config.c b/src/kernel/config.c index 1f3cd92b2..88130056b 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -713,6 +713,34 @@ bool config_changed(int *cache_key) { return false; } +#define MAXKEYS 16 +void config_set_from(const dictionary *d) +{ + int s, nsec = iniparser_getnsec(d); + for (s=0;s!=nsec;++s) { + char key[128]; + const char *sec = iniparser_getsecname(d, s); + int k, nkeys = iniparser_getsecnkeys(d, sec); + const char *keys[MAXKEYS]; + size_t slen = strlen(sec); + assert(nkeys <= MAXKEYS); + assert(slen #include #include "types.h" -struct param; + + struct param; + struct _dictionary_; #define DISPLAYSIZE 8192 /* max. L�nge einer Beschreibung, incl trailing 0 */ #define ORDERSIZE (DISPLAYSIZE*2) /* max. length of an order */ @@ -108,7 +110,6 @@ struct param; struct attrib *attribs; unsigned int data_turn; void *vm_state; - struct _dictionary_ *inifile; struct global_functions { int(*wage) (const struct region * r, const struct faction * f, const struct race * rc, int in_turn); @@ -123,6 +124,7 @@ struct param; void free_params(struct param **pp); void config_set(const char *key, const char *value); + void config_set_from(const struct _dictionary_ *d); const char *config_get(const char *key); int config_get_int(const char *key, int def); double config_get_flt(const char *key, double def); diff --git a/src/kernel/config.test.c b/src/kernel/config.test.c index 405587443..719ff840e 100644 --- a/src/kernel/config.test.c +++ b/src/kernel/config.test.c @@ -9,6 +9,8 @@ #include #include +#include + #include #include @@ -233,9 +235,24 @@ static void test_rules(CuTest *tc) { CuAssertIntEquals(tc, 1000, rule_faction_limit()); } +static void test_config_inifile(CuTest *tc) { + dictionary *ini; + test_setup(); + ini = dictionary_new(0); + dictionary_set(ini, "game", NULL); + iniparser_set(ini, "game:id", "42"); + iniparser_set(ini, "game:name", "Eressea"); + config_set_from(ini); + CuAssertStrEquals(tc, "Eressea", config_get("game.name")); + CuAssertIntEquals(tc, 42, game_id()); + iniparser_freedict(ini); + test_cleanup(); +} + CuSuite *get_config_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_config_inifile); SUITE_ADD_TEST(suite, test_config_cache); SUITE_ADD_TEST(suite, test_get_set_param); SUITE_ADD_TEST(suite, test_param_int); diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index 414704309..03e210ce0 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -806,7 +806,9 @@ static void json_settings(cJSON *json) { else { sprintf(value, "%d", child->valueint); } - config_set(child->string, value); + if (config_get(child->string) == NULL) { + config_set(child->string, value); + } } } } diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 950868046..93fcd8d10 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -69,12 +69,15 @@ static void test_settings(CuTest * tc) "\"string\" : \"1d4\"," "\"integer\" : 14," "\"true\": true," + "\"game.id\": 4," "\"false\": false," "\"float\" : 1.5 }}"; cJSON *json = cJSON_Parse(data); test_cleanup(); + config_set("game.id", "42"); // should not be replaced json_config(json); + CuAssertStrEquals(tc, "42", config_get("game.id")); CuAssertStrEquals(tc, "1", config_get("true")); CuAssertStrEquals(tc, "0", config_get("false")); CuAssertStrEquals(tc, "1d4", config_get("string")); diff --git a/src/main.c b/src/main.c index 2d81041ec..777770f0a 100644 --- a/src/main.c +++ b/src/main.c @@ -72,17 +72,15 @@ static void load_inifile(dictionary * d) verbosity = iniparser_getint(d, "game:verbose", 2); str = iniparser_getstring(d, "game:locales", "de,en"); make_locales(str); - - if (global.inifile) iniparser_freedict(global.inifile); - global.inifile = d; } -static void parse_config(const char *filename) +static dictionary *parse_config(const char *filename) { dictionary *d = iniparser_load(filename); if (d) { load_inifile(d); log_debug("reading from configuration file %s\n", filename); + config_set_from(d); memdebug = iniparser_getint(d, "game:memcheck", memdebug); #ifdef USE_CURSES @@ -91,6 +89,7 @@ static void parse_config(const char *filename) gm_codepage = iniparser_getint(d, "editor:codepage", gm_codepage); #endif } + return d; } static int usage(const char *prog, const char *arg) @@ -271,10 +270,11 @@ int main(int argc, char **argv) { int err = 0; lua_State *L; + dictionary *d; setup_signal_handler(); /* ini file sets defaults for arguments*/ - parse_config(inifile); - if (!global.inifile) { + d = parse_config(inifile); + if (!d) { log_error("could not open ini configuration %s\n", inifile); } /* parse arguments again, to override ini file */ @@ -282,7 +282,7 @@ int main(int argc, char **argv) locale_init(); - L = lua_init(); + L = lua_init(d); game_init(); bind_monsters(L); err = eressea_run(L, luafile); @@ -293,8 +293,8 @@ int main(int argc, char **argv) game_done(); lua_done(L); log_close(); - if (global.inifile) { - iniparser_freedict(global.inifile); + if (d) { + iniparser_freedict(d); } return 0; }