summaryrefslogtreecommitdiff
path: root/library/LuaTools.cpp
diff options
context:
space:
mode:
authorAlexander Gavrilov2012-04-17 11:45:09 +0400
committerAlexander Gavrilov2012-04-17 11:45:09 +0400
commit3beb2ebf25b94736a3823c317f603a8fc8bd0f53 (patch)
tree3b1af8b380074ba53ff617ddb5177e82dbb83eb2 /library/LuaTools.cpp
parent7946cafc8673401bcfa2ff2e116592755a0968f4 (diff)
downloaddfhack-3beb2ebf25b94736a3823c317f603a8fc8bd0f53.tar.gz
dfhack-3beb2ebf25b94736a3823c317f603a8fc8bd0f53.tar.bz2
dfhack-3beb2ebf25b94736a3823c317f603a8fc8bd0f53.tar.xz
Export the onStateChange event to core lua context & add some docs.
Diffstat (limited to 'library/LuaTools.cpp')
-rw-r--r--library/LuaTools.cpp58
1 files changed, 47 insertions, 11 deletions
diff --git a/library/LuaTools.cpp b/library/LuaTools.cpp
index be4fa864..469caa65 100644
--- a/library/LuaTools.cpp
+++ b/library/LuaTools.cpp
@@ -1098,6 +1098,18 @@ int DFHack::Lua::NewEvent(lua_State *state)
return 1;
}
+static void do_invoke_event(lua_State *L, int argbase, int num_args, int errorfun)
+{
+ for (int i = 0; i < num_args; i++)
+ lua_pushvalue(L, argbase+i);
+
+ if (lua_pcall(L, num_args, 0, errorfun) != LUA_OK)
+ {
+ report_error(L);
+ lua_pop(L, 1);
+ }
+}
+
static void dfhack_event_invoke(lua_State *L, int base, bool from_c)
{
int event = base+1;
@@ -1108,23 +1120,31 @@ static void dfhack_event_invoke(lua_State *L, int base, bool from_c)
lua_insert(L, errorfun);
int argbase = base+3;
- lua_pushnil(L);
- // stack: |base| event errorfun (args) key cb (args)
+ // stack: |base| event errorfun (args)
- while (lua_next(L, event))
+ if (!from_c)
{
- if (from_c && lua_islightuserdata(L, -1) && !lua_touserdata(L, -1))
- continue;
+ // Invoke the NULL key first
+ lua_rawgetp(L, event, NULL);
- for (int i = 0; i < num_args; i++)
- lua_pushvalue(L, argbase+i);
+ if (lua_isnil(L, -1))
+ lua_pop(L, 1);
+ else
+ do_invoke_event(L, argbase, num_args, errorfun);
+ }
- if (lua_pcall(L, num_args, 0, errorfun) != LUA_OK)
- {
- report_error(L);
+ lua_pushnil(L);
+
+ // stack: |base| event errorfun (args) key || cb (args)
+
+ while (lua_next(L, event))
+ {
+ // Skip the NULL key in the main loop
+ if (lua_islightuserdata(L, -2) && !lua_touserdata(L, -2))
lua_pop(L, 1);
- }
+ else
+ do_invoke_event(L, argbase, num_args, errorfun);
}
lua_settop(L, base);
@@ -1285,13 +1305,29 @@ lua_State *DFHack::Lua::Open(color_ostream &out, lua_State *state)
return state;
}
+void DFHack::Lua::Core::onStateChange(color_ostream &out, int code) {
+ if (!State) return;
+
+ Lua::Push(State, code);
+ Lua::InvokeEvent(out, State, (void*)onStateChange, 1);
+}
+
void DFHack::Lua::Core::Init(color_ostream &out)
{
if (State)
return;
State = luaL_newstate();
+
Lua::Open(out, State);
+
+ // Register events
+ lua_getglobal(State, "dfhack");
+
+ MakeEvent(State, (void*)onStateChange);
+ lua_setfield(State, -2, "onStateChange");
+
+ lua_pop(State, 1);
}
void DFHack::Lua::Core::Reset(color_ostream &out, const char *where)