17 Commits

120 changed files with 3074 additions and 282 deletions

33
.vscode/settings.json vendored
View File

@ -1,4 +1,37 @@
{
"editor.formatOnSave": false,
"mesonbuild.configureOnOpen": false,
"files.associations": {
"string.h": "c",
"stdbool.h": "c",
"windows.h": "c",
"dprintf.h": "c",
"touch.h": "c",
"mai2-dll.h": "c",
"led.h": "c",
"path.h": "c",
"reg.h": "c",
"platform.h": "c",
"procaddr.h": "c",
"table.h": "c",
"serial.h": "c",
"stdarg.h": "c",
"iphlpapi.h": "c",
"iptypes.h": "c",
"netenv.h": "c",
"nusec.h": "c",
"vfs.h": "c",
"ws2ipdef.h": "c",
"winternl.h": "c",
"wincrypt.h": "c",
"assert.h": "c",
"stdint.h": "c",
"limits.h": "c",
"stdlib.h": "c",
"config.h": "c",
"mu3-dll.h": "c",
"io4.h": "c",
"dvd.h": "c",
"uart.h": "c"
},
}

View File

@ -104,6 +104,36 @@ $(BUILD_DIR_ZIP)/mu3.zip:
$(V)strip $(BUILD_DIR_ZIP)/mu3/*.{exe,dll}
$(V)cd $(BUILD_DIR_ZIP)/mu3 ; zip -r ../mu3.zip *
$(BUILD_DIR_ZIP)/hkb.zip:
$(V)echo ... $@
$(V)mkdir -p $(BUILD_DIR_ZIP)/hkb
$(V)mkdir -p $(BUILD_DIR_ZIP)/hkb/DEVICE
$(V)cp $(BUILD_DIR_64)/subprojects/capnhook/inject/inject.exe \
$(BUILD_DIR_64)/hkbhook/hkbhook.dll \
$(DIST_DIR)/hkb/segatools.ini \
$(DIST_DIR)/hkb/start.bat \
$(BUILD_DIR_ZIP)/hkb
$(V)cp pki/billing.pub \
pki/ca.crt \
$(BUILD_DIR_ZIP)/hkb/DEVICE
$(V)strip $(BUILD_DIR_ZIP)/hkb/*.{exe,dll}
$(V)cd $(BUILD_DIR_ZIP)/hkb ; zip -r ../hkb.zip *
$(BUILD_DIR_ZIP)/mai2.zip:
$(V)echo ... $@
$(V)mkdir -p $(BUILD_DIR_ZIP)/mai2
$(V)mkdir -p $(BUILD_DIR_ZIP)/mai2/DEVICE
$(V)cp $(BUILD_DIR_64)/subprojects/capnhook/inject/inject.exe \
$(BUILD_DIR_64)/mai2hook/mai2hook.dll \
$(DIST_DIR)/mai2/segatools.ini \
$(DIST_DIR)/mai2/start.bat \
$(BUILD_DIR_ZIP)/mai2
$(V)cp pki/billing.pub \
pki/ca.crt \
$(BUILD_DIR_ZIP)/mai2/DEVICE
$(V)strip $(BUILD_DIR_ZIP)/mai2/*.{exe,dll}
$(V)cd $(BUILD_DIR_ZIP)/mai2 ; zip -r ../mai2.zip *
$(BUILD_DIR_ZIP)/doc.zip: \
$(DOC_DIR)/config \
$(DOC_DIR)/chunihook.md \
@ -121,6 +151,8 @@ $(BUILD_DIR_ZIP)/segatools.zip: \
$(BUILD_DIR_ZIP)/idz.zip \
$(BUILD_DIR_ZIP)/mercury.zip \
$(BUILD_DIR_ZIP)/mu3.zip \
$(BUILD_DIR_ZIP)/hkb.zip \
$(BUILD_DIR_ZIP)/mai2.zip \
CHANGELOG.md \
README.md \

View File

@ -1,33 +1,2 @@
# Segatools
Version: `v005`
Loaders and hardware emulators for SEGA games that run on the Nu and ALLS platforms.
## List of supported games
* Chunithm
* [Chunithm (Plus)](doc/chunihook.md)
* [Chunithm Air (Plus)](doc/chunihook.md)
* [Chunithm Star (Plus)](doc/chunihook.md)
* [Chunithm Amazon (Plus)](doc/chunihook.md)
* [Chunithm Crystal (Plus)](doc/chunihook.md)
* Initial D
* [Initial D Arcade Stage Zero](doc/idzhook.md)
* Wacca
* Wacca Lilly R (WIP)
## End-users
For setup and configuration guides, refer to the dedicated documents available for each game, see
[the links in the previous section](#list-of-supported-games).
## Contributors
If you are/want to be a contributor of any kind, e.g. new features, bug fixes, documentation improvements, ..., please
read the [contributing documentation](CONTRIBUTING.md), first.
## Developers
For development setup and instructions how to build the project, refer to the
[dedicated development documentation](doc/development.md).
# This repository is no longer maintained
Please see the new joint repository from [TeamTofuShop](https://gitea.tendokyu.moe/TeamTofuShop/segatools)

View File

@ -3,7 +3,6 @@ aimeio_lib = static_library(
name_prefix : '',
include_directories: inc,
implicit_include_directories : false,
c_pch : '../precompiled.h',
link_with : [
util_lib,
],

View File

@ -1,6 +1,7 @@
#include <windows.h>
#include <devioctl.h>
#include <ntdddisk.h>
#include <stdlib.h>
#include <winioctl.h>
#include <assert.h>
#include <ctype.h>
@ -19,13 +20,11 @@
#include "util/dprintf.h"
#include "util/str.h"
#pragma pack(push, 1)
#define DS_IOCTL_GET_ABI_VERSION CTL_CODE(0x8000, 0x800, METHOD_BUFFERED, FILE_READ_ACCESS)
#define DS_IOCTL_SETUP CTL_CODE(0x8000, 0x801, METHOD_BUFFERED, FILE_READ_ACCESS)
#define DS_IOCTL_READ_SECTOR CTL_CODE(0x8000, 0x804, METHOD_BUFFERED, FILE_READ_ACCESS)
enum {
DS_IOCTL_GET_ABI_VERSION = 0x80006000,
DS_IOCTL_SETUP = 0x80006004,
DS_IOCTL_READ_SECTOR = 0x80006010,
};
#pragma pack(push, 1)
struct ds_eeprom {
uint32_t crc32;

View File

@ -6,7 +6,7 @@
#include <winnt.h>
#endif
#include <devioctl.h>
#include <ntdddisk.h>
#include <winioctl.h>
#include <assert.h>
@ -20,9 +20,7 @@
#include "util/dprintf.h"
#include "util/str.h"
enum {
EEPROM_IOCTL_GET_ABI_VERSION = 0x80006000,
};
#define EEPROM_IOCTL_GET_ABI_VERSION CTL_CODE(0x8000, 0x800, METHOD_BUFFERED, FILE_READ_ACCESS)
static HRESULT eeprom_handle_irp(struct irp *irp);
static HRESULT eeprom_handle_open(struct irp *irp);

View File

@ -1,5 +1,6 @@
#include <windows.h>
#include <ntstatus.h>
#include <winioctl.h>
#include <assert.h>
#include <string.h>
@ -13,12 +14,10 @@
#include "util/dprintf.h"
#include "util/str.h"
enum {
GPIO_IOCTL_SET_LEDS = 0x8000A004,
GPIO_IOCTL_GET_PSW = 0x80006008,
GPIO_IOCTL_GET_DIPSW = 0x8000600C,
GPIO_IOCTL_DESCRIBE = 0x80006014,
};
#define GPIO_IOCTL_SET_LEDS CTL_CODE(0x8000, 0x801, METHOD_BUFFERED, FILE_WRITE_ACCESS)
#define GPIO_IOCTL_GET_PSW CTL_CODE(0x8000, 0x802, METHOD_BUFFERED, FILE_READ_ACCESS)
#define GPIO_IOCTL_GET_DIPSW CTL_CODE(0x8000, 0x803, METHOD_BUFFERED, FILE_READ_ACCESS)
#define GPIO_IOCTL_DESCRIBE CTL_CODE(0x8000, 0x805, METHOD_BUFFERED, FILE_READ_ACCESS)
enum {
GPIO_TYPE_NONE = 0,

View File

@ -4,6 +4,7 @@
#include <winternl.h>
#include <ntstatus.h>
#include <winioctl.h>
#include <assert.h>
#include <stddef.h>
@ -21,11 +22,9 @@
#include "util/dump.h"
#include "util/str.h"
enum {
JVS_IOCTL_HELLO = 0x80006004,
JVS_IOCTL_SENSE = 0x8000600C,
JVS_IOCTL_TRANSACT = 0x8000E008,
};
#define JVS_IOCTL_HELLO CTL_CODE(0x8000, 0x801, METHOD_BUFFERED, FILE_READ_ACCESS)
#define JVS_IOCTL_TRANSACT CTL_CODE(0x8000, 0x802, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
#define JVS_IOCTL_SENSE CTL_CODE(0x8000, 0x803, METHOD_BUFFERED, FILE_READ_ACCESS)
static HRESULT jvs_handle_irp(struct irp *irp);
static HRESULT jvs_handle_open(struct irp *irp);

View File

@ -2,7 +2,6 @@ amex_lib = static_library(
'amex',
include_directories : inc,
implicit_include_directories : false,
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
],

View File

@ -6,7 +6,7 @@
#include <winnt.h>
#endif
#include <devioctl.h>
#include <ntdddisk.h>
#include <winioctl.h>
#include <assert.h>
@ -20,9 +20,7 @@
#include "util/dprintf.h"
#include "util/str.h"
enum {
SRAM_IOCTL_GET_ABI_VERSION = 0x80006000,
};
#define SRAM_IOCTL_GET_ABI_VERSION CTL_CODE(0x8000, 0x800, METHOD_BUFFERED, FILE_READ_ACCESS)
static HRESULT sram_handle_irp(struct irp *irp);
static HRESULT sram_handle_open(struct irp *irp);

View File

@ -16,6 +16,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include "board/io3.h"

View File

@ -3,6 +3,7 @@
#include <windows.h>
#include <stdint.h>
#include <stdbool.h>
enum {
/* System buttons in button[0] */

View File

@ -2,7 +2,6 @@ board_lib = static_library(
'board',
include_directories : inc,
implicit_include_directories : false,
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
],

View File

