Allow lua scripts to be enabled and disabled with built-in commands

develop
lethosor 2015-03-10 18:29:36 -04:00
parent db423c1aa6
commit eaf282c18e
2 changed files with 45 additions and 2 deletions

@ -288,6 +288,10 @@ namespace {
const string *pcmd;
vector<string> *pargs;
};
struct ScriptEnableState {
const string *pcmd;
bool pstate;
};
}
static bool init_run_script(color_ostream &out, lua_State *state, void *info)
@ -315,6 +319,30 @@ static command_result runLuaScript(color_ostream &out, std::string name, vector<
return ok ? CR_OK : CR_FAILURE;
}
static bool init_enable_script(color_ostream &out, lua_State *state, void *info)
{
auto args = (ScriptEnableState*)info;
if (!lua_checkstack(state, 4))
return false;
Lua::PushDFHack(state);
lua_getfield(state, -1, "enable_script");
lua_remove(state, -2);
lua_pushstring(state, args->pcmd->c_str());
lua_pushboolean(state, args->pstate);
return true;
}
static command_result enableLuaScript(color_ostream &out, std::string name, bool state)
{
ScriptEnableState data;
data.pcmd = &name;
data.pstate = state;
bool ok = Lua::RunCoreQueryLoop(out, Lua::Core::State, init_enable_script, &data);
return ok ? CR_OK : CR_FAILURE;
}
static command_result runRubyScript(color_ostream &out, PluginManager *plug_mgr, std::string name, vector<string> &args)
{
if (!plug_mgr->ruby || !plug_mgr->ruby->is_enabled())
@ -637,8 +665,16 @@ command_result Core::runCommand(color_ostream &con, const std::string &first_, v
if(!plug)
{
res = CR_NOT_FOUND;
con.printerr("No such plugin: %s\n", parts[i].c_str());
std::string lua = findScript(this->p->getPath(), parts[i] + ".lua");
if (lua.size())
{
res = enableLuaScript(con, parts[i], enable);
}
else
{
res = CR_NOT_FOUND;
con.printerr("No such plugin or Lua script: %s\n", parts[i].c_str());
}
}
else if (!plug->can_set_enabled())
{

@ -443,6 +443,13 @@ function dfhack.run_script(name,...)
return dfhack.run_script_with_env(nil, name, nil, ...)
end
function dfhack.enable_script(name, state)
local res = dfhack.pcall(dfhack.run_script_with_env, nil, name, {enable=true, enable_state=state})
if not res then
qerror('Cannot ' .. (state and 'enable' or 'disable') .. ' Lua script: ' .. name)
end
end
function dfhack.script_environment(name)
_, env = dfhack.run_script_with_env(nil, name, {module=true})
return env