2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** filesystem-binding.cpp
|
|
|
|
**
|
|
|
|
** 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/>.
|
|
|
|
*/
|
|
|
|
|
2015-06-18 19:41:32 +00:00
|
|
|
#include "binding-util.h"
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
#include "sharedstate.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "filesystem.h"
|
2015-06-18 19:41:32 +00:00
|
|
|
#include "util.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-10-20 13:51:24 +00:00
|
|
|
#include "ruby/encoding.h"
|
|
|
|
#include "ruby/intern.h"
|
|
|
|
|
2014-08-29 11:52:10 +00:00
|
|
|
static void
|
|
|
|
fileIntFreeInstance(void *inst)
|
|
|
|
{
|
|
|
|
SDL_RWops *ops = static_cast<SDL_RWops*>(inst);
|
|
|
|
|
|
|
|
SDL_RWclose(ops);
|
|
|
|
SDL_FreeRW(ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TYPE_CUSTOMFREE(FileInt, fileIntFreeInstance);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
static VALUE
|
2014-12-23 18:05:08 +00:00
|
|
|
fileIntForPath(const char *path, bool rubyExc)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
SDL_RWops *ops = SDL_AllocRW();
|
2014-09-13 00:54:44 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
shState->fileSystem().openRead(*ops, path);
|
|
|
|
}
|
|
|
|
catch (const Exception &e)
|
|
|
|
{
|
|
|
|
SDL_FreeRW(ops);
|
2014-12-23 18:05:08 +00:00
|
|
|
|
|
|
|
if (rubyExc)
|
|
|
|
raiseRbExc(e);
|
|
|
|
else
|
|
|
|
throw e;
|
2014-09-13 00:54:44 +00:00
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
VALUE klass = rb_const_get(rb_cObject, rb_intern("FileInt"));
|
|
|
|
|
|
|
|
VALUE obj = rb_obj_alloc(klass);
|
|
|
|
|
2013-10-30 09:06:24 +00:00
|
|
|
setPrivateData(obj, ops);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
RB_METHOD(fileIntRead)
|
|
|
|
{
|
|
|
|
|
|
|
|
int length = -1;
|
2013-12-20 10:29:12 +00:00
|
|
|
rb_get_args(argc, argv, "i", &length RB_ARG_END);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
SDL_RWops *ops = getPrivateData<SDL_RWops>(self);
|
|
|
|
|
|
|
|
if (length == -1)
|
|
|
|
{
|
|
|
|
Sint64 cur = SDL_RWtell(ops);
|
|
|
|
Sint64 end = SDL_RWseek(ops, 0, SEEK_END);
|
|
|
|
length = end - cur;
|
|
|
|
SDL_RWseek(ops, cur, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
return Qnil;
|
|
|
|
|
|
|
|
VALUE data = rb_str_new(0, length);
|
|
|
|
|
|
|
|
SDL_RWread(ops, RSTRING_PTR(data), 1, length);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
RB_METHOD(fileIntClose)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
SDL_RWops *ops = getPrivateData<SDL_RWops>(self);
|
|
|
|
SDL_RWclose(ops);
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
RB_METHOD(fileIntGetByte)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
SDL_RWops *ops = getPrivateData<SDL_RWops>(self);
|
|
|
|
|
|
|
|
unsigned char byte;
|
|
|
|
size_t result = SDL_RWread(ops, &byte, 1, 1);
|
|
|
|
|
|
|
|
return (result == 1) ? rb_fix_new(byte) : Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
RB_METHOD(fileIntBinmode)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2014-12-23 18:05:08 +00:00
|
|
|
kernelLoadDataInt(const char *filename, bool rubyExc)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
rb_gc_start();
|
|
|
|
|
2014-12-23 18:05:08 +00:00
|
|
|
VALUE port = fileIntForPath(filename, rubyExc);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal"));
|
|
|
|
|
2013-09-10 02:21:57 +00:00
|
|
|
// FIXME need to catch exceptions here with begin rescue
|
2014-01-24 21:23:30 +00:00
|
|
|
VALUE result = rb_funcall2(marsh, rb_intern("load"), 1, &port);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-01-24 21:23:30 +00:00
|
|
|
rb_funcall2(port, rb_intern("close"), 0, NULL);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
RB_METHOD(kernelLoadData)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
const char *filename;
|
2013-12-20 10:29:12 +00:00
|
|
|
rb_get_args(argc, argv, "z", &filename RB_ARG_END);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-12-23 18:05:08 +00:00
|
|
|
return kernelLoadDataInt(filename, true);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RB_METHOD(kernelSaveData)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
VALUE obj;
|
|
|
|
VALUE filename;
|
|
|
|
|
2013-12-20 10:29:12 +00:00
|
|
|
rb_get_args(argc, argv, "oS", &obj, &filename RB_ARG_END);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-01-24 21:23:30 +00:00
|
|
|
VALUE file = rb_file_open_str(filename, "wb");
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal"));
|
|
|
|
|
2014-01-24 21:23:30 +00:00
|
|
|
VALUE v[] = { obj, file };
|
|
|
|
rb_funcall2(marsh, rb_intern("dump"), ARRAY_SIZE(v), v);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-01-24 21:23:30 +00:00
|
|
|
rb_io_close(file);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:51:24 +00:00
|
|
|
static VALUE stringForceUTF8(VALUE arg)
|
|
|
|
{
|
2014-08-25 08:22:23 +00:00
|
|
|
if (RB_TYPE_P(arg, RUBY_T_STRING) && ENCODING_IS_ASCII8BIT(arg))
|
2013-10-22 11:45:57 +00:00
|
|
|
rb_enc_associate_index(arg, rb_utf8_encindex());
|
2013-10-20 13:51:24 +00:00
|
|
|
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
2013-10-22 13:24:55 +00:00
|
|
|
static VALUE customProc(VALUE arg, VALUE proc)
|
2013-10-22 11:45:57 +00:00
|
|
|
{
|
|
|
|
VALUE obj = stringForceUTF8(arg);
|
|
|
|
obj = rb_funcall2(proc, rb_intern("call"), 1, &obj);
|
2013-10-22 13:24:55 +00:00
|
|
|
|
2013-10-22 11:45:57 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:51:24 +00:00
|
|
|
RB_METHOD(_marshalLoad)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
VALUE port, proc = Qnil;
|
|
|
|
|
2013-12-20 10:29:12 +00:00
|
|
|
rb_get_args(argc, argv, "o|o", &port, &proc RB_ARG_END);
|
2013-10-20 13:51:24 +00:00
|
|
|
|
2013-10-22 11:45:57 +00:00
|
|
|
VALUE utf8Proc;
|
|
|
|
if (NIL_P(proc))
|
|
|
|
utf8Proc = rb_proc_new(RUBY_METHOD_FUNC(stringForceUTF8), Qnil);
|
|
|
|
else
|
2013-10-22 13:24:55 +00:00
|
|
|
utf8Proc = rb_proc_new(RUBY_METHOD_FUNC(customProc), proc);
|
2013-10-20 13:51:24 +00:00
|
|
|
|
|
|
|
VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal"));
|
|
|
|
|
2014-01-24 21:23:30 +00:00
|
|
|
VALUE v[] = { port, utf8Proc };
|
|
|
|
return rb_funcall2(marsh, rb_intern("_mkxp_load_alias"), ARRAY_SIZE(v), v);
|
2013-10-20 13:51:24 +00:00
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
void
|
|
|
|
fileIntBindingInit()
|
|
|
|
{
|
|
|
|
VALUE klass = rb_define_class("FileInt", rb_cIO);
|
2013-10-30 09:06:24 +00:00
|
|
|
rb_define_alloc_func(klass, classAllocate<&FileIntType>);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
_rb_define_method(klass, "read", fileIntRead);
|
|
|
|
_rb_define_method(klass, "getbyte", fileIntGetByte);
|
|
|
|
_rb_define_method(klass, "binmode", fileIntBinmode);
|
|
|
|
_rb_define_method(klass, "close", fileIntClose);
|
|
|
|
|
|
|
|
_rb_define_module_function(rb_mKernel, "load_data", kernelLoadData);
|
|
|
|
_rb_define_module_function(rb_mKernel, "save_data", kernelSaveData);
|
2013-10-20 13:51:24 +00:00
|
|
|
|
|
|
|
/* We overload the built-in 'Marshal::load()' function to silently
|
|
|
|
* insert our utf8proc that ensures all read strings will be
|
|
|
|
* UTF-8 encoded */
|
|
|
|
VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal"));
|
|
|
|
rb_define_alias(rb_singleton_class(marsh), "_mkxp_load_alias", "load");
|
|
|
|
_rb_define_module_function(marsh, "load", _marshalLoad);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|