2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** binding-mruby.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"
|
|
|
|
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <mruby.h>
|
|
|
|
#include <mruby/string.h>
|
|
|
|
#include <mruby/array.h>
|
|
|
|
#include <mruby/class.h>
|
|
|
|
#include <mruby/irep.h>
|
|
|
|
#include <mruby/compile.h>
|
|
|
|
#include <mruby/proc.h>
|
|
|
|
#include <mruby/dump.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <zlib.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
#include <string>
|
|
|
|
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <SDL_messagebox.h>
|
|
|
|
#include <SDL_rwops.h>
|
|
|
|
#include <SDL_timer.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
#include "sharedstate.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "texpool.h"
|
|
|
|
#include "eventthread.h"
|
|
|
|
#include "filesystem.h"
|
2013-09-10 02:21:41 +00:00
|
|
|
#include "exception.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
#include "binding-util.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "binding-types.h"
|
|
|
|
#include "mrb-ext/marshal.h"
|
2020-05-04 07:43:57 +00:00
|
|
|
#include "mrb-ext/file-helper.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-09-28 19:27:11 +00:00
|
|
|
static void mrbBindingExecute();
|
|
|
|
static void mrbBindingTerminate();
|
2014-08-24 05:36:19 +00:00
|
|
|
static void mrbBindingReset();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
ScriptBinding scriptBindingImpl =
|
|
|
|
{
|
|
|
|
mrbBindingExecute,
|
2014-08-24 05:36:19 +00:00
|
|
|
mrbBindingTerminate,
|
|
|
|
mrbBindingReset
|
2013-09-01 14:27:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ScriptBinding *scriptBinding = &scriptBindingImpl;
|
|
|
|
|
|
|
|
void kernelBindingInit(mrb_state *);
|
|
|
|
|
|
|
|
void tableBindingInit(mrb_state *);
|
|
|
|
void etcBindingInit(mrb_state *);
|
|
|
|
void fontBindingInit(mrb_state *);
|
|
|
|
void bitmapBindingInit(mrb_state *);
|
|
|
|
void spriteBindingInit(mrb_state *);
|
|
|
|
void planeBindingInit(mrb_state *);
|
|
|
|
void viewportBindingInit(mrb_state *);
|
|
|
|
void windowBindingInit(mrb_state *);
|
|
|
|
void tilemapBindingInit(mrb_state *);
|
|
|
|
|
|
|
|
void inputBindingInit(mrb_state *);
|
|
|
|
void audioBindingInit(mrb_state *);
|
|
|
|
void graphicsBindingInit(mrb_state *);
|
|
|
|
|
|
|
|
static void mrbBindingInit(mrb_state *mrb)
|
|
|
|
{
|
|
|
|
int arena = mrb_gc_arena_save(mrb);
|
|
|
|
|
|
|
|
/* Init standard classes */
|
|
|
|
kernelBindingInit(mrb);
|
|
|
|
|
|
|
|
/* Init RGSS classes */
|
|
|
|
tableBindingInit(mrb);
|
|
|
|
etcBindingInit(mrb);
|
|
|
|
fontBindingInit(mrb);
|
|
|
|
bitmapBindingInit(mrb);
|
|
|
|
spriteBindingInit(mrb);
|
|
|
|
planeBindingInit(mrb);
|
|
|
|
viewportBindingInit(mrb);
|
|
|
|
windowBindingInit(mrb);
|
|
|
|
tilemapBindingInit(mrb);
|
|
|
|
|
|
|
|
/* Init RGSS modules */
|
|
|
|
inputBindingInit(mrb);
|
|
|
|
audioBindingInit(mrb);
|
|
|
|
graphicsBindingInit(mrb);
|
|
|
|
|
2018-02-22 07:37:47 +00:00
|
|
|
/* Load global constants */
|
2013-09-01 14:27:21 +00:00
|
|
|
mrb_define_global_const(mrb, "MKXP", mrb_true_value());
|
|
|
|
|
2020-05-04 07:43:57 +00:00
|
|
|
mrb_value debug = mrb_bool_value(shState->config().editor.debug);
|
2018-02-22 07:37:47 +00:00
|
|
|
if (rgssVer == 1)
|
|
|
|
mrb_define_global_const(mrb, "DEBUG", debug);
|
|
|
|
else if (rgssVer >= 2)
|
|
|
|
mrb_define_global_const(mrb, "TEST", debug);
|
|
|
|
|
|
|
|
mrb_define_global_const(mrb, "BTEST", mrb_bool_value(shState->config().editor.battleTest));
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
mrb_gc_arena_restore(mrb, arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
static mrb_value
|
|
|
|
mkxpTimeOp(mrb_state *mrb, mrb_value)
|
|
|
|
{
|
2013-09-02 09:15:12 +00:00
|
|
|
mrb_int iterations = 1;
|
2013-09-02 09:21:06 +00:00
|
|
|
const char *opName = "";
|
2013-09-01 14:27:21 +00:00
|
|
|
mrb_value block;
|
|
|
|
|
2013-09-02 09:15:12 +00:00
|
|
|
mrb_get_args(mrb, "|iz&", &iterations, &opName, &block);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
Uint64 start = SDL_GetPerformanceCounter();
|
|
|
|
|
2013-09-02 09:15:12 +00:00
|
|
|
for (int i = 0; i < iterations; ++i)
|
|
|
|
mrb_yield(mrb, block, mrb_nil_value());
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
Uint64 diff = SDL_GetPerformanceCounter() - start;
|
2013-09-02 09:15:12 +00:00
|
|
|
double avg = (double) diff / iterations;
|
|
|
|
|
|
|
|
double sec = avg / SDL_GetPerformanceFrequency();
|
2013-09-01 14:27:21 +00:00
|
|
|
float ms = sec * 1000;
|
|
|
|
|
|
|
|
printf("<%s> [%f ms]\n", opName, ms);
|
|
|
|
fflush(stdout);
|
|
|
|
|
2014-07-19 00:22:22 +00:00
|
|
|
return mrb_float_value(mrb, ms);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
mrbValueString(mrb_value value)
|
|
|
|
{
|
|
|
|
return mrb_string_p(value) ? RSTRING_PTR(value) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
showExcMessageBox(mrb_state *mrb, mrb_value exc)
|
|
|
|
{
|
|
|
|
/* Display actual exception in a message box */
|
|
|
|
mrb_value mesg = mrb_funcall(mrb, exc, "message", 0);
|
2013-12-27 03:36:24 +00:00
|
|
|
mrb_value line = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "line"));
|
|
|
|
mrb_value file = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "file"));
|
2013-09-01 14:27:21 +00:00
|
|
|
const char *excClass = mrb_class_name(mrb, mrb_class(mrb, exc));
|
|
|
|
|
|
|
|
char msgBoxText[512];
|
2014-04-14 07:39:23 +00:00
|
|
|
snprintf(msgBoxText, sizeof(msgBoxText), "Script '%s' line %d: %s occured.\n\n%s",
|
2013-09-01 14:27:21 +00:00
|
|
|
mrbValueString(file), mrb_fixnum(line), excClass, mrbValueString(mesg));
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
shState->eThread().showMessageBox(msgBoxText, SDL_MESSAGEBOX_ERROR);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
checkException(mrb_state *mrb)
|
|
|
|
{
|
|
|
|
if (!mrb->exc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mrb_value exc = mrb_obj_value(mrb->exc);
|
|
|
|
MrbData &mrbData = *getMrbData(mrb);
|
|
|
|
|
|
|
|
/* Check if an actual exception occured, or just a shutdown was called */
|
|
|
|
if (mrb_obj_class(mrb, exc) != mrbData.exc[Shutdown])
|
|
|
|
showExcMessageBox(mrb, exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2013-12-11 19:46:54 +00:00
|
|
|
showError(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
|
|
|
|
runCustomScript(mrb_state *mrb, mrbc_context *ctx, const char *filename)
|
|
|
|
{
|
|
|
|
/* Execute custom script */
|
2014-11-17 06:18:39 +00:00
|
|
|
FILE *f = fopen(filename, "rb");
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
if (!f)
|
|
|
|
{
|
|
|
|
static char buffer[256];
|
|
|
|
snprintf(buffer, sizeof(buffer), "Unable to open script '%s'", filename);
|
|
|
|
showError(buffer);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->filename = strdup(filename);
|
|
|
|
ctx->lineno = 1;
|
|
|
|
|
|
|
|
/* Run code */
|
|
|
|
mrb_load_file_cxt(mrb, f, ctx);
|
|
|
|
|
|
|
|
free(ctx->filename);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2013-09-03 09:11:12 +00:00
|
|
|
static void
|
|
|
|
runMrbFile(mrb_state *mrb, const char *filename)
|
|
|
|
{
|
|
|
|
/* Execute compiled script */
|
2014-11-17 06:18:39 +00:00
|
|
|
FILE *f = fopen(filename, "rb");
|
2013-09-03 09:11:12 +00:00
|
|
|
|
|
|
|
if (!f)
|
|
|
|
{
|
|
|
|
static char buffer[256];
|
|
|
|
snprintf(buffer, sizeof(buffer), "Unable to open compiled script '%s'", filename);
|
|
|
|
showError(buffer);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-27 03:36:24 +00:00
|
|
|
mrb_irep *irep = mrb_read_irep_file(mrb, f);
|
2013-09-03 09:11:12 +00:00
|
|
|
|
2013-12-27 03:36:24 +00:00
|
|
|
if (!irep)
|
2013-09-03 09:11:12 +00:00
|
|
|
{
|
|
|
|
static char buffer[256];
|
|
|
|
snprintf(buffer, sizeof(buffer), "Unable to read compiled script '%s'", filename);
|
|
|
|
showError(buffer);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-27 03:36:24 +00:00
|
|
|
RProc *proc = mrb_proc_new(mrb, irep);
|
|
|
|
mrb_run(mrb, proc, mrb_top_self(mrb));
|
2013-09-03 09:11:12 +00:00
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
static void
|
|
|
|
runRMXPScripts(mrb_state *mrb, mrbc_context *ctx)
|
|
|
|
{
|
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
|
|
|
{
|
|
|
|
showError("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
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
showError(std::string("Unable to open '") + scriptPack + "'");
|
2013-09-01 14:27:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We use a secondary util state to unmarshal the scripts */
|
|
|
|
mrb_state *scriptMrb = mrb_open();
|
|
|
|
|
2013-09-10 02:21:41 +00:00
|
|
|
mrb_value scriptArray = mrb_nil_value();
|
2013-12-11 19:46:54 +00:00
|
|
|
std::string readError;
|
2013-09-10 02:21:41 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-05-04 07:43:57 +00:00
|
|
|
SDL_rw_file_helper fileHelper;
|
|
|
|
fileHelper.filename = scriptPack.c_str();
|
|
|
|
char * contents = fileHelper.read();
|
|
|
|
mrb_value rawdata = mrb_str_new_static(mrb, contents, fileHelper.length);
|
|
|
|
scriptArray = mrb_marshal_load(mrb, rawdata);
|
2013-09-10 02:21:41 +00:00
|
|
|
}
|
|
|
|
catch (const Exception &e)
|
|
|
|
{
|
2013-12-29 17:05:11 +00:00
|
|
|
readError = std::string(": ") + e.msg;
|
2013-09-10 02:21:41 +00:00
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
if (!mrb_array_p(scriptArray))
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
showError(std::string("Failed to read script data") + readError);
|
2013-09-01 14:27:21 +00:00
|
|
|
mrb_close(scriptMrb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-04 07:43:57 +00:00
|
|
|
int 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);
|
|
|
|
|
|
|
|
for (int i = 0; i < scriptCount; ++i)
|
|
|
|
{
|
|
|
|
mrb_value script = mrb_ary_entry(scriptArray, i);
|
|
|
|
|
|
|
|
mrb_value scriptChksum = mrb_ary_entry(script, 0);
|
|
|
|
mrb_value scriptName = mrb_ary_entry(script, 1);
|
|
|
|
mrb_value scriptString = mrb_ary_entry(script, 2);
|
|
|
|
|
|
|
|
(void) scriptChksum;
|
|
|
|
|
|
|
|
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
|
|
|
unsigned char *sourcePtr =
|
|
|
|
reinterpret_cast<unsigned char*>(RSTRING_PTR(scriptString));
|
|
|
|
|
|
|
|
bufferLen = decodeBuffer.length();
|
|
|
|
|
|
|
|
result = uncompress(bufferPtr, &bufferLen,
|
|
|
|
sourcePtr, RSTRING_LEN(scriptString));
|
|
|
|
|
|
|
|
bufferPtr[bufferLen] = '\0';
|
|
|
|
|
|
|
|
if (result != Z_BUF_ERROR)
|
|
|
|
break;
|
|
|
|
|
|
|
|
decodeBuffer.resize(decodeBuffer.size()*2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result != Z_OK)
|
|
|
|
{
|
|
|
|
static char buffer[256];
|
|
|
|
snprintf(buffer, sizeof(buffer), "Error decoding script %d: '%s'",
|
|
|
|
i, RSTRING_PTR(scriptName));
|
|
|
|
|
|
|
|
showError(buffer);
|
2014-01-01 11:56:45 +00:00
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->filename = RSTRING_PTR(scriptName);
|
|
|
|
ctx->lineno = 1;
|
|
|
|
|
|
|
|
int ai = mrb_gc_arena_save(mrb);
|
|
|
|
|
|
|
|
/* Execute code */
|
2013-12-11 19:46:54 +00:00
|
|
|
mrb_load_nstring_cxt(mrb, decodeBuffer.c_str(), bufferLen, ctx);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
mrb_gc_arena_restore(mrb, ai);
|
|
|
|
|
2020-05-04 07:43:57 +00:00
|
|
|
if (mrb->exc) {
|
|
|
|
printf("%s - err\n", ctx->filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-05-04 17:21:21 +00:00
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
mrb_close(scriptMrb);
|
|
|
|
}
|
|
|
|
|
2013-09-28 19:27:11 +00:00
|
|
|
static void mrbBindingExecute()
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
mrb_state *mrb = mrb_open();
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
shState->setBindingData(mrb);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2020-05-04 07:43:57 +00:00
|
|
|
MrbData * mrbData = new MrbData(mrb);
|
|
|
|
mrb->ud = mrbData;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-09-02 09:15:12 +00:00
|
|
|
mrb_define_module_function(mrb, mrb->kernel_module, "time_op",
|
|
|
|
mkxpTimeOp, MRB_ARGS_OPT(2) | MRB_ARGS_BLOCK());
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
mrbBindingInit(mrb);
|
|
|
|
|
2020-05-04 17:21:21 +00:00
|
|
|
mrbc_context * ctx = mrbc_context_new(mrb);
|
|
|
|
ctx->capture_errors = 1;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
const Config &conf = shState->rtData().config;
|
|
|
|
const std::string &customScript = conf.customScript;
|
|
|
|
(void) runMrbFile; // FIXME mrbFile support on ice for now
|
2013-09-03 09:11:12 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
if (!customScript.empty())
|
2020-05-04 17:21:21 +00:00
|
|
|
runCustomScript(mrb, ctx, customScript.c_str());
|
2013-09-01 14:27:21 +00:00
|
|
|
else
|
2020-05-04 17:21:21 +00:00
|
|
|
runRMXPScripts(mrb, ctx);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
checkException(mrb);
|
|
|
|
|
2014-08-24 05:36:19 +00:00
|
|
|
shState->rtData().rqTermAck.set();
|
2013-10-09 10:30:33 +00:00
|
|
|
shState->texPool().disable();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
mrbc_context_free(mrb, ctx);
|
|
|
|
mrb_close(mrb);
|
|
|
|
}
|
|
|
|
|
2013-09-28 19:27:11 +00:00
|
|
|
static void mrbBindingTerminate()
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-10-09 10:30:33 +00:00
|
|
|
mrb_state *mrb = static_cast<mrb_state*>(shState->bindingData());
|
2013-09-01 14:27:21 +00:00
|
|
|
MrbData *data = static_cast<MrbData*>(mrb->ud);
|
|
|
|
|
|
|
|
mrb_raise(mrb, data->exc[Shutdown], "");
|
|
|
|
}
|
2014-08-24 05:36:19 +00:00
|
|
|
|
|
|
|
static void mrbBindingReset()
|
|
|
|
{
|
|
|
|
// No idea how to do this with mruby yet
|
|
|
|
}
|