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"
|
|
|
|
#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"
|
2013-12-11 19:46:54 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "debugwriter.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
#include <ruby.h>
|
|
|
|
#include <ruby/encoding.h>
|
|
|
|
|
|
|
|
#include <string>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
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
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
extern const char module_rpg[];
|
|
|
|
|
|
|
|
static void mriBindingExecute();
|
|
|
|
static void mriBindingTerminate();
|
|
|
|
|
|
|
|
ScriptBinding scriptBindingImpl =
|
|
|
|
{
|
2013-10-08 04:05:06 +00:00
|
|
|
mriBindingExecute,
|
|
|
|
mriBindingTerminate
|
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();
|
|
|
|
|
|
|
|
void inputBindingInit();
|
|
|
|
void audioBindingInit();
|
|
|
|
void graphicsBindingInit();
|
|
|
|
|
|
|
|
void fileIntBindingInit();
|
|
|
|
|
2013-10-08 04:05:06 +00:00
|
|
|
RB_METHOD(mriPrint);
|
|
|
|
RB_METHOD(mriP);
|
2013-10-08 04:00:04 +00:00
|
|
|
RB_METHOD(mriDataDirectory);
|
2013-10-19 13:54:21 +00:00
|
|
|
RB_METHOD(mkxpPuts);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
static void mriBindingInit()
|
|
|
|
{
|
|
|
|
tableBindingInit();
|
|
|
|
etcBindingInit();
|
|
|
|
fontBindingInit();
|
|
|
|
bitmapBindingInit();
|
|
|
|
spriteBindingInit();
|
|
|
|
viewportBindingInit();
|
|
|
|
planeBindingInit();
|
|
|
|
windowBindingInit();
|
|
|
|
tilemapBindingInit();
|
|
|
|
|
|
|
|
inputBindingInit();
|
|
|
|
audioBindingInit();
|
|
|
|
graphicsBindingInit();
|
|
|
|
|
|
|
|
fileIntBindingInit();
|
|
|
|
|
|
|
|
_rb_define_module_function(rb_mKernel, "print", mriPrint);
|
|
|
|
_rb_define_module_function(rb_mKernel, "p", mriP);
|
|
|
|
|
|
|
|
rb_eval_string(module_rpg);
|
|
|
|
|
2013-10-08 04:00:04 +00:00
|
|
|
VALUE mod = rb_define_module("System");
|
|
|
|
_rb_define_module_function(mod, "data_directory", mriDataDirectory);
|
2013-10-19 13:54:21 +00:00
|
|
|
_rb_define_module_function(mod, "puts", mkxpPuts);
|
2013-10-08 04:00:04 +00:00
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
rb_define_global_const("MKXP", Qtrue);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-10-19 13:54:21 +00:00
|
|
|
RB_METHOD(mkxpPuts)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
const char *str;
|
2013-12-20 10:29:12 +00:00
|
|
|
rb_get_args(argc, argv, "z", &str RB_ARG_END);
|
2013-10-19 13:54:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
Debug() << str;
|
2013-10-19 13:54:21 +00:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
VALUE str = rb_funcall(argv[i], conv, 0);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-08 04:00:04 +00:00
|
|
|
RB_METHOD(mriDataDirectory)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
const char *org, *app;
|
|
|
|
|
2013-12-20 10:29:12 +00:00
|
|
|
rb_get_args(argc, argv, "zz", &org, &app RB_ARG_END);
|
2013-10-08 04:00:04 +00:00
|
|
|
|
|
|
|
char *path = SDL_GetPrefPath(org, app);
|
|
|
|
|
|
|
|
VALUE pathStr = rb_str_new_cstr(path);
|
|
|
|
|
|
|
|
SDL_free(path);
|
|
|
|
|
|
|
|
return pathStr;
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
static void runCustomScript(const char *filename)
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
std::string scriptData("#encoding:utf-8\n");
|
|
|
|
|
|
|
|
if (!readFile(filename, 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;
|
|
|
|
}
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
rb_eval_string_protect(scriptData.c_str(), 0);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE kernelLoadDataInt(const char *filename);
|
|
|
|
|
|
|
|
static void runRMXPScripts()
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
const std::string &scriptPack = shState->rtData().config.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;
|
|
|
|
}
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
VALUE scriptArray = kernelLoadDataInt(scriptPack.c_str());
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
if (rb_type(scriptArray) != RUBY_T_ARRAY)
|
|
|
|
{
|
|
|
|
showMsg("Failed to read script data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-26 19:18:33 +00:00
|
|
|
size_t 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);
|
|
|
|
|
2013-12-26 19:18:33 +00:00
|
|
|
for (size_t i = 0; i < scriptCount; ++i)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
VALUE script = rb_ary_entry(scriptArray, i);
|
|
|
|
|
|
|
|
if (rb_type(script) != RUBY_T_ARRAY)
|
|
|
|
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];
|
2013-12-26 19:18:33 +00:00
|
|
|
/* FIXME: '%zu' apparently gcc only? */
|
|
|
|
snprintf(buffer, sizeof(buffer), "Error decoding script %zu: '%s'",
|
2013-12-29 13:55:49 +00:00
|
|
|
i, RSTRING_PTR(scriptName));
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
showMsg(buffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
/* Store encoding header + the decoded script
|
|
|
|
* in 'sc.decData' */
|
2013-12-29 13:55:49 +00:00
|
|
|
std::string decData = "#encoding:utf-8\n";
|
|
|
|
size_t hdSize = decData.size();
|
|
|
|
decData.resize(hdSize + bufferLen);
|
|
|
|
memcpy(&decData[hdSize], decodeBuffer.c_str(), bufferLen);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-29 13:55:49 +00:00
|
|
|
ruby_script(RSTRING_PTR(scriptName));
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
rb_gc_start();
|
|
|
|
|
|
|
|
/* Execute code */
|
2013-12-29 13:55:49 +00:00
|
|
|
rb_eval_string_protect(decData.c_str(), 0);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
VALUE exc = rb_gv_get("$!");
|
|
|
|
if (rb_type(exc) != RUBY_T_NIL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mriBindingExecute()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
RbData rbData;
|
2013-10-09 10:30:33 +00:00
|
|
|
shState->setBindingData(&rbData);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
mriBindingInit();
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
std::string &customScript = shState->rtData().config.customScript;
|
|
|
|
if (!customScript.empty())
|
|
|
|
runCustomScript(customScript.c_str());
|
2013-09-01 14:27:21 +00:00
|
|
|
else
|
|
|
|
runRMXPScripts();
|
|
|
|
|
|
|
|
VALUE exc = rb_gv_get("$!");
|
|
|
|
if (rb_type(exc) != RUBY_T_NIL && !rb_eql(rb_obj_class(exc), rb_eSystemExit))
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
Debug() << "Had exception:" << rb_class2name(rb_obj_class(exc));
|
2013-09-01 14:27:21 +00:00
|
|
|
VALUE bt = rb_funcall(exc, rb_intern("backtrace"), 0);
|
|
|
|
rb_p(bt);
|
|
|
|
VALUE msg = rb_funcall(exc, rb_intern("message"), 0);
|
|
|
|
if (RSTRING_LEN(msg) < 256)
|
|
|
|
showMsg(RSTRING_PTR(msg));
|
|
|
|
else
|
2013-12-11 19:46:54 +00:00
|
|
|
Debug() << (RSTRING_PTR(msg));
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ruby_cleanup(0);
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
shState->rtData().rqTermAck = true;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mriBindingTerminate()
|
|
|
|
{
|
|
|
|
rb_raise(rb_eSystemExit, " ");
|
|
|
|
}
|