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;
}
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)
{
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());
}

@ -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(...)