diff --git a/library/Core.cpp b/library/Core.cpp index e2f45c5f7..9c2295435 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -288,6 +288,10 @@ namespace { const string *pcmd; vector *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 &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()) { diff --git a/library/lua/dfhack.lua b/library/lua/dfhack.lua index 9f7b2649e..4f2f3f580 100644 --- a/library/lua/dfhack.lua +++ b/library/lua/dfhack.lua @@ -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