2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** binding-mri.cpp
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** mkxp is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 2 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** mkxp is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "binding.h"
|
2015-06-18 19:41:32 +00:00
|
|
|
#include "binding-util.h"
|
2013-10-09 10:30:33 +00:00
|
|
|
#include "sharedstate.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "eventthread.h"
|
|
|
|
#include "filesystem.h"
|
2015-06-18 19:41:32 +00:00
|
|
|
#include "util.h"
|
2014-12-24 04:30:43 +00:00
|
|
|
#include "sdl-util.h"
|
2013-12-11 19:46:54 +00:00
|
|
|
#include "debugwriter.h"
|
2014-08-29 12:28:23 +00:00
|
|
|
#include "graphics.h"
|
2014-08-24 05:36:19 +00:00
|
|
|
#include "audio.h"
|
2014-09-02 14:35:19 +00:00
|
|
|
#include "boost-hash.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
#include <ruby.h>
|
|
|
|
#include <ruby/encoding.h>
|
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
#include <assert.h>
|
2013-12-11 19:46:54 +00:00
|
|
|
#include <string>
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <zlib.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <SDL_filesystem.h>
|
2013-10-08 04:00:04 +00:00
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
extern const char module_rpg1[];
|
|
|
|
extern const char module_rpg2[];
|
|
|
|
extern const char module_rpg3[];
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
static void mriBindingExecute();
|
|
|
|
static void mriBindingTerminate();
|
2014-08-24 05:36:19 +00:00
|
|
|
static void mriBindingReset();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
ScriptBinding scriptBindingImpl =
|
|
|
|
{
|
2013-10-08 04:05:06 +00:00
|
|
|
mriBindingExecute,
|
2014-08-24 05:36:19 +00:00
|
|
|
mriBindingTerminate,
|
|
|
|
mriBindingReset
|
2013-09-01 14:27:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ScriptBinding *scriptBinding = &scriptBindingImpl;
|
|
|
|
|
|
|
|
void tableBindingInit();
|
|
|
|
void etcBindingInit();
|
|
|
|
void fontBindingInit();
|
|
|
|
void bitmapBindingInit();
|
|
|
|
void spriteBindingInit();
|
|
|
|
void viewportBindingInit();
|
|
|
|
void planeBindingInit();
|
|
|
|
void windowBindingInit();
|
|
|
|
void tilemapBindingInit();
|
2014-08-12 19:50:59 +00:00
|
|
|
void windowVXBindingInit();
|
2014-08-15 11:59:28 +00:00
|
|
|
void tilemapVXBindingInit();
|
2014-08-12 19:50:59 +00:00
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
void inputBindingInit();
|
|
|
|
void audioBindingInit();
|
|
|
|
void graphicsBindingInit();
|
|
|
|
|
|
|
|
void fileIntBindingInit();
|
|
|
|
|
2013-10-08 04:05:06 +00:00
|
|
|
RB_METHOD(mriPrint);
|
|
|
|
RB_METHOD(mriP);
|
2014-10-20 08:27:01 +00:00
|
|
|
RB_METHOD(mkxpDataDirectory);
|
2013-10-19 13:54:21 +00:00
|
|
|
RB_METHOD(mkxpPuts);
|
2014-10-20 08:27:01 +00:00
|
|
|
RB_METHOD(mkxpRawKeyStates);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-08-15 20:35:57 +00:00
|
|
|
RB_METHOD(mriRgssMain);
|
2014-08-29 12:28:23 +00:00
|
|
|
RB_METHOD(mriRgssStop);
|
2014-05-06 10:13:12 +00:00
|
|
|
RB_METHOD(_kernelCaller);
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
static void mriBindingInit()
|
|
|
|
{
|
|
|
|
tableBindingInit();
|
|
|
|
etcBindingInit();
|
|
|
|
fontBindingInit();
|
|
|
|
bitmapBindingInit();
|
|
|
|
spriteBindingInit();
|
|
|
|
viewportBindingInit();
|
|
|
|
planeBindingInit();
|
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
if (rgssVer == 1)
|
|
|
|
{
|
|
|
|
windowBindingInit();
|
|
|
|
tilemapBindingInit();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
windowVXBindingInit();
|
|
|
|
tilemapVXBindingInit();
|
|
|
|
}
|
2014-08-12 19:50:59 +00:00
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
inputBindingInit();
|
|
|
|
audioBindingInit();
|
|
|
|
graphicsBindingInit();
|
|
|
|
|
|
|
|
fileIntBindingInit();
|
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
if (rgssVer >= 3)
|
|
|
|
{
|
|
|
|
_rb_define_module_function(rb_mKernel, "rgss_main", mriRgssMain);
|
2014-08-29 12:28:23 +00:00
|
|
|
_rb_define_module_function(rb_mKernel, "rgss_stop", mriRgssStop);
|
2014-08-28 21:11:10 +00:00
|
|
|
|
|
|
|
_rb_define_module_function(rb_mKernel, "msgbox", mriPrint);
|
|
|
|
_rb_define_module_function(rb_mKernel, "msgbox_p", mriP);
|
2014-08-31 03:38:01 +00:00
|
|
|
|
2014-09-02 17:48:47 +00:00
|
|
|
rb_define_global_const("RGSS_VERSION", rb_str_new_cstr("3.0.1"));
|
2014-08-28 21:11:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_rb_define_module_function(rb_mKernel, "print", mriPrint);
|
|
|
|
_rb_define_module_function(rb_mKernel, "p", mriP);
|
2014-08-15 20:35:57 +00:00
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
rb_define_alias(rb_singleton_class(rb_mKernel), "_mkxp_kernel_caller_alias", "caller");
|
|
|
|
_rb_define_module_function(rb_mKernel, "caller", _kernelCaller);
|
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
if (rgssVer == 1)
|
|
|
|
rb_eval_string(module_rpg1);
|
|
|
|
else if (rgssVer == 2)
|
|
|
|
rb_eval_string(module_rpg2);
|
|
|
|
else if (rgssVer == 3)
|
|
|
|
rb_eval_string(module_rpg3);
|
|
|
|
else
|
|
|
|
assert(!"unreachable");
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-10-22 02:12:21 +00:00
|
|
|
VALUE mod = rb_define_module("MKXP");
|
2014-10-20 08:27:01 +00:00
|
|
|
_rb_define_module_function(mod, "data_directory", mkxpDataDirectory);
|
2013-10-19 13:54:21 +00:00
|
|
|
_rb_define_module_function(mod, "puts", mkxpPuts);
|
2014-10-20 08:27:01 +00:00
|
|
|
_rb_define_module_function(mod, "raw_key_states", mkxpRawKeyStates);
|
2013-10-08 04:00:04 +00:00
|
|
|
|
2014-08-24 05:21:26 +00:00
|
|
|
rb_gv_set("MKXP", Qtrue);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-12-11 19:46:54 +00:00
|
|
|
showMsg(const std::string &msg)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
shState->eThread().showMessageBox(msg.c_str());
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void printP(int argc, VALUE *argv,
|
|
|
|
const char *convMethod, const char *sep)
|
|
|
|
{
|
|
|
|
VALUE dispString = rb_str_buf_new(128);
|
|
|
|
ID conv = rb_intern(convMethod);
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; ++i)
|
|
|
|
{
|
2014-01-24 21:23:30 +00:00
|
|
|
VALUE str = rb_funcall2(argv[i], conv, 0, NULL);
|
2013-09-01 14:27:21 +00:00
|
|
|
rb_str_buf_append(dispString, str);
|
|
|
|
|
|
|
|
if (i < argc)
|
|
|
|
rb_str_buf_cat2(dispString, sep);
|
|
|
|
}
|
|
|
|
|
|
|
|
showMsg(RSTRING_PTR(dispString));
|
|
|
|
}
|
|
|
|
|
|
|
|
RB_METHOD(mriPrint)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
printP(argc, argv, "to_s", "");
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
RB_METHOD(mriP)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
printP(argc, argv, "inspect", "\n");
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2014-10-20 08:27:01 +00:00
|
|
|
RB_METHOD(mkxpDataDirectory)
|
2013-10-08 04:00:04 +00:00
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
2014-12-23 19:10:28 +00:00
|
|
|
|
|
|
|
const std::string &path = shState->config().customDataPath;
|
|
|
|
const char *s = path.empty() ? "." : path.c_str();
|
2013-10-08 04:00:04 +00:00
|
|
|
|
2014-12-23 19:10:28 +00:00
|
|
|
return rb_str_new_cstr(s);
|
2013-10-08 04:00:04 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 08:27:01 +00:00
|
|
|
RB_METHOD(mkxpPuts)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
const char *str;
|
|
|
|
rb_get_args(argc, argv, "z", &str RB_ARG_END);
|
|
|
|
|
|
|
|
Debug() << str;
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
RB_METHOD(mkxpRawKeyStates)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
VALUE str = rb_str_new(0, sizeof(EventThread::keyStates));
|
|
|
|
memcpy(RSTRING_PTR(str), EventThread::keyStates, sizeof(EventThread::keyStates));
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2014-08-24 05:36:19 +00:00
|
|
|
static VALUE rgssMainCb(VALUE block)
|
|
|
|
{
|
|
|
|
rb_funcall2(block, rb_intern("call"), 0, 0);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rgssMainRescue(VALUE arg, VALUE exc)
|
|
|
|
{
|
|
|
|
VALUE *excRet = (VALUE*) arg;
|
|
|
|
|
|
|
|
*excRet = exc;
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void processReset()
|
|
|
|
{
|
|
|
|
shState->graphics().reset();
|
|
|
|
shState->audio().reset();
|
|
|
|
|
|
|
|
shState->rtData().rqReset.clear();
|
|
|
|
shState->graphics().repaintWait(shState->rtData().rqResetFinish,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
2014-08-15 20:35:57 +00:00
|
|
|
RB_METHOD(mriRgssMain)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
2014-08-24 05:36:19 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
VALUE exc = Qnil;
|
|
|
|
|
|
|
|
rb_rescue2((VALUE(*)(ANYARGS)) rgssMainCb, rb_block_proc(),
|
|
|
|
(VALUE(*)(ANYARGS)) rgssMainRescue, (VALUE) &exc,
|
|
|
|
rb_eException, (VALUE) 0);
|
|
|
|
|
|
|
|
if (NIL_P(exc))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (rb_obj_class(exc) == getRbData()->exc[Reset])
|
|
|
|
processReset();
|
|
|
|
else
|
|
|
|
rb_exc_raise(exc);
|
|
|
|
}
|
2014-08-15 20:35:57 +00:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2014-08-29 12:28:23 +00:00
|
|
|
RB_METHOD(mriRgssStop)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
shState->graphics().update();
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2014-05-06 10:13:12 +00:00
|
|
|
RB_METHOD(_kernelCaller)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
VALUE trace = rb_funcall2(rb_mKernel, rb_intern("_mkxp_kernel_caller_alias"), 0, 0);
|
|
|
|
|
2014-08-25 08:22:23 +00:00
|
|
|
if (!RB_TYPE_P(trace, RUBY_T_ARRAY))
|
2014-05-06 10:13:12 +00:00
|
|
|
return trace;
|
|
|
|
|
|
|
|
long len = RARRAY_LEN(trace);
|
|
|
|
|
|
|
|
if (len < 2)
|
|
|
|
return trace;
|
|
|
|
|
|
|
|
/* Remove useless "ruby:1:in 'eval'" */
|
|
|
|
rb_ary_pop(trace);
|
|
|
|
|
|
|
|
/* Also remove trace of this helper function */
|
|
|
|
rb_ary_shift(trace);
|
|
|
|
|
|
|
|
len -= 2;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return trace;
|
|
|
|
|
|
|
|
/* RMXP does this, not sure if specific or 1.8 related */
|
|
|
|
VALUE args[] = { rb_str_new_cstr(":in `<main>'"), rb_str_new_cstr("") };
|
2014-05-25 10:57:04 +00:00
|
|
|
rb_funcall2(rb_ary_entry(trace, len-1), rb_intern("gsub!"), 2, args);
|
2014-05-06 10:13:12 +00:00
|
|
|
|
|
|
|
return trace;
|
|
|
|
}
|
|
|
|
|
2014-04-11 14:40:49 +00:00
|
|
|
static VALUE newStringUTF8(const char *string, long length)
|
2014-04-10 12:00:05 +00:00
|
|
|
{
|
2014-04-11 14:40:49 +00:00
|
|
|
return rb_enc_str_new(string, length, rb_utf8_encoding());
|
2014-04-10 12:00:05 +00:00
|
|
|
}
|
|
|
|
|
2014-04-11 14:40:49 +00:00
|
|
|
struct evalArg
|
|
|
|
{
|
2014-04-10 12:00:05 +00:00
|
|
|
VALUE string;
|
|
|
|
VALUE filename;
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE evalHelper(evalArg *arg)
|
|
|
|
{
|
|
|
|
VALUE argv[] = { arg->string, Qnil, arg->filename };
|
|
|
|
return rb_funcall2(Qnil, rb_intern("eval"), ARRAY_SIZE(argv), argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE evalString(VALUE string, VALUE filename, int *state)
|
|
|
|
{
|
|
|
|
evalArg arg = { string, filename };
|
|
|
|
return rb_protect((VALUE (*)(VALUE))evalHelper, (VALUE)&arg, state);
|
|
|
|
}
|
|
|
|
|
2014-04-11 14:40:49 +00:00
|
|
|
static void runCustomScript(const std::string &filename)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2014-04-10 12:00:05 +00:00
|
|
|
std::string scriptData;
|
2013-12-11 19:46:54 +00:00
|
|
|
|
2014-12-24 04:30:43 +00:00
|
|
|
if (!readFileSDL(filename.c_str(), scriptData))
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
showMsg(std::string("Unable to open '") + filename + "'");
|
2013-09-01 14:27:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-11 14:40:49 +00:00
|
|
|
evalString(newStringUTF8(scriptData.c_str(), scriptData.size()),
|
|
|
|
newStringUTF8(filename.c_str(), filename.size()), NULL);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 18:05:08 +00:00
|
|
|
VALUE kernelLoadDataInt(const char *filename, bool rubyExc);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-09-02 14:35:19 +00:00
|
|
|
struct BacktraceData
|
|
|
|
{
|
|
|
|
/* Maps: Ruby visible filename, To: Actual script name */
|
|
|
|
BoostHash<std::string, std::string> scriptNames;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SCRIPT_SECTION_FMT (rgssVer >= 3 ? "{%04ld}" : "Section%03ld")
|
|
|
|
|
|
|
|
static void runRMXPScripts(BacktraceData &btData)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2014-08-24 05:27:36 +00:00
|
|
|
const Config &conf = shState->rtData().config;
|
|
|
|
const std::string &scriptPack = conf.game.scripts;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
if (scriptPack.empty())
|
2013-09-01 14:36:11 +00:00
|
|
|
{
|
|
|
|
showMsg("No game scripts specified (missing Game.ini?)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
if (!shState->fileSystem().exists(scriptPack.c_str()))
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
showMsg("Unable to open '" + scriptPack + "'");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-23 18:05:08 +00:00
|
|
|
VALUE scriptArray;
|
|
|
|
|
|
|
|
/* We checked if Scripts.rxdata exists, but something might
|
|
|
|
* still go wrong */
|
|
|
|
try
|
|
|
|
{
|
|
|
|
scriptArray = kernelLoadDataInt(scriptPack.c_str(), false);
|
|
|
|
}
|
|
|
|
catch (const Exception &e)
|
|
|
|
{
|
|
|
|
showMsg(std::string("Failed to read script data: ") + e.msg);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-08-25 08:22:23 +00:00
|
|
|
if (!RB_TYPE_P(scriptArray, RUBY_T_ARRAY))
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
showMsg("Failed to read script data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-09 20:36:56 +00:00
|
|
|
rb_gv_set("$RGSS_SCRIPTS", scriptArray);
|
|
|
|
|
2014-01-24 21:23:30 +00:00
|
|
|
long scriptCount = RARRAY_LEN(scriptArray);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
std::string decodeBuffer;
|
2013-09-01 14:27:21 +00:00
|
|
|
decodeBuffer.resize(0x1000);
|
|
|
|
|
2014-01-24 21:23:30 +00:00
|
|
|
for (long i = 0; i < scriptCount; ++i)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
VALUE script = rb_ary_entry(scriptArray, i);
|
|
|
|
|
2014-08-25 08:22:23 +00:00
|
|
|
if (!RB_TYPE_P(script, RUBY_T_ARRAY))
|
2013-09-01 14:27:21 +00:00
|
|
|
continue;
|
|
|
|
|
2013-12-29 13:55:49 +00:00
|
|
|
VALUE scriptName = rb_ary_entry(script, 1);
|
|
|
|
VALUE scriptString = rb_ary_entry(script, 2);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
int result = Z_OK;
|
2013-12-31 21:24:56 +00:00
|
|
|
unsigned long bufferLen;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
unsigned char *bufferPtr =
|
2013-12-11 19:46:54 +00:00
|
|
|
reinterpret_cast<unsigned char*>(const_cast<char*>(decodeBuffer.c_str()));
|
2013-09-01 14:27:21 +00:00
|
|
|
const unsigned char *sourcePtr =
|
2013-12-29 13:55:49 +00:00
|
|
|
reinterpret_cast<const unsigned char*>(RSTRING_PTR(scriptString));
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
bufferLen = decodeBuffer.length();
|
|
|
|
|
|
|
|
result = uncompress(bufferPtr, &bufferLen,
|
2013-12-29 13:55:49 +00:00
|
|
|
sourcePtr, RSTRING_LEN(scriptString));
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
bufferPtr[bufferLen] = '\0';
|
|
|
|
|
|
|
|
if (result != Z_BUF_ERROR)
|
|
|
|
break;
|
|
|
|
|
|
|
|
decodeBuffer.resize(decodeBuffer.size()*2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result != Z_OK)
|
|
|
|
{
|
|
|
|
static char buffer[256];
|
2014-01-24 21:23:30 +00:00
|
|
|
snprintf(buffer, sizeof(buffer), "Error decoding script %ld: '%s'",
|
2013-12-29 13:55:49 +00:00
|
|
|
i, RSTRING_PTR(scriptName));
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
showMsg(buffer);
|
2014-01-01 11:56:45 +00:00
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-04-09 20:36:56 +00:00
|
|
|
rb_ary_store(script, 3, rb_str_new_cstr(decodeBuffer.c_str()));
|
2014-04-09 23:16:31 +00:00
|
|
|
}
|
|
|
|
|
2014-08-24 05:27:36 +00:00
|
|
|
/* Execute preloaded scripts */
|
|
|
|
for (size_t i = 0; i < conf.preloadScripts.size(); ++i)
|
|
|
|
runCustomScript(conf.preloadScripts[i]);
|
|
|
|
|
2014-10-15 04:27:43 +00:00
|
|
|
VALUE exc = rb_gv_get("$!");
|
|
|
|
if (exc != Qnil)
|
|
|
|
return;
|
|
|
|
|
2014-08-24 05:36:19 +00:00
|
|
|
while (true)
|
2014-04-09 23:16:31 +00:00
|
|
|
{
|
2014-08-24 05:36:19 +00:00
|
|
|
for (long i = 0; i < scriptCount; ++i)
|
|
|
|
{
|
|
|
|
VALUE script = rb_ary_entry(scriptArray, i);
|
|
|
|
VALUE scriptDecoded = rb_ary_entry(script, 3);
|
|
|
|
VALUE string = newStringUTF8(RSTRING_PTR(scriptDecoded),
|
|
|
|
RSTRING_LEN(scriptDecoded));
|
|
|
|
|
|
|
|
VALUE fname;
|
|
|
|
const char *scriptName = RSTRING_PTR(rb_ary_entry(script, 1));
|
|
|
|
char buf[512];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (conf.useScriptNames)
|
|
|
|
len = snprintf(buf, sizeof(buf), "%03ld:%s", i, scriptName);
|
|
|
|
else
|
|
|
|
len = snprintf(buf, sizeof(buf), SCRIPT_SECTION_FMT, i);
|
|
|
|
|
|
|
|
fname = newStringUTF8(buf, len);
|
|
|
|
btData.scriptNames.insert(buf, scriptName);
|
|
|
|
|
|
|
|
int state;
|
|
|
|
evalString(string, fname, &state);
|
|
|
|
if (state)
|
|
|
|
break;
|
|
|
|
}
|
2014-04-09 20:36:56 +00:00
|
|
|
|
2014-08-24 05:36:19 +00:00
|
|
|
VALUE exc = rb_gv_get("$!");
|
|
|
|
if (rb_obj_class(exc) != getRbData()->exc[Reset])
|
2013-09-01 14:27:21 +00:00
|
|
|
break;
|
2014-08-24 05:36:19 +00:00
|
|
|
|
|
|
|
processReset();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 14:35:19 +00:00
|
|
|
static void showExc(VALUE exc, const BacktraceData &btData)
|
2014-04-13 22:07:51 +00:00
|
|
|
{
|
|
|
|
VALUE bt = rb_funcall2(exc, rb_intern("backtrace"), 0, NULL);
|
2014-09-02 14:35:19 +00:00
|
|
|
VALUE msg = rb_funcall2(exc, rb_intern("message"), 0, NULL);
|
2014-04-13 22:07:51 +00:00
|
|
|
VALUE bt0 = rb_ary_entry(bt, 0);
|
|
|
|
VALUE name = rb_class_path(rb_obj_class(exc));
|
|
|
|
|
|
|
|
VALUE ds = rb_sprintf("%" PRIsVALUE ": %" PRIsVALUE " (%" PRIsVALUE ")",
|
|
|
|
bt0, exc, name);
|
2014-04-16 18:05:15 +00:00
|
|
|
/* omit "useless" last entry (from ruby:1:in `eval') */
|
2014-04-13 22:07:51 +00:00
|
|
|
for (long i = 1, btlen = RARRAY_LEN(bt) - 1; i < btlen; ++i)
|
|
|
|
rb_str_catf(ds, "\n\tfrom %" PRIsVALUE, rb_ary_entry(bt, i));
|
|
|
|
Debug() << StringValueCStr(ds);
|
|
|
|
|
2014-09-02 14:35:19 +00:00
|
|
|
char *s = RSTRING_PTR(bt0);
|
|
|
|
|
|
|
|
char line[16];
|
|
|
|
std::string file(512, '\0');
|
|
|
|
|
|
|
|
char *p = s + strlen(s);
|
|
|
|
char *e;
|
|
|
|
|
|
|
|
while (p != s)
|
|
|
|
if (*--p == ':')
|
|
|
|
break;
|
|
|
|
|
|
|
|
e = p;
|
|
|
|
|
|
|
|
while (p != s)
|
|
|
|
if (*--p == ':')
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* s p e
|
|
|
|
* SectionXXX:YY: in 'blabla' */
|
|
|
|
|
|
|
|
*e = '\0';
|
|
|
|
strncpy(line, *p ? p+1 : p, sizeof(line));
|
|
|
|
line[sizeof(line)-1] = '\0';
|
|
|
|
*e = ':';
|
|
|
|
e = p;
|
|
|
|
|
|
|
|
/* s e
|
|
|
|
* SectionXXX:YY: in 'blabla' */
|
|
|
|
|
|
|
|
*e = '\0';
|
|
|
|
strncpy(&file[0], s, file.size());
|
|
|
|
*e = ':';
|
|
|
|
|
|
|
|
/* Shrink to fit */
|
|
|
|
file.resize(strlen(file.c_str()));
|
|
|
|
file = btData.scriptNames.value(file, file);
|
|
|
|
|
|
|
|
std::string ms(640, '\0');
|
|
|
|
snprintf(&ms[0], ms.size(), "Script '%s' line %s: %s occured.\n\n%s",
|
|
|
|
file.c_str(), line, RSTRING_PTR(name), RSTRING_PTR(msg));
|
|
|
|
|
|
|
|
showMsg(ms);
|
2014-04-13 22:07:51 +00:00
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
static void mriBindingExecute()
|
|
|
|
{
|
2014-11-20 12:51:35 +00:00
|
|
|
/* Normally only a ruby executable would do a sysinit,
|
|
|
|
* but not doing it will lead to crashes due to closed
|
|
|
|
* stdio streams on some platforms (eg. Windows) */
|
|
|
|
int argc = 0;
|
|
|
|
char **argv = 0;
|
|
|
|
ruby_sysinit(&argc, &argv);
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
ruby_setup();
|
2013-10-19 16:57:34 +00:00
|
|
|
rb_enc_set_default_external(rb_enc_from_encoding(rb_utf8_encoding()));
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-07-24 21:11:12 +00:00
|
|
|
Config &conf = shState->rtData().config;
|
|
|
|
|
|
|
|
if (!conf.rubyLoadpaths.empty())
|
|
|
|
{
|
|
|
|
/* Setup custom load paths */
|
2014-07-25 05:57:01 +00:00
|
|
|
VALUE lpaths = rb_gv_get(":");
|
2014-07-24 21:11:12 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < conf.rubyLoadpaths.size(); ++i)
|
|
|
|
{
|
|
|
|
std::string &path = conf.rubyLoadpaths[i];
|
|
|
|
|
|
|
|
VALUE pathv = rb_str_new(path.c_str(), path.size());
|
|
|
|
rb_ary_push(lpaths, pathv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
RbData rbData;
|
2013-10-09 10:30:33 +00:00
|
|
|
shState->setBindingData(&rbData);
|
2014-09-02 14:35:19 +00:00
|
|
|
BacktraceData btData;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
mriBindingInit();
|
|
|
|
|
2014-07-24 21:11:12 +00:00
|
|
|
std::string &customScript = conf.customScript;
|
2013-12-11 19:46:54 +00:00
|
|
|
if (!customScript.empty())
|
2014-04-11 14:40:49 +00:00
|
|
|
runCustomScript(customScript);
|
2013-09-01 14:27:21 +00:00
|
|
|
else
|
2014-09-02 14:35:19 +00:00
|
|
|
runRMXPScripts(btData);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-01-24 21:23:30 +00:00
|
|
|
VALUE exc = rb_errinfo();
|
|
|
|
if (!NIL_P(exc) && !rb_obj_is_kind_of(exc, rb_eSystemExit))
|
2014-09-02 14:35:19 +00:00
|
|
|
showExc(exc, btData);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
ruby_cleanup(0);
|
|
|
|
|
2014-08-24 05:36:19 +00:00
|
|
|
shState->rtData().rqTermAck.set();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mriBindingTerminate()
|
|
|
|
{
|
|
|
|
rb_raise(rb_eSystemExit, " ");
|
|
|
|
}
|
2014-08-24 05:36:19 +00:00
|
|
|
|
|
|
|
static void mriBindingReset()
|
|
|
|
{
|
|
|
|
rb_raise(getRbData()->exc[Reset], " ");
|
|
|
|
}
|