mirror of
https://github.com/mkxp-z/mkxp-z.git
synced 2025-08-07 07:25:49 +02:00
always alias MiniFFI class as Win32API
This commit is contained in:
parent
a213b45e84
commit
faef84e1ae
1 changed files with 239 additions and 235 deletions
|
@ -1,8 +1,8 @@
|
||||||
// Most of the MiniFFI class was taken from Ruby 1.8's Win32API.c,
|
// Most of the MiniFFI class was taken from Ruby 1.8's Win32API.c,
|
||||||
// it's just as basic but should work fine for the moment
|
// it's just as basic but should work fine for the moment
|
||||||
|
|
||||||
#include <SDL.h>
|
|
||||||
#include "fake-api.h"
|
#include "fake-api.h"
|
||||||
|
#include <SDL.h>
|
||||||
|
|
||||||
#include "binding-util.h"
|
#include "binding-util.h"
|
||||||
|
|
||||||
|
@ -12,7 +12,6 @@
|
||||||
#define _T_INTEGER 3
|
#define _T_INTEGER 3
|
||||||
#define _T_BOOL 4
|
#define _T_BOOL 4
|
||||||
|
|
||||||
|
|
||||||
// Might need to let MiniFFI.initialize set calling convention
|
// Might need to let MiniFFI.initialize set calling convention
|
||||||
// as an optional arg, this won't work with everything
|
// as an optional arg, this won't work with everything
|
||||||
// Maybe libffi would help out with this
|
// Maybe libffi would help out with this
|
||||||
|
@ -24,7 +23,10 @@
|
||||||
|
|
||||||
// L O N G, but variables won't get set up correctly otherwise
|
// L O N G, but variables won't get set up correctly otherwise
|
||||||
// should allow for __fastcalls (macOS likes these) and whatever else
|
// should allow for __fastcalls (macOS likes these) and whatever else
|
||||||
typedef PREFABI void* (*MINIFFI_FUNC)(unsigned long,unsigned long,unsigned long,unsigned long,unsigned long,unsigned long,unsigned long,unsigned long);
|
typedef PREFABI void *(*MINIFFI_FUNC)(unsigned long, unsigned long,
|
||||||
|
unsigned long, unsigned long,
|
||||||
|
unsigned long, unsigned long,
|
||||||
|
unsigned long, unsigned long);
|
||||||
|
|
||||||
// MiniFFI class, also named Win32API on Windows
|
// MiniFFI class, also named Win32API on Windows
|
||||||
// Uses LoadLibrary/GetProcAddress on Windows, dlopen/dlsym everywhere else
|
// Uses LoadLibrary/GetProcAddress on Windows, dlopen/dlsym everywhere else
|
||||||
|
@ -35,11 +37,11 @@ DEF_TYPE_CUSTOMFREE(MiniFFI, SDL_UnloadObject);
|
||||||
DEF_ALLOCFUNC_CUSTOMFREE(MiniFFI, SDL_UnloadObject);
|
DEF_ALLOCFUNC_CUSTOMFREE(MiniFFI, SDL_UnloadObject);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static void*
|
static void *MiniFFI_GetFunctionHandle(void *libhandle, const char *func) {
|
||||||
MiniFFI_GetFunctionHandle(void *libhandle, const char *func)
|
|
||||||
{
|
|
||||||
#ifdef USE_FAKEAPI
|
#ifdef USE_FAKEAPI
|
||||||
#define CAPTURE(n) if (!strcmp(#n,func)) return (void*)&MKXP_##n
|
#define CAPTURE(n) \
|
||||||
|
if (!strcmp(#n, func)) \
|
||||||
|
return (void *)&MKXP_##n
|
||||||
CAPTURE(GetCurrentThreadId);
|
CAPTURE(GetCurrentThreadId);
|
||||||
CAPTURE(GetWindowThreadProcessId);
|
CAPTURE(GetWindowThreadProcessId);
|
||||||
CAPTURE(FindWindowEx);
|
CAPTURE(FindWindowEx);
|
||||||
|
@ -70,15 +72,15 @@ MiniFFI_GetFunctionHandle(void *libhandle, const char *func)
|
||||||
CAPTURE(GetUserName);
|
CAPTURE(GetUserName);
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
if (!libhandle) return 0;
|
if (!libhandle)
|
||||||
|
return 0;
|
||||||
return SDL_LoadFunction(libhandle, func);
|
return SDL_LoadFunction(libhandle, func);
|
||||||
}
|
}
|
||||||
|
|
||||||
// MiniFFI.new(library, function[, imports[, exports]])
|
// MiniFFI.new(library, function[, imports[, exports]])
|
||||||
// Yields itself in blocks
|
// Yields itself in blocks
|
||||||
|
|
||||||
RB_METHOD(MiniFFI_initialize)
|
RB_METHOD(MiniFFI_initialize) {
|
||||||
{
|
|
||||||
VALUE libname, func, imports, exports;
|
VALUE libname, func, imports, exports;
|
||||||
rb_scan_args(argc, argv, "22", &libname, &func, &imports, &exports);
|
rb_scan_args(argc, argv, "22", &libname, &func, &imports, &exports);
|
||||||
SafeStringValue(libname);
|
SafeStringValue(libname);
|
||||||
|
@ -87,16 +89,14 @@ RB_METHOD(MiniFFI_initialize)
|
||||||
setPrivateData(self, hlib);
|
setPrivateData(self, hlib);
|
||||||
void *hfunc = MiniFFI_GetFunctionHandle(hlib, RSTRING_PTR(func));
|
void *hfunc = MiniFFI_GetFunctionHandle(hlib, RSTRING_PTR(func));
|
||||||
#ifdef __WIN32__
|
#ifdef __WIN32__
|
||||||
if (hlib && !hfunc)
|
if (hlib && !hfunc) {
|
||||||
{
|
|
||||||
VALUE func_a = rb_str_new3(func);
|
VALUE func_a = rb_str_new3(func);
|
||||||
func_a = rb_str_cat(func_a, "A", 1);
|
func_a = rb_str_cat(func_a, "A", 1);
|
||||||
hfunc = SDL_LoadFunction(hlib, RSTRING_PTR(func_a));
|
hfunc = SDL_LoadFunction(hlib, RSTRING_PTR(func_a));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if (!hfunc)
|
if (!hfunc)
|
||||||
rb_raise(rb_eRuntimeError, SDL_GetError());
|
rb_raise(rb_eRuntimeError, "%s", SDL_GetError());
|
||||||
|
|
||||||
|
|
||||||
rb_iv_set(self, "_func", ULONG2NUM((unsigned long)hfunc));
|
rb_iv_set(self, "_func", ULONG2NUM((unsigned long)hfunc));
|
||||||
rb_iv_set(self, "_funcname", func);
|
rb_iv_set(self, "_funcname", func);
|
||||||
|
@ -104,30 +104,33 @@ RB_METHOD(MiniFFI_initialize)
|
||||||
|
|
||||||
VALUE ary_imports = rb_ary_new();
|
VALUE ary_imports = rb_ary_new();
|
||||||
VALUE *entry;
|
VALUE *entry;
|
||||||
switch (TYPE(imports))
|
switch (TYPE(imports)) {
|
||||||
{
|
|
||||||
case T_NIL:
|
case T_NIL:
|
||||||
break;
|
break;
|
||||||
case T_ARRAY:
|
case T_ARRAY:
|
||||||
entry = RARRAY_PTR(imports);
|
entry = RARRAY_PTR(imports);
|
||||||
for (int i = 0; i < RARRAY_LEN(imports); i++)
|
for (int i = 0; i < RARRAY_LEN(imports); i++) {
|
||||||
{
|
|
||||||
SafeStringValue(entry[i]);
|
SafeStringValue(entry[i]);
|
||||||
switch (*(char*)RSTRING_PTR(entry[i]))
|
switch (*(char *)RSTRING_PTR(entry[i])) {
|
||||||
{
|
case 'N':
|
||||||
case 'N': case 'n': case 'L': case 'l':
|
case 'n':
|
||||||
|
case 'L':
|
||||||
|
case 'l':
|
||||||
rb_ary_push(ary_imports, INT2FIX(_T_NUMBER));
|
rb_ary_push(ary_imports, INT2FIX(_T_NUMBER));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'P': case 'p':
|
case 'P':
|
||||||
|
case 'p':
|
||||||
rb_ary_push(ary_imports, INT2FIX(_T_POINTER));
|
rb_ary_push(ary_imports, INT2FIX(_T_POINTER));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'I': case 'i':
|
case 'I':
|
||||||
|
case 'i':
|
||||||
rb_ary_push(ary_imports, INT2FIX(_T_INTEGER));
|
rb_ary_push(ary_imports, INT2FIX(_T_INTEGER));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'B': case 'b':
|
case 'B':
|
||||||
|
case 'b':
|
||||||
rb_ary_push(ary_imports, INT2FIX(_T_BOOL));
|
rb_ary_push(ary_imports, INT2FIX(_T_BOOL));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -136,23 +139,27 @@ RB_METHOD(MiniFFI_initialize)
|
||||||
default:
|
default:
|
||||||
SafeStringValue(imports);
|
SafeStringValue(imports);
|
||||||
const char *s = RSTRING_PTR(imports);
|
const char *s = RSTRING_PTR(imports);
|
||||||
for (int i = 0; i < RSTRING_LEN(imports); i++)
|
for (int i = 0; i < RSTRING_LEN(imports); i++) {
|
||||||
{
|
switch (*s++) {
|
||||||
switch (*s++)
|
case 'N':
|
||||||
{
|
case 'n':
|
||||||
case 'N': case 'n': case 'L': case 'l':
|
case 'L':
|
||||||
|
case 'l':
|
||||||
rb_ary_push(ary_imports, INT2FIX(_T_NUMBER));
|
rb_ary_push(ary_imports, INT2FIX(_T_NUMBER));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'P': case 'p':
|
case 'P':
|
||||||
|
case 'p':
|
||||||
rb_ary_push(ary_imports, INT2FIX(_T_POINTER));
|
rb_ary_push(ary_imports, INT2FIX(_T_POINTER));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'I': case 'i':
|
case 'I':
|
||||||
|
case 'i':
|
||||||
rb_ary_push(ary_imports, INT2FIX(_T_INTEGER));
|
rb_ary_push(ary_imports, INT2FIX(_T_INTEGER));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'B': case 'b':
|
case 'B':
|
||||||
|
case 'b':
|
||||||
rb_ary_push(ary_imports, INT2FIX(_T_BOOL));
|
rb_ary_push(ary_imports, INT2FIX(_T_BOOL));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -161,47 +168,51 @@ RB_METHOD(MiniFFI_initialize)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (8 < RARRAY_LEN(ary_imports))
|
if (8 < RARRAY_LEN(ary_imports))
|
||||||
rb_raise(rb_eRuntimeError, "too many parameters: %ld\n", RARRAY_LEN(ary_imports));
|
rb_raise(rb_eRuntimeError, "too many parameters: %ld\n",
|
||||||
|
RARRAY_LEN(ary_imports));
|
||||||
|
|
||||||
rb_iv_set(self, "_imports", ary_imports);
|
rb_iv_set(self, "_imports", ary_imports);
|
||||||
int ex;
|
int ex;
|
||||||
if (NIL_P(exports))
|
if (NIL_P(exports)) {
|
||||||
{
|
|
||||||
ex = _T_VOID;
|
ex = _T_VOID;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
SafeStringValue(exports);
|
SafeStringValue(exports);
|
||||||
switch(*RSTRING_PTR(exports))
|
switch (*RSTRING_PTR(exports)) {
|
||||||
{
|
case 'V':
|
||||||
case 'V': case 'v':
|
case 'v':
|
||||||
ex = _T_VOID;
|
ex = _T_VOID;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'N': case 'n': case 'L': case 'l':
|
case 'N':
|
||||||
|
case 'n':
|
||||||
|
case 'L':
|
||||||
|
case 'l':
|
||||||
ex = _T_NUMBER;
|
ex = _T_NUMBER;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'P': case 'p':
|
case 'P':
|
||||||
|
case 'p':
|
||||||
ex = _T_POINTER;
|
ex = _T_POINTER;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'I': case 'i':
|
case 'I':
|
||||||
|
case 'i':
|
||||||
ex = _T_INTEGER;
|
ex = _T_INTEGER;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'B': case 'b':
|
case 'B':
|
||||||
|
case 'b':
|
||||||
ex = _T_BOOL;
|
ex = _T_BOOL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rb_iv_set(self, "_exports", INT2FIX(ex));
|
rb_iv_set(self, "_exports", INT2FIX(ex));
|
||||||
if (rb_block_given_p()) rb_yield(self);
|
if (rb_block_given_p())
|
||||||
|
rb_yield(self);
|
||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
|
|
||||||
RB_METHOD(MiniFFI_call)
|
RB_METHOD(MiniFFI_call) {
|
||||||
{
|
|
||||||
struct {
|
struct {
|
||||||
unsigned long params[8];
|
unsigned long params[8];
|
||||||
} param;
|
} param;
|
||||||
|
@ -214,26 +225,19 @@ RB_METHOD(MiniFFI_call)
|
||||||
int items = rb_scan_args(argc, argv, "0*", &args);
|
int items = rb_scan_args(argc, argv, "0*", &args);
|
||||||
int nimport = RARRAY_LEN(own_imports);
|
int nimport = RARRAY_LEN(own_imports);
|
||||||
if (items != nimport)
|
if (items != nimport)
|
||||||
rb_raise(rb_eRuntimeError, "wrong number of parameters: expected %d, got %d",
|
rb_raise(rb_eRuntimeError,
|
||||||
nimport, items);
|
"wrong number of parameters: expected %d, got %d", nimport, items);
|
||||||
|
|
||||||
for (int i = 0; i < nimport; i++)
|
for (int i = 0; i < nimport; i++) {
|
||||||
{
|
|
||||||
VALUE str = rb_ary_entry(args, i);
|
VALUE str = rb_ary_entry(args, i);
|
||||||
unsigned long lParam = 0;
|
unsigned long lParam = 0;
|
||||||
switch(FIX2INT(rb_ary_entry(own_imports, i)))
|
switch (FIX2INT(rb_ary_entry(own_imports, i))) {
|
||||||
{
|
|
||||||
case _T_POINTER:
|
case _T_POINTER:
|
||||||
if (NIL_P(str))
|
if (NIL_P(str)) {
|
||||||
{
|
|
||||||
lParam = 0;
|
lParam = 0;
|
||||||
}
|
} else if (FIXNUM_P(str)) {
|
||||||
else if (FIXNUM_P(str))
|
|
||||||
{
|
|
||||||
lParam = NUM2ULONG(str);
|
lParam = NUM2ULONG(str);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
StringValue(str);
|
StringValue(str);
|
||||||
rb_str_modify(str);
|
rb_str_modify(str);
|
||||||
lParam = (unsigned long)RSTRING_PTR(str);
|
lParam = (unsigned long)RSTRING_PTR(str);
|
||||||
|
@ -244,17 +248,21 @@ RB_METHOD(MiniFFI_call)
|
||||||
lParam = RTEST(rb_ary_entry(args, i));
|
lParam = RTEST(rb_ary_entry(args, i));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case _T_NUMBER: case _T_INTEGER: default:
|
case _T_NUMBER:
|
||||||
|
case _T_INTEGER:
|
||||||
|
default:
|
||||||
lParam = NUM2ULONG(rb_ary_entry(args, i));
|
lParam = NUM2ULONG(rb_ary_entry(args, i));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params[i] = lParam;
|
params[i] = lParam;
|
||||||
}
|
}
|
||||||
unsigned long ret = (unsigned long)ApiFunction(params[0],params[1],params[2],params[3],params[4],params[5],params[6],params[7]);
|
unsigned long ret =
|
||||||
|
(unsigned long)ApiFunction(params[0], params[1], params[2], params[3],
|
||||||
|
params[4], params[5], params[6], params[7]);
|
||||||
|
|
||||||
switch (FIX2INT(own_exports))
|
switch (FIX2INT(own_exports)) {
|
||||||
{
|
case _T_NUMBER:
|
||||||
case _T_NUMBER: case _T_INTEGER:
|
case _T_INTEGER:
|
||||||
return ULONG2NUM(ret);
|
return ULONG2NUM(ret);
|
||||||
|
|
||||||
case _T_POINTER:
|
case _T_POINTER:
|
||||||
|
@ -263,15 +271,13 @@ RB_METHOD(MiniFFI_call)
|
||||||
case _T_BOOL:
|
case _T_BOOL:
|
||||||
return rb_bool_new(ret);
|
return rb_bool_new(ret);
|
||||||
|
|
||||||
case _T_VOID: default:
|
case _T_VOID:
|
||||||
|
default:
|
||||||
return ULONG2NUM(0);
|
return ULONG2NUM(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MiniFFIBindingInit() {
|
||||||
void
|
|
||||||
MiniFFIBindingInit()
|
|
||||||
{
|
|
||||||
VALUE cMiniFFI = rb_define_class("MiniFFI", rb_cObject);
|
VALUE cMiniFFI = rb_define_class("MiniFFI", rb_cObject);
|
||||||
#ifndef OLD_RUBY
|
#ifndef OLD_RUBY
|
||||||
rb_define_alloc_func(cMiniFFI, classAllocate<&MiniFFIType>);
|
rb_define_alloc_func(cMiniFFI, classAllocate<&MiniFFIType>);
|
||||||
|
@ -282,7 +288,5 @@ MiniFFIBindingInit()
|
||||||
_rb_define_method(cMiniFFI, "call", MiniFFI_call);
|
_rb_define_method(cMiniFFI, "call", MiniFFI_call);
|
||||||
rb_define_alias(cMiniFFI, "Call", "call");
|
rb_define_alias(cMiniFFI, "Call", "call");
|
||||||
|
|
||||||
#if defined(__WIN32__) || defined(USE_FAKEAPI)
|
|
||||||
rb_define_const(rb_cObject, "Win32API", cMiniFFI);
|
rb_define_const(rb_cObject, "Win32API", cMiniFFI);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue