From 804a697c03b19574eb277debbb754a5810aa6710 Mon Sep 17 00:00:00 2001 From: cremno Date: Wed, 9 Apr 2014 22:36:56 +0200 Subject: [PATCH 1/7] MRI: add $RGSS_SCRIPTS --- binding-mri/binding-mri.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp index 0160757..22f6eb3 100644 --- a/binding-mri/binding-mri.cpp +++ b/binding-mri/binding-mri.cpp @@ -210,6 +210,8 @@ static void runRMXPScripts() return; } + rb_gv_set("$RGSS_SCRIPTS", scriptArray); + long scriptCount = RARRAY_LEN(scriptArray); std::string decodeBuffer; @@ -259,6 +261,8 @@ static void runRMXPScripts() break; } + rb_ary_store(script, 3, rb_str_new_cstr(decodeBuffer.c_str())); + /* Store encoding header + the decoded script * in 'sc.decData' */ std::string decData = "#encoding:utf-8\n"; -- 2.43.0 From f8b87eb1885b366aba00fa2d432a9556619529dd Mon Sep 17 00:00:00 2001 From: cremno Date: Wed, 9 Apr 2014 23:45:24 +0200 Subject: [PATCH 2/7] MRI: don't manually peform GC --- binding-mri/binding-mri.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp index 22f6eb3..e3dbcf5 100644 --- a/binding-mri/binding-mri.cpp +++ b/binding-mri/binding-mri.cpp @@ -272,8 +272,6 @@ static void runRMXPScripts() ruby_script(RSTRING_PTR(scriptName)); - rb_gc_start(); - /* Execute code */ rb_eval_string_protect(decData.c_str(), 0); -- 2.43.0 From 4e0262d2a797448280d405cf41a12d70ecf72e3d Mon Sep 17 00:00:00 2001 From: cremno Date: Thu, 10 Apr 2014 01:16:31 +0200 Subject: [PATCH 3/7] MRI: fix $RGSS_SCRIPTS --- binding-mri/binding-mri.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp index e3dbcf5..189368a 100644 --- a/binding-mri/binding-mri.cpp +++ b/binding-mri/binding-mri.cpp @@ -262,15 +262,24 @@ static void runRMXPScripts() } rb_ary_store(script, 3, rb_str_new_cstr(decodeBuffer.c_str())); + } + + for (long i = 0; i < scriptCount; ++i) + { + VALUE script = rb_ary_entry(scriptArray, i); + VALUE scriptDecoded = rb_ary_entry(script, 3); /* Store encoding header + the decoded script * in 'sc.decData' */ std::string decData = "#encoding:utf-8\n"; size_t hdSize = decData.size(); - decData.resize(hdSize + bufferLen); - memcpy(&decData[hdSize], decodeBuffer.c_str(), bufferLen); + const char *scriptDecPtr; + long scriptDecLen; + RSTRING_GETMEM(scriptDecoded, scriptDecPtr, scriptDecLen); + decData.resize(hdSize + scriptDecLen); + memcpy(&decData[hdSize], scriptDecPtr, scriptDecLen); - ruby_script(RSTRING_PTR(scriptName)); + ruby_script(RSTRING_PTR(rb_ary_entry(script, 1))); /* Execute code */ rb_eval_string_protect(decData.c_str(), 0); @@ -281,6 +290,7 @@ static void runRMXPScripts() } } + static void mriBindingExecute() { ruby_setup(); -- 2.43.0 From 06b877a78c9fd6399438798710f85dd8ccfc322e Mon Sep 17 00:00:00 2001 From: cremno Date: Thu, 10 Apr 2014 02:14:25 +0200 Subject: [PATCH 4/7] MRI: don't set $0 (see #24) --- binding-mri/binding-mri.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp index 189368a..56391b5 100644 --- a/binding-mri/binding-mri.cpp +++ b/binding-mri/binding-mri.cpp @@ -279,8 +279,6 @@ static void runRMXPScripts() decData.resize(hdSize + scriptDecLen); memcpy(&decData[hdSize], scriptDecPtr, scriptDecLen); - ruby_script(RSTRING_PTR(rb_ary_entry(script, 1))); - /* Execute code */ rb_eval_string_protect(decData.c_str(), 0); @@ -290,7 +288,6 @@ static void runRMXPScripts() } } - static void mriBindingExecute() { ruby_setup(); -- 2.43.0 From f6ec36463261ad11bb95eb5fc220fad66ad8deeb Mon Sep 17 00:00:00 2001 From: cremno Date: Thu, 10 Apr 2014 14:00:05 +0200 Subject: [PATCH 5/7] MRI: rewrite script eval - set __FILE__ (RGSS 1 and 2) - set Ruby string encoding to UTF-8 instead of using a magic comment --- binding-mri/binding-mri.cpp | 51 +++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp index 56391b5..5c6abab 100644 --- a/binding-mri/binding-mri.cpp +++ b/binding-mri/binding-mri.cpp @@ -171,9 +171,36 @@ RB_METHOD(mriDataDirectory) return pathStr; } +static VALUE newStringUTF8(const char *string) +{ + return rb_enc_str_new(string, strlen(string), rb_utf8_encoding()); +} + +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 VALUE evalCString(const char *string, const char *filename, int *state) +{ + return evalString(newStringUTF8(string), newStringUTF8(filename), state); +} + static void runCustomScript(const char *filename) { - std::string scriptData("#encoding:utf-8\n"); + std::string scriptData; if (!readFile(filename, scriptData)) { @@ -181,7 +208,7 @@ static void runCustomScript(const char *filename) return; } - rb_eval_string_protect(scriptData.c_str(), 0); + evalCString(scriptData.c_str(), "Section000", NULL); } VALUE kernelLoadDataInt(const char *filename); @@ -268,22 +295,14 @@ static void runRMXPScripts() { VALUE script = rb_ary_entry(scriptArray, i); VALUE scriptDecoded = rb_ary_entry(script, 3); + const char *string = StringValueCStr(scriptDecoded); - /* Store encoding header + the decoded script - * in 'sc.decData' */ - std::string decData = "#encoding:utf-8\n"; - size_t hdSize = decData.size(); - const char *scriptDecPtr; - long scriptDecLen; - RSTRING_GETMEM(scriptDecoded, scriptDecPtr, scriptDecLen); - decData.resize(hdSize + scriptDecLen); - memcpy(&decData[hdSize], scriptDecPtr, scriptDecLen); + char fname[32]; + snprintf(fname, sizeof(fname), "Section%03ld", i); - /* Execute code */ - rb_eval_string_protect(decData.c_str(), 0); - - VALUE exc = rb_errinfo(); - if (!NIL_P(exc)) + int state; + evalCString(string, fname, &state); + if (state) break; } } -- 2.43.0 From 8bc17a9a988143e47e2178c492c8b48f917889bf Mon Sep 17 00:00:00 2001 From: cremno Date: Fri, 11 Apr 2014 16:40:49 +0200 Subject: [PATCH 6/7] MRI: less `strlen` calls, use custom script's filename --- binding-mri/binding-mri.cpp | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp index 5c6abab..ca61a4f 100644 --- a/binding-mri/binding-mri.cpp +++ b/binding-mri/binding-mri.cpp @@ -171,12 +171,13 @@ RB_METHOD(mriDataDirectory) return pathStr; } -static VALUE newStringUTF8(const char *string) +static VALUE newStringUTF8(const char *string, long length) { - return rb_enc_str_new(string, strlen(string), rb_utf8_encoding()); + return rb_enc_str_new(string, length, rb_utf8_encoding()); } -struct evalArg { +struct evalArg +{ VALUE string; VALUE filename; }; @@ -193,22 +194,18 @@ static VALUE evalString(VALUE string, VALUE filename, int *state) return rb_protect((VALUE (*)(VALUE))evalHelper, (VALUE)&arg, state); } -static VALUE evalCString(const char *string, const char *filename, int *state) -{ - return evalString(newStringUTF8(string), newStringUTF8(filename), state); -} - -static void runCustomScript(const char *filename) +static void runCustomScript(const std::string &filename) { std::string scriptData; - if (!readFile(filename, scriptData)) + if (!readFile(filename.c_str(), scriptData)) { showMsg(std::string("Unable to open '") + filename + "'"); return; } - evalCString(scriptData.c_str(), "Section000", NULL); + evalString(newStringUTF8(scriptData.c_str(), scriptData.size()), + newStringUTF8(filename.c_str(), filename.size()), NULL); } VALUE kernelLoadDataInt(const char *filename); @@ -295,13 +292,14 @@ static void runRMXPScripts() { VALUE script = rb_ary_entry(scriptArray, i); VALUE scriptDecoded = rb_ary_entry(script, 3); - const char *string = StringValueCStr(scriptDecoded); + VALUE string = newStringUTF8(RSTRING_PTR(scriptDecoded), + RSTRING_LEN(scriptDecoded)); char fname[32]; - snprintf(fname, sizeof(fname), "Section%03ld", i); + int len = snprintf(fname, sizeof(fname), "Section%03ld", i); int state; - evalCString(string, fname, &state); + evalString(string, newStringUTF8(fname, len), &state); if (state) break; } @@ -319,7 +317,7 @@ static void mriBindingExecute() std::string &customScript = shState->rtData().config.customScript; if (!customScript.empty()) - runCustomScript(customScript.c_str()); + runCustomScript(customScript); else runRMXPScripts(); -- 2.43.0 From 95c112bbe4c849db5cec2af571620ff44cb9c4b2 Mon Sep 17 00:00:00 2001 From: cremno Date: Fri, 11 Apr 2014 18:11:32 +0200 Subject: [PATCH 7/7] MRI: add option to use a script's name as filename --- binding-mri/binding-mri.cpp | 15 ++++++++++++--- mkxp.conf.sample | 6 ++++++ src/config.cpp | 6 ++++-- src/config.h | 2 ++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp index ca61a4f..34482df 100644 --- a/binding-mri/binding-mri.cpp +++ b/binding-mri/binding-mri.cpp @@ -295,11 +295,20 @@ static void runRMXPScripts() VALUE string = newStringUTF8(RSTRING_PTR(scriptDecoded), RSTRING_LEN(scriptDecoded)); - char fname[32]; - int len = snprintf(fname, sizeof(fname), "Section%03ld", i); + VALUE fname; + if (shState->rtData().config.useScriptNames) + { + fname = rb_ary_entry(script, 1); + } + else + { + char buf[32]; + int len = snprintf(buf, sizeof(buf), "Section%03ld", i); + fname = newStringUTF8(buf, len); + } int state; - evalString(string, newStringUTF8(fname, len), &state); + evalString(string, fname, &state); if (state) break; } diff --git a/mkxp.conf.sample b/mkxp.conf.sample index 11fb146..cfe7268 100644 --- a/mkxp.conf.sample +++ b/mkxp.conf.sample @@ -113,3 +113,9 @@ # RTP=/path/to/rtp1 # RTP=/path/to/rtp2.zip # RTP=/path/to/game.rgssad + + +# Use the script's name as filename in warnings and error messages +# (default: disabled) +# +# useScriptNames=false diff --git a/src/config.cpp b/src/config.cpp index 7e3091f..fcde0c0 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -49,7 +49,8 @@ Config::Config() gameFolder("."), anyAltToggleFS(false), allowSymlinks(false), - pathCache(true) + pathCache(true), + useScriptNames(false) {} void Config::read(int argc, char *argv[]) @@ -72,7 +73,8 @@ void Config::read(int argc, char *argv[]) PO_DESC(allowSymlinks, bool) \ PO_DESC(iconPath, std::string) \ PO_DESC(customScript, std::string) \ - PO_DESC(pathCache, bool) + PO_DESC(pathCache, bool) \ + PO_DESC(useScriptNames, bool) // Not gonna take your shit boost #define GUARD_ALL( exp ) try { exp } catch(...) {} diff --git a/src/config.h b/src/config.h index 411ce51..d91e13a 100644 --- a/src/config.h +++ b/src/config.h @@ -51,6 +51,8 @@ struct Config std::string iconPath; + bool useScriptNames; + std::string customScript; std::vector rtps; -- 2.43.0