rogfsdh[pd

This commit is contained in:
ZakatTeamI2P 2026-04-20 18:41:04 +02:00
parent 948de95a89
commit a8ee9eb3f5
1 changed files with 44 additions and 26 deletions

View File

@ -37,11 +37,12 @@ static void fileIntFreeInstance(void *inst){
DEF_TYPE_CUSTOMFREE(FileInt, fileIntFreeInstance); DEF_TYPE_CUSTOMFREE(FileInt, fileIntFreeInstance);
static VALUE fileIntForPath(const char *path, bool rubyExc){ static VALUE fileIntForPath(const char *path, bool rubyExc){
SDL_IOStream* ops; SDL_IOStream* ops = nullptr;
try{ try{
shState->fileSystem().openReadRaw(ops, path); shState->fileSystem().openReadRaw(ops, path);
} }
catch (const Exception &e){ catch (const Exception &e){
if (ops)
SDL_CloseIO(ops); SDL_CloseIO(ops);
if (rubyExc) if (rubyExc)
@ -62,14 +63,14 @@ static VALUE fileIntForPath(const char *path, bool rubyExc){
RB_METHOD(fileIntRead){ RB_METHOD(fileIntRead){
int length = -1; int length = -1;
rb_get_args(argc, argv, "i", &length RB_ARG_END); rb_get_args(argc, argv, "i", &length);
SDL_IOStream *ops = getPrivateData<SDL_IOStream>(self); SDL_IOStream *ops = getPrivateData<SDL_IOStream>(self);
if (length == -1){ if (length == -1){
Sint64 cur = SDL_TellIO(ops); Sint64 cur = SDL_TellIO(ops);
Sint64 end = SDL_SeekIO(ops, 0, SDL_IO_SEEK_END); Sint64 end = SDL_SeekIO(ops, 0, SDL_IO_SEEK_END);
length = end - cur; length = static_cast<int>(end - cur);
SDL_SeekIO(ops, cur, SDL_IO_SEEK_SET); SDL_SeekIO(ops, cur, SDL_IO_SEEK_SET);
} }
@ -85,10 +86,13 @@ RB_METHOD(fileIntRead){
RB_METHOD(fileIntClose){ RB_METHOD(fileIntClose){
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
SDL_IOStream *ops = getPrivateData<SDL_IOStream>(self); SDL_IOStream *ops = getPrivateData<SDL_IOStream>(self);
if (!ops){
printf("NULL guard! fileIntClose broken!");
return Qnil;
}
SDL_CloseIO(ops); SDL_CloseIO(ops);
setPrivateData(self, nullptr);
return Qnil; return Qnil;
} }
@ -96,9 +100,9 @@ RB_METHOD(fileIntGetByte){
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
SDL_IOStream *ops = getPrivateData<SDL_IOStream>(self); SDL_IOStream *ops = getPrivateData<SDL_IOStream>(self);
unsigned char byte; unsigned char byte = 0;
size_t result = SDL_ReadIO(ops, &byte, 1); size_t result = SDL_ReadIO(ops, &byte, 1);
return (result == 1) ? rb_fix_new(byte) : Qnil; return (result == 1) ? INT2NUM(byte) : Qnil;
} }
RB_METHOD(fileIntBinmode){ RB_METHOD(fileIntBinmode){
@ -106,18 +110,26 @@ RB_METHOD(fileIntBinmode){
return Qnil; return Qnil;
} }
static VALUE load_protect(VALUE marsh_and_port) {
VALUE *arr = (VALUE *)marsh_and_port;
VALUE marsh = arr[0];
VALUE port = arr[1];
return rb_funcallv(marsh, rb_intern("load"), 1, &port);
}
VALUE kernelLoadDataInt(const char *filename, bool rubyExc){ VALUE kernelLoadDataInt(const char *filename, bool rubyExc){
rb_gc_start();
VALUE port = fileIntForPath(filename, rubyExc); VALUE port = fileIntForPath(filename, rubyExc);
VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal")); VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal"));
// FIXME need to catch exceptions here with begin rescue VALUE args[2] = { marsh, port };
VALUE result = rb_funcall2(marsh, rb_intern("load"), 1, &port); int state = 0;
VALUE result = rb_protect(load_protect, (VALUE)args, &state);
rb_funcall2(port, rb_intern("close"), 0, NULL); rb_funcall(port, rb_intern("close"), 0);
if (state) {
rb_jump_tag(state);
}
return result; return result;
} }
@ -125,7 +137,7 @@ RB_METHOD(kernelLoadData){
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
const char *filename; const char *filename;
rb_get_args(argc, argv, "z", &filename RB_ARG_END); rb_get_args(argc, argv, "z", &filename);
return kernelLoadDataInt(filename, true); return kernelLoadDataInt(filename, true);
} }
@ -136,7 +148,7 @@ RB_METHOD(kernelSaveData){
VALUE obj; VALUE obj;
VALUE filename; VALUE filename;
rb_get_args(argc, argv, "oS", &obj, &filename RB_ARG_END); rb_get_args(argc, argv, "oS", &obj, &filename);
VALUE file = rb_file_open_str(filename, "wb"); VALUE file = rb_file_open_str(filename, "wb");
@ -151,9 +163,15 @@ RB_METHOD(kernelSaveData){
} }
static VALUE stringForceUTF8(VALUE arg){ static VALUE stringForceUTF8(VALUE arg){
if (RB_TYPE_P(arg, RUBY_T_STRING) && ENCODING_IS_ASCII8BIT(arg)) if (RB_TYPE_P(arg, RUBY_T_STRING)) {
/* If current encoding is ASCII-8BIT (binary), associate UTF-8 so
subsequent Ruby-level handling treats it as UTF-8. This preserves
the original logic while using newer C-API helpers. */
int enc_idx = rb_enc_get_index(arg);
if (enc_idx == rb_ascii8bit_encindex()) {
rb_enc_associate_index(arg, rb_utf8_encindex()); rb_enc_associate_index(arg, rb_utf8_encindex());
}
}
return arg; return arg;
} }
@ -167,7 +185,6 @@ RB_METHOD(_marshalLoad){
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
VALUE port, proc = Qnil; VALUE port, proc = Qnil;
rb_scan_args(argc, argv, "01", &port, &proc); rb_scan_args(argc, argv, "01", &port, &proc);
//rb_get_args(argc, argv, "o|o", &port, &proc RB_ARG_END);
VALUE utf8Proc; VALUE utf8Proc;
if (NIL_P(proc)) if (NIL_P(proc))
utf8Proc = rb_proc_new(RUBY_METHOD_FUNC(stringForceUTF8), Qnil); utf8Proc = rb_proc_new(RUBY_METHOD_FUNC(stringForceUTF8), Qnil);
@ -196,9 +213,10 @@ void fileIntBindingInit(){
* UTF-8 encoded */ * UTF-8 encoded */
VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal")); VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal"));
if(marsh == Qnil){ if(marsh == Qnil){
printf("BrUh: cant get Marshal\n"); fprintf(stderr, "Cannot get Marshal\n");
exit(1); rb_raise(rb_eRuntimeError, "Marshal constant not found");
} }
rb_define_alias(rb_singleton_class(marsh), "_mkxp_load_alias", "load"); rb_define_alias(rb_singleton_class(marsh), "_mkxp_load_alias", "load");
rb_define_module_function(marsh, "load", RUBY_METHOD_FUNC(_marshalLoad), -1); rb_define_module_function(marsh, "load", RUBY_METHOD_FUNC(_marshalLoad), -1);
} }