ruby: use the color_ostream argument from dfhack to output to dfhack-run

develop
jj 2012-07-03 16:10:10 +02:00
parent 0bdff401ca
commit a7a25b80a0
4 changed files with 31 additions and 18 deletions

@ -281,7 +281,7 @@ static command_result runLuaScript(color_ostream &out, std::string name, vector<
return ok ? CR_OK : CR_FAILURE; return ok ? CR_OK : CR_FAILURE;
} }
static command_result runRubyScript(PluginManager *plug_mgr, std::string name, vector<string> &args) static command_result runRubyScript(color_ostream &out, PluginManager *plug_mgr, std::string name, vector<string> &args)
{ {
std::string rbcmd = "$script_args = ["; std::string rbcmd = "$script_args = [";
for (size_t i = 0; i < args.size(); i++) for (size_t i = 0; i < args.size(); i++)
@ -290,7 +290,7 @@ static command_result runRubyScript(PluginManager *plug_mgr, std::string name, v
rbcmd += "load './hack/scripts/" + name + ".rb'"; rbcmd += "load './hack/scripts/" + name + ".rb'";
return plug_mgr->eval_ruby(rbcmd.c_str()); return plug_mgr->eval_ruby(out, rbcmd.c_str());
} }
command_result Core::runCommand(color_ostream &out, const std::string &command) command_result Core::runCommand(color_ostream &out, const std::string &command)
@ -632,7 +632,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve
if (fileExists(filename + ".lua")) if (fileExists(filename + ".lua"))
res = runLuaScript(con, first, parts); res = runLuaScript(con, first, parts);
else if (plug_mgr->eval_ruby && fileExists(filename + ".rb")) else if (plug_mgr->eval_ruby && fileExists(filename + ".rb"))
res = runRubyScript(plug_mgr, first, parts); res = runRubyScript(con, plug_mgr, first, parts);
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());
} }

@ -188,7 +188,7 @@ bool Plugin::load(color_ostream &con)
plugin_shutdown = (command_result (*)(color_ostream &)) LookupPlugin(plug, "plugin_shutdown"); plugin_shutdown = (command_result (*)(color_ostream &)) LookupPlugin(plug, "plugin_shutdown");
plugin_onstatechange = (command_result (*)(color_ostream &, state_change_event)) LookupPlugin(plug, "plugin_onstatechange"); plugin_onstatechange = (command_result (*)(color_ostream &, state_change_event)) LookupPlugin(plug, "plugin_onstatechange");
plugin_rpcconnect = (RPCService* (*)(color_ostream &)) LookupPlugin(plug, "plugin_rpcconnect"); plugin_rpcconnect = (RPCService* (*)(color_ostream &)) LookupPlugin(plug, "plugin_rpcconnect");
plugin_eval_ruby = (command_result (*)(const char*)) LookupPlugin(plug, "plugin_eval_ruby"); plugin_eval_ruby = (command_result (*)(color_ostream &, const char*)) LookupPlugin(plug, "plugin_eval_ruby");
index_lua(plug); index_lua(plug);
this->name = *plug_name; this->name = *plug_name;
plugin_lib = plug; plugin_lib = plug;

@ -209,7 +209,7 @@ namespace DFHack
command_result (*plugin_onupdate)(color_ostream &); command_result (*plugin_onupdate)(color_ostream &);
command_result (*plugin_onstatechange)(color_ostream &, state_change_event); command_result (*plugin_onstatechange)(color_ostream &, state_change_event);
RPCService* (*plugin_rpcconnect)(color_ostream &); RPCService* (*plugin_rpcconnect)(color_ostream &);
command_result (*plugin_eval_ruby)(const char*); command_result (*plugin_eval_ruby)(color_ostream &, const char*);
}; };
class DFHACK_EXPORT PluginManager class DFHACK_EXPORT PluginManager
{ {
@ -238,7 +238,7 @@ namespace DFHack
{ {
return all_plugins.size(); return all_plugins.size();
} }
command_result (*eval_ruby)(const char*); command_result (*eval_ruby)(color_ostream &, const char*);
// DATA // DATA
private: private:
tthread::mutex * cmdlist_mutex; tthread::mutex * cmdlist_mutex;

@ -35,9 +35,12 @@ tthread::mutex *m_irun;
tthread::mutex *m_mutex; tthread::mutex *m_mutex;
static volatile RB_command r_type; static volatile RB_command r_type;
static volatile command_result r_result; static volatile command_result r_result;
static color_ostream *r_console; // color_ostream given as argument, if NULL resort to console_proxy
static const char *r_command; static const char *r_command;
static tthread::thread *r_thread; static tthread::thread *r_thread;
static int onupdate_active; static int onupdate_active;
static color_ostream_proxy *console_proxy;
DFHACK_PLUGIN("ruby") DFHACK_PLUGIN("ruby")
@ -115,7 +118,7 @@ DFhackCExport command_result plugin_shutdown ( color_ostream &out )
} }
// send a single ruby line to be evaluated by the ruby thread // send a single ruby line to be evaluated by the ruby thread
DFhackCExport command_result plugin_eval_ruby(const char *command) DFhackCExport command_result plugin_eval_ruby( color_ostream &out, const char *command)
{ {
// if dlopen failed // if dlopen failed
if (!r_thread) if (!r_thread)
@ -136,6 +139,7 @@ DFhackCExport command_result plugin_eval_ruby(const char *command)
r_type = RB_EVAL; r_type = RB_EVAL;
r_command = command; r_command = command;
r_console = &out;
// wake ruby thread up // wake ruby thread up
m_irun->unlock(); m_irun->unlock();
@ -144,6 +148,7 @@ DFhackCExport command_result plugin_eval_ruby(const char *command)
tthread::this_thread::yield(); tthread::this_thread::yield();
ret = r_result; ret = r_result;
r_console = NULL;
// block ruby thread // block ruby thread
m_irun->lock(); m_irun->lock();
@ -164,7 +169,7 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
if (!onupdate_active) if (!onupdate_active)
return CR_OK; return CR_OK;
return plugin_eval_ruby("DFHack.onupdate"); return plugin_eval_ruby(out, "DFHack.onupdate");
} }
DFhackCExport command_result plugin_onstatechange ( color_ostream &out, state_change_event e) DFhackCExport command_result plugin_onstatechange ( color_ostream &out, state_change_event e)
@ -187,7 +192,7 @@ DFhackCExport command_result plugin_onstatechange ( color_ostream &out, state_ch
#undef SCASE #undef SCASE
} }
return plugin_eval_ruby(cmd.c_str()); return plugin_eval_ruby(out, cmd.c_str());
} }
static command_result df_rubyeval(color_ostream &out, std::vector <std::string> & parameters) static command_result df_rubyeval(color_ostream &out, std::vector <std::string> & parameters)
@ -209,7 +214,7 @@ static command_result df_rubyeval(color_ostream &out, std::vector <std::string>
full += " "; full += " ";
} }
return plugin_eval_ruby(full.c_str()); return plugin_eval_ruby(out, full.c_str());
} }
@ -310,6 +315,13 @@ static void df_unloadruby(void)
} }
} }
static void printerr(const char* fmt, const char *arg)
{
if (r_console)
r_console->printerr(fmt, arg);
else
Core::printerr(fmt, arg);
}
// ruby thread code // ruby thread code
static void dump_rb_error(void) static void dump_rb_error(void)
@ -320,19 +332,17 @@ static void dump_rb_error(void)
s = rb_funcall(err, rb_intern("class"), 0); s = rb_funcall(err, rb_intern("class"), 0);
s = rb_funcall(s, rb_intern("name"), 0); s = rb_funcall(s, rb_intern("name"), 0);
Core::printerr("E: %s: ", rb_string_value_ptr(&s)); printerr("E: %s: ", rb_string_value_ptr(&s));
s = rb_funcall(err, rb_intern("message"), 0); s = rb_funcall(err, rb_intern("message"), 0);
Core::printerr("%s\n", rb_string_value_ptr(&s)); printerr("%s\n", rb_string_value_ptr(&s));
err = rb_funcall(err, rb_intern("backtrace"), 0); err = rb_funcall(err, rb_intern("backtrace"), 0);
for (int i=0 ; i<8 ; ++i) for (int i=0 ; i<8 ; ++i)
if ((s = rb_ary_shift(err)) != Qnil) if ((s = rb_ary_shift(err)) != Qnil)
Core::printerr(" %s\n", rb_string_value_ptr(&s)); printerr(" %s\n", rb_string_value_ptr(&s));
} }
static color_ostream_proxy *console_proxy;
// ruby thread main loop // ruby thread main loop
static void df_rubythread(void *p) static void df_rubythread(void *p)
{ {
@ -428,13 +438,16 @@ static VALUE rb_dfonupdateactiveset(VALUE self, VALUE val)
static VALUE rb_dfprint_str(VALUE self, VALUE s) static VALUE rb_dfprint_str(VALUE self, VALUE s)
{ {
if (r_console)
r_console->print("%s", rb_string_value_ptr(&s));
else
console_proxy->print("%s", rb_string_value_ptr(&s)); console_proxy->print("%s", rb_string_value_ptr(&s));
return Qnil; return Qnil;
} }
static VALUE rb_dfprint_err(VALUE self, VALUE s) static VALUE rb_dfprint_err(VALUE self, VALUE s)
{ {
Core::printerr("%s", rb_string_value_ptr(&s)); printerr("%s", rb_string_value_ptr(&s));
return Qnil; return Qnil;
} }