Make changes for Emscripten

This commit is contained in:
Varun Patil 2020-05-04 13:13:57 +05:30
parent 776564d583
commit 8c5db3a401
33 changed files with 5082 additions and 3720 deletions

View file

@ -0,0 +1,43 @@
#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(filename, "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);
return true;
}
return false;
}

View file

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

View file

@ -454,10 +454,11 @@ MRB_METHOD(fileReadLines)
FILE *f = p->fp();
mrb_value arg;
mrb_get_args(mrb, "|o", &arg);
int argc = mrb_get_args(mrb, "|o", &arg);
const char *rs = "\n"; (void) rs;
if (mrb->c->ci->argc > 0)
if (argc > 0)
{
Debug() << "FIXME: File.readlines: rs not implemented";
@ -596,7 +597,7 @@ fileBindingInit(mrb_state *mrb)
mrb_define_method(mrb, klass, "path", fileGetPath, MRB_ARGS_NONE());
/* FileTest */
RClass *module = mrb_define_module(mrb, "FileTest");
RClass *module = mrb_define_module(mrb, "MKXPFileTest");
mrb_define_module_function(mrb, module, "exist?", fileTestDoesExist, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, module, "directory?", fileTestIsDirectory, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, module, "file?", fileTestIsFile, MRB_ARGS_REQ(1));

View file

@ -30,6 +30,7 @@
#include "../binding-util.h"
#include "marshal.h"
#include "file-helper.h"
#include "sharedstate.h"
#include "eventthread.h"
#include "exception.h"
@ -136,11 +137,12 @@ MRB_FUNCTION(kernelRand)
MRB_FUNCTION(kernelSrand)
{
srandCalled = true;
mrb_int seed;
int argc = mrb_get_args(mrb, "|i", &seed);
if (mrb->c->ci->argc == 1)
if (argc == 1)
{
mrb_int seed;
mrb_get_args(mrb, "i", &seed);
srand(seed);
int oldSeed = currentSeed;
@ -171,19 +173,19 @@ MRB_FUNCTION(kernelLoadData)
const char *filename;
mrb_get_args(mrb, "z", &filename);
SDL_RWops ops;
GUARD_EXC( shState->fileSystem().openRead(ops, filename); )
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)
{
SDL_RWclose(&ops);
raiseMrbExc(mrb, e);
}
SDL_RWclose(&ops);
return obj;
}
@ -194,19 +196,18 @@ MRB_FUNCTION(kernelSaveData)
mrb_get_args(mrb, "oz", &obj, &filename);
SDL_RWops *ops = SDL_RWFromFile(filename, "w");
if (!ops)
mrb_raise(mrb, getMrbData(mrb)->exc[SDL], SDL_GetError());
try { marshalDumpInt(mrb, ops, obj); }
try {
mrb_value dumped = mrb_nil_value();
mrb_marshal_dump(mrb, obj, dumped);
SDL_rw_file_helper fileHelper;
fileHelper.filename = filename;
fileHelper.write(RSTRING_PTR(dumped));
}
catch (const Exception &e)
{
SDL_RWclose(ops);
raiseMrbExc(mrb, e);
}
SDL_RWclose(ops);
return mrb_nil_value();
}

File diff suppressed because it is too large Load diff

View file

@ -1,31 +1,15 @@
/*
** 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 MRUBY_MARSHAL_H
#define MRUBY_MARSHAL_H
#ifndef MARSHAL_H
#define MARSHAL_H
#if defined(__cplusplus)
extern "C" {
#endif
#include <mruby.h>
#include <SDL_rwops.h>
mrb_value mrb_marshal_dump(mrb_state* M, mrb_value v, mrb_value out);
mrb_value mrb_marshal_load(mrb_state* M, mrb_value str);
void marshalDumpInt(mrb_state *, SDL_RWops *, mrb_value);
mrb_value marshalLoadInt(mrb_state *, SDL_RWops *);
#if defined(__cplusplus)
} /* extern "C" { */
#endif
#endif // MARSHAL_H
#endif /* MRUBY_ARRAY_H */

View file

@ -27,7 +27,7 @@
#include "time.h"
#include <sys/time.h>
#include <stdio.h>
struct TimeImpl
{