@ -4,7 +4,6 @@ shared_library(
include_directories : inc,
implicit_include_directories : false,
vs_module_defs : 'carolhook.def',
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
capnhook.get_variable('hooklib_dep'),

View File

@ -3,7 +3,6 @@ carolio_lib = static_library(
name_prefix : '',
include_directories : inc,
implicit_include_directories : false,
c_pch : '../precompiled.h',
sources : [
'carolio.c',
'carolio.h',

View File

@ -3,6 +3,7 @@
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include "amex/amex.h"
#include "amex/config.h"

View File

@ -4,7 +4,6 @@ shared_library(
include_directories : inc,
implicit_include_directories : false,
vs_module_defs : 'chunihook.def',
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
capnhook.get_variable('hooklib_dep'),

View File

@ -3,6 +3,7 @@
#include <process.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "chuniio/chuniio.h"
#include "chuniio/config.h"

View File

@ -1,5 +1,6 @@
#include <windows.h>
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>
#include <stdio.h>

View File

@ -3,7 +3,6 @@ chuniio_lib = static_library(
name_prefix : '',
include_directories : inc,
implicit_include_directories : false,
c_pch : '../precompiled.h',
sources : [
'chuniio.c',
'chuniio.h',

View File

@ -1,11 +1,12 @@
#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "cxbhook/led.h"
#include "cxbhook/cxb-dll.h"
#include "hooklib/procaddr.h"
#include "hook/procaddr.h"
#include "hook/table.h"
@ -50,7 +51,7 @@ static struct hook_symbol lamp_syms[] = {
HRESULT led_hook_init(struct led_config *cfg)
{
dprintf("LED: Init\n");
return proc_addr_table_push("CommLamp.dll", lamp_syms, _countof(lamp_syms));
return proc_addr_table_push(NULL, "CommLamp.dll", lamp_syms, _countof(lamp_syms));
}
static int my_cCommLamp_Open(char *port)

View File

@ -4,7 +4,6 @@ shared_library(
include_directories : inc,
implicit_include_directories : false,
vs_module_defs : 'cxbhook.def',
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
capnhook.get_variable('hooklib_dep'),

View File

@ -3,6 +3,7 @@
#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include <limits.h>
struct network_config {
bool enable;

View File

@ -2,11 +2,12 @@
#include <stdbool.h>
#include <stdint.h>
#include <winuser.h>
#include <stdlib.h>
#include "cxbhook/revio.h"
#include "cxbhook/cxb-dll.h"
#include "hooklib/procaddr.h"
#include "hook/procaddr.h"
#include "hook/table.h"
@ -83,7 +84,7 @@ static struct hook_symbol revio_syms[] = {
HRESULT revio_hook_init(struct revio_config *cfg)
{
dprintf("Revio: Init\n");
return proc_addr_table_push("CommIo.dll", revio_syms, _countof(revio_syms));
return proc_addr_table_push(NULL, "CommIo.dll", revio_syms, _countof(revio_syms));
}
static int my_cCommIo_Open(char *port)

View File

@ -3,7 +3,6 @@ cxbio_lib = static_library(
name_prefix : '',
include_directories : inc,
implicit_include_directories : false,
c_pch : '../precompiled.h',
sources : [
'cxbio.c',
'cxbio.h',

41
dist/hkb/segatools.ini vendored Normal file
View File

@ -0,0 +1,41 @@
[vfs]
; Insert the path to the game AMFS directory here (contains ICF1 and ICF2)
amfs=amfs
; Create an empty directory somewhere and insert the path here.
; This directory may be shared between multiple SEGA games.
; NOTE: This has nothing to do with Windows %APPDATA%.
appdata=appdata
option=Option
[dns]
; Insert the hostname or IP address of the server you wish to use here.
; Note that 127.0.0.1, localhost etc are specifically rejected.
default=127.0.0.1
[ds]
; Region code on the emulated AMEX board DS EEPROM.
; 1: Japan
; 4: Export (some UI elements in English)
;
; NOTE: Changing this setting causes a factory reset.
region=1
[netenv]
; Simulate an ideal LAN environment. This may interfere with head-to-head play.
; SEGA games are somewhat picky about their LAN environment, so leaving this
; setting enabled is recommended.
enable=1
[keychip]
; The /24 LAN subnet that the emulated keychip will tell the game to expect.
; If you disable netenv then you must set this to your LAN's IP subnet, and
; that subnet must start with 192.168.
subnet=192.168.155.0
[gfx]
enable=1
[io4]
test=0x31
service=0x32
coin=0x33

11
dist/hkb/start.bat vendored Normal file
View File

@ -0,0 +1,11 @@
@echo off
pushd %~dp0
taskkill /f /im amdaemon.exe > nul 2>&1
start inject.exe -d -k hkbhook.dll amdaemon.exe -f -c common.json terminal.json satellite.json
inject.exe -d -k hkbhook.dll hkb.exe -screen-fullscreen 0 -screen-quality Fantastic -silent-crashes -logFile gameLog.txt
taskkill /f /im amdaemon.exe > nul 2>&1
echo Game processes have terminated

44
dist/mai2/segatools.ini vendored Normal file
View File

@ -0,0 +1,44 @@
[vfs]
; Insert the path to the game AMFS directory here (contains ICF1 and ICF2)
amfs=amfs
; Create an empty directory somewhere and insert the path here.
; This directory may be shared between multiple SEGA games.
; NOTE: This has nothing to do with Windows %APPDATA%.
appdata=appdata
option=option
[dns]
; Insert the hostname or IP address of the server you wish to use here.
; Note that 127.0.0.1, localhost etc are specifically rejected.
default=127.0.0.1
[ds]
; Region code on the emulated AMEX board DS EEPROM.
; 1: Japan
; 4: Export (some UI elements in English)
;
; NOTE: Changing this setting causes a factory reset.
region=1
[netenv]
; Simulate an ideal LAN environment. This may interfere with head-to-head play.
; SEGA games are somewhat picky about their LAN environment, so leaving this
; setting enabled is recommended.
enable=1
[keychip]
; The /24 LAN subnet that the emulated keychip will tell the game to expect.
; If you disable netenv then you must set this to your LAN's IP subnet, and
; that subnet must start with 192.168.
subnet=192.168.172.0
[gfx]
enable=1
[io4]
; Delete
test=0x2E
; End
service=0x23
; Insert
coin=0x2D

11
dist/mai2/start.bat vendored Normal file
View File

@ -0,0 +1,11 @@
@echo off
pushd %~dp0
taskkill /f /im amdaemon.exe > nul 2>&1
start inject -d -k mai2hook.dll amdaemon.exe -f -c config_client.json config_common.json config_server.json
inject.exe -d -k mai2hook.dll Sinmai.exe -screen-fullscreen 0 -screen-width 2160 -screen-height 1920
taskkill /f /im amdaemon.exe > nul 2>&1
echo Game processes have terminated

View File

@ -4,7 +4,6 @@ shared_library(
include_directories : inc,
implicit_include_directories : false,
vs_module_defs : 'divahook.def',
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
capnhook.get_variable('hooklib_dep'),

View File

@ -3,7 +3,6 @@ divaio_lib = static_library(
name_prefix : '',
include_directories : inc,
implicit_include_directories : false,
c_pch : '../precompiled.h',
sources : [
'divaio.c',
'divaio.h',

View File

@ -2,7 +2,6 @@ gfxhook_lib = static_library(
'gfxhook',
include_directories : inc,
implicit_include_directories : false,
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
dxguid_lib,

53
hkbhook/config.c Normal file
View File

@ -0,0 +1,53 @@
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include "board/config.h"
#include "gfxhook/config.h"
#include "hooklib/config.h"
#include "hooklib/dvd.h"
#include "hkbhook/config.h"
#include "platform/config.h"
void hkb_dll_config_load(
struct hkb_dll_config *cfg,
const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
GetPrivateProfileStringW(
L"hkbio",
L"path",
L"",
cfg->path,
_countof(cfg->path),
filename);
}
void hkb_led_config_load(
struct hkb_led_config *cfg,
const wchar_t *filename)
{
cfg->enable = GetPrivateProfileIntW(L"led", L"enable", 1, filename);
}
void hkb_hook_config_load(
struct hkb_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);
hkb_dll_config_load(&cfg->dll, filename);
hkb_led_config_load(&cfg->led, filename);
}

33
hkbhook/config.h Normal file
View File

@ -0,0 +1,33 @@
#pragma once
#include <stddef.h>
#include "board/config.h"
#include "gfxhook/gfx.h"
#include "hooklib/dvd.h"
#include "hkbhook/hkb-dll.h"
#include "platform/config.h"
#include "hkbhook/led.h"
struct hkb_hook_config {
struct platform_config platform;
struct aime_config aime;
struct dvd_config dvd;
struct io4_config io4;
struct gfx_config gfx;
struct hkb_dll_config dll;
struct hkb_led_config led;
};
void hkb_dll_config_load(
struct hkb_dll_config *cfg,
const wchar_t *filename);
void hkb_hook_config_load(
struct hkb_hook_config *cfg,
const wchar_t *filename);

154
hkbhook/dllmain.c Normal file
View File

@ -0,0 +1,154 @@
#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);
}

103
hkbhook/hkb-dll.c Normal file
View File

@ -0,0 +1,103 @@
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include "hkbhook/hkb-dll.h"
#include "util/dll-bind.h"
#include "util/dprintf.h"
const struct dll_bind_sym hkb_dll_syms[] = {
{
.sym = "hkb_io_init",
.off = offsetof(struct hkb_dll, init),
}, {
.sym = "hkb_io_poll",
.off = offsetof(struct hkb_dll, poll),
},
};
struct hkb_dll hkb_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 hkb_dll_init(const struct hkb_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("Ongeki IO: Failed to load IO DLL: %lx: %S\n",
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, "hkb_io_get_api_version");
if (get_api_version != NULL) {
hkb_dll.api_version = get_api_version();
} else {
hkb_dll.api_version = 0x0100;
dprintf("Custom IO DLL does not expose hkb_io_get_api_version, "
"assuming API version 1.0.\n"
"Please ask the developer to update their DLL.\n");
}
if (hkb_dll.api_version >= 0x0200) {
hr = E_NOTIMPL;
dprintf("Ongeki IO: Custom IO DLL implements an unsupported "
"API version (%#04x). Please update Segatools.\n",
hkb_dll.api_version);
goto end;
}
sym = hkb_dll_syms;
hr = dll_bind(&hkb_dll, src, &sym, _countof(hkb_dll_syms));
if (FAILED(hr)) {
if (src != self) {
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;
} else {
dprintf("Internal error: could not reflect \"%s\"\n", sym->sym);
}
}
owned = NULL;
end:
if (owned != NULL) {
FreeLibrary(owned);
}
return hr;
}

19
hkbhook/hkb-dll.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <windows.h>
#include "hkbio/hkbio.h"
struct hkb_dll {
uint16_t api_version;
HRESULT (*init)(void);
HRESULT (*poll)(uint8_t *opbtn, uint8_t *gamebtn);
};
struct hkb_dll_config {
wchar_t path[MAX_PATH];
};
extern struct hkb_dll hkb_dll;
HRESULT hkb_dll_init(const struct hkb_dll_config *cfg, HINSTANCE self);

22
hkbhook/hkbhook.def Normal file
View File

@ -0,0 +1,22 @@
LIBRARY hkbhook
EXPORTS
CreateDXGIFactory
CreateDXGIFactory1
CreateDXGIFactory2
D3D11CreateDevice
D3D11CreateDeviceAndSwapChain
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
hkb_io_get_api_version
hkb_io_init
hkb_io_poll

109
hkbhook/io4.c Normal file
View File

@ -0,0 +1,109 @@
#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "board/io4.h"
#include "hkbhook/hkb-dll.h"
#include "util/dprintf.h"
bool hkb_io_coin = false;
uint16_t hkb_io_coins = 0;
static HRESULT hkb_io4_poll(void *ctx, struct io4_state *state);
static const struct io4_ops hkb_io4_ops = {
.poll = hkb_io4_poll,
};
HRESULT hkb_io4_hook_init(const struct io4_config *cfg)
{
HRESULT hr;
assert(hkb_dll.init != NULL);
hr = io4_hook_init(cfg, &hkb_io4_ops, NULL);
if (FAILED(hr)) {
return hr;
}
return hkb_dll.init();
}
static HRESULT hkb_io4_poll(void *ctx, struct io4_state *state)
{
uint8_t opbtn;
uint8_t gamebtn;
HRESULT hr;
assert(hkb_dll.poll != NULL);
memset(state, 0, sizeof(*state));
hr = hkb_dll.poll(&opbtn, &gamebtn);
if (FAILED(hr)) {
return hr;
}
if (opbtn & HKB_IO_OPBTN_TEST) {
state->buttons[0] |= IO4_BUTTON_TEST;
}
if (opbtn & HKB_IO_OPBTN_SERVICE) {
state->buttons[0] |= IO4_BUTTON_SERVICE;
}
if (opbtn & HKB_IO_OPBTN_COIN) {
if (!hkb_io_coin) {
hkb_io_coin = true;
hkb_io_coins++;
state->buttons[0] |= 1 << 25;
}
}
else {
hkb_io_coin = false;
}
state->chutes[0] = 128 + 256 * hkb_io_coins;
if (gamebtn & HKB_IO_GAMEBTN_RIGHT) {
state->buttons[1] |= 1 << 1;
}
if (gamebtn & HKB_IO_GAMEBTN_LEFT) {
state->buttons[1] |= 1 << 0;
}
if (gamebtn & HKB_IO_GAMEBTN_UP) {
state->buttons[1] |= 1 << 15;
}
if (gamebtn & HKB_IO_GAMEBTN_DOWN) {
state->buttons[1] |= 1 << 14;
}
if (gamebtn & HKB_IO_GAMEBTN_ENTER) {
state->buttons[1] |= 1 << 13;
}
if (gamebtn & HKB_IO_GAMEBTN_CANCEL) {
state->buttons[1] |= 1 << 12;
}
if (gamebtn & HKB_IO_GAMEBTN_ARR_RIGHT) {
state->buttons[1] |= 1 << 2;
}
if (gamebtn & HKB_IO_GAMEBTN_ARR_LEFT) {
state->buttons[1] |= 1 << 3;
}
return S_OK;
}

7
hkbhook/io4.h Normal file
View File

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

99
hkbhook/led.c Normal file
View File

@ -0,0 +1,99 @@
#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include <assert.h>
#include "hook/iohook.h"
#include "hooklib/uart.h"
#include "util/dprintf.h"
#include "util/dump.h"
#include "hkbhook/led.h"
static HRESULT hkb_led_handle_irp(struct irp *irp);
static HRESULT hkb_led_handle_irp_locked(struct irp *irp);
static CRITICAL_SECTION hkb_led_lock;
static bool hkb_led_started;
static struct uart hkb_led_uart;
static uint8_t hkb_led_written_bytes[520];
static uint8_t hkb_led_readable_bytes[520];
HRESULT led_hook_init(const struct hkb_led_config *cfg, uint8_t port)
{
if (!cfg->enable) {
return S_OK;
}
InitializeCriticalSection(&hkb_led_lock);
uart_init(&hkb_led_uart, port);
hkb_led_uart.written.bytes = hkb_led_written_bytes;
hkb_led_uart.written.nbytes = sizeof(hkb_led_written_bytes);
hkb_led_uart.readable.bytes = hkb_led_readable_bytes;
hkb_led_uart.readable.nbytes = sizeof(hkb_led_readable_bytes);
return iohook_push_handler(hkb_led_handle_irp);
}
static HRESULT hkb_led_handle_irp(struct irp *irp)
{
HRESULT hr;
assert(irp != NULL);
if (!uart_match_irp(&hkb_led_uart, irp)) {
return iohook_invoke_next(irp);
}
EnterCriticalSection(&hkb_led_lock);
hr = hkb_led_handle_irp_locked(irp);
LeaveCriticalSection(&hkb_led_lock);
return hr;
}
static HRESULT hkb_led_handle_irp_locked(struct irp *irp)
{
HRESULT hr;
#if 1
if (irp->op == IRP_OP_WRITE) {
dprintf("WRITE:\n");
dump_const_iobuf(&irp->write);
}
#endif
#if 0
if (irp->op == IRP_OP_READ) {
dprintf("READ:\n");
dump_iobuf(&hkb_led_uart.readable);
}
#endif
if (irp->op == IRP_OP_OPEN) {
/* Unfortunately the card reader UART gets opened and closed
repeatedly */
if (!hkb_led_started) {
dprintf("Led: Open\n");
hkb_led_started = true;
} else {
return S_OK;
}
}
hr = uart_handle_irp(&hkb_led_uart, irp);
if (FAILED(hr) || irp->op != IRP_OP_WRITE) {
return hr;
}
hkb_led_uart.written.pos = 0;
return hr;
}

39
hkbhook/led.h Normal file
View File

@ -0,0 +1,39 @@
#pragma once
#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#pragma pack(push, 1)
struct hkb_led_config {
bool enable;
};
struct hbk_led_packet_head {
uint8_t sync;
uint8_t dst;
uint8_t src;
uint8_t len;
};
struct hbk_led_req_header {
struct hbk_led_packet_head head;
uint8_t cmd;
};
struct hbk_led_resp_header {
struct hbk_led_packet_head head;
uint8_t status;
uint8_t cmd;
uint8_t report;
};
struct hbk_led_packet_tail {
uint8_t checksum;
};
struct hbk_led_req_any {
struct hbk_led_req_header header;
struct hbk_led_packet_tail tail;
};
struct hbk_led_resp_any {
struct hbk_led_resp_header header;
struct hbk_led_packet_tail tail;
};
#pragma pack(pop)
HRESULT led_hook_init(const struct hkb_led_config *cfg, uint8_t port);

34
hkbhook/meson.build Normal file
View File

@ -0,0 +1,34 @@
shared_library(
'hkbhook',
name_prefix : '',
include_directories : inc,
implicit_include_directories : false,
vs_module_defs : 'hkbhook.def',
dependencies : [
capnhook.get_variable('hook_dep'),
capnhook.get_variable('hooklib_dep'),
xinput_lib,
],
link_with : [
aimeio_lib,
board_lib,
gfxhook_lib,
hooklib_lib,
hkbio_lib,
platform_lib,
util_lib,
],
sources : [
'config.c',
'config.h',
'dllmain.c',
'io4.c',
'io4.h',
'led.c',
'led.h',
'hkb-dll.c',
'hkb-dll.h',
'unity.h',
'unity.c',
],
)

108
hkbhook/unity.c Normal file
View File

@ -0,0 +1,108 @@
#include <stdbool.h>
#include <windows.h>
#include <stdlib.h>
#include "hook/table.h"
#include "hook/iohook.h"
#include "hooklib/dll.h"
#include "hooklib/path.h"
#include "hooklib/reg.h"
#include "hook/procaddr.h"
#include "hooklib/serial.h"
#include "platform/clock.h"
#include "util/dprintf.h"
static void dll_hook_insert_hooks(HMODULE target);
static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name);
static HMODULE (WINAPI *next_LoadLibraryW)(const wchar_t *name);
static const struct hook_symbol unity_kernel32_syms[] = {
{
.name = "LoadLibraryW",
.patch = my_LoadLibraryW,
.link = (void **) &next_LoadLibraryW,
},
};
static const wchar_t *target_modules[] = {
L"mono.dll",
L"cri_ware_unity.dll",
L"HKBSys_api.dll",
L"amdaemon_api.dll",
};
static const size_t target_modules_len = _countof(target_modules);
void unity_hook_init(void)
{
dll_hook_insert_hooks(NULL);
}
static void dll_hook_insert_hooks(HMODULE target)
{
hook_table_apply(
target,
"kernel32.dll",
unity_kernel32_syms,
_countof(unity_kernel32_syms));
}
static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name)
{
const wchar_t *name_end;
const wchar_t *target_module;
bool already_loaded;
HMODULE result;
size_t name_len;
size_t target_module_len;
if (name == NULL) {
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
// Check if the module is already loaded
already_loaded = GetModuleHandleW(name) != NULL;
// Must call the next handler so the DLL reference count is incremented
result = next_LoadLibraryW(name);
if (!already_loaded && result != NULL) {
name_len = wcslen(name);
for (size_t i = 0; i < target_modules_len; i++) {
target_module = target_modules[i];
target_module_len = wcslen(target_module);
// Check if the newly loaded library is at least the length of
// the name of the target module
if (name_len < target_module_len) {
continue;
}
name_end = &name[name_len - target_module_len];
// Check if the name of the newly loaded library is one of the
// modules the path hooks should be injected into
if (_wcsicmp(name_end, target_module) != 0) {
continue;
}
dprintf("Unity: Loaded %S\n", target_module);
dll_hook_insert_hooks(result);
path_hook_insert_hooks(result);
reg_hook_insert_hooks(result);
proc_addr_insert_hooks(result);
clock_hook_insert_hooks(result);
serial_hook_apply_hooks(result);
iohook_apply_hooks(result);
}
}
return result;
}

3
hkbhook/unity.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
void unity_hook_init(void);

31
hkbio/config.c Normal file
View File

@ -0,0 +1,31 @@
#include <windows.h>
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include "hkbio/config.h"
void hkb_io_config_load(
struct hkb_io_config *cfg,
const wchar_t *filename)
{
wchar_t key[240];
int i;
assert(cfg != NULL);
assert(filename != NULL);
cfg->vk_test = GetPrivateProfileIntW(L"io4", L"test", '1', filename);
cfg->vk_service = GetPrivateProfileIntW(L"io4", L"service", '2', filename);
cfg->vk_coin = GetPrivateProfileIntW(L"io4", L"coin", '3', filename);
cfg->vk_up = GetPrivateProfileIntW(L"io4", L"up", VK_UP, filename);
cfg->vk_down = GetPrivateProfileIntW(L"io4", L"down", VK_DOWN, filename);
cfg->vk_left = GetPrivateProfileIntW(L"io4", L"left", VK_LEFT, filename);
cfg->vk_right = GetPrivateProfileIntW(L"io4", L"right", VK_RIGHT, filename);
cfg->vk_enter = GetPrivateProfileIntW(L"io4", L"enter", VK_SPACE, filename);
cfg->vk_cancel = GetPrivateProfileIntW(L"io4", L"cancel", VK_LSHIFT, filename);
cfg->vk_arr_right = GetPrivateProfileIntW(L"io4", L"right_arrow", 'A', filename);
cfg->vk_arr_left = GetPrivateProfileIntW(L"io4", L"left_arrow", 'D', filename);
}

24
hkbio/config.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
struct hkb_io_config {
uint8_t vk_test;
uint8_t vk_service;
uint8_t vk_coin;
uint8_t vk_up;
uint8_t vk_down;
uint8_t vk_left;
uint8_t vk_right;
uint8_t vk_enter;
uint8_t vk_cancel;
uint8_t vk_arr_right;
uint8_t vk_arr_left;
};
void hkb_io_config_load(
struct hkb_io_config *cfg,
const wchar_t *filename);

74
hkbio/hkbio.c Normal file
View File

@ -0,0 +1,74 @@
#include <windows.h>
#include <xinput.h>
#include <limits.h>
#include <stdint.h>
#include "hkbio/hkbio.h"
#include "hkbio/config.h"
static struct hkb_io_config cfg;
uint16_t hkb_io_get_api_version(void)
{
return 0x0100;
}
HRESULT hkb_io_init(void)
{
hkb_io_config_load(&cfg, L".\\segatools.ini");
return S_OK;
}
HRESULT hkb_io_poll(uint8_t *opbtn, uint8_t *gamebtn)
{
*opbtn = 0;
*gamebtn = 0;
if (GetAsyncKeyState(cfg.vk_test) & 0x8000) {
*opbtn |= HKB_IO_OPBTN_TEST;
}
if (GetAsyncKeyState(cfg.vk_service) & 0x8000) {
*opbtn |= HKB_IO_OPBTN_SERVICE;
}
if (GetAsyncKeyState(cfg.vk_coin) & 0x8000) {
*opbtn |= HKB_IO_OPBTN_COIN;
}
if (GetAsyncKeyState(cfg.vk_right) & 0x8000) {
*gamebtn |= HKB_IO_GAMEBTN_RIGHT;
}
if (GetAsyncKeyState(cfg.vk_left) & 0x8000) {
*gamebtn |= HKB_IO_GAMEBTN_LEFT;
}
if (GetAsyncKeyState(cfg.vk_up) & 0x8000) {
*gamebtn |= HKB_IO_GAMEBTN_UP;
}
if (GetAsyncKeyState(cfg.vk_down) & 0x8000) {
*gamebtn |= HKB_IO_GAMEBTN_DOWN;
}
if (GetAsyncKeyState(cfg.vk_enter) & 0x8000) {
*gamebtn |= HKB_IO_GAMEBTN_ENTER;
}
if (GetAsyncKeyState(cfg.vk_cancel) & 0x8000) {
*gamebtn |= HKB_IO_GAMEBTN_CANCEL;
}
if (GetAsyncKeyState(cfg.vk_arr_right) & 0x8000) {
*gamebtn |= HKB_IO_GAMEBTN_ARR_RIGHT;
}
if (GetAsyncKeyState(cfg.vk_arr_left) & 0x8000) {
*gamebtn |= HKB_IO_GAMEBTN_ARR_LEFT;
}
return S_OK;
}

47
hkbio/hkbio.h Normal file
View File

@ -0,0 +1,47 @@
#pragma once
#include <windows.h>
#include <stdint.h>
enum {
HKB_IO_OPBTN_TEST = 0x01,
HKB_IO_OPBTN_SERVICE = 0x02,
HKB_IO_OPBTN_COIN = 0x04,
};
enum {
HKB_IO_GAMEBTN_RIGHT = 0x01,
HKB_IO_GAMEBTN_LEFT = 0x02,
HKB_IO_GAMEBTN_UP = 0x04,
HKB_IO_GAMEBTN_DOWN = 0x08,
HKB_IO_GAMEBTN_ENTER = 0x10,
HKB_IO_GAMEBTN_CANCEL = 0x20,
HKB_IO_GAMEBTN_ARR_RIGHT = 0x40,
HKB_IO_GAMEBTN_ARR_LEFT = 0x80,
};
/* Get the version of the Ongeki IO API that this DLL supports. This
function should return a positive 16-bit integer, where the high byte is
the major version and the low byte is the minor version (as defined by the
Semantic Versioning standard).
The latest API version as of this writing is 0x0100. */
uint16_t hkb_io_get_api_version(void);
/* Initialize the IO DLL. This is the second function that will be called on
your DLL, after hkb_io_get_api_version.
All subsequent calls to this API may originate from arbitrary threads.
Minimum API version: 0x0100 */
HRESULT hkb_io_init(void);
/* Send any queued outputs (of which there are currently none, though this may
change in subsequent API versions) and retrieve any new inputs.
Minimum API version: 0x0100 */
HRESULT hkb_io_poll(uint8_t *opbtn, uint8_t *gamebtn);

12
hkbio/meson.build Normal file
View File

@ -0,0 +1,12 @@
hkbio_lib = static_library(
'hkbio',
name_prefix : '',
include_directories : inc,
implicit_include_directories : false,
sources : [
'hkbio.c',
'hkbio.h',
'config.c',
'config.h',
],
)

View File

@ -1,5 +1,5 @@
#include <windows.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>

View File

@ -1,5 +1,5 @@
#include <windows.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
@ -25,10 +25,10 @@ static const struct hook_symbol cursor_syms[] = {
},*/ {
.name = "SetPhysicalCursorPos",
.patch = my_SetPhysicalCursorPos
}, /*{
}, {
.name = "ShowCursor",
.patch = my_ShowCursor
}*/
}
};
void cursor_hook_init()
@ -56,13 +56,11 @@ static BOOL my_SetPhysicalCursorPos(int x, int y)
static int my_ShowCursor(BOOL bShow)
{
dprintf("my_ShowCursor Hit!\n");
return 0;
}
static HCURSOR my_SetCursor(HCURSOR hCursor)
{
dprintf("my_SetCursor Hit!\n");
HCURSOR fake_cursor;
if ( hCursor )

View File

@ -2,7 +2,6 @@ hooklib_lib = static_library(
'hooklib',
include_directories : inc,
implicit_include_directories : false,
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
],
@ -23,8 +22,6 @@ hooklib_lib = static_library(
'fdshark.h',
'path.c',
'path.h',
'procaddr.c',
'procaddr.h',
'reg.c',
'reg.h',
'setupapi.c',

View File

@ -1,125 +0,0 @@
#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include <libgen.h>
#include "hooklib/procaddr.h"
#include "hook/table.h"
#include "util/dprintf.h"
static struct proc_addr_table *proc_addr_hook_list;
static size_t proc_addr_hook_count;
static CRITICAL_SECTION proc_addr_hook_lock;
static bool proc_addr_hook_initted;
static FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name);
static FARPROC (WINAPI *next_GetProcAddress)(HMODULE hModule, const char *name);
static void proc_addr_hook_init(void);
static const struct hook_symbol win32_hooks[] = {
{
.name = "GetProcAddress",
.patch = my_GetProcAddress,
.link = (void **) &next_GetProcAddress
}
};
HRESULT proc_addr_table_push(
const char *target,
struct hook_symbol *syms,
size_t nsyms
)
{
HRESULT hr;
struct proc_addr_table *new_item;
struct proc_addr_table *new_mem;
proc_addr_hook_init();
EnterCriticalSection(&proc_addr_hook_lock);
new_mem = realloc(
proc_addr_hook_list,
(proc_addr_hook_count + 1) * sizeof(struct proc_addr_table));
if (new_mem == NULL) {
hr = E_OUTOFMEMORY;
LeaveCriticalSection(&proc_addr_hook_lock);
return hr;
}
new_item = &new_mem[proc_addr_hook_count];
new_item->name = target;
new_item->nsyms = nsyms;
new_item->syms = syms;
proc_addr_hook_list = new_mem;
proc_addr_hook_count++;
hr = S_OK;
LeaveCriticalSection(&proc_addr_hook_lock);
return hr;
}
static void proc_addr_hook_init(void)
{
if (proc_addr_hook_initted) {
return;
}
dprintf("ProcAddr: Hook init\n");
proc_addr_hook_initted = true;
InitializeCriticalSection(&proc_addr_hook_lock);
hook_table_apply(
NULL,
"kernel32.dll",
win32_hooks,
_countof(win32_hooks));
}
FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name)
{
uintptr_t ordinal = (uintptr_t) name;
char mod_path[PATH_MAX];
char *mod_name;
const struct hook_symbol *sym;
FARPROC result = next_GetProcAddress(hModule, name);
GetModuleFileNameA(hModule, mod_path, PATH_MAX);
mod_name = basename(mod_path);
for (int i = 0; i < proc_addr_hook_count; i++) {
if (strcmp(proc_addr_hook_list[i].name, mod_name) == 0) {
for (int j = 0; j < proc_addr_hook_list[i].nsyms; j++) {
sym = &proc_addr_hook_list[i].syms[j];
if (ordinal > 0xFFFF) {
if (strcmp(sym->name, name) == 0) {
dprintf("ProcAddr: Hooking %s from %s\n", name, mod_name);
result = (FARPROC) sym->patch;
}
}
else {
if (sym->ordinal == ordinal) {
dprintf("ProcAddr: Hooking Ord %p from %s\n", (void *)ordinal, mod_name);
result = (FARPROC) sym->patch;
}
}
}
}
}
return result;
}

View File

@ -1,18 +0,0 @@
#pragma once
#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include "hook/table.h"
struct proc_addr_table {
const char *name;
size_t nsyms;
struct hook_symbol *syms;
};
HRESULT proc_addr_table_push(
const char *target,
struct hook_symbol *syms,
size_t nsyms
);

View File

@ -7,6 +7,7 @@
#include "hook/table.h"
#include "hooklib/reg.h"
#include "hook/procaddr.h"
#include "util/dprintf.h"
#include "util/str.h"
@ -99,6 +100,29 @@ static LSTATUS WINAPI hook_RegGetValueW(
uint32_t *numData
);
static LSTATUS WINAPI hook_RegQueryInfoKeyW(
HKEY hKey,
LPWSTR lpClass,
LPDWORD lpcchClass,
LPDWORD lpReserved,
LPDWORD lpcSubKeys,
LPDWORD lpcbMaxSubKeyLen,
LPDWORD lpcbMaxClassLen,
LPDWORD lpcValues,
LPDWORD lpcbMaxValueNameLen,
LPDWORD lpcbMaxValueLen,
LPDWORD lpcbSecurityDescriptor,
PFILETIME lpftLastWriteTime);
static LSTATUS WINAPI hook_RegEnumValueW(
HKEY hkey,
DWORD dwIndex,
LPWSTR lpValueName,
LPDWORD lpcchValueName,
LPDWORD lpReserved,
LPDWORD lpType,
LPBYTE lpData,
LPDWORD lpcbData);
/* Link pointers */
static LSTATUS (WINAPI *next_RegOpenKeyExW)(
@ -155,6 +179,30 @@ static LSTATUS (WINAPI *next_RegGetValueW)(
uint32_t *numData
);
static LSTATUS (WINAPI *next_RegQueryInfoKeyW)(
HKEY hKey,
LPWSTR lpClass,
LPDWORD lpcchClass,
LPDWORD lpReserved,
LPDWORD lpcSubKeys,
LPDWORD lpcbMaxSubKeyLen,
LPDWORD lpcbMaxClassLen,
LPDWORD lpcValues,
LPDWORD lpcbMaxValueNameLen,
LPDWORD lpcbMaxValueLen,
LPDWORD lpcbSecurityDescriptor,
PFILETIME lpftLastWriteTime);
static LSTATUS (WINAPI *next_RegEnumValueW)(
HKEY hkey,
DWORD dwIndex,
LPWSTR lpValueName,
LPDWORD lpcchValueName,
LPDWORD lpReserved,
LPDWORD lpType,
LPBYTE lpData,
LPDWORD lpcbData);
static const struct hook_symbol reg_hook_syms[] = {
{
.name = "RegOpenKeyExW",
@ -184,6 +232,14 @@ static const struct hook_symbol reg_hook_syms[] = {
.name = "RegGetValueW",
.patch = hook_RegGetValueW,
.link = (void **) &next_RegGetValueW,
}, {
.name = "RegQueryInfoKeyW",
.patch = hook_RegQueryInfoKeyW,
.link = (void **) &next_RegQueryInfoKeyW,
}, {
.name = "RegEnumValueW",
.patch = hook_RegEnumValueW,
.link = (void **) &next_RegEnumValueW,
}
};
@ -254,11 +310,24 @@ static void reg_hook_init(void)
InitializeCriticalSection(&reg_hook_lock);
dprintf("Reg hook init\n");
hook_table_apply(
reg_hook_insert_hooks(NULL);
proc_addr_table_push(
NULL,
"ADVAPI32.dll",
(struct hook_symbol *) reg_hook_syms,
_countof(reg_hook_syms));
}
void reg_hook_insert_hooks(HMODULE target)
{
hook_table_apply(
target,
"advapi32.dll",
reg_hook_syms,
_countof(reg_hook_syms));
}
static LRESULT reg_hook_propagate_hr(HRESULT hr)
@ -331,6 +400,7 @@ static LSTATUS reg_hook_open_locked(
/* Assume reg keys are referenced from a root key and not from some
intermediary key */
key = &reg_hook_keys[i];
//dprintf("Reg: %ls vs %ls\n", name, key->name);
if (key->root == parent && wstr_ieq(key->name, name)) {
break;
@ -821,6 +891,99 @@ static LSTATUS WINAPI hook_RegGetValueW(
return err;
}
static LSTATUS WINAPI hook_RegQueryInfoKeyW(
HKEY hKey,
LPWSTR lpClass,
LPDWORD lpcchClass,
LPDWORD lpReserved,
LPDWORD lpcSubKeys,
LPDWORD lpcbMaxSubKeyLen,
LPDWORD lpcbMaxClassLen,
LPDWORD lpcValues,
LPDWORD lpcbMaxValueNameLen,
LPDWORD lpcbMaxValueLen,
LPDWORD lpcbSecurityDescriptor,
PFILETIME lpftLastWriteTime)
{
struct reg_hook_key *key;
LSTATUS err;
EnterCriticalSection(&reg_hook_lock);
key = reg_hook_match_key_locked(hKey);
/* Check if this is a virtualized registry key */
if (key == NULL) {
LeaveCriticalSection(&reg_hook_lock);
return next_RegQueryInfoKeyW(
hKey,
lpClass,
lpcchClass,
lpReserved,
lpcSubKeys,
lpcbMaxSubKeyLen,
lpcbMaxClassLen,
lpcValues,
lpcbMaxValueNameLen,
lpcbMaxValueLen,
lpcbSecurityDescriptor,
lpftLastWriteTime);
}
// This is the only one I've seen even be changed, so it's all I'm doing
// until I see otherwise.
*lpcValues = key->nvals;
LeaveCriticalSection(&reg_hook_lock);
return ERROR_SUCCESS;
}
static LSTATUS WINAPI hook_RegEnumValueW(
HKEY hkey,
DWORD dwIndex,
LPWSTR lpValueName,
LPDWORD lpcchValueName,
LPDWORD lpReserved,
LPDWORD lpType,
LPBYTE lpData,
LPDWORD lpcbData)
{
struct reg_hook_key *key;
HRESULT hr;
LSTATUS err;
EnterCriticalSection(&reg_hook_lock);
key = reg_hook_match_key_locked(hkey);
/* Check if this is a virtualized registry key */
if (key == NULL) {
LeaveCriticalSection(&reg_hook_lock);
return next_RegEnumValueW(
hkey,
dwIndex,
lpValueName,
lpcchValueName,
lpReserved,
lpType,
lpData,
lpcbData);
}
if (dwIndex >= key->nvals) {
LeaveCriticalSection(&reg_hook_lock);
return ERROR_NO_MORE_ITEMS; // Pretty sure this is what it actually returns here?
}
wcscpy_s(lpValueName, *lpcchValueName, key->vals[dwIndex].name);
*lpcchValueName = wcslen(key->vals[dwIndex].name);
LeaveCriticalSection(&reg_hook_lock);
return ERROR_SUCCESS;
}
HRESULT reg_hook_read_bin(
void *bytes,
uint32_t *nbytes,

View File

@ -12,6 +12,8 @@ struct reg_hook_val {
uint32_t type;
};
void reg_hook_insert_hooks(HMODULE target);
HRESULT reg_hook_push_key(
HKEY root,
const wchar_t *name,

View File

@ -1,5 +1,6 @@
#include <windows.h>
#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>

View File

@ -1,9 +1,12 @@
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "iccard/aime.h"
#include "iccard/mifare.h"
#include "iccard/solitaire.h"
#include "util/dprintf.h"
@ -14,6 +17,9 @@ HRESULT aime_card_populate(
{
uint8_t b;
size_t i;
char accessCode[21];
char hashed_id_wrk[9];
char id_wrk[9];
assert(mifare != NULL);
assert(luid != NULL);
@ -37,11 +43,29 @@ HRESULT aime_card_populate(
mifare->sectors[0].blocks[2].bytes[6 + i] = b;
}
/* TODO An authentic Aime pass has a checksum of the LUID in the last few
bytes of block 1. The output of this function fails authenticity check
in its current form. */
// Set the card ID, nothing else matters in the first block
mifare->sectors[0].blocks[0].bytes[0] = luid[0];
mifare->sectors[0].blocks[0].bytes[1] = luid[1];
mifare->sectors[0].blocks[0].bytes[2] = luid[2];
mifare->sectors[0].blocks[0].bytes[3] = luid[3];
dprintf("AiMe IC: WARNING: Authenticity hash not yet implemented!\n");
sprintf_s(accessCode, sizeof accessCode, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
luid[0], luid[1], luid[2], luid[3], luid[4], luid[5], luid[6], luid[7], luid[8], luid[9]);
memcpy_s(hashed_id_wrk, sizeof(hashed_id_wrk), &accessCode[5],
8);
hashed_id_wrk[8] = '\0';
SolitaireCipherDecode(&accessCode[13], hashed_id_wrk, id_wrk);
DWORD nSerial = atoi(id_wrk);
mifare->sectors[0].blocks[1].bytes[12] = (nSerial >> 24) & 0xff;
mifare->sectors[0].blocks[1].bytes[13] = (nSerial >> 16) & 0xff;
mifare->sectors[0].blocks[1].bytes[14] = (nSerial >> 8) & 0xff;
mifare->sectors[0].blocks[1].bytes[15] = nSerial & 0xff;
return S_OK;
}

View File

@ -2,7 +2,6 @@ iccard_lib = static_library(
'iccard',
include_directories : inc,
implicit_include_directories : false,
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
],
@ -12,5 +11,7 @@ iccard_lib = static_library(
'felica.c',
'felica.h',
'mifare.h',
'solitaire.c',
'solitaire.h',
],
)

143
iccard/solitaire.c Normal file
View File

@ -0,0 +1,143 @@
#include "solitaire.h"
#include <memory.h>
#define DECK_SIZE 22
#define JOKER_A 21
#define JOKER_B 22
typedef struct {
char m_Deck[DECK_SIZE];
} DECK, *PDECK;
static DECK SOL_INIT_DECK = {
.m_Deck = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 },
};
#define char2num(c) ((c) - '0' + 1)
static inline char num2char(char num) {
while (num < 1) num = num + 10;
return (num - 1) % 10 + '0';
}
static void SolMoveCard(PDECK lpDeck, char card) {
int p = 0;
for (int i = 0; i < DECK_SIZE; i++) {
if (lpDeck->m_Deck[i] == card) {
p = i;
break;
}
}
if (p < DECK_SIZE - 1) {
lpDeck->m_Deck[p] = lpDeck->m_Deck[p + 1];
lpDeck->m_Deck[p + 1] = card;
} else {
for (int i = DECK_SIZE - 1; i > 1; i--) lpDeck->m_Deck[i] = lpDeck->m_Deck[i - 1];
lpDeck->m_Deck[1] = card;
}
}
static void SolCutDeck(PDECK lpDeck, char point) {
DECK tmp;
memcpy(tmp.m_Deck, &lpDeck->m_Deck[(size_t)point], DECK_SIZE - point - 1);
memcpy(&tmp.m_Deck[DECK_SIZE - point - 1], lpDeck->m_Deck, point);
memcpy(lpDeck->m_Deck, tmp.m_Deck, DECK_SIZE - 1);
}
static void SolSwapOutsideJoker(PDECK lpDeck) {
int j1 = -1;
int j2 = -1;
DECK tmp;
for (int i = 0; i < DECK_SIZE; i++) {
if (lpDeck->m_Deck[i] == JOKER_A || lpDeck->m_Deck[i] == JOKER_B) {
if (j1 == -1) {
j1 = i;
} else {
j2 = i;
}
}
}
if (0 < DECK_SIZE - j2 - 1) memcpy(tmp.m_Deck, &lpDeck->m_Deck[j2 + 1], DECK_SIZE - j2 - 1);
tmp.m_Deck[DECK_SIZE - j2 - 1] = lpDeck->m_Deck[j1];
if (0 < j2 - j1 - 1) memcpy(&tmp.m_Deck[DECK_SIZE - j2], &lpDeck->m_Deck[j1 + 1], j2 - j1 - 1);
tmp.m_Deck[DECK_SIZE - j1 - 1] = lpDeck->m_Deck[j2];
if (0 < j1) memcpy(&tmp.m_Deck[DECK_SIZE - j1], lpDeck->m_Deck, j1);
memcpy(lpDeck->m_Deck, tmp.m_Deck, DECK_SIZE);
}
static void SolCutByBottomCard(PDECK lpDeck) {
char p = lpDeck->m_Deck[DECK_SIZE - 1];
if (p == JOKER_B) p = JOKER_A;
SolCutDeck(lpDeck, p);
}
static char SolGetTopCardNum(PDECK lpDeck) {
char p = lpDeck->m_Deck[0];
if (p == JOKER_B) p = JOKER_A;
return lpDeck->m_Deck[(size_t)p];
}
static void SolDeckHash(PDECK lpDeck) {
char p;
do {
SolMoveCard(lpDeck, JOKER_A);
SolMoveCard(lpDeck, JOKER_B);
SolMoveCard(lpDeck, JOKER_B);
SolSwapOutsideJoker(lpDeck);
SolCutByBottomCard(lpDeck);
p = SolGetTopCardNum(lpDeck);
} while (p == JOKER_A || p == JOKER_B);
}
static void SolCreateDeck(PDECK lpDeck, const char *key) {
memcpy_s(lpDeck, sizeof *lpDeck, &SOL_INIT_DECK, sizeof SOL_INIT_DECK);
int p = 0;
while (key[p] != '\0') {
SolDeckHash(lpDeck);
char c = char2num(key[p]);
SolCutDeck(lpDeck, c);
p++;
}
}
void SolitaireCipherEncode(const char *szKey, const char *szSrc, char *szDst) {
DECK deck;
SolCreateDeck(&deck, szKey);
int i = 0;
while (szSrc[i] != '\0') {
SolDeckHash(&deck);
char p = SolGetTopCardNum(&deck);
szDst[i] = num2char(char2num(szSrc[i]) + p);
i++;
}
szDst[i] = '\0';
}
void SolitaireCipherDecode(const char *szKey, const char *szSrc, char *szDst) {
DECK deck;
SolCreateDeck(&deck, szKey);
int i = 0;
while (szSrc[i] != '\0') {
SolDeckHash(&deck);
char p = SolGetTopCardNum(&deck);
szDst[i] = num2char(char2num(szSrc[i]) - p);
i++;
}
szDst[i] = '\0';
}
void SolitaireCipher(int nMode, const char *szKey, const char *szSrc, char *szDst) {
if (nMode == 0)
SolitaireCipherEncode(szKey, szSrc, szDst);
else if (nMode == 1)
SolitaireCipherDecode(szKey, szSrc, szDst);
}

3
iccard/solitaire.h Normal file
View File

@ -0,0 +1,3 @@
void SolitaireCipherDecode(const char *szKey, const char *szSrc, char *szDst);
void SolitaireCipherEncode(const char *szKey, const char *szSrc, char *szDst);
void SolitaireCipher(int mode, const char *key, const char *src_str, char *dest_str);

View File

@ -1,5 +1,6 @@
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include "amex/amex.h"
#include "amex/config.h"

View File

@ -4,7 +4,6 @@ shared_library(
include_directories : inc,
implicit_include_directories : false,
vs_module_defs : 'idzhook.def',
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
capnhook.get_variable('hooklib_dep'),

View File

@ -4,6 +4,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "idzhook/config.h"
#include "idzhook/zinput.h"

View File

@ -4,6 +4,7 @@
#include <stddef.h>
#include <stdint.h>
#include <wchar.h>
#include <assert.h>
#include "idzio/backend.h"
#include "idzio/config.h"

View File

@ -3,7 +3,6 @@ idzio_lib = static_library(
name_prefix : '',
include_directories : inc,
implicit_include_directories : false,
c_pch : '../precompiled.h',
dependencies : [
dinput8_lib,
dxguid_lib,

View File

@ -2,7 +2,6 @@ jvs_lib = static_library(
'jvs',
include_directories : inc,
implicit_include_directories : false,
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
],

67
mai2hook/config.c Normal file
View File

@ -0,0 +1,67 @@
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include "board/config.h"
#include "gfxhook/config.h"
#include "hooklib/config.h"
#include "hooklib/dvd.h"
#include "mai2hook/config.h"
#include "platform/config.h"
void mai2_dll_config_load(
struct mai2_dll_config *cfg,
const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
GetPrivateProfileStringW(
L"mai2io",
L"path",
L"",
cfg->path,
_countof(cfg->path),
filename);
}
void touch_config_load(
struct touch_config *cfg,
const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
cfg->enable = GetPrivateProfileIntW(L"touch", L"enable", 1, filename);
}
void led_config_load(
struct led_config *cfg,
const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
cfg->enable = GetPrivateProfileIntW(L"led", L"enable", 1, filename);
}
void mai2_hook_config_load(
struct mai2_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);
mai2_dll_config_load(&cfg->dll, filename);
touch_config_load(&cfg->touch, filename);
led_config_load(&cfg->led, filename);
}

34
mai2hook/config.h Normal file
View File

@ -0,0 +1,34 @@
#pragma once
#include <stddef.h>
#include "board/config.h"
#include "gfxhook/gfx.h"
#include "hooklib/dvd.h"
#include "mai2hook/mai2-dll.h"
#include "mai2hook/touch.h"
#include "mai2hook/led.h"
#include "platform/config.h"
struct mai2_hook_config {
struct platform_config platform;
struct aime_config aime;
struct dvd_config dvd;
struct io4_config io4;
struct gfx_config gfx;
struct mai2_dll_config dll;
struct touch_config touch;
struct led_config led;
};
void mai2_dll_config_load(
struct mai2_dll_config *cfg,
const wchar_t *filename);
void mai2_hook_config_load(
struct mai2_hook_config *cfg,
const wchar_t *filename);

148
mai2hook/dllmain.c Normal file
View File

@ -0,0 +1,148 @@
#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 "hook/table.h"
#include "hooklib/dvd.h"
#include "hooklib/spike.h"
#include "hooklib/path.h"
#include "hooklib/reg.h"
#include "hook/procaddr.h"
#include "hooklib/serial.h"
#include "mai2hook/config.h"
#include "mai2hook/io4.h"
#include "mai2hook/mai2-dll.h"
#include "mai2hook/unity.h"
#include "mai2hook/touch.h"
#include "mai2hook/led.h"
#include "platform/platform.h"
#include "util/dprintf.h"
static HMODULE mai2_hook_mod;
static process_entry_t mai2_startup;
static struct mai2_hook_config mai2_hook_cfg;
static DWORD CALLBACK mai2_pre_startup(void)
{
HRESULT hr;
dprintf("--- Begin mai2_pre_startup ---\n");
/* Load config */
mai2_hook_config_load(&mai2_hook_cfg, L".\\segatools.ini");
/* Hook Win32 APIs */
dvd_hook_init(&mai2_hook_cfg.dvd, mai2_hook_mod);
gfx_hook_init(&mai2_hook_cfg.gfx);
gfx_d3d9_hook_init(&mai2_hook_cfg.gfx, mai2_hook_mod);
gfx_d3d11_hook_init(&mai2_hook_cfg.gfx, mai2_hook_mod);
gfx_dxgi_hook_init(&mai2_hook_cfg.gfx, mai2_hook_mod);
serial_hook_init();
/* Initialize emulation hooks */
hr = platform_hook_init(
&mai2_hook_cfg.platform,
"SDEZ",
"ACA1",
mai2_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = sg_reader_hook_init(&mai2_hook_cfg.aime, 1, mai2_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = vfd_hook_init(2);
if (FAILED(hr)) {
goto fail;
}
hr = mai2_dll_init(&mai2_hook_cfg.dll, mai2_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = mai2_io4_hook_init(&mai2_hook_cfg.io4);
if (FAILED(hr)) {
goto fail;
}
// TODO: The handling of the fake registry COM values is extraordinarly lazy
// and I need to make a better method for tacking fake values onto common keys
// that multiple modules may try to use.
hr = led_hook_init(&mai2_hook_cfg.led);
if (FAILED(hr)) {
goto fail;
}
hr = touch_hook_init(&mai2_hook_cfg.touch);
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 `mai2hook` initialization. */
unity_hook_init();
/* Initialize debug helpers */
spike_hook_init(L".\\segatools.ini");
dprintf("--- End mai2_pre_startup ---\n");
/* Jump to EXE start address */
return mai2_startup();
fail:
ExitProcess(EXIT_FAILURE);
}
BOOL WINAPI DllMain(HMODULE mod, DWORD cause, void *ctx)
{
HRESULT hr;
if (cause != DLL_PROCESS_ATTACH) {
return TRUE;
}
mai2_hook_mod = mod;
hr = process_hijack_startup(mai2_pre_startup, &mai2_startup);
if (!SUCCEEDED(hr)) {
dprintf("Failed to hijack process startup: %x\n", (int) hr);
}
return SUCCEEDED(hr);
}

147
mai2hook/io4.c Normal file
View File

@ -0,0 +1,147 @@
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "board/io4.h"
#include "mai2hook/mai2-dll.h"
#include "util/dprintf.h"
bool mai2_io_coin = false;
uint16_t mai2_io_coins = 0;
static HRESULT mai2_io4_poll(void *ctx, struct io4_state *state);
static const struct io4_ops mai2_io4_ops = {
.poll = mai2_io4_poll,
};
HRESULT mai2_io4_hook_init(const struct io4_config *cfg)
{
HRESULT hr;
assert(mai2_dll.init != NULL);
hr = io4_hook_init(cfg, &mai2_io4_ops, NULL);
if (FAILED(hr)) {
return hr;
}
return mai2_dll.init();
}
static HRESULT mai2_io4_poll(void *ctx, struct io4_state *state)
{
uint8_t opbtn = 0;
uint8_t player1 = 0;
uint8_t player2 = 0;
HRESULT hr;
assert(mai2_dll.poll != NULL);
hr = mai2_dll.poll(&opbtn, &player1, &player2);
if (FAILED(hr)) {
return hr;
}
if (opbtn & MAI2_IO_OPBTN_TEST) {
state->buttons[0] |= IO4_BUTTON_TEST;
}
if (opbtn & MAI2_IO_OPBTN_SERVICE) {
state->buttons[0] |= IO4_BUTTON_SERVICE;
}
if (opbtn & MAI2_IO_P1_START) {
state->buttons[0] |= 1 << 1;
}
if (opbtn & MAI2_IO_P2_START) {
state->buttons[1] |= 1 << 4;
}
if (!(player1 & MAI2_IO_GAMEBTN_1)) {
state->buttons[0] |= 1 << 2;
}
if (!(player1 & MAI2_IO_GAMEBTN_2)) {
state->buttons[0] |= 1 << 3;
}
if (!(player1 & MAI2_IO_GAMEBTN_3)) {
state->buttons[0] |= 1 << 0;
}
if (!(player1 & MAI2_IO_GAMEBTN_4)) {
state->buttons[0] |= 1 << 15;
}
if (!(player1 & MAI2_IO_GAMEBTN_5)) {
state->buttons[0] |= 1 << 14;
}
if (!(player1 & MAI2_IO_GAMEBTN_6)) {
state->buttons[0] |= 1 << 13;
}
if (!(player1 & MAI2_IO_GAMEBTN_7)) {
state->buttons[0] |= 1 << 12;
}
if (!(player1 & MAI2_IO_GAMEBTN_8)) {
state->buttons[0] |= 1 << 11;
}
if (!(player2 & MAI2_IO_GAMEBTN_1)) {
state->buttons[1] |= 1 << 2;
}
if (!(player2 & MAI2_IO_GAMEBTN_2)) {
state->buttons[1] |= 1 << 3;
}
if (!(player2 & MAI2_IO_GAMEBTN_3)) {
state->buttons[1] |= 1 << 0;
}
if (!(player2 & MAI2_IO_GAMEBTN_4)) {
state->buttons[1] |= 1 << 15;
}
if (!(player2 & MAI2_IO_GAMEBTN_5)) {
state->buttons[1] |= 1 << 14;
}
if (!(player2 & MAI2_IO_GAMEBTN_6)) {
state->buttons[1] |= 1 << 13;
}
if (!(player2 & MAI2_IO_GAMEBTN_7)) {
state->buttons[1] |= 1 << 12;
}
if (!(player2 & MAI2_IO_GAMEBTN_8)) {
state->buttons[1] |= 1 << 11;
}
if (opbtn & MAI2_IO_OPBTN_COIN) {
if (!mai2_io_coin) {
mai2_io_coin = true;
mai2_io_coins++;
}
}
else {
mai2_io_coin = false;
}
state->chutes[0] = 128 + 256 * mai2_io_coins;
return S_OK;
}

7
mai2hook/io4.h Normal file
View File

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

212
mai2hook/led.c Normal file
View File

@ -0,0 +1,212 @@
#include <windows.h>
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
#include "hooklib/reg.h"
#include "hooklib/uart.h"
#include "hooklib/fdshark.h"
#include "mai2hook/led.h"
#include "util/dprintf.h"
static HRESULT read_fake_com0(void *bytes, uint32_t *nbytes);
static HRESULT read_fake_com1(void *bytes, uint32_t *nbytes);
static HRESULT read_fake_com2(void *bytes, uint32_t *nbytes);
static HRESULT led_handle_irp(struct irp *irp);
static HRESULT led0_handle_irp_locked(struct irp *irp);
static HRESULT led1_handle_irp_locked(struct irp *irp);
static CRITICAL_SECTION led0_lock;
static struct uart led0_uart;
static uint8_t led0_written_bytes[520];
static uint8_t led0_readable_bytes[520];
static CRITICAL_SECTION led1_lock;
static struct uart led1_uart;
static uint8_t led1_written_bytes[520];
static uint8_t led1_readable_bytes[520];
static const struct reg_hook_val fake_com_keys[] = {
{
.name = L"\\Device\\RealTouchBoard0",
.read = read_fake_com0,
.type = REG_SZ,
},{
.name = L"\\Device\\RealTouchBoard1",
.read = read_fake_com1,
.type = REG_SZ,
},{
.name = L"\\Device\\RealLedBoard0",
.read = read_fake_com2,
.type = REG_SZ,
},
};
HRESULT led_hook_init(const struct led_config *cfg)
{
HRESULT hr;
if (!cfg->enable) {
return S_FALSE;
}
dprintf("Mai2 LED: Init\n");
InitializeCriticalSection(&led0_lock);
InitializeCriticalSection(&led1_lock);
hr = reg_hook_push_key(
HKEY_LOCAL_MACHINE,
L"HARDWARE\\DEVICEMAP\\SERIALCOMM",
fake_com_keys,
_countof(fake_com_keys));
if (FAILED(hr)) {
return hr;
}
uart_init(&led0_uart, 21);
led0_uart.written.bytes = led0_written_bytes;
led0_uart.written.nbytes = sizeof(led0_written_bytes);
led0_uart.readable.bytes = led0_readable_bytes;
led0_uart.readable.nbytes = sizeof(led0_readable_bytes);
uart_init(&led1_uart, 23);
led1_uart.written.bytes = led1_written_bytes;
led1_uart.written.nbytes = sizeof(led1_written_bytes);
led1_uart.readable.bytes = led1_readable_bytes;
led1_uart.readable.nbytes = sizeof(led1_readable_bytes);
return iohook_push_handler(led_handle_irp);
}
static HRESULT read_fake_com0(void *bytes, uint32_t *nbytes)
{
//dprintf("Mai2 Touch: Read COM3 reg val\n");
return reg_hook_read_wstr(bytes, nbytes, L"COM3");
}
static HRESULT read_fake_com1(void *bytes, uint32_t *nbytes)
{
//dprintf("Mai2 Touch: Read COM4 reg val\n");
return reg_hook_read_wstr(bytes, nbytes, L"COM4");
}
static HRESULT read_fake_com2(void *bytes, uint32_t *nbytes)
{
//dprintf("Mai2 LED: Read COM20 reg val\n");
return reg_hook_read_wstr(bytes, nbytes, L"COM20");
}
static HRESULT led_handle_irp(struct irp *irp)
{
HRESULT hr;
assert(irp != NULL);
if (uart_match_irp(&led0_uart, irp)) {
EnterCriticalSection(&led0_lock);
hr = led0_handle_irp_locked(irp);
LeaveCriticalSection(&led0_lock);
}
else if (uart_match_irp(&led1_uart, irp)) {
EnterCriticalSection(&led1_lock);
hr = led1_handle_irp_locked(irp);
LeaveCriticalSection(&led1_lock);
}
else {
return iohook_invoke_next(irp);
}
return hr;
}
static HRESULT led0_handle_irp_locked(struct irp *irp)
{
HRESULT hr = S_OK;
if (irp->op == IRP_OP_OPEN) {
dprintf("Mai2 led0: Starting backend\n");
//hr = mai2_dll.led_init();
if (FAILED(hr)) {
dprintf("Mai2 led: Backend error: %x\n", (int) hr);
return hr;
}
}
hr = uart_handle_irp(&led0_uart, irp);
if (FAILED(hr) || irp->op != IRP_OP_WRITE) {
return hr;
}
for (;;) {
#if 0
dprintf("TX0 Buffer:\n");
dump_iobuf(&led0_uart.written);
#endif
//hr = led_frame_decode(&req, &led0_uart.written, 0);
if (hr != S_OK) {
if (FAILED(hr)) {
dprintf("Mai2 led: Deframe error: %x\n", (int) hr);
}
return hr;
}
//hr = led_req_dispatch(&req);
if (FAILED(hr)) {
dprintf("Mai2 led: Processing error: %x\n", (int) hr);
}
return hr;
}
}
static HRESULT led1_handle_irp_locked(struct irp *irp)
{
HRESULT hr = S_OK;
if (irp->op == IRP_OP_OPEN) {
dprintf("Mai2 led1: Starting backend\n");
//hr = mai2_dll.led_init();
if (FAILED(hr)) {
dprintf("Mai2 led: Backend error: %x\n", (int) hr);
return hr;
}
}
hr = uart_handle_irp(&led0_uart, irp);
if (FAILED(hr) || irp->op != IRP_OP_WRITE) {
return hr;
}
for (;;) {
#if 0
dprintf("TX0 Buffer:\n");
dump_iobuf(&led0_uart.written);
#endif
//hr = led_frame_decode(&req, &led0_uart.written, 0);
if (hr != S_OK) {
if (FAILED(hr)) {
dprintf("Mai2 led: Deframe error: %x\n", (int) hr);
}
return hr;
}
//hr = led_req_dispatch(&req);
if (FAILED(hr)) {
dprintf("Mai2 led: Processing error: %x\n", (int) hr);
}
return hr;
}
}

9
mai2hook/led.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include <windows.h>
#include <stdbool.h>
struct led_config {
bool enable;
};
HRESULT led_hook_init(const struct led_config *cfg);

103
mai2hook/mai2-dll.c Normal file
View File

@ -0,0 +1,103 @@
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include "mai2hook/mai2-dll.h"
#include "util/dll-bind.h"
#include "util/dprintf.h"
const struct dll_bind_sym mai2_dll_syms[] = {
{
.sym = "mai2_io_init",
.off = offsetof(struct mai2_dll, init),
}, {
.sym = "mai2_io_poll",
.off = offsetof(struct mai2_dll, poll),
},
};
struct mai2_dll mai2_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 mai2_dll_init(const struct mai2_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("Mai2 IO: Failed to load IO DLL: %lx: %S\n",
hr,
cfg->path);
goto end;
}
dprintf("Mai2 IO: Using custom IO DLL: %S\n", cfg->path);
src = owned;
} else {
owned = NULL;
src = self;
}
get_api_version = (void *) GetProcAddress(src, "mai2_io_get_api_version");
if (get_api_version != NULL) {
mai2_dll.api_version = get_api_version();
} else {
mai2_dll.api_version = 0x0100;
dprintf("Custom IO DLL does not expose mai2_io_get_api_version, "
"assuming API version 1.0.\n"
"Please ask the developer to update their DLL.\n");
}
if (mai2_dll.api_version >= 0x0200) {
hr = E_NOTIMPL;
dprintf("Mai2 IO: Custom IO DLL implements an unsupported "
"API version (%#04x). Please update Segatools.\n",
mai2_dll.api_version);
goto end;
}
sym = mai2_dll_syms;
hr = dll_bind(&mai2_dll, src, &sym, _countof(mai2_dll_syms));
if (FAILED(hr)) {
if (src != self) {
dprintf("Mai2 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;
}

19
mai2hook/mai2-dll.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <windows.h>
#include "mai2io/mai2io.h"
struct mai2_dll {
uint16_t api_version;
HRESULT (*init)(void);
HRESULT (*poll)(uint8_t *opbtn, uint8_t *p1, uint8_t *p2);
};
struct mai2_dll_config {
wchar_t path[MAX_PATH];
};
extern struct mai2_dll mai2_dll;
HRESULT mai2_dll_init(const struct mai2_dll_config *cfg, HINSTANCE self);

22
mai2hook/mai2hook.def Normal file
View File

@ -0,0 +1,22 @@
LIBRARY mai2hook
EXPORTS
CreateDXGIFactory
CreateDXGIFactory1
CreateDXGIFactory2
D3D11CreateDevice
D3D11CreateDeviceAndSwapChain
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
mai2_io_get_api_version
mai2_io_init
mai2_io_poll

36
mai2hook/meson.build Normal file
View File

@ -0,0 +1,36 @@
shared_library(
'mai2hook',
name_prefix : '',
include_directories : inc,
implicit_include_directories : false,
vs_module_defs : 'mai2hook.def',
dependencies : [
capnhook.get_variable('hook_dep'),
capnhook.get_variable('hooklib_dep'),
xinput_lib,
],
link_with : [
aimeio_lib,
board_lib,
gfxhook_lib,
hooklib_lib,
mai2io_lib,
platform_lib,
util_lib,
],
sources : [
'config.c',
'config.h',
'dllmain.c',
'led.c',
'led.h',
'io4.c',
'io4.h',
'mai2-dll.c',
'mai2-dll.h',
'touch.c',
'touch.h',
'unity.h',
'unity.c',
],
)

239
mai2hook/touch.c Normal file
View File

@ -0,0 +1,239 @@
#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include "hooklib/reg.h"
#include "hooklib/uart.h"
#include "hooklib/fdshark.h"
#include "mai2hook/touch.h"
#include "mai2hook/mai2-dll.h"
#include "util/dprintf.h"
#include "util/dump.h"
const char CMD_START = '{';
const char CMD_END = '}';
const char RESP_START = '(';
const char RESP_END = ')';
const char BLANK = '@';
// Commands with parameters
const char CMD_RATIO[1] = "r";
const char CMD_SENS[1] = "k";
const char CMD_SENS_CHECK[2] = "th";
// Commands that never change
const char CMD_RESET[7] = "{RSET}"; // Reset board to default state
const char CMD_STAT[7] = "{STAT}"; // Start sending touch state
const char CMD_HALT[7] = "{HALT}"; // Stop sending touch state
const char RSP_ANY[6] = "(0000)"; // Stop sending touch state
static HRESULT read_fake_com0(void *bytes, uint32_t *nbytes);
static HRESULT read_fake_com1(void *bytes, uint32_t *nbytes);
static HRESULT read_fake_com2(void *bytes, uint32_t *nbytes);
static HRESULT touch_handle_irp(struct irp *irp);
static HRESULT touch0_handle_irp_locked(struct irp *irp);
static HRESULT touch1_handle_irp_locked(struct irp *irp);
static HRESULT touch_cmd_dispatch(char* cmd, struct iobuf *dest, uint8_t side);
bool touch0_auto = false;
bool touch1_auto = false;
static CRITICAL_SECTION touch0_lock;
static struct uart touch0_uart;
static uint8_t touch0_written_bytes[520];
static uint8_t touch0_readable_bytes[520];
static CRITICAL_SECTION touch1_lock;
static struct uart touch1_uart;
static uint8_t touch1_written_bytes[520];
static uint8_t touch1_readable_bytes[520];
static const struct reg_hook_val fake_com_keys[] = {
{
.name = L"\\Device\\RealTouchBoard0",
.read = read_fake_com0,
.type = REG_SZ,
},{
.name = L"\\Device\\RealTouchBoard1",
.read = read_fake_com1,
.type = REG_SZ,
},{
.name = L"\\Device\\RealLedBoard0",
.read = read_fake_com2,
.type = REG_SZ,
},
};
HRESULT touch_hook_init(const struct touch_config *cfg)
{
HRESULT hr;
if (!cfg->enable) {
return S_FALSE;
}
dprintf("Mai2 touch: Init\n");
InitializeCriticalSection(&touch0_lock);
InitializeCriticalSection(&touch1_lock);
hr = reg_hook_push_key(
HKEY_LOCAL_MACHINE,
L"HARDWARE\\DEVICEMAP\\SERIALCOMM",
fake_com_keys,
_countof(fake_com_keys));
if (FAILED(hr)) {
return hr;
}
uart_init(&touch0_uart, 3);
touch0_uart.written.bytes = touch0_written_bytes;
touch0_uart.written.nbytes = sizeof(touch0_written_bytes);
touch0_uart.readable.bytes = touch0_readable_bytes;
touch0_uart.readable.nbytes = sizeof(touch0_readable_bytes);
uart_init(&touch1_uart, 4);
touch1_uart.written.bytes = touch1_written_bytes;
touch1_uart.written.nbytes = sizeof(touch1_written_bytes);
touch1_uart.readable.bytes = touch1_readable_bytes;
touch1_uart.readable.nbytes = sizeof(touch1_readable_bytes);
return iohook_push_handler(touch_handle_irp);
}
static HRESULT read_fake_com0(void *bytes, uint32_t *nbytes)
{
// dprintf("Mai2 Touch: Read COM3 reg val\n");
return reg_hook_read_wstr(bytes, nbytes, L"COM3");
}
static HRESULT read_fake_com1(void *bytes, uint32_t *nbytes)
{
// dprintf("Mai2 Touch: Read COM4 reg val\n");
return reg_hook_read_wstr(bytes, nbytes, L"COM4");
}
static HRESULT read_fake_com2(void *bytes, uint32_t *nbytes)
{
// dprintf("Mai2 LED: Read COM20 reg val\n");
return reg_hook_read_wstr(bytes, nbytes, L"COM20");
}
static HRESULT touch_handle_irp(struct irp *irp)
{
HRESULT hr;
assert(irp != NULL);
if (uart_match_irp(&touch0_uart, irp)) {
EnterCriticalSection(&touch0_lock);
hr = touch0_handle_irp_locked(irp);
LeaveCriticalSection(&touch0_lock);
}
else if (uart_match_irp(&touch1_uart, irp)) {
EnterCriticalSection(&touch1_lock);
hr = touch1_handle_irp_locked(irp);
LeaveCriticalSection(&touch1_lock);
}
else {
return iohook_invoke_next(irp);
}
return hr;
}
static HRESULT touch0_handle_irp_locked(struct irp *irp)
{
HRESULT hr = S_OK;
if (irp->op == IRP_OP_OPEN) {
dprintf("Mai2 touch0: Starting backend\n");
//hr = mai2_dll.touch_init();
if (FAILED(hr)) {
dprintf("Mai2 touch0: Backend error: %x\n", (int) hr);
return hr;
}
}
hr = uart_handle_irp(&touch0_uart, irp);
if (FAILED(hr) || irp->op != IRP_OP_WRITE) {
return hr;
}
for (;;) {
#if 0
dprintf("touch0 Buffer:\n");
dump_iobuf(&touch0_uart.written);
#endif
hr = touch_cmd_dispatch((char*)touch0_uart.written.bytes, &touch0_uart.readable, 0);
if (FAILED(hr)) {
dprintf("Mai2 touch0: Dispatch failed %08lX\n", hr);
return hr;
}
touch0_uart.written.pos = 0;
return hr;
}
}
static HRESULT touch1_handle_irp_locked(struct irp *irp)
{
HRESULT hr = S_OK;
if (irp->op == IRP_OP_OPEN) {
dprintf("Mai2 touch1: Starting backend\n");
//hr = mai2_dll.touch_init();
if (FAILED(hr)) {
dprintf("Mai2 touch1: Backend error: %x\n", (int) hr);
return hr;
}
}
hr = uart_handle_irp(&touch1_uart, irp);
if (FAILED(hr) || irp->op != IRP_OP_WRITE) {
return hr;
}
for (;;) {
#if 0
dprintf("touch1 Buffer:\n");
dump_iobuf(&touch0_uart.written);
#endif
hr = touch_cmd_dispatch((char*)touch1_uart.written.bytes, &touch1_uart.readable, 1);
if (FAILED(hr)) {
dprintf("Mai2 touch1: Dispatch failed %08lX\n", hr);
return hr;
}
touch1_uart.written.pos = 0;
return hr;
}
}
static HRESULT touch_cmd_dispatch(char* cmd, struct iobuf *dest, uint8_t side)
{
if (!strcmp(cmd, CMD_RESET)) {
dprintf("Mai2 touch%d: Reset\n", side);
return S_OK;
}
else if (!strcmp(cmd, CMD_HALT)) {
dprintf("Mai2 touch%d: Halt\n", side);
return S_OK;
}
dprintf("Mai2 touch%d: Unknow %s\n", side, cmd);
return S_OK;
}

9
mai2hook/touch.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include <windows.h>
#include <stdbool.h>
struct touch_config {
bool enable;
};
HRESULT touch_hook_init(const struct touch_config *cfg);

119
mai2hook/unity.c Normal file
View File

@ -0,0 +1,119 @@
#include <stdbool.h>
#include <windows.h>
#include <stdlib.h>
#include "hook/table.h"
#include "hook/iohook.h"
#include "hooklib/dll.h"
#include "hooklib/path.h"
#include "hooklib/reg.h"
#include "hook/procaddr.h"
#include "hooklib/serial.h"
#include "platform/clock.h"
#include "util/dprintf.h"
static void dll_hook_insert_hooks(HMODULE target);
static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name);
static HMODULE (WINAPI *next_LoadLibraryW)(const wchar_t *name);
static HMODULE WINAPI my_LoadLibraryExW(const wchar_t *name, HANDLE hFile, DWORD dwFlags);
static HMODULE (WINAPI *next_LoadLibraryExW)(const wchar_t *name, HANDLE hFile, DWORD dwFlags);
static const struct hook_symbol unity_kernel32_syms[] = {
{
.name = "LoadLibraryW",
.patch = my_LoadLibraryW,
.link = (void **) &next_LoadLibraryW,
},{
.name = "LoadLibraryExW",
.patch = my_LoadLibraryExW,
.link = (void **) &next_LoadLibraryExW,
}
};
static const wchar_t *target_modules[] = {
L"mono-2.0-bdwgc.dll",
L"cri_ware_unity.dll",
};
static const size_t target_modules_len = _countof(target_modules);
void unity_hook_init(void)
{
dll_hook_insert_hooks(NULL);
}
static void dll_hook_insert_hooks(HMODULE target)
{
hook_table_apply(
target,
"kernel32.dll",
unity_kernel32_syms,
_countof(unity_kernel32_syms));
}
static HMODULE WINAPI my_LoadLibraryExW(const wchar_t *name, HANDLE hFile, DWORD dwFlags)
{
//dprintf("Unity: LoadLibraryExW %ls\n", name);
return my_LoadLibraryW(name);
}
static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name)
{
const wchar_t *name_end;
const wchar_t *target_module;
bool already_loaded;
HMODULE result;
size_t name_len;
size_t target_module_len;
if (name == NULL) {
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
// Check if the module is already loaded
already_loaded = GetModuleHandleW(name) != NULL;
// Must call the next handler so the DLL reference count is incremented
result = next_LoadLibraryW(name);
if (!already_loaded && result != NULL) {
name_len = wcslen(name);
for (size_t i = 0; i < target_modules_len; i++) {
target_module = target_modules[i];
target_module_len = wcslen(target_module);
// Check if the newly loaded library is at least the length of
// the name of the target module
if (name_len < target_module_len) {
continue;
}
name_end = &name[name_len - target_module_len];
// Check if the name of the newly loaded library is one of the
// modules the path hooks should be injected into
if (_wcsicmp(name_end, target_module) != 0) {
continue;
}
dprintf("Unity: Loaded %S\n", target_module);
dll_hook_insert_hooks(result);
path_hook_insert_hooks(result);
reg_hook_insert_hooks(result);
proc_addr_insert_hooks(result);
clock_hook_insert_hooks(result);
serial_hook_apply_hooks(result);
iohook_apply_hooks(result);
}
}
return result;
}

3
mai2hook/unity.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
void unity_hook_init(void);

47
mai2io/config.c Normal file
View File

@ -0,0 +1,47 @@
#include <windows.h>
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "mai2io/config.h"
static const int mai2_io_1p_default[] = {'W', 'E', 'D', 'C', 'X', 'Z', 'A', 'Q'};
static const int mai2_io_2p_default[] = {0x68, 0x69, 0x66, 0x63, 0x62, 0x61, 0x64, 0x67};
void mai2_io_config_load(
struct mai2_io_config *cfg,
const wchar_t *filename)
{
wchar_t key[240];
int i;
assert(cfg != NULL);
assert(filename != NULL);
cfg->vk_test = GetPrivateProfileIntW(L"io4", L"test", VK_DELETE, filename);
cfg->vk_service = GetPrivateProfileIntW(L"io4", L"service", VK_END, filename);
cfg->vk_coin = GetPrivateProfileIntW(L"io4", L"coin", VK_INSERT, filename);
cfg->vk_p1_start = GetPrivateProfileIntW(L"io4", L"p1_start", '1', filename);
cfg->vk_p2_start = GetPrivateProfileIntW(L"io4", L"p2_start", '2', filename);
for (i = 0 ; i < 8 ; i++) {
swprintf_s(key, _countof(key), L"1p_btn%i", i + 1);
cfg->vk_p1_btn[i] = GetPrivateProfileIntW(
L"button",
key,
mai2_io_1p_default[i],
filename);
}
for (i = 0 ; i < 8 ; i++) {
swprintf_s(key, _countof(key), L"2p_btn%i", i + 1);
cfg->vk_p2_btn[i] = GetPrivateProfileIntW(
L"button",
key,
mai2_io_2p_default[i],
filename);
}
}

20
mai2io/config.h Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
struct mai2_io_config {
uint8_t vk_test;
uint8_t vk_service;
uint8_t vk_coin;
uint8_t vk_p1_start;
uint8_t vk_p2_start;
uint8_t vk_p1_btn[8];
uint8_t vk_p2_btn[8];
};
void mai2_io_config_load(
struct mai2_io_config *cfg,
const wchar_t *filename);

83
mai2io/mai2io.c Normal file
View File

@ -0,0 +1,83 @@
#include <windows.h>
#include <xinput.h>
#include <limits.h>
#include <stdint.h>
#include "mai2io/mai2io.h"
#include "mai2io/config.h"
static struct mai2_io_config mai2_io_cfg;
uint16_t mai2_io_get_api_version(void)
{
return 0x0100;
}
HRESULT mai2_io_init(void)
{
mai2_io_config_load(&mai2_io_cfg, L".\\segatools.ini");
return S_OK;
}
HRESULT mai2_io_poll(uint8_t *opbtn, uint8_t *player1, uint8_t *player2)
{
uint8_t opts = 0;
uint8_t p1 = 0;
uint8_t p2 = 0;
if (GetAsyncKeyState(mai2_io_cfg.vk_test) & 0x8000) {
opts |= MAI2_IO_OPBTN_TEST;
}if (GetAsyncKeyState(mai2_io_cfg.vk_service) & 0x8000) {
opts |= MAI2_IO_OPBTN_SERVICE;
}if (GetAsyncKeyState(mai2_io_cfg.vk_coin) & 0x8000) {
opts |= MAI2_IO_OPBTN_COIN;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p1_start) & 0x8000) {
opts |= MAI2_IO_P1_START;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p2_start) & 0x8000) {
opts |= MAI2_IO_P2_START;
}
*opbtn = opts;
if (GetAsyncKeyState(mai2_io_cfg.vk_p1_btn[0]) & 0x8000) {
p1 |= MAI2_IO_GAMEBTN_1;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p1_btn[1]) & 0x8000) {
p1 |= MAI2_IO_GAMEBTN_2;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p1_btn[2]) & 0x8000) {
p1 |= MAI2_IO_GAMEBTN_3;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p1_btn[3]) & 0x8000) {
p1 |= MAI2_IO_GAMEBTN_4;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p1_btn[4]) & 0x8000) {
p1 |= MAI2_IO_GAMEBTN_5;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p1_btn[5]) & 0x8000) {
p1 |= MAI2_IO_GAMEBTN_6;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p1_btn[6]) & 0x8000) {
p1 |= MAI2_IO_GAMEBTN_7;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p1_btn[7]) & 0x8000) {
p1 |= MAI2_IO_GAMEBTN_8;
}
*player1 = p1;
if (GetAsyncKeyState(mai2_io_cfg.vk_p2_btn[0]) & 0x8000) {
p2 |= MAI2_IO_GAMEBTN_1;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p2_btn[1]) & 0x8000) {
p2 |= MAI2_IO_GAMEBTN_2;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p2_btn[2]) & 0x8000) {
p2 |= MAI2_IO_GAMEBTN_3;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p2_btn[3]) & 0x8000) {
p2 |= MAI2_IO_GAMEBTN_4;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p2_btn[4]) & 0x8000) {
p2 |= MAI2_IO_GAMEBTN_5;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p2_btn[5]) & 0x8000) {
p2 |= MAI2_IO_GAMEBTN_6;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p2_btn[6]) & 0x8000) {
p2 |= MAI2_IO_GAMEBTN_7;
}if (GetAsyncKeyState(mai2_io_cfg.vk_p2_btn[7]) & 0x8000) {
p2 |= MAI2_IO_GAMEBTN_8;
}
*player2 = p2;
return S_OK;
}

49
mai2io/mai2io.h Normal file
View File

@ -0,0 +1,49 @@
#pragma once
#include <windows.h>
#include <stdint.h>
enum {
MAI2_IO_OPBTN_TEST = 0x01,
MAI2_IO_OPBTN_SERVICE = 0x02,
MAI2_IO_OPBTN_COIN = 0x04,
MAI2_IO_P1_START = 0x08,
MAI2_IO_P2_START = 0x10,
};
enum {
MAI2_IO_GAMEBTN_1 = 0x01,
MAI2_IO_GAMEBTN_2 = 0x02,
MAI2_IO_GAMEBTN_3 = 0x04,
MAI2_IO_GAMEBTN_4 = 0x08,
MAI2_IO_GAMEBTN_5 = 0x10,
MAI2_IO_GAMEBTN_6 = 0x20,
MAI2_IO_GAMEBTN_7 = 0x40,
MAI2_IO_GAMEBTN_8 = 0x80,
};
/* Get the version of the Mai2 IO API that this DLL supports. This
function should return a positive 16-bit integer, where the high byte is
the major version and the low byte is the minor version (as defined by the
Semantic Versioning standard).
The latest API version as of this writing is 0x0100. */
uint16_t mai2_io_get_api_version(void);
/* Initialize the IO DLL. This is the second function that will be called on
your DLL, after mai2_io_get_api_version.
All subsequent calls to this API may originate from arbitrary threads.
Minimum API version: 0x0100 */
HRESULT mai2_io_init(void);
/* Send any queued outputs (of which there are currently none, though this may
change in subsequent API versions) and retrieve any new inputs.
Minimum API version: 0x0100 */
HRESULT mai2_io_poll(uint8_t *opbtn, uint8_t *player1, uint8_t *player2);

15
mai2io/meson.build Normal file
View File

@ -0,0 +1,15 @@
mai2io_lib = static_library(
'mai2io',
name_prefix : '',
include_directories : inc,
implicit_include_directories : false,
dependencies : [
xinput_lib,
],
sources : [
'mai2io.c',
'mai2io.h',
'config.c',
'config.h',
],
)

View File

@ -1,5 +1,6 @@
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include "board/config.h"

View File

@ -1,4 +1,5 @@
#include <windows.h>
#include <stdlib.h>
#include "board/io4.h"
#include "board/sg-reader.h"

View File

@ -4,6 +4,7 @@
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include "mercuryhook/elisabeth.h"
#include "mercuryhook/mercury-dll.h"

View File

@ -4,7 +4,6 @@ shared_library(
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'),

View File

@ -3,6 +3,7 @@
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "mercuryio/config.h"

View File

@ -3,6 +3,7 @@
#include <limits.h>
#include <stdint.h>
#include <process.h>
#include <stdlib.h>
#include "mercuryio/mercuryio.h"
#include "mercuryio/config.h"

View File

@ -3,7 +3,6 @@ mercuryio_lib = static_library(
name_prefix : '',
include_directories : inc,
implicit_include_directories : false,
c_pch : '../precompiled.h',
sources : [
'mercuryio.c',
'mercuryio.h',

View File

@ -61,6 +61,8 @@ subdir('idzio')
subdir('mu3io')
subdir('mercuryio')
subdir('cxbio')
subdir('mai2io')
subdir('hkbio')
subdir('chunihook')
subdir('divahook')
@ -70,3 +72,5 @@ subdir('minihook')
subdir('mu3hook')
subdir('mercuryhook')
subdir('cxbhook')
subdir('mai2hook')
subdir('hkbhook')

View File

@ -3,7 +3,6 @@ shared_library(
name_prefix : '',
include_directories: inc,
implicit_include_directories : false,
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
],

View File

@ -1,5 +1,6 @@
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include "board/config.h"

View File

@ -4,7 +4,6 @@ shared_library(
include_directories : inc,
implicit_include_directories : false,
vs_module_defs : 'mu3hook.def',
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
capnhook.get_variable('hooklib_dep'),

View File

@ -1,11 +1,13 @@
#include <stdbool.h>
#include <windows.h>
#include <stdlib.h>
#include "hook/table.h"
#include "hooklib/dll.h"
#include "hooklib/path.h"
#include "platform/clock.h"
#include "util/dprintf.h"
@ -88,6 +90,7 @@ static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name)
dll_hook_insert_hooks(result);
path_hook_insert_hooks(result);
clock_hook_insert_hooks(result);
}
}

Some files were not shown because too many files have changed in this diff Show More