diff --git a/binding/binding-mri.cpp b/binding/binding-mri.cpp index 39d62d2..0aa1c54 100644 --- a/binding/binding-mri.cpp +++ b/binding/binding-mri.cpp @@ -779,9 +779,54 @@ static bool processReset(bool rubyExc) { return 0; } +#if RAPI_FULL > 187 +static VALUE newStringUTF8(const char *string, long length) { + return rb_enc_str_new(string, length, rb_utf8_encoding()); +} +#else +#define newStringUTF8 rb_str_new +#endif + +struct evalArg { + 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); +} + +static void runCustomScript(const std::string &filename) { + std::string scriptData; + + if (!readFileSDL(filename.c_str(), scriptData)) { + showMsg(std::string("Unable to open '") + filename + "'"); + return; + } + + evalString(newStringUTF8(scriptData.c_str(), scriptData.size()), + newStringUTF8(filename.c_str(), filename.size()), NULL); +} + RB_METHOD_GUARD(mriRgssMain) { RB_UNUSED_PARAM; - + + /* Execute postload scripts */ + const Config &conf = shState->rtData().config; + for (std::vector::const_iterator i = conf.postloadScripts.begin(); + i != conf.postloadScripts.end(); ++i) + { + if (shState->rtData().rqTerm) + break; + runCustomScript(*i); + } + while (true) { VALUE exc = Qnil; #if RAPI_FULL < 270 @@ -848,41 +893,6 @@ RB_METHOD(_kernelCaller) { return trace; } -#if RAPI_FULL > 187 -static VALUE newStringUTF8(const char *string, long length) { - return rb_enc_str_new(string, length, rb_utf8_encoding()); -} -#else -#define newStringUTF8 rb_str_new -#endif - -struct evalArg { - 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); -} - -static void runCustomScript(const std::string &filename) { - std::string scriptData; - - if (!readFileSDL(filename.c_str(), scriptData)) { - showMsg(std::string("Unable to open '") + filename + "'"); - return; - } - - evalString(newStringUTF8(scriptData.c_str(), scriptData.size()), - newStringUTF8(filename.c_str(), filename.size()), NULL); -} - VALUE kernelLoadDataInt(const char *filename, bool rubyExc, bool raw); struct BacktraceData { diff --git a/mkxp.json b/mkxp.json index 3430db8..91c31c3 100644 --- a/mkxp.json +++ b/mkxp.json @@ -368,6 +368,15 @@ // ], + // Define raw scripts to be executed before the + // rgss_main execution starts for RGSS 3. + // (default: none) + // + // "postloadScript": [ + // "/path/to/script.rb", + // ], + + // Index all accesible assets via their lower case path // (emulates windows case insensitivity) // (default: enabled) diff --git a/src/config.cpp b/src/config.cpp index 6d89d8e..cdd7a29 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -187,6 +187,7 @@ void Config::read(int argc, char *argv[]) { {"pathCache", true}, {"useScriptNames", true}, {"preloadScript", json::array({})}, + {"postloadScript", json::array({})}, {"RTP", json::array({})}, {"patches", json::array({})}, {"fontSub", json::array({})}, @@ -316,6 +317,7 @@ try { exp } catch (...) {} SET_OPT(dumpAtlas, boolean); fillStringVec(opts["preloadScript"], preloadScripts); + fillStringVec(opts["postloadScript"], postloadScripts); fillStringVec(opts["RTP"], rtps); fillStringVec(opts["patches"], patches); fillStringVec(opts["fontSub"], fontSubs); diff --git a/src/config.h b/src/config.h index 7460577..b3e0cae 100644 --- a/src/config.h +++ b/src/config.h @@ -113,6 +113,7 @@ struct Config { std::vector launchArgs; std::vector preloadScripts; + std::vector postloadScripts; std::vector rtps; std::vector patches;