From 3c06f3badad353829f21bc00842c905e0960f6c1 Mon Sep 17 00:00:00 2001 From: expwnent Date: Mon, 7 Jul 2014 08:50:40 -0400 Subject: [PATCH] Allowed per-save script folders. --- library/Core.cpp | 58 ++++++++++++++++++++++++++++++------------ library/lua/dfhack.lua | 35 ++++++++++++++++++++----- 2 files changed, 70 insertions(+), 23 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index 15c7064c8..175610700 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -393,6 +393,26 @@ static bool try_autocomplete(color_ostream &con, const std::string &first, std:: return false; } +string findScript(string path, string name) { + //first try the save folder if it exists + string save = World::ReadWorldFolder(); + if ( save != "" ) { + string file = path + "/data/save/" + save + "/raw/scripts/" + name; + if (fileExists(file)) { + return file; + } + } + string file = path + "/raw/scripts/" + name; + if (fileExists(file)) { + return file; + } + file = path + "/hack/scripts/" + name; + if (fileExists(file)) { + return file; + } + return ""; +} + command_result Core::runCommand(color_ostream &con, const std::string &first, vector &parts) { if (!first.empty()) @@ -446,18 +466,20 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve return CR_OK; } } - auto filename = getHackPath() + "scripts/" + parts[0]; - if (fileExists(filename + ".lua")) - { - string help = getScriptHelp(filename + ".lua", "-- "); + string path = this->p->getPath(); + string file = findScript(path, parts[0] + ".lua"); + if ( file != "" ) { + string help = getScriptHelp(file, "-- "); con.print("%s: %s\n", parts[0].c_str(), help.c_str()); return CR_OK; } - if (plug_mgr->ruby && plug_mgr->ruby->is_enabled() && fileExists(filename + ".rb")) - { - string help = getScriptHelp(filename + ".rb", "# "); - con.print("%s: %s\n", parts[0].c_str(), help.c_str()); - return CR_OK; + if (plug_mgr->ruby && plug_mgr->ruby->is_enabled() ) { + file = findScript(path, parts[0] + ".rb"); + if ( file != "" ) { + string help = getScriptHelp(file, "# "); + con.print("%s: %s\n", parts[0].c_str(), help.c_str()); + return CR_OK; + } } con.printerr("Unknown command: %s\n", parts[0].c_str()); } @@ -765,15 +787,19 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve command_result res = plug_mgr->InvokeCommand(con, first, parts); if(res == CR_NOT_IMPLEMENTED) { - auto filename = getHackPath() + "scripts/" + first; - std::string completed; - - if (fileExists(filename + ".lua")) + string completed; + string path = this->p->getPath(); + string filename = findScript(path, first + ".lua"); + bool lua = filename != ""; + if ( !lua ) { + filename = findScript(path, first + ".rb"); + } + if ( lua ) res = runLuaScript(con, first, parts); - else if (plug_mgr->ruby && plug_mgr->ruby->is_enabled() && fileExists(filename + ".rb")) + else if ( filename != "" && plug_mgr->ruby && plug_mgr->ruby->is_enabled() ) res = runRubyScript(con, plug_mgr, first, parts); - else if (try_autocomplete(con, first, completed)) - return CR_NOT_IMPLEMENTED;// runCommand(con, completed, parts); + else if ( try_autocomplete(con, first, completed) ) + return CR_NOT_IMPLEMENTED; else con.printerr("%s is not a recognized command.\n", first.c_str()); } diff --git a/library/lua/dfhack.lua b/library/lua/dfhack.lua index ca9d43d1e..ea79c371b 100644 --- a/library/lua/dfhack.lua +++ b/library/lua/dfhack.lua @@ -371,20 +371,41 @@ internal.scripts = internal.scripts or {} local scripts = internal.scripts local hack_path = dfhack.getHackPath() -function dfhack.run_script(name,...) - local key = string.lower(name) +local function findScript(name) local file = hack_path..'scripts/'..name..'.lua' - local env = scripts[key] + if dfhack.filesystem.exists(file) then + return file + end + file = dfhack.getSavePath() + if file then + file = file .. '/raw/scripts/' .. name .. '.lua' + if dfhack.filesystem.exists(file) then + return file + end + end + file = hack_path..'../raw/scripts/' .. name .. '.lua' + if dfhack.filesystem.exists(file) then + return file + end + return nil +end + +function dfhack.run_script(name,...) + local file = findScript(name) + if not file then + error('Could not find script ' .. name) + end + local env = scripts[file] if env == nil then env = {} setmetatable(env, { __index = base_env }) end local f,perr = loadfile(file, 't', env) - if f == nil then - error(perr) + if f then + scripts[file] = env + return f(...) end - scripts[key] = env - return f(...) + error(perr) end function dfhack.run_command(...)