2012-03-21 11:49:50 -06:00
|
|
|
// blindly copied imports from fastdwarf
|
|
|
|
#include "Core.h"
|
|
|
|
#include "Console.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
2012-04-13 08:17:56 -06:00
|
|
|
#include "VersionInfo.h"
|
2012-08-23 10:44:03 -06:00
|
|
|
#include "MemAccess.h"
|
2012-03-21 11:49:50 -06:00
|
|
|
#include "DataDefs.h"
|
2017-06-06 21:34:34 -06:00
|
|
|
|
|
|
|
#include "modules/Gui.h"
|
2012-07-05 07:35:37 -06:00
|
|
|
#include "df/global_objects.h"
|
2017-06-06 21:34:34 -06:00
|
|
|
#include "df/unit.h"
|
2012-03-21 11:49:50 -06:00
|
|
|
|
|
|
|
#include "tinythread.h"
|
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
|
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
|
|
|
|
// DFHack stuff
|
|
|
|
|
|
|
|
|
2012-06-02 15:44:52 -06:00
|
|
|
static int df_loadruby(void);
|
|
|
|
static void df_unloadruby(void);
|
2012-03-21 11:49:50 -06:00
|
|
|
static void df_rubythread(void*);
|
2012-04-12 11:12:46 -06:00
|
|
|
static command_result df_rubyeval(color_ostream &out, std::vector <std::string> & parameters);
|
2012-03-21 11:49:50 -06:00
|
|
|
static void ruby_bind_dfhack(void);
|
|
|
|
|
|
|
|
// inter-thread communication stuff
|
|
|
|
enum RB_command {
|
|
|
|
RB_IDLE,
|
|
|
|
RB_INIT,
|
|
|
|
RB_DIE,
|
|
|
|
RB_EVAL,
|
|
|
|
};
|
|
|
|
tthread::mutex *m_irun;
|
|
|
|
tthread::mutex *m_mutex;
|
2012-06-24 17:45:50 -06:00
|
|
|
static volatile RB_command r_type;
|
|
|
|
static volatile command_result r_result;
|
2012-07-03 08:10:10 -06:00
|
|
|
static color_ostream *r_console; // color_ostream given as argument, if NULL resort to console_proxy
|
2012-03-21 11:49:50 -06:00
|
|
|
static const char *r_command;
|
|
|
|
static tthread::thread *r_thread;
|
|
|
|
static int onupdate_active;
|
2012-12-11 09:25:51 -07:00
|
|
|
static int onupdate_minyear, onupdate_minyeartick=-1, onupdate_minyeartickadv=-1;
|
2012-07-03 08:10:10 -06:00
|
|
|
static color_ostream_proxy *console_proxy;
|
2013-04-12 06:47:56 -06:00
|
|
|
static std::vector<std::string> *dfhack_run_queue;
|
2012-07-03 08:10:10 -06:00
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
|
|
|
|
DFHACK_PLUGIN("ruby")
|
|
|
|
|
2013-10-07 05:58:28 -06:00
|
|
|
|
|
|
|
DFhackDataExport bool plugin_is_enabled = true;
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable)
|
|
|
|
{
|
|
|
|
plugin_is_enabled = enable;
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
|
|
|
|
{
|
2012-06-24 17:45:50 -06:00
|
|
|
onupdate_active = 0;
|
|
|
|
|
|
|
|
// fail silently instead of spamming the console with 'failed to initialize'
|
|
|
|
// if libruby is not present, the error is still logged in stderr.log
|
2012-06-12 10:51:33 -06:00
|
|
|
if (!df_loadruby())
|
|
|
|
return CR_OK;
|
2012-06-02 15:44:52 -06:00
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// the ruby thread sleeps trying to lock this
|
|
|
|
// when it gets it, it runs according to r_type
|
|
|
|
// when finished, it sets r_type to IDLE and unlocks
|
2012-03-21 11:49:50 -06:00
|
|
|
m_irun = new tthread::mutex();
|
2012-06-24 17:45:50 -06:00
|
|
|
|
|
|
|
// when any thread is going to request something to the ruby thread,
|
|
|
|
// lock this before anything, and release when everything is done
|
2012-03-21 11:49:50 -06:00
|
|
|
m_mutex = new tthread::mutex();
|
2012-06-24 17:45:50 -06:00
|
|
|
|
2013-04-12 06:47:56 -06:00
|
|
|
// list of dfhack commands to run when the current ruby run is done (once locks are released)
|
|
|
|
dfhack_run_queue = new std::vector<std::string>;
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
r_type = RB_INIT;
|
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// create the dedicated ruby thread
|
|
|
|
// df_rubythread starts the ruby interpreter and goes to type=IDLE when done
|
2012-03-21 11:49:50 -06:00
|
|
|
r_thread = new tthread::thread(df_rubythread, 0);
|
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// wait until init phase 1 is done
|
2012-03-21 11:49:50 -06:00
|
|
|
while (r_type != RB_IDLE)
|
|
|
|
tthread::this_thread::yield();
|
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// ensure the ruby thread sleeps until we have a command to handle
|
2012-03-21 11:49:50 -06:00
|
|
|
m_irun->lock();
|
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// check return value from rbinit
|
2012-03-21 11:49:50 -06:00
|
|
|
if (r_result == CR_FAILURE)
|
|
|
|
return CR_FAILURE;
|
|
|
|
|
|
|
|
commands.push_back(PluginCommand("rb_eval",
|
|
|
|
"Ruby interpreter. Eval() a ruby string.",
|
|
|
|
df_rubyeval));
|
|
|
|
|
2013-04-16 13:54:03 -06:00
|
|
|
commands.push_back(PluginCommand("rb",
|
2015-11-09 20:37:45 -07:00
|
|
|
"Ruby interpreter. Eval() a ruby string.",
|
2013-04-16 13:54:03 -06:00
|
|
|
df_rubyeval));
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
|
|
|
{
|
2012-06-24 17:45:50 -06:00
|
|
|
// if dlopen failed
|
2012-03-21 11:49:50 -06:00
|
|
|
if (!r_thread)
|
|
|
|
return CR_OK;
|
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// ensure ruby thread is idle
|
2012-06-12 10:51:33 -06:00
|
|
|
m_mutex->lock();
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
r_type = RB_DIE;
|
2012-06-24 17:45:50 -06:00
|
|
|
r_command = NULL;
|
|
|
|
// start ruby thread
|
2012-03-21 11:49:50 -06:00
|
|
|
m_irun->unlock();
|
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// wait until ruby thread ends after RB_DIE
|
2012-03-21 11:49:50 -06:00
|
|
|
r_thread->join();
|
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// cleanup everything
|
2012-03-21 11:49:50 -06:00
|
|
|
delete r_thread;
|
|
|
|
r_thread = 0;
|
|
|
|
delete m_irun;
|
2012-06-24 17:45:50 -06:00
|
|
|
// we can release m_mutex, other users will check r_thread
|
2012-03-21 11:49:50 -06:00
|
|
|
m_mutex->unlock();
|
|
|
|
delete m_mutex;
|
2013-04-12 06:47:56 -06:00
|
|
|
delete dfhack_run_queue;
|
2012-03-21 11:49:50 -06:00
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// dlclose libruby
|
2012-06-02 15:44:52 -06:00
|
|
|
df_unloadruby();
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-12 03:12:31 -06:00
|
|
|
static command_result do_plugin_eval_ruby(color_ostream &out, const char *command)
|
2012-03-21 11:49:50 -06:00
|
|
|
{
|
|
|
|
command_result ret;
|
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// ensure ruby thread is idle
|
2012-03-21 11:49:50 -06:00
|
|
|
m_mutex->lock();
|
|
|
|
if (!r_thread)
|
2012-06-24 17:45:50 -06:00
|
|
|
// raced with plugin_shutdown
|
2012-03-21 11:49:50 -06:00
|
|
|
return CR_OK;
|
|
|
|
|
|
|
|
r_type = RB_EVAL;
|
2012-04-25 12:21:09 -06:00
|
|
|
r_command = command;
|
2012-07-03 08:10:10 -06:00
|
|
|
r_console = &out;
|
2012-06-24 17:45:50 -06:00
|
|
|
// wake ruby thread up
|
2012-03-21 11:49:50 -06:00
|
|
|
m_irun->unlock();
|
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// semi-active loop until ruby thread is done
|
2012-03-21 11:49:50 -06:00
|
|
|
while (r_type != RB_IDLE)
|
|
|
|
tthread::this_thread::yield();
|
|
|
|
|
|
|
|
ret = r_result;
|
2012-07-03 08:10:10 -06:00
|
|
|
r_console = NULL;
|
2012-03-21 11:49:50 -06:00
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// block ruby thread
|
2012-03-21 11:49:50 -06:00
|
|
|
m_irun->lock();
|
2012-06-24 17:45:50 -06:00
|
|
|
// let other plugin_eval_ruby run
|
2012-03-21 11:49:50 -06:00
|
|
|
m_mutex->unlock();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-12 03:12:31 -06:00
|
|
|
// send a single ruby line to be evaluated by the ruby thread
|
|
|
|
DFhackCExport command_result plugin_eval_ruby( color_ostream &out, const char *command)
|
|
|
|
{
|
2013-04-12 06:47:56 -06:00
|
|
|
command_result ret;
|
|
|
|
|
2012-10-12 03:12:31 -06:00
|
|
|
// if dlopen failed
|
|
|
|
if (!r_thread)
|
2015-02-21 19:50:18 -07:00
|
|
|
{
|
|
|
|
out.printerr("Failed to load ruby library.\n");
|
2012-10-12 03:12:31 -06:00
|
|
|
return CR_FAILURE;
|
2015-02-21 19:50:18 -07:00
|
|
|
}
|
2012-10-12 03:12:31 -06:00
|
|
|
|
|
|
|
if (!strncmp(command, "nolock ", 7)) {
|
|
|
|
// debug only!
|
|
|
|
// run ruby commands without locking the main thread
|
|
|
|
// useful when the game is frozen after a segfault
|
2013-04-12 06:47:56 -06:00
|
|
|
ret = do_plugin_eval_ruby(out, command+7);
|
2012-10-12 03:12:31 -06:00
|
|
|
} else {
|
|
|
|
// wrap all ruby code inside a suspend block
|
|
|
|
// if we dont do that and rely on ruby code doing it, we'll deadlock in
|
|
|
|
// onupdate
|
|
|
|
CoreSuspender suspend;
|
2013-04-12 06:47:56 -06:00
|
|
|
ret = do_plugin_eval_ruby(out, command);
|
2012-10-12 03:12:31 -06:00
|
|
|
}
|
2013-04-12 06:47:56 -06:00
|
|
|
|
|
|
|
// if any dfhack command is queued for run, do it now
|
|
|
|
while (!dfhack_run_queue->empty()) {
|
|
|
|
std::string cmd = dfhack_run_queue->at(0);
|
|
|
|
// delete before running the command, which may be ruby and cause infinite loops
|
|
|
|
dfhack_run_queue->erase(dfhack_run_queue->begin());
|
|
|
|
Core::getInstance().runCommand(out, cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2012-10-12 03:12:31 -06:00
|
|
|
}
|
|
|
|
|
2012-04-25 12:21:09 -06:00
|
|
|
DFhackCExport command_result plugin_onupdate ( color_ostream &out )
|
|
|
|
{
|
2012-06-12 10:51:33 -06:00
|
|
|
if (!r_thread)
|
|
|
|
return CR_OK;
|
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// ruby sets this flag when needed, to avoid lag running ruby code every
|
|
|
|
// frame if not necessary
|
2012-04-25 12:21:09 -06:00
|
|
|
if (!onupdate_active)
|
|
|
|
return CR_OK;
|
2012-03-21 11:49:50 -06:00
|
|
|
|
2016-10-28 08:40:14 -06:00
|
|
|
if (df::global::cur_year && *df::global::cur_year < onupdate_minyear)
|
2012-07-05 07:35:37 -06:00
|
|
|
return CR_OK;
|
2016-10-28 08:40:14 -06:00
|
|
|
if (df::global::cur_year_tick && onupdate_minyeartick >= 0 &&
|
|
|
|
*df::global::cur_year_tick < onupdate_minyeartick)
|
2012-12-11 09:25:51 -07:00
|
|
|
return CR_OK;
|
2016-10-28 08:40:14 -06:00
|
|
|
if (df::global::cur_year_tick_advmode && onupdate_minyeartickadv >= 0 &&
|
|
|
|
*df::global::cur_year_tick_advmode < onupdate_minyeartickadv)
|
2012-07-05 07:35:37 -06:00
|
|
|
return CR_OK;
|
|
|
|
|
2012-07-03 08:10:10 -06:00
|
|
|
return plugin_eval_ruby(out, "DFHack.onupdate");
|
2012-04-25 12:21:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_onstatechange ( color_ostream &out, state_change_event e)
|
|
|
|
{
|
2012-06-12 10:51:33 -06:00
|
|
|
if (!r_thread)
|
|
|
|
return CR_OK;
|
|
|
|
|
2012-04-25 12:21:09 -06:00
|
|
|
std::string cmd = "DFHack.onstatechange ";
|
|
|
|
switch (e) {
|
|
|
|
#define SCASE(s) case SC_ ## s : cmd += ":" # s ; break
|
|
|
|
SCASE(WORLD_LOADED);
|
|
|
|
SCASE(WORLD_UNLOADED);
|
|
|
|
SCASE(MAP_LOADED);
|
|
|
|
SCASE(MAP_UNLOADED);
|
|
|
|
SCASE(VIEWSCREEN_CHANGED);
|
|
|
|
SCASE(CORE_INITIALIZED);
|
2012-06-27 06:52:34 -06:00
|
|
|
// if we go through plugin_eval at BEGIN_UNLOAD, it'll
|
|
|
|
// try to get the suspend lock and deadlock at df exit
|
|
|
|
case SC_BEGIN_UNLOAD : return CR_OK;
|
2012-07-05 10:03:02 -06:00
|
|
|
SCASE(PAUSED);
|
|
|
|
SCASE(UNPAUSED);
|
2012-06-09 17:45:30 -06:00
|
|
|
#undef SCASE
|
2012-04-25 12:21:09 -06:00
|
|
|
}
|
|
|
|
|
2012-07-03 08:10:10 -06:00
|
|
|
return plugin_eval_ruby(out, cmd.c_str());
|
2012-04-25 12:21:09 -06:00
|
|
|
}
|
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
static command_result df_rubyeval(color_ostream &out, std::vector <std::string> & parameters)
|
2012-03-21 11:49:50 -06:00
|
|
|
{
|
|
|
|
if (parameters.size() == 1 && (parameters[0] == "help" || parameters[0] == "?"))
|
|
|
|
{
|
|
|
|
out.print("This command executes an arbitrary ruby statement.\n");
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// reconstruct the text from dfhack console line
|
2012-03-21 11:49:50 -06:00
|
|
|
std::string full = "";
|
|
|
|
|
|
|
|
for (unsigned i=0 ; i<parameters.size() ; ++i) {
|
|
|
|
full += parameters[i];
|
2012-04-25 12:21:09 -06:00
|
|
|
if (i != parameters.size()-1)
|
|
|
|
full += " ";
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
|
|
|
|
2012-07-03 08:10:10 -06:00
|
|
|
return plugin_eval_ruby(out, full.c_str());
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
// ruby stuff
|
|
|
|
|
2012-06-24 08:09:31 -06:00
|
|
|
// - ruby-dev on windows is messy
|
|
|
|
// - ruby.h with gcc -m32 on linux 64 is broken
|
|
|
|
// so we dynamically load libruby with dlopen/LoadLibrary
|
|
|
|
// lib path is hardcoded here, and by default downloaded by cmake
|
2012-06-02 15:44:52 -06:00
|
|
|
|
2016-10-21 06:52:26 -06:00
|
|
|
typedef uintptr_t VALUE;
|
|
|
|
typedef uintptr_t ID;
|
2012-06-02 15:44:52 -06:00
|
|
|
|
2016-10-28 08:40:14 -06:00
|
|
|
static VALUE Qfalse = 0;
|
|
|
|
static VALUE Qtrue = 2;
|
|
|
|
static VALUE Qnil = 4;
|
2012-06-02 15:44:52 -06:00
|
|
|
|
2016-10-21 06:52:26 -06:00
|
|
|
#define INT2FIX(i) ((VALUE)((((intptr_t)i) << 1) | 1))
|
|
|
|
#define FIX2INT(i) (((intptr_t)i) >> 1)
|
2012-06-02 15:44:52 -06:00
|
|
|
#define RUBY_METHOD_FUNC(func) ((VALUE(*)(...))func)
|
|
|
|
|
2014-09-19 13:38:22 -06:00
|
|
|
void (*ruby_init_stack)(VALUE*);
|
2012-06-02 15:44:52 -06:00
|
|
|
void (*ruby_sysinit)(int *, const char ***);
|
|
|
|
void (*ruby_init)(void);
|
|
|
|
void (*ruby_init_loadpath)(void);
|
|
|
|
void (*ruby_script)(const char*);
|
2017-05-09 11:49:07 -06:00
|
|
|
int (*ruby_cleanup)(int);
|
2012-06-02 15:44:52 -06:00
|
|
|
ID (*rb_intern)(const char*);
|
|
|
|
VALUE (*rb_funcall)(VALUE, ID, int, ...);
|
|
|
|
VALUE (*rb_define_module)(const char*);
|
|
|
|
void (*rb_define_singleton_method)(VALUE, const char*, VALUE(*)(...), int);
|
|
|
|
VALUE (*rb_gv_get)(const char*);
|
|
|
|
VALUE (*rb_str_new)(const char*, long);
|
|
|
|
char* (*rb_string_value_ptr)(VALUE*);
|
|
|
|
VALUE (*rb_eval_string_protect)(const char*, int*);
|
|
|
|
VALUE (*rb_ary_shift)(VALUE);
|
|
|
|
VALUE (*rb_float_new)(double);
|
|
|
|
double (*rb_num2dbl)(VALUE);
|
2016-10-30 23:58:40 -06:00
|
|
|
VALUE (*rb_int2inum)(intptr_t); // XXX check on win64 long vs intptr_t
|
2016-10-21 06:52:26 -06:00
|
|
|
VALUE (*rb_uint2inum)(uintptr_t);
|
|
|
|
uintptr_t (*rb_num2ulong)(VALUE);
|
2012-06-02 15:44:52 -06:00
|
|
|
// end of rip(ruby.h)
|
|
|
|
|
|
|
|
DFHack::DFLibrary *libruby_handle;
|
|
|
|
|
|
|
|
// load the ruby library, initialize function pointers
|
|
|
|
static int df_loadruby(void)
|
|
|
|
{
|
2016-11-17 08:31:48 -07:00
|
|
|
const char *libpaths[] = {
|
2012-06-29 09:51:26 -06:00
|
|
|
#if defined(WIN32)
|
2016-11-17 08:31:48 -07:00
|
|
|
"./libruby.dll",
|
2012-06-29 09:51:26 -06:00
|
|
|
#elif defined(__APPLE__)
|
2016-11-17 08:31:48 -07:00
|
|
|
"hack/libruby.dylib",
|
|
|
|
"/System/Library/Frameworks/Ruby.framework/Ruby",
|
2012-06-02 15:44:52 -06:00
|
|
|
#else
|
2016-11-17 08:31:48 -07:00
|
|
|
"hack/libruby.so",
|
|
|
|
"libruby.so",
|
2012-06-02 15:44:52 -06:00
|
|
|
#endif
|
2016-11-17 08:31:48 -07:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const char **path = libpaths; *path; path++) {
|
|
|
|
if ((libruby_handle = OpenPlugin(*path)))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
fprintf(stderr, "ruby: warning: Failed to load %s\n", *path);
|
|
|
|
}
|
2012-06-02 15:44:52 -06:00
|
|
|
|
2012-06-12 10:51:33 -06:00
|
|
|
if (!libruby_handle) {
|
2016-11-17 08:31:48 -07:00
|
|
|
Core::printerr("Cannot initialize ruby plugin: failed to load ruby library\n");
|
2012-06-02 15:44:52 -06:00
|
|
|
return 0;
|
2012-06-12 10:51:33 -06:00
|
|
|
}
|
2012-06-02 15:44:52 -06:00
|
|
|
|
|
|
|
// ruby_sysinit is optional (ruby1.9 only)
|
|
|
|
ruby_sysinit = (decltype(ruby_sysinit))LookupPlugin(libruby_handle, "ruby_sysinit");
|
2016-12-13 22:55:20 -07:00
|
|
|
#define rbloadsyma(s,a) if (!(s = (decltype(s))LookupPlugin(libruby_handle, #a))) return 0
|
|
|
|
#define rbloadsym(s) rbloadsyma(s,s)
|
2014-09-19 13:38:22 -06:00
|
|
|
rbloadsym(ruby_init_stack);
|
2012-06-02 15:44:52 -06:00
|
|
|
rbloadsym(ruby_init);
|
|
|
|
rbloadsym(ruby_init_loadpath);
|
|
|
|
rbloadsym(ruby_script);
|
2017-05-09 11:49:07 -06:00
|
|
|
rbloadsym(ruby_cleanup);
|
2012-06-02 15:44:52 -06:00
|
|
|
rbloadsym(rb_intern);
|
|
|
|
rbloadsym(rb_funcall);
|
|
|
|
rbloadsym(rb_define_module);
|
|
|
|
rbloadsym(rb_define_singleton_method);
|
|
|
|
rbloadsym(rb_gv_get);
|
|
|
|
rbloadsym(rb_str_new);
|
|
|
|
rbloadsym(rb_string_value_ptr);
|
|
|
|
rbloadsym(rb_eval_string_protect);
|
|
|
|
rbloadsym(rb_ary_shift);
|
|
|
|
rbloadsym(rb_num2dbl);
|
|
|
|
rbloadsym(rb_int2inum);
|
2016-12-13 22:55:20 -07:00
|
|
|
#if defined(_WIN64)
|
|
|
|
rbloadsyma(rb_uint2inum, rb_ull2inum);
|
|
|
|
rbloadsyma(rb_num2ulong, rb_num2ull);
|
|
|
|
#else
|
2012-06-02 15:44:52 -06:00
|
|
|
rbloadsym(rb_uint2inum);
|
|
|
|
rbloadsym(rb_num2ulong);
|
2016-12-13 22:55:20 -07:00
|
|
|
#endif
|
|
|
|
|
2012-06-02 15:44:52 -06:00
|
|
|
#undef rbloadsym
|
2016-10-26 18:40:47 -06:00
|
|
|
// rb_float_new_in_heap in ruby 2
|
2016-10-08 14:59:03 -06:00
|
|
|
if (!((rb_float_new = (decltype(rb_float_new))(LookupPlugin(libruby_handle, "rb_float_new"))) ||
|
|
|
|
(rb_float_new = (decltype(rb_float_new))(LookupPlugin(libruby_handle, "rb_float_new_in_heap")))))
|
|
|
|
return 0;
|
2012-06-02 15:44:52 -06:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void df_unloadruby(void)
|
|
|
|
{
|
|
|
|
if (libruby_handle) {
|
|
|
|
ClosePlugin(libruby_handle);
|
|
|
|
libruby_handle = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 08:10:10 -06:00
|
|
|
static void printerr(const char* fmt, const char *arg)
|
|
|
|
{
|
|
|
|
if (r_console)
|
|
|
|
r_console->printerr(fmt, arg);
|
|
|
|
else
|
|
|
|
Core::printerr(fmt, arg);
|
|
|
|
}
|
2012-06-02 15:44:52 -06:00
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
// ruby thread code
|
|
|
|
static void dump_rb_error(void)
|
|
|
|
{
|
|
|
|
VALUE s, err;
|
|
|
|
|
|
|
|
err = rb_gv_get("$!");
|
|
|
|
|
|
|
|
s = rb_funcall(err, rb_intern("class"), 0);
|
|
|
|
s = rb_funcall(s, rb_intern("name"), 0);
|
2012-07-03 08:10:10 -06:00
|
|
|
printerr("E: %s: ", rb_string_value_ptr(&s));
|
2012-03-21 11:49:50 -06:00
|
|
|
|
|
|
|
s = rb_funcall(err, rb_intern("message"), 0);
|
2012-07-03 08:10:10 -06:00
|
|
|
printerr("%s\n", rb_string_value_ptr(&s));
|
2012-03-21 11:49:50 -06:00
|
|
|
|
|
|
|
err = rb_funcall(err, rb_intern("backtrace"), 0);
|
|
|
|
for (int i=0 ; i<8 ; ++i)
|
|
|
|
if ((s = rb_ary_shift(err)) != Qnil)
|
2012-07-03 08:10:10 -06:00
|
|
|
printerr(" %s\n", rb_string_value_ptr(&s));
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ruby thread main loop
|
|
|
|
static void df_rubythread(void *p)
|
|
|
|
{
|
|
|
|
int state, running;
|
|
|
|
|
2014-09-19 13:38:22 -06:00
|
|
|
// may need to be run from df main thread?
|
|
|
|
VALUE foo;
|
|
|
|
ruby_init_stack(&foo);
|
|
|
|
|
2012-06-09 17:45:30 -06:00
|
|
|
if (ruby_sysinit) {
|
|
|
|
// ruby1.9 specific API
|
|
|
|
static int argc;
|
|
|
|
static const char *argv[] = { "dfhack", 0 };
|
|
|
|
ruby_sysinit(&argc, (const char ***)&argv);
|
|
|
|
}
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
// initialize the ruby interpreter
|
|
|
|
ruby_init();
|
|
|
|
ruby_init_loadpath();
|
|
|
|
// default value for the $0 "current script name"
|
|
|
|
ruby_script("dfhack");
|
|
|
|
|
|
|
|
// create the ruby objects to map DFHack to ruby methods
|
|
|
|
ruby_bind_dfhack();
|
|
|
|
|
2012-04-25 12:21:09 -06:00
|
|
|
console_proxy = new color_ostream_proxy(Core::getInstance().getConsole());
|
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// ensure noone bothers us while we load data defs in the background
|
|
|
|
m_mutex->lock();
|
|
|
|
|
|
|
|
// tell the main thread our initialization is finished
|
2012-03-21 11:49:50 -06:00
|
|
|
r_result = CR_OK;
|
|
|
|
r_type = RB_IDLE;
|
|
|
|
|
2016-10-28 08:40:14 -06:00
|
|
|
// initialize ruby constants (may depend on libruby compilation flags/version)
|
|
|
|
Qnil = rb_eval_string_protect("nil", &state);
|
|
|
|
Qtrue = rb_eval_string_protect("true", &state);
|
|
|
|
Qfalse = rb_eval_string_protect("false", &state);
|
|
|
|
|
2012-06-24 17:45:50 -06:00
|
|
|
// load the default ruby-level definitions in the background
|
|
|
|
state=0;
|
|
|
|
rb_eval_string_protect("require './hack/ruby/ruby'", &state);
|
|
|
|
if (state)
|
|
|
|
dump_rb_error();
|
|
|
|
|
|
|
|
// ready to go
|
|
|
|
m_mutex->unlock();
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
running = 1;
|
|
|
|
while (running) {
|
2012-06-24 17:45:50 -06:00
|
|
|
// sleep waiting for new command
|
2012-03-21 11:49:50 -06:00
|
|
|
m_irun->lock();
|
|
|
|
|
|
|
|
switch (r_type) {
|
|
|
|
case RB_IDLE:
|
|
|
|
case RB_INIT:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RB_DIE:
|
|
|
|
running = 0;
|
2017-05-09 11:49:07 -06:00
|
|
|
ruby_cleanup(0);
|
2012-03-21 11:49:50 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RB_EVAL:
|
|
|
|
state = 0;
|
|
|
|
rb_eval_string_protect(r_command, &state);
|
|
|
|
if (state)
|
|
|
|
dump_rb_error();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_result = CR_OK;
|
|
|
|
r_type = RB_IDLE;
|
|
|
|
m_irun->unlock();
|
2012-04-17 14:42:23 -06:00
|
|
|
tthread::this_thread::yield();
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-25 09:18:24 -06:00
|
|
|
#define BOOL_ISFALSE(v) ((v) == Qfalse || (v) == Qnil || (v) == INT2FIX(0))
|
2012-03-21 11:49:50 -06:00
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
// main DFHack ruby module
|
2012-03-21 11:49:50 -06:00
|
|
|
static VALUE rb_cDFHack;
|
|
|
|
|
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
// DFHack module ruby methods, binds specific dfhack methods
|
|
|
|
|
2012-11-28 05:33:07 -07:00
|
|
|
// df-dfhack version (eg "0.34.11-r2")
|
|
|
|
static VALUE rb_dfversion(VALUE self)
|
|
|
|
{
|
2015-04-02 14:37:58 -06:00
|
|
|
const char *dfhack_version = Version::dfhack_version();
|
|
|
|
return rb_str_new(dfhack_version, strlen(dfhack_version));
|
2012-11-28 05:33:07 -07:00
|
|
|
}
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
// enable/disable calls to DFHack.onupdate()
|
2012-07-05 07:35:37 -06:00
|
|
|
static VALUE rb_dfonupdate_active(VALUE self)
|
2012-03-21 11:49:50 -06:00
|
|
|
{
|
|
|
|
if (onupdate_active)
|
|
|
|
return Qtrue;
|
|
|
|
else
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2012-07-05 07:35:37 -06:00
|
|
|
static VALUE rb_dfonupdate_active_set(VALUE self, VALUE val)
|
2012-03-21 11:49:50 -06:00
|
|
|
{
|
2012-04-20 09:57:41 -06:00
|
|
|
onupdate_active = (BOOL_ISFALSE(val) ? 0 : 1);
|
2012-03-21 11:49:50 -06:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2012-07-05 07:35:37 -06:00
|
|
|
static VALUE rb_dfonupdate_minyear(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_uint2inum(onupdate_minyear);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_dfonupdate_minyear_set(VALUE self, VALUE val)
|
|
|
|
{
|
|
|
|
onupdate_minyear = rb_num2ulong(val);
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_dfonupdate_minyeartick(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_uint2inum(onupdate_minyeartick);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_dfonupdate_minyeartick_set(VALUE self, VALUE val)
|
|
|
|
{
|
|
|
|
onupdate_minyeartick = rb_num2ulong(val);
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2012-12-11 09:25:51 -07:00
|
|
|
static VALUE rb_dfonupdate_minyeartickadv(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_uint2inum(onupdate_minyeartickadv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_dfonupdate_minyeartickadv_set(VALUE self, VALUE val)
|
|
|
|
{
|
|
|
|
onupdate_minyeartickadv = rb_num2ulong(val);
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
static VALUE rb_dfprint_str(VALUE self, VALUE s)
|
|
|
|
{
|
2012-07-03 08:10:10 -06:00
|
|
|
if (r_console)
|
|
|
|
r_console->print("%s", rb_string_value_ptr(&s));
|
|
|
|
else
|
|
|
|
console_proxy->print("%s", rb_string_value_ptr(&s));
|
2012-03-21 11:49:50 -06:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2014-09-25 07:53:03 -06:00
|
|
|
static VALUE rb_dfprint_color(VALUE self, VALUE c, VALUE s)
|
|
|
|
{
|
|
|
|
if (r_console) {
|
|
|
|
color_value old_col = r_console->color();
|
|
|
|
r_console->color(color_value(rb_num2ulong(c)));
|
|
|
|
r_console->print("%s", rb_string_value_ptr(&s));
|
|
|
|
r_console->color(old_col);
|
|
|
|
} else
|
|
|
|
console_proxy->print("%s", rb_string_value_ptr(&s));
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
static VALUE rb_dfprint_err(VALUE self, VALUE s)
|
|
|
|
{
|
2012-07-03 08:10:10 -06:00
|
|
|
printerr("%s", rb_string_value_ptr(&s));
|
2012-03-21 11:49:50 -06:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2012-04-13 08:17:56 -06:00
|
|
|
static VALUE rb_dfget_global_address(VALUE self, VALUE name)
|
|
|
|
{
|
2012-04-19 17:29:52 -06:00
|
|
|
return rb_uint2inum(Core::getInstance().vinfo->getAddress(rb_string_value_ptr(&name)));
|
2012-04-13 08:17:56 -06:00
|
|
|
}
|
|
|
|
|
2012-04-17 14:42:23 -06:00
|
|
|
static VALUE rb_dfget_vtable(VALUE self, VALUE name)
|
|
|
|
{
|
2016-08-10 13:24:07 -06:00
|
|
|
return rb_uint2inum((uintptr_t)Core::getInstance().vinfo->getVTable(rb_string_value_ptr(&name)));
|
2012-04-17 14:42:23 -06:00
|
|
|
}
|
|
|
|
|
2012-04-18 10:46:33 -06:00
|
|
|
// read the c++ class name from a vtable pointer, inspired from doReadClassName
|
|
|
|
// XXX virtual classes only! dark pointer arithmetic, use with caution !
|
|
|
|
static VALUE rb_dfget_rtti_classname(VALUE self, VALUE vptr)
|
2012-04-17 14:42:23 -06:00
|
|
|
{
|
2012-04-18 10:46:33 -06:00
|
|
|
char *ptr = (char*)rb_num2ulong(vptr);
|
2016-10-21 06:52:26 -06:00
|
|
|
#if defined(_WIN64)
|
|
|
|
// win64
|
|
|
|
char *rtti = *(char**)(ptr - 0x8);
|
2016-11-22 20:43:48 -07:00
|
|
|
char *typeinfo = (char*)Core::getInstance().p->getBase() + *(uint32_t*)(rtti + 0xC);
|
2016-10-21 06:52:26 -06:00
|
|
|
// skip the .?AV, trim @@ from end
|
|
|
|
return rb_str_new(typeinfo+0x14, strlen(typeinfo+0x14)-2);
|
|
|
|
#elif defined(WIN32)
|
|
|
|
// win32
|
2012-04-18 10:46:33 -06:00
|
|
|
char *rtti = *(char**)(ptr - 0x4);
|
|
|
|
char *typeinfo = *(char**)(rtti + 0xC);
|
|
|
|
// skip the .?AV, trim @@ from end
|
|
|
|
return rb_str_new(typeinfo+0xc, strlen(typeinfo+0xc)-2);
|
|
|
|
#else
|
2016-10-21 09:46:53 -06:00
|
|
|
// linux/osx 32/64
|
|
|
|
char *typeinfo = *(char**)(ptr - sizeof(void*));
|
|
|
|
char *typestring = *(char**)(typeinfo + sizeof(void*));
|
2012-04-18 10:46:33 -06:00
|
|
|
while (*typestring >= '0' && *typestring <= '9')
|
|
|
|
typestring++;
|
2012-06-24 08:09:31 -06:00
|
|
|
return rb_str_new(typestring, strlen(typestring));
|
2012-04-18 10:46:33 -06:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_dfget_vtable_ptr(VALUE self, VALUE objptr)
|
|
|
|
{
|
2016-10-21 09:46:53 -06:00
|
|
|
return rb_uint2inum(*(uintptr_t*)rb_num2ulong(objptr));
|
2012-04-17 14:42:23 -06:00
|
|
|
}
|
|
|
|
|
2017-06-06 21:34:34 -06:00
|
|
|
static VALUE rb_dfget_selected_unit_id(VALUE self)
|
|
|
|
{
|
|
|
|
df::unit *u = Gui::getAnyUnit(Core::getTopViewscreen());
|
|
|
|
return rb_int2inum(u ? u->id : -1);
|
|
|
|
}
|
|
|
|
|
2012-12-11 09:25:51 -07:00
|
|
|
// run a dfhack command, as if typed from the dfhack console
|
|
|
|
static VALUE rb_dfhack_run(VALUE self, VALUE cmd)
|
|
|
|
{
|
|
|
|
std::string s;
|
2016-12-05 02:29:16 -07:00
|
|
|
int strlen = FIX2INT(rb_funcall(cmd, rb_intern("bytesize"), 0));
|
2012-12-11 09:25:51 -07:00
|
|
|
s.assign(rb_string_value_ptr(&cmd), strlen);
|
2013-04-12 06:47:56 -06:00
|
|
|
dfhack_run_queue->push_back(s);
|
2012-12-11 09:25:51 -07:00
|
|
|
return Qtrue;
|
|
|
|
}
|
2012-03-21 11:49:50 -06:00
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
|
|
|
|
|
|
|
|
// raw memory access
|
|
|
|
// used by the ruby class definitions
|
2012-04-18 10:46:33 -06:00
|
|
|
// XXX may cause game crash ! double-check your addresses !
|
2012-03-21 11:49:50 -06:00
|
|
|
|
|
|
|
static VALUE rb_dfmalloc(VALUE self, VALUE len)
|
|
|
|
{
|
2012-04-25 11:22:04 -06:00
|
|
|
char *ptr = (char*)malloc(FIX2INT(len));
|
|
|
|
if (!ptr)
|
2012-06-24 08:09:31 -06:00
|
|
|
return Qnil;
|
2012-04-25 11:22:04 -06:00
|
|
|
memset(ptr, 0, FIX2INT(len));
|
2016-08-10 13:24:07 -06:00
|
|
|
return rb_uint2inum((uintptr_t)ptr);
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_dffree(VALUE self, VALUE ptr)
|
|
|
|
{
|
|
|
|
free((void*)rb_num2ulong(ptr));
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
// memory reading (buffer)
|
|
|
|
static VALUE rb_dfmemory_read(VALUE self, VALUE addr, VALUE len)
|
2012-03-21 11:49:50 -06:00
|
|
|
{
|
2012-04-12 11:12:46 -06:00
|
|
|
return rb_str_new((char*)rb_num2ulong(addr), rb_num2ulong(len));
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
// memory reading (integers/floats)
|
|
|
|
static VALUE rb_dfmemory_read_int8(VALUE self, VALUE addr)
|
2012-03-21 11:49:50 -06:00
|
|
|
{
|
2012-04-12 11:12:46 -06:00
|
|
|
return rb_int2inum(*(char*)rb_num2ulong(addr));
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
2012-04-12 11:12:46 -06:00
|
|
|
static VALUE rb_dfmemory_read_int16(VALUE self, VALUE addr)
|
2012-03-21 11:49:50 -06:00
|
|
|
{
|
2012-04-12 11:12:46 -06:00
|
|
|
return rb_int2inum(*(short*)rb_num2ulong(addr));
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
2012-04-12 11:12:46 -06:00
|
|
|
static VALUE rb_dfmemory_read_int32(VALUE self, VALUE addr)
|
2012-03-21 11:49:50 -06:00
|
|
|
{
|
2012-04-12 11:12:46 -06:00
|
|
|
return rb_int2inum(*(int*)rb_num2ulong(addr));
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
static VALUE rb_dfmemory_read_float(VALUE self, VALUE addr)
|
|
|
|
{
|
|
|
|
return rb_float_new(*(float*)rb_num2ulong(addr));
|
|
|
|
}
|
2012-03-21 11:49:50 -06:00
|
|
|
|
2012-12-04 09:18:09 -07:00
|
|
|
static VALUE rb_dfmemory_read_double(VALUE self, VALUE addr)
|
|
|
|
{
|
|
|
|
return rb_float_new(*(double*)rb_num2ulong(addr));
|
|
|
|
}
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
// memory writing (buffer)
|
|
|
|
static VALUE rb_dfmemory_write(VALUE self, VALUE addr, VALUE raw)
|
|
|
|
{
|
|
|
|
// no stable api for raw.length between rb1.8/rb1.9 ...
|
2016-12-05 02:29:16 -07:00
|
|
|
int strlen = FIX2INT(rb_funcall(raw, rb_intern("bytesize"), 0));
|
2012-03-21 11:49:50 -06:00
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
memcpy((void*)rb_num2ulong(addr), rb_string_value_ptr(&raw), strlen);
|
2012-03-21 11:49:50 -06:00
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
return Qtrue;
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
// memory writing (integers/floats)
|
|
|
|
static VALUE rb_dfmemory_write_int8(VALUE self, VALUE addr, VALUE val)
|
|
|
|
{
|
|
|
|
*(char*)rb_num2ulong(addr) = rb_num2ulong(val);
|
2012-03-21 11:49:50 -06:00
|
|
|
return Qtrue;
|
|
|
|
}
|
2012-04-12 11:12:46 -06:00
|
|
|
static VALUE rb_dfmemory_write_int16(VALUE self, VALUE addr, VALUE val)
|
|
|
|
{
|
|
|
|
*(short*)rb_num2ulong(addr) = rb_num2ulong(val);
|
|
|
|
return Qtrue;
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
2012-04-12 11:12:46 -06:00
|
|
|
static VALUE rb_dfmemory_write_int32(VALUE self, VALUE addr, VALUE val)
|
|
|
|
{
|
|
|
|
*(int*)rb_num2ulong(addr) = rb_num2ulong(val);
|
2012-03-21 11:49:50 -06:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
static VALUE rb_dfmemory_write_float(VALUE self, VALUE addr, VALUE val)
|
|
|
|
{
|
|
|
|
*(float*)rb_num2ulong(addr) = rb_num2dbl(val);
|
|
|
|
return Qtrue;
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
|
|
|
|
2012-12-04 09:18:09 -07:00
|
|
|
static VALUE rb_dfmemory_write_double(VALUE self, VALUE addr, VALUE val)
|
|
|
|
{
|
|
|
|
*(double*)rb_num2ulong(addr) = rb_num2dbl(val);
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2012-08-23 10:44:03 -06:00
|
|
|
// return memory permissions at address (eg "rx", nil if unmapped)
|
|
|
|
static VALUE rb_dfmemory_check(VALUE self, VALUE addr)
|
|
|
|
{
|
|
|
|
void *ptr = (void*)rb_num2ulong(addr);
|
|
|
|
std::vector<t_memrange> ranges;
|
|
|
|
Core::getInstance().p->getMemRanges(ranges);
|
|
|
|
|
|
|
|
unsigned i = 0;
|
|
|
|
while (i < ranges.size() && ranges[i].end <= ptr)
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (i >= ranges.size() || ranges[i].start > ptr || !ranges[i].valid)
|
|
|
|
return Qnil;
|
|
|
|
|
|
|
|
std::string perm = "";
|
|
|
|
if (ranges[i].read)
|
|
|
|
perm += "r";
|
|
|
|
if (ranges[i].write)
|
|
|
|
perm += "w";
|
|
|
|
if (ranges[i].execute)
|
|
|
|
perm += "x";
|
|
|
|
if (ranges[i].shared)
|
|
|
|
perm += "s";
|
|
|
|
|
|
|
|
return rb_str_new(perm.c_str(), perm.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
// memory write (tmp override page permissions, eg patch code)
|
|
|
|
static VALUE rb_dfmemory_patch(VALUE self, VALUE addr, VALUE raw)
|
|
|
|
{
|
2016-12-05 02:29:16 -07:00
|
|
|
int strlen = FIX2INT(rb_funcall(raw, rb_intern("bytesize"), 0));
|
2012-08-23 10:44:03 -06:00
|
|
|
bool ret;
|
|
|
|
|
|
|
|
ret = Core::getInstance().p->patchMemory((void*)rb_num2ulong(addr),
|
2012-08-31 07:51:22 -06:00
|
|
|
rb_string_value_ptr(&raw), strlen);
|
2012-08-23 10:44:03 -06:00
|
|
|
|
|
|
|
return ret ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
2012-12-11 09:25:51 -07:00
|
|
|
// allocate memory pages
|
|
|
|
static VALUE rb_dfmemory_pagealloc(VALUE self, VALUE len)
|
|
|
|
{
|
|
|
|
void *ret = Core::getInstance().p->memAlloc(rb_num2ulong(len));
|
|
|
|
|
2016-08-10 13:24:07 -06:00
|
|
|
return (ret == (void*)-1) ? Qnil : rb_uint2inum((uintptr_t)ret);
|
2012-12-11 09:25:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// free memory from pagealloc
|
|
|
|
static VALUE rb_dfmemory_pagedealloc(VALUE self, VALUE ptr, VALUE len)
|
|
|
|
{
|
|
|
|
int ret = Core::getInstance().p->memDealloc((void*)rb_num2ulong(ptr), rb_num2ulong(len));
|
|
|
|
|
|
|
|
return ret ? Qfalse : Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// change memory page permissions
|
|
|
|
// ptr must be page-aligned
|
|
|
|
// prot is a String, eg 'rwx', 'r', 'x'
|
|
|
|
static VALUE rb_dfmemory_pageprotect(VALUE self, VALUE ptr, VALUE len, VALUE prot_str)
|
|
|
|
{
|
|
|
|
int ret, prot=0;
|
|
|
|
char *prot_p = rb_string_value_ptr(&prot_str);
|
|
|
|
|
|
|
|
if (*prot_p == 'r') {
|
|
|
|
prot |= Process::MemProt::READ;
|
|
|
|
++prot_p;
|
|
|
|
}
|
|
|
|
if (*prot_p == 'w') {
|
|
|
|
prot |= Process::MemProt::WRITE;
|
|
|
|
++prot_p;
|
|
|
|
}
|
|
|
|
if (*prot_p == 'x') {
|
|
|
|
prot |= Process::MemProt::EXEC;
|
|
|
|
++prot_p;
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::printerr("pageprot %x %x %x\n", rb_num2ulong(ptr), rb_num2ulong(len), prot);
|
|
|
|
ret = Core::getInstance().p->memProtect((void*)rb_num2ulong(ptr), rb_num2ulong(len), prot);
|
|
|
|
|
|
|
|
return ret ? Qfalse : Qtrue;
|
|
|
|
}
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
|
2012-04-25 11:22:04 -06:00
|
|
|
// stl::string
|
2012-08-09 06:40:14 -06:00
|
|
|
static VALUE rb_dfmemory_stlstring_new(VALUE self)
|
|
|
|
{
|
|
|
|
std::string *ptr = new std::string;
|
2016-08-10 13:24:07 -06:00
|
|
|
return rb_uint2inum((uintptr_t)ptr);
|
2012-08-09 06:40:14 -06:00
|
|
|
}
|
|
|
|
static VALUE rb_dfmemory_stlstring_delete(VALUE self, VALUE addr)
|
|
|
|
{
|
|
|
|
std::string *ptr = (std::string*)rb_num2ulong(addr);
|
|
|
|
if (ptr)
|
|
|
|
delete ptr;
|
|
|
|
return Qtrue;
|
|
|
|
}
|
2012-04-25 11:22:04 -06:00
|
|
|
static VALUE rb_dfmemory_stlstring_init(VALUE self, VALUE addr)
|
|
|
|
{
|
2014-04-01 07:05:39 -06:00
|
|
|
new((void*)rb_num2ulong(addr)) std::string();
|
2012-04-25 11:22:04 -06:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
static VALUE rb_dfmemory_read_stlstring(VALUE self, VALUE addr)
|
|
|
|
{
|
|
|
|
std::string *s = (std::string*)rb_num2ulong(addr);
|
|
|
|
return rb_str_new(s->c_str(), s->length());
|
|
|
|
}
|
|
|
|
static VALUE rb_dfmemory_write_stlstring(VALUE self, VALUE addr, VALUE val)
|
|
|
|
{
|
|
|
|
std::string *s = (std::string*)rb_num2ulong(addr);
|
2016-12-05 02:29:16 -07:00
|
|
|
int strlen = FIX2INT(rb_funcall(val, rb_intern("bytesize"), 0));
|
2012-04-25 11:22:04 -06:00
|
|
|
s->assign(rb_string_value_ptr(&val), strlen);
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
// vector access
|
2012-08-09 06:40:14 -06:00
|
|
|
static VALUE rb_dfmemory_vec_new(VALUE self)
|
|
|
|
{
|
|
|
|
std::vector<uint8_t> *ptr = new std::vector<uint8_t>;
|
2016-08-10 13:24:07 -06:00
|
|
|
return rb_uint2inum((uintptr_t)ptr);
|
2012-08-09 06:40:14 -06:00
|
|
|
}
|
|
|
|
static VALUE rb_dfmemory_vec_delete(VALUE self, VALUE addr)
|
|
|
|
{
|
|
|
|
std::vector<uint8_t> *ptr = (std::vector<uint8_t>*)rb_num2ulong(addr);
|
|
|
|
if (ptr)
|
|
|
|
delete ptr;
|
|
|
|
return Qtrue;
|
|
|
|
}
|
2012-04-25 11:22:04 -06:00
|
|
|
static VALUE rb_dfmemory_vec_init(VALUE self, VALUE addr)
|
|
|
|
{
|
2014-04-01 07:05:39 -06:00
|
|
|
new((void*)rb_num2ulong(addr)) std::vector<uint8_t>();
|
2012-04-25 11:22:04 -06:00
|
|
|
return Qtrue;
|
|
|
|
}
|
2012-04-12 11:12:46 -06:00
|
|
|
// vector<uint8>
|
|
|
|
static VALUE rb_dfmemory_vec8_length(VALUE self, VALUE addr)
|
|
|
|
{
|
|
|
|
std::vector<uint8_t> *v = (std::vector<uint8_t>*)rb_num2ulong(addr);
|
|
|
|
return rb_uint2inum(v->size());
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
2012-04-19 11:35:55 -06:00
|
|
|
static VALUE rb_dfmemory_vec8_ptrat(VALUE self, VALUE addr, VALUE idx)
|
2012-04-12 11:12:46 -06:00
|
|
|
{
|
|
|
|
std::vector<uint8_t> *v = (std::vector<uint8_t>*)rb_num2ulong(addr);
|
2016-08-10 13:24:07 -06:00
|
|
|
return rb_uint2inum((uintptr_t)&v->at(FIX2INT(idx)));
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
2012-08-09 06:40:14 -06:00
|
|
|
static VALUE rb_dfmemory_vec8_insertat(VALUE self, VALUE addr, VALUE idx, VALUE val)
|
2012-04-12 11:12:46 -06:00
|
|
|
{
|
|
|
|
std::vector<uint8_t> *v = (std::vector<uint8_t>*)rb_num2ulong(addr);
|
|
|
|
v->insert(v->begin()+FIX2INT(idx), rb_num2ulong(val));
|
|
|
|
return Qtrue;
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
2012-08-09 06:40:14 -06:00
|
|
|
static VALUE rb_dfmemory_vec8_deleteat(VALUE self, VALUE addr, VALUE idx)
|
2012-04-12 11:12:46 -06:00
|
|
|
{
|
|
|
|
std::vector<uint8_t> *v = (std::vector<uint8_t>*)rb_num2ulong(addr);
|
|
|
|
v->erase(v->begin()+FIX2INT(idx));
|
|
|
|
return Qtrue;
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
// vector<uint16>
|
|
|
|
static VALUE rb_dfmemory_vec16_length(VALUE self, VALUE addr)
|
|
|
|
{
|
|
|
|
std::vector<uint16_t> *v = (std::vector<uint16_t>*)rb_num2ulong(addr);
|
|
|
|
return rb_uint2inum(v->size());
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
2012-04-19 11:35:55 -06:00
|
|
|
static VALUE rb_dfmemory_vec16_ptrat(VALUE self, VALUE addr, VALUE idx)
|
2012-04-12 11:12:46 -06:00
|
|
|
{
|
|
|
|
std::vector<uint16_t> *v = (std::vector<uint16_t>*)rb_num2ulong(addr);
|
2016-08-10 13:24:07 -06:00
|
|
|
return rb_uint2inum((uintptr_t)&v->at(FIX2INT(idx)));
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
2012-08-09 06:40:14 -06:00
|
|
|
static VALUE rb_dfmemory_vec16_insertat(VALUE self, VALUE addr, VALUE idx, VALUE val)
|
2012-03-21 11:49:50 -06:00
|
|
|
{
|
2012-04-12 11:12:46 -06:00
|
|
|
std::vector<uint16_t> *v = (std::vector<uint16_t>*)rb_num2ulong(addr);
|
|
|
|
v->insert(v->begin()+FIX2INT(idx), rb_num2ulong(val));
|
|
|
|
return Qtrue;
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
2012-08-09 06:40:14 -06:00
|
|
|
static VALUE rb_dfmemory_vec16_deleteat(VALUE self, VALUE addr, VALUE idx)
|
2012-03-21 11:49:50 -06:00
|
|
|
{
|
2012-04-12 11:12:46 -06:00
|
|
|
std::vector<uint16_t> *v = (std::vector<uint16_t>*)rb_num2ulong(addr);
|
|
|
|
v->erase(v->begin()+FIX2INT(idx));
|
|
|
|
return Qtrue;
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
|
|
|
|
2012-04-12 11:12:46 -06:00
|
|
|
// vector<uint32>
|
|
|
|
static VALUE rb_dfmemory_vec32_length(VALUE self, VALUE addr)
|
2012-03-21 11:49:50 -06:00
|
|
|
{
|
2012-04-12 11:12:46 -06:00
|
|
|
std::vector<uint32_t> *v = (std::vector<uint32_t>*)rb_num2ulong(addr);
|
|
|
|
return rb_uint2inum(v->size());
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
2012-04-19 11:35:55 -06:00
|
|
|
static VALUE rb_dfmemory_vec32_ptrat(VALUE self, VALUE addr, VALUE idx)
|
2012-03-21 11:49:50 -06:00
|
|
|
{
|
2012-04-12 11:12:46 -06:00
|
|
|
std::vector<uint32_t> *v = (std::vector<uint32_t>*)rb_num2ulong(addr);
|
2016-08-10 13:24:07 -06:00
|
|
|
return rb_uint2inum((uintptr_t)&v->at(FIX2INT(idx)));
|
2012-04-12 11:12:46 -06:00
|
|
|
}
|
2012-08-09 06:40:14 -06:00
|
|
|
static VALUE rb_dfmemory_vec32_insertat(VALUE self, VALUE addr, VALUE idx, VALUE val)
|
2012-04-12 11:12:46 -06:00
|
|
|
{
|
|
|
|
std::vector<uint32_t> *v = (std::vector<uint32_t>*)rb_num2ulong(addr);
|
|
|
|
v->insert(v->begin()+FIX2INT(idx), rb_num2ulong(val));
|
|
|
|
return Qtrue;
|
|
|
|
}
|
2012-08-09 06:40:14 -06:00
|
|
|
static VALUE rb_dfmemory_vec32_deleteat(VALUE self, VALUE addr, VALUE idx)
|
2012-04-12 11:12:46 -06:00
|
|
|
{
|
|
|
|
std::vector<uint32_t> *v = (std::vector<uint32_t>*)rb_num2ulong(addr);
|
|
|
|
v->erase(v->begin()+FIX2INT(idx));
|
|
|
|
return Qtrue;
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|
|
|
|
|
2016-10-21 06:52:26 -06:00
|
|
|
// vector<uint64>
|
|
|
|
static VALUE rb_dfmemory_vec64_length(VALUE self, VALUE addr)
|
|
|
|
{
|
|
|
|
std::vector<uint64_t> *v = (std::vector<uint64_t>*)rb_num2ulong(addr);
|
|
|
|
return rb_uint2inum(v->size());
|
|
|
|
}
|
|
|
|
static VALUE rb_dfmemory_vec64_ptrat(VALUE self, VALUE addr, VALUE idx)
|
|
|
|
{
|
|
|
|
std::vector<uint64_t> *v = (std::vector<uint64_t>*)rb_num2ulong(addr);
|
|
|
|
return rb_uint2inum((uintptr_t)&v->at(FIX2INT(idx)));
|
|
|
|
}
|
|
|
|
static VALUE rb_dfmemory_vec64_insertat(VALUE self, VALUE addr, VALUE idx, VALUE val)
|
|
|
|
{
|
|
|
|
std::vector<uint64_t> *v = (std::vector<uint64_t>*)rb_num2ulong(addr);
|
|
|
|
v->insert(v->begin()+FIX2INT(idx), rb_num2ulong(val));
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
static VALUE rb_dfmemory_vec64_deleteat(VALUE self, VALUE addr, VALUE idx)
|
|
|
|
{
|
|
|
|
std::vector<uint64_t> *v = (std::vector<uint64_t>*)rb_num2ulong(addr);
|
|
|
|
v->erase(v->begin()+FIX2INT(idx));
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2012-04-19 11:35:55 -06:00
|
|
|
// vector<bool>
|
2012-08-09 06:40:14 -06:00
|
|
|
static VALUE rb_dfmemory_vecbool_new(VALUE self)
|
|
|
|
{
|
|
|
|
std::vector<bool> *ptr = new std::vector<bool>;
|
2016-08-10 13:24:07 -06:00
|
|
|
return rb_uint2inum((uintptr_t)ptr);
|
2012-08-09 06:40:14 -06:00
|
|
|
}
|
|
|
|
static VALUE rb_dfmemory_vecbool_delete(VALUE self, VALUE addr)
|
|
|
|
{
|
|
|
|
std::vector<bool> *ptr = (std::vector<bool>*)rb_num2ulong(addr);
|
|
|
|
if (ptr)
|
|
|
|
delete ptr;
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
static VALUE rb_dfmemory_vecbool_init(VALUE self, VALUE addr)
|
|
|
|
{
|
2014-04-01 07:05:39 -06:00
|
|
|
new((void*)rb_num2ulong(addr)) std::vector<bool>();
|
2012-08-09 06:40:14 -06:00
|
|
|
return Qtrue;
|
|
|
|
}
|
2012-04-19 11:35:55 -06:00
|
|
|
static VALUE rb_dfmemory_vecbool_length(VALUE self, VALUE addr)
|
|
|
|
{
|
|
|
|
std::vector<bool> *v = (std::vector<bool>*)rb_num2ulong(addr);
|
|
|
|
return rb_uint2inum(v->size());
|
|
|
|
}
|
|
|
|
static VALUE rb_dfmemory_vecbool_at(VALUE self, VALUE addr, VALUE idx)
|
|
|
|
{
|
|
|
|
std::vector<bool> *v = (std::vector<bool>*)rb_num2ulong(addr);
|
|
|
|
return v->at(FIX2INT(idx)) ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
static VALUE rb_dfmemory_vecbool_setat(VALUE self, VALUE addr, VALUE idx, VALUE val)
|
|
|
|
{
|
|
|
|
std::vector<bool> *v = (std::vector<bool>*)rb_num2ulong(addr);
|
2012-04-20 09:57:41 -06:00
|
|
|
v->at(FIX2INT(idx)) = (BOOL_ISFALSE(val) ? 0 : 1);
|
2012-04-19 11:35:55 -06:00
|
|
|
return Qtrue;
|
|
|
|
}
|
2012-08-09 06:40:14 -06:00
|
|
|
static VALUE rb_dfmemory_vecbool_insertat(VALUE self, VALUE addr, VALUE idx, VALUE val)
|
2012-04-19 11:35:55 -06:00
|
|
|
{
|
|
|
|
std::vector<bool> *v = (std::vector<bool>*)rb_num2ulong(addr);
|
2012-04-20 09:57:41 -06:00
|
|
|
v->insert(v->begin()+FIX2INT(idx), (BOOL_ISFALSE(val) ? 0 : 1));
|
2012-04-19 11:35:55 -06:00
|
|
|
return Qtrue;
|
|
|
|
}
|
2012-08-09 06:40:14 -06:00
|
|
|
static VALUE rb_dfmemory_vecbool_deleteat(VALUE self, VALUE addr, VALUE idx)
|
2012-04-19 11:35:55 -06:00
|
|
|
{
|
|
|
|
std::vector<bool> *v = (std::vector<bool>*)rb_num2ulong(addr);
|
|
|
|
v->erase(v->begin()+FIX2INT(idx));
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2012-04-20 09:57:41 -06:00
|
|
|
// BitArray
|
|
|
|
static VALUE rb_dfmemory_bitarray_length(VALUE self, VALUE addr)
|
|
|
|
{
|
|
|
|
DFHack::BitArray<int> *b = (DFHack::BitArray<int>*)rb_num2ulong(addr);
|
2012-04-25 12:21:09 -06:00
|
|
|
return rb_uint2inum(b->size*8); // b->size is in bytes
|
2012-04-20 09:57:41 -06:00
|
|
|
}
|
|
|
|
static VALUE rb_dfmemory_bitarray_resize(VALUE self, VALUE addr, VALUE sz)
|
|
|
|
{
|
|
|
|
DFHack::BitArray<int> *b = (DFHack::BitArray<int>*)rb_num2ulong(addr);
|
|
|
|
b->resize(rb_num2ulong(sz));
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
static VALUE rb_dfmemory_bitarray_isset(VALUE self, VALUE addr, VALUE idx)
|
|
|
|
{
|
|
|
|
DFHack::BitArray<int> *b = (DFHack::BitArray<int>*)rb_num2ulong(addr);
|
|
|
|
return b->is_set(rb_num2ulong(idx)) ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
static VALUE rb_dfmemory_bitarray_set(VALUE self, VALUE addr, VALUE idx, VALUE val)
|
|
|
|
{
|
|
|
|
DFHack::BitArray<int> *b = (DFHack::BitArray<int>*)rb_num2ulong(addr);
|
|
|
|
b->set(rb_num2ulong(idx), (BOOL_ISFALSE(val) ? 0 : 1));
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2012-09-14 05:55:22 -06:00
|
|
|
// add basic support for std::set<uint32> used for passing keyboard keys to viewscreens
|
|
|
|
#include <set>
|
|
|
|
static VALUE rb_dfmemory_set_new(VALUE self)
|
|
|
|
{
|
|
|
|
std::set<unsigned long> *ptr = new std::set<unsigned long>;
|
2016-08-10 13:24:07 -06:00
|
|
|
return rb_uint2inum((uintptr_t)ptr);
|
2012-09-14 05:55:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_dfmemory_set_delete(VALUE self, VALUE set)
|
|
|
|
{
|
|
|
|
std::set<unsigned long> *ptr = (std::set<unsigned long>*)rb_num2ulong(set);
|
|
|
|
if (ptr)
|
|
|
|
delete ptr;
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_dfmemory_set_set(VALUE self, VALUE set, VALUE key)
|
|
|
|
{
|
|
|
|
std::set<unsigned long> *ptr = (std::set<unsigned long>*)rb_num2ulong(set);
|
|
|
|
ptr->insert(rb_num2ulong(key));
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_dfmemory_set_isset(VALUE self, VALUE set, VALUE key)
|
|
|
|
{
|
|
|
|
std::set<unsigned long> *ptr = (std::set<unsigned long>*)rb_num2ulong(set);
|
|
|
|
return ptr->count(rb_num2ulong(key)) ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_dfmemory_set_deletekey(VALUE self, VALUE set, VALUE key)
|
|
|
|
{
|
|
|
|
std::set<unsigned long> *ptr = (std::set<unsigned long>*)rb_num2ulong(set);
|
|
|
|
ptr->erase(rb_num2ulong(key));
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_dfmemory_set_clear(VALUE self, VALUE set)
|
|
|
|
{
|
|
|
|
std::set<unsigned long> *ptr = (std::set<unsigned long>*)rb_num2ulong(set);
|
|
|
|
ptr->clear();
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
|
2012-05-03 10:49:12 -06:00
|
|
|
/* call an arbitrary object virtual method */
|
2016-07-31 18:01:57 -06:00
|
|
|
#if defined(_WIN32) && !defined(_WIN64)
|
2016-10-28 08:40:14 -06:00
|
|
|
__declspec(naked) static intptr_t raw_vcall(void *that, void *fptr, uintptr_t a0,
|
|
|
|
uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5)
|
2012-06-23 18:59:56 -06:00
|
|
|
{
|
|
|
|
// __thiscall requires that the callee cleans up the stack
|
|
|
|
// here we dont know how many arguments it will take, so
|
|
|
|
// we simply fix esp across the funcall
|
|
|
|
__asm {
|
|
|
|
push ebp
|
|
|
|
mov ebp, esp
|
|
|
|
|
2012-08-31 07:51:22 -06:00
|
|
|
push a5
|
|
|
|
push a4
|
2012-06-23 18:59:56 -06:00
|
|
|
push a3
|
|
|
|
push a2
|
|
|
|
push a1
|
|
|
|
push a0
|
|
|
|
|
|
|
|
mov ecx, that
|
|
|
|
|
2012-08-31 07:51:22 -06:00
|
|
|
call fptr
|
2012-06-23 18:59:56 -06:00
|
|
|
|
|
|
|
mov esp, ebp
|
|
|
|
pop ebp
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
2012-06-14 06:25:16 -06:00
|
|
|
#else
|
2016-10-28 08:40:14 -06:00
|
|
|
static intptr_t raw_vcall(void *that, void *fptr, uintptr_t a0,
|
|
|
|
uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5)
|
2012-06-23 18:59:56 -06:00
|
|
|
{
|
2016-10-28 08:40:14 -06:00
|
|
|
intptr_t (*t_fptr)(void *me, uintptr_t, uintptr_t, uintptr_t,
|
|
|
|
uintptr_t, uintptr_t, uintptr_t);
|
2012-08-31 07:51:22 -06:00
|
|
|
t_fptr = (decltype(t_fptr))fptr;
|
|
|
|
return t_fptr(that, a0, a1, a2, a3, a4, a5);
|
2012-06-23 18:59:56 -06:00
|
|
|
}
|
2012-06-14 06:25:16 -06:00
|
|
|
#endif
|
2012-06-23 18:59:56 -06:00
|
|
|
|
|
|
|
// call an arbitrary vmethod, convert args/ret to native values for raw_vcall
|
2012-08-31 07:51:22 -06:00
|
|
|
static VALUE rb_dfvcall(VALUE self, VALUE cppobj, VALUE fptr, VALUE a0, VALUE a1, VALUE a2, VALUE a3, VALUE a4, VALUE a5)
|
2012-06-23 18:59:56 -06:00
|
|
|
{
|
2012-08-31 07:51:22 -06:00
|
|
|
return rb_int2inum(raw_vcall((void*)rb_num2ulong(cppobj), (void*)rb_num2ulong(fptr),
|
2012-06-23 18:59:56 -06:00
|
|
|
rb_num2ulong(a0), rb_num2ulong(a1),
|
2012-08-31 07:51:22 -06:00
|
|
|
rb_num2ulong(a2), rb_num2ulong(a3),
|
|
|
|
rb_num2ulong(a4), rb_num2ulong(a5)));
|
2012-05-03 10:49:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-21 11:49:50 -06:00
|
|
|
// define module DFHack and its methods
|
|
|
|
static void ruby_bind_dfhack(void) {
|
|
|
|
rb_cDFHack = rb_define_module("DFHack");
|
|
|
|
|
2012-07-05 07:35:37 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "onupdate_active", RUBY_METHOD_FUNC(rb_dfonupdate_active), 0);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "onupdate_active=", RUBY_METHOD_FUNC(rb_dfonupdate_active_set), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "onupdate_minyear", RUBY_METHOD_FUNC(rb_dfonupdate_minyear), 0);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "onupdate_minyear=", RUBY_METHOD_FUNC(rb_dfonupdate_minyear_set), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "onupdate_minyeartick", RUBY_METHOD_FUNC(rb_dfonupdate_minyeartick), 0);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "onupdate_minyeartick=", RUBY_METHOD_FUNC(rb_dfonupdate_minyeartick_set), 1);
|
2012-12-11 09:25:51 -07:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "onupdate_minyeartickadv", RUBY_METHOD_FUNC(rb_dfonupdate_minyeartickadv), 0);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "onupdate_minyeartickadv=", RUBY_METHOD_FUNC(rb_dfonupdate_minyeartickadv_set), 1);
|
2012-04-13 08:17:56 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "get_global_address", RUBY_METHOD_FUNC(rb_dfget_global_address), 1);
|
2012-04-17 14:42:23 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "get_vtable", RUBY_METHOD_FUNC(rb_dfget_vtable), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "get_rtti_classname", RUBY_METHOD_FUNC(rb_dfget_rtti_classname), 1);
|
2012-04-18 10:46:33 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "get_vtable_ptr", RUBY_METHOD_FUNC(rb_dfget_vtable_ptr), 1);
|
2017-06-06 21:34:34 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "get_selected_unit_id", RUBY_METHOD_FUNC(rb_dfget_selected_unit_id), 0);
|
2013-04-12 06:47:56 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "dfhack_run", RUBY_METHOD_FUNC(rb_dfhack_run), 1);
|
2012-03-21 11:49:50 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "print_str", RUBY_METHOD_FUNC(rb_dfprint_str), 1);
|
2014-09-25 07:53:03 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "print_color", RUBY_METHOD_FUNC(rb_dfprint_color), 2);
|
2012-03-21 11:49:50 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "print_err", RUBY_METHOD_FUNC(rb_dfprint_err), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "malloc", RUBY_METHOD_FUNC(rb_dfmalloc), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "free", RUBY_METHOD_FUNC(rb_dffree), 1);
|
2012-12-11 09:25:51 -07:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "pagealloc", RUBY_METHOD_FUNC(rb_dfmemory_pagealloc), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "pagedealloc", RUBY_METHOD_FUNC(rb_dfmemory_pagedealloc), 2);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "pageprotect", RUBY_METHOD_FUNC(rb_dfmemory_pageprotect), 3);
|
2012-08-31 07:51:22 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "vmethod_do_call", RUBY_METHOD_FUNC(rb_dfvcall), 8);
|
2012-11-28 05:33:07 -07:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "version", RUBY_METHOD_FUNC(rb_dfversion), 0);
|
2012-04-12 11:12:46 -06:00
|
|
|
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_read", RUBY_METHOD_FUNC(rb_dfmemory_read), 2);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_read_int8", RUBY_METHOD_FUNC(rb_dfmemory_read_int8), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_read_int16", RUBY_METHOD_FUNC(rb_dfmemory_read_int16), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_read_int32", RUBY_METHOD_FUNC(rb_dfmemory_read_int32), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_read_float", RUBY_METHOD_FUNC(rb_dfmemory_read_float), 1);
|
2012-12-04 09:18:09 -07:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_read_double", RUBY_METHOD_FUNC(rb_dfmemory_read_double), 1);
|
2012-04-12 11:12:46 -06:00
|
|
|
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_write", RUBY_METHOD_FUNC(rb_dfmemory_write), 2);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_write_int8", RUBY_METHOD_FUNC(rb_dfmemory_write_int8), 2);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_write_int16", RUBY_METHOD_FUNC(rb_dfmemory_write_int16), 2);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_write_int32", RUBY_METHOD_FUNC(rb_dfmemory_write_int32), 2);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_write_float", RUBY_METHOD_FUNC(rb_dfmemory_write_float), 2);
|
2012-12-04 09:18:09 -07:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_write_double", RUBY_METHOD_FUNC(rb_dfmemory_write_double), 2);
|
2012-08-23 10:44:03 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_check", RUBY_METHOD_FUNC(rb_dfmemory_check), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_patch", RUBY_METHOD_FUNC(rb_dfmemory_patch), 2);
|
2012-04-12 11:12:46 -06:00
|
|
|
|
2012-08-09 06:40:14 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_stlstring_new", RUBY_METHOD_FUNC(rb_dfmemory_stlstring_new), 0);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_stlstring_delete", RUBY_METHOD_FUNC(rb_dfmemory_stlstring_delete), 1);
|
2012-04-25 11:22:04 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_stlstring_init", RUBY_METHOD_FUNC(rb_dfmemory_stlstring_init), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_read_stlstring", RUBY_METHOD_FUNC(rb_dfmemory_read_stlstring), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_write_stlstring", RUBY_METHOD_FUNC(rb_dfmemory_write_stlstring), 2);
|
2012-08-09 06:40:14 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector_new", RUBY_METHOD_FUNC(rb_dfmemory_vec_new), 0);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector_delete", RUBY_METHOD_FUNC(rb_dfmemory_vec_delete), 1);
|
2012-04-25 11:22:04 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector_init", RUBY_METHOD_FUNC(rb_dfmemory_vec_init), 1);
|
2012-04-12 11:12:46 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector8_length", RUBY_METHOD_FUNC(rb_dfmemory_vec8_length), 1);
|
2012-04-19 11:35:55 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector8_ptrat", RUBY_METHOD_FUNC(rb_dfmemory_vec8_ptrat), 2);
|
2012-08-09 06:40:14 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector8_insertat", RUBY_METHOD_FUNC(rb_dfmemory_vec8_insertat), 3);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector8_deleteat", RUBY_METHOD_FUNC(rb_dfmemory_vec8_deleteat), 2);
|
2012-04-12 11:12:46 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector16_length", RUBY_METHOD_FUNC(rb_dfmemory_vec16_length), 1);
|
2012-04-19 11:35:55 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector16_ptrat", RUBY_METHOD_FUNC(rb_dfmemory_vec16_ptrat), 2);
|
2012-08-09 06:40:14 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector16_insertat", RUBY_METHOD_FUNC(rb_dfmemory_vec16_insertat), 3);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector16_deleteat", RUBY_METHOD_FUNC(rb_dfmemory_vec16_deleteat), 2);
|
2012-04-12 11:12:46 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector32_length", RUBY_METHOD_FUNC(rb_dfmemory_vec32_length), 1);
|
2012-04-19 11:35:55 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector32_ptrat", RUBY_METHOD_FUNC(rb_dfmemory_vec32_ptrat), 2);
|
2012-08-09 06:40:14 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector32_insertat", RUBY_METHOD_FUNC(rb_dfmemory_vec32_insertat), 3);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector32_deleteat", RUBY_METHOD_FUNC(rb_dfmemory_vec32_deleteat), 2);
|
2016-10-21 06:52:26 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector64_length", RUBY_METHOD_FUNC(rb_dfmemory_vec64_length), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector64_ptrat", RUBY_METHOD_FUNC(rb_dfmemory_vec64_ptrat), 2);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector64_insertat", RUBY_METHOD_FUNC(rb_dfmemory_vec64_insertat), 3);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vector64_deleteat", RUBY_METHOD_FUNC(rb_dfmemory_vec64_deleteat), 2);
|
2012-08-09 06:40:14 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_new", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_new), 0);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_delete", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_delete), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_init", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_init), 1);
|
2012-04-19 11:35:55 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_length", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_length), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_at", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_at), 2);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_setat", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_setat), 3);
|
2012-08-09 06:40:14 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_insertat", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_insertat), 3);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_vectorbool_deleteat", RUBY_METHOD_FUNC(rb_dfmemory_vecbool_deleteat), 2);
|
2012-04-20 09:57:41 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_bitarray_length", RUBY_METHOD_FUNC(rb_dfmemory_bitarray_length), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_bitarray_resize", RUBY_METHOD_FUNC(rb_dfmemory_bitarray_resize), 2);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_bitarray_isset", RUBY_METHOD_FUNC(rb_dfmemory_bitarray_isset), 2);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_bitarray_set", RUBY_METHOD_FUNC(rb_dfmemory_bitarray_set), 3);
|
2012-09-14 05:55:22 -06:00
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_stlset_new", RUBY_METHOD_FUNC(rb_dfmemory_set_new), 0);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_stlset_delete", RUBY_METHOD_FUNC(rb_dfmemory_set_delete), 1);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_stlset_set", RUBY_METHOD_FUNC(rb_dfmemory_set_set), 2);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_stlset_isset", RUBY_METHOD_FUNC(rb_dfmemory_set_isset), 2);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_stlset_deletekey", RUBY_METHOD_FUNC(rb_dfmemory_set_deletekey), 2);
|
|
|
|
rb_define_singleton_method(rb_cDFHack, "memory_stlset_clear", RUBY_METHOD_FUNC(rb_dfmemory_set_clear), 1);
|
2012-03-21 11:49:50 -06:00
|
|
|
}
|