segatools/mu3hook/mu3-dll.c

141 lines
3.8 KiB
C
Raw Permalink Normal View History

2021-06-06 18:23:55 +00:00
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include "mu3hook/mu3-dll.h"
#include "util/dll-bind.h"
#include "util/dprintf.h"
const struct dll_bind_sym mu3_dll_syms[] = {
{
.sym = "mu3_io_init",
.off = offsetof(struct mu3_dll, init),
}, {
.sym = "mu3_io_poll",
.off = offsetof(struct mu3_dll, poll),
}, {
.sym = "mu3_io_get_opbtns",
.off = offsetof(struct mu3_dll, get_opbtns),
}, {
.sym = "mu3_io_get_gamebtns",
.off = offsetof(struct mu3_dll, get_gamebtns),
}, {
.sym = "mu3_io_get_lever",
.off = offsetof(struct mu3_dll, get_lever),
}, {
.sym = "mu3_io_led_init",
.off = offsetof(struct mu3_dll, led_init),
}, {
.sym = "mu3_io_led_set_colors",
.off = offsetof(struct mu3_dll, led_set_leds),
2021-06-06 18:23:55 +00:00
}
};
/* Helper function to determine upon dll_bind failure whether the required functions were found
NOTE: relies on symbols order declared above */
static HRESULT has_enough_symbols(uint16_t version, uint8_t count)
{
if ( version <= 0x0100 && count == 5 )
return S_OK;
if ( version >= 0x0101 && count == 7 )
return S_OK;
return E_FAIL;
}
2021-06-06 18:23:55 +00:00
struct mu3_dll mu3_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 mu3_dll_init(const struct mu3_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("Ongeki IO: Failed to load IO DLL: %lx: %S\n",
2021-06-06 18:23:55 +00:00
hr,
cfg->path);
goto end;
}
dprintf("Ongeki IO: Using custom IO DLL: %S\n", cfg->path);
src = owned;
} else {
owned = NULL;
src = self;
}
get_api_version = (void *) GetProcAddress(src, "mu3_io_get_api_version");
if (get_api_version != NULL) {
mu3_dll.api_version = get_api_version();
} else {
mu3_dll.api_version = 0x0100;
dprintf("Custom IO DLL does not expose mu3_io_get_api_version, "
"assuming API version 1.0.\n"
"Please ask the developer to update their DLL.\n");
}
if (mu3_dll.api_version >= 0x0200) {
hr = E_NOTIMPL;
dprintf("Ongeki IO: Custom IO DLL implements an unsupported "
"API version (%#04x). Please update Segatools.\n",
mu3_dll.api_version);
goto end;
}
sym = mu3_dll_syms;
const struct dll_bind_sym *init_sym = &sym[0];
2021-06-06 18:23:55 +00:00
hr = dll_bind(&mu3_dll, src, &sym, _countof(mu3_dll_syms));
if (FAILED(hr)) {
if (src != self) {
// Might still be ok depending on external dll API version
int bind_count = sym - init_sym;
if (has_enough_symbols(mu3_dll.api_version, bind_count) == S_OK)
{
hr = S_OK;
} else {
dprintf("Ongeki IO: Custom IO DLL does not provide function "
"\"%s\". Please contact your IO DLL's developer for "
"further assistance.\n",
sym->sym);
goto end;
}
2021-06-06 18:23:55 +00:00
} else {
dprintf("Internal error: could not reflect \"%s\"\n", sym->sym);
}
}
owned = NULL;
end:
if (owned != NULL) {
FreeLibrary(owned);
}
return hr;
}