Load and bind divaio at runtime

This commit is contained in:
Tau 2021-06-06 09:38:20 -04:00
parent 645e6cba1d
commit d2b9c6034d
8 changed files with 199 additions and 10 deletions

View File

@ -1,5 +1,6 @@
#include <assert.h> #include <assert.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h>
#include "amex/amex.h" #include "amex/amex.h"
#include "amex/config.h" #include "amex/config.h"
@ -12,6 +13,22 @@
#include "platform/config.h" #include "platform/config.h"
#include "platform/platform.h" #include "platform/platform.h"
void diva_dll_config_load(
struct diva_dll_config *cfg,
const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
GetPrivateProfileStringW(
L"divaio",
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);
@ -30,5 +47,6 @@ void diva_hook_config_load(
platform_config_load(&cfg->platform, filename); platform_config_load(&cfg->platform, filename);
amex_config_load(&cfg->amex, filename); amex_config_load(&cfg->amex, filename);
aime_config_load(&cfg->aime, filename); aime_config_load(&cfg->aime, filename);
diva_dll_config_load(&cfg->dll, filename);
slider_config_load(&cfg->slider, filename); slider_config_load(&cfg->slider, filename);
} }

View File

@ -6,6 +6,7 @@
#include "board/sg-reader.h" #include "board/sg-reader.h"
#include "divahook/diva-dll.h"
#include "divahook/slider.h" #include "divahook/slider.h"
#include "platform/platform.h" #include "platform/platform.h"
@ -14,9 +15,13 @@ struct diva_hook_config {
struct platform_config platform; struct platform_config platform;
struct amex_config amex; struct amex_config amex;
struct aime_config aime; struct aime_config aime;
struct diva_dll_config dll;
struct slider_config slider; struct slider_config slider;
}; };
void diva_dll_config_load(
struct diva_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 diva_hook_config_load( void diva_hook_config_load(
struct diva_hook_config *cfg, struct diva_hook_config *cfg,

121
divahook/diva-dll.c Normal file
View File

@ -0,0 +1,121 @@
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include "divahook/diva-dll.h"
#include "util/dll-bind.h"
#include "util/dprintf.h"
const struct dll_bind_sym diva_dll_syms[] = {
{
.sym = "diva_io_jvs_init",
.off = offsetof(struct diva_dll, jvs_init),
}, {
.sym = "diva_io_jvs_poll",
.off = offsetof(struct diva_dll, jvs_poll),
}, {
.sym = "diva_io_jvs_read_coin_counter",
.off = offsetof(struct diva_dll, jvs_read_coin_counter),
}, {
.sym = "diva_io_jvs_set_coin_blocker",
.off = offsetof(struct diva_dll, jvs_set_coin_blocker),
}, {
.sym = "diva_io_slider_init",
.off = offsetof(struct diva_dll, slider_init),
}, {
.sym = "diva_io_slider_start",
.off = offsetof(struct diva_dll, slider_start),
}, {
.sym = "diva_io_slider_stop",
.off = offsetof(struct diva_dll, slider_stop),
}, {
.sym = "diva_io_slider_set_leds",
.off = offsetof(struct diva_dll, slider_set_leds),
}
};
struct diva_dll diva_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 diva_dll_init(const struct diva_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("Diva IO: Failed to load IO DLL: %x: %S\n",
hr,
cfg->path);
goto end;
}
dprintf("Diva IO: Using custom IO DLL: %S\n", cfg->path);
src = owned;
} else {
owned = NULL;
src = self;
}
get_api_version = (void *) GetProcAddress(src, "diva_io_get_api_version");
if (get_api_version != NULL) {
diva_dll.api_version = get_api_version();
} else {
diva_dll.api_version = 0x0100;
dprintf("Custom IO DLL does not expose diva_io_get_api_version, "
"assuming API version 1.0.\n"
"Please ask the developer to update their DLL.\n");
}
if (diva_dll.api_version >= 0x0200) {
hr = E_NOTIMPL;
dprintf("Diva IO: Custom IO DLL implements an unsupported "
"API version (%#04x). Please update Segatools.\n",
diva_dll.api_version);
goto end;
}
sym = diva_dll_syms;
hr = dll_bind(&diva_dll, src, &sym, _countof(diva_dll_syms));
if (FAILED(hr)) {
if (src != self) {
dprintf("Diva 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
divahook/diva-dll.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include <windows.h>
#include "divaio/divaio.h"
struct diva_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)(diva_io_slider_callback_t callback);
void (*slider_stop)(void);
void (*slider_set_leds)(const uint8_t *rgb);
};
struct diva_dll_config {
wchar_t path[MAX_PATH];
};
extern struct diva_dll diva_dll;
HRESULT diva_dll_init(const struct diva_dll_config *cfg, HINSTANCE self);

View File

@ -7,6 +7,7 @@
#include "board/sg-reader.h" #include "board/sg-reader.h"
#include "divahook/config.h" #include "divahook/config.h"
#include "divahook/diva-dll.h"
#include "divahook/jvs.h" #include "divahook/jvs.h"
#include "divahook/slider.h" #include "divahook/slider.h"
@ -50,6 +51,12 @@ static DWORD CALLBACK diva_pre_startup(void)
goto fail; goto fail;
} }
hr = diva_dll_init(&diva_hook_cfg.dll, diva_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = amex_hook_init(&diva_hook_cfg.amex, diva_jvs_init); hr = amex_hook_init(&diva_hook_cfg.amex, diva_jvs_init);
if (FAILED(hr)) { if (FAILED(hr)) {

View File

@ -9,7 +9,7 @@
#include "board/io3.h" #include "board/io3.h"
#include "divaio/divaio.h" #include "divahook/diva-dll.h"
#include "jvs/jvs-bus.h" #include "jvs/jvs-bus.h"
@ -33,9 +33,10 @@ HRESULT diva_jvs_init(struct jvs_node **out)
HRESULT hr; HRESULT hr;
assert(out != NULL); assert(out != NULL);
assert(diva_dll.jvs_init != NULL);
dprintf("JVS I/O: Starting Diva backend DLL\n"); dprintf("JVS I/O: Starting Diva backend DLL\n");
hr = diva_io_jvs_init(); hr = diva_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);
@ -55,11 +56,12 @@ static void diva_jvs_read_switches(void *ctx, struct io3_switch_state *out)
uint8_t gamebtn; uint8_t gamebtn;
assert(out != NULL); assert(out != NULL);
assert(diva_dll.jvs_poll != NULL);
opbtn = 0; opbtn = 0;
gamebtn = 0; gamebtn = 0;
diva_io_jvs_poll(&opbtn, &gamebtn); diva_dll.jvs_poll(&opbtn, &gamebtn);
if (gamebtn & 0x01) { if (gamebtn & 0x01) {
out->p1 |= 1 << 6; out->p1 |= 1 << 6;
@ -97,9 +99,11 @@ static void diva_jvs_read_coin_counter(
uint8_t slot_no, uint8_t slot_no,
uint16_t *out) uint16_t *out)
{ {
assert(diva_dll.jvs_read_coin_counter != NULL);
if (slot_no > 0) { if (slot_no > 0) {
return; return;
} }
diva_io_jvs_read_coin_counter(out); diva_dll.jvs_read_coin_counter(out);
} }

View File

@ -22,6 +22,8 @@ shared_library(
sources : [ sources : [
'config.c', 'config.c',
'config.h', 'config.h',
'diva-dll.c',
'diva-dll.h',
'dllmain.c', 'dllmain.c',
'jvs.c', 'jvs.c',
'jvs.h', 'jvs.h',

View File

@ -8,10 +8,9 @@
#include "board/slider-cmd.h" #include "board/slider-cmd.h"
#include "board/slider-frame.h" #include "board/slider-frame.h"
#include "divahook/diva-dll.h"
#include "divahook/slider.h" #include "divahook/slider.h"
#include "divaio/divaio.h"
#include "hook/iobuf.h" #include "hook/iobuf.h"
#include "hook/iohook.h" #include "hook/iohook.h"
@ -80,9 +79,11 @@ static HRESULT slider_handle_irp_locked(struct irp *irp)
struct iobuf req_iobuf; struct iobuf req_iobuf;
HRESULT hr; HRESULT hr;
assert(diva_dll.slider_init != NULL);
if (irp->op == IRP_OP_OPEN) { if (irp->op == IRP_OP_OPEN) {
dprintf("Diva slider: Starting backend DLL\n"); dprintf("Diva slider: Starting backend DLL\n");
hr = diva_io_slider_init(); hr = diva_dll.slider_init();
if (FAILED(hr)) { if (FAILED(hr)) {
dprintf("Diva slider: Backend DLL error: %x\n", (int) hr); dprintf("Diva slider: Backend DLL error: %x\n", (int) hr);
@ -207,8 +208,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(diva_dll.slider_start != NULL);
dprintf("Diva slider: Start slider thread\n"); dprintf("Diva slider: Start slider thread\n");
diva_io_slider_start(slider_res_auto_scan); diva_dll.slider_start(slider_res_auto_scan);
/* This message is not acknowledged */ /* This message is not acknowledged */
@ -219,6 +222,8 @@ static HRESULT slider_req_auto_scan_stop(void)
{ {
struct slider_hdr resp; struct slider_hdr resp;
assert(diva_dll.slider_stop != NULL);
dprintf("Diva slider: Stop slider thread\n"); dprintf("Diva slider: Stop slider thread\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
@ -227,7 +232,7 @@ static HRESULT slider_req_auto_scan_stop(void)
situation. */ situation. */
LeaveCriticalSection(&slider_lock); LeaveCriticalSection(&slider_lock);
diva_io_slider_stop(); diva_dll.slider_stop();
EnterCriticalSection(&slider_lock); EnterCriticalSection(&slider_lock);
resp.sync = SLIDER_FRAME_SYNC; resp.sync = SLIDER_FRAME_SYNC;
@ -239,8 +244,10 @@ 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)
{ {
assert(diva_dll.slider_set_leds != NULL);
/* This message is not acknowledged */ /* This message is not acknowledged */
diva_io_slider_set_leds(req->payload.rgb); diva_dll.slider_set_leds(req->payload.rgb);
return S_OK; return S_OK;
} }