segatools/hkbhook/dllmain.c

155 lines
3.2 KiB
C

#include <windows.h>
#include <stdlib.h>
#include "board/io4.h"
#include "board/sg-reader.h"
#include "board/vfd.h"
#include "gfxhook/d3d9.h"
#include "gfxhook/d3d11.h"
#include "gfxhook/dxgi.h"
#include "gfxhook/gfx.h"
#include "hook/process.h"
#include "hooklib/dvd.h"
#include "hooklib/serial.h"
#include "hooklib/spike.h"
#include "hooklib/cursor.h"
#include "hkbhook/config.h"
#include "hkbhook/io4.h"
#include "hkbhook/led.h"
#include "hkbhook/hkb-dll.h"
#include "hkbhook/unity.h"
#include "platform/platform.h"
#include "util/dprintf.h"
static HMODULE hkb_hook_mod;
static process_entry_t hkb_startup;
static struct hkb_hook_config hkb_hook_cfg;
static DWORD CALLBACK hkb_pre_startup(void)
{
HRESULT hr;
dprintf("--- Begin hkb_pre_startup ---\n");
/* Load config */
hkb_hook_config_load(&hkb_hook_cfg, L".\\segatools.ini");
/* Hook Win32 APIs */
dvd_hook_init(&hkb_hook_cfg.dvd, hkb_hook_mod);
gfx_hook_init(&hkb_hook_cfg.gfx);
gfx_d3d9_hook_init(&hkb_hook_cfg.gfx, hkb_hook_mod);
gfx_d3d11_hook_init(&hkb_hook_cfg.gfx, hkb_hook_mod);
gfx_dxgi_hook_init(&hkb_hook_cfg.gfx, hkb_hook_mod);
serial_hook_init();
/* Initialize emulation hooks */
hr = platform_hook_init(
&hkb_hook_cfg.platform,
"SDEC",
"ACA2",
hkb_hook_mod);
if (FAILED(hr)) {
goto fail;
}
cursor_hook_init();
// COM1: Touch
// COM2: LED (Satalite)
// COM3: Reader (Satalite)
// COM3: LED (Terminal)
if (FAILED(hr)) {
goto fail;
}
if (hkb_hook_cfg.platform.nusec.platform_id[0] != '\0' && hkb_hook_cfg.platform.nusec.platform_id[3] == '4') {
hr = led_hook_init(&hkb_hook_cfg.led, 3);
if (FAILED(hr)) {
goto fail;
}
hr = sg_reader_hook_init(&hkb_hook_cfg.aime, 1, hkb_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = vfd_hook_init(4);
}
else {
hr = led_hook_init(&hkb_hook_cfg.led, 2);
if (FAILED(hr)) {
goto fail;
}
hr = sg_reader_hook_init(&hkb_hook_cfg.aime, 3, hkb_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = vfd_hook_init(1);
}
if (FAILED(hr)) {
goto fail;
}
hr = hkb_dll_init(&hkb_hook_cfg.dll, hkb_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = hkb_io4_hook_init(&hkb_hook_cfg.io4);
if (FAILED(hr)) {
goto fail;
}
/* Initialize Unity native plugin DLL hooks
There seems to be an issue with other DLL hooks if `LoadLibraryW` is
hooked earlier in the `hkbhook` initialization. */
unity_hook_init();
/* Initialize debug helpers */
spike_hook_init(L".\\segatools.ini");
dprintf("--- End hkb_pre_startup ---\n");
/* Jump to EXE start address */
return hkb_startup();
fail:
ExitProcess(EXIT_FAILURE);
}
BOOL WINAPI DllMain(HMODULE mod, DWORD cause, void *ctx)
{
HRESULT hr;
if (cause != DLL_PROCESS_ATTACH) {
return TRUE;
}
hkb_hook_mod = mod;
hr = process_hijack_startup(hkb_pre_startup, &hkb_startup);
if (!SUCCEEDED(hr)) {
dprintf("Failed to hijack process startup: %x\n", (int) hr);
}
return SUCCEEDED(hr);
}