From 23b1d33f775f7d91280d328c9b1a64151d09c75c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 1 Jul 2009 11:51:21 +0000 Subject: [PATCH] preloading lua files (for running tests.lua) from eressea.ini fixed building taxes test --- src/common/kernel/building.h | 2 +- src/common/kernel/xmlreader.c | 2 +- src/eressea/server.c | 43 ++++++++++++++++++++++++------- src/eressea/tolua/bind_building.c | 9 +++++++ src/eressea/tolua/helpers.c | 6 ++--- src/res/e3a.xml | 5 ++++ src/scripts/run-e3a.lua | 3 --- src/scripts/tests.lua | 2 +- 8 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/common/kernel/building.h b/src/common/kernel/building.h index da12dbcdf..0e379571f 100644 --- a/src/common/kernel/building.h +++ b/src/common/kernel/building.h @@ -61,7 +61,7 @@ typedef struct building_type { void (*init)(struct building_type*); void (*age)(struct building *); int (*protection)(struct building *, struct unit *); - float (*taxes)(struct building *); + double (*taxes)(struct building *); struct attrib * attribs; } building_type; diff --git a/src/common/kernel/xmlreader.c b/src/common/kernel/xmlreader.c index 0488274e4..c39855ef6 100644 --- a/src/common/kernel/xmlreader.c +++ b/src/common/kernel/xmlreader.c @@ -320,7 +320,7 @@ parse_buildings(xmlDocPtr doc) } else if (strcmp((const char*)propValue, "protection")==0) { btype->protection = (int (*)(struct building*, struct unit *))fun; } else if (strcmp((const char*)propValue, "taxes")==0) { - btype->taxes = (float (*)(struct building*))fun; + btype->taxes = (double (*)(struct building*))fun; } else if (strcmp((const char*)propValue, "age")==0) { btype->age = (void (*)(struct building*))fun; } else { diff --git a/src/eressea/server.c b/src/eressea/server.c index 9ce3d827b..11f9a13d6 100644 --- a/src/eressea/server.c +++ b/src/eressea/server.c @@ -171,6 +171,7 @@ static boolean g_writemap = false; static boolean g_ignore_errors = false; static boolean opt_reportonly = false; static const char * luafile = NULL; +static const char * preload = NULL; static const char * script_path = "scripts"; static int memdebug = 0; @@ -267,6 +268,10 @@ static const struct { {LUA_IOLIBNAME, luaopen_io}, {LUA_STRLIBNAME, luaopen_string}, {LUA_MATHLIBNAME, luaopen_math}, +/* + {LUA_LOADLIBNAME, luaopen_package}, + {LUA_DBLIBNAME, luaopen_debug}, +*/ #if LUA_VERSION_NUM>=501 {LUA_OSLIBNAME, luaopen_os}, #endif @@ -588,6 +593,7 @@ load_inifile(const char * filename) verbosity = iniparser_getint(d, "eressea:verbose", 2); battledebug = iniparser_getint(d, "eressea:debug", battledebug)?1:0; + preload = iniparser_getstring(d, "eressea:preload", preload); luafile = iniparser_getstring(d, "eressea:run", luafile); g_reportdir = iniparser_getstring(d, "eressea:report", g_reportdir); @@ -657,7 +663,7 @@ main(int argc, char *argv[]) int i; char * lc_ctype; char * lc_numeric; - lua_State * luaState = lua_init(); + lua_State * L = lua_init(); static int write_csv = 1; setup_signal_handler(); @@ -671,14 +677,14 @@ main(int argc, char *argv[]) if (lc_ctype) lc_ctype = strdup(lc_ctype); if (lc_numeric) lc_numeric = strdup(lc_numeric); - global.vm_state = luaState; + global.vm_state = L; load_inifile("eressea.ini"); if (verbosity>=4) { printf("\n%s PBEM host\n" "Copyright (C) 1996-2005 C. Schlittchen, K. Zedel, E. Rehling, H. Peters.\n\n" "Compilation: " __DATE__ " at " __TIME__ "\nVersion: %f\n\n", global.gamename, version()); } - if ((i=read_args(argc, argv, luaState))!=0) return i; + if ((i=read_args(argc, argv, L))!=0) return i; #ifdef CRTDBG init_crtdbg(); @@ -692,7 +698,24 @@ main(int argc, char *argv[]) write_spells(); } /* run the main script */ - if (luafile==NULL) lua_console(luaState); + if (preload!=NULL) { + char * tokens = strdup(preload); + char * filename = strtok(tokens, ":"); + while (filename) { + char buf[MAX_PATH]; + if (script_path) { + sprintf(buf, "%s/%s", script_path, filename); + } + else strcpy(buf, filename); + lua_getglobal(L, "dofile"); + lua_pushstring(L, buf); + if (lua_pcall(L, 1, 0, 0) != 0) { + my_lua_error(L); + } + filename = strtok(NULL, ":"); + } + } + if (luafile==NULL) lua_console(L); else { char buf[MAX_PATH]; if (script_path) { @@ -701,7 +724,7 @@ main(int argc, char *argv[]) else strcpy(buf, luafile); #ifdef BINDINGS_LUABIND try { - luabind::call_function(luaState, "dofile", buf); + luabind::call_function(L, "dofile", buf); } catch (std::runtime_error& rte) { log_error(("%s.\n", rte.what())); @@ -711,10 +734,10 @@ main(int argc, char *argv[]) my_lua_error(L); } #elif defined(BINDINGS_TOLUA) - lua_getglobal(luaState, "dofile"); - lua_pushstring(luaState, buf); - if (lua_pcall(luaState, 1, 0, 0) != 0) { - my_lua_error(luaState); + lua_getglobal(L, "dofile"); + lua_pushstring(L, buf); + if (lua_pcall(L, 1, 0, 0) != 0) { + my_lua_error(L); } #endif } @@ -723,7 +746,7 @@ main(int argc, char *argv[]) #endif game_done(); kernel_done(); - lua_done(luaState); + lua_done(L); log_close(); setlocale(LC_CTYPE, lc_ctype); diff --git a/src/eressea/tolua/bind_building.c b/src/eressea/tolua/bind_building.c index 3926fe905..38dd5fe9c 100644 --- a/src/eressea/tolua/bind_building.c +++ b/src/eressea/tolua/bind_building.c @@ -122,6 +122,14 @@ tolua_building_get_id(lua_State* L) return 1; } +static int +tolua_building_get_type(lua_State* L) +{ + building * self = (building *)tolua_tousertype(L, 1, 0); + tolua_pushstring(L, self->type->_name); + return 1; +} + static int tolua_building_create(lua_State* L) { @@ -160,6 +168,7 @@ tolua_building_open(lua_State* L) tolua_function(L, "__tostring", tolua_building_tostring); tolua_variable(L, "id", tolua_building_get_id, NULL); + tolua_variable(L, "type", tolua_building_get_type, NULL); tolua_variable(L, "name", tolua_building_get_name, tolua_building_set_name); tolua_variable(L, "units", tolua_building_get_units, NULL); tolua_variable(L, "region", tolua_building_get_region, tolua_building_set_region); diff --git a/src/eressea/tolua/helpers.c b/src/eressea/tolua/helpers.c index e2d7a5f16..bf597bc47 100644 --- a/src/eressea/tolua/helpers.c +++ b/src/eressea/tolua/helpers.c @@ -435,12 +435,12 @@ lua_building_protection(building * b, unit * u) return result; } -static float +static double lua_building_taxes(building * b) { lua_State * L = (lua_State *)global.vm_state; const char * fname = "building_taxes"; - float result = 0.0F; + double result = 0.0F; lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); @@ -453,7 +453,7 @@ lua_building_taxes(building * b) buildingname(b), fname, error)); lua_pop(L, 1); } else { - result = (float)lua_tonumber(L, -1); + result = (double)lua_tonumber(L, -1); } } else { log_error(("building_taxes(%s) calling '%s': not a function.\n", diff --git a/src/res/e3a.xml b/src/res/e3a.xml index 6476faf32..40762acd5 100644 --- a/src/res/e3a.xml +++ b/src/res/e3a.xml @@ -116,6 +116,11 @@ + + + + + diff --git a/src/scripts/run-e3a.lua b/src/scripts/run-e3a.lua index b8882a042..7f04ced84 100644 --- a/src/scripts/run-e3a.lua +++ b/src/scripts/run-e3a.lua @@ -9,8 +9,6 @@ function loadscript(name) end end -loadscript("default.lua") - function change_locales() -- local localechange = { } local localechange = { de = { "rtph" } } @@ -30,7 +28,6 @@ function load_scripts() scripts = { "spells.lua", "extensions.lua", - "e3a/rules.lua" } for index, value in pairs(scripts) do loadscript(value) diff --git a/src/scripts/tests.lua b/src/scripts/tests.lua index 74d5eef74..858464f2c 100644 --- a/src/scripts/tests.lua +++ b/src/scripts/tests.lua @@ -360,7 +360,7 @@ function test_taxes() u:clear_orders() u:add_order("LERNE Wahrnehmung") local b = building.create(r, "watch") - b.size = 4 + b.size = 10 u.building = b update_owners() process_orders()