Load and bind chuniio at runtime

This commit is contained in:
Tau 2021-05-31 12:58:22 -04:00
parent ef8c844403
commit 01cfc4e48f
9 changed files with 207 additions and 13 deletions

121
chunihook/chuni-dll.c Normal file
View File

@ -0,0 +1,121 @@
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include "chunihook/chuni-dll.h"
#include "util/dll-bind.h"
#include "util/dprintf.h"
const struct dll_bind_sym chuni_dll_syms[] = {
{
.sym = "chuni_io_jvs_init",
.off = offsetof(struct chuni_dll, jvs_init),
}, {
.sym = "chuni_io_jvs_poll",
.off = offsetof(struct chuni_dll, jvs_poll),
}, {
.sym = "chuni_io_jvs_read_coin_counter",
.off = offsetof(struct chuni_dll, jvs_read_coin_counter),
}, {
.sym = "chuni_io_jvs_set_coin_blocker",
.off = offsetof(struct chuni_dll, jvs_set_coin_blocker),
}, {
.sym = "chuni_io_slider_init",
.off = offsetof(struct chuni_dll, slider_init),
}, {
.sym = "chuni_io_slider_start",
.off = offsetof(struct chuni_dll, slider_start),
}, {
.sym = "chuni_io_slider_stop",
.off = offsetof(struct chuni_dll, slider_stop),
}, {
.sym = "chuni_io_slider_set_leds",
.off = offsetof(struct chuni_dll, slider_set_leds),
}
};
struct chuni_dll chuni_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 chuni_dll_init(const struct chuni_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());
dprintf("Chunithm IO: Failed to load IO DLL: %x: %S\n",
hr,
cfg->path);
goto end;
}
dprintf("Chunithm IO: Using custom IO DLL: %S\n", cfg->path);
src = owned;
} else {
owned = NULL;
src = self;
}
get_api_version = (void *) GetProcAddress(src, "chuni_io_get_api_version");
if (get_api_version != NULL) {
chuni_dll.api_version = get_api_version();
} else {
chuni_dll.api_version = 0x0100;
dprintf("Custom IO DLL does not expose chuni_io_get_api_version, "
"assuming API version 1.0.\n"
"Please ask the developer to update their DLL.\n");
}
if (chuni_dll.api_version >= 0x0200) {
hr = E_NOTIMPL;
dprintf("Chunithm IO: Custom IO DLL implements an unsupported "
"API version (%#04x). Please update Segatools.\n",
chuni_dll.api_version);
goto end;
}
sym = chuni_dll_syms;
hr = dll_bind(&chuni_dll, src, &sym, _countof(chuni_dll_syms));
if (FAILED(hr)) {
if (src != self) {
dprintf("Chunithm 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;
}

25
chunihook/chuni-dll.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include <windows.h>
#include "chuniio/chuniio.h"
struct chuni_dll {
uint16_t api_version;
HRESULT (*jvs_init)(void);
void (*jvs_poll)(uint8_t *opbtn, uint8_t *beams);
void (*jvs_read_coin_counter)(uint16_t *total);
void (*jvs_set_coin_blocker)(bool open);
HRESULT (*slider_init)(void);
void (*slider_start)(chuni_io_slider_callback_t callback);
void (*slider_stop)(void);
void (*slider_set_leds)(const uint8_t *rgb);
};
struct chuni_dll_config {
wchar_t path[MAX_PATH];
};
extern struct chuni_dll chuni_dll;
HRESULT chuni_dll_init(const struct chuni_dll_config *cfg, HINSTANCE self);

View File

@ -12,3 +12,12 @@ EXPORTS
amDllVideoGetVBiosVersion @4 amDllVideoGetVBiosVersion @4
amDllVideoOpen @1 amDllVideoOpen @1
amDllVideoSetResolution @3 amDllVideoSetResolution @3
chuni_io_get_api_version
chuni_io_jvs_init
chuni_io_jvs_poll
chuni_io_jvs_read_coin_counter
chuni_io_jvs_set_coin_blocker
chuni_io_slider_init
chuni_io_slider_set_leds
chuni_io_slider_start
chuni_io_slider_stop

View File

@ -18,6 +18,22 @@
#include "platform/config.h" #include "platform/config.h"
#include "platform/platform.h" #include "platform/platform.h"
void chuni_dll_config_load(
struct chuni_dll_config *cfg,
const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
GetPrivateProfileStringW(
L"chuniio",
L"path",
L"",
cfg->path,
_countof(cfg->path),
filename);
}
void slider_config_load(struct slider_config *cfg, const wchar_t *filename) void slider_config_load(struct slider_config *cfg, const wchar_t *filename)
{ {
assert(cfg != NULL); assert(cfg != NULL);
@ -39,5 +55,6 @@ void chuni_hook_config_load(
amex_config_load(&cfg->amex, filename); amex_config_load(&cfg->amex, filename);
aime_config_load(&cfg->aime, filename); aime_config_load(&cfg->aime, filename);
gfx_config_load(&cfg->gfx, filename); gfx_config_load(&cfg->gfx, filename);
chuni_dll_config_load(&cfg->dll, filename);
slider_config_load(&cfg->slider, filename); slider_config_load(&cfg->slider, filename);
} }

View File

@ -7,6 +7,7 @@
#include "board/sg-reader.h" #include "board/sg-reader.h"
#include "chunihook/chuni-dll.h"
#include "chunihook/slider.h" #include "chunihook/slider.h"
#include "hooklib/gfx.h" #include "hooklib/gfx.h"
@ -18,9 +19,13 @@ struct chuni_hook_config {
struct amex_config amex; struct amex_config amex;
struct aime_config aime; struct aime_config aime;
struct gfx_config gfx; struct gfx_config gfx;
struct chuni_dll_config dll;
struct slider_config slider; struct slider_config slider;
}; };
void chuni_dll_config_load(
struct chuni_dll_config *cfg,
const wchar_t *filename);
void slider_config_load(struct slider_config *cfg, const wchar_t *filename); void slider_config_load(struct slider_config *cfg, const wchar_t *filename);
void chuni_hook_config_load( void chuni_hook_config_load(
struct chuni_hook_config *cfg, struct chuni_hook_config *cfg,

View File

@ -75,6 +75,12 @@ static DWORD CALLBACK chuni_pre_startup(void)
goto fail; goto fail;
} }
hr = chuni_dll_init(&chuni_hook_cfg.dll, chuni_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = amex_hook_init(&chuni_hook_cfg.amex, chunithm_jvs_init); hr = amex_hook_init(&chuni_hook_cfg.amex, chunithm_jvs_init);
if (FAILED(hr)) { if (FAILED(hr)) {

View File

@ -9,7 +9,7 @@
#include "board/io3.h" #include "board/io3.h"
#include "chuniio/chuniio.h" #include "chunihook/chuni-dll.h"
#include "jvs/jvs-bus.h" #include "jvs/jvs-bus.h"
@ -47,9 +47,10 @@ HRESULT chunithm_jvs_init(struct jvs_node **out)
HRESULT hr; HRESULT hr;
assert(out != NULL); assert(out != NULL);
assert(chuni_dll.jvs_init != NULL);
dprintf("JVS I/O: Starting Chunithm backend DLL\n"); dprintf("JVS I/O: Starting IO backend\n");
hr = chuni_io_jvs_init(); hr = chuni_dll.jvs_init();
if (FAILED(hr)) { if (FAILED(hr)) {
dprintf("JVS I/O: Backend error, I/O disconnected: %x\n", (int) hr); dprintf("JVS I/O: Backend error, I/O disconnected: %x\n", (int) hr);
@ -70,11 +71,12 @@ static void chunithm_jvs_read_switches(void *ctx, struct io3_switch_state *out)
size_t i; size_t i;
assert(out != NULL); assert(out != NULL);
assert(chuni_dll.jvs_poll != NULL);
opbtn = 0; opbtn = 0;
beams = 0; beams = 0;
chuni_io_jvs_poll(&opbtn, &beams); chuni_dll.jvs_poll(&opbtn, &beams);
out->system = 0x00; out->system = 0x00;
out->p1 = 0x0000; out->p1 = 0x0000;
@ -105,10 +107,11 @@ static void chunithm_jvs_read_coin_counter(
uint16_t *out) uint16_t *out)
{ {
assert(out != NULL); assert(out != NULL);
assert(chuni_dll.jvs_read_coin_counter != NULL);
if (slot_no > 0) { if (slot_no > 0) {
return; return;
} }
chuni_io_jvs_read_coin_counter(out); chuni_dll.jvs_read_coin_counter(out);
} }

View File

@ -20,6 +20,8 @@ shared_library(
util_lib, util_lib,
], ],
sources : [ sources : [
'chuni-dll.c',
'chuni-dll.h',
'config.c', 'config.c',
'config.h', 'config.h',
'dllmain.c', 'dllmain.c',

View File

@ -9,10 +9,9 @@
#include "board/slider-cmd.h" #include "board/slider-cmd.h"
#include "board/slider-frame.h" #include "board/slider-frame.h"
#include "chunihook/chuni-dll.h"
#include "chunihook/slider.h" #include "chunihook/slider.h"
#include "chuniio/chuniio.h"
#include "hook/iobuf.h" #include "hook/iobuf.h"
#include "hook/iohook.h" #include "hook/iohook.h"
@ -41,6 +40,7 @@ static uint8_t slider_readable_bytes[520];
HRESULT slider_hook_init(const struct slider_config *cfg) HRESULT slider_hook_init(const struct slider_config *cfg)
{ {
assert(cfg != NULL); assert(cfg != NULL);
assert(chuni_dll.slider_init != NULL);
if (!cfg->enable) { if (!cfg->enable) {
return S_FALSE; return S_FALSE;
@ -81,11 +81,11 @@ static HRESULT slider_handle_irp_locked(struct irp *irp)
HRESULT hr; HRESULT hr;
if (irp->op == IRP_OP_OPEN) { if (irp->op == IRP_OP_OPEN) {
dprintf("Chunithm slider: Starting backend DLL\n"); dprintf("Chunithm slider: Starting backend\n");
hr = chuni_io_slider_init(); hr = chuni_dll.slider_init();
if (FAILED(hr)) { if (FAILED(hr)) {
dprintf("Chunithm slider: Backend DLL error: %x\n", (int) hr); dprintf("Chunithm slider: Backend error: %x\n", (int) hr);
return hr; return hr;
} }
@ -189,8 +189,10 @@ static HRESULT slider_req_get_board_info(void)
static HRESULT slider_req_auto_scan_start(void) static HRESULT slider_req_auto_scan_start(void)
{ {
assert(chuni_dll.slider_start != NULL);
dprintf("Chunithm slider: Start slider notifications\n"); dprintf("Chunithm slider: Start slider notifications\n");
chuni_io_slider_start(slider_res_auto_scan); chuni_dll.slider_start(slider_res_auto_scan);
/* This message is not acknowledged */ /* This message is not acknowledged */
@ -201,6 +203,8 @@ static HRESULT slider_req_auto_scan_stop(void)
{ {
struct slider_hdr resp; struct slider_hdr resp;
assert(chuni_dll.slider_stop != NULL);
dprintf("Chunithm slider: Stop slider notifications\n"); dprintf("Chunithm slider: Stop slider notifications\n");
/* IO DLL worker thread might attempt to invoke the callback (which needs /* IO DLL worker thread might attempt to invoke the callback (which needs
@ -209,7 +213,7 @@ static HRESULT slider_req_auto_scan_stop(void)
situation. */ situation. */
LeaveCriticalSection(&slider_lock); LeaveCriticalSection(&slider_lock);
chuni_io_slider_stop(); chuni_dll.slider_stop();
EnterCriticalSection(&slider_lock); EnterCriticalSection(&slider_lock);
resp.sync = SLIDER_FRAME_SYNC; resp.sync = SLIDER_FRAME_SYNC;
@ -221,7 +225,9 @@ static HRESULT slider_req_auto_scan_stop(void)
static HRESULT slider_req_set_led(const struct slider_req_set_led *req) static HRESULT slider_req_set_led(const struct slider_req_set_led *req)
{ {
chuni_io_slider_set_leds(req->payload.rgb); assert(chuni_dll.slider_set_leds != NULL);
chuni_dll.slider_set_leds(req->payload.rgb);
/* This message is not acknowledged */ /* This message is not acknowledged */