From b86049034fceb2137bcc1aa28ab954ae2da42caa Mon Sep 17 00:00:00 2001 From: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> Date: Wed, 12 Nov 2025 22:33:32 +0000 Subject: [PATCH 01/37] Add setting to only allow game input in foreground (#83) This adds a setting to the global configuration to allow only input when the game is focused. This prevents issues, such as triggering game buttons or service buttons while being tabbed out. Turned off by default. The implementation is not the best, but in the context how these games are structured, the most acceptable. It will keep scanning if the foreground window title fully matches (some games partially match, like FGO because due to the revision number in the title), and if found, will grab the HWND of that window and compare that from then on, to not tank performance due to constant string operations. Tested with FGO, chusan and ongeki. Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/83 Reviewed-by: Dniel97 Co-authored-by: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> Co-committed-by: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> --- common/amex/config.c | 1 + common/amex/jvs.h | 1 + common/board/config.c | 1 + common/board/io3.c | 11 ++++++- common/board/io4.c | 31 +++++++++++++----- common/board/io4.h | 16 +++++---- common/util/fg-detect.c | 66 ++++++++++++++++++++++++++++++++++++++ common/util/fg-detect.h | 8 +++++ common/util/meson.build | 2 ++ doc/config/common.md | 13 ++++++++ games/apm3hook/io4.c | 2 +- games/chunihook/dllmain.c | 5 +++ games/chusanhook/io4.c | 39 +++++++++++----------- games/cmhook/io4.c | 13 ++++---- games/cxbhook/config.c | 1 + games/cxbhook/dllmain.c | 5 +++ games/cxbhook/revio.c | 38 +++++++++++++--------- games/cxbhook/revio.h | 1 + games/divahook/dllmain.c | 5 +++ games/fgohook/io4.c | 13 ++++---- games/idachook/io4.c | 31 +++++++++--------- games/idzhook/dllmain.c | 5 +++ games/kemonohook/dllmain.c | 5 +++ games/mai2hook/io4.c | 13 ++++---- games/mercuryhook/io4.c | 15 ++++----- games/mu3hook/io4.c | 28 ++++++++-------- games/swdchook/io4.c | 33 ++++++++++--------- games/tokyohook/io4.c | 2 +- 28 files changed, 276 insertions(+), 128 deletions(-) create mode 100644 common/util/fg-detect.c create mode 100644 common/util/fg-detect.h diff --git a/common/amex/config.c b/common/amex/config.c index e138447..3604c08 100644 --- a/common/amex/config.c +++ b/common/amex/config.c @@ -73,6 +73,7 @@ void jvs_config_load(struct jvs_config *cfg, const wchar_t *filename) assert(filename != NULL); cfg->enable = GetPrivateProfileIntW(L"jvs", L"enable", 1, filename); + cfg->foreground = GetPrivateProfileIntW(L"jvs", L"foreground", 0, filename); } void sram_config_load(struct sram_config *cfg, const wchar_t *filename) diff --git a/common/amex/jvs.h b/common/amex/jvs.h index 0bedb0f..ab3ea36 100644 --- a/common/amex/jvs.h +++ b/common/amex/jvs.h @@ -15,6 +15,7 @@ DEFINE_GUID( struct jvs_config { bool enable; + bool foreground; }; typedef HRESULT (*jvs_provider_t)(struct jvs_node **root); diff --git a/common/board/config.c b/common/board/config.c index 96c4146..be98ebe 100644 --- a/common/board/config.c +++ b/common/board/config.c @@ -92,6 +92,7 @@ void io4_config_load(struct io4_config *cfg, const wchar_t *filename) assert(filename != NULL); cfg->enable = GetPrivateProfileIntW(L"io4", L"enable", 1, filename); + cfg->foreground_only = GetPrivateProfileIntW(L"io4", L"foreground", 0, filename); } void vfd_config_load(struct vfd_config *cfg, const wchar_t *filename) diff --git a/common/board/io3.c b/common/board/io3.c index d61cfdc..2a7ca83 100644 --- a/common/board/io3.c +++ b/common/board/io3.c @@ -26,6 +26,7 @@ #include "util/dprintf.h" #include "util/dump.h" +#include "util/fg-detect.h" static void io3_transact( struct jvs_node *node, @@ -146,6 +147,8 @@ static uint8_t io3_features[] = { 0x00, }; +static struct io3_switch_state prev_state = {0}; + void io3_init( struct io3 *io3, struct jvs_node *next, @@ -423,7 +426,13 @@ static HRESULT io3_cmd_read_switches( memset(&state, 0, sizeof(state)); if (io3->ops != NULL) { - io3->ops->read_switches(io3->ops_ctx, &state); + fgdet_poll(); + if (fgdet_in_foreground()) { // returns true if fgdet is not enabled + io3->ops->read_switches(io3->ops_ctx, &state); + memcpy(&prev_state, &state, sizeof(state)); + } else { + state = prev_state; + } } hr = iobuf_write_8(resp_buf, state.system); /* Test, Tilt lines */ diff --git a/common/board/io4.c b/common/board/io4.c index e1def19..8001457 100644 --- a/common/board/io4.c +++ b/common/board/io4.c @@ -20,6 +20,7 @@ #include "util/async.h" #include "util/dprintf.h" +#include "util/fg-detect.h" #pragma pack(push, 1) @@ -99,12 +100,14 @@ static struct async io4_async; static uint8_t io4_system_status; static const struct io4_ops *io4_ops; static void *io4_ops_ctx; +static struct io4_state prev_state; HRESULT io4_hook_init( - const struct io4_config *cfg, - const struct io4_ops *ops, - void *ctx) -{ + const struct io4_config *cfg, + const struct io4_ops *ops, + void *ctx, + const wchar_t* window_name, + const bool window_name_is_partial_match) { HRESULT hr; assert(cfg != NULL); @@ -122,9 +125,14 @@ HRESULT io4_hook_init( return hr; } + if (window_name != NULL && cfg->foreground_only) { + fgdet_init(window_name, window_name_is_partial_match); + } + io4_ops = ops; io4_ops_ctx = ctx; io4_system_status = 0x02; /* idk */ + memset(&prev_state, 0, sizeof(prev_state)); iohook_push_handler(io4_handle_irp); hr = setupapi_add_phantom_dev(&hid_guid, io4_path); @@ -325,11 +333,18 @@ static HRESULT io4_async_poll(void *ctx, struct irp *irp) /* Call into ops to poll the underlying inputs */ - memset(&state, 0, sizeof(state)); - hr = io4_ops->poll(io4_ops_ctx, &state); + fgdet_poll(); + if (fgdet_in_foreground()) { // returns true if fgdet is not enabled + memset(&state, 0, sizeof(state)); + hr = io4_ops->poll(io4_ops_ctx, &state); - if (FAILED(hr)) { - return hr; + if (FAILED(hr)) { + return hr; + } + + memcpy(&prev_state, &state, sizeof(state)); + } else { + state = prev_state; // if we're unfocused, freeze the current input } /* Construct IN report. Values are all little-endian, unlike JVS. */ diff --git a/common/board/io4.h b/common/board/io4.h index 09a921b..1f93cf1 100644 --- a/common/board/io4.h +++ b/common/board/io4.h @@ -10,12 +10,13 @@ enum { /* System buttons in button[0] */ - IO4_BUTTON_TEST = 1 << 9, - IO4_BUTTON_SERVICE = 1 << 6, + IO4_BUTTON_TEST = 1 << 9, + IO4_BUTTON_SERVICE = 1 << 6, }; struct io4_config { bool enable; + bool foreground_only; }; struct io4_state { @@ -26,11 +27,14 @@ struct io4_state { }; struct io4_ops { - HRESULT (*poll)(void *ctx, struct io4_state *state); + HRESULT (*poll)(void* ctx, struct io4_state* state); + HRESULT (*write_gpio)(uint8_t* payload, size_t len); }; HRESULT io4_hook_init( - const struct io4_config *cfg, - const struct io4_ops *ops, - void *ctx); + const struct io4_config* cfg, + const struct io4_ops* ops, + void* ctx, + const wchar_t* window_name, + const bool window_name_is_partial_match); \ No newline at end of file diff --git a/common/util/fg-detect.c b/common/util/fg-detect.c new file mode 100644 index 0000000..e5ed83c --- /dev/null +++ b/common/util/fg-detect.c @@ -0,0 +1,66 @@ +#include +#include + +#include "util/dprintf.h" +#include "util/fg-detect.h" + +#include + +static HWND window_handle; +static const wchar_t* window_title = NULL; +static bool foreground_state = true; +static bool partial_match; +static wchar_t scanned_title[256]; + +static HWND get_window(){ + if (window_handle != NULL){ + return window_handle; + } + + // try detecting the window + HWND hwnd = GetForegroundWindow(); + if (GetWindowTextW(hwnd, scanned_title, sizeof(scanned_title)) == 0) { + return NULL; + } + if (partial_match) { + if (wcsstr(scanned_title, window_title) == NULL) { + return NULL; + } + } else { + if (wcscmp(scanned_title, window_title) != 0) { + return NULL; + } + } + dprintf("FG-Detect: Program window detected\n"); + window_handle = hwnd; + return window_handle; +} + +void fgdet_init(const wchar_t* wnd_title, const bool wnd_partial_match) { + assert(wnd_title != NULL); + + window_handle = NULL; + window_title = wnd_title; + partial_match = wnd_partial_match; + dprintf("FG-Detect: Searching for \"%ls\"\n", window_title); +} + +bool fgdet_in_foreground(){ + return foreground_state; +} + +void fgdet_poll(){ + if (window_title == NULL){ + return; + } else if (GetForegroundWindow() == get_window()){ + if (!foreground_state){ + dprintf("FG-Detect: Got focus\n"); + foreground_state = true; + } + } else { + if (foreground_state){ + dprintf("FG-Detect: Lost focus\n"); + foreground_state = false; + } + } +} diff --git a/common/util/fg-detect.h b/common/util/fg-detect.h new file mode 100644 index 0000000..3961fac --- /dev/null +++ b/common/util/fg-detect.h @@ -0,0 +1,8 @@ +#pragma once +#include + +void fgdet_init(const wchar_t* wnd_title, const bool wnd_partial_match); + +bool fgdet_in_foreground(); + +void fgdet_poll(); \ No newline at end of file diff --git a/common/util/meson.build b/common/util/meson.build index 58782d3..a0150a7 100644 --- a/common/util/meson.build +++ b/common/util/meson.build @@ -17,6 +17,8 @@ util_lib = static_library( 'dprintf.h', 'dump.c', 'dump.h', + 'fg-detect.c', + 'fg-detect.h', 'get_function_ordinal.c', 'get_function_ordinal.h', 'lib.c', diff --git a/doc/config/common.md b/doc/config/common.md index d2eb385..1be9b7c 100644 --- a/doc/config/common.md +++ b/doc/config/common.md @@ -409,6 +409,12 @@ Default `1` Enable JVS port emulation. Disable to use the JVS port on a real AMEX. +### `foreground` + +Default `0` + +Only enables input when the game's main window is focused. This does not work for all games. + ## `[io4]` Configure emulation of the IO4 board. Same settings also apply to `[io3]`. @@ -419,6 +425,13 @@ Default `1` Enable IO4 port emulation. Disable to use the IO4 port on a real ALLS. + +### `foreground` + +Default `0` + +Only enables input when the game's main window is focused. This does not work for all games, notably APMv3, due to the game switching involved. + ### `test` Default `0x31` diff --git a/games/apm3hook/io4.c b/games/apm3hook/io4.c index 4d04f97..596aa80 100644 --- a/games/apm3hook/io4.c +++ b/games/apm3hook/io4.c @@ -23,7 +23,7 @@ HRESULT apm3_io4_hook_init(const struct io4_config *cfg) assert(apm3_dll.init != NULL); - hr = io4_hook_init(cfg, &apm3_io4_ops, NULL); + hr = io4_hook_init(cfg, &apm3_io4_ops, NULL, NULL, false); // can't use fgdet here because the main window closes if (FAILED(hr)) { return hr; diff --git a/games/chunihook/dllmain.c b/games/chunihook/dllmain.c index a3fa8ab..16e663f 100644 --- a/games/chunihook/dllmain.c +++ b/games/chunihook/dllmain.c @@ -37,6 +37,7 @@ #include "util/dprintf.h" #include "util/env.h" +#include "util/fg-detect.h" static HMODULE chuni_hook_mod; static process_entry_t chuni_startup; @@ -129,6 +130,10 @@ static DWORD CALLBACK chuni_pre_startup(void) goto fail; } + if (chuni_hook_cfg.amex.jvs.enable && chuni_hook_cfg.amex.jvs.foreground) { + fgdet_init(L"teaGfx DirectX Release", false); + } + /* Initialize debug helpers */ spike_hook_init(get_config_path()); diff --git a/games/chusanhook/io4.c b/games/chusanhook/io4.c index c7f8265..48234a3 100644 --- a/games/chusanhook/io4.c +++ b/games/chusanhook/io4.c @@ -16,32 +16,32 @@ struct chunithm_jvs_ir_mask { // Incorrect IR beam mappings retained for backward compatibility static const struct chunithm_jvs_ir_mask chunithm_jvs_ir_masks_v1[] = { - { 0, 1 << 13 }, - { 1 << 13, 0 }, - { 0, 1 << 12 }, - { 1 << 12, 0 }, - { 0, 1 << 11 }, - { 1 << 11, 0 }, + {0, 1 << 13}, + {1 << 13, 0}, + {0, 1 << 12}, + {1 << 12, 0}, + {0, 1 << 11}, + {1 << 11, 0}, }; static const struct chunithm_jvs_ir_mask chunithm_jvs_ir_masks[] = { - { 1 << 13, 0 }, - { 0, 1 << 13 }, - { 1 << 12, 0 }, - { 0, 1 << 12 }, - { 1 << 11, 0 }, - { 0, 1 << 11 }, + {1 << 13, 0}, + {0, 1 << 13}, + {1 << 12, 0}, + {0, 1 << 12}, + {1 << 11, 0}, + {0, 1 << 11}, }; static HRESULT chusan_io4_poll(void* ctx, struct io4_state* state); + static uint16_t coins; static const struct io4_ops chusan_io4_ops = { .poll = chusan_io4_poll, }; -HRESULT chusan_io4_hook_init(const struct io4_config* cfg) -{ +HRESULT chusan_io4_hook_init(const struct io4_config* cfg) { HRESULT hr; assert(chuni_dll.jvs_init != NULL); @@ -50,19 +50,18 @@ HRESULT chusan_io4_hook_init(const struct io4_config* cfg) hr = chuni_dll.jvs_init(); if (FAILED(hr)) { - dprintf("USB I/O: Backend error, I/O disconnected: %x\n", (int)hr); + dprintf("USB I/O: Backend error, I/O disconnected: %x\n", (int) hr); return hr; } - io4_hook_init(cfg, &chusan_io4_ops, NULL); + io4_hook_init(cfg, &chusan_io4_ops, NULL, L"teaGfx DirectX Release", false); return S_OK; } -static HRESULT chusan_io4_poll(void* ctx, struct io4_state* state) -{ - const struct chunithm_jvs_ir_mask *masks; +static HRESULT chusan_io4_poll(void* ctx, struct io4_state* state) { + const struct chunithm_jvs_ir_mask* masks; uint8_t opbtn; uint8_t beams; size_t i; @@ -103,4 +102,4 @@ static HRESULT chusan_io4_poll(void* ctx, struct io4_state* state) } return S_OK; -} +} \ No newline at end of file diff --git a/games/cmhook/io4.c b/games/cmhook/io4.c index 073f681..c497b37 100644 --- a/games/cmhook/io4.c +++ b/games/cmhook/io4.c @@ -10,20 +10,20 @@ #include "util/dprintf.h" -static HRESULT cm_io4_poll(void *ctx, struct io4_state *state); +static HRESULT cm_io4_poll(void* ctx, struct io4_state* state); + static uint16_t coins; static const struct io4_ops cm_io4_ops = { .poll = cm_io4_poll, }; -HRESULT cm_io4_hook_init(const struct io4_config *cfg) -{ +HRESULT cm_io4_hook_init(const struct io4_config* cfg) { HRESULT hr; assert(cm_dll.init != NULL); - hr = io4_hook_init(cfg, &cm_io4_ops, NULL); + hr = io4_hook_init(cfg, &cm_io4_ops, NULL, L"MU3CardViewer", false); if (FAILED(hr)) { return hr; @@ -32,8 +32,7 @@ HRESULT cm_io4_hook_init(const struct io4_config *cfg) return cm_dll.init(); } -static HRESULT cm_io4_poll(void *ctx, struct io4_state *state) -{ +static HRESULT cm_io4_poll(void* ctx, struct io4_state* state) { uint8_t opbtn; HRESULT hr; @@ -66,4 +65,4 @@ static HRESULT cm_io4_poll(void *ctx, struct io4_state *state) state->chutes[0] = coins << 8; return S_OK; -} +} \ No newline at end of file diff --git a/games/cxbhook/config.c b/games/cxbhook/config.c index 98d326d..8a2d6fb 100644 --- a/games/cxbhook/config.c +++ b/games/cxbhook/config.c @@ -42,6 +42,7 @@ void revio_config_load(struct revio_config *cfg, const wchar_t *filename) assert(filename != NULL); cfg->enable = GetPrivateProfileIntW(L"revio", L"enable", 1, filename); + cfg->foreground = GetPrivateProfileIntW(L"revio", L"foreground", 0, filename); } void led_config_load(struct led_config *cfg, const wchar_t *filename) diff --git a/games/cxbhook/dllmain.c b/games/cxbhook/dllmain.c index d644b5e..fd6f4e0 100644 --- a/games/cxbhook/dllmain.c +++ b/games/cxbhook/dllmain.c @@ -24,6 +24,7 @@ #include "util/dprintf.h" #include "util/env.h" +#include "util/fg-detect.h" static HMODULE cxb_hook_mod; static process_entry_t cxb_startup; @@ -103,6 +104,10 @@ static DWORD CALLBACK cxb_pre_startup(void) goto fail; } + if (cxb_hook_cfg.revio.enable && cxb_hook_cfg.revio.foreground) { + fgdet_init(L"MT Framework", false); + } + hr = led_hook_init(&cxb_hook_cfg.led); if (FAILED(hr)) { diff --git a/games/cxbhook/revio.c b/games/cxbhook/revio.c index 84ab016..b06a007 100644 --- a/games/cxbhook/revio.c +++ b/games/cxbhook/revio.c @@ -14,6 +14,7 @@ #include "hook/table.h" #include "util/dprintf.h" +#include "util/fg-detect.h" static int my_cCommIo_Open(char *port); static int my_cCommIo_Close(); @@ -31,6 +32,7 @@ static int my_cCommIo_SetAmpMute(int amp_id, int a2); int amp_volume[] = {20, 20, 20}; int last_triggers = 0; int last_is_mouse_down = false; +int prev_out = 0; static struct hook_symbol revio_syms[] = { { @@ -139,26 +141,32 @@ static int my_cCommIo_GetTrigger() uint16_t btns = 0; int out = 0; - cxb_dll.revio_poll(&btns); + fgdet_poll(); + if (fgdet_in_foreground()) { + cxb_dll.revio_poll(&btns); - if (btns & 0x01) { - out |= 1 << 4; // test - } + if (btns & 0x01) { + out |= 1 << 4; // test + } - if (btns & 0x02) { - out |= 1 << 5; // service? - } + if (btns & 0x02) { + out |= 1 << 5; // service? + } - if (btns & 0x04) { - out |= 1 << 1; // up - } + if (btns & 0x04) { + out |= 1 << 1; // up + } - if (btns & 0x08) { - out |= 1 << 3; // down - } + if (btns & 0x08) { + out |= 1 << 3; // down + } - if (btns & 0x0F) { - out |= 1 << 2; // cancel + if (btns & 0x0F) { + out |= 1 << 2; // cancel + } + prev_out = out; + } else { + out = prev_out; } out &= ~last_triggers; diff --git a/games/cxbhook/revio.h b/games/cxbhook/revio.h index 8150c97..c787607 100644 --- a/games/cxbhook/revio.h +++ b/games/cxbhook/revio.h @@ -6,6 +6,7 @@ struct revio_config { bool enable; + bool foreground; uint8_t test; uint8_t service; uint8_t coin; diff --git a/games/divahook/dllmain.c b/games/divahook/dllmain.c index eb6e16f..52789b1 100644 --- a/games/divahook/dllmain.c +++ b/games/divahook/dllmain.c @@ -34,6 +34,7 @@ #include "util/dprintf.h" #include "util/env.h" +#include "util/fg-detect.h" static HMODULE diva_hook_mod; static process_entry_t diva_startup; @@ -104,6 +105,10 @@ static DWORD CALLBACK diva_pre_startup(void) goto fail; } + if (diva_hook_cfg.amex.jvs.enable && diva_hook_cfg.amex.jvs.foreground) { + fgdet_init(L"Hatsune Miku Project DIVA Arcade Future Tone", false); + } + /* Initialize debug helpers */ spike_hook_init(get_config_path()); diff --git a/games/fgohook/io4.c b/games/fgohook/io4.c index 67dfba4..5194fa3 100644 --- a/games/fgohook/io4.c +++ b/games/fgohook/io4.c @@ -10,20 +10,20 @@ #include "util/dprintf.h" -static HRESULT fgo_io4_poll(void *ctx, struct io4_state *state); +static HRESULT fgo_io4_poll(void* ctx, struct io4_state* state); + static uint16_t coins; static const struct io4_ops fgo_io4_ops = { .poll = fgo_io4_poll, }; -HRESULT fgo_io4_hook_init(const struct io4_config *cfg) -{ +HRESULT fgo_io4_hook_init(const struct io4_config* cfg) { HRESULT hr; assert(fgo_dll.init != NULL); - hr = io4_hook_init(cfg, &fgo_io4_ops, NULL); + hr = io4_hook_init(cfg, &fgo_io4_ops, NULL, L"Fate/Grand Order Arcade r", true); if (FAILED(hr)) { return hr; @@ -32,8 +32,7 @@ HRESULT fgo_io4_hook_init(const struct io4_config *cfg) return fgo_dll.init(); } -static HRESULT fgo_io4_poll(void *ctx, struct io4_state *state) -{ +static HRESULT fgo_io4_poll(void* ctx, struct io4_state* state) { uint8_t opbtn; uint8_t gamebtn; int16_t stick_x; @@ -101,4 +100,4 @@ static HRESULT fgo_io4_poll(void *ctx, struct io4_state *state) state->adcs[4] = 0x8000 + stick_y; return S_OK; -} +} \ No newline at end of file diff --git a/games/idachook/io4.c b/games/idachook/io4.c index 3861ef8..fa4f82b 100644 --- a/games/idachook/io4.c +++ b/games/idachook/io4.c @@ -10,12 +10,14 @@ #include "util/dprintf.h" -static HRESULT idac_io4_poll(void *ctx, struct io4_state *state); +static HRESULT idac_io4_poll(void* ctx, struct io4_state* state); + static HRESULT idac_io4_write_gpio(uint8_t* payload, size_t len); + static uint16_t coins; static const struct io4_ops idac_io4_ops = { - .poll = idac_io4_poll, + .poll = idac_io4_poll, .write_gpio = idac_io4_write_gpio }; @@ -36,13 +38,12 @@ static const uint16_t idac_gear_signals[] = { 0x0014, }; -HRESULT idac_io4_hook_init(const struct io4_config *cfg) -{ +HRESULT idac_io4_hook_init(const struct io4_config* cfg) { HRESULT hr; assert(idac_dll.init != NULL); - hr = io4_hook_init(cfg, &idac_io4_ops, NULL); + hr = io4_hook_init(cfg, &idac_io4_ops, NULL, L"GameProject", false); if (FAILED(hr)) { return hr; @@ -51,8 +52,7 @@ HRESULT idac_io4_hook_init(const struct io4_config *cfg) return idac_dll.init(); } -static HRESULT idac_io4_poll(void *ctx, struct io4_state *state) -{ +static HRESULT idac_io4_poll(void* ctx, struct io4_state* state) { uint8_t opbtn; uint8_t gamebtn; uint8_t gear; @@ -131,21 +131,20 @@ static HRESULT idac_io4_poll(void *ctx, struct io4_state *state) return S_OK; } -static HRESULT idac_io4_write_gpio(uint8_t* payload, size_t len) -{ +static HRESULT idac_io4_write_gpio(uint8_t* payload, size_t len) { assert(idac_dll.led_set_leds != NULL); - + // Just fast fail if there aren't enough bytes in the payload - if (len < 3) + if (len < 3) return S_OK; - + // This command is used for lights in IDAC, but it only contains button lights, // and only in the first 3 bytes of the payload; everything else is padding to // make the payload 62 bytes. The rest of the cabinet lights and the side button // lights are handled separately, by the 15070 lights controller. - uint32_t lights_data = (uint32_t) ((uint8_t)(payload[0]) << 24 | - (uint8_t)(payload[1]) << 16 | - (uint8_t)(payload[2]) << 8); + uint32_t lights_data = (uint32_t)((uint8_t)(payload[0]) << 24 | + (uint8_t)(payload[1]) << 16 | + (uint8_t)(payload[2]) << 8); // Since Sega uses an odd ordering for the first part of the bitfield, // let's normalize the data and just send over bytes for the receiver @@ -162,4 +161,4 @@ static HRESULT idac_io4_write_gpio(uint8_t* payload, size_t len) idac_dll.led_set_leds(0, rgb_out); return S_OK; -} +} \ No newline at end of file diff --git a/games/idzhook/dllmain.c b/games/idzhook/dllmain.c index 534f4d0..b73a6a4 100644 --- a/games/idzhook/dllmain.c +++ b/games/idzhook/dllmain.c @@ -41,6 +41,7 @@ #include "util/dprintf.h" #include "util/lib.h" #include "util/env.h" +#include "util/fg-detect.h" static HMODULE idz_hook_mod; static process_entry_t idz_startup; @@ -136,6 +137,10 @@ static DWORD CALLBACK idz_pre_startup(void) goto fail; } + if (idz_hook_cfg.amex.jvs.enable && idz_hook_cfg.amex.jvs.foreground) { + fgdet_init(L"Direct3D Window", false); + } + /* Initialize debug helpers */ spike_hook_init(get_config_path()); diff --git a/games/kemonohook/dllmain.c b/games/kemonohook/dllmain.c index 3373489..f429299 100644 --- a/games/kemonohook/dllmain.c +++ b/games/kemonohook/dllmain.c @@ -18,6 +18,7 @@ #include "unityhook/hook.h" #include "util/dprintf.h" #include "util/env.h" +#include "util/fg-detect.h" static HMODULE kemono_hook_mod; static process_entry_t kemono_startup; @@ -103,6 +104,10 @@ static DWORD CALLBACK kemono_pre_startup(void) { goto fail; } + if (kemono_hook_cfg.amex.jvs.enable && kemono_hook_cfg.amex.jvs.foreground) { + fgdet_init(L"Parade", false); + } + kemono_extra_hooks_init(); /* Initialize Unity native plugin DLL hooks diff --git a/games/mai2hook/io4.c b/games/mai2hook/io4.c index 4eb7f6b..cb0b38d 100644 --- a/games/mai2hook/io4.c +++ b/games/mai2hook/io4.c @@ -10,20 +10,20 @@ #include "util/dprintf.h" -static HRESULT mai2_io4_poll(void *ctx, struct io4_state *state); +static HRESULT mai2_io4_poll(void* ctx, struct io4_state* state); + static uint16_t coins; static const struct io4_ops mai2_io4_ops = { .poll = mai2_io4_poll, }; -HRESULT mai2_io4_hook_init(const struct io4_config *cfg) -{ +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); + hr = io4_hook_init(cfg, &mai2_io4_ops, NULL, L"Sinmai", false); if (FAILED(hr)) { return hr; @@ -32,8 +32,7 @@ HRESULT mai2_io4_hook_init(const struct io4_config *cfg) return mai2_dll.init(); } -static HRESULT mai2_io4_poll(void *ctx, struct io4_state *state) -{ +static HRESULT mai2_io4_poll(void* ctx, struct io4_state* state) { uint8_t opbtn; uint16_t player1; uint16_t player2; @@ -150,4 +149,4 @@ static HRESULT mai2_io4_poll(void *ctx, struct io4_state *state) } return S_OK; -} +} \ No newline at end of file diff --git a/games/mercuryhook/io4.c b/games/mercuryhook/io4.c index df22656..c34eb6f 100644 --- a/games/mercuryhook/io4.c +++ b/games/mercuryhook/io4.c @@ -14,19 +14,18 @@ bool mercury_io_coin = false; uint16_t mercury_io_coins = 0; -static HRESULT mercury_io4_poll(void *ctx, struct io4_state *state); +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 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); + hr = io4_hook_init(cfg, &mercury_io4_ops, NULL, L"Mercury", false); if (FAILED(hr)) { return hr; @@ -35,8 +34,7 @@ HRESULT mercury_io4_hook_init(const struct io4_config *cfg) return mercury_dll.init(); } -static HRESULT mercury_io4_poll(void *ctx, struct io4_state *state) -{ +static HRESULT mercury_io4_poll(void* ctx, struct io4_state* state) { uint8_t opbtn; uint8_t gamebtn; HRESULT hr; @@ -72,8 +70,7 @@ static HRESULT mercury_io4_poll(void *ctx, struct io4_state *state) mercury_io_coin = true; mercury_io_coins++; } - } - else { + } else { mercury_io_coin = false; } @@ -88,4 +85,4 @@ static HRESULT mercury_io4_poll(void *ctx, struct io4_state *state) } return S_OK; -} +} \ No newline at end of file diff --git a/games/mu3hook/io4.c b/games/mu3hook/io4.c index dfa7ad8..430d17a 100644 --- a/games/mu3hook/io4.c +++ b/games/mu3hook/io4.c @@ -10,7 +10,8 @@ #include "util/dprintf.h" -static HRESULT mu3_io4_poll(void *ctx, struct io4_state *state); +static HRESULT mu3_io4_poll(void* ctx, struct io4_state* state); + static HRESULT mu3_io4_write_gpio(uint8_t* payload, size_t len); static uint16_t coins; @@ -20,13 +21,12 @@ static const struct io4_ops mu3_io4_ops = { .write_gpio = mu3_io4_write_gpio, }; -HRESULT mu3_io4_hook_init(const struct io4_config *cfg) -{ +HRESULT mu3_io4_hook_init(const struct io4_config* cfg) { HRESULT hr; assert(mu3_dll.init != NULL); - hr = io4_hook_init(cfg, &mu3_io4_ops, NULL); + hr = io4_hook_init(cfg, &mu3_io4_ops, NULL, L"Otoge", false); if (FAILED(hr)) { return hr; @@ -35,8 +35,7 @@ HRESULT mu3_io4_hook_init(const struct io4_config *cfg) return mu3_dll.init(); } -static HRESULT mu3_io4_poll(void *ctx, struct io4_state *state) -{ +static HRESULT mu3_io4_poll(void* ctx, struct io4_state* state) { uint8_t opbtn; uint8_t left; uint8_t right; @@ -111,11 +110,11 @@ static HRESULT mu3_io4_poll(void *ctx, struct io4_state *state) } if (!(left & MU3_IO_GAMEBTN_SIDE)) { - state->buttons[1] |= 1 << 15; /* L-Side, active-low */ + state->buttons[1] |= 1 << 15; /* L-Side, active-low */ } if (!(right & MU3_IO_GAMEBTN_SIDE)) { - state->buttons[0] |= 1 << 14; /* R-Side, active-low */ + state->buttons[0] |= 1 << 14; /* R-Side, active-low */ } /* Lever increases right-to-left, not left-to-right. @@ -128,19 +127,18 @@ static HRESULT mu3_io4_poll(void *ctx, struct io4_state *state) return S_OK; } -static HRESULT mu3_io4_write_gpio(uint8_t* payload, size_t len) -{ +static HRESULT mu3_io4_write_gpio(uint8_t* payload, size_t len) { // Just fast fail if there aren't enough bytes in the payload - if (len < 3) + if (len < 3) return S_OK; // This command is used for lights in Ongeki, but it only contains button lights, // and only in the first 3 bytes of the payload; everything else is padding to // make the payload 62 bytes. The rest of the cabinet lights and the side button // lights are handled separately, by the 15093 lights controller. - uint32_t lights_data = (uint32_t) ((uint8_t)(payload[0]) << 24 | - (uint8_t)(payload[1]) << 16 | - (uint8_t)(payload[2]) << 8); + uint32_t lights_data = (uint32_t)((uint8_t)(payload[0]) << 24 | + (uint8_t)(payload[1]) << 16 | + (uint8_t)(payload[2]) << 8); // Since Sega uses an odd ordering for the first part of the bitfield, // let's normalize the data and just send over bytes for the receiver @@ -169,4 +167,4 @@ static HRESULT mu3_io4_write_gpio(uint8_t* payload, size_t len) mu3_dll.led_set_leds(1, rgb_out); return S_OK; -} +} \ No newline at end of file diff --git a/games/swdchook/io4.c b/games/swdchook/io4.c index 679a646..1d3ffcf 100644 --- a/games/swdchook/io4.c +++ b/games/swdchook/io4.c @@ -11,24 +11,28 @@ #include "swdchook/swdc-dll.h" static HANDLE mmf; + static HRESULT init_mmf(void); + static void swdc_set_gamebtns(uint16_t value); -static HRESULT swdc_io4_poll(void *ctx, struct io4_state *state); +static HRESULT swdc_io4_poll(void* ctx, struct io4_state* state); + static HRESULT swdc_io4_write_gpio(uint8_t* payload, size_t len); + static uint16_t coins; static const struct io4_ops swdc_io4_ops = { - .poll = swdc_io4_poll, + .poll = swdc_io4_poll, .write_gpio = swdc_io4_write_gpio }; -HRESULT swdc_io4_hook_init(const struct io4_config *cfg) { +HRESULT swdc_io4_hook_init(const struct io4_config* cfg) { HRESULT hr; assert(swdc_dll.init != NULL); - hr = io4_hook_init(cfg, &swdc_io4_ops, NULL); + hr = io4_hook_init(cfg, &swdc_io4_ops, NULL, L"Todoroki", false); if (FAILED(hr)) { return hr; @@ -61,7 +65,7 @@ void swdc_set_gamebtns(uint16_t value) { // Update the memory-mapped file LPVOID mmf_view = MapViewOfFile(mmf, FILE_MAP_ALL_ACCESS, 0, 0, 2); if (mmf_view != NULL) { - uint16_t* ptr = (uint16_t*)mmf_view; + uint16_t* ptr = (uint16_t *) mmf_view; *ptr = value; UnmapViewOfFile(mmf_view); @@ -70,7 +74,7 @@ void swdc_set_gamebtns(uint16_t value) { // ReleaseMutex(mutex); } -static HRESULT swdc_io4_poll(void *ctx, struct io4_state *state) { +static HRESULT swdc_io4_poll(void* ctx, struct io4_state* state) { uint8_t opbtn; uint16_t gamebtn; struct swdc_io_analog_state analog_state; @@ -128,7 +132,7 @@ static HRESULT swdc_io4_poll(void *ctx, struct io4_state *state) { state->buttons[0] |= 1 << 2; } -/* + /* Update steering wheel buttons Those are connected to the SEGA 838-15415 INDICATOR BD MAIN @@ -175,21 +179,20 @@ static HRESULT swdc_io4_poll(void *ctx, struct io4_state *state) { return S_OK; } -static HRESULT swdc_io4_write_gpio(uint8_t* payload, size_t len) -{ +static HRESULT swdc_io4_write_gpio(uint8_t* payload, size_t len) { assert(swdc_dll.led_set_leds != NULL); // Just fast fail if there aren't enough bytes in the payload - if (len < 3) + if (len < 3) return S_OK; - + // This command is used for lights in SWDC, but it only contains button lights, // and only in the first 3 bytes of the payload; everything else is padding to // make the payload 62 bytes. The rest of the cabinet lights and the side button // lights are handled separately, by the 15070 lights controller. - uint32_t lights_data = (uint32_t) ((uint8_t)(payload[0]) << 24 | - (uint8_t)(payload[1]) << 16 | - (uint8_t)(payload[2]) << 8); + uint32_t lights_data = (uint32_t)((uint8_t)(payload[0]) << 24 | + (uint8_t)(payload[1]) << 16 | + (uint8_t)(payload[2]) << 8); // Since Sega uses an odd ordering for the first part of the bitfield, // let's normalize the data and just send over bytes for the receiver @@ -206,4 +209,4 @@ static HRESULT swdc_io4_write_gpio(uint8_t* payload, size_t len) swdc_dll.led_set_leds(0, rgb_out); return S_OK; -} +} \ No newline at end of file diff --git a/games/tokyohook/io4.c b/games/tokyohook/io4.c index b2924c7..e1995eb 100644 --- a/games/tokyohook/io4.c +++ b/games/tokyohook/io4.c @@ -26,7 +26,7 @@ HRESULT tokyo_io4_hook_init(const struct io4_config *cfg) assert(tokyo_dll.init != NULL); - hr = io4_hook_init(cfg, &tokyo_io4_ops, NULL); + hr = io4_hook_init(cfg, &tokyo_io4_ops, NULL, L"STAR", false); if (FAILED(hr)) { return hr; From ce77d6090551f11cec3e2ddadff14f45ebcecd1e Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Sun, 21 Dec 2025 18:58:01 +0100 Subject: [PATCH 02/37] mai2: fix memory leak and high CPU usage --- games/chusanhook/dllmain.c | 2 +- subprojects/capnhook.wrap | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/games/chusanhook/dllmain.c b/games/chusanhook/dllmain.c index 6ba4cd4..bb0a243 100644 --- a/games/chusanhook/dllmain.c +++ b/games/chusanhook/dllmain.c @@ -104,7 +104,7 @@ static DWORD CALLBACK chusan_pre_startup(void) { hr = platform_hook_init( &chusan_hook_cfg.platform, "SDHD", - "ACA2", + "ACA1", chusan_hook_mod); if (FAILED(hr)) { diff --git a/subprojects/capnhook.wrap b/subprojects/capnhook.wrap index 8c6369d..2d30039 100644 --- a/subprojects/capnhook.wrap +++ b/subprojects/capnhook.wrap @@ -1,4 +1,4 @@ [wrap-git] directory = capnhook url = https://gitea.tendokyu.moe/TeamTofuShop/capnhook -revision = 252465b23f51f36206e614190496663425f4ee5d +revision = 4e192325bcc785a88dc32b751e2d3ae610d553ac From e4bd0543c9883d1e7e8cd3ba8aec255bc721c057 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Sun, 21 Dec 2025 20:43:08 +0100 Subject: [PATCH 03/37] diva: add partition and button LED support --- games/divahook/diva-dll.c | 37 +++++++++++++++++--- games/divahook/diva-dll.h | 2 ++ games/divahook/divahook.def | 2 ++ games/divahook/jvs.c | 25 ++++++++++++++ games/divaio/divaio.c | 27 ++++++++++++++- games/divaio/divaio.h | 69 +++++++++++++++++++++++++++++++++---- games/idzhook/jvs.c | 1 + 7 files changed, 150 insertions(+), 13 deletions(-) diff --git a/games/divahook/diva-dll.c b/games/divahook/diva-dll.c index c24cf01..9a5d057 100644 --- a/games/divahook/diva-dll.c +++ b/games/divahook/diva-dll.c @@ -30,9 +30,28 @@ const struct dll_bind_sym diva_dll_syms[] = { }, { .sym = "diva_io_slider_set_leds", .off = offsetof(struct diva_dll, slider_set_leds), + }, { + .sym = "diva_io_led_init", + .off = offsetof(struct diva_dll, led_init), + }, { + .sym = "diva_io_led_set_leds", + .off = offsetof(struct diva_dll, led_set_leds), } }; +/* Helper function to determine upon dll_bind failure whether the required functions were found + NOTE: relies on symbols order declared above */ +static HRESULT has_enough_symbols(uint16_t version, uint8_t count) +{ + if ( version < 0x0101 && count == 7 ) + return S_OK; + + if ( version >= 0x0101 && count == 9 ) + return S_OK; + + return E_FAIL; +} + struct diva_dll diva_dll; // Copypasta DLL binding and diagnostic message boilerplate. @@ -92,16 +111,24 @@ HRESULT diva_dll_init(const struct diva_dll_config *cfg, HINSTANCE self) } sym = diva_dll_syms; + const struct dll_bind_sym *init_sym = &sym[0]; hr = dll_bind(&diva_dll, src, &sym, _countof(diva_dll_syms)); if (FAILED(hr)) { if (src != self) { - dprintf("Diva IO: Custom IO DLL does not provide function " - "\"%s\". Please contact your IO DLL's developer for " - "further assistance.\n", - sym->sym); + // Might still be ok depending on external dll API version + int bind_count = sym - init_sym; + if ( has_enough_symbols(diva_dll.api_version, bind_count) == S_OK ) + { + hr = S_OK; + } else { + dprintf("Diva IO: Custom IO DLL does not provide function " + "\"%s\". Please contact your IO DLL's developer for " + "further assistance.\n", + sym->sym); - goto end; + goto end; + } } else { dprintf("Internal error: could not reflect \"%s\"\n", sym->sym); } diff --git a/games/divahook/diva-dll.h b/games/divahook/diva-dll.h index 1e57947..67cf335 100644 --- a/games/divahook/diva-dll.h +++ b/games/divahook/diva-dll.h @@ -13,6 +13,8 @@ struct diva_dll { void (*slider_start)(diva_io_slider_callback_t callback); void (*slider_stop)(void); void (*slider_set_leds)(const uint8_t *rgb); + HRESULT (*led_init)(void); + void (*led_set_leds)(uint8_t board, const uint8_t *rgb); }; struct diva_dll_config { diff --git a/games/divahook/divahook.def b/games/divahook/divahook.def index 9101509..4d2ae44 100644 --- a/games/divahook/divahook.def +++ b/games/divahook/divahook.def @@ -19,3 +19,5 @@ EXPORTS diva_io_slider_set_leds diva_io_slider_start diva_io_slider_stop + diva_io_led_init + diva_io_led_set_leds diff --git a/games/divahook/jvs.c b/games/divahook/jvs.c index f718438..e53f1ac 100644 --- a/games/divahook/jvs.c +++ b/games/divahook/jvs.c @@ -20,10 +20,12 @@ static void diva_jvs_read_coin_counter( void *ctx, uint8_t slot_no, uint16_t *out); +static void diva_jvs_write_gpio(void *ctx, uint32_t state); static const struct io3_ops diva_jvs_io3_ops = { .read_switches = diva_jvs_read_switches, .read_coin_counter = diva_jvs_read_coin_counter, + .write_gpio = diva_jvs_write_gpio }; static struct io3 diva_jvs_io3; @@ -107,3 +109,26 @@ static void diva_jvs_read_coin_counter( diva_dll.jvs_read_coin_counter(out); } + +static void diva_jvs_write_gpio(void *ctx, uint32_t state) +{ + assert(diva_dll.led_set_leds != NULL); + + // Since Sega uses an odd ordering for the first part of the bitfield, + // let's normalize the data and just send over bytes for the receiver + // to interpret as ON/OFF values. + uint8_t rgb_out[10] = { + state & DIVA_IO_LED_LEFT_PARTITION_RED ? 0xFF : 0x00, + state & DIVA_IO_LED_LEFT_PARTITION_GREEN ? 0xFF : 0x00, + state & DIVA_IO_LED_LEFT_PARTITION_BLUE ? 0xFF : 0x00, + state & DIVA_IO_LED_RIGHT_PARTITION_RED ? 0xFF : 0x00, + state & DIVA_IO_LED_RIGHT_PARTITION_GREEN ? 0xFF : 0x00, + state & DIVA_IO_LED_RIGHT_PARTITION_BLUE ? 0xFF : 0x00, + state & DIVA_IO_LED_BTN_TRIANGLE ? 0xFF : 0x00, + state & DIVA_IO_LED_BTN_CROSS ? 0xFF : 0x00, + state & DIVA_IO_LED_BTN_SQUARE ? 0xFF : 0x00, + state & DIVA_IO_LED_BTN_CIRCLE ? 0xFF : 0x00 + }; + + diva_dll.led_set_leds(0, rgb_out); +} diff --git a/games/divaio/divaio.c b/games/divaio/divaio.c index 0001f2e..d44d723 100644 --- a/games/divaio/divaio.c +++ b/games/divaio/divaio.c @@ -7,7 +7,9 @@ #include "divaio/divaio.h" #include "divaio/config.h" + #include "util/env.h" +#include "util/dprintf.h" static unsigned int __stdcall diva_io_slider_thread_proc(void *ctx); @@ -19,7 +21,7 @@ static struct diva_io_config diva_io_cfg; uint16_t diva_io_get_api_version(void) { - return 0x0100; + return 0x0101; } HRESULT diva_io_jvs_init(void) @@ -132,3 +134,26 @@ static unsigned int __stdcall diva_io_slider_thread_proc(void *ctx) return 0; } + +HRESULT diva_io_led_init(void) +{ + return S_OK; +} + +void diva_io_led_set_leds(uint8_t board, const uint8_t *rgb) +{ +#if 0 + dprintf("DIVA LED: LEFT PARTITION RED: %02X\n", rgb[0]); + dprintf("DIVA LED: LEFT PARTITION GREEN: %02X\n", rgb[1]); + dprintf("DIVA LED: LEFT PARTITION BLUE: %02X\n", rgb[2]); + dprintf("DIVA LED: RIGHT PARTITION RED: %02X\n", rgb[3]); + dprintf("DIVA LED: RIGHT PARTITION GREEN: %02X\n", rgb[4]); + dprintf("DIVA LED: RIGHT PARTITION BLUE: %02X\n", rgb[5]); + dprintf("DIVA LED: BTN TRIANGLE: %02X\n", rgb[6]); + dprintf("DIVA LED: BTN CROSS: %02X\n", rgb[7]); + dprintf("DIVA LED: BTN SQUARE: %02X\n", rgb[8]); + dprintf("DIVA LED: BTN CIRCLE: %02X\n", rgb[9]); +#endif + + return; +} diff --git a/games/divaio/divaio.h b/games/divaio/divaio.h index 7dd13b8..c04dfb0 100644 --- a/games/divaio/divaio.h +++ b/games/divaio/divaio.h @@ -1,21 +1,45 @@ #pragma once +/* + DIVA CUSTOM IO API + + Changelog: + + - 0x0100: Initial API version + - 0x0101: Add partition and button led support +*/ + #include #include #include enum { - DIVA_IO_OPBTN_TEST = 0x01, - DIVA_IO_OPBTN_SERVICE = 0x02 + DIVA_IO_OPBTN_TEST = 0x01, + DIVA_IO_OPBTN_SERVICE = 0x02 }; enum { - DIVA_IO_GAMEBTN_CIRCLE = 0x01, - DIVA_IO_GAMEBTN_CROSS = 0x02, - DIVA_IO_GAMEBTN_SQUARE = 0x04, - DIVA_IO_GAMEBTN_TRIANGLE = 0x08, - DIVA_IO_GAMEBTN_START = 0x10, + DIVA_IO_GAMEBTN_CIRCLE = 0x01, + DIVA_IO_GAMEBTN_CROSS = 0x02, + DIVA_IO_GAMEBTN_SQUARE = 0x04, + DIVA_IO_GAMEBTN_TRIANGLE = 0x08, + DIVA_IO_GAMEBTN_START = 0x10, +}; + +enum { + /* These are the bitmasks to use when checking which + lights are triggered on incoming IO3 GPIO writes. */ + DIVA_IO_LED_LEFT_PARTITION_RED = 1 << 1, + DIVA_IO_LED_LEFT_PARTITION_GREEN = 1 << 0, + DIVA_IO_LED_LEFT_PARTITION_BLUE = 1 << 15, + DIVA_IO_LED_RIGHT_PARTITION_RED = 1 << 14, + DIVA_IO_LED_RIGHT_PARTITION_GREEN = 1 << 13, + DIVA_IO_LED_RIGHT_PARTITION_BLUE = 1 << 12, + DIVA_IO_LED_BTN_TRIANGLE = 1 << 6, + DIVA_IO_LED_BTN_CROSS = 1 << 3, + DIVA_IO_LED_BTN_SQUARE = 1 << 5, + DIVA_IO_LED_BTN_CIRCLE = 1 << 2 }; /* Get the version of the Project Diva IO API that this DLL supports. This @@ -118,3 +142,34 @@ void diva_io_slider_stop(void); Minimum API version: 0x0100 */ void diva_io_slider_set_leds(const uint8_t *rgb); + +/* Initialize LED emulation. This function will be called before any + other diva_io_led_*() function calls. + + All subsequent calls may originate from arbitrary threads and some may + overlap with each other. Ensuring synchronization inside your IO DLL is + your responsibility. + + Minimum API version: 0x0101 */ + +HRESULT diva_io_led_init(void); + +/* Update the cabinet button LEDs. rgb is a pointer to an array up to 10 bytes. + + The LEDs are laid out as follows: + [0]: LEFT PARTITION RED LED + [1]: LEFT PARTITION GREEN LED + [2]: LEFT PARTITION BLUE LED + [3]: RIGHT PARTITION RED LED + [4]: RIGHT PARTITION GREEN LED + [5]: RIGHT PARTITION BLUE LED + [6]: BTN TRIANGLE LED + [7]: BTN CROSS LED + [8]: BTN SQUARE LED + [9]: BTN CIRCLE LED + + The LED is turned on when the byte is 255 and turned off when the byte is 0. + + Minimum API version: 0x0101 */ + +void diva_io_led_set_leds(uint8_t board, const uint8_t *rgb); diff --git a/games/idzhook/jvs.c b/games/idzhook/jvs.c index 2ded71c..64318ca 100644 --- a/games/idzhook/jvs.c +++ b/games/idzhook/jvs.c @@ -176,6 +176,7 @@ static void idz_jvs_read_coin_counter( idz_dll.jvs_read_coin_counter(out); } + static void idz_jvs_write_gpio(void *ctx, uint32_t state) { assert(idz_dll.led_set_leds != NULL); From 0be4b4744828d6435e79f2d9e2e8ac40052bf383 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Wed, 24 Dec 2025 14:26:46 +0100 Subject: [PATCH 04/37] misc: add GetDriveType hook --- common/hooklib/path.c | 56 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/common/hooklib/path.c b/common/hooklib/path.c index b47fe1f..a88d5f9 100644 --- a/common/hooklib/path.c +++ b/common/hooklib/path.c @@ -161,6 +161,14 @@ static DWORD WINAPI hook_GetPrivateProfileSectionW( LPCWSTR lpFileName ); +static UINT WINAPI hook_GetDriveTypeA( + LPCSTR lpRootPathName +); + +static UINT WINAPI hook_GetDriveTypeW( + LPCWSTR lpRootPathName +); + /* Link pointers */ static BOOL (WINAPI *next_CreateDirectoryA)( @@ -303,6 +311,14 @@ static DWORD (WINAPI *next_GetPrivateProfileSectionW)( LPCWSTR lpFileName ); +static UINT (WINAPI *next_GetDriveTypeW)( + LPCWSTR lpRootPathName +); + +static UINT (WINAPI *next_GetDriveTypeA)( + LPCSTR lpRootPathName +); + /* Hook table */ static const struct hook_symbol path_hook_syms[] = { @@ -418,6 +434,14 @@ static const struct hook_symbol path_hook_syms[] = { .name = "GetPrivateProfileSectionW", .patch = hook_GetPrivateProfileSectionW, .link = (void **) &next_GetPrivateProfileSectionW, + }, { + .name = "GetDriveTypeA", + .patch = hook_GetDriveTypeA, + .link = (void **) &next_GetDriveTypeA, + }, { + .name = "GetDriveTypeW", + .patch = hook_GetDriveTypeW, + .link = (void **) &next_GetDriveTypeW, } }; @@ -1338,3 +1362,35 @@ static DWORD WINAPI hook_GetPrivateProfileSectionW( return next_GetPrivateProfileSectionW(lpAppName, lpReturnedString, nSize, trans ? trans: lpFileName); } + + +static UINT WINAPI hook_GetDriveTypeA( + LPCSTR lpRootPathName +) { + char *trans; + BOOL ok; + + ok = path_transform_a(&trans, lpRootPathName); + + if (!ok) { + return FALSE; + } + + return next_GetDriveTypeA(trans ? trans : lpRootPathName); +} + + +static UINT WINAPI hook_GetDriveTypeW( + LPCWSTR lpRootPathName +) { + wchar_t *trans; + BOOL ok; + + ok = path_transform_w(&trans, lpRootPathName); + + if (!ok) { + return FALSE; + } + + return next_GetDriveTypeW(trans ? trans : lpRootPathName); +} From bad219be4cbf985eb9aca4dd66ee351b39087b74 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Wed, 24 Dec 2025 23:19:42 +0100 Subject: [PATCH 05/37] misc: fix path hooks - Added missing MoveFileExW hook - Fixed path memory leaks - Added all CopyFile hooks --- common/hooklib/path.c | 329 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 324 insertions(+), 5 deletions(-) diff --git a/common/hooklib/path.c b/common/hooklib/path.c index a88d5f9..a6f2342 100644 --- a/common/hooklib/path.c +++ b/common/hooklib/path.c @@ -115,6 +115,41 @@ static BOOL WINAPI hook_MoveFileExA( const char *lpNewFileName, uint32_t dwFlags); +static BOOL WINAPI hook_MoveFileExW( + const wchar_t *lpExistingFileName, + const wchar_t *lpNewFileName, + uint32_t dwFlags); + + +static BOOL WINAPI hook_CopyFileA( + const LPCSTR lpExistingFileName, + const LPCSTR lpNewFileName, + BOOL bFailIfExists +); + +static BOOL WINAPI hook_CopyFileW( + const LPCWSTR lpExistingFileName, + const LPCWSTR lpNewFileName, + BOOL bFailIfExists +); + +static BOOL WINAPI hook_CopyFileExA( + LPCSTR lpExistingFileName, + LPCSTR lpNewFileName, + LPPROGRESS_ROUTINE lpProgressRoutine, + LPVOID lpData, + LPBOOL pbCancel, + DWORD dwCopyFlags +); + +static BOOL WINAPI hook_CopyFileExW( + LPCWSTR lpExistingFileName, + LPCWSTR lpNewFileName, + LPPROGRESS_ROUTINE lpProgressRoutine, + LPVOID lpData, + LPBOOL pbCancel, + DWORD dwCopyFlags +); static BOOL WINAPI hook_ReplaceFileA( const char *lpReplacedFileName, @@ -266,6 +301,41 @@ static BOOL (WINAPI *next_MoveFileExA)( const char *lpNewFileName, uint32_t dwFlags); +static BOOL (WINAPI *next_MoveFileExW)( + const wchar_t *lpExistingFileName, + const wchar_t *lpNewFileName, + uint32_t dwFlags); + +static BOOL (WINAPI *next_CopyFileA)( + const LPCSTR lpExistingFileName, + const LPCSTR lpNewFileName, + BOOL bFailIfExists +); + +static BOOL (WINAPI *next_CopyFileW)( + const LPCWSTR lpExistingFileName, + const LPCWSTR lpNewFileName, + BOOL bFailIfExists +); + +static BOOL (WINAPI *next_CopyFileExA)( + LPCSTR lpExistingFileName, + LPCSTR lpNewFileName, + LPPROGRESS_ROUTINE lpProgressRoutine, + LPVOID lpData, + LPBOOL pbCancel, + DWORD dwCopyFlags +); + +static BOOL (WINAPI *next_CopyFileExW)( + LPCWSTR lpExistingFileName, + LPCWSTR lpNewFileName, + LPPROGRESS_ROUTINE lpProgressRoutine, + LPVOID lpData, + LPBOOL pbCancel, + DWORD dwCopyFlags +); + static BOOL (WINAPI *next_ReplaceFileA)( const char *lpReplacedFileName, const char *lpReplacementFileName, @@ -406,6 +476,26 @@ static const struct hook_symbol path_hook_syms[] = { .name = "MoveFileExA", .patch = hook_MoveFileExA, .link = (void **) &next_MoveFileExA, + }, { + .name = "MoveFileExW", + .patch = hook_MoveFileExW, + .link = (void **) &next_MoveFileExW, + }, { + .name = "CopyFileA", + .patch = hook_CopyFileA, + .link = (void **) &next_CopyFileA, + }, { + .name = "CopyFileW", + .patch = hook_CopyFileW, + .link = (void **) &next_CopyFileW, + }, { + .name = "CopyFileExA", + .patch = hook_CopyFileExW, + .link = (void **) &next_CopyFileExA, + }, { + .name = "CopyFileExW", + .patch = hook_CopyFileW, + .link = (void **) &next_CopyFileExW, }, { .name = "ReplaceFileA", .patch = hook_ReplaceFileA, @@ -1193,6 +1283,191 @@ static BOOL WINAPI hook_MoveFileExA( return ok; } +static BOOL WINAPI hook_MoveFileExW( + const wchar_t *lpExistingFileName, + const wchar_t *lpNewFileName, + uint32_t dwFlags) +{ + wchar_t *oldTrans; + wchar_t *newTrans; + BOOL ok; + + ok = path_transform_w(&oldTrans, lpExistingFileName); + + if (!ok) { + return FALSE; + } + + ok = path_transform_w(&newTrans, lpNewFileName); + + if (!ok) { + free(oldTrans); + + return FALSE; + } + + ok = next_MoveFileExW( + oldTrans ? oldTrans : lpExistingFileName, + newTrans ? newTrans : lpNewFileName, + dwFlags); + + free(oldTrans); + free(newTrans); + + return ok; +} + +static BOOL WINAPI hook_CopyFileA( + const LPCSTR lpExistingFileName, + const LPCSTR lpNewFileName, + BOOL bFailIfExists +) +{ + char *oldTrans; + char *newTrans; + BOOL ok; + + ok = path_transform_a(&oldTrans, lpExistingFileName); + + if (!ok) { + return FALSE; + } + + ok = path_transform_a(&newTrans, lpNewFileName); + + if (!ok) { + free(oldTrans); + + return FALSE; + } + + ok = next_CopyFileA( + oldTrans ? oldTrans : lpExistingFileName, + newTrans ? newTrans : lpNewFileName, + bFailIfExists); + + free(oldTrans); + free(newTrans); + + return ok; +} + +static BOOL WINAPI hook_CopyFileW( + const LPCWSTR lpExistingFileName, + const LPCWSTR lpNewFileName, + BOOL bFailIfExists +) { + wchar_t *oldTrans; + wchar_t *newTrans; + BOOL ok; + + ok = path_transform_w(&oldTrans, lpExistingFileName); + + if (!ok) { + return FALSE; + } + + ok = path_transform_w(&newTrans, lpNewFileName); + + if (!ok) { + free(oldTrans); + + return FALSE; + } + + ok = next_CopyFileW( + oldTrans ? oldTrans : lpExistingFileName, + newTrans ? newTrans : lpNewFileName, + bFailIfExists); + + free(oldTrans); + free(newTrans); + + return ok; +} + +static BOOL WINAPI hook_CopyFileExA( + LPCSTR lpExistingFileName, + LPCSTR lpNewFileName, + LPPROGRESS_ROUTINE lpProgressRoutine, + LPVOID lpData, + LPBOOL pbCancel, + DWORD dwCopyFlags +) +{ + char *oldTrans; + char *newTrans; + BOOL ok; + + ok = path_transform_a(&oldTrans, lpExistingFileName); + + if (!ok) { + return FALSE; + } + + ok = path_transform_a(&newTrans, lpNewFileName); + + if (!ok) { + free(oldTrans); + + return FALSE; + } + + ok = next_CopyFileExA( + oldTrans ? oldTrans : lpExistingFileName, + newTrans ? newTrans : lpNewFileName, + lpProgressRoutine, + lpData, + pbCancel, + dwCopyFlags); + + free(oldTrans); + free(newTrans); + + return ok; +} + +static BOOL WINAPI hook_CopyFileExW( + LPCWSTR lpExistingFileName, + LPCWSTR lpNewFileName, + LPPROGRESS_ROUTINE lpProgressRoutine, + LPVOID lpData, + LPBOOL pbCancel, + DWORD dwCopyFlags +) +{ + wchar_t *oldTrans; + wchar_t *newTrans; + BOOL ok; + + ok = path_transform_w(&oldTrans, lpExistingFileName); + + if (!ok) { + return FALSE; + } + + ok = path_transform_w(&newTrans, lpNewFileName); + + if (!ok) { + free(oldTrans); + + return FALSE; + } + + ok = next_CopyFileExW( + oldTrans ? oldTrans : lpExistingFileName, + newTrans ? newTrans : lpNewFileName, + lpProgressRoutine, + lpData, + pbCancel, + dwCopyFlags); + + free(oldTrans); + free(newTrans); + + return ok; +} + static BOOL WINAPI hook_ReplaceFileA( const char *lpReplacedFileName, const char *lpReplacementFileName, @@ -1286,6 +1561,8 @@ static BOOL WINAPI hook_DeleteFileA(const char *lpFileName) ok = next_DeleteFileA(trans ? trans: lpFileName); + free(trans); + return ok; } @@ -1302,6 +1579,8 @@ static BOOL WINAPI hook_DeleteFileW(const wchar_t *lpFileName) ok = next_DeleteFileW(trans ? trans: lpFileName); + free(trans); + return ok; } @@ -1314,6 +1593,7 @@ static DWORD WINAPI hook_GetPrivateProfileStringA( LPCSTR lpFileName ) { char *trans; + DWORD result; BOOL ok; ok = path_transform_a(&trans, lpFileName); @@ -1322,7 +1602,17 @@ static DWORD WINAPI hook_GetPrivateProfileStringA( return FALSE; } - return next_GetPrivateProfileStringA(lpAppName, lpKeyName, lpDefault, lpReturnedString, nSize, trans ? trans: lpFileName); + result = next_GetPrivateProfileStringA( + lpAppName, + lpKeyName, + lpDefault, + lpReturnedString, + nSize, + trans ? trans: lpFileName); + + free(trans); + + return result; } static DWORD WINAPI hook_GetPrivateProfileStringW( @@ -1334,6 +1624,7 @@ static DWORD WINAPI hook_GetPrivateProfileStringW( LPCWSTR lpFileName ) { wchar_t *trans; + DWORD result; BOOL ok; ok = path_transform_w(&trans, lpFileName); @@ -1342,7 +1633,16 @@ static DWORD WINAPI hook_GetPrivateProfileStringW( return FALSE; } - return next_GetPrivateProfileStringW(lpAppName, lpKeyName, lpDefault, lpReturnedString, nSize, trans ? trans: lpFileName); + result = next_GetPrivateProfileStringW( + lpAppName, + lpKeyName, + lpDefault, + lpReturnedString, + nSize, trans ? trans: lpFileName); + + free(trans); + + return result; } static DWORD WINAPI hook_GetPrivateProfileSectionW( @@ -1352,6 +1652,7 @@ static DWORD WINAPI hook_GetPrivateProfileSectionW( LPCWSTR lpFileName ) { wchar_t *trans; + DWORD result; BOOL ok; ok = path_transform_w(&trans, lpFileName); @@ -1360,7 +1661,15 @@ static DWORD WINAPI hook_GetPrivateProfileSectionW( return FALSE; } - return next_GetPrivateProfileSectionW(lpAppName, lpReturnedString, nSize, trans ? trans: lpFileName); + result = next_GetPrivateProfileSectionW( + lpAppName, + lpReturnedString, + nSize, + trans ? trans: lpFileName); + + free(trans); + + return result; } @@ -1368,6 +1677,7 @@ static UINT WINAPI hook_GetDriveTypeA( LPCSTR lpRootPathName ) { char *trans; + UINT result; BOOL ok; ok = path_transform_a(&trans, lpRootPathName); @@ -1376,7 +1686,11 @@ static UINT WINAPI hook_GetDriveTypeA( return FALSE; } - return next_GetDriveTypeA(trans ? trans : lpRootPathName); + result = next_GetDriveTypeA(trans ? trans : lpRootPathName); + + free(trans); + + return result; } @@ -1384,6 +1698,7 @@ static UINT WINAPI hook_GetDriveTypeW( LPCWSTR lpRootPathName ) { wchar_t *trans; + UINT result; BOOL ok; ok = path_transform_w(&trans, lpRootPathName); @@ -1392,5 +1707,9 @@ static UINT WINAPI hook_GetDriveTypeW( return FALSE; } - return next_GetDriveTypeW(trans ? trans : lpRootPathName); + result = next_GetDriveTypeW(trans ? trans : lpRootPathName); + + free(trans); + + return result; } From d5bde32df71c0dde3b16894e84a3706d57e5b1c4 Mon Sep 17 00:00:00 2001 From: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> Date: Wed, 24 Dec 2025 23:02:42 +0000 Subject: [PATCH 06/37] ewf: implement EWF virtualization (#84) (Yeah, I know the OS may cache disk reads, but that wasn't the concern) The ALLS series of cabs runs EWF to prevent anything from being written to C:\. This PR adds another layer to iohook to redirect specific open/read/write/close operations to a virtual file table to prevent disk activity, and thus simulate the presence of EWF. This was mainly aimed at ALPB's billing files. Seriously, look at this - especially the timestamps, Sega wtf are you doing. This does not affect other path hooks. It's turned off by default and will throw an error if the current executable resides on C:\. Tested with FGO and APMv3. ![22bf755754[1].png](/attachments/31771aee-2e4d-4e4f-85e3-c5eec63c6f78) Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/84 Co-authored-by: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> Co-committed-by: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> --- common/platform/config.c | 9 + common/platform/config.h | 1 + common/platform/ewf.c | 558 ++++++++++++++++++++++++++++++++++++ common/platform/ewf.h | 41 +++ common/platform/meson.build | 3 + common/platform/platform.c | 6 + common/platform/platform.h | 2 + common/platform/vfs.c | 1 + doc/config/common.md | 28 ++ meson.build | 3 + meson_options.txt | 5 + 11 files changed, 657 insertions(+) create mode 100644 common/platform/ewf.c create mode 100644 common/platform/ewf.h diff --git a/common/platform/config.c b/common/platform/config.c index 7461430..4f948d0 100644 --- a/common/platform/config.c +++ b/common/platform/config.c @@ -44,6 +44,7 @@ void platform_config_load(struct platform_config *cfg, const wchar_t *filename) vfs_config_load(&cfg->vfs, filename); system_config_load(&cfg->system, filename); openssl_config_load(&cfg->openssl, filename); + ewf_config_load(&cfg->ewf, filename); } void amvideo_config_load(struct amvideo_config *cfg, const wchar_t *filename) @@ -374,3 +375,11 @@ void openssl_config_load(struct openssl_config *cfg, const wchar_t *filename) cfg->enable = GetPrivateProfileIntW(L"openssl", L"enable", 1, filename); cfg->override = GetPrivateProfileIntW(L"openssl", L"override", 0, filename); } + +void ewf_config_load(struct ewf_config* cfg, const wchar_t* filename) { + assert(cfg != NULL); + assert(filename != NULL); + + cfg->enable = GetPrivateProfileIntW(L"ewf", L"enable", 0, filename); + cfg->full = GetPrivateProfileIntW(L"ewf", L"full", 0, filename); +} diff --git a/common/platform/config.h b/common/platform/config.h index efad5a3..db6aad8 100644 --- a/common/platform/config.h +++ b/common/platform/config.h @@ -38,3 +38,4 @@ void pcbid_config_load(struct pcbid_config *cfg, const wchar_t *filename); void vfs_config_load(struct vfs_config *cfg, const wchar_t *filename); void system_config_load(struct system_config *cfg, const wchar_t *filename); void openssl_config_load(struct openssl_config *cfg, const wchar_t *filename); +void ewf_config_load(struct ewf_config *cfg, const wchar_t *filename); diff --git a/common/platform/ewf.c b/common/platform/ewf.c new file mode 100644 index 0000000..800e3cd --- /dev/null +++ b/common/platform/ewf.c @@ -0,0 +1,558 @@ +#include +#include + +#include +#include +#include +#include +#include + +#include "hook/iohook.h" +#include "hook/procaddr.h" + +#include "platform/ewf.h" + +#include + +#include "hooklib/path.h" +#include "util/dprintf.h" +#include "util/dump.h" + +/* EWF hook */ + +const static struct ewf_config* ewf_config; +static struct ewf_virtual_file virtual_file_table[EWF_MAX_VIRTUAL_FILES] = {0}; +static struct ewf_real_handle handle_table[EWF_MAX_HANDLES] = {0}; +static CRITICAL_SECTION file_table_lock; +static CRITICAL_SECTION handle_table_lock; +static wchar_t windows_directory[MAX_PATH]; + +static const wchar_t* default_drive = L"C:\\"; +static const wchar_t default_paths[][MAX_PATH] = { + L"alib.conf", + L"cacert.pem", + L"first_ar.conf", + L"last_pras.log", + L"last_shime.log", + L"play_history.csv" +}; + +/* Helper functions */ + +static HRESULT atow(const char* string, wchar_t** result) { + if (string == NULL) { + *result = NULL; + return S_FALSE; + } + const size_t n = strlen(string); + wchar_t* widestring = malloc((n + 1) * sizeof(wchar_t)); + if (widestring == NULL) { + return E_OUTOFMEMORY; + } + mbstowcs_s(NULL, widestring, n + 1, string, n); + + *result = widestring; + return S_OK; +} + +static inline bool wprefix(const wchar_t* pre, const wchar_t* str) { + return wcsncmp(pre, str, wcslen(pre)) == 0; +} + +static inline bool wsuffix(const wchar_t* suffix, const wchar_t* str) { + if (str == NULL || suffix == NULL) { + return false; + } + const size_t str_len = wcslen(str); + const size_t suf_len = wcslen(suffix); + if (suf_len > str_len) { + return false; + } + return wcsncmp(str + str_len - suf_len, suffix, suf_len) == 0; +} + + +static BOOL ewf_needs_virtualization(const wchar_t* path) { + if (path == NULL) { + return FALSE; + } + if (ewf_config->full) { + if (wprefix(default_drive, path) && !wprefix(windows_directory, path)) { + return TRUE; + } + } else { + for (int i = 0; i < _countof(default_paths); i++) { + if (wsuffix(default_paths[i], path)) { + return TRUE; + } + } + } + return FALSE; +} + +static struct ewf_virtual_file* ewf_get_virtual_file(HANDLE handle) { + struct ewf_virtual_file* ret = NULL; + EnterCriticalSection(&file_table_lock); + for (int i = 0; i < EWF_MAX_VIRTUAL_FILES; i++) { + if (virtual_file_table[i].virtual_handle == handle) { + ret = &virtual_file_table[i]; + break; + } + } + LeaveCriticalSection(&file_table_lock); + return ret; +} + +static struct ewf_virtual_file* ewf_find_virtual_file(const wchar_t* path) { + if (path == NULL) { + return NULL; + } + HANDLE ret = NULL; + EnterCriticalSection(&file_table_lock); + for (int i = 0; i < EWF_MAX_VIRTUAL_FILES; i++) { + if (virtual_file_table[i].virtual_handle != NULL && wcscmp(virtual_file_table[i].path, path) == 0) { + ret = &virtual_file_table[i]; + break; + } + } + LeaveCriticalSection(&file_table_lock); + return ret; +} + +static struct ewf_virtual_file* ewf_create_virtual_file(const wchar_t* path) { + assert(path != NULL); + struct ewf_virtual_file* ret = NULL; + EnterCriticalSection(&file_table_lock); + for (int i = 0; i < EWF_MAX_VIRTUAL_FILES; i++) { + if (virtual_file_table[i].virtual_handle == NULL) { + struct ewf_virtual_file* h = &virtual_file_table[i]; + HRESULT hr = iohook_open_nul_fd(&h->virtual_handle); + if (!SUCCEEDED(hr)) { + dprintf("EWF: Could not create handle: Failed to get NUL handle: %lx", hr); + break; + } + dprintf("EWF: Created virtual file: %ls\n", path); + wcscpy_s(h->path, MAX_PATH, path); + h->length = 0; + h->data = NULL; + ret = h; + break; + } + } + if (ret == NULL) { + dprintf("EWF: Could not create handle: Too many virtualized files\n"); + } + LeaveCriticalSection(&file_table_lock); + return ret; +} + +static BOOL ewf_delete_virtual_file(HANDLE virtual_handle) { + if (virtual_handle == NULL) { + return TRUE; + } + struct ewf_virtual_file* match = NULL; + EnterCriticalSection(&file_table_lock); + for (int i = 0; i < EWF_MAX_VIRTUAL_FILES; i++) { + if (virtual_file_table[i].virtual_handle == virtual_handle) { + match = &virtual_file_table[i]; + break; + } + } + LeaveCriticalSection(&file_table_lock); + if (match) { + match->virtual_handle = NULL; +#if LOG_EWF + dprintf("EWF: Deleted file: %ls\n", match->path); +#endif + return CloseHandle(virtual_handle); + } else { + SetLastError(ERROR_INVALID_HANDLE); + return FALSE; + } +} + +static struct ewf_real_handle* ewf_open_virtual_file(HANDLE virtual_handle) { + struct ewf_virtual_file* file = ewf_get_virtual_file(virtual_handle); + struct ewf_real_handle* h = NULL; + if (file == NULL) { + dprintf("EWF: open failed: invalid handle\n"); + return NULL; + } + EnterCriticalSection(&handle_table_lock); + for (int i = 0; i < EWF_MAX_HANDLES; i++) { + if (handle_table[i].real_handle == NULL) { + h = &handle_table[i]; + HRESULT hr = iohook_open_nul_fd(&h->real_handle); + if (!SUCCEEDED(hr)) { + dprintf("EWF: Could not create handle: Failed to get NUL handle: %lx", hr); + break; + } + h->virtual_file = file; + h->offset = 0; +#if LOG_EWF + dprintf("EWF: Virtual file opened: %ls\n", file->path); +#endif + break; + } + } + LeaveCriticalSection(&handle_table_lock); + if (h == NULL) { + dprintf("EWF: Could not create handle: Too many open files\n"); + } + return h; +} + +static struct ewf_real_handle* ewf_get_real_handle(HANDLE real_handle) { + if (real_handle == NULL) { + return NULL; + } + struct ewf_real_handle* match = NULL; + EnterCriticalSection(&handle_table_lock); + for (int i = 0; i < EWF_MAX_HANDLES; i++) { + if (handle_table[i].real_handle == real_handle) { + match = &handle_table[i]; + break; + } + } + LeaveCriticalSection(&handle_table_lock); + return match; +} + +static BOOL ewf_close_virtual_file(HANDLE real_handle) { + const struct ewf_virtual_file* match = NULL; + EnterCriticalSection(&handle_table_lock); + for (int i = 0; i < EWF_MAX_HANDLES; i++) { + if (handle_table[i].real_handle == real_handle) { + match = handle_table[i].virtual_file; + handle_table[i].real_handle = NULL; + break; + } + } + LeaveCriticalSection(&handle_table_lock); + if (match != NULL) { +#if LOG_EWF + dprintf("EWF: Virtual file closed: %ls\n", match->path); +#endif + return CloseHandle(real_handle); + } else { + SetLastError(ERROR_INVALID_HANDLE); + return FALSE; + } +} + +/* Hooks */ + +static int __cdecl hook_stat64i32( + const char* path, + struct _stat64i32* buffer +); + +static int (__cdecl *next_stat64i32)( + const char* path, + struct _stat64i32* buffer); + +static const struct hook_symbol ewf_hook_syms[] = { + { + .name = "__imp__stat64i32", + .patch = hook_stat64i32, + .link = (void **) &next_stat64i32, + }, + { + .name = "_stat64i32", + .patch = hook_stat64i32, + .link = (void **) &next_stat64i32, + } +}; + +/* EWF hook main functions */ + +static HRESULT ewf_handle_irp(struct irp* irp); + +static HRESULT ewf_handle_open(struct irp* irp); + +static HRESULT ewf_handle_close(const struct irp* irp, const struct ewf_real_handle* file); + +static HRESULT ewf_handle_read(struct irp* irp, struct ewf_real_handle* handle); + +static HRESULT ewf_handle_write(const struct irp* irp, const struct ewf_real_handle* file); + +void ewf_hook_insert_hooks(HMODULE target) { + hook_table_apply( + target, + "msvcr110d.dll", + ewf_hook_syms, + _countof(ewf_hook_syms)); + hook_table_apply( + target, + "msvcr110.dll", + ewf_hook_syms, + _countof(ewf_hook_syms)); +} + +HRESULT ewf_hook_init(const struct ewf_config* config) { + assert(config != NULL); + + ewf_config = config; + + if (!config->enable) { + return S_FALSE; + } + + GetWindowsDirectoryW(windows_directory, MAX_PATH); + + if (config->full) { + wchar_t executable_path[MAX_PATH]; + GetModuleFileNameW(NULL, executable_path, MAX_PATH); + if (wprefix(default_drive, executable_path)) { + dprintf("FATAL: EWF full virtualization cannot be enabled with the game executable being located on %ls\n", + default_drive); + return E_FAIL; + } + } + + dprintf("EWF: Virtualizing disk I/O to %s\n", + config->full ? "the entirety of the C:\\ drive" : "the ALPB billing directory"); + + InitializeCriticalSection(&handle_table_lock); + InitializeCriticalSection(&file_table_lock); + + ewf_hook_insert_hooks(NULL); + + return iohook_push_handler(ewf_handle_irp); +} + +static HRESULT ewf_handle_irp(struct irp* irp) { + assert(irp != NULL); + + struct ewf_real_handle* h = NULL; + + if (irp->op == IRP_OP_OPEN) { + if (!ewf_needs_virtualization(irp->open_filename)) { + return iohook_invoke_next(irp); + } + } else { + h = ewf_get_real_handle(irp->fd); + if (h == NULL) { + return iohook_invoke_next(irp); + } + } + + + switch (irp->op) { + case IRP_OP_OPEN: return ewf_handle_open(irp); + case IRP_OP_WRITE: return ewf_handle_write(irp, h); + case IRP_OP_READ: return ewf_handle_read(irp, h); + case IRP_OP_CLOSE: return ewf_handle_close(irp, h); + default: return HRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION); + } +} + +static HRESULT ewf_handle_open(struct irp* irp) { + assert(irp != NULL); + + if (irp->ovl != NULL) { + dprintf("EWF: async file operations not supported\n"); + return E_NOTIMPL; + } + + struct ewf_virtual_file* f = ewf_find_virtual_file(irp->open_filename); + + if (irp->open_creation == CREATE_NEW) { + if (f != NULL) { + return HRESULT_FROM_WIN32(ERROR_FILE_EXISTS); + } else { + f = ewf_create_virtual_file(irp->open_filename); + } + } else if (irp->open_creation == CREATE_ALWAYS) { + if (f != NULL) { + SetLastError(ERROR_ALREADY_EXISTS); +#if LOG_EWF + dprintf("EWF: File was truncated\n"); +#endif + f->length = 0; + free(f->data); + } else { + f = ewf_create_virtual_file(irp->open_filename); + } + } else if (irp->open_creation == OPEN_EXISTING) { + if (f == NULL) { + return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND); + } + } else if (irp->open_creation == OPEN_ALWAYS) { + if (f != NULL) { + SetLastError(ERROR_ALREADY_EXISTS); + } else { + f = ewf_create_virtual_file(irp->open_filename); + } + } else if (irp->open_creation == TRUNCATE_EXISTING) { + if (f != NULL) { +#if LOG_EWF + dprintf("EWF: File was truncated\n"); +#endif + f->length = 0; + free(f->data); + } else { + return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND); + } + } else { + return E_INVALIDARG; + } + + const struct ewf_real_handle* h = ewf_open_virtual_file(f->virtual_handle); + irp->fd = h->real_handle; + return S_OK; +} + +static HRESULT ewf_handle_close(const struct irp* irp, const struct ewf_real_handle* file) { + assert(irp != NULL); + assert(file != NULL); + + return ewf_close_virtual_file(file->real_handle) ? S_OK : E_FAIL; +} + +static HRESULT ewf_handle_read(struct irp* irp, struct ewf_real_handle* handle) { + assert(irp != NULL); + assert(handle != NULL); + + const struct ewf_virtual_file* f = handle->virtual_file; + size_t pos = handle->offset; + size_t to_read = irp->read.nbytes; + size_t max = f->length; + + if (pos > max || pos < 0) { + dprintf("EWF: Out-of-bounds read from %ls\n", f->path); + return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF); + } + if (max == 0 || pos >= max || to_read == 0) { +#if LOG_EWF + dprintf("EWF: Zero read of %d at %d/%d from %ls\n", (int) to_read, (int) pos, (int) max, f->path); +#endif + irp->read.pos = 0; + return S_FALSE; + } + + if (pos + to_read > max) { + to_read = max - pos; + } + + memcpy(irp->read.bytes, f->data, to_read); + irp->read.pos = to_read; + handle->offset += to_read; + +#if LOG_EWF + dprintf("EWF: Read %d/%d bytes (offset %d, max %d) from %ls\n", (int) to_read, (int) irp->read.nbytes, (int) pos, + (int) f->length, f->path); + dump_iobuf(&irp->read); +#endif + + return S_OK; +} + +static HRESULT ewf_handle_write(const struct irp* irp, const struct ewf_real_handle* file) { + assert(irp != NULL); + assert(file != NULL); + + struct ewf_virtual_file* f = file->virtual_file; + const size_t n = irp->write.nbytes; + const void* data = irp->write.bytes; + + if (f->length == 0) { + const size_t initial_buf = max(n, EWF_DEFAULT_FILE_BUFFER_SIZE); + f->data = malloc(initial_buf); + if (f->data == NULL) { + return E_OUTOFMEMORY; + } + f->alloc_length = initial_buf; + } else if (f->length + n > f->alloc_length) { + void* ptr = realloc(f->data, f->alloc_length + n); + if (ptr == NULL) { + return E_OUTOFMEMORY; + } + f->data = ptr; + f->alloc_length = f->alloc_length + n; + } + + if (memcpy_s((char *) f->data + f->length, f->alloc_length - f->length, data, n) != 0) { + dprintf("EWF: Failed to copy %d bytes at offset %d (max %d)\n", (int) n, (int) f->length, + (int) f->alloc_length); + return E_NOT_SUFFICIENT_BUFFER; + } + f->length += n; + +#if LOG_EWF + dprintf("EWF: Write %d bytes to %ls\n", (int) n, f->path); + dump_const_iobuf(&irp->write); + dprintf("EWF: File content (%d, allocated %d)\n", (int) f->length, (int) f->alloc_length); + dump(f->data, f->length); +#endif + + return S_OK; +} + +// MSVC implementation detail. amdaemon depends on this succeeding, or you will be spammed with "cannot get accounting info" +static int __cdecl hook_stat64i32( + const char* path, + struct _stat64i32* buffer +) { + if (buffer == NULL || path == NULL) { + _set_errno(EINVAL); + return -1; + } + + wchar_t* wpath = NULL; + wchar_t* trans; + BOOL ok; + + HRESULT result = atow(path, &wpath); + if (!SUCCEEDED(result)) { + _set_errno(ENOMEM); + return -1; + } + + ok = path_transform_w(&trans, wpath); + + if (!ok) { + _set_errno(EINVAL); + return -1; + } + + wchar_t* target_path = trans ? trans : wpath; + + if (!ewf_needs_virtualization(target_path)) { + free(trans); + free(wpath); +#if LOG_EWF + dprintf("EWF: stat64i32: ignore: %s\n", path); +#endif + return next_stat64i32(path, buffer); + } + + const struct ewf_virtual_file* f = ewf_find_virtual_file(target_path); + if (f == NULL) { + free(trans); + free(wpath); + dprintf("EWF: stat64i32: File not found: %s\n", path); + _set_errno(ENOENT); + return -1; + } + + buffer->st_gid = 0; + buffer->st_atime = time(NULL); + buffer->st_ctime = time(NULL); + buffer->st_dev = 0; + buffer->st_ino = 0; + buffer->st_mode = _S_IFREG; + buffer->st_mtime = time(NULL); + buffer->st_nlink = 1; + buffer->st_rdev = 0; + buffer->st_size = (_off_t) f->length; + buffer->st_uid = 0; + +#if LOG_EWF + dprintf("EWF: stat64i32: file length of %s: %d\n", path, (int) f->length); +#endif + free(trans); + free(wpath); + + return 0; +} diff --git a/common/platform/ewf.h b/common/platform/ewf.h new file mode 100644 index 0000000..2d7fc63 --- /dev/null +++ b/common/platform/ewf.h @@ -0,0 +1,41 @@ +#pragma once + +#include + +#include +#include + +#define EWF_MAX_VIRTUAL_FILES 255 +#define EWF_MAX_HANDLES 50000 +#define EWF_DEFAULT_FILE_BUFFER_SIZE 1024 + +struct ewf_config { + bool enable; + bool full; +}; + +// A virtual file that we are storing in memory. +struct ewf_virtual_file { + // The handle that we use internally. This is NULL for a handle that doesn't exist and non-NULL for a handle that maps to a virtual file. + HANDLE virtual_handle; + // The path that is being virtualized (ex. C:\sample.txt). + wchar_t path[MAX_PATH]; + // The current length of the virtual file. + size_t length; + // The current length of the allocated data buffer. + size_t alloc_length; + // Pointer to some buffer holding file data. This is guaranteed to be at least length bytes in size. If length is zero, this may be NULL. + void* data; +}; + +// An open handle to a ewf_virtual_file. +struct ewf_real_handle { + // The "real" handle passed to the application as a result of CreateFile, etc. + HANDLE real_handle; + // The virtual file the real handle points to. + struct ewf_virtual_file* virtual_file; + // The current read offset. + size_t offset; +}; + +HRESULT ewf_hook_init(const struct ewf_config* config); diff --git a/common/platform/meson.build b/common/platform/meson.build index 0e51ddd..8f90208 100644 --- a/common/platform/meson.build +++ b/common/platform/meson.build @@ -4,6 +4,7 @@ platform_lib = static_library( implicit_include_directories : false, dependencies : [ capnhook.get_variable('hook_dep'), + capnhook.get_variable('hooklib_dep'), shlwapi_lib, bcrypt_lib, ], @@ -18,6 +19,8 @@ platform_lib = static_library( 'dns.h', 'epay.c', 'epay.h', + 'ewf.c', + 'ewf.h', 'hwmon.c', 'hwmon.h', 'hwreset.c', diff --git a/common/platform/platform.c b/common/platform/platform.c index d1f380d..2558809 100644 --- a/common/platform/platform.c +++ b/common/platform/platform.c @@ -101,5 +101,11 @@ HRESULT platform_hook_init( return hr; } + hr = ewf_hook_init(&cfg->ewf); + + if (FAILED(hr)) { + return hr; + } + return S_OK; } diff --git a/common/platform/platform.h b/common/platform/platform.h index dc8dfc2..0f01eaf 100644 --- a/common/platform/platform.h +++ b/common/platform/platform.h @@ -2,6 +2,7 @@ #include +#include "ewf.h" #include "platform/amvideo.h" #include "platform/clock.h" #include "platform/dns.h" @@ -30,6 +31,7 @@ struct platform_config { struct vfs_config vfs; struct system_config system; struct openssl_config openssl; + struct ewf_config ewf; }; HRESULT platform_hook_init( diff --git a/common/platform/vfs.c b/common/platform/vfs.c index 372add1..22f0583 100644 --- a/common/platform/vfs.c +++ b/common/platform/vfs.c @@ -14,6 +14,7 @@ #include "platform/vfs.h" +#include "ewf.h" #include "util/dprintf.h" static void vfs_fixup_path(wchar_t *path, size_t max_count); diff --git a/doc/config/common.md b/doc/config/common.md index 1be9b7c..3e9361e 100644 --- a/doc/config/common.md +++ b/doc/config/common.md @@ -694,3 +694,31 @@ Default: `0` Enables the override to always hook the OpenSSL env variable. By default the hook is only applied to Intel CPUs with the SHA extension present. + +## `[ewf]` + +Configure Enhanced Write Filter / Unified Write Filter emulation. + +### `enable` + +Default: `0` + +Enables the EWF/UWF hook. This will cause anything the game writes to the following paths to become virtualized to +reduce disk I/O: + +* C:\Users\AppUser\temp\alib.conf +* C:\Users\AppUser\temp\cacert.pem +* C:\Users\AppUser\temp\first_ar.conf +* C:\Users\AppUser\temp\last_pras.log +* C:\Users\AppUser\temp\last_shime.log +* C:\Users\AppUser\temp\play_history.csv +* and their equivalent Windows XP variants + +### `full` + +Default: `0` + +Enabling full virtualization will virtualize ALL writes to the C:\ drive. Using this setting is highly discouraged +unless you know what you're doing! + +The Windows directory is always excluded from virtualization. diff --git a/meson.build b/meson.build index 76e9577..b4376bb 100644 --- a/meson.build +++ b/meson.build @@ -85,6 +85,9 @@ endif if get_option('log_all') or get_option('log_vfs') add_project_arguments('-DLOG_VFS', language: 'c') endif +if get_option('log_all') or get_option('log_ewf') + add_project_arguments('-DLOG_EWF', language : 'c') +endif shlwapi_lib = cc.find_library('shlwapi') dinput8_lib = cc.find_library('dinput8') diff --git a/meson_options.txt b/meson_options.txt index b6989f4..7cce000 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -78,3 +78,8 @@ option('log_vfs', value : false, description : 'Enable debug logging for file system redirections' ) +option('log_ewf', + type : 'boolean', + value : false, + description : 'Enable debug logging for file system virtualization' +) From 60e7e1d4324a96562ed585611bafa343a2c25350 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Thu, 25 Dec 2025 00:05:51 +0100 Subject: [PATCH 07/37] mai2: add all net auth 1.0 --- dist/mai2/config_hook.json | 6 ++++++ dist/mai2/launch.bat | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 dist/mai2/config_hook.json diff --git a/dist/mai2/config_hook.json b/dist/mai2/config_hook.json new file mode 100644 index 0000000..cfdea9d --- /dev/null +++ b/dist/mai2/config_hook.json @@ -0,0 +1,6 @@ +{ + "allnet_auth" : + { + "type" : "1.0" + } +} diff --git a/dist/mai2/launch.bat b/dist/mai2/launch.bat index e7c6d9a..4cecdc3 100644 --- a/dist/mai2/launch.bat +++ b/dist/mai2/launch.bat @@ -2,7 +2,7 @@ pushd %~dp0 -start "AM Daemon" /min inject -d -k mai2hook.dll amdaemon.exe -f -c config_common.json config_server.json config_client.json +start "AM Daemon" /min inject -d -k mai2hook.dll amdaemon.exe -f -c config_common.json config_server.json config_client.json config_hook.json inject -d -k mai2hook.dll sinmai -screen-fullscreen 0 -popupwindow -screen-width 2160 -screen-height 1920 -silent-crashes taskkill /f /im amdaemon.exe > nul 2>&1 From 7c10698ca9f17fda268cc55fe9cec8132d4a42b7 Mon Sep 17 00:00:00 2001 From: octocat Date: Wed, 24 Dec 2025 23:08:08 +0000 Subject: [PATCH 08/37] Fix compiler errors for 64bit gcc (#87) Why would anyone detect ISA at runtime? Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/87 Co-authored-by: octocat Co-committed-by: octocat --- common/hooklib/spike.c | 34 +++++----------------------------- games/mercuryhook/elisabeth.c | 2 +- 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/common/hooklib/spike.c b/common/hooklib/spike.c index a66057d..a6c1353 100644 --- a/common/hooklib/spike.c +++ b/common/hooklib/spike.c @@ -89,29 +89,6 @@ static void spike_fn_perror( OutputDebugStringA(line); } -BOOL is_current_module_x64() -{ - HMODULE hModule = GetModuleHandleW(NULL); - MODULEINFO moduleInfo = {0}; - if (!GetModuleInformation(GetCurrentProcess(), hModule, &moduleInfo, - sizeof(moduleInfo))) { - return FALSE; - } - - PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)moduleInfo.lpBaseOfDll; - PIMAGE_NT_HEADERS ntHeaders = - (PIMAGE_NT_HEADERS)((BYTE *)moduleInfo.lpBaseOfDll + - dosHeader->e_lfanew); - - if (ntHeaders->FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64) { - return TRUE; - } else if (ntHeaders->FileHeader.Machine == IMAGE_FILE_MACHINE_I386) { - return FALSE; - } else { - return FALSE; - } -} - BOOL is_valid_rva(LPCWSTR module_name, uintptr_t rva) { HMODULE module_base = GetModuleHandleW(module_name); if (!module_base) { @@ -136,19 +113,18 @@ static void spike_insert_jmp(LPCWSTR module_name, uintptr_t rva, void *proc) { uintptr_t target_addr = (uintptr_t)target; uintptr_t func_addr = (uintptr_t)func_ptr; - if (is_current_module_x64()) { + #if defined(_WIN64) || defined(__amd64__) uint64_t relativeOffset = (uint64_t)(func_addr - target_addr - 5); uint8_t absoluteJump[] = {0x48, 0xB8, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0xFF, 0xE0}; + 0x00, 0x00, 0x00, 0x00, 0xFF, 0xE0}; memcpy(absoluteJump + 2, &func_ptr, 8); pe_patch(target, absoluteJump, sizeof(absoluteJump)); - } - else { + #else uint32_t jumpOffset = (uint32_t)(func_addr - target_addr - 5); uint8_t relativeJump[] = {0xE9, 0x00, 0x00, 0x00, 0x00}; memcpy(relativeJump + 1, &jumpOffset, 4); pe_patch(target, relativeJump, sizeof(relativeJump)); - } + #endif } static void spike_insert_ptr(LPCWSTR module_name, uintptr_t rva, void *ptr) { @@ -242,7 +218,7 @@ void spike_hook_init(const wchar_t *ini_file) basename = slash + 1; } else { basename = module; - } + } /* Check our INI file to see if any spikes are configured for this EXE. Normally we separate out config reading into a separate module... */ diff --git a/games/mercuryhook/elisabeth.c b/games/mercuryhook/elisabeth.c index 7078a21..dad8f9b 100644 --- a/games/mercuryhook/elisabeth.c +++ b/games/mercuryhook/elisabeth.c @@ -23,7 +23,7 @@ static void dll_hook_insert_hooks(HMODULE target); static FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name); static FARPROC (WINAPI *next_GetProcAddress)(HMODULE hModule, const char *name); static int my_USBIntLED_Init(); -static int my_USBIntLED_set(); +static int my_USBIntLED_set(int data1, struct led_data data2); static const struct hook_symbol win32_hooks[] = { { From 16942a03d4c9c3412ed3360c6c2fe50d04810b55 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Thu, 25 Dec 2025 00:12:03 +0100 Subject: [PATCH 09/37] mercury: rename elisabeth to elizabeth --- games/mercuryhook/config.c | 8 ++++---- games/mercuryhook/config.h | 4 ++-- games/mercuryhook/dllmain.c | 4 ++-- games/mercuryhook/{elisabeth.c => elizabeth.c} | 10 +++++----- games/mercuryhook/{elisabeth.h => elizabeth.h} | 4 ++-- games/mercuryhook/mercury-dll.h | 2 +- games/mercuryhook/meson.build | 4 ++-- games/mercuryio/mercuryio.c | 3 ++- games/mercuryio/mercuryio.h | 2 +- 9 files changed, 21 insertions(+), 20 deletions(-) rename games/mercuryhook/{elisabeth.c => elizabeth.c} (88%) rename games/mercuryhook/{elisabeth.h => elizabeth.h} (63%) diff --git a/games/mercuryhook/config.c b/games/mercuryhook/config.c index d15c305..e781273 100644 --- a/games/mercuryhook/config.c +++ b/games/mercuryhook/config.c @@ -42,15 +42,15 @@ void touch_config_load( filename); } -void elisabeth_config_load( - struct elisabeth_config *cfg, +void elizabeth_config_load( + struct elizabeth_config *cfg, const wchar_t *filename) { assert(cfg != NULL); assert(filename != NULL); cfg->enable = GetPrivateProfileIntW( - L"elisabeth", + L"elizabeth", L"enable", 1, filename); @@ -72,5 +72,5 @@ void mercury_hook_config_load( vfd_config_load(&cfg->vfd, filename); mercury_dll_config_load(&cfg->dll, filename); touch_config_load(&cfg->touch, filename); - elisabeth_config_load(&cfg->elisabeth, filename); + elizabeth_config_load(&cfg->elizabeth, filename); } diff --git a/games/mercuryhook/config.h b/games/mercuryhook/config.h index 2105704..c80b318 100644 --- a/games/mercuryhook/config.h +++ b/games/mercuryhook/config.h @@ -9,7 +9,7 @@ #include "mercuryhook/mercury-dll.h" #include "mercuryhook/touch.h" -#include "mercuryhook/elisabeth.h" +#include "mercuryhook/elizabeth.h" #include "platform/config.h" @@ -22,7 +22,7 @@ struct mercury_hook_config { struct vfd_config vfd; struct mercury_dll_config dll; struct touch_config touch; - struct elisabeth_config elisabeth; + struct elizabeth_config elizabeth; }; void mercury_dll_config_load( diff --git a/games/mercuryhook/dllmain.c b/games/mercuryhook/dllmain.c index 745b420..11a5527 100644 --- a/games/mercuryhook/dllmain.c +++ b/games/mercuryhook/dllmain.c @@ -30,7 +30,7 @@ #include "mercuryhook/config.h" #include "mercuryhook/io4.h" #include "mercuryhook/mercury-dll.h" -#include "mercuryhook/elisabeth.h" +#include "mercuryhook/elizabeth.h" #include "mercuryhook/touch.h" #include "platform/platform.h" @@ -107,7 +107,7 @@ static DWORD CALLBACK mercury_pre_startup(void) } /* Start elisabeth Hooks for the LED and IO Board DLLs */ - elisabeth_hook_init(&mercury_hook_cfg.elisabeth); + elizabeth_hook_init(&mercury_hook_cfg.elizabeth); touch_hook_init(&mercury_hook_cfg.touch); diff --git a/games/mercuryhook/elisabeth.c b/games/mercuryhook/elizabeth.c similarity index 88% rename from games/mercuryhook/elisabeth.c rename to games/mercuryhook/elizabeth.c index dad8f9b..9fd4aaa 100644 --- a/games/mercuryhook/elisabeth.c +++ b/games/mercuryhook/elizabeth.c @@ -5,7 +5,7 @@ #include #include -#include "mercuryhook/elisabeth.h" +#include "mercuryhook/elizabeth.h" #include "mercuryhook/mercury-dll.h" #include "hook/table.h" @@ -17,7 +17,7 @@ #include "util/dprintf.h" -/* Hooks targeted DLLs dynamically loaded by elisabeth. */ +/* Hooks targeted DLLs dynamically loaded by elizabeth. */ static void dll_hook_insert_hooks(HMODULE target); static FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name); @@ -33,13 +33,13 @@ static const struct hook_symbol win32_hooks[] = { } }; -HRESULT elisabeth_hook_init(struct elisabeth_config *cfg) +HRESULT elizabeth_hook_init(struct elizabeth_config *cfg) { if (!cfg->enable) { return S_OK; } dll_hook_insert_hooks(NULL); - dprintf("Elisabeth: Init\n"); + dprintf("elizabeth: Init\n"); return S_OK; } @@ -75,7 +75,7 @@ FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name) /* Intercept the call to initialize the LED board. */ static int my_USBIntLED_Init() { - dprintf("Elisabeth: my_USBIntLED_Init hit!\n"); + dprintf("elizabeth: my_USBIntLED_Init hit!\n"); return 1; } diff --git a/games/mercuryhook/elisabeth.h b/games/mercuryhook/elizabeth.h similarity index 63% rename from games/mercuryhook/elisabeth.h rename to games/mercuryhook/elizabeth.h index 32512f3..f31e249 100644 --- a/games/mercuryhook/elisabeth.h +++ b/games/mercuryhook/elizabeth.h @@ -8,8 +8,8 @@ struct led_data { uint8_t rgba[480 * 4]; }; -struct elisabeth_config { +struct elizabeth_config { bool enable; }; -HRESULT elisabeth_hook_init(struct elisabeth_config *cfg); +HRESULT elizabeth_hook_init(struct elizabeth_config *cfg); diff --git a/games/mercuryhook/mercury-dll.h b/games/mercuryhook/mercury-dll.h index fa27edb..77c5bc6 100644 --- a/games/mercuryhook/mercury-dll.h +++ b/games/mercuryhook/mercury-dll.h @@ -3,7 +3,7 @@ #include #include "mercuryio/mercuryio.h" -#include "mercuryhook/elisabeth.h" +#include "mercuryhook/elizabeth.h" struct mercury_dll { uint16_t api_version; diff --git a/games/mercuryhook/meson.build b/games/mercuryhook/meson.build index fd6f46a..f11ba29 100644 --- a/games/mercuryhook/meson.build +++ b/games/mercuryhook/meson.build @@ -25,8 +25,8 @@ shared_library( 'io4.h', 'mercury-dll.c', 'mercury-dll.h', - 'elisabeth.h', - 'elisabeth.c', + 'elizabeth.h', + 'elizabeth.c', 'touch.h', 'touch.c' ], diff --git a/games/mercuryio/mercuryio.c b/games/mercuryio/mercuryio.c index 880349c..2191650 100644 --- a/games/mercuryio/mercuryio.c +++ b/games/mercuryio/mercuryio.c @@ -7,7 +7,8 @@ #include "mercuryio/mercuryio.h" #include "mercuryio/config.h" -#include "mercuryhook/elisabeth.h" +#include "mercuryhook/elizabeth.h" + #include "util/env.h" static unsigned int __stdcall mercury_io_touch_thread_proc(void *ctx); diff --git a/games/mercuryio/mercuryio.h b/games/mercuryio/mercuryio.h index 4d029ca..e869bea 100644 --- a/games/mercuryio/mercuryio.h +++ b/games/mercuryio/mercuryio.h @@ -5,7 +5,7 @@ #include #include -#include "mercuryhook/elisabeth.h" +#include "mercuryhook/elizabeth.h" enum { MERCURY_IO_OPBTN_TEST = 0x01, From c0f9b8f1ad539c8c2594202759433e7168d2db9b Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Thu, 25 Dec 2025 00:15:15 +0100 Subject: [PATCH 10/37] mu3: fix led data --- games/mu3io/leddata.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/games/mu3io/leddata.h b/games/mu3io/leddata.h index 84bc8b7..3ee82e2 100644 --- a/games/mu3io/leddata.h +++ b/games/mu3io/leddata.h @@ -20,4 +20,4 @@ struct _ongeki_led_data_buf_t { uint8_t data_len; // How many bytes to output from the buffer }; -static uint8_t ongeki_led_board_data_lens[LED_BOARDS_TOTAL] = {9*3, 6*3}; +static uint8_t ongeki_led_board_data_lens[LED_BOARDS_TOTAL] = {61*3, 6*3}; From 41d7ce111d95525bc169f5139d23328f1f48a188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=B0=E3=83=AD=E3=83=BC=E3=83=A9=E3=83=B3=E3=83=97?= <130208311+Gl0w1amp@users.noreply.github.com> Date: Thu, 25 Dec 2025 20:10:59 +0000 Subject: [PATCH 11/37] mai2io/led15070: add LED control APIs and PWM/fade handling (#89) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary - feat(io4): implement PWM/GPIO support for Billboard and Code Reader lights - feat(mai2io): expose LED control APIs to support new io4 features - fix(led15070): correct multi-LED color logic and add fade support ## Description **io4 & mai2io Updates** Introduced PWM control and GPIO write support in `io4`. These additions allow `mai2io` to properly drive the **Billboard LEDs** and **Code Reader / Player Camera lights**. The APIs have been exposed through `mai2hook` to facilitate scriptable control of these peripherals. **led15070 Improvements** Fixed an issue with the multi-LED color changing logic in the `led15070` driver. This ensures correct color output sequence and includes improvements for fade calculation. Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/89 Co-authored-by: グローランプ <130208311+Gl0w1amp@users.noreply.github.com> Co-committed-by: グローランプ <130208311+Gl0w1amp@users.noreply.github.com> --- common/board/io4.c | 10 ++- common/board/io4.h | 2 + common/board/led15070.c | 140 ++++++++++++++++++++++++++++-------- games/mai2hook/io4.c | 63 ++++++++++++++++ games/mai2hook/mai2-dll.c | 15 +++- games/mai2hook/mai2-dll.h | 1 + games/mai2hook/mai2hook.def | 1 + games/mai2io/mai2io.c | 10 ++- games/mai2io/mai2io.h | 11 ++- 9 files changed, 221 insertions(+), 32 deletions(-) diff --git a/common/board/io4.c b/common/board/io4.c index 8001457..d8a5c33 100644 --- a/common/board/io4.c +++ b/common/board/io4.c @@ -241,13 +241,21 @@ static HRESULT io4_handle_write(struct irp *irp) return S_OK; case IO4_CMD_SET_PWM_OUTPUT: - dprintf("USB I/O: PWM Out\n"); + // dprintf("USB I/O: PWM Out\n"); + + if (io4_ops->write_pwm != NULL) { + return io4_ops->write_pwm(out.payload, IO4_REPORT_OUT_PAYLOAD_LEN); + } return S_OK; case IO4_CMD_SET_UNIQUE_OUTPUT: // dprintf("USB I/O: Unique Out\n"); + if (io4_ops->write_unique != NULL) { + return io4_ops->write_unique(out.payload, IO4_REPORT_OUT_PAYLOAD_LEN); + } + return S_OK; case IO4_CMD_UPDATE_FIRMWARE: diff --git a/common/board/io4.h b/common/board/io4.h index 1f93cf1..db83219 100644 --- a/common/board/io4.h +++ b/common/board/io4.h @@ -30,6 +30,8 @@ struct io4_ops { HRESULT (*poll)(void* ctx, struct io4_state* state); HRESULT (*write_gpio)(uint8_t* payload, size_t len); + HRESULT (*write_pwm)(uint8_t* payload, size_t len); + HRESULT (*write_unique)(uint8_t* payload, size_t len); }; HRESULT io4_hook_init( diff --git a/common/board/led15070.c b/common/board/led15070.c index 43a015d..1cd9be5 100644 --- a/common/board/led15070.c +++ b/common/board/led15070.c @@ -71,6 +71,7 @@ static uint16_t led15070_fw_sum; static uint8_t led15070_host_adr = 0x01; #define led15070_nboards 2 +#define led15070_nleds 32 typedef struct { CRITICAL_SECTION lock; @@ -79,8 +80,10 @@ typedef struct { struct uart boarduart; uint8_t written_bytes[520]; uint8_t readable_bytes[520]; - uint8_t gs[32][4]; - uint8_t dc[32][3]; + uint8_t gs[led15070_nleds][4]; + uint8_t gs_fade[led15070_nleds][4]; + bool gs_fade_pending[led15070_nleds]; + uint8_t dc[led15070_nleds][3]; uint8_t fet[3]; uint8_t gs_palette[8][3]; wchar_t eeprom_path[MAX_PATH]; @@ -150,6 +153,8 @@ HRESULT led15070_hook_init( v->boarduart.readable.nbytes = sizeof(v->readable_bytes); memset(v->gs, 0, sizeof(v->gs)); + memset(v->gs_fade, 0, sizeof(v->gs_fade)); + memset(v->gs_fade_pending, 0, sizeof(v->gs_fade_pending)); memset(v->dc, 0, sizeof(v->dc)); memset(v->fet, 0, sizeof(v->fet)); memset(v->gs_palette, 0, sizeof(v->gs_palette)); @@ -454,6 +459,8 @@ static HRESULT led15070_req_set_normal_8bit(int board, const struct led15070_req led15070_per_board_vars[board].gs[idx][0] = req->payload[1]; // R led15070_per_board_vars[board].gs[idx][1] = req->payload[2]; // G led15070_per_board_vars[board].gs[idx][2] = req->payload[3]; // B + led15070_per_board_vars[board].gs[idx][3] = 0; + led15070_per_board_vars[board].gs_fade_pending[idx] = false; if (!led15070_per_board_vars[board].enable_response) return S_OK; @@ -473,31 +480,65 @@ static HRESULT led15070_req_set_normal_8bit(int board, const struct led15070_req return led15070_frame_encode(&led15070_per_board_vars[board].boarduart.readable, &resp, sizeof(resp.hdr) + resp.hdr.nbytes); } +static void led15070_calc_range( + uint8_t start, + uint8_t count, + uint8_t skip, + uint8_t *out_start, + uint8_t *out_end) +{ + uint16_t s = start; + uint16_t c = count; + + if (c == 0) { + *out_start = 0; + *out_end = 0; + return; + } + + if (c >= led15070_nleds) { + c = led15070_nleds; + } + + if (skip > 0 && skip <= c) { + s += skip; + c -= skip; + } + + if (s >= led15070_nleds || c == 0) { + *out_start = led15070_nleds; + *out_end = led15070_nleds; + return; + } + + *out_start = (uint8_t) s; + *out_end = (s + c > led15070_nleds) ? led15070_nleds : (uint8_t) (s + c); +} + static HRESULT led15070_req_set_multi_flash_8bit(int board, const struct led15070_req_any *req) { uint8_t idx_start = req->payload[0]; - uint8_t idx_end = req->payload[1]; + uint8_t idx_count = req->payload[1]; uint8_t idx_skip = req->payload[2]; + uint8_t start; + uint8_t end; // TODO: useful? #if defined(LOG_LED15070) - dprintf("LED 15070: Set LED - Multi flash 8bit (board %u, start %u, end %u, skip %u)\n", - board, idx_start, idx_end, idx_skip); + dprintf("LED 15070: Set LED - Multi flash 8bit (board %u, start %u, count %u, skip %u)\n", + board, idx_start, idx_count, idx_skip); #endif - if (idx_skip > 0 && idx_skip <= (idx_end - idx_start + 1)) { - idx_start += idx_skip; - } + led15070_calc_range(idx_start, idx_count, idx_skip, &start, &end); - int i = idx_start; - do { + for (int i = start; i < end; i++) { led15070_per_board_vars[board].gs[i][0] = req->payload[3]; // R led15070_per_board_vars[board].gs[i][1] = req->payload[4]; // G led15070_per_board_vars[board].gs[i][2] = req->payload[5]; // B /* Always 0, tells the controller to immediately change to this color */ led15070_per_board_vars[board].gs[i][3] = req->payload[6]; // Speed - i++; - } while (i < idx_end); + led15070_per_board_vars[board].gs_fade_pending[i] = false; + } if (!led15070_per_board_vars[board].enable_response) return S_OK; @@ -520,25 +561,24 @@ static HRESULT led15070_req_set_multi_flash_8bit(int board, const struct led1507 static HRESULT led15070_req_set_multi_fade_8bit(int board, const struct led15070_req_any *req) { uint8_t idx_start = req->payload[0]; - uint8_t idx_end = req->payload[1]; + uint8_t idx_count = req->payload[1]; uint8_t idx_skip = req->payload[2]; + uint8_t start; + uint8_t end; #if defined(LOG_LED15070) - dprintf("LED 15070: Set LED - Multi fade 8bit (board %u, start %u, end %u, skip %u)\n", - board, idx_start, idx_end, idx_skip); + dprintf("LED 15070: Set LED - Multi fade 8bit (board %u, start %u, count %u, skip %u)\n", + board, idx_start, idx_count, idx_skip); #endif - if (idx_skip > 0 && idx_skip <= (idx_end - idx_start + 1)) { - idx_start += idx_skip; - } + led15070_calc_range(idx_start, idx_count, idx_skip, &start, &end); - int i = idx_start; - do { - led15070_per_board_vars[board].gs[i][0] = req->payload[3]; // R - led15070_per_board_vars[board].gs[i][1] = req->payload[4]; // G - led15070_per_board_vars[board].gs[i][2] = req->payload[5]; // B - led15070_per_board_vars[board].gs[i][3] = req->payload[6]; // Speed - i++; - } while (i < idx_end); + for (int i = start; i < end; i++) { + led15070_per_board_vars[board].gs_fade[i][0] = req->payload[3]; // R + led15070_per_board_vars[board].gs_fade[i][1] = req->payload[4]; // G + led15070_per_board_vars[board].gs_fade[i][2] = req->payload[5]; // B + led15070_per_board_vars[board].gs_fade[i][3] = req->payload[6]; // Speed + led15070_per_board_vars[board].gs_fade_pending[i] = true; + } if (!led15070_per_board_vars[board].enable_response) return S_OK; @@ -767,12 +807,56 @@ static HRESULT led15070_req_dc_update(int board, const struct led15070_req_any * static HRESULT led15070_req_gs_update(int board, const struct led15070_req_any *req) { + _led15070_per_board_vars *v = &led15070_per_board_vars[board]; #if defined(LOG_LED15070) dprintf("LED 15070: GS update (board %u)\n", board); #endif - if (led_gs_update) - led_gs_update(board, (const uint8_t*)led15070_per_board_vars[board].gs); + if (led_gs_update) { + bool has_fade = false; + uint8_t payload[led15070_nleds][4]; + + for (int i = 0; i < led15070_nleds; i++) { + if (v->gs_fade_pending[i]) { + has_fade = true; + break; + } + } + + if (has_fade) { + for (int i = 0; i < led15070_nleds; i++) { + payload[i][0] = v->gs[i][0]; + payload[i][1] = v->gs[i][1]; + payload[i][2] = v->gs[i][2]; + payload[i][3] = 0; + } + + led_gs_update(board, (const uint8_t*)payload); + + for (int i = 0; i < led15070_nleds; i++) { + if (v->gs_fade_pending[i]) { + payload[i][0] = v->gs_fade[i][0]; + payload[i][1] = v->gs_fade[i][1]; + payload[i][2] = v->gs_fade[i][2]; + payload[i][3] = v->gs_fade[i][3]; + v->gs_fade_pending[i] = false; + } else { + payload[i][0] = v->gs[i][0]; + payload[i][1] = v->gs[i][1]; + payload[i][2] = v->gs[i][2]; + payload[i][3] = v->gs[i][3]; + } + } + + led_gs_update(board, (const uint8_t*)payload); + } else { + led_gs_update(board, (const uint8_t*)v->gs); + } + } else { + for (int i = 0; i < led15070_nleds; i++) { + v->gs_fade_pending[i] = false; + } + } if (!led15070_per_board_vars[board].enable_response) return S_OK; diff --git a/games/mai2hook/io4.c b/games/mai2hook/io4.c index cb0b38d..a1903d2 100644 --- a/games/mai2hook/io4.c +++ b/games/mai2hook/io4.c @@ -11,11 +11,17 @@ #include "util/dprintf.h" static HRESULT mai2_io4_poll(void* ctx, struct io4_state* state); +static HRESULT mai2_io4_write_gpio(uint8_t* payload, size_t len); +static HRESULT mai2_io4_write_pwm(uint8_t* payload, size_t len); +static HRESULT mai2_io4_write_unique(uint8_t* payload, size_t len); static uint16_t coins; static const struct io4_ops mai2_io4_ops = { .poll = mai2_io4_poll, + .write_gpio = mai2_io4_write_gpio, + .write_pwm = mai2_io4_write_pwm, + .write_unique = mai2_io4_write_unique, }; HRESULT mai2_io4_hook_init(const struct io4_config* cfg) { @@ -148,5 +154,62 @@ static HRESULT mai2_io4_poll(void* ctx, struct io4_state* state) { state->buttons[1] |= 1 << 4; } + return S_OK; +} + +static HRESULT mai2_io4_write_gpio(uint8_t* payload, size_t len) { + if (len >= 4) { + char bin0[9]; + char bin1[9]; + for (int i = 0; i < 8; i++) { + bin0[7 - i] = (payload[0] & (1 << i)) ? '1' : '0'; + bin1[7 - i] = (payload[1] & (1 << i)) ? '1' : '0'; + } + bin0[8] = '\0'; + bin1[8] = '\0'; + +#if defined(LOG_IO4) + dprintf("IO4 LED: [%02X %02X %02X %02X] (Byte0: %s, Byte1: %s)\n", + payload[0], payload[1], payload[2], payload[3], bin0, bin1); + + dprintf(" 1P Code Reader: %s\n", (payload[0] & 0x20) ? "ON" : "OFF"); + dprintf(" 2P Code Reader: %s\n", (payload[0] & 0x04) ? "ON" : "OFF"); + dprintf(" Camera Ring: %s\n", (payload[1] & 0x80) ? "ON" : "OFF"); + dprintf(" Camera Rec: %s\n", (payload[1] & 0x40) ? "ON" : "OFF"); +#endif + } + return S_OK; +} + +static HRESULT mai2_io4_write_pwm(uint8_t* payload, size_t len) { + if (len >= 16) { +#if defined(LOG_IO4) + dprintf("IO4 PWM: %02X %02X %02X %02X %02X %02X %02X %02X ...\n", + payload[0], payload[1], payload[2], payload[3], + payload[4], payload[5], payload[6], payload[7]); +#endif + } + return S_OK; +} + +static HRESULT mai2_io4_write_unique(uint8_t* payload, size_t len) { + if (len < 8) { + return S_OK; + } + +#if defined(LOG_IO4) + dprintf("IO4 Unique: %02X %02X %02X %02X %02X %02X %02X %02X ...\n", + payload[0], payload[1], payload[2], payload[3], + payload[4], payload[5], payload[6], payload[7]); +#endif + + if (mai2_dll.led_set_leds) { + uint8_t rgb_1p[3] = { payload[2], payload[4], payload[6] }; + uint8_t rgb_2p[3] = { payload[3], payload[5], payload[7] }; + + mai2_dll.led_set_leds(0, rgb_1p); + mai2_dll.led_set_leds(1, rgb_2p); + } + return S_OK; } \ No newline at end of file diff --git a/games/mai2hook/mai2-dll.c b/games/mai2hook/mai2-dll.c index 121750e..9fa591d 100644 --- a/games/mai2hook/mai2-dll.c +++ b/games/mai2hook/mai2-dll.c @@ -8,6 +8,11 @@ #include "util/dll-bind.h" #include "util/dprintf.h" +enum { + MAI2_DLL_SYM_COUNT_V101 = 11, + MAI2_DLL_SYM_COUNT_V102 = 12, +}; + const struct dll_bind_sym mai2_dll_syms[] = { { .sym = "mai2_io_init", @@ -42,6 +47,9 @@ const struct dll_bind_sym mai2_dll_syms[] = { }, { .sym = "mai2_io_led_gs_update", .off = offsetof(struct mai2_dll, led_gs_update), + }, { + .sym = "mai2_io_led_billboard_set", + .off = offsetof(struct mai2_dll, led_set_leds), }, }; @@ -60,6 +68,7 @@ HRESULT mai2_dll_init(const struct mai2_dll_config *cfg, HINSTANCE self) HINSTANCE owned; HINSTANCE src; HRESULT hr; + size_t sym_count; assert(cfg != NULL); assert(self != NULL); @@ -104,7 +113,11 @@ HRESULT mai2_dll_init(const struct mai2_dll_config *cfg, HINSTANCE self) } sym = mai2_dll_syms; - hr = dll_bind(&mai2_dll, src, &sym, _countof(mai2_dll_syms)); + sym_count = (mai2_dll.api_version < 0x0102) + ? MAI2_DLL_SYM_COUNT_V101 + : MAI2_DLL_SYM_COUNT_V102; + + hr = dll_bind(&mai2_dll, src, &sym, sym_count); if (FAILED(hr)) { if (src != self) { diff --git a/games/mai2hook/mai2-dll.h b/games/mai2hook/mai2-dll.h index 599d44a..ac456e8 100644 --- a/games/mai2hook/mai2-dll.h +++ b/games/mai2hook/mai2-dll.h @@ -17,6 +17,7 @@ struct mai2_dll { void (*led_set_fet_output)(uint8_t board, const uint8_t *rgb); void (*led_dc_update)(uint8_t board, const uint8_t *rgb); void (*led_gs_update)(uint8_t board, const uint8_t *rgb); + void (*led_set_leds)(uint8_t board, const uint8_t *rgb); }; struct mai2_dll_config { diff --git a/games/mai2hook/mai2hook.def b/games/mai2hook/mai2hook.def index 50e4058..a3b3df8 100644 --- a/games/mai2hook/mai2hook.def +++ b/games/mai2hook/mai2hook.def @@ -23,3 +23,4 @@ EXPORTS mai2_io_led_set_fet_output mai2_io_led_dc_update mai2_io_led_gs_update + mai2_io_led_billboard_set diff --git a/games/mai2io/mai2io.c b/games/mai2io/mai2io.c index 57404cb..4864887 100644 --- a/games/mai2io/mai2io.c +++ b/games/mai2io/mai2io.c @@ -19,7 +19,7 @@ static bool mai2_io_touch_1p_stop_flag; static HANDLE mai2_io_touch_2p_thread; static bool mai2_io_touch_2p_stop_flag; -uint16_t mai2_io_get_api_version(void) { return 0x0101; } +uint16_t mai2_io_get_api_version(void) { return 0x0102; } HRESULT mai2_io_init(void) { mai2_io_config_load(&mai2_io_cfg, get_config_path()); @@ -265,3 +265,11 @@ void mai2_io_led_gs_update(uint8_t board, const uint8_t *rgb) { #endif return; } + +void mai2_io_led_billboard_set(uint8_t board, const uint8_t *rgb) { +#if 0 + uint8_t player = board + 1; + dprintf("Mai2 LED %dP: Billboard set R:%02X G:%02X B:%02X\n", player, rgb[0], rgb[1], rgb[2]); +#endif + return; +} diff --git a/games/mai2io/mai2io.h b/games/mai2io/mai2io.h index 9c0a7f0..4519a01 100644 --- a/games/mai2io/mai2io.h +++ b/games/mai2io/mai2io.h @@ -28,7 +28,7 @@ enum { 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. */ + The latest API version as of this writing is 0x0102. */ uint16_t mai2_io_get_api_version(void); @@ -190,3 +190,12 @@ void mai2_io_led_dc_update(uint8_t board, const uint8_t *rgb); Minimum API version: 0x0101 */ void mai2_io_led_gs_update(uint8_t board, const uint8_t *rgb); + +/* Update the Billboard LEDs. rgb is a pointer to an array. + + maimai DX uses two boards. Board 0 is for the player 1 side (left) and board 1 + is for the player 2 side (right). + + Minimum API version: 0x0102 */ + +void mai2_io_led_billboard_set(uint8_t board, const uint8_t *rgb); From ff28d4ff09865b883cb65c3b8544a8d280080a68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=B0=E3=83=AD=E3=83=BC=E3=83=A9=E3=83=B3=E3=83=97?= <130208311+Gl0w1amp@users.noreply.github.com> Date: Sun, 4 Jan 2026 08:41:56 +0000 Subject: [PATCH 12/37] mai2io: add camera LED control functions (#91) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a follow-up to the previous PR. I realized I omitted the camera LED control interface, so I'm adding it now to complete the v1.02 API set. **Changes:** * Added `mai2_io_led_cam_set` to the 0x0102 API. * Wired IO4 GPIO outputs to forward code reader/player camera light states. * Updated `mai2hook.def` to export the new symbol. * Updated `MAI2_DLL_SYM_COUNT_V102` to reflect the additional symbol count. **Versioning Note:** Since the previous PR was just merged and v1.02 hasn't been officially released yet, I've opted to include this in the existing **0x0102** version rather than bumping to 0x0103. Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/91 Co-authored-by: グローランプ <130208311+Gl0w1amp@users.noreply.github.com> Co-committed-by: グローランプ <130208311+Gl0w1amp@users.noreply.github.com> --- games/mai2hook/io4.c | 24 +++++++++++++++++++++++- games/mai2hook/mai2-dll.c | 5 ++++- games/mai2hook/mai2-dll.h | 1 + games/mai2hook/mai2hook.def | 1 + games/mai2io/mai2io.c | 11 +++++++++++ games/mai2io/mai2io.h | 14 ++++++++++++++ 6 files changed, 54 insertions(+), 2 deletions(-) diff --git a/games/mai2hook/io4.c b/games/mai2hook/io4.c index a1903d2..f0f9a67 100644 --- a/games/mai2hook/io4.c +++ b/games/mai2hook/io4.c @@ -177,6 +177,28 @@ static HRESULT mai2_io4_write_gpio(uint8_t* payload, size_t len) { dprintf(" Camera Ring: %s\n", (payload[1] & 0x80) ? "ON" : "OFF"); dprintf(" Camera Rec: %s\n", (payload[1] & 0x40) ? "ON" : "OFF"); #endif + + if (mai2_dll.led_cam_set != NULL) { + uint8_t state = 0; + + if (payload[0] & 0x20) { + state |= MAI2_IO_LED_CAM_CODE_READER_1P; + } + + if (payload[0] & 0x04) { + state |= MAI2_IO_LED_CAM_CODE_READER_2P; + } + + if (payload[1] & 0x80) { + state |= MAI2_IO_LED_CAM_RING; + } + + if (payload[1] & 0x40) { + state |= MAI2_IO_LED_CAM_REC; + } + + mai2_dll.led_cam_set(state); + } } return S_OK; } @@ -212,4 +234,4 @@ static HRESULT mai2_io4_write_unique(uint8_t* payload, size_t len) { } return S_OK; -} \ No newline at end of file +} diff --git a/games/mai2hook/mai2-dll.c b/games/mai2hook/mai2-dll.c index 9fa591d..c414340 100644 --- a/games/mai2hook/mai2-dll.c +++ b/games/mai2hook/mai2-dll.c @@ -10,7 +10,7 @@ enum { MAI2_DLL_SYM_COUNT_V101 = 11, - MAI2_DLL_SYM_COUNT_V102 = 12, + MAI2_DLL_SYM_COUNT_V102 = 13, }; const struct dll_bind_sym mai2_dll_syms[] = { @@ -50,6 +50,9 @@ const struct dll_bind_sym mai2_dll_syms[] = { }, { .sym = "mai2_io_led_billboard_set", .off = offsetof(struct mai2_dll, led_set_leds), + }, { + .sym = "mai2_io_led_cam_set", + .off = offsetof(struct mai2_dll, led_cam_set), }, }; diff --git a/games/mai2hook/mai2-dll.h b/games/mai2hook/mai2-dll.h index ac456e8..bbaf69a 100644 --- a/games/mai2hook/mai2-dll.h +++ b/games/mai2hook/mai2-dll.h @@ -18,6 +18,7 @@ struct mai2_dll { void (*led_dc_update)(uint8_t board, const uint8_t *rgb); void (*led_gs_update)(uint8_t board, const uint8_t *rgb); void (*led_set_leds)(uint8_t board, const uint8_t *rgb); + void (*led_cam_set)(uint8_t state); }; struct mai2_dll_config { diff --git a/games/mai2hook/mai2hook.def b/games/mai2hook/mai2hook.def index a3b3df8..4b32c24 100644 --- a/games/mai2hook/mai2hook.def +++ b/games/mai2hook/mai2hook.def @@ -24,3 +24,4 @@ EXPORTS mai2_io_led_dc_update mai2_io_led_gs_update mai2_io_led_billboard_set + mai2_io_led_cam_set diff --git a/games/mai2io/mai2io.c b/games/mai2io/mai2io.c index 4864887..6a15bdf 100644 --- a/games/mai2io/mai2io.c +++ b/games/mai2io/mai2io.c @@ -273,3 +273,14 @@ void mai2_io_led_billboard_set(uint8_t board, const uint8_t *rgb) { #endif return; } + +void mai2_io_led_cam_set(uint8_t state) { +#if 0 + dprintf("Mai2 LED cam: CodeReader1P=%s CodeReader2P=%s Ring=%s Rec=%s\n", + (state & MAI2_IO_LED_CAM_CODE_READER_1P) ? "ON" : "OFF", + (state & MAI2_IO_LED_CAM_CODE_READER_2P) ? "ON" : "OFF", + (state & MAI2_IO_LED_CAM_RING) ? "ON" : "OFF", + (state & MAI2_IO_LED_CAM_REC) ? "ON" : "OFF"); +#endif + return; +} diff --git a/games/mai2io/mai2io.h b/games/mai2io/mai2io.h index 4519a01..297f35c 100644 --- a/games/mai2io/mai2io.h +++ b/games/mai2io/mai2io.h @@ -199,3 +199,17 @@ void mai2_io_led_gs_update(uint8_t board, const uint8_t *rgb); Minimum API version: 0x0102 */ void mai2_io_led_billboard_set(uint8_t board, const uint8_t *rgb); + +enum { + MAI2_IO_LED_CAM_CODE_READER_1P = 0x01, + MAI2_IO_LED_CAM_CODE_READER_2P = 0x02, + MAI2_IO_LED_CAM_RING = 0x04, + MAI2_IO_LED_CAM_REC = 0x08, +}; + +/* Update the Code Reader and Player Camera lights. State is a bitmask of + MAI2_IO_LED_CAM_* values. + + Minimum API version: 0x0102 */ + +void mai2_io_led_cam_set(uint8_t state); From bf8f3dd6f92fdef9f9bfa0c7912d2a4cd46ba89e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=B0=E3=83=AD=E3=83=BC=E3=83=A9=E3=83=B3=E3=83=97?= <130208311+Gl0w1amp@users.noreply.github.com> Date: Sun, 4 Jan 2026 08:43:52 +0000 Subject: [PATCH 13/37] aimeio-vfd: Add VFD text forwarding and state callbacks to AIME IO (#90) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary - feat(aimeio): add VFD text forwarding API and wire through DLL/VFD backend - feat(aimeio): add VFD state callback and export symbols across hooks - refactor(aimeio): rename VFD callbacks and align VFD parsing - feat(aimeio): extend VFD state fields for richer reporting ## Description This PR expands the AIME IO VFD interface by adding text forwarding and a VFD state callback, then wires them through the AIME DLL and VFD backend. It also renames VFD callbacks to align parsing behavior, updates all hook exports accordingly, and extends VFD state fields to carry more detailed state data. Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/90 Co-authored-by: グローランプ <130208311+Gl0w1amp@users.noreply.github.com> Co-committed-by: グローランプ <130208311+Gl0w1amp@users.noreply.github.com> --- common/aimeio/aimeio.c | 17 ++- common/aimeio/aimeio.h | 46 ++++++- common/board/aime-dll.c | 19 ++- common/board/aime-dll.h | 5 + common/board/vfd-cmd.h | 1 + common/board/vfd.c | 194 +++++++++++++++++++++++------- games/apm3hook/apm3hook.def | 2 + games/carolhook/carolhook.def | 2 + games/chunihook/chunihook.def | 2 + games/chusanhook/chusanhook.def | 2 + games/cmhook/cmhook.def | 2 + games/cxbhook/cxbhook.def | 2 + games/divahook/divahook.def | 2 + games/fgohook/fgohook.def | 2 + games/idachook/idachook.def | 2 + games/idzhook/idzhook.def | 2 + games/kemonohook/kemonohook.def | 2 + games/mai2hook/mai2hook.def | 2 + games/mercuryhook/mercuryhook.def | 2 + games/mu3hook/mu3hook.def | 2 + games/swdchook/swdchook.def | 2 + games/tokyohook/tokyohook.def | 2 + 22 files changed, 269 insertions(+), 45 deletions(-) diff --git a/common/aimeio/aimeio.c b/common/aimeio/aimeio.c index 8192bae..7efffb5 100644 --- a/common/aimeio/aimeio.c +++ b/common/aimeio/aimeio.c @@ -218,7 +218,7 @@ static HRESULT aime_io_generate_aime( uint16_t aime_io_get_api_version(void) { - return 0x0100; + return 0x0101; } HRESULT aime_io_init(void) @@ -351,3 +351,18 @@ HRESULT aime_io_nfc_get_felica_id(uint8_t unit_no, uint64_t *IDm) void aime_io_led_set_color(uint8_t unit_no, uint8_t r, uint8_t g, uint8_t b) {} + +void aime_io_vfd_set_text( + const uint8_t *text, + size_t text_len, + const struct aime_io_vfd_state *state) +{ + (void) text; + (void) text_len; + (void) state; +} + +void aime_io_vfd_set_state(const struct aime_io_vfd_state *state) +{ + (void) state; +} diff --git a/common/aimeio/aimeio.h b/common/aimeio/aimeio.h index 221f8e0..bb851cd 100644 --- a/common/aimeio/aimeio.h +++ b/common/aimeio/aimeio.h @@ -11,7 +11,7 @@ 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. + The latest API version as of this writing is 0x0101. */ uint16_t aime_io_get_api_version(void); @@ -85,3 +85,47 @@ HRESULT aime_io_nfc_get_felica_id(uint8_t unit_no, uint64_t *IDm); Minimum API version: 0x0100 */ void aime_io_led_set_color(uint8_t unit_no, uint8_t r, uint8_t g, uint8_t b); + +/* + VFD text forwarding. This is intended to pass through the text payload + plus the most recent VFD state so external handlers can emulate scrolling + or layout if desired. + + - text: Pointer to raw text bytes (not null-terminated) + - text_len: Length of the text buffer + - state: Current VFD state at the time of rendering + + The encoding field uses VFD encoding values (0=GB2312, 1=Big5, + 2=Shift-JIS, 3=KSC5601). + + Minimum API version: 0x0101 +*/ +struct aime_io_vfd_state { + uint8_t encoding; + uint8_t text_speed; + uint8_t scroll_enabled; + uint16_t h_scroll; + uint16_t cursor_x; + uint8_t cursor_y; + uint16_t wnd_x0; + uint8_t wnd_y0; + uint16_t wnd_x1; + uint8_t wnd_y1; + uint8_t rotate; + uint8_t brightness; + uint8_t screen_on; + uint32_t clear_seq; +}; + +void aime_io_vfd_set_text( + const uint8_t *text, + size_t text_len, + const struct aime_io_vfd_state *state); + +/* + VFD state change notification. Called when the VFD state changes even + when no text is written. + + Minimum API version: 0x0101 +*/ +void aime_io_vfd_set_state(const struct aime_io_vfd_state *state); diff --git a/common/board/aime-dll.c b/common/board/aime-dll.c index 465fcfd..7c00dfd 100644 --- a/common/board/aime-dll.c +++ b/common/board/aime-dll.c @@ -8,6 +8,11 @@ #include "util/dll-bind.h" #include "util/dprintf.h" +enum { + AIME_DLL_SYM_COUNT_V100 = 5, + AIME_DLL_SYM_COUNT_V101 = 7, +}; + const struct dll_bind_sym aime_dll_syms[] = { { .sym = "aime_io_init", @@ -24,7 +29,13 @@ const struct dll_bind_sym aime_dll_syms[] = { }, { .sym = "aime_io_led_set_color", .off = offsetof(struct aime_dll, led_set_color), - } + }, { + .sym = "aime_io_vfd_set_text", + .off = offsetof(struct aime_dll, vfd_set_text), + }, { + .sym = "aime_io_vfd_set_state", + .off = offsetof(struct aime_dll, vfd_set_state), + }, }; struct aime_dll aime_dll; @@ -42,6 +53,7 @@ HRESULT aime_dll_init(const struct aime_dll_config *cfg, HINSTANCE self) HINSTANCE owned; HINSTANCE src; HRESULT hr; + size_t sym_count; assert(cfg != NULL); assert(self != NULL); @@ -86,7 +98,10 @@ HRESULT aime_dll_init(const struct aime_dll_config *cfg, HINSTANCE self) } sym = aime_dll_syms; - hr = dll_bind(&aime_dll, src, &sym, _countof(aime_dll_syms)); + sym_count = (aime_dll.api_version < 0x0101) + ? AIME_DLL_SYM_COUNT_V100 + : AIME_DLL_SYM_COUNT_V101; + hr = dll_bind(&aime_dll, src, &sym, sym_count); if (FAILED(hr)) { if (src != self) { diff --git a/common/board/aime-dll.h b/common/board/aime-dll.h index 25c52f0..7113cc1 100644 --- a/common/board/aime-dll.h +++ b/common/board/aime-dll.h @@ -15,6 +15,11 @@ struct aime_dll { size_t luid_size); HRESULT (*nfc_get_felica_id)(uint8_t unit_no, uint64_t *IDm); void (*led_set_color)(uint8_t unit_no, uint8_t r, uint8_t g, uint8_t b); + void (*vfd_set_text)( + const uint8_t *text, + size_t text_len, + const struct aime_io_vfd_state *state); + void (*vfd_set_state)(const struct aime_io_vfd_state *state); }; struct aime_dll_config { diff --git a/common/board/vfd-cmd.h b/common/board/vfd-cmd.h index 28bcb16..8c8cd81 100644 --- a/common/board/vfd-cmd.h +++ b/common/board/vfd-cmd.h @@ -3,6 +3,7 @@ #include "board/vfd-frame.h" enum { + VFD_CMD_WRITE_STATIC = 0x00, VFD_CMD_GET_VERSION = 0x5B, VFD_CMD_RESET = 0x0B, VFD_CMD_CLEAR_SCREEN = 0x0C, diff --git a/common/board/vfd.c b/common/board/vfd.c index 4cfb20b..3937a26 100644 --- a/common/board/vfd.c +++ b/common/board/vfd.c @@ -11,6 +11,7 @@ #include #include "board/config.h" +#include "board/aime-dll.h" #include "board/vfd.h" #include "board/vfd-cmd.h" @@ -22,6 +23,7 @@ #include "util/dump.h" #define SUPER_VERBOSE 0 +#define VFD_BRIGHTNESS_MAX 4 static HRESULT vfd_handle_irp(struct irp *irp); @@ -29,7 +31,7 @@ static struct uart vfd_uart; static uint8_t vfd_written[4096]; static uint8_t vfd_readable[4096]; -static int encoding = VFD_ENC_SHIFT_JIS; +static struct aime_io_vfd_state vfd_state; HRESULT vfd_handle_get_version(struct const_iobuf* reader, struct iobuf* writer, struct uart* vfd_uart); HRESULT vfd_handle_reset(struct const_iobuf* reader, struct iobuf* writer, struct uart* vfd_uart); @@ -51,6 +53,13 @@ HRESULT vfd_handle_create_char2(struct const_iobuf* reader, struct iobuf* writer static bool utf_enabled; +static void vfd_publish_state(void) +{ + if (aime_dll.vfd_set_state != NULL) { + aime_dll.vfd_set_state(&vfd_state); + } +} + HRESULT vfd_hook_init(struct vfd_config *cfg, unsigned int default_port_no) { if (!cfg->enable){ @@ -58,6 +67,9 @@ HRESULT vfd_hook_init(struct vfd_config *cfg, unsigned int default_port_no) } utf_enabled = cfg->utf_conversion; + memset(&vfd_state, 0, sizeof(vfd_state)); + vfd_state.encoding = VFD_ENC_SHIFT_JIS; + vfd_publish_state(); unsigned int port_no = cfg->port_no; if (port_no == 0){ @@ -93,13 +105,13 @@ void print_vfd_text(const char* str, int len){ memset(encoded, 0, 1024 * sizeof(wchar_t)); int codepage = 0; - if (encoding == VFD_ENC_GB2312){ + if (vfd_state.encoding == VFD_ENC_GB2312){ codepage = 936; - } else if (encoding == VFD_ENC_BIG5){ + } else if (vfd_state.encoding == VFD_ENC_BIG5){ codepage = 950; - } else if (encoding == VFD_ENC_SHIFT_JIS){ + } else if (vfd_state.encoding == VFD_ENC_SHIFT_JIS){ codepage = 932; - } else if (encoding == VFD_ENC_KSC5601) { + } else if (vfd_state.encoding == VFD_ENC_KSC5601) { codepage = 949; } @@ -114,6 +126,37 @@ void print_vfd_text(const char* str, int len){ dprintf("VFD: Text: %s\n", str); } + + if (aime_dll.vfd_set_text != NULL) { + aime_dll.vfd_set_text((const uint8_t *) str, len, &vfd_state); + } +} + +static void vfd_read_text_until_sync(struct const_iobuf *reader) +{ + int len; + + if (reader->pos >= reader->nbytes) { + return; + } + + len = 0; + while (reader->pos + len < reader->nbytes && + reader->bytes[reader->pos + len] != VFD_SYNC_BYTE && + reader->bytes[reader->pos + len] != VFD_SYNC_BYTE2) { + len++; + } + + if (len <= 0) { + return; + } + + char *str = malloc((size_t) len + 1); + memset(str, 0, (size_t) len + 1); + iobuf_read(reader, str, len); + str[len] = '\0'; + print_vfd_text(str, len); + free(str); } static HRESULT vfd_handle_irp(struct irp *irp) @@ -178,6 +221,10 @@ static HRESULT vfd_handle_irp(struct irp *irp) hr = vfd_handle_set_text_wnd(&reader, writer, &vfd_uart); } else if (cmd == VFD_CMD_SET_TEXT_SPEED) { hr = vfd_handle_set_text_speed(&reader, writer, &vfd_uart); + } else if (cmd == VFD_CMD_WRITE_STATIC) { + dprintf("VFD: Write Static Text\n"); + vfd_read_text_until_sync(&reader); + hr = S_FALSE; } else if (cmd == VFD_CMD_WRITE_TEXT) { hr = vfd_handle_write_text(&reader, writer, &vfd_uart); } else if (cmd == VFD_CMD_ENABLE_SCROLL) { @@ -199,22 +246,7 @@ static HRESULT vfd_handle_irp(struct irp *irp) // if no sync byte is sent, we are just getting plain text... - if (reader.pos < reader.nbytes){ - int len = 0; - - // read chars until we hit a new sync byte or the data ends - while (reader.pos + len + 1 < reader.nbytes && reader.bytes[reader.pos + len] != VFD_SYNC_BYTE && reader.bytes[reader.pos + len] != VFD_SYNC_BYTE2){ - len++; - } - - char* str = malloc(len); - memset(str, 0, len); - iobuf_read(&reader, str, len); - print_vfd_text(str, len); - free(str); - - reader.pos += len; - } + vfd_read_text_until_sync(&reader); } @@ -230,7 +262,16 @@ static HRESULT vfd_handle_irp(struct irp *irp) } HRESULT vfd_handle_get_version(struct const_iobuf* reader, struct iobuf* writer, struct uart* vfd_uart){ - dprintf("VFD: Get Version\n"); + uint8_t subcmd; + + if (reader->pos < reader->nbytes && + reader->bytes[reader->pos] != VFD_SYNC_BYTE && + reader->bytes[reader->pos] != VFD_SYNC_BYTE2) { + iobuf_read_8(reader, &subcmd); + dprintf("VFD: Get Version (0x%02x)\n", subcmd); + } else { + dprintf("VFD: Get Version\n"); + } struct vfd_resp_board_info resp; @@ -244,12 +285,39 @@ HRESULT vfd_handle_get_version(struct const_iobuf* reader, struct iobuf* writer, HRESULT vfd_handle_reset(struct const_iobuf* reader, struct iobuf* writer, struct uart* vfd_uart){ dprintf("VFD: Reset\n"); - encoding = VFD_ENC_SHIFT_JIS; + memset(&vfd_state, 0, sizeof(vfd_state)); + vfd_state.encoding = VFD_ENC_SHIFT_JIS; + vfd_publish_state(); return S_FALSE; } HRESULT vfd_handle_clear_screen(struct const_iobuf* reader, struct iobuf* writer, struct uart* vfd_uart){ + if (reader->pos + 1 <= reader->nbytes) { + uint8_t next = reader->bytes[reader->pos]; + bool next_is_sync = (next == VFD_SYNC_BYTE || next == VFD_SYNC_BYTE2); + + if (!next_is_sync && next <= VFD_BRIGHTNESS_MAX) { + bool end_or_sync = (reader->pos + 1 >= reader->nbytes); + + if (!end_or_sync) { + uint8_t follow = reader->bytes[reader->pos + 1]; + end_or_sync = (follow == VFD_SYNC_BYTE || follow == VFD_SYNC_BYTE2); + } + + if (end_or_sync) { + uint8_t b; + iobuf_read_8(reader, &b); + dprintf("VFD: Brightness (compat), %d\n", b); + vfd_state.brightness = b; + vfd_publish_state(); + return S_FALSE; + } + } + } + dprintf("VFD: Clear Screen\n"); + vfd_state.clear_seq++; + vfd_publish_state(); return S_FALSE; } @@ -257,12 +325,14 @@ HRESULT vfd_handle_set_brightness(struct const_iobuf* reader, struct iobuf* writ uint8_t b; iobuf_read_8(reader, &b); - if (b > 4){ + if (b > VFD_BRIGHTNESS_MAX){ dprintf("VFD: Brightness, invalid argument\n"); return E_FAIL; } dprintf("VFD: Brightness, %d\n", b); + vfd_state.brightness = b; + vfd_publish_state(); return S_FALSE; } @@ -276,30 +346,49 @@ HRESULT vfd_handle_set_screen_on(struct const_iobuf* reader, struct iobuf* write } dprintf("VFD: Screen Power, %d\n", b); + vfd_state.screen_on = b; + vfd_publish_state(); return S_FALSE; } HRESULT vfd_handle_set_h_scroll(struct const_iobuf* reader, struct iobuf* writer, struct uart* vfd_uart){ - uint8_t x; - iobuf_read_8(reader, &x); + uint16_t x; + iobuf_read_be16(reader, &x); dprintf("VFD: Horizontal Scroll, X=%d\n", x); + vfd_state.h_scroll = x; + vfd_publish_state(); return S_FALSE; } HRESULT vfd_handle_draw_image(struct const_iobuf* reader, struct iobuf* writer, struct uart* vfd_uart){ - int w, h; - uint16_t x0, x1; + uint16_t x0; + uint16_t w; + uint16_t h_lines; + uint16_t h_pixels; uint8_t y0, y1; - uint8_t image[2048]; + size_t payload; + size_t remaining; iobuf_read_be16(reader, &x0); iobuf_read_8(reader, &y0); - iobuf_read_be16(reader, &x1); + iobuf_read_be16(reader, &w); iobuf_read_8(reader, &y1); - w = x1 - x0; - h = y1 - y0; - iobuf_read(reader, image, w*h); - dprintf("VFD: Draw image, %dx%d\n", w, h); + if (y1 >= y0) { + h_lines = (uint16_t) (y1 - y0 + 1); + } else { + h_lines = 0; + } + + h_pixels = (uint16_t) (h_lines * 8); + dprintf("VFD: Draw image, %dx%d @%d,%d\n", w, h_pixels, x0, y0); + + payload = (size_t) w * (size_t) h_pixels; + remaining = reader->nbytes - reader->pos; + if (payload > remaining) { + payload = remaining; + } + + reader->pos += payload; return S_FALSE; } @@ -311,6 +400,9 @@ HRESULT vfd_handle_set_cursor(struct const_iobuf* reader, struct iobuf* writer, iobuf_read_8(reader, &y); dprintf("VFD: Set Cursor, x=%d,y=%d\n", x, y); + vfd_state.cursor_x = x; + vfd_state.cursor_y = y; + vfd_publish_state(); return S_FALSE; } @@ -326,20 +418,31 @@ HRESULT vfd_handle_set_encoding(struct const_iobuf* reader, struct iobuf* writer return E_FAIL; } - encoding = b; + vfd_state.encoding = b; + vfd_publish_state(); return S_FALSE; } HRESULT vfd_handle_set_text_wnd(struct const_iobuf* reader, struct iobuf* writer, struct uart* vfd_uart){ - uint16_t x0, x1; - uint8_t y0, y1; + uint16_t x0, w; + uint8_t y0, h; + uint16_t x1; + uint8_t y1; iobuf_read_be16(reader, &x0); iobuf_read_8(reader, &y0); - iobuf_read_be16(reader, &x1); - iobuf_read_8(reader, &y1); + iobuf_read_be16(reader, &w); + iobuf_read_8(reader, &h); - dprintf("VFD: Set Text Window, p0:%d,%d, p1:%d,%d\n", x0, y0, x1, y1); + x1 = (uint16_t) (x0 + w); + y1 = (uint8_t) (y0 + h); + + dprintf("VFD: Set Text Window, x=%d,y=%d,w=%d,h=%d\n", x0, y0, w, h); + vfd_state.wnd_x0 = x0; + vfd_state.wnd_y0 = y0; + vfd_state.wnd_x1 = x1; + vfd_state.wnd_y1 = y1; + vfd_publish_state(); return S_FALSE; } HRESULT vfd_handle_set_text_speed(struct const_iobuf* reader, struct iobuf* writer, struct uart* vfd_uart){ @@ -347,14 +450,17 @@ HRESULT vfd_handle_set_text_speed(struct const_iobuf* reader, struct iobuf* writ iobuf_read_8(reader, &b); dprintf("VFD: Set Text Speed, %d\n", b); + vfd_state.text_speed = b; + vfd_publish_state(); return S_FALSE; } HRESULT vfd_handle_write_text(struct const_iobuf* reader, struct iobuf* writer, struct uart* vfd_uart){ uint8_t len; iobuf_read_8(reader, &len); - char* str = malloc(len); + char* str = malloc((size_t) len + 1); iobuf_read(reader, str, len); + str[len] = '\0'; print_vfd_text(str, len); free(str); @@ -363,10 +469,14 @@ HRESULT vfd_handle_write_text(struct const_iobuf* reader, struct iobuf* writer, } HRESULT vfd_handle_enable_scroll(struct const_iobuf* reader, struct iobuf* writer, struct uart* vfd_uart){ dprintf("VFD: Enable Scrolling\n"); + vfd_state.scroll_enabled = 1; + vfd_publish_state(); return S_FALSE; } HRESULT vfd_handle_disable_scroll(struct const_iobuf* reader, struct iobuf* writer, struct uart* vfd_uart){ dprintf("VFD: Disable Scrolling\n"); + vfd_state.scroll_enabled = 0; + vfd_publish_state(); return S_FALSE; } HRESULT vfd_handle_rotate(struct const_iobuf* reader, struct iobuf* writer, struct uart* vfd_uart){ @@ -374,6 +484,8 @@ HRESULT vfd_handle_rotate(struct const_iobuf* reader, struct iobuf* writer, stru iobuf_read_8(reader, &b); dprintf("VFD: Rotate, %d\n", b); + vfd_state.rotate = b; + vfd_publish_state(); return S_FALSE; } HRESULT vfd_handle_create_char(struct const_iobuf* reader, struct iobuf* writer, struct uart* vfd_uart){ diff --git a/games/apm3hook/apm3hook.def b/games/apm3hook/apm3hook.def index ec636df..c965fb8 100644 --- a/games/apm3hook/apm3hook.def +++ b/games/apm3hook/apm3hook.def @@ -4,6 +4,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/carolhook/carolhook.def b/games/carolhook/carolhook.def index 5102937..1a8312d 100644 --- a/games/carolhook/carolhook.def +++ b/games/carolhook/carolhook.def @@ -4,6 +4,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/chunihook/chunihook.def b/games/chunihook/chunihook.def index 517a10b..4128451 100644 --- a/games/chunihook/chunihook.def +++ b/games/chunihook/chunihook.def @@ -5,6 +5,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/chusanhook/chusanhook.def b/games/chusanhook/chusanhook.def index ea786f5..8786d4e 100644 --- a/games/chusanhook/chusanhook.def +++ b/games/chusanhook/chusanhook.def @@ -5,6 +5,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/cmhook/cmhook.def b/games/cmhook/cmhook.def index ab7d3af..526442d 100644 --- a/games/cmhook/cmhook.def +++ b/games/cmhook/cmhook.def @@ -4,6 +4,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/cxbhook/cxbhook.def b/games/cxbhook/cxbhook.def index 9efaaf6..8466898 100644 --- a/games/cxbhook/cxbhook.def +++ b/games/cxbhook/cxbhook.def @@ -5,6 +5,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/divahook/divahook.def b/games/divahook/divahook.def index 4d2ae44..a92e61d 100644 --- a/games/divahook/divahook.def +++ b/games/divahook/divahook.def @@ -4,6 +4,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/fgohook/fgohook.def b/games/fgohook/fgohook.def index 3e77ccc..a2088b9 100644 --- a/games/fgohook/fgohook.def +++ b/games/fgohook/fgohook.def @@ -4,6 +4,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/idachook/idachook.def b/games/idachook/idachook.def index 4653c37..4ef2b98 100644 --- a/games/idachook/idachook.def +++ b/games/idachook/idachook.def @@ -4,6 +4,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/idzhook/idzhook.def b/games/idzhook/idzhook.def index 53e01c6..b13642b 100644 --- a/games/idzhook/idzhook.def +++ b/games/idzhook/idzhook.def @@ -9,6 +9,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/kemonohook/kemonohook.def b/games/kemonohook/kemonohook.def index 601a00b..4db5e98 100644 --- a/games/kemonohook/kemonohook.def +++ b/games/kemonohook/kemonohook.def @@ -4,6 +4,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/mai2hook/mai2hook.def b/games/mai2hook/mai2hook.def index 4b32c24..cf1ed65 100644 --- a/games/mai2hook/mai2hook.def +++ b/games/mai2hook/mai2hook.def @@ -4,6 +4,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/mercuryhook/mercuryhook.def b/games/mercuryhook/mercuryhook.def index 32f33c1..4d3c8ec 100644 --- a/games/mercuryhook/mercuryhook.def +++ b/games/mercuryhook/mercuryhook.def @@ -4,6 +4,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/mu3hook/mu3hook.def b/games/mu3hook/mu3hook.def index 0393a2e..ab34a0f 100644 --- a/games/mu3hook/mu3hook.def +++ b/games/mu3hook/mu3hook.def @@ -10,6 +10,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/swdchook/swdchook.def b/games/swdchook/swdchook.def index ad4f93e..09c40a9 100644 --- a/games/swdchook/swdchook.def +++ b/games/swdchook/swdchook.def @@ -4,6 +4,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll diff --git a/games/tokyohook/tokyohook.def b/games/tokyohook/tokyohook.def index d60bf17..8b30d5f 100644 --- a/games/tokyohook/tokyohook.def +++ b/games/tokyohook/tokyohook.def @@ -4,6 +4,8 @@ EXPORTS aime_io_get_api_version aime_io_init aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll From e2257a6fa74a7812e777e6e7d707d818636666b2 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Sun, 4 Jan 2026 10:06:23 +0100 Subject: [PATCH 14/37] misc: cleanup --- dist/mercury/launch.bat | 4 +++- dist/mercury/segatools.ini | 4 ++-- games/apm3hook/dllmain.c | 5 ----- games/apm3io/dllmain.c | 3 ++- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/dist/mercury/launch.bat b/dist/mercury/launch.bat index 4a64365..7445a6e 100644 --- a/dist/mercury/launch.bat +++ b/dist/mercury/launch.bat @@ -10,4 +10,6 @@ inject -d -k mercuryhook.dll ../WindowsNoEditor/Mercury/Binaries/Win64/Mercury-W taskkill /f /im amdaemon.exe > nul 2>&1 -echo Game processes have terminated \ No newline at end of file +echo. +echo Game processes have terminated +pause \ No newline at end of file diff --git a/dist/mercury/segatools.ini b/dist/mercury/segatools.ini index 0d5f68f..64e11d0 100644 --- a/dist/mercury/segatools.ini +++ b/dist/mercury/segatools.ini @@ -99,8 +99,8 @@ dpiAware=1 [touch] enable=1 -; Hooks related to the LED board (codenamed Elisabeth) -[elisabeth] +; Hooks related to the LED board (codenamed Elizabeth) +[elizabeth] enable=1 ; ----------------------------------------------------------------------------- diff --git a/games/apm3hook/dllmain.c b/games/apm3hook/dllmain.c index ab937e0..49f0c56 100644 --- a/games/apm3hook/dllmain.c +++ b/games/apm3hook/dllmain.c @@ -24,11 +24,6 @@ #include "hooklib/serial.h" #include "hooklib/spike.h" -#include "platform/clock.h" -#include "platform/config.h" -#include "platform/nusec.h" -#include "platform/security.h" - #include "unityhook/hook.h" #include "util/dprintf.h" diff --git a/games/apm3io/dllmain.c b/games/apm3io/dllmain.c index ee14dd2..bf4dff9 100644 --- a/games/apm3io/dllmain.c +++ b/games/apm3io/dllmain.c @@ -13,6 +13,7 @@ #include "util/dprintf.h" #include "util/str.h" +#include "util/env.h" static struct apm3_io_config apm3_io_cfg; static const struct apm3_io_backend *apm3_io_backend; @@ -39,7 +40,7 @@ HRESULT apm3_io_init(void) return hr; } - apm3_io_config_load(&apm3_io_cfg, L".\\segatools.ini"); + apm3_io_config_load(&apm3_io_cfg, get_config_path()); if (wstr_ieq(apm3_io_cfg.mode, L"keyboard")) { hr = apm3_kb_init(&apm3_io_cfg.kb, &apm3_io_backend); From 0a27ed2aae2e3cddd66ae33ae73de2c0a79394d6 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Sun, 4 Jan 2026 10:11:15 +0100 Subject: [PATCH 15/37] idz, swdc: add up/down/left/right DInput button config --- dist/idz/segatools.ini | 8 +++++++ dist/swdc/segatools.ini | 5 ++++ games/idacio/config.c | 4 ++-- games/idacio/config.h | 4 ++-- games/idacio/di.c | 50 ++++++++++++++++++++------------------- games/idzio/config.c | 4 ++++ games/idzio/config.h | 4 ++++ games/idzio/di.c | 52 +++++++++++++++++++++++++++++++++++++++++ games/swdcio/config.c | 4 ++++ games/swdcio/config.h | 4 ++++ games/swdcio/di.c | 52 +++++++++++++++++++++++++++++++++++++++++ 11 files changed, 163 insertions(+), 28 deletions(-) diff --git a/dist/idz/segatools.ini b/dist/idz/segatools.ini index 7e2035b..fc2ec69 100644 --- a/dist/idz/segatools.ini +++ b/dist/idz/segatools.ini @@ -230,6 +230,14 @@ accelAxis=Y ; numbered from 1; some software numbers buttons from 0. start=1 viewChg=2 +; DPad is already emulated, but in order to trigger "Time Up" and exit the +; course you need to press both left and right on the DPad at the same time. +; This is not possible on most devices, so we set the left and right button again. +left=7 +right=8 +; Additional mapping for up and down buttons in DPad. +up=4 +down=3 ; Button mappings for the simulated six-speed shifter. shiftDn=5 shiftUp=6 diff --git a/dist/swdc/segatools.ini b/dist/swdc/segatools.ini index 46d8dd8..6c93ae7 100644 --- a/dist/swdc/segatools.ini +++ b/dist/swdc/segatools.ini @@ -187,6 +187,11 @@ accelAxis=Y ; numbered from 1; some software numbers buttons from 0. start=1 viewChg=2 +; DPad is already emulated, but some wheels use buttons rather than a DPad. +up=4 +down=3 +left=7 +right=8 ; Button mappings for the steering wheel paddles. paddleLeft=6 paddleRight=5 diff --git a/games/idacio/config.c b/games/idacio/config.c index a325fa8..a5c1769 100644 --- a/games/idacio/config.c +++ b/games/idacio/config.c @@ -58,10 +58,10 @@ void idac_di_config_load(struct idac_di_config *cfg, const wchar_t *filename) cfg->start = GetPrivateProfileIntW(L"dinput", L"start", 0, filename); cfg->view_chg = GetPrivateProfileIntW(L"dinput", L"viewChg", 0, filename); - cfg->left = GetPrivateProfileIntW(L"dinput", L"left", 0, filename); - cfg->right = GetPrivateProfileIntW(L"dinput", L"right", 0, filename); cfg->up = GetPrivateProfileIntW(L"dinput", L"up", 0, filename); cfg->down = GetPrivateProfileIntW(L"dinput", L"down", 0, filename); + cfg->left = GetPrivateProfileIntW(L"dinput", L"left", 0, filename); + cfg->right = GetPrivateProfileIntW(L"dinput", L"right", 0, filename); cfg->shift_dn = GetPrivateProfileIntW(L"dinput", L"shiftDn", 0, filename); cfg->shift_up = GetPrivateProfileIntW(L"dinput", L"shiftUp", 0, filename); diff --git a/games/idacio/config.h b/games/idacio/config.h index df70404..5b7e736 100644 --- a/games/idacio/config.h +++ b/games/idacio/config.h @@ -16,10 +16,10 @@ struct idac_di_config { wchar_t accel_axis[16]; uint8_t start; uint8_t view_chg; - uint8_t left; - uint8_t right; uint8_t up; uint8_t down; + uint8_t left; + uint8_t right; uint8_t shift_dn; uint8_t shift_up; uint8_t gear[6]; diff --git a/games/idacio/di.c b/games/idacio/di.c index e676062..848abe1 100644 --- a/games/idacio/di.c +++ b/games/idacio/di.c @@ -69,10 +69,10 @@ static uint8_t idac_di_shift_dn; static uint8_t idac_di_shift_up; static uint8_t idac_di_view_chg; static uint8_t idac_di_start; -static uint8_t idac_di_left; -static uint8_t idac_di_right; static uint8_t idac_di_up; static uint8_t idac_di_down; +static uint8_t idac_di_left; +static uint8_t idac_di_right; static uint8_t idac_di_gear[6]; static bool idac_di_use_pedals; static bool idac_di_reverse_brake_axis; @@ -237,6 +237,18 @@ static HRESULT idac_di_config_apply(const struct idac_di_config *cfg) return E_INVALIDARG; } + if (cfg->up > 32) { + dprintf("Wheel: Invalid up button: %i\n", cfg->up); + + return E_INVALIDARG; + } + + if (cfg->down > 32) { + dprintf("Wheel: Invalid down button: %i\n", cfg->down); + + return E_INVALIDARG; + } + if (cfg->left > 32) { dprintf("Wheel: Invalid left button: %i\n", cfg->left); @@ -249,16 +261,6 @@ static HRESULT idac_di_config_apply(const struct idac_di_config *cfg) return E_INVALIDARG; } - if (cfg->up > 32) { - dprintf("Wheel: Invalid up button: %i\n", cfg->up); - return E_INVALIDARG; - } - - if (cfg->down > 32) { - dprintf("Wheel: Invalid down button: %i\n", cfg->down); - return E_INVALIDARG; - } - if (cfg->shift_dn > 32) { dprintf("Wheel: Invalid shift down button: %i\n", cfg->shift_dn); @@ -292,10 +294,10 @@ static HRESULT idac_di_config_apply(const struct idac_di_config *cfg) } dprintf("Wheel: Start button . . . : %i\n", cfg->start); dprintf("Wheel: View Change button : %i\n", cfg->view_chg); - dprintf("Wheel: Left button . . . . : %i\n", cfg->left); - dprintf("Wheel: Right button . . . : %i\n", cfg->right); dprintf("Wheel: Up button . . . . . : %i\n", cfg->up); dprintf("Wheel: Down button . . . : %i\n", cfg->down); + dprintf("Wheel: Left button . . . . : %i\n", cfg->left); + dprintf("Wheel: Right button . . . : %i\n", cfg->right); dprintf("Wheel: Shift Down button . : %i\n", cfg->shift_dn); dprintf("Wheel: Shift Up button . . : %i\n", cfg->shift_up); dprintf("Wheel: Reverse Brake Axis : %i\n", cfg->reverse_brake_axis); @@ -329,10 +331,10 @@ static HRESULT idac_di_config_apply(const struct idac_di_config *cfg) idac_di_off_accel = accel_axis->off; idac_di_start = cfg->start; idac_di_view_chg = cfg->view_chg; - idac_di_left = cfg->left; - idac_di_right = cfg->right; idac_di_up = cfg->up; idac_di_down = cfg->down; + idac_di_left = cfg->left; + idac_di_right = cfg->right; idac_di_shift_dn = cfg->shift_dn; idac_di_shift_up = cfg->shift_up; idac_di_reverse_brake_axis = cfg->reverse_brake_axis; @@ -488,14 +490,6 @@ static void idac_di_get_buttons(uint8_t *gamebtn_out) gamebtn |= IDAC_IO_GAMEBTN_VIEW_CHANGE; } - if (idac_di_left && state.st.rgbButtons[idac_di_left - 1]) { - gamebtn |= IDAC_IO_GAMEBTN_LEFT; - } - - if (idac_di_right && state.st.rgbButtons[idac_di_right - 1]) { - gamebtn |= IDAC_IO_GAMEBTN_RIGHT; - } - if (idac_di_up && state.st.rgbButtons[idac_di_up - 1]) { gamebtn |= IDAC_IO_GAMEBTN_UP; } @@ -504,6 +498,14 @@ static void idac_di_get_buttons(uint8_t *gamebtn_out) gamebtn |= IDAC_IO_GAMEBTN_DOWN; } + if (idac_di_left && state.st.rgbButtons[idac_di_left - 1]) { + gamebtn |= IDAC_IO_GAMEBTN_LEFT; + } + + if (idac_di_right && state.st.rgbButtons[idac_di_right - 1]) { + gamebtn |= IDAC_IO_GAMEBTN_RIGHT; + } + *gamebtn_out = gamebtn; } diff --git a/games/idzio/config.c b/games/idzio/config.c index 369a4c5..81b3a70 100644 --- a/games/idzio/config.c +++ b/games/idzio/config.c @@ -58,6 +58,10 @@ void idz_di_config_load(struct idz_di_config *cfg, const wchar_t *filename) cfg->start = GetPrivateProfileIntW(L"dinput", L"start", 0, filename); cfg->view_chg = GetPrivateProfileIntW(L"dinput", L"viewChg", 0, filename); + cfg->up = GetPrivateProfileIntW(L"dinput", L"up", 0, filename); + cfg->down = GetPrivateProfileIntW(L"dinput", L"down", 0, filename); + cfg->left = GetPrivateProfileIntW(L"dinput", L"left", 0, filename); + cfg->right = GetPrivateProfileIntW(L"dinput", L"right", 0, filename); cfg->shift_dn = GetPrivateProfileIntW(L"dinput", L"shiftDn", 0, filename); cfg->shift_up = GetPrivateProfileIntW(L"dinput", L"shiftUp", 0, filename); diff --git a/games/idzio/config.h b/games/idzio/config.h index 7a17f00..78b4b50 100644 --- a/games/idzio/config.h +++ b/games/idzio/config.h @@ -16,6 +16,10 @@ struct idz_di_config { wchar_t accel_axis[16]; uint8_t start; uint8_t view_chg; + uint8_t up; + uint8_t down; + uint8_t left; + uint8_t right; uint8_t shift_dn; uint8_t shift_up; uint8_t gear[6]; diff --git a/games/idzio/di.c b/games/idzio/di.c index 00968fa..39be873 100644 --- a/games/idzio/di.c +++ b/games/idzio/di.c @@ -76,6 +76,10 @@ static uint8_t idz_di_shift_dn; static uint8_t idz_di_shift_up; static uint8_t idz_di_view_chg; static uint8_t idz_di_start; +static uint8_t idz_di_up; +static uint8_t idz_di_down; +static uint8_t idz_di_left; +static uint8_t idz_di_right; static uint8_t idz_di_gear[6]; static bool idz_di_use_pedals; static bool idz_di_reverse_brake_axis; @@ -276,6 +280,30 @@ static HRESULT idz_di_config_apply(const struct idz_di_config *cfg) return E_INVALIDARG; } + if (cfg->up > 32) { + dprintf("Wheel: Invalid up button: %i\n", cfg->up); + + return E_INVALIDARG; + } + + if (cfg->down > 32) { + dprintf("Wheel: Invalid down button: %i\n", cfg->down); + + return E_INVALIDARG; + } + + if (cfg->left > 32) { + dprintf("Wheel: Invalid left button: %i\n", cfg->left); + + return E_INVALIDARG; + } + + if (cfg->right > 32) { + dprintf("Wheel: Invalid right button: %i\n", cfg->right); + + return E_INVALIDARG; + } + if (cfg->shift_dn > 32) { dprintf("Wheel: Invalid shift down button: %i\n", cfg->shift_dn); @@ -309,6 +337,10 @@ static HRESULT idz_di_config_apply(const struct idz_di_config *cfg) } dprintf("Wheel: Start button . . . : %i\n", cfg->start); dprintf("Wheel: View Change button : %i\n", cfg->view_chg); + dprintf("Wheel: Up button . . . . . : %i\n", cfg->up); + dprintf("Wheel: Down button . . . : %i\n", cfg->down); + dprintf("Wheel: Left button . . . . : %i\n", cfg->left); + dprintf("Wheel: Right button . . . : %i\n", cfg->right); dprintf("Wheel: Shift Down button . : %i\n", cfg->shift_dn); dprintf("Wheel: Shift Up button . . : %i\n", cfg->shift_up); dprintf("Wheel: Reverse Brake Axis : %i\n", cfg->reverse_brake_axis); @@ -341,6 +373,10 @@ static HRESULT idz_di_config_apply(const struct idz_di_config *cfg) idz_di_off_brake = brake_axis->off; idz_di_off_accel = accel_axis->off; idz_di_start = cfg->start; + idz_di_up = cfg->up; + idz_di_down = cfg->down; + idz_di_left = cfg->left; + idz_di_right = cfg->right; idz_di_view_chg = cfg->view_chg; idz_di_shift_dn = cfg->shift_dn; idz_di_shift_up = cfg->shift_up; @@ -497,6 +533,22 @@ static void idz_di_jvs_read_buttons(uint8_t *gamebtn_out) gamebtn |= IDZ_IO_GAMEBTN_VIEW_CHANGE; } + if (idz_di_up && state.st.rgbButtons[idz_di_up - 1]) { + gamebtn |= IDZ_IO_GAMEBTN_UP; + } + + if (idz_di_down && state.st.rgbButtons[idz_di_down - 1]) { + gamebtn |= IDZ_IO_GAMEBTN_DOWN; + } + + if (idz_di_left && state.st.rgbButtons[idz_di_left - 1]) { + gamebtn |= IDZ_IO_GAMEBTN_LEFT; + } + + if (idz_di_right && state.st.rgbButtons[idz_di_right - 1]) { + gamebtn |= IDZ_IO_GAMEBTN_RIGHT; + } + *gamebtn_out = gamebtn; } diff --git a/games/swdcio/config.c b/games/swdcio/config.c index 4f095ce..9bf45a8 100644 --- a/games/swdcio/config.c +++ b/games/swdcio/config.c @@ -50,6 +50,10 @@ void swdc_di_config_load(struct swdc_di_config *cfg, const wchar_t *filename) cfg->start = GetPrivateProfileIntW(L"dinput", L"start", 0, filename); cfg->view_chg = GetPrivateProfileIntW(L"dinput", L"viewChg", 0, filename); + cfg->up = GetPrivateProfileIntW(L"dinput", L"up", 0, filename); + cfg->down = GetPrivateProfileIntW(L"dinput", L"down", 0, filename); + cfg->left = GetPrivateProfileIntW(L"dinput", L"left", 0, filename); + cfg->right = GetPrivateProfileIntW(L"dinput", L"right", 0, filename); cfg->paddle_left = GetPrivateProfileIntW(L"dinput", L"paddleLeft", 0, filename); cfg->paddle_right = GetPrivateProfileIntW(L"dinput", L"paddleRight", 0, filename); cfg->wheel_green = GetPrivateProfileIntW(L"dinput", L"wheelGreen", 0, filename); diff --git a/games/swdcio/config.h b/games/swdcio/config.h index 9a630a2..c4e2dc3 100644 --- a/games/swdcio/config.h +++ b/games/swdcio/config.h @@ -11,6 +11,10 @@ struct swdc_di_config { wchar_t accel_axis[16]; uint8_t start; uint8_t view_chg; + uint8_t up; + uint8_t down; + uint8_t left; + uint8_t right; uint8_t paddle_left; uint8_t paddle_right; uint8_t wheel_green; diff --git a/games/swdcio/di.c b/games/swdcio/di.c index 14c8a45..d0ac006 100644 --- a/games/swdcio/di.c +++ b/games/swdcio/di.c @@ -61,6 +61,10 @@ static uint8_t swdc_di_paddle_left; static uint8_t swdc_di_paddle_right; static uint8_t swdc_di_view_chg; static uint8_t swdc_di_start; +static uint8_t swdc_di_up; +static uint8_t swdc_di_down; +static uint8_t swdc_di_left; +static uint8_t swdc_di_right; static uint8_t swdc_di_wheel_green; static uint8_t swdc_di_wheel_red; static uint8_t swdc_di_wheel_blue; @@ -202,6 +206,30 @@ static HRESULT swdc_di_config_apply(const struct swdc_di_config *cfg) return E_INVALIDARG; } + if (cfg->up > 32) { + dprintf("Wheel: Invalid up button: %i\n", cfg->up); + + return E_INVALIDARG; + } + + if (cfg->down > 32) { + dprintf("Wheel: Invalid down button: %i\n", cfg->down); + + return E_INVALIDARG; + } + + if (cfg->left > 32) { + dprintf("Wheel: Invalid left button: %i\n", cfg->left); + + return E_INVALIDARG; + } + + if (cfg->right > 32) { + dprintf("Wheel: Invalid right button: %i\n", cfg->right); + + return E_INVALIDARG; + } + if (cfg->paddle_left > 32) { dprintf("Wheel: Invalid left paddle button: %i\n", cfg->paddle_left); @@ -249,6 +277,10 @@ static HRESULT swdc_di_config_apply(const struct swdc_di_config *cfg) } dprintf("Wheel: Start button . . . . . : %i\n", cfg->start); dprintf("Wheel: View Change button . . : %i\n", cfg->view_chg); + dprintf("Wheel: Up button . . . . . . : %i\n", cfg->up); + dprintf("Wheel: Down button . . . . . : %i\n", cfg->down); + dprintf("Wheel: Left button . . . . . : %i\n", cfg->left); + dprintf("Wheel: Right button . . . . : %i\n", cfg->right); dprintf("Wheel: Paddle Left button . . : %i\n", cfg->paddle_left); dprintf("Wheel: Paddle Right button . : %i\n", cfg->paddle_right); dprintf("Wheel: Steering Green button : %i\n", cfg->wheel_green); @@ -272,6 +304,10 @@ static HRESULT swdc_di_config_apply(const struct swdc_di_config *cfg) swdc_di_off_accel = accel_axis->off; swdc_di_start = cfg->start; swdc_di_view_chg = cfg->view_chg; + swdc_di_up = cfg->up; + swdc_di_down = cfg->down; + swdc_di_left = cfg->left; + swdc_di_right = cfg->right; swdc_di_paddle_left = cfg->paddle_left; swdc_di_paddle_right = cfg->paddle_right; swdc_di_wheel_green = cfg->wheel_green; @@ -399,6 +435,22 @@ static void swdc_di_get_buttons(uint16_t *gamebtn_out) gamebtn |= SWDC_IO_GAMEBTN_VIEW_CHANGE; } + if (swdc_di_up && state.st.rgbButtons[swdc_di_up - 1]) { + gamebtn |= SWDC_IO_GAMEBTN_UP; + } + + if (swdc_di_down && state.st.rgbButtons[swdc_di_down - 1]) { + gamebtn |= SWDC_IO_GAMEBTN_DOWN; + } + + if (swdc_di_left && state.st.rgbButtons[swdc_di_left - 1]) { + gamebtn |= SWDC_IO_GAMEBTN_LEFT; + } + + if (swdc_di_right && state.st.rgbButtons[swdc_di_right - 1]) { + gamebtn |= SWDC_IO_GAMEBTN_RIGHT; + } + if (swdc_di_paddle_left && state.st.rgbButtons[swdc_di_paddle_left - 1]) { gamebtn |= SWDC_IO_GAMEBTN_STEERING_PADDLE_LEFT; } From 318272089016d3d419dd5b36be834048e50e4617 Mon Sep 17 00:00:00 2001 From: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> Date: Thu, 8 Jan 2026 20:20:02 +0000 Subject: [PATCH 16/37] ewf: dynamic allocation to shrink output binary size (#92) as noted on discord Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/92 Co-authored-by: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> Co-committed-by: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> --- common/platform/ewf.c | 13 +++++++++---- common/platform/ewf.h | 1 - 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/common/platform/ewf.c b/common/platform/ewf.c index 800e3cd..d85c6e5 100644 --- a/common/platform/ewf.c +++ b/common/platform/ewf.c @@ -22,7 +22,8 @@ const static struct ewf_config* ewf_config; static struct ewf_virtual_file virtual_file_table[EWF_MAX_VIRTUAL_FILES] = {0}; -static struct ewf_real_handle handle_table[EWF_MAX_HANDLES] = {0}; +static struct ewf_real_handle* handle_table; +static uint32_t handle_table_size; static CRITICAL_SECTION file_table_lock; static CRITICAL_SECTION handle_table_lock; static wchar_t windows_directory[MAX_PATH]; @@ -179,7 +180,7 @@ static struct ewf_real_handle* ewf_open_virtual_file(HANDLE virtual_handle) { return NULL; } EnterCriticalSection(&handle_table_lock); - for (int i = 0; i < EWF_MAX_HANDLES; i++) { + for (int i = 0; i < handle_table_size; i++) { if (handle_table[i].real_handle == NULL) { h = &handle_table[i]; HRESULT hr = iohook_open_nul_fd(&h->real_handle); @@ -208,7 +209,7 @@ static struct ewf_real_handle* ewf_get_real_handle(HANDLE real_handle) { } struct ewf_real_handle* match = NULL; EnterCriticalSection(&handle_table_lock); - for (int i = 0; i < EWF_MAX_HANDLES; i++) { + for (int i = 0; i < handle_table_size; i++) { if (handle_table[i].real_handle == real_handle) { match = &handle_table[i]; break; @@ -221,7 +222,7 @@ static struct ewf_real_handle* ewf_get_real_handle(HANDLE real_handle) { static BOOL ewf_close_virtual_file(HANDLE real_handle) { const struct ewf_virtual_file* match = NULL; EnterCriticalSection(&handle_table_lock); - for (int i = 0; i < EWF_MAX_HANDLES; i++) { + for (int i = 0; i < handle_table_size; i++) { if (handle_table[i].real_handle == real_handle) { match = handle_table[i].virtual_file; handle_table[i].real_handle = NULL; @@ -298,6 +299,10 @@ HRESULT ewf_hook_init(const struct ewf_config* config) { return S_FALSE; } + handle_table_size = config->full ? 50000 : 1024; + handle_table = malloc(sizeof(struct ewf_real_handle) * handle_table_size); + ZeroMemory(handle_table, handle_table_size); + GetWindowsDirectoryW(windows_directory, MAX_PATH); if (config->full) { diff --git a/common/platform/ewf.h b/common/platform/ewf.h index 2d7fc63..5ab71f5 100644 --- a/common/platform/ewf.h +++ b/common/platform/ewf.h @@ -6,7 +6,6 @@ #include #define EWF_MAX_VIRTUAL_FILES 255 -#define EWF_MAX_HANDLES 50000 #define EWF_DEFAULT_FILE_BUFFER_SIZE 1024 struct ewf_config { From 1a7a53f328644a4193dc1f4956d50202c9383a2a Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Fri, 9 Jan 2026 13:32:35 +0000 Subject: [PATCH 17/37] Update deadzone in wheel center for IDAC/IDZ/SWDC (#93) Original PR by @HaseoSora :https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/82 Co-authored-by: HaseoSora Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/93 Co-authored-by: Dniel97 Co-committed-by: Dniel97 --- dist/idac/segatools.ini | 11 ++ dist/idz/segatools.ini | 14 +- dist/swdc/segatools.ini | 12 +- games/idacio/config.c | 12 ++ games/idacio/config.h | 3 +- games/idacio/di-dev.c | 393 ++++++++++++++++++--------------------- games/idzio/config.c | 12 ++ games/idzio/config.h | 3 +- games/idzio/di-dev.c | 386 ++++++++++++++++++-------------------- games/swdcio/config.c | 125 ++++++++----- games/swdcio/config.h | 3 +- games/swdcio/di-dev.c | 397 ++++++++++++++++++---------------------- 12 files changed, 665 insertions(+), 706 deletions(-) diff --git a/dist/idac/segatools.ini b/dist/idac/segatools.ini index f596979..7cf57fc 100644 --- a/dist/idac/segatools.ini +++ b/dist/idac/segatools.ini @@ -271,3 +271,14 @@ damperStrength=100 rumbleStrength=100 ; Rumble duration factor from ms to µs, used to scale the duration of the rumble effect. rumbleDuration=1000 +; Sets the minimum amount of "weight" or stiffness in the wheel, +; even when the car is stationary or moving slowly. +; Higher values make the wheel feel heavier and less "springy." +baseDamperFraction = 20 + +; Deadband (Granularity: 0.1% per unit, max 20.0%) +; Creates a small "no-force" zone at the wheel's center. +; This prevents the wheel from oscillating or "shaking" when driving straight. +; Recommended: 10 (1.0%) to 100 (10.0%). +; Default: 20 = 2.0% +deadband = 20 diff --git a/dist/idz/segatools.ini b/dist/idz/segatools.ini index fc2ec69..d27d992 100644 --- a/dist/idz/segatools.ini +++ b/dist/idz/segatools.ini @@ -266,8 +266,18 @@ reverseBrakeAxis=0 constantForceStrength=100 ; Damper strength, used for steering wheel damper effect. damperStrength=100 - ; Rumble strength, used for road surface effects. rumbleStrength=100 ; Rumble duration factor from ms to µs, used to scale the duration of the rumble effect. -rumbleDuration=1000 \ No newline at end of file +rumbleDuration=1000 +; Sets the minimum amount of "weight" or stiffness in the wheel, +; even when the car is stationary or moving slowly. +; Higher values make the wheel feel heavier and less "springy." +baseDamperFraction = 20 + +; Deadband (Granularity: 0.1% per unit, max 20.0%) +; Creates a small "no-force" zone at the wheel's center. +; This prevents the wheel from oscillating or "shaking" when driving straight. +; Recommended: 10 (1.0%) to 100 (10.0%). +; Default: 20 = 2.0% +deadband = 20 diff --git a/dist/swdc/segatools.ini b/dist/swdc/segatools.ini index 6c93ae7..1d43e50 100644 --- a/dist/swdc/segatools.ini +++ b/dist/swdc/segatools.ini @@ -218,8 +218,18 @@ reverseBrakeAxis=0 constantForceStrength=100 ; Damper strength, used for steering wheel damper effect. damperStrength=100 - ; Rumble strength, used for road surface effects. rumbleStrength=100 ; Rumble duration factor from ms to µs, used to scale the duration of the rumble effect. rumbleDuration=1000 +; Sets the minimum amount of "weight" or stiffness in the wheel, +; even when the car is stationary or moving slowly. +; Higher values make the wheel feel heavier and less "springy." +baseDamperFraction = 20 + +; Deadband (Granularity: 0.1% per unit, max 20.0%) +; Creates a small "no-force" zone at the wheel's center. +; This prevents the wheel from oscillating or "shaking" when driving straight. +; Recommended: 10 (1.0%) to 100 (10.0%). +; Default: 20 = 2.0% +deadband = 20 diff --git a/games/idacio/config.c b/games/idacio/config.c index a5c1769..8b85c54 100644 --- a/games/idacio/config.c +++ b/games/idacio/config.c @@ -105,6 +105,18 @@ void idac_di_config_load(struct idac_di_config *cfg, const wchar_t *filename) L"rumbleDuration", 1000, filename); + + cfg->ffb_base_damper_fraction = GetPrivateProfileIntW( + L"dinput", + L"baseDamperFraction", + 20, + filename); + + cfg->ffb_deadband = GetPrivateProfileIntW( + L"dinput", + L"deadband", + 2, + filename); } void idac_xi_config_load(struct idac_xi_config *cfg, const wchar_t *filename) diff --git a/games/idacio/config.h b/games/idacio/config.h index 5b7e736..698fb7c 100644 --- a/games/idacio/config.h +++ b/games/idacio/config.h @@ -30,7 +30,8 @@ struct idac_di_config { uint8_t ffb_constant_force_strength; uint8_t ffb_rumble_strength; uint8_t ffb_damper_strength; - + uint8_t ffb_deadband; + uint8_t ffb_base_damper_fraction; uint32_t ffb_rumble_duration; }; diff --git a/games/idacio/di-dev.c b/games/idacio/di-dev.c index 5f959f6..1bc714a 100644 --- a/games/idacio/di-dev.c +++ b/games/idacio/di-dev.c @@ -1,46 +1,102 @@ -#include +#include #include #include -#include +#include #include "idacio/di-dev.h" + #include "util/dprintf.h" -const struct idac_di_config *idac_di_cfg; -static HWND idac_di_wnd; -static IDirectInputDevice8W *idac_di_dev; +/* Globals */ +static HWND idac_di_wnd = NULL; +static IDirectInputDevice8W* idac_di_dev = NULL; -/* Individual DI Effects */ -static IDirectInputEffect *idac_di_fx; -static IDirectInputEffect *idac_di_fx_rumble; -static IDirectInputEffect *idac_di_fx_damper; +static void update_or_create_effect(LPDIRECTINPUTEFFECT* effect_ptr, REFGUID guid, DIEFFECT* fx); + +/* Effect handles */ +static IDirectInputEffect* idac_di_fx_constant = NULL; +static IDirectInputEffect* idac_di_fx_rumble = NULL; +static IDirectInputEffect* idac_di_fx_spring = NULL; +static IDirectInputEffect* idac_di_fx_damper = NULL; /* Max FFB Board value is 127 */ static const double idac_di_ffb_scale = 127.0; +static const double idac_di_damper_spring_ratio = 0.5; -HRESULT idac_di_dev_init( - const struct idac_di_config *cfg, - IDirectInputDevice8W *dev, - HWND wnd) -{ +/* Clamped config settings */ +static uint8_t idac_di_constant_force_strength; +static uint8_t idac_di_rumble_strength; +static uint8_t idac_di_damper_strength; +static uint8_t idac_di_rumble_duration; +static uint8_t idac_di_base_damper; +static uint8_t idac_di_deadband; + +HRESULT idac_di_dev_init(const struct idac_di_config* cfg, + IDirectInputDevice8W* dev, HWND wnd) { + assert(cfg != NULL); + assert(dev != NULL); + assert(wnd != NULL); + + idac_di_dev = dev; + idac_di_wnd = wnd; + + idac_di_rumble_duration = cfg->ffb_rumble_duration; + + idac_di_constant_force_strength = cfg->ffb_constant_force_strength > 100 + ? 100 + : cfg->ffb_constant_force_strength; + + idac_di_rumble_strength = cfg->ffb_rumble_strength > 100 + ? 100 + : cfg->ffb_rumble_strength; + + idac_di_damper_strength = cfg->ffb_damper_strength > 100 + ? 100 + : cfg->ffb_damper_strength; + + idac_di_base_damper = cfg->ffb_base_damper_fraction > 100 + ? 100 + : cfg->ffb_base_damper_fraction; + + /* Deadband is the only setting from 0.0% to 20.0%*/ + idac_di_deadband = cfg->ffb_deadband > 200 + ? 200 + : cfg->ffb_deadband; + + return S_OK; +} + +HRESULT idac_di_dev_start(IDirectInputDevice8W* dev, HWND wnd) { HRESULT hr; assert(dev != NULL); assert(wnd != NULL); - idac_di_cfg = cfg; - idac_di_dev = dev; - idac_di_wnd = wnd; + hr = IDirectInputDevice8_SetCooperativeLevel( + dev, wnd, DISCL_BACKGROUND | DISCL_EXCLUSIVE); + if (FAILED(hr)) { + dprintf("DirectInput: SetCooperativeLevel failed: %08x\n", (int)hr); + return hr; + } + + hr = IDirectInputDevice8_SetDataFormat(dev, &c_dfDIJoystick); + if (FAILED(hr)) { + dprintf("DirectInput: SetDataFormat failed: %08x\n", (int)hr); + return hr; + } + + hr = IDirectInputDevice8_Acquire(dev); + if (FAILED(hr)) { + dprintf("DirectInput: Acquire failed: %08x\n", (int)hr); + return hr; + } return S_OK; } -HRESULT idac_di_dev_poll( - IDirectInputDevice8W *dev, - HWND wnd, - union idac_di_state *out) -{ +HRESULT idac_di_dev_poll(IDirectInputDevice8W* dev, HWND wnd, + union idac_di_state* out) { HRESULT hr; MSG msg; @@ -50,19 +106,12 @@ HRESULT idac_di_dev_poll( memset(out, 0, sizeof(*out)); - /* Pump our dummy window's message queue just in case DirectInput or an - IHV DirectInput driver somehow relies on it */ - while (PeekMessageW(&msg, wnd, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } - hr = IDirectInputDevice8_GetDeviceState( - dev, - sizeof(out->st), - &out->st); - + hr = IDirectInputDevice8_GetDeviceState(dev, sizeof(out->st), &out->st); if (FAILED(hr)) { dprintf("DirectInput: GetDeviceState error: %08x\n", (int)hr); } @@ -70,100 +119,56 @@ HRESULT idac_di_dev_poll( return hr; } -HRESULT idac_di_dev_start(IDirectInputDevice8W *dev, HWND wnd) { - HRESULT hr; - - assert(dev != NULL); - assert(wnd != NULL); - - hr = IDirectInputDevice8_SetCooperativeLevel( - dev, - wnd, - DISCL_BACKGROUND | DISCL_EXCLUSIVE); - - if (FAILED(hr)) { - dprintf("DirectInput: SetCooperativeLevel failed: %08x\n", (int)hr); - return hr; +HRESULT idac_di_ffb_init(void) { + if (!idac_di_dev || !idac_di_wnd) { + return E_FAIL; } - hr = IDirectInputDevice8_SetDataFormat(dev, &c_dfDIJoystick); - - if (FAILED(hr)) { - dprintf("DirectInput: SetDataFormat failed: %08x\n", (int)hr); - return hr; - } - - hr = IDirectInputDevice8_Acquire(dev); - - if (FAILED(hr)) { - dprintf("DirectInput: Acquire failed: %08x\n", (int)hr); - return hr; - } - - return hr; + return idac_di_dev_start(idac_di_dev, idac_di_wnd); } -HRESULT idac_di_ffb_init(void) -{ - HRESULT hr; - - hr = idac_di_dev_start(idac_di_dev, idac_di_wnd); - - if (FAILED(hr)) { - return hr; - } - - return S_OK; -} - -void idac_di_ffb_toggle(bool active) -{ +void idac_di_ffb_toggle(bool active) { if (active) { return; } - /* Stop and release all effects */ - /* I never programmed DirectInput Effects, so this might be bad practice. */ - if (idac_di_fx != NULL) { - IDirectInputEffect_Stop(idac_di_fx); - IDirectInputEffect_Release(idac_di_fx); - idac_di_fx = NULL; + if (idac_di_fx_constant) { + IDirectInputEffect_Stop(idac_di_fx_constant); + IDirectInputEffect_Release(idac_di_fx_constant); + idac_di_fx_constant = NULL; } - - if (idac_di_fx_rumble != NULL) { + if (idac_di_fx_rumble) { IDirectInputEffect_Stop(idac_di_fx_rumble); IDirectInputEffect_Release(idac_di_fx_rumble); idac_di_fx_rumble = NULL; } - - if (idac_di_fx_damper != NULL) { + if (idac_di_fx_spring) { + IDirectInputEffect_Stop(idac_di_fx_spring); + IDirectInputEffect_Release(idac_di_fx_spring); + idac_di_fx_spring = NULL; + } + if (idac_di_fx_damper) { IDirectInputEffect_Stop(idac_di_fx_damper); IDirectInputEffect_Release(idac_di_fx_damper); idac_di_fx_damper = NULL; } } -void idac_di_ffb_constant_force(uint8_t direction_ffb, uint8_t force) -{ - /* DI expects a magnitude in the range of -10.000 to 10.000 */ - uint16_t ffb_strength = idac_di_cfg->ffb_constant_force_strength * 100; +void idac_di_ffb_constant_force(uint8_t direction_ffb, uint8_t force) { + uint16_t ffb_strength = idac_di_constant_force_strength * 100; if (ffb_strength == 0) { return; } - DWORD axis; - LONG direction; + DWORD axis = DIJOFS_X; + LONG direction = 0; DIEFFECT fx; DICONSTANTFORCE cf; HRESULT hr; - /* Direction 0: move to the right, 1: move to the left */ LONG magnitude = (LONG)(((double)force / idac_di_ffb_scale) * ffb_strength); cf.lMagnitude = (direction_ffb == 0) ? -magnitude : magnitude; - axis = DIJOFS_X; - direction = 0; - memset(&fx, 0, sizeof(fx)); fx.dwSize = sizeof(fx); fx.dwFlags = DIEFF_CARTESIAN | DIEFF_OBJECTOFFSETS; @@ -177,71 +182,24 @@ void idac_di_ffb_constant_force(uint8_t direction_ffb, uint8_t force) fx.cbTypeSpecificParams = sizeof(cf); fx.lpvTypeSpecificParams = &cf; - /* Check if the effect already exists */ - if (idac_di_fx != NULL) { - hr = IDirectInputEffect_SetParameters(idac_di_fx, &fx, DIEP_TYPESPECIFICPARAMS); - if (SUCCEEDED(hr)) { - return; // Successfully updated existing effect - } - else { - dprintf("DirectInput: Failed to update constant force feedback, recreating effect: %08x\n", (int)hr); - IDirectInputEffect_Stop(idac_di_fx); - IDirectInputEffect_Release(idac_di_fx); - idac_di_fx = NULL; // Reset the pointer - } - } - - /* Create a new constant force effect */ - IDirectInputEffect *obj; - hr = IDirectInputDevice8_CreateEffect( - idac_di_dev, - &GUID_ConstantForce, - &fx, - &obj, - NULL); - - if (FAILED(hr)) { - dprintf("DirectInput: Constant force feedback creation failed: %08x\n", (int)hr); - return; - } - - /* Start the effect */ - hr = IDirectInputEffect_Start(obj, INFINITE, 0); - if (FAILED(hr)) { - dprintf("DirectInput: Constant force feedback start failed: %08x\n", (int)hr); - IDirectInputEffect_Release(obj); - return; - } - - idac_di_fx = obj; + update_or_create_effect(&idac_di_fx_constant, &GUID_ConstantForce, &fx); } -void idac_di_ffb_rumble(uint8_t force, uint8_t period) -{ - /* DI expects a magnitude in the range of -10.000 to 10.000 */ - uint16_t ffb_strength = idac_di_cfg->ffb_rumble_strength * 100; +void idac_di_ffb_rumble(uint8_t force, uint8_t period) { + uint16_t ffb_strength = idac_di_rumble_strength * 100; if (ffb_strength == 0) { return; } - uint32_t ffb_duration = idac_di_cfg->ffb_rumble_duration; - - DWORD axis; - LONG direction; + DWORD axis = DIJOFS_X; + LONG direction = 0; DIEFFECT fx; DIPERIODIC pe; HRESULT hr; - DWORD duration = (DWORD)((double)force * ffb_duration); - memset(&pe, 0, sizeof(pe)); pe.dwMagnitude = (DWORD)(((double)force / idac_di_ffb_scale) * ffb_strength); - pe.lOffset = 0; - pe.dwPhase = 0; - pe.dwPeriod = duration; - - axis = DIJOFS_X; - direction = 0; + pe.dwPeriod = (DWORD)force * idac_di_rumble_duration; memset(&fx, 0, sizeof(fx)); fx.dwSize = sizeof(fx); @@ -256,70 +214,41 @@ void idac_di_ffb_rumble(uint8_t force, uint8_t period) fx.cbTypeSpecificParams = sizeof(pe); fx.lpvTypeSpecificParams = &pe; - /* Check if the effect already exists */ - if (idac_di_fx_rumble != NULL) { - hr = IDirectInputEffect_SetParameters(idac_di_fx_rumble, &fx, DIEP_TYPESPECIFICPARAMS); - if (SUCCEEDED(hr)) { - return; - } - else { - dprintf("DirectInput: Failed to update rumble feedback, recreating effect: %08x\n", (int)hr); - IDirectInputEffect_Stop(idac_di_fx_rumble); - IDirectInputEffect_Release(idac_di_fx_rumble); - idac_di_fx_rumble = NULL; - } - } - - /* Create a new rumble effect */ - IDirectInputEffect *obj; - hr = IDirectInputDevice8_CreateEffect( - idac_di_dev, - &GUID_Sine, - &fx, - &obj, - NULL); - - if (FAILED(hr)) { - dprintf("DirectInput: Rumble effect creation failed: %08x\n", (int)hr); - return; - } - - /* Start the effect */ - hr = IDirectInputEffect_Start(obj, INFINITE, 0); - if (FAILED(hr)) { - dprintf("DirectInput: Rumble effect start failed: %08x\n", (int)hr); - IDirectInputEffect_Release(obj); - return; - } - - idac_di_fx_rumble = obj; + update_or_create_effect(&idac_di_fx_rumble, &GUID_Sine, &fx); } -void idac_di_ffb_damper(uint8_t force) -{ - /* DI expects a coefficient in the range of -10.000 to 10.000 */ - uint16_t ffb_strength = idac_di_cfg->ffb_damper_strength * 100; +void idac_di_ffb_damper(uint8_t force) { + HRESULT hr; + uint16_t ffb_strength = idac_di_damper_strength * 100; + if (ffb_strength == 0) { + if (idac_di_fx_spring) { + IDirectInputEffect_Stop(idac_di_fx_spring); + IDirectInputEffect_Release(idac_di_fx_spring); + idac_di_fx_spring = NULL; + } + if (idac_di_fx_damper) { + IDirectInputEffect_Stop(idac_di_fx_damper); + IDirectInputEffect_Release(idac_di_fx_damper); + idac_di_fx_damper = NULL; + } return; } - DWORD axis; - LONG direction; + DWORD axis = DIJOFS_X; + LONG direction = 0; DIEFFECT fx; DICONDITION cond; - HRESULT hr; + /* SPRING (centering) */ memset(&cond, 0, sizeof(cond)); - cond.lOffset = 0; - cond.lPositiveCoefficient = (LONG)(((double)force / idac_di_ffb_scale) * ffb_strength); - cond.lNegativeCoefficient = (LONG)(((double)force / idac_di_ffb_scale) * ffb_strength); - /* Not sure on this one */ + cond.lPositiveCoefficient = (LONG)(((uint32_t)force * ffb_strength) / idac_di_ffb_scale); + cond.lNegativeCoefficient = cond.lPositiveCoefficient; cond.dwPositiveSaturation = DI_FFNOMINALMAX; cond.dwNegativeSaturation = DI_FFNOMINALMAX; - cond.lDeadBand = 0; - - axis = DIJOFS_X; - direction = 0; + + /* If user enters 25, result is 0.025 * DI_FFNOMINALMAX */ + cond.lDeadBand = (DI_FFNOMINALMAX * (LONG)idac_di_deadband) / 1000; memset(&fx, 0, sizeof(fx)); fx.dwSize = sizeof(fx); @@ -334,38 +263,64 @@ void idac_di_ffb_damper(uint8_t force) fx.cbTypeSpecificParams = sizeof(cond); fx.lpvTypeSpecificParams = &cond; - /* Check if the damper effect already exists */ - if (idac_di_fx_damper != NULL) { - hr = IDirectInputEffect_SetParameters(idac_di_fx_damper, &fx, DIEP_TYPESPECIFICPARAMS); + update_or_create_effect(&idac_di_fx_spring, &GUID_Spring, &fx); + + /* DAMPER (resistance with baseline) */ + memset(&cond, 0, sizeof(cond)); + + LONG nominal = (LONG)(( (LONG)force * ffb_strength * idac_di_damper_spring_ratio ) / idac_di_ffb_scale); + LONG min_baseline = (LONG)(DI_FFNOMINALMAX * (idac_di_base_damper / 100.0)); + + if (nominal < min_baseline) { + nominal = min_baseline; + } + + cond.lPositiveCoefficient = nominal; + cond.lNegativeCoefficient = nominal; + cond.dwPositiveSaturation = DI_FFNOMINALMAX; + cond.dwNegativeSaturation = DI_FFNOMINALMAX; + + memset(&fx, 0, sizeof(fx)); + fx.dwSize = sizeof(fx); + fx.dwFlags = DIEFF_CARTESIAN | DIEFF_OBJECTOFFSETS; + fx.dwDuration = INFINITE; + fx.dwGain = DI_FFNOMINALMAX; + fx.dwTriggerButton = DIEB_NOTRIGGER; + fx.dwTriggerRepeatInterval = INFINITE; + fx.cAxes = 1; + fx.rgdwAxes = &axis; + fx.rglDirection = &direction; + fx.cbTypeSpecificParams = sizeof(cond); + fx.lpvTypeSpecificParams = &cond; + + update_or_create_effect(&idac_di_fx_damper, &GUID_Damper, &fx); +} + +static void update_or_create_effect(LPDIRECTINPUTEFFECT* effect_ptr, REFGUID guid, DIEFFECT* fx) { + HRESULT hr; + + if (*effect_ptr != NULL) { + hr = IDirectInputEffect_SetParameters(*effect_ptr, fx, DIEP_TYPESPECIFICPARAMS); if (SUCCEEDED(hr)) { return; - } - else { - IDirectInputEffect_Stop(idac_di_fx_damper); - IDirectInputEffect_Release(idac_di_fx_damper); - idac_di_fx_damper = NULL; + } else { + IDirectInputEffect_Stop(*effect_ptr); + IDirectInputEffect_Release(*effect_ptr); + *effect_ptr = NULL; } } - /* Create a new damper effect */ - IDirectInputEffect *obj; - hr = IDirectInputDevice8_CreateEffect( - idac_di_dev, - &GUID_Damper, - &fx, - &obj, - NULL); - + IDirectInputEffect* obj = NULL; + hr = IDirectInputDevice8_CreateEffect(idac_di_dev, guid, fx, &obj, NULL); if (FAILED(hr)) { return; } - /* Start the effect */ - hr = IDirectInputEffect_Start(obj, fx.dwDuration, 0); + hr = IDirectInputEffect_Start(obj, fx->dwDuration, 0); if (FAILED(hr)) { IDirectInputEffect_Release(obj); return; } - idac_di_fx_damper = obj; + *effect_ptr = obj; } diff --git a/games/idzio/config.c b/games/idzio/config.c index 81b3a70..215934f 100644 --- a/games/idzio/config.c +++ b/games/idzio/config.c @@ -105,6 +105,18 @@ void idz_di_config_load(struct idz_di_config *cfg, const wchar_t *filename) L"rumbleDuration", 1000, filename); + + cfg->ffb_base_damper_fraction = GetPrivateProfileIntW( + L"dinput", + L"baseDamperFraction", + 20, + filename); + + cfg->ffb_deadband = GetPrivateProfileIntW( + L"dinput", + L"deadband", + 2, + filename); } void idz_xi_config_load(struct idz_xi_config *cfg, const wchar_t *filename) diff --git a/games/idzio/config.h b/games/idzio/config.h index 78b4b50..acb8f0d 100644 --- a/games/idzio/config.h +++ b/games/idzio/config.h @@ -30,7 +30,8 @@ struct idz_di_config { uint8_t ffb_constant_force_strength; uint8_t ffb_rumble_strength; uint8_t ffb_damper_strength; - + uint8_t ffb_base_damper_fraction; + uint8_t ffb_deadband; uint32_t ffb_rumble_duration; }; diff --git a/games/idzio/di-dev.c b/games/idzio/di-dev.c index 10b67d6..25d2b79 100644 --- a/games/idzio/di-dev.c +++ b/games/idzio/di-dev.c @@ -1,85 +1,79 @@ -#include +#include #include #include -#include +#include #include "idzio/di-dev.h" #include "util/dprintf.h" -const struct idz_di_config *idz_di_cfg; -static HWND idz_di_wnd; -static IDirectInputDevice8W *idz_di_dev; +/* Globals */ +static HWND idz_di_wnd = NULL; +static IDirectInputDevice8W* idz_di_dev = NULL; + +static void update_or_create_effect(LPDIRECTINPUTEFFECT* effect_ptr, REFGUID guid, DIEFFECT* fx); /* Individual DI Effects */ -static IDirectInputEffect *idz_di_fx; -static IDirectInputEffect *idz_di_fx_rumble; -static IDirectInputEffect *idz_di_fx_damper; +static IDirectInputEffect* idz_di_fx_constant = NULL; +static IDirectInputEffect* idz_di_fx_rumble = NULL; +static IDirectInputEffect* idz_di_fx_spring = NULL; +static IDirectInputEffect* idz_di_fx_damper = NULL; /* Max FFB Board value is 127 */ static const double idz_di_ffb_scale = 127.0; +static const double idz_di_damper_spring_ratio = 0.5; -HRESULT idz_di_dev_init( - const struct idz_di_config *cfg, - IDirectInputDevice8W *dev, - HWND wnd) -{ - HRESULT hr; +/* Clamped config settings */ +static uint8_t idz_di_constant_force_strength; +static uint8_t idz_di_rumble_strength; +static uint8_t idz_di_damper_strength; +static uint8_t idz_di_rumble_duration; +static uint8_t idz_di_base_damper; +static uint8_t idz_di_deadband; +HRESULT idz_di_dev_init(const struct idz_di_config* cfg, + IDirectInputDevice8W* dev, HWND wnd) { + assert(cfg != NULL); assert(dev != NULL); assert(wnd != NULL); - idz_di_cfg = cfg; idz_di_dev = dev; idz_di_wnd = wnd; + idz_di_rumble_duration = cfg->ffb_rumble_duration; + + idz_di_constant_force_strength = cfg->ffb_constant_force_strength > 100 + ? 100 + : cfg->ffb_constant_force_strength; + + idz_di_rumble_strength = cfg->ffb_rumble_strength > 100 + ? 100 + : cfg->ffb_rumble_strength; + + idz_di_damper_strength = cfg->ffb_damper_strength > 100 + ? 100 + : cfg->ffb_damper_strength; + + idz_di_base_damper = cfg->ffb_base_damper_fraction > 100 + ? 100 + : cfg->ffb_base_damper_fraction; + + /* Deadband is the only setting from 0.0% to 20.0%*/ + idz_di_deadband = cfg->ffb_deadband > 200 + ? 200 + : cfg->ffb_deadband; + return S_OK; } -HRESULT idz_di_dev_poll( - IDirectInputDevice8W *dev, - HWND wnd, - union idz_di_state *out) -{ - HRESULT hr; - MSG msg; - - assert(dev != NULL); - assert(wnd != NULL); - assert(out != NULL); - - memset(out, 0, sizeof(*out)); - - /* Pump our dummy window's message queue just in case DirectInput or an - IHV DirectInput driver somehow relies on it */ - - while (PeekMessageW(&msg, wnd, 0, 0, PM_REMOVE)) { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - - hr = IDirectInputDevice8_GetDeviceState( - dev, - sizeof(out->st), - &out->st); - - if (FAILED(hr)) { - dprintf("DirectInput: GetDeviceState error: %08x\n", (int)hr); - } - - return hr; -} - -HRESULT idz_di_dev_start(IDirectInputDevice8W *dev, HWND wnd) { +HRESULT idz_di_dev_start(IDirectInputDevice8W* dev, HWND wnd) { HRESULT hr; assert(dev != NULL); assert(wnd != NULL); hr = IDirectInputDevice8_SetCooperativeLevel( - dev, - wnd, - DISCL_BACKGROUND | DISCL_EXCLUSIVE); + dev, wnd, DISCL_BACKGROUND | DISCL_EXCLUSIVE); if (FAILED(hr)) { dprintf("DirectInput: SetCooperativeLevel failed: %08x\n", (int)hr); @@ -100,70 +94,90 @@ HRESULT idz_di_dev_start(IDirectInputDevice8W *dev, HWND wnd) { return hr; } - return hr; -} - -HRESULT idz_di_ffb_init(void) -{ - HRESULT hr; - - hr = idz_di_dev_start(idz_di_dev, idz_di_wnd); - - if (FAILED(hr)) { - return hr; - } - return S_OK; } -void idz_di_ffb_toggle(bool active) -{ +HRESULT idz_di_dev_poll(IDirectInputDevice8W* dev, HWND wnd, + union idz_di_state* out) { + HRESULT hr; + MSG msg; + + assert(dev != NULL); + assert(wnd != NULL); + assert(out != NULL); + + memset(out, 0, sizeof(*out)); + + /* Pump our dummy window's message queue just in case DirectInput or an + IHV DirectInput driver somehow relies on it */ + + while (PeekMessageW(&msg, wnd, 0, 0, PM_REMOVE)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + hr = IDirectInputDevice8_GetDeviceState(dev, sizeof(out->st), &out->st); + + if (FAILED(hr)) { + dprintf("DirectInput: GetDeviceState error: %08x\n", (int)hr); + } + + return hr; +} + +HRESULT idz_di_ffb_init(void) { + if (!idz_di_dev || !idz_di_wnd) { + return E_FAIL; + } + + return idz_di_dev_start(idz_di_dev, idz_di_wnd); +} + +void idz_di_ffb_toggle(bool active) { if (active) { return; } - /* Stop and release all effects */ - /* I never programmed DirectInput Effects, so this might be bad practice. */ - if (idz_di_fx != NULL) { - IDirectInputEffect_Stop(idz_di_fx); - IDirectInputEffect_Release(idz_di_fx); - idz_di_fx = NULL; + if (idz_di_fx_constant) { + IDirectInputEffect_Stop(idz_di_fx_constant); + IDirectInputEffect_Release(idz_di_fx_constant); + idz_di_fx_constant = NULL; } - if (idz_di_fx_rumble != NULL) { + if (idz_di_fx_rumble) { IDirectInputEffect_Stop(idz_di_fx_rumble); IDirectInputEffect_Release(idz_di_fx_rumble); idz_di_fx_rumble = NULL; } - if (idz_di_fx_damper != NULL) { + if (idz_di_fx_spring) { + IDirectInputEffect_Stop(idz_di_fx_spring); + IDirectInputEffect_Release(idz_di_fx_spring); + idz_di_fx_spring = NULL; + } + + if (idz_di_fx_damper) { IDirectInputEffect_Stop(idz_di_fx_damper); IDirectInputEffect_Release(idz_di_fx_damper); idz_di_fx_damper = NULL; } } -void idz_di_ffb_constant_force(uint8_t direction_ffb, uint8_t force) -{ - /* DI expects a magnitude in the range of -10.000 to 10.000 */ - uint16_t ffb_strength = idz_di_cfg->ffb_constant_force_strength * 100; +void idz_di_ffb_constant_force(uint8_t direction_ffb, uint8_t force) { + uint16_t ffb_strength = idz_di_constant_force_strength * 100; if (ffb_strength == 0) { return; } - DWORD axis; - LONG direction; + DWORD axis = DIJOFS_X; + LONG direction = 0; DIEFFECT fx; DICONSTANTFORCE cf; HRESULT hr; - /* Direction 0: move to the right, 1: move to the left */ LONG magnitude = (LONG)(((double)force / idz_di_ffb_scale) * ffb_strength); cf.lMagnitude = (direction_ffb == 0) ? -magnitude : magnitude; - axis = DIJOFS_X; - direction = 0; - memset(&fx, 0, sizeof(fx)); fx.dwSize = sizeof(fx); fx.dwFlags = DIEFF_CARTESIAN | DIEFF_OBJECTOFFSETS; @@ -177,71 +191,24 @@ void idz_di_ffb_constant_force(uint8_t direction_ffb, uint8_t force) fx.cbTypeSpecificParams = sizeof(cf); fx.lpvTypeSpecificParams = &cf; - /* Check if the effect already exists */ - if (idz_di_fx != NULL) { - hr = IDirectInputEffect_SetParameters(idz_di_fx, &fx, DIEP_TYPESPECIFICPARAMS); - if (SUCCEEDED(hr)) { - return; // Successfully updated existing effect - } - else { - dprintf("DirectInput: Failed to update constant force feedback, recreating effect: %08x\n", (int)hr); - IDirectInputEffect_Stop(idz_di_fx); - IDirectInputEffect_Release(idz_di_fx); - idz_di_fx = NULL; // Reset the pointer - } - } - - /* Create a new constant force effect */ - IDirectInputEffect *obj; - hr = IDirectInputDevice8_CreateEffect( - idz_di_dev, - &GUID_ConstantForce, - &fx, - &obj, - NULL); - - if (FAILED(hr)) { - dprintf("DirectInput: Constant force feedback creation failed: %08x\n", (int)hr); - return; - } - - /* Start the effect */ - hr = IDirectInputEffect_Start(obj, INFINITE, 0); - if (FAILED(hr)) { - dprintf("DirectInput: Constant force feedback start failed: %08x\n", (int)hr); - IDirectInputEffect_Release(obj); - return; - } - - idz_di_fx = obj; + update_or_create_effect(&idz_di_fx_constant, &GUID_ConstantForce, &fx); } -void idz_di_ffb_rumble(uint8_t force, uint8_t period) -{ - /* DI expects a magnitude in the range of -10.000 to 10.000 */ - uint16_t ffb_strength = idz_di_cfg->ffb_rumble_strength * 100; +void idz_di_ffb_rumble(uint8_t force, uint8_t period) { + uint16_t ffb_strength = idz_di_rumble_strength * 100; if (ffb_strength == 0) { return; } - uint32_t ffb_duration = idz_di_cfg->ffb_rumble_duration; - - DWORD axis; - LONG direction; + DWORD axis = DIJOFS_X; + LONG direction = 0; DIEFFECT fx; DIPERIODIC pe; HRESULT hr; - DWORD duration = (DWORD)((double)force * ffb_duration); - memset(&pe, 0, sizeof(pe)); pe.dwMagnitude = (DWORD)(((double)force / idz_di_ffb_scale) * ffb_strength); - pe.lOffset = 0; - pe.dwPhase = 0; - pe.dwPeriod = duration; - - axis = DIJOFS_X; - direction = 0; + pe.dwPeriod = (DWORD)force * idz_di_rumble_duration; memset(&fx, 0, sizeof(fx)); fx.dwSize = sizeof(fx); @@ -256,70 +223,41 @@ void idz_di_ffb_rumble(uint8_t force, uint8_t period) fx.cbTypeSpecificParams = sizeof(pe); fx.lpvTypeSpecificParams = &pe; - /* Check if the effect already exists */ - if (idz_di_fx_rumble != NULL) { - hr = IDirectInputEffect_SetParameters(idz_di_fx_rumble, &fx, DIEP_TYPESPECIFICPARAMS); - if (SUCCEEDED(hr)) { - return; - } - else { - dprintf("DirectInput: Failed to update rumble feedback, recreating effect: %08x\n", (int)hr); - IDirectInputEffect_Stop(idz_di_fx_rumble); - IDirectInputEffect_Release(idz_di_fx_rumble); - idz_di_fx_rumble = NULL; - } - } - - /* Create a new rumble effect */ - IDirectInputEffect *obj; - hr = IDirectInputDevice8_CreateEffect( - idz_di_dev, - &GUID_Sine, - &fx, - &obj, - NULL); - - if (FAILED(hr)) { - dprintf("DirectInput: Rumble effect creation failed: %08x\n", (int)hr); - return; - } - - /* Start the effect */ - hr = IDirectInputEffect_Start(obj, INFINITE, 0); - if (FAILED(hr)) { - dprintf("DirectInput: Rumble effect start failed: %08x\n", (int)hr); - IDirectInputEffect_Release(obj); - return; - } - - idz_di_fx_rumble = obj; + update_or_create_effect(&idz_di_fx_rumble, &GUID_Sine, &fx); } -void idz_di_ffb_damper(uint8_t force) -{ - /* DI expects a coefficient in the range of -10.000 to 10.000 */ - uint16_t ffb_strength = idz_di_cfg->ffb_damper_strength * 100; +void idz_di_ffb_damper(uint8_t force) { + HRESULT hr; + uint16_t ffb_strength = idz_di_damper_strength * 100; + if (ffb_strength == 0) { + if (idz_di_fx_spring) { + IDirectInputEffect_Stop(idz_di_fx_spring); + IDirectInputEffect_Release(idz_di_fx_spring); + idz_di_fx_spring = NULL; + } + if (idz_di_fx_damper) { + IDirectInputEffect_Stop(idz_di_fx_damper); + IDirectInputEffect_Release(idz_di_fx_damper); + idz_di_fx_damper = NULL; + } return; } - DWORD axis; - LONG direction; + DWORD axis = DIJOFS_X; + LONG direction = 0; DIEFFECT fx; DICONDITION cond; - HRESULT hr; + /* SPRING (centering) */ memset(&cond, 0, sizeof(cond)); - cond.lOffset = 0; - cond.lPositiveCoefficient = (LONG)(((double)force / idz_di_ffb_scale) * ffb_strength); - cond.lNegativeCoefficient = (LONG)(((double)force / idz_di_ffb_scale) * ffb_strength); - /* Not sure on this one */ + cond.lPositiveCoefficient = (LONG)(((uint32_t)force * ffb_strength) / idz_di_ffb_scale); + cond.lNegativeCoefficient = cond.lPositiveCoefficient; cond.dwPositiveSaturation = DI_FFNOMINALMAX; cond.dwNegativeSaturation = DI_FFNOMINALMAX; - cond.lDeadBand = 0; - - axis = DIJOFS_X; - direction = 0; + + /* If user enters 25, result is 0.025 * DI_FFNOMINALMAX */ + cond.lDeadBand = (DI_FFNOMINALMAX * (LONG)idz_di_deadband) / 1000; memset(&fx, 0, sizeof(fx)); fx.dwSize = sizeof(fx); @@ -334,38 +272,64 @@ void idz_di_ffb_damper(uint8_t force) fx.cbTypeSpecificParams = sizeof(cond); fx.lpvTypeSpecificParams = &cond; - /* Check if the damper effect already exists */ - if (idz_di_fx_damper != NULL) { - hr = IDirectInputEffect_SetParameters(idz_di_fx_damper, &fx, DIEP_TYPESPECIFICPARAMS); + update_or_create_effect(&idz_di_fx_spring, &GUID_Spring, &fx); + + /* DAMPER (resistance with baseline) */ + memset(&cond, 0, sizeof(cond)); + + LONG nominal = (LONG)(( (LONG)force * ffb_strength * idz_di_damper_spring_ratio ) / idz_di_ffb_scale); + LONG min_baseline = (LONG)(DI_FFNOMINALMAX * (idz_di_base_damper / 100.0)); + + if (nominal < min_baseline) { + nominal = min_baseline; + } + + cond.lPositiveCoefficient = nominal; + cond.lNegativeCoefficient = nominal; + cond.dwPositiveSaturation = DI_FFNOMINALMAX; + cond.dwNegativeSaturation = DI_FFNOMINALMAX; + + memset(&fx, 0, sizeof(fx)); + fx.dwSize = sizeof(fx); + fx.dwFlags = DIEFF_CARTESIAN | DIEFF_OBJECTOFFSETS; + fx.dwDuration = INFINITE; + fx.dwGain = DI_FFNOMINALMAX; + fx.dwTriggerButton = DIEB_NOTRIGGER; + fx.dwTriggerRepeatInterval = INFINITE; + fx.cAxes = 1; + fx.rgdwAxes = &axis; + fx.rglDirection = &direction; + fx.cbTypeSpecificParams = sizeof(cond); + fx.lpvTypeSpecificParams = &cond; + + update_or_create_effect(&idz_di_fx_damper, &GUID_Damper, &fx); +} + +static void update_or_create_effect(LPDIRECTINPUTEFFECT* effect_ptr, REFGUID guid, DIEFFECT* fx) { + HRESULT hr; + + if (*effect_ptr != NULL) { + hr = IDirectInputEffect_SetParameters(*effect_ptr, fx, DIEP_TYPESPECIFICPARAMS); if (SUCCEEDED(hr)) { return; - } - else { - IDirectInputEffect_Stop(idz_di_fx_damper); - IDirectInputEffect_Release(idz_di_fx_damper); - idz_di_fx_damper = NULL; + } else { + IDirectInputEffect_Stop(*effect_ptr); + IDirectInputEffect_Release(*effect_ptr); + *effect_ptr = NULL; } } - /* Create a new damper effect */ - IDirectInputEffect *obj; - hr = IDirectInputDevice8_CreateEffect( - idz_di_dev, - &GUID_Damper, - &fx, - &obj, - NULL); - + IDirectInputEffect* obj = NULL; + hr = IDirectInputDevice8_CreateEffect(idz_di_dev, guid, fx, &obj, NULL); if (FAILED(hr)) { return; } - /* Start the effect */ - hr = IDirectInputEffect_Start(obj, fx.dwDuration, 0); + hr = IDirectInputEffect_Start(obj, fx->dwDuration, 0); if (FAILED(hr)) { IDirectInputEffect_Release(obj); return; } - idz_di_fx_damper = obj; + *effect_ptr = obj; } diff --git a/games/swdcio/config.c b/games/swdcio/config.c index 9bf45a8..aedc0cc 100644 --- a/games/swdcio/config.c +++ b/games/swdcio/config.c @@ -18,35 +18,32 @@ void swdc_di_config_load(struct swdc_di_config *cfg, const wchar_t *filename) GetPrivateProfileStringW( L"dinput", - L"deviceName", - L"", - cfg->device_name, - _countof(cfg->device_name), - filename); - + L"deviceName", + L"", + cfg->device_name, + _countof(cfg->device_name), + filename); GetPrivateProfileStringW( - L"dinput", - L"pedalsName", - L"", - cfg->pedals_name, - _countof(cfg->pedals_name), - filename); - + L"dinput", + L"pedalsName", + L"", + cfg->pedals_name, + _countof(cfg->pedals_name), + filename); GetPrivateProfileStringW( - L"dinput", - L"brakeAxis", - L"RZ", - cfg->brake_axis, - _countof(cfg->brake_axis), - filename); - + L"dinput", + L"brakeAxis", + L"RZ", + cfg->brake_axis, + _countof(cfg->brake_axis), + filename); GetPrivateProfileStringW( - L"dinput", - L"accelAxis", - L"Y", - cfg->accel_axis, - _countof(cfg->accel_axis), - filename); + L"dinput", + L"accelAxis", + L"Y", + cfg->accel_axis, + _countof(cfg->accel_axis), + filename); cfg->start = GetPrivateProfileIntW(L"dinput", L"start", 0, filename); cfg->view_chg = GetPrivateProfileIntW(L"dinput", L"viewChg", 0, filename); @@ -62,15 +59,15 @@ void swdc_di_config_load(struct swdc_di_config *cfg, const wchar_t *filename) cfg->wheel_yellow = GetPrivateProfileIntW(L"dinput", L"wheelYellow", 0, filename); cfg->reverse_brake_axis = GetPrivateProfileIntW( - L"dinput", - L"reverseBrakeAxis", - 0, - filename); + L"dinput", + L"reverseBrakeAxis", + 0, + filename); cfg->reverse_accel_axis = GetPrivateProfileIntW( - L"dinput", - L"reverseAccelAxis", - 0, - filename); + L"dinput", + L"reverseAccelAxis", + 0, + filename); // FFB configuration cfg->ffb_constant_force_strength = GetPrivateProfileIntW( @@ -96,6 +93,18 @@ void swdc_di_config_load(struct swdc_di_config *cfg, const wchar_t *filename) L"rumbleDuration", 1000, filename); + + cfg->ffb_base_damper_fraction = GetPrivateProfileIntW( + L"dinput", + L"baseDamperFraction", + 20, + filename); + + cfg->ffb_deadband = GetPrivateProfileIntW( + L"dinput", + L"deadband", + 2, + filename); } void swdc_xi_config_load(struct swdc_xi_config *cfg, const wchar_t *filename) @@ -114,13 +123,13 @@ void swdc_xi_config_load(struct swdc_xi_config *cfg, const wchar_t *filename) L"linearSteering", 0, filename); - + cfg->left_stick_deadzone = GetPrivateProfileIntW( - L"xinput", - L"leftStickDeadzone", - 7849, - filename); - + L"xinput", + L"leftStickDeadzone", + 7849, + filename); + cfg->right_stick_deadzone = GetPrivateProfileIntW( L"xinput", L"rightStickDeadzone", @@ -133,18 +142,34 @@ void swdc_io_config_load(struct swdc_io_config *cfg, const wchar_t *filename) assert(cfg != NULL); assert(filename != NULL); - cfg->vk_test = GetPrivateProfileIntW(L"io4", L"test", VK_F1, filename); - cfg->vk_service = GetPrivateProfileIntW(L"io4", L"service", VK_F2, filename); - cfg->vk_coin = GetPrivateProfileIntW(L"io4", L"coin", VK_F3, filename); - cfg->restrict_ = GetPrivateProfileIntW(L"io4", L"restrict", 128, filename); + cfg->vk_test = GetPrivateProfileIntW( + L"io4", + L"test", + VK_F1, + filename); + cfg->vk_service = GetPrivateProfileIntW( + L"io4", + L"service", + VK_F2, + filename); + cfg->vk_coin = GetPrivateProfileIntW( + L"io4", + L"coin", + VK_F3, + filename); + cfg->restrict_ = GetPrivateProfileIntW( + L"io4", + L"restrict", + 128, + filename); GetPrivateProfileStringW( - L"io4", - L"mode", - L"xinput", - cfg->mode, - _countof(cfg->mode), - filename); + L"io4", + L"mode", + L"xinput", + cfg->mode, + _countof(cfg->mode), + filename); swdc_di_config_load(&cfg->di, filename); swdc_xi_config_load(&cfg->xi, filename); diff --git a/games/swdcio/config.h b/games/swdcio/config.h index c4e2dc3..26b3157 100644 --- a/games/swdcio/config.h +++ b/games/swdcio/config.h @@ -28,7 +28,8 @@ struct swdc_di_config { uint8_t ffb_constant_force_strength; uint8_t ffb_rumble_strength; uint8_t ffb_damper_strength; - + uint8_t ffb_deadband; + uint8_t ffb_base_damper_fraction; uint32_t ffb_rumble_duration; }; diff --git a/games/swdcio/di-dev.c b/games/swdcio/di-dev.c index 4f099cc..e913927 100644 --- a/games/swdcio/di-dev.c +++ b/games/swdcio/di-dev.c @@ -1,46 +1,102 @@ -#include +#include "swdcio/di-dev.h" + +#include #include #include -#include - -#include "swdcio/di-dev.h" +#include #include "util/dprintf.h" -const struct swdc_di_config *swdc_di_cfg; -static HWND swdc_di_wnd; -static IDirectInputDevice8W *swdc_di_dev; +/* Globals */ +static HWND swdc_di_wnd = NULL; +static IDirectInputDevice8W* swdc_di_dev = NULL; + +static void update_or_create_effect(LPDIRECTINPUTEFFECT* effect_ptr, + REFGUID guid, DIEFFECT* fx); /* Individual DI Effects */ -static IDirectInputEffect *swdc_di_fx; -static IDirectInputEffect *swdc_di_fx_rumble; -static IDirectInputEffect *swdc_di_fx_damper; +static IDirectInputEffect* swdc_di_fx_constant = NULL; +static IDirectInputEffect* swdc_di_fx_rumble = NULL; +static IDirectInputEffect* swdc_di_fx_spring = NULL; +static IDirectInputEffect* swdc_di_fx_damper = NULL; /* Max FFB Board value is 127 */ static const double swdc_di_ffb_scale = 127.0; +static const double swdc_di_damper_spring_ratio = 0.5; -HRESULT swdc_di_dev_init( - const struct swdc_di_config *cfg, - IDirectInputDevice8W *dev, - HWND wnd) -{ +/* Clamped config settings */ +static uint8_t swdc_di_constant_force_strength; +static uint8_t swdc_di_rumble_strength; +static uint8_t swdc_di_damper_strength; +static uint8_t swdc_di_rumble_duration; +static uint8_t swdc_di_base_damper; +static uint8_t swdc_di_deadband; + +HRESULT swdc_di_dev_init(const struct swdc_di_config* cfg, + IDirectInputDevice8W* dev, HWND wnd) { + assert(cfg != NULL); + assert(dev != NULL); + assert(wnd != NULL); + + swdc_di_dev = dev; + swdc_di_wnd = wnd; + + swdc_di_rumble_duration = cfg->ffb_rumble_duration; + + swdc_di_constant_force_strength = cfg->ffb_constant_force_strength > 100 + ? 100 + : cfg->ffb_constant_force_strength; + + swdc_di_rumble_strength = cfg->ffb_rumble_strength > 100 + ? 100 + : cfg->ffb_rumble_strength; + + swdc_di_damper_strength = cfg->ffb_damper_strength > 100 + ? 100 + : cfg->ffb_damper_strength; + + swdc_di_base_damper = cfg->ffb_base_damper_fraction > 100 + ? 100 + : cfg->ffb_base_damper_fraction; + + /* Deadband is the only setting from 0.0% to 20.0%*/ + swdc_di_deadband = cfg->ffb_deadband > 200 + ? 200 + : cfg->ffb_deadband; + + return S_OK; +} + +HRESULT swdc_di_dev_start(IDirectInputDevice8W* dev, HWND wnd) { HRESULT hr; assert(dev != NULL); assert(wnd != NULL); - swdc_di_cfg = cfg; - swdc_di_dev = dev; - swdc_di_wnd = wnd; + hr = IDirectInputDevice8_SetCooperativeLevel( + dev, wnd, DISCL_BACKGROUND | DISCL_EXCLUSIVE); + if (FAILED(hr)) { + dprintf("DirectInput: SetCooperativeLevel failed: %08x\n", (int)hr); + return hr; + } + + hr = IDirectInputDevice8_SetDataFormat(dev, &c_dfDIJoystick); + if (FAILED(hr)) { + dprintf("DirectInput: SetDataFormat failed: %08x\n", (int)hr); + return hr; + } + + hr = IDirectInputDevice8_Acquire(dev); + if (FAILED(hr)) { + dprintf("DirectInput: Acquire failed: %08x\n", (int)hr); + return hr; + } return S_OK; } -HRESULT swdc_di_dev_poll( - IDirectInputDevice8W *dev, - HWND wnd, - union swdc_di_state *out) -{ +HRESULT swdc_di_dev_poll(IDirectInputDevice8W* dev, HWND wnd, + union swdc_di_state* out) { HRESULT hr; MSG msg; @@ -50,19 +106,12 @@ HRESULT swdc_di_dev_poll( memset(out, 0, sizeof(*out)); - /* Pump our dummy window's message queue just in case DirectInput or an - IHV DirectInput driver somehow relies on it */ - while (PeekMessageW(&msg, wnd, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } - hr = IDirectInputDevice8_GetDeviceState( - dev, - sizeof(out->st), - &out->st); - + hr = IDirectInputDevice8_GetDeviceState(dev, sizeof(out->st), &out->st); if (FAILED(hr)) { dprintf("DirectInput: GetDeviceState error: %08x\n", (int)hr); } @@ -70,100 +119,56 @@ HRESULT swdc_di_dev_poll( return hr; } -HRESULT swdc_di_dev_start(IDirectInputDevice8W *dev, HWND wnd) { - HRESULT hr; - - assert(dev != NULL); - assert(wnd != NULL); - - hr = IDirectInputDevice8_SetCooperativeLevel( - dev, - wnd, - DISCL_BACKGROUND | DISCL_EXCLUSIVE); - - if (FAILED(hr)) { - dprintf("DirectInput: SetCooperativeLevel failed: %08x\n", (int)hr); - return hr; +HRESULT swdc_di_ffb_init(void) { + if (!swdc_di_dev || !swdc_di_wnd) { + return E_FAIL; } - hr = IDirectInputDevice8_SetDataFormat(dev, &c_dfDIJoystick); - - if (FAILED(hr)) { - dprintf("DirectInput: SetDataFormat failed: %08x\n", (int)hr); - return hr; - } - - hr = IDirectInputDevice8_Acquire(dev); - - if (FAILED(hr)) { - dprintf("DirectInput: Acquire failed: %08x\n", (int)hr); - return hr; - } - - return hr; + return swdc_di_dev_start(swdc_di_dev, swdc_di_wnd); } -HRESULT swdc_di_ffb_init(void) -{ - HRESULT hr; - - hr = swdc_di_dev_start(swdc_di_dev, swdc_di_wnd); - - if (FAILED(hr)) { - return hr; - } - - return S_OK; -} - -void swdc_di_ffb_toggle(bool active) -{ +void swdc_di_ffb_toggle(bool active) { if (active) { return; } - /* Stop and release all effects */ - /* I never programmed DirectInput Effects, so this might be bad practice. */ - if (swdc_di_fx != NULL) { - IDirectInputEffect_Stop(swdc_di_fx); - IDirectInputEffect_Release(swdc_di_fx); - swdc_di_fx = NULL; + if (swdc_di_fx_constant) { + IDirectInputEffect_Stop(swdc_di_fx_constant); + IDirectInputEffect_Release(swdc_di_fx_constant); + swdc_di_fx_constant = NULL; } - - if (swdc_di_fx_rumble != NULL) { + if (swdc_di_fx_rumble) { IDirectInputEffect_Stop(swdc_di_fx_rumble); IDirectInputEffect_Release(swdc_di_fx_rumble); swdc_di_fx_rumble = NULL; } - - if (swdc_di_fx_damper != NULL) { + if (swdc_di_fx_spring) { + IDirectInputEffect_Stop(swdc_di_fx_spring); + IDirectInputEffect_Release(swdc_di_fx_spring); + swdc_di_fx_spring = NULL; + } + if (swdc_di_fx_damper) { IDirectInputEffect_Stop(swdc_di_fx_damper); IDirectInputEffect_Release(swdc_di_fx_damper); swdc_di_fx_damper = NULL; } } -void swdc_di_ffb_constant_force(uint8_t direction_ffb, uint8_t force) -{ - /* DI expects a magnitude in the range of -10.000 to 10.000 */ - uint16_t ffb_strength = swdc_di_cfg->ffb_constant_force_strength * 100; +void swdc_di_ffb_constant_force(uint8_t direction_ffb, uint8_t force) { + uint16_t ffb_strength = swdc_di_constant_force_strength * 100; if (ffb_strength == 0) { return; } - DWORD axis; - LONG direction; + DWORD axis = DIJOFS_X; + LONG direction = 0; DIEFFECT fx; DICONSTANTFORCE cf; HRESULT hr; - /* Direction 0: move to the right, 1: move to the left */ LONG magnitude = (LONG)(((double)force / swdc_di_ffb_scale) * ffb_strength); cf.lMagnitude = (direction_ffb == 0) ? -magnitude : magnitude; - axis = DIJOFS_X; - direction = 0; - memset(&fx, 0, sizeof(fx)); fx.dwSize = sizeof(fx); fx.dwFlags = DIEFF_CARTESIAN | DIEFF_OBJECTOFFSETS; @@ -177,71 +182,24 @@ void swdc_di_ffb_constant_force(uint8_t direction_ffb, uint8_t force) fx.cbTypeSpecificParams = sizeof(cf); fx.lpvTypeSpecificParams = &cf; - /* Check if the effect already exists */ - if (swdc_di_fx != NULL) { - hr = IDirectInputEffect_SetParameters(swdc_di_fx, &fx, DIEP_TYPESPECIFICPARAMS); - if (SUCCEEDED(hr)) { - return; // Successfully updated existing effect - } - else { - dprintf("DirectInput: Failed to update constant force feedback, recreating effect: %08x\n", (int)hr); - IDirectInputEffect_Stop(swdc_di_fx); - IDirectInputEffect_Release(swdc_di_fx); - swdc_di_fx = NULL; // Reset the pointer - } - } - - /* Create a new constant force effect */ - IDirectInputEffect *obj; - hr = IDirectInputDevice8_CreateEffect( - swdc_di_dev, - &GUID_ConstantForce, - &fx, - &obj, - NULL); - - if (FAILED(hr)) { - dprintf("DirectInput: Constant force feedback creation failed: %08x\n", (int)hr); - return; - } - - /* Start the effect */ - hr = IDirectInputEffect_Start(obj, INFINITE, 0); - if (FAILED(hr)) { - dprintf("DirectInput: Constant force feedback start failed: %08x\n", (int)hr); - IDirectInputEffect_Release(obj); - return; - } - - swdc_di_fx = obj; + update_or_create_effect(&swdc_di_fx_constant, &GUID_ConstantForce, &fx); } -void swdc_di_ffb_rumble(uint8_t force, uint8_t period) -{ - /* DI expects a magnitude in the range of -10.000 to 10.000 */ - uint16_t ffb_strength = swdc_di_cfg->ffb_rumble_strength * 100; +void swdc_di_ffb_rumble(uint8_t force, uint8_t period) { + uint16_t ffb_strength = swdc_di_rumble_strength * 100; if (ffb_strength == 0) { return; } - uint32_t ffb_duration = swdc_di_cfg->ffb_rumble_duration; - - DWORD axis; - LONG direction; + DWORD axis = DIJOFS_X; + LONG direction = 0; DIEFFECT fx; DIPERIODIC pe; HRESULT hr; - DWORD duration = (DWORD)((double)force * ffb_duration); - memset(&pe, 0, sizeof(pe)); pe.dwMagnitude = (DWORD)(((double)force / swdc_di_ffb_scale) * ffb_strength); - pe.lOffset = 0; - pe.dwPhase = 0; - pe.dwPeriod = duration; - - axis = DIJOFS_X; - direction = 0; + pe.dwPeriod = (DWORD)force * swdc_di_rumble_duration; memset(&fx, 0, sizeof(fx)); fx.dwSize = sizeof(fx); @@ -256,70 +214,41 @@ void swdc_di_ffb_rumble(uint8_t force, uint8_t period) fx.cbTypeSpecificParams = sizeof(pe); fx.lpvTypeSpecificParams = &pe; - /* Check if the effect already exists */ - if (swdc_di_fx_rumble != NULL) { - hr = IDirectInputEffect_SetParameters(swdc_di_fx_rumble, &fx, DIEP_TYPESPECIFICPARAMS); - if (SUCCEEDED(hr)) { - return; - } - else { - dprintf("DirectInput: Failed to update rumble feedback, recreating effect: %08x\n", (int)hr); - IDirectInputEffect_Stop(swdc_di_fx_rumble); - IDirectInputEffect_Release(swdc_di_fx_rumble); - swdc_di_fx_rumble = NULL; - } - } - - /* Create a new rumble effect */ - IDirectInputEffect *obj; - hr = IDirectInputDevice8_CreateEffect( - swdc_di_dev, - &GUID_Sine, - &fx, - &obj, - NULL); - - if (FAILED(hr)) { - dprintf("DirectInput: Rumble effect creation failed: %08x\n", (int)hr); - return; - } - - /* Start the effect */ - hr = IDirectInputEffect_Start(obj, INFINITE, 0); - if (FAILED(hr)) { - dprintf("DirectInput: Rumble effect start failed: %08x\n", (int)hr); - IDirectInputEffect_Release(obj); - return; - } - - swdc_di_fx_rumble = obj; + update_or_create_effect(&swdc_di_fx_rumble, &GUID_Sine, &fx); } -void swdc_di_ffb_damper(uint8_t force) -{ - /* DI expects a coefficient in the range of -10.000 to 10.000 */ - uint16_t ffb_strength = swdc_di_cfg->ffb_damper_strength * 100; +void swdc_di_ffb_damper(uint8_t force) { + HRESULT hr; + uint16_t ffb_strength = swdc_di_damper_strength * 100; + if (ffb_strength == 0) { + if (swdc_di_fx_spring) { + IDirectInputEffect_Stop(swdc_di_fx_spring); + IDirectInputEffect_Release(swdc_di_fx_spring); + swdc_di_fx_spring = NULL; + } + if (swdc_di_fx_damper) { + IDirectInputEffect_Stop(swdc_di_fx_damper); + IDirectInputEffect_Release(swdc_di_fx_damper); + swdc_di_fx_damper = NULL; + } return; } - DWORD axis; - LONG direction; + DWORD axis = DIJOFS_X; + LONG direction = 0; DIEFFECT fx; DICONDITION cond; - HRESULT hr; + /* SPRING (centering) */ memset(&cond, 0, sizeof(cond)); - cond.lOffset = 0; - cond.lPositiveCoefficient = (LONG)(((double)force / swdc_di_ffb_scale) * ffb_strength); - cond.lNegativeCoefficient = (LONG)(((double)force / swdc_di_ffb_scale) * ffb_strength); - /* Not sure on this one */ + cond.lPositiveCoefficient = (LONG)(((uint32_t)force * ffb_strength) / swdc_di_ffb_scale); + cond.lNegativeCoefficient = cond.lPositiveCoefficient; cond.dwPositiveSaturation = DI_FFNOMINALMAX; cond.dwNegativeSaturation = DI_FFNOMINALMAX; - cond.lDeadBand = 0; - - axis = DIJOFS_X; - direction = 0; + + /* If user enters 25, result is 0.025 * DI_FFNOMINALMAX */ + cond.lDeadBand = (DI_FFNOMINALMAX * (LONG)swdc_di_deadband) / 1000; memset(&fx, 0, sizeof(fx)); fx.dwSize = sizeof(fx); @@ -334,38 +263,66 @@ void swdc_di_ffb_damper(uint8_t force) fx.cbTypeSpecificParams = sizeof(cond); fx.lpvTypeSpecificParams = &cond; - /* Check if the damper effect already exists */ - if (swdc_di_fx_damper != NULL) { - hr = IDirectInputEffect_SetParameters(swdc_di_fx_damper, &fx, DIEP_TYPESPECIFICPARAMS); + update_or_create_effect(&swdc_di_fx_spring, &GUID_Spring, &fx); + + /* DAMPER (resistance with baseline) */ + memset(&cond, 0, sizeof(cond)); + + LONG nominal = (LONG)(( (LONG)force * ffb_strength * swdc_di_damper_spring_ratio ) / swdc_di_ffb_scale); + LONG min_baseline = (LONG)(DI_FFNOMINALMAX * (swdc_di_base_damper / 100.0)); + + if (nominal < min_baseline) { + nominal = min_baseline; + } + + cond.lPositiveCoefficient = nominal; + cond.lNegativeCoefficient = nominal; + cond.dwPositiveSaturation = DI_FFNOMINALMAX; + cond.dwNegativeSaturation = DI_FFNOMINALMAX; + + memset(&fx, 0, sizeof(fx)); + fx.dwSize = sizeof(fx); + fx.dwFlags = DIEFF_CARTESIAN | DIEFF_OBJECTOFFSETS; + fx.dwDuration = INFINITE; + fx.dwGain = DI_FFNOMINALMAX; + fx.dwTriggerButton = DIEB_NOTRIGGER; + fx.dwTriggerRepeatInterval = INFINITE; + fx.cAxes = 1; + fx.rgdwAxes = &axis; + fx.rglDirection = &direction; + fx.cbTypeSpecificParams = sizeof(cond); + fx.lpvTypeSpecificParams = &cond; + + update_or_create_effect(&swdc_di_fx_damper, &GUID_Damper, &fx); +} + +static void update_or_create_effect(LPDIRECTINPUTEFFECT* effect_ptr, + REFGUID guid, DIEFFECT* fx) { + HRESULT hr; + + if (*effect_ptr != NULL) { + hr = IDirectInputEffect_SetParameters(*effect_ptr, fx, + DIEP_TYPESPECIFICPARAMS); if (SUCCEEDED(hr)) { return; - } - else { - IDirectInputEffect_Stop(swdc_di_fx_damper); - IDirectInputEffect_Release(swdc_di_fx_damper); - swdc_di_fx_damper = NULL; + } else { + IDirectInputEffect_Stop(*effect_ptr); + IDirectInputEffect_Release(*effect_ptr); + *effect_ptr = NULL; } } - /* Create a new damper effect */ - IDirectInputEffect *obj; - hr = IDirectInputDevice8_CreateEffect( - swdc_di_dev, - &GUID_Damper, - &fx, - &obj, - NULL); - + IDirectInputEffect* obj = NULL; + hr = IDirectInputDevice8_CreateEffect(swdc_di_dev, guid, fx, &obj, NULL); if (FAILED(hr)) { return; } - /* Start the effect */ - hr = IDirectInputEffect_Start(obj, fx.dwDuration, 0); + hr = IDirectInputEffect_Start(obj, fx->dwDuration, 0); if (FAILED(hr)) { IDirectInputEffect_Release(obj); return; } - swdc_di_fx_damper = obj; + *effect_ptr = obj; } From a2bfd840d6ae6c51ed38a9f37ab5f4acaa4ad4e2 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Fri, 9 Jan 2026 15:34:30 +0100 Subject: [PATCH 18/37] idac: improve DNS hooking --- common/hooklib/dns.c | 254 ++++++++++++++++++++++++++++++++++++++++ common/platform/dns.c | 6 + dist/idac/segatools.ini | 4 + 3 files changed, 264 insertions(+) diff --git a/common/hooklib/dns.c b/common/hooklib/dns.c index 9e06e5b..bfde652 100644 --- a/common/hooklib/dns.c +++ b/common/hooklib/dns.c @@ -71,6 +71,36 @@ static int WSAAPI hook_getaddrinfo( const ADDRINFOA *pHints, ADDRINFOA **ppResult); +static int WSAAPI hook_GetAddrInfoW( + const wchar_t *pNodeName, + const wchar_t *pServiceName, + const ADDRINFOW *pHints, + PADDRINFOW *ppResult); + +static int WSAAPI hook_GetAddrInfoExA( + const char *pName, + const char *pServiceName, + DWORD dwNameSpace, + LPGUID lpNspId, + const ADDRINFOEXW *hints, + PADDRINFOEXA *ppResult, + struct timeval *timeout, + LPOVERLAPPED lpOverlapped, + LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, + LPHANDLE lpHandle); + +static int WSAAPI hook_GetAddrInfoExW( + const wchar_t *pName, + const wchar_t *pServiceName, + DWORD dwNameSpace, + LPGUID lpNspId, + const ADDRINFOEXW *hints, + PADDRINFOEXW *ppResult, + struct timeval *timeout, + LPOVERLAPPED lpOverlapped, + LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, + LPHANDLE lpHandle); + static HINTERNET WINAPI hook_WinHttpConnect( HINTERNET hSession, const wchar_t *pwszServerName, @@ -123,6 +153,36 @@ static int (WSAAPI *next_getaddrinfo)( const ADDRINFOA *pHints, ADDRINFOA **ppResult); +static int (WSAAPI *next_GetAddrInfoW)( + const wchar_t *pNodeName, + const wchar_t *pServiceName, + const ADDRINFOW *pHints, + PADDRINFOW *ppResult); + +static int (WSAAPI *next_GetAddrInfoExA)( + const char *pName, + const char *pServiceName, + DWORD dwNameSpace, + LPGUID lpNspId, + const ADDRINFOEXW *hints, + PADDRINFOEXA *ppResult, + struct timeval *timeout, + LPOVERLAPPED lpOverlapped, + LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, + LPHANDLE lpHandle); + +static int (WSAAPI *next_GetAddrInfoExW)( + const wchar_t *pName, + const wchar_t *pServiceName, + DWORD dwNameSpace, + LPGUID lpNspId, + const ADDRINFOEXW *hints, + PADDRINFOEXW *ppResult, + struct timeval *timeout, + LPOVERLAPPED lpOverlapped, + LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, + LPHANDLE lpHandle); + static HINTERNET (WINAPI *next_WinHttpConnect)( HINTERNET hSession, const wchar_t *pwszServerName, @@ -169,6 +229,21 @@ static const struct hook_symbol dns_hook_syms_ws2[] = { .patch = hook_getaddrinfo, .link = (void **) &next_getaddrinfo, }, + { + .name = "GetAddrInfoW", + .patch = hook_GetAddrInfoW, + .link = (void **) &next_GetAddrInfoW, + }, + { + .name = "GetAddrInfoExA", + .patch = hook_GetAddrInfoExA, + .link = (void **) &next_GetAddrInfoExA, + }, + { + .name = "GetAddrInfoExW", + .patch = hook_GetAddrInfoExW, + .link = (void **) &next_GetAddrInfoExW, + } }; static const struct hook_symbol dns_hook_syms_winhttp[] = { @@ -612,6 +687,185 @@ end: return result; } +static int WSAAPI hook_GetAddrInfoW( + const wchar_t *pNodeName, + const wchar_t *pServiceName, + const ADDRINFOW *pHints, + ADDRINFOW **ppResult) +{ + const struct dns_hook_entry *pos; + int result; + size_t i; + + if (pNodeName == NULL) { + result = WSA_INVALID_PARAMETER; + + goto end; + } + + EnterCriticalSection(&dns_hook_lock); + + for (i = 0 ; i < dns_hook_nentries ; i++) { + pos = &dns_hook_entries[i]; + + if (match_domain(pNodeName, pos->from)) { + if(pos->to == NULL) { + LeaveCriticalSection(&dns_hook_lock); + result = EAI_NONAME; + + goto end; + } + + // dprintf("GetAddrInfoW: %ls -> %ls\n", pNodeName, pos->to); + + pNodeName = pos->to; + + break; + } + } + + LeaveCriticalSection(&dns_hook_lock); + + result = next_GetAddrInfoW(pNodeName, pServiceName, pHints, ppResult); + +end: + return result; +} + +static int WSAAPI hook_GetAddrInfoExA( + const char *pName, + const char *pServiceName, + DWORD dwNameSpace, + LPGUID lpNspId, + const ADDRINFOEXW *hints, + PADDRINFOEXA *ppResult, + struct timeval *timeout, + LPOVERLAPPED lpOverlapped, + LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, + LPHANDLE lpHandle) +{ + const struct dns_hook_entry *pos; + char *str; + size_t str_c; + wchar_t *wstr; + size_t wstr_c; + int result; + size_t i; + + str = NULL; + wstr = NULL; + + if (pName == NULL) { + result = WSA_INVALID_PARAMETER; + + goto end; + } + + mbstowcs_s(&wstr_c, NULL, 0, pName, 0); + wstr = malloc(wstr_c * sizeof(wchar_t)); + + if (wstr == NULL) { + result = WSA_NOT_ENOUGH_MEMORY; + + goto end; + } + + mbstowcs_s(NULL, wstr, wstr_c, pName, wstr_c - 1); + + EnterCriticalSection(&dns_hook_lock); + + for (i = 0; i < dns_hook_nentries; i++) { + pos = &dns_hook_entries[i]; + + if (match_domain(wstr, pos->from)) { + if (pos->to == NULL) { + LeaveCriticalSection(&dns_hook_lock); + result = WSAHOST_NOT_FOUND; + goto end; + } + + // dprintf("GetAddrInfoExA: %ls -> %ls\n", pName, pos->to); + + wcstombs_s(&str_c, NULL, 0, pos->to, 0); + str = malloc(str_c * sizeof(char)); + + if (str == NULL) { + LeaveCriticalSection(&dns_hook_lock); + result = WSA_NOT_ENOUGH_MEMORY; + + goto end; + } + + wcstombs_s(NULL, str, str_c, pos->to, str_c - 1); + pName = str; + + break; + } + } + + LeaveCriticalSection(&dns_hook_lock); + + result = next_GetAddrInfoExA(pName, pServiceName, dwNameSpace, lpNspId, + hints, ppResult, timeout, lpOverlapped, + lpCompletionRoutine, lpHandle); + +end: + free(wstr); + free(str); + + return result; +} + +static int WSAAPI hook_GetAddrInfoExW( + const wchar_t *pName, + const wchar_t *pServiceName, + DWORD dwNameSpace, + LPGUID lpNspId, + const ADDRINFOEXW *hints, + PADDRINFOEXW *ppResult, + struct timeval *timeout, + LPOVERLAPPED lpOverlapped, + LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, + LPHANDLE lpHandle) +{ + const struct dns_hook_entry *pos; + int result; + size_t i; + + if (pName == NULL) { + result = WSA_INVALID_PARAMETER; + goto end; + } + + EnterCriticalSection(&dns_hook_lock); + + for (i = 0; i < dns_hook_nentries; i++) { + pos = &dns_hook_entries[i]; + + if (match_domain(pName, pos->from)) { + if (pos->to == NULL) { + LeaveCriticalSection(&dns_hook_lock); + result = WSAHOST_NOT_FOUND; + goto end; + } + + // dprintf("GetAddrInfoExW: %ls -> %ls\n", pName, pos->to); + + pName = pos->to; + break; + } + } + + LeaveCriticalSection(&dns_hook_lock); + + result = next_GetAddrInfoExW(pName, pServiceName, dwNameSpace, lpNspId, + hints, ppResult, timeout, lpOverlapped, + lpCompletionRoutine, lpHandle); + +end: + return result; +} + static HINTERNET WINAPI hook_WinHttpConnect( HINTERNET hSession, const wchar_t *pwszServerName, diff --git a/common/platform/dns.c b/common/platform/dns.c index f5745f0..c10e0d0 100644 --- a/common/platform/dns.c +++ b/common/platform/dns.c @@ -90,6 +90,12 @@ HRESULT dns_platform_hook_init(const struct dns_config *cfg) return hr; } + hr = dns_hook_push(L"sega-initiald.net", cfg->startup); + + if (FAILED(hr)) { + return hr; + } + // crossbeats REV. hr = dns_hook_push(L"https://rev-ent.ac.capcom.jp:443", cfg->title); diff --git a/dist/idac/segatools.ini b/dist/idac/segatools.ini index 7cf57fc..ca3e211 100644 --- a/dist/idac/segatools.ini +++ b/dist/idac/segatools.ini @@ -36,6 +36,10 @@ scan=0x0D ; Note that 127.0.0.1, localhost etc are specifically rejected. default=127.0.0.1 +; Overrides the target of the `tenporouter.loc` and `bbrouter.loc` hostname +; lookups. Season 3 needs a tenporouter.loc address of 10.x.y.254. +router=10.0.0.254 + [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 From 2475b48ef30a19fa0725169f368a353e04f99321 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Fri, 9 Jan 2026 15:36:10 +0100 Subject: [PATCH 19/37] misc: fix foreground window issues, enable by default --- common/board/config.c | 2 +- common/platform/ewf.h | 1 - common/platform/meson.build | 1 - common/platform/platform.h | 2 +- games/idachook/io4.c | 2 +- games/swdchook/io4.c | 2 +- 6 files changed, 4 insertions(+), 6 deletions(-) diff --git a/common/board/config.c b/common/board/config.c index be98ebe..a36d7c2 100644 --- a/common/board/config.c +++ b/common/board/config.c @@ -92,7 +92,7 @@ void io4_config_load(struct io4_config *cfg, const wchar_t *filename) assert(filename != NULL); cfg->enable = GetPrivateProfileIntW(L"io4", L"enable", 1, filename); - cfg->foreground_only = GetPrivateProfileIntW(L"io4", L"foreground", 0, filename); + cfg->foreground_only = GetPrivateProfileIntW(L"io4", L"foreground", 1, filename); } void vfd_config_load(struct vfd_config *cfg, const wchar_t *filename) diff --git a/common/platform/ewf.h b/common/platform/ewf.h index 5ab71f5..958a88d 100644 --- a/common/platform/ewf.h +++ b/common/platform/ewf.h @@ -3,7 +3,6 @@ #include #include -#include #define EWF_MAX_VIRTUAL_FILES 255 #define EWF_DEFAULT_FILE_BUFFER_SIZE 1024 diff --git a/common/platform/meson.build b/common/platform/meson.build index 8f90208..331ea0e 100644 --- a/common/platform/meson.build +++ b/common/platform/meson.build @@ -4,7 +4,6 @@ platform_lib = static_library( implicit_include_directories : false, dependencies : [ capnhook.get_variable('hook_dep'), - capnhook.get_variable('hooklib_dep'), shlwapi_lib, bcrypt_lib, ], diff --git a/common/platform/platform.h b/common/platform/platform.h index 0f01eaf..76e639e 100644 --- a/common/platform/platform.h +++ b/common/platform/platform.h @@ -2,7 +2,7 @@ #include -#include "ewf.h" +#include "platform/ewf.h" #include "platform/amvideo.h" #include "platform/clock.h" #include "platform/dns.h" diff --git a/games/idachook/io4.c b/games/idachook/io4.c index fa4f82b..a803600 100644 --- a/games/idachook/io4.c +++ b/games/idachook/io4.c @@ -43,7 +43,7 @@ HRESULT idac_io4_hook_init(const struct io4_config* cfg) { assert(idac_dll.init != NULL); - hr = io4_hook_init(cfg, &idac_io4_ops, NULL, L"GameProject", false); + hr = io4_hook_init(cfg, &idac_io4_ops, NULL, L"GameProject", true); if (FAILED(hr)) { return hr; diff --git a/games/swdchook/io4.c b/games/swdchook/io4.c index 1d3ffcf..2f5381a 100644 --- a/games/swdchook/io4.c +++ b/games/swdchook/io4.c @@ -32,7 +32,7 @@ HRESULT swdc_io4_hook_init(const struct io4_config* cfg) { assert(swdc_dll.init != NULL); - hr = io4_hook_init(cfg, &swdc_io4_ops, NULL, L"Todoroki", false); + hr = io4_hook_init(cfg, &swdc_io4_ops, NULL, L"Todoroki", true); if (FAILED(hr)) { return hr; From b9233207f5ba85ec212e6a5c494e987e615237e9 Mon Sep 17 00:00:00 2001 From: zaphkito Date: Tue, 13 Jan 2026 06:57:52 +0000 Subject: [PATCH 20/37] mai2io: change 2p select button default key 0x54 is "T", "*" should be VK_MULTIPLY aka 0X6A tested work --- games/mai2io/config.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/games/mai2io/config.c b/games/mai2io/config.c index c6c9913..50992c7 100644 --- a/games/mai2io/config.c +++ b/games/mai2io/config.c @@ -14,7 +14,7 @@ Maimai DX Default key binding 2P: (Numpad) 8, 9, 6, 3, 2, 1, 4, 7, * */ static const int mai2_io_1p_default[] = {'W', 'E', 'D', 'C', 'X', 'Z', 'A', 'Q', '3'}; -static const int mai2_io_2p_default[] = {0x68, 0x69, 0x66, 0x63, 0x62, 0x61, 0x64, 0x67, 0x54}; +static const int mai2_io_2p_default[] = {0x68, 0x69, 0x66, 0x63, 0x62, 0x61, 0x64, 0x67, 0x6A}; static const int mai2_io_1p_touch_default[] = { 'T', 'Y', 'H', 'N', 'B', 'V', 'F', 'R', From 3a8bde7ca8d08c21d4f3520a8242da90a05e5e8d Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Wed, 14 Jan 2026 23:30:00 +0100 Subject: [PATCH 21/37] mercury: fix left touch board, window focus, launch params --- dist/mercury/launch.bat | 6 +-- dist/mercury/segatools.ini | 1 - games/mercuryhook/dllmain.c | 7 ++-- games/mercuryhook/elizabeth.c | 34 +++++++++++------ games/mercuryhook/io4.c | 2 +- games/mercuryhook/touch.c | 69 ++++++++++++++++++++--------------- 6 files changed, 68 insertions(+), 51 deletions(-) diff --git a/dist/mercury/launch.bat b/dist/mercury/launch.bat index 7445a6e..46caed9 100644 --- a/dist/mercury/launch.bat +++ b/dist/mercury/launch.bat @@ -2,14 +2,14 @@ pushd %~dp0 REM USA -REM start "AM Daemon" /min inject -d -k mercuryhook.dll amdaemon.exe -f -c config.json config_lan_install_client.json config_lan_install_server.json config_video_clone.json config_video_dual.json config_video_clone_flip.json config_video_dual_flip.json config_region_exp.json config_region_chn.json config_region_usa.json +REM start "AM Daemon" /min inject -d -k mercuryhook.dll amdaemon.exe -c config.json config_lan_install_client.json config_lan_install_server.json config_video_clone.json config_video_dual.json config_video_clone_flip.json config_video_dual_flip.json config_region_exp.json config_region_chn.json config_region_usa.json REM JP -start "AM Daemon" /min inject -d -k mercuryhook.dll amdaemon.exe -f -c config.json config_lan_install_client.json config_lan_install_server.json config_video_clone.json config_video_dual.json config_video_clone_flip.json config_video_dual_flip.json config_region_exp.json config_region_chn.json config_region_jpn.json +start "AM Daemon" /min inject -d -k mercuryhook.dll amdaemon.exe -c config.json config_lan_install_client.json config_lan_install_server.json config_video_clone.json config_video_dual.json config_video_clone_flip.json config_video_dual_flip.json config_region_exp.json config_region_chn.json config_region_jpn.json inject -d -k mercuryhook.dll ../WindowsNoEditor/Mercury/Binaries/Win64/Mercury-Win64-Shipping.exe taskkill /f /im amdaemon.exe > nul 2>&1 echo. echo Game processes have terminated -pause \ No newline at end of file +pause diff --git a/dist/mercury/segatools.ini b/dist/mercury/segatools.ini index 64e11d0..07fdffb 100644 --- a/dist/mercury/segatools.ini +++ b/dist/mercury/segatools.ini @@ -94,7 +94,6 @@ enable=1 ; Enable DPI awareness for the game process, preventing Windows from stretching the game window if a DPI scaling higher than 100% is used dpiAware=1 - ; Hooks related to the touch boards [touch] enable=1 diff --git a/games/mercuryhook/dllmain.c b/games/mercuryhook/dllmain.c index 11a5527..62261c6 100644 --- a/games/mercuryhook/dllmain.c +++ b/games/mercuryhook/dllmain.c @@ -57,10 +57,9 @@ static DWORD CALLBACK mercury_pre_startup(void) /* Hook Win32 APIs */ dvd_hook_init(&mercury_hook_cfg.dvd, mercury_hook_mod); - serial_hook_init(); - gfx_hook_init(&mercury_hook_cfg.gfx); gfx_d3d11_hook_init(&mercury_hook_cfg.gfx, mercury_hook_mod); + serial_hook_init(); /* Initialize emulation hooks */ @@ -106,9 +105,9 @@ static DWORD CALLBACK mercury_pre_startup(void) goto fail; } - /* Start elisabeth Hooks for the LED and IO Board DLLs */ - elizabeth_hook_init(&mercury_hook_cfg.elizabeth); + /* Start elizabeth hooks for the LED and IO board DLLs */ + elizabeth_hook_init(&mercury_hook_cfg.elizabeth); touch_hook_init(&mercury_hook_cfg.touch); /* Initialize debug helpers */ diff --git a/games/mercuryhook/elizabeth.c b/games/mercuryhook/elizabeth.c index 9fd4aaa..ffc7c26 100644 --- a/games/mercuryhook/elizabeth.c +++ b/games/mercuryhook/elizabeth.c @@ -1,4 +1,3 @@ -#include #include #include @@ -20,15 +19,24 @@ /* Hooks targeted DLLs dynamically loaded by elizabeth. */ static void dll_hook_insert_hooks(HMODULE target); -static FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name); + +/* Hook functions */ + +static FARPROC WINAPI hook_GetProcAddress(HMODULE hModule, const char *name); + +static int hook_USBIntLED_Init(); + +static int hook_USBIntLED_set(int data1, struct led_data data2); + +/* Link pointers */ + static FARPROC (WINAPI *next_GetProcAddress)(HMODULE hModule, const char *name); -static int my_USBIntLED_Init(); -static int my_USBIntLED_set(int data1, struct led_data data2); + static const struct hook_symbol win32_hooks[] = { { .name = "GetProcAddress", - .patch = my_GetProcAddress, + .patch = hook_GetProcAddress, .link = (void **) &next_GetProcAddress } }; @@ -38,8 +46,10 @@ HRESULT elizabeth_hook_init(struct elizabeth_config *cfg) if (!cfg->enable) { return S_OK; } + dll_hook_insert_hooks(NULL); - dprintf("elizabeth: Init\n"); + dprintf("Elizabeth: Init\n"); + return S_OK; } @@ -52,7 +62,7 @@ static void dll_hook_insert_hooks(HMODULE target) _countof(win32_hooks)); } -FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name) +FARPROC WINAPI hook_GetProcAddress(HMODULE hModule, const char *name) { uintptr_t ordinal = (uintptr_t) name; @@ -61,11 +71,11 @@ FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name) if (ordinal > 0xFFFF) { /* Import by name */ if (strcmp(name, "USBIntLED_Init") == 0) { - result = (FARPROC) my_USBIntLED_Init; + result = (FARPROC) hook_USBIntLED_Init; } if (strcmp(name, "USBIntLED_set") == 0) { - result = (FARPROC) my_USBIntLED_set; + result = (FARPROC) hook_USBIntLED_set; } } @@ -73,13 +83,13 @@ FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name) } /* Intercept the call to initialize the LED board. */ -static int my_USBIntLED_Init() +static int hook_USBIntLED_Init() { - dprintf("elizabeth: my_USBIntLED_Init hit!\n"); + dprintf("Elizabeth: hook_USBIntLED_Init hit!\n"); return 1; } -static int my_USBIntLED_set(int data1, struct led_data data2) +static int hook_USBIntLED_set(int data1, struct led_data data2) { assert(mercury_dll.set_leds != NULL); mercury_dll.set_leds(data2); diff --git a/games/mercuryhook/io4.c b/games/mercuryhook/io4.c index c34eb6f..a5bb088 100644 --- a/games/mercuryhook/io4.c +++ b/games/mercuryhook/io4.c @@ -25,7 +25,7 @@ HRESULT mercury_io4_hook_init(const struct io4_config* cfg) { assert(mercury_dll.init != NULL); - hr = io4_hook_init(cfg, &mercury_io4_ops, NULL, L"Mercury", false); + hr = io4_hook_init(cfg, &mercury_io4_ops, NULL, L"Mercury", true); if (FAILED(hr)) { return hr; diff --git a/games/mercuryhook/touch.c b/games/mercuryhook/touch.c index 3f73a2b..89189bb 100644 --- a/games/mercuryhook/touch.c +++ b/games/mercuryhook/touch.c @@ -41,10 +41,10 @@ static HRESULT touch_handle_mystery2(const struct touch_req *req); static HRESULT touch_handle_start_auto_scan(const struct touch_req *req); static void touch_res_auto_scan(const bool *state); -uint8_t input_frame_count_0 = 0x7b; -uint8_t input_frame_count_1 = 0x7b; -bool touch0_auto = false; -bool touch1_auto = false; +static uint8_t input_frame_count_0 = 0; +static uint8_t input_frame_count_1 = 0; +static bool touch0_auto = false; +static bool touch1_auto = false; static CRITICAL_SECTION touch0_lock; static struct uart touch0_uart; @@ -67,7 +67,7 @@ HRESULT touch_hook_init(const struct touch_config *cfg) InitializeCriticalSection(&touch0_lock); InitializeCriticalSection(&touch1_lock); - dprintf("Wacca touch: Init\n"); + dprintf("Wacca Touch: Init\n"); uart_init(&touch0_uart, 3); touch0_uart.written.bytes = touch0_written_bytes; @@ -113,11 +113,11 @@ static HRESULT touch0_handle_irp_locked(struct irp *irp) HRESULT hr; if (irp->op == IRP_OP_OPEN) { - dprintf("Wacca touch0: Starting backend\n"); + dprintf("Wacca Touch0: Starting backend\n"); hr = mercury_dll.touch_init(); if (FAILED(hr)) { - dprintf("Wacca touch: Backend error: %x\n", (int) hr); + dprintf("Wacca Touch: Backend error: %x\n", (int) hr); return hr; } @@ -138,7 +138,7 @@ static HRESULT touch0_handle_irp_locked(struct irp *irp) if (hr != S_OK) { if (FAILED(hr)) { - dprintf("Wacca touch: Deframe error: %x\n", (int) hr); + dprintf("Wacca Touch: Deframe error: %x\n", (int) hr); } return hr; @@ -147,7 +147,7 @@ static HRESULT touch0_handle_irp_locked(struct irp *irp) hr = touch_req_dispatch(&req); if (FAILED(hr)) { - dprintf("Wacca touch: Processing error: %x\n", (int) hr); + dprintf("Wacca Touch: Processing error: %x\n", (int) hr); } return hr; @@ -160,11 +160,11 @@ static HRESULT touch1_handle_irp_locked(struct irp *irp) HRESULT hr; if (irp->op == IRP_OP_OPEN) { - dprintf("Wacca touch1: Starting backend\n"); + dprintf("Wacca Touch1: Starting backend\n"); hr = mercury_dll.touch_init(); if (FAILED(hr)) { - dprintf("Wacca touch: Backend error: %x\n", (int) hr); + dprintf("Wacca Touch: Backend error: %x\n", (int) hr); return hr; } @@ -186,7 +186,7 @@ static HRESULT touch1_handle_irp_locked(struct irp *irp) if (hr != S_OK) { if (FAILED(hr)) { - dprintf("Wacca touch: Deframe error: %x\n", (int) hr); + dprintf("Wacca Touch: Deframe error: %x\n", (int) hr); } return hr; @@ -195,7 +195,7 @@ static HRESULT touch1_handle_irp_locked(struct irp *irp) hr = touch_req_dispatch(&req); if (FAILED(hr)) { - dprintf("Wacca touch: Processing error: %x\n", (int) hr); + dprintf("Wacca Touch: Processing error: %x\n", (int) hr); } return hr; @@ -218,13 +218,13 @@ static HRESULT touch_req_dispatch(const struct touch_req *req) case CMD_START_AUTO_SCAN: return touch_handle_start_auto_scan(req); case CMD_BEGIN_WRITE: - dprintf("Wacca touch: Begin write for side %d\n", req->side); + dprintf("Wacca Touch: Begin write for side %d\n", req->side); return S_OK; case CMD_NEXT_WRITE: - dprintf("Wacca touch: continue write for side %d\n", req->side); + dprintf("Wacca Touch: continue write for side %d\n", req->side); return S_OK; default: - dprintf("Wacca touch: Unhandled command %02x\n", req->cmd); + dprintf("Wacca Touch: Unhandled command %02x\n", req->cmd); return S_OK; } } @@ -272,7 +272,7 @@ static HRESULT touch_handle_next_read(const struct touch_req *req) rev = " 101 115 98 86 76 67 68 48 117 0 82 154 0 6 35 4"; break; default: - dprintf("Wacca touch: BAD READ REQUEST %2hx\n", req->data[2]); + dprintf("Wacca Touch: BAD READ REQUEST %2hx\n", req->data[2]); return 1; } @@ -294,13 +294,14 @@ static HRESULT touch_handle_get_unit_board_ver(const struct touch_req *req) struct touch_resp_get_unit_board_ver resp; HRESULT hr; memset(&resp, 0, sizeof(resp)); - dprintf("Wacca Touch%d: get unit board version\n", req->side); + dprintf("Wacca Touch%d: Get unit board version\n", req->side); memset(resp.version, 0, sizeof(resp.version)); memcpy(resp.version, SYNC_BOARD_VER, sizeof(SYNC_BOARD_VER)); - for (int i = 0; i < 6; i++ ) + for (int i = 0; i < 6; i++) { memcpy(&resp.version[7 + (6 * i)], UNIT_BOARD_VER, sizeof(UNIT_BOARD_VER)); + } resp.cmd = 0xa8; resp.checksum = 0; @@ -386,15 +387,17 @@ static HRESULT touch_handle_start_auto_scan(const struct touch_req *req) // but the game doesn't seem to mind that it's the same uint8_t data2[9] = { 0x0d, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00 }; - dprintf("Wacca Touch%d: Start Auto", req->side); + dprintf("Wacca Touch%d: Start Auto\n", req->side); #if defined(LOG_MERCURY_SLIDER) - for (int i = 0; i < req->data_length; i++) + for (int i = 0; i < req->data_length; i++) { dprintf("0x%02x ", req->data[i]); - #endif - dprintf("\n"); + } - resp.cmd = 0x9c; + dprintf("\n"); + #endif + + resp.cmd = 0xC9; resp.data = 0; resp.checksum = 0x49; @@ -416,6 +419,7 @@ static HRESULT touch_handle_start_auto_scan(const struct touch_req *req) } mercury_dll.touch_start(touch_res_auto_scan); + return hr; } @@ -434,11 +438,16 @@ static void touch_res_auto_scan(const bool *state) frame0.cmd = 0x81; frame0.count = input_frame_count_0++; - input_frame_count_0 %= 0x7f; + if (input_frame_count_0 > 127) { + input_frame_count_0 = 0; + } + frame1.cmd = 0x81; frame1.count = input_frame_count_1++; - input_frame_count_1 %= 0x7f; + if (input_frame_count_1 > 127) { + input_frame_count_1 = 0; + } for (int i = 0; i < 24; i++) { for (int j = 0; j < 5; j++) { @@ -465,14 +474,14 @@ static void touch_res_auto_scan(const bool *state) frame1.checksum = calc_checksum(&frame1, sizeof(frame1)); if (touch0_auto) { - //dprintf("Wacca touch: Touch0 auto frame #%2hx sent\n", frame0.count); + //dprintf("Wacca Touch: Touch0 auto frame #%2hx sent\n", frame0.count); EnterCriticalSection(&touch0_lock); iobuf_write(&touch0_uart.readable, &frame0, sizeof(frame0)); LeaveCriticalSection(&touch0_lock); } if (touch1_auto) { - //dprintf("Wacca touch: Touch1 auto frame #%2hx sent\n", frame0.count); + //dprintf("Wacca Touch: Touch1 auto frame #%2hx sent\n", frame0.count); EnterCriticalSection(&touch1_lock); iobuf_write(&touch1_uart.readable, &frame1, sizeof(frame1)); LeaveCriticalSection(&touch1_lock); @@ -510,9 +519,9 @@ static uint8_t calc_checksum(const void *ptr, size_t nbytes) src = ptr; for (size_t i = 0; i < nbytes; i++) { - //dprintf("Wacca touch: Calculating %2hx\n", src[i]); + //dprintf("Wacca Touch: Calculating %2hx\n", src[i]); checksum = checksum^(src[i]); } - //dprintf("Wacca touch: Checksum is %2hx\n", checksum&0x7f); + //dprintf("Wacca Touch: Checksum is %2hx\n", checksum&0x7f); return checksum&0x7f; } From 966eeeeb875d97747452fb92fd547498e1805fee Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Mon, 19 Jan 2026 19:51:48 +0100 Subject: [PATCH 22/37] mercury: update mystery cmds, add fg detect, cleanup --- common/util/fg-detect.c | 1 + games/mercuryhook/touch.c | 83 +++++++++++++++++++++++---------------- games/mercuryhook/touch.h | 26 ++++++------ 3 files changed, 66 insertions(+), 44 deletions(-) diff --git a/common/util/fg-detect.c b/common/util/fg-detect.c index e5ed83c..a05e128 100644 --- a/common/util/fg-detect.c +++ b/common/util/fg-detect.c @@ -31,6 +31,7 @@ static HWND get_window(){ return NULL; } } + dprintf("FG-Detect: Program window detected\n"); window_handle = hwnd; return window_handle; diff --git a/games/mercuryhook/touch.c b/games/mercuryhook/touch.c index 89189bb..6c16c9f 100644 --- a/games/mercuryhook/touch.c +++ b/games/mercuryhook/touch.c @@ -20,6 +20,7 @@ #include "util/dprintf.h" #include "util/dump.h" +#include "util/fg-detect.h" const char SYNC_BOARD_VER[6] = "190523"; const char UNIT_BOARD_VER[6] = "190514"; @@ -36,8 +37,8 @@ static uint8_t calc_checksum(const void *ptr, size_t nbytes); static HRESULT touch_handle_get_sync_board_ver(const struct touch_req *req); static HRESULT touch_handle_next_read(const struct touch_req *req); static HRESULT touch_handle_get_unit_board_ver(const struct touch_req *req); -static HRESULT touch_handle_mystery1(const struct touch_req *req); -static HRESULT touch_handle_mystery2(const struct touch_req *req); +static HRESULT touch_handle_get_unit_board_alive(const struct touch_req *req); +static HRESULT touch_handle_set_onoff_threshold(const struct touch_req *req); static HRESULT touch_handle_start_auto_scan(const struct touch_req *req); static void touch_res_auto_scan(const bool *state); @@ -46,6 +47,9 @@ static uint8_t input_frame_count_1 = 0; static bool touch0_auto = false; static bool touch1_auto = false; +static uint8_t prev_dataR[TOUCH_DATA_SIZE] = { 0 }; +static uint8_t prev_dataL[TOUCH_DATA_SIZE] = { 0 }; + static CRITICAL_SECTION touch0_lock; static struct uart touch0_uart; static uint8_t touch0_written_bytes[520]; @@ -211,17 +215,17 @@ static HRESULT touch_req_dispatch(const struct touch_req *req) return touch_handle_next_read(req); case CMD_GET_UNIT_BOARD_VER: return touch_handle_get_unit_board_ver(req); - case CMD_MYSTERY1: - return touch_handle_mystery1(req); - case CMD_MYSTERY2: - return touch_handle_mystery2(req); + case CMD_GET_UNIT_BOARD_ALIVE: + return touch_handle_get_unit_board_alive(req); + case CMD_SET_ONOFF_THRESHOLD: + return touch_handle_set_onoff_threshold(req); case CMD_START_AUTO_SCAN: return touch_handle_start_auto_scan(req); case CMD_BEGIN_WRITE: dprintf("Wacca Touch: Begin write for side %d\n", req->side); return S_OK; case CMD_NEXT_WRITE: - dprintf("Wacca Touch: continue write for side %d\n", req->side); + dprintf("Wacca Touch: Continue write for side %d\n", req->side); return S_OK; default: dprintf("Wacca Touch: Unhandled command %02x\n", req->cmd); @@ -236,7 +240,7 @@ static HRESULT touch_handle_get_sync_board_ver(const struct touch_req *req) memset(&resp, 0, sizeof(resp)); dprintf("Wacca Touch%d: Get sync board version\n", req->side); - resp.cmd = 0xa0; + resp.cmd = CMD_GET_SYNC_BOARD_VER; memcpy(resp.version, SYNC_BOARD_VER, sizeof(SYNC_BOARD_VER)); resp.checksum = 0; resp.checksum = calc_checksum(&resp, sizeof(resp)); @@ -259,7 +263,6 @@ static HRESULT touch_handle_next_read(const struct touch_req *req) memset(&resp, 0, sizeof(resp)); dprintf("Wacca Touch%d: Read section %2hx\n", req->side, req->data[2]); - switch (req->data[2]) { // These can be found in the config file case 0x30: @@ -303,7 +306,7 @@ static HRESULT touch_handle_get_unit_board_ver(const struct touch_req *req) memcpy(&resp.version[7 + (6 * i)], UNIT_BOARD_VER, sizeof(UNIT_BOARD_VER)); } - resp.cmd = 0xa8; + resp.cmd = CMD_GET_UNIT_BOARD_VER; resp.checksum = 0; if (req->side == 0) { @@ -336,14 +339,14 @@ static HRESULT touch_handle_get_unit_board_ver(const struct touch_req *req) return hr; } -static HRESULT touch_handle_mystery1(const struct touch_req *req) +static HRESULT touch_handle_get_unit_board_alive(const struct touch_req *req) { - struct touch_resp_mystery1 resp; + struct touch_resp_get_unit_board_alive resp; HRESULT hr; memset(&resp, 0, sizeof(resp)); - dprintf("Wacca Touch%d: Command A2\n", req->side); + dprintf("Wacca Touch%d: Get unit board alive\n", req->side); - resp.cmd = 0xa2; + resp.cmd = CMD_GET_UNIT_BOARD_ALIVE; resp.data = 0x3f; resp.checksum = 0; resp.checksum = calc_checksum(&resp, sizeof(resp)); @@ -357,14 +360,14 @@ static HRESULT touch_handle_mystery1(const struct touch_req *req) return hr; } -static HRESULT touch_handle_mystery2(const struct touch_req *req) +static HRESULT touch_handle_set_onoff_threshold(const struct touch_req *req) { - struct touch_resp_mystery2 resp; + struct touch_resp_set_onoff_threshold resp; HRESULT hr; memset(&resp, 0, sizeof(resp)); - dprintf("Wacca Touch%d: Command 94\n", req->side); + dprintf("Wacca Touch%d: Set on/off threshold\n", req->side); - resp.cmd = 0x94; + resp.cmd = CMD_SET_ONOFF_THRESHOLD; resp.data = 0; resp.checksum = 0; resp.checksum = calc_checksum(&resp, sizeof(resp)); @@ -397,7 +400,7 @@ static HRESULT touch_handle_start_auto_scan(const struct touch_req *req) dprintf("\n"); #endif - resp.cmd = 0xC9; + resp.cmd = CMD_START_AUTO_SCAN; resp.data = 0; resp.checksum = 0x49; @@ -427,10 +430,13 @@ static void touch_res_auto_scan(const bool *state) { struct touch_input_frame frame0; struct touch_input_frame frame1; + memset(&frame0, 0, sizeof(frame0)); memset(&frame1, 0, sizeof(frame1)); - uint8_t dataR[24] = { 0 }; - uint8_t dataL[24] = { 0 }; + + uint8_t dataR[TOUCH_DATA_SIZE] = { 0 }; + uint8_t dataL[TOUCH_DATA_SIZE] = { 0 }; + // this changes every input on a real board but // the game doesn't seem to care about it... uint8_t data2[9] = { 0x0d, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00 }; @@ -449,16 +455,26 @@ static void touch_res_auto_scan(const bool *state) input_frame_count_1 = 0; } - for (int i = 0; i < 24; i++) { - for (int j = 0; j < 5; j++) { - if (state[counter]) { - dataR[i] |= (1 << j); + fgdet_poll(); + if (fgdet_in_foreground()) { + for (int i = 0; i < 24; i++) { + for (int j = 0; j < 5; j++) { + if (state[counter]) { + dataR[i] |= (1 << j); + } + if (state[counter+120]) { + dataL[i] |= (1 << j); + } + counter++; } - if (state[counter+120]) { - dataL[i] |= (1 << j); - } - counter++; } + + memcpy(prev_dataL, dataL, sizeof(dataL)); + memcpy(prev_dataR, dataR, sizeof(dataR)); + } else { + // if we're unfocused, freeze the current input + memcpy(dataL, prev_dataL, sizeof(prev_dataL)); + memcpy(dataR, prev_dataR, sizeof(prev_dataR)); } memcpy(frame0.data1, dataR, sizeof(dataR)); @@ -474,14 +490,14 @@ static void touch_res_auto_scan(const bool *state) frame1.checksum = calc_checksum(&frame1, sizeof(frame1)); if (touch0_auto) { - //dprintf("Wacca Touch: Touch0 auto frame #%2hx sent\n", frame0.count); + // dprintf("Wacca Touch: Touch0 auto frame #%2hx sent\n", frame0.count); EnterCriticalSection(&touch0_lock); iobuf_write(&touch0_uart.readable, &frame0, sizeof(frame0)); LeaveCriticalSection(&touch0_lock); } if (touch1_auto) { - //dprintf("Wacca Touch: Touch1 auto frame #%2hx sent\n", frame0.count); + // dprintf("Wacca Touch: Touch1 auto frame #%2hx sent\n", frame0.count); EnterCriticalSection(&touch1_lock); iobuf_write(&touch1_uart.readable, &frame1, sizeof(frame1)); LeaveCriticalSection(&touch1_lock); @@ -519,9 +535,10 @@ static uint8_t calc_checksum(const void *ptr, size_t nbytes) src = ptr; for (size_t i = 0; i < nbytes; i++) { - //dprintf("Wacca Touch: Calculating %2hx\n", src[i]); + // dprintf("Wacca Touch: Calculating %2hx\n", src[i]); checksum = checksum^(src[i]); } - //dprintf("Wacca Touch: Checksum is %2hx\n", checksum&0x7f); + + // dprintf("Wacca Touch: Checksum is %2hx\n", checksum&0x7f); return checksum&0x7f; } diff --git a/games/mercuryhook/touch.h b/games/mercuryhook/touch.h index cc665c8..3445214 100644 --- a/games/mercuryhook/touch.h +++ b/games/mercuryhook/touch.h @@ -8,15 +8,19 @@ struct touch_config { bool enable; }; +enum { + TOUCH_DATA_SIZE = 0x18, +}; + enum touch_cmd { - CMD_GET_SYNC_BOARD_VER = 0xa0, - CMD_NEXT_READ = 0x72, - CMD_GET_UNIT_BOARD_VER = 0xa8, - CMD_MYSTERY1 = 0xa2, - CMD_MYSTERY2 = 0x94, - CMD_START_AUTO_SCAN = 0xc9, - CMD_BEGIN_WRITE = 0x77, - CMD_NEXT_WRITE = 0x20 + CMD_NEXT_WRITE = 0x20, + CMD_NEXT_READ = 0x72, + CMD_BEGIN_WRITE = 0x77, + CMD_SET_ONOFF_THRESHOLD = 0x94, + CMD_GET_SYNC_BOARD_VER = 0xA0, + CMD_GET_UNIT_BOARD_ALIVE = 0xA2, + CMD_GET_UNIT_BOARD_VER = 0xA8, + CMD_START_AUTO_SCAN = 0xC9 }; struct touch_req { @@ -28,7 +32,7 @@ struct touch_req { struct touch_input_frame { uint8_t cmd; - uint8_t data1[24]; + uint8_t data1[TOUCH_DATA_SIZE]; uint8_t data2[9]; uint8_t count; uint8_t checksum; @@ -51,13 +55,13 @@ struct touch_resp_get_unit_board_ver { uint8_t checksum; }; -struct touch_resp_mystery1 { +struct touch_resp_get_unit_board_alive { uint8_t cmd; uint8_t data; uint8_t checksum; }; -struct touch_resp_mystery2 { +struct touch_resp_set_onoff_threshold { uint8_t cmd; uint8_t data; uint8_t checksum; From 21e5238b0a5c0dfcc5f01359422fc3a7fb9ca571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=B0=E3=83=AD=E3=83=BC=E3=83=A9=E3=83=B3=E3=83=97?= <130208311+Gl0w1amp@users.noreply.github.com> Date: Sun, 8 Feb 2026 11:01:01 +0000 Subject: [PATCH 23/37] aimeio: Implement full aime reader function in aimeio (#94) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR implements the missing NFC, FeliCa, and Mifare command handlers in `aimeio`. This expands the reader's capability to support games and tools that utilize these specific radio functions. **Changes:** * Implemented NFC radio control functions. * Added MIFARE and FeliCa command handling. * Updated `aimeio` DLL exports and hook definitions. **Testing Status:** * I have verified the implementation using a virtual `aimeio` test environment, and the command handling works as expected. But I currently do not have access to a physical card reader, so haven't been able to verify the behavior on actual hardware yet. Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/94 Co-authored-by: グローランプ <130208311+Gl0w1amp@users.noreply.github.com> Co-committed-by: グローランプ <130208311+Gl0w1amp@users.noreply.github.com> --- common/aimeio/aimeio.c | 166 ++++++++++++++ common/aimeio/aimeio.h | 134 +++++++++++ common/board/aime-dll.c | 32 ++- common/board/aime-dll.h | 40 ++++ common/board/sg-nfc.c | 368 ++++++++++++++++++++++++++++-- common/board/sg-nfc.h | 34 +++ common/board/sg-reader.c | 186 +++++++++++++++ games/apm3hook/apm3hook.def | 10 + games/carolhook/carolhook.def | 10 + games/chunihook/chunihook.def | 10 + games/chusanhook/chusanhook.def | 10 + games/cmhook/cmhook.def | 10 + games/cxbhook/cxbhook.def | 10 + games/divahook/divahook.def | 10 + games/fgohook/fgohook.def | 10 + games/idachook/idachook.def | 10 + games/idzhook/idzhook.def | 10 + games/kemonohook/kemonohook.def | 10 + games/mai2hook/mai2hook.def | 10 + games/mercuryhook/mercuryhook.def | 10 + games/mu3hook/mu3hook.def | 10 + games/swdchook/swdchook.def | 10 + games/tokyohook/tokyohook.def | 10 + 23 files changed, 1103 insertions(+), 17 deletions(-) diff --git a/common/aimeio/aimeio.c b/common/aimeio/aimeio.c index 7efffb5..fbb694e 100644 --- a/common/aimeio/aimeio.c +++ b/common/aimeio/aimeio.c @@ -27,6 +27,8 @@ static uint8_t aime_io_aime_id[10]; static uint8_t aime_io_felica_id[8]; static bool aime_io_aime_id_present; static bool aime_io_felica_id_present; +static bool aime_io_radio_on = true; +static bool aime_io_update_mode; static void aime_io_config_read( struct aime_io_config *cfg, @@ -244,6 +246,10 @@ HRESULT aime_io_nfc_poll(uint8_t unit_no) /* Don't do anything more if the scan key is not held */ + if (!aime_io_radio_on) { + return S_OK; + } + sense = GetAsyncKeyState(aime_io_cfg.vk_scan) & 0x8000; if (!sense) { @@ -349,6 +355,166 @@ HRESULT aime_io_nfc_get_felica_id(uint8_t unit_no, uint64_t *IDm) return S_OK; } +HRESULT aime_io_nfc_get_mifare_uid( + uint8_t unit_no, + uint8_t *uid, + size_t uid_size) +{ + (void) uid; + (void) uid_size; + + if (unit_no != 0 || !aime_io_aime_id_present) { + return S_FALSE; + } + + return S_FALSE; +} + +HRESULT aime_io_nfc_mifare_select( + uint8_t unit_no, + const uint8_t *uid, + size_t uid_size) +{ + (void) uid; + (void) uid_size; + + if (unit_no != 0) { + return S_FALSE; + } + + return S_FALSE; +} + +HRESULT aime_io_nfc_mifare_set_key( + uint8_t unit_no, + uint8_t key_type, + const uint8_t *key, + size_t key_size) +{ + (void) key_type; + (void) key; + (void) key_size; + + if (unit_no != 0) { + return S_FALSE; + } + + return S_FALSE; +} + +HRESULT aime_io_nfc_mifare_authenticate( + uint8_t unit_no, + uint8_t key_type, + const uint8_t *payload, + size_t payload_size) +{ + (void) key_type; + (void) payload; + (void) payload_size; + + if (unit_no != 0) { + return S_FALSE; + } + + return S_FALSE; +} + +HRESULT aime_io_nfc_mifare_read_block( + uint8_t unit_no, + const uint8_t *uid, + size_t uid_size, + uint8_t block_no, + uint8_t *block, + size_t block_size) +{ + (void) uid; + (void) uid_size; + (void) block_no; + (void) block; + (void) block_size; + + if (unit_no != 0) { + return S_FALSE; + } + + return S_FALSE; +} + +HRESULT aime_io_nfc_felica_transact( + uint8_t unit_no, + const uint8_t *req, + size_t req_size, + uint8_t *res, + size_t res_size, + size_t *res_size_written) +{ + (void) req; + (void) req_size; + (void) res; + (void) res_size; + (void) res_size_written; + + if (unit_no != 0) { + return S_FALSE; + } + + return S_FALSE; +} + +HRESULT aime_io_nfc_radio_on(uint8_t unit_no) +{ + if (unit_no != 0) { + return S_FALSE; + } + + aime_io_radio_on = true; + aime_io_update_mode = false; + + return S_OK; +} + +HRESULT aime_io_nfc_radio_off(uint8_t unit_no) +{ + if (unit_no != 0) { + return S_FALSE; + } + + aime_io_radio_on = false; + + return S_OK; +} + +HRESULT aime_io_nfc_to_update_mode(uint8_t unit_no) +{ + if (unit_no != 0) { + return S_FALSE; + } + + aime_io_update_mode = true; + + return S_OK; +} + +HRESULT aime_io_nfc_send_hex_data( + uint8_t unit_no, + const uint8_t *payload, + size_t payload_size, + uint8_t *status_out) +{ + (void) payload; + (void) payload_size; + + if (unit_no != 0) { + return S_FALSE; + } + + if (status_out != NULL) { + *status_out = (payload_size == 0x2b) ? 0x20 : 0x00; + } + + return S_OK; +} + void aime_io_led_set_color(uint8_t unit_no, uint8_t r, uint8_t g, uint8_t b) {} diff --git a/common/aimeio/aimeio.h b/common/aimeio/aimeio.h index bb851cd..96dd241 100644 --- a/common/aimeio/aimeio.h +++ b/common/aimeio/aimeio.h @@ -76,6 +76,140 @@ HRESULT aime_io_nfc_get_aime_id( */ HRESULT aime_io_nfc_get_felica_id(uint8_t unit_no, uint64_t *IDm); +/* + MIFARE key selector values used by the set key/authenticate functions. + + Minimum API version: 0x0101 +*/ +enum { + AIME_IO_MIFARE_KEY_AIME = 0, + AIME_IO_MIFARE_KEY_BANA = 1, +}; + +/* + Attempt to read the 4-byte MIFARE UID of the currently present card. + + Parameters: + + - unit_no: Always 0 as of the current API version + - uid: Pointer to a four-byte buffer that will receive the UID + - uid_size: Size of the buffer at *uid. Always 4. + + Returns: + + - S_OK if a MIFARE card is present and the UID was read successfully + - S_FALSE if no MIFARE card is present (*uid will be ignored) + - Any HRESULT error if an error occured. + + Minimum API version: 0x0101 +*/ +HRESULT aime_io_nfc_get_mifare_uid( + uint8_t unit_no, + uint8_t *uid, + size_t uid_size); + +/* + Select a MIFARE card by UID (optional for real readers). + + Minimum API version: 0x0101 +*/ +HRESULT aime_io_nfc_mifare_select( + uint8_t unit_no, + const uint8_t *uid, + size_t uid_size); + +/* + Supply a MIFARE authentication key to the reader. + + Minimum API version: 0x0101 +*/ +HRESULT aime_io_nfc_mifare_set_key( + uint8_t unit_no, + uint8_t key_type, + const uint8_t *key, + size_t key_size); + +/* + Perform a MIFARE authentication sequence. + + Minimum API version: 0x0101 +*/ +HRESULT aime_io_nfc_mifare_authenticate( + uint8_t unit_no, + uint8_t key_type, + const uint8_t *payload, + size_t payload_size); + +/* + Read a 16-byte MIFARE block from the card. + + Minimum API version: 0x0101 +*/ +HRESULT aime_io_nfc_mifare_read_block( + uint8_t unit_no, + const uint8_t *uid, + size_t uid_size, + uint8_t block_no, + uint8_t *block, + size_t block_size); + +/* + Forward a raw FeliCa request to a real reader. + + Parameters: + + - req: FeliCa request buffer, including the length byte + - res: FeliCa response buffer, including the length byte + - res_size_written: Output size of the response (bytes written to *res) + + Minimum API version: 0x0101 +*/ +HRESULT aime_io_nfc_felica_transact( + uint8_t unit_no, + const uint8_t *req, + size_t req_size, + uint8_t *res, + size_t res_size, + size_t *res_size_written); + +/* + Enable the reader's RF field. + + Minimum API version: 0x0101 +*/ +HRESULT aime_io_nfc_radio_on(uint8_t unit_no); + +/* + Disable the reader's RF field. + + Minimum API version: 0x0101 +*/ +HRESULT aime_io_nfc_radio_off(uint8_t unit_no); + +/* + Put the reader into firmware update mode. + + Minimum API version: 0x0101 +*/ +HRESULT aime_io_nfc_to_update_mode(uint8_t unit_no); + +/* + Forward a raw hex-data command to the reader. + + Parameters: + + - payload: Command payload bytes + - payload_size: Size of the payload + - status_out: Optional pointer to receive the SG status byte + + Minimum API version: 0x0101 +*/ +HRESULT aime_io_nfc_send_hex_data( + uint8_t unit_no, + const uint8_t *payload, + size_t payload_size, + uint8_t *status_out); + /* Change the color and brightness of the card reader's RGB lighting diff --git a/common/board/aime-dll.c b/common/board/aime-dll.c index 7c00dfd..94b380c 100644 --- a/common/board/aime-dll.c +++ b/common/board/aime-dll.c @@ -10,7 +10,7 @@ enum { AIME_DLL_SYM_COUNT_V100 = 5, - AIME_DLL_SYM_COUNT_V101 = 7, + AIME_DLL_SYM_COUNT_V101 = 17, }; const struct dll_bind_sym aime_dll_syms[] = { @@ -35,6 +35,36 @@ const struct dll_bind_sym aime_dll_syms[] = { }, { .sym = "aime_io_vfd_set_state", .off = offsetof(struct aime_dll, vfd_set_state), + }, { + .sym = "aime_io_nfc_get_mifare_uid", + .off = offsetof(struct aime_dll, nfc_get_mifare_uid), + }, { + .sym = "aime_io_nfc_mifare_select", + .off = offsetof(struct aime_dll, nfc_mifare_select), + }, { + .sym = "aime_io_nfc_mifare_set_key", + .off = offsetof(struct aime_dll, nfc_mifare_set_key), + }, { + .sym = "aime_io_nfc_mifare_authenticate", + .off = offsetof(struct aime_dll, nfc_mifare_authenticate), + }, { + .sym = "aime_io_nfc_mifare_read_block", + .off = offsetof(struct aime_dll, nfc_mifare_read_block), + }, { + .sym = "aime_io_nfc_felica_transact", + .off = offsetof(struct aime_dll, nfc_felica_transact), + }, { + .sym = "aime_io_nfc_radio_on", + .off = offsetof(struct aime_dll, nfc_radio_on), + }, { + .sym = "aime_io_nfc_radio_off", + .off = offsetof(struct aime_dll, nfc_radio_off), + }, { + .sym = "aime_io_nfc_to_update_mode", + .off = offsetof(struct aime_dll, nfc_to_update_mode), + }, { + .sym = "aime_io_nfc_send_hex_data", + .off = offsetof(struct aime_dll, nfc_send_hex_data), }, }; diff --git a/common/board/aime-dll.h b/common/board/aime-dll.h index 7113cc1..00a5368 100644 --- a/common/board/aime-dll.h +++ b/common/board/aime-dll.h @@ -14,6 +14,46 @@ struct aime_dll { uint8_t *luid, size_t luid_size); HRESULT (*nfc_get_felica_id)(uint8_t unit_no, uint64_t *IDm); + HRESULT (*nfc_get_mifare_uid)( + uint8_t unit_no, + uint8_t *uid, + size_t uid_size); + HRESULT (*nfc_mifare_select)( + uint8_t unit_no, + const uint8_t *uid, + size_t uid_size); + HRESULT (*nfc_mifare_set_key)( + uint8_t unit_no, + uint8_t key_type, + const uint8_t *key, + size_t key_size); + HRESULT (*nfc_mifare_authenticate)( + uint8_t unit_no, + uint8_t key_type, + const uint8_t *payload, + size_t payload_size); + HRESULT (*nfc_mifare_read_block)( + uint8_t unit_no, + const uint8_t *uid, + size_t uid_size, + uint8_t block_no, + uint8_t *block, + size_t block_size); + HRESULT (*nfc_felica_transact)( + uint8_t unit_no, + const uint8_t *req, + size_t req_size, + uint8_t *res, + size_t res_size, + size_t *res_size_written); + HRESULT (*nfc_radio_on)(uint8_t unit_no); + HRESULT (*nfc_radio_off)(uint8_t unit_no); + HRESULT (*nfc_to_update_mode)(uint8_t unit_no); + HRESULT (*nfc_send_hex_data)( + uint8_t unit_no, + const uint8_t *payload, + size_t payload_size, + uint8_t *status_out); void (*led_set_color)(uint8_t unit_no, uint8_t r, uint8_t g, uint8_t b); void (*vfd_set_text)( const uint8_t *text, diff --git a/common/board/sg-nfc.c b/common/board/sg-nfc.c index b103336..82a01a6 100644 --- a/common/board/sg-nfc.c +++ b/common/board/sg-nfc.c @@ -12,6 +12,8 @@ #include "board/sg-nfc.h" #include "board/sg-nfc-cmd.h" +#include "aimeio/aimeio.h" + #include "iccard/aime.h" #include "iccard/felica.h" @@ -57,6 +59,38 @@ static HRESULT sg_nfc_cmd_mifare_read_block( const struct sg_nfc_req_mifare_read_block *req, struct sg_nfc_res_mifare_read_block *res); +static HRESULT sg_nfc_cmd_mifare_select( + struct sg_nfc *nfc, + const struct sg_req_header *req, + struct sg_res_header *res); + +static HRESULT sg_nfc_cmd_mifare_set_key( + struct sg_nfc *nfc, + const struct sg_req_header *req, + struct sg_res_header *res, + uint8_t key_type); + +static HRESULT sg_nfc_cmd_mifare_authenticate( + struct sg_nfc *nfc, + const struct sg_req_header *req, + struct sg_res_header *res, + uint8_t key_type); + +static HRESULT sg_nfc_cmd_radio_on( + struct sg_nfc *nfc, + const struct sg_req_header *req, + struct sg_res_header *res); + +static HRESULT sg_nfc_cmd_radio_off( + struct sg_nfc *nfc, + const struct sg_req_header *req, + struct sg_res_header *res); + +static HRESULT sg_nfc_cmd_to_update_mode( + struct sg_nfc *nfc, + const struct sg_req_header *req, + struct sg_res_header *res); + static HRESULT sg_nfc_cmd_felica_encap( struct sg_nfc *nfc, const struct sg_nfc_req_felica_encap *req, @@ -195,19 +229,49 @@ static HRESULT sg_nfc_dispatch( &req->felica_encap, &res->felica_encap); + case SG_NFC_CMD_MIFARE_SELECT_TAG: + return sg_nfc_cmd_mifare_select(nfc, &req->simple, &res->simple); + + case SG_NFC_CMD_MIFARE_SET_KEY_AIME: + return sg_nfc_cmd_mifare_set_key( + nfc, + &req->simple, + &res->simple, + AIME_IO_MIFARE_KEY_AIME); + + case SG_NFC_CMD_MIFARE_SET_KEY_BANA: + return sg_nfc_cmd_mifare_set_key( + nfc, + &req->simple, + &res->simple, + AIME_IO_MIFARE_KEY_BANA); + case SG_NFC_CMD_MIFARE_AUTHENTICATE_AIME: + return sg_nfc_cmd_mifare_authenticate( + nfc, + &req->simple, + &res->simple, + AIME_IO_MIFARE_KEY_AIME); + case SG_NFC_CMD_MIFARE_AUTHENTICATE_BANA: + return sg_nfc_cmd_mifare_authenticate( + nfc, + &req->simple, + &res->simple, + AIME_IO_MIFARE_KEY_BANA); + + case SG_NFC_CMD_RADIO_ON: + return sg_nfc_cmd_radio_on(nfc, &req->simple, &res->simple); + + case SG_NFC_CMD_RADIO_OFF: + return sg_nfc_cmd_radio_off(nfc, &req->simple, &res->simple); + + case SG_NFC_CMD_TO_UPDATE_MODE: + return sg_nfc_cmd_to_update_mode(nfc, &req->simple, &res->simple); + case SG_NFC_CMD_SEND_HEX_DATA: return sg_nfc_cmd_send_hex_data(nfc, &req->simple, &res->simple); - case SG_NFC_CMD_MIFARE_SELECT_TAG: - case SG_NFC_CMD_MIFARE_SET_KEY_AIME: - case SG_NFC_CMD_MIFARE_SET_KEY_BANA: - case SG_NFC_CMD_RADIO_ON: - case SG_NFC_CMD_RADIO_OFF: - case SG_NFC_CMD_TO_UPDATE_MODE: - return sg_nfc_cmd_dummy(nfc, &req->simple, &res->simple); - default: sg_nfc_dprintf(nfc, "Unimpl command %02x\n", req->simple.hdr.cmd); @@ -300,7 +364,9 @@ static HRESULT sg_nfc_poll_aime( struct sg_nfc *nfc, struct sg_nfc_poll_mifare *mifare) { + bool has_uid; uint8_t luid[10]; + uint8_t uid[4]; HRESULT hr; /* Call backend */ @@ -317,12 +383,30 @@ static HRESULT sg_nfc_poll_aime( sg_nfc_dprintf(nfc, "AiMe card is present\n"); - /* Construct response (use an arbitrary UID) */ + has_uid = false; + + if (nfc->ops->get_mifare_uid != NULL) { + hr = nfc->ops->get_mifare_uid(nfc->ops_ctx, uid, sizeof(uid)); + + if (FAILED(hr)) { + return hr; + } + + if (hr == S_OK) { + has_uid = true; + } + } + + /* Construct response */ mifare->type = 0x10; mifare->id_len = sizeof(mifare->uid); - // mifare->uid = _byteswap_ulong(0x8FBECBFF); - mifare->uid = _byteswap_ulong(0x01020304); + if (has_uid) { + memcpy(&mifare->uid, uid, sizeof(uid)); + } else { + // mifare->uid = _byteswap_ulong(0x8FBECBFF); + mifare->uid = _byteswap_ulong(0x01020304); + } /* Initialize MIFARE IC emulator */ @@ -372,11 +456,182 @@ static HRESULT sg_nfc_poll_felica( return S_OK; } +static HRESULT sg_nfc_cmd_mifare_select( + struct sg_nfc *nfc, + const struct sg_req_header *req, + struct sg_res_header *res) +{ + const uint8_t *payload; + HRESULT hr; + + if (req->payload_len != sizeof(uint32_t)) { + sg_nfc_dprintf(nfc, "%s: Payload size is incorrect\n", __func__); + + return E_FAIL; + } + + payload = (const uint8_t *) req + sizeof(*req); + + if (nfc->ops->mifare_select != NULL) { + hr = nfc->ops->mifare_select(nfc->ops_ctx, payload, req->payload_len); + + if (FAILED(hr)) { + return hr; + } + } + + sg_res_init(res, req, 0); + + return S_OK; +} + +static HRESULT sg_nfc_cmd_mifare_set_key( + struct sg_nfc *nfc, + const struct sg_req_header *req, + struct sg_res_header *res, + uint8_t key_type) +{ + const uint8_t *payload; + HRESULT hr; + + payload = (const uint8_t *) req + sizeof(*req); + + if (nfc->ops->mifare_set_key != NULL) { + hr = nfc->ops->mifare_set_key( + nfc->ops_ctx, + key_type, + payload, + req->payload_len); + + if (FAILED(hr)) { + return hr; + } + } + + sg_res_init(res, req, 0); + + return S_OK; +} + +static HRESULT sg_nfc_cmd_mifare_authenticate( + struct sg_nfc *nfc, + const struct sg_req_header *req, + struct sg_res_header *res, + uint8_t key_type) +{ + const uint8_t *payload; + HRESULT hr; + + payload = (const uint8_t *) req + sizeof(*req); + + if (nfc->ops->mifare_authenticate != NULL) { + hr = nfc->ops->mifare_authenticate( + nfc->ops_ctx, + key_type, + payload, + req->payload_len); + + if (FAILED(hr)) { + return hr; + } + } + + sg_res_init(res, req, 0); + + return S_OK; +} + +static HRESULT sg_nfc_cmd_radio_on( + struct sg_nfc *nfc, + const struct sg_req_header *req, + struct sg_res_header *res) +{ + HRESULT hr; + + if (nfc->ops->radio_on == NULL) { + sg_res_init(res, req, 0); + return S_OK; + } + + hr = nfc->ops->radio_on(nfc->ops_ctx); + + if (FAILED(hr)) { + return hr; + } + + if (hr == S_FALSE) { + sg_res_init(res, req, 0); + return S_OK; + } + + sg_res_init(res, req, 0); + + return S_OK; +} + +static HRESULT sg_nfc_cmd_radio_off( + struct sg_nfc *nfc, + const struct sg_req_header *req, + struct sg_res_header *res) +{ + HRESULT hr; + + if (nfc->ops->radio_off == NULL) { + sg_res_init(res, req, 0); + return S_OK; + } + + hr = nfc->ops->radio_off(nfc->ops_ctx); + + if (FAILED(hr)) { + return hr; + } + + if (hr == S_FALSE) { + sg_res_init(res, req, 0); + return S_OK; + } + + sg_res_init(res, req, 0); + + return S_OK; +} + +static HRESULT sg_nfc_cmd_to_update_mode( + struct sg_nfc *nfc, + const struct sg_req_header *req, + struct sg_res_header *res) +{ + HRESULT hr; + + if (nfc->ops->to_update_mode == NULL) { + sg_res_init(res, req, 0); + return S_OK; + } + + hr = nfc->ops->to_update_mode(nfc->ops_ctx); + + if (FAILED(hr)) { + return hr; + } + + if (hr == S_FALSE) { + sg_res_init(res, req, 0); + return S_OK; + } + + sg_res_init(res, req, 0); + + return S_OK; +} + static HRESULT sg_nfc_cmd_mifare_read_block( struct sg_nfc *nfc, const struct sg_nfc_req_mifare_read_block *req, struct sg_nfc_res_mifare_read_block *res) { + const uint8_t *uid_bytes; + HRESULT hr; uint32_t uid; if (req->req.payload_len != sizeof(req->payload)) { @@ -385,10 +640,30 @@ static HRESULT sg_nfc_cmd_mifare_read_block( return E_FAIL; } + uid_bytes = (const uint8_t *) &req->payload.uid; uid = _byteswap_ulong(req->payload.uid); sg_nfc_dprintf(nfc, "Read uid %08x block %i\n", uid, req->payload.block_no); + if (nfc->ops->mifare_read_block != NULL) { + hr = nfc->ops->mifare_read_block( + nfc->ops_ctx, + uid_bytes, + sizeof(req->payload.uid), + req->payload.block_no, + res->block, + sizeof(res->block)); + + if (FAILED(hr)) { + return hr; + } + + if (hr == S_OK) { + sg_res_init(&res->res, &req->req, sizeof(res->block)); + return S_OK; + } + } + if (req->payload.block_no > 14) { sg_nfc_dprintf(nfc, "MIFARE block number out of range\n"); @@ -455,6 +730,7 @@ static HRESULT sg_nfc_cmd_felica_encap( { struct const_iobuf f_req; struct iobuf f_res; + size_t res_size_written; HRESULT hr; /* First byte of encapsulated request and response is a length byte @@ -472,6 +748,32 @@ static HRESULT sg_nfc_cmd_felica_encap( return E_FAIL; } + if (nfc->ops->felica_transact != NULL) { + res_size_written = 0; + hr = nfc->ops->felica_transact( + nfc->ops_ctx, + req->payload, + req->payload[0], + res->payload, + sizeof(res->payload), + &res_size_written); + + if (FAILED(hr)) { + return hr; + } + + if (hr == S_OK) { + if (res_size_written == 0 || + res_size_written > sizeof(res->payload)) { + return E_FAIL; + } + + sg_res_init(&res->res, &req->req, res_size_written); + + return S_OK; + } + } + f_req.bytes = req->payload; f_req.nbytes = req->payload[0]; f_req.pos = 1; @@ -507,14 +809,48 @@ static HRESULT sg_nfc_cmd_send_hex_data( const struct sg_req_header *req, struct sg_res_header *res) { - sg_res_init(res, req, 0); + const uint8_t *payload; + HRESULT hr; + uint8_t status; - /* Firmware checksum length? */ - if (req->payload_len == 0x2b) { - /* The firmware is identical flag? */ - res->status = 0x20; + if (nfc->ops->send_hex_data == NULL) { + sg_res_init(res, req, 0); + + /* Firmware checksum length? */ + if (req->payload_len == 0x2b) { + /* The firmware is identical flag? */ + res->status = 0x20; + } + + return S_OK; } + payload = (const uint8_t *) req + sizeof(*req); + status = 0; + + hr = nfc->ops->send_hex_data( + nfc->ops_ctx, + payload, + req->payload_len, + &status); + + if (FAILED(hr)) { + return hr; + } + + if (hr == S_FALSE) { + sg_res_init(res, req, 0); + + if (req->payload_len == 0x2b) { + res->status = 0x20; + } + + return S_OK; + } + + sg_res_init(res, req, 0); + res->status = status; + return S_OK; } diff --git a/common/board/sg-nfc.h b/common/board/sg-nfc.h index 092b3e3..855d8cf 100644 --- a/common/board/sg-nfc.h +++ b/common/board/sg-nfc.h @@ -14,6 +14,40 @@ struct sg_nfc_ops { HRESULT (*poll)(void *ctx); HRESULT (*get_aime_id)(void *ctx, uint8_t *luid, size_t nbytes); HRESULT (*get_felica_id)(void *ctx, uint64_t *IDm); + HRESULT (*get_mifare_uid)(void *ctx, uint8_t *uid, size_t nbytes); + HRESULT (*mifare_select)(void *ctx, const uint8_t *uid, size_t nbytes); + HRESULT (*mifare_set_key)( + void *ctx, + uint8_t key_type, + const uint8_t *key, + size_t nbytes); + HRESULT (*mifare_authenticate)( + void *ctx, + uint8_t key_type, + const uint8_t *payload, + size_t nbytes); + HRESULT (*mifare_read_block)( + void *ctx, + const uint8_t *uid, + size_t uid_size, + uint8_t block_no, + uint8_t *block, + size_t block_size); + HRESULT (*felica_transact)( + void *ctx, + const uint8_t *req, + size_t req_size, + uint8_t *res, + size_t res_size, + size_t *res_size_written); + HRESULT (*radio_on)(void *ctx); + HRESULT (*radio_off)(void *ctx); + HRESULT (*to_update_mode)(void *ctx); + HRESULT (*send_hex_data)( + void *ctx, + const uint8_t *payload, + size_t payload_size, + uint8_t *status_out); // TODO Banapass, AmuseIC }; diff --git a/common/board/sg-reader.c b/common/board/sg-reader.c index 2dc3c3d..c1935dd 100644 --- a/common/board/sg-reader.c +++ b/common/board/sg-reader.c @@ -24,12 +24,62 @@ static HRESULT sg_reader_nfc_get_aime_id( uint8_t *luid, size_t luid_size); static HRESULT sg_reader_nfc_get_felica_id(void *ctx, uint64_t *IDm); +static HRESULT sg_reader_nfc_get_mifare_uid( + void *ctx, + uint8_t *uid, + size_t uid_size); +static HRESULT sg_reader_nfc_mifare_select( + void *ctx, + const uint8_t *uid, + size_t uid_size); +static HRESULT sg_reader_nfc_mifare_set_key( + void *ctx, + uint8_t key_type, + const uint8_t *key, + size_t key_size); +static HRESULT sg_reader_nfc_mifare_authenticate( + void *ctx, + uint8_t key_type, + const uint8_t *payload, + size_t payload_size); +static HRESULT sg_reader_nfc_mifare_read_block( + void *ctx, + const uint8_t *uid, + size_t uid_size, + uint8_t block_no, + uint8_t *block, + size_t block_size); +static HRESULT sg_reader_nfc_felica_transact( + void *ctx, + const uint8_t *req, + size_t req_size, + uint8_t *res, + size_t res_size, + size_t *res_size_written); +static HRESULT sg_reader_nfc_radio_on(void *ctx); +static HRESULT sg_reader_nfc_radio_off(void *ctx); +static HRESULT sg_reader_nfc_to_update_mode(void *ctx); +static HRESULT sg_reader_nfc_send_hex_data( + void *ctx, + const uint8_t *payload, + size_t payload_size, + uint8_t *status_out); static void sg_reader_led_set_color(void *ctx, uint8_t r, uint8_t g, uint8_t b); static const struct sg_nfc_ops sg_reader_nfc_ops = { .poll = sg_reader_nfc_poll, .get_aime_id = sg_reader_nfc_get_aime_id, .get_felica_id = sg_reader_nfc_get_felica_id, + .get_mifare_uid = sg_reader_nfc_get_mifare_uid, + .mifare_select = sg_reader_nfc_mifare_select, + .mifare_set_key = sg_reader_nfc_mifare_set_key, + .mifare_authenticate = sg_reader_nfc_mifare_authenticate, + .mifare_read_block = sg_reader_nfc_mifare_read_block, + .felica_transact = sg_reader_nfc_felica_transact, + .radio_on = sg_reader_nfc_radio_on, + .radio_off = sg_reader_nfc_radio_off, + .to_update_mode = sg_reader_nfc_to_update_mode, + .send_hex_data = sg_reader_nfc_send_hex_data, }; static const struct sg_led_ops sg_reader_led_ops = { @@ -201,6 +251,142 @@ static HRESULT sg_reader_nfc_get_felica_id(void *ctx, uint64_t *IDm) return aime_dll.nfc_get_felica_id(0, IDm); } +static HRESULT sg_reader_nfc_get_mifare_uid( + void *ctx, + uint8_t *uid, + size_t uid_size) +{ + if (aime_dll.nfc_get_mifare_uid == NULL) { + return S_FALSE; + } + + return aime_dll.nfc_get_mifare_uid(0, uid, uid_size); +} + +static HRESULT sg_reader_nfc_mifare_select( + void *ctx, + const uint8_t *uid, + size_t uid_size) +{ + if (aime_dll.nfc_mifare_select == NULL) { + return S_FALSE; + } + + return aime_dll.nfc_mifare_select(0, uid, uid_size); +} + +static HRESULT sg_reader_nfc_mifare_set_key( + void *ctx, + uint8_t key_type, + const uint8_t *key, + size_t key_size) +{ + if (aime_dll.nfc_mifare_set_key == NULL) { + return S_FALSE; + } + + return aime_dll.nfc_mifare_set_key(0, key_type, key, key_size); +} + +static HRESULT sg_reader_nfc_mifare_authenticate( + void *ctx, + uint8_t key_type, + const uint8_t *payload, + size_t payload_size) +{ + if (aime_dll.nfc_mifare_authenticate == NULL) { + return S_FALSE; + } + + return aime_dll.nfc_mifare_authenticate( + 0, + key_type, + payload, + payload_size); +} + +static HRESULT sg_reader_nfc_mifare_read_block( + void *ctx, + const uint8_t *uid, + size_t uid_size, + uint8_t block_no, + uint8_t *block, + size_t block_size) +{ + if (aime_dll.nfc_mifare_read_block == NULL) { + return S_FALSE; + } + + return aime_dll.nfc_mifare_read_block( + 0, + uid, + uid_size, + block_no, + block, + block_size); +} + +static HRESULT sg_reader_nfc_felica_transact( + void *ctx, + const uint8_t *req, + size_t req_size, + uint8_t *res, + size_t res_size, + size_t *res_size_written) +{ + if (aime_dll.nfc_felica_transact == NULL) { + return S_FALSE; + } + + return aime_dll.nfc_felica_transact( + 0, + req, + req_size, + res, + res_size, + res_size_written); +} + +static HRESULT sg_reader_nfc_radio_on(void *ctx) +{ + if (aime_dll.nfc_radio_on == NULL) { + return S_FALSE; + } + + return aime_dll.nfc_radio_on(0); +} + +static HRESULT sg_reader_nfc_radio_off(void *ctx) +{ + if (aime_dll.nfc_radio_off == NULL) { + return S_FALSE; + } + + return aime_dll.nfc_radio_off(0); +} + +static HRESULT sg_reader_nfc_to_update_mode(void *ctx) +{ + if (aime_dll.nfc_to_update_mode == NULL) { + return S_FALSE; + } + + return aime_dll.nfc_to_update_mode(0); +} + +static HRESULT sg_reader_nfc_send_hex_data( + void *ctx, + const uint8_t *payload, + size_t payload_size, + uint8_t *status_out) +{ + if (aime_dll.nfc_send_hex_data == NULL) { + return S_FALSE; + } + + return aime_dll.nfc_send_hex_data(0, payload, payload_size, status_out); +} + static void sg_reader_led_set_color(void *ctx, uint8_t r, uint8_t g, uint8_t b) { aime_dll.led_set_color(0, r, g, b); diff --git a/games/apm3hook/apm3hook.def b/games/apm3hook/apm3hook.def index c965fb8..776de48 100644 --- a/games/apm3hook/apm3hook.def +++ b/games/apm3hook/apm3hook.def @@ -9,6 +9,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/carolhook/carolhook.def b/games/carolhook/carolhook.def index 1a8312d..a1dccdb 100644 --- a/games/carolhook/carolhook.def +++ b/games/carolhook/carolhook.def @@ -9,6 +9,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/chunihook/chunihook.def b/games/chunihook/chunihook.def index 4128451..1961d46 100644 --- a/games/chunihook/chunihook.def +++ b/games/chunihook/chunihook.def @@ -10,6 +10,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/chusanhook/chusanhook.def b/games/chusanhook/chusanhook.def index 8786d4e..c471207 100644 --- a/games/chusanhook/chusanhook.def +++ b/games/chusanhook/chusanhook.def @@ -10,6 +10,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/cmhook/cmhook.def b/games/cmhook/cmhook.def index 526442d..5727411 100644 --- a/games/cmhook/cmhook.def +++ b/games/cmhook/cmhook.def @@ -9,6 +9,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/cxbhook/cxbhook.def b/games/cxbhook/cxbhook.def index 8466898..7f9b190 100644 --- a/games/cxbhook/cxbhook.def +++ b/games/cxbhook/cxbhook.def @@ -10,6 +10,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/divahook/divahook.def b/games/divahook/divahook.def index a92e61d..e9e430d 100644 --- a/games/divahook/divahook.def +++ b/games/divahook/divahook.def @@ -9,6 +9,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/fgohook/fgohook.def b/games/fgohook/fgohook.def index a2088b9..b341a65 100644 --- a/games/fgohook/fgohook.def +++ b/games/fgohook/fgohook.def @@ -9,6 +9,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/idachook/idachook.def b/games/idachook/idachook.def index 4ef2b98..325a286 100644 --- a/games/idachook/idachook.def +++ b/games/idachook/idachook.def @@ -9,6 +9,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/idzhook/idzhook.def b/games/idzhook/idzhook.def index b13642b..52d42a8 100644 --- a/games/idzhook/idzhook.def +++ b/games/idzhook/idzhook.def @@ -14,6 +14,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/kemonohook/kemonohook.def b/games/kemonohook/kemonohook.def index 4db5e98..fdb8251 100644 --- a/games/kemonohook/kemonohook.def +++ b/games/kemonohook/kemonohook.def @@ -9,6 +9,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/mai2hook/mai2hook.def b/games/mai2hook/mai2hook.def index cf1ed65..31bce4d 100644 --- a/games/mai2hook/mai2hook.def +++ b/games/mai2hook/mai2hook.def @@ -9,6 +9,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/mercuryhook/mercuryhook.def b/games/mercuryhook/mercuryhook.def index 4d3c8ec..d1fa0d1 100644 --- a/games/mercuryhook/mercuryhook.def +++ b/games/mercuryhook/mercuryhook.def @@ -9,6 +9,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/mu3hook/mu3hook.def b/games/mu3hook/mu3hook.def index ab34a0f..392310a 100644 --- a/games/mu3hook/mu3hook.def +++ b/games/mu3hook/mu3hook.def @@ -15,6 +15,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/swdchook/swdchook.def b/games/swdchook/swdchook.def index 09c40a9..eda115e 100644 --- a/games/swdchook/swdchook.def +++ b/games/swdchook/swdchook.def @@ -9,6 +9,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 diff --git a/games/tokyohook/tokyohook.def b/games/tokyohook/tokyohook.def index 8b30d5f..6a88879 100644 --- a/games/tokyohook/tokyohook.def +++ b/games/tokyohook/tokyohook.def @@ -9,6 +9,16 @@ EXPORTS aime_io_nfc_get_aime_id aime_io_nfc_get_felica_id aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data amDllVideoClose @2 amDllVideoGetVBiosVersion @4 amDllVideoOpen @1 From a3ac466dba880e4bb07087328a27b9dca46699fd Mon Sep 17 00:00:00 2001 From: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> Date: Sun, 8 Feb 2026 11:12:02 +0000 Subject: [PATCH 24/37] Vfs: Add ability to redirect arbitary paths (= serial ports) (#62) As someone running several games on the same cabinet that share most of the hardware (like KCA and FGO), it's a bit of a hassle switching the COM ports of the hardware around all the time. This commit adds the ability to place arbitary path redirections in segatools.ini, which is mostly useful for hardcoded serial ports. Technically this would also allow you to redirect arbitary paths, but I haven't tested that yet. An example that I personally use is: ``` [vfs] amfs=amfs appdata=. option=option ; aime reader redirection0from=\\.\COM10 redirection0to=\\.\COM3 ; rfid reader redirection1from=\\.\COM12 redirection1to=\\.\COM2 ; POP led begone redirection2from=\\.\COM9 redirection2to=\\.\COM90 ; partition to pop redirection3from=\\.\COM11 redirection3to=\\.\COM9 ``` Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/62 Co-authored-by: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> Co-committed-by: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> --- common/platform/config.c | 27 +++++++++++++++++++ common/platform/vfs.c | 56 ++++++++++++++++++++++++++++++++++++++++ common/platform/vfs.h | 5 ++++ doc/config/common.md | 13 ++++++++++ meson.build | 3 +++ meson_options.txt | 5 ++++ 6 files changed, 109 insertions(+) diff --git a/common/platform/config.c b/common/platform/config.c index 4f948d0..d089977 100644 --- a/common/platform/config.c +++ b/common/platform/config.c @@ -24,6 +24,7 @@ #include "platform/vfs.h" #include "platform/system.h" #include "platform/openssl.h" +#include "util/dprintf.h" void platform_config_load(struct platform_config *cfg, const wchar_t *filename) @@ -337,6 +338,32 @@ void vfs_config_load(struct vfs_config *cfg, const wchar_t *filename) cfg->option, _countof(cfg->option), filename); + + for (int i = 0; i < MAX_REDIRECTIONS; i++){ + wchar_t key[32]; + wsprintfW(key, L"redirection%dfrom", i); + GetPrivateProfileStringW( + L"vfs", + key, + L"", + cfg->redirections_from[i], + _countof(cfg->redirections_from[i]), + filename); + wsprintfW(key, L"redirection%dto", i); + GetPrivateProfileStringW( + L"vfs", + key, + L"", + cfg->redirections_to[i], + _countof(cfg->redirections_to[i]), + filename); + + cfg->redirections_from_len[i] = (int)wcslen(cfg->redirections_from[i]); + + if (cfg->redirections_from_len[i] > 0) { + dprintf("Vfs: Set up custom redirection from %ls to %ls\n", cfg->redirections_from[i], cfg->redirections_to[i]); + } + } } void system_config_load(struct system_config *cfg, const wchar_t *filename) diff --git a/common/platform/vfs.c b/common/platform/vfs.c index 22f0583..97f7c13 100644 --- a/common/platform/vfs.c +++ b/common/platform/vfs.c @@ -36,6 +36,10 @@ static HRESULT vfs_path_hook_apm( const wchar_t *src, wchar_t *dest, size_t *count); +static HRESULT vfs_custom_path_hook( + const wchar_t *src, + wchar_t *dest, + size_t *count); static HRESULT vfs_reg_read_amfs(void *bytes, uint32_t *nbytes); static HRESULT vfs_reg_read_appdata(void *bytes, uint32_t *nbytes); @@ -217,6 +221,12 @@ HRESULT vfs_hook_init(const struct vfs_config *config, const char* game_id) } } + hr = path_hook_push(vfs_custom_path_hook); + + if (FAILED(hr)) { + return hr; + } + hr = reg_hook_push_key( HKEY_LOCAL_MACHINE, L"SYSTEM\\SEGA\\SystemProperty\\mount", @@ -601,3 +611,49 @@ static wchar_t* hook_AppImage_getOptionMountRootPath() return path; } + +static HRESULT vfs_custom_path_hook( + const wchar_t *src, + wchar_t *dest, + size_t *count){ + + assert(src != NULL); + assert(count != NULL); + + /* Case-insensitive check to see if src starts with one of our custom paths */ + + for (int i = 0; i < MAX_REDIRECTIONS; i++){ + + wchar_t* from = vfs_config.redirections_from[i]; + wchar_t* to = vfs_config.redirections_to[i]; + + if (from[0] == '\0' || to[0] == '\0'){ + return S_FALSE; + } + + if (path_compare_w(src, from, vfs_config.redirections_from_len[i]) != 0) { + continue; + } + + size_t required = wcslen(to) + 1; + +#if defined(LOG_CUSTOM_VFS) + dprintf("Vfs: Redirection matched: %ls -> %ls\n", from, to); +#endif + + if (dest != NULL) { + + if (required > *count) { + return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); + } + + wcscpy_s(dest, *count, to); + } + + *count = required; + + break; + } + + return S_OK; +} diff --git a/common/platform/vfs.h b/common/platform/vfs.h index a7e513f..7502053 100644 --- a/common/platform/vfs.h +++ b/common/platform/vfs.h @@ -5,11 +5,16 @@ #include #include +#define MAX_REDIRECTIONS 64 + struct vfs_config { bool enable; wchar_t amfs[MAX_PATH]; wchar_t appdata[MAX_PATH]; wchar_t option[MAX_PATH]; + wchar_t redirections_from[MAX_REDIRECTIONS][MAX_PATH]; + int redirections_from_len[MAX_REDIRECTIONS]; + wchar_t redirections_to[MAX_REDIRECTIONS][MAX_PATH]; }; HRESULT vfs_hook_init(const struct vfs_config *config, const char* game_id); diff --git a/doc/config/common.md b/doc/config/common.md index 3e9361e..666be50 100644 --- a/doc/config/common.md +++ b/doc/config/common.md @@ -722,3 +722,16 @@ Enabling full virtualization will virtualize ALL writes to the C:\ drive. Using unless you know what you're doing! The Windows directory is always excluded from virtualization. + +### `redirection#from`, `redirection#to` + +Default: Empty string + +Advanced feature intended for owners of real hardware. This allows arbitrary file reads to be redirected to other paths. Since serial ports are also "file" reads, this can be used to redirect hardcoded COM ports to other ports. Up to 64 redirections can be used by incrementing the number in the key: `redirection1from`, `redirection2from`, ... + +Example for redirecting COM 5 to COM 10: + +``` +redirection0from=\\.\COM5 +redirection0to=\\.\COM10 +``` \ No newline at end of file diff --git a/meson.build b/meson.build index b4376bb..1e88269 100644 --- a/meson.build +++ b/meson.build @@ -88,6 +88,9 @@ endif if get_option('log_all') or get_option('log_ewf') add_project_arguments('-DLOG_EWF', language : 'c') endif +if get_option('log_all') or get_option('log_custom_vfs') + add_project_arguments('-DLOG_CUSTOM_VFS', language: 'c') +endif shlwapi_lib = cc.find_library('shlwapi') dinput8_lib = cc.find_library('dinput8') diff --git a/meson_options.txt b/meson_options.txt index 7cce000..957503d 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -83,3 +83,8 @@ option('log_ewf', value : false, description : 'Enable debug logging for file system virtualization' ) +option('log_custom_vfs', + type : 'boolean', + value : false, + description : 'Enable debug logging for custom path redirections' +) From 351188d285c068d7c81e92f1ef1146c3d6fcf392 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Sun, 8 Feb 2026 12:21:04 +0100 Subject: [PATCH 25/37] change MAX_REDIRECTIONS from 64 to 8 --- common/platform/vfs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/platform/vfs.h b/common/platform/vfs.h index 7502053..9ff8111 100644 --- a/common/platform/vfs.h +++ b/common/platform/vfs.h @@ -5,7 +5,7 @@ #include #include -#define MAX_REDIRECTIONS 64 +#define MAX_REDIRECTIONS 8 struct vfs_config { bool enable; From c24363b65d88a64b647122535b2207b39ac57c86 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Sun, 8 Feb 2026 13:56:30 +0100 Subject: [PATCH 26/37] diva: add ELO touch support, thanks @emihiok --- common/board/elo-cmd.h | 77 ++++++ common/board/elo-frame.c | 158 +++++++++++ common/board/elo-frame.h | 21 ++ common/board/meson.build | 3 + dist/diva/segatools.ini | 8 +- games/divahook/config.c | 10 + games/divahook/config.h | 3 +- games/divahook/diva-dll.c | 57 ++-- games/divahook/diva-dll.h | 3 + games/divahook/divahook.def | 3 + games/divahook/dllmain.c | 8 +- games/divahook/elo.c | 528 ++++++++++++++++++++++++++++++++++++ games/divahook/elo.h | 12 + games/divahook/meson.build | 2 + games/divaio/config.c | 9 + games/divaio/config.h | 1 + games/divaio/divaio.c | 195 ++++++++++++- games/divaio/divaio.h | 57 ++++ 18 files changed, 1124 insertions(+), 31 deletions(-) create mode 100644 common/board/elo-cmd.h create mode 100644 common/board/elo-frame.c create mode 100644 common/board/elo-frame.h create mode 100644 games/divahook/elo.c create mode 100644 games/divahook/elo.h diff --git a/common/board/elo-cmd.h b/common/board/elo-cmd.h new file mode 100644 index 0000000..510f727 --- /dev/null +++ b/common/board/elo-cmd.h @@ -0,0 +1,77 @@ +#pragma once + +#include "board/elo-frame.h" + +enum { + ELO_CMD_TOUCH = 'T', + ELO_CMD_QUERY_ACKNOWLEDGE = 'a', + ELO_CMD_ACKNOWLEDGE = 'A', + ELO_CMD_SET_RESET = 'R', + ELO_CMD_QUERY_PARAMETER = 'p', + ELO_CMD_SET_PARAMETER = 'P' +}; + +enum { + ELO_ERR_NONE = '0', + ELO_ERR_DIVIDE_BY_ZERO = '1', + ELO_ERR_BAD_INPUT_PACKET = '2', + ELO_ERR_BAD_INPUT_CHECKSUM = '3', + ELO_ERR_INPUT_PACKET_OVERRUN = '4', + ELO_ERR_ILLEGAL_COMMAND = '5', + ELO_ERR_CALIBRATION_CANCELLED = '6', + ELO_ERR_BAD_SERIAL_SETUP = '8', + ELO_ERR_INVALID_NVRAM = '9', + ELO_ERR_SET_UNAVAILABLE = 'A', + ELO_ERR_UNSUPPORTED_IN_FIRM = 'B', + ELO_ERR_ILLEGAL_SUBCOMMAND = 'C', + ELO_ERR_OPERAND_OUT_OF_RANGE = 'D', + ELO_ERR_INVALID_TYPE = 'E', + ELO_ERR_FATAL_ERROR = 'F', + ELO_ERR_QUERY_UNAVAILABLE = 'G', + ELO_ERR_INVALID_INTERRUPT = 'H', + ELO_ERR_NVRAM_FAILURE = 'I', + ELO_ERR_INVALID_ADDRESS = 'J', + ELO_ERR_FAILED_POWER_ON = 'K', +}; + +/* Touch report packet (IntelliTouch format) */ +struct elo_packet_touch { + struct elo_packet_hdr hdr; + uint8_t status; /* Touch status flags */ + uint8_t x_low; /* X coordinate low byte */ + uint8_t x_high; /* X coordinate high byte */ + uint8_t y_low; /* Y coordinate low byte */ + uint8_t y_high; /* Y coordinate high byte */ + uint8_t z_low; /* Z coordinate (pressure) low byte */ + uint8_t z_high; /* Z coordinate (pressure) high byte */ +}; + +/* Acknowledge packet */ +struct elo_packet_acknowledge { + struct elo_packet_hdr hdr; + uint8_t error_code[4]; /* Up to 4 error codes */ +}; + +/* Reset packet */ +struct elo_packet_reset { + struct elo_packet_hdr hdr; + uint8_t r_type; /* '0' = hard reset, '1' = soft reset, '2' = NVRAM reset */ +}; + +/* Parameter packet */ +struct elo_packet_parameter { + struct elo_packet_hdr hdr; + uint8_t io; /* I/O type: '0' = serial */ + uint8_t ser1; /* Serial parameter 1 (baud rate, parity, etc.) */ + uint8_t ser2; /* Serial parameter 2 (handshaking, etc.) */ +}; + +/* Union of all packet types for easier handling */ +union elo_packet_any { + struct elo_packet_hdr hdr; + struct elo_packet_touch touch; + struct elo_packet_acknowledge ack; + struct elo_packet_reset reset; + struct elo_packet_parameter param; + uint8_t bytes[10]; +}; \ No newline at end of file diff --git a/common/board/elo-frame.c b/common/board/elo-frame.c new file mode 100644 index 0000000..cf29f75 --- /dev/null +++ b/common/board/elo-frame.c @@ -0,0 +1,158 @@ +/* + SmartSet protocol framing implementation + + The SmartSet protocol uses the following frame format: + [0x55] [CMD] [DATA...] [CHECKSUM] + + - Lead byte: 0x55 (alternating bit pattern 01010101) + - Command byte: ASCII command character + - Data: Variable length based on command + - Checksum: Sum of all bytes (including lead) + 0xAA, low byte only +*/ + +#include + +#include +#include +#include +#include + +#include "board/elo-cmd.h" +#include "board/elo-frame.h" + +#include "hook/iobuf.h" + +#include "util/dprintf.h" + +static void elo_frame_sync(struct iobuf *src); +static HRESULT elo_frame_accept(const struct iobuf *dest); + +/* Checksum offset to cancel out the lead byte */ +#define ELO_CHECKSUM_OFFSET 0xAA + +static void elo_frame_sync(struct iobuf *src) +{ + size_t i; + + for (i = 0; i < src->pos && src->bytes[i] != ELO_FRAME_LEAD; i++); + + src->pos -= i; + memmove(&src->bytes[0], &src->bytes[i], i); +} + +static HRESULT elo_frame_accept(const struct iobuf *dest) +{ + uint8_t checksum; + uint8_t calc_checksum; + size_t i; + + if (dest->pos < 10) { + return S_FALSE; + } + + /* Try to validate checksum with current buffer length + Calculate checksum (sum of all bytes including lead + 0xAA) */ + calc_checksum = ELO_CHECKSUM_OFFSET; + + for (i = 0; i < dest->pos - 1; i++) { + calc_checksum += dest->bytes[i]; + } + + calc_checksum &= 0xFF; + + /* Check if last byte matches calculated checksum */ + checksum = dest->bytes[dest->pos - 1]; + + if (checksum != calc_checksum) { + dprintf("Checksum missmatch: %d != %d\n", checksum, calc_checksum); + return S_FALSE; + } + + return S_OK; +} + +HRESULT elo_frame_decode(struct iobuf *dest, struct iobuf *src) +{ + uint8_t byte; + size_t i; + HRESULT hr; + + assert(dest != NULL); + assert(dest->bytes != NULL || dest->nbytes == 0); + assert(dest->pos <= dest->nbytes); + assert(src != NULL); + assert(src->bytes != NULL || src->nbytes == 0); + assert(src->pos <= src->nbytes); + + elo_frame_sync(src); + + dest->pos = 0; + + for (i = 0, hr = S_FALSE; i < src->pos && hr == S_FALSE; i++) { + byte = src->bytes[i]; + + if (dest->pos >= dest->nbytes) { + hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); + } else if (i == 0 && byte != 0x55) { + /* Invalid lead byte */ + hr = E_FAIL; + } else { + dest->bytes[dest->pos++] = byte; + } + + if (SUCCEEDED(hr)) { + hr = elo_frame_accept(dest); + } + } + + if (hr != S_FALSE) { + memmove(&src->bytes[0], &src->bytes[i], src->pos - i); + src->pos -= i; + + /* If accepted, remove checksum from destination */ + if (hr == S_OK) { + dest->pos--; + } + } + + return hr; +} + +HRESULT elo_frame_encode( + struct iobuf *dest, + const void *ptr, + size_t nbytes) +{ + const uint8_t *src; + uint8_t checksum; + size_t i; + + assert(dest != NULL); + assert(dest->bytes != NULL || dest->nbytes == 0); + assert(dest->pos <= dest->nbytes); + assert(ptr != NULL); + + src = ptr; + + /* Requires exactly 10 bytes */ + if (dest->pos + 10 > dest->nbytes) { + return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); + } + + /* First byte must be lead-in */ + assert(nbytes >= 1 && src[0] == ELO_FRAME_LEAD); + + checksum = ELO_CHECKSUM_OFFSET; + + /* Write exactly 9 bytes (lead-in + 8 data bytes) */ + for (i = 0; i < 9; i++) { + uint8_t b = (i < nbytes) ? src[i] : 0x00; + dest->bytes[dest->pos++] = b; + checksum += b; + } + + /* Append checksum as 10th byte */ + dest->bytes[dest->pos++] = checksum; + + return S_OK; +} diff --git a/common/board/elo-frame.h b/common/board/elo-frame.h new file mode 100644 index 0000000..ae81883 --- /dev/null +++ b/common/board/elo-frame.h @@ -0,0 +1,21 @@ +#pragma once + + +#include +#include + +#include "hook/iobuf.h" + +/* SmartSet Protocol Lead-in byte */ +enum { + ELO_FRAME_LEAD = 0x55 +}; + +struct elo_packet_hdr { + uint8_t lead; + uint8_t cmd; +}; + +HRESULT elo_frame_decode(struct iobuf *dest, struct iobuf *src); + +HRESULT elo_frame_encode(struct iobuf *dest, const void *src, size_t nbytes); diff --git a/common/board/meson.build b/common/board/meson.build index a9c24ea..ca3c2ca 100644 --- a/common/board/meson.build +++ b/common/board/meson.build @@ -13,6 +13,9 @@ board_lib = static_library( 'aime-dll.h', 'config.c', 'config.h', + 'elo-cmd.h', + 'elo-frame.c', + 'elo-frame.h', 'guid.c', 'guid.h', 'io3.c', diff --git a/dist/diva/segatools.ini b/dist/diva/segatools.ini index 03cceed..e37d2c4 100644 --- a/dist/diva/segatools.ini +++ b/dist/diva/segatools.ini @@ -77,6 +77,10 @@ serialNo=ACAE01A99999999 ; Misc. hooks settings ; ----------------------------------------------------------------------------- +[touch] +; Enable/Disable Elo Touchsystems 2701 (SEGA 838-14772) emulation. +enable=1 + [gfx] ; Enables the graphics hook. enable=1 @@ -87,7 +91,6 @@ framed=0 ; Enable DPI awareness for the game process, preventing Windows from stretching the game window if a DPI scaling higher than 100% is used dpiAware=1 - ; ----------------------------------------------------------------------------- ; Custom IO settings ; ----------------------------------------------------------------------------- @@ -122,6 +125,9 @@ service=0x71 ; Keyboard button to increment coin counter. Default is the F3 key. coin=0x72 +; Touchscreen input type, currently only "mouse" is supported. +mode=mouse + [slider] cell1=0x51 cell2=0x57 diff --git a/games/divahook/config.c b/games/divahook/config.c index 142cf73..45d1968 100644 --- a/games/divahook/config.c +++ b/games/divahook/config.c @@ -40,6 +40,15 @@ void slider_config_load(struct slider_config *cfg, const wchar_t *filename) cfg->enable = GetPrivateProfileIntW(L"slider", L"enable", 1, filename); } +void elo_config_load(struct elo_config *cfg, const wchar_t *filename) +{ + assert(cfg != NULL); + assert(filename != NULL); + + cfg->enable = GetPrivateProfileIntW(L"touch", L"enable", 1, filename); + cfg->port_no = GetPrivateProfileIntW(L"touch", L"port_no", 0, filename); +} + void diva_hook_config_load( struct diva_hook_config *cfg, const wchar_t *filename) @@ -54,4 +63,5 @@ void diva_hook_config_load( gfx_config_load(&cfg->gfx, filename); diva_dll_config_load(&cfg->dll, filename); slider_config_load(&cfg->slider, filename); + elo_config_load(&cfg->touch, filename); } diff --git a/games/divahook/config.h b/games/divahook/config.h index 694b055..3828252 100644 --- a/games/divahook/config.h +++ b/games/divahook/config.h @@ -15,6 +15,7 @@ #include "divahook/diva-dll.h" #include "divahook/slider.h" +#include "divahook/elo.h" struct diva_hook_config { struct platform_config platform; @@ -22,9 +23,9 @@ struct diva_hook_config { struct aime_config aime; struct dvd_config dvd; struct gfx_config gfx; - struct touch_screen_config touch; struct diva_dll_config dll; struct slider_config slider; + struct elo_config touch; }; void diva_dll_config_load( diff --git a/games/divahook/diva-dll.c b/games/divahook/diva-dll.c index 9a5d057..a02023e 100644 --- a/games/divahook/diva-dll.c +++ b/games/divahook/diva-dll.c @@ -8,6 +8,11 @@ #include "util/dll-bind.h" #include "util/dprintf.h" +enum { + DIVA_DLL_SYM_COUNT_V100 = 7, + DIVA_DLL_SYM_COUNT_V101 = 12, +}; + const struct dll_bind_sym diva_dll_syms[] = { { .sym = "diva_io_jvs_init", @@ -36,22 +41,18 @@ const struct dll_bind_sym diva_dll_syms[] = { }, { .sym = "diva_io_led_set_leds", .off = offsetof(struct diva_dll, led_set_leds), + }, { + .sym = "diva_io_touch_init", + .off = offsetof(struct diva_dll, touch_init), + }, { + .sym = "diva_io_touch_start", + .off = offsetof(struct diva_dll, touch_start), + }, { + .sym = "diva_io_touch_stop", + .off = offsetof(struct diva_dll, touch_stop), } }; -/* Helper function to determine upon dll_bind failure whether the required functions were found - NOTE: relies on symbols order declared above */ -static HRESULT has_enough_symbols(uint16_t version, uint8_t count) -{ - if ( version < 0x0101 && count == 7 ) - return S_OK; - - if ( version >= 0x0101 && count == 9 ) - return S_OK; - - return E_FAIL; -} - struct diva_dll diva_dll; // Copypasta DLL binding and diagnostic message boilerplate. @@ -67,6 +68,7 @@ HRESULT diva_dll_init(const struct diva_dll_config *cfg, HINSTANCE self) HINSTANCE owned; HINSTANCE src; HRESULT hr; + size_t sym_count; assert(cfg != NULL); assert(self != NULL); @@ -111,24 +113,25 @@ HRESULT diva_dll_init(const struct diva_dll_config *cfg, HINSTANCE self) } sym = diva_dll_syms; - const struct dll_bind_sym *init_sym = &sym[0]; - hr = dll_bind(&diva_dll, src, &sym, _countof(diva_dll_syms)); + switch (diva_dll.api_version) { + case 0x0101: + sym_count = DIVA_DLL_SYM_COUNT_V101; + break; + + default: + sym_count = DIVA_DLL_SYM_COUNT_V100; + break; + } + hr = dll_bind(&diva_dll, src, &sym, sym_count); if (FAILED(hr)) { if (src != self) { - // Might still be ok depending on external dll API version - int bind_count = sym - init_sym; - if ( has_enough_symbols(diva_dll.api_version, bind_count) == S_OK ) - { - hr = S_OK; - } else { - dprintf("Diva IO: Custom IO DLL does not provide function " - "\"%s\". Please contact your IO DLL's developer for " - "further assistance.\n", - sym->sym); + dprintf("Diva IO: Custom IO DLL does not provide function " + "\"%s\". Please contact your IO DLL's developer for " + "further assistance.\n", + sym->sym); - goto end; - } + goto end; } else { dprintf("Internal error: could not reflect \"%s\"\n", sym->sym); } diff --git a/games/divahook/diva-dll.h b/games/divahook/diva-dll.h index 67cf335..5b8c8d0 100644 --- a/games/divahook/diva-dll.h +++ b/games/divahook/diva-dll.h @@ -15,6 +15,9 @@ struct diva_dll { void (*slider_set_leds)(const uint8_t *rgb); HRESULT (*led_init)(void); void (*led_set_leds)(uint8_t board, const uint8_t *rgb); + HRESULT (*touch_init)(void); + void (*touch_start)(diva_io_touch_callback_t callback); + void (*touch_stop)(void); }; struct diva_dll_config { diff --git a/games/divahook/divahook.def b/games/divahook/divahook.def index e9e430d..267c7dc 100644 --- a/games/divahook/divahook.def +++ b/games/divahook/divahook.def @@ -33,3 +33,6 @@ EXPORTS diva_io_slider_stop diva_io_led_init diva_io_led_set_leds + diva_io_touch_init + diva_io_touch_start + diva_io_touch_stop diff --git a/games/divahook/dllmain.c b/games/divahook/dllmain.c index 52789b1..33d412e 100644 --- a/games/divahook/dllmain.c +++ b/games/divahook/dllmain.c @@ -4,7 +4,7 @@ Devices JVS: 837-14572 "Type 3" I/O Board - COM1: 3M Touch Systems 78-0011-2353-4 Touch Controller Board + COM1: 838-14772 (Elo Touchsystems 2701) Touch Controller Board COM10: TN32MSEC003S "Gen 1" Aime Reader COM11: 837-15275 Touch Slider */ @@ -105,6 +105,12 @@ static DWORD CALLBACK diva_pre_startup(void) goto fail; } + hr = elo_hook_init(&diva_hook_cfg.touch, 1); + + if (FAILED(hr)) { + goto fail; + } + if (diva_hook_cfg.amex.jvs.enable && diva_hook_cfg.amex.jvs.foreground) { fgdet_init(L"Hatsune Miku Project DIVA Arcade Future Tone", false); } diff --git a/games/divahook/elo.c b/games/divahook/elo.c new file mode 100644 index 0000000..2948765 --- /dev/null +++ b/games/divahook/elo.c @@ -0,0 +1,528 @@ +/* + Elo Touchsystems 2701 (Sega 838-14772) touchscreen controller emulator + + This board only supports single-touch. + + References: + + Elo 'SmartSet' protocol + https://archive.org/details/manualzilla-id-5973842 + + Credits: + + emihiok +*/ + +#include + +#include +#include +#include +#include +#include + +#include "board/elo-cmd.h" +#include "board/elo-frame.h" + +#include "divahook/elo.h" +#include "divahook/diva-dll.h" + +#include "hook/iobuf.h" +#include "hook/iohook.h" +#include "hook/table.h" + +#include "hooklib/uart.h" + +#include "util/dprintf.h" +#include "util/dump.h" + +static HRESULT elo_handle_irp(struct irp *irp); +static HRESULT elo_handle_irp_locked(struct irp *irp); + +static HRESULT elo_req_dispatch(const union elo_packet_any *req); +static HRESULT elo_req_acknowledge(void); +static HRESULT elo_req_reset(const struct elo_packet_reset *req); +static HRESULT elo_query_parameter(const struct elo_packet_parameter *req); +static HRESULT elo_set_parameter(const struct elo_packet_parameter *req); + +static HRESULT elo_send_acknowledge(void); +static void elo_send_touch( + const uint8_t status, + const uint16_t x, + const uint16_t y, + const uint8_t id); + +static void elo_set_error(uint8_t error_code); +static void elo_clear_error(void); + +static CRITICAL_SECTION elo_lock; +static struct uart elo_uart; +static uint8_t elo_written_bytes[520]; +static uint8_t elo_readable_bytes[520]; + +static uint8_t elo_last_error[4]; +static bool elo_acknowledge_required; +static bool elo_error_report_required; +static bool elo_touch_started; + +/* Cursor specific API hooks */ + +static HCURSOR hook_SetCursor(HCURSOR hCursor); +static HCURSOR (*next_SetCursor)(HCURSOR hCursor); + +static const struct hook_symbol cursor_syms[] = { + { + .name = "SetCursor", + .patch = hook_SetCursor, + .link = (void **) &next_SetCursor + }, +}; + +HRESULT elo_hook_init( + const struct elo_config *cfg, + unsigned int port_no) +{ + assert(cfg != NULL); + + if (!cfg->enable) { + return S_FALSE; + } + + if (cfg->port_no != 0) { + port_no = cfg->port_no; + } + + hook_table_apply( + NULL, + "user32.dll", + cursor_syms, + _countof(cursor_syms)); + + InitializeCriticalSection(&elo_lock); + + uart_init(&elo_uart, port_no); + elo_uart.baud.BaudRate = 9600; + elo_uart.written.bytes = elo_written_bytes; + elo_uart.written.nbytes = sizeof(elo_written_bytes); + elo_uart.readable.bytes = elo_readable_bytes; + elo_uart.readable.nbytes = sizeof(elo_readable_bytes); + + memset(elo_last_error, ELO_ERR_NONE, sizeof(elo_last_error)); + elo_acknowledge_required = false; + elo_error_report_required = false; + elo_touch_started = false; + + return iohook_push_handler(elo_handle_irp); +} + +static HCURSOR hook_SetCursor(HCURSOR hCursor) +{ + HCURSOR fake_cursor; + + if (hCursor) + return next_SetCursor(hCursor); + + fake_cursor = LoadCursorA(NULL, IDC_CROSS); + return next_SetCursor(fake_cursor); +} + + +static HRESULT elo_handle_irp(struct irp *irp) +{ + HRESULT hr; + + assert(irp != NULL); + + if (!uart_match_irp(&elo_uart, irp)) { + return iohook_invoke_next(irp); + } + + EnterCriticalSection(&elo_lock); + hr = elo_handle_irp_locked(irp); + LeaveCriticalSection(&elo_lock); + + return hr; +} + +static HRESULT elo_handle_irp_locked(struct irp *irp) +{ + union elo_packet_any req; + struct iobuf req_iobuf; + HRESULT hr; + + assert(diva_dll.touch_init != NULL); + + if (irp->op == IRP_OP_OPEN) { + dprintf("Elo Touch: Starting DIVA Touch backend\n"); + hr = diva_dll.touch_init(); + + if (FAILED(hr)) { + dprintf("Elo Touch: Backend error, touchscreen disconnected: %x\n", + (int) hr); + + return hr; + } + + dprintf("Elo Touch: Start touch thread\n"); + diva_dll.touch_start(elo_send_touch); + elo_touch_started = true; + } + + hr = uart_handle_irp(&elo_uart, irp); + + if (FAILED(hr) || irp->op != IRP_OP_WRITE) { + return hr; + } + + for (;;) { + if (elo_acknowledge_required) { + hr = elo_send_acknowledge(); + + if (FAILED(hr)) { + dprintf("Elo Touch: Acknowledge failed: %x\n", (int) hr); + } + } + +#if 0 + dprintf("Elo Touch DEBUG: TX Buffer:\n"); + dump_iobuf(&elo_uart.written); +#endif + + req_iobuf.bytes = req.bytes; + req_iobuf.nbytes = sizeof(req.bytes); + req_iobuf.pos = 0; + + hr = elo_frame_decode(&req_iobuf, &elo_uart.written); + + if (hr != S_OK) { + if (hr == HRESULT_FROM_WIN32(ERROR_CRC)) { + elo_set_error(ELO_ERR_BAD_INPUT_CHECKSUM); + } + + if (FAILED(hr)) { + dprintf("Elo Touch: Deframe error: %x\n", (int) hr); + } + + return hr; + } + +#if 0 + dprintf("Elo Touch DEBUG: Deframe Buffer:\n"); + dump_iobuf(&req_iobuf); +#endif + hr = elo_req_dispatch(&req); + + if (FAILED(hr)) { + dprintf("Elo Touch: Processing error: %x\n", (int) hr); + } + + if (SUCCEEDED(hr)) { + if (!elo_touch_started) { + dprintf("Elo Touch: Restart touch reports\n"); + diva_dll.touch_start(elo_send_touch); + elo_touch_started = true; + } + } + } +} + +static HRESULT elo_req_dispatch(const union elo_packet_any *req) +{ + switch (req->hdr.cmd) { + case ELO_CMD_QUERY_ACKNOWLEDGE: + return elo_req_acknowledge(); + + case ELO_CMD_SET_RESET: + return elo_req_reset(&req->reset); + + case ELO_CMD_QUERY_PARAMETER: + return elo_query_parameter(&req->param); + + case ELO_CMD_SET_PARAMETER: + return elo_set_parameter(&req->param); + + case ELO_CMD_ACKNOWLEDGE: + dprintf("Elo Touch: Invalid command, ACK cannot be set\n"); + elo_set_error(ELO_ERR_SET_UNAVAILABLE); + + return E_INVALIDARG; + + case 'r': + dprintf("Elo Touch: Invalid command, Reset cannot be queried\n"); + elo_set_error(ELO_ERR_QUERY_UNAVAILABLE); + + return E_INVALIDARG; + + default: + dprintf("Elo Touch: Unhandled command %02x\n", req->hdr.cmd); + elo_set_error(ELO_ERR_ILLEGAL_COMMAND); + + return S_OK; + } +} + +static HRESULT elo_req_acknowledge(void) +{ + dprintf("Elo Touch: Query acknowledge\n"); + + elo_acknowledge_required = true; + + return S_OK; +} + +static HRESULT elo_req_reset(const struct elo_packet_reset *req) +{ + if (req->r_type == '0') { + dprintf("Elo Touch: Hard reset\n"); + elo_clear_error(); + + /* IO DLL worker thread might attempt to invoke the callback (which needs + to take elo_lock, which we are currently holding) before noticing that + it needs to shut down. Unlock here so that we don't deadlock in that + situation. */ + + LeaveCriticalSection(&elo_lock); + dprintf("Elo Touch: Stop touch reports\n"); + diva_dll.touch_stop(); + elo_touch_started = false; + EnterCriticalSection(&elo_lock); + + /* No response or ack whatsoever as controller fully reboots. + Game will wait approx. 5 seconds before sending the next command */ + return S_OK; + } else if (req->r_type == '1') { + dprintf("Elo Touch: Soft reset\n"); + } else { + /* Type '2' (reset to NVRAM defaults) is only available on COACh IV + controllers, which the 2701 does not have. */ + dprintf("Elo Touch: Warning -- Unknown reset type %02x\n", req->r_type); + } + + union elo_packet_any resp; + + memset(&resp, 0, sizeof(resp)); + resp.hdr.lead = ELO_FRAME_LEAD; + resp.hdr.cmd = ELO_CMD_SET_RESET; + + /* Response only, message is not acknowledged */ + return elo_frame_encode(&elo_uart.readable, &resp, sizeof(resp)); +} + +static HRESULT elo_query_parameter(const struct elo_packet_parameter *req) +{ + dprintf("Elo Touch: Query parameters\n"); + + struct elo_packet_parameter resp; + + memset(&resp, 0, sizeof(resp)); + resp.hdr.lead = ELO_FRAME_LEAD; + resp.hdr.cmd = ELO_CMD_SET_PARAMETER; + + resp.io = '0'; // Serial + resp.ser1 = 0b00000110; // 9600 bps, 8-N-1 + resp.ser2 = 0b00000100; // Hardware handshaking enabled + + elo_acknowledge_required = true; + + return elo_frame_encode(&elo_uart.readable, &resp, sizeof(resp)); +} + +static HRESULT elo_set_parameter(const struct elo_packet_parameter *req) +{ + dprintf("Elo Touch: Set parameters\n"); + dprintf("Elo Touch: --- Begin Parameter Receive ---\n"); + + bool bad_bit_flag = false; + bool unimpl_flag = false; + + if (req->io == '0') { + dprintf("Elo Touch: Param: Serial interface\n"); + } else { + dprintf("Elo Touch: Param: Unknown IO preset: %02x\n", req->io); + unimpl_flag = true; + goto end; + } + + switch (req->ser1 & 0b111) { + case 0b101: + dprintf("Elo Touch: Param: Baudrate 4800bps\n"); + elo_uart.baud.BaudRate = 4800; + break; + case 0b110: + dprintf("Elo Touch: Param: Baudrate 9600bps\n"); + elo_uart.baud.BaudRate = 9600; + break; + case 0b111: + dprintf("Elo Touch: Param: Baudrate 19200bps\n"); + elo_uart.baud.BaudRate = 19200; + break; + default: + dprintf("Elo Touch: Param: Unknown Serial1 preset: %02x\n", req->ser1); + unimpl_flag = true; + goto end; + } + + for (int i = 0 ; i < 8 ; i++) { + bool set = (req->ser2) & (1 << i); + switch (i) { + case 0: + if (set) dprintf("Elo Touch: Param: Checksum required\n"); + break; + case 1: + if (set) dprintf("Elo Touch: Param: Software handshaking " + "enabled\n"); + break; + case 2: + if (set) { + dprintf("Elo Touch: Param: Hardware handshaking enabled\n"); + elo_uart.handflow.ControlHandShake = + SERIAL_DTR_CONTROL | SERIAL_DTR_HANDSHAKE; + elo_uart.handflow.FlowReplace = + SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE; + } + break; + case 3: + if (set) dprintf("Elo Touch: Param: Invert hardware " + "handshaking\n"); + break; + /* Reserved bits should not be set */ + case 4: + case 5: + case 6: + if (set) bad_bit_flag = true; + break; + case 7: + if (set) dprintf("Elo Touch: Param: Full duplex\n"); + break; + default: + break; + } + } + +end: + dprintf("Elo Touch: --- End Parameter Receive ---\n"); + + elo_acknowledge_required = true; + + if (bad_bit_flag) { + elo_set_error(ELO_ERR_BAD_INPUT_PACKET); + + return E_INVALIDARG; + } else if (unimpl_flag) { + elo_set_error(ELO_ERR_BAD_SERIAL_SETUP); + + return E_NOTIMPL; + } + + return S_OK; +} + +static HRESULT elo_send_acknowledge(void) +{ + dprintf("Elo Touch: Acknowledge\n"); + + struct elo_packet_acknowledge resp; + HRESULT hr; + + memset(&resp, 0, sizeof(resp)); + resp.hdr.lead = ELO_FRAME_LEAD; + resp.hdr.cmd = ELO_CMD_ACKNOWLEDGE; + + for (int i = 0 ; i < sizeof(resp.error_code) ; i++) { + resp.error_code[i] = elo_last_error[i]; + } + + hr = elo_frame_encode(&elo_uart.readable, &resp, sizeof(resp)); + + elo_acknowledge_required = false; + + return hr; +} + +static void elo_send_touch( + const uint8_t status, + const uint16_t x, + const uint16_t y, + const uint8_t id) +{ + if (!elo_touch_started || !status || !id) return; + if (id > 1) return; + + /* Generate "IntelliTouch" packet */ + struct elo_packet_touch resp; + + memset(&resp, 0, sizeof(resp)); + resp.hdr.lead = ELO_FRAME_LEAD; + resp.hdr.cmd = ELO_CMD_TOUCH; + + /* IntelliTouch touch events always have MSB (Z-Axis Valid) set */ + switch (status) { + case DIVA_IO_TOUCH_DOWN: + resp.status |= (1 << 7) | 1; + break; + case DIVA_IO_TOUCH_STREAM: + resp.status |= (1 << 7) | (1 << 1); + break; + case DIVA_IO_TOUCH_LIFTOFF: + resp.status |= (1 << 7) | (1 << 2); + break; + default: + /* No touch events occurred..? */ + return; + } + + /* Touch event detected, report coordinates */ + if ((resp.status & 0b111) != 0) { + /* Max resolution: 4095 x 4095 */ + uint16_t x_restrict = x & 0x0FFF; + uint16_t y_restrict = y & 0x0FFF; + + resp.x_low = x_restrict & UINT8_MAX; + resp.x_high = x_restrict >> 8; + resp.y_low = y_restrict & UINT8_MAX; + resp.y_high = y_restrict >> 8; + resp.z_low = UINT8_MAX; // Ignore pressure for now + resp.z_high = 0; // Unused + } + + if (elo_error_report_required) { + resp.status |= 1 << 4; + } + +#if 0 + dprintf("Elo Touch DEBUG: STATUS:%02x, X LOW:%02x, X HIGH:%02x, " + "Y LOW:%02x, Y HIGH:%02x, Z LOW:%02x, Z HIGH:%02x\n", + resp.status, resp.x_low, resp.x_high, resp.y_low, resp.y_high, + resp.z_low, resp.z_high); +#endif + + EnterCriticalSection(&elo_lock); + elo_frame_encode(&elo_uart.readable, &resp, sizeof(resp)); + LeaveCriticalSection(&elo_lock); + + elo_error_report_required = false; +} + +static void elo_set_error(uint8_t error_code) +{ + bool empty_slot_found = false; + + for (int i = 0 ; i < 4 && empty_slot_found == false ; i++) { + if (elo_last_error[i] == ELO_ERR_NONE) { + elo_last_error[i] = error_code; + empty_slot_found = true; + } + } + + /* All error slots full, overwrite first slot */ + if (!empty_slot_found) { + elo_last_error[0] = error_code; + } + + elo_error_report_required = true; +} + +static void elo_clear_error(void) +{ + memset(&elo_last_error, ELO_ERR_NONE, sizeof(elo_last_error)); +} diff --git a/games/divahook/elo.h b/games/divahook/elo.h new file mode 100644 index 0000000..c754830 --- /dev/null +++ b/games/divahook/elo.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +#include + +struct elo_config { + bool enable; + unsigned int port_no; +}; + +HRESULT elo_hook_init(const struct elo_config *cfg, unsigned int port_no); diff --git a/games/divahook/meson.build b/games/divahook/meson.build index 4c66c00..83c9446 100644 --- a/games/divahook/meson.build +++ b/games/divahook/meson.build @@ -25,6 +25,8 @@ shared_library( 'diva-dll.c', 'diva-dll.h', 'dllmain.c', + 'elo.c', + 'elo.h', 'jvs.c', 'jvs.h', 'slider.c', diff --git a/games/divaio/config.c b/games/divaio/config.c index 110392e..ef5f0fc 100644 --- a/games/divaio/config.c +++ b/games/divaio/config.c @@ -31,6 +31,15 @@ void diva_io_config_load( cfg->vk_service = GetPrivateProfileIntW(L"io3", L"service", VK_F2, filename); cfg->vk_coin = GetPrivateProfileIntW(L"io3", L"coin", VK_F3, filename); + /* Load touch input type. Mouse input by default. */ + GetPrivateProfileStringW( + L"touch", + L"mode", + L"mouse", + cfg->touch_mode, + _countof(cfg->touch_mode), + filename); + for (i = 0 ; i < _countof(cfg->vk_buttons) ; i++) { swprintf_s(key, _countof(key), L"key%i", i + 1); cfg->vk_buttons[i] = GetPrivateProfileIntW( diff --git a/games/divaio/config.h b/games/divaio/config.h index 86654c8..8e01d81 100644 --- a/games/divaio/config.h +++ b/games/divaio/config.h @@ -9,6 +9,7 @@ struct diva_io_config { uint8_t vk_test; uint8_t vk_service; uint8_t vk_coin; + wchar_t touch_mode[16]; }; void diva_io_config_load( diff --git a/games/divaio/divaio.c b/games/divaio/divaio.c index d44d723..e854ee4 100644 --- a/games/divaio/divaio.c +++ b/games/divaio/divaio.c @@ -10,14 +10,28 @@ #include "util/env.h" #include "util/dprintf.h" +#include "util/str.h" static unsigned int __stdcall diva_io_slider_thread_proc(void *ctx); +static unsigned int __stdcall diva_io_touch_thread_proc(void *ctx); + +static HRESULT diva_io_touch_config_apply( + const struct diva_io_config *cfg); + +static const wchar_t app_title[] = L"Hatsune Miku Project DIVA Arcade Future Tone"; static bool diva_io_coin; static uint16_t diva_io_coins; static HANDLE diva_io_slider_thread; static bool diva_io_slider_stop_flag; static struct diva_io_config diva_io_cfg; +static bool diva_io_config_initted = false; +static bool diva_io_window_focus = true; + +static HANDLE diva_io_touch_thread; +static int diva_io_m1 = VK_LBUTTON; +static int diva_io_touch_points = 1; +static bool diva_io_touch_stop_flag; uint16_t diva_io_get_api_version(void) { @@ -26,7 +40,10 @@ uint16_t diva_io_get_api_version(void) HRESULT diva_io_jvs_init(void) { - diva_io_config_load(&diva_io_cfg, get_config_path()); + if (!diva_io_config_initted) { + diva_io_config_load(&diva_io_cfg, get_config_path()); + diva_io_config_initted = true; + } return S_OK; } @@ -39,6 +56,18 @@ void diva_io_jvs_poll(uint8_t *opbtn_out, uint8_t *gamebtn_out) opbtn = 0; + HWND hwnd = GetForegroundWindow(); + wchar_t window_title[MAX_PATH]; + GetWindowTextW(hwnd, window_title, MAX_PATH); + + if (!wstr_eq(window_title, app_title)) { + if (diva_io_window_focus) { + diva_io_window_focus = false; + } + } else if (!diva_io_window_focus) { + diva_io_window_focus = true; + } + if (GetAsyncKeyState(diva_io_cfg.vk_test) & 0x8000) { opbtn |= DIVA_IO_OPBTN_TEST; } @@ -157,3 +186,167 @@ void diva_io_led_set_leds(uint8_t board, const uint8_t *rgb) return; } + +HRESULT diva_io_touch_init() +{ + HRESULT hr; + + if(!diva_io_config_initted) { + diva_io_config_load(&diva_io_cfg, get_config_path()); + diva_io_config_initted = true; + } + + hr = diva_io_touch_config_apply(&diva_io_cfg); + + return hr; +} + +static HRESULT diva_io_touch_config_apply( + const struct diva_io_config *cfg + ) +{ + dprintf("Diva IO: Touch: --- Begin Configuration ---\n"); + dprintf("Diva IO: Touch: Using Elo controller\n"); + + if (wstr_ieq(cfg->touch_mode, L"mouse")) { + dprintf("Diva IO: Touch: Mouse emulation\n"); + + /* Work around GetAsyncKeyState returning physical mouse button states + instead of logical */ + diva_io_m1 = GetSystemMetrics(SM_SWAPBUTTON) ? VK_RBUTTON : VK_LBUTTON; + + } else { + dprintf("Diva IO: Touch: Invalid touch mode \"%S\". Use 'mouse'." + "\n", cfg->touch_mode); + + return E_INVALIDARG; + } + + dprintf("Diva IO: Touch: --- End Configuration ---\n"); + return S_OK; +} + +void diva_io_touch_start(diva_io_touch_callback_t callback) +{ + if (diva_io_touch_thread != NULL) { + return; + } + + diva_io_touch_thread = (HANDLE) _beginthreadex( + NULL, + 0, + diva_io_touch_thread_proc, + callback, + 0, + NULL); +} + +void diva_io_touch_stop(void) +{ + diva_io_touch_stop_flag = true; + + WaitForSingleObject(diva_io_touch_thread, INFINITE); + CloseHandle(diva_io_touch_thread); + diva_io_touch_thread = NULL; + diva_io_touch_stop_flag = false; +} + +static unsigned int __stdcall diva_io_touch_thread_proc(void *ctx) +{ + diva_io_touch_callback_t callback; + HWND hwnd; + POINT point; + BOOL ok; + + uint8_t status = 0; + uint16_t x = 0; + uint16_t y = 0; + uint16_t last_x = 0; + uint16_t last_y = 0; + uint8_t id = 0; + bool touch = false; + + callback = ctx; + + while (!diva_io_touch_stop_flag) { + + if (!diva_io_window_focus) { + status = 0; + x = 0; + y = 0; + id = 0; + touch = false; + goto end; + } + + if (wstr_ieq(diva_io_cfg.touch_mode, L"mouse")) + { + if (GetAsyncKeyState(diva_io_m1) & 0x8000) + { + /* Get cursor location and map to window size */ + ok = GetCursorPos(&point); + + if (!ok) { + status = 0; + x = 0; + y = 0; + id = 0; + touch = false; + goto end; + } + + hwnd = GetForegroundWindow(); + + ok = ScreenToClient(hwnd, &point); + + if (!ok) { + status = 0; + x = 0; + y = 0; + id = 0; + touch = false; + goto end; + } + + /* Set status */ + if (!touch) { + status = DIVA_IO_TOUCH_DOWN; + touch = true; + } else { + status = DIVA_IO_TOUCH_STREAM; + } + + /* Set coordinates */ + if (point.x < 0) point.x = 0; + if (point.y < 0) point.y = 0; + x = (uint16_t)point.x; + y = (uint16_t)point.y; + last_x = x; + last_y = y; + } + else + { + if (touch) { + status = DIVA_IO_TOUCH_LIFTOFF; + x = last_x; + y = last_y; + touch = false; + } else { + /* No touch event */ + status = 0; + x = 0; + y = 0; + } + } + + /* Mouse always acts as single-touch */ + id = 1; + } + +end: + callback(status, x, y, id); + Sleep(1); + } + + return 0; +} diff --git a/games/divaio/divaio.h b/games/divaio/divaio.h index c04dfb0..47be34c 100644 --- a/games/divaio/divaio.h +++ b/games/divaio/divaio.h @@ -27,6 +27,12 @@ enum { DIVA_IO_GAMEBTN_START = 0x10, }; +enum { + DIVA_IO_TOUCH_DOWN = 0x01, + DIVA_IO_TOUCH_STREAM = 0x02, + DIVA_IO_TOUCH_LIFTOFF = 0x04, +}; + enum { /* These are the bitmasks to use when checking which lights are triggered on incoming IO3 GPIO writes. */ @@ -173,3 +179,54 @@ HRESULT diva_io_led_init(void); Minimum API version: 0x0101 */ void diva_io_led_set_leds(uint8_t board, const uint8_t *rgb); + +/* Initialize touchscreen output. This function will be called before any + other diva_io_touch*() function calls. Errors returned from this function will + manifest as a disconnected touchscreen controller. + + All subsequent calls may originate from arbitrary threads and some may + overlap with each other. Ensuring synchronization inside your IO DLL is + your responsibility. + + Minimum API version: 0x0101 */ + +HRESULT diva_io_touch_init(); + +/* Callback function supplied to your IO DLL. + If the id parameter is higher than 1, and the Elo touchscreen controller + (DIVA_IO_TOUCH_CONTROLLER_ELO) is the current touchscreen type, the current + poll will be ignored, as the Elo controller can only recognize single-touch. + Be sure to only allow for single-touch if diva_io_touch_init() was called with + the type parameter set to this touchscreen type. */ + +typedef void (*diva_io_touch_callback_t)( + const uint8_t status, + const uint16_t x, + const uint16_t y, + const uint8_t id); + +/* Start polling the touchscreen. Your DLL must start a polling thread and call + the supplied function periodically from that thread with new status, + coordinates, and ID. + The update interval is up to you, but if your input device doesn't have any + preferred interval then 1 kHz is a reasonable maximum frequency. + + Minimum API version: 0x0101 */ + +void diva_io_touch_start(diva_io_touch_callback_t callback); + +/* Stop polling the touchscreen. You must cease to invoke the input callback + before returning from this function. + + This function will only be called for specific touchscreen types. As of API + version 0x0100, only the Elo touch controller (DIVA_IO_TOUCH_CONTROLLER_ELO) + will make use of this function, and only at startup, where the game requests + the controller to do a hard reset twice. + + Following on from the above, the touchscreen polling loop *will* be + restarted after being stopped in the course of regular operation. Do not + permanently tear down your input driver in response to this function call. + + Minimum API version: 0x0101 */ + +void diva_io_touch_stop(void); From ef3fa640fd1a249be9b7d8477e887bab3e6a5694 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Mon, 9 Feb 2026 21:29:36 +0100 Subject: [PATCH 27/37] diva: small touch controller improvements --- common/board/elo-frame.c | 11 +++++++---- games/divaio/divaio.c | 20 +++++++------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/common/board/elo-frame.c b/common/board/elo-frame.c index cf29f75..baa5f6d 100644 --- a/common/board/elo-frame.c +++ b/common/board/elo-frame.c @@ -30,6 +30,9 @@ static HRESULT elo_frame_accept(const struct iobuf *dest); /* Checksum offset to cancel out the lead byte */ #define ELO_CHECKSUM_OFFSET 0xAA +/* SmartSet frame length */ +#define ELO_FRAME_LENGTH 10 + static void elo_frame_sync(struct iobuf *src) { size_t i; @@ -46,7 +49,7 @@ static HRESULT elo_frame_accept(const struct iobuf *dest) uint8_t calc_checksum; size_t i; - if (dest->pos < 10) { + if (dest->pos < ELO_FRAME_LENGTH) { return S_FALSE; } @@ -93,7 +96,7 @@ HRESULT elo_frame_decode(struct iobuf *dest, struct iobuf *src) if (dest->pos >= dest->nbytes) { hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); - } else if (i == 0 && byte != 0x55) { + } else if (i == 0 && byte != ELO_FRAME_LEAD) { /* Invalid lead byte */ hr = E_FAIL; } else { @@ -135,7 +138,7 @@ HRESULT elo_frame_encode( src = ptr; /* Requires exactly 10 bytes */ - if (dest->pos + 10 > dest->nbytes) { + if (dest->pos + ELO_FRAME_LENGTH > dest->nbytes) { return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); } @@ -145,7 +148,7 @@ HRESULT elo_frame_encode( checksum = ELO_CHECKSUM_OFFSET; /* Write exactly 9 bytes (lead-in + 8 data bytes) */ - for (i = 0; i < 9; i++) { + for (i = 0; i < ELO_FRAME_LENGTH-1; i++) { uint8_t b = (i < nbytes) ? src[i] : 0x00; dest->bytes[dest->pos++] = b; checksum += b; diff --git a/games/divaio/divaio.c b/games/divaio/divaio.c index e854ee4..f74b599 100644 --- a/games/divaio/divaio.c +++ b/games/divaio/divaio.c @@ -40,12 +40,16 @@ uint16_t diva_io_get_api_version(void) HRESULT diva_io_jvs_init(void) { - if (!diva_io_config_initted) { + HRESULT hr; + + if(!diva_io_config_initted) { diva_io_config_load(&diva_io_cfg, get_config_path()); diva_io_config_initted = true; } - return S_OK; + hr = diva_io_touch_config_apply(&diva_io_cfg); + + return hr; } void diva_io_jvs_poll(uint8_t *opbtn_out, uint8_t *gamebtn_out) @@ -189,16 +193,7 @@ void diva_io_led_set_leds(uint8_t board, const uint8_t *rgb) HRESULT diva_io_touch_init() { - HRESULT hr; - - if(!diva_io_config_initted) { - diva_io_config_load(&diva_io_cfg, get_config_path()); - diva_io_config_initted = true; - } - - hr = diva_io_touch_config_apply(&diva_io_cfg); - - return hr; + return S_OK; } static HRESULT diva_io_touch_config_apply( @@ -206,7 +201,6 @@ static HRESULT diva_io_touch_config_apply( ) { dprintf("Diva IO: Touch: --- Begin Configuration ---\n"); - dprintf("Diva IO: Touch: Using Elo controller\n"); if (wstr_ieq(cfg->touch_mode, L"mouse")) { dprintf("Diva IO: Touch: Mouse emulation\n"); From b9e714dd1d5e0824c526d2495262c0b259e0c35d Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Wed, 11 Feb 2026 23:39:09 +0100 Subject: [PATCH 28/37] touch: always show cursor if enabled --- common/hooklib/touch.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/common/hooklib/touch.c b/common/hooklib/touch.c index a76692a..c5564df 100644 --- a/common/hooklib/touch.c +++ b/common/hooklib/touch.c @@ -43,6 +43,8 @@ static BOOL WINAPI hook_GetTouchInputInfo( static HCURSOR WINAPI hook_SetCursor(HCURSOR cursor); +static int WINAPI hook_ShowCursor(BOOL bShow); + /* Link pointers */ static ATOM (WINAPI *next_RegisterClassExA)( @@ -67,6 +69,9 @@ static BOOL (WINAPI *next_GetTouchInputInfo)( static HCURSOR(WINAPI *next_SetCursor)(HCURSOR cursor); +static int (WINAPI *next_ShowCursor)(BOOL bShow); + + static bool touch_hook_initted; static bool touch_held; static HWND registered_hWnd; @@ -100,6 +105,11 @@ static const struct hook_symbol touch_hooks[] = { .patch = hook_SetCursor, .link = (void **) &next_SetCursor }, + { + .name = "ShowCursor", + .patch = hook_ShowCursor, + .link = (void **) &next_ShowCursor + }, }; void touch_screen_hook_init(const struct touch_screen_config *cfg, HINSTANCE self) @@ -132,8 +142,15 @@ void touch_hook_insert_hooks(HMODULE target) _countof(touch_hooks)); } +static int WINAPI hook_ShowCursor(BOOL bShow) { + if (touch_config.cursor) + return next_ShowCursor(1); + + return next_ShowCursor(bShow); +} + static HCURSOR WINAPI hook_SetCursor(HCURSOR cursor) { - if (cursor == 0 && touch_config.cursor) + if (touch_config.cursor) return next_SetCursor(defaultCursor); return next_SetCursor(cursor); From 9c67f53902c39a8d53873c219a020b68abb3d539 Mon Sep 17 00:00:00 2001 From: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> Date: Thu, 26 Feb 2026 19:31:45 +0000 Subject: [PATCH 29/37] Add support for Sangokushi Taisen and Eiketsu Taisen (#85) This adds full support for the Taisen series of games, namely Sangokushi Taisen and Eiketsu Taisen. Games added: * Sangokushi Taisen (SDDD) * Eiketsu Taisen (SDGY) Devices added: * CHC-320 printer (SGT) * "Printer camera" (SGT, unsure what this actually really is) * CX-7000 printer (EKT) * Y3CR BD SIE F720MM (SGT, EKT) Notable changes in the codebase: * Renamed everything printer specific to seperate between CHC and CX. * Many new function and registry hooks were added across the board. * An error is now logged when segatools.ini (or the path in `SEGATOOLS_CONFIG_PATH`) cannot be found. * Netenv now redirects UDP broadcasts targeted at the subnet that is specified in the keychip configuration. The terminal announces it's presence by broadcasting UDP to 192.168.189.255, this will be redirected to 255.255.255.255. * Vfs now seperates between absolute and relative paths in `vfs_fixup_path` via an environment variable called `SEGATOOLS_VFS_RELATIVE_PATH`. This is needed because amcapture accesses files as workingdirectory-relative. * The Y3 board emulation has support for external Y3 I/O dlls. The default implementation (y3ws) that comes with this is a websocket implementation. The docs are available under `doc\y3ws.txt` and a sample card player .html file is under `dist\ekt\card_player.html`. I already know one person that is hosting a massively improved version of it. * For websockets, my own websocket implementation is used as a subproject (MIT license): https://github.com/akechi-haruka/cwinwebsocket * For JSON, cJSON was embedded (MIT license): https://github.com/DaveGamble/cJSON * y3ws reads all printed cards from `DEVICE\print` by default including card back sides. It's up to the client to merge or skip them. Remarks: * SGT takes ~8 minutes to load. This seems to be intentional. * SGT uses some weird TCP network implementation like IDZ. I have not bothered reversing that yet and I have confirmed everything working from the test menu. * EKT will throw a network error if no terminal is found. You must run the terminal on another computer to be able to launch the satellite. * EKT has a very bizzare speed glitch that will speed up the ingame unit movement by several 1000% and also slows down cutscene animations by 90%. When this effect is active, you also take a ton more damage than usual. I do not know what causes this and it seems PC specific. * EKT is very stutter sensitive and will throw error 6401 (I/O timeout) at random when trying to alt+tab or have other things running. * EKT features a livestream system called Enbu (or "Dojo Upload"). While you are in a match, regardless of vs. AI or another player, the game will record your screen and live-stream it to the Enbu server as defined by the game server's startup response. The application responsible for that, "AM Capture" will not limit it's recording to the game window, but also anything that overlays the game window (notifications, popups, alt+tabbed windows, web browsers, etc). Since this is live-streamed, killing the process will have no effect afterwards, as the frames showing unwanted things will already have been transmitted. To make people aware of this, a one-time dialog message will pop up when starting EKT. The flag for that is stored in the DEVICE folder. Closes #25. Co-authored-by: Dniel97 Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/85 Co-authored-by: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> Co-committed-by: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> --- .gitignore | 1 + Package.mk | 53 + README.md | 14 +- common/aimeio/aimeio.c | 8 +- common/aimeio/aimeio.h | 8 +- common/board/config.c | 18 +- common/board/config.h | 1 + common/board/meson.build | 2 + common/board/sg-reader-queue.c | 208 ++ common/board/sg-reader-queue.h | 14 + common/hooklib/config.c | 162 +- common/hooklib/config.h | 25 +- common/hooklib/createprocess.c | 118 +- common/hooklib/createprocess.h | 7 +- common/hooklib/imageutil.c | 212 ++ common/hooklib/imageutil.h | 21 + common/hooklib/meson.build | 12 +- common/hooklib/path.c | 350 +- common/hooklib/path.h | 3 + common/hooklib/{printer.c => printer_chc.c} | 597 +-- common/hooklib/{printer.h => printer_chc.h} | 6 +- common/hooklib/printer_cx.c | 906 +++++ common/hooklib/printer_cx.h | 73 + common/hooklib/y3-dll.c | 109 + common/hooklib/y3-dll.h | 19 + common/hooklib/y3.c | 625 ++++ common/hooklib/y3.h | 71 + common/platform/amvideo.c | 10 + common/platform/config.c | 25 + common/platform/netenv.c | 86 +- common/platform/netenv.h | 1 + common/platform/nusec.h | 1 + common/platform/vfs.c | 31 +- common/unityhook/hook.c | 2 +- common/y3io/impl/dummy/y3io.c | 68 + .../impl/websockets/3rdparty/cjson/LICENSE | 20 + .../impl/websockets/3rdparty/cjson/cJSON.c | 3191 +++++++++++++++++ .../impl/websockets/3rdparty/cjson/cJSON.h | 306 ++ .../websockets/3rdparty/cjson/cJSON_Utils.c | 1481 ++++++++ .../websockets/3rdparty/cjson/cJSON_Utils.h | 88 + common/y3io/impl/websockets/config.c | 39 + common/y3io/impl/websockets/config.h | 19 + common/y3io/impl/websockets/y3ws.c | 354 ++ common/y3io/impl/websockets/y3ws.h | 4 + common/y3io/impl/y3io.h | 31 + common/y3io/meson.build | 25 + dist/ekt/card_player.html | 224 ++ dist/ekt/config_hook.json | 13 + dist/ekt/launch_satellite.bat | 24 + dist/ekt/launch_terminal.bat | 13 + dist/ekt/segatools_satellite.ini | 181 + dist/ekt/segatools_terminal.ini | 145 + dist/sekito/card_player.html | 228 ++ dist/sekito/config_hook_satellite.json | 6 + dist/sekito/config_hook_terminal.json | 35 + dist/sekito/launch_satellite.bat | 15 + dist/sekito/launch_terminal.bat | 19 + dist/sekito/segatools_satellite.ini | 207 ++ dist/sekito/segatools_terminal.ini | 174 + doc/config/common.md | 10 + doc/config/taisen.md | 107 + doc/y3ws.txt | 57 + games/apm3hook/config.h | 2 +- games/carolhook/dllmain.c | 2 +- games/cmhook/config.c | 2 +- games/cmhook/config.h | 4 +- games/cmhook/dllmain.c | 2 +- games/ekthook/config.c | 107 + games/ekthook/config.h | 37 + games/ekthook/dllmain.c | 224 ++ games/ekthook/ekt-dll.c | 118 + games/ekthook/ekt-dll.h | 24 + games/ekthook/ekthook.def | 73 + games/ekthook/io4.c | 223 ++ games/ekthook/io4.h | 24 + games/ekthook/meson.build | 31 + games/ektio/backend.h | 10 + games/ektio/config.c | 77 + games/ektio/config.h | 62 + games/ektio/ektio.c | 108 + games/ektio/ektio.h | 106 + games/ektio/keyboard.c | 176 + games/ektio/keyboard.h | 8 + games/ektio/meson.build | 18 + games/fgohook/config.c | 2 +- games/fgohook/config.h | 4 +- games/fgohook/dllmain.c | 6 +- games/fgohook/fgohook.def | 2 +- games/kemonohook/config.c | 2 +- games/kemonohook/config.h | 2 +- games/kemonohook/dllmain.c | 4 +- games/sekitohook/config.c | 113 + games/sekitohook/config.h | 41 + games/sekitohook/dllmain.c | 207 ++ games/sekitohook/jvs.c | 258 ++ games/sekitohook/jvs.h | 18 + games/sekitohook/meson.build | 32 + games/sekitohook/sekito-dll.c | 118 + games/sekitohook/sekito-dll.h | 24 + games/sekitohook/sekitohook.def | 78 + games/sekitoio/backend.h | 10 + games/sekitoio/config.c | 74 + games/sekitoio/config.h | 59 + games/sekitoio/keyboard.c | 168 + games/sekitoio/keyboard.h | 8 + games/sekitoio/meson.build | 18 + games/sekitoio/sekitoio.c | 108 + games/sekitoio/sekitoio.h | 104 + meson.build | 11 + subprojects/cwinwebsocket.wrap | 5 + 110 files changed, 13209 insertions(+), 588 deletions(-) create mode 100644 common/board/sg-reader-queue.c create mode 100644 common/board/sg-reader-queue.h create mode 100644 common/hooklib/imageutil.c create mode 100644 common/hooklib/imageutil.h rename common/hooklib/{printer.c => printer_chc.c} (88%) rename common/hooklib/{printer.h => printer_chc.h} (74%) create mode 100644 common/hooklib/printer_cx.c create mode 100644 common/hooklib/printer_cx.h create mode 100644 common/hooklib/y3-dll.c create mode 100644 common/hooklib/y3-dll.h create mode 100644 common/hooklib/y3.c create mode 100644 common/hooklib/y3.h create mode 100644 common/y3io/impl/dummy/y3io.c create mode 100644 common/y3io/impl/websockets/3rdparty/cjson/LICENSE create mode 100644 common/y3io/impl/websockets/3rdparty/cjson/cJSON.c create mode 100644 common/y3io/impl/websockets/3rdparty/cjson/cJSON.h create mode 100644 common/y3io/impl/websockets/3rdparty/cjson/cJSON_Utils.c create mode 100644 common/y3io/impl/websockets/3rdparty/cjson/cJSON_Utils.h create mode 100644 common/y3io/impl/websockets/config.c create mode 100644 common/y3io/impl/websockets/config.h create mode 100644 common/y3io/impl/websockets/y3ws.c create mode 100644 common/y3io/impl/websockets/y3ws.h create mode 100644 common/y3io/impl/y3io.h create mode 100644 common/y3io/meson.build create mode 100644 dist/ekt/card_player.html create mode 100644 dist/ekt/config_hook.json create mode 100644 dist/ekt/launch_satellite.bat create mode 100644 dist/ekt/launch_terminal.bat create mode 100644 dist/ekt/segatools_satellite.ini create mode 100644 dist/ekt/segatools_terminal.ini create mode 100644 dist/sekito/card_player.html create mode 100644 dist/sekito/config_hook_satellite.json create mode 100644 dist/sekito/config_hook_terminal.json create mode 100644 dist/sekito/launch_satellite.bat create mode 100644 dist/sekito/launch_terminal.bat create mode 100644 dist/sekito/segatools_satellite.ini create mode 100644 dist/sekito/segatools_terminal.ini create mode 100644 doc/config/taisen.md create mode 100644 doc/y3ws.txt create mode 100644 games/ekthook/config.c create mode 100644 games/ekthook/config.h create mode 100644 games/ekthook/dllmain.c create mode 100644 games/ekthook/ekt-dll.c create mode 100644 games/ekthook/ekt-dll.h create mode 100644 games/ekthook/ekthook.def create mode 100644 games/ekthook/io4.c create mode 100644 games/ekthook/io4.h create mode 100644 games/ekthook/meson.build create mode 100644 games/ektio/backend.h create mode 100644 games/ektio/config.c create mode 100644 games/ektio/config.h create mode 100644 games/ektio/ektio.c create mode 100644 games/ektio/ektio.h create mode 100644 games/ektio/keyboard.c create mode 100644 games/ektio/keyboard.h create mode 100644 games/ektio/meson.build create mode 100644 games/sekitohook/config.c create mode 100644 games/sekitohook/config.h create mode 100644 games/sekitohook/dllmain.c create mode 100644 games/sekitohook/jvs.c create mode 100644 games/sekitohook/jvs.h create mode 100644 games/sekitohook/meson.build create mode 100644 games/sekitohook/sekito-dll.c create mode 100644 games/sekitohook/sekito-dll.h create mode 100644 games/sekitohook/sekitohook.def create mode 100644 games/sekitoio/backend.h create mode 100644 games/sekitoio/config.c create mode 100644 games/sekitoio/config.h create mode 100644 games/sekitoio/keyboard.c create mode 100644 games/sekitoio/keyboard.h create mode 100644 games/sekitoio/meson.build create mode 100644 games/sekitoio/sekitoio.c create mode 100644 games/sekitoio/sekitoio.h create mode 100644 subprojects/cwinwebsocket.wrap diff --git a/.gitignore b/.gitignore index f5d9008..8e442f9 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ build/ # External dependencies subprojects/capnhook +subprojects/cwinwebsocket # For enabling debug logging on local builds MesonLocalOptions.mk diff --git a/Package.mk b/Package.mk index 3c974e7..f79a357 100644 --- a/Package.mk +++ b/Package.mk @@ -256,6 +256,57 @@ $(BUILD_DIR_ZIP)/apm3.zip: $(V)strip $(BUILD_DIR_ZIP)/apm3/*.{exe,dll} $(V)cd $(BUILD_DIR_ZIP)/apm3 ; zip -r ../apm3.zip * +$(BUILD_DIR_ZIP)/ekt.zip: + $(V)echo ... $@ + $(V)mkdir -p $(BUILD_DIR_ZIP)/ekt + $(V)mkdir -p $(BUILD_DIR_ZIP)/ekt/DEVICE + $(V)cp $(DIST_DIR)/ekt/segatools_terminal.ini \ + $(DIST_DIR)/ekt/segatools_satellite.ini \ + $(DIST_DIR)/ekt/launch_terminal.bat \ + $(DIST_DIR)/ekt/launch_satellite.bat \ + $(DIST_DIR)/ekt/card_player.html \ + $(DIST_DIR)/ekt/config_hook.json \ + $(BUILD_DIR_ZIP)/ekt + $(V)cp pki/billing.pub \ + pki/ca.crt \ + $(BUILD_DIR_ZIP)/ekt/DEVICE + $(V)cp $(BUILD_DIR_32)/subprojects/capnhook/inject/inject.exe \ + $(BUILD_DIR_ZIP)/ekt/inject_x86.exe + $(V)cp $(BUILD_DIR_64)/subprojects/capnhook/inject/inject.exe \ + $(BUILD_DIR_ZIP)/ekt/inject_x64.exe + $(V)cp $(BUILD_DIR_GAMES_32)/ekthook/ekthook.dll \ + $(BUILD_DIR_ZIP)/ekt/ekthook_x86.dll + $(V)cp $(BUILD_DIR_GAMES_64)/ekthook/ekthook.dll \ + $(BUILD_DIR_ZIP)/ekt/ekthook_x64.dll + $(V)strip $(BUILD_DIR_ZIP)/ekt/*.{exe,dll} + $(V)cd $(BUILD_DIR_ZIP)/ekt ; zip -r ../ekt.zip * + +$(BUILD_DIR_ZIP)/sekito.zip: + $(V)echo ... $@ + $(V)mkdir -p $(BUILD_DIR_ZIP)/sekito + $(V)mkdir -p $(BUILD_DIR_ZIP)/sekito/DEVICE + $(V)cp $(DIST_DIR)/sekito/segatools_terminal.ini \ + $(DIST_DIR)/sekito/segatools_satellite.ini \ + $(DIST_DIR)/sekito/launch_terminal.bat \ + $(DIST_DIR)/sekito/launch_satellite.bat \ + $(DIST_DIR)/sekito/card_player.html \ + $(DIST_DIR)/sekito/config_hook_satellite.json \ + $(DIST_DIR)/sekito/config_hook_terminal.json \ + $(BUILD_DIR_ZIP)/sekito + $(V)cp pki/billing.pub \ + pki/ca.crt \ + $(BUILD_DIR_ZIP)/sekito/DEVICE + $(V)cp $(BUILD_DIR_32)/subprojects/capnhook/inject/inject.exe \ + $(BUILD_DIR_ZIP)/sekito/inject_x86.exe + $(V)cp $(BUILD_DIR_64)/subprojects/capnhook/inject/inject.exe \ + $(BUILD_DIR_ZIP)/sekito/inject_x64.exe + $(V)cp $(BUILD_DIR_GAMES_32)/sekitohook/sekitohook.dll \ + $(BUILD_DIR_ZIP)/sekito/sekitohook_x86.dll + $(V)cp $(BUILD_DIR_GAMES_64)/sekitohook/sekitohook.dll \ + $(BUILD_DIR_ZIP)/sekito/sekitohook_x64.dll + $(V)strip $(BUILD_DIR_ZIP)/sekito/*.{exe,dll} + $(V)cd $(BUILD_DIR_ZIP)/sekito ; zip -r ../sekito.zip * + $(BUILD_DIR_ZIP)/doc.zip: \ $(DOC_DIR)/config \ $(DOC_DIR)/chunihook.md \ @@ -282,6 +333,8 @@ $(BUILD_DIR_ZIP)/segatools.zip: \ $(BUILD_DIR_ZIP)/fgo.zip \ $(BUILD_DIR_ZIP)/kemono.zip \ $(BUILD_DIR_ZIP)/apm3.zip \ + $(BUILD_DIR_ZIP)/ekt.zip \ + $(BUILD_DIR_ZIP)/sekito.zip \ CHANGELOG.md \ README.md \ diff --git a/README.md b/README.md index 9a4b29a..484c2c0 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,13 @@ # Segatools -Version: `2025-11-04` +Version: `2026-02-26` Loaders and hardware emulators for SEGA games that run on the Nu and ALLS platforms. ## List of supported games +* ALL.Net P-ras MULTI Version 3 + * starting from ALL.Net P-ras MULTI Version 3 1.01 * Card Maker * starting from Card Maker * CHUNITHM @@ -13,6 +15,8 @@ Loaders and hardware emulators for SEGA games that run on the Nu and ALLS platfo * starting from CHUNITHM NEW!! * crossbeats REV. * up to crossbeats REV. SUNRISE +* Eiketsu Taisen (英傑大戦) + * starting from Sanzensekai no Hadou (三千世界の波動) * Fate/Grand Order * Fate/Grand Order Arcade * Hatsune Miku: Project DIVA Arcade @@ -20,20 +24,20 @@ Loaders and hardware emulators for SEGA games that run on the Nu and ALLS platfo * Initial D * [Initial D Arcade Stage Zero](doc/idzhook.md) * Initial D THE ARCADE +* Kemono Friends + * Kemono Friends 3: Planet Tours * maimai DX * starting from maimai DX * Mario & Sonic * Mario & Sonic at the Tokyo 2020 Olympics Arcade * O.N.G.E.K.I. * starting from O.N.G.E.K.I. +* Sangokushi Taisen (三国志大戦) + * starting from 1.01 * SEGA World Drivers Championship * SEGA World Drivers Championship 2019 * WACCA * starting from WACCA -* Kemono Friends - * Kemono Friends 3: Planet Tours -* ALL.Net P-ras MULTI Version 3 - * starting from ALL.Net P-ras MULTI Version 3 1.01 ## End-users diff --git a/common/aimeio/aimeio.c b/common/aimeio/aimeio.c index fbb694e..1b26b0d 100644 --- a/common/aimeio/aimeio.c +++ b/common/aimeio/aimeio.c @@ -235,10 +235,6 @@ HRESULT aime_io_nfc_poll(uint8_t unit_no) bool sense; HRESULT hr; - if (unit_no != 0) { - return S_OK; - } - /* Reset presence flags */ aime_io_aime_id_present = false; @@ -324,7 +320,7 @@ HRESULT aime_io_nfc_get_aime_id( assert(luid != NULL); assert(luid_size == sizeof(aime_io_aime_id)); - if (unit_no != 0 || !aime_io_aime_id_present) { + if (!aime_io_aime_id_present) { return S_FALSE; } @@ -340,7 +336,7 @@ HRESULT aime_io_nfc_get_felica_id(uint8_t unit_no, uint64_t *IDm) assert(IDm != NULL); - if (unit_no != 0 || !aime_io_felica_id_present) { + if (!aime_io_felica_id_present) { return S_FALSE; } diff --git a/common/aimeio/aimeio.h b/common/aimeio/aimeio.h index 96dd241..3c15dcf 100644 --- a/common/aimeio/aimeio.h +++ b/common/aimeio/aimeio.h @@ -27,7 +27,7 @@ HRESULT aime_io_init(void); /* Poll for IC cards in the vicinity. - - unit_no: Always 0 as of the current API version + - unit_no: 0 on the primary Aime reader (most games), may be 1 on Sangokushi Taisen for the queue reader Minimum API version: 0x0100 */ @@ -36,7 +36,7 @@ HRESULT aime_io_nfc_poll(uint8_t unit_no); /* Attempt to read out a classic Aime card ID - - unit_no: Always 0 as of the current API version + - unit_no: 0 on the primary Aime reader (most games), may be 1 on Sangokushi Taisen for the queue reader - luid: Pointer to a ten-byte buffer that will receive the ID - luid_size: Size of the buffer at *luid. Always 10. @@ -63,7 +63,7 @@ HRESULT aime_io_nfc_get_aime_id( Parameters: - - unit_no: Always 0 as of the current API version + - unit_no: 0 on the primary Aime reader (most games), may be 1 on Sangokushi Taisen for the queue reader - IDm: Output parameter that will receive the card ID Returns: @@ -213,7 +213,7 @@ HRESULT aime_io_nfc_send_hex_data( /* Change the color and brightness of the card reader's RGB lighting - - unit_no: Always 0 as of the current API version + - unit_no: 0 on the primary Aime reader (most games), may be 1 on Sangokushi Taisen for the queue reader - r, g, b: Primary color intensity, from 0 to 255 inclusive. Minimum API version: 0x0100 diff --git a/common/board/config.c b/common/board/config.c index a36d7c2..cb2f948 100644 --- a/common/board/config.c +++ b/common/board/config.c @@ -65,20 +65,24 @@ static void aime_dll_config_load(struct aime_dll_config *cfg, const wchar_t *fil } } -void aime_config_load(struct aime_config *cfg, const wchar_t *filename) +void aime_config_load(struct aime_config *cfg, const wchar_t *filename) { + aime_config_load_bykey(cfg, filename, L"aime"); +} + +void aime_config_load_bykey(struct aime_config *cfg, const wchar_t *filename, const wchar_t* config_key) { assert(cfg != NULL); assert(filename != NULL); aime_dll_config_load(&cfg->dll, filename); - cfg->enable = GetPrivateProfileIntW(L"aime", L"enable", 1, filename); - cfg->port_no = GetPrivateProfileIntW(L"aime", L"portNo", 0, filename); - cfg->high_baudrate = GetPrivateProfileIntW(L"aime", L"highBaud", 1, filename); - cfg->gen = GetPrivateProfileIntW(L"aime", L"gen", 0, filename); - cfg->proxy_flag = GetPrivateProfileIntW(L"aime", L"proxyFlag", 2, filename); + cfg->enable = GetPrivateProfileIntW(config_key, L"enable", 1, filename); + cfg->port_no = GetPrivateProfileIntW(config_key, L"portNo", 0, filename); + cfg->high_baudrate = GetPrivateProfileIntW(config_key, L"highBaud", 1, filename); + cfg->gen = GetPrivateProfileIntW(config_key, L"gen", 0, filename); + cfg->proxy_flag = GetPrivateProfileIntW(config_key, L"proxyFlag", 2, filename); GetPrivateProfileStringW( - L"aime", + config_key, L"authdataPath", L"DEVICE\\authdata.bin", cfg->authdata_path, diff --git a/common/board/config.h b/common/board/config.h index 34977dc..58fd8e2 100644 --- a/common/board/config.h +++ b/common/board/config.h @@ -9,6 +9,7 @@ #include "board/ffb.h" void aime_config_load(struct aime_config *cfg, const wchar_t *filename); +void aime_config_load_bykey(struct aime_config *cfg, const wchar_t *filename, const wchar_t *config_key); void io4_config_load(struct io4_config *cfg, const wchar_t *filename); void vfd_config_load(struct vfd_config *cfg, const wchar_t *filename); void ffb_config_load(struct ffb_config *cfg, const wchar_t *filename); diff --git a/common/board/meson.build b/common/board/meson.build index ca3c2ca..4931a82 100644 --- a/common/board/meson.build +++ b/common/board/meson.build @@ -44,6 +44,8 @@ board_lib = static_library( 'sg-nfc-cmd.h', 'sg-reader.c', 'sg-reader.h', + 'sg-reader-queue.c', + 'sg-reader-queue.h', 'slider-cmd.h', 'slider-frame.c', 'slider-frame.h', diff --git a/common/board/sg-reader-queue.c b/common/board/sg-reader-queue.c new file mode 100644 index 0000000..29637e7 --- /dev/null +++ b/common/board/sg-reader-queue.c @@ -0,0 +1,208 @@ +#include + +#include +#include +#include + +#include "board/aime-dll.h" +#include "board/sg-led.h" +#include "board/sg-nfc.h" +#include "board/sg-reader.h" +#include "board/sg-reader-queue.h" + +#include "hook/iohook.h" + +#include "hooklib/uart.h" + +#include "util/dprintf.h" +#include "util/dump.h" + +static HRESULT sg_reader_handle_irp(struct irp *irp); +static HRESULT sg_reader_handle_irp_locked(struct irp *irp); +static HRESULT sg_reader_nfc_poll(void *ctx); +static HRESULT sg_reader_nfc_get_aime_id( + void *ctx, + uint8_t *luid, + size_t luid_size); +static HRESULT sg_reader_nfc_get_felica_id(void *ctx, uint64_t *IDm); +static void sg_reader_led_set_color(void *ctx, uint8_t r, uint8_t g, uint8_t b); + +static const struct sg_nfc_ops sg_reader_nfc_ops = { + .poll = sg_reader_nfc_poll, + .get_aime_id = sg_reader_nfc_get_aime_id, + .get_felica_id = sg_reader_nfc_get_felica_id, +}; + +static const struct sg_led_ops sg_reader_led_ops = { + .set_color = sg_reader_led_set_color, +}; + +static CRITICAL_SECTION sg_reader_lock; +static bool sg_reader_started; +static HRESULT sg_reader_start_hr; +static struct uart sg_reader_uart; +static uint8_t sg_reader_written_bytes[520]; +static uint8_t sg_reader_readable_bytes[520]; +static struct sg_nfc sg_reader_nfc; +static struct sg_led sg_reader_led; + +HRESULT sg_reader_queue_hook_init( + const struct aime_config *cfg, + unsigned int default_port_no, + unsigned int gen, + HINSTANCE self) +{ + HRESULT hr; + + assert(cfg != NULL); + assert(self != NULL); + + if (!cfg->enable) { + return S_FALSE; + } + + hr = aime_dll_init(&cfg->dll, self); + + if (FAILED(hr)) { + return hr; + } + + unsigned int port_no = cfg->port_no; + if (port_no == 0){ + port_no = default_port_no; + } + + if (cfg->gen != 0) { + gen = cfg->gen; + } + + if (gen < 1 || gen > 3) { + dprintf("NFC Assembly (Queue): Invalid reader generation: %u\n", gen); + + return E_INVALIDARG; + } + + sg_nfc_init(&sg_reader_nfc, 0x01, &sg_reader_nfc_ops, gen, cfg->proxy_flag, cfg->authdata_path, NULL); + sg_led_init(&sg_reader_led, 0x08, &sg_reader_led_ops, gen, NULL); + + InitializeCriticalSection(&sg_reader_lock); + + if (!cfg->high_baudrate) { + sg_reader_uart.baud.BaudRate = 38400; + } + + dprintf("NFC Assembly (Queue): enabling (port=%d)\n", port_no); + uart_init(&sg_reader_uart, port_no); + sg_reader_uart.written.bytes = sg_reader_written_bytes; + sg_reader_uart.written.nbytes = sizeof(sg_reader_written_bytes); + sg_reader_uart.readable.bytes = sg_reader_readable_bytes; + sg_reader_uart.readable.nbytes = sizeof(sg_reader_readable_bytes); + + return iohook_push_handler(sg_reader_handle_irp); +} + +static HRESULT sg_reader_handle_irp(struct irp *irp) +{ + HRESULT hr; + + assert(irp != NULL); + + if (!uart_match_irp(&sg_reader_uart, irp)) { + return iohook_invoke_next(irp); + } + + EnterCriticalSection(&sg_reader_lock); + hr = sg_reader_handle_irp_locked(irp); + LeaveCriticalSection(&sg_reader_lock); + + return hr; +} + +static HRESULT sg_reader_handle_irp_locked(struct irp *irp) +{ + HRESULT hr; + +#if defined(LOG_NFC) + if (irp->op == IRP_OP_WRITE) { + dprintf("WRITE:\n"); + dump_const_iobuf(&irp->write); + } +#endif + +#if defined(LOG_NFC) + if (irp->op == IRP_OP_READ) { + dprintf("READ:\n"); + dump_iobuf(&sg_reader_uart.readable); + } +#endif + + if (irp->op == IRP_OP_OPEN) { + /* Unfortunately the card reader UART gets opened and closed + repeatedly */ + + if (!sg_reader_started) { + dprintf("NFC Assembly (Queue): Starting backend DLL\n"); + hr = aime_dll.init(); + + sg_reader_started = true; + sg_reader_start_hr = hr; + + if (FAILED(hr)) { + dprintf("NFC Assembly (Queue): Backend error: %x\n", (int) hr); + + return hr; + } + } else { + hr = sg_reader_start_hr; + + if (FAILED(hr)) { + return hr; + } + } + } + + hr = uart_handle_irp(&sg_reader_uart, irp); + + if (FAILED(hr) || irp->op != IRP_OP_WRITE) { + return hr; + } + + sg_nfc_transact( + &sg_reader_nfc, + &sg_reader_uart.readable, + sg_reader_uart.written.bytes, + sg_reader_uart.written.pos); + + sg_led_transact( + &sg_reader_led, + &sg_reader_uart.readable, + sg_reader_uart.written.bytes, + sg_reader_uart.written.pos); + + sg_reader_uart.written.pos = 0; + + return hr; +} + +static HRESULT sg_reader_nfc_poll(void *ctx) +{ + return aime_dll.nfc_poll(1); +} + +static HRESULT sg_reader_nfc_get_aime_id( + void *ctx, + uint8_t *luid, + size_t luid_size) +{ + return aime_dll.nfc_get_aime_id(1, luid, luid_size); +} + +static HRESULT sg_reader_nfc_get_felica_id(void *ctx, uint64_t *IDm) +{ + return aime_dll.nfc_get_felica_id(1, IDm); +} + +static void sg_reader_led_set_color(void *ctx, uint8_t r, uint8_t g, uint8_t b) +{ + aime_dll.led_set_color(1, r, g, b); +} diff --git a/common/board/sg-reader-queue.h b/common/board/sg-reader-queue.h new file mode 100644 index 0000000..d81b41a --- /dev/null +++ b/common/board/sg-reader-queue.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +#include + +#include "board/aime-dll.h" +#include "board/sg-reader.h" + +HRESULT sg_reader_queue_hook_init( + const struct aime_config *cfg, + unsigned int default_port_no, + unsigned int gen, + HINSTANCE self); diff --git a/common/hooklib/config.c b/common/hooklib/config.c index b7a84cf..705dcb1 100644 --- a/common/hooklib/config.c +++ b/common/hooklib/config.c @@ -7,6 +7,8 @@ #include "hooklib/config.h" #include "hooklib/dvd.h" +#include "hooklib/y3.h" +#include "hooklib/y3-dll.h" void dvd_config_load(struct dvd_config *cfg, const wchar_t *filename) { @@ -26,7 +28,7 @@ void touch_screen_config_load(struct touch_screen_config *cfg, const wchar_t *fi cfg->cursor = GetPrivateProfileIntW(L"touch", L"cursor", 1, filename); } -void printer_config_load(struct printer_config *cfg, const wchar_t *filename) +void printer_chc_config_load(struct printer_chc_config *cfg, const wchar_t *filename) { assert(cfg != NULL); assert(filename != NULL); @@ -84,3 +86,161 @@ void printer_config_load(struct printer_config *cfg, const wchar_t *filename) cfg->wait_time = GetPrivateProfileIntW(L"printer", L"waitTime", 0, filename); } + +void printer_cx_config_load(struct printer_cx_config *cfg, const wchar_t *filename){ + assert(cfg != NULL); + assert(filename != NULL); + + char filenameA[MAX_PATH]; + + size_t n = wcstombs(filenameA, filename, MAX_PATH); + for (int i = n; i < MAX_PATH; i++) + { + filenameA[i] = '\0'; + } + + cfg->enable = GetPrivateProfileIntW(L"printer", L"enable", 1, filename); + + GetPrivateProfileStringA( + "printer", + "firmwareVersion", + "V04-03B", + cfg->printer_firm_version, + _countof(cfg->printer_firm_version), + filenameA); + + GetPrivateProfileStringA( + "printer", + "configVersion", + "V01-75", + cfg->printer_config_version, + _countof(cfg->printer_config_version), + filenameA); + + GetPrivateProfileStringA( + "printer", + "tableVersion", + "V01-E0", + cfg->printer_table_version, + _countof(cfg->printer_table_version), + filenameA); + + GetPrivateProfileStringA( + "printer", + "cameraVersion", + "00.19", + cfg->printer_camera_version, + _countof(cfg->printer_camera_version), + filenameA); + + GetPrivateProfileStringW( + L"printer", + L"printerOutPath", + L"DEVICE\\print", + cfg->printer_out_path, + _countof(cfg->printer_out_path), + filename); + + GetPrivateProfileStringW( + L"printer", + L"printerDataPath", + L"DEVICE\\cx7000_data.bin", + cfg->printer_data_path, + _countof(cfg->printer_data_path), + filename); + +} + +void y3_dll_config_load( + struct y3_dll_config *cfg, + const wchar_t *filename) +{ + assert(cfg != NULL); + assert(filename != NULL); + + GetPrivateProfileStringW( + L"y3io", + L"path", + L"", + cfg->path, + _countof(cfg->path), + filename); +} + +void y3_config_load( + struct y3_config *cfg, + const wchar_t *filename) +{ + assert(cfg != NULL); + assert(filename != NULL); + + wchar_t tmpstr[5]; + + memset(cfg->firm_name_field, ' ', sizeof(cfg->firm_name_field) - 1); + cfg->firm_name_field[sizeof(cfg->firm_name_field) - 1] = '\0'; + + memset(cfg->firm_name_printer, ' ', sizeof(cfg->firm_name_printer) - 1); + cfg->firm_name_printer[sizeof(cfg->firm_name_printer) - 1] = '\0'; + + memset(cfg->target_code_field, ' ', sizeof(cfg->target_code_field) - 1); + cfg->target_code_field[sizeof(cfg->target_code_field) - 1] = '\0'; + + memset(cfg->target_code_printer, ' ', sizeof(cfg->target_code_printer) - 1); + cfg->target_code_printer[sizeof(cfg->target_code_printer) - 1] = '\0'; + + cfg->enable = GetPrivateProfileIntW(L"flatPanelReader", L"enable", 1, filename); + cfg->port_field = GetPrivateProfileIntW(L"flatPanelReader", L"port_field", 10, filename); + cfg->port_printer = GetPrivateProfileIntW(L"flatPanelReader", L"port_printer", 11, filename); + + cfg->dll_version = (float)GetPrivateProfileIntW( + L"flatPanelReader", + L"dllVersion", + 1, + filename); + + cfg->firm_version = (float)GetPrivateProfileIntW( + L"flatPanelReader", + L"firmVersion", + 1, + filename); + + GetPrivateProfileStringW( + L"flatPanelReader", + L"firmNameField", + L"SFPR", + tmpstr, + _countof(tmpstr), + filename); + + wcstombs(cfg->firm_name_field, tmpstr, sizeof(cfg->firm_name_field) - 1); + + GetPrivateProfileStringW( + L"flatPanelReader", + L"firmNamePrinter", + L"SPRT", + tmpstr, + _countof(tmpstr), + filename); + + wcstombs(cfg->firm_name_printer, tmpstr, sizeof(cfg->firm_name_printer) - 1); + + GetPrivateProfileStringW( + L"flatPanelReader", + L"targetCodeField", + L"SFR0", + tmpstr, + _countof(tmpstr), + filename); + + wcstombs(cfg->target_code_field, tmpstr, sizeof(cfg->target_code_field) - 1); + + GetPrivateProfileStringW( + L"flatPanelReader", + L"targetCodePrinter", + L"SPT0", + tmpstr, + _countof(tmpstr), + filename); + + wcstombs(cfg->target_code_printer, tmpstr, sizeof(cfg->target_code_printer) - 1); +} \ No newline at end of file diff --git a/common/hooklib/config.h b/common/hooklib/config.h index 9daed18..b776407 100644 --- a/common/hooklib/config.h +++ b/common/hooklib/config.h @@ -4,8 +4,29 @@ #include "hooklib/dvd.h" #include "hooklib/touch.h" -#include "hooklib/printer.h" +#include "hooklib/printer_chc.h" +#include "hooklib/printer_cx.h" + +struct y3_config { + bool enable; + + float dll_version; + float firm_version; + char firm_name_field[5]; + char firm_name_printer[5]; + char target_code_field[5]; + char target_code_printer[5]; + uint8_t port_field; + uint8_t port_printer; +}; + +struct y3_dll_config { + wchar_t path[MAX_PATH]; +}; void dvd_config_load(struct dvd_config *cfg, const wchar_t *filename); void touch_screen_config_load(struct touch_screen_config *cfg, const wchar_t *filename); -void printer_config_load(struct printer_config *cfg, const wchar_t *filename); +void printer_chc_config_load(struct printer_chc_config *cfg, const wchar_t *filename); +void printer_cx_config_load(struct printer_cx_config *cfg, const wchar_t *filename); +void y3_config_load(struct y3_config *cfg, const wchar_t *filename); +void y3_dll_config_load(struct y3_dll_config *cfg, const wchar_t *filename); \ No newline at end of file diff --git a/common/hooklib/createprocess.c b/common/hooklib/createprocess.c index 6462394..e141aca 100644 --- a/common/hooklib/createprocess.c +++ b/common/hooklib/createprocess.c @@ -5,11 +5,14 @@ #include #include #include +#include #include "hook/table.h" #include "hooklib/createprocess.h" +#include "path.h" +#include "hook/procaddr.h" #include "util/dprintf.h" void createprocess_hook_init(); @@ -36,7 +39,15 @@ BOOL my_CreateProcessW( LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation -); + ); + +BOOL my_ShellExecuteExA(SHELLEXECUTEINFOA *pExecInfo); + +BOOL my_ShellExecuteExW(SHELLEXECUTEINFOW *pExecInfo); + +static BOOL (WINAPI *next_ShellExecuteExA)(SHELLEXECUTEINFOA *pExecInfo); + +static BOOL (WINAPI *next_ShellExecuteExW)(SHELLEXECUTEINFOW *pExecInfo); static BOOL (WINAPI *next_CreateProcessA)( LPCSTR lpApplicationName, @@ -76,6 +87,18 @@ static const struct hook_symbol win32_hooks[] = { .link = (void **) &next_CreateProcessW }, }; +static const struct hook_symbol shell32_hooks[] = { + { + .name = "ShellExecuteExA", + .patch = my_ShellExecuteExA, + .link = (void **) &next_ShellExecuteExA + }, + { + .name = "ShellExecuteExW", + .patch = my_ShellExecuteExW, + .link = (void **) &next_ShellExecuteExW + }, +}; static bool did_init = false; @@ -87,7 +110,7 @@ static size_t process_nsyms_w = 0; static CRITICAL_SECTION createproc_lock; -HRESULT createprocess_push_hook_w(const wchar_t *name, const wchar_t *head, const wchar_t *tail, bool replace_all) { +HRESULT createprocess_push_hook_w(const wchar_t *name, const wchar_t *head, const wchar_t *tail, bool replace_all, bool replace_paths) { struct process_hook_sym_w *new_mem; struct process_hook_sym_w *new_proc; HRESULT hr; @@ -114,6 +137,7 @@ HRESULT createprocess_push_hook_w(const wchar_t *name, const wchar_t *head, cons new_proc->head = head; new_proc->tail = tail; new_proc->replace_all = replace_all; + new_proc->replace_paths = replace_paths; process_syms_w = new_mem; process_nsyms_w++; @@ -122,7 +146,7 @@ HRESULT createprocess_push_hook_w(const wchar_t *name, const wchar_t *head, cons return S_OK; } -HRESULT createprocess_push_hook_a(const char *name, const char *head, const char *tail, bool replace_all) { +HRESULT createprocess_push_hook_a(const char *name, const char *head, const char *tail, bool replace_all, bool replace_paths) { struct process_hook_sym_a *new_mem; struct process_hook_sym_a *new_proc; @@ -149,6 +173,7 @@ HRESULT createprocess_push_hook_a(const char *name, const char *head, const char new_proc->head = head; new_proc->tail = tail; new_proc->replace_all = replace_all; + new_proc->replace_paths = replace_paths; process_syms_a = new_mem; process_nsyms_a++; @@ -157,18 +182,31 @@ HRESULT createprocess_push_hook_a(const char *name, const char *head, const char return S_OK; } +void createprocess_hook_apply_hooks(HMODULE mod) { + hook_table_apply( + mod, + "kernel32.dll", + win32_hooks, + _countof(win32_hooks)); + hook_table_apply( + mod, + "shell32.dll", + shell32_hooks, + _countof(shell32_hooks)); + + proc_addr_table_push(mod, "kernel32.dll", win32_hooks, _countof(win32_hooks)); + proc_addr_table_push(mod, "shell32.dll", shell32_hooks, _countof(shell32_hooks)); + + InitializeCriticalSection(&createproc_lock); +} + void createprocess_hook_init() { if (did_init) { return; } did_init = true; - hook_table_apply( - NULL, - "kernel32.dll", - win32_hooks, - _countof(win32_hooks)); - InitializeCriticalSection(&createproc_lock); + createprocess_hook_apply_hooks(NULL); dprintf("CreateProcess: Init\n"); } @@ -255,4 +293,66 @@ BOOL my_CreateProcessW( lpStartupInfo, lpProcessInformation ); +} + +BOOL my_ShellExecuteExA(SHELLEXECUTEINFOA *pExecInfo) { + for (int i = 0; i < process_nsyms_a; i++) { + if (strncmp(process_syms_a[i].name, pExecInfo->lpFile, strlen(process_syms_a[i].name))) { + continue; + } + + dprintf("CreateProcess: Hooking child process %s %s\n", pExecInfo->lpFile, pExecInfo->lpParameters); + char new_args[MAX_PATH] = {0}; + strcat_s(new_args, MAX_PATH, process_syms_a[i].head); + + if (!process_syms_a[i].replace_all) { + strcat_s(new_args, MAX_PATH, pExecInfo->lpParameters); + } + if (process_syms_a[i].replace_paths) { + char result[MAX_PATH]; + if (path_transform_args_a(pExecInfo->lpParameters, ' ', result, MAX_PATH)) { + strcat_s(new_args, MAX_PATH, result); + } + } + + if (process_syms_a[i].tail != NULL) { + strcat_s(new_args, MAX_PATH, process_syms_a[i].tail); + } + + pExecInfo->lpParameters = new_args; + + dprintf("CreateProcess: Replaced ShellExecuteExA %s %s\n", pExecInfo->lpFile, new_args); + } + return next_ShellExecuteExA(pExecInfo); +} + +BOOL my_ShellExecuteExW(SHELLEXECUTEINFOW *pExecInfo) { + for (int i = 0; i < process_nsyms_w; i++) { + if (wcsncmp(process_syms_w[i].name, pExecInfo->lpFile, wcslen(process_syms_w[i].name))) { + continue; + } + + dprintf("CreateProcess: Hooking child process %ls %ls\n", pExecInfo->lpFile, pExecInfo->lpParameters); + wchar_t new_args[MAX_PATH] = {0}; + wcscat_s(new_args, MAX_PATH, process_syms_w[i].head); + + if (!process_syms_w[i].replace_all) { + wcscat_s(new_args, MAX_PATH, pExecInfo->lpParameters); + } + if (process_syms_w[i].replace_paths) { + wchar_t result[MAX_PATH]; + if (path_transform_args_w(pExecInfo->lpParameters, ' ', result, MAX_PATH)) { + wcscat_s(new_args, MAX_PATH, result); + } + } + + if (process_syms_w[i].tail != NULL) { + wcscat_s(new_args, MAX_PATH, process_syms_w[i].tail); + } + + pExecInfo->lpParameters = new_args; + + dprintf("CreateProcess: Replaced ShellExecuteExW %ls %ls\n", pExecInfo->lpFile, new_args); + } + return next_ShellExecuteExW(pExecInfo); } \ No newline at end of file diff --git a/common/hooklib/createprocess.h b/common/hooklib/createprocess.h index bf226d5..96e0e98 100644 --- a/common/hooklib/createprocess.h +++ b/common/hooklib/createprocess.h @@ -3,14 +3,16 @@ #include #include -HRESULT createprocess_push_hook_w(const wchar_t *name, const wchar_t *head, const wchar_t *tail, bool replace_all); -HRESULT createprocess_push_hook_a(const char *name, const char *head, const char *tail, bool replace_all); +HRESULT createprocess_push_hook_w(const wchar_t *name, const wchar_t *head, const wchar_t *tail, bool replace_all, bool replace_paths); +HRESULT createprocess_push_hook_a(const char *name, const char *head, const char *tail, bool replace_all, bool replace_paths); +void createprocess_hook_apply_hooks(HMODULE mod); struct process_hook_sym_w { const wchar_t *name; const wchar_t *head; const wchar_t *tail; bool replace_all; + bool replace_paths; }; struct process_hook_sym_a { @@ -18,4 +20,5 @@ struct process_hook_sym_a { const char *head; const char *tail; bool replace_all; + bool replace_paths; }; \ No newline at end of file diff --git a/common/hooklib/imageutil.c b/common/hooklib/imageutil.c new file mode 100644 index 0000000..0c38b69 --- /dev/null +++ b/common/hooklib/imageutil.c @@ -0,0 +1,212 @@ +#include +#include +#include + +#include "imageutil.h" + +// copy pasted from https://dev.s-ul.net/domeori/c310emu +#define BITMAPHEADERSIZE 0x36 + +int ConvertDataToBitmap( + DWORD dwBitCount, + DWORD dwWidth, DWORD dwHeight, + PBYTE pbInput, DWORD cbInput, + PBYTE pbOutput, DWORD cbOutput, + PDWORD pcbResult, + bool pFlip) { + if (!pbInput || !pbOutput || dwBitCount < 8) return -3; + + if (cbInput < (dwWidth * dwHeight * dwBitCount / 8)) return -3; + + PBYTE pBuffer = malloc(cbInput); + if (!pBuffer) return -2; + + BYTE dwColors = (BYTE)(dwBitCount / 8); + if (!dwColors) { + free(pBuffer); + return -1; + } + + UINT16 cbColors; + RGBQUAD pbColors[256]; + + switch (dwBitCount) { + case 1: + cbColors = 1; + break; + case 2: + cbColors = 4; + break; + case 4: + cbColors = 16; + break; + case 8: + cbColors = 256; + break; + default: + cbColors = 0; + break; + } + + if (cbColors) { + BYTE dwStep = (BYTE)(256 / cbColors); + + for (UINT16 i = 0; i < cbColors; ++i) { + pbColors[i].rgbRed = dwStep * i; + pbColors[i].rgbGreen = dwStep * i; + pbColors[i].rgbBlue = dwStep * i; + pbColors[i].rgbReserved = 0; + } + } + + DWORD dwTable = cbColors * sizeof(RGBQUAD); + DWORD dwOffset = BITMAPHEADERSIZE + dwTable; + + // calculate the padded row size, again + DWORD dwLineSize = (dwWidth * dwBitCount / 8 + 3) & ~3; + + BITMAPFILEHEADER bFile = {0}; + BITMAPINFOHEADER bInfo = {0}; + + bFile.bfType = 0x4D42; // MAGIC + bFile.bfSize = dwOffset + cbInput; + bFile.bfOffBits = dwOffset; + + bInfo.biSize = sizeof(BITMAPINFOHEADER); + bInfo.biWidth = dwWidth; + bInfo.biHeight = dwHeight; + bInfo.biPlanes = 1; + bInfo.biBitCount = (WORD)dwBitCount; + bInfo.biCompression = BI_RGB; + bInfo.biSizeImage = cbInput; + + if (cbOutput < bFile.bfSize) { + free(pBuffer); + return -1; + } + + // Flip the image (if necessary) and add padding to each row + if (pFlip) { + for (size_t i = 0; i < dwHeight; i++) { + for (size_t j = 0; j < dwWidth; j++) { + for (size_t k = 0; k < dwColors; k++) { + // Calculate the position in the padded buffer + // Make sure to also flip the colors from RGB to BRG + size_t x = (dwHeight - i - 1) * dwLineSize + (dwWidth - j - 1) * dwColors + (dwColors - k - 1); + size_t y = (dwHeight - i - 1) * dwWidth * dwColors + j * dwColors + k; + *(pBuffer + x) = *(pbInput + y); + } + } + } + } else { + for (size_t i = 0; i < dwHeight; i++) { + for (size_t j = 0; j < dwWidth; j++) { + for (size_t k = 0; k < dwColors; k++) { + // Calculate the position in the padded buffer + size_t x = i * dwLineSize + j * dwColors + (dwColors - k - 1); + size_t y = (dwHeight - i - 1) * dwWidth * dwColors + j * dwColors + k; + *(pBuffer + x) = *(pbInput + y); + } + } + } + } + + memcpy(pbOutput, &bFile, sizeof(BITMAPFILEHEADER)); + memcpy(pbOutput + sizeof(BITMAPFILEHEADER), &bInfo, sizeof(BITMAPINFOHEADER)); + if (cbColors) memcpy(pbOutput + BITMAPHEADERSIZE, pbColors, dwTable); + memcpy(pbOutput + dwOffset, pBuffer, cbInput); + + *pcbResult = bFile.bfSize; + + free(pBuffer); + return 0; +} + +int WriteDataToBitmapFile( + LPCWSTR lpFilePath, DWORD dwBitCount, + DWORD dwWidth, DWORD dwHeight, + PBYTE pbInput, DWORD cbInput, + PBYTE pbMetadata, DWORD cbMetadata, + bool pFlip) { + if (!lpFilePath || !pbInput) return -3; + + HANDLE hFile; + DWORD dwBytesWritten; + + hFile = CreateFileW( + lpFilePath, + GENERIC_WRITE, + FILE_SHARE_READ, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, + NULL); + if (hFile == INVALID_HANDLE_VALUE) return -1; + + // calculate the padded row size and padded image size + DWORD dwLineSize = (dwWidth * dwBitCount / 8 + 3) & ~3; + DWORD dwImageSize = dwLineSize * dwHeight; + + DWORD cbResult; + DWORD cbBuffer = dwImageSize + 0x500; + PBYTE pbBuffer = calloc(cbBuffer, 1); + if (!pbBuffer) return -2; + + if (ConvertDataToBitmap(dwBitCount, dwWidth, dwHeight, pbInput, dwImageSize, pbBuffer, cbBuffer, &cbResult, pFlip) < 0) { + cbResult = -1; + goto WriteDataToBitmapFile_End; + } + + WriteFile(hFile, pbBuffer, cbResult, &dwBytesWritten, NULL); + + if (pbMetadata) + WriteFile(hFile, pbMetadata, cbMetadata, &dwBytesWritten, NULL); + + CloseHandle(hFile); + + cbResult = dwBytesWritten; + +WriteDataToBitmapFile_End: + free(pbBuffer); + return cbResult; +} + +int WriteArrayToFile(LPCSTR lpOutputFilePath, LPVOID lpDataTemp, DWORD nDataSize, BOOL isAppend) { +#ifdef NDEBUG + + return nDataSize; + +#else + + HANDLE hFile; + DWORD dwBytesWritten; + DWORD dwDesiredAccess; + DWORD dwCreationDisposition; + + if (isAppend) { + dwDesiredAccess = FILE_APPEND_DATA; + dwCreationDisposition = OPEN_ALWAYS; + } else { + dwDesiredAccess = GENERIC_WRITE; + dwCreationDisposition = CREATE_ALWAYS; + } + + hFile = CreateFileA( + lpOutputFilePath, + dwDesiredAccess, + FILE_SHARE_READ, + NULL, + dwCreationDisposition, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, + NULL); + if (hFile == INVALID_HANDLE_VALUE) { + return FALSE; + } + + WriteFile(hFile, lpDataTemp, nDataSize, &dwBytesWritten, NULL); + CloseHandle(hFile); + + return dwBytesWritten; + +#endif +} \ No newline at end of file diff --git a/common/hooklib/imageutil.h b/common/hooklib/imageutil.h new file mode 100644 index 0000000..0c29c45 --- /dev/null +++ b/common/hooklib/imageutil.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +int ConvertDataToBitmap( + DWORD dwBitCount, + DWORD dwWidth, DWORD dwHeight, + PBYTE pbInput, DWORD cbInput, + PBYTE pbOutput, DWORD cbOutput, + PDWORD pcbResult, + bool pFlip); + +int WriteDataToBitmapFile( + LPCWSTR lpFilePath, DWORD dwBitCount, + DWORD dwWidth, DWORD dwHeight, + PBYTE pbInput, DWORD cbInput, + PBYTE pbMetadata, DWORD cbMetadata, + bool pFlip); + +int WriteArrayToFile(LPCSTR lpOutputFilePath, LPVOID lpDataTemp, DWORD nDataSize, BOOL isAppend); \ No newline at end of file diff --git a/common/hooklib/meson.build b/common/hooklib/meson.build index b0ad6c2..de87676 100644 --- a/common/hooklib/meson.build +++ b/common/hooklib/meson.build @@ -21,6 +21,8 @@ hooklib_lib = static_library( 'dvd.h', 'fdshark.c', 'fdshark.h', + 'imageutil.c', + 'imageutil.h', 'path.c', 'path.h', 'reg.c', @@ -31,7 +33,13 @@ hooklib_lib = static_library( 'spike.h', 'touch.c', 'touch.h', - 'printer.c', - 'printer.h', + 'printer_chc.c', + 'printer_chc.h', + 'printer_cx.c', + 'printer_cx.h', + 'y3.c', + 'y3.h', + 'y3-dll.c', + 'y3-dll.h', ], ) diff --git a/common/hooklib/path.c b/common/hooklib/path.c index a6f2342..f480c63 100644 --- a/common/hooklib/path.c +++ b/common/hooklib/path.c @@ -16,7 +16,6 @@ /* Helpers */ static void path_hook_init(void); -static BOOL path_transform_a(char **out, const char *src); /* API hooks */ @@ -110,6 +109,16 @@ static BOOL WINAPI hook_MoveFileW( const wchar_t *lpExistingFileName, const wchar_t *lpNewFileName); +static BOOL WINAPI hook_CopyFileA( + const char *lpExistingFileName, + const char *lpNewFileName, + BOOL bFailIfExists); + +static BOOL WINAPI hook_CopyFileW( + const wchar_t *lpExistingFileName, + const wchar_t *lpNewFileName, + BOOL bFailIfExists); + static BOOL WINAPI hook_MoveFileExA( const char *lpExistingFileName, const char *lpNewFileName, @@ -120,19 +129,6 @@ static BOOL WINAPI hook_MoveFileExW( const wchar_t *lpNewFileName, uint32_t dwFlags); - -static BOOL WINAPI hook_CopyFileA( - const LPCSTR lpExistingFileName, - const LPCSTR lpNewFileName, - BOOL bFailIfExists -); - -static BOOL WINAPI hook_CopyFileW( - const LPCWSTR lpExistingFileName, - const LPCWSTR lpNewFileName, - BOOL bFailIfExists -); - static BOOL WINAPI hook_CopyFileExA( LPCSTR lpExistingFileName, LPCSTR lpNewFileName, @@ -296,6 +292,16 @@ static BOOL (WINAPI *next_MoveFileW)( const wchar_t *lpExistingFileName, const wchar_t *lpNewFileName); +static BOOL (WINAPI *next_CopyFileA)( + const char *lpExistingFileName, + const char *lpNewFileName, + BOOL bFailIfExists); + +static BOOL (WINAPI *next_CopyFileW)( + const wchar_t *lpExistingFileName, + const wchar_t *lpNewFileName, + BOOL bFailIfExists); + static BOOL (WINAPI *next_MoveFileExA)( const char *lpExistingFileName, const char *lpNewFileName, @@ -490,7 +496,7 @@ static const struct hook_symbol path_hook_syms[] = { .link = (void **) &next_CopyFileW, }, { .name = "CopyFileExA", - .patch = hook_CopyFileExW, + .patch = hook_CopyFileExA, .link = (void **) &next_CopyFileExA, }, { .name = "CopyFileExW", @@ -596,7 +602,7 @@ void path_hook_insert_hooks(HMODULE target) _countof(path_hook_syms)); } -static BOOL path_transform_a(char **out, const char *src) +BOOL path_transform_a(char **out, const char *src) { wchar_t *src_w; size_t src_c; @@ -1249,6 +1255,75 @@ static BOOL WINAPI hook_MoveFileW( return ok; } + +static BOOL WINAPI hook_CopyFileA( + const char *lpExistingFileName, + const char *lpNewFileName, + BOOL bFailIfExists) +{ + char *oldTrans; + char *newTrans; + BOOL ok; + + ok = path_transform_a(&oldTrans, lpExistingFileName); + + if (!ok) { + return FALSE; + } + + ok = path_transform_a(&newTrans, lpNewFileName); + + if (!ok) { + free(oldTrans); + + return FALSE; + } + + ok = next_CopyFileA( + oldTrans ? oldTrans : lpExistingFileName, + newTrans ? newTrans : lpNewFileName, + bFailIfExists); + + free(oldTrans); + free(newTrans); + + return ok; +} + +static BOOL WINAPI hook_CopyFileW( + const wchar_t *lpExistingFileName, + const wchar_t *lpNewFileName, + BOOL bFailIfExists) +{ + wchar_t *oldTrans; + wchar_t *newTrans; + BOOL ok; + + ok = path_transform_w(&oldTrans, lpExistingFileName); + + if (!ok) { + return FALSE; + } + + ok = path_transform_w(&newTrans, lpNewFileName); + + if (!ok) { + free(oldTrans); + + return FALSE; + } + + ok = next_CopyFileW( + oldTrans ? oldTrans : lpExistingFileName, + newTrans ? newTrans : lpNewFileName, + bFailIfExists); + + free(oldTrans); + free(newTrans); + + return ok; +} + static BOOL WINAPI hook_MoveFileExA( const char *lpExistingFileName, const char *lpNewFileName, @@ -1317,75 +1392,6 @@ static BOOL WINAPI hook_MoveFileExW( return ok; } -static BOOL WINAPI hook_CopyFileA( - const LPCSTR lpExistingFileName, - const LPCSTR lpNewFileName, - BOOL bFailIfExists -) -{ - char *oldTrans; - char *newTrans; - BOOL ok; - - ok = path_transform_a(&oldTrans, lpExistingFileName); - - if (!ok) { - return FALSE; - } - - ok = path_transform_a(&newTrans, lpNewFileName); - - if (!ok) { - free(oldTrans); - - return FALSE; - } - - ok = next_CopyFileA( - oldTrans ? oldTrans : lpExistingFileName, - newTrans ? newTrans : lpNewFileName, - bFailIfExists); - - free(oldTrans); - free(newTrans); - - return ok; -} - -static BOOL WINAPI hook_CopyFileW( - const LPCWSTR lpExistingFileName, - const LPCWSTR lpNewFileName, - BOOL bFailIfExists -) { - wchar_t *oldTrans; - wchar_t *newTrans; - BOOL ok; - - ok = path_transform_w(&oldTrans, lpExistingFileName); - - if (!ok) { - return FALSE; - } - - ok = path_transform_w(&newTrans, lpNewFileName); - - if (!ok) { - free(oldTrans); - - return FALSE; - } - - ok = next_CopyFileW( - oldTrans ? oldTrans : lpExistingFileName, - newTrans ? newTrans : lpNewFileName, - bFailIfExists); - - free(oldTrans); - free(newTrans); - - return ok; -} - static BOOL WINAPI hook_CopyFileExA( LPCSTR lpExistingFileName, LPCSTR lpNewFileName, @@ -1713,3 +1719,177 @@ static UINT WINAPI hook_GetDriveTypeW( return result; } + +char** str_split_a(char* a_str, const char a_delim) { + char** result = 0; + size_t count = 0; + char* tmp = a_str; + char* last_comma = 0; + char delim[2]; + delim[0] = a_delim; + delim[1] = 0; + + /* Count how many elements will be extracted. */ + while (*tmp) + { + if (a_delim == *tmp) + { + count++; + last_comma = tmp; + } + tmp++; + } + + /* Add space for trailing token. */ + count += last_comma < (a_str + strlen(a_str) - 1); + + /* Add space for terminating null string so caller + knows where the list of returned strings ends. */ + count++; + + result = malloc(sizeof(char*) * count); + + if (result) + { + size_t idx = 0; + char* token = strtok(a_str, delim); + + while (token) + { + assert(idx < count); + *(result + idx++) = strdup(token); + token = strtok(0, delim); + } + assert(idx == count - 1); + *(result + idx) = 0; + } + + return result; +} + +BOOL path_transform_args_a(const char* str, char delimiter, char* buf, size_t size) { + assert(str != NULL); + assert(buf != NULL); + + if (size <= 0) { + return FALSE; + } + + char* copy = strdup(str); + char** tokens = str_split_a(copy, delimiter); + char *trans; + BOOL ok; + BOOL failed = FALSE; + + strcpy(buf, ""); + + if (tokens) { + int j; + for (j = 0; *(tokens + j); j++) { + ok = path_transform_a(&trans, *(tokens + j)); + free(*(tokens + j)); + if (ok) { + strcat_s(buf, size, trans ? trans : *(tokens+j)); + if (*(tokens + j + 1)) { + strcat_s(buf, size, " "); + } + free(trans); + } else { + failed = true; + } + } + free(tokens); + } + + free(copy); + + return !failed; +} + +wchar_t** str_split_w(wchar_t* a_str, const wchar_t a_delim) { + wchar_t** result = 0; + size_t count = 0; + wchar_t* tmp = a_str; + wchar_t* last_comma = 0; + wchar_t delim[2]; + delim[0] = a_delim; + delim[1] = 0; + + /* Count how many elements will be extracted. */ + while (*tmp) + { + if (a_delim == *tmp) + { + count++; + last_comma = tmp; + } + tmp++; + } + + /* Add space for trailing token. */ + count += last_comma < (a_str + wcslen(a_str) - 1); + + /* Add space for terminating null string so caller + knows where the list of returned strings ends. */ + count++; + + result = malloc(sizeof(wchar_t*) * count); + + wchar_t* pt; + + if (result) + { + size_t idx = 0; + wchar_t* token = wcstok_s(a_str, delim, &pt); + + while (token) + { + assert(idx < count); + *(result + idx++) = wcsdup(token); + token = wcstok_s(0, delim, &pt); + } + assert(idx == count - 1); + *(result + idx) = 0; + } + + return result; +} + +BOOL path_transform_args_w(const wchar_t* str, wchar_t delimiter, wchar_t* buf, size_t size) { + assert(str != NULL); + assert(buf != NULL); + + if (size <= 0) { + return FALSE; + } + + wchar_t* copy = wcsdup(str); + wchar_t** tokens = str_split_w(copy, delimiter); + wchar_t *trans; + BOOL ok; + BOOL failed = FALSE; + + wcscpy(buf, L""); + + if (tokens) { + int j; + for (j = 0; *(tokens + j); j++) { + ok = path_transform_w(&trans, *(tokens + j)); + if (ok) { + wcscat_s(buf, size, trans ? trans : *(tokens+j)); + if (*(tokens + j + 1)) { + wcscat_s(buf, size, L" "); + } + free(trans); + } else { + failed = true; + } + free(*(tokens + j)); + } + free(tokens); + } + + free(copy); + + return !failed; +} \ No newline at end of file diff --git a/common/hooklib/path.h b/common/hooklib/path.h index eb55dd7..aca9356 100644 --- a/common/hooklib/path.h +++ b/common/hooklib/path.h @@ -13,7 +13,10 @@ typedef HRESULT (*path_hook_t)( HRESULT path_hook_push(path_hook_t hook); void path_hook_insert_hooks(HMODULE target); int path_compare_w(const wchar_t *string1, const wchar_t *string2, size_t count); +BOOL path_transform_a(char **out, const char *src); BOOL path_transform_w(wchar_t **out, const wchar_t *src); +BOOL path_transform_args_a(const char* str, char delimiter, char* buf, size_t size); +BOOL path_transform_args_w(const wchar_t* str, wchar_t delimiter, wchar_t* buf, size_t size); static inline bool path_is_separator_w(wchar_t c) { diff --git a/common/hooklib/printer.c b/common/hooklib/printer_chc.c similarity index 88% rename from common/hooklib/printer.c rename to common/hooklib/printer_chc.c index dbd56f9..0100f2f 100644 --- a/common/hooklib/printer.c +++ b/common/hooklib/printer_chc.c @@ -9,7 +9,9 @@ chc (emihiok) */ -#include "hooklib/printer.h" +// ReSharper disable CppParameterNeverUsed +// ReSharper disable CppDFAConstantFunctionResult +#include "hooklib/printer_chc.h" #include #include @@ -90,6 +92,7 @@ int WINAPI chcusb_listupPrinterSN(uint64_t *rSerialArray); int WINAPI chcusb_selectPrinter(uint8_t printerId, uint16_t *rResult); int WINAPI chcusb_selectPrinterSN(uint64_t printerSN, uint16_t *rResult); int WINAPI chcusb_getPrinterInfo(uint16_t tagNumber, uint8_t *rBuffer, uint32_t *rLen); +int WINAPI chcusb_getPrinterInfo_300(uint16_t tagNumber, uint8_t *rBuffer, uint32_t *rLen); int WINAPI chcusb_imageformat(uint16_t format, uint16_t ncomp, uint16_t depth, uint16_t width, uint16_t height, uint8_t * image, uint16_t* rResult); int WINAPI chcusb_imageformat_330( uint16_t format, @@ -98,7 +101,7 @@ int WINAPI chcusb_imageformat_330( uint16_t width, uint16_t height, uint16_t *rResult); -int __fastcall chcusb_setmtf(int32_t *mtf); +int WINAPI chcusb_setmtf(int32_t *mtf); int WINAPI chcusb_makeGamma(uint16_t k, uint8_t *intoneR, uint8_t *intoneG, uint8_t *intoneB); int WINAPI chcusb_setIcctable( LPCSTR icc1, @@ -147,6 +150,7 @@ int WINAPI chcusb_getEEPROM(uint8_t index, uint8_t *rData, uint16_t *rResult); int WINAPI chcusb_setParameter(uint8_t a1, uint32_t a2, uint16_t *rResult); int WINAPI chcusb_getParameter(uint8_t a1, uint8_t *a2, uint16_t *rResult); int WINAPI chcusb_universal_command(int32_t a1, uint8_t a2, int32_t a3, uint8_t *a4, uint16_t *rResult); +int WINAPI chcusb_writeIred(uint8_t* a1, uint8_t* a2, uint16_t* rResult); /* PrintDLL API hooks */ @@ -410,217 +414,6 @@ static const struct hook_symbol C3XXFWDLusb_hooks[] = { }, }; -/* C300usb hook tbl */ - -static const struct hook_symbol C300usb_hooks[] = { - { - .name = "chcusb_MakeThread", - .ordinal = 0x0001, - .patch = chcusb_MakeThread, - .link = NULL - }, { - .name = "chcusb_open", - .ordinal = 0x0002, - .patch = chcusb_open, - .link = NULL - }, { - .name = "chcusb_close", - .ordinal = 0x0003, - .patch = chcusb_close, - .link = NULL - }, { - .name = "chcusb_ReleaseThread", - .ordinal = 0x0004, - .patch = chcusb_ReleaseThread, - .link = NULL - }, { - .name = "chcusb_listupPrinter", - .ordinal = 0x0005, - .patch = chcusb_listupPrinter, - .link = NULL - }, { - .name = "chcusb_listupPrinterSN", - .ordinal = 0x0006, - .patch = chcusb_listupPrinterSN, - .link = NULL - }, { - .name = "chcusb_selectPrinter", - .ordinal = 0x0007, - .patch = chcusb_selectPrinter, - .link = NULL - }, { - .name = "chcusb_selectPrinterSN", - .ordinal = 0x0008, - .patch = chcusb_selectPrinterSN, - .link = NULL - }, { - .name = "chcusb_getPrinterInfo", - .ordinal = 0x0009, - .patch = chcusb_getPrinterInfo, - .link = NULL - }, { - .name = "chcusb_imageformat", - .ordinal = 0x000a, - .patch = chcusb_imageformat, - .link = NULL - }, { - .name = "chcusb_setmtf", - .ordinal = 0x000b, - .patch = chcusb_setmtf, - .link = NULL - }, { - .name = "chcusb_makeGamma", - .ordinal = 0x000c, - .patch = chcusb_makeGamma, - .link = NULL - }, { - .name = "chcusb_setIcctable", - .ordinal = 0x000d, - .patch = chcusb_setIcctable, - .link = NULL - }, { - .name = "chcusb_copies", - .ordinal = 0x000e, - .patch = chcusb_copies, - .link = NULL - }, { - .name = "chcusb_status", - .ordinal = 0x000f, - .patch = chcusb_status, - .link = NULL - }, { - .name = "chcusb_statusAll", - .ordinal = 0x0010, - .patch = chcusb_statusAll, - .link = NULL - }, { - .name = "chcusb_startpage", - .ordinal = 0x0011, - .patch = chcusb_startpage_300, - .link = NULL - }, { - .name = "chcusb_endpage", - .ordinal = 0x0012, - .patch = chcusb_endpage, - .link = NULL - }, { - .name = "chcusb_write", - .ordinal = 0x0013, - .patch = chcusb_write, - .link = NULL - }, { - .name = "chcusb_writeLaminate", - .ordinal = 0x0014, - .patch = chcusb_writeLaminate, - .link = NULL - }, { - .name = "chcusb_setPrinterInfo", - .ordinal = 0x0015, - .patch = chcusb_setPrinterInfo, - .link = NULL - }, { - .name = "chcusb_getGamma", - .ordinal = 0x0016, - .patch = chcusb_getGamma, - .link = NULL - }, { - .name = "chcusb_getMtf", - .ordinal = 0x0017, - .patch = chcusb_getMtf, - .link = NULL - }, { - .name = "chcusb_cancelCopies", - .ordinal = 0x0018, - .patch = chcusb_cancelCopies, - .link = NULL - }, { - .name = "chcusb_setPrinterToneCurve", - .ordinal = 0x0019, - .patch = chcusb_setPrinterToneCurve, - .link = NULL - }, { - .name = "chcusb_getPrinterToneCurve", - .ordinal = 0x001a, - .patch = chcusb_getPrinterToneCurve, - .link = NULL - }, { - .name = "chcusb_blinkLED", - .ordinal = 0x001b, - .patch = chcusb_blinkLED, - .link = NULL - }, { - .name = "chcusb_resetPrinter", - .ordinal = 0x001c, - .patch = chcusb_resetPrinter, - .link = NULL - }, { - .name = "chcusb_AttachThreadCount", - .ordinal = 0x001d, - .patch = chcusb_AttachThreadCount, - .link = NULL - }, { - .name = "chcusb_getPrintIDStatus", - .ordinal = 0x001e, - .patch = chcusb_getPrintIDStatus, - .link = NULL - }, { - .name = "chcusb_setPrintStandby", - .ordinal = 0x001f, - .patch = chcusb_setPrintStandby_300, - .link = NULL - }, { - .name = "chcusb_testCardFeed", - .ordinal = 0x0020, - .patch = chcusb_testCardFeed, - .link = NULL - }, { - .name = "chcusb_setParameter", - .ordinal = 0x0021, - .patch = chcusb_setParameter, - .link = NULL - }, { - .name = "chcusb_getParameter", - .ordinal = 0x0022, - .patch = chcusb_getParameter, - .link = NULL - }, { - .name = "chcusb_getErrorStatus", - .ordinal = 0x0023, - .patch = chcusb_getErrorStatus, - .link = NULL - }, { - .name = "chcusb_setCutList", - .ordinal = 0x0028, - .patch = chcusb_setCutList, - .link = NULL - }, { - .name = "chcusb_setLaminatePattern", - .ordinal = 0x0029, - .patch = chcusb_setLaminatePattern, - .link = NULL - }, { - .name = "chcusb_color_adjustment", - .ordinal = 0x002a, - .patch = chcusb_color_adjustment, - .link = NULL - }, { - .name = "chcusb_color_adjustmentEx", - .ordinal = 0x002b, - .patch = chcusb_color_adjustmentEx, - .link = NULL - }, { - .name = "chcusb_getEEPROM", - .ordinal = 0x003a, - .patch = chcusb_getEEPROM, - .link = NULL - }, { - .name = "chcusb_universal_command", - .ordinal = 0x0049, - .patch = chcusb_universal_command, - .link = NULL - }, -}; - /* C310A-Busb/C320Ausb/C330Ausb hook tbl. The ordinals are required, as some games, for example Sekito, will import this library by ordinal and not by name. */ @@ -666,11 +459,6 @@ static const struct hook_symbol C3XXusb_hooks[] = { .ordinal = 0x0008, .patch = chcusb_selectPrinterSN, .link = NULL - }, { - .name = "chcusb_getPrinterInfo", - .ordinal = 0x0009, - .patch = chcusb_getPrinterInfo, - .link = NULL }, { .name = "chcusb_imageformat", .ordinal = 0x000a, @@ -706,11 +494,6 @@ static const struct hook_symbol C3XXusb_hooks[] = { .ordinal = 0x0010, .patch = chcusb_statusAll, .link = NULL - }, { - .name = "chcusb_startpage", - .ordinal = 0x0011, - .patch = chcusb_startpage, - .link = NULL }, { .name = "chcusb_endpage", .ordinal = 0x0012, @@ -781,11 +564,6 @@ static const struct hook_symbol C3XXusb_hooks[] = { .ordinal = 0x001f, .patch = chcusb_getPrintIDStatus, .link = NULL - }, { - .name = "chcusb_setPrintStandby", - .ordinal = 0x0020, - .patch = chcusb_setPrintStandby, - .link = NULL }, { .name = "chcusb_testCardFeed", .ordinal = 0x0021, @@ -841,6 +619,11 @@ static const struct hook_symbol C3XXusb_hooks[] = { .ordinal = 0x002b, .patch = chcusb_color_adjustmentEx, .link = NULL + }, { + .name = "chcusb_writeIred", + .ordinal = 0x0032, + .patch = chcusb_writeIred, + .link = NULL }, { .name = "chcusb_getEEPROM", .ordinal = 0x003a, @@ -864,6 +647,79 @@ static const struct hook_symbol C3XXusb_hooks[] = { }, }; +/* C300A-Busb specific hook tbl. */ + +static const struct hook_symbol C300usb_hooks[] = { + { + .name = "chcusb_getPrinterInfo", + .ordinal = 0x0009, + .patch = chcusb_getPrinterInfo_300, + .link = NULL + }, { + .name = "chcusb_startpage", + .ordinal = 0x0011, + .patch = chcusb_startpage_300, + .link = NULL + }, { + .name = "chcusb_setPrintStandby", + .ordinal = 0x0020, + .patch = chcusb_setPrintStandby_300, + .link = NULL + } +}; + +/* C310A-Busb specific hook tbl. */ + +static const struct hook_symbol C310usb_hooks[] = { + { + .name = "chcusb_getPrinterInfo", + .ordinal = 0x0009, + .patch = chcusb_getPrinterInfo, + .link = NULL + }, { + .name = "chcusb_imageformat", + .ordinal = 0x000a, + .patch = chcusb_imageformat, + .link = NULL + }, { + .name = "chcusb_startpage", + .ordinal = 0x0011, + .patch = chcusb_startpage, + .link = NULL + }, { + .name = "chcusb_setPrintStandby", + .ordinal = 0x0020, + .patch = chcusb_setPrintStandby, + .link = NULL + } +}; + +/* C330Ausb specific hook tbl. */ + +static const struct hook_symbol C330usb_hooks[] = { + { + .name = "chcusb_getPrinterInfo", + .ordinal = 0x0009, + .patch = chcusb_getPrinterInfo, + .link = NULL + }, { + .name = "chcusb_imageformat", + .ordinal = 0x000a, + .patch = chcusb_imageformat_330, + .link = NULL + }, { + .name = "chcusb_startpage", + .ordinal = 0x0011, + .patch = chcusb_startpage, + .link = NULL + }, { + .name = "chcusb_setPrintStandby", + .ordinal = 0x0020, + .patch = chcusb_setPrintStandby, + .link = NULL + } +}; + /* PrintDLL hook tbl */ static struct hook_symbol printdll_hooks[] = { @@ -1150,9 +1006,9 @@ static struct hook_symbol printdll_hooks[] = { }, }; -static struct printer_config printer_config; +static struct printer_chc_config printer_config; -void printer_hook_init(const struct printer_config *cfg, int rfid_port_no, HINSTANCE self) { +void printer_chc_hook_init(const struct printer_chc_config *cfg, int rfid_port_no, HINSTANCE self) { HANDLE fwFile = NULL; DWORD bytesRead = 0; @@ -1170,7 +1026,7 @@ void printer_hook_init(const struct printer_config *cfg, int rfid_port_no, HINST rotate180 = cfg->rotate_180; memcpy(&printer_config, cfg, sizeof(*cfg)); - printer_hook_insert_hooks(NULL); + printer_chc_hook_insert_hooks(NULL); /* if (self != NULL) { @@ -1229,7 +1085,7 @@ void printer_hook_init(const struct printer_config *cfg, int rfid_port_no, HINST dprintf("Printer: hook enabled.\n"); } -void printer_hook_insert_hooks(HMODULE target) { +void printer_chc_hook_insert_hooks(HMODULE target) { hook_table_apply(target, "C310Ausb.dll", C3XXusb_hooks, _countof(C3XXusb_hooks)); hook_table_apply(target, "C310Busb.dll", C3XXusb_hooks, _countof(C3XXusb_hooks)); hook_table_apply(target, "C310FWDLusb.dll", C3XXFWDLusb_hooks, _countof(C3XXFWDLusb_hooks)); @@ -1239,9 +1095,18 @@ void printer_hook_insert_hooks(HMODULE target) { hook_table_apply(target, "C330Ausb.dll", C3XXusb_hooks, _countof(C3XXusb_hooks)); hook_table_apply(target, "C330AFWDLusb.dll", C3XXFWDLusb_hooks, _countof(C3XXFWDLusb_hooks)); + /* specific C300Ausb hooks */ + hook_table_apply(target, "C300usb.dll", C300usb_hooks, _countof(C300usb_hooks)); + proc_addr_table_push(target, "C300usb.dll", C300usb_hooks, _countof(C300usb_hooks)); + + /* specific C310Ausb/C320usb/C330Ausb hooks */ + hook_table_apply(target, "C310Ausb.dll", C330usb_hooks, _countof(C330usb_hooks)); + hook_table_apply(target, "C320Ausb.dll", C330usb_hooks, _countof(C330usb_hooks)); + hook_table_apply(target, "C330Ausb.dll", C330usb_hooks, _countof(C330usb_hooks)); + /* Unity workaround */ proc_addr_table_push(target, "PrintDLL.dll", printdll_hooks, _countof(printdll_hooks)); - proc_addr_table_push(target, "C300usb.dll", C300usb_hooks, _countof(C300usb_hooks)); + proc_addr_table_push(target, "C300usb.dll", C3XXusb_hooks, _countof(C3XXusb_hooks)); proc_addr_table_push(target, "C300FWDLusb.dll", C3XXFWDLusb_hooks, _countof(C3XXFWDLusb_hooks)); } @@ -2160,6 +2025,11 @@ int WINAPI chcusb_getPrinterInfo(uint16_t tagNumber, uint8_t *rBuffer, uint32_t if (rBuffer) memset(rBuffer, 0, *rLen); break; + case 2: // unknown + if (*rLen != 0x17) *rLen = 0x17; + if (rBuffer) memset(rBuffer, 0, *rLen); + break; + case 3: // getFirmwareVersion if (*rLen != 0x99) *rLen = 0x99; if (rBuffer) { @@ -2307,6 +2177,58 @@ int WINAPI chcusb_getPrinterInfo(uint16_t tagNumber, uint8_t *rBuffer, uint32_t return 1; } +int WINAPI chcusb_getPrinterInfo_300(uint16_t tagNumber, uint8_t *rBuffer, uint32_t *rLen) { + // dprintf("Printer: C3XXusb: %s(%d)\n", __func__, tagNumber); + + switch (tagNumber) { + case 3: // getFirmwareVersion + if (*rLen != 0x99) *rLen = 0x99; + if (rBuffer) { + memset(rBuffer, 0, *rLen); + // C300 has 4 firmwares + rBuffer[0] = 4; // firmware count + + // bootFirmware + int i = 1; + memcpy(rBuffer + i, mainFirmware, sizeof(mainFirmware)); + // mainFirmware + i += 0x26; + memcpy(rBuffer + i, mainFirmware, sizeof(mainFirmware)); + // printParameterTable + i += 0x26; + memcpy(rBuffer + i, paramFirmware, sizeof(paramFirmware)); + // dspFirmware (C300 only) + i += 0x26; + memcpy(rBuffer + i, dspFirmware, sizeof(dspFirmware)); + } + return 1; + + case 4: // getPrintCountInfo (C300 only) + if (!rBuffer) { + *rLen = 0x1C; + return 1; + } + int32_t bInfoC300[10] = {0}; + bInfoC300[0] = 22; // printCounter0 + bInfoC300[1] = 23; // printCounter1 + bInfoC300[2] = 33; // feedRollerCount + bInfoC300[3] = 55; // cutterCount + bInfoC300[4] = 88; // headCount + bInfoC300[5] = 999; // ribbonRemain + bInfoC300[6] = 0; // dummy + if (*rLen <= 0x1Cu) { + memcpy(rBuffer, bInfoC300, *rLen); + } else { + bInfoC300[7] = 0; // TODO + if (*rLen > 0x20u) *rLen = 0x20; + memcpy(rBuffer, bInfoC300, *rLen); + } + return 1; + } + + return chcusb_getPrinterInfo(tagNumber, rBuffer, rLen); +} + int WINAPI chcusb_imageformat( uint16_t format, uint16_t ncomp, @@ -2334,7 +2256,7 @@ int WINAPI chcusb_imageformat_330( return 1; } -int __fastcall chcusb_setmtf(int32_t *mtf) { +int WINAPI chcusb_setmtf(int32_t *mtf) { dprintf("Printer: C3XXusb: %s\n", __func__); memcpy(MTF, mtf, sizeof(MTF)); @@ -3147,208 +3069,13 @@ int CHCUSB_writeLaminate(const void *handle, uint8_t *data, uint32_t offset, uin return chcusb_writeLaminate(data, writeSize, rResult); } -// copy pasted from https://dev.s-ul.net/domeori/c310emu -#define BITMAPHEADERSIZE 0x36 - -DWORD ConvertDataToBitmap( - DWORD dwBitCount, - DWORD dwWidth, DWORD dwHeight, - PBYTE pbInput, DWORD cbInput, - PBYTE pbOutput, DWORD cbOutput, - PDWORD pcbResult, - bool pFlip) { - if (!pbInput || !pbOutput || dwBitCount < 8) return -3; - - if (cbInput < (dwWidth * dwHeight * dwBitCount / 8)) return -3; - - PBYTE pBuffer = (PBYTE)malloc(cbInput); - if (!pBuffer) return -2; - - BYTE dwColors = (BYTE)(dwBitCount / 8); - if (!dwColors) return -1; - - UINT16 cbColors; - RGBQUAD pbColors[256]; - - switch (dwBitCount) { - case 1: - cbColors = 1; - break; - case 2: - cbColors = 4; - break; - case 4: - cbColors = 16; - break; - case 8: - cbColors = 256; - break; - default: - cbColors = 0; - break; - } - - if (cbColors) { - BYTE dwStep = (BYTE)(256 / cbColors); - - for (UINT16 i = 0; i < cbColors; ++i) { - pbColors[i].rgbRed = dwStep * i; - pbColors[i].rgbGreen = dwStep * i; - pbColors[i].rgbBlue = dwStep * i; - pbColors[i].rgbReserved = 0; - } - } - - DWORD dwTable = cbColors * sizeof(RGBQUAD); - DWORD dwOffset = BITMAPHEADERSIZE + dwTable; - - // calculate the padded row size, again - DWORD dwLineSize = (dwWidth * dwBitCount / 8 + 3) & ~3; - - BITMAPFILEHEADER bFile = {0}; - BITMAPINFOHEADER bInfo = {0}; - - bFile.bfType = 0x4D42; // MAGIC - bFile.bfSize = dwOffset + cbInput; - bFile.bfOffBits = dwOffset; - - bInfo.biSize = sizeof(BITMAPINFOHEADER); - bInfo.biWidth = dwWidth; - bInfo.biHeight = dwHeight; - bInfo.biPlanes = 1; - bInfo.biBitCount = (WORD)dwBitCount; - bInfo.biCompression = BI_RGB; - bInfo.biSizeImage = cbInput; - - if (cbOutput < bFile.bfSize) return -1; - - // Flip the image (if necessary) and add padding to each row - if (pFlip) { - for (size_t i = 0; i < dwHeight; i++) { - for (size_t j = 0; j < dwWidth; j++) { - for (size_t k = 0; k < dwColors; k++) { - // Calculate the position in the padded buffer - // Make sure to also flip the colors from RGB to BRG - size_t x = (dwHeight - i - 1) * dwLineSize + (dwWidth - j - 1) * dwColors + (dwColors - k - 1); - size_t y = (dwHeight - i - 1) * dwWidth * dwColors + j * dwColors + k; - *(pBuffer + x) = *(pbInput + y); - } - } - } - } else { - for (size_t i = 0; i < dwHeight; i++) { - for (size_t j = 0; j < dwWidth; j++) { - for (size_t k = 0; k < dwColors; k++) { - // Calculate the position in the padded buffer - size_t x = i * dwLineSize + j * dwColors + (dwColors - k - 1); - size_t y = (dwHeight - i - 1) * dwWidth * dwColors + j * dwColors + k; - *(pBuffer + x) = *(pbInput + y); - } - } - } - } - - memcpy(pbOutput, &bFile, sizeof(BITMAPFILEHEADER)); - memcpy(pbOutput + sizeof(BITMAPFILEHEADER), &bInfo, sizeof(BITMAPINFOHEADER)); - if (cbColors) memcpy(pbOutput + BITMAPHEADERSIZE, pbColors, dwTable); - memcpy(pbOutput + dwOffset, pBuffer, cbInput); - - *pcbResult = bFile.bfSize; - - free(pBuffer); - return 0; -} - -DWORD WriteDataToBitmapFile( - LPCWSTR lpFilePath, DWORD dwBitCount, - DWORD dwWidth, DWORD dwHeight, - PBYTE pbInput, DWORD cbInput, - PBYTE pbMetadata, DWORD cbMetadata, - bool pFlip) { - if (!lpFilePath || !pbInput) return -3; - - HANDLE hFile; - DWORD dwBytesWritten; - - hFile = CreateFileW( - lpFilePath, - GENERIC_WRITE, - FILE_SHARE_READ, - NULL, - CREATE_ALWAYS, - FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, - NULL); - if (hFile == INVALID_HANDLE_VALUE) return -1; - - // calculate the padded row size and padded image size - DWORD dwLineSize = (dwWidth * dwBitCount / 8 + 3) & ~3; - DWORD dwImageSize = dwLineSize * dwHeight; - - DWORD cbResult; - DWORD cbBuffer = dwImageSize + 0x500; - PBYTE pbBuffer = (PBYTE)calloc(cbBuffer, 1); - if (!pbBuffer) return -2; - - if (ConvertDataToBitmap(dwBitCount, dwWidth, dwHeight, pbInput, dwImageSize, pbBuffer, cbBuffer, &cbResult, pFlip) < 0) { - cbResult = -1; - goto WriteDataToBitmapFile_End; - } - - WriteFile(hFile, pbBuffer, cbResult, &dwBytesWritten, NULL); - - if (pbMetadata) - WriteFile(hFile, pbMetadata, cbMetadata, &dwBytesWritten, NULL); - - CloseHandle(hFile); - - cbResult = dwBytesWritten; - -WriteDataToBitmapFile_End: - free(pbBuffer); - return cbResult; -} - -DWORD WriteArrayToFile(LPCSTR lpOutputFilePath, LPVOID lpDataTemp, DWORD nDataSize, BOOL isAppend) { -#ifdef NDEBUG - - return nDataSize; - -#else - - HANDLE hFile; - DWORD dwBytesWritten; - DWORD dwDesiredAccess; - DWORD dwCreationDisposition; - - if (isAppend) { - dwDesiredAccess = FILE_APPEND_DATA; - dwCreationDisposition = OPEN_ALWAYS; - } else { - dwDesiredAccess = GENERIC_WRITE; - dwCreationDisposition = CREATE_ALWAYS; - } - - hFile = CreateFileA( - lpOutputFilePath, - dwDesiredAccess, - FILE_SHARE_READ, - NULL, - dwCreationDisposition, - FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, - NULL); - if (hFile == INVALID_HANDLE_VALUE) { - return FALSE; - } - - WriteFile(hFile, lpDataTemp, nDataSize, &dwBytesWritten, NULL); - CloseHandle(hFile); - - return dwBytesWritten; - -#endif -} - void printer_set_dimensions(int width, int height){ WIDTH = width; HEIGHT = height; } + +int WINAPI chcusb_writeIred(uint8_t* a1, uint8_t* a2, uint16_t* rResult) { + dprintf("Printer: C3XXusb: %s\n", __func__); + *rResult = 0; + return 1; +} diff --git a/common/hooklib/printer.h b/common/hooklib/printer_chc.h similarity index 74% rename from common/hooklib/printer.h rename to common/hooklib/printer_chc.h index 3e451d1..cb6886b 100644 --- a/common/hooklib/printer.h +++ b/common/hooklib/printer_chc.h @@ -4,7 +4,7 @@ #include #include -struct printer_config { +struct printer_chc_config { bool enable; bool rotate_180; char serial_no[8]; @@ -15,8 +15,8 @@ struct printer_config { uint32_t wait_time; }; -void printer_hook_init(const struct printer_config *cfg, int rfid_port_no, HINSTANCE self); -void printer_hook_insert_hooks(HMODULE target); +void printer_chc_hook_init(const struct printer_chc_config *cfg, int rfid_port_no, HINSTANCE self); +void printer_chc_hook_insert_hooks(HMODULE target); void printer_set_dimensions(int width, int height); int WINAPI fwdlusb_updateFirmware_main(uint8_t update, LPCSTR filename, uint16_t *rResult); diff --git a/common/hooklib/printer_cx.c b/common/hooklib/printer_cx.c new file mode 100644 index 0000000..b2f5e3e --- /dev/null +++ b/common/hooklib/printer_cx.c @@ -0,0 +1,906 @@ +// ReSharper disable CppParameterNeverUsed +// ReSharper disable CppParameterMayBeConstPtrOrRef +#include "printer_cx.h" + +#include +#include +#include +#include +#include +#include + +#include "imageutil.h" +#include "hook/procaddr.h" +#include "hook/table.h" +#include "util/dprintf.h" + +#pragma region prototypes +static int WINAPI hook_CXCMD_Retransfer(); + +static bool WINAPI hook_CXCMD_CheckIfConnected(int* pSlotId, int* pId); + +static int WINAPI hook_CXCMD_xImageOut(); + +static int WINAPI hook_CXCMD_MoveCard(int slotId, int id, int dest, int flip, int filmInit, int immed); + +static int WINAPI hook_CXCMD_xWriteMagData(); + +static int WINAPI hook_CXCMD_SecurityPrint(int slotId, int id, int color, int bufferIndex, int immed); + +static int WINAPI hook_CXCMD_ScanPrinterNext(); + +static int WINAPI hook_CXCMD_Print(int slotId, int id, int color, int bufferIndex, int immed); + +static int WINAPI hook_CXCMD_RezeroUnit(int slotId, int id, int action); + +static int WINAPI hook_CXCMD_WriteMagData(); + +static int WINAPI hook_CXCMD_LogSense(int iSlot, int iID, int iPage, uint8_t* pbyBuffer); + +static int WINAPI hook_CXCMD_StandardInquiry(int iSlot, int iID, uint8_t* pbyBuffer); + +static int WINAPI hook_CXCMD_ModeSense(int iSlot, int iID, int iPC, int iPage, uint8_t* pbyBuffer); + +static int WINAPI hook_CXCMD_UpdateFirmware(int iSlot, int iID, char* pFile, int iDataID); + +static int WINAPI hook_CXCMD_ModeSelect(int iSlot, int iID, int iSp, int iPage, uint8_t* pbyData); + +static int WINAPI hook_CXCMD_GetPrintingStatus(); + +static int WINAPI hook_CXCMD_SendDiagnostic(int iSlot, int iID, int iTestMode, int iTestPatten, int iTestCount); + +static int WINAPI hook_CXCMD_RetransferAndTurn(int slotId, int id, int immed); + +static int WINAPI hook_CXCMD_LogSelect(int iSlot, int iID, int iMod); + +static int WINAPI hook_CXCMD_ReadPosition(int slotId, int id, uint8_t* buffer); + +static int WINAPI hook_CXCMD_SecurityLock(); + +static int WINAPI hook_CXCMD_SetPrintingStatus(); + +static int WINAPI hook_CXCMD_xReadISOMagData(); + +static int WINAPI hook_CXCMD_WriteISO3TrackMagData(); + +static int WINAPI hook_CXCMD_PasswordSet(); + +static int WINAPI hook_CXCMD_GetPrinterStatus(); + +static int WINAPI hook_CXCMD_WriteProjectCode(); + +static int WINAPI hook_CXCMD_LoadCard(int slotId, int id, int dest, int flip, int filmInit, int immed); + +static int WINAPI hook_CXCMD_ReadMagData(); + +static int WINAPI hook_CXCMD_ScanPrinter(int* pSlotId, int* pId); + +static int WINAPI hook_CXCMD_xWriteISOMagData(); + +static int WINAPI hook_CXCMD_ICControl(); + +static int WINAPI hook_CXCMD_ImageOut(int slotId, int id, uint8_t* plane, int length, int color, int bufferIndex); + +static int WINAPI hook_CXCMD_ReadBuffer(int iSlot, int iID, int iMode, int iBufferID, uint8_t* pbyData, int iOffset, + int iLength); + +static int WINAPI hook_CXCMD_xReadMagData(); + +static int WINAPI hook_CXCMD_WriteBuffer(int iSlot, int iID, int iMode, int iBufferID, uint8_t* pbyData, int iOffset, + int iLength); + +static int WINAPI hook_CXCMD_DefineLUT(int slotId, int id, int color, int length, uint8_t* buffer); + +static int WINAPI hook_CXCMD_ReadISO3TrackMagData(); + +static int WINAPI hook_CXCMD_TestUnitReady(int slotId, int id); + +static int WINAPI hook_CXCMD_RetransferAndEject(int slotId, int id, int immed); + +static bool WINAPI hook_Lut24_Exchange(const wchar_t* pFile, uint8_t* y, uint8_t* m, uint8_t* c, int sizeY, int sizeM, + int sizeC); + +static bool WINAPI hook_Wdata_create(uint8_t* pbyRdata, uint8_t* pbyGdata, uint8_t* pbyBdata, int iWidth, + int iHeight, bool bPortrait, int iAlgorithim, uint8_t* pbyWdata); +#pragma endregion + +#pragma region hooktables +static const struct hook_symbol hook_pcp_syms[] = { + { + .name = "CXCMD_Retransfer", + .patch = hook_CXCMD_Retransfer, + .ordinal = 19, + }, + { + .name = "CXCMD_CheckIfConnected", + .patch = hook_CXCMD_CheckIfConnected, + .ordinal = 1, + }, + { + .name = "CXCMD_xImageOut", + .patch = hook_CXCMD_xImageOut, + .ordinal = 36, + }, + { + .name = "CXCMD_MoveCard", + .patch = hook_CXCMD_MoveCard, + .ordinal = 12, + }, + { + .name = "CXCMD_xWriteMagData", + .patch = hook_CXCMD_xWriteMagData, + .ordinal = 40, + }, + { + .name = "CXCMD_SecurityPrint", + .patch = hook_CXCMD_SecurityPrint, + .ordinal = 26, + }, + { + .name = "CXCMD_ScanPrinterNext", + .patch = hook_CXCMD_ScanPrinterNext, + .ordinal = 24, + }, + { + .name = "CXCMD_Print", + .patch = hook_CXCMD_Print, + .ordinal = 14, + }, + { + .name = "CXCMD_RezeroUnit", + .patch = hook_CXCMD_RezeroUnit, + .ordinal = 22, + }, + { + .name = "CXCMD_WriteMagData", + .patch = hook_CXCMD_WriteMagData, + .ordinal = 34, + }, + { + .name = "CXCMD_LogSense", + .patch = hook_CXCMD_LogSense, + .ordinal = 9, + }, + { + .name = "CXCMD_StandardInquiry", + .patch = hook_CXCMD_StandardInquiry, + .ordinal = 29, + }, + { + .name = "CXCMD_ModeSense", + .patch = hook_CXCMD_ModeSense, + .ordinal = 11, + }, + { + .name = "CXCMD_UpdateFirmware", + .patch = hook_CXCMD_UpdateFirmware, + .ordinal = 31, + }, + { + .name = "CXCMD_ModeSelect", + .patch = hook_CXCMD_ModeSelect, + .ordinal = 10, + }, + { + .name = "CXCMD_GetPrintingStatus", + .patch = hook_CXCMD_GetPrintingStatus, + .ordinal = 4, + }, + { + .name = "CXCMD_SendDiagnostic", + .patch = hook_CXCMD_SendDiagnostic, + .ordinal = 27, + }, + { + .name = "CXCMD_RetransferAndTurn", + .patch = hook_CXCMD_RetransferAndTurn, + .ordinal = 21, + }, + { + .name = "CXCMD_LogSelect", + .patch = hook_CXCMD_LogSelect, + .ordinal = 8, + }, + { + .name = "CXCMD_ReadPosition", + .patch = hook_CXCMD_ReadPosition, + .ordinal = 18, + }, + { + .name = "CXCMD_SecurityLock", + .patch = hook_CXCMD_SecurityLock, + .ordinal = 25, + }, + { + .name = "CXCMD_SetPrintingStatus", + .patch = hook_CXCMD_SetPrintingStatus, + .ordinal = 28, + }, + { + .name = "CXCMD_xReadISOMagData", + .patch = hook_CXCMD_xReadISOMagData, + .ordinal = 37, + }, + { + .name = "CXCMD_WriteISO3TrackMagData", + .patch = hook_CXCMD_WriteISO3TrackMagData, + .ordinal = 33, + }, + { + .name = "CXCMD_PasswordSet", + .patch = hook_CXCMD_PasswordSet, + .ordinal = 13, + }, + { + .name = "CXCMD_GetPrinterStatus", + .patch = hook_CXCMD_GetPrinterStatus, + .ordinal = 3, + }, + { + .name = "CXCMD_WriteProjectCode", + .patch = hook_CXCMD_WriteProjectCode, + .ordinal = 35, + }, + { + .name = "CXCMD_LoadCard", + .patch = hook_CXCMD_LoadCard, + .ordinal = 7, + }, + { + .name = "CXCMD_ReadMagData", + .patch = hook_CXCMD_ReadMagData, + .ordinal = 17, + }, + { + .name = "CXCMD_ScanPrinter", + .patch = hook_CXCMD_ScanPrinter, + .ordinal = 23, + }, + { + .name = "CXCMD_xWriteISOMagData", + .patch = hook_CXCMD_xWriteISOMagData, + .ordinal = 39, + }, + { + .name = "CXCMD_ICControl", + .patch = hook_CXCMD_ICControl, + .ordinal = 5, + }, + { + .name = "CXCMD_ImageOut", + .patch = hook_CXCMD_ImageOut, + .ordinal = 6, + }, + { + .name = "CXCMD_ReadBuffer", + .patch = hook_CXCMD_ReadBuffer, + .ordinal = 15, + }, + { + .name = "CXCMD_xReadMagData", + .patch = hook_CXCMD_xReadMagData, + .ordinal = 38, + }, + { + .name = "CXCMD_WriteBuffer", + .patch = hook_CXCMD_WriteBuffer, + .ordinal = 32, + }, + { + .name = "CXCMD_DefineLUT", + .patch = hook_CXCMD_DefineLUT, + .ordinal = 2, + }, + { + .name = "CXCMD_ReadISO3TrackMagData", + .patch = hook_CXCMD_ReadISO3TrackMagData, + .ordinal = 16, + }, + { + .name = "CXCMD_TestUnitReady", + .patch = hook_CXCMD_TestUnitReady, + .ordinal = 30, + }, + { + .name = "CXCMD_RetransferAndEject", + .patch = hook_CXCMD_RetransferAndEject, + .ordinal = 20, + }, +}; + +static const struct hook_symbol hook_lut_syms[] = { + { + .name = "Lut24_Exchange", + .patch = hook_Lut24_Exchange, + .ordinal = 1, + }, +}; + +static const struct hook_symbol hook_wdata_syms[] = { + { + .name = "Wdata_create", + .patch = hook_Wdata_create, + .ordinal = 1, + }, +}; +#pragma endregion + +static void write_int(uint8_t* data, int index, int value) { + data[index] = value >> 24; + data[index + 1] = value >> 16; + data[index + 2] = value >> 8; + data[index + 3] = value; +} + +static void write_short(uint8_t* data, int index, short value) { + data[index] = value >> 8; + data[index + 1] = value; +} + +static struct printer_cx_config printer_config; +static wchar_t printer_out_path[MAX_PATH]; +static struct printer_cx_data printer_data; + +#define HEIGHT 664 +#define WIDTH 1036 +#define IMAGE_BUFFER_SIZE WIDTH * HEIGHT +static uint8_t* back_buffer = NULL; +static uint8_t* front_buffer = NULL; +static uint64_t current_card_id; + +DWORD load_printer_data() { + DWORD bytesRead = 0; + HANDLE hSave = CreateFileW(printer_config.printer_data_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (hSave != INVALID_HANDLE_VALUE) { + if (!ReadFile(hSave, &printer_data, sizeof(printer_data), &bytesRead, NULL)){ + CloseHandle(hSave); + return GetLastError(); + } + CloseHandle(hSave); + if (bytesRead != sizeof(printer_data)){ + return -1; + } + if (printer_data.version != PRINTER_DATA_VERSION) { + return -2; + } + return 0; + } else { + return GetLastError(); + } +} + +DWORD save_printer_data() { + DWORD bytesWritten = 0; + HANDLE hSave = CreateFileW(printer_config.printer_data_path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (hSave != NULL) { + if (!WriteFile(hSave, &printer_data, sizeof(printer_data), &bytesWritten, NULL)){ + CloseHandle(hSave); + dprintf("CX7000: Failed writing data: %lx\n", GetLastError()); + return GetLastError(); + } + CloseHandle(hSave); + return 0; + } else { + dprintf("CX7000: Failed opening data file for writing: %lx\n", GetLastError()); + return GetLastError(); + } +} + +void printer_cx_hook_init(const struct printer_cx_config* cfg, HINSTANCE self) { + assert(cfg != NULL); + + if (!cfg->enable) { + return; + } + + memcpy(&printer_config, cfg, sizeof(*cfg)); + printer_cx_hook_insert_hooks(NULL); + + CreateDirectoryW(cfg->printer_out_path, NULL); + memcpy(printer_out_path, cfg->printer_out_path, MAX_PATH); + + if (load_printer_data() != 0) { + memset(&printer_data, 0, sizeof(printer_data)); + printer_data.version = PRINTER_DATA_VERSION; + if (save_printer_data() == 0) { + dprintf("CX7000: Printer data initialized.\n"); + } + } + + dprintf("CX7000: hook enabled.\n"); +} + +void printer_cx_hook_insert_hooks(HMODULE target) { + hook_table_apply(target, "PCP21CT64.dll", hook_pcp_syms, _countof(hook_pcp_syms)); + hook_table_apply(target, "LUT24EXG64.dll", hook_lut_syms, _countof(hook_lut_syms)); + hook_table_apply(target, "WCREATE64.dll", hook_wdata_syms, _countof(hook_wdata_syms)); + + /* Unity workaround */ + proc_addr_table_push(target, "PCP21CT64.dll", hook_pcp_syms, _countof(hook_pcp_syms)); + proc_addr_table_push(target, "LUT24EXG64.dll", hook_lut_syms, _countof(hook_lut_syms)); + proc_addr_table_push(target, "WCREATE64.dll", hook_wdata_syms, _countof(hook_wdata_syms)); +} + +static int WINAPI hook_CXCMD_Retransfer() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static bool WINAPI hook_CXCMD_CheckIfConnected(int* pSlotId, int* pId) { + dprintf("CX7000: %s\n", __func__); + return printer_config.enable; +} + +static int WINAPI hook_CXCMD_xImageOut() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_MoveCard(int slotId, int id, int dest, int flip, int filmInit, int immed) { + dprintf("CX7000: %s(%d, %d, %d, %d)\n", __func__, dest, flip, filmInit, immed); + return CX_OK; +} + +static int WINAPI hook_CXCMD_xWriteMagData() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_SecurityPrint(int slotId, int id, int color, int bufferIndex, int immed) { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_ScanPrinterNext() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_ImageOut(int slotId, int id, uint8_t* plane, int length, int color, int bufferIndex) { + dprintf("CX7000: %s\n", __func__); + + assert(color >= 0 && color <= 3); + assert(bufferIndex >= 0 && bufferIndex <= 1); + assert(length == IMAGE_BUFFER_SIZE); + + // colorIndex: 0 = w, 1 = c, 2 = m, 3 = y + // bufferIndex: 0 = back, 1 = front + + if (bufferIndex == 0) { + if (back_buffer == NULL) { + back_buffer = (uint8_t*) malloc(4 * IMAGE_BUFFER_SIZE); + } + memcpy(back_buffer + color * IMAGE_BUFFER_SIZE, plane, length); + } else { + if (front_buffer == NULL) { + front_buffer = (uint8_t*) malloc(4 * IMAGE_BUFFER_SIZE); + } + memcpy(front_buffer + color * IMAGE_BUFFER_SIZE, plane, length); + } + + return CX_OK; +} + +static int WINAPI hook_CXCMD_Print(int slotId, int id, int color, int bufferIndex, int immed) { + dprintf("CX7000: %s(%d, %d, %d)\n", __func__, color, bufferIndex, immed); + + assert(bufferIndex >= 0 && bufferIndex <= 1); + + SYSTEMTIME t; + GetLocalTime(&t); + + // color: 1 = back, 3 = front + // bufferIndex: 0 = back, 1 = front + + wchar_t dumpPath[MAX_PATH]; + uint8_t metadata[5]; + + metadata[0] = current_card_id >> 32; + write_int(metadata, 1, (int32_t)current_card_id); + + swprintf_s( + dumpPath, MAX_PATH, + L"%s\\CX7000_%04d%02d%02d_%02d%02d%02d_%s.bmp", + printer_out_path, t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond, bufferIndex == 0 ? L"back" : L"front"); + + // convert image from seperate CMY arrays to one RGB array + int size = IMAGE_BUFFER_SIZE * 3; + uint8_t* raw_image = (uint8_t*) malloc(size); + for (int i = 0; i < IMAGE_BUFFER_SIZE; i++) { + // 0 is "white" and we don't really care about that + raw_image[i * 3] = 0xFF - *((bufferIndex == 0 ? back_buffer : front_buffer) + IMAGE_BUFFER_SIZE + i); + raw_image[i * 3 + 1] = 0xFF - *((bufferIndex == 0 ? back_buffer : front_buffer) + 2 * IMAGE_BUFFER_SIZE + i); + raw_image[i * 3 + 2] = 0xFF - *((bufferIndex == 0 ? back_buffer : front_buffer) + 3 * IMAGE_BUFFER_SIZE + i); + } + + dprintf("CX7000: Saving %s image to %ls\n", bufferIndex == 0 ? "back" : "front", dumpPath); + + int ret = WriteDataToBitmapFile(dumpPath, 24, WIDTH, HEIGHT, raw_image, size, metadata, 5, false); + + free(raw_image); + if (bufferIndex == 0) { + free(back_buffer); + back_buffer = NULL; + } else { + free(front_buffer); + front_buffer = NULL; + } + + if (ret < 0) { + dprintf("CX7000: WriteDataToBitmapFile returned %d\n", ret); + return CX_ERROR_FATAL_3301; + } + + return CX_OK; +} + +static int WINAPI hook_CXCMD_RezeroUnit(int slotId, int id, int action) { + dprintf("CX7000: %s\n", __func__); + return CX_OK; +} + +static int WINAPI hook_CXCMD_WriteMagData() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_LogSense(int iSlot, int iID, int iPage, uint8_t* pbyBuffer) { + dprintf("CX7000: %s\n", __func__); + + const int size = 108; + memset(pbyBuffer, 0, size); + + + if (iPage == 56) { + dprintf("CX7000: MediumQuantity\n"); + + write_int(pbyBuffer, 8, (int)printer_data.print_counter); // total count + write_int(pbyBuffer, 16, 22); // free count + write_int(pbyBuffer, 24, 33); // head count + write_int(pbyBuffer, 32, (int)printer_data.print_counter_since_clean); // cleaning count + write_int(pbyBuffer, 40, 55); // error count + write_int(pbyBuffer, 48, 66); // cru cleaning count + + return CX_OK; + } else if (iPage == 57) { + dprintf("CX7000: Miscellaneous\n"); + + write_int(pbyBuffer, 16, 234); // re transfer hr power on time + write_int(pbyBuffer, 24, 456); // remedy hr power on time + write_int(pbyBuffer, 40, 789); // unresettable re transfer hr power on time + write_int(pbyBuffer, 48, 1023); // unresettable remedy hr power on time + + return CX_OK; + } + + dprintf("CX7000: Unknown LogSense\n"); + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_StandardInquiry(int iSlot, int iID, uint8_t* pbyBuffer) { + const int size = 96; + dprintf("CX7000: %s\n", __func__); + + memset(pbyBuffer, 0, size); + memcpy(pbyBuffer + 32, printer_config.printer_firm_version, 8); + memcpy(pbyBuffer + 50, printer_config.printer_camera_version, 8); + memcpy(pbyBuffer + 71, printer_config.printer_config_version, 8); + memcpy(pbyBuffer + 79, printer_config.printer_table_version, 8); + //memcpy(pbyBuffer + 58, printer_config.thermal_head_info, 13); // unused + + return CX_OK; +} + +static int WINAPI hook_CXCMD_ModeSense(int iSlot, int iID, int iPC, int iPage, uint8_t* pbyBuffer) { + dprintf("CX7000: %s(%d, %d)\n", __func__, iPC, iPage); + + const int size = 104; + memset(pbyBuffer, 0, size); + + if (iPC == 1 && iPage == 40) { // GetMediaInfo + pbyBuffer[51] = 10; // film count (10=100%) + pbyBuffer[52] = 50; // ink count (50=100%) + return CX_OK; + } else if (iPC == 1 && iPage == 35) { // ReadInkInfo + pbyBuffer[6] = 0; // "b" + write_short(pbyBuffer, 8, 50); // Remain + return CX_OK; + } + + dprintf("CX7000: Unknown ModeSense\n"); + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_UpdateFirmware(int iSlot, int iID, char* pFile, int iDataID) { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // intentionally not implemented + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_ModeSelect(int iSlot, int iID, int iSp, int iPage, uint8_t* pbyData) { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_GetPrintingStatus() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_SendDiagnostic(int iSlot, int iID, int iTestMode, int iTestPatten, int iTestCount) { + dprintf("CX7000: %s(%d, %d, %d)\n", __func__, iTestMode, iTestPatten, iTestCount); + if (iTestMode == 19) { + dprintf("CX7000: Printer Front Buttons Enabled: %d\n", iTestPatten); + return CX_OK; + } else if (iTestMode == 17) { + dprintf("CX7000: Printer was cleaned (haha)\n"); + printer_data.print_counter_since_clean = 0; + save_printer_data(); + return CX_OK; + } else if (iTestMode == 18) { + dprintf("CX7000: Transport Mode enabled\n"); + printer_data.is_transport = true; + save_printer_data(); + return CX_OK; + } else if (iTestMode == 20) { + dprintf("CX7000: Transport Mode disabled\n"); + printer_data.is_transport = false; + save_printer_data(); + return CX_OK; + } + + dprintf("CX7000: Unknown SendDiagnostic\n"); + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_RetransferAndTurn(int slotId, int id, int immed) { + dprintf("CX7000: %s\n", __func__); + return CX_OK; +} + +static int WINAPI hook_CXCMD_LogSelect(int iSlot, int iID, int iMod) { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_ReadPosition(int slotId, int id, uint8_t* buffer) { + dprintf("CX7000: %s\n", __func__); + const int size = 8; + + memset(buffer, 0, size); + buffer[0] = 1 << 2; // IsExist (0 means YES!) + buffer[7] = 0; // position (of card; 0 = printer, 1 = "IR") + + return CX_OK; +} + +static int WINAPI hook_CXCMD_SecurityLock() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_SetPrintingStatus() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_xReadISOMagData() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_WriteISO3TrackMagData() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_PasswordSet() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_GetPrinterStatus() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_WriteProjectCode() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_LoadCard(int slotId, int id, int dest, int flip, int filmInit, int immed) { + dprintf("CX7000: %s(%d, %d, %d, %d)\n", __func__, dest, flip, filmInit, immed); + return CX_OK; +} + +static int WINAPI hook_CXCMD_ReadMagData() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_ScanPrinter(int* pSlotId, int* pId) { + dprintf("CX7000: %s\n", __func__); + + if (!printer_config.enable) { + return CX_ERROR_NOT_CONNECTED_6804; + } + + *pSlotId = 1; + *pId = 1; + + return CX_OK; +} + +static int WINAPI hook_CXCMD_xWriteISOMagData() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_ICControl() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_ReadBuffer(int iSlot, int iID, int iMode, int iBufferID, uint8_t* pbyData, int iOffset, + int iLength) { + dprintf("CX7000: %s(%d, %d, %d)\n", __func__, iMode, iBufferID, iLength); + + memset(pbyData, 0, iLength); + + if (iMode == 2 && iBufferID == 87 && iLength == 10) { + dprintf("CX7000: ReadCondition\n"); + + pbyData[0] = printer_data.is_transport; // transport mode + pbyData[1] = 0; // head white ink level, unused + pbyData[2] = 0; // main white ink level, unused + pbyData[3] = 0; // total white ink level, unused + + return CX_OK; + } else if (iMode == 2 && iBufferID == 112 && iLength == 6) { + dprintf("CX7000: GetMacAddress\n"); + + pbyData[0] = 0x12; // displays in test menu, else ununused? + pbyData[1] = 0x34; + pbyData[2] = 0x56; + pbyData[3] = 0x78; + pbyData[4] = 0x9A; + pbyData[5] = 0xBC; + + return CX_OK; + } else if (iMode == 2 && iBufferID == 82 && iLength == 4) { + dprintf("CX7000: GetCleaningWaitCount\n"); + + // seemingly unused + write_short(pbyData, 0, 0); + + return CX_OK; + } else if (iMode == 2 && iBufferID == 85 && iLength == 10) { + dprintf("CX7000: GetSensorInfo\n"); + + pbyData[0] = 244; // retransfer heat roller thermistor; must be over 243 + pbyData[1] = 0; // main pwb thermistor; unused + pbyData[2] = 0; // thermal head thermistor; unused + pbyData[3] = 0; // heater cover thermistor; unused + + return CX_OK; + } else if (iMode == 2 && iBufferID == 88 && iLength == 16) { + dprintf("CX7000: ReadErrorStatus\n"); + + pbyData[1] = 0; // is door open? + //pbyData[2...] = // any of the error codes that fit in a byte (from CX_ERROR_FATAL_3301 to CX_ERROR_PRINT_INTERRUPT_6805_3) + + return CX_OK; + } else if (iMode == 2 && iBufferID == 224 && iLength == 10) { + dprintf("CX7000: ReadCode\n"); + + printer_data.print_counter_since_clean++; + current_card_id = ++printer_data.print_counter; + dprintf("CX7000: Generated new card ID: %lld\n", current_card_id); + + save_printer_data(); + + pbyData[0] = current_card_id >> 32; // MSB of card id + write_int(pbyData, 1, (int32_t)current_card_id); // lower 4 bytes of card id + pbyData[5] = 0x0; // Direction (1 = rotate image by 180 degrees) + pbyData[6] = 0x1; // CheckCode (0 = error) + + return CX_OK; + } else if (iMode == 2 && iBufferID == 144 && iLength == 260) { + dprintf("CX7000: ReadErrorLog\n"); + + write_int(pbyData, 0, 0); // LogCount + /*for (int i = 0; false; i++) { // list of error ids + write_int(pbyData, i + 4, 0); + }*/ + + return CX_OK; + } + + dprintf("CX7000: Unknown ReadBuffer\n"); + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_xReadMagData() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_WriteBuffer(int iSlot, int iID, int iMode, int iBufferID, uint8_t* pbyData, int iOffset, // NOLINT(*-non-const-parameter) + int iLength) { + dprintf("CX7000: %s(%d, %d, %d, %d)\n", __func__, iMode, iBufferID, iOffset, iLength); + if (iMode == 2 && iBufferID == 82 && iLength == 4) { + int val = pbyData[0] << 8 | pbyData[1]; + dprintf("CX7000: Set cleaning limit: %d\n", val); + + return CX_OK; + } + + dprintf("CX7000: Unknown WriteBuffer\n"); + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_DefineLUT(int slotId, int id, int color, int length, uint8_t* buffer) { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_ReadISO3TrackMagData() { + dprintf("CX7000: %s\n", __func__); + dprintf("CX7000: Unimplemented\n"); // unused + return CX_ERROR_USB_COM_3201; +} + +static int WINAPI hook_CXCMD_TestUnitReady(int slotId, int id) { + dprintf("CX7000: %s\n", __func__); + + if (!printer_config.enable) { + return CX_ERROR_NOT_CONNECTED_6804; + } + + return CX_OK; +} + +static int WINAPI hook_CXCMD_RetransferAndEject(int slotId, int id, int immed) { + dprintf("CX7000: %s\n", __func__); + return CX_OK; +} + +static bool WINAPI hook_Lut24_Exchange(const wchar_t* pFile, uint8_t* y, uint8_t* m, uint8_t* c, int sizeY, int sizeM, + int sizeC) { + dprintf("CX7000: %s(%ls)\n", __func__, pFile); + + // stub? + + return true; +} + +static bool WINAPI hook_Wdata_create(uint8_t* pbyRdata, uint8_t* pbyGdata, uint8_t* pbyBdata, int iWidth, + int iHeight, bool bPortrait, int iAlgorithim, uint8_t* pbyWdata) { + dprintf("CX7000: %s(%d, %d, %d)\n", __func__, iHeight, bPortrait, iAlgorithim); + + // stub? + + return true; +} \ No newline at end of file diff --git a/common/hooklib/printer_cx.h b/common/hooklib/printer_cx.h new file mode 100644 index 0000000..d86773c --- /dev/null +++ b/common/hooklib/printer_cx.h @@ -0,0 +1,73 @@ +#pragma once +#include +#include +#include + +#define PRINTER_DATA_VERSION 1 + +struct printer_cx_config { + bool enable; + wchar_t printer_out_path[MAX_PATH]; + wchar_t printer_data_path[MAX_PATH]; + char printer_firm_version[8]; + char printer_camera_version[8]; + char printer_config_version[8]; + char printer_table_version[8]; +}; + +struct printer_cx_data { + uint8_t version; + uint64_t print_counter; + uint64_t print_counter_since_clean; + bool is_transport; +}; + +enum { + CX_OK = 0, + CX_ERROR_FATAL_3301 = -1, + CX_ERROR_USB_COM_3201 = -2, + CX_ERROR_PRINT_INTERRUPT_6805_4 = -4, + CX_ERROR_CODE_UNREADABLE_3303 = -5, + CX_ERROR_INK_LOW_3202 = -6, + CX_ERROR_PRINT_INTERRUPT_6805_3 = -7, + CX_ERROR_NO_CARD_6801 = -16961536, + CX_ERROR_DOOR_OPEN_6808 = -16961792, + CX_ERROR_6831 = -16963328, + CX_ERROR_6810 = -16964864, + CX_ERROR_CLEAN_PRINTER_3999 = -16973056, + CX_ERROR_JAM_6805_1 = -17010688, + CX_ERROR_REVERSE_JAM_6805_2 = -17010944, + CX_ERROR_CAMERA_JAM_6805_3 = -17011200, + CX_ERROR_TRANSPORT_JAM_6805_4 = -17011456, + CX_ERROR_PAPER_SENSOR_JAM_6805_5 = -17011712, + CX_ERROR_RETRANSFER_JAM_6805_6 = -17011968, + CX_ERROR_PAPER_RIPPED_6813_3 = -17015040, + CX_ERROR_CODE_READ_6811 = -17018112, + CX_ERROR_UNAUTHORIZED_INK_6803_1 = -17018880, + CX_ERROR_INK_EMPTY_6813_1 = -17019136, + CX_ERROR_PRINT_TIMEOUT_6810_3 = -17056768, + CX_ERROR_CAMERA_HARDWARE_FAULT_6810_5 = -17083136, + CX_ERROR_CAMERA_COM_6810_6 = -17083392, + CX_ERROR_ROLLER_6810_15 = -17088768, + CX_ERROR_OVER_TEMPERATURE_6810_16 = -17089024, + CX_ERROR_POWER_INTERRUPT_6810_1 = -17089280, + CX_ERROR_INITIALIZATION_6810_2 = -17094656, + CX_ERROR_OVER_TEMPERATURE_6810_10 = -17100800, + CX_ERROR_RETRANSFER_ROLLER_6810_11 = -17101056, + CX_ERROR_THERMOSTAT_6810_12 = -17101312, + CX_ERROR_OVER_TEMPERATURE_6810_20 = -17101568, + CX_ERROR_STRAIGHTEN_ROLLER_6810_21 = -17101824, + CX_ERROR_THERMOSTAT_6810_22 = -17102080, + CX_ERROR_PRINTER_TOO_COLD_6833_1 = -16971264, + CX_ERROR_OVER_TEMPERATURE_6810_25 = -17102848, + CX_ERROR_CAMERA_NOT_FOUND_6810_7 = -17116672, + CX_ERROR_RETRANSFER_ROLL_EMPTY_6802_3 = -21144064, + CX_ERROR_INK_ROLL_EMPTY_6802_1 = -21148160, + CX_ERROR_NOT_CONNECTED_6804 = -33554432, + CX_ERROR_CAMERA_JAM_6805_7 = -17012224, + CX_ERROR_INVALID_CLEANING_CARD_6832_1 = -16965376, + CX_ERROR_TEMPERATURE_RESOLVED_6833_2 = -16971520, +}; + +void printer_cx_hook_init(const struct printer_cx_config *cfg, HINSTANCE self); +void printer_cx_hook_insert_hooks(HMODULE target); \ No newline at end of file diff --git a/common/hooklib/y3-dll.c b/common/hooklib/y3-dll.c new file mode 100644 index 0000000..e3f112f --- /dev/null +++ b/common/hooklib/y3-dll.c @@ -0,0 +1,109 @@ +#include + +#include +#include + +#include "hooklib/config.h" + +#include "util/dll-bind.h" +#include "util/dprintf.h" + +#include "y3.h" +#include "y3-dll.h" + +const struct dll_bind_sym y3_dll_syms[] = { + { + .sym = "y3_io_init", + .off = offsetof(struct y3_dll, init), + }, { + .sym = "y3_io_get_cards", + .off = offsetof(struct y3_dll, get_cards), + }, { + .sym = "y3_io_close", + .off = offsetof(struct y3_dll, close), + } +}; + +struct y3_dll y3_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 y3_dll_init(const struct y3_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("Y3: Failed to load IO DLL: %lx: %S\n", + hr, + cfg->path); + + goto end; + } + + dprintf("Y3: Using custom IO DLL: %S\n", cfg->path); + src = owned; + } else { + owned = NULL; + src = self; + } + + get_api_version = (void *) GetProcAddress(src, "y3_io_get_api_version"); + + if (get_api_version != NULL) { + y3_dll.api_version = get_api_version(); + } else { + y3_dll.api_version = 0x0100; + dprintf("Custom IO DLL does not expose y3_io_get_api_version, " + "assuming API version 1.0.\n" + "Please ask the developer to update their DLL.\n"); + } + + if (y3_dll.api_version >= 0x0200) { + hr = E_NOTIMPL; + dprintf("Y3: Custom IO DLL implements an unsupported " + "API version (%#04x). Please update Segatools.\n", + y3_dll.api_version); + + goto end; + } + + sym = y3_dll_syms; + hr = dll_bind(&y3_dll, src, &sym, _countof(y3_dll_syms)); + + if (FAILED(hr)) { + if (src != self) { + dprintf("Y3: 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; +} diff --git a/common/hooklib/y3-dll.h b/common/hooklib/y3-dll.h new file mode 100644 index 0000000..5b2eec8 --- /dev/null +++ b/common/hooklib/y3-dll.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include +#include + +#include "hooklib/y3.h" + + +struct y3_dll { + uint16_t api_version; + HRESULT (*init)(void); + HRESULT (*close)(void); + HRESULT (*get_cards)(struct CardInfo* cards, int* len); +}; + +extern struct y3_dll y3_dll; + +HRESULT y3_dll_init(const struct y3_dll_config *cfg, HINSTANCE self); diff --git a/common/hooklib/y3.c b/common/hooklib/y3.c new file mode 100644 index 0000000..833d58c --- /dev/null +++ b/common/hooklib/y3.c @@ -0,0 +1,625 @@ +// ReSharper disable CppParameterNeverUsed +#include + +#include +#include +#include +#include + +#include "y3.h" + +#include "hook/table.h" +#include "hook/procaddr.h" + +#include "hooklib/y3-dll.h" + +#include "util/dprintf.h" + +#if _WIN32 || _WIN64 + #if _WIN64 + #define ENV64BIT + #else + #define ENV32BIT + #endif +#endif + +// Check GCC +#if __GNUC__ + #if __x86_64__ || __ppc64__ + #define ENV64BIT + #else + #define ENV32BIT + #endif +#endif + +#ifdef ENV64BIT + #define CALL +#else + #define CALL __cdecl +#endif + +float CALL API_DLLVersion(); +uint32_t CALL API_GetLastError(int* hDevice); +uint32_t CALL API_GetErrorMessage(uint32_t errNo, char* szMessage, int numBytes); +int* CALL API_Connect(char* szPortName); +int CALL API_Close(int* hDevice); +int CALL API_Start(int* hDevice); +int CALL API_Stop(int* hDevice); +float CALL API_GetFirmVersion(int* hDevice); +uint32_t CALL API_GetFirmName(int* hDevice); +uint32_t CALL API_GetTargetCode(int* hDevice); +uint32_t CALL API_GetStatus(int* hDevice); +uint32_t CALL API_GetCounter(int* hDevice); +int CALL API_ClearError(int* hDevice); +int CALL API_Reset(int* hDevice, bool isHardReset); +int CALL API_GetCardInfo(int* hDevice, int numCards, struct CardInfo* pCardInfo); +int CALL API_GetCardInfoCharSize(); +int CALL API_FirmwareUpdate(int* hDevice, uint32_t address, uint32_t size, uint8_t* buffer); +int CALL API_Calibration(int* hDevice, int calib); +int CALL API_GetCalibrationResult(int* hDevice, int calib, uint32_t* result); +uint32_t CALL API_GetProcTime(int* hDevice); +uint32_t CALL API_GetMemStatus(int* hDevice); +uint32_t CALL API_GetMemCounter(int* hDevice); +int CALL API_SetParameter(int* hDevice, uint32_t uParam, uint32_t* pParam); +int CALL API_GetParameter(int* hDevice, uint32_t uParam, uint32_t* pParam); + +signed int CALL API_SetDevice(int a1, int a2); +signed int CALL API_SetCommand(int a1, int a2, int a3, int* a4); +signed int CALL API_SetSysControl(int a1, int a2, int* a3); +signed int CALL API_GetSysControl(int a1, int a2, int* a3); +int CALL API_TestReset(int a1); +signed int API_DebugReset(int a1, ...); +int CALL API_GetBoardType(int a1); +int CALL API_GetCardDataSize(int a1); +int CALL API_GetFirmDate(int a1); +int API_SystemCommand(int a1, char a2, ...); +int CALL API_CalcCheckSum(DWORD* a1, int a2, int a3); +int CALL API_GetCheckSumResult(int a1); +int CALL API_BlockRead(int a1, int a2, int a3, SIZE_T dwBytes); +int CALL API_GetBlockReadResult(int a1, void* a2); +int CALL API_BlockWrite(int a1, int a2, int a3, SIZE_T dwBytes, void* a5); +signed int CALL API_GetDebugParam(int a1, int a2, DWORD* a3); + +uint32_t convert_string_to_uint(const char* firmName); + +static const struct hook_symbol Y3_hooks[] = { + { + .name = "API_DLLVersion", + .patch = API_DLLVersion, + .link = NULL + }, + { + .name = "API_GetLastError", + .patch = API_GetLastError, + .link = NULL + }, + { + .name = "API_GetErrorMessage", + .patch = API_GetErrorMessage, + .link = NULL + }, + { + .name = "API_Connect", + .patch = API_Connect, + .link = NULL + }, + { + .name = "API_Close", + .patch = API_Close, + .link = NULL + }, + { + .name = "API_Start", + .patch = API_Start, + .link = NULL + }, + { + .name = "API_Stop", + .patch = API_Stop, + .link = NULL + }, + { + .name = "API_GetFirmVersion", + .patch = API_GetFirmVersion, + .link = NULL + }, + { + .name = "API_GetFirmName", + .patch = API_GetFirmName, + .link = NULL + }, + { + .name = "API_GetTargetCode", + .patch = API_GetTargetCode, + .link = NULL + }, + { + .name = "API_GetStatus", + .patch = API_GetStatus, + .link = NULL + }, + { + .name = "API_GetCounter", + .patch = API_GetCounter, + .link = NULL + }, + { + .name = "API_Reset", + .patch = API_Reset, + .link = NULL + }, + { + .name = "API_GetCardInfo", + .patch = API_GetCardInfo, + .link = NULL + }, + { + .name = "API_GetCardInfoCharSize", + .patch = API_GetCardInfoCharSize, + .link = NULL + }, + { + .name = "API_FirmwareUpdate", + .patch = API_FirmwareUpdate, + .link = NULL + }, + { + .name = "API_Calibration", + .patch = API_Calibration, + .link = NULL + }, + { + .name = "API_GetCalibrationResult", + .patch = API_GetCalibrationResult, + .link = NULL + }, + { + .name = "API_GetProcTime", + .patch = API_GetProcTime, + .link = NULL + }, + { + .name = "API_GetMemStatus", + .patch = API_GetMemStatus, + .link = NULL + }, + { + .name = "API_GetMemCounter", + .patch = API_GetMemCounter, + .link = NULL + }, + { + .name = "API_SetParameter", + .patch = API_SetParameter, + .link = NULL + }, + { + .name = "API_GetParameter", + .patch = API_GetParameter, + .link = NULL + }, + { + .name = "API_SetDevice", + .patch = API_SetDevice, + .link = NULL + }, + { + .name = "API_SetCommand", + .patch = API_SetCommand, + .link = NULL + }, + { + .name = "API_SetSysControl", + .patch = API_SetSysControl, + .link = NULL + }, + { + .name = "API_GetSysControl", + .patch = API_GetSysControl, + .link = NULL + }, + { + .name = "API_TestReset", + .patch = API_TestReset, + .link = NULL + }, + { + .name = "API_DebugReset", + .patch = API_DebugReset, + .link = NULL + }, + { + .name = "API_GetBoardType", + .patch = API_GetBoardType, + .link = NULL + }, + { + .name = "API_GetCardDataSize", + .patch = API_GetCardDataSize, + .link = NULL + }, + { + .name = "API_GetFirmDate", + .patch = API_GetFirmDate, + .link = NULL + }, + { + .name = "API_SystemCommand", + .patch = API_SystemCommand, + .link = NULL + }, + { + .name = "API_CalcCheckSum", + .patch = API_CalcCheckSum, + .link = NULL + }, + { + .name = "API_GetCheckSumResult", + .patch = API_GetCheckSumResult, + .link = NULL + }, + { + .name = "API_BlockRead", + .patch = API_BlockRead, + .link = NULL + }, + { + .name = "API_GetBlockReadResult", + .patch = API_GetBlockReadResult, + .link = NULL + }, + { + .name = "API_BlockWrite", + .patch = API_BlockWrite, + .link = NULL + }, + { + .name = "API_GetDebugParam", + .patch = API_GetDebugParam, + .link = NULL + }, +}; + +static struct y3_config y3_config; + +#define MAX_CARD_SIZE 32 +static struct CardInfo card_data[MAX_CARD_SIZE]; + +static int* Y3_COM_FIELD = (int*)10; +static int* Y3_COM_PRINT = (int*)11; + +HRESULT y3_hook_init(const struct y3_config* cfg, HINSTANCE self, const wchar_t* config_filename) { + HRESULT hr; + assert(cfg != NULL); + + if (!cfg->enable) { + return S_FALSE; + } + + memcpy(&y3_config, cfg, sizeof(*cfg)); + Y3_COM_FIELD = (int*)(uintptr_t)cfg->port_field; + Y3_COM_PRINT = (int*)(uintptr_t)cfg->port_printer; + + y3_insert_hooks(NULL); + + memset(card_data, 0, sizeof(card_data)); + + struct y3_dll_config config; + y3_dll_config_load(&config, config_filename); + + hr = y3_dll_init(&config, self); + if (FAILED(hr)) { + return hr; + } + + dprintf("Y3: hook enabled (field port = %d, printer port = %d)\n", cfg->port_field, cfg->port_printer); + + return hr; +} + +void y3_insert_hooks(HMODULE target) { + hook_table_apply( + target, + "Y3CodeReaderNE.dll", + Y3_hooks, + _countof(Y3_hooks)); + + proc_addr_table_push( + target, + "Y3CodeReaderNE.dll", + Y3_hooks, + _countof(Y3_hooks)); +} + +float CALL API_DLLVersion() { + dprintf("Y3: %s\n", __func__); + return 1; +} + +uint32_t CALL API_GetLastError(int* hDevice) { + dprintf("Y3: %s\n", __func__); + if (!y3_config.enable) { + return 1; + } + return 0; +} + +uint32_t CALL API_GetErrorMessage(uint32_t errNo, char* szMessage, + int numBytes) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int* CALL API_Connect(char* szPortName) { + HRESULT hr; + + dprintf("Y3: %s(%s)\n", __func__, szPortName); + + if (!y3_config.enable) { + return NULL; + } + + char number[2]; + strncpy(number, szPortName + 3, 2); + int* hDevice = (int*)(uintptr_t)atoi(number); + + if (hDevice == Y3_COM_FIELD) { + hr = y3_dll.init(); + if (FAILED(hr)) { + dprintf("Y3: Hook DLL initialization failed: %lx\n", hr); + return NULL; + } + } + + return hDevice; +} + +int CALL API_Close(int* hDevice) { + dprintf("Y3: %s(%p)\n", __func__, hDevice); + + if (hDevice == Y3_COM_FIELD) { + y3_dll.close(); + } + + return 0; +} + +int CALL API_Start(int* hDevice) { + dprintf("Y3: %s(%p)\n", __func__, hDevice); + return 0; +} + +int CALL API_Stop(int* hDevice) { + dprintf("Y3: %s(%p)\n", __func__, hDevice); + return 0; +} + +float CALL API_GetFirmVersion(int* hDevice) { + dprintf("Y3: %s(%p)\n", __func__, hDevice); + return 1; +} + +uint32_t CALL API_GetFirmName(int* hDevice) { + uint32_t result = 0; + dprintf("Y3: %s(%p)\n", __func__, hDevice); + + if (hDevice == Y3_COM_FIELD) { + result = convert_string_to_uint(y3_config.firm_name_field); + dprintf("Y3: This device is a FIELD: %s\n", y3_config.firm_name_field); + } else if (hDevice == Y3_COM_PRINT) { + result = convert_string_to_uint(y3_config.firm_name_printer); + dprintf("Y3: This device is a PRINTER: %s\n", y3_config.firm_name_printer); + } else { + dprintf("Y3: This device is UNKNOWN\n"); + } + + return result; +} + +uint32_t CALL API_GetTargetCode(int* hDevice) { + uint32_t result = 1162760014; + dprintf("Y3: %s(%p)\n", __func__, hDevice); + + if (hDevice == Y3_COM_FIELD) { + result = convert_string_to_uint(y3_config.target_code_field); + dprintf("Y3: This device is a FIELD: %s\n", y3_config.target_code_field); + } else if (hDevice == Y3_COM_PRINT) { + result = convert_string_to_uint(y3_config.target_code_printer); + dprintf("Y3: This device is a PRINTER: %s\n", y3_config.target_code_printer); + } else { + dprintf("Y3: This Y3 device is UNKNOWN\n"); + } + + return result; +} + +uint32_t CALL API_GetStatus(int* hDevice) { + // dprintf("Y3: %s\n", __func__); + return 0; +} + +uint32_t CALL API_GetCounter(int* hDevice) { + // dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_ClearError(int* hDevice) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_Reset(int* hDevice, bool isHardReset) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_GetCardInfo(int* hDevice, int numCards, struct CardInfo* pCardInfo) { + // dprintf("Y3: %s(%p), %d\n", __func__, hDevice, numCards); + // ret = num cards + // numCards = max cards + + if (hDevice == Y3_COM_FIELD) { + + int cards = numCards; + HRESULT hr = y3_dll.get_cards(pCardInfo, &cards); + if (FAILED(hr)) { + dprintf("Y3: DLL returned error when retrieving cards: %lx\n", hr); + return 0; + } + + return cards; + } else if (hDevice == Y3_COM_PRINT) { + pCardInfo[0].fX = 0; + pCardInfo[0].fY = 0; + pCardInfo[0].fAngle = 0; + pCardInfo[0].eCardType = TYPE0; + pCardInfo[0].eCardStatus = MARKER; + pCardInfo[0].uID = 0x10; + pCardInfo[0].nNumChars = 0; + pCardInfo[0].ubChar0.Data = 0; + pCardInfo[0].ubChar1.Data = 0x4000; + pCardInfo[0].ubChar2.Data = 0; + pCardInfo[0].ubChar3.Data = 0x0; // 40 + pCardInfo[0].ubChar4.Data = 0; + pCardInfo[0].ubChar5.Data = 0; + return 1; + } + + return 0; +} + +int CALL API_GetCardInfoCharSize() { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_FirmwareUpdate(int* hDevice, uint32_t address, uint32_t size, + uint8_t* buffer) { + dprintf("Y3: %s\n", __func__); + return 1; // not supported +} + +int CALL API_Calibration(int* hDevice, int calib) { + dprintf("Y3: %s\n", __func__); + return 1; +} + +int CALL API_GetCalibrationResult(int* hDevice, int calib, uint32_t* result) { + dprintf("Y3: %s\n", __func__); + return 1; +} + +uint32_t CALL API_GetProcTime(int* hDevice) { + // dprintf("Y3: %s\n", __func__); + return 0; +} + +uint32_t CALL API_GetMemStatus(int* hDevice) { + dprintf("Y3: %s\n", __func__); + return 0; +} +uint32_t CALL API_GetMemCounter(int* hDevice) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_SetParameter(int* hDevice, uint32_t uParam, uint32_t* pParam) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_GetParameter(int* hDevice, uint32_t uParam, uint32_t* pParam) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +signed int CALL API_SetDevice(int a1, int a2) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +signed int CALL API_SetCommand(int a1, int a2, int a3, int* a4) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +signed int CALL API_SetSysControl(int a1, int a2, int* a3) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +signed int CALL API_GetSysControl(int a1, int a2, int* a3) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_TestReset(int a1) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +signed int API_DebugReset(int a1, ...) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_GetBoardType(int a1) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_GetCardDataSize(int a1) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_GetFirmDate(int a1) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int API_SystemCommand(int a1, char a2, ...) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_CalcCheckSum(DWORD* a1, int a2, int a3) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_GetCheckSumResult(int a1) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_BlockRead(int a1, int a2, int a3, SIZE_T dwBytes) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_GetBlockReadResult(int a1, void* a2) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +int CALL API_BlockWrite(int a1, int a2, int a3, SIZE_T dwBytes, void* a5) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +signed int CALL API_GetDebugParam(int a1, int a2, DWORD* a3) { + dprintf("Y3: %s\n", __func__); + return 0; +} + +uint32_t convert_string_to_uint(const char* firmName) { + uint32_t result = 0; + + // Iterate over each character in the string and construct the uint32_t + for (int i = 0; i < 4; i++) { + result |= (uint32_t)firmName[i] << (i * 8); + } + + return result; +} diff --git a/common/hooklib/y3.h b/common/hooklib/y3.h new file mode 100644 index 0000000..a42841b --- /dev/null +++ b/common/hooklib/y3.h @@ -0,0 +1,71 @@ +#pragma once + +#include +#include + +#include "hooklib/config.h" + +#pragma pack(push, 1) + +// Value held on a card. +struct CardByteData { + unsigned int Data; +}; + +// Unused +enum CardType { + TYPE0 = 0, + TYPE1, + TYPE2, + TYPE3, + TYPE4, + TYPE5, + TYPE6, + TYPE7 = 7 +}; + +enum CardStatus { + // Unset entry + INVALID = 0, + // Valid card + VALID = 1, + // Not a card but rather infrared interference. Only relevant in test mode. + INFERENCE = 2, + // This is only used by the printer camera. + MARKER = 3 +}; + +struct CardInfo { + // X position of the card. + float fX; // 0x00|0 + // Y position of the card. + float fY; // 0x04|4 + // Rotation of the card in degrees >=0.0 && <360.0 + float fAngle; // 0x08|8 + // Unused + enum CardType eCardType; // 0x0C|12 + // see enum CardStatus + enum CardStatus eCardStatus; // 0x10|16 + // card's BaseCode. used for a reference to the card being tracked as well as part of the IvCode. + unsigned int uID; // 0x14|20 + // Unused + int nNumChars; // 0x18|24 + // Title Code. Is 8589934592 for EKT. + struct CardByteData ubChar0; // 0x1C|28 + // Must be 0x4000 for the printer camera. + struct CardByteData ubChar1; // 0x20|32 + // Unused + struct CardByteData ubChar2; // 0x24|36 + // Must be 0x0 for the printer camera. + struct CardByteData ubChar3; // 0x28|40 + // Unused + struct CardByteData ubChar4; // 0x2C|44 + // Unused + struct CardByteData ubChar5; // 0x30|48 +}; + +#pragma pack(pop) + +HRESULT y3_hook_init(const struct y3_config *cfg, HINSTANCE self, const wchar_t* config_path); + +void y3_insert_hooks(HMODULE target); diff --git a/common/platform/amvideo.c b/common/platform/amvideo.c index 001a52a..e0c51ab 100644 --- a/common/platform/amvideo.c +++ b/common/platform/amvideo.c @@ -18,6 +18,7 @@ static HRESULT amvideo_reg_read_name(void *bytes, uint32_t *nbytes); static HRESULT amvideo_reg_read_port_X(void *bytes, uint32_t *nbytes); static HRESULT amvideo_reg_read_resolution_1(void *bytes, uint32_t *nbytes); +static HRESULT amvideo_reg_read_resolution_2(void *bytes, uint32_t *nbytes); static HRESULT amvideo_reg_read_setting(void *bytes, uint32_t *nbytes); static HRESULT amvideo_reg_read_use_segatiming(void *bytes, uint32_t *nbytes); @@ -81,6 +82,10 @@ static const struct reg_hook_val amvideo_reg_mode_vals[] = { .name = L"resolution_1", .read = amvideo_reg_read_resolution_1, .type = REG_SZ, + }, { + .name = L"resolution_2", + .read = amvideo_reg_read_resolution_2, + .type = REG_SZ, }, { .name = L"use_segatiming", .read = amvideo_reg_read_use_segatiming, @@ -171,6 +176,11 @@ static HRESULT amvideo_reg_read_resolution_1(void *bytes, uint32_t *nbytes) return reg_hook_read_wstr(bytes, nbytes, L"1920x1080"); } +static HRESULT amvideo_reg_read_resolution_2(void *bytes, uint32_t *nbytes) +{ + return reg_hook_read_wstr(bytes, nbytes, L"1920x1080"); +} + static HRESULT amvideo_reg_read_setting(void *bytes, uint32_t *nbytes) { return reg_hook_read_wstr(bytes, nbytes, L"0"); diff --git a/common/platform/config.c b/common/platform/config.c index d089977..e707126 100644 --- a/common/platform/config.c +++ b/common/platform/config.c @@ -12,6 +12,9 @@ #include "platform/amvideo.h" #include "platform/clock.h" #include "platform/config.h" + +#include + #include "platform/dns.h" #include "platform/epay.h" #include "platform/hwmon.h" @@ -23,6 +26,7 @@ #include "platform/platform.h" #include "platform/vfs.h" #include "platform/system.h" +#include "util/dprintf.h" #include "platform/openssl.h" #include "util/dprintf.h" @@ -32,6 +36,14 @@ void platform_config_load(struct platform_config *cfg, const wchar_t *filename) assert(cfg != NULL); assert(filename != NULL); + if (!PathFileExistsW(filename)) { + wchar_t temp[MAX_PATH]; + dprintf("ERROR: Configuration does not exist\n"); + dprintf(" Configured: \"%ls\"\n", filename); + GetFullPathNameW(filename, _countof(temp), temp, NULL); + dprintf(" Expanded: \"%ls\"\n", temp); + } + amvideo_config_load(&cfg->amvideo, filename); clock_config_load(&cfg->clock, filename); dns_config_load(&cfg->dns, filename); @@ -206,6 +218,7 @@ void nusec_config_load(struct nusec_config *cfg, const wchar_t *filename) wchar_t game_id[5]; wchar_t platform_id[5]; wchar_t subnet[16]; + wchar_t bcast[16]; unsigned int ip[4]; size_t i; @@ -217,6 +230,7 @@ void nusec_config_load(struct nusec_config *cfg, const wchar_t *filename) memset(game_id, 0, sizeof(game_id)); memset(platform_id, 0, sizeof(platform_id)); memset(subnet, 0, sizeof(subnet)); + memset(bcast, 0, sizeof(bcast)); cfg->enable = GetPrivateProfileIntW(L"keychip", L"enable", 1, filename); @@ -260,6 +274,14 @@ void nusec_config_load(struct nusec_config *cfg, const wchar_t *filename) _countof(subnet), filename); + GetPrivateProfileStringW( + L"netenv", + L"broadcast", + L"255.255.255.255", + bcast, + _countof(bcast), + filename); + for (i = 0 ; i < 16 ; i++) { cfg->keychip_id[i] = (char) keychip_id[i]; } @@ -275,6 +297,9 @@ void nusec_config_load(struct nusec_config *cfg, const wchar_t *filename) swscanf(subnet, L"%u.%u.%u.%u", &ip[0], &ip[1], &ip[2], &ip[3]); cfg->subnet = (ip[0] << 24) | (ip[1] << 16) | (ip[2] << 8) | 0; + swscanf(bcast, L"%u.%u.%u.%u", &ip[0], &ip[1], &ip[2], &ip[3]); + cfg->bcast = (ip[0] << 24) | (ip[1] << 16) | (ip[2] << 8) | (ip[3]); + GetPrivateProfileStringW( L"keychip", L"billingCa", diff --git a/common/platform/netenv.c b/common/platform/netenv.c index 4fb77d2..ecf2d7e 100644 --- a/common/platform/netenv.c +++ b/common/platform/netenv.c @@ -16,6 +16,8 @@ #include "hook/table.h" #include "platform/netenv.h" + +#include "hook/procaddr.h" #include "platform/nusec.h" #include "util/dprintf.h" @@ -72,6 +74,14 @@ static uint32_t WINAPI hook_IcmpSendEcho2( uint32_t ReplySize, uint32_t Timeout); +static int WINAPI hook_sendto( + SOCKET s, + const char* buf, + int len, + int flags, + const struct sockaddr *to, + int tolen); + /* Link pointers */ static uint32_t (WINAPI *next_GetAdaptersAddresses)( @@ -108,6 +118,15 @@ static uint32_t (WINAPI *next_IcmpSendEcho2)( uint32_t ReplySize, uint32_t Timeout); +static int (WINAPI *next_sendto)( + SOCKET s, + const char *buf, + int len, + int flags, + const struct sockaddr *to, + int tolen); + + static const struct hook_symbol netenv_hook_syms[] = { { .name = "GetAdaptersAddresses", @@ -132,7 +151,17 @@ static const struct hook_symbol netenv_hook_syms[] = { } }; +static struct hook_symbol netenv_hook_syms_ws2[] = { + { + .name = "sendto", + .patch = hook_sendto, + .ordinal = 20, + .link = (void **) &next_sendto + }, +}; + static uint32_t netenv_ip_prefix; +static uint32_t netenv_ip_bcast; static uint32_t netenv_ip_iface; static uint32_t netenv_ip_router; static uint8_t netenv_mac_addr[6]; @@ -155,17 +184,34 @@ HRESULT netenv_hook_init( } netenv_ip_prefix = kc_cfg->subnet; + netenv_ip_bcast = kc_cfg->bcast; netenv_ip_iface = kc_cfg->subnet | cfg->addr_suffix; netenv_ip_router = kc_cfg->subnet | cfg->router_suffix; memcpy(netenv_mac_addr, cfg->mac_addr, sizeof(netenv_mac_addr)); + netenv_hook_apply_hooks(NULL); + + return S_OK; +} + +void netenv_hook_apply_hooks(HMODULE mod) { hook_table_apply( - NULL, + mod, "iphlpapi.dll", netenv_hook_syms, _countof(netenv_hook_syms)); - return S_OK; + hook_table_apply( + mod, + "ws2_32.dll", + netenv_hook_syms_ws2, + _countof(netenv_hook_syms_ws2)); + + proc_addr_table_push( + mod, + "ws2_32.dll", + netenv_hook_syms_ws2, + _countof(netenv_hook_syms_ws2)); } static uint32_t WINAPI hook_GetAdaptersAddresses( @@ -506,3 +552,39 @@ static uint32_t WINAPI hook_IcmpSendEcho2( return 1; } + +static int WINAPI hook_sendto( + SOCKET s, + const char* buf, + int len, + int flags, + const struct sockaddr* to, + int tolen) { + if (to->sa_family != AF_INET) { + // we only care about IP packets + return next_sendto(s, buf, len, flags, to, tolen); + } + + const struct sockaddr_in* original_to = (struct sockaddr_in*)to; + + uint32_t bc_addr = _byteswap_ulong(netenv_ip_prefix | 0xFF); + + if (original_to->sin_addr.S_un.S_addr == bc_addr) { + + uint32_t src_addr = _byteswap_ulong(original_to->sin_addr.S_un.S_addr); + uint32_t dest_addr = _byteswap_ulong(netenv_ip_bcast); + + dprintf("Netenv: sendTo broadcast %u.%u.%u.%u -> %u.%u.%u.%u\n", + (src_addr >> 24) & 0xff, (src_addr >> 16) & 0xff, (src_addr >> 8) & 0xff, src_addr & 0xff, + (dest_addr >> 24) & 0xff, (dest_addr >> 16) & 0xff, (dest_addr >> 8) & 0xff, dest_addr & 0xff); + + struct sockaddr_in modified_to = {0}; + memcpy(&modified_to, original_to, tolen); + + modified_to.sin_addr.S_un.S_addr = dest_addr; + + return next_sendto(s, buf, len, flags, (struct sockaddr*)&modified_to, sizeof(modified_to)); + } + + return next_sendto(s, buf, len, flags, to, tolen); +} \ No newline at end of file diff --git a/common/platform/netenv.h b/common/platform/netenv.h index 977a3f6..88cae15 100644 --- a/common/platform/netenv.h +++ b/common/platform/netenv.h @@ -18,3 +18,4 @@ HRESULT netenv_hook_init( const struct netenv_config *cfg, const struct nusec_config *kc_cfg); +void netenv_hook_apply_hooks(HMODULE mod); \ No newline at end of file diff --git a/common/platform/nusec.h b/common/platform/nusec.h index 5d3dd4f..5d86f8d 100644 --- a/common/platform/nusec.h +++ b/common/platform/nusec.h @@ -14,6 +14,7 @@ struct nusec_config { uint8_t region; uint8_t system_flag; uint32_t subnet; + uint32_t bcast; uint16_t billing_type; wchar_t billing_ca[MAX_PATH]; wchar_t billing_pub[MAX_PATH]; diff --git a/common/platform/vfs.c b/common/platform/vfs.c index 97f7c13..ae5953e 100644 --- a/common/platform/vfs.c +++ b/common/platform/vfs.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "hooklib/path.h" @@ -17,7 +18,7 @@ #include "ewf.h" #include "util/dprintf.h" -static void vfs_fixup_path(wchar_t *path, size_t max_count); +static void vfs_fixup_path(wchar_t *path, size_t max_count, bool use_relative_envvar); static HRESULT vfs_mkdir_rec(const wchar_t *path); static HRESULT vfs_path_hook(const wchar_t *src, wchar_t *dest, size_t *count); static HRESULT vfs_path_hook_nthome( @@ -90,6 +91,15 @@ static const struct reg_hook_val vfs_reg_vals[] = { static struct vfs_config vfs_config; +const wchar_t* get_vfs_relative_envvar() { + static wchar_t path[MAX_PATH]; + if (!GetEnvironmentVariableW(L"SEGATOOLS_VFS_RELATIVE_PATH", path, MAX_PATH)) { + return NULL; + } + + return path; +} + HRESULT vfs_hook_init(const struct vfs_config *config, const char* game_id) { wchar_t temp[MAX_PATH]; @@ -150,12 +160,12 @@ HRESULT vfs_hook_init(const struct vfs_config *config, const char* game_id) memcpy(&vfs_config, config, sizeof(*config)); - vfs_fixup_path(vfs_nthome_real, _countof(vfs_nthome_real)); - vfs_fixup_path(vfs_config.amfs, _countof(vfs_config.amfs)); - vfs_fixup_path(vfs_config.appdata, _countof(vfs_config.appdata)); + vfs_fixup_path(vfs_nthome_real, _countof(vfs_nthome_real), false); + vfs_fixup_path(vfs_config.amfs, _countof(vfs_config.amfs), true); + vfs_fixup_path(vfs_config.appdata, _countof(vfs_config.appdata), true); if (vfs_config.option[0] != L'\0') { - vfs_fixup_path(vfs_config.option, _countof(vfs_config.option)); + vfs_fixup_path(vfs_config.option, _countof(vfs_config.option), true); } hr = vfs_mkdir_rec(vfs_config.amfs); @@ -247,7 +257,7 @@ HRESULT vfs_hook_init(const struct vfs_config *config, const char* game_id) return S_OK; } -static void vfs_fixup_path(wchar_t *path, size_t max_count) +static void vfs_fixup_path(wchar_t *path, size_t max_count, bool use_adjustment_envvar) { size_t count; wchar_t abspath[MAX_PATH]; @@ -257,7 +267,14 @@ static void vfs_fixup_path(wchar_t *path, size_t max_count) assert(max_count <= MAX_PATH); if (PathIsRelativeW(path)) { - count = GetFullPathNameW(path, _countof(abspath), abspath, NULL); + const wchar_t* append = get_vfs_relative_envvar(); + if (append != NULL && wcslen(append) > 0 && use_adjustment_envvar) { + wchar_t temp[MAX_PATH]; + swprintf_s(temp, MAX_PATH, L"%ls\\%ls", append, path); + count = GetFullPathNameW(temp, _countof(abspath), abspath, NULL); + } else { + count = GetFullPathNameW(path, _countof(abspath), abspath, NULL); + } /* GetFullPathName's length return value is tricky, because it includes the NUL terminator on failure, but doesn't on success. diff --git a/common/unityhook/hook.c b/common/unityhook/hook.c index 0683a6a..c8b70e0 100644 --- a/common/unityhook/hook.c +++ b/common/unityhook/hook.c @@ -11,7 +11,7 @@ #include "hooklib/dll.h" #include "hooklib/path.h" -#include "hooklib/printer.h" +#include "hooklib/printer_chc.h" #include "hooklib/reg.h" #include "hooklib/touch.h" #include "hooklib/serial.h" diff --git a/common/y3io/impl/dummy/y3io.c b/common/y3io/impl/dummy/y3io.c new file mode 100644 index 0000000..c8ccdc3 --- /dev/null +++ b/common/y3io/impl/dummy/y3io.c @@ -0,0 +1,68 @@ +#include +#include + +#include "util/dprintf.h" + +#include "hooklib/y3.h" + + +uint16_t y3_io_get_api_version() { + return 0x0100; +} + +HRESULT y3_io_init() { + dprintf("Y3 Dummy Cards: initialized\n"); + return S_OK; +} + +HRESULT y3_io_close() { + return S_OK; +} + +HRESULT y3_io_get_cards(struct CardInfo* pCardInfo, int* numCards) { + memset(pCardInfo, 0, sizeof(struct CardInfo) * *numCards); + + const float paddingX = 86.0f; + const float paddingY = 54.0f; + + // Dimensions of the flat panel + const float panelHeight = 1232.0f; + const float panelWidth = 1160.0f; + + // Number of cards in each row and column + const int numRows = 4; + const int numCols = 4; + int activeCards = numRows * numCols; + + if (*numCards < activeCards) { + activeCards = *numCards; + } + *numCards = activeCards; + + + // Calculate spacing between cards + const float cardWidth = (panelWidth - paddingY * 2) / (numCols - 1); + const float cardHeight = (panelHeight - paddingX * 2) / (numRows - 1); + + // Create example card info + for (int i = 0; i < activeCards; i++) { + int row = i / numCols; + int col = i % numCols; + + pCardInfo[i].fX = paddingX + (col * cardHeight); + pCardInfo[i].fY = paddingY + (row * cardWidth); + pCardInfo[i].fAngle = 0.0f; + pCardInfo[i].eCardType = TYPE0; + pCardInfo[i].eCardStatus = VALID; + pCardInfo[i].uID = 1000 + i; + pCardInfo[i].nNumChars = 0; + pCardInfo[i].ubChar0.Data = 0; + pCardInfo[i].ubChar1.Data = 0; + pCardInfo[i].ubChar2.Data = 0; + pCardInfo[i].ubChar3.Data = 0; + pCardInfo[i].ubChar4.Data = 0; + pCardInfo[i].ubChar5.Data = 0; + } + + return S_OK; +} \ No newline at end of file diff --git a/common/y3io/impl/websockets/3rdparty/cjson/LICENSE b/common/y3io/impl/websockets/3rdparty/cjson/LICENSE new file mode 100644 index 0000000..78deb04 --- /dev/null +++ b/common/y3io/impl/websockets/3rdparty/cjson/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/common/y3io/impl/websockets/3rdparty/cjson/cJSON.c b/common/y3io/impl/websockets/3rdparty/cjson/cJSON.c new file mode 100644 index 0000000..ca824f0 --- /dev/null +++ b/common/y3io/impl/websockets/3rdparty/cjson/cJSON.c @@ -0,0 +1,3191 @@ +/* + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/* cJSON */ +/* JSON parser in C. */ + +/* disable warnings about old C89 functions in MSVC */ +#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) +#define _CRT_SECURE_NO_DEPRECATE +#endif + +#ifdef __GNUC__ +#pragma GCC visibility push(default) +#endif +#if defined(_MSC_VER) +#pragma warning (push) +/* disable warning about single line comments in system headers */ +#pragma warning (disable : 4001) +#endif + +#include +#include +#include +#include +#include +#include +#include + +#ifdef ENABLE_LOCALES +#include +#endif + +#if defined(_MSC_VER) +#pragma warning (pop) +#endif +#ifdef __GNUC__ +#pragma GCC visibility pop +#endif + +#include "cJSON.h" + +/* define our own boolean type */ +#ifdef true +#undef true +#endif +#define true ((cJSON_bool)1) + +#ifdef false +#undef false +#endif +#define false ((cJSON_bool)0) + +/* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */ +#ifndef isinf +#define isinf(d) (isnan((d - d)) && !isnan(d)) +#endif +#ifndef isnan +#define isnan(d) (d != d) +#endif + +#ifndef NAN +#ifdef _WIN32 +#define NAN sqrt(-1.0) +#else +#define NAN 0.0/0.0 +#endif +#endif + +typedef struct { + const unsigned char *json; + size_t position; +} error; +static error global_error = { NULL, 0 }; + +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) +{ + return (const char*) (global_error.json + global_error.position); +} + +CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item) +{ + if (!cJSON_IsString(item)) + { + return NULL; + } + + return item->valuestring; +} + +CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item) +{ + if (!cJSON_IsNumber(item)) + { + return (double) NAN; + } + + return item->valuedouble; +} + +/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ +#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 18) + #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. +#endif + +CJSON_PUBLIC(const char*) cJSON_Version(void) +{ + static char version[15]; + sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); + + return version; +} + +/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */ +static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2) +{ + if ((string1 == NULL) || (string2 == NULL)) + { + return 1; + } + + if (string1 == string2) + { + return 0; + } + + for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++) + { + if (*string1 == '\0') + { + return 0; + } + } + + return tolower(*string1) - tolower(*string2); +} + +typedef struct internal_hooks +{ + void *(CJSON_CDECL *allocate)(size_t size); + void (CJSON_CDECL *deallocate)(void *pointer); + void *(CJSON_CDECL *reallocate)(void *pointer, size_t size); +} internal_hooks; + +#if defined(_MSC_VER) +/* work around MSVC error C2322: '...' address of dllimport '...' is not static */ +static void * CJSON_CDECL internal_malloc(size_t size) +{ + return malloc(size); +} +static void CJSON_CDECL internal_free(void *pointer) +{ + free(pointer); +} +static void * CJSON_CDECL internal_realloc(void *pointer, size_t size) +{ + return realloc(pointer, size); +} +#else +#define internal_malloc malloc +#define internal_free free +#define internal_realloc realloc +#endif + +/* strlen of character literals resolved at compile time */ +#define static_strlen(string_literal) (sizeof(string_literal) - sizeof("")) + +static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc }; + +static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks) +{ + size_t length = 0; + unsigned char *copy = NULL; + + if (string == NULL) + { + return NULL; + } + + length = strlen((const char*)string) + sizeof(""); + copy = (unsigned char*)hooks->allocate(length); + if (copy == NULL) + { + return NULL; + } + memcpy(copy, string, length); + + return copy; +} + +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) +{ + if (hooks == NULL) + { + /* Reset hooks */ + global_hooks.allocate = malloc; + global_hooks.deallocate = free; + global_hooks.reallocate = realloc; + return; + } + + global_hooks.allocate = malloc; + if (hooks->malloc_fn != NULL) + { + global_hooks.allocate = hooks->malloc_fn; + } + + global_hooks.deallocate = free; + if (hooks->free_fn != NULL) + { + global_hooks.deallocate = hooks->free_fn; + } + + /* use realloc only if both free and malloc are used */ + global_hooks.reallocate = NULL; + if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free)) + { + global_hooks.reallocate = realloc; + } +} + +/* Internal constructor. */ +static cJSON *cJSON_New_Item(const internal_hooks * const hooks) +{ + cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON)); + if (node) + { + memset(node, '\0', sizeof(cJSON)); + } + + return node; +} + +/* Delete a cJSON structure. */ +CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) +{ + cJSON *next = NULL; + while (item != NULL) + { + next = item->next; + if (!(item->type & cJSON_IsReference) && (item->child != NULL)) + { + cJSON_Delete(item->child); + } + if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) + { + global_hooks.deallocate(item->valuestring); + item->valuestring = NULL; + } + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) + { + global_hooks.deallocate(item->string); + item->string = NULL; + } + global_hooks.deallocate(item); + item = next; + } +} + +/* get the decimal point character of the current locale */ +static unsigned char get_decimal_point(void) +{ +#ifdef ENABLE_LOCALES + struct lconv *lconv = localeconv(); + return (unsigned char) lconv->decimal_point[0]; +#else + return '.'; +#endif +} + +typedef struct +{ + const unsigned char *content; + size_t length; + size_t offset; + size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */ + internal_hooks hooks; +} parse_buffer; + +/* check if the given size is left to read in a given parse buffer (starting with 1) */ +#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) +/* check if the buffer can be accessed at the given index (starting with 0) */ +#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) +#define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index)) +/* get a pointer to the buffer at the position */ +#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) + +/* Parse the input text to generate a number, and populate the result into item. */ +static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) +{ + double number = 0; + unsigned char *after_end = NULL; + unsigned char *number_c_string; + unsigned char decimal_point = get_decimal_point(); + size_t i = 0; + size_t number_string_length = 0; + cJSON_bool has_decimal_point = false; + + if ((input_buffer == NULL) || (input_buffer->content == NULL)) + { + return false; + } + + /* copy the number into a temporary buffer and replace '.' with the decimal point + * of the current locale (for strtod) + * This also takes care of '\0' not necessarily being available for marking the end of the input */ + for (i = 0; can_access_at_index(input_buffer, i); i++) + { + switch (buffer_at_offset(input_buffer)[i]) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '+': + case '-': + case 'e': + case 'E': + number_string_length++; + break; + + case '.': + number_string_length++; + has_decimal_point = true; + break; + + default: + goto loop_end; + } + } +loop_end: + /* malloc for temporary buffer, add 1 for '\0' */ + number_c_string = (unsigned char *) input_buffer->hooks.allocate(number_string_length + 1); + if (number_c_string == NULL) + { + return false; /* allocation failure */ + } + + memcpy(number_c_string, buffer_at_offset(input_buffer), number_string_length); + number_c_string[number_string_length] = '\0'; + + if (has_decimal_point) + { + for (i = 0; i < number_string_length; i++) + { + if (number_c_string[i] == '.') + { + /* replace '.' with the decimal point of the current locale (for strtod) */ + number_c_string[i] = decimal_point; + } + } + } + + number = strtod((const char*)number_c_string, (char**)&after_end); + if (number_c_string == after_end) + { + /* free the temporary buffer */ + input_buffer->hooks.deallocate(number_c_string); + return false; /* parse_error */ + } + + item->valuedouble = number; + + /* use saturation in case of overflow */ + if (number >= INT_MAX) + { + item->valueint = INT_MAX; + } + else if (number <= (double)INT_MIN) + { + item->valueint = INT_MIN; + } + else + { + item->valueint = (int)number; + } + + item->type = cJSON_Number; + + input_buffer->offset += (size_t)(after_end - number_c_string); + /* free the temporary buffer */ + input_buffer->hooks.deallocate(number_c_string); + return true; +} + +/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */ +CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number) +{ + if (number >= INT_MAX) + { + object->valueint = INT_MAX; + } + else if (number <= (double)INT_MIN) + { + object->valueint = INT_MIN; + } + else + { + object->valueint = (int)number; + } + + return object->valuedouble = number; +} + +/* Note: when passing a NULL valuestring, cJSON_SetValuestring treats this as an error and return NULL */ +CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) +{ + char *copy = NULL; + size_t v1_len; + size_t v2_len; + /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */ + if ((object == NULL) || !(object->type & cJSON_String) || (object->type & cJSON_IsReference)) + { + return NULL; + } + /* return NULL if the object is corrupted or valuestring is NULL */ + if (object->valuestring == NULL || valuestring == NULL) + { + return NULL; + } + + v1_len = strlen(valuestring); + v2_len = strlen(object->valuestring); + + if (v1_len <= v2_len) + { + /* strcpy does not handle overlapping string: [X1, X2] [Y1, Y2] => X2 < Y1 or Y2 < X1 */ + if (!( valuestring + v1_len < object->valuestring || object->valuestring + v2_len < valuestring )) + { + return NULL; + } + strcpy(object->valuestring, valuestring); + return object->valuestring; + } + copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks); + if (copy == NULL) + { + return NULL; + } + if (object->valuestring != NULL) + { + cJSON_free(object->valuestring); + } + object->valuestring = copy; + + return copy; +} + +typedef struct +{ + unsigned char *buffer; + size_t length; + size_t offset; + size_t depth; /* current nesting depth (for formatted printing) */ + cJSON_bool noalloc; + cJSON_bool format; /* is this print a formatted print */ + internal_hooks hooks; +} printbuffer; + +/* realloc printbuffer if necessary to have at least "needed" bytes more */ +static unsigned char* ensure(printbuffer * const p, size_t needed) +{ + unsigned char *newbuffer = NULL; + size_t newsize = 0; + + if ((p == NULL) || (p->buffer == NULL)) + { + return NULL; + } + + if ((p->length > 0) && (p->offset >= p->length)) + { + /* make sure that offset is valid */ + return NULL; + } + + if (needed > INT_MAX) + { + /* sizes bigger than INT_MAX are currently not supported */ + return NULL; + } + + needed += p->offset + 1; + if (needed <= p->length) + { + return p->buffer + p->offset; + } + + if (p->noalloc) { + return NULL; + } + + /* calculate new buffer size */ + if (needed > (INT_MAX / 2)) + { + /* overflow of int, use INT_MAX if possible */ + if (needed <= INT_MAX) + { + newsize = INT_MAX; + } + else + { + return NULL; + } + } + else + { + newsize = needed * 2; + } + + if (p->hooks.reallocate != NULL) + { + /* reallocate with realloc if available */ + newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize); + if (newbuffer == NULL) + { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + } + else + { + /* otherwise reallocate manually */ + newbuffer = (unsigned char*)p->hooks.allocate(newsize); + if (!newbuffer) + { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + + memcpy(newbuffer, p->buffer, p->offset + 1); + p->hooks.deallocate(p->buffer); + } + p->length = newsize; + p->buffer = newbuffer; + + return newbuffer + p->offset; +} + +/* calculate the new length of the string in a printbuffer and update the offset */ +static void update_offset(printbuffer * const buffer) +{ + const unsigned char *buffer_pointer = NULL; + if ((buffer == NULL) || (buffer->buffer == NULL)) + { + return; + } + buffer_pointer = buffer->buffer + buffer->offset; + + buffer->offset += strlen((const char*)buffer_pointer); +} + +/* securely comparison of floating-point variables */ +static cJSON_bool compare_double(double a, double b) +{ + double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b); + return (fabs(a - b) <= maxVal * DBL_EPSILON); +} + +/* Render the number nicely from the given item into a string. */ +static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + double d = item->valuedouble; + int length = 0; + size_t i = 0; + unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */ + unsigned char decimal_point = get_decimal_point(); + double test = 0.0; + + if (output_buffer == NULL) + { + return false; + } + + /* This checks for NaN and Infinity */ + if (isnan(d) || isinf(d)) + { + length = sprintf((char*)number_buffer, "null"); + } + else if(d == (double)item->valueint) + { + length = sprintf((char*)number_buffer, "%d", item->valueint); + } + else + { + /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ + length = sprintf((char*)number_buffer, "%1.15g", d); + + /* Check whether the original double can be recovered */ + if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d)) + { + /* If not, print with 17 decimal places of precision */ + length = sprintf((char*)number_buffer, "%1.17g", d); + } + } + + /* sprintf failed or buffer overrun occurred */ + if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) + { + return false; + } + + /* reserve appropriate space in the output */ + output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); + if (output_pointer == NULL) + { + return false; + } + + /* copy the printed number to the output and replace locale + * dependent decimal point with '.' */ + for (i = 0; i < ((size_t)length); i++) + { + if (number_buffer[i] == decimal_point) + { + output_pointer[i] = '.'; + continue; + } + + output_pointer[i] = number_buffer[i]; + } + output_pointer[i] = '\0'; + + output_buffer->offset += (size_t)length; + + return true; +} + +/* parse 4 digit hexadecimal number */ +static unsigned parse_hex4(const unsigned char * const input) +{ + unsigned int h = 0; + size_t i = 0; + + for (i = 0; i < 4; i++) + { + /* parse digit */ + if ((input[i] >= '0') && (input[i] <= '9')) + { + h += (unsigned int) input[i] - '0'; + } + else if ((input[i] >= 'A') && (input[i] <= 'F')) + { + h += (unsigned int) 10 + input[i] - 'A'; + } + else if ((input[i] >= 'a') && (input[i] <= 'f')) + { + h += (unsigned int) 10 + input[i] - 'a'; + } + else /* invalid */ + { + return 0; + } + + if (i < 3) + { + /* shift left to make place for the next nibble */ + h = h << 4; + } + } + + return h; +} + +/* converts a UTF-16 literal to UTF-8 + * A literal can be one or two sequences of the form \uXXXX */ +static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer) +{ + long unsigned int codepoint = 0; + unsigned int first_code = 0; + const unsigned char *first_sequence = input_pointer; + unsigned char utf8_length = 0; + unsigned char utf8_position = 0; + unsigned char sequence_length = 0; + unsigned char first_byte_mark = 0; + + if ((input_end - first_sequence) < 6) + { + /* input ends unexpectedly */ + goto fail; + } + + /* get the first utf16 sequence */ + first_code = parse_hex4(first_sequence + 2); + + /* check that the code is valid */ + if (((first_code >= 0xDC00) && (first_code <= 0xDFFF))) + { + goto fail; + } + + /* UTF16 surrogate pair */ + if ((first_code >= 0xD800) && (first_code <= 0xDBFF)) + { + const unsigned char *second_sequence = first_sequence + 6; + unsigned int second_code = 0; + sequence_length = 12; /* \uXXXX\uXXXX */ + + if ((input_end - second_sequence) < 6) + { + /* input ends unexpectedly */ + goto fail; + } + + if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u')) + { + /* missing second half of the surrogate pair */ + goto fail; + } + + /* get the second utf16 sequence */ + second_code = parse_hex4(second_sequence + 2); + /* check that the code is valid */ + if ((second_code < 0xDC00) || (second_code > 0xDFFF)) + { + /* invalid second half of the surrogate pair */ + goto fail; + } + + + /* calculate the unicode codepoint from the surrogate pair */ + codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF)); + } + else + { + sequence_length = 6; /* \uXXXX */ + codepoint = first_code; + } + + /* encode as UTF-8 + * takes at maximum 4 bytes to encode: + * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ + if (codepoint < 0x80) + { + /* normal ascii, encoding 0xxxxxxx */ + utf8_length = 1; + } + else if (codepoint < 0x800) + { + /* two bytes, encoding 110xxxxx 10xxxxxx */ + utf8_length = 2; + first_byte_mark = 0xC0; /* 11000000 */ + } + else if (codepoint < 0x10000) + { + /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */ + utf8_length = 3; + first_byte_mark = 0xE0; /* 11100000 */ + } + else if (codepoint <= 0x10FFFF) + { + /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */ + utf8_length = 4; + first_byte_mark = 0xF0; /* 11110000 */ + } + else + { + /* invalid unicode codepoint */ + goto fail; + } + + /* encode as utf8 */ + for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--) + { + /* 10xxxxxx */ + (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF); + codepoint >>= 6; + } + /* encode first byte */ + if (utf8_length > 1) + { + (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF); + } + else + { + (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F); + } + + *output_pointer += utf8_length; + + return sequence_length; + +fail: + return 0; +} + +/* Parse the input text into an unescaped cinput, and populate item. */ +static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) +{ + const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; + const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; + unsigned char *output_pointer = NULL; + unsigned char *output = NULL; + + /* not a string */ + if (buffer_at_offset(input_buffer)[0] != '\"') + { + goto fail; + } + + { + /* calculate approximate size of the output (overestimate) */ + size_t allocation_length = 0; + size_t skipped_bytes = 0; + while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"')) + { + /* is escape sequence */ + if (input_end[0] == '\\') + { + if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) + { + /* prevent buffer overflow when last input character is a backslash */ + goto fail; + } + skipped_bytes++; + input_end++; + } + input_end++; + } + if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"')) + { + goto fail; /* string ended unexpectedly */ + } + + /* This is at most how much we need for the output */ + allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes; + output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof("")); + if (output == NULL) + { + goto fail; /* allocation failure */ + } + } + + output_pointer = output; + /* loop through the string literal */ + while (input_pointer < input_end) + { + if (*input_pointer != '\\') + { + *output_pointer++ = *input_pointer++; + } + /* escape sequence */ + else + { + unsigned char sequence_length = 2; + if ((input_end - input_pointer) < 1) + { + goto fail; + } + + switch (input_pointer[1]) + { + case 'b': + *output_pointer++ = '\b'; + break; + case 'f': + *output_pointer++ = '\f'; + break; + case 'n': + *output_pointer++ = '\n'; + break; + case 'r': + *output_pointer++ = '\r'; + break; + case 't': + *output_pointer++ = '\t'; + break; + case '\"': + case '\\': + case '/': + *output_pointer++ = input_pointer[1]; + break; + + /* UTF-16 literal */ + case 'u': + sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); + if (sequence_length == 0) + { + /* failed to convert UTF16-literal to UTF-8 */ + goto fail; + } + break; + + default: + goto fail; + } + input_pointer += sequence_length; + } + } + + /* zero terminate the output */ + *output_pointer = '\0'; + + item->type = cJSON_String; + item->valuestring = (char*)output; + + input_buffer->offset = (size_t) (input_end - input_buffer->content); + input_buffer->offset++; + + return true; + +fail: + if (output != NULL) + { + input_buffer->hooks.deallocate(output); + output = NULL; + } + + if (input_pointer != NULL) + { + input_buffer->offset = (size_t)(input_pointer - input_buffer->content); + } + + return false; +} + +/* Render the cstring provided to an escaped version that can be printed. */ +static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) +{ + const unsigned char *input_pointer = NULL; + unsigned char *output = NULL; + unsigned char *output_pointer = NULL; + size_t output_length = 0; + /* numbers of additional characters needed for escaping */ + size_t escape_characters = 0; + + if (output_buffer == NULL) + { + return false; + } + + /* empty string */ + if (input == NULL) + { + output = ensure(output_buffer, sizeof("\"\"")); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "\"\""); + + return true; + } + + /* set "flag" to 1 if something needs to be escaped */ + for (input_pointer = input; *input_pointer; input_pointer++) + { + switch (*input_pointer) + { + case '\"': + case '\\': + case '\b': + case '\f': + case '\n': + case '\r': + case '\t': + /* one character escape sequence */ + escape_characters++; + break; + default: + if (*input_pointer < 32) + { + /* UTF-16 escape sequence uXXXX */ + escape_characters += 5; + } + break; + } + } + output_length = (size_t)(input_pointer - input) + escape_characters; + + output = ensure(output_buffer, output_length + sizeof("\"\"")); + if (output == NULL) + { + return false; + } + + /* no characters have to be escaped */ + if (escape_characters == 0) + { + output[0] = '\"'; + memcpy(output + 1, input, output_length); + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; + } + + output[0] = '\"'; + output_pointer = output + 1; + /* copy the string */ + for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++) + { + if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\')) + { + /* normal character, copy */ + *output_pointer = *input_pointer; + } + else + { + /* character needs to be escaped */ + *output_pointer++ = '\\'; + switch (*input_pointer) + { + case '\\': + *output_pointer = '\\'; + break; + case '\"': + *output_pointer = '\"'; + break; + case '\b': + *output_pointer = 'b'; + break; + case '\f': + *output_pointer = 'f'; + break; + case '\n': + *output_pointer = 'n'; + break; + case '\r': + *output_pointer = 'r'; + break; + case '\t': + *output_pointer = 't'; + break; + default: + /* escape and print as unicode codepoint */ + sprintf((char*)output_pointer, "u%04x", *input_pointer); + output_pointer += 4; + break; + } + } + } + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; +} + +/* Invoke print_string_ptr (which is useful) on an item. */ +static cJSON_bool print_string(const cJSON * const item, printbuffer * const p) +{ + return print_string_ptr((unsigned char*)item->valuestring, p); +} + +/* Predeclare these prototypes. */ +static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer); + +/* Utility to jump whitespace and cr/lf */ +static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) +{ + if ((buffer == NULL) || (buffer->content == NULL)) + { + return NULL; + } + + if (cannot_access_at_index(buffer, 0)) + { + return buffer; + } + + while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) + { + buffer->offset++; + } + + if (buffer->offset == buffer->length) + { + buffer->offset--; + } + + return buffer; +} + +/* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */ +static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) +{ + if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0)) + { + return NULL; + } + + if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) + { + buffer->offset += 3; + } + + return buffer; +} + +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated) +{ + size_t buffer_length; + + if (NULL == value) + { + return NULL; + } + + /* Adding null character size due to require_null_terminated. */ + buffer_length = strlen(value) + sizeof(""); + + return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated); +} + +/* Parse an object - create a new root, and populate. */ +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated) +{ + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + cJSON *item = NULL; + + /* reset error position */ + global_error.json = NULL; + global_error.position = 0; + + if (value == NULL || 0 == buffer_length) + { + goto fail; + } + + buffer.content = (const unsigned char*)value; + buffer.length = buffer_length; + buffer.offset = 0; + buffer.hooks = global_hooks; + + item = cJSON_New_Item(&global_hooks); + if (item == NULL) /* memory fail */ + { + goto fail; + } + + if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) + { + /* parse failure. ep is set. */ + goto fail; + } + + /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ + if (require_null_terminated) + { + buffer_skip_whitespace(&buffer); + if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') + { + goto fail; + } + } + if (return_parse_end) + { + *return_parse_end = (const char*)buffer_at_offset(&buffer); + } + + return item; + +fail: + if (item != NULL) + { + cJSON_Delete(item); + } + + if (value != NULL) + { + error local_error; + local_error.json = (const unsigned char*)value; + local_error.position = 0; + + if (buffer.offset < buffer.length) + { + local_error.position = buffer.offset; + } + else if (buffer.length > 0) + { + local_error.position = buffer.length - 1; + } + + if (return_parse_end != NULL) + { + *return_parse_end = (const char*)local_error.json + local_error.position; + } + + global_error = local_error; + } + + return NULL; +} + +/* Default options for cJSON_Parse */ +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) +{ + return cJSON_ParseWithOpts(value, 0, 0); +} + +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length) +{ + return cJSON_ParseWithLengthOpts(value, buffer_length, 0, 0); +} + +#define cjson_min(a, b) (((a) < (b)) ? (a) : (b)) + +static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks) +{ + static const size_t default_buffer_size = 256; + printbuffer buffer[1]; + unsigned char *printed = NULL; + + memset(buffer, 0, sizeof(buffer)); + + /* create buffer */ + buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size); + buffer->length = default_buffer_size; + buffer->format = format; + buffer->hooks = *hooks; + if (buffer->buffer == NULL) + { + goto fail; + } + + /* print the value */ + if (!print_value(item, buffer)) + { + goto fail; + } + update_offset(buffer); + + /* check if reallocate is available */ + if (hooks->reallocate != NULL) + { + printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1); + if (printed == NULL) { + goto fail; + } + buffer->buffer = NULL; + } + else /* otherwise copy the JSON over to a new buffer */ + { + printed = (unsigned char*) hooks->allocate(buffer->offset + 1); + if (printed == NULL) + { + goto fail; + } + memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1)); + printed[buffer->offset] = '\0'; /* just to be sure */ + + /* free the buffer */ + hooks->deallocate(buffer->buffer); + buffer->buffer = NULL; + } + + return printed; + +fail: + if (buffer->buffer != NULL) + { + hooks->deallocate(buffer->buffer); + buffer->buffer = NULL; + } + + if (printed != NULL) + { + hooks->deallocate(printed); + printed = NULL; + } + + return NULL; +} + +/* Render a cJSON item/entity/structure to text. */ +CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item) +{ + return (char*)print(item, true, &global_hooks); +} + +CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item) +{ + return (char*)print(item, false, &global_hooks); +} + +CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt) +{ + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + + if (prebuffer < 0) + { + return NULL; + } + + p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer); + if (!p.buffer) + { + return NULL; + } + + p.length = (size_t)prebuffer; + p.offset = 0; + p.noalloc = false; + p.format = fmt; + p.hooks = global_hooks; + + if (!print_value(item, &p)) + { + global_hooks.deallocate(p.buffer); + p.buffer = NULL; + return NULL; + } + + return (char*)p.buffer; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format) +{ + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + + if ((length < 0) || (buffer == NULL)) + { + return false; + } + + p.buffer = (unsigned char*)buffer; + p.length = (size_t)length; + p.offset = 0; + p.noalloc = true; + p.format = format; + p.hooks = global_hooks; + + return print_value(item, &p); +} + +/* Parser core - when encountering text, process appropriately. */ +static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer) +{ + if ((input_buffer == NULL) || (input_buffer->content == NULL)) + { + return false; /* no input */ + } + + /* parse the different types of values */ + /* null */ + if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0)) + { + item->type = cJSON_NULL; + input_buffer->offset += 4; + return true; + } + /* false */ + if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0)) + { + item->type = cJSON_False; + input_buffer->offset += 5; + return true; + } + /* true */ + if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0)) + { + item->type = cJSON_True; + item->valueint = 1; + input_buffer->offset += 4; + return true; + } + /* string */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"')) + { + return parse_string(item, input_buffer); + } + /* number */ + if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9')))) + { + return parse_number(item, input_buffer); + } + /* array */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '[')) + { + return parse_array(item, input_buffer); + } + /* object */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{')) + { + return parse_object(item, input_buffer); + } + + return false; +} + +/* Render a value to text. */ +static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output = NULL; + + if ((item == NULL) || (output_buffer == NULL)) + { + return false; + } + + switch ((item->type) & 0xFF) + { + case cJSON_NULL: + output = ensure(output_buffer, 5); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "null"); + return true; + + case cJSON_False: + output = ensure(output_buffer, 6); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "false"); + return true; + + case cJSON_True: + output = ensure(output_buffer, 5); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "true"); + return true; + + case cJSON_Number: + return print_number(item, output_buffer); + + case cJSON_Raw: + { + size_t raw_length = 0; + if (item->valuestring == NULL) + { + return false; + } + + raw_length = strlen(item->valuestring) + sizeof(""); + output = ensure(output_buffer, raw_length); + if (output == NULL) + { + return false; + } + memcpy(output, item->valuestring, raw_length); + return true; + } + + case cJSON_String: + return print_string(item, output_buffer); + + case cJSON_Array: + return print_array(item, output_buffer); + + case cJSON_Object: + return print_object(item, output_buffer); + + default: + return false; + } +} + +/* Build an array from input text. */ +static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer) +{ + cJSON *head = NULL; /* head of the linked list */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (buffer_at_offset(input_buffer)[0] != '[') + { + /* not an array */ + goto fail; + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']')) + { + /* empty array */ + goto success; + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) + { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do + { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) + { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) + { + /* start the linked list */ + current_item = head = new_item; + } + else + { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + /* parse next value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) + { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } + while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']') + { + goto fail; /* expected end of array */ + } + +success: + input_buffer->depth--; + + if (head != NULL) { + head->prev = current_item; + } + + item->type = cJSON_Array; + item->child = head; + + input_buffer->offset++; + + return true; + +fail: + if (head != NULL) + { + cJSON_Delete(head); + } + + return false; +} + +/* Render an array to text */ +static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_element = item->child; + + if (output_buffer == NULL) + { + return false; + } + + /* Compose the output array. */ + /* opening square bracket */ + output_pointer = ensure(output_buffer, 1); + if (output_pointer == NULL) + { + return false; + } + + *output_pointer = '['; + output_buffer->offset++; + output_buffer->depth++; + + while (current_element != NULL) + { + if (!print_value(current_element, output_buffer)) + { + return false; + } + update_offset(output_buffer); + if (current_element->next) + { + length = (size_t) (output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ','; + if(output_buffer->format) + { + *output_pointer++ = ' '; + } + *output_pointer = '\0'; + output_buffer->offset += length; + } + current_element = current_element->next; + } + + output_pointer = ensure(output_buffer, 2); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ']'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; +} + +/* Build an object from the text. */ +static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer) +{ + cJSON *head = NULL; /* linked list head */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{')) + { + goto fail; /* not an object */ + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}')) + { + goto success; /* empty object */ + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) + { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do + { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) + { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) + { + /* start the linked list */ + current_item = head = new_item; + } + else + { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + if (cannot_access_at_index(input_buffer, 1)) + { + goto fail; /* nothing comes after the comma */ + } + + /* parse the name of the child */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_string(current_item, input_buffer)) + { + goto fail; /* failed to parse name */ + } + buffer_skip_whitespace(input_buffer); + + /* swap valuestring and string, because we parsed the name */ + current_item->string = current_item->valuestring; + current_item->valuestring = NULL; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':')) + { + goto fail; /* invalid object */ + } + + /* parse the value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) + { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } + while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}')) + { + goto fail; /* expected end of object */ + } + +success: + input_buffer->depth--; + + if (head != NULL) { + head->prev = current_item; + } + + item->type = cJSON_Object; + item->child = head; + + input_buffer->offset++; + return true; + +fail: + if (head != NULL) + { + cJSON_Delete(head); + } + + return false; +} + +/* Render an object to text. */ +static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_item = item->child; + + if (output_buffer == NULL) + { + return false; + } + + /* Compose the output: */ + length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */ + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + + *output_pointer++ = '{'; + output_buffer->depth++; + if (output_buffer->format) + { + *output_pointer++ = '\n'; + } + output_buffer->offset += length; + + while (current_item) + { + if (output_buffer->format) + { + size_t i; + output_pointer = ensure(output_buffer, output_buffer->depth); + if (output_pointer == NULL) + { + return false; + } + for (i = 0; i < output_buffer->depth; i++) + { + *output_pointer++ = '\t'; + } + output_buffer->offset += output_buffer->depth; + } + + /* print key */ + if (!print_string_ptr((unsigned char*)current_item->string, output_buffer)) + { + return false; + } + update_offset(output_buffer); + + length = (size_t) (output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ':'; + if (output_buffer->format) + { + *output_pointer++ = '\t'; + } + output_buffer->offset += length; + + /* print value */ + if (!print_value(current_item, output_buffer)) + { + return false; + } + update_offset(output_buffer); + + /* print comma if not last */ + length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0)); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + if (current_item->next) + { + *output_pointer++ = ','; + } + + if (output_buffer->format) + { + *output_pointer++ = '\n'; + } + *output_pointer = '\0'; + output_buffer->offset += length; + + current_item = current_item->next; + } + + output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); + if (output_pointer == NULL) + { + return false; + } + if (output_buffer->format) + { + size_t i; + for (i = 0; i < (output_buffer->depth - 1); i++) + { + *output_pointer++ = '\t'; + } + } + *output_pointer++ = '}'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; +} + +/* Get Array size/item / object item. */ +CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array) +{ + cJSON *child = NULL; + size_t size = 0; + + if (array == NULL) + { + return 0; + } + + child = array->child; + + while(child != NULL) + { + size++; + child = child->next; + } + + /* FIXME: Can overflow here. Cannot be fixed without breaking the API */ + + return (int)size; +} + +static cJSON* get_array_item(const cJSON *array, size_t index) +{ + cJSON *current_child = NULL; + + if (array == NULL) + { + return NULL; + } + + current_child = array->child; + while ((current_child != NULL) && (index > 0)) + { + index--; + current_child = current_child->next; + } + + return current_child; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index) +{ + if (index < 0) + { + return NULL; + } + + return get_array_item(array, (size_t)index); +} + +static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive) +{ + cJSON *current_element = NULL; + + if ((object == NULL) || (name == NULL)) + { + return NULL; + } + + current_element = object->child; + if (case_sensitive) + { + while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0)) + { + current_element = current_element->next; + } + } + else + { + while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) + { + current_element = current_element->next; + } + } + + if ((current_element == NULL) || (current_element->string == NULL)) { + return NULL; + } + + return current_element; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string) +{ + return get_object_item(object, string, false); +} + +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string) +{ + return get_object_item(object, string, true); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string) +{ + return cJSON_GetObjectItem(object, string) ? 1 : 0; +} + +/* Utility for array list handling. */ +static void suffix_object(cJSON *prev, cJSON *item) +{ + prev->next = item; + item->prev = prev; +} + +/* Utility for handling references. */ +static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks) +{ + cJSON *reference = NULL; + if (item == NULL) + { + return NULL; + } + + reference = cJSON_New_Item(hooks); + if (reference == NULL) + { + return NULL; + } + + memcpy(reference, item, sizeof(cJSON)); + reference->string = NULL; + reference->type |= cJSON_IsReference; + reference->next = reference->prev = NULL; + return reference; +} + +static cJSON_bool add_item_to_array(cJSON *array, cJSON *item) +{ + cJSON *child = NULL; + + if ((item == NULL) || (array == NULL) || (array == item)) + { + return false; + } + + child = array->child; + /* + * To find the last item in array quickly, we use prev in array + */ + if (child == NULL) + { + /* list is empty, start new one */ + array->child = item; + item->prev = item; + item->next = NULL; + } + else + { + /* append to the end */ + if (child->prev) + { + suffix_object(child->prev, item); + array->child->prev = item; + } + } + + return true; +} + +/* Add item to array/object. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item) +{ + return add_item_to_array(array, item); +} + +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) + #pragma GCC diagnostic push +#endif +#ifdef __GNUC__ +#pragma GCC diagnostic ignored "-Wcast-qual" +#endif +/* helper function to cast away const */ +static void* cast_away_const(const void* string) +{ + return (void*)string; +} +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) + #pragma GCC diagnostic pop +#endif + + +static cJSON_bool add_item_to_object(cJSON * const object, const char * const string, cJSON * const item, const internal_hooks * const hooks, const cJSON_bool constant_key) +{ + char *new_key = NULL; + int new_type = cJSON_Invalid; + + if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item)) + { + return false; + } + + if (constant_key) + { + new_key = (char*)cast_away_const(string); + new_type = item->type | cJSON_StringIsConst; + } + else + { + new_key = (char*)cJSON_strdup((const unsigned char*)string, hooks); + if (new_key == NULL) + { + return false; + } + + new_type = item->type & ~cJSON_StringIsConst; + } + + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) + { + hooks->deallocate(item->string); + } + + item->string = new_key; + item->type = new_type; + + return add_item_to_array(object, item); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) +{ + return add_item_to_object(object, string, item, &global_hooks, false); +} + +/* Add an item to an object with constant string as key */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) +{ + return add_item_to_object(object, string, item, &global_hooks, true); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) +{ + if (array == NULL) + { + return false; + } + + return add_item_to_array(array, create_reference(item, &global_hooks)); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) +{ + if ((object == NULL) || (string == NULL)) + { + return false; + } + + return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false); +} + +CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name) +{ + cJSON *null = cJSON_CreateNull(); + if (add_item_to_object(object, name, null, &global_hooks, false)) + { + return null; + } + + cJSON_Delete(null); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name) +{ + cJSON *true_item = cJSON_CreateTrue(); + if (add_item_to_object(object, name, true_item, &global_hooks, false)) + { + return true_item; + } + + cJSON_Delete(true_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name) +{ + cJSON *false_item = cJSON_CreateFalse(); + if (add_item_to_object(object, name, false_item, &global_hooks, false)) + { + return false_item; + } + + cJSON_Delete(false_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean) +{ + cJSON *bool_item = cJSON_CreateBool(boolean); + if (add_item_to_object(object, name, bool_item, &global_hooks, false)) + { + return bool_item; + } + + cJSON_Delete(bool_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number) +{ + cJSON *number_item = cJSON_CreateNumber(number); + if (add_item_to_object(object, name, number_item, &global_hooks, false)) + { + return number_item; + } + + cJSON_Delete(number_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string) +{ + cJSON *string_item = cJSON_CreateString(string); + if (add_item_to_object(object, name, string_item, &global_hooks, false)) + { + return string_item; + } + + cJSON_Delete(string_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw) +{ + cJSON *raw_item = cJSON_CreateRaw(raw); + if (add_item_to_object(object, name, raw_item, &global_hooks, false)) + { + return raw_item; + } + + cJSON_Delete(raw_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name) +{ + cJSON *object_item = cJSON_CreateObject(); + if (add_item_to_object(object, name, object_item, &global_hooks, false)) + { + return object_item; + } + + cJSON_Delete(object_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name) +{ + cJSON *array = cJSON_CreateArray(); + if (add_item_to_object(object, name, array, &global_hooks, false)) + { + return array; + } + + cJSON_Delete(array); + return NULL; +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item) +{ + if ((parent == NULL) || (item == NULL) || (item != parent->child && item->prev == NULL)) + { + return NULL; + } + + if (item != parent->child) + { + /* not the first element */ + item->prev->next = item->next; + } + if (item->next != NULL) + { + /* not the last element */ + item->next->prev = item->prev; + } + + if (item == parent->child) + { + /* first element */ + parent->child = item->next; + } + else if (item->next == NULL) + { + /* last element */ + parent->child->prev = item->prev; + } + + /* make sure the detached item doesn't point anywhere anymore */ + item->prev = NULL; + item->next = NULL; + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which) +{ + if (which < 0) + { + return NULL; + } + + return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which)); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) +{ + cJSON_Delete(cJSON_DetachItemFromArray(array, which)); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string) +{ + cJSON *to_detach = cJSON_GetObjectItem(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string) +{ + cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) +{ + cJSON_Delete(cJSON_DetachItemFromObject(object, string)); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string) +{ + cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string)); +} + +/* Replace array/object items with new ones. */ +CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) +{ + cJSON *after_inserted = NULL; + + if (which < 0 || newitem == NULL) + { + return false; + } + + after_inserted = get_array_item(array, (size_t)which); + if (after_inserted == NULL) + { + return add_item_to_array(array, newitem); + } + + if (after_inserted != array->child && after_inserted->prev == NULL) { + /* return false if after_inserted is a corrupted array item */ + return false; + } + + newitem->next = after_inserted; + newitem->prev = after_inserted->prev; + after_inserted->prev = newitem; + if (after_inserted == array->child) + { + array->child = newitem; + } + else + { + newitem->prev->next = newitem; + } + return true; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement) +{ + if ((parent == NULL) || (parent->child == NULL) || (replacement == NULL) || (item == NULL)) + { + return false; + } + + if (replacement == item) + { + return true; + } + + replacement->next = item->next; + replacement->prev = item->prev; + + if (replacement->next != NULL) + { + replacement->next->prev = replacement; + } + if (parent->child == item) + { + if (parent->child->prev == parent->child) + { + replacement->prev = replacement; + } + parent->child = replacement; + } + else + { /* + * To find the last item in array quickly, we use prev in array. + * We can't modify the last item's next pointer where this item was the parent's child + */ + if (replacement->prev != NULL) + { + replacement->prev->next = replacement; + } + if (replacement->next == NULL) + { + parent->child->prev = replacement; + } + } + + item->next = NULL; + item->prev = NULL; + cJSON_Delete(item); + + return true; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) +{ + if (which < 0) + { + return false; + } + + return cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); +} + +static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive) +{ + if ((replacement == NULL) || (string == NULL)) + { + return false; + } + + /* replace the name in the replacement */ + if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL)) + { + cJSON_free(replacement->string); + } + replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); + if (replacement->string == NULL) + { + return false; + } + + replacement->type &= ~cJSON_StringIsConst; + + return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) +{ + return replace_item_in_object(object, string, newitem, false); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem) +{ + return replace_item_in_object(object, string, newitem, true); +} + +/* Create basic types: */ +CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_NULL; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_True; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_False; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = boolean ? cJSON_True : cJSON_False; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_Number; + item->valuedouble = num; + + /* use saturation in case of overflow */ + if (num >= INT_MAX) + { + item->valueint = INT_MAX; + } + else if (num <= (double)INT_MIN) + { + item->valueint = INT_MIN; + } + else + { + item->valueint = (int)num; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_String; + item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) + { + item->type = cJSON_String | cJSON_IsReference; + item->valuestring = (char*)cast_away_const(string); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_Object | cJSON_IsReference; + item->child = (cJSON*)cast_away_const(child); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_Array | cJSON_IsReference; + item->child = (cJSON*)cast_away_const(child); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_Raw; + item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type=cJSON_Array; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) + { + item->type = cJSON_Object; + } + + return item; +} + +/* Create Arrays: */ +CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber(numbers[i]); + if (!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber((double)numbers[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber(numbers[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (strings == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for (i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateString(strings[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p,n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +/* Duplication */ +cJSON * cJSON_Duplicate_rec(const cJSON *item, size_t depth, cJSON_bool recurse); + +CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) +{ + return cJSON_Duplicate_rec(item, 0, recurse ); +} + +cJSON * cJSON_Duplicate_rec(const cJSON *item, size_t depth, cJSON_bool recurse) +{ + cJSON *newitem = NULL; + cJSON *child = NULL; + cJSON *next = NULL; + cJSON *newchild = NULL; + + /* Bail on bad ptr */ + if (!item) + { + goto fail; + } + /* Create new item */ + newitem = cJSON_New_Item(&global_hooks); + if (!newitem) + { + goto fail; + } + /* Copy over all vars */ + newitem->type = item->type & (~cJSON_IsReference); + newitem->valueint = item->valueint; + newitem->valuedouble = item->valuedouble; + if (item->valuestring) + { + newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks); + if (!newitem->valuestring) + { + goto fail; + } + } + if (item->string) + { + newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks); + if (!newitem->string) + { + goto fail; + } + } + /* If non-recursive, then we're done! */ + if (!recurse) + { + return newitem; + } + /* Walk the ->next chain for the child. */ + child = item->child; + while (child != NULL) + { + if(depth >= CJSON_CIRCULAR_LIMIT) { + goto fail; + } + newchild = cJSON_Duplicate_rec(child, depth + 1, true); /* Duplicate (with recurse) each item in the ->next chain */ + if (!newchild) + { + goto fail; + } + if (next != NULL) + { + /* If newitem->child already set, then crosswire ->prev and ->next and move on */ + next->next = newchild; + newchild->prev = next; + next = newchild; + } + else + { + /* Set newitem->child and move to it */ + newitem->child = newchild; + next = newchild; + } + child = child->next; + } + if (newitem && newitem->child) + { + newitem->child->prev = newchild; + } + + return newitem; + +fail: + if (newitem != NULL) + { + cJSON_Delete(newitem); + } + + return NULL; +} + +static void skip_oneline_comment(char **input) +{ + *input += static_strlen("//"); + + for (; (*input)[0] != '\0'; ++(*input)) + { + if ((*input)[0] == '\n') { + *input += static_strlen("\n"); + return; + } + } +} + +static void skip_multiline_comment(char **input) +{ + *input += static_strlen("/*"); + + for (; (*input)[0] != '\0'; ++(*input)) + { + if (((*input)[0] == '*') && ((*input)[1] == '/')) + { + *input += static_strlen("*/"); + return; + } + } +} + +static void minify_string(char **input, char **output) { + (*output)[0] = (*input)[0]; + *input += static_strlen("\""); + *output += static_strlen("\""); + + + for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) { + (*output)[0] = (*input)[0]; + + if ((*input)[0] == '\"') { + (*output)[0] = '\"'; + *input += static_strlen("\""); + *output += static_strlen("\""); + return; + } else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) { + (*output)[1] = (*input)[1]; + *input += static_strlen("\""); + *output += static_strlen("\""); + } + } +} + +CJSON_PUBLIC(void) cJSON_Minify(char *json) +{ + char *into = json; + + if (json == NULL) + { + return; + } + + while (json[0] != '\0') + { + switch (json[0]) + { + case ' ': + case '\t': + case '\r': + case '\n': + json++; + break; + + case '/': + if (json[1] == '/') + { + skip_oneline_comment(&json); + } + else if (json[1] == '*') + { + skip_multiline_comment(&json); + } else { + json++; + } + break; + + case '\"': + minify_string(&json, (char**)&into); + break; + + default: + into[0] = json[0]; + json++; + into++; + } + } + + /* and null-terminate. */ + *into = '\0'; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Invalid; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_False; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xff) == cJSON_True; +} + + +CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & (cJSON_True | cJSON_False)) != 0; +} +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_NULL; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Number; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_String; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Array; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Object; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Raw; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive) +{ + if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF))) + { + return false; + } + + /* check if type is valid */ + switch (a->type & 0xFF) + { + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + case cJSON_Number: + case cJSON_String: + case cJSON_Raw: + case cJSON_Array: + case cJSON_Object: + break; + + default: + return false; + } + + /* identical objects are equal */ + if (a == b) + { + return true; + } + + switch (a->type & 0xFF) + { + /* in these cases and equal type is enough */ + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + return true; + + case cJSON_Number: + if (compare_double(a->valuedouble, b->valuedouble)) + { + return true; + } + return false; + + case cJSON_String: + case cJSON_Raw: + if ((a->valuestring == NULL) || (b->valuestring == NULL)) + { + return false; + } + if (strcmp(a->valuestring, b->valuestring) == 0) + { + return true; + } + + return false; + + case cJSON_Array: + { + cJSON *a_element = a->child; + cJSON *b_element = b->child; + + for (; (a_element != NULL) && (b_element != NULL);) + { + if (!cJSON_Compare(a_element, b_element, case_sensitive)) + { + return false; + } + + a_element = a_element->next; + b_element = b_element->next; + } + + /* one of the arrays is longer than the other */ + if (a_element != b_element) { + return false; + } + + return true; + } + + case cJSON_Object: + { + cJSON *a_element = NULL; + cJSON *b_element = NULL; + cJSON_ArrayForEach(a_element, a) + { + /* TODO This has O(n^2) runtime, which is horrible! */ + b_element = get_object_item(b, a_element->string, case_sensitive); + if (b_element == NULL) + { + return false; + } + + if (!cJSON_Compare(a_element, b_element, case_sensitive)) + { + return false; + } + } + + /* doing this twice, once on a and b to prevent true comparison if a subset of b + * TODO: Do this the proper way, this is just a fix for now */ + cJSON_ArrayForEach(b_element, b) + { + a_element = get_object_item(a, b_element->string, case_sensitive); + if (a_element == NULL) + { + return false; + } + + if (!cJSON_Compare(b_element, a_element, case_sensitive)) + { + return false; + } + } + + return true; + } + + default: + return false; + } +} + +CJSON_PUBLIC(void *) cJSON_malloc(size_t size) +{ + return global_hooks.allocate(size); +} + +CJSON_PUBLIC(void) cJSON_free(void *object) +{ + global_hooks.deallocate(object); + object = NULL; +} diff --git a/common/y3io/impl/websockets/3rdparty/cjson/cJSON.h b/common/y3io/impl/websockets/3rdparty/cjson/cJSON.h new file mode 100644 index 0000000..37520bb --- /dev/null +++ b/common/y3io/impl/websockets/3rdparty/cjson/cJSON.h @@ -0,0 +1,306 @@ +/* + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef cJSON__h +#define cJSON__h + +#ifdef __cplusplus +extern "C" +{ +#endif + +#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)) +#define __WINDOWS__ +#endif + +#ifdef __WINDOWS__ + +/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options: + +CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols +CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default) +CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol + +For *nix builds that support visibility attribute, you can define similar behavior by + +setting default visibility to hidden by adding +-fvisibility=hidden (for gcc) +or +-xldscope=hidden (for sun cc) +to CFLAGS + +then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does + +*/ + +#define CJSON_CDECL __cdecl +#define CJSON_STDCALL __stdcall + +/* export symbols by default, this is necessary for copy pasting the C and header file */ +#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS) +#define CJSON_EXPORT_SYMBOLS +#endif + +#if defined(CJSON_HIDE_SYMBOLS) +#define CJSON_PUBLIC(type) type CJSON_STDCALL +#elif defined(CJSON_EXPORT_SYMBOLS) +#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL +#elif defined(CJSON_IMPORT_SYMBOLS) +#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL +#endif +#else /* !__WINDOWS__ */ +#define CJSON_CDECL +#define CJSON_STDCALL + +#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY) +#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type +#else +#define CJSON_PUBLIC(type) type +#endif +#endif + +/* project version */ +#define CJSON_VERSION_MAJOR 1 +#define CJSON_VERSION_MINOR 7 +#define CJSON_VERSION_PATCH 18 + +#include + +/* cJSON Types: */ +#define cJSON_Invalid (0) +#define cJSON_False (1 << 0) +#define cJSON_True (1 << 1) +#define cJSON_NULL (1 << 2) +#define cJSON_Number (1 << 3) +#define cJSON_String (1 << 4) +#define cJSON_Array (1 << 5) +#define cJSON_Object (1 << 6) +#define cJSON_Raw (1 << 7) /* raw json */ + +#define cJSON_IsReference 256 +#define cJSON_StringIsConst 512 + +/* The cJSON structure: */ +typedef struct cJSON +{ + /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ + struct cJSON *next; + struct cJSON *prev; + /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ + struct cJSON *child; + + /* The type of the item, as above. */ + int type; + + /* The item's string, if type==cJSON_String and type == cJSON_Raw */ + char *valuestring; + /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */ + int valueint; + /* The item's number, if type==cJSON_Number */ + double valuedouble; + + /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ + char *string; +} cJSON; + +typedef struct cJSON_Hooks +{ + /* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */ + void *(CJSON_CDECL *malloc_fn)(size_t sz); + void (CJSON_CDECL *free_fn)(void *ptr); +} cJSON_Hooks; + +typedef int cJSON_bool; + +/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them. + * This is to prevent stack overflows. */ +#ifndef CJSON_NESTING_LIMIT +#define CJSON_NESTING_LIMIT 1000 +#endif + +/* Limits the length of circular references can be before cJSON rejects to parse them. + * This is to prevent stack overflows. */ +#ifndef CJSON_CIRCULAR_LIMIT +#define CJSON_CIRCULAR_LIMIT 10000 +#endif + +/* returns the version of cJSON as a string */ +CJSON_PUBLIC(const char*) cJSON_Version(void); + +/* Supply malloc, realloc and free functions to cJSON */ +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks); + +/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */ +/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */ +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value); +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length); +/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */ +/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */ +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated); +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated); + +/* Render a cJSON entity to text for transfer/storage. */ +CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item); +/* Render a cJSON entity to text for transfer/storage without any formatting. */ +CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item); +/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */ +CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt); +/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */ +/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */ +CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format); +/* Delete a cJSON entity and all subentities. */ +CJSON_PUBLIC(void) cJSON_Delete(cJSON *item); + +/* Returns the number of items in an array (or object). */ +CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array); +/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */ +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index); +/* Get item "string" from object. Case insensitive. */ +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string); +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string); +CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string); +/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void); + +/* Check item type and return its value */ +CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item); +CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item); + +/* These functions check the type of an item */ +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item); + +/* These calls create a cJSON item of the appropriate type. */ +CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean); +CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num); +CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string); +/* raw json */ +CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw); +CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); + +/* Create a string where valuestring references a string so + * it will not be freed by cJSON_Delete */ +CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string); +/* Create an object/array that only references it's elements so + * they will not be freed by cJSON_Delete */ +CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child); +CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child); + +/* These utilities create an Array of count items. + * The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/ +CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count); + +/* Append item to the specified array/object. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); +/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object. + * WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before + * writing to `item->string` */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); +/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); + +/* Remove/Detach items from Arrays/Objects. */ +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which); +CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string); +CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string); + +/* Update array items. */ +CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */ +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement); +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem); +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem); + +/* Duplicate a cJSON item */ +CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse); +/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will + * need to be released. With recurse!=0, it will duplicate any children connected to the item. + * The item->next and ->prev pointers are always zero on return from Duplicate. */ +/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal. + * case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */ +CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive); + +/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings. + * The input pointer json cannot point to a read-only address area, such as a string constant, + * but should point to a readable and writable address area. */ +CJSON_PUBLIC(void) cJSON_Minify(char *json); + +/* Helper functions for creating and adding items to an object at the same time. + * They return the added item or NULL on failure. */ +CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean); +CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number); +CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string); +CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw); +CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name); + +/* When assigning an integer value, it needs to be propagated to valuedouble too. */ +#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number)) +/* helper for the cJSON_SetNumberValue macro */ +CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number); +#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number)) +/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */ +CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring); + +/* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/ +#define cJSON_SetBoolValue(object, boolValue) ( \ + (object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \ + (object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \ + cJSON_Invalid\ +) + +/* Macro for iterating over an array or object */ +#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next) + +/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */ +CJSON_PUBLIC(void *) cJSON_malloc(size_t size); +CJSON_PUBLIC(void) cJSON_free(void *object); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/common/y3io/impl/websockets/3rdparty/cjson/cJSON_Utils.c b/common/y3io/impl/websockets/3rdparty/cjson/cJSON_Utils.c new file mode 100644 index 0000000..63651df --- /dev/null +++ b/common/y3io/impl/websockets/3rdparty/cjson/cJSON_Utils.c @@ -0,0 +1,1481 @@ +/* + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/* disable warnings about old C89 functions in MSVC */ +#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) +#define _CRT_SECURE_NO_DEPRECATE +#endif + +#ifdef __GNUCC__ +#pragma GCC visibility push(default) +#endif +#if defined(_MSC_VER) +#pragma warning (push) +/* disable warning about single line comments in system headers */ +#pragma warning (disable : 4001) +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(_MSC_VER) +#pragma warning (pop) +#endif +#ifdef __GNUCC__ +#pragma GCC visibility pop +#endif + +#include "cJSON_Utils.h" + +/* define our own boolean type */ +#ifdef true +#undef true +#endif +#define true ((cJSON_bool)1) + +#ifdef false +#undef false +#endif +#define false ((cJSON_bool)0) + +static unsigned char* cJSONUtils_strdup(const unsigned char* const string) +{ + size_t length = 0; + unsigned char *copy = NULL; + + length = strlen((const char*)string) + sizeof(""); + copy = (unsigned char*) cJSON_malloc(length); + if (copy == NULL) + { + return NULL; + } + memcpy(copy, string, length); + + return copy; +} + +/* string comparison which doesn't consider NULL pointers equal */ +static int compare_strings(const unsigned char *string1, const unsigned char *string2, const cJSON_bool case_sensitive) +{ + if ((string1 == NULL) || (string2 == NULL)) + { + return 1; + } + + if (string1 == string2) + { + return 0; + } + + if (case_sensitive) + { + return strcmp((const char*)string1, (const char*)string2); + } + + for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++) + { + if (*string1 == '\0') + { + return 0; + } + } + + return tolower(*string1) - tolower(*string2); +} + +/* securely comparison of floating-point variables */ +static cJSON_bool compare_double(double a, double b) +{ + double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b); + return (fabs(a - b) <= maxVal * DBL_EPSILON); +} + + +/* Compare the next path element of two JSON pointers, two NULL pointers are considered unequal: */ +static cJSON_bool compare_pointers(const unsigned char *name, const unsigned char *pointer, const cJSON_bool case_sensitive) +{ + if ((name == NULL) || (pointer == NULL)) + { + return false; + } + + for (; (*name != '\0') && (*pointer != '\0') && (*pointer != '/'); (void)name++, pointer++) /* compare until next '/' */ + { + if (*pointer == '~') + { + /* check for escaped '~' (~0) and '/' (~1) */ + if (((pointer[1] != '0') || (*name != '~')) && ((pointer[1] != '1') || (*name != '/'))) + { + /* invalid escape sequence or wrong character in *name */ + return false; + } + else + { + pointer++; + } + } + else if ((!case_sensitive && (tolower(*name) != tolower(*pointer))) || (case_sensitive && (*name != *pointer))) + { + return false; + } + } + if (((*pointer != 0) && (*pointer != '/')) != (*name != 0)) + { + /* one string has ended, the other not */ + return false;; + } + + return true; +} + +/* calculate the length of a string if encoded as JSON pointer with ~0 and ~1 escape sequences */ +static size_t pointer_encoded_length(const unsigned char *string) +{ + size_t length; + for (length = 0; *string != '\0'; (void)string++, length++) + { + /* character needs to be escaped? */ + if ((*string == '~') || (*string == '/')) + { + length++; + } + } + + return length; +} + +/* copy a string while escaping '~' and '/' with ~0 and ~1 JSON pointer escape codes */ +static void encode_string_as_pointer(unsigned char *destination, const unsigned char *source) +{ + for (; source[0] != '\0'; (void)source++, destination++) + { + if (source[0] == '/') + { + destination[0] = '~'; + destination[1] = '1'; + destination++; + } + else if (source[0] == '~') + { + destination[0] = '~'; + destination[1] = '0'; + destination++; + } + else + { + destination[0] = source[0]; + } + } + + destination[0] = '\0'; +} + +CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON * const object, const cJSON * const target) +{ + size_t child_index = 0; + cJSON *current_child = 0; + + if ((object == NULL) || (target == NULL)) + { + return NULL; + } + + if (object == target) + { + /* found */ + return (char*)cJSONUtils_strdup((const unsigned char*)""); + } + + /* recursively search all children of the object or array */ + for (current_child = object->child; current_child != NULL; (void)(current_child = current_child->next), child_index++) + { + unsigned char *target_pointer = (unsigned char*)cJSONUtils_FindPointerFromObjectTo(current_child, target); + /* found the target? */ + if (target_pointer != NULL) + { + if (cJSON_IsArray(object)) + { + /* reserve enough memory for a 64 bit integer + '/' and '\0' */ + unsigned char *full_pointer = (unsigned char*)cJSON_malloc(strlen((char*)target_pointer) + 20 + sizeof("/")); + /* check if conversion to unsigned long is valid + * This should be eliminated at compile time by dead code elimination + * if size_t is an alias of unsigned long, or if it is bigger */ + if (child_index > ULONG_MAX) + { + cJSON_free(target_pointer); + cJSON_free(full_pointer); + return NULL; + } + sprintf((char*)full_pointer, "/%lu%s", (unsigned long)child_index, target_pointer); /* / */ + cJSON_free(target_pointer); + + return (char*)full_pointer; + } + + if (cJSON_IsObject(object)) + { + unsigned char *full_pointer = (unsigned char*)cJSON_malloc(strlen((char*)target_pointer) + pointer_encoded_length((unsigned char*)current_child->string) + 2); + full_pointer[0] = '/'; + encode_string_as_pointer(full_pointer + 1, (unsigned char*)current_child->string); + strcat((char*)full_pointer, (char*)target_pointer); + cJSON_free(target_pointer); + + return (char*)full_pointer; + } + + /* reached leaf of the tree, found nothing */ + cJSON_free(target_pointer); + return NULL; + } + } + + /* not found */ + return NULL; +} + +/* non broken version of cJSON_GetArrayItem */ +static cJSON *get_array_item(const cJSON *array, size_t item) +{ + cJSON *child = array ? array->child : NULL; + while ((child != NULL) && (item > 0)) + { + item--; + child = child->next; + } + + return child; +} + +static cJSON_bool decode_array_index_from_pointer(const unsigned char * const pointer, size_t * const index) +{ + size_t parsed_index = 0; + size_t position = 0; + + if ((pointer[0] == '0') && ((pointer[1] != '\0') && (pointer[1] != '/'))) + { + /* leading zeroes are not permitted */ + return 0; + } + + for (position = 0; (pointer[position] >= '0') && (pointer[0] <= '9'); position++) + { + parsed_index = (10 * parsed_index) + (size_t)(pointer[position] - '0'); + + } + + if ((pointer[position] != '\0') && (pointer[position] != '/')) + { + return 0; + } + + *index = parsed_index; + + return 1; +} + +static cJSON *get_item_from_pointer(cJSON * const object, const char * pointer, const cJSON_bool case_sensitive) +{ + cJSON *current_element = object; + + if (pointer == NULL) + { + return NULL; + } + + /* follow path of the pointer */ + while ((pointer[0] == '/') && (current_element != NULL)) + { + pointer++; + if (cJSON_IsArray(current_element)) + { + size_t index = 0; + if (!decode_array_index_from_pointer((const unsigned char*)pointer, &index)) + { + return NULL; + } + + current_element = get_array_item(current_element, index); + } + else if (cJSON_IsObject(current_element)) + { + current_element = current_element->child; + /* GetObjectItem. */ + while ((current_element != NULL) && !compare_pointers((unsigned char*)current_element->string, (const unsigned char*)pointer, case_sensitive)) + { + current_element = current_element->next; + } + } + else + { + return NULL; + } + + /* skip to the next path token or end of string */ + while ((pointer[0] != '\0') && (pointer[0] != '/')) + { + pointer++; + } + } + + return current_element; +} + +CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *pointer) +{ + return get_item_from_pointer(object, pointer, false); +} + +CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointerCaseSensitive(cJSON * const object, const char *pointer) +{ + return get_item_from_pointer(object, pointer, true); +} + +/* JSON Patch implementation. */ +static void decode_pointer_inplace(unsigned char *string) +{ + unsigned char *decoded_string = string; + + if (string == NULL) { + return; + } + + for (; *string; (void)decoded_string++, string++) + { + if (string[0] == '~') + { + if (string[1] == '0') + { + decoded_string[0] = '~'; + } + else if (string[1] == '1') + { + decoded_string[1] = '/'; + } + else + { + /* invalid escape sequence */ + return; + } + + string++; + } + } + + decoded_string[0] = '\0'; +} + +/* non-broken cJSON_DetachItemFromArray */ +static cJSON *detach_item_from_array(cJSON *array, size_t which) +{ + cJSON *c = array->child; + while (c && (which > 0)) + { + c = c->next; + which--; + } + if (!c) + { + /* item doesn't exist */ + return NULL; + } + if (c != array->child) + { + /* not the first element */ + c->prev->next = c->next; + } + if (c->next) + { + c->next->prev = c->prev; + } + if (c == array->child) + { + array->child = c->next; + } + else if (c->next == NULL) + { + array->child->prev = c->prev; + } + /* make sure the detached item doesn't point anywhere anymore */ + c->prev = c->next = NULL; + + return c; +} + +/* detach an item at the given path */ +static cJSON *detach_path(cJSON *object, const unsigned char *path, const cJSON_bool case_sensitive) +{ + unsigned char *parent_pointer = NULL; + unsigned char *child_pointer = NULL; + cJSON *parent = NULL; + cJSON *detached_item = NULL; + + /* copy path and split it in parent and child */ + parent_pointer = cJSONUtils_strdup(path); + if (parent_pointer == NULL) { + goto cleanup; + } + + child_pointer = (unsigned char*)strrchr((char*)parent_pointer, '/'); /* last '/' */ + if (child_pointer == NULL) + { + goto cleanup; + } + /* split strings */ + child_pointer[0] = '\0'; + child_pointer++; + + parent = get_item_from_pointer(object, (char*)parent_pointer, case_sensitive); + decode_pointer_inplace(child_pointer); + + if (cJSON_IsArray(parent)) + { + size_t index = 0; + if (!decode_array_index_from_pointer(child_pointer, &index)) + { + goto cleanup; + } + detached_item = detach_item_from_array(parent, index); + } + else if (cJSON_IsObject(parent)) + { + detached_item = cJSON_DetachItemFromObject(parent, (char*)child_pointer); + } + else + { + /* Couldn't find object to remove child from. */ + goto cleanup; + } + +cleanup: + if (parent_pointer != NULL) + { + cJSON_free(parent_pointer); + } + + return detached_item; +} + +/* sort lists using mergesort */ +static cJSON *sort_list(cJSON *list, const cJSON_bool case_sensitive) +{ + cJSON *first = list; + cJSON *second = list; + cJSON *current_item = list; + cJSON *result = list; + cJSON *result_tail = NULL; + + if ((list == NULL) || (list->next == NULL)) + { + /* One entry is sorted already. */ + return result; + } + + while ((current_item != NULL) && (current_item->next != NULL) && (compare_strings((unsigned char*)current_item->string, (unsigned char*)current_item->next->string, case_sensitive) < 0)) + { + /* Test for list sorted. */ + current_item = current_item->next; + } + if ((current_item == NULL) || (current_item->next == NULL)) + { + /* Leave sorted lists unmodified. */ + return result; + } + + /* reset pointer to the beginning */ + current_item = list; + while (current_item != NULL) + { + /* Walk two pointers to find the middle. */ + second = second->next; + current_item = current_item->next; + /* advances current_item two steps at a time */ + if (current_item != NULL) + { + current_item = current_item->next; + } + } + if ((second != NULL) && (second->prev != NULL)) + { + /* Split the lists */ + second->prev->next = NULL; + second->prev = NULL; + } + + /* Recursively sort the sub-lists. */ + first = sort_list(first, case_sensitive); + second = sort_list(second, case_sensitive); + result = NULL; + + /* Merge the sub-lists */ + while ((first != NULL) && (second != NULL)) + { + cJSON *smaller = NULL; + if (compare_strings((unsigned char*)first->string, (unsigned char*)second->string, case_sensitive) < 0) + { + smaller = first; + } + else + { + smaller = second; + } + + if (result == NULL) + { + /* start merged list with the smaller element */ + result_tail = smaller; + result = smaller; + } + else + { + /* add smaller element to the list */ + result_tail->next = smaller; + smaller->prev = result_tail; + result_tail = smaller; + } + + if (first == smaller) + { + first = first->next; + } + else + { + second = second->next; + } + } + + if (first != NULL) + { + /* Append rest of first list. */ + if (result == NULL) + { + return first; + } + result_tail->next = first; + first->prev = result_tail; + } + if (second != NULL) + { + /* Append rest of second list */ + if (result == NULL) + { + return second; + } + result_tail->next = second; + second->prev = result_tail; + } + + return result; +} + +static void sort_object(cJSON * const object, const cJSON_bool case_sensitive) +{ + if (object == NULL) + { + return; + } + object->child = sort_list(object->child, case_sensitive); +} + +static cJSON_bool compare_json(cJSON *a, cJSON *b, const cJSON_bool case_sensitive) +{ + if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF))) + { + /* mismatched type. */ + return false; + } + switch (a->type & 0xFF) + { + case cJSON_Number: + /* numeric mismatch. */ + if ((a->valueint != b->valueint) || (!compare_double(a->valuedouble, b->valuedouble))) + { + return false; + } + else + { + return true; + } + + case cJSON_String: + /* string mismatch. */ + if (strcmp(a->valuestring, b->valuestring) != 0) + { + return false; + } + else + { + return true; + } + + case cJSON_Array: + for ((void)(a = a->child), b = b->child; (a != NULL) && (b != NULL); (void)(a = a->next), b = b->next) + { + cJSON_bool identical = compare_json(a, b, case_sensitive); + if (!identical) + { + return false; + } + } + + /* array size mismatch? (one of both children is not NULL) */ + if ((a != NULL) || (b != NULL)) + { + return false; + } + else + { + return true; + } + + case cJSON_Object: + sort_object(a, case_sensitive); + sort_object(b, case_sensitive); + for ((void)(a = a->child), b = b->child; (a != NULL) && (b != NULL); (void)(a = a->next), b = b->next) + { + cJSON_bool identical = false; + /* compare object keys */ + if (compare_strings((unsigned char*)a->string, (unsigned char*)b->string, case_sensitive)) + { + /* missing member */ + return false; + } + identical = compare_json(a, b, case_sensitive); + if (!identical) + { + return false; + } + } + + /* object length mismatch (one of both children is not null) */ + if ((a != NULL) || (b != NULL)) + { + return false; + } + else + { + return true; + } + + default: + break; + } + + /* null, true or false */ + return true; +} + +/* non broken version of cJSON_InsertItemInArray */ +static cJSON_bool insert_item_in_array(cJSON *array, size_t which, cJSON *newitem) +{ + cJSON *child = array->child; + while (child && (which > 0)) + { + child = child->next; + which--; + } + if (which > 0) + { + /* item is after the end of the array */ + return 0; + } + if (child == NULL) + { + cJSON_AddItemToArray(array, newitem); + return 1; + } + + /* insert into the linked list */ + newitem->next = child; + newitem->prev = child->prev; + child->prev = newitem; + + /* was it at the beginning */ + if (child == array->child) + { + array->child = newitem; + } + else + { + newitem->prev->next = newitem; + } + + return 1; +} + +static cJSON *get_object_item(const cJSON * const object, const char* name, const cJSON_bool case_sensitive) +{ + if (case_sensitive) + { + return cJSON_GetObjectItemCaseSensitive(object, name); + } + + return cJSON_GetObjectItem(object, name); +} + +enum patch_operation { INVALID, ADD, REMOVE, REPLACE, MOVE, COPY, TEST }; + +static enum patch_operation decode_patch_operation(const cJSON * const patch, const cJSON_bool case_sensitive) +{ + cJSON *operation = get_object_item(patch, "op", case_sensitive); + if (!cJSON_IsString(operation)) + { + return INVALID; + } + + if (strcmp(operation->valuestring, "add") == 0) + { + return ADD; + } + + if (strcmp(operation->valuestring, "remove") == 0) + { + return REMOVE; + } + + if (strcmp(operation->valuestring, "replace") == 0) + { + return REPLACE; + } + + if (strcmp(operation->valuestring, "move") == 0) + { + return MOVE; + } + + if (strcmp(operation->valuestring, "copy") == 0) + { + return COPY; + } + + if (strcmp(operation->valuestring, "test") == 0) + { + return TEST; + } + + return INVALID; +} + +/* overwrite and existing item with another one and free resources on the way */ +static void overwrite_item(cJSON * const root, const cJSON replacement) +{ + if (root == NULL) + { + return; + } + + if (root->string != NULL) + { + cJSON_free(root->string); + } + if (root->valuestring != NULL) + { + cJSON_free(root->valuestring); + } + if (root->child != NULL) + { + cJSON_Delete(root->child); + } + + memcpy(root, &replacement, sizeof(cJSON)); +} + +static int apply_patch(cJSON *object, const cJSON *patch, const cJSON_bool case_sensitive) +{ + cJSON *path = NULL; + cJSON *value = NULL; + cJSON *parent = NULL; + enum patch_operation opcode = INVALID; + unsigned char *parent_pointer = NULL; + unsigned char *child_pointer = NULL; + int status = 0; + + path = get_object_item(patch, "path", case_sensitive); + if (!cJSON_IsString(path)) + { + /* malformed patch. */ + status = 2; + goto cleanup; + } + + opcode = decode_patch_operation(patch, case_sensitive); + if (opcode == INVALID) + { + status = 3; + goto cleanup; + } + else if (opcode == TEST) + { + /* compare value: {...} with the given path */ + status = !compare_json(get_item_from_pointer(object, path->valuestring, case_sensitive), get_object_item(patch, "value", case_sensitive), case_sensitive); + goto cleanup; + } + + /* special case for replacing the root */ + if (path->valuestring[0] == '\0') + { + if (opcode == REMOVE) + { + static const cJSON invalid = { NULL, NULL, NULL, cJSON_Invalid, NULL, 0, 0, NULL}; + + overwrite_item(object, invalid); + + status = 0; + goto cleanup; + } + + if ((opcode == REPLACE) || (opcode == ADD)) + { + value = get_object_item(patch, "value", case_sensitive); + if (value == NULL) + { + /* missing "value" for add/replace. */ + status = 7; + goto cleanup; + } + + value = cJSON_Duplicate(value, 1); + if (value == NULL) + { + /* out of memory for add/replace. */ + status = 8; + goto cleanup; + } + + overwrite_item(object, *value); + + /* delete the duplicated value */ + cJSON_free(value); + value = NULL; + + /* the string "value" isn't needed */ + if (object->string != NULL) + { + cJSON_free(object->string); + object->string = NULL; + } + + status = 0; + goto cleanup; + } + } + + if ((opcode == REMOVE) || (opcode == REPLACE)) + { + /* Get rid of old. */ + cJSON *old_item = detach_path(object, (unsigned char*)path->valuestring, case_sensitive); + if (old_item == NULL) + { + status = 13; + goto cleanup; + } + cJSON_Delete(old_item); + if (opcode == REMOVE) + { + /* For Remove, this job is done. */ + status = 0; + goto cleanup; + } + } + + /* Copy/Move uses "from". */ + if ((opcode == MOVE) || (opcode == COPY)) + { + cJSON *from = get_object_item(patch, "from", case_sensitive); + if (from == NULL) + { + /* missing "from" for copy/move. */ + status = 4; + goto cleanup; + } + + if (opcode == MOVE) + { + value = detach_path(object, (unsigned char*)from->valuestring, case_sensitive); + } + if (opcode == COPY) + { + value = get_item_from_pointer(object, from->valuestring, case_sensitive); + } + if (value == NULL) + { + /* missing "from" for copy/move. */ + status = 5; + goto cleanup; + } + if (opcode == COPY) + { + value = cJSON_Duplicate(value, 1); + } + if (value == NULL) + { + /* out of memory for copy/move. */ + status = 6; + goto cleanup; + } + } + else /* Add/Replace uses "value". */ + { + value = get_object_item(patch, "value", case_sensitive); + if (value == NULL) + { + /* missing "value" for add/replace. */ + status = 7; + goto cleanup; + } + value = cJSON_Duplicate(value, 1); + if (value == NULL) + { + /* out of memory for add/replace. */ + status = 8; + goto cleanup; + } + } + + /* Now, just add "value" to "path". */ + + /* split pointer in parent and child */ + parent_pointer = cJSONUtils_strdup((unsigned char*)path->valuestring); + if (parent_pointer) { + child_pointer = (unsigned char*)strrchr((char*)parent_pointer, '/'); + } + if (child_pointer != NULL) + { + child_pointer[0] = '\0'; + child_pointer++; + } + parent = get_item_from_pointer(object, (char*)parent_pointer, case_sensitive); + decode_pointer_inplace(child_pointer); + + /* add, remove, replace, move, copy, test. */ + if ((parent == NULL) || (child_pointer == NULL)) + { + /* Couldn't find object to add to. */ + status = 9; + goto cleanup; + } + else if (cJSON_IsArray(parent)) + { + if (strcmp((char*)child_pointer, "-") == 0) + { + cJSON_AddItemToArray(parent, value); + value = NULL; + } + else + { + size_t index = 0; + if (!decode_array_index_from_pointer(child_pointer, &index)) + { + status = 11; + goto cleanup; + } + + if (!insert_item_in_array(parent, index, value)) + { + status = 10; + goto cleanup; + } + value = NULL; + } + } + else if (cJSON_IsObject(parent)) + { + if (case_sensitive) + { + cJSON_DeleteItemFromObjectCaseSensitive(parent, (char*)child_pointer); + } + else + { + cJSON_DeleteItemFromObject(parent, (char*)child_pointer); + } + cJSON_AddItemToObject(parent, (char*)child_pointer, value); + value = NULL; + } + else /* parent is not an object */ + { + /* Couldn't find object to add to. */ + status = 9; + goto cleanup; + } + +cleanup: + if (value != NULL) + { + cJSON_Delete(value); + } + if (parent_pointer != NULL) + { + cJSON_free(parent_pointer); + } + + return status; +} + +CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON * const object, const cJSON * const patches) +{ + const cJSON *current_patch = NULL; + int status = 0; + + if (!cJSON_IsArray(patches)) + { + /* malformed patches. */ + return 1; + } + + if (patches != NULL) + { + current_patch = patches->child; + } + + while (current_patch != NULL) + { + status = apply_patch(object, current_patch, false); + if (status != 0) + { + return status; + } + current_patch = current_patch->next; + } + + return 0; +} + +CJSON_PUBLIC(int) cJSONUtils_ApplyPatchesCaseSensitive(cJSON * const object, const cJSON * const patches) +{ + const cJSON *current_patch = NULL; + int status = 0; + + if (!cJSON_IsArray(patches)) + { + /* malformed patches. */ + return 1; + } + + if (patches != NULL) + { + current_patch = patches->child; + } + + while (current_patch != NULL) + { + status = apply_patch(object, current_patch, true); + if (status != 0) + { + return status; + } + current_patch = current_patch->next; + } + + return 0; +} + +static void compose_patch(cJSON * const patches, const unsigned char * const operation, const unsigned char * const path, const unsigned char *suffix, const cJSON * const value) +{ + cJSON *patch = NULL; + + if ((patches == NULL) || (operation == NULL) || (path == NULL)) + { + return; + } + + patch = cJSON_CreateObject(); + if (patch == NULL) + { + return; + } + cJSON_AddItemToObject(patch, "op", cJSON_CreateString((const char*)operation)); + + if (suffix == NULL) + { + cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)path)); + } + else + { + size_t suffix_length = pointer_encoded_length(suffix); + size_t path_length = strlen((const char*)path); + unsigned char *full_path = (unsigned char*)cJSON_malloc(path_length + suffix_length + sizeof("/")); + + sprintf((char*)full_path, "%s/", (const char*)path); + encode_string_as_pointer(full_path + path_length + 1, suffix); + + cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)full_path)); + cJSON_free(full_path); + } + + if (value != NULL) + { + cJSON_AddItemToObject(patch, "value", cJSON_Duplicate(value, 1)); + } + cJSON_AddItemToArray(patches, patch); +} + +CJSON_PUBLIC(void) cJSONUtils_AddPatchToArray(cJSON * const array, const char * const operation, const char * const path, const cJSON * const value) +{ + compose_patch(array, (const unsigned char*)operation, (const unsigned char*)path, NULL, value); +} + +static void create_patches(cJSON * const patches, const unsigned char * const path, cJSON * const from, cJSON * const to, const cJSON_bool case_sensitive) +{ + if ((from == NULL) || (to == NULL)) + { + return; + } + + if ((from->type & 0xFF) != (to->type & 0xFF)) + { + compose_patch(patches, (const unsigned char*)"replace", path, 0, to); + return; + } + + switch (from->type & 0xFF) + { + case cJSON_Number: + if ((from->valueint != to->valueint) || !compare_double(from->valuedouble, to->valuedouble)) + { + compose_patch(patches, (const unsigned char*)"replace", path, NULL, to); + } + return; + + case cJSON_String: + if (strcmp(from->valuestring, to->valuestring) != 0) + { + compose_patch(patches, (const unsigned char*)"replace", path, NULL, to); + } + return; + + case cJSON_Array: + { + size_t index = 0; + cJSON *from_child = from->child; + cJSON *to_child = to->child; + unsigned char *new_path = (unsigned char*)cJSON_malloc(strlen((const char*)path) + 20 + sizeof("/")); /* Allow space for 64bit int. log10(2^64) = 20 */ + + /* generate patches for all array elements that exist in both "from" and "to" */ + for (index = 0; (from_child != NULL) && (to_child != NULL); (void)(from_child = from_child->next), (void)(to_child = to_child->next), index++) + { + /* check if conversion to unsigned long is valid + * This should be eliminated at compile time by dead code elimination + * if size_t is an alias of unsigned long, or if it is bigger */ + if (index > ULONG_MAX) + { + cJSON_free(new_path); + return; + } + sprintf((char*)new_path, "%s/%lu", path, (unsigned long)index); /* path of the current array element */ + create_patches(patches, new_path, from_child, to_child, case_sensitive); + } + + /* remove leftover elements from 'from' that are not in 'to' */ + for (; (from_child != NULL); (void)(from_child = from_child->next)) + { + /* check if conversion to unsigned long is valid + * This should be eliminated at compile time by dead code elimination + * if size_t is an alias of unsigned long, or if it is bigger */ + if (index > ULONG_MAX) + { + cJSON_free(new_path); + return; + } + sprintf((char*)new_path, "%lu", (unsigned long)index); + compose_patch(patches, (const unsigned char*)"remove", path, new_path, NULL); + } + /* add new elements in 'to' that were not in 'from' */ + for (; (to_child != NULL); (void)(to_child = to_child->next), index++) + { + compose_patch(patches, (const unsigned char*)"add", path, (const unsigned char*)"-", to_child); + } + cJSON_free(new_path); + return; + } + + case cJSON_Object: + { + cJSON *from_child = NULL; + cJSON *to_child = NULL; + sort_object(from, case_sensitive); + sort_object(to, case_sensitive); + + from_child = from->child; + to_child = to->child; + /* for all object values in the object with more of them */ + while ((from_child != NULL) || (to_child != NULL)) + { + int diff; + if (from_child == NULL) + { + diff = 1; + } + else if (to_child == NULL) + { + diff = -1; + } + else + { + diff = compare_strings((unsigned char*)from_child->string, (unsigned char*)to_child->string, case_sensitive); + } + + if (diff == 0) + { + /* both object keys are the same */ + size_t path_length = strlen((const char*)path); + size_t from_child_name_length = pointer_encoded_length((unsigned char*)from_child->string); + unsigned char *new_path = (unsigned char*)cJSON_malloc(path_length + from_child_name_length + sizeof("/")); + + sprintf((char*)new_path, "%s/", path); + encode_string_as_pointer(new_path + path_length + 1, (unsigned char*)from_child->string); + + /* create a patch for the element */ + create_patches(patches, new_path, from_child, to_child, case_sensitive); + cJSON_free(new_path); + + from_child = from_child->next; + to_child = to_child->next; + } + else if (diff < 0) + { + /* object element doesn't exist in 'to' --> remove it */ + compose_patch(patches, (const unsigned char*)"remove", path, (unsigned char*)from_child->string, NULL); + + from_child = from_child->next; + } + else + { + /* object element doesn't exist in 'from' --> add it */ + compose_patch(patches, (const unsigned char*)"add", path, (unsigned char*)to_child->string, to_child); + + to_child = to_child->next; + } + } + return; + } + + default: + break; + } +} + +CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatches(cJSON * const from, cJSON * const to) +{ + cJSON *patches = NULL; + + if ((from == NULL) || (to == NULL)) + { + return NULL; + } + + patches = cJSON_CreateArray(); + create_patches(patches, (const unsigned char*)"", from, to, false); + + return patches; +} + +CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatchesCaseSensitive(cJSON * const from, cJSON * const to) +{ + cJSON *patches = NULL; + + if ((from == NULL) || (to == NULL)) + { + return NULL; + } + + patches = cJSON_CreateArray(); + create_patches(patches, (const unsigned char*)"", from, to, true); + + return patches; +} + +CJSON_PUBLIC(void) cJSONUtils_SortObject(cJSON * const object) +{ + sort_object(object, false); +} + +CJSON_PUBLIC(void) cJSONUtils_SortObjectCaseSensitive(cJSON * const object) +{ + sort_object(object, true); +} + +static cJSON *merge_patch(cJSON *target, const cJSON * const patch, const cJSON_bool case_sensitive) +{ + cJSON *patch_child = NULL; + + if (!cJSON_IsObject(patch)) + { + /* scalar value, array or NULL, just duplicate */ + cJSON_Delete(target); + return cJSON_Duplicate(patch, 1); + } + + if (!cJSON_IsObject(target)) + { + cJSON_Delete(target); + target = cJSON_CreateObject(); + } + + patch_child = patch->child; + while (patch_child != NULL) + { + if (cJSON_IsNull(patch_child)) + { + /* NULL is the indicator to remove a value, see RFC7396 */ + if (case_sensitive) + { + cJSON_DeleteItemFromObjectCaseSensitive(target, patch_child->string); + } + else + { + cJSON_DeleteItemFromObject(target, patch_child->string); + } + } + else + { + cJSON *replace_me = NULL; + cJSON *replacement = NULL; + + if (case_sensitive) + { + replace_me = cJSON_DetachItemFromObjectCaseSensitive(target, patch_child->string); + } + else + { + replace_me = cJSON_DetachItemFromObject(target, patch_child->string); + } + + replacement = merge_patch(replace_me, patch_child, case_sensitive); + if (replacement == NULL) + { + cJSON_Delete(target); + return NULL; + } + + cJSON_AddItemToObject(target, patch_child->string, replacement); + } + patch_child = patch_child->next; + } + return target; +} + +CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, const cJSON * const patch) +{ + return merge_patch(target, patch, false); +} + +CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatchCaseSensitive(cJSON *target, const cJSON * const patch) +{ + return merge_patch(target, patch, true); +} + +static cJSON *generate_merge_patch(cJSON * const from, cJSON * const to, const cJSON_bool case_sensitive) +{ + cJSON *from_child = NULL; + cJSON *to_child = NULL; + cJSON *patch = NULL; + if (to == NULL) + { + /* patch to delete everything */ + return cJSON_CreateNull(); + } + if (!cJSON_IsObject(to) || !cJSON_IsObject(from)) + { + return cJSON_Duplicate(to, 1); + } + + sort_object(from, case_sensitive); + sort_object(to, case_sensitive); + + from_child = from->child; + to_child = to->child; + patch = cJSON_CreateObject(); + if (patch == NULL) + { + return NULL; + } + while (from_child || to_child) + { + int diff; + if (from_child != NULL) + { + if (to_child != NULL) + { + diff = strcmp(from_child->string, to_child->string); + } + else + { + diff = -1; + } + } + else + { + diff = 1; + } + + if (diff < 0) + { + /* from has a value that to doesn't have -> remove */ + cJSON_AddItemToObject(patch, from_child->string, cJSON_CreateNull()); + + from_child = from_child->next; + } + else if (diff > 0) + { + /* to has a value that from doesn't have -> add to patch */ + cJSON_AddItemToObject(patch, to_child->string, cJSON_Duplicate(to_child, 1)); + + to_child = to_child->next; + } + else + { + /* object key exists in both objects */ + if (!compare_json(from_child, to_child, case_sensitive)) + { + /* not identical --> generate a patch */ + cJSON_AddItemToObject(patch, to_child->string, cJSONUtils_GenerateMergePatch(from_child, to_child)); + } + + /* next key in the object */ + from_child = from_child->next; + to_child = to_child->next; + } + } + if (patch->child == NULL) + { + /* no patch generated */ + cJSON_Delete(patch); + return NULL; + } + + return patch; +} + +CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatch(cJSON * const from, cJSON * const to) +{ + return generate_merge_patch(from, to, false); +} + +CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatchCaseSensitive(cJSON * const from, cJSON * const to) +{ + return generate_merge_patch(from, to, true); +} diff --git a/common/y3io/impl/websockets/3rdparty/cjson/cJSON_Utils.h b/common/y3io/impl/websockets/3rdparty/cjson/cJSON_Utils.h new file mode 100644 index 0000000..a970c65 --- /dev/null +++ b/common/y3io/impl/websockets/3rdparty/cjson/cJSON_Utils.h @@ -0,0 +1,88 @@ +/* + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef cJSON_Utils__h +#define cJSON_Utils__h + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "cJSON.h" + +/* Implement RFC6901 (https://tools.ietf.org/html/rfc6901) JSON Pointer spec. */ +CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *pointer); +CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointerCaseSensitive(cJSON * const object, const char *pointer); + +/* Implement RFC6902 (https://tools.ietf.org/html/rfc6902) JSON Patch spec. */ +/* NOTE: This modifies objects in 'from' and 'to' by sorting the elements by their key */ +CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatches(cJSON * const from, cJSON * const to); +CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatchesCaseSensitive(cJSON * const from, cJSON * const to); +/* Utility for generating patch array entries. */ +CJSON_PUBLIC(void) cJSONUtils_AddPatchToArray(cJSON * const array, const char * const operation, const char * const path, const cJSON * const value); +/* Returns 0 for success. */ +CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON * const object, const cJSON * const patches); +CJSON_PUBLIC(int) cJSONUtils_ApplyPatchesCaseSensitive(cJSON * const object, const cJSON * const patches); + +/* +// Note that ApplyPatches is NOT atomic on failure. To implement an atomic ApplyPatches, use: +//int cJSONUtils_AtomicApplyPatches(cJSON **object, cJSON *patches) +//{ +// cJSON *modme = cJSON_Duplicate(*object, 1); +// int error = cJSONUtils_ApplyPatches(modme, patches); +// if (!error) +// { +// cJSON_Delete(*object); +// *object = modme; +// } +// else +// { +// cJSON_Delete(modme); +// } +// +// return error; +//} +// Code not added to library since this strategy is a LOT slower. +*/ + +/* Implement RFC7386 (https://tools.ietf.org/html/rfc7396) JSON Merge Patch spec. */ +/* target will be modified by patch. return value is new ptr for target. */ +CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, const cJSON * const patch); +CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatchCaseSensitive(cJSON *target, const cJSON * const patch); +/* generates a patch to move from -> to */ +/* NOTE: This modifies objects in 'from' and 'to' by sorting the elements by their key */ +CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatch(cJSON * const from, cJSON * const to); +CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatchCaseSensitive(cJSON * const from, cJSON * const to); + +/* Given a root object and a target object, construct a pointer from one to the other. */ +CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON * const object, const cJSON * const target); + +/* Sorts the members of the object into alphabetical order. */ +CJSON_PUBLIC(void) cJSONUtils_SortObject(cJSON * const object); +CJSON_PUBLIC(void) cJSONUtils_SortObjectCaseSensitive(cJSON * const object); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/common/y3io/impl/websockets/config.c b/common/y3io/impl/websockets/config.c new file mode 100644 index 0000000..84f0127 --- /dev/null +++ b/common/y3io/impl/websockets/config.c @@ -0,0 +1,39 @@ +#include + +#include +#include +#include +#include + +#include "config.h" + +void y3ws_config_load(struct y3ws_config *cfg, const wchar_t *filename) +{ + assert(cfg != NULL); + assert(filename != NULL); + + cfg->enable = GetPrivateProfileIntW(L"y3ws", L"enable", 1, filename); + cfg->debug = GetPrivateProfileIntW(L"y3ws", L"debug", 0, filename); + + cfg->port = GetPrivateProfileIntW(L"y3ws", L"port", 3594, filename); + + wchar_t tmpstr[16]; + + GetPrivateProfileStringW( + L"y3ws", + L"gameId", + L"SDEY", + tmpstr, + _countof(tmpstr), + filename); + + wcstombs(cfg->game_id, tmpstr, sizeof(cfg->game_id)); + + GetPrivateProfileStringW( + L"y3ws", + L"cardDirectory", + L"DEVICE\\print", + cfg->card_path, + _countof(cfg->card_path), + filename); +} diff --git a/common/y3io/impl/websockets/config.h b/common/y3io/impl/websockets/config.h new file mode 100644 index 0000000..91ad18f --- /dev/null +++ b/common/y3io/impl/websockets/config.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +#include "hooklib/dvd.h" +#include "hooklib/touch.h" +#include "hooklib/printer_chc.h" +#include "hooklib/printer_cx.h" + +struct y3ws_config { + bool enable; + bool debug; + + uint16_t port; + char game_id[5]; + wchar_t card_path[MAX_PATH]; +}; + +void y3ws_config_load(struct y3ws_config *cfg, const wchar_t *filename); \ No newline at end of file diff --git a/common/y3io/impl/websockets/y3ws.c b/common/y3io/impl/websockets/y3ws.c new file mode 100644 index 0000000..318f8aa --- /dev/null +++ b/common/y3io/impl/websockets/y3ws.c @@ -0,0 +1,354 @@ +#include +#include +#include +#include +#include +#include + +#include "3rdparty/cjson/cJSON.h" + +#include "config.h" +#include "y3ws.h" + +#include + +#include "hooklib/y3.h" + +#include "winwebsocket.h" +#include "lib/base64.h" + +#include "util/dprintf.h" +#include "util/env.h" + +static void onopen(struct wws_connection*); +static void onclose(struct wws_connection*); +static void onmessage(struct wws_connection*, const char*, size_t); +static void onlog(const char*, ...); + +#define PROTOCOL_VERSION 1 +#define GAME_MAX_CARDS 32 +#define MAX_CARDS 500 +#define CARD_ID_LEN 5 +#define OUTPUT_BUFFER_SIZE (1024 * 1024 * 5) + +static struct y3ws_config cfg; + +static bool is_initialized = false; +static struct CardInfo card_info[GAME_MAX_CARDS]; +static int card_info_size = 0; +static CRITICAL_SECTION card_info_lock; + +#pragma region y3-dll functions +uint16_t y3_io_get_api_version() { + return 0x0100; +} + +HRESULT y3_io_init() { + if (is_initialized) { + return S_FALSE; + } + + y3ws_config_load(&cfg, get_config_path()); + + memset(card_info, 0, sizeof(card_info)); + InitializeCriticalSection(&card_info_lock); + + wws_set_callbacks(onopen, onclose, onmessage, onlog); + wws_set_verbose(cfg.debug); + + HRESULT hr = wws_start(cfg.port); + + if (SUCCEEDED(hr)) { + dprintf("Y3WS: Started server on port %d\n", cfg.port); + if (cfg.debug) { + dprintf("Y3WS: WS debug logging is on\n"); + } + is_initialized = true; + } else { + dprintf("Y3WS: Error starting server: %lx\n", hr); + } + + return hr; +} + +HRESULT y3_io_close() { + + HRESULT hr = wws_stop(); + + DeleteCriticalSection(&card_info_lock); + + is_initialized = false; + + return hr; +} + +HRESULT y3_io_get_cards(struct CardInfo* pCardInfo, int* numCards) { + EnterCriticalSection(&card_info_lock); + for (int i = 0; i < card_info_size; i++) { + memcpy(&pCardInfo[i], &card_info[i], sizeof(struct CardInfo)); + } + *numCards = card_info_size; + LeaveCriticalSection(&card_info_lock); + return S_OK; +} +#pragma endregion + +#pragma region y3ws functions +static void y3ws_make_error_packet(const char* message, char* output_data, size_t* output_size) { + dprintf("Y3WS: Error: %s\n", message); + *output_size = sprintf_s((char*)output_data, *output_size, "{\"version\":%d, \"success\":false,\"error\":\"%s\"}", PROTOCOL_VERSION, message); +} + +static void y3ws_make_success_packet(char* output_data, size_t* output_size) { + *output_size = sprintf_s((char*)output_data, *output_size, "{\"version\":%d, \"success\":true}", PROTOCOL_VERSION); +} + +static void y3ws_read_cards(char* output_data, size_t* output_size) { + WIN32_FIND_DATAW ffd; + + wchar_t path[MAX_PATH]; + swprintf(path, MAX_PATH, L"%ls\\*", cfg.card_path); + + HANDLE hFind = FindFirstFileW(path, &ffd); + if (INVALID_HANDLE_VALUE == hFind) { + dprintf("Y3WS: Failed to access directory: %ls (%ld)\n", path, GetLastError()); + y3ws_make_error_packet("Could not read from card directory", output_data, output_size); + return; + } + + cJSON* response = cJSON_CreateObject(); + cJSON* pversion = cJSON_CreateNumber(PROTOCOL_VERSION); + cJSON_AddItemToObject(response, "version", pversion); + cJSON* success = cJSON_CreateBool(true); + cJSON_AddItemToObject(response, "success", success); + cJSON* cards = cJSON_CreateArray(); + cJSON_AddItemToObject(response, "cards", cards); + + int count = 0; + do { + if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + continue; + } + + swprintf(path, MAX_PATH, L"%ls\\%ls", cfg.card_path, ffd.cFileName); + + HANDLE hImage = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (hImage == INVALID_HANDLE_VALUE) { + dprintf("Y3WS: Error opening %ls: %ld\n", path, GetLastError()); + continue; + } + DWORD ret = SetFilePointer(hImage, -CARD_ID_LEN, NULL, FILE_END); + if (ret == INVALID_SET_FILE_POINTER) { + dprintf("Y3WS: Error seeking in %ls: %ld\n", path, GetLastError()); + continue; + } + uint8_t buf[CARD_ID_LEN]; + DWORD bytesRead = 0; + if (!ReadFile(hImage, &buf, CARD_ID_LEN, &bytesRead, NULL) || bytesRead != CARD_ID_LEN) { + dprintf("Y3WS: Error reading card ID from %ls: %ld\n", path, GetLastError()); + continue; + } + + cJSON* card = cJSON_CreateObject(); + cJSON_AddItemToArray(cards, card); + + // cJSON isn't wide... + char fpatha[MAX_PATH]; + size_t fpatha_len = 0; + wcstombs_s(&fpatha_len, fpatha, MAX_PATH, ffd.cFileName, MAX_PATH); + fpatha[fpatha_len] = 0; + + // ReSharper disable once CppRedundantCastExpression + cJSON* path_str = cJSON_CreateString(fpatha); + cJSON_AddItemToObject(card, "path", path_str); + cJSON* card_id = cJSON_CreateNumber((double)((uint64_t)buf[0] >> 32 | (uint64_t)buf[1] >> 24 | (uint64_t)buf[2] >> 16 | (uint64_t)buf[3] >> 8 | buf[4])); + cJSON_AddItemToObject(card, "card_id", card_id); + + } while (FindNextFileW(hFind, &ffd) != 0 && count++ < MAX_CARDS); + + cJSON_PrintPreallocated(response, output_data, (int)*output_size, false); + *output_size = strlen(output_data); + + dprintf("Y3WS: Sent %d card(s) to the client\n", count); + + cJSON_Delete(response); + FindClose(hFind); +} + +static void y3ws_read_card_image(char* file_name, char* output_data, size_t* output_size) { + wchar_t path[MAX_PATH]; + swprintf(path, MAX_PATH, L"%ls\\%hs", cfg.card_path, file_name); + + HANDLE hImage = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (hImage == INVALID_HANDLE_VALUE) { + dprintf("Y3WS: Failed to access file: %ls (%ld)\n", path, GetLastError()); + y3ws_make_error_packet("Could not read file", output_data, output_size); + return; + } + + DWORD size = GetFileSize(hImage, NULL); + if (size == INVALID_FILE_SIZE) { + dprintf("Y3WS: Failed to access file size: %ls (%ld)\n", path, GetLastError()); + y3ws_make_error_packet("Could not read file", output_data, output_size); + return; + } + + uint8_t* buf = malloc(size); + size_t b64size = ((size * 4) / 3) + (size / 96) + 6; + char* b64buf = malloc(b64size); + if (buf == NULL) { + dprintf("Y3WS: Allocation error for file: %ls (%ld)\n", path, GetLastError()); + y3ws_make_error_packet("Could not read file", output_data, output_size); + goto end; + } + + DWORD bytesRead = 0; + if (!ReadFile(hImage, buf, size, &bytesRead, NULL) || bytesRead != size) { + dprintf("Y3WS: File read failed: %ls (%ld)\n", path, GetLastError()); + y3ws_make_error_packet("Could not read file", output_data, output_size); + goto end; + } + + b64size = base64_encode(buf, size, b64buf); + + if (b64size + 100 > *output_size) { + dprintf("Y3WS: Encoded file size exceeds buffer: %ls (%llu / %llu)\n", path, (uint64_t) b64size, (uint64_t) *output_size); + y3ws_make_error_packet("File too large", output_data, output_size); + } + + *output_size = sprintf_s((char*)output_data, *output_size, "{\"version\":%d, \"success\":true,\"data\":\"%s\"}", PROTOCOL_VERSION, (char*)b64buf); +end: + free(buf); + free(b64buf); +} + +static void y3ws_set_cards_from_json(const cJSON* cards, char* output_data, size_t* output_size) { + + const cJSON* card = NULL; + card_info_size = 0; + + EnterCriticalSection(&card_info_lock); + memset(card_info, 0, sizeof(card_info)); + cJSON_ArrayForEach(card, cards) { + cJSON* x = cJSON_GetObjectItemCaseSensitive(card, "x"); + cJSON* y = cJSON_GetObjectItemCaseSensitive(card, "y"); + cJSON* r = cJSON_GetObjectItemCaseSensitive(card, "rotation"); + cJSON* id = cJSON_GetObjectItemCaseSensitive(card, "card_id"); + + if (cJSON_IsNumber(x) && cJSON_IsNumber(y) && cJSON_IsNumber(r) && cJSON_IsNumber(id)) { + card_info[card_info_size].eCardStatus = VALID; + card_info[card_info_size].fX = (float)x->valuedouble; + card_info[card_info_size].fY = (float)y->valuedouble; + card_info[card_info_size].fAngle = (float)r->valuedouble; + card_info[card_info_size].uID = id->valueint; + } else { + dprintf("Y3WS: Invalid object in card array at index %d\n", card_info_size); + } + + if (++card_info_size >= GAME_MAX_CARDS) { + dprintf("Y3WS: too many cards specified, truncating!\n"); + break; + } + + } + LeaveCriticalSection(&card_info_lock); + y3ws_make_success_packet(output_data, output_size); +} + +static void y3ws_handle(const char* input_data, uint32_t input_length, char* output_data, size_t* output_size) { + cJSON* json = cJSON_ParseWithLength(input_data, input_length); + if (json == NULL) { + const char *error_ptr = cJSON_GetErrorPtr(); + + dprintf("Y3WS: Invalid JSON received!\n"); + dprintf("Y3WS: Message was: %s\n", input_data); + dprintf("Y3WS: Error at: %s\n", error_ptr); + y3ws_make_error_packet("Invalid JSON", output_data, output_size); + goto end; + } + + const cJSON* ver = cJSON_GetObjectItemCaseSensitive(json, "version"); + const cJSON* cmd = cJSON_GetObjectItemCaseSensitive(json, "command"); + + if (!cJSON_IsNumber(ver) || ver->valueint <= 0) { + y3ws_make_error_packet("Missing version attribute", output_data, output_size); + goto end; + } + if (!cJSON_IsString(cmd)) { + y3ws_make_error_packet("Missing command attribute", output_data, output_size); + goto end; + } + if (ver->valueint != PROTOCOL_VERSION) { + y3ws_make_error_packet("Incompatible version", output_data, output_size); + goto end; + } + + if (strcmp(cmd->valuestring, "ping") == 0) { + y3ws_make_success_packet(output_data, output_size); + } else if (strcmp(cmd->valuestring, "get_game_id") == 0) { + *output_size = sprintf_s((char*)output_data, *output_size, "{\"version\":%d, \"success\":true,\"game_id\":\"%s\"}", PROTOCOL_VERSION, cfg.game_id); + } else if (strcmp(cmd->valuestring, "get_cards") == 0) { + y3ws_read_cards(output_data, output_size); + } else if (strcmp(cmd->valuestring, "get_card_image") == 0) { + const cJSON* path = cJSON_GetObjectItemCaseSensitive(json, "path"); + if (cJSON_IsString(path)) { + y3ws_read_card_image(path->valuestring, output_data, output_size); + } else { + y3ws_make_error_packet("Missing attribute", output_data, output_size); + } + } else if (strcmp(cmd->valuestring, "set_field") == 0) { + const cJSON* cards = cJSON_GetObjectItemCaseSensitive(json, "cards"); + if (cJSON_IsArray(cards)) { + y3ws_set_cards_from_json(cards, output_data, output_size); + } else { + y3ws_make_error_packet("Missing attribute", output_data, output_size); + } + } else { + y3ws_make_error_packet("Unknown command", output_data, output_size); + } + +end: + cJSON_Delete(json); +} +#pragma endregion + +#pragma region websocket callbacks + +static void onopen(struct wws_connection* client) { + dprintf("Y3WS: Connection opened, addr: %s\n", client->ip_str); +} + +static void onclose(struct wws_connection* client) { + dprintf("Y3WS: Connection closed, addr: %s\n", client->ip_str); +} + +static void onmessage(struct wws_connection* client, const char* msg, size_t size) { + if (cfg.debug) { + dprintf("Y3WS: Message: %.*s\n", (int)size, msg); + } + char* out_buf = malloc(OUTPUT_BUFFER_SIZE); + if (out_buf == NULL) { + dprintf("Y3WS: out of memory for allocating response buffer\n"); + client->is_connected = false; + return; + } + size_t out_size = OUTPUT_BUFFER_SIZE; + + y3ws_handle(msg, size, out_buf, &out_size); + HRESULT hr = wws_send(client, out_buf, out_size); + if (!SUCCEEDED(hr)) { + dprintf("Y3WS: Error sending message: %lx\n", hr); + } + + free(out_buf); +} + +static void onlog(const char* format, ...) { + va_list args; + va_start (args, format); + dprintf("Websocket: "); + dprintfv(format, args); + va_end (args); +} + +#pragma endregion \ No newline at end of file diff --git a/common/y3io/impl/websockets/y3ws.h b/common/y3io/impl/websockets/y3ws.h new file mode 100644 index 0000000..0b88e80 --- /dev/null +++ b/common/y3io/impl/websockets/y3ws.h @@ -0,0 +1,4 @@ +#pragma once + +#include +#include \ No newline at end of file diff --git a/common/y3io/impl/y3io.h b/common/y3io/impl/y3io.h new file mode 100644 index 0000000..24fe971 --- /dev/null +++ b/common/y3io/impl/y3io.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include + +#include "hooklib/y3.h" + +/* Get the version of the Y3 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 y3_io_get_api_version(); + +/* Initialize the Y3 board. This function will be called before any other + y3_io_*() function calls. Errors returned from this function will + manifest as a disconnected Y3 board. + + This method may be called multiple times. + + Minimum API version: 0x0100 */ +HRESULT y3_io_init(); + +/* Fills the given buffer with cards that are detected on the play field. + For the values inside CardInfo, see y3.h. + The input value of numCards is the size of the pCardInfo array. + The output value of numCards is how many cards (>=0) have been set in pCardInfo. + + Minimum API version: 0x0100 */ +HRESULT y3_io_get_cards(struct CardInfo* pCardInfo, int* numCards); \ No newline at end of file diff --git a/common/y3io/meson.build b/common/y3io/meson.build new file mode 100644 index 0000000..c998949 --- /dev/null +++ b/common/y3io/meson.build @@ -0,0 +1,25 @@ +y3io_lib = static_library( + 'y3io', + name_prefix : '', + include_directories: inc, + implicit_include_directories : false, + dependencies : [ + capnhook.get_variable('hook_dep'), + capnhook.get_variable('hooklib_dep'), + cwinwebsocket.get_variable('cws_dep'), + ], + link_with : [ + util_lib, + ], + sources : [ + 'impl/websockets/config.c', + 'impl/websockets/config.h', + 'impl/websockets/y3ws.c', + 'impl/websockets/y3ws.h', + 'impl/websockets/3rdparty/cjson/cJSON.c', + 'impl/websockets/3rdparty/cjson/cJSON.h', + 'impl/websockets/3rdparty/cjson/cJSON_Utils.c', + 'impl/websockets/3rdparty/cjson/cJSON_Utils.h', + 'impl/y3io.h', + ], +) diff --git a/dist/ekt/card_player.html b/dist/ekt/card_player.html new file mode 100644 index 0000000..a92696d --- /dev/null +++ b/dist/ekt/card_player.html @@ -0,0 +1,224 @@ + + + + + Taisen Card Field + + + + + +
+ +
+
+
Please wait...
+
+ +
+
+ + \ No newline at end of file diff --git a/dist/ekt/config_hook.json b/dist/ekt/config_hook.json new file mode 100644 index 0000000..6319f45 --- /dev/null +++ b/dist/ekt/config_hook.json @@ -0,0 +1,13 @@ +{ + "common": { + "language": "english" + }, + "network": { + "property": { + "dhcp": true + } + }, + "allnet_auth": { + "type": "1.0" + } +} \ No newline at end of file diff --git a/dist/ekt/launch_satellite.bat b/dist/ekt/launch_satellite.bat new file mode 100644 index 0000000..aa4249d --- /dev/null +++ b/dist/ekt/launch_satellite.bat @@ -0,0 +1,24 @@ +@echo off +set SEGATOOLS_CONFIG_PATH=.\segatools_satellite.ini + +set SEGATOOLS_VFS_RELATIVE_PATH=..\..\exe +set SEGATOOLS_CONFIG_PATH=%SEGATOOLS_VFS_RELATIVE_PATH%\%SEGATOOLS_CONFIG_PATH% + +pushd ..\PackageBase\am_capture +start "AM Capture" /min %SEGATOOLS_VFS_RELATIVE_PATH%\inject_x86.exe -d -k %SEGATOOLS_VFS_RELATIVE_PATH%\ekthook_x86.dll AmCapture.exe +popd + +set SEGATOOLS_CONFIG_PATH=.\segatools_satellite.ini +set SEGATOOLS_VFS_RELATIVE_PATH= + +pushd %~dp0 +start "AM Daemon" /min inject_x64.exe -d -k ekthook_x64.dll ..\PackageBase\amdaemon.exe -c ..\PackageBase\config_sate.json config_hook.json + +inject_x64 -d -k ekthook_x64.dll ekt.exe -logfile satellite.log -screen-fullscreen 1 -screen-width 1920 -screen-height 1080 -screen-quality Ultra -silent-crashes + +taskkill /f /im AmCapture.exe > nul 2>&1 +taskkill /f /im amdaemon.exe > nul 2>&1 + +echo. +echo Game processes have terminated +pause \ No newline at end of file diff --git a/dist/ekt/launch_terminal.bat b/dist/ekt/launch_terminal.bat new file mode 100644 index 0000000..63d0ea8 --- /dev/null +++ b/dist/ekt/launch_terminal.bat @@ -0,0 +1,13 @@ +@echo off +set SEGATOOLS_CONFIG_PATH=.\segatools_terminal.ini + +pushd %~dp0 + +start "AM Daemon" /min inject_x64 -d -k ekthook_x64.dll ..\PackageBase\amdaemon.exe -c ..\PackageBase\config_terminal.json config_hook.json +inject_x64 -d -k ekthook_x64.dll ekt.exe -terminal -logfile terminal.log -screen-fullscreen 1 -screen-width 1920 -screen-height 1080 -screen-quality Ultra -silent-crashes + +taskkill /f /im amdaemon.exe > nul 2>&1 + +echo. +echo Game processes have terminated +pause \ No newline at end of file diff --git a/dist/ekt/segatools_satellite.ini b/dist/ekt/segatools_satellite.ini new file mode 100644 index 0000000..1f3474c --- /dev/null +++ b/dist/ekt/segatools_satellite.ini @@ -0,0 +1,181 @@ +; ----------------------------------------------------------------------------- +; Path settings +; ----------------------------------------------------------------------------- + +[vfs] +; Insert the path to the game AMFS directory here (contains ICF1 and ICF2) +amfs=amfs_satellite +; Insert the path to the game Option directory here (contains Axxx directories) +option= +; 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_satellite + +; ----------------------------------------------------------------------------- +; Device settings +; ----------------------------------------------------------------------------- + +[aime] +; Enable Aime card reader assembly emulation. Disable to use a real SEGA Aime +; reader. +enable=1 +aimePath=DEVICE\aime.txt + +; Virtual-key code. If this button is **held** then the emulated IC card reader +; emulates an IC card in its proximity. A variety of different IC cards can be +; emulated; the exact choice of card that is emulated depends on the presence or +; absence of the configured card ID files. Default is the Return key. +scan=0x0D + +; ----------------------------------------------------------------------------- +; Network settings +; ----------------------------------------------------------------------------- + +[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 + +[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 + +; ----------------------------------------------------------------------------- +; Board settings +; ----------------------------------------------------------------------------- + +[keychip] +; Keychip serial number. Keychip serials observed in the wild follow this +; pattern: `A\d{2}(E|X)-(01|20)[ABCDU]\d{8}`. +id=A69E-01A88888888 + +; 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.189.0 + +; Override the game's four-character platform code (e.g. `AAV2` for Nu 2). This +; is actually supposed to be a separate three-character `platformId` and +; integer `modelType` setting, but they are combined here for convenience. +; 1 = Terminal (TM) +; 2 = Satellite (ST) +platformId=ACA2 + +[system] +; Enable ALLS system settings. +enable=1 + +; ----------------------------------------------------------------------------- +; Misc. hooks settings +; ----------------------------------------------------------------------------- + +[unity] +; Path to a .NET DLL that should run before the game. Useful for loading +; modding frameworks such as BepInEx. +targetAssembly= + +[printer] +; G-Printec CX-7000 Printer printer emulation setting. +enable=1 +; Insert the path to the image output directory here. +printerOutPath=DEVICE\print + +[flatPanelReader] +; Enable the Y3 board emulation. +enable=1 + +[y3ws] +; Enable the Y3 websocket server. +enable=1 +; Set the TCP port on which the Y3 websocket server runs. +port=3594 + +; ----------------------------------------------------------------------------- +; LED settings +; ----------------------------------------------------------------------------- + +[led15093] +; Enable the 837-15093-06 board emulation. +enable=1 + +; ----------------------------------------------------------------------------- +; Custom IO settings +; ----------------------------------------------------------------------------- + +[aimeio] +; To use a custom card reader IO DLL enter its path here. +; Leave empty if you want to use Segatools built-in keyboard input. +path= + +[ektio] +; To use a custom Eiketsu Taisen IO DLL enter its path here. +; Leave empty if you want to use Segatools built-in keyboard/gamepad input. +path= + +[y3io] +; To use a custom Y3 IO DLL enter its path here. +; Leave empty if you want to use ... TBA ... +path= + +; ----------------------------------------------------------------------------- +; Input settings +; ----------------------------------------------------------------------------- + +; Keyboard bindings are as hexadecimal (prefixed with 0x) or decimal +; (not prefixed with 0x) virtual-key codes, a list of which can be found here: +; +; https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes +; +; This is, admittedly, not the most user-friendly configuration method in the +; world. An improved solution will be provided later. + +[io4] +; Test button virtual-key code. Default is the F1 key. +test=0x70 +; Service button virtual-key code. Default is the F2 key. +service=0x71 +; Keyboard button to increment coin counter. Default is the F3 key. +coin=0x72 + +; SW1. Default is the 4 key. +sw1=0x34 +; SW2. Default is the 5 key. +sw2=0x35 + +; Input API selection for IO4 input emulator. +; For now only "keyboard" is supported. +mode=keyboard + +[keyboard] + +menu=0x41 +start=0x42 +stratagem=0x43 +stratagem_lock=0x44 +hougu=0x45 +ryuuha=0x46 + +tenkey_0=0x60 +tenkey_1=0x61 +tenkey_2=0x62 +tenkey_3=0x63 +tenkey_4=0x64 +tenkey_5=0x65 +tenkey_6=0x66 +tenkey_7=0x67 +tenkey_8=0x68 +tenkey_9=0x69 +tenkey_clear=0x6E +tenkey_enter=0x0D + +vol_up=0x21 +vol_down=0x22 + +trackball_up=0x26 +trackball_right=0x27 +trackball_down=0x28 +trackball_left=0x25 +speed_modifier=10 diff --git a/dist/ekt/segatools_terminal.ini b/dist/ekt/segatools_terminal.ini new file mode 100644 index 0000000..bfb74ee --- /dev/null +++ b/dist/ekt/segatools_terminal.ini @@ -0,0 +1,145 @@ +; ----------------------------------------------------------------------------- +; Path settings +; ----------------------------------------------------------------------------- + +[vfs] +; Insert the path to the game AMFS directory here (contains ICF1 and ICF2) +amfs=amfs_terminal +; Insert the path to the game Option directory here (contains Axxx directories) +option= +; 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_terminal + +; ----------------------------------------------------------------------------- +; Device settings +; ----------------------------------------------------------------------------- + +[aime] +; Enable Aime card reader assembly emulation. Disable to use a real SEGA Aime +; reader. +enable=1 +aimePath=DEVICE\aime.txt + +; Virtual-key code. If this button is **held** then the emulated IC card reader +; emulates an IC card in its proximity. A variety of different IC cards can be +; emulated; the exact choice of card that is emulated depends on the presence or +; absence of the configured card ID files. Default is the Return key. +scan=0x0D + +; ----------------------------------------------------------------------------- +; Network settings +; ----------------------------------------------------------------------------- + +[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 + +[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 + +; ----------------------------------------------------------------------------- +; Board settings +; ----------------------------------------------------------------------------- + +[keychip] +; Keychip serial number. Keychip serials observed in the wild follow this +; pattern: `A\d{2}(E|X)-(01|20)[ABCDU]\d{8}`. +id=A69E-01A88888888 + +; 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.189.0 + +; Override the game's four-character platform code (e.g. `AAV2` for Nu 2). This +; is actually supposed to be a separate three-character `platformId` and +; integer `modelType` setting, but they are combined here for convenience. +; 1 = Terminal (TM) +; 2 = Satellite (ST) +platformId=ACA1 + +[system] +; Enable ALLS system settings. +enable=1 + +; ----------------------------------------------------------------------------- +; Misc. hooks settings +; ----------------------------------------------------------------------------- + +[unity] +; Enable Unity hook. This will allow you to run custom .NET code before the game +enable=1 + +; Path to a .NET DLL that should run before the game. Useful for loading +; modding frameworks such as BepInEx. +targetAssembly= + +; ----------------------------------------------------------------------------- +; LED settings +; ----------------------------------------------------------------------------- + +[led15093] +; Enable the 837-15093-06 board emulation. +enable=1 + +; ----------------------------------------------------------------------------- +; Custom IO settings +; ----------------------------------------------------------------------------- + +[aimeio] +; To use a custom card reader IO DLL enter its path here. +; Leave empty if you want to use Segatools built-in keyboard input. +path= + +[ektio] +; To use a custom Eiketsu Taisen IO DLL enter its path here. +; Leave empty if you want to use Segatools built-in keyboard/gamepad input. +path= + +; ----------------------------------------------------------------------------- +; Input settings +; ----------------------------------------------------------------------------- + +; Keyboard bindings are as hexadecimal (prefixed with 0x) or decimal +; (not prefixed with 0x) virtual-key codes, a list of which can be found here: +; +; https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes +; +; This is, admittedly, not the most user-friendly configuration method in the +; world. An improved solution will be provided later. + +[io4] +; Test button virtual-key code. Default is the F1 key. +test=0x70 +; Service button virtual-key code. Default is the F2 key. +service=0x71 +; Keyboard button to increment coin counter. Default is the F3 key. +coin=0x72 + +; SW1. Default is the 4 key. +sw1=0x34 +; SW2. Default is the 5 key. +sw2=0x35 + +; Input API selection for IO4 input emulator. +; For now only "keyboard" is supported. +mode=keyboard + +[keyboard] + +cancel=0x53 +decide=0x41 + +up=0x26 +right=0x27 +down=0x28 +left=0x25 + +left_2=0x4F +right_2=0x57 diff --git a/dist/sekito/card_player.html b/dist/sekito/card_player.html new file mode 100644 index 0000000..20a54fa --- /dev/null +++ b/dist/sekito/card_player.html @@ -0,0 +1,228 @@ + + + + + Taisen Card Field + + + + + +
+ +
+
+
Please wait...
+
+ +
+
+ + \ No newline at end of file diff --git a/dist/sekito/config_hook_satellite.json b/dist/sekito/config_hook_satellite.json new file mode 100644 index 0000000..3680937 --- /dev/null +++ b/dist/sekito/config_hook_satellite.json @@ -0,0 +1,6 @@ +{ + "common": + { + "language": "english" + } +} diff --git a/dist/sekito/config_hook_terminal.json b/dist/sekito/config_hook_terminal.json new file mode 100644 index 0000000..b566e90 --- /dev/null +++ b/dist/sekito/config_hook_terminal.json @@ -0,0 +1,35 @@ +{ + "common": + { + "language": "english" + }, + "network": + { + "property": + { + "dhcp": true + } + }, + "credit" : + { + "coin_selector_AS6DB" : + { + "enable" : false + } + }, + "aime" : + { + "enable" : true, + "unit" : + [ + { + "port" : 1, + "id" : 1 + }, + { + "port" : 12, + "id" : 2 + } + ] + } +} diff --git a/dist/sekito/launch_satellite.bat b/dist/sekito/launch_satellite.bat new file mode 100644 index 0000000..0e42fa8 --- /dev/null +++ b/dist/sekito/launch_satellite.bat @@ -0,0 +1,15 @@ +@echo off +set SEGATOOLS_CONFIG_PATH=.\segatools_satellite.ini + +pushd %~dp0 + +start "AM Daemon" /min inject_x64 -d -k sekitohook_x64.dll bin\amdaemon.exe -c bin\config_new.json -c bin\config_video_single.json -c bin\config_video_multi.json -c bin\config_input_sate.json -c bin\config_input_terminal.json -c bin\config_input_terminal_exp.json -c config_hook_satellite.json + +inject_x86 -d -k sekitohook_x86.dll bin\appSate.exe + +taskkill /f /im appSate.exe > nul 2>&1 +taskkill /f /im amdaemon.exe > nul 2>&1 + +echo. +echo Game processes have terminated +pause \ No newline at end of file diff --git a/dist/sekito/launch_terminal.bat b/dist/sekito/launch_terminal.bat new file mode 100644 index 0000000..89a0084 --- /dev/null +++ b/dist/sekito/launch_terminal.bat @@ -0,0 +1,19 @@ +@echo off +set SEGATOOLS_CONFIG_PATH=.\segatools_terminal.ini + +pushd %~dp0 + +start "AM Daemon" /min inject_x64 -d -k sekitohook_x64.dll bin\amdaemon.exe -c bin\config_new.json -c bin\config_video_single.json -c bin\config_video_multi.json -c bin\config_input_sate.json -c bin\config_input_terminal.json -c bin\config_input_terminal_exp.json -c config_hook_terminal.json + +start "SKT Server Processes" /min cmd /C bin\server\server_start.bat + +inject_x86 -d -k sekitohook_x86.dll bin\appTerminal.exe + +taskkill /f /im appTerminal.exe > nul 2>&1 +taskkill /f /im amdaemon.exe > nul 2>&1 + +call bin\server\server_stop.bat + +echo. +echo Game processes have terminated +pause \ No newline at end of file diff --git a/dist/sekito/segatools_satellite.ini b/dist/sekito/segatools_satellite.ini new file mode 100644 index 0000000..0dd89c6 --- /dev/null +++ b/dist/sekito/segatools_satellite.ini @@ -0,0 +1,207 @@ +; ----------------------------------------------------------------------------- +; Path settings +; ----------------------------------------------------------------------------- + +[vfs] +; Insert the path to the game AMFS directory here (contains ICF1 and ICF2) +amfs= +; Insert the path to the game Option directory here (contains Axxx directories) +option= +; 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_satellite + +; ----------------------------------------------------------------------------- +; Device settings +; ----------------------------------------------------------------------------- + +[aime] +; Enable Aime card reader assembly emulation. Disable to use a real SEGA Aime +; reader. +enable=1 +aimePath=DEVICE\aime.txt + +; Virtual-key code. If this button is **held** then the emulated IC card reader +; emulates an IC card in its proximity. A variety of different IC cards can be +; emulated; the exact choice of card that is emulated depends on the presence or +; absence of the configured card ID files. Default is the Return key. +scan=0x0D + +; ----------------------------------------------------------------------------- +; Network settings +; ----------------------------------------------------------------------------- + +[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 + +[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 + +; ----------------------------------------------------------------------------- +; Board settings +; ----------------------------------------------------------------------------- + +[keychip] +; Keychip serial number. Keychip serials observed in the wild follow this +; pattern: `A\d{2}(E|X)-(01|20)[ABCDU]\d{8}`. +id=A69E-01A88888888 + +; 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.189.0 + +; Override the game's four-character platform code (e.g. `AAV2` for Nu 2). This +; is actually supposed to be a separate three-character `platformId` and +; integer `modelType` setting, but they are combined here for convenience. +; 1 = Terminal (TM) +; 2 = Satellite (ST) +platformId=AAV2 + +[pcbid] +; Set the Windows host name. This should be an ALLS MAIN ID, without the +; hyphen (which is not a valid character in a Windows host name). +serialNo=ACAE01A99999999 + +[gpio] +; Emulated Nu DIP switch for Distribution Server setting. +; +; TODO +dipsw1=1 + +[eeprom] +; Path to the storage file for EEPROM emulation. This file is automatically +; created and initialized with a suitable number of zero bytes if it does not +; already exist. +path=appdata_satellite\eeprom.bin + +[sram] +; Path to the storage file for SRAM emulation. +path=appdata_satellite\sram.bin + +; ----------------------------------------------------------------------------- +; Misc. hooks settings +; ----------------------------------------------------------------------------- + +[printer] +; Sinfonia CHC-C320 printer emulation setting. +enable=1 +; Change the printer serial number here. +serial_no="5A-A123" +; Insert the path to the image output directory here. +printerOutPath="DEVICE\print" +; Rotate all printed images by 180 degrees. +rotate180=1 + +[gfx] +; Enables the graphics hook. +enable=1 +; Force the game to run windowed. +windowed=1 +; Add a frame to the game window if running windowed. +framed=0 +; Select the monitor to run the game on. (Fullscreen only, 0 =primary screen) +monitor=0 +; Enable DPI awareness for the game process, preventing Windows from stretching the game window if a DPI scaling higher than 100% is used +dpiAware=1 + +[flatPanelReader] +; Enable the Y3 board emulation. +enable=1 + +[y3ws] +; Enable the Y3 websocket server. +enable=1 +; Set the TCP port on which the Y3 websocket server runs. +port=3594 +; Game ID used for clients. +gameId=SDDD + +; ----------------------------------------------------------------------------- +; LED settings +; ----------------------------------------------------------------------------- + +[led15093] +; Enable the 837-15093-06 board emulation. +enable=1 + +; ----------------------------------------------------------------------------- +; Custom IO settings +; ----------------------------------------------------------------------------- + +[aimeio] +; To use a custom card reader IO DLL enter its path here. +; Leave empty if you want to use Segatools built-in keyboard input. +path= + +[sekitoio] +; To use a custom Eiketsu Taisen IO DLL enter its path here. +; Leave empty if you want to use Segatools built-in keyboard/gamepad input. +path= + +[y3io] +; To use a custom Y3 IO DLL enter its path here. +; Leave empty if you want to use ... TBA ... +path= + +; ----------------------------------------------------------------------------- +; Input settings +; ----------------------------------------------------------------------------- + +; Keyboard bindings are as hexadecimal (prefixed with 0x) or decimal +; (not prefixed with 0x) virtual-key codes, a list of which can be found here: +; +; https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes +; +; This is, admittedly, not the most user-friendly configuration method in the +; world. An improved solution will be provided later. + +[io4] +; Test button virtual-key code. Default is the F1 key. +test=0x70 +; Service button virtual-key code. Default is the F2 key. +service=0x71 +; Keyboard button to increment coin counter. Default is the F3 key. +coin=0x72 + +; SW1. Default is the 4 key. +sw1=0x34 +; SW2. Default is the 5 key. +sw2=0x35 + +; Input API selection for IO4 input emulator. +; For now only "keyboard" is supported. +mode=keyboard + +[keyboard] + +menu=0x41 +start=0x42 +stratagem=0x43 +stratagem_lock=0x44 +hougu=0x45 + +tenkey_0=0x60 +tenkey_1=0x61 +tenkey_2=0x62 +tenkey_3=0x63 +tenkey_4=0x64 +tenkey_5=0x65 +tenkey_6=0x66 +tenkey_7=0x67 +tenkey_8=0x68 +tenkey_9=0x69 +tenkey_clear=0x6E +tenkey_enter=0x0D + +trackball_up=0x26 +trackball_right=0x27 +trackball_down=0x28 +trackball_left=0x25 +speed_modifier=10 diff --git a/dist/sekito/segatools_terminal.ini b/dist/sekito/segatools_terminal.ini new file mode 100644 index 0000000..fbd0455 --- /dev/null +++ b/dist/sekito/segatools_terminal.ini @@ -0,0 +1,174 @@ +; ----------------------------------------------------------------------------- +; Path settings +; ----------------------------------------------------------------------------- + +[vfs] +; Insert the path to the game AMFS directory here (contains ICF1 and ICF2) +amfs= +; Insert the path to the game Option directory here (contains Axxx directories) +option= +; 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_terminal + +; ----------------------------------------------------------------------------- +; Device settings +; ----------------------------------------------------------------------------- + +[aime] +; Enable Aime card reader assembly emulation. Disable to use a real SEGA Aime +; reader. +enable=1 +aimePath=DEVICE\aime.txt + +; Virtual-key code. If this button is **held** then the emulated IC card reader +; emulates an IC card in its proximity. A variety of different IC cards can be +; emulated; the exact choice of card that is emulated depends on the presence or +; absence of the configured card ID files. Default is the Return key. +scan=0x0D + +; The terminal-specific Aime card reader to queue for a satellite. +[aime2] +; Enable Aime card reader assembly emulation. Disable to use a real SEGA Aime +; reader. +enable=1 +aimePath=DEVICE\aime.txt + +; Virtual-key code. If this button is **held** then the emulated IC card reader +; emulates an IC card in its proximity. A variety of different IC cards can be +; emulated; the exact choice of card that is emulated depends on the presence or +; absence of the configured card ID files. Default is the Return key. +scan=0x0D + +; ----------------------------------------------------------------------------- +; Network settings +; ----------------------------------------------------------------------------- + +[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 + +[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 + +; ----------------------------------------------------------------------------- +; Board settings +; ----------------------------------------------------------------------------- + +[keychip] +; Keychip serial number. Keychip serials observed in the wild follow this +; pattern: `A\d{2}(E|X)-(01|20)[ABCDU]\d{8}`. +id=A69E-01A88888888 + +; 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.189.0 + +; Override the game's four-character platform code (e.g. `AAV2` for Nu 2). This +; is actually supposed to be a separate three-character `platformId` and +; integer `modelType` setting, but they are combined here for convenience. +; 1 = Terminal (TM) +; 2 = Satellite (ST) +platformId=AAV1 + +[pcbid] +; Set the Windows host name. This should be an ALLS MAIN ID, without the +; hyphen (which is not a valid character in a Windows host name). +serialNo=ACAE01A99999999 + +[eeprom] +; Path to the storage file for EEPROM emulation. This file is automatically +; created and initialized with a suitable number of zero bytes if it does not +; already exist. +path=appdata_terminal\eeprom.bin + +[sram] +; Path to the storage file for SRAM emulation. +path=appdata_terminal\sram.bin + +; ----------------------------------------------------------------------------- +; Misc. hooks settings +; ----------------------------------------------------------------------------- + +[gfx] +; Enables the graphics hook. +enable=1 +; Force the game to run windowed. +windowed=1 +; Add a frame to the game window if running windowed. +framed=0 +; Select the monitor to run the game on. (Fullscreen only, 0 =primary screen) +monitor=0 +; Enable DPI awareness for the game process, preventing Windows from stretching the game window if a DPI scaling higher than 100% is used +dpiAware=1 + +; ----------------------------------------------------------------------------- +; LED settings +; ----------------------------------------------------------------------------- + +[led15093] +; Enable the 837-15093-06 board emulation. +enable=1 + +; ----------------------------------------------------------------------------- +; Custom IO settings +; ----------------------------------------------------------------------------- + +[aimeio] +; To use a custom card reader IO DLL enter its path here. +; Leave empty if you want to use Segatools built-in keyboard input. +path= + +[sekitoio] +; To use a custom Eiketsu Taisen IO DLL enter its path here. +; Leave empty if you want to use Segatools built-in keyboard/gamepad input. +path= + +; ----------------------------------------------------------------------------- +; Input settings +; ----------------------------------------------------------------------------- + +; Keyboard bindings are as hexadecimal (prefixed with 0x) or decimal +; (not prefixed with 0x) virtual-key codes, a list of which can be found here: +; +; https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes +; +; This is, admittedly, not the most user-friendly configuration method in the +; world. An improved solution will be provided later. + +[io4] +; Test button virtual-key code. Default is the F1 key. +test=0x70 +; Service button virtual-key code. Default is the F2 key. +service=0x71 +; Keyboard button to increment coin counter. Default is the F3 key. +coin=0x72 + +; SW1. Default is the 4 key. +sw1=0x34 +; SW2. Default is the 5 key. +sw2=0x35 + +; Input API selection for IO4 input emulator. +; For now only "keyboard" is supported. +mode=keyboard + +[keyboard] + +cancel=0x53 +decide=0x41 +reserve=0x45 + +up=0x26 +right=0x27 +down=0x28 +left=0x25 + +left_2=0x4F +right_2=0x57 diff --git a/doc/config/common.md b/doc/config/common.md index 666be50..a9df591 100644 --- a/doc/config/common.md +++ b/doc/config/common.md @@ -589,6 +589,12 @@ Default: `01:02:03:04:05:06` The MAC address of the virtualized Ethernet adapter. The exact value shouldn't ever matter. +### `broadcast` + +Default: `255.255.255.255` + +The UDP broadcast address that should be used if packets are being sent to the virtual keychip's subnet. This is used for cab-to-cab communication (Local Play, Satellite to Terminal, etc.). Depending on your network adapters (VPNs etc), sometimes you must explicitely specify your real LANs subnet. + ## `[pcbid]` Configure Windows host name virtualization. The ALLS-series platform no longer @@ -633,6 +639,10 @@ Path to the storage file for SRAM emulation. Configure Windows path redirection hooks. +All of these paths can have a path-part prepended to them by setting the environment variable +`SEGATOOLS_VFS_RELATIVE_PATH`. This will only apply if the path set in the configuration is relative. Absolute parts are +not touched. + ### `enable` Default: `1` diff --git a/doc/config/taisen.md b/doc/config/taisen.md new file mode 100644 index 0000000..bc9187d --- /dev/null +++ b/doc/config/taisen.md @@ -0,0 +1,107 @@ +# Taisen configuration settings + +This file describes configuration settings specific to the Taisen series. + +Keyboard binding settings use +[Virtual-Key Codes](https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes). + +## `[ektio]` / `[sekitoio]` + +Controls the input driver. + +### `path` + +Specify a path for a third-party input driver DLL. Default is empty +(use built-in keyboard IO emulation). + +## `[flatPanelReader]` + +Controls settings for the Flat Panel (a.k.a. Y3 Board) + +### `enable` + +Default: `1` + +Whether or not to enable Flat Panel emulation. Disable to use a real board. + +### `port_field` + +Default: `10` + +The COM port on which the emulated Flat Panel is connected to. This differs per game. + +### `port_printer` + +Default: `11` + +The COM port on which the emulated Printer Camera is connected to. This only exists in Sangokushi. + +### `dllVersion` + +Default: `1` + +The version of the emulated Y3CodeReader dll. Unsure if that is used anywhere. + +### `firmVersion` + +Default: `1` + +The version of the emulated Y3CodeReader firmware. Unsure if that is used anywhere. + +### `firmNameField` + +Default: `SFPR` + +The device name of the emulated Flat Panel. This should never need changing. + +### `firmNamePrinter` + +Default: `SPRT` + +The device name of the emulated Printer Camera. This should never need changing. + +### `targetCodeField` + +Default: `SFR0` + +The target name of the emulated Flat Panel. This should never need changing. + +### `targetCodePrinter` + +Default: `SPT0` + +The target name of the emulated Printer Camera. This should never need changing. + +## `[y3ws]` + +Settings for the default implementation of setting cards on the Flat Panel remotely via websockets + +### `enable` + +Default: `1` + +Enable the websocket server. + +### `debug` + +Default: `0` + +Makes y3ws I/O very verbose. For debugging only. May lag the game. + +### `port` + +Default: `3594` + +The TCP port the websocket server listens on. + +### `gameId` + +Default: `SDEY` + +The game ID that the websocket server transmits, so clients can change their behaviour based on that (UI, etc.) + +### `cardDirectory` + +Default: `DEVICE\print` + +The directory where printed card images are placed. Should be the same as in `[printer]`. \ No newline at end of file diff --git a/doc/y3ws.txt b/doc/y3ws.txt new file mode 100644 index 0000000..5689440 --- /dev/null +++ b/doc/y3ws.txt @@ -0,0 +1,57 @@ +Y3WS websocket protocol for card I/O for the Taisen series by Haruka: + +Default listening port: 3594 (san-go-ku-shi) + +All packets are JSON and, unless otherwise specified contain these fields: + +* Requests: +{"version": 1, "command": "..."} + +* Responses: +{"version": 1, "success": , "error": } + +Commands: + +- ping + +Does nothing except answering with a success response. + + +- get_game_id + +Extra response fields: + - game_id: + +Returns the current game ID that is being run (SDDD, SDGY). + + +- get_cards + +Extra response fields: + - cards: + - card_id: + - path: + +Returns all cards that the player has in possession. + + +- get_card_image + +Extra request fields: + - path: +Extra response fields: + - data: + +Returns the card image for the given path (format TBA). + + +- set_field + +Extra request fields: + - array of objects + - card_id: + - x: + - y: + - rotation: + +Sets the given cards onto the given positions on the field. Always replaces the entire board. diff --git a/games/apm3hook/config.h b/games/apm3hook/config.h index 8ff2cc3..d42751d 100644 --- a/games/apm3hook/config.h +++ b/games/apm3hook/config.h @@ -7,7 +7,7 @@ #include "hooklib/dvd.h" #include "hooklib/touch.h" -#include "hooklib/printer.h" +#include "hooklib/printer_chc.h" #include "gfxhook/config.h" diff --git a/games/carolhook/dllmain.c b/games/carolhook/dllmain.c index 35fc7f0..6d18169 100644 --- a/games/carolhook/dllmain.c +++ b/games/carolhook/dllmain.c @@ -156,7 +156,7 @@ static DWORD CALLBACK carol_pre_startup(void) goto fail; } - hr = createprocess_push_hook_a(".\\15312firm\\firmupdate_1113.exe", "inject -d -k carolhook.dll ", NULL, false); + hr = createprocess_push_hook_a(".\\15312firm\\firmupdate_1113.exe", "inject -d -k carolhook.dll ", NULL, false, false); if (FAILED(hr)) { goto fail; diff --git a/games/cmhook/config.c b/games/cmhook/config.c index 47fcfa8..c69a5b9 100644 --- a/games/cmhook/config.c +++ b/games/cmhook/config.c @@ -40,7 +40,7 @@ void cm_hook_config_load( io4_config_load(&cfg->io4, filename); vfd_config_load(&cfg->vfd, filename); touch_screen_config_load(&cfg->touch, filename); - printer_config_load(&cfg->printer, filename); + printer_chc_config_load(&cfg->printer, filename); cm_dll_config_load(&cfg->dll, filename); unity_config_load(&cfg->unity, filename); } diff --git a/games/cmhook/config.h b/games/cmhook/config.h index 26d9a7c..c1e5066 100644 --- a/games/cmhook/config.h +++ b/games/cmhook/config.h @@ -6,7 +6,7 @@ #include "hooklib/dvd.h" #include "hooklib/touch.h" -#include "hooklib/printer.h" +#include "hooklib/printer_chc.h" #include "cmhook/cm-dll.h" @@ -22,7 +22,7 @@ struct cm_hook_config { struct vfd_config vfd; struct cm_dll_config dll; struct touch_screen_config touch; - struct printer_config printer; + struct printer_chc_config printer; struct unity_config unity; }; diff --git a/games/cmhook/dllmain.c b/games/cmhook/dllmain.c index 7d5161d..c1d9c4c 100644 --- a/games/cmhook/dllmain.c +++ b/games/cmhook/dllmain.c @@ -59,7 +59,7 @@ static DWORD CALLBACK cm_pre_startup(void) /* Hook external DLL APIs */ - printer_hook_init(&cm_hook_cfg.printer, 0, cm_hook_mod); + printer_chc_hook_init(&cm_hook_cfg.printer, 0, cm_hook_mod); /* Initialize emulation hooks */ diff --git a/games/ekthook/config.c b/games/ekthook/config.c new file mode 100644 index 0000000..e3f015b --- /dev/null +++ b/games/ekthook/config.c @@ -0,0 +1,107 @@ +#include +#include + +#include "board/config.h" + +#include "ekthook/config.h" +#include "ekthook/ekt-dll.h" + +#include "hooklib/config.h" + +#include "platform/config.h" + +void led15093_config_load(struct led15093_config *cfg, const wchar_t *filename) +{ + assert(cfg != NULL); + assert(filename != NULL); + + wchar_t tmpstr[16]; + + memset(cfg->board_number, ' ', sizeof(cfg->board_number)); + memset(cfg->chip_number, ' ', sizeof(cfg->chip_number)); + memset(cfg->boot_chip_number, ' ', sizeof(cfg->boot_chip_number)); + + cfg->enable = GetPrivateProfileIntW(L"led15093", L"enable", 1, filename); + cfg->port_no[0] = GetPrivateProfileIntW(L"led15093", L"portNo1", 0, filename); + cfg->port_no[1] = GetPrivateProfileIntW(L"led15093", L"portNo2", 0, filename); + cfg->high_baudrate = GetPrivateProfileIntW(L"led15093", L"highBaud", 0, filename); + cfg->fw_ver = GetPrivateProfileIntW(L"led15093", L"fwVer", 0xA0, filename); + cfg->fw_sum = GetPrivateProfileIntW(L"led15093", L"fwSum", 0xAA53, filename); + + GetPrivateProfileStringW( + L"led15093", + L"boardNumber", + L"15093-06", + tmpstr, + _countof(tmpstr), + filename); + + size_t n = wcstombs(cfg->board_number, tmpstr, sizeof(cfg->board_number)); + for (int i = n; i < sizeof(cfg->board_number); i++) + { + cfg->board_number[i] = ' '; + } + + GetPrivateProfileStringW( + L"led15093", + L"chipNumber", + L"6710A", + tmpstr, + _countof(tmpstr), + filename); + + n = wcstombs(cfg->chip_number, tmpstr, sizeof(cfg->chip_number)); + for (int i = n; i < sizeof(cfg->chip_number); i++) + { + cfg->chip_number[i] = ' '; + } + + GetPrivateProfileStringW( + L"led15093", + L"bootChipNumber", + L"6709 ", + tmpstr, + _countof(tmpstr), + filename); + + n = wcstombs(cfg->boot_chip_number, tmpstr, sizeof(cfg->boot_chip_number)); + for (int i = n; i < sizeof(cfg->boot_chip_number); i++) + { + cfg->boot_chip_number[i] = ' '; + } +} + +void ekt_dll_config_load( + struct ekt_dll_config *cfg, + const wchar_t *filename) +{ + assert(cfg != NULL); + assert(filename != NULL); + + GetPrivateProfileStringW( + L"ektio", + L"path", + L"", + cfg->path, + _countof(cfg->path), + filename); +} + + +void ekt_hook_config_load( + struct ekt_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); + io4_config_load(&cfg->io4, filename); + dvd_config_load(&cfg->dvd, filename); + led15093_config_load(&cfg->led15093, filename); + y3_config_load(&cfg->y3, filename); + printer_cx_config_load(&cfg->printer, filename); + unity_config_load(&cfg->unity, filename); + ekt_dll_config_load(&cfg->dll, filename); +} diff --git a/games/ekthook/config.h b/games/ekthook/config.h new file mode 100644 index 0000000..358e3b9 --- /dev/null +++ b/games/ekthook/config.h @@ -0,0 +1,37 @@ +#pragma once + +#include + +#include "board/sg-reader.h" +#include "board/config.h" +#include "board/led15093.h" + +#include "ekthook/ekt-dll.h" + +#include "hooklib/config.h" +#include "hooklib/dvd.h" +#include "hooklib/printer_cx.h" + +#include "platform/config.h" + +#include "unityhook/config.h" + +struct ekt_hook_config { + struct platform_config platform; + struct aime_config aime; + struct io4_config io4; + struct dvd_config dvd; + struct led15093_config led15093; + struct y3_config y3; + struct ekt_dll_config dll; + struct unity_config unity; + struct printer_cx_config printer; +}; + +void ekt_dll_config_load( + struct ekt_dll_config *cfg, + const wchar_t *filename); + +void ekt_hook_config_load( + struct ekt_hook_config *cfg, + const wchar_t *filename); diff --git a/games/ekthook/dllmain.c b/games/ekthook/dllmain.c new file mode 100644 index 0000000..a449df4 --- /dev/null +++ b/games/ekthook/dllmain.c @@ -0,0 +1,224 @@ +/* + "Eiketsu Taisen" (ekt) hook + + Devices + + USB: 837-15257-01 "Type 4" I/O Board + + [Satellite] + + USB: 630-00011 G-Printec CX-7000 Printer + COM2: 837-15093-06 LED Controller Board + COM3: 837-15396 "Gen 3" Aime Reader + COM4: 601-13160-01 "Flat Panel Reader" Y3CR BD SIE F720MM Board + + [Terminal] + + COM1: 837-15396 "Gen 3" Aime Reader + COM3: 837-15093-06 LED Controller Board +*/ + +#include +#include +#include +#include + +#include + +#include "ekt-dll.h" +#include "board/sg-reader.h" +#include "board/led15093.h" + +#include "hook/process.h" +#include "hook/iohook.h" + +#include "hooklib/serial.h" +#include "hooklib/spike.h" + +#include "ekthook/config.h" +#include "ekthook/io4.h" +#include "hooklib/createprocess.h" +#include "hooklib/printer_cx.h" + +#include "platform/platform.h" + +#include "unityhook/hook.h" + +#include "util/dprintf.h" +#include "util/env.h" +#include "hooklib/y3-dll.h" +#include "hooklib/y3.h" +#include "util/lib.h" + +static HMODULE ekt_hook_mod; +static process_entry_t ekt_startup; +static struct ekt_hook_config ekt_hook_cfg; + +static void unity_hook_callback(HMODULE hmodule, const wchar_t* p) { + netenv_hook_apply_hooks(hmodule); + createprocess_hook_apply_hooks(hmodule); +} + +static void check_and_display_warning(void) { + wchar_t* module_path; + wchar_t* file_name; + + module_path = module_file_name(NULL); + + if (module_path != NULL) { + file_name = PathFindFileNameW(module_path); + + free(module_path); + module_path = NULL; + + _wcslwr(file_name); + + if (wcsstr(file_name, L"ekt.exe") != NULL) { + wchar_t recording_flag_path[MAX_PATH]; + PathCombineW(recording_flag_path, ekt_hook_cfg.platform.vfs.appdata, L"recording_warning_seen"); + if (!PathFileExistsW(recording_flag_path)) { + if (MessageBoxW( + NULL, + L"This game has an ability during battle to record and upload your entire desktop content (including other windows, task bar, browsers, etc.)\n\nMake sure you trust the server you are playing on.\n\nThis message will not be shown again.", + L"Segatools Privacy Warning", MB_ICONWARNING | MB_OKCANCEL) != IDOK) { + ExitProcess(0); + } + CreateFileW(recording_flag_path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_NEW, 0, NULL); + } + } + } +} + +static DWORD CALLBACK ekt_pre_startup(void) +{ + HRESULT hr; + bool is_terminal; + + dprintf("--- Begin ekt_pre_startup ---\n"); + + /* Load config */ + + ekt_hook_config_load(&ekt_hook_cfg, get_config_path()); + + /* Hook Win32 APIs */ + + dvd_hook_init(&ekt_hook_cfg.dvd, ekt_hook_mod); + serial_hook_init(); + + createprocess_push_hook_w(L"fsutil", L"", L"", true, true); + + /* Hook external DLL APIs */ + + hr = y3_hook_init(&ekt_hook_cfg.y3, ekt_hook_mod, get_config_path()); + + if (FAILED(hr)) { + goto fail; + } + + printer_cx_hook_init(&ekt_hook_cfg.printer, ekt_hook_mod); + + /* Initialize emulation hooks */ + + hr = platform_hook_init( + &ekt_hook_cfg.platform, + "SDGY", + "ACA1", + ekt_hook_mod); + + if (FAILED(hr)) { + goto fail; + } + + /* Initialize Terminal/Satellite hooks */ + if (strncmp(ekt_hook_cfg.platform.nusec.platform_id, "ACA1", 4) == 0) { + // Terminal + is_terminal = true; + } else if (strncmp(ekt_hook_cfg.platform.nusec.platform_id, "ACA2", 4) == 0) { + // Satellite + is_terminal = false; + } else { + // Unknown + dprintf("Unknown platform ID: %s\n", ekt_hook_cfg.platform.nusec.platform_id); + goto fail; + } + + dprintf("System: Cabinet Type: %s\n", is_terminal ? "Terminal" : "Satellite"); + + // LED: terminal uses COM 3 and satellite use COM 2 + unsigned int led_port_no[2] = {is_terminal ? 3 : 2, 0}; + + // AIME: terminal uses COM 1 and satellite use COM 3 + unsigned int aime_port_no = is_terminal ? 1 : 3; + + if (FAILED(hr)) { + goto fail; + } + + hr = ekt_dll_init(&ekt_hook_cfg.dll, ekt_hook_mod); + + if (FAILED(hr)) { + goto fail; + } + + hr = ekt_io4_hook_init(&ekt_hook_cfg.io4, is_terminal); + + if (FAILED(hr)) { + goto fail; + } + + hr = led15093_hook_init(&ekt_hook_cfg.led15093, + ekt_dll.led_init, ekt_dll.led_set_leds, led_port_no); + + if (FAILED(hr)) { + goto fail; + } + + hr = sg_reader_hook_init(&ekt_hook_cfg.aime, aime_port_no, 3, + ekt_hook_mod); + + 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 `ekthook` initialization. */ + + unity_hook_init(&ekt_hook_cfg.unity, ekt_hook_mod, unity_hook_callback); + + /* Initialize debug helpers */ + + spike_hook_init(get_config_path()); + + dprintf("--- End ekt_pre_startup ---\n"); + + /* Recording warning */ + check_and_display_warning(); + + /* Jump to EXE start address */ + + return ekt_startup(); + +fail: + ExitProcess(EXIT_FAILURE); +} + +BOOL WINAPI DllMain(HMODULE mod, DWORD cause, void *ctx) +{ + HRESULT hr; + + if (cause != DLL_PROCESS_ATTACH) { + return TRUE; + } + + ekt_hook_mod = mod; + + hr = process_hijack_startup(ekt_pre_startup, &ekt_startup); + + if (!SUCCEEDED(hr)) { + dprintf("Failed to hijack process startup: %x\n", (int) hr); + } + + return SUCCEEDED(hr); +} diff --git a/games/ekthook/ekt-dll.c b/games/ekthook/ekt-dll.c new file mode 100644 index 0000000..95a9fd1 --- /dev/null +++ b/games/ekthook/ekt-dll.c @@ -0,0 +1,118 @@ +#include + +#include +#include + +#include "ekthook/ekt-dll.h" + +#include "util/dll-bind.h" +#include "util/dprintf.h" + +const struct dll_bind_sym ekt_dll_syms[] = { + { + .sym = "ekt_io_init", + .off = offsetof(struct ekt_dll, init), + }, { + .sym = "ekt_io_poll", + .off = offsetof(struct ekt_dll, poll), + }, { + .sym = "ekt_io_get_opbtns", + .off = offsetof(struct ekt_dll, get_opbtns), + }, { + .sym = "ekt_io_get_gamebtns", + .off = offsetof(struct ekt_dll, get_gamebtns), + }, { + .sym = "ekt_io_get_trackball_position", + .off = offsetof(struct ekt_dll, get_trackball_position), + }, { + .sym = "ekt_io_led_init", + .off = offsetof(struct ekt_dll, led_init), + }, { + .sym = "ekt_io_led_set_colors", + .off = offsetof(struct ekt_dll, led_set_leds), + } +}; + +struct ekt_dll ekt_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 ekt_dll_init(const struct ekt_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("EKT IO: Failed to load IO DLL: %lx: %S\n", + hr, + cfg->path); + + goto end; + } + + dprintf("EKT IO: Using custom IO DLL: %S\n", cfg->path); + src = owned; + } else { + owned = NULL; + src = self; + } + + get_api_version = (void *) GetProcAddress(src, "ekt_io_get_api_version"); + + if (get_api_version != NULL) { + ekt_dll.api_version = get_api_version(); + } else { + ekt_dll.api_version = 0x0100; + dprintf("Custom IO DLL does not expose ekt_io_get_api_version, " + "assuming API version 1.0.\n" + "Please ask the developer to update their DLL.\n"); + } + + if (ekt_dll.api_version >= 0x0200) { + hr = E_NOTIMPL; + dprintf("EKT IO: Custom IO DLL implements an unsupported " + "API version (%#04x). Please update Segatools.\n", + ekt_dll.api_version); + + goto end; + } + + sym = ekt_dll_syms; + hr = dll_bind(&ekt_dll, src, &sym, _countof(ekt_dll_syms)); + + if (FAILED(hr)) { + if (src != self) { + dprintf("EKT 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; +} diff --git a/games/ekthook/ekt-dll.h b/games/ekthook/ekt-dll.h new file mode 100644 index 0000000..d2d324e --- /dev/null +++ b/games/ekthook/ekt-dll.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +#include "ektio/ektio.h" + +struct ekt_dll { + uint16_t api_version; + HRESULT (*init)(void); + HRESULT (*poll)(void); + void (*get_opbtns)(uint8_t *opbtn); + void (*get_gamebtns)(uint32_t *gamebtn); + void (*get_trackball_position)(uint16_t *x, uint16_t *y); + HRESULT (*led_init)(void); + void (*led_set_leds)(uint8_t board, uint8_t *rgb); +}; + +struct ekt_dll_config { + wchar_t path[MAX_PATH]; +}; + +extern struct ekt_dll ekt_dll; + +HRESULT ekt_dll_init(const struct ekt_dll_config *cfg, HINSTANCE self); diff --git a/games/ekthook/ekthook.def b/games/ekthook/ekthook.def new file mode 100644 index 0000000..f03d8d8 --- /dev/null +++ b/games/ekthook/ekthook.def @@ -0,0 +1,73 @@ +LIBRARY ekthook + +EXPORTS + aime_io_get_api_version + aime_io_init + aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state + aime_io_nfc_get_aime_id + aime_io_nfc_get_felica_id + aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data + ekt_io_get_api_version + ekt_io_get_gamebtns + ekt_io_get_opbtns + ekt_io_get_trackball_position + ekt_io_init + ekt_io_poll + ekt_io_led_init + ekt_io_led_set_colors + y3_io_get_api_version + y3_io_init + y3_io_close + y3_io_get_cards + API_DLLVersion @1 + API_GetLastError @2 + API_GetErrorMessage @3 + API_Connect @4 + API_Close @5 + API_Start @6 + API_Stop @7 + API_GetFirmVersion @8 + API_GetFirmName @9 + API_GetTargetCode @10 + API_GetStatus @11 + API_GetCounter @12 + API_ClearError @13 + API_Reset @14 + API_GetCardInfo @15 + API_GetCardInfoCharSize @16 + API_SetDevice @17 + API_SetCommand @18 + API_FirmwareUpdate @19 + API_Calibration @20 + API_GetCalibrationResult @21 + API_GetProcTime @22 + API_GetMemStatus @23 + API_GetMemCounter @24 + API_SetSysControl @25 + API_GetSysControl @26 + API_SetParameter @27 + API_GetParameter @28 + API_TestReset @29 + API_DebugReset @30 + API_GetBoardType @31 + API_GetCardDataSize @32 + API_GetFirmDate @33 + API_SystemCommand @34 + API_CalcCheckSum @35 + API_GetCheckSumResult @36 + API_BlockRead @37 + API_GetBlockReadResult @38 + API_BlockWrite @39 + API_GetDebugParam @40 diff --git a/games/ekthook/io4.c b/games/ekthook/io4.c new file mode 100644 index 0000000..2eec5e5 --- /dev/null +++ b/games/ekthook/io4.c @@ -0,0 +1,223 @@ +#include "io4.h" + +#include + +#include +#include +#include + +#include "board/io4.h" + +#include "ekthook/ekt-dll.h" + +#include "util/dprintf.h" + +static HRESULT ekt_io4_poll(void *ctx, struct io4_state *state); +static uint16_t coins; + +static const struct io4_ops ekt_io4_ops = { + .poll = ekt_io4_poll, +}; + +static bool io_is_terminal; + +HRESULT ekt_io4_hook_init(const struct io4_config *cfg, bool is_terminal) +{ + HRESULT hr; + + assert(ekt_dll.init != NULL); + + hr = io4_hook_init(cfg, &ekt_io4_ops, NULL, L"ekt", false); + + io_is_terminal = is_terminal; + + if (FAILED(hr)) { + return hr; + } + + return ekt_dll.init(); +} + +static HRESULT ekt_io4_poll(void *ctx, struct io4_state *state) +{ + uint8_t opbtn; + uint16_t x, y; + uint32_t gamebtn; + HRESULT hr; + + assert(ekt_dll.poll != NULL); + assert(ekt_dll.get_opbtns != NULL); + assert(ekt_dll.get_gamebtns != NULL); + assert(ekt_dll.get_trackball_position != NULL); + + memset(state, 0, sizeof(*state)); + + hr = ekt_dll.poll(); + + if (FAILED(hr)) { + return hr; + } + + opbtn = 0; + gamebtn = 0; + x = 0; + y = 0; + + ekt_dll.get_opbtns(&opbtn); + ekt_dll.get_gamebtns(&gamebtn); + ekt_dll.get_trackball_position(&x, &y); + + if (opbtn & EKT_IO_OPBTN_TEST) { + state->buttons[0] |= IO4_BUTTON_TEST; + } + + if (opbtn & EKT_IO_OPBTN_SERVICE) { + state->buttons[0] |= IO4_BUTTON_SERVICE; + } + + if (opbtn & EKT_IO_OPBTN_SW1) { + state->buttons[0] |= 1 << 2; + } + + if (opbtn & EKT_IO_OPBTN_SW2) { + state->buttons[0] |= 1 << 3; + } + + if (opbtn & EKT_IO_OPBTN_COIN) { + coins++; + } + state->chutes[0] = coins << 8; + + if (!io_is_terminal) { + if (gamebtn & EKT_IO_GAMEBTN_HOUGU) { + state->buttons[1] |= 1 << 14; + } + + if (gamebtn & EKT_IO_GAMEBTN_RYUUHA) { + state->buttons[1] |= 1 << 12; + } + + if (gamebtn & EKT_IO_GAMEBTN_MENU) { + state->buttons[0] |= 1 << 13; + } + + if (gamebtn & EKT_IO_GAMEBTN_START) { + state->buttons[0] |= 1 << 7; + } + + if (gamebtn & EKT_IO_GAMEBTN_STRATAGEM) { + state->buttons[1] |= 1 << 15; + } + + if (gamebtn & EKT_IO_GAMEBTN_STRATAGEM_LOCK) { + state->buttons[1] |= 1 << 13; + } + + if (gamebtn & EKT_IO_GAMEBTN_VOL_DOWN) { + state->buttons[1] |= 1 << 10; + } + + if (gamebtn & EKT_IO_GAMEBTN_VOL_UP) { + state->buttons[1] |= 1 << 11; + } + } + + if (gamebtn & EKT_IO_GAMEBTN_NUMPAD_0) { + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_C2 : EKT_NUMPAD_TERM_C2; + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_R4 : EKT_NUMPAD_TERM_R4; + } + + if (gamebtn & EKT_IO_GAMEBTN_NUMPAD_1) { + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_C1 : EKT_NUMPAD_TERM_C1; + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_R1 : EKT_NUMPAD_TERM_R1; + } + + if (gamebtn & EKT_IO_GAMEBTN_NUMPAD_2) { + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_C2 : EKT_NUMPAD_TERM_C2; + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_R1 : EKT_NUMPAD_TERM_R1; + } + + if (gamebtn & EKT_IO_GAMEBTN_NUMPAD_3) { + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_C3 : EKT_NUMPAD_TERM_C3; + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_R1 : EKT_NUMPAD_TERM_R1; + } + + if (gamebtn & EKT_IO_GAMEBTN_NUMPAD_4) { + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_C1 : EKT_NUMPAD_TERM_C1; + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_R2 : EKT_NUMPAD_TERM_R2; + } + + if (gamebtn & EKT_IO_GAMEBTN_NUMPAD_5) { + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_C2 : EKT_NUMPAD_TERM_C2; + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_R2 : EKT_NUMPAD_TERM_R2; + } + + if (gamebtn & EKT_IO_GAMEBTN_NUMPAD_6) { + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_C3 : EKT_NUMPAD_TERM_C3; + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_R2 : EKT_NUMPAD_TERM_R2; + } + + if (gamebtn & EKT_IO_GAMEBTN_NUMPAD_7) { + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_C1 : EKT_NUMPAD_TERM_C1; + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_R3 : EKT_NUMPAD_TERM_R3; + } + + if (gamebtn & EKT_IO_GAMEBTN_NUMPAD_8) { + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_C2 : EKT_NUMPAD_TERM_C2; + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_R3 : EKT_NUMPAD_TERM_R3; + } + + if (gamebtn & EKT_IO_GAMEBTN_NUMPAD_9) { + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_C3 : EKT_NUMPAD_TERM_C3; + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_R3 : EKT_NUMPAD_TERM_R3; + } + + if (gamebtn & EKT_IO_GAMEBTN_NUMPAD_CLEAR) { + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_C1 : EKT_NUMPAD_TERM_C1; + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_R4 : EKT_NUMPAD_TERM_R4; + } + + if (gamebtn & EKT_IO_GAMEBTN_NUMPAD_ENTER) { + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_C3 : EKT_NUMPAD_TERM_C3; + state->buttons[0] |= !io_is_terminal ? EKT_NUMPAD_SATE_R4 : EKT_NUMPAD_TERM_R4; + } + + if (io_is_terminal) { + if (gamebtn & EKT_IO_GAMEBTN_TERMINAL_CANCEL) { + state->buttons[1] |= 1 << 0; + } + + if (gamebtn & EKT_IO_GAMEBTN_TERMINAL_DECIDE) { + state->buttons[1] |= 1 << 1; + } + + if (gamebtn & EKT_IO_GAMEBTN_TERMINAL_LEFT) { + state->buttons[0] |= 1 << 3; + } + + if (gamebtn & EKT_IO_GAMEBTN_TERMINAL_UP) { + state->buttons[0] |= 1 << 5; + } + + if (gamebtn & EKT_IO_GAMEBTN_TERMINAL_RIGHT) { + state->buttons[0] |= 1 << 2; + } + + if (gamebtn & EKT_IO_GAMEBTN_TERMINAL_DOWN) { + state->buttons[0] |= 1 << 4; + } + + if (gamebtn & EKT_IO_GAMEBTN_TERMINAL_LEFT_2) { + state->buttons[1] |= 1 << 3; + } + + if (gamebtn & EKT_IO_GAMEBTN_TERMINAL_RIGHT_2) { + state->buttons[1] |= 1 << 2; + } + } + + state->spinners[2] = x; + state->spinners[3] = y; + + return S_OK; +} diff --git a/games/ekthook/io4.h b/games/ekthook/io4.h new file mode 100644 index 0000000..4fff5f3 --- /dev/null +++ b/games/ekthook/io4.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +#include "board/io4.h" + +enum { + EKT_NUMPAD_SATE_R1 = 1 << 1, + EKT_NUMPAD_SATE_R2 = 1 << 0, + EKT_NUMPAD_SATE_R3 = 1 << 15, + EKT_NUMPAD_SATE_R4 = 1 << 14, + EKT_NUMPAD_SATE_C1 = 1 << 12, + EKT_NUMPAD_SATE_C2 = 1 << 11, + EKT_NUMPAD_SATE_C3 = 1 << 10, + EKT_NUMPAD_TERM_R1 = 1 << 1, + EKT_NUMPAD_TERM_R2 = 1 << 0, + EKT_NUMPAD_TERM_R3 = 1 << 15, + EKT_NUMPAD_TERM_R4 = 1 << 14, + EKT_NUMPAD_TERM_C1 = 1 << 13, + EKT_NUMPAD_TERM_C2 = 1 << 12, + EKT_NUMPAD_TERM_C3 = 1 << 11, +}; + +HRESULT ekt_io4_hook_init(const struct io4_config *cfg, bool is_terminal); diff --git a/games/ekthook/meson.build b/games/ekthook/meson.build new file mode 100644 index 0000000..2b3ef34 --- /dev/null +++ b/games/ekthook/meson.build @@ -0,0 +1,31 @@ +shared_library( + 'ekthook', + name_prefix : '', + include_directories : inc, + implicit_include_directories : false, + vs_module_defs : 'ekthook.def', + dependencies : [ + capnhook.get_variable('hook_dep'), + capnhook.get_variable('hooklib_dep') + ], + link_with : [ + aimeio_lib, + board_lib, + ektio_lib, + hooklib_lib, + jvs_lib, + platform_lib, + unityhook_lib, + util_lib, + y3io_lib, + ], + sources : [ + 'config.c', + 'config.h', + 'dllmain.c', + 'ekt-dll.c', + 'ekt-dll.h', + 'io4.c', + 'io4.h', + ], +) diff --git a/games/ektio/backend.h b/games/ektio/backend.h new file mode 100644 index 0000000..4fcf885 --- /dev/null +++ b/games/ektio/backend.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +#include "ektio/ektio.h" + +struct ekt_io_backend { + void (*get_gamebtns)(uint32_t *gamebtn); + void (*get_trackball)(uint16_t *x, uint16_t *y); +}; diff --git a/games/ektio/config.c b/games/ektio/config.c new file mode 100644 index 0000000..7108d66 --- /dev/null +++ b/games/ektio/config.c @@ -0,0 +1,77 @@ +#include + +#include +#include +#include + +#include "ektio/config.h" + +#include + + +void ekt_kb_config_load( + struct ekt_kb_config *cfg, + const wchar_t *filename) { + + cfg->vk_menu = GetPrivateProfileIntW(L"keyboard", L"menu", 'A', filename); + cfg->vk_start = GetPrivateProfileIntW(L"keyboard", L"start", 'S', filename); + cfg->vk_stratagem = GetPrivateProfileIntW(L"keyboard", L"stratagem", 'D', filename); + cfg->vk_stratagem_lock = GetPrivateProfileIntW(L"keyboard", L"stratagem_lock", 'F', filename); + cfg->vk_hougu = GetPrivateProfileIntW(L"keyboard", L"hougu", 'G', filename); + cfg->vk_ryuuha = GetPrivateProfileIntW(L"keyboard", L"ryuuha", 'H', filename); + + cfg->vk_tenkey_0 = GetPrivateProfileIntW(L"keyboard", L"tenkey_0", VK_NUMPAD0, filename); + cfg->vk_tenkey_1 = GetPrivateProfileIntW(L"keyboard", L"tenkey_1", VK_NUMPAD1, filename); + cfg->vk_tenkey_2 = GetPrivateProfileIntW(L"keyboard", L"tenkey_2", VK_NUMPAD2, filename); + cfg->vk_tenkey_3 = GetPrivateProfileIntW(L"keyboard", L"tenkey_3", VK_NUMPAD3, filename); + cfg->vk_tenkey_4 = GetPrivateProfileIntW(L"keyboard", L"tenkey_4", VK_NUMPAD4, filename); + cfg->vk_tenkey_5 = GetPrivateProfileIntW(L"keyboard", L"tenkey_5", VK_NUMPAD5, filename); + cfg->vk_tenkey_6 = GetPrivateProfileIntW(L"keyboard", L"tenkey_6", VK_NUMPAD6, filename); + cfg->vk_tenkey_7 = GetPrivateProfileIntW(L"keyboard", L"tenkey_7", VK_NUMPAD7, filename); + cfg->vk_tenkey_8 = GetPrivateProfileIntW(L"keyboard", L"tenkey_8", VK_NUMPAD8, filename); + cfg->vk_tenkey_9 = GetPrivateProfileIntW(L"keyboard", L"tenkey_9", VK_NUMPAD9, filename); + cfg->vk_tenkey_clear = GetPrivateProfileIntW(L"keyboard", L"tenkey_clear", VK_DECIMAL, filename); + cfg->vk_tenkey_enter = GetPrivateProfileIntW(L"keyboard", L"tenkey_enter", VK_RETURN, filename); + + cfg->vk_vol_down = GetPrivateProfileIntW(L"keyboard", L"vol_down", VK_NEXT, filename); + cfg->vk_vol_up = GetPrivateProfileIntW(L"keyboard", L"vol_up", VK_PRIOR, filename); + + cfg->vk_terminal_decide = GetPrivateProfileIntW(L"keyboard", L"decide", 'A', filename); + cfg->vk_terminal_cancel = GetPrivateProfileIntW(L"keyboard", L"cancel", 'S', filename); + cfg->vk_terminal_up = GetPrivateProfileIntW(L"keyboard", L"up", VK_UP, filename); + cfg->vk_terminal_right = GetPrivateProfileIntW(L"keyboard", L"right", VK_RIGHT, filename); + cfg->vk_terminal_down = GetPrivateProfileIntW(L"keyboard", L"down", VK_DOWN, filename); + cfg->vk_terminal_left = GetPrivateProfileIntW(L"keyboard", L"left", VK_LEFT, filename); + cfg->vk_terminal_left_2 = GetPrivateProfileIntW(L"keyboard", L"left2", 'Q', filename); + cfg->vk_terminal_right_2 = GetPrivateProfileIntW(L"keyboard", L"right2", 'W', filename); + + cfg->x_down = GetPrivateProfileIntW(L"keyboard", L"trackball_left", VK_LEFT, filename); + cfg->x_up = GetPrivateProfileIntW(L"keyboard", L"trackball_right", VK_RIGHT, filename); + cfg->y_down = GetPrivateProfileIntW(L"keyboard", L"trackball_up", VK_UP, filename); + cfg->y_up = GetPrivateProfileIntW(L"keyboard", L"trackball_down", VK_DOWN, filename); + cfg->speed = GetPrivateProfileIntW(L"keyboard", L"speed_modifier", 1, filename); +} + +void ekt_io_config_load( + struct ekt_io_config *cfg, + const wchar_t *filename) +{ + 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_sw1 = GetPrivateProfileIntW(L"io4", L"sw1", '4', filename); + cfg->vk_sw2 = GetPrivateProfileIntW(L"io4", L"sw2", '5', filename); + + GetPrivateProfileStringW( + L"io4", + L"mode", + L"keyboard", + cfg->mode, + _countof(cfg->mode), + filename); + + ekt_kb_config_load(&cfg->kb, filename); +} diff --git a/games/ektio/config.h b/games/ektio/config.h new file mode 100644 index 0000000..0f828e7 --- /dev/null +++ b/games/ektio/config.h @@ -0,0 +1,62 @@ +#pragma once + +#include +#include + +#include + +struct ekt_kb_config { + uint8_t vk_menu; + uint8_t vk_start; + uint8_t vk_stratagem; + uint8_t vk_stratagem_lock; + uint8_t vk_hougu; + uint8_t vk_ryuuha; + + uint8_t vk_tenkey_0; + uint8_t vk_tenkey_1; + uint8_t vk_tenkey_2; + uint8_t vk_tenkey_3; + uint8_t vk_tenkey_4; + uint8_t vk_tenkey_5; + uint8_t vk_tenkey_6; + uint8_t vk_tenkey_7; + uint8_t vk_tenkey_8; + uint8_t vk_tenkey_9; + uint8_t vk_tenkey_clear; + uint8_t vk_tenkey_enter; + + uint8_t vk_vol_down; + uint8_t vk_vol_up; + + uint8_t vk_terminal_up; + uint8_t vk_terminal_right; + uint8_t vk_terminal_down; + uint8_t vk_terminal_left; + uint8_t vk_terminal_left_2; + uint8_t vk_terminal_right_2; + uint8_t vk_terminal_cancel; + uint8_t vk_terminal_decide; + + uint8_t x_down; + uint8_t x_up; + uint8_t y_down; + uint8_t y_up; + uint8_t speed; +}; + +struct ekt_io_config { + uint8_t vk_test; + uint8_t vk_service; + uint8_t vk_coin; + uint8_t vk_sw1; + uint8_t vk_sw2; + + wchar_t mode[12]; + struct ekt_kb_config kb; +}; + +void ekt_kb_config_load(struct ekt_kb_config *cfg, const wchar_t *filename); +void ekt_io_config_load( + struct ekt_io_config *cfg, + const wchar_t *filename); diff --git a/games/ektio/ektio.c b/games/ektio/ektio.c new file mode 100644 index 0000000..2ebb879 --- /dev/null +++ b/games/ektio/ektio.c @@ -0,0 +1,108 @@ +#include +#include +#include + +#include + +#include "ektio/ektio.h" + +#include + +#include "keyboard.h" +#include "ektio/config.h" +#include "util/dprintf.h" +#include "util/env.h" +#include "util/str.h" + +static uint8_t ekt_opbtn; +static uint32_t ekt_gamebtn; +static uint8_t ekt_stick_x; +static uint8_t ekt_stick_y; +static struct ekt_io_config ekt_io_cfg; +static const struct ekt_io_backend* ekt_io_backend; +static bool ekt_io_coin; + +uint16_t ekt_io_get_api_version(void) { + return 0x0100; +} + +HRESULT ekt_io_init(void) { + ekt_io_config_load(&ekt_io_cfg, get_config_path()); + + HRESULT hr; + + if (wstr_ieq(ekt_io_cfg.mode, L"keyboard")) { + hr = ekt_kb_init(&ekt_io_cfg.kb, &ekt_io_backend); + } else { + hr = E_INVALIDARG; + dprintf("EKT IO: Invalid IO mode \"%S\", use keyboard\n", + ekt_io_cfg.mode); + } + + return hr; +} + +HRESULT ekt_io_poll(void) { + assert(ekt_io_backend != NULL); + + ekt_opbtn = 0; + ekt_gamebtn = 0; + ekt_stick_x = 0; + ekt_stick_y = 0; + + if (GetAsyncKeyState(ekt_io_cfg.vk_test) & 0x8000) { + ekt_opbtn |= EKT_IO_OPBTN_TEST; + } + + if (GetAsyncKeyState(ekt_io_cfg.vk_service) & 0x8000) { + ekt_opbtn |= EKT_IO_OPBTN_SERVICE; + } + + if (GetAsyncKeyState(ekt_io_cfg.vk_sw1) & 0x8000) { + ekt_opbtn |= EKT_IO_OPBTN_SW1; + } + + if (GetAsyncKeyState(ekt_io_cfg.vk_sw2) & 0x8000) { + ekt_opbtn |= EKT_IO_OPBTN_SW2; + } + + if (GetAsyncKeyState(ekt_io_cfg.vk_coin) & 0x8000) { + if (!ekt_io_coin) { + ekt_io_coin = true; + ekt_opbtn |= EKT_IO_OPBTN_COIN; + } + } else { + ekt_io_coin = false; + } + + return S_OK; +} + +void ekt_io_get_opbtns(uint8_t* opbtn) { + if (opbtn != NULL) { + *opbtn = ekt_opbtn; + } +} + +void ekt_io_get_gamebtns(uint32_t* btn) { + assert(ekt_io_backend != NULL); + assert(btn != NULL); + + ekt_io_backend->get_gamebtns(btn); +} + +void ekt_io_get_trackball_position(uint16_t* stick_x, uint16_t* stick_y) { + assert(ekt_io_backend != NULL); + assert(stick_x != NULL); + assert(stick_y != NULL); + + ekt_io_backend->get_trackball(stick_x, stick_y); +} + +HRESULT ekt_io_led_init(void) { + return S_OK; +} + +void ekt_io_led_set_colors(uint8_t board, uint8_t* rgb) { + return; +} diff --git a/games/ektio/ektio.h b/games/ektio/ektio.h new file mode 100644 index 0000000..6915375 --- /dev/null +++ b/games/ektio/ektio.h @@ -0,0 +1,106 @@ +#pragma once + +#include + +#include + +enum { + EKT_IO_OPBTN_TEST = 0x01, + EKT_IO_OPBTN_SERVICE = 0x02, + EKT_IO_OPBTN_COIN = 0x04, + EKT_IO_OPBTN_SW1 = 0x08, + EKT_IO_OPBTN_SW2 = 0x10, +}; + +enum { + EKT_IO_GAMEBTN_MENU = 0x01, + EKT_IO_GAMEBTN_START = 0x02, + EKT_IO_GAMEBTN_STRATAGEM = 0x04, + EKT_IO_GAMEBTN_STRATAGEM_LOCK = 0x08, + EKT_IO_GAMEBTN_HOUGU = 0x10, + EKT_IO_GAMEBTN_RYUUHA = 0x20, + EKT_IO_GAMEBTN_NUMPAD_0 = 0x100, + EKT_IO_GAMEBTN_NUMPAD_1 = 0x200, + EKT_IO_GAMEBTN_NUMPAD_2 = 0x400, + EKT_IO_GAMEBTN_NUMPAD_3 = 0x800, + EKT_IO_GAMEBTN_NUMPAD_4 = 0x1000, + EKT_IO_GAMEBTN_NUMPAD_5 = 0x2000, + EKT_IO_GAMEBTN_NUMPAD_6 = 0x4000, + EKT_IO_GAMEBTN_NUMPAD_7 = 0x8000, + EKT_IO_GAMEBTN_NUMPAD_8 = 0x10000, + EKT_IO_GAMEBTN_NUMPAD_9 = 0x20000, + EKT_IO_GAMEBTN_NUMPAD_CLEAR = 0x40000, + EKT_IO_GAMEBTN_NUMPAD_ENTER = 0x80000, + EKT_IO_GAMEBTN_VOL_UP = 0x100000, + EKT_IO_GAMEBTN_VOL_DOWN = 0x200000, + EKT_IO_GAMEBTN_TERMINAL_LEFT = 0x400000, + EKT_IO_GAMEBTN_TERMINAL_UP = 0x800000, + EKT_IO_GAMEBTN_TERMINAL_RIGHT = 0x1000000, + EKT_IO_GAMEBTN_TERMINAL_DOWN = 0x2000000, + EKT_IO_GAMEBTN_TERMINAL_LEFT_2 = 0x4000000, + EKT_IO_GAMEBTN_TERMINAL_RIGHT_2 = 0x8000000, + EKT_IO_GAMEBTN_TERMINAL_DECIDE = 0x10000000, + EKT_IO_GAMEBTN_TERMINAL_CANCEL = 0x20000000, +}; + +/* Get the version of the Eiketsu Taisen 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 ekt_io_get_api_version(void); + +/* Initialize the IO DLL. This is the second function that will be called on + your DLL, after ekt_io_get_api_version. + + All subsequent calls to this API may originate from arbitrary threads. + + Minimum API version: 0x0100 */ + +HRESULT ekt_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 ekt_io_poll(void); + +/* Get the state of the cabinet's operator buttons as of the last poll. See + EKT_IO_OPBTN enum above: this contains bit mask definitions for button + states returned in *opbtn. All buttons are active-high. + + Minimum API version: 0x0100 */ + +void ekt_io_get_opbtns(uint8_t *opbtn); + +/* Get the state of the cabinet's gameplay buttons as of the last poll. See + EKT_IO_GAMEBTN enum above: this contains bit mask definitions for button + states returned in *gamebtn. All buttons are active-high. + + Minimum API version: 0x0100 */ + +void ekt_io_get_gamebtns(uint32_t *gamebtn); + +/* Get the position of the trackball as of the last poll. + + Minimum API version: 0x0100 */ + +void ekt_io_get_trackball_position(uint16_t *stick_x, uint16_t *stick_y); + +/* Initialize LED emulation. This function will be called before any + other ekt_io_led_*() function calls. + + All subsequent calls may originate from arbitrary threads and some may + overlap with each other. Ensuring synchronization inside your IO DLL is + your responsibility. */ + +HRESULT ekt_io_led_init(void); + +/* Update the RGB LEDs. + + Exact layout is TBD. */ + +void ekt_io_led_set_colors(uint8_t board, uint8_t *rgb); diff --git a/games/ektio/keyboard.c b/games/ektio/keyboard.c new file mode 100644 index 0000000..68ee47b --- /dev/null +++ b/games/ektio/keyboard.c @@ -0,0 +1,176 @@ +#include + +#include +#include +#include +#include + +#include "ektio/backend.h" +#include "ektio/config.h" +#include "ektio/ektio.h" +#include "ektio/keyboard.h" + +#include "util/dprintf.h" + +static void ekt_kb_get_gamebtns(uint32_t* gamebtn_out); +static void ekt_kb_get_trackball(uint16_t* x, uint16_t* y); + +static const struct ekt_io_backend ekt_kb_backend = { + .get_gamebtns = ekt_kb_get_gamebtns, + .get_trackball = ekt_kb_get_trackball +}; + +static uint16_t current_x; +static uint16_t current_y; + +static struct ekt_kb_config config; + +HRESULT ekt_kb_init(const struct ekt_kb_config* cfg, const struct ekt_io_backend** backend) { + assert(cfg != NULL); + assert(backend != NULL); + + dprintf("Keyboard: Using keyboard input\n"); + *backend = &ekt_kb_backend; + config = *cfg; + + return S_OK; +} + +static void ekt_kb_get_gamebtns(uint32_t* gamebtn_out) { + assert(gamebtn_out != NULL); + + uint32_t gamebtn = 0; + + if (GetAsyncKeyState(config.vk_hougu) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_HOUGU; + } + + if (GetAsyncKeyState(config.vk_menu) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_MENU; + } + + if (GetAsyncKeyState(config.vk_start) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_START; + } + + if (GetAsyncKeyState(config.vk_stratagem) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_STRATAGEM; + } + + if (GetAsyncKeyState(config.vk_stratagem_lock) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_STRATAGEM_LOCK; + } + + if (GetAsyncKeyState(config.vk_ryuuha) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_RYUUHA; + } + + if (GetAsyncKeyState(config.vk_tenkey_0) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_NUMPAD_0; + } + + if (GetAsyncKeyState(config.vk_tenkey_1) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_NUMPAD_1; + } + + if (GetAsyncKeyState(config.vk_tenkey_2) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_NUMPAD_2; + } + + if (GetAsyncKeyState(config.vk_tenkey_3) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_NUMPAD_3; + } + + if (GetAsyncKeyState(config.vk_tenkey_4) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_NUMPAD_4; + } + + if (GetAsyncKeyState(config.vk_tenkey_5) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_NUMPAD_5; + } + + if (GetAsyncKeyState(config.vk_tenkey_6) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_NUMPAD_6; + } + + if (GetAsyncKeyState(config.vk_tenkey_7) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_NUMPAD_7; + } + + if (GetAsyncKeyState(config.vk_tenkey_8) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_NUMPAD_8; + } + + if (GetAsyncKeyState(config.vk_tenkey_9) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_NUMPAD_9; + } + + if (GetAsyncKeyState(config.vk_tenkey_clear) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_NUMPAD_CLEAR; + } + + if (GetAsyncKeyState(config.vk_tenkey_enter) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_NUMPAD_ENTER; + } + + if (GetAsyncKeyState(config.vk_vol_down) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_VOL_DOWN; + } + + if (GetAsyncKeyState(config.vk_vol_up) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_VOL_UP; + } + + if (GetAsyncKeyState(config.vk_terminal_cancel) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_TERMINAL_CANCEL; + } + + if (GetAsyncKeyState(config.vk_terminal_decide) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_TERMINAL_DECIDE; + } + + if (GetAsyncKeyState(config.vk_terminal_up) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_TERMINAL_UP; + } + + if (GetAsyncKeyState(config.vk_terminal_right) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_TERMINAL_RIGHT; + } + + if (GetAsyncKeyState(config.vk_terminal_down) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_TERMINAL_DOWN; + } + + if (GetAsyncKeyState(config.vk_terminal_left) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_TERMINAL_LEFT; + } + + if (GetAsyncKeyState(config.vk_terminal_left_2) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_TERMINAL_LEFT_2; + } + + if (GetAsyncKeyState(config.vk_terminal_right_2) & 0x8000) { + gamebtn |= EKT_IO_GAMEBTN_TERMINAL_RIGHT_2; + } + + *gamebtn_out = gamebtn; +} + +static void ekt_kb_get_trackball(uint16_t* x, uint16_t* y) { + assert(x != NULL); + assert(y != NULL); + + if (GetAsyncKeyState(config.x_down) & 0x8000) { + current_x -= config.speed; + } else if (GetAsyncKeyState(config.x_up) & 0x8000) { + current_x += config.speed; + } + if (GetAsyncKeyState(config.y_down) & 0x8000) { + current_y += config.speed; + } else if (GetAsyncKeyState(config.y_up) & 0x8000) { + current_y -= config.speed; + } + + *x = current_x; + *y = current_y; +} diff --git a/games/ektio/keyboard.h b/games/ektio/keyboard.h new file mode 100644 index 0000000..d0b25dd --- /dev/null +++ b/games/ektio/keyboard.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +#include "ektio/backend.h" +#include "ektio/config.h" + +HRESULT ekt_kb_init(const struct ekt_kb_config *cfg, const struct ekt_io_backend **backend); diff --git a/games/ektio/meson.build b/games/ektio/meson.build new file mode 100644 index 0000000..4ea0e68 --- /dev/null +++ b/games/ektio/meson.build @@ -0,0 +1,18 @@ +ektio_lib = static_library( + 'ektio', + name_prefix : '', + include_directories : inc, + implicit_include_directories : false, + dependencies : [ + xinput_lib, + ], + sources : [ + 'ektio.c', + 'ektio.h', + 'config.c', + 'config.h', + 'backend.h', + 'keyboard.c', + 'keyboard.h', + ], +) diff --git a/games/fgohook/config.c b/games/fgohook/config.c index 766240c..b5b1d24 100644 --- a/games/fgohook/config.c +++ b/games/fgohook/config.c @@ -120,7 +120,7 @@ void fgo_hook_config_load( io4_config_load(&cfg->io4, filename); vfd_config_load(&cfg->vfd, filename); touch_screen_config_load(&cfg->touch, filename); - printer_config_load(&cfg->printer, filename); + printer_chc_config_load(&cfg->printer, filename); fgo_deck_config_load(&cfg->deck, filename); ftdi_config_load(&cfg->ftdi, filename); led15093_config_load(&cfg->led15093, filename); diff --git a/games/fgohook/config.h b/games/fgohook/config.h index ca8895a..57b8bdd 100644 --- a/games/fgohook/config.h +++ b/games/fgohook/config.h @@ -7,7 +7,7 @@ #include "hooklib/dvd.h" #include "hooklib/touch.h" -#include "hooklib/printer.h" +#include "hooklib/printer_chc.h" #include "gfxhook/config.h" @@ -24,7 +24,7 @@ struct fgo_hook_config { struct io4_config io4; struct vfd_config vfd; struct touch_screen_config touch; - struct printer_config printer; + struct printer_chc_config printer; struct deck_config deck; struct ftdi_config ftdi; struct led15093_config led15093; diff --git a/games/fgohook/dllmain.c b/games/fgohook/dllmain.c index fcf5132..43b36dc 100644 --- a/games/fgohook/dllmain.c +++ b/games/fgohook/dllmain.c @@ -29,7 +29,7 @@ #include "hooklib/dll.h" #include "hooklib/dvd.h" #include "hooklib/touch.h" -#include "hooklib/printer.h" +#include "hooklib/printer_chc.h" #include "hooklib/createprocess.h" #include "hooklib/serial.h" #include "hooklib/spike.h" @@ -81,7 +81,7 @@ static DWORD CALLBACK fgo_pre_startup(void) /* Hook external DLL APIs */ - printer_hook_init(&fgo_hook_cfg.printer, 4, fgo_hook_mod); + printer_chc_hook_init(&fgo_hook_cfg.printer, 4, fgo_hook_mod); if (fgo_hook_cfg.printer.enable) { dll_hook_push(fgo_hook_mod, L"C330Ausb.dll"); dll_hook_push(fgo_hook_mod, L"C330AFWDLusb.dll"); @@ -143,7 +143,7 @@ static DWORD CALLBACK fgo_pre_startup(void) goto fail; } - hr = createprocess_push_hook_a("am/amdaemon.exe", "inject -d -k fgohook.dll ", "", false); + hr = createprocess_push_hook_a("am/amdaemon.exe", "inject -d -k fgohook.dll ", "", false, false); if (FAILED(hr)) { goto fail; diff --git a/games/fgohook/fgohook.def b/games/fgohook/fgohook.def index b341a65..b7dee86 100644 --- a/games/fgohook/fgohook.def +++ b/games/fgohook/fgohook.def @@ -56,7 +56,7 @@ EXPORTS chcusb_selectPrinter chcusb_selectPrinterSN chcusb_getPrinterInfo - chcusb_imageformat=chcusb_imageformat_330 + chcusb_imageformat chcusb_setmtf chcusb_makeGamma chcusb_setIcctable diff --git a/games/kemonohook/config.c b/games/kemonohook/config.c index c008d88..bd539dd 100644 --- a/games/kemonohook/config.c +++ b/games/kemonohook/config.c @@ -126,7 +126,7 @@ void kemono_hook_config_load( vfd_config_load(&cfg->vfd, filename); kemono_dll_config_load(&cfg->dll, filename); unity_config_load(&cfg->unity, filename); - printer_config_load(&cfg->printer, filename); + printer_chc_config_load(&cfg->printer, filename); amex_config_load(&cfg->amex, filename); led15093_config_load(&cfg->led15093, filename); } diff --git a/games/kemonohook/config.h b/games/kemonohook/config.h index bf76cff..2d3e1ea 100644 --- a/games/kemonohook/config.h +++ b/games/kemonohook/config.h @@ -22,7 +22,7 @@ struct kemono_hook_config { struct vfd_config vfd; struct kemono_dll_config dll; struct unity_config unity; - struct printer_config printer; + struct printer_chc_config printer; struct amex_config amex; struct led15093_config led15093; }; diff --git a/games/kemonohook/dllmain.c b/games/kemonohook/dllmain.c index f429299..60d53da 100644 --- a/games/kemonohook/dllmain.c +++ b/games/kemonohook/dllmain.c @@ -7,7 +7,7 @@ #include "hook/iohook.h" #include "hook/process.h" #include "hook/table.h" -#include "hooklib/printer.h" +#include "hooklib/printer_chc.h" #include "hooklib/serial.h" #include "hooklib/spike.h" #include "kemonohook/config.h" @@ -66,7 +66,7 @@ static DWORD CALLBACK kemono_pre_startup(void) { goto fail; } - printer_hook_init(&kemono_hook_cfg.printer, 0, kemono_hook_mod); + printer_chc_hook_init(&kemono_hook_cfg.printer, 0, kemono_hook_mod); printer_set_dimensions(720, 1028); // printer doesn't call setimageformat /* Initialize emulation hooks */ diff --git a/games/sekitohook/config.c b/games/sekitohook/config.c new file mode 100644 index 0000000..4aae6cb --- /dev/null +++ b/games/sekitohook/config.c @@ -0,0 +1,113 @@ +#include "amex/config.h" + +#include +#include + +#include "board/config.h" +#include "gfxhook/config.h" + +#include "sekitohook/config.h" +#include "sekitohook/sekito-dll.h" + +#include "hooklib/config.h" + +#include "platform/config.h" + +void led15093_config_load(struct led15093_config *cfg, const wchar_t *filename) +{ + assert(cfg != NULL); + assert(filename != NULL); + + wchar_t tmpstr[16]; + + memset(cfg->board_number, ' ', sizeof(cfg->board_number)); + memset(cfg->chip_number, ' ', sizeof(cfg->chip_number)); + memset(cfg->boot_chip_number, ' ', sizeof(cfg->boot_chip_number)); + + cfg->enable = GetPrivateProfileIntW(L"led15093", L"enable", 1, filename); + cfg->port_no[0] = GetPrivateProfileIntW(L"led15093", L"portNo1", 0, filename); + cfg->port_no[1] = GetPrivateProfileIntW(L"led15093", L"portNo2", 0, filename); + cfg->high_baudrate = GetPrivateProfileIntW(L"led15093", L"highBaud", 0, filename); + cfg->fw_ver = GetPrivateProfileIntW(L"led15093", L"fwVer", 0xA0, filename); + cfg->fw_sum = GetPrivateProfileIntW(L"led15093", L"fwSum", 0xAA53, filename); + + GetPrivateProfileStringW( + L"led15093", + L"boardNumber", + L"15093-06", + tmpstr, + _countof(tmpstr), + filename); + + size_t n = wcstombs(cfg->board_number, tmpstr, sizeof(cfg->board_number)); + for (int i = n; i < sizeof(cfg->board_number); i++) + { + cfg->board_number[i] = ' '; + } + + GetPrivateProfileStringW( + L"led15093", + L"chipNumber", + L"6710A", + tmpstr, + _countof(tmpstr), + filename); + + n = wcstombs(cfg->chip_number, tmpstr, sizeof(cfg->chip_number)); + for (int i = n; i < sizeof(cfg->chip_number); i++) + { + cfg->chip_number[i] = ' '; + } + + GetPrivateProfileStringW( + L"led15093", + L"bootChipNumber", + L"6709 ", + tmpstr, + _countof(tmpstr), + filename); + + n = wcstombs(cfg->boot_chip_number, tmpstr, sizeof(cfg->boot_chip_number)); + for (int i = n; i < sizeof(cfg->boot_chip_number); i++) + { + cfg->boot_chip_number[i] = ' '; + } +} + +void sekito_dll_config_load( + struct sekito_dll_config *cfg, + const wchar_t *filename) +{ + assert(cfg != NULL); + assert(filename != NULL); + + GetPrivateProfileStringW( + L"sekitoio", + L"path", + L"", + cfg->path, + _countof(cfg->path), + filename); +} + + +void sekito_hook_config_load( + struct sekito_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); + aime_config_load_bykey(&cfg->aime_queue, filename, L"aime2"); + io4_config_load(&cfg->io4, filename); + dvd_config_load(&cfg->dvd, filename); + led15093_config_load(&cfg->led15093, filename); + y3_config_load(&cfg->y3, filename); + printer_chc_config_load(&cfg->printer, filename); + sekito_dll_config_load(&cfg->dll, filename); + gfx_config_load(&cfg->gfx, filename); + amex_config_load(&cfg->amex, filename); + amvideo_config_load(&cfg->amvideo, filename); +} diff --git a/games/sekitohook/config.h b/games/sekitohook/config.h new file mode 100644 index 0000000..a1274b5 --- /dev/null +++ b/games/sekitohook/config.h @@ -0,0 +1,41 @@ +#pragma once + +#include + +#include "amex/amex.h" +#include "board/sg-reader.h" +#include "board/config.h" +#include "board/led15093.h" +#include "gfxhook/gfx.h" + +#include "sekitohook/sekito-dll.h" + +#include "hooklib/config.h" +#include "hooklib/dvd.h" +#include "hooklib/printer_chc.h" + +#include "platform/config.h" + + +struct sekito_hook_config { + struct platform_config platform; + struct aime_config aime; + struct aime_config aime_queue; + struct io4_config io4; + struct dvd_config dvd; + struct led15093_config led15093; + struct y3_config y3; + struct sekito_dll_config dll; + struct printer_chc_config printer; + struct gfx_config gfx; + struct amex_config amex; + struct amvideo_config amvideo; +}; + +void sekito_dll_config_load( + struct sekito_dll_config *cfg, + const wchar_t *filename); + +void sekito_hook_config_load( + struct sekito_hook_config *cfg, + const wchar_t *filename); diff --git a/games/sekitohook/dllmain.c b/games/sekitohook/dllmain.c new file mode 100644 index 0000000..29b6305 --- /dev/null +++ b/games/sekitohook/dllmain.c @@ -0,0 +1,207 @@ +/* + "Sangokushi Taisen" (sekito) hook + + Devices + + USB: 837-14572 "Type 3" I/O Board + COM12: 837-15084 "Gen 2" Aime Reader + + [Satellite] + + USB: Sinfonia CHC-C320 Printer + COM1: 837-15093-06 LED Controller Board + COM10: 601-13160-01 "Flat Panel Reader" Y3CR BD SIE F720MM Board + COM11: Printer Camera + + [Terminal] + + COM1: 837-15084 "Gen 2" Aime Reader + COM11: 837-15093-06 LED Controller Board +*/ + +#include + +#include + +#include "amex/amex.h" +#include "board/sg-reader.h" +#include "board/led15093.h" +#include "board/sg-reader-queue.h" + +#include "gfxhook/d3d9.h" +#include "gfxhook/gfx.h" + +#include "hook/process.h" +#include "hook/iohook.h" + +#include "hooklib/dll.h" +#include "hooklib/serial.h" +#include "hooklib/spike.h" +#include "hooklib/y3.h" + +#include "sekitohook/config.h" +#include "sekitohook/jvs.h" + +#include "platform/platform.h" + +#include "util/dprintf.h" +#include "util/env.h" +#include "util/fg-detect.h" + +static HMODULE sekito_hook_mod; +static process_entry_t sekito_startup; +static struct sekito_hook_config sekito_hook_cfg; + +static DWORD CALLBACK sekito_pre_startup(void) +{ + HMODULE dbghelp; + HRESULT hr; + bool is_terminal; + + dprintf("--- Begin sekito_pre_startup ---\n"); + + /* Pin dbghelp so the path hooks apply to it. */ + + dbghelp = LoadLibraryW(L"dbghelp.dll"); + + if (dbghelp != NULL) { + dprintf("Pinned debug helper library, hMod=%p\n", dbghelp); + } else { + dprintf("Failed to load debug helper library!\n"); + } + + /* Load config */ + + sekito_hook_config_load(&sekito_hook_cfg, get_config_path()); + + /* Hook Win32 APIs */ + + dvd_hook_init(&sekito_hook_cfg.dvd, sekito_hook_mod); + gfx_hook_init(&sekito_hook_cfg.gfx); + gfx_d3d9_hook_init(&sekito_hook_cfg.gfx, sekito_hook_mod); + serial_hook_init(); + sekito_io_init(); + + /* Hook external DLL APIs */ + + hr = y3_hook_init(&sekito_hook_cfg.y3, sekito_hook_mod, get_config_path()); + + if (FAILED(hr)) { + goto fail; + } + + printer_chc_hook_init(&sekito_hook_cfg.printer, 0, sekito_hook_mod); + if (sekito_hook_cfg.printer.enable) { + dll_hook_push(sekito_hook_mod, L"C320Ausb.dll"); + dll_hook_push(sekito_hook_mod, L"C320AFWDLusb.dll"); + } + + /* Initialize emulation hooks */ + + hr = platform_hook_init( + &sekito_hook_cfg.platform, + "SDDD", + "AAV2", + sekito_hook_mod); + + if (FAILED(hr)) { + goto fail; + } + + /* Initialize Terminal/Satellite hooks */ + if (strncmp(sekito_hook_cfg.platform.nusec.platform_id, "AAV1", 4) == 0) { + // Terminal + is_terminal = true; + } else if (strncmp(sekito_hook_cfg.platform.nusec.platform_id, "AAV2", 4) == 0) { + // Satellite + is_terminal = false; + } else { + // Unknown + dprintf("Unknown platform ID: %s\n", sekito_hook_cfg.platform.nusec.platform_id); + goto fail; + } + + dprintf("System: Cabinet Type: %s\n", is_terminal ? "Terminal" : "Satellite"); + + // LED: terminal uses COM 11 and satellite use COM 1 + unsigned int led_port_no[2] = {is_terminal ? 11 : 1, 0}; + + hr = sekito_dll_init(&sekito_hook_cfg.dll, sekito_hook_mod); + + if (FAILED(hr)) { + goto fail; + } + + hr = led15093_hook_init(&sekito_hook_cfg.led15093, + sekito_dll.led_init, sekito_dll.led_set_leds, led_port_no); + + if (FAILED(hr)) { + goto fail; + } + + if (is_terminal) { + hr = sg_reader_queue_hook_init(&sekito_hook_cfg.aime_queue, 12, 2, + sekito_hook_mod); + + if (FAILED(hr)) { + goto fail; + } + + hr = sg_reader_hook_init(&sekito_hook_cfg.aime, 1, 2, + sekito_hook_mod); + + if (FAILED(hr)) { + goto fail; + } + } else { + hr = sg_reader_hook_init(&sekito_hook_cfg.aime, 12, 2, + sekito_hook_mod); + + if (FAILED(hr)) { + goto fail; + } + } + + sekito_jvs_set_terminal(is_terminal); + hr = amex_hook_init(&sekito_hook_cfg.amex, sekito_jvs_init); + + if (FAILED(hr)) { + goto fail; + } + + if (sekito_hook_cfg.amex.jvs.enable && sekito_hook_cfg.amex.jvs.foreground) { + fgdet_init(L"teaGfx DirectX Release", false); + } + + /* Initialize debug helpers */ + + spike_hook_init(get_config_path()); + + dprintf("--- End sekito_pre_startup ---\n"); + + /* Jump to EXE start address */ + + return sekito_startup(); + +fail: + ExitProcess(EXIT_FAILURE); +} + +BOOL WINAPI DllMain(HMODULE mod, DWORD cause, void *ctx) +{ + HRESULT hr; + + if (cause != DLL_PROCESS_ATTACH) { + return TRUE; + } + + sekito_hook_mod = mod; + + hr = process_hijack_startup(sekito_pre_startup, &sekito_startup); + + if (!SUCCEEDED(hr)) { + dprintf("Failed to hijack process startup: %x\n", (int) hr); + } + + return SUCCEEDED(hr); +} diff --git a/games/sekitohook/jvs.c b/games/sekitohook/jvs.c new file mode 100644 index 0000000..a7c2c0e --- /dev/null +++ b/games/sekitohook/jvs.c @@ -0,0 +1,258 @@ +#include "jvs.h" + +#include + +#include +#include +#include +#include + +#include "amex/jvs.h" + +#include "board/io3.h" + +#include "sekitohook/config.h" + +#include "jvs/jvs-bus.h" + +#include "util/dprintf.h" + +static void sekito_jvs_read_switches(void* ctx, struct io3_switch_state* out); + +static void sekito_jvs_read_coin_counter( + void* ctx, + uint8_t slot_no, + uint16_t* out); + +static void sekito_jvs_read_rotary( + void* ctx, + uint16_t* rotary, + uint8_t nrotary); + +static const struct io3_ops sekito_jvs_io3_ops = { + .read_switches = sekito_jvs_read_switches, + .read_rotarys = sekito_jvs_read_rotary, + .read_coin_counter = sekito_jvs_read_coin_counter, +}; + +static struct io3 sekito_jvs_io3; + +static bool io_is_terminal; + +HRESULT sekito_jvs_init(struct jvs_node** out) { + assert(out != NULL); + + dprintf("JVS I/O: Starting JVS\n"); + + io3_init(&sekito_jvs_io3, NULL, &sekito_jvs_io3_ops, NULL); + *out = io3_to_jvs_node(&sekito_jvs_io3); + + return S_OK; +} + +void sekito_jvs_set_terminal(bool is_terminal) { + dprintf("JVS I/O: Terminal: %d\n", is_terminal); + io_is_terminal = is_terminal; +} + +static void sekito_jvs_read_switches(void* ctx, struct io3_switch_state* out) { + assert(out != NULL); + + uint8_t opbtn; + uint32_t gamebtn; + + assert(sekito_dll.poll != NULL); + assert(sekito_dll.get_opbtns != NULL); + assert(sekito_dll.get_gamebtns != NULL); + + memset(out, 0, sizeof(*out)); + + sekito_dll.poll(); + + opbtn = 0; + gamebtn = 0; + + sekito_dll.get_opbtns(&opbtn); + sekito_dll.get_gamebtns(&gamebtn); + + if (opbtn & SEKITO_IO_OPBTN_TEST) { + out->system = 0x80; + } else { + out->system = 0; + } + + if (opbtn & SEKITO_IO_OPBTN_SERVICE) { + out->p1 |= 1 << 1; + } + + if (opbtn & SEKITO_IO_OPBTN_SW1) { + out->p1 |= 1 << 10; + } + + if (opbtn & SEKITO_IO_OPBTN_SW2) { + out->p1 |= 1 << 11; + } + + if (opbtn & SEKITO_IO_OPBTN_COIN) { + out->p1 |= 1 << 14; + } + + + if (!io_is_terminal) { + if (gamebtn & SEKITO_IO_GAMEBTN_HOUGU) { + out->p2 |= 1 << 6; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_MENU) { + out->p2 |= 1 << 4; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_START) { + out->p1 |= 1 << 15; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_STRATAGEM) { + out->p2 |= 1 << 7; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_STRATAGEM_LOCK) { + out->p2 |= 1 << 5; + } + + out->p1 |= 0 << 2; // card_sensor + out->p2 |= 1 << 2; // open_sensor + out->p2 |= 1 << 3; // lock + } + + if (gamebtn & SEKITO_IO_GAMEBTN_NUMPAD_0) { + out->p1 |= SEKITO_NUMPAD_C2; + out->p1 |= SEKITO_NUMPAD_R4; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_NUMPAD_1) { + out->p1 |= SEKITO_NUMPAD_C1; + out->p1 |= SEKITO_NUMPAD_R1; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_NUMPAD_2) { + out->p1 |= SEKITO_NUMPAD_C2; + out->p1 |= SEKITO_NUMPAD_R1; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_NUMPAD_3) { + out->p1 |= SEKITO_NUMPAD_C3; + out->p1 |= SEKITO_NUMPAD_R1; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_NUMPAD_4) { + out->p1 |= SEKITO_NUMPAD_C1; + out->p1 |= SEKITO_NUMPAD_R2; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_NUMPAD_5) { + out->p1 |= SEKITO_NUMPAD_C2; + out->p1 |= SEKITO_NUMPAD_R2; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_NUMPAD_6) { + out->p1 |= SEKITO_NUMPAD_C3; + out->p1 |= SEKITO_NUMPAD_R2; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_NUMPAD_7) { + out->p1 |= SEKITO_NUMPAD_C1; + out->p1 |= SEKITO_NUMPAD_R3; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_NUMPAD_8) { + out->p1 |= SEKITO_NUMPAD_C2; + out->p1 |= SEKITO_NUMPAD_R3; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_NUMPAD_9) { + out->p1 |= SEKITO_NUMPAD_C3; + out->p1 |= SEKITO_NUMPAD_R3; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_NUMPAD_CLEAR) { + out->p1 |= SEKITO_NUMPAD_C1; + out->p1 |= SEKITO_NUMPAD_R4; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_NUMPAD_ENTER) { + out->p1 |= SEKITO_NUMPAD_C3; + out->p1 |= SEKITO_NUMPAD_R4; + } + + if (io_is_terminal) { + if (gamebtn & SEKITO_IO_GAMEBTN_TERMINAL_CANCEL) { + out->p2 |= 1 << 8; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_TERMINAL_DECIDE) { + out->p2 |= 1 << 9; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_TERMINAL_LEFT) { + out->p1 |= 1 << 11; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_TERMINAL_UP) { + out->p1 |= 1 << 13; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_TERMINAL_RIGHT) { + out->p1 |= 1 << 10; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_TERMINAL_DOWN) { + out->p1 |= 1 << 12; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_TERMINAL_LEFT_2) { + out->p2 |= 1 << 11; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_TERMINAL_RIGHT_2) { + out->p2 |= 1 << 10; + } + + if (gamebtn & SEKITO_IO_GAMEBTN_TERMINAL_RESERVE) { + out->p2 |= 1 << 3; + } + + } + +} + +static void sekito_jvs_read_rotary( + void* ctx, + uint16_t* rotary, + uint8_t nrotary) { + assert(rotary != NULL); + assert(sekito_dll.get_trackball_position != NULL); + + uint16_t x, y; + + x = 0; + y = 0; + + sekito_dll.get_trackball_position(&x, &y); + + if (nrotary >= 4) { + rotary[2] = x; + rotary[3] = y; + } +} + +static void sekito_jvs_read_coin_counter( + void* ctx, + uint8_t slot_no, + uint16_t* out) { + if (slot_no > 0) { + return; + } + + // unused(!) + *out = 0; +} diff --git a/games/sekitohook/jvs.h b/games/sekitohook/jvs.h new file mode 100644 index 0000000..fa0584e --- /dev/null +++ b/games/sekitohook/jvs.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +#include "jvs/jvs-bus.h" + +enum { + SEKITO_NUMPAD_R1 = 1 << 9, + SEKITO_NUMPAD_R2 = 1 << 8, + SEKITO_NUMPAD_R3 = 1 << 7, + SEKITO_NUMPAD_R4 = 1 << 6, + SEKITO_NUMPAD_C1 = 1 << 5, + SEKITO_NUMPAD_C2 = 1 << 4, + SEKITO_NUMPAD_C3 = 1 << 3 +}; + +HRESULT sekito_jvs_init(struct jvs_node **root); +void sekito_jvs_set_terminal(bool is_terminal); diff --git a/games/sekitohook/meson.build b/games/sekitohook/meson.build new file mode 100644 index 0000000..c8c647d --- /dev/null +++ b/games/sekitohook/meson.build @@ -0,0 +1,32 @@ +shared_library( + 'sekitohook', + name_prefix : '', + include_directories : inc, + implicit_include_directories : false, + vs_module_defs : 'sekitohook.def', + dependencies : [ + capnhook.get_variable('hook_dep'), + capnhook.get_variable('hooklib_dep') + ], + link_with : [ + aimeio_lib, + amex_lib, + board_lib, + hooklib_lib, + gfxhook_lib, + jvs_lib, + platform_lib, + sekitoio_lib, + util_lib, + y3io_lib, + ], + sources : [ + 'config.c', + 'config.h', + 'dllmain.c', + 'jvs.c', + 'jvs.h', + 'sekito-dll.c', + 'sekito-dll.h', + ], +) diff --git a/games/sekitohook/sekito-dll.c b/games/sekitohook/sekito-dll.c new file mode 100644 index 0000000..a0d9b2e --- /dev/null +++ b/games/sekitohook/sekito-dll.c @@ -0,0 +1,118 @@ +#include + +#include +#include + +#include "sekitohook/sekito-dll.h" + +#include "util/dll-bind.h" +#include "util/dprintf.h" + +const struct dll_bind_sym sekito_dll_syms[] = { + { + .sym = "sekito_io_init", + .off = offsetof(struct sekito_dll, init), + }, { + .sym = "sekito_io_poll", + .off = offsetof(struct sekito_dll, poll), + }, { + .sym = "sekito_io_get_opbtns", + .off = offsetof(struct sekito_dll, get_opbtns), + }, { + .sym = "sekito_io_get_gamebtns", + .off = offsetof(struct sekito_dll, get_gamebtns), + }, { + .sym = "sekito_io_get_trackball_position", + .off = offsetof(struct sekito_dll, get_trackball_position), + }, { + .sym = "sekito_io_led_init", + .off = offsetof(struct sekito_dll, led_init), + }, { + .sym = "sekito_io_led_set_colors", + .off = offsetof(struct sekito_dll, led_set_leds), + } +}; + +struct sekito_dll sekito_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 sekito_dll_init(const struct sekito_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("Sekito IO: Failed to load IO DLL: %lx: %S\n", + hr, + cfg->path); + + goto end; + } + + dprintf("Sekito IO: Using custom IO DLL: %S\n", cfg->path); + src = owned; + } else { + owned = NULL; + src = self; + } + + get_api_version = (void *) GetProcAddress(src, "sekito_io_get_api_version"); + + if (get_api_version != NULL) { + sekito_dll.api_version = get_api_version(); + } else { + sekito_dll.api_version = 0x0100; + dprintf("Custom IO DLL does not expose sekito_io_get_api_version, " + "assuming API version 1.0.\n" + "Please ask the developer to update their DLL.\n"); + } + + if (sekito_dll.api_version >= 0x0200) { + hr = E_NOTIMPL; + dprintf("Sekito IO: Custom IO DLL implements an unsupported " + "API version (%#04x). Please update Segatools.\n", + sekito_dll.api_version); + + goto end; + } + + sym = sekito_dll_syms; + hr = dll_bind(&sekito_dll, src, &sym, _countof(sekito_dll_syms)); + + if (FAILED(hr)) { + if (src != self) { + dprintf("Sekito 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; +} diff --git a/games/sekitohook/sekito-dll.h b/games/sekitohook/sekito-dll.h new file mode 100644 index 0000000..1f2560f --- /dev/null +++ b/games/sekitohook/sekito-dll.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +#include "sekitoio/sekitoio.h" + +struct sekito_dll { + uint16_t api_version; + HRESULT (*init)(void); + HRESULT (*poll)(void); + void (*get_opbtns)(uint8_t *opbtn); + void (*get_gamebtns)(uint32_t *gamebtn); + void (*get_trackball_position)(uint16_t *x, uint16_t *y); + HRESULT (*led_init)(void); + void (*led_set_leds)(uint8_t board, uint8_t *rgb); +}; + +struct sekito_dll_config { + wchar_t path[MAX_PATH]; +}; + +extern struct sekito_dll sekito_dll; + +HRESULT sekito_dll_init(const struct sekito_dll_config *cfg, HINSTANCE self); diff --git a/games/sekitohook/sekitohook.def b/games/sekitohook/sekitohook.def new file mode 100644 index 0000000..6153e94 --- /dev/null +++ b/games/sekitohook/sekitohook.def @@ -0,0 +1,78 @@ +LIBRARY sekitohook + +EXPORTS + Direct3DCreate9 + aime_io_get_api_version + aime_io_init + aime_io_led_set_color + aime_io_vfd_set_text + aime_io_vfd_set_state + aime_io_nfc_get_aime_id + aime_io_nfc_get_felica_id + aime_io_nfc_poll + aime_io_nfc_get_mifare_uid + aime_io_nfc_mifare_select + aime_io_nfc_mifare_set_key + aime_io_nfc_mifare_authenticate + aime_io_nfc_mifare_read_block + aime_io_nfc_felica_transact + aime_io_nfc_radio_on + aime_io_nfc_radio_off + aime_io_nfc_to_update_mode + aime_io_nfc_send_hex_data + sekito_io_get_api_version + sekito_io_get_gamebtns + sekito_io_get_opbtns + sekito_io_get_trackball_position + sekito_io_init + sekito_io_poll + sekito_io_led_init + sekito_io_led_set_colors + y3_io_get_api_version + y3_io_init + y3_io_close + y3_io_get_cards + amDllVideoClose + amDllVideoGetVBiosVersion + amDllVideoOpen + amDllVideoSetResolution + API_DLLVersion @1 + API_GetLastError @2 + API_GetErrorMessage @3 + API_Connect @4 + API_Close @5 + API_Start @6 + API_Stop @7 + API_GetFirmVersion @8 + API_GetFirmName @9 + API_GetTargetCode @10 + API_GetStatus @11 + API_GetCounter @12 + API_ClearError @13 + API_Reset @14 + API_GetCardInfo @15 + API_GetCardInfoCharSize @16 + API_SetDevice @17 + API_SetCommand @18 + API_FirmwareUpdate @19 + API_Calibration @20 + API_GetCalibrationResult @21 + API_GetProcTime @22 + API_GetMemStatus @23 + API_GetMemCounter @24 + API_SetSysControl @25 + API_GetSysControl @26 + API_SetParameter @27 + API_GetParameter @28 + API_TestReset @29 + API_DebugReset @30 + API_GetBoardType @31 + API_GetCardDataSize @32 + API_GetFirmDate @33 + API_SystemCommand @34 + API_CalcCheckSum @35 + API_GetCheckSumResult @36 + API_BlockRead @37 + API_GetBlockReadResult @38 + API_BlockWrite @39 + API_GetDebugParam @40 diff --git a/games/sekitoio/backend.h b/games/sekitoio/backend.h new file mode 100644 index 0000000..6c84ca5 --- /dev/null +++ b/games/sekitoio/backend.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +#include "sekitoio/sekitoio.h" + +struct sekito_io_backend { + void (*get_gamebtns)(uint32_t *gamebtn); + void (*get_trackball)(uint16_t *x, uint16_t *y); +}; diff --git a/games/sekitoio/config.c b/games/sekitoio/config.c new file mode 100644 index 0000000..bdd94bd --- /dev/null +++ b/games/sekitoio/config.c @@ -0,0 +1,74 @@ +#include + +#include +#include +#include + +#include "sekitoio/config.h" + +#include + + +void sekito_kb_config_load( + struct sekito_kb_config *cfg, + const wchar_t *filename) { + + cfg->vk_menu = GetPrivateProfileIntW(L"keyboard", L"menu", 'A', filename); + cfg->vk_start = GetPrivateProfileIntW(L"keyboard", L"start", 'S', filename); + cfg->vk_stratagem = GetPrivateProfileIntW(L"keyboard", L"stratagem", 'D', filename); + cfg->vk_stratagem_lock = GetPrivateProfileIntW(L"keyboard", L"stratagem_lock", 'F', filename); + cfg->vk_hougu = GetPrivateProfileIntW(L"keyboard", L"hougu", 'G', filename); + + cfg->vk_tenkey_0 = GetPrivateProfileIntW(L"keyboard", L"tenkey_0", VK_NUMPAD0, filename); + cfg->vk_tenkey_1 = GetPrivateProfileIntW(L"keyboard", L"tenkey_1", VK_NUMPAD1, filename); + cfg->vk_tenkey_2 = GetPrivateProfileIntW(L"keyboard", L"tenkey_2", VK_NUMPAD2, filename); + cfg->vk_tenkey_3 = GetPrivateProfileIntW(L"keyboard", L"tenkey_3", VK_NUMPAD3, filename); + cfg->vk_tenkey_4 = GetPrivateProfileIntW(L"keyboard", L"tenkey_4", VK_NUMPAD4, filename); + cfg->vk_tenkey_5 = GetPrivateProfileIntW(L"keyboard", L"tenkey_5", VK_NUMPAD5, filename); + cfg->vk_tenkey_6 = GetPrivateProfileIntW(L"keyboard", L"tenkey_6", VK_NUMPAD6, filename); + cfg->vk_tenkey_7 = GetPrivateProfileIntW(L"keyboard", L"tenkey_7", VK_NUMPAD7, filename); + cfg->vk_tenkey_8 = GetPrivateProfileIntW(L"keyboard", L"tenkey_8", VK_NUMPAD8, filename); + cfg->vk_tenkey_9 = GetPrivateProfileIntW(L"keyboard", L"tenkey_9", VK_NUMPAD9, filename); + cfg->vk_tenkey_clear = GetPrivateProfileIntW(L"keyboard", L"tenkey_clear", VK_DECIMAL, filename); + cfg->vk_tenkey_enter = GetPrivateProfileIntW(L"keyboard", L"tenkey_enter", VK_RETURN, filename); + + cfg->vk_terminal_decide = GetPrivateProfileIntW(L"keyboard", L"decide", 'A', filename); + cfg->vk_terminal_cancel = GetPrivateProfileIntW(L"keyboard", L"cancel", 'S', filename); + cfg->vk_terminal_up = GetPrivateProfileIntW(L"keyboard", L"up", VK_UP, filename); + cfg->vk_terminal_right = GetPrivateProfileIntW(L"keyboard", L"right", VK_RIGHT, filename); + cfg->vk_terminal_down = GetPrivateProfileIntW(L"keyboard", L"down", VK_DOWN, filename); + cfg->vk_terminal_left = GetPrivateProfileIntW(L"keyboard", L"left", VK_LEFT, filename); + cfg->vk_terminal_left_2 = GetPrivateProfileIntW(L"keyboard", L"left2", 'Q', filename); + cfg->vk_terminal_right_2 = GetPrivateProfileIntW(L"keyboard", L"right2", 'W', filename); + cfg->vk_terminal_reserve = GetPrivateProfileIntW(L"keyboard", L"reserve", 'E', filename); + + cfg->x_down = GetPrivateProfileIntW(L"keyboard", L"trackball_left", VK_LEFT, filename); + cfg->x_up = GetPrivateProfileIntW(L"keyboard", L"trackball_right", VK_RIGHT, filename); + cfg->y_down = GetPrivateProfileIntW(L"keyboard", L"trackball_up", VK_UP, filename); + cfg->y_up = GetPrivateProfileIntW(L"keyboard", L"trackball_down", VK_DOWN, filename); + cfg->speed = GetPrivateProfileIntW(L"keyboard", L"speed_modifier", 1, filename); +} + +void sekito_io_config_load( + struct sekito_io_config *cfg, + const wchar_t *filename) +{ + 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_sw1 = GetPrivateProfileIntW(L"io4", L"sw1", '4', filename); + cfg->vk_sw2 = GetPrivateProfileIntW(L"io4", L"sw2", '5', filename); + + GetPrivateProfileStringW( + L"io4", + L"mode", + L"keyboard", + cfg->mode, + _countof(cfg->mode), + filename); + + sekito_kb_config_load(&cfg->kb, filename); +} diff --git a/games/sekitoio/config.h b/games/sekitoio/config.h new file mode 100644 index 0000000..9bfeed7 --- /dev/null +++ b/games/sekitoio/config.h @@ -0,0 +1,59 @@ +#pragma once + +#include +#include + +#include + +struct sekito_kb_config { + uint8_t vk_menu; + uint8_t vk_start; + uint8_t vk_stratagem; + uint8_t vk_stratagem_lock; + uint8_t vk_hougu; + + uint8_t vk_tenkey_0; + uint8_t vk_tenkey_1; + uint8_t vk_tenkey_2; + uint8_t vk_tenkey_3; + uint8_t vk_tenkey_4; + uint8_t vk_tenkey_5; + uint8_t vk_tenkey_6; + uint8_t vk_tenkey_7; + uint8_t vk_tenkey_8; + uint8_t vk_tenkey_9; + uint8_t vk_tenkey_clear; + uint8_t vk_tenkey_enter; + + uint8_t vk_terminal_up; + uint8_t vk_terminal_right; + uint8_t vk_terminal_down; + uint8_t vk_terminal_left; + uint8_t vk_terminal_left_2; + uint8_t vk_terminal_right_2; + uint8_t vk_terminal_reserve; + uint8_t vk_terminal_cancel; + uint8_t vk_terminal_decide; + + uint8_t x_down; + uint8_t x_up; + uint8_t y_down; + uint8_t y_up; + uint8_t speed; +}; + +struct sekito_io_config { + uint8_t vk_test; + uint8_t vk_service; + uint8_t vk_coin; + uint8_t vk_sw1; + uint8_t vk_sw2; + + wchar_t mode[12]; + struct sekito_kb_config kb; +}; + +void sekito_kb_config_load(struct sekito_kb_config *cfg, const wchar_t *filename); +void sekito_io_config_load( + struct sekito_io_config *cfg, + const wchar_t *filename); diff --git a/games/sekitoio/keyboard.c b/games/sekitoio/keyboard.c new file mode 100644 index 0000000..d6060d4 --- /dev/null +++ b/games/sekitoio/keyboard.c @@ -0,0 +1,168 @@ +#include + +#include +#include +#include +#include + +#include "sekitoio/backend.h" +#include "sekitoio/config.h" +#include "sekitoio/sekitoio.h" +#include "sekitoio/keyboard.h" + +#include "util/dprintf.h" + +static void sekito_kb_get_gamebtns(uint32_t* gamebtn_out); +static void sekito_kb_get_trackball(uint16_t* x, uint16_t* y); + +static const struct sekito_io_backend sekito_kb_backend = { + .get_gamebtns = sekito_kb_get_gamebtns, + .get_trackball = sekito_kb_get_trackball +}; + +static uint16_t current_x; +static uint16_t current_y; + +static struct sekito_kb_config config; + +HRESULT sekito_kb_init(const struct sekito_kb_config* cfg, const struct sekito_io_backend** backend) { + assert(cfg != NULL); + assert(backend != NULL); + + dprintf("Keyboard: Using keyboard input\n"); + *backend = &sekito_kb_backend; + config = *cfg; + + return S_OK; +} + +static void sekito_kb_get_gamebtns(uint32_t* gamebtn_out) { + assert(gamebtn_out != NULL); + + uint32_t gamebtn = 0; + + if (GetAsyncKeyState(config.vk_hougu) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_HOUGU; + } + + if (GetAsyncKeyState(config.vk_menu) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_MENU; + } + + if (GetAsyncKeyState(config.vk_start) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_START; + } + + if (GetAsyncKeyState(config.vk_stratagem) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_STRATAGEM; + } + + if (GetAsyncKeyState(config.vk_stratagem_lock) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_STRATAGEM_LOCK; + } + + if (GetAsyncKeyState(config.vk_tenkey_0) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_NUMPAD_0; + } + + if (GetAsyncKeyState(config.vk_tenkey_1) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_NUMPAD_1; + } + + if (GetAsyncKeyState(config.vk_tenkey_2) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_NUMPAD_2; + } + + if (GetAsyncKeyState(config.vk_tenkey_3) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_NUMPAD_3; + } + + if (GetAsyncKeyState(config.vk_tenkey_4) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_NUMPAD_4; + } + + if (GetAsyncKeyState(config.vk_tenkey_5) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_NUMPAD_5; + } + + if (GetAsyncKeyState(config.vk_tenkey_6) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_NUMPAD_6; + } + + if (GetAsyncKeyState(config.vk_tenkey_7) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_NUMPAD_7; + } + + if (GetAsyncKeyState(config.vk_tenkey_8) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_NUMPAD_8; + } + + if (GetAsyncKeyState(config.vk_tenkey_9) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_NUMPAD_9; + } + + if (GetAsyncKeyState(config.vk_tenkey_clear) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_NUMPAD_CLEAR; + } + + if (GetAsyncKeyState(config.vk_tenkey_enter) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_NUMPAD_ENTER; + } + + if (GetAsyncKeyState(config.vk_terminal_cancel) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_TERMINAL_CANCEL; + } + + if (GetAsyncKeyState(config.vk_terminal_decide) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_TERMINAL_DECIDE; + } + + if (GetAsyncKeyState(config.vk_terminal_up) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_TERMINAL_UP; + } + + if (GetAsyncKeyState(config.vk_terminal_right) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_TERMINAL_RIGHT; + } + + if (GetAsyncKeyState(config.vk_terminal_down) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_TERMINAL_DOWN; + } + + if (GetAsyncKeyState(config.vk_terminal_left) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_TERMINAL_LEFT; + } + + if (GetAsyncKeyState(config.vk_terminal_left_2) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_TERMINAL_LEFT_2; + } + + if (GetAsyncKeyState(config.vk_terminal_right_2) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_TERMINAL_RIGHT_2; + } + + if (GetAsyncKeyState(config.vk_terminal_reserve) & 0x8000) { + gamebtn |= SEKITO_IO_GAMEBTN_TERMINAL_RESERVE; + } + + *gamebtn_out = gamebtn; +} + +static void sekito_kb_get_trackball(uint16_t* x, uint16_t* y) { + assert(x != NULL); + assert(y != NULL); + + if (GetAsyncKeyState(config.x_down) & 0x8000) { + current_x -= config.speed; + } else if (GetAsyncKeyState(config.x_up) & 0x8000) { + current_x += config.speed; + } + if (GetAsyncKeyState(config.y_down) & 0x8000) { + current_y += config.speed; + } else if (GetAsyncKeyState(config.y_up) & 0x8000) { + current_y -= config.speed; + } + + *x = current_x; + *y = current_y; +} diff --git a/games/sekitoio/keyboard.h b/games/sekitoio/keyboard.h new file mode 100644 index 0000000..ae093e8 --- /dev/null +++ b/games/sekitoio/keyboard.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +#include "sekitoio/backend.h" +#include "sekitoio/config.h" + +HRESULT sekito_kb_init(const struct sekito_kb_config *cfg, const struct sekito_io_backend **backend); diff --git a/games/sekitoio/meson.build b/games/sekitoio/meson.build new file mode 100644 index 0000000..15b1e63 --- /dev/null +++ b/games/sekitoio/meson.build @@ -0,0 +1,18 @@ +sekitoio_lib = static_library( + 'sekitoio', + name_prefix : '', + include_directories : inc, + implicit_include_directories : false, + dependencies : [ + xinput_lib, + ], + sources : [ + 'config.c', + 'config.h', + 'backend.h', + 'keyboard.c', + 'keyboard.h', + 'sekitoio.c', + 'sekitoio.h', + ], +) diff --git a/games/sekitoio/sekitoio.c b/games/sekitoio/sekitoio.c new file mode 100644 index 0000000..75ec654 --- /dev/null +++ b/games/sekitoio/sekitoio.c @@ -0,0 +1,108 @@ +#include +#include +#include + +#include + +#include "sekitoio/sekitoio.h" + +#include + +#include "keyboard.h" +#include "sekitoio/config.h" +#include "util/dprintf.h" +#include "util/env.h" +#include "util/str.h" + +static uint8_t sekito_opbtn; +static uint32_t sekito_gamebtn; +static uint8_t sekito_stick_x; +static uint8_t sekito_stick_y; +static struct sekito_io_config sekito_io_cfg; +static const struct sekito_io_backend* sekito_io_backend; +static bool sekito_io_coin; + +uint16_t sekito_io_get_api_version(void) { + return 0x0100; +} + +HRESULT sekito_io_init(void) { + sekito_io_config_load(&sekito_io_cfg, get_config_path()); + + HRESULT hr; + + if (wstr_ieq(sekito_io_cfg.mode, L"keyboard")) { + hr = sekito_kb_init(&sekito_io_cfg.kb, &sekito_io_backend); + } else { + hr = E_INVALIDARG; + dprintf("Sekito IO: Invalid IO mode \"%S\", use keyboard\n", + sekito_io_cfg.mode); + } + + return hr; +} + +HRESULT sekito_io_poll(void) { + assert(sekito_io_backend != NULL); + + sekito_opbtn = 0; + sekito_gamebtn = 0; + sekito_stick_x = 0; + sekito_stick_y = 0; + + if (GetAsyncKeyState(sekito_io_cfg.vk_test) & 0x8000) { + sekito_opbtn |= SEKITO_IO_OPBTN_TEST; + } + + if (GetAsyncKeyState(sekito_io_cfg.vk_service) & 0x8000) { + sekito_opbtn |= SEKITO_IO_OPBTN_SERVICE; + } + + if (GetAsyncKeyState(sekito_io_cfg.vk_sw1) & 0x8000) { + sekito_opbtn |= SEKITO_IO_OPBTN_SW1; + } + + if (GetAsyncKeyState(sekito_io_cfg.vk_sw2) & 0x8000) { + sekito_opbtn |= SEKITO_IO_OPBTN_SW2; + } + + if (GetAsyncKeyState(sekito_io_cfg.vk_coin) & 0x8000) { + if (!sekito_io_coin) { + sekito_io_coin = true; + sekito_opbtn |= SEKITO_IO_OPBTN_COIN; + } + } else { + sekito_io_coin = false; + } + + return S_OK; +} + +void sekito_io_get_opbtns(uint8_t* opbtn) { + if (opbtn != NULL) { + *opbtn = sekito_opbtn; + } +} + +void sekito_io_get_gamebtns(uint32_t* btn) { + assert(sekito_io_backend != NULL); + assert(btn != NULL); + + sekito_io_backend->get_gamebtns(btn); +} + +void sekito_io_get_trackball_position(uint16_t* stick_x, uint16_t* stick_y) { + assert(sekito_io_backend != NULL); + assert(stick_x != NULL); + assert(stick_y != NULL); + + sekito_io_backend->get_trackball(stick_x, stick_y); +} + +HRESULT sekito_io_led_init(void) { + return S_OK; +} + +void sekito_io_led_set_colors(uint8_t board, uint8_t* rgb) { + return; +} diff --git a/games/sekitoio/sekitoio.h b/games/sekitoio/sekitoio.h new file mode 100644 index 0000000..2e19653 --- /dev/null +++ b/games/sekitoio/sekitoio.h @@ -0,0 +1,104 @@ +#pragma once + +#include + +#include + +enum { + SEKITO_IO_OPBTN_TEST = 0x01, + SEKITO_IO_OPBTN_SERVICE = 0x02, + SEKITO_IO_OPBTN_COIN = 0x04, + SEKITO_IO_OPBTN_SW1 = 0x08, + SEKITO_IO_OPBTN_SW2 = 0x10, +}; + +enum { + SEKITO_IO_GAMEBTN_MENU = 0x01, + SEKITO_IO_GAMEBTN_START = 0x02, + SEKITO_IO_GAMEBTN_STRATAGEM = 0x04, + SEKITO_IO_GAMEBTN_STRATAGEM_LOCK = 0x08, + SEKITO_IO_GAMEBTN_HOUGU = 0x10, + SEKITO_IO_GAMEBTN_NUMPAD_0 = 0x100, + SEKITO_IO_GAMEBTN_NUMPAD_1 = 0x200, + SEKITO_IO_GAMEBTN_NUMPAD_2 = 0x400, + SEKITO_IO_GAMEBTN_NUMPAD_3 = 0x800, + SEKITO_IO_GAMEBTN_NUMPAD_4 = 0x1000, + SEKITO_IO_GAMEBTN_NUMPAD_5 = 0x2000, + SEKITO_IO_GAMEBTN_NUMPAD_6 = 0x4000, + SEKITO_IO_GAMEBTN_NUMPAD_7 = 0x8000, + SEKITO_IO_GAMEBTN_NUMPAD_8 = 0x10000, + SEKITO_IO_GAMEBTN_NUMPAD_9 = 0x20000, + SEKITO_IO_GAMEBTN_NUMPAD_CLEAR = 0x40000, + SEKITO_IO_GAMEBTN_NUMPAD_ENTER = 0x80000, + SEKITO_IO_GAMEBTN_TERMINAL_LEFT = 0x400000, + SEKITO_IO_GAMEBTN_TERMINAL_UP = 0x800000, + SEKITO_IO_GAMEBTN_TERMINAL_RIGHT = 0x1000000, + SEKITO_IO_GAMEBTN_TERMINAL_DOWN = 0x2000000, + SEKITO_IO_GAMEBTN_TERMINAL_LEFT_2 = 0x4000000, + SEKITO_IO_GAMEBTN_TERMINAL_RIGHT_2 = 0x8000000, + SEKITO_IO_GAMEBTN_TERMINAL_DECIDE = 0x10000000, + SEKITO_IO_GAMEBTN_TERMINAL_CANCEL = 0x20000000, + SEKITO_IO_GAMEBTN_TERMINAL_RESERVE = 0x40000000, +}; + +/* Get the version of the Eiketsu Taisen 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 sekito_io_get_api_version(void); + +/* Initialize the IO DLL. This is the second function that will be called on + your DLL, after sekito_io_get_api_version. + + All subsequent calls to this API may originate from arbitrary threads. + + Minimum API version: 0x0100 */ + +HRESULT sekito_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 sekito_io_poll(void); + +/* Get the state of the cabinet's operator buttons as of the last poll. See + SEKITO_IO_OPBTN enum above: this contains bit mask definitions for button + states returned in *opbtn. All buttons are active-high. + + Minimum API version: 0x0100 */ + +void sekito_io_get_opbtns(uint8_t *opbtn); + +/* Get the state of the cabinet's gameplay buttons as of the last poll. See + SEKITO_IO_GAMEBTN enum above: this contains bit mask definitions for button + states returned in *gamebtn. All buttons are active-high. + + Minimum API version: 0x0100 */ + +void sekito_io_get_gamebtns(uint32_t *gamebtn); + +/* Get the position of the trackball as of the last poll. + + Minimum API version: 0x0100 */ + +void sekito_io_get_trackball_position(uint16_t *stick_x, uint16_t *stick_y); + +/* Initialize LED emulation. This function will be called before any + other sekito_io_led_*() function calls. + + All subsequent calls may originate from arbitrary threads and some may + overlap with each other. Ensuring synchronization inside your IO DLL is + your responsibility. */ + +HRESULT sekito_io_led_init(void); + +/* Update the RGB LEDs. + + Exact layout is TBD. */ + +void sekito_io_led_set_colors(uint8_t board, uint8_t *rgb); diff --git a/meson.build b/meson.build index 1e88269..7d35c79 100644 --- a/meson.build +++ b/meson.build @@ -27,6 +27,11 @@ if cc.get_id() != 'msvc' language: 'c', ) + add_global_arguments( + '-Wno-unknown-pragmas', + language: 'c', + ) + add_project_link_arguments( '-Wl,--enable-stdcall-fixup', '-Wl,--exclude-all-symbols', @@ -103,6 +108,7 @@ bcrypt_lib = cc.find_library('bcrypt') inc = include_directories('common', 'games') capnhook = subproject('capnhook') +cwinwebsocket = subproject('cwinwebsocket') subdir('common/amex') subdir('common/iccard') @@ -112,6 +118,7 @@ subdir('common/jvs') subdir('common/platform') subdir('common/util') subdir('common/aimeio') +subdir('common/y3io') subdir('common/gfxhook') subdir('common/unityhook') @@ -131,6 +138,8 @@ subdir('games/tokyoio') subdir('games/fgoio') subdir('games/kemonoio') subdir('games/apm3io') +subdir('games/sekitoio') +subdir('games/ektio') subdir('games/chunihook') subdir('games/divahook') @@ -148,3 +157,5 @@ subdir('games/tokyohook') subdir('games/fgohook') subdir('games/kemonohook') subdir('games/apm3hook') +subdir('games/ekthook') +subdir('games/sekitohook') diff --git a/subprojects/cwinwebsocket.wrap b/subprojects/cwinwebsocket.wrap new file mode 100644 index 0000000..b23eb43 --- /dev/null +++ b/subprojects/cwinwebsocket.wrap @@ -0,0 +1,5 @@ +[wrap-git] +directory = cwinwebsocket +url = https://github.com/akechi-haruka/cwinwebsocket +revision = HEAD + From 1c7531e45e0951fbd7f5e09118ed2f6429b2dfe3 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Thu, 26 Feb 2026 23:26:00 +0100 Subject: [PATCH 30/37] ekt: add gfx hook, misc: bugfixes --- Package.mk | 1 + dist/ekt/segatools_satellite.ini | 6 ++++++ dist/ekt/segatools_terminal.ini | 6 ++++++ dist/mercury/segatools.ini | 4 ++++ dist/mu3/segatools.ini | 1 - games/ekthook/config.c | 3 +++ games/ekthook/config.h | 3 +++ games/ekthook/dllmain.c | 9 +++++++++ games/ekthook/ekthook.def | 5 +++++ games/ekthook/meson.build | 1 + 10 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Package.mk b/Package.mk index f79a357..e85047f 100644 --- a/Package.mk +++ b/Package.mk @@ -179,6 +179,7 @@ $(BUILD_DIR_ZIP)/mai2.zip: $(V)cp $(BUILD_DIR_64)/subprojects/capnhook/inject/inject.exe \ $(BUILD_DIR_GAMES_64)/mai2hook/mai2hook.dll \ $(DIST_DIR)/mai2/segatools.ini \ + $(DIST_DIR)/mai2/config_hook.json \ $(DIST_DIR)/mai2/launch.bat \ $(BUILD_DIR_ZIP)/mai2 $(V)cp pki/billing.pub \ diff --git a/dist/ekt/segatools_satellite.ini b/dist/ekt/segatools_satellite.ini index 1f3474c..a07a2b2 100644 --- a/dist/ekt/segatools_satellite.ini +++ b/dist/ekt/segatools_satellite.ini @@ -77,6 +77,12 @@ enable=1 ; modding frameworks such as BepInEx. targetAssembly= +[gfx] +; Enables the graphics hook. +enable=1 +; Enable DPI awareness for the game process, preventing Windows from stretching the game window if a DPI scaling higher than 100% is used +dpiAware=1 + [printer] ; G-Printec CX-7000 Printer printer emulation setting. enable=1 diff --git a/dist/ekt/segatools_terminal.ini b/dist/ekt/segatools_terminal.ini index bfb74ee..71c0195 100644 --- a/dist/ekt/segatools_terminal.ini +++ b/dist/ekt/segatools_terminal.ini @@ -80,6 +80,12 @@ enable=1 ; modding frameworks such as BepInEx. targetAssembly= +[gfx] +; Enables the graphics hook. +enable=1 +; Enable DPI awareness for the game process, preventing Windows from stretching the game window if a DPI scaling higher than 100% is used +dpiAware=1 + ; ----------------------------------------------------------------------------- ; LED settings ; ----------------------------------------------------------------------------- diff --git a/dist/mercury/segatools.ini b/dist/mercury/segatools.ini index 07fdffb..d866a6b 100644 --- a/dist/mercury/segatools.ini +++ b/dist/mercury/segatools.ini @@ -129,6 +129,10 @@ path= ; world. An improved solution will be provided later. [io4] +; Only enables input when the game's main window is focused. Needs to be +; disabled for WACVR. +foreground=0 + ; Test button virtual-key code. Default is the F1 key. test=0x70 ; Service button virtual-key code. Default is the F2 key. diff --git a/dist/mu3/segatools.ini b/dist/mu3/segatools.ini index 784800d..dc72f69 100644 --- a/dist/mu3/segatools.ini +++ b/dist/mu3/segatools.ini @@ -90,7 +90,6 @@ enable=1 ; Enable DPI awareness for the game process, preventing Windows from stretching the game window if a DPI scaling higher than 100% is used dpiAware=1 - [unity] ; Enable Unity hook. This will allow you to run custom .NET code before the game enable=1 diff --git a/games/ekthook/config.c b/games/ekthook/config.c index e3f015b..e98cafe 100644 --- a/games/ekthook/config.c +++ b/games/ekthook/config.c @@ -3,6 +3,8 @@ #include "board/config.h" +#include "gfxhook/config.h" + #include "ekthook/config.h" #include "ekthook/ekt-dll.h" @@ -98,6 +100,7 @@ void ekt_hook_config_load( platform_config_load(&cfg->platform, filename); aime_config_load(&cfg->aime, filename); io4_config_load(&cfg->io4, filename); + gfx_config_load(&cfg->gfx, filename); dvd_config_load(&cfg->dvd, filename); led15093_config_load(&cfg->led15093, filename); y3_config_load(&cfg->y3, filename); diff --git a/games/ekthook/config.h b/games/ekthook/config.h index 358e3b9..5584443 100644 --- a/games/ekthook/config.h +++ b/games/ekthook/config.h @@ -6,6 +6,8 @@ #include "board/config.h" #include "board/led15093.h" +#include "gfxhook/gfx.h" + #include "ekthook/ekt-dll.h" #include "hooklib/config.h" @@ -20,6 +22,7 @@ struct ekt_hook_config { struct platform_config platform; struct aime_config aime; struct io4_config io4; + struct gfx_config gfx; struct dvd_config dvd; struct led15093_config led15093; struct y3_config y3; diff --git a/games/ekthook/dllmain.c b/games/ekthook/dllmain.c index a449df4..159d149 100644 --- a/games/ekthook/dllmain.c +++ b/games/ekthook/dllmain.c @@ -29,6 +29,12 @@ #include "board/sg-reader.h" #include "board/led15093.h" +#include "gfxhook/d3d9.h" +#include "gfxhook/d3d11.h" +#include "gfxhook/dxgi.h" +#include "gfxhook/gfx.h" + + #include "hook/process.h" #include "hook/iohook.h" @@ -103,6 +109,9 @@ static DWORD CALLBACK ekt_pre_startup(void) /* Hook Win32 APIs */ dvd_hook_init(&ekt_hook_cfg.dvd, ekt_hook_mod); + gfx_hook_init(&ekt_hook_cfg.gfx); + gfx_d3d11_hook_init(&ekt_hook_cfg.gfx, ekt_hook_mod); + gfx_dxgi_hook_init(&ekt_hook_cfg.gfx, ekt_hook_mod); serial_hook_init(); createprocess_push_hook_w(L"fsutil", L"", L"", true, true); diff --git a/games/ekthook/ekthook.def b/games/ekthook/ekthook.def index f03d8d8..634623a 100644 --- a/games/ekthook/ekthook.def +++ b/games/ekthook/ekthook.def @@ -1,6 +1,11 @@ LIBRARY ekthook EXPORTS + CreateDXGIFactory + CreateDXGIFactory1 + CreateDXGIFactory2 + D3D11CreateDevice + D3D11CreateDeviceAndSwapChain aime_io_get_api_version aime_io_init aime_io_led_set_color diff --git a/games/ekthook/meson.build b/games/ekthook/meson.build index 2b3ef34..8ef7850 100644 --- a/games/ekthook/meson.build +++ b/games/ekthook/meson.build @@ -11,6 +11,7 @@ shared_library( link_with : [ aimeio_lib, board_lib, + gfxhook_lib, ektio_lib, hooklib_lib, jvs_lib, From 85b8ae02fac90fc76546ac4820469442c8b150a5 Mon Sep 17 00:00:00 2001 From: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> Date: Wed, 4 Mar 2026 07:58:01 +0000 Subject: [PATCH 31/37] misc: add ability to re-enable a few things (#100) This adds two new config flags to re-enable master registry key access and rebooting. This is needed on real hardware if you want to be able to move to System Test Mode. Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/100 Reviewed-by: Dniel97 Co-authored-by: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> Co-committed-by: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> --- common/platform/config.c | 2 ++ common/platform/misc.c | 16 ++++++++++------ common/platform/misc.h | 4 ++++ doc/config/common.md | 18 +++++++++++++++++- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/common/platform/config.c b/common/platform/config.c index e707126..62cf167 100644 --- a/common/platform/config.c +++ b/common/platform/config.c @@ -168,6 +168,8 @@ void misc_config_load(struct misc_config *cfg, const wchar_t *filename) assert(filename != NULL); cfg->enable = GetPrivateProfileIntW(L"misc", L"enable", 1, filename); + cfg->allowMasterKeyWrite = GetPrivateProfileIntW(L"misc", L"allowMasterKeyWrite", 0, filename); + cfg->allowReboot = GetPrivateProfileIntW(L"misc", L"allowReboot", 0, filename); } void netenv_config_load(struct netenv_config *cfg, const wchar_t *filename) diff --git a/common/platform/misc.c b/common/platform/misc.c index 31a4d16..9ede751 100644 --- a/common/platform/misc.c +++ b/common/platform/misc.c @@ -118,11 +118,13 @@ HRESULT misc_hook_init(const struct misc_config *cfg, const char *platform_id) return hr; } - hr = reg_hook_push_key( - HKEY_LOCAL_MACHINE, - L"SYSTEM\\SEGA\\SystemProperty\\Master", - misc_master_keys, - _countof(misc_master_keys)); + if (!cfg->allowMasterKeyWrite) { + hr = reg_hook_push_key( + HKEY_LOCAL_MACHINE, + L"SYSTEM\\SEGA\\SystemProperty\\Master", + misc_master_keys, + _countof(misc_master_keys)); + } if (FAILED(hr)) { return hr; @@ -130,7 +132,9 @@ HRESULT misc_hook_init(const struct misc_config *cfg, const char *platform_id) /* Apply function hooks */ - hook_table_apply(NULL, "user32.dll", misc_syms, _countof(misc_syms)); + if (!cfg->allowReboot) { + hook_table_apply(NULL, "user32.dll", misc_syms, _countof(misc_syms)); + } return S_OK; } diff --git a/common/platform/misc.h b/common/platform/misc.h index ce4aec4..e9695bc 100644 --- a/common/platform/misc.h +++ b/common/platform/misc.h @@ -1,9 +1,13 @@ #pragma once #include +#include +#include struct misc_config { bool enable; + bool allowReboot; + bool allowMasterKeyWrite; }; HRESULT misc_hook_init(const struct misc_config *cfg, const char *platform_id); diff --git a/doc/config/common.md b/doc/config/common.md index a9df591..3e475b8 100644 --- a/doc/config/common.md +++ b/doc/config/common.md @@ -744,4 +744,20 @@ Example for redirecting COM 5 to COM 10: ``` redirection0from=\\.\COM5 redirection0to=\\.\COM10 -``` \ No newline at end of file +``` + +## `[misc]` + +Configure miscellaneous hooks and features. + +### `allowMasterKeyWrite` + +Default: `0` + +Allows the game to write to specific registry keys relevant for the boot process. Only intended for owners of real hardware. + +### `allowReboot` + +Default: `0` + +Allows the game to reboot the computer. Only intended for owners of real hardware. From 0b81ea38dd1a611c494f9c57143d2bb7d809e4b5 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Mon, 9 Mar 2026 21:13:27 +0100 Subject: [PATCH 32/37] bump capnhook rev --- subprojects/capnhook.wrap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subprojects/capnhook.wrap b/subprojects/capnhook.wrap index 2d30039..b862df9 100644 --- a/subprojects/capnhook.wrap +++ b/subprojects/capnhook.wrap @@ -1,4 +1,4 @@ [wrap-git] directory = capnhook url = https://gitea.tendokyu.moe/TeamTofuShop/capnhook -revision = 4e192325bcc785a88dc32b751e2d3ae610d553ac +revision = 35169a505788ad414f831fd707fffd0b6d025477 From e387f582a91875a44c07501c04f84cffed550aa5 Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Tue, 24 Mar 2026 22:10:27 +0100 Subject: [PATCH 33/37] doc: region improvements and vfs corrected --- doc/config/common.md | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/doc/config/common.md b/doc/config/common.md index 3e475b8..3956a65 100644 --- a/doc/config/common.md +++ b/doc/config/common.md @@ -500,14 +500,14 @@ Default: `1` Override the keychip's region code. Most games seem to pay attention to the DS EEPROM region code and not the keychip region code, and this seems to be -a bit mask that controls which Nu PCB region codes this keychip is authorized -for. So it probably only affects the system software and not the game software. -Bit values are: +a bit mask that controls which Nu/ALLS PCB region codes this keychip is authorized +for. It can affect the system software and the game software. +Values are: -- 1: JPN: Japan -- 2: USA (unused) -- 3: EXP: Export (for Asian markets) -- 4: CHS: China (Simplified Chinese?) +- `1`: JPN: Japan +- `2`: USA (unused) +- `4`: EXP: Export (for Asian markets) +- `8`: CHS: China (Simplified Chinese?) ### `billingCa` @@ -672,6 +672,19 @@ Configure the location of the "Option" data mount point. This mount point is optional (hence the name, probably) and contains directories which contain minor over-the-air content updates. +### `redirection#from`, `redirection#to` + +Default: Empty string + +Advanced feature intended for owners of real hardware. This allows arbitrary file reads to be redirected to other paths. Since serial ports are also "file" reads, this can be used to redirect hardcoded COM ports to other ports. Up to 64 redirections can be used by incrementing the number in the key: `redirection1from`, `redirection2from`, ... + +Example for redirecting COM 5 to COM 10: + +``` +redirection0from=\\.\COM5 +redirection0to=\\.\COM10 +``` + ## `[epay]` Configure Thinca Payment (E-Money) emulation and hooks. @@ -733,19 +746,6 @@ unless you know what you're doing! The Windows directory is always excluded from virtualization. -### `redirection#from`, `redirection#to` - -Default: Empty string - -Advanced feature intended for owners of real hardware. This allows arbitrary file reads to be redirected to other paths. Since serial ports are also "file" reads, this can be used to redirect hardcoded COM ports to other ports. Up to 64 redirections can be used by incrementing the number in the key: `redirection1from`, `redirection2from`, ... - -Example for redirecting COM 5 to COM 10: - -``` -redirection0from=\\.\COM5 -redirection0to=\\.\COM10 -``` - ## `[misc]` Configure miscellaneous hooks and features. From ba6ec91ccea0bf35064cfef636756ef4e1eec01e Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Fri, 3 Apr 2026 15:19:49 +0200 Subject: [PATCH 34/37] idz: bypass size check --- games/idzhook/crt.c | 72 +++++++++++++++++++++++++++++++++++++++ games/idzhook/crt.h | 3 ++ games/idzhook/dllmain.c | 2 ++ games/idzhook/meson.build | 2 ++ 4 files changed, 79 insertions(+) create mode 100644 games/idzhook/crt.c create mode 100644 games/idzhook/crt.h diff --git a/games/idzhook/crt.c b/games/idzhook/crt.c new file mode 100644 index 0000000..2bd8be3 --- /dev/null +++ b/games/idzhook/crt.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +#include "hook/table.h" + +#include "crt.h" + +#include "util/dprintf.h" + +/* API hooks */ + +static int WINAPIV hook__stat64i32( + const char *path, + struct _stat64i32 *buffer +); + +/* Link pointers */ + +static int (WINAPIV *next__stat64i32)( + const char *path, + struct _stat64i32 *buffer +); + +static const char *target_executables[] = { + "InitialD0_DX11_Nu.exe", + "amdaemon.exe", +}; + +static const size_t target_executables_len = _countof(target_executables); + + +static const struct hook_symbol msvcr110_hooks[] = { + { + .name = "_stat64i32", + .patch = hook__stat64i32, + .link = (void**) &next__stat64i32 + } +}; + +void crt_hook_init(void) +{ + dprintf("CRT: Hooks init.\n"); + + hook_table_apply( + NULL, + "msvcr110.dll", + msvcr110_hooks, + _countof(msvcr110_hooks)); +} + +static int __cdecl hook__stat64i32( + const char *path, + struct _stat64i32 *buffer +) +{ + const char *target_executable; + + if (path != NULL) { + for (size_t i = 0; i < target_executables_len; i++) { + target_executable = target_executables[i]; + + if (strstr(path, target_executable)) { + dprintf("CRT: Bypassing size check for %s\n", path); + return 0; + } + } + } + + return next__stat64i32(path, buffer); +} diff --git a/games/idzhook/crt.h b/games/idzhook/crt.h new file mode 100644 index 0000000..27ba9ca --- /dev/null +++ b/games/idzhook/crt.h @@ -0,0 +1,3 @@ +#include + +void crt_hook_init(void); diff --git a/games/idzhook/dllmain.c b/games/idzhook/dllmain.c index b73a6a4..f501640 100644 --- a/games/idzhook/dllmain.c +++ b/games/idzhook/dllmain.c @@ -35,6 +35,7 @@ #include "idzhook/jvs.h" #include "idzhook/ffb.h" #include "idzhook/zinput.h" +#include "idzhook/crt.h" #include "platform/platform.h" @@ -86,6 +87,7 @@ static DWORD CALLBACK idz_pre_startup(void) gfx_dxgi_hook_init(&idz_hook_cfg.gfx, idz_hook_mod); zinput_hook_init(&idz_hook_cfg.zinput); dvd_hook_init(&idz_hook_cfg.dvd, idz_hook_mod); + crt_hook_init(); /* Initialize emulation hooks */ diff --git a/games/idzhook/meson.build b/games/idzhook/meson.build index 6649489..32e1dfb 100644 --- a/games/idzhook/meson.build +++ b/games/idzhook/meson.build @@ -24,6 +24,8 @@ shared_library( sources : [ 'config.c', 'config.h', + 'crt.c', + 'crt.h', 'dllmain.c', 'idz-dll.c', 'idz-dll.h', From 2c2b8b11b46f4838e5cf184db608b28b94a7c466 Mon Sep 17 00:00:00 2001 From: Gl0w1amp Date: Fri, 3 Apr 2026 15:13:27 +0000 Subject: [PATCH 35/37] Fix mai2 memory leak via LED 15070 rate-limiting and enhance touch handling (#101) ## Overview This PR addresses the severe memory leak and performance issues observed in `mai2`, while also introducing improvements to the touch emulation logic. ## Root Cause Analysis As discussed previously, the root cause of the `mai2` memory leak is not a global `segatools` buffer bug. Instead, the game aggressively spams overlapped empty reads specifically on the `LED 15070` UART. On real hardware, the serial driver naturally throttles this. Under emulation, without a throttle, it hits approximately **260kHz of empty async reads**, which causes the memory usage to explode. ## The Fix Instead of introducing complex locking mechanisms and condition variables globally in `uart.c`, this PR applies a targeted fix: * Added a local `Sleep(1)` directly in `common/board/led15070.c` to rate-limit empty reads on the LED path. * Because this is isolated to LED communications, it completely resolves the memory leak without introducing any lag, livelocks, or overhead to other critical inputs. ## Additional Changes in this PR Alongside the memory leak fix, this PR includes a few touch-related improvements (as touch emulation was reviewed during the debugging process): * Enhanced touch input handling and improved auto-scan state management. * Implemented IOCTL handling for touch input to properly manage communication status. ## Testing * **mai2:** Tested successfully on multiple machines. The memory leak is completely gone, and the game runs smoothly. * **chusan:** Tested to ensure no regressions. Sliders and inputs work flawlessly without the lag. Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/101 Co-authored-by: Gl0w1amp Co-committed-by: Gl0w1amp --- common/board/led15070.c | 6 + games/mai2hook/touch.c | 442 +++++++++++++++++++++++++++++++++++++--- games/mai2hook/touch.h | 7 - games/mai2io/mai2io.c | 69 ++++--- games/mai2io/mai2io.h | 3 +- 5 files changed, 454 insertions(+), 73 deletions(-) diff --git a/common/board/led15070.c b/common/board/led15070.c index 1cd9be5..6644ef9 100644 --- a/common/board/led15070.c +++ b/common/board/led15070.c @@ -244,6 +244,12 @@ static HRESULT led15070_handle_irp_locked(int board, struct irp *irp) } } + if (irp->op == IRP_OP_READ) { + if (irp->ovl != NULL && boarduart->readable.pos == 0) { + Sleep(1); + } + } + hr = uart_handle_irp(boarduart, irp); if (FAILED(hr) || irp->op != IRP_OP_WRITE) { diff --git a/games/mai2hook/touch.c b/games/mai2hook/touch.c index 305d005..7e0e615 100644 --- a/games/mai2hook/touch.c +++ b/games/mai2hook/touch.c @@ -1,5 +1,8 @@ +#include + #include #include +#include #include "hooklib/fdshark.h" #include "hooklib/reg.h" @@ -10,6 +13,59 @@ #include "util/dprintf.h" #include "util/dump.h" +static HRESULT touch_handle_irp(struct irp *irp); +static HRESULT touch_handle_irp_locked( + struct irp *irp, + struct uart *uart); +static HRESULT touch_handle_read(struct irp *irp, struct uart *uart); +static HRESULT touch_handle_ioctl(struct irp *irp, struct uart *uart); +static void touch_complete_pending_read(struct uart *uart); +static void touch_release_pending_read(struct uart *uart); +static void touch_clear_auto_scan(struct uart *uart); +static bool touch_read_available(struct uart *uart); +static size_t touch_current_depth(struct uart *uart); +static void touch_shift_read(struct uart *uart, struct iobuf *read); +static HRESULT touch_enqueue( + struct uart *uart, + CONDITION_VARIABLE *cv, + const void *bytes, + size_t nbytes); +static HRESULT touch_enqueue_reply( + struct uart *uart, + CONDITION_VARIABLE *cv, + uint8_t side, + uint8_t sensor, + uint8_t command, + uint8_t value); +static void touch_auto_scan(const uint8_t player, const uint8_t state[7]); + +struct touch_pending_read { + OVERLAPPED *ovl; + uint8_t *bytes; + size_t nbytes; +}; + +enum { + touch_pending_reads_capacity = 4096, +}; + +struct touch_pending_reads { + struct touch_pending_read entries[touch_pending_reads_capacity]; + size_t head; + size_t count; +}; + +struct touch_auto_scan_state { + uint8_t frame[9]; + size_t pos; + bool valid; +}; + +enum { + touch_status_pending = 0x00000103UL, + touch_status_success = 0x00000000UL, +}; + static HRESULT read_reg_touch_1p(void *bytes, uint32_t *nbytes) { @@ -53,17 +109,38 @@ const char *sensor_to_str(uint8_t sensor) } static CRITICAL_SECTION touch_1p_lock; +static CONDITION_VARIABLE touch_1p_cv; +static struct touch_pending_reads touch_1p_pending_reads; +static struct touch_auto_scan_state touch_1p_auto_scan; static struct uart touch_1p_uart; static uint8_t touch_1p_written_bytes[64]; -static uint8_t touch_1p_readable_bytes[64]; +static uint8_t touch_1p_readable_bytes[1024]; static bool touch_1p_status = false; static CRITICAL_SECTION touch_2p_lock; +static CONDITION_VARIABLE touch_2p_cv; +static struct touch_pending_reads touch_2p_pending_reads; +static struct touch_auto_scan_state touch_2p_auto_scan; static struct uart touch_2p_uart; static uint8_t touch_2p_written_bytes[64]; -static uint8_t touch_2p_readable_bytes[64]; +static uint8_t touch_2p_readable_bytes[1024]; static bool touch_2p_status = false; +static struct touch_pending_reads *touch_get_pending_reads(struct uart *uart) +{ + return uart->port_no == 3 ? &touch_1p_pending_reads : &touch_2p_pending_reads; +} + +static bool *touch_get_status_flag(struct uart *uart) +{ + return uart->port_no == 3 ? &touch_1p_status : &touch_2p_status; +} + +static struct touch_auto_scan_state *touch_get_auto_scan(struct uart *uart) +{ + return uart->port_no == 3 ? &touch_1p_auto_scan : &touch_2p_auto_scan; +} + HRESULT touch_hook_init(const struct touch_config *cfg) { assert(cfg != NULL); @@ -85,6 +162,7 @@ HRESULT touch_hook_init(const struct touch_config *cfg) dprintf("Mai2 touch 1P: Init.\n"); InitializeCriticalSection(&touch_1p_lock); + InitializeConditionVariable(&touch_1p_cv); uart_init(&touch_1p_uart, 3); touch_1p_uart.written.bytes = touch_1p_written_bytes; touch_1p_uart.written.nbytes = sizeof(touch_1p_written_bytes); @@ -97,6 +175,7 @@ HRESULT touch_hook_init(const struct touch_config *cfg) dprintf("Mai2 touch 2P: Init.\n"); InitializeCriticalSection(&touch_2p_lock); + InitializeConditionVariable(&touch_2p_cv); uart_init(&touch_2p_uart, 4); touch_2p_uart.written.bytes = touch_2p_written_bytes; touch_2p_uart.written.nbytes = sizeof(touch_2p_written_bytes); @@ -133,12 +212,16 @@ static HRESULT touch_handle_irp(struct irp *irp) return hr; } -static HRESULT touch_handle_irp_locked(struct irp *irp, struct uart *uart) +static HRESULT touch_handle_irp_locked( + struct irp *irp, + struct uart *uart) { HRESULT hr; if (irp->op == IRP_OP_OPEN) { + touch_clear_auto_scan(uart); + touch_release_pending_read(uart); dprintf("Mai2 touch port %d: Starting backend\n", uart->port_no); hr = mai2_dll.touch_init(touch_auto_scan); @@ -149,10 +232,23 @@ static HRESULT touch_handle_irp_locked(struct irp *irp, struct uart *uart) } } + if (irp->op == IRP_OP_READ) { + return touch_handle_read(irp, uart); + } + + if (irp->op == IRP_OP_IOCTL) { + return touch_handle_ioctl(irp, uart); + } + hr = uart_handle_irp(uart, irp); if (FAILED(hr) || irp->op != IRP_OP_WRITE) { + if (irp->op == IRP_OP_CLOSE) { + *touch_get_status_flag(uart) = false; + touch_clear_auto_scan(uart); + touch_release_pending_read(uart); + } return hr; } @@ -160,9 +256,18 @@ static HRESULT touch_handle_irp_locked(struct irp *irp, struct uart *uart) dprintf("Mai2 touch port %d WRITE:\n", uart->port_no); dump_iobuf(&uart->written); #endif + + if (uart->written.pos < 6) { + dprintf("Mai2 touch port %d: Short write (%u bytes)\n", + uart->port_no, + (unsigned int) uart->written.pos); + uart->written.pos = 0; + + return HRESULT_FROM_WIN32(ERROR_INVALID_DATA); + } + uint8_t port_no = uart->port_no; uint8_t *src = uart->written.bytes; - uint8_t *dest = uart->readable.bytes; switch (src[3]) { @@ -175,18 +280,16 @@ static HRESULT touch_handle_irp_locked(struct irp *irp, struct uart *uart) assert(mai2_dll.touch_update != NULL); if (port_no == 3) { - EnterCriticalSection(&touch_1p_lock); touch_1p_status = false; mai2_dll.touch_update(touch_1p_status, touch_2p_status); - LeaveCriticalSection(&touch_1p_lock); } else { - EnterCriticalSection(&touch_2p_lock); touch_2p_status = false; mai2_dll.touch_update(touch_1p_status, touch_2p_status); - LeaveCriticalSection(&touch_2p_lock); } + touch_clear_auto_scan(uart); + touch_release_pending_read(uart); break; case commandSTAT: // Exit Conditioning mode and resume sending touch data. @@ -194,17 +297,13 @@ static HRESULT touch_handle_irp_locked(struct irp *irp, struct uart *uart) assert(mai2_dll.touch_update != NULL); if (port_no == 3) { - EnterCriticalSection(&touch_1p_lock); touch_1p_status = true; mai2_dll.touch_update(touch_1p_status, touch_2p_status); - LeaveCriticalSection(&touch_1p_lock); } else { - EnterCriticalSection(&touch_2p_lock); touch_2p_status = true; mai2_dll.touch_update(touch_1p_status, touch_2p_status); - LeaveCriticalSection(&touch_2p_lock); } break; @@ -212,28 +311,40 @@ static HRESULT touch_handle_irp_locked(struct irp *irp, struct uart *uart) #if defined(LOG_MAI2_TOUCH) dprintf("Mai2 touch side %c: set sensor %s ratio to %d\n", src[1], sensor_to_str(src[2]), src[4]); #endif - dest[0] = res_start; - dest[1] = src[1]; // L,R - dest[2] = src[2]; // sensor - dest[3] = commandRatio; - dest[4] = src[4]; // Ratio - dest[5] = res_end; - uart->readable.pos = 6; - // The Ratio is fixed at 0x72 and does not need to be sent to mai2io for processing. + hr = touch_enqueue_reply( + uart, + uart == &touch_1p_uart ? &touch_1p_cv : &touch_2p_cv, + src[1], + src[2], + commandRatio, + src[4]); break; case commandSens: #if defined(LOG_MAI2_TOUCH) dprintf("Mai2 touch side %c: set sensor %s sensitivity to %d\n", src[1], sensor_to_str(src[2]), src[4]); #endif - dest[0] = res_start; - dest[1] = src[1]; // L,R - dest[2] = src[2]; // sensor - dest[3] = commandSens; - dest[4] = src[4]; // Sensitivity - dest[5] = res_end; - uart->readable.pos = 6; - mai2_dll.touch_set_sens(dest); + assert(mai2_dll.touch_set_sens != NULL); + hr = touch_enqueue_reply( + uart, + uart == &touch_1p_uart ? &touch_1p_cv : &touch_2p_cv, + src[1], + src[2], + commandSens, + src[4]); + + if (SUCCEEDED(hr)) { + uint8_t sens_bytes[6]; + + sens_bytes[0] = res_start; + sens_bytes[1] = src[1]; + sens_bytes[2] = src[2]; + sens_bytes[3] = commandSens; + sens_bytes[4] = src[4]; + sens_bytes[5] = res_end; + mai2_dll.touch_set_sens(sens_bytes); + } + break; default: @@ -249,11 +360,276 @@ static HRESULT touch_handle_irp_locked(struct irp *irp, struct uart *uart) return hr; } +static HRESULT touch_handle_read(struct irp *irp, struct uart *uart) +{ + struct touch_pending_reads *pending_reads; + struct touch_pending_read *pending; + bool *status; + size_t tail; + + pending_reads = touch_get_pending_reads(uart); + status = touch_get_status_flag(uart); + + if (!touch_read_available(uart)) { + if (irp->ovl != NULL && + *status && + pending_reads->count < touch_pending_reads_capacity) { + tail = (pending_reads->head + pending_reads->count) % + touch_pending_reads_capacity; + pending = &pending_reads->entries[tail]; + pending_reads->count++; + + pending->ovl = irp->ovl; + pending->bytes = irp->read.bytes; + pending->nbytes = irp->read.nbytes; + pending->ovl->Internal = touch_status_pending; + pending->ovl->InternalHigh = 0; + + if (pending->ovl->hEvent != NULL) { + ResetEvent(pending->ovl->hEvent); + } + + return HRESULT_FROM_WIN32(ERROR_IO_PENDING); + } + return E_PENDING; + } + + touch_shift_read(uart, &irp->read); + + return S_OK; +} + +static HRESULT touch_handle_ioctl(struct irp *irp, struct uart *uart) +{ + if (irp->ioctl == IOCTL_SERIAL_GET_COMMSTATUS) { + uart->status.AmountInInQueue = (ULONG) touch_current_depth(uart); + uart->status.AmountInOutQueue = uart->written.pos; + + return iobuf_write(&irp->read, &uart->status, sizeof(uart->status)); + } + + return uart_handle_irp(uart, irp); +} + +static void touch_complete_pending_read(struct uart *uart) +{ + struct touch_pending_reads *pending_reads; + struct touch_pending_read *pending; + struct iobuf read; + OVERLAPPED *ovl; + HANDLE event; + + pending_reads = touch_get_pending_reads(uart); + + if (pending_reads->count == 0 || !touch_read_available(uart)) { + return; + } + + pending = &pending_reads->entries[pending_reads->head]; + + read.bytes = pending->bytes; + read.nbytes = pending->nbytes; + read.pos = 0; + + touch_shift_read(uart, &read); + + ovl = pending->ovl; + memset(pending, 0, sizeof(*pending)); + pending_reads->head = (pending_reads->head + 1) % + touch_pending_reads_capacity; + pending_reads->count--; + + ovl->InternalHigh = (ULONG_PTR) read.pos; + + event = ovl->hEvent; + MemoryBarrier(); + ovl->Internal = touch_status_success; + + if (event != NULL) { + SetEvent(event); + } +} + +static void touch_release_pending_read(struct uart *uart) +{ + struct touch_pending_reads *pending_reads; + struct touch_pending_read *pending; + OVERLAPPED *ovl; + HANDLE event; + size_t i; + + pending_reads = touch_get_pending_reads(uart); + + for (i = 0; i < pending_reads->count; i++) { + pending = &pending_reads->entries[ + (pending_reads->head + i) % touch_pending_reads_capacity]; + + if (pending->ovl == NULL) { + continue; + } + + ovl = pending->ovl; + ovl->InternalHigh = 0; + event = ovl->hEvent; + MemoryBarrier(); + ovl->Internal = touch_status_success; + + if (event != NULL) { + SetEvent(event); + } + } + + memset(pending_reads, 0, sizeof(*pending_reads)); +} + +static void touch_clear_auto_scan(struct uart *uart) +{ + struct touch_auto_scan_state *auto_scan; + + auto_scan = touch_get_auto_scan(uart); + auto_scan->pos = 0; + auto_scan->valid = false; +} + +static bool touch_read_available(struct uart *uart) +{ + return uart->readable.pos > 0 || touch_get_auto_scan(uart)->valid; +} + +static size_t touch_current_depth(struct uart *uart) +{ + size_t depth; + struct touch_auto_scan_state *auto_scan; + + depth = uart->readable.pos; + auto_scan = touch_get_auto_scan(uart); + + if (auto_scan->valid) { + depth += sizeof(auto_scan->frame) - auto_scan->pos; + } + + return depth; +} + +static void touch_shift_read(struct uart *uart, struct iobuf *read) +{ + struct touch_auto_scan_state *auto_scan; + size_t read_avail; + size_t frame_avail; + size_t chunksz; + + if (uart->readable.pos > 0) { + iobuf_shift(read, &uart->readable); + return; + } + + auto_scan = touch_get_auto_scan(uart); + + if (!auto_scan->valid) { + return; + } + + read_avail = read->nbytes - read->pos; + frame_avail = sizeof(auto_scan->frame) - auto_scan->pos; + chunksz = read_avail < frame_avail ? read_avail : frame_avail; + + memcpy(&read->bytes[read->pos], + &auto_scan->frame[auto_scan->pos], + chunksz); + read->pos += chunksz; + auto_scan->pos += chunksz; + + if (auto_scan->pos == sizeof(auto_scan->frame)) { + auto_scan->pos = 0; + auto_scan->valid = false; + } +} + +static HRESULT touch_enqueue( + struct uart *uart, + CONDITION_VARIABLE *cv, + const void *bytes, + size_t nbytes) +{ + HRESULT hr; + + hr = iobuf_write(&uart->readable, bytes, nbytes); + + if (FAILED(hr)) { + dprintf("Mai2 touch port %d: RX queue overflow (%u/%u bytes)\n", + uart->port_no, + (unsigned int) uart->readable.pos, + (unsigned int) uart->readable.nbytes); + } else { + while (touch_read_available(uart) && + touch_get_pending_reads(uart)->count > 0) { + touch_complete_pending_read(uart); + } + WakeConditionVariable(cv); + } + + return hr; +} + +static HRESULT touch_enqueue_reply( + struct uart *uart, + CONDITION_VARIABLE *cv, + uint8_t side, + uint8_t sensor, + uint8_t command, + uint8_t value) +{ + uint8_t reply[6]; + + reply[0] = res_start; + reply[1] = side; + reply[2] = sensor; + reply[3] = command; + reply[4] = value; + reply[5] = res_end; + + return touch_enqueue(uart, cv, reply, sizeof(reply)); +} + static void touch_auto_scan(const uint8_t player, const uint8_t state[7]) { - struct uart *touch_uart = player == 1 ? &touch_1p_uart : &touch_2p_uart; - touch_uart->readable.bytes[0] = res_start; - memcpy(&touch_uart->readable.bytes[1], state, 7); - touch_uart->readable.bytes[8] = res_end; - touch_uart->readable.pos = 9; + struct touch_auto_scan_state *auto_scan; + struct uart *touch_uart; + CRITICAL_SECTION *touch_lock; + CONDITION_VARIABLE *touch_cv; + uint8_t frame[9]; + + if (player == 1) { + touch_uart = &touch_1p_uart; + touch_lock = &touch_1p_lock; + touch_cv = &touch_1p_cv; + } else { + touch_uart = &touch_2p_uart; + touch_lock = &touch_2p_lock; + touch_cv = &touch_2p_cv; + } + + if (touch_uart->readable.bytes == NULL) { + return; + } + + frame[0] = res_start; + memcpy(&frame[1], state, 7); + frame[8] = res_end; + + EnterCriticalSection(touch_lock); + if (!*touch_get_status_flag(touch_uart)) { + LeaveCriticalSection(touch_lock); + return; + } + auto_scan = touch_get_auto_scan(touch_uart); + memcpy(auto_scan->frame, frame, sizeof(frame)); + auto_scan->pos = 0; + auto_scan->valid = true; + while (touch_read_available(touch_uart) && + touch_get_pending_reads(touch_uart)->count > 0) { + touch_complete_pending_read(touch_uart); + } + WakeConditionVariable(touch_cv); + LeaveCriticalSection(touch_lock); } diff --git a/games/mai2hook/touch.h b/games/mai2hook/touch.h index f6fc72a..23d67ec 100644 --- a/games/mai2hook/touch.h +++ b/games/mai2hook/touch.h @@ -29,10 +29,3 @@ extern const char *sensor_map[34]; const char *sensor_to_str(uint8_t sensor); HRESULT touch_hook_init(const struct touch_config *cfg); -static HRESULT touch_handle_irp(struct irp *irp); -static HRESULT touch_handle_irp_locked(struct irp *irp, struct uart *uart); - -/* Called in mai2io to send touch data. - Similar to chuni slider_res_auto_scan, but the host does not require periodic updates. - Touch data is sent only when there is a change. */ -static void touch_auto_scan(const uint8_t player, const uint8_t state[7]); diff --git a/games/mai2io/mai2io.c b/games/mai2io/mai2io.c index 6a15bdf..6729538 100644 --- a/games/mai2io/mai2io.c +++ b/games/mai2io/mai2io.c @@ -2,6 +2,7 @@ #include #include +#include #include "mai2hook/touch.h" #include "mai2io/config.h" @@ -160,34 +161,30 @@ void mai2_io_touch_set_sens(uint8_t *bytes) { } void mai2_io_touch_update(bool player1, bool player2) { - if (mai2_io_cfg.debug_input_1p) { - if (player1 && mai2_io_touch_1p_thread == NULL) { - mai2_io_touch_1p_thread = (HANDLE)_beginthreadex( - NULL, 0, mai2_io_touch_1p_thread_proc, _callback, 0, NULL); - } else if (!player1 && mai2_io_touch_1p_thread != NULL) { - mai2_io_touch_1p_stop_flag = true; + if (player1 && mai2_io_touch_1p_thread == NULL) { + mai2_io_touch_1p_thread = (HANDLE)_beginthreadex( + NULL, 0, mai2_io_touch_1p_thread_proc, _callback, 0, NULL); + } else if (!player1 && mai2_io_touch_1p_thread != NULL) { + mai2_io_touch_1p_stop_flag = true; - WaitForSingleObject(mai2_io_touch_1p_thread, INFINITE); - CloseHandle(mai2_io_touch_1p_thread); - mai2_io_touch_1p_thread = NULL; + WaitForSingleObject(mai2_io_touch_1p_thread, INFINITE); + CloseHandle(mai2_io_touch_1p_thread); + mai2_io_touch_1p_thread = NULL; - mai2_io_touch_1p_stop_flag = false; - } + mai2_io_touch_1p_stop_flag = false; } - if (mai2_io_cfg.debug_input_2p) { - if (player2 && mai2_io_touch_2p_thread == NULL) { - mai2_io_touch_2p_thread = (HANDLE)_beginthreadex( - NULL, 0, mai2_io_touch_2p_thread_proc, _callback, 0, NULL); - } else if (!player2 && mai2_io_touch_2p_thread != NULL) { - mai2_io_touch_2p_stop_flag = true; + if (player2 && mai2_io_touch_2p_thread == NULL) { + mai2_io_touch_2p_thread = (HANDLE)_beginthreadex( + NULL, 0, mai2_io_touch_2p_thread_proc, _callback, 0, NULL); + } else if (!player2 && mai2_io_touch_2p_thread != NULL) { + mai2_io_touch_2p_stop_flag = true; - WaitForSingleObject(mai2_io_touch_2p_thread, INFINITE); - CloseHandle(mai2_io_touch_2p_thread); - mai2_io_touch_2p_thread = NULL; + WaitForSingleObject(mai2_io_touch_2p_thread, INFINITE); + CloseHandle(mai2_io_touch_2p_thread); + mai2_io_touch_2p_thread = NULL; - mai2_io_touch_2p_stop_flag = false; - } + mai2_io_touch_2p_stop_flag = false; } } @@ -197,14 +194,18 @@ static unsigned int __stdcall mai2_io_touch_1p_thread_proc(void *ctx) { while (!mai2_io_touch_1p_stop_flag) { uint8_t state[7] = {0, 0, 0, 0, 0, 0, 0}; - for (int i = 0; i < 34; i++) { - if (GetAsyncKeyState(mai2_io_cfg.vk_1p_touch[i])) { - int byteIndex = i / 5; - int bitIndex = i % 5; - state[byteIndex] |= (1 << bitIndex); + if (mai2_io_cfg.debug_input_1p) { + for (int i = 0; i < 34; i++) { + if (GetAsyncKeyState(mai2_io_cfg.vk_1p_touch[i])) { + int byteIndex = i / 5; + int bitIndex = i % 5; + state[byteIndex] |= (1 << bitIndex); + } } } + callback(1, state); + Sleep(1); } return 0; @@ -216,14 +217,18 @@ static unsigned int __stdcall mai2_io_touch_2p_thread_proc(void *ctx) { while (!mai2_io_touch_2p_stop_flag) { uint8_t state[7] = {0, 0, 0, 0, 0, 0, 0}; - for (int i = 0; i < 34; i++) { - if (GetAsyncKeyState(mai2_io_cfg.vk_2p_touch[i])) { - int byteIndex = i / 5; - int bitIndex = i % 5; - state[byteIndex] |= (1 << bitIndex); + if (mai2_io_cfg.debug_input_2p) { + for (int i = 0; i < 34; i++) { + if (GetAsyncKeyState(mai2_io_cfg.vk_2p_touch[i])) { + int byteIndex = i / 5; + int bitIndex = i % 5; + state[byteIndex] |= (1 << bitIndex); + } } } + callback(2, state); + Sleep(1); } return 0; diff --git a/games/mai2io/mai2io.h b/games/mai2io/mai2io.h index 297f35c..e061bbf 100644 --- a/games/mai2io/mai2io.h +++ b/games/mai2io/mai2io.h @@ -120,7 +120,8 @@ void mai2_io_touch_set_sens(uint8_t *bytes); * * This function determines whether the game is ready to accept touch input based on the states of player 1 and player 2. * If the game is ready, it creates or stops the corresponding threads to handle touch data for each player. - * Whether or not threads are created for each player is controlled by `mai2_io_cfg.debug_input_1p` and `mai2_io_cfg.debug_input_2p` configuration. + * The `debug_input_1p` and `debug_input_2p` settings only control whether keyboard input is mapped into touch bits. + * When a player is active, the thread continues to send idle all-zero frames even with debug input disabled. * * @param player1 If `true`, indicates the game is ready to accept touch data from player 1, `false` means the game is not ready. * @param player2 If `true`, indicates the game is ready to accept touch data from player 2, `false` means the game is not ready. From ddc969ff04ff7c305e91887b381f9776ad4e44fd Mon Sep 17 00:00:00 2001 From: HaseoSora Date: Mon, 6 Apr 2026 12:22:00 +0000 Subject: [PATCH 36/37] haseosora-springBoost-Integration (#102) Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/102 Reviewed-by: Dniel97 Co-authored-by: HaseoSora Co-committed-by: HaseoSora --- dist/idac/segatools.ini | 1 + games/idacio/di-dev.c | 14 +++++++++++--- games/idzio/di-dev.c | 7 ++++++- games/swdcio/di-dev.c | 7 ++++++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/dist/idac/segatools.ini b/dist/idac/segatools.ini index ca3e211..0b8c083 100644 --- a/dist/idac/segatools.ini +++ b/dist/idac/segatools.ini @@ -286,3 +286,4 @@ baseDamperFraction = 20 ; Recommended: 10 (1.0%) to 100 (10.0%). ; Default: 20 = 2.0% deadband = 20 + diff --git a/games/idacio/di-dev.c b/games/idacio/di-dev.c index 1bc714a..c3b7bcc 100644 --- a/games/idacio/di-dev.c +++ b/games/idacio/di-dev.c @@ -32,6 +32,9 @@ static uint8_t idac_di_rumble_duration; static uint8_t idac_di_base_damper; static uint8_t idac_di_deadband; +/* Fixed spring boost multiplier (1.5x) */ +static const float idac_di_spring_boost = 1.5f; + HRESULT idac_di_dev_init(const struct idac_di_config* cfg, IDirectInputDevice8W* dev, HWND wnd) { assert(cfg != NULL); @@ -240,13 +243,18 @@ void idac_di_ffb_damper(uint8_t force) { DIEFFECT fx; DICONDITION cond; + /* SPRING (centering) */ memset(&cond, 0, sizeof(cond)); - cond.lPositiveCoefficient = (LONG)(((uint32_t)force * ffb_strength) / idac_di_ffb_scale); - cond.lNegativeCoefficient = cond.lPositiveCoefficient; + LONG base_coeff = (LONG)(((uint32_t)force * ffb_strength) / idac_di_ffb_scale); + + LONG boosted_coeff = (LONG)((float)base_coeff * idac_di_spring_boost); + + cond.lPositiveCoefficient = boosted_coeff; + cond.lNegativeCoefficient = boosted_coeff; cond.dwPositiveSaturation = DI_FFNOMINALMAX; cond.dwNegativeSaturation = DI_FFNOMINALMAX; - + /* If user enters 25, result is 0.025 * DI_FFNOMINALMAX */ cond.lDeadBand = (DI_FFNOMINALMAX * (LONG)idac_di_deadband) / 1000; diff --git a/games/idzio/di-dev.c b/games/idzio/di-dev.c index 25d2b79..c0a5cff 100644 --- a/games/idzio/di-dev.c +++ b/games/idzio/di-dev.c @@ -31,6 +31,9 @@ static uint8_t idz_di_rumble_duration; static uint8_t idz_di_base_damper; static uint8_t idz_di_deadband; +/* Fixed spring boost multiplier (1.5x) */ +static const float idz_di_spring_boost = 1.5f; + HRESULT idz_di_dev_init(const struct idz_di_config* cfg, IDirectInputDevice8W* dev, HWND wnd) { assert(cfg != NULL); @@ -63,6 +66,7 @@ HRESULT idz_di_dev_init(const struct idz_di_config* cfg, ? 200 : cfg->ffb_deadband; + return S_OK; } @@ -251,7 +255,8 @@ void idz_di_ffb_damper(uint8_t force) { /* SPRING (centering) */ memset(&cond, 0, sizeof(cond)); - cond.lPositiveCoefficient = (LONG)(((uint32_t)force * ffb_strength) / idz_di_ffb_scale); + LONG base_coeff = (LONG)(((uint32_t)force * ffb_strength) / idz_di_ffb_scale); + LONG boosted_coeff = (LONG)((float)base_coeff * idz_di_spring_boost); cond.lNegativeCoefficient = cond.lPositiveCoefficient; cond.dwPositiveSaturation = DI_FFNOMINALMAX; cond.dwNegativeSaturation = DI_FFNOMINALMAX; diff --git a/games/swdcio/di-dev.c b/games/swdcio/di-dev.c index e913927..9c5a6c8 100644 --- a/games/swdcio/di-dev.c +++ b/games/swdcio/di-dev.c @@ -32,6 +32,9 @@ static uint8_t swdc_di_rumble_duration; static uint8_t swdc_di_base_damper; static uint8_t swdc_di_deadband; +/* Fixed spring boost multiplier (1.5x) */ +static const float swdc_di_spring_boost = 1.5f; + HRESULT swdc_di_dev_init(const struct swdc_di_config* cfg, IDirectInputDevice8W* dev, HWND wnd) { assert(cfg != NULL); @@ -64,6 +67,7 @@ HRESULT swdc_di_dev_init(const struct swdc_di_config* cfg, ? 200 : cfg->ffb_deadband; + return S_OK; } @@ -242,7 +246,8 @@ void swdc_di_ffb_damper(uint8_t force) { /* SPRING (centering) */ memset(&cond, 0, sizeof(cond)); - cond.lPositiveCoefficient = (LONG)(((uint32_t)force * ffb_strength) / swdc_di_ffb_scale); + LONG base_coeff = (LONG)(((uint32_t)force * ffb_strength) / swdc_di_ffb_scale); + LONG boosted_coeff = (LONG)((float)base_coeff * swdc_di_spring_boost); cond.lNegativeCoefficient = cond.lPositiveCoefficient; cond.dwPositiveSaturation = DI_FFNOMINALMAX; cond.dwNegativeSaturation = DI_FFNOMINALMAX; From 54614224fd8bbcfe497104deed49ac043bc187ac Mon Sep 17 00:00:00 2001 From: Dniel97 Date: Mon, 6 Apr 2026 14:25:43 +0200 Subject: [PATCH 37/37] update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 484c2c0..95359a2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Segatools -Version: `2026-02-26` +Version: `2026-04-06` Loaders and hardware emulators for SEGA games that run on the Nu and ALLS platforms. @@ -23,7 +23,7 @@ Loaders and hardware emulators for SEGA games that run on the Nu and ALLS platfo * up to Future Tone * Initial D * [Initial D Arcade Stage Zero](doc/idzhook.md) - * Initial D THE ARCADE + * Initial D THE ARCADE (up to Season 3) * Kemono Friends * Kemono Friends 3: Planet Tours * maimai DX