/* ** binding-mri.cpp ** ** This file is part of mkxp. ** ** Copyright (C) 2013 Jonas Kulla ** ** 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 . */ #include "binding.h" #include "binding-util.h" #include "sharedstate.h" #include "eventthread.h" #include "filesystem.h" #include "util.h" #include "debugwriter.h" #include #include #include #include #include extern const char module_rpg[]; static void mriBindingExecute(); static void mriBindingTerminate(); ScriptBinding scriptBindingImpl = { mriBindingExecute, mriBindingTerminate }; 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(); RB_METHOD(mriPrint); RB_METHOD(mriP); RB_METHOD(mriDataDirectory); RB_METHOD(mkxpPuts); 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); VALUE mod = rb_define_module("System"); _rb_define_module_function(mod, "data_directory", mriDataDirectory); _rb_define_module_function(mod, "puts", mkxpPuts); rb_define_global_const("MKXP", Qtrue); } static void showMsg(const std::string &msg) { shState->eThread().showMessageBox(msg.c_str()); } RB_METHOD(mkxpPuts) { RB_UNUSED_PARAM; const char *str; rb_get_args(argc, argv, "z", &str RB_ARG_END); Debug() << str; return Qnil; } 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; } RB_METHOD(mriDataDirectory) { RB_UNUSED_PARAM; const char *org, *app; rb_get_args(argc, argv, "zz", &org, &app RB_ARG_END); char *path = SDL_GetPrefPath(org, app); VALUE pathStr = rb_str_new_cstr(path); SDL_free(path); return pathStr; } static void runCustomScript(const char *filename) { std::string scriptData("#encoding:utf-8\n"); if (!readFile(filename, scriptData)) { showMsg(std::string("Unable to open '") + filename + "'"); return; } rb_eval_string_protect(scriptData.c_str(), 0); } VALUE kernelLoadDataInt(const char *filename); struct Script { std::string name; std::string encData; uint32_t unknown; std::string decData; }; static void runRMXPScripts() { const std::string &scriptPack = shState->rtData().config.game.scripts; if (scriptPack.empty()) { showMsg("No game scripts specified (missing Game.ini?)"); return; } if (!shState->fileSystem().exists(scriptPack.c_str())) { showMsg("Unable to open '" + scriptPack + "'"); return; } VALUE scriptArray = kernelLoadDataInt(scriptPack.c_str()); if (rb_type(scriptArray) != RUBY_T_ARRAY) { showMsg("Failed to read script data"); return; } size_t scriptCount = RARRAY_LEN(scriptArray); std::string decodeBuffer; decodeBuffer.resize(0x1000); std::vector