From b39964a49abd488fb8d8a178c152a9b740538e11 Mon Sep 17 00:00:00 2001 From: Jonas Kulla Date: Tue, 23 Dec 2014 19:05:08 +0100 Subject: [PATCH] MRI: Make error handling during script load more robust --- binding-mri/binding-mri.cpp | 16 ++++++++++++++-- binding-mri/filesystem-binding.cpp | 14 +++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp index c64c1a6..b695c48 100644 --- a/binding-mri/binding-mri.cpp +++ b/binding-mri/binding-mri.cpp @@ -346,7 +346,7 @@ static void runCustomScript(const std::string &filename) newStringUTF8(filename.c_str(), filename.size()), NULL); } -VALUE kernelLoadDataInt(const char *filename); +VALUE kernelLoadDataInt(const char *filename, bool rubyExc); struct BacktraceData { @@ -373,7 +373,19 @@ static void runRMXPScripts(BacktraceData &btData) return; } - VALUE scriptArray = kernelLoadDataInt(scriptPack.c_str()); + 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)) { diff --git a/binding-mri/filesystem-binding.cpp b/binding-mri/filesystem-binding.cpp index 050f25c..17cf50d 100644 --- a/binding-mri/filesystem-binding.cpp +++ b/binding-mri/filesystem-binding.cpp @@ -40,7 +40,7 @@ fileIntFreeInstance(void *inst) DEF_TYPE_CUSTOMFREE(FileInt, fileIntFreeInstance); static VALUE -fileIntForPath(const char *path) +fileIntForPath(const char *path, bool rubyExc) { SDL_RWops *ops = SDL_AllocRW(); @@ -51,7 +51,11 @@ fileIntForPath(const char *path) catch (const Exception &e) { SDL_FreeRW(ops); - raiseRbExc(e); + + if (rubyExc) + raiseRbExc(e); + else + throw e; } VALUE klass = rb_const_get(rb_cObject, rb_intern("FileInt")); @@ -119,11 +123,11 @@ RB_METHOD(fileIntBinmode) } VALUE -kernelLoadDataInt(const char *filename) +kernelLoadDataInt(const char *filename, bool rubyExc) { rb_gc_start(); - VALUE port = fileIntForPath(filename); + VALUE port = fileIntForPath(filename, rubyExc); VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal")); @@ -142,7 +146,7 @@ RB_METHOD(kernelLoadData) const char *filename; rb_get_args(argc, argv, "z", &filename RB_ARG_END); - return kernelLoadDataInt(filename); + return kernelLoadDataInt(filename, true); } RB_METHOD(kernelSaveData)