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); }
static void runRMXPScripts(BacktraceData &btData) { const Config &conf = shState->rtData().config; const std::string &scriptPack = conf.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; /* We checked if Scripts.rxdata exists, but something might * still go wrong */ try { scriptArray = kernelLoadDataInt(scriptPack.c_str(), false); } catch (const Exception &e) { showMsg(std::string("Failed to read script data: ") + e.msg); return; } if (!RB_TYPE_P(scriptArray, RUBY_T_ARRAY)) { showMsg("Failed to read script data"); return; } rb_gv_set("$RGSS_SCRIPTS", scriptArray); long scriptCount = RARRAY_LEN(scriptArray); std::string decodeBuffer; decodeBuffer.resize(0x1000); for (long i = 0; i < scriptCount; ++i) { VALUE script = rb_ary_entry(scriptArray, i); if (!RB_TYPE_P(script, RUBY_T_ARRAY)) continue; VALUE scriptName = rb_ary_entry(script, 1); VALUE scriptString = rb_ary_entry(script, 2); int result = Z_OK; unsigned long bufferLen; while (true) { unsigned char *bufferPtr = reinterpret_cast<unsigned char*>(const_cast<char*>(decodeBuffer.c_str())); const unsigned char *sourcePtr = reinterpret_cast<const 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 %ld: '%s'", i, RSTRING_PTR(scriptName)); showMsg(buffer); break; } rb_ary_store(script, 3, rb_str_new_cstr(decodeBuffer.c_str())); } /* Execute preloaded scripts */ for (std::set<std::string>::iterator i = conf.preloadScripts.begin(); i != conf.preloadScripts.end(); ++i) runCustomScript(*i); VALUE exc = rb_gv_get("$!"); if (exc != Qnil) return; while (true) { for (long i = 0; i < scriptCount; ++i) { VALUE script = rb_ary_entry(scriptArray, i); VALUE scriptDecoded = rb_ary_entry(script, 3); VALUE string = newStringUTF8(RSTRING_PTR(scriptDecoded), RSTRING_LEN(scriptDecoded)); VALUE fname; const char *scriptName = RSTRING_PTR(rb_ary_entry(script, 1)); char buf[512]; int len; if (conf.useScriptNames) len = snprintf(buf, sizeof(buf), "%03ld:%s", i, scriptName); else len = snprintf(buf, sizeof(buf), SCRIPT_SECTION_FMT, i); fname = newStringUTF8(buf, len); btData.scriptNames.insert(buf, scriptName); int state; evalString(string, fname, &state); if (state) break; } VALUE exc = rb_gv_get("$!"); if (rb_obj_class(exc) != getRbData()->exc[Reset]) break; processReset(); } }
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; } rb_gv_set("$RGSS_SCRIPTS", scriptArray); long scriptCount = RARRAY_LEN(scriptArray); std::string decodeBuffer; decodeBuffer.resize(0x1000); for (long i = 0; i < scriptCount; ++i) { VALUE script = rb_ary_entry(scriptArray, i); if (rb_type(script) != RUBY_T_ARRAY) continue; VALUE scriptName = rb_ary_entry(script, 1); VALUE scriptString = rb_ary_entry(script, 2); int result = Z_OK; unsigned long bufferLen; while (true) { unsigned char *bufferPtr = reinterpret_cast<unsigned char*>(const_cast<char*>(decodeBuffer.c_str())); const unsigned char *sourcePtr = reinterpret_cast<const 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 %ld: '%s'", i, RSTRING_PTR(scriptName)); showMsg(buffer); break; } 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); VALUE string = newStringUTF8(RSTRING_PTR(scriptDecoded), RSTRING_LEN(scriptDecoded)); VALUE fname; if (shState->rtData().config.useScriptNames) { fname = rb_ary_entry(script, 1); } else { char buf[32]; int len = snprintf(buf, sizeof(buf), RGSS_SECTION_STR, i); fname = newStringUTF8(buf, len); } int state; evalString(string, fname, &state); if (state) break; } }