summaryrefslogtreecommitdiff
path: root/library/Core.cpp
diff options
context:
space:
mode:
authorjj2012-06-24 19:52:40 +0200
committerjj2012-06-24 20:24:46 +0200
commit552da8417e35cf5e22312b663aec5e266d4fafa9 (patch)
tree457d17a58da550c9c9e237efad6cb59f0b6edf55 /library/Core.cpp
parent3f4d2e4792850991ae3ab5a11e813488e993b2a4 (diff)
downloaddfhack-552da8417e35cf5e22312b663aec5e266d4fafa9.tar.gz
dfhack-552da8417e35cf5e22312b663aec5e266d4fafa9.tar.bz2
dfhack-552da8417e35cf5e22312b663aec5e266d4fafa9.tar.xz
ruby: handle .rb files in df/hack/scripts/
Diffstat (limited to 'library/Core.cpp')
-rw-r--r--library/Core.cpp50
1 files changed, 38 insertions, 12 deletions
diff --git a/library/Core.cpp b/library/Core.cpp
index df11ca5b..ad9fdc61 100644
--- a/library/Core.cpp
+++ b/library/Core.cpp
@@ -204,7 +204,7 @@ struct sortable
};
};
-static std::string getLuaHelp(std::string path)
+static std::string getScriptHelp(std::string path, std::string helpprefix)
{
ifstream script(path.c_str());
@@ -212,14 +212,14 @@ static std::string getLuaHelp(std::string path)
{
std::string help;
if (getline(script, help) &&
- help.substr(0,3) == "-- ")
- return help.substr(3);
+ help.substr(0,helpprefix.length()) == helpprefix)
+ return help.substr(helpprefix.length());
}
- return "Lua script.";
+ return "No help available.";
}
-static std::map<string,string> listLuaScripts(std::string path)
+static std::map<string,string> listScripts(PluginManager *plug_mgr, std::string path)
{
std::vector<string> files;
getdir(path, files);
@@ -229,10 +229,16 @@ static std::map<string,string> listLuaScripts(std::string path)
{
if (hasEnding(files[i], ".lua"))
{
- std::string help = getLuaHelp(path + files[i]);
+ std::string help = getScriptHelp(path + files[i], "-- ");
pset[files[i].substr(0, files[i].size()-4)] = help;
}
+ else if (plug_mgr->eval_ruby && hasEnding(files[i], ".rb"))
+ {
+ std::string help = getScriptHelp(path + files[i], "# ");
+
+ pset[files[i].substr(0, files[i].size()-3)] = help;
+ }
}
return pset;
}
@@ -275,6 +281,18 @@ static command_result runLuaScript(color_ostream &out, std::string name, vector<
return ok ? CR_OK : CR_FAILURE;
}
+static command_result runRubyScript(PluginManager *plug_mgr, std::string name, vector<string> &args)
+{
+ std::string rbcmd = "$script_args = [";
+ for (size_t i = 0; i < args.size(); i++)
+ rbcmd += "'" + args[i] + "', ";
+ rbcmd += "]\n";
+
+ rbcmd += "load './hack/scripts/" + name + ".rb'";
+
+ return plug_mgr->eval_ruby(rbcmd.c_str());
+}
+
command_result Core::runCommand(color_ostream &out, const std::string &command)
{
if (!command.empty())
@@ -348,10 +366,16 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve
return CR_OK;
}
}
- auto filename = getHackPath() + "scripts/" + parts[0] + ".lua";
- if (fileExists(filename))
+ auto filename = getHackPath() + "scripts/" + parts[0];
+ if (fileExists(filename + ".lua"))
+ {
+ string help = getScriptHelp(filename + ".lua", "-- ");
+ con.print("%s: %s\n", parts[0].c_str(), help.c_str());
+ return CR_OK;
+ }
+ if (plug_mgr->eval_ruby && fileExists(filename + ".rb"))
{
- string help = getLuaHelp(filename);
+ string help = getScriptHelp(filename + ".rb", "# ");
con.print("%s: %s\n", parts[0].c_str(), help.c_str());
return CR_OK;
}
@@ -499,7 +523,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve
con.print(" %-22s- %s\n",(*iter).name.c_str(), (*iter).description.c_str());
con.reset_color();
}
- auto scripts = listLuaScripts(getHackPath() + "scripts/");
+ auto scripts = listScripts(plug_mgr, getHackPath() + "scripts/");
if (!scripts.empty())
{
con.print("\nscripts:\n");
@@ -604,9 +628,11 @@ 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 + ".lua";
- if (fileExists(filename))
+ auto filename = getHackPath() + "scripts/" + first;
+ if (fileExists(filename + ".lua"))
res = runLuaScript(con, first, parts);
+ else if (plug_mgr->eval_ruby && fileExists(filename + ".rb"))
+ res = runRubyScript(plug_mgr, first, parts);
else
con.printerr("%s is not a recognized command.\n", first.c_str());
}