segatools/idzhook/idz-dll.c

140 lines
3.9 KiB
C
Raw Permalink Normal View History

2021-06-12 16:40:19 +00:00
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include "idzhook/idz-dll.h"
#include "util/dll-bind.h"
#include "util/dprintf.h"
const struct dll_bind_sym idz_dll_syms[] = {
{
.sym = "idz_io_jvs_init",
.off = offsetof(struct idz_dll, jvs_init),
}, {
.sym = "idz_io_jvs_read_analogs",
.off = offsetof(struct idz_dll, jvs_read_analogs),
}, {
.sym = "idz_io_jvs_read_buttons",
.off = offsetof(struct idz_dll, jvs_read_buttons),
}, {
.sym = "idz_io_jvs_read_shifter",
.off = offsetof(struct idz_dll, jvs_read_shifter),
}, {
.sym = "idz_io_jvs_read_coin_counter",
.off = offsetof(struct idz_dll, jvs_read_coin_counter),
2024-09-30 21:10:16 +00:00
}, {
.sym = "idz_io_led_init",
.off = offsetof(struct idz_dll, led_init),
}, {
.sym = "idz_io_led_set_fet_output",
.off = offsetof(struct idz_dll, led_set_fet_output),
}, {
.sym = "idz_io_led_gs_update",
.off = offsetof(struct idz_dll, led_gs_update),
}, {
.sym = "idz_io_led_set_leds",
.off = offsetof(struct idz_dll, led_set_leds),
}, {
.sym = "idz_io_ffb_init",
.off = offsetof(struct idz_dll, ffb_init),
}, {
.sym = "idz_io_ffb_toggle",
.off = offsetof(struct idz_dll, ffb_toggle),
}, {
.sym = "idz_io_ffb_constant_force",
.off = offsetof(struct idz_dll, ffb_constant_force),
}, {
.sym = "idz_io_ffb_rumble",
.off = offsetof(struct idz_dll, ffb_rumble),
}, {
.sym = "idz_io_ffb_damper",
.off = offsetof(struct idz_dll, ffb_damper),
2021-06-12 16:40:19 +00:00
}
};
struct idz_dll idz_dll;
// Copypasta DLL binding and diagnostic message boilerplate.
// Not much of this lends itself to being easily factored out. Also there
// will be a lot of API-specific branching code here eventually as new API
// versions get defined, so even though these functions all look the same
// now this won't remain the case forever.
HRESULT idz_dll_init(const struct idz_dll_config *cfg, HINSTANCE self)
{
uint16_t (*get_api_version)(void);
const struct dll_bind_sym *sym;
HINSTANCE owned;
HINSTANCE src;
HRESULT hr;
assert(cfg != NULL);
assert(self != NULL);
if (cfg->path[0] != L'\0') {
owned = LoadLibraryW(cfg->path);
if (owned == NULL) {
hr = HRESULT_FROM_WIN32(GetLastError());
2021-06-16 17:51:20 +00:00
dprintf("IDZ IO: Failed to load IO DLL: %lx: %S\n",
2021-06-12 16:40:19 +00:00
hr,
cfg->path);
goto end;
}
dprintf("IDZ IO: Using custom IO DLL: %S\n", cfg->path);
src = owned;
} else {
owned = NULL;
src = self;
}
get_api_version = (void *) GetProcAddress(src, "idz_io_get_api_version");
if (get_api_version != NULL) {
idz_dll.api_version = get_api_version();
} else {
idz_dll.api_version = 0x0100;
dprintf("Custom IO DLL does not expose idz_io_get_api_version, "
"assuming API version 1.0.\n"
"Please ask the developer to update their DLL.\n");
}
if (idz_dll.api_version >= 0x0200) {
hr = E_NOTIMPL;
dprintf("IDZ IO: Custom IO DLL implements an unsupported "
"API version (%#04x). Please update Segatools.\n",
idz_dll.api_version);
goto end;
}
sym = idz_dll_syms;
hr = dll_bind(&idz_dll, src, &sym, _countof(idz_dll_syms));
if (FAILED(hr)) {
if (src != self) {
dprintf("IDZ IO: Custom IO DLL does not provide function "
"\"%s\". Please contact your IO DLL's developer for "
"further assistance.\n",
sym->sym);
goto end;
} else {
dprintf("Internal error: could not reflect \"%s\"\n", sym->sym);
}
}
owned = NULL;
end:
if (owned != NULL) {
FreeLibrary(owned);
}
return hr;
}