Allowed per-save script folders.

develop
expwnent 2014-07-07 08:50:40 -04:00
parent e04ab11659
commit 3c06f3bada
2 changed files with 70 additions and 23 deletions

@ -393,6 +393,26 @@ static bool try_autocomplete(color_ostream &con, const std::string &first, std::
return false; 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<string> &parts) command_result Core::runCommand(color_ostream &con, const std::string &first, vector<string> &parts)
{ {
if (!first.empty()) if (!first.empty())
@ -446,18 +466,20 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve
return CR_OK; return CR_OK;
} }
} }
auto filename = getHackPath() + "scripts/" + parts[0]; string path = this->p->getPath();
if (fileExists(filename + ".lua")) string file = findScript(path, parts[0] + ".lua");
{ if ( file != "" ) {
string help = getScriptHelp(filename + ".lua", "-- "); string help = getScriptHelp(file, "-- ");
con.print("%s: %s\n", parts[0].c_str(), help.c_str()); con.print("%s: %s\n", parts[0].c_str(), help.c_str());
return CR_OK; return CR_OK;
} }
if (plug_mgr->ruby && plug_mgr->ruby->is_enabled() && fileExists(filename + ".rb")) if (plug_mgr->ruby && plug_mgr->ruby->is_enabled() ) {
{ file = findScript(path, parts[0] + ".rb");
string help = getScriptHelp(filename + ".rb", "# "); if ( file != "" ) {
con.print("%s: %s\n", parts[0].c_str(), help.c_str()); string help = getScriptHelp(file, "# ");
return CR_OK; 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()); 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); command_result res = plug_mgr->InvokeCommand(con, first, parts);
if(res == CR_NOT_IMPLEMENTED) if(res == CR_NOT_IMPLEMENTED)
{ {
auto filename = getHackPath() + "scripts/" + first; string completed;
std::string completed; string path = this->p->getPath();
string filename = findScript(path, first + ".lua");
if (fileExists(filename + ".lua")) bool lua = filename != "";
if ( !lua ) {
filename = findScript(path, first + ".rb");
}
if ( lua )
res = runLuaScript(con, first, parts); 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); res = runRubyScript(con, plug_mgr, first, parts);
else if (try_autocomplete(con, first, completed)) else if ( try_autocomplete(con, first, completed) )
return CR_NOT_IMPLEMENTED;// runCommand(con, completed, parts); return CR_NOT_IMPLEMENTED;
else else
con.printerr("%s is not a recognized command.\n", first.c_str()); con.printerr("%s is not a recognized command.\n", first.c_str());
} }

@ -371,20 +371,41 @@ internal.scripts = internal.scripts or {}
local scripts = internal.scripts local scripts = internal.scripts
local hack_path = dfhack.getHackPath() local hack_path = dfhack.getHackPath()
function dfhack.run_script(name,...) local function findScript(name)
local key = string.lower(name)
local file = hack_path..'scripts/'..name..'.lua' 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 if env == nil then
env = {} env = {}
setmetatable(env, { __index = base_env }) setmetatable(env, { __index = base_env })
end end
local f,perr = loadfile(file, 't', env) local f,perr = loadfile(file, 't', env)
if f == nil then if f then
error(perr) scripts[file] = env
return f(...)
end end
scripts[key] = env error(perr)
return f(...)
end end
function dfhack.run_command(...) function dfhack.run_command(...)