From ebbe4fa4acd4c82cb58284d4efc912ed2dc908d8 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 21 Jan 2017 19:53:47 +0100 Subject: [PATCH] issue #629: fix eressea.ini config fixed reading of ini data into config (keys have a . here, not a :). added a test. removed obsolete global.inifile variable. --- src/bindings.c | 10 +++++----- src/bindings.h | 6 +++--- src/kernel/config.c | 10 +++++----- src/kernel/config.h | 5 +++-- src/kernel/config.test.c | 17 +++++++++++++++++ src/main.c | 17 ++++++++--------- 6 files changed, 41 insertions(+), 24 deletions(-) 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 955a9d598..4b8bc7e72 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -721,12 +721,12 @@ void config_set_from(const dictionary *d) char key[128]; const char *sec = iniparser_getsecname(d, s); int k, nkeys = iniparser_getsecnkeys(d, sec); - const char *akeys[MAXKEYS]; - const char ** keys = akeys; + char *akeys[MAXKEYS]; + char ** keys = akeys; size_t slen = strlen(sec); assert(slenMAXKEYS) { keys = malloc(sizeof(const char *) * nkeys); } @@ -735,8 +735,8 @@ void config_set_from(const dictionary *d) const char *val; size_t klen = strlen(keys[k]); assert(klen+slen+1 #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); 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/main.c b/src/main.c index 97c84dffe..777770f0a 100644 --- a/src/main.c +++ b/src/main.c @@ -72,12 +72,9 @@ 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) { @@ -92,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) @@ -272,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 */ @@ -283,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); @@ -294,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; }