Use a separate lua state in dwarfmonitor render hook

develop
lethosor 2015-07-31 14:25:26 -04:00
parent f3080b4fb5
commit 01e04c24c5
1 changed files with 12 additions and 11 deletions

@ -44,8 +44,6 @@
#include "df/descriptor_shape.h" #include "df/descriptor_shape.h"
#include "df/descriptor_color.h" #include "df/descriptor_color.h"
#include "jsonxx.h"
using std::deque; using std::deque;
DFHACK_PLUGIN("dwarfmonitor"); DFHACK_PLUGIN("dwarfmonitor");
@ -157,6 +155,7 @@ static void open_stats_srceen();
namespace dm_lua { namespace dm_lua {
static color_ostream_proxy *out; static color_ostream_proxy *out;
static lua_State *state;
typedef int(*initializer)(lua_State*); typedef int(*initializer)(lua_State*);
int no_args (lua_State *L) { return 0; } int no_args (lua_State *L) { return 0; }
void cleanup() void cleanup()
@ -166,26 +165,26 @@ namespace dm_lua {
delete out; delete out;
out = NULL; out = NULL;
} }
lua_close(state);
} }
bool init_call (lua_State *L, const char *func) bool init_call (const char *func)
{ {
if (!out) if (!out)
out = new color_ostream_proxy(Core::getInstance().getConsole()); out = new color_ostream_proxy(Core::getInstance().getConsole());
return Lua::PushModulePublic(*out, L, "plugins.dwarfmonitor", func); return Lua::PushModulePublic(*out, state, "plugins.dwarfmonitor", func);
} }
bool safe_call (lua_State *L, int nargs) bool safe_call (int nargs)
{ {
return Lua::SafeCall(*out, L, nargs, 0); return Lua::SafeCall(*out, state, nargs, 0);
} }
bool call (const char *func, initializer init = no_args) bool call (const char *func, initializer init = no_args)
{ {
auto L = Lua::Core::State; Lua::StackUnwinder top(state);
Lua::StackUnwinder top(L); if (!init_call(func))
if (!init_call(L, func))
return false; return false;
int nargs = init(L); int nargs = init(state);
return safe_call(L, nargs); return safe_call(nargs);
} }
template <typename KeyType, typename ValueType> template <typename KeyType, typename ValueType>
@ -1977,6 +1976,8 @@ DFhackCExport command_result plugin_init(color_ostream &out, std::vector <Plugin
" Reload configuration file (dfhack-config/dwarfmonitor.json)\n" " Reload configuration file (dfhack-config/dwarfmonitor.json)\n"
)); ));
dm_lua::state = Lua::Open(out);
return CR_OK; return CR_OK;
} }