From f4e25fe20e625dc6b8cbf71b4e8ae8a7246785c9 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 22 Jan 2017 18:01:09 +0100 Subject: [PATCH] try loading config.lua and custom.lua files, if they exist. --- src/bindings.c | 68 ++++++++++++++++++++++++++++++++--------------- src/kernel/save.h | 1 - 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/bindings.c b/src/bindings.c index 576c54939..e7c4859b4 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -1168,32 +1168,56 @@ lua_State *lua_init(const dictionary *inifile) { return L; } +static int run_script(lua_State *L, const char *luafile) { + int err; + FILE *F; + + F = fopen(luafile, "r"); + if (!F) { + log_debug("dofile('%s'): %s", luafile, strerror(errno)); + return errno; + } + fclose(F); + + log_debug("executing script %s", luafile); + lua_getglobal(L, "dofile"); + lua_pushstring(L, luafile); + err = lua_pcall(L, 1, 1, -3); /* error handler (debug.traceback) is now at stack -3 */ + if (err != 0) { + log_lua_error(L); + assert(!"Lua syntax error? check log."); + } + else { + if (lua_isnumber(L, -1)) { + err = (int)lua_tonumber(L, -1); + } + lua_pop(L, 1); + } + return err; +} + int eressea_run(lua_State *L, const char *luafile) { - int err = 0; - + int err; global.vm_state = L; + + /* push an error handling function on the stack: */ + lua_getglobal(L, "debug"); + lua_getfield(L, -1, "traceback"); + lua_remove(L, -2); + + /* try to run configuration scripts: */ + err = run_script(L, "config.lua"); + err = run_script(L, "custom.lua"); + /* run the main script */ if (luafile) { - log_debug("executing script %s\n", luafile); - - lua_getglobal(L, "debug"); - lua_getfield(L, -1, "traceback"); - lua_remove(L, -2); - lua_getglobal(L, "dofile"); - lua_pushstring(L, luafile); - err = lua_pcall(L, 1, 1, -3); - if (err != 0) { - log_lua_error(L); - assert(!"Lua syntax error? check log."); - } - else { - if (lua_isnumber(L, -1)) { - err = (int)lua_tonumber(L, -1); - } - lua_pop(L, 1); - } - return err; + err = run_script(L, luafile); } - return lua_console(L); + else { + err = lua_console(L); + } + /* pop error handler off the stack: */ + lua_pop(L, 1); + return err; } diff --git a/src/kernel/save.h b/src/kernel/save.h index be78b9c68..4ead34efe 100644 --- a/src/kernel/save.h +++ b/src/kernel/save.h @@ -44,7 +44,6 @@ extern "C" { extern int enc_gamedata; int readorders(const char *filename); - int creategame(void); int readgame(const char *filename); int writegame(const char *filename);