Skip to content

Commit 711f9a4

Browse files
Merge pull request mkxp-z#158 from Nathan-MV/plugin-script
Postload Scripts
2 parents cd8f1c2 + 17f9ec9 commit 711f9a4

File tree

4 files changed

+58
-36
lines changed

4 files changed

+58
-36
lines changed

binding/binding-mri.cpp

+46-36
Original file line numberDiff line numberDiff line change
@@ -811,9 +811,54 @@ static bool processReset(bool rubyExc) {
811811
return 0;
812812
}
813813

814+
#if RAPI_FULL > 187
815+
static VALUE newStringUTF8(const char *string, long length) {
816+
return rb_enc_str_new(string, length, rb_utf8_encoding());
817+
}
818+
#else
819+
#define newStringUTF8 rb_str_new
820+
#endif
821+
822+
struct evalArg {
823+
VALUE string;
824+
VALUE filename;
825+
};
826+
827+
static VALUE evalHelper(evalArg *arg) {
828+
VALUE argv[] = {arg->string, Qnil, arg->filename};
829+
return rb_funcall2(Qnil, rb_intern("eval"), ARRAY_SIZE(argv), argv);
830+
}
831+
832+
static VALUE evalString(VALUE string, VALUE filename, int *state) {
833+
evalArg arg = {string, filename};
834+
return rb_protect((VALUE(*)(VALUE))evalHelper, (VALUE)&arg, state);
835+
}
836+
837+
static void runCustomScript(const std::string &filename) {
838+
std::string scriptData;
839+
840+
if (!readFileSDL(filename.c_str(), scriptData)) {
841+
showMsg(std::string("Unable to open '") + filename + "'");
842+
return;
843+
}
844+
845+
evalString(newStringUTF8(scriptData.c_str(), scriptData.size()),
846+
newStringUTF8(filename.c_str(), filename.size()), NULL);
847+
}
848+
814849
RB_METHOD_GUARD(mriRgssMain) {
815850
RB_UNUSED_PARAM;
816-
851+
852+
/* Execute postload scripts */
853+
const Config &conf = shState->rtData().config;
854+
for (std::vector<std::string>::const_iterator i = conf.postloadScripts.begin();
855+
i != conf.postloadScripts.end(); ++i)
856+
{
857+
if (shState->rtData().rqTerm)
858+
break;
859+
runCustomScript(*i);
860+
}
861+
817862
while (true) {
818863
VALUE exc = Qnil;
819864
#if RAPI_FULL < 270
@@ -880,41 +925,6 @@ RB_METHOD(_kernelCaller) {
880925
return trace;
881926
}
882927

883-
#if RAPI_FULL > 187
884-
static VALUE newStringUTF8(const char *string, long length) {
885-
return rb_enc_str_new(string, length, rb_utf8_encoding());
886-
}
887-
#else
888-
#define newStringUTF8 rb_str_new
889-
#endif
890-
891-
struct evalArg {
892-
VALUE string;
893-
VALUE filename;
894-
};
895-
896-
static VALUE evalHelper(evalArg *arg) {
897-
VALUE argv[] = {arg->string, Qnil, arg->filename};
898-
return rb_funcall2(Qnil, rb_intern("eval"), ARRAY_SIZE(argv), argv);
899-
}
900-
901-
static VALUE evalString(VALUE string, VALUE filename, int *state) {
902-
evalArg arg = {string, filename};
903-
return rb_protect((VALUE(*)(VALUE))evalHelper, (VALUE)&arg, state);
904-
}
905-
906-
static void runCustomScript(const std::string &filename) {
907-
std::string scriptData;
908-
909-
if (!readFileSDL(filename.c_str(), scriptData)) {
910-
showMsg(std::string("Unable to open '") + filename + "'");
911-
return;
912-
}
913-
914-
evalString(newStringUTF8(scriptData.c_str(), scriptData.size()),
915-
newStringUTF8(filename.c_str(), filename.size()), NULL);
916-
}
917-
918928
VALUE kernelLoadDataInt(const char *filename, bool rubyExc, bool raw);
919929

920930
struct BacktraceData {

mkxp.json

+9
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,15 @@
368368
// ],
369369

370370

371+
// Define raw scripts to be executed before the
372+
// rgss_main execution starts for RGSS 3.
373+
// (default: none)
374+
//
375+
// "postloadScript": [
376+
// "/path/to/script.rb",
377+
// ],
378+
379+
371380
// Index all accesible assets via their lower case path
372381
// (emulates windows case insensitivity)
373382
// (default: enabled)

src/config.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ void Config::read(int argc, char *argv[]) {
187187
{"pathCache", true},
188188
{"useScriptNames", true},
189189
{"preloadScript", json::array({})},
190+
{"postloadScript", json::array({})},
190191
{"RTP", json::array({})},
191192
{"patches", json::array({})},
192193
{"fontSub", json::array({})},
@@ -316,6 +317,7 @@ try { exp } catch (...) {}
316317
SET_OPT(dumpAtlas, boolean);
317318

318319
fillStringVec(opts["preloadScript"], preloadScripts);
320+
fillStringVec(opts["postloadScript"], postloadScripts);
319321
fillStringVec(opts["RTP"], rtps);
320322
fillStringVec(opts["patches"], patches);
321323
fillStringVec(opts["fontSub"], fontSubs);

src/config.h

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ struct Config {
113113

114114
std::vector<std::string> launchArgs;
115115
std::vector<std::string> preloadScripts;
116+
std::vector<std::string> postloadScripts;
116117
std::vector<std::string> rtps;
117118
std::vector<std::string> patches;
118119

0 commit comments

Comments
 (0)