Merge pull request #630 from ennorehling/develop

remove per-game configuration from git
This commit is contained in:
Enno Rehling 2017-01-21 21:04:09 +01:00 committed by GitHub
commit 65d188eeaa
9 changed files with 73 additions and 20 deletions

View File

@ -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

View File

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

View File

@ -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

View File

@ -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<sizeof(key));
memcpy(key, sec, slen);
key[slen] = '.';
iniparser_getseckeys(d, sec, keys);
for (k=0;k!=nkeys;++k) {
const char *val;
size_t klen = strlen(keys[k]);
assert(klen+slen+1<sizeof(key));
memcpy(key+slen+1, keys[k]+slen+1, klen-slen);
val = iniparser_getstring(d, keys[k], NULL);
if (val) {
config_set(key, val);
}
}
}
}
void config_set(const char *key, const char *value) {
++config_cache_key;
set_param(&configuration, key, value);

View File

@ -27,7 +27,9 @@ extern "C" {
#include <stddef.h>
#include <stdbool.h>
#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);

View File

@ -9,6 +9,8 @@
#include <util/base36.h>
#include <util/attrib.h>
#include <iniparser.h>
#include <CuTest.h>
#include <tests.h>
@ -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);

View File

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

View File

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

View File

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