ruby: download lib from github as tgz, switch to ruby18, simply log to stderr.log if cannot load libruby

develop
jj 2012-06-12 18:51:33 +02:00
parent 95606ee3dc
commit 3c1cb24d9f
3 changed files with 39 additions and 25 deletions

@ -1,19 +1,19 @@
OPTION(DL_RUBY "download libruby from the internet" ON)
IF (DL_RUBY)
IF (UNIX)
FILE(DOWNLOAD http://cloud.github.com/downloads/jjyg/dfhack/libruby-1.9.1.so.zip
${dfhack_SOURCE_DIR}/libruby-1.9.1.so.zip
EXPECTED_MD5 f38c4d3effc547ccc60ac4620f40bd14)
execute_process(COMMAND unzip ${dfhack_SOURCE_DIR}/libruby-1.9.1.so.zip
WORKING_DIRECTORY ${dfhack_SOURCE_DIR})
SET(RUBYLIB ${dfhack_SOURCE_DIR}/libruby-1.9.1.so.1.9.1)
FILE(DOWNLOAD http://github.com/downloads/jjyg/dfhack/libruby187.tar.gz ${CMAKE_CURRENT_SOURCE_DIR}/libruby187.tar.gz
EXPECTED_MD5 eb2adea59911f68e6066966c1352f291)
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E tar xzf libruby187.tar.gz
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
FILE(RENAME libruby1.8.so.1.8.7 libruby.so)
SET(RUBYLIB libruby.so)
ELSE (UNIX)
FILE(DOWNLOAD http://cloud.github.com/downloads/jjyg/dfhack/msvcrt-ruby191.zip
${dfhack_SOURCE_DIR}/msvcrt-ruby191.zip
EXPECTED_MD5 ed90e11e150b4b00588b66807a6276e7)
execute_process(COMMAND unzip ${dfhack_SOURCE_DIR}/msvcrt-ruby191.zip
WORKING_DIRECTORY ${dfhack_SOURCE_DIR})
SET(RUBYLIB ${dfhack_SOURCE_DIR}/msvcrt-ruby191.dll)
FILE(DOWNLOAD http://github.com/downloads/jjyg/dfhack/msvcrtruby187.tar.gz ${CMAKE_CURRENT_SOURCE_DIR}/msvcrtruby187.tar.gz
EXPECTED_MD5 9f4a1659ac3a5308f32d3a1937bbeeae)
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E tar xzf msvcrtruby187.tar.gz
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
FILE(RENAME msvcrt-ruby18.dll libruby.dll)
SET(RUBYLIB libruby.dll)
ENDIF(UNIX)
ENDIF(DL_RUBY)
@ -25,9 +25,9 @@ ADD_CUSTOM_COMMAND(
)
ADD_CUSTOM_TARGET(ruby-autogen-rb DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ruby-autogen.rb)
include_directories("${dfhack_SOURCE_DIR}/depends/tthread")
INCLUDE_DIRECTORIES("${dfhack_SOURCE_DIR}/depends/tthread")
DFHACK_PLUGIN(ruby ruby.cpp LINK_LIBRARIES dfhack-tinythread)
ADD_DEPENDENCIES(ruby ruby-autogen-rb)
install(FILES ruby.rb ruby-autogen.rb ${RUBYLIB} DESTINATION ${DFHACK_LIBRARY_DESTINATION})
INSTALL(FILES ruby.rb ruby-autogen.rb ${RUBYLIB} DESTINATION ${DFHACK_LIBRARY_DESTINATION})

@ -11,8 +11,7 @@ access to the raw DF data structures in memory is provided.
Some library methods are stored in the ruby.rb file, with shortcuts to read a
map block, find an unit or an item, etc.
Global objects are stored in the GlobalObjects class ; each object accessor is
mirrored as a DFHack module method (eg df.world).
Global objects are accessible through the 'df' accessor (eg df.world).
The ruby plugin defines 2 dfhack console commands:
rb_load <filename> ; load a ruby script. Ex: rb_load hack/plants.rb (no quotes)
@ -21,6 +20,12 @@ console. Ex: rb_eval df.find_unit.name.first_name
You can use single-quotes for strings ; avoid double-quotes that are parsed
and removed by the dfhack console.
If dfhack reports 'rb_eval is not a recognized command', check stderr.log. You
need a valid 32-bit ruby library to work, and ruby1.8 is prefered (ruby1.9 may
crash DF on startup for now). Install the library in the df root folder (or
hack/ on linux), the library should be named 'libruby.dll' (.so on linux).
You can download a tested version at http://github.com/jjyg/dfhack/downloads/
The plugin also interfaces with dfhack 'onupdate' hook.
To register ruby code to be run every graphic frame, use:
handle = df.onupdate_register { puts 'i love flooding the console' }
@ -33,7 +38,7 @@ The same mechanism is available for onstatechange.
Exemples
--------
For more complex exemples, check the ruby/plugins/ folder.
For more complex exemples, check the ruby/plugins/ source folder.
Show info on the currently selected unit ('v' or 'k' DF menu)
p df.find_unit.flags1

@ -45,10 +45,10 @@ DFHACK_PLUGIN("ruby")
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
{
if (!df_loadruby()) {
Core::printerr("failed to load libruby\n");
return CR_FAILURE;
}
// fail silently instead of spamming the console with 'failed to initialize' if libruby is not present
// the error is still logged in stderr.log
if (!df_loadruby())
return CR_OK;
m_irun = new tthread::mutex();
m_mutex = new tthread::mutex();
@ -79,10 +79,11 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <Plug
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
m_mutex->lock();
if (!r_thread)
return CR_OK;
m_mutex->lock();
r_type = RB_DIE;
r_command = 0;
m_irun->unlock();
@ -135,6 +136,9 @@ static command_result plugin_eval_rb(std::string &command)
DFhackCExport command_result plugin_onupdate ( color_ostream &out )
{
if (!r_thread)
return CR_OK;
if (!onupdate_active)
return CR_OK;
@ -143,6 +147,9 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
DFhackCExport command_result plugin_onstatechange ( color_ostream &out, state_change_event e)
{
if (!r_thread)
return CR_OK;
std::string cmd = "DFHack.onstatechange ";
switch (e) {
#define SCASE(s) case SC_ ## s : cmd += ":" # s ; break
@ -251,14 +258,16 @@ static int df_loadruby(void)
{
const char *libpath =
#ifdef WIN32
"msvcrt-ruby191.dll";
"./libruby.dll";
#else
"./libruby-1.9.1.so.1.9.1";
"hack/libruby.so";
#endif
libruby_handle = OpenPlugin(libpath);
if (!libruby_handle)
if (!libruby_handle) {
fprintf(stderr, "Cannot initialize ruby plugin: failed to load %s\n", libpath);
return 0;
}
if (!(rb_eRuntimeError = (VALUE*)LookupPlugin(libruby_handle, "rb_eRuntimeError")))
return 0;