summaryrefslogtreecommitdiff
path: root/library/LuaTools.cpp
diff options
context:
space:
mode:
authorAlexander Gavrilov2012-05-04 19:47:18 +0400
committerAlexander Gavrilov2012-05-04 19:47:18 +0400
commitd4d6349f48d01b31f59f94238d6656e3b5d08508 (patch)
tree43e47bbb5bab970825ff217ddbc16e21a3711033 /library/LuaTools.cpp
parent5afe2ca0020404d46ad3de840030a7b9e186b059 (diff)
downloaddfhack-d4d6349f48d01b31f59f94238d6656e3b5d08508.tar.gz
dfhack-d4d6349f48d01b31f59f94238d6656e3b5d08508.tar.bz2
dfhack-d4d6349f48d01b31f59f94238d6656e3b5d08508.tar.xz
Expose builtin commands to dfhack-run, and add lua script support.
Move builtin command implementation to Core methods, and fall back to hack/scripts/*.lua for otherwise unrecognized commands.
Diffstat (limited to 'library/LuaTools.cpp')
-rw-r--r--library/LuaTools.cpp20
1 files changed, 16 insertions, 4 deletions
diff --git a/library/LuaTools.cpp b/library/LuaTools.cpp
index 85e93fbb..17ad0abd 100644
--- a/library/LuaTools.cpp
+++ b/library/LuaTools.cpp
@@ -706,6 +706,16 @@ static int DFHACK_DFHACK_TOKEN = 0;
static int DFHACK_BASE_G_TOKEN = 0;
static int DFHACK_REQUIRE_TOKEN = 0;
+void Lua::PushDFHack(lua_State *state)
+{
+ lua_rawgetp(state, LUA_REGISTRYINDEX, &DFHACK_DFHACK_TOKEN);
+}
+
+void Lua::PushBaseGlobals(lua_State *state)
+{
+ lua_rawgetp(state, LUA_REGISTRYINDEX, &DFHACK_BASE_G_TOKEN);
+}
+
bool DFHack::Lua::PushModule(color_ostream &out, lua_State *state, const char *module)
{
AssertCoreSuspend(state);
@@ -884,13 +894,9 @@ bool DFHack::Lua::RunCoreQueryLoop(color_ostream &out, lua_State *state,
bool (*init)(color_ostream&, lua_State*, void*),
void *arg)
{
- if (!out.is_console())
- return false;
if (!lua_checkstack(state, 20))
return false;
- Console &con = static_cast<Console&>(out);
-
lua_State *thread;
int rv;
std::string prompt;
@@ -910,6 +916,10 @@ bool DFHack::Lua::RunCoreQueryLoop(color_ostream &out, lua_State *state,
return false;
}
+ // If not interactive, run without coroutine and bail out
+ if (!out.is_console())
+ return SafeCall(out, state, lua_gettop(state)-base-1, 0);
+
lua_rawgetp(state, LUA_REGISTRYINDEX, &DFHACK_QUERY_COROTABLE_TOKEN);
lua_pushvalue(state, base+1);
lua_remove(state, base+1);
@@ -920,6 +930,8 @@ bool DFHack::Lua::RunCoreQueryLoop(color_ostream &out, lua_State *state,
rv = resume_query_loop(out, thread, state, lua_gettop(state)-base, prompt, histfile);
}
+ Console &con = static_cast<Console&>(out);
+
while (rv == LUA_YIELD)
{
if (histfile != histname)