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.
This commit is contained in:
Enno Rehling 2017-01-21 19:53:47 +01:00
parent 23e0943baa
commit ebbe4fa4ac
6 changed files with 41 additions and 24 deletions

View File

@ -978,7 +978,7 @@ static int tolua_report_unit(lua_State * L)
return 1; 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; int i;
const char *arg; 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); 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); tolua_open(L);
@ -1072,7 +1072,7 @@ int tolua_bindings_open(lua_State * L)
tolua_module(L, TOLUA_CAST "config", 1); tolua_module(L, TOLUA_CAST "config", 1);
tolua_beginmodule(L, TOLUA_CAST "config"); 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_variable(L, TOLUA_CAST "locales", &config_get_locales, 0);
tolua_function(L, TOLUA_CAST "get_resource", &config_get_resource); tolua_function(L, TOLUA_CAST "get_resource", &config_get_resource);
tolua_variable(L, TOLUA_CAST "buildings", &config_get_buildings, 0); tolua_variable(L, TOLUA_CAST "buildings", &config_get_buildings, 0);
@ -1142,12 +1142,12 @@ void lua_done(lua_State * L) {
lua_close(L); lua_close(L);
} }
lua_State *lua_init(void) { lua_State *lua_init(const dictionary *inifile) {
lua_State *L = luaL_newstate(); lua_State *L = luaL_newstate();
openlibs(L); openlibs(L);
register_tolua_helpers(); register_tolua_helpers();
tolua_bindings_open(L); tolua_bindings_open(L, inifile);
tolua_eressea_open(L); tolua_eressea_open(L);
#ifdef USE_SQLITE #ifdef USE_SQLITE
tolua_sqlite_open(L); tolua_sqlite_open(L);

View File

@ -16,10 +16,10 @@ extern "C" {
struct lua_State; struct lua_State;
struct quicklist; struct quicklist;
struct _dictionary_;
int tolua_sqlite_open(struct lua_State *L); int tolua_sqlite_open(struct lua_State *L);
int tolua_bindings_open(struct lua_State *L); int tolua_bindings_open(struct lua_State *L, const struct _dictionary_ *d);
int tolua_spelllist_next(struct lua_State *L);
int tolua_itemlist_next(struct lua_State *L); int tolua_itemlist_next(struct lua_State *L);
int tolua_orderlist_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, 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); int log_lua_error(struct lua_State *L);
void lua_done(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); int eressea_run(struct lua_State *L, const char *luafile);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -721,12 +721,12 @@ void config_set_from(const dictionary *d)
char key[128]; char key[128];
const char *sec = iniparser_getsecname(d, s); const char *sec = iniparser_getsecname(d, s);
int k, nkeys = iniparser_getsecnkeys(d, sec); int k, nkeys = iniparser_getsecnkeys(d, sec);
const char *akeys[MAXKEYS]; char *akeys[MAXKEYS];
const char ** keys = akeys; char ** keys = akeys;
size_t slen = strlen(sec); size_t slen = strlen(sec);
assert(slen<sizeof(key)); assert(slen<sizeof(key));
memcpy(key, sec, slen); memcpy(key, sec, slen);
key[slen] = ':'; key[slen] = '.';
if (nkeys>MAXKEYS) { if (nkeys>MAXKEYS) {
keys = malloc(sizeof(const char *) * nkeys); keys = malloc(sizeof(const char *) * nkeys);
} }
@ -735,8 +735,8 @@ void config_set_from(const dictionary *d)
const char *val; const char *val;
size_t klen = strlen(keys[k]); size_t klen = strlen(keys[k]);
assert(klen+slen+1<sizeof(key)); assert(klen+slen+1<sizeof(key));
memcpy(key+slen+1, keys[k], klen+1); memcpy(key+slen+1, keys[k]+slen+1, klen+1);
val = iniparser_getstring(d, key, NULL); val = iniparser_getstring(d, keys[k], NULL);
if (val) { if (val) {
config_set(key, val); config_set(key, val);
} }

View File

@ -27,7 +27,9 @@ extern "C" {
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include "types.h" #include "types.h"
struct param;
struct param;
struct _dictionary_;
#define DISPLAYSIZE 8192 /* max. L<>nge einer Beschreibung, incl trailing 0 */ #define DISPLAYSIZE 8192 /* max. L<>nge einer Beschreibung, incl trailing 0 */
#define ORDERSIZE (DISPLAYSIZE*2) /* max. length of an order */ #define ORDERSIZE (DISPLAYSIZE*2) /* max. length of an order */
@ -108,7 +110,6 @@ struct param;
struct attrib *attribs; struct attrib *attribs;
unsigned int data_turn; unsigned int data_turn;
void *vm_state; void *vm_state;
struct _dictionary_ *inifile;
struct global_functions { struct global_functions {
int(*wage) (const struct region * r, const struct faction * f, int(*wage) (const struct region * r, const struct faction * f,
const struct race * rc, int in_turn); const struct race * rc, int in_turn);

View File

@ -9,6 +9,8 @@
#include <util/base36.h> #include <util/base36.h>
#include <util/attrib.h> #include <util/attrib.h>
#include <iniparser.h>
#include <CuTest.h> #include <CuTest.h>
#include <tests.h> #include <tests.h>
@ -233,9 +235,24 @@ static void test_rules(CuTest *tc) {
CuAssertIntEquals(tc, 1000, rule_faction_limit()); 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 *get_config_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_config_inifile);
SUITE_ADD_TEST(suite, test_config_cache); SUITE_ADD_TEST(suite, test_config_cache);
SUITE_ADD_TEST(suite, test_get_set_param); SUITE_ADD_TEST(suite, test_get_set_param);
SUITE_ADD_TEST(suite, test_param_int); SUITE_ADD_TEST(suite, test_param_int);

View File

@ -72,12 +72,9 @@ static void load_inifile(dictionary * d)
verbosity = iniparser_getint(d, "game:verbose", 2); verbosity = iniparser_getint(d, "game:verbose", 2);
str = iniparser_getstring(d, "game:locales", "de,en"); str = iniparser_getstring(d, "game:locales", "de,en");
make_locales(str); 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); dictionary *d = iniparser_load(filename);
if (d) { if (d) {
@ -92,6 +89,7 @@ static void parse_config(const char *filename)
gm_codepage = iniparser_getint(d, "editor:codepage", gm_codepage); gm_codepage = iniparser_getint(d, "editor:codepage", gm_codepage);
#endif #endif
} }
return d;
} }
static int usage(const char *prog, const char *arg) static int usage(const char *prog, const char *arg)
@ -272,10 +270,11 @@ int main(int argc, char **argv)
{ {
int err = 0; int err = 0;
lua_State *L; lua_State *L;
dictionary *d;
setup_signal_handler(); setup_signal_handler();
/* ini file sets defaults for arguments*/ /* ini file sets defaults for arguments*/
parse_config(inifile); d = parse_config(inifile);
if (!global.inifile) { if (!d) {
log_error("could not open ini configuration %s\n", inifile); log_error("could not open ini configuration %s\n", inifile);
} }
/* parse arguments again, to override ini file */ /* parse arguments again, to override ini file */
@ -283,7 +282,7 @@ int main(int argc, char **argv)
locale_init(); locale_init();
L = lua_init(); L = lua_init(d);
game_init(); game_init();
bind_monsters(L); bind_monsters(L);
err = eressea_run(L, luafile); err = eressea_run(L, luafile);
@ -294,8 +293,8 @@ int main(int argc, char **argv)
game_done(); game_done();
lua_done(L); lua_done(L);
log_close(); log_close();
if (global.inifile) { if (d) {
iniparser_freedict(global.inifile); iniparser_freedict(d);
} }
return 0; return 0;
} }