Added wacca stub code

This commit is contained in:
2021-12-21 00:02:17 -05:00
parent 68e71c845b
commit 6b2a4e5c65
20 changed files with 764 additions and 1 deletions

43
mercuryhook/config.c Normal file
View File

@ -0,0 +1,43 @@
#include <assert.h>
#include <stddef.h>
#include "board/config.h"
#include "hooklib/config.h"
#include "hooklib/dvd.h"
#include "hooklib/gfx.h"
#include "mercuryhook/config.h"
#include "platform/config.h"
void mercury_dll_config_load(
struct mercury_dll_config *cfg,
const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
GetPrivateProfileStringW(
L"mercuryio",
L"path",
L"",
cfg->path,
_countof(cfg->path),
filename);
}
void mercury_hook_config_load(
struct mercury_hook_config *cfg,
const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
platform_config_load(&cfg->platform, filename);
aime_config_load(&cfg->aime, filename);
dvd_config_load(&cfg->dvd, filename);
io4_config_load(&cfg->io4, filename);
gfx_config_load(&cfg->gfx, filename);
mercury_dll_config_load(&cfg->dll, filename);
}

29
mercuryhook/config.h Normal file
View File

@ -0,0 +1,29 @@
#pragma once
#include <stddef.h>
#include "board/config.h"
#include "hooklib/dvd.h"
#include "hooklib/gfx.h"
#include "mercuryhook/mercury-dll.h"
#include "platform/config.h"
struct mercury_hook_config {
struct platform_config platform;
struct aime_config aime;
struct dvd_config dvd;
struct io4_config io4;
struct gfx_config gfx;
struct mercury_dll_config dll;
};
void mercury_dll_config_load(
struct mercury_dll_config *cfg,
const wchar_t *filename);
void mercury_hook_config_load(
struct mercury_hook_config *cfg,
const wchar_t *filename);

109
mercuryhook/dllmain.c Normal file
View File

@ -0,0 +1,109 @@
#include <windows.h>
#include "board/io4.h"
#include "board/sg-reader.h"
#include "board/vfd.h"
#include "hook/process.h"
#include "hooklib/serial.h"
#include "hooklib/spike.h"
#include "mercuryhook/config.h"
#include "mercuryhook/io4.h"
#include "mercuryhook/mercury-dll.h"
#include "platform/platform.h"
#include "util/dprintf.h"
static HMODULE mercury_hook_mod;
static process_entry_t mercury_startup;
static struct mercury_hook_config mercury_hook_cfg;
/* This hook is based on mu3hook, with leaked mercuryhook i/o codes. */
static DWORD CALLBACK mercury_pre_startup(void)
{
HRESULT hr;
dprintf("--- Begin mercury_pre_startup ---\n");
/* Load config */
mercury_hook_config_load(&mercury_hook_cfg, L".\\segatools.ini");
/* Hook Win32 APIs */
dvd_hook_init(&mercury_hook_cfg.dvd, mercury_hook_mod);
gfx_hook_init(&mercury_hook_cfg.gfx, mercury_hook_mod);
serial_hook_init();
/* Initialize emulation hooks */
hr = platform_hook_init(
&mercury_hook_cfg.platform,
"SDFE",
"ACA1",
mercury_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = sg_reader_hook_init(&mercury_hook_cfg.aime, 1, mercury_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = vfd_hook_init(2);
if (FAILED(hr)) {
goto fail;
}
hr = mercury_dll_init(&mercury_hook_cfg.dll, mercury_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = mercury_io4_hook_init(&mercury_hook_cfg.io4);
if (FAILED(hr)) {
goto fail;
}
/* Initialize debug helpers */
spike_hook_init(L".\\segatools.ini");
dprintf("--- End mercury_pre_startup ---\n");
/* Jump to EXE start address */
return mercury_startup();
fail:
ExitProcess(EXIT_FAILURE);
}
BOOL WINAPI DllMain(HMODULE mod, DWORD cause, void *ctx)
{
HRESULT hr;
if (cause != DLL_PROCESS_ATTACH) {
return TRUE;
}
mercury_hook_mod = mod;
hr = process_hijack_startup(mercury_pre_startup, &mercury_startup);
if (!SUCCEEDED(hr)) {
dprintf("Failed to hijack process startup: %x\n", (int) hr);
}
return SUCCEEDED(hr);
}

64
mercuryhook/io4.c Normal file
View File

@ -0,0 +1,64 @@
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "board/io4.h"
#include "mercuryhook/mercury-dll.h"
#include "util/dprintf.h"
static HRESULT mercury_io4_poll(void *ctx, struct io4_state *state);
static const struct io4_ops mercury_io4_ops = {
.poll = mercury_io4_poll,
};
HRESULT mercury_io4_hook_init(const struct io4_config *cfg)
{
HRESULT hr;
assert(mercury_dll.init != NULL);
hr = io4_hook_init(cfg, &mercury_io4_ops, NULL);
if (FAILED(hr)) {
return hr;
}
return mercury_dll.init();
}
static HRESULT mercury_io4_poll(void *ctx, struct io4_state *state)
{
uint8_t opbtn;
HRESULT hr;
assert(mercury_dll.poll != NULL);
assert(mercury_dll.get_opbtns != NULL);
assert(mercury_dll.get_gamebtns != NULL);
memset(state, 0, sizeof(*state));
hr = mercury_dll.poll();
if (FAILED(hr)) {
return hr;
}
opbtn = 0;
mercury_dll.get_opbtns(&opbtn);
if (opbtn & MAI2_IO_OPBTN_TEST) {
state->buttons[0] |= IO4_BUTTON_TEST;
}
if (opbtn & MAI2_IO_OPBTN_SERVICE) {
state->buttons[0] |= IO4_BUTTON_SERVICE;
}
return S_OK;
}

7
mercuryhook/io4.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include <windows.h>
#include "board/io4.h"
HRESULT mercury_io4_hook_init(const struct io4_config *cfg);

109
mercuryhook/mercury-dll.c Normal file
View File

@ -0,0 +1,109 @@
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include "mercuryhook/mercury-dll.h"
#include "util/dll-bind.h"
#include "util/dprintf.h"
const struct dll_bind_sym mercury_dll_syms[] = {
{
.sym = "mercury_io_init",
.off = offsetof(struct mercury_dll, init),
}, {
.sym = "mercury_io_poll",
.off = offsetof(struct mercury_dll, poll),
}, {
.sym = "mercury_io_get_opbtns",
.off = offsetof(struct mercury_dll, get_opbtns),
}, {
.sym = "mercury_io_get_gamebtns",
.off = offsetof(struct mercury_dll, get_gamebtns),
}
};
struct mercury_dll mercury_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 mercury_dll_init(const struct mercury_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("Wacca IO: Failed to load IO DLL: %lx: %S\n",
hr,
cfg->path);
goto end;
}
dprintf("Wacca IO: Using custom IO DLL: %S\n", cfg->path);
src = owned;
} else {
owned = NULL;
src = self;
}
get_api_version = (void *) GetProcAddress(src, "mercury_io_get_api_version");
if (get_api_version != NULL) {
mercury_dll.api_version = get_api_version();
} else {
mercury_dll.api_version = 0x0100;
dprintf("Custom IO DLL does not expose mercury_io_get_api_version, "
"assuming API version 1.0.\n"
"Please ask the developer to update their DLL.\n");
}
if (mercury_dll.api_version >= 0x0200) {
hr = E_NOTIMPL;
dprintf("Wacca IO: Custom IO DLL implements an unsupported "
"API version (%#04x). Please update Segatools.\n",
mercury_dll.api_version);
goto end;
}
sym = mercury_dll_syms;
hr = dll_bind(&mercury_dll, src, &sym, _countof(mercury_dll_syms));
if (FAILED(hr)) {
if (src != self) {
dprintf("Wacca 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;
}

21
mercuryhook/mercury-dll.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include <windows.h>
#include "mercuryio/mercuryio.h"
struct mercury_dll {
uint16_t api_version;
HRESULT (*init)(void);
HRESULT (*poll)(void);
void (*get_opbtns)(uint8_t *opbtn);
void (*get_gamebtns)(uint16_t *player1, uint16_t *player2);
};
struct mercury_dll_config {
wchar_t path[MAX_PATH];
};
extern struct mercury_dll mercury_dll;
HRESULT mercury_dll_init(const struct mercury_dll_config *cfg, HINSTANCE self);

View File

@ -0,0 +1,19 @@
LIBRARY mercuryhook
EXPORTS
Direct3DCreate9
aime_io_get_api_version
aime_io_init
aime_io_led_set_color
aime_io_nfc_get_aime_id
aime_io_nfc_get_felica_id
aime_io_nfc_poll
amDllVideoClose @2
amDllVideoGetVBiosVersion @4
amDllVideoOpen @1
amDllVideoSetResolution @3
mercury_io_get_api_version
mercury_io_get_gamebtns
mercury_io_get_opbtns
mercury_io_init
mercury_io_poll

29
mercuryhook/meson.build Normal file
View File

@ -0,0 +1,29 @@
shared_library(
'mercuryhook',
name_prefix : '',
include_directories : inc,
implicit_include_directories : false,
vs_module_defs : 'mercuryhook.def',
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
capnhook.get_variable('hooklib_dep'),
],
link_with : [
aimeio_lib,
board_lib,
hooklib_lib,
mercuryio_lib,
platform_lib,
util_lib,
],
sources : [
'config.c',
'config.h',
'dllmain.c',
'io4.c',
'io4.h',
'mercury-dll.c',
'mercury-dll.h'
],
)