Adapt to use take-cheeze's marshal mrbgem

This commit is contained in:
Varun Patil 2018-05-05 07:56:53 +05:30
parent 737d5add01
commit bd75100d24
6 changed files with 82 additions and 1079 deletions

View File

@ -48,6 +48,7 @@
#include "binding-util.h" #include "binding-util.h"
#include "binding-types.h" #include "binding-types.h"
#include "mrb-ext/marshal.h" #include "mrb-ext/marshal.h"
#include "mrb-ext/file-helper.h"
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
#include <emscripten.h> #include <emscripten.h>
@ -69,7 +70,6 @@ ScriptBinding *scriptBinding = &scriptBindingImpl;
void fileBindingInit(mrb_state *); void fileBindingInit(mrb_state *);
void timeBindingInit(mrb_state *); void timeBindingInit(mrb_state *);
void marshalBindingInit(mrb_state *);
void kernelBindingInit(mrb_state *); void kernelBindingInit(mrb_state *);
void tableBindingInit(mrb_state *); void tableBindingInit(mrb_state *);
@ -96,7 +96,6 @@ static void mrbBindingInit(mrb_state *mrb)
/* Init standard classes */ /* Init standard classes */
fileBindingInit(mrb); fileBindingInit(mrb);
timeBindingInit(mrb); timeBindingInit(mrb);
marshalBindingInit(mrb);
kernelBindingInit(mrb); kernelBindingInit(mrb);
/* Init RGSS classes */ /* Init RGSS classes */
@ -263,12 +262,14 @@ static mrb_state * static_scriptmrb;
void __attribute__ ((optnone)) main_update_loop() { void __attribute__ ((optnone)) main_update_loop() {
mrb_load_nstring_cxt(static_mrb, "main_update_loop", 16, NULL); mrb_load_nstring_cxt(static_mrb, "main_update_loop", 16, NULL);
#ifdef __EMSCRIPTEN__
if (static_mrb->exc) { if (static_mrb->exc) {
printf("Execution Errored\n"); printf("Execution Errored\n");
mrb_close(static_scriptmrb); mrb_close(static_scriptmrb);
shState->texPool().disable(); shState->texPool().disable();
mrb_close(static_mrb); mrb_close(static_mrb);
} }
#endif
} }
static void static void
@ -290,24 +291,23 @@ runRMXPScripts(mrb_state *mrb, mrbc_context *ctx)
/* We use a secondary util state to unmarshal the scripts */ /* We use a secondary util state to unmarshal the scripts */
mrb_state *scriptMrb = mrb_open(); mrb_state *scriptMrb = mrb_open();
SDL_RWops ops;
shState->fileSystem().openReadRaw(ops, scriptPack.c_str());
mrb_value scriptArray = mrb_nil_value(); mrb_value scriptArray = mrb_nil_value();
std::string readError; std::string readError;
try try
{ {
scriptArray = marshalLoadInt(scriptMrb, &ops); SDL_rw_file_helper fileHelper;
fileHelper.filename = scriptPack.c_str();
char * contents = fileHelper.read();
mrb_value rawdata = mrb_str_new_static(mrb, contents, fileHelper.length);
scriptArray = mrb_marshal_load(mrb, rawdata);
} }
catch (const Exception &e) catch (const Exception &e)
{ {
readError = std::string(": ") + e.msg; readError = std::string(": ") + e.msg;
} }
SDL_RWclose(&ops);
if (!mrb_array_p(scriptArray)) if (!mrb_array_p(scriptArray))
{ {
showError(std::string("Failed to read script data") + readError); showError(std::string("Failed to read script data") + readError);
@ -389,7 +389,7 @@ runRMXPScripts(mrb_state *mrb, mrbc_context *ctx)
#else #else
while (true) { while (true) {
main_update_loop(); main_update_loop();
SDL_Delay(20); SDL_Delay(3);
if (static_mrb->exc) if (static_mrb->exc)
break; break;
} }

View File

@ -0,0 +1,41 @@
#include "file-helper.h"
#include <SDL_rwops.h>
char* SDL_rw_file_helper::read() {
SDL_RWops *rw = SDL_RWFromFile(filename, "rb");
if (rw == NULL) return NULL;
Sint64 res_size = SDL_RWsize(rw);
char* res = (char*)malloc(res_size + 1);
Sint64 nb_read_total = 0, nb_read = 1;
char* buf = res;
while (nb_read_total < res_size && nb_read != 0) {
nb_read = SDL_RWread(rw, buf, 1, (res_size - nb_read_total));
nb_read_total += nb_read;
buf += nb_read;
}
SDL_RWclose(rw);
if (nb_read_total != res_size) {
free(res);
return NULL;
}
res[nb_read_total] = '\0';
length = nb_read_total;
return res;
}
bool SDL_rw_file_helper::write(char * data) {
SDL_RWops *rw = SDL_RWFromFile("hello.txt", "w");
if(rw != NULL) {
size_t len = SDL_strlen(data);
if (SDL_RWwrite(rw, data, 1, len) != len) {
return false;
} else {
return true;
}
SDL_RWclose(rw);
}
}

View File

@ -0,0 +1,6 @@
struct SDL_rw_file_helper {
const char * filename;
char * read();
int length;
bool write(char * data);
};

View File

@ -30,6 +30,7 @@
#include "../binding-util.h" #include "../binding-util.h"
#include "marshal.h" #include "marshal.h"
#include "file-helper.h"
#include "sharedstate.h" #include "sharedstate.h"
#include "eventthread.h" #include "eventthread.h"
#include "exception.h" #include "exception.h"
@ -171,19 +172,19 @@ MRB_FUNCTION(kernelLoadData)
const char *filename; const char *filename;
mrb_get_args(mrb, "z", &filename); mrb_get_args(mrb, "z", &filename);
SDL_RWops ops;
GUARD_EXC( shState->fileSystem().openReadRaw(ops, filename); )
mrb_value obj; mrb_value obj;
try { obj = marshalLoadInt(mrb, &ops); } try {
SDL_rw_file_helper fileHelper;
fileHelper.filename = filename;
char * contents = fileHelper.read();
mrb_value rawdata = mrb_str_new_static(mrb, contents, fileHelper.length);
obj = mrb_marshal_load(mrb, rawdata);
}
catch (const Exception &e) catch (const Exception &e)
{ {
SDL_RWclose(&ops);
raiseMrbExc(mrb, e); raiseMrbExc(mrb, e);
} }
SDL_RWclose(&ops);
return obj; return obj;
} }
@ -194,19 +195,18 @@ MRB_FUNCTION(kernelSaveData)
mrb_get_args(mrb, "oz", &obj, &filename); mrb_get_args(mrb, "oz", &obj, &filename);
SDL_RWops *ops = SDL_RWFromFile(filename, "w"); try {
if (!ops) mrb_value dumped = mrb_nil_value();
mrb_raise(mrb, getMrbData(mrb)->exc[SDL], SDL_GetError()); mrb_marshal_dump(mrb, obj, dumped);
SDL_rw_file_helper fileHelper;
try { marshalDumpInt(mrb, ops, obj); } fileHelper.filename = filename;
fileHelper.write(RSTRING_PTR(dumped));
}
catch (const Exception &e) catch (const Exception &e)
{ {
SDL_RWclose(ops);
raiseMrbExc(mrb, e); raiseMrbExc(mrb, e);
} }
SDL_RWclose(ops);
return mrb_nil_value(); return mrb_nil_value();
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,31 +1,15 @@
/* #ifndef MRUBY_MARSHAL_H
** marshal.h #define MRUBY_MARSHAL_H
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MARSHAL_H #if defined(__cplusplus)
#define MARSHAL_H extern "C" {
#endif
#include <mruby.h> mrb_value mrb_marshal_dump(mrb_state* M, mrb_value v, mrb_value out);
#include <SDL_rwops.h> mrb_value mrb_marshal_load(mrb_state* M, mrb_value str);
void marshalDumpInt(mrb_state *, SDL_RWops *, mrb_value); #if defined(__cplusplus)
mrb_value marshalLoadInt(mrb_state *, SDL_RWops *); } /* extern "C" { */
#endif
#endif // MARSHAL_H #endif /* MRUBY_ARRAY_H */