From 95e167c01cf659e076145f87c617110a804277ba Mon Sep 17 00:00:00 2001 From: icex2 Date: Thu, 15 Aug 2024 13:26:19 +0200 Subject: [PATCH] refact(iidxhook2): Use sdk-hook and support new config api Summary: Test Plan: --- Module.mk | 2 +- dist/iidx/gamestart-13.bat | 7 +- dist/iidx/iidxhook-13.conf | 74 ------ dist/iidx/inject-13.xml | 105 ++++++++ src/main/iidxhook-util/config-gfx.c | 23 ++ src/main/iidxhook-util/config-gfx.h | 1 + src/main/iidxhook2/Module.mk | 4 +- src/main/iidxhook2/config-iidxhook2.c | 36 --- src/main/iidxhook2/config-iidxhook2.h | 15 -- src/main/iidxhook2/dllmain.c | 339 ++++---------------------- src/main/iidxhook2/iidxhook2.c | 258 ++++++++++++++++++++ src/main/iidxhook2/iidxhook2.def | 7 +- src/main/iidxhook2/iidxhook2.h | 9 + 13 files changed, 452 insertions(+), 428 deletions(-) delete mode 100755 dist/iidx/iidxhook-13.conf create mode 100644 dist/iidx/inject-13.xml delete mode 100644 src/main/iidxhook2/config-iidxhook2.c delete mode 100644 src/main/iidxhook2/config-iidxhook2.h create mode 100644 src/main/iidxhook2/iidxhook2.c create mode 100644 src/main/iidxhook2/iidxhook2.h diff --git a/Module.mk b/Module.mk index bf727e4..b36346f 100644 --- a/Module.mk +++ b/Module.mk @@ -301,7 +301,7 @@ $(zipdir)/iidx-13.zip: \ dist/dwarfstack/32/dwarfstack.dll \ dist/iidx/config.bat \ dist/iidx/gamestart-13.bat \ - dist/iidx/iidxhook-13.conf \ + dist/iidx/inject-13.xml \ dist/iidx/vefx.txt \ | $(zipdir)/ $(V)echo ... $@ diff --git a/dist/iidx/gamestart-13.bat b/dist/iidx/gamestart-13.bat index 04ec81c..77e17c4 100755 --- a/dist/iidx/gamestart-13.bat +++ b/dist/iidx/gamestart-13.bat @@ -54,8 +54,5 @@ if not exist e\avs_conf\CONF\NVRAM mkdir e\avs_conf\CONF\NVRAM if not exist e\avs_conf\CONF\RAW mkdir e\avs_conf\CONF\RAW %BEMANITOOLS_DIR%\inject.exe^ - %BEMANITOOLS_DIR%\iidxhook2.dll^ - %REVISION_DIR%\bm2dx.exe^ - -D^ - --config %BEMANITOOLS_DIR%\iidxhook-13.conf^ - %* + %BEMANITOOLS_DIR%\inject-13.xml^ + %* \ No newline at end of file diff --git a/dist/iidx/iidxhook-13.conf b/dist/iidx/iidxhook-13.conf deleted file mode 100755 index fd32ad0..0000000 --- a/dist/iidx/iidxhook-13.conf +++ /dev/null @@ -1,74 +0,0 @@ -# Magnetic card type, format XXX, 3 digit string (supports: C02, D01, E11, ECO) -eamuse.card_type=C02 - -# URL (e.g. http://my.eamuse.server:80/whatever) or IPV4 (e.g. 127.0.0.1:80) of the target eamuse server. The port is optional but defaults to 80. -eamuse.server=localhost:80 - -# PCBID -eamuse.pcbid=0101020304050607086F - -# EAMID -eamuse.eamid=0101020304050607086F - -# Fix stretched BG videos on newer GPUs. Might appear on SIRIUS and older. On 9th and 10th style this issue may only affect older BGAs (from 1st-3rd style) -gfx.bgvideo_uv_fix=false - -# Run the game in a framed window (requires windowed option) -gfx.framed=false - -# Software limit the frame rate of the rendering loop in hz, e.g. 60 or 59.95 (0.0 = no software limit) -gfx.frame_rate_limit=0.0 - -# Enable/disable software monitor check/auto timebase or set a pre-determined refresh value. -1 disables this feature. 0 enables auto detecting the current refresh rate on startup. Setting any positive value > 0 allows you to set a pre-determined refresh rate (e.g. retrieved from the monitor check on newer IIDX games). Either the auto detected value or pre-determined value is used to patch any chart files in-memory to fix song synchronization issues. Requires constant refresh rate!!! -gfx.monitor_check=-1.000000 - -# Run the game windowed -gfx.windowed=false - -# Windowed width, 0 for default size -gfx.window_width=0 - -# Windowed height, 0 for default size -gfx.window_height=0 - -# Up-/downscale the back buffer's width. This does not change the game's rendering resolution but scales the final frame. Use this to target the native resolution of your monitor/TV, e.g. to avoid over-/underscan, bad image quality or latency caused by the monitors internal upscaler. 0 to disable this feature. Must be set in combination with the corresponding height parameter. -gfx.scale_back_buffer_width=0 - -# Up-/downscale the back buffer's height. This does not change the game's rendering resolution but scales the final frame. Use this to target the native resolution of your monitor/TV, e.g. to avoid over-/underscan, bad image quality or latency caused by the monitors internal upscaler. 0 to disable this feature. Must be set in combination with the corresponding width parameter. -gfx.scale_back_buffer_height=0 - -# Filter type to use for up-/downscaling the back buffer. Only used if scaling feature was enabled by setting the scaling width and height parameters. Available types: none, linear, point (refer to D3DTEXTUREFILTERTYPE for explanation). -gfx.scale_back_buffer_filter=none - -# Forced refresh rate, -1 to not force any (try 59 or 60 if monitor check fails to lock on high refresh rate monitors) -gfx.forced_refresh_rate=-1 - -# D3D9 device adapter (monitor), -1 to use default, 0, 1, 2 etc. to use specified adapter -gfx.device_adapter=-1 - -# Enable monitoring of ezusb.dll calls by logging call traces. Only works on 9th and 10th style! -ezusb.api_call_monitoring=false - -# Set the type of ezusb IO board. 0 = C02, 1 = D01. Note: Impacts security settings! -ezusb.io_board_type=1 - -# Fix broken 3D background on DistorteD's music select (if appearing completely black) -misc.distorted_ms_bg_fix=false - -# Disable operator clock setting system clock time -misc.disable_clock_set=false - -# Stub calls to rteffect.dll (10th to DistorteD) -misc.rteffect_stub=true - -# Path to store the settings, e.g. bookkeeping, operator settings. d:, e: and f: drive configuration/settings data -misc.settings_path=.\ - -# Security boot version (e.g. GEC02). -sec.boot_version=GEC02 - -# Security boot seeds for ezusb, format: X:X:X where X is a number of 0-9 (e.g. 0:0:0). -sec.boot_seeds=0:4:4 - -# Security black plug mcode id string (e.g. GQC02JAA). -sec.black_plug_mcode=GQFDDJAA \ No newline at end of file diff --git a/dist/iidx/inject-13.xml b/dist/iidx/inject-13.xml new file mode 100644 index 0000000..9a8066c --- /dev/null +++ b/dist/iidx/inject-13.xml @@ -0,0 +1,105 @@ + + + 1 + + + bm2dx.exe + + + inject + + + + 1 + iidxhook2.dll + + + + + localhost:80 + 0101020304050607086F + 0101020304050607086F + + + + + -1 + + -1 + + 0.0 + + + 1 + 1 + + 0 + 0 + + + + 0 + 0 + + none + + + + -1.000000 + + 1 + + 0 + + + + D01 + + + GEC02 + 0:4:4 + + GQFDDJAA + + + 0 + + + + + .\ + 1 + 1 + + + + + + + 1 + misc + 8192 + + + 0 + 64 + discard_new + + + 1 + 1 + + + 1 + inject.log + 0 + 0 + 1 + + + + + 1 + 0 + + \ No newline at end of file diff --git a/src/main/iidxhook-util/config-gfx.c b/src/main/iidxhook-util/config-gfx.c index 977252e..e97c10a 100644 --- a/src/main/iidxhook-util/config-gfx.c +++ b/src/main/iidxhook-util/config-gfx.c @@ -26,6 +26,7 @@ #define IIDXHOOK_CONFIG_GFX_FORCED_REFRESHRATE_KEY "gfx.forced_refresh_rate" #define IIDXHOOK_CONFIG_GFX_DEVICE_ADAPTER_KEY "gfx.device_adapter" #define IIDXHOOK_CONFIG_GFX_DIAGONAL_TEARING_FIX_KEY "gfx.diagonal_tearing_fix" +#define IIDXHOOK_CONFIG_MISC_DISTORTED_MS_BG_FIX_KEY "gfx.distorted_ms_bg_fix" #define IIDXHOOK_CONFIG_GFX_DEFAULT_BGVIDEO_UV_FIX_VALUE false #define IIDXHOOK_CONFIG_GFX_DEFAULT_FRAMED_VALUE false @@ -41,6 +42,7 @@ #define IIDXHOOK_CONFIG_GFX_DEFAULT_FORCED_RR_VALUE -1 #define IIDXHOOK_CONFIG_GFX_DEFAULT_DEVICE_ADAPTER_VALUE -1 #define IIDXHOOK_CONFIG_GFX_DEFAULT_DIAGONAL_TEARING_FIX_VALUE false +#define IIDXHOOK_CONFIG_MISC_DEFAULT_DISTORTED_MS_BG_FIX_VALUE false void iidxhook_config_gfx_init(struct cconfig *config) { @@ -172,6 +174,14 @@ void iidxhook_config_gfx_init(struct cconfig *config) IIDXHOOK_CONFIG_GFX_DEFAULT_DIAGONAL_TEARING_FIX_VALUE, "Fix diagonal tearing with video cards " "other than Radeon X1300 and HD3450"); + + cconfig_util_set_bool( + config, + IIDXHOOK_CONFIG_MISC_DISTORTED_MS_BG_FIX_KEY, + IIDXHOOK_CONFIG_MISC_DEFAULT_DISTORTED_MS_BG_FIX_VALUE, + "Fix broken 3D background on DistorteD's music select (if " + "appearing " + "completely black)"); } void iidxhook_config_gfx_get( @@ -392,6 +402,18 @@ void iidxhook_config_gfx_get( IIDXHOOK_CONFIG_GFX_DIAGONAL_TEARING_FIX_KEY, IIDXHOOK_CONFIG_GFX_DEFAULT_DIAGONAL_TEARING_FIX_VALUE); } + + if (!cconfig_util_get_bool( + config, + IIDXHOOK_CONFIG_MISC_DISTORTED_MS_BG_FIX_KEY, + &config_gfx->distorted_ms_bg_fix, + IIDXHOOK_CONFIG_MISC_DEFAULT_DISTORTED_MS_BG_FIX_VALUE)) { + log_warning( + "Invalid value for key '%s' specified, fallback " + "to default '%d'", + IIDXHOOK_CONFIG_MISC_DISTORTED_MS_BG_FIX_KEY, + IIDXHOOK_CONFIG_MISC_DEFAULT_DISTORTED_MS_BG_FIX_VALUE); + } } static void _iidxhook_util_config_gfx_verify(const iidxhook_config_gfx_t *config) @@ -473,6 +495,7 @@ void iidxhook_util_config_gfx_get2( bt_core_config_float_get(config, "gfx/game/monitor_check", &config_gfx->monitor_check); bt_core_config_bool_get(config, "gfx/game/diagonal_tearing_fix", &config_gfx->diagonal_tearing_fix); bt_core_config_bool_get(config, "gfx/game/happy_sky_ms_bg_fix", &config_gfx->happy_sky_ms_bg_fix); + bt_core_config_bool_get(config, "gfx/game/distorted_ms_bg_fix", &config_gfx->distorted_ms_bg_fix); _iidxhook_util_config_gfx_verify(config_gfx); } \ No newline at end of file diff --git a/src/main/iidxhook-util/config-gfx.h b/src/main/iidxhook-util/config-gfx.h index da95aa9..9a8a414 100644 --- a/src/main/iidxhook-util/config-gfx.h +++ b/src/main/iidxhook-util/config-gfx.h @@ -25,6 +25,7 @@ typedef struct iidxhook_config_gfx { int8_t device_adapter; bool diagonal_tearing_fix; bool happy_sky_ms_bg_fix; + bool distorted_ms_bg_fix; } iidxhook_config_gfx_t; void iidxhook_config_gfx_init(struct cconfig *config); diff --git a/src/main/iidxhook2/Module.mk b/src/main/iidxhook2/Module.mk index 6c9022e..70c2a40 100644 --- a/src/main/iidxhook2/Module.mk +++ b/src/main/iidxhook2/Module.mk @@ -4,6 +4,7 @@ ldflags_iidxhook2 := \ -lws2_32 \ -liphlpapi \ -lpsapi \ + -lshlwapi \ libs_iidxhook2 := \ core \ @@ -23,7 +24,8 @@ libs_iidxhook2 := \ module \ util \ mxml \ + sdk-hook \ src_iidxhook2 := \ - config-iidxhook2.c \ dllmain.c \ + iidxhook2.c \ diff --git a/src/main/iidxhook2/config-iidxhook2.c b/src/main/iidxhook2/config-iidxhook2.c deleted file mode 100644 index 9dca3dc..0000000 --- a/src/main/iidxhook2/config-iidxhook2.c +++ /dev/null @@ -1,36 +0,0 @@ -#include "cconfig/cconfig-util.h" - -#include "iface-core/log.h" - -#include "iidxhook2/config-iidxhook2.h" - -#define IIDXHOOK_CONFIG_MISC_DISTORTED_MS_BG_FIX_KEY "misc.distorted_ms_bg_fix" - -#define IIDXHOOK_CONFIG_MISC_DEFAULT_DISTORTED_MS_BG_FIX_VALUE false - -void iidxhook_config_iidxhook2_init(struct cconfig *config) -{ - cconfig_util_set_bool( - config, - IIDXHOOK_CONFIG_MISC_DISTORTED_MS_BG_FIX_KEY, - IIDXHOOK_CONFIG_MISC_DEFAULT_DISTORTED_MS_BG_FIX_VALUE, - "Fix broken 3D background on DistorteD's music select (if " - "appearing " - "completely black)"); -} - -void iidxhook_config_iidxhook2_get( - struct iidxhook_config_iidxhook2 *config_iidxhook2, struct cconfig *config) -{ - if (!cconfig_util_get_bool( - config, - IIDXHOOK_CONFIG_MISC_DISTORTED_MS_BG_FIX_KEY, - &config_iidxhook2->distorted_ms_bg_fix, - IIDXHOOK_CONFIG_MISC_DEFAULT_DISTORTED_MS_BG_FIX_VALUE)) { - log_warning( - "Invalid value for key '%s' specified, fallback " - "to default '%d'", - IIDXHOOK_CONFIG_MISC_DISTORTED_MS_BG_FIX_KEY, - IIDXHOOK_CONFIG_MISC_DEFAULT_DISTORTED_MS_BG_FIX_VALUE); - } -} \ No newline at end of file diff --git a/src/main/iidxhook2/config-iidxhook2.h b/src/main/iidxhook2/config-iidxhook2.h deleted file mode 100644 index 50c85e1..0000000 --- a/src/main/iidxhook2/config-iidxhook2.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef IIDXHOOK_CONFIG_IIDXHOOK2_H -#define IIDXHOOK_CONFIG_IIDXHOOK2_H - -#include "cconfig/cconfig.h" - -struct iidxhook_config_iidxhook2 { - bool distorted_ms_bg_fix; -}; - -void iidxhook_config_iidxhook2_init(struct cconfig *config); - -void iidxhook_config_iidxhook2_get( - struct iidxhook_config_iidxhook2 *config_iidxhook2, struct cconfig *config); - -#endif \ No newline at end of file diff --git a/src/main/iidxhook2/dllmain.c b/src/main/iidxhook2/dllmain.c index f96e388..4a2b478 100644 --- a/src/main/iidxhook2/dllmain.c +++ b/src/main/iidxhook2/dllmain.c @@ -1,321 +1,70 @@ #include -#include -#include -#include - -#include "cconfig/cconfig-hook.h" - -#include "core/boot.h" -#include "core/log-bt-ext.h" -#include "core/log-bt.h" -#include "core/log-sink-debug.h" -#include "core/thread-crt.h" - -#include "ezusb-emu/desc.h" -#include "ezusb-emu/device.h" -#include "ezusb-emu/node-security-plug.h" - -#include "ezusb-iidx-emu/msg.h" -#include "ezusb-iidx-emu/node-serial.h" -#include "ezusb-iidx-emu/nodes.h" - -#include "hook/d3d9.h" -#include "hook/iohook.h" #include "hook/table.h" -#include "hooklib/acp.h" -#include "hooklib/adapter.h" -#include "hooklib/rs232.h" -#include "hooklib/setupapi.h" +#include "iidxhook2/iidxhook2.h" -#include "iface-core/log.h" -#include "iface-core/thread.h" +#include "main/sdk-hook/dllentry.h" -#include "iface-io/eam.h" -#include "iface-io/iidx.h" +#include "util/defs.h" -#include "iidxhook-util/acio.h" -#include "iidxhook-util/chart-patch.h" -#include "iidxhook-util/clock.h" -#include "iidxhook-util/config-eamuse.h" -#include "iidxhook-util/config-ezusb.h" -#include "iidxhook-util/config-gfx.h" -#include "iidxhook-util/config-misc.h" -#include "iidxhook-util/config-sec.h" -#include "iidxhook-util/d3d9.h" -#include "iidxhook-util/eamuse.h" -#include "iidxhook-util/effector.h" -#include "iidxhook-util/settings.h" +static HANDLE STDCALL _iidxhook2_dllmain_my_OpenProcess(DWORD, BOOL, DWORD); +static HANDLE(STDCALL *_iidxhook2_dllmain_real_OpenProcess)(DWORD, BOOL, DWORD); -#include "module/io-ext.h" -#include "module/io.h" +static bool _iidxhook2_dllmain_openprocess_call_check; -#include "iidxhook2/config-iidxhook2.h" - -#include "util/proc.h" - -#define IIDXHOOK2_INFO_HEADER \ - "iidxhook for DistorteD" \ - ", build " __DATE__ " " __TIME__ ", gitrev " STRINGIFY(GITREV) -#define IIDXHOOK2_CMD_USAGE \ - "Usage: inject.exe iidxhook2.dll [options...]" - -static const hook_d3d9_irp_handler_t iidxhook_d3d9_handlers[] = { - iidxhook_util_d3d9_irp_handler, +static const struct hook_symbol _iidxhook2_dllmain_hook_syms[] = { + { + .name = "OpenProcess", + .patch = _iidxhook2_dllmain_my_OpenProcess, + .link = (void **) &_iidxhook2_dllmain_real_OpenProcess, + }, }; -static HANDLE STDCALL my_OpenProcess(DWORD, BOOL, DWORD); -static HANDLE(STDCALL *real_OpenProcess)(DWORD, BOOL, DWORD); - -static bool iidxhook_init_check; -static module_io_t *iidxhook_module_io_iidx; -static module_io_t *iidxhook_module_io_eam; - -static const struct hook_symbol init_hook_syms[] = { - {.name = "OpenProcess", - .patch = my_OpenProcess, - .link = (void **) &real_OpenProcess}, -}; - -static void _iidxhook2_log_init() +static HANDLE STDCALL +_iidxhook2_dllmain_my_OpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) { - core_log_bt_ext_init_with_debug(); - // TODO change log level support - core_log_bt_level_set(CORE_LOG_BT_LOG_LEVEL_MISC); + if (_iidxhook2_dllmain_openprocess_call_check) { + return _iidxhook2_dllmain_real_OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); + } + + _iidxhook2_dllmain_openprocess_call_check = true; + + bt_hook_dllentry_main_init(); + + return _iidxhook2_dllmain_real_OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); } -static void _iidxhook2_io_iidx_init(module_io_t **module) -{ - bt_io_iidx_api_t api; +// TODO find another call right before main exits to hook cleanup and stuff with +// bt_hook_dllentry_main_fini() - module_io_ext_load_and_init( - "iidxio.dll", "bt_module_io_iidx_api_get", module); - module_io_api_get(*module, &api); - bt_io_iidx_api_set(&api); -} - -static void _iidxhook2_io_eam_init(module_io_t **module) -{ - bt_io_eam_api_t api; - - module_io_ext_load_and_init( - "eamio.dll", "bt_module_io_eam_api_get", module); - module_io_api_get(*module, &api); - bt_io_eam_api_set(&api); -} - -static void iidxhook2_setup_d3d9_hooks( - const struct iidxhook_config_gfx *config_gfx, - const struct iidxhook_config_iidxhook2 *config_iidxhook2) -{ - struct iidxhook_util_d3d9_config d3d9_config; - - if (!proc_is_module_loaded("d3d9.dll")) { - log_fatal( - "d3d8 hook module deprecated, using d3d9 hook modules requires " - "d3d8to9 to work! Could not detect loaded d3d9.dll"); - } - - iidxhook_util_d3d9_init_config(&d3d9_config); - - d3d9_config.windowed = config_gfx->windowed; - d3d9_config.framed = config_gfx->framed; - d3d9_config.override_window_width = config_gfx->window_width; - d3d9_config.override_window_height = config_gfx->window_height; - d3d9_config.framerate_limit = config_gfx->frame_rate_limit; - d3d9_config.iidx13_fix_song_select_bg = - config_iidxhook2->distorted_ms_bg_fix; - d3d9_config.iidx09_to_17_fix_uvs_bg_videos = config_gfx->bgvideo_uv_fix; - d3d9_config.scale_back_buffer_width = config_gfx->scale_back_buffer_width; - d3d9_config.scale_back_buffer_height = config_gfx->scale_back_buffer_height; - d3d9_config.scale_back_buffer_filter = config_gfx->scale_back_buffer_filter; - d3d9_config.forced_refresh_rate = config_gfx->forced_refresh_rate; - d3d9_config.device_adapter = config_gfx->device_adapter; - - if (config_gfx->monitor_check == 0) { - log_info("Auto monitor check enabled"); - - d3d9_config.iidx09_to_19_monitor_check_cb = - iidxhook_util_chart_patch_set_refresh_rate; - iidxhook_util_chart_patch_init( - IIDXHOOK_UTIL_CHART_PATCH_TIMEBASE_9_TO_13); - } else if (config_gfx->monitor_check > 0) { - log_info( - "Manual monitor check, resulting refresh rate: %f", - config_gfx->monitor_check); - - iidxhook_util_chart_patch_init( - IIDXHOOK_UTIL_CHART_PATCH_TIMEBASE_9_TO_13); - iidxhook_util_chart_patch_set_refresh_rate(config_gfx->monitor_check); - } - - iidxhook_util_d3d9_configure(&d3d9_config); - - hook_d3d9_init(iidxhook_d3d9_handlers, lengthof(iidxhook_d3d9_handlers)); -} - -/** - * This seems to be a good entry point to intercept - * before the game calls anything important - */ -HANDLE STDCALL -my_OpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) -{ - struct cconfig *config; - - struct iidxhook_util_config_ezusb config_ezusb; - struct iidxhook_util_config_eamuse config_eamuse; - struct iidxhook_config_gfx config_gfx; - struct iidxhook_config_iidxhook2 config_iidxhook2; - struct iidxhook_config_misc config_misc; - struct iidxhook_config_sec config_sec; - - if (iidxhook_init_check) { - goto skip; - } - - iidxhook_init_check = true; - - log_info("-------------------------------------------------------------"); - log_info("--------------- Begin iidxhook my_OpenProcess ---------------"); - log_info("-------------------------------------------------------------"); - - config = cconfig_init(); - - iidxhook_util_config_ezusb_init(config); - iidxhook_util_config_eamuse_init(config); - iidxhook_config_gfx_init(config); - iidxhook_config_iidxhook2_init(config); - iidxhook_config_misc_init(config); - iidxhook_config_sec_init(config); - - if (!cconfig_hook_config_init( - config, - IIDXHOOK2_INFO_HEADER "\n" IIDXHOOK2_CMD_USAGE, - CCONFIG_CMD_USAGE_OUT_DBG)) { - cconfig_finit(config); - exit(EXIT_FAILURE); - } - - iidxhook_util_config_ezusb_get(&config_ezusb, config); - iidxhook_util_config_eamuse_get(&config_eamuse, config); - iidxhook_config_gfx_get(&config_gfx, config); - iidxhook_config_iidxhook2_get(&config_iidxhook2, config); - iidxhook_config_misc_get(&config_misc, config); - iidxhook_config_sec_get(&config_sec, config); - - cconfig_finit(config); - - log_info(IIDXHOOK2_INFO_HEADER); - log_info("Initializing iidxhook..."); - - /* Round plug security */ - - ezusb_iidx_emu_node_security_plug_set_boot_version( - &config_sec.boot_version); - ezusb_iidx_emu_node_security_plug_set_boot_seeds(config_sec.boot_seeds); - ezusb_iidx_emu_node_security_plug_set_plug_black_mcode( - &config_sec.black_plug_mcode); - ezusb_iidx_emu_node_security_plug_set_pcbid(&config_eamuse.pcbid); - - /* eAmusement server IP */ - - eamuse_set_addr(&config_eamuse.server); - eamuse_check_connection(); - - /* Patch rteffect.dll calls */ - - if (config_misc.rteffect_stub) { - effector_hook_init(); - } - - /* Settings paths */ - - if (strlen(config_misc.settings_path) > 0) { - settings_hook_set_path(config_misc.settings_path); - } - - /* Direct3D and USER32 hooks */ - - iidxhook2_setup_d3d9_hooks(&config_gfx, &config_iidxhook2); - - /* Disable operator menu clock setting system clock time */ - - if (config_misc.disable_clock_set) { - iidxhook_util_clock_hook_init(); - } - - /* Start up IIDXIO.DLL */ - - log_info("Starting IIDX IO backend"); - - _iidxhook2_io_iidx_init(&iidxhook_module_io_iidx); - - if (!bt_io_iidx_init()) { - log_fatal("Initializing IIDX IO backend failed"); - } - - /* Start up EAMIO.DLL */ - - log_misc("Initializing card reader backend"); - - _iidxhook2_io_eam_init(&iidxhook_module_io_eam); - - if (!bt_io_eam_init()) { - log_fatal("Initializing card reader backend failed"); - } - - /* Set up IO emulation hooks _after_ IO API setup to allow - API implementations with real IO devices */ - iohook_push_handler(ezusb_emu_device_dispatch_irp); - iohook_push_handler(iidxhook_util_acio_dispatch_irp); - iohook_push_handler(iidxhook_util_chart_patch_dispatch_irp); - iohook_push_handler(settings_hook_dispatch_irp); - - hook_setupapi_init(&ezusb_emu_desc_device.setupapi); - ezusb_emu_device_hook_init( - ezusb_iidx_emu_msg_init(config_ezusb.io_board_type)); - - /* Card reader emulation, same issue with hooking as IO emulation */ - rs232_hook_init(); - - iidxhook_util_acio_init(true); - - log_info("-------------------------------------------------------------"); - log_info("---------------- End iidxhook my_OpenProcess ----------------"); - log_info("-------------------------------------------------------------"); - -skip: - return real_OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); -} - -/** - * Hook library for DistorteD - */ BOOL WINAPI DllMain(HMODULE mod, DWORD reason, void *ctx) { if (reason == DLL_PROCESS_ATTACH) { - core_boot_dll("iidxhook3"); + bt_hook_dllentry_init( + mod, + "iidxhook2", + bt_module_core_config_api_set, + bt_module_core_log_api_set, + bt_module_core_thread_api_set, + bt_module_hook_api_get); - // Use bemanitools core APIs - core_log_bt_core_api_set(); - core_thread_crt_core_api_set(); - - _iidxhook2_log_init(); - - /* Bootstrap hook for further init tasks (see above) */ + _iidxhook2_dllmain_openprocess_call_check = false; hook_table_apply( - NULL, "kernel32.dll", init_hook_syms, lengthof(init_hook_syms)); + NULL, "kernel32.dll", _iidxhook2_dllmain_hook_syms, lengthof(_iidxhook2_dllmain_hook_syms)); - /* Actual hooks for game specific stuff */ + } else if (reason == DLL_PROCESS_DETACH) { + // https://learn.microsoft.com/en-us/windows/win32/dlls/dllmain#remarks + if (ctx == NULL) { + hook_table_revert(NULL, "kernel32.dll", _iidxhook2_dllmain_hook_syms, lengthof(_iidxhook2_dllmain_hook_syms)); - acp_hook_init(); - adapter_hook_init(); - eamuse_hook_init(); - settings_hook_init(); + // Hacky to have this here, should be close/right after application main exits, see TODO above + bt_hook_dllentry_main_fini(); + + bt_hook_dllentry_fini(); + } } return TRUE; -} +} \ No newline at end of file diff --git a/src/main/iidxhook2/iidxhook2.c b/src/main/iidxhook2/iidxhook2.c new file mode 100644 index 0000000..5007c02 --- /dev/null +++ b/src/main/iidxhook2/iidxhook2.c @@ -0,0 +1,258 @@ +#define LOG_MODULE "iidxhook2" + +#include + +#include +#include +#include + +#include "ezusb-emu/desc.h" +#include "ezusb-emu/device.h" +#include "ezusb-emu/node-security-plug.h" + +#include "ezusb-iidx-emu/msg.h" +#include "ezusb-iidx-emu/node-serial.h" +#include "ezusb-iidx-emu/nodes.h" + +#include "hook/d3d9.h" +#include "hook/iohook.h" + +#include "hooklib/acp.h" +#include "hooklib/adapter.h" +#include "hooklib/rs232.h" +#include "hooklib/setupapi.h" + +#include "iface-core/config.h" +#include "iface-core/log.h" +#include "iface-core/thread.h" + +#include "iface-io/eam.h" +#include "iface-io/iidx.h" + +#include "iidxhook-util/acio.h" +#include "iidxhook-util/chart-patch.h" +#include "iidxhook-util/clock.h" +#include "iidxhook-util/config-eamuse.h" +#include "iidxhook-util/config-ezusb.h" +#include "iidxhook-util/config-gfx.h" +#include "iidxhook-util/config-misc.h" +#include "iidxhook-util/config-sec.h" +#include "iidxhook-util/d3d9.h" +#include "iidxhook-util/eamuse.h" +#include "iidxhook-util/effector.h" +#include "iidxhook-util/settings.h" + +#include "module/io-ext.h" +#include "module/io.h" + +#include "sdk/module/core/config.h" +#include "sdk/module/core/log.h" +#include "sdk/module/core/thread.h" +#include "sdk/module/hook.h" + +#include "util/proc.h" + +static const hook_d3d9_irp_handler_t iidxhook_d3d9_handlers[] = { + iidxhook_util_d3d9_irp_handler, +}; + +static module_io_t *_iidxhook2_module_io_iidx; +static module_io_t *_iidxhook2_module_io_eam; + +static void _iidxhook2_io_iidx_init(module_io_t **module) +{ + bt_io_iidx_api_t api; + + module_io_ext_load_and_init( + "iidxio.dll", "bt_module_io_iidx_api_get", module); + module_io_api_get(*module, &api); + bt_io_iidx_api_set(&api); +} + +static void _iidxhook2_io_eam_init(module_io_t **module) +{ + bt_io_eam_api_t api; + + module_io_ext_load_and_init( + "eamio.dll", "bt_module_io_eam_api_get", module); + module_io_api_get(*module, &api); + bt_io_eam_api_set(&api); +} + +static void iidxhook2_setup_d3d9_hooks( + const struct iidxhook_config_gfx *config_gfx) +{ + struct iidxhook_util_d3d9_config d3d9_config; + + if (!proc_is_module_loaded("d3d9.dll")) { + log_fatal( + "d3d8 hook module deprecated, using d3d9 hook modules requires " + "d3d8to9 to work! Could not detect loaded d3d9.dll"); + } + + iidxhook_util_d3d9_init_config(&d3d9_config); + + d3d9_config.windowed = config_gfx->windowed; + d3d9_config.framed = config_gfx->framed; + d3d9_config.override_window_width = config_gfx->window_width; + d3d9_config.override_window_height = config_gfx->window_height; + d3d9_config.framerate_limit = config_gfx->frame_rate_limit; + d3d9_config.iidx09_to_17_fix_uvs_bg_videos = config_gfx->bgvideo_uv_fix; + d3d9_config.scale_back_buffer_width = config_gfx->scale_back_buffer_width; + d3d9_config.scale_back_buffer_height = config_gfx->scale_back_buffer_height; + d3d9_config.scale_back_buffer_filter = config_gfx->scale_back_buffer_filter; + d3d9_config.forced_refresh_rate = config_gfx->forced_refresh_rate; + d3d9_config.device_adapter = config_gfx->device_adapter; + d3d9_config.iidx13_fix_song_select_bg = config_gfx->distorted_ms_bg_fix; + + if (config_gfx->monitor_check == 0) { + log_info("Auto monitor check enabled"); + + d3d9_config.iidx09_to_19_monitor_check_cb = + iidxhook_util_chart_patch_set_refresh_rate; + iidxhook_util_chart_patch_init( + IIDXHOOK_UTIL_CHART_PATCH_TIMEBASE_9_TO_13); + } else if (config_gfx->monitor_check > 0) { + log_info( + "Manual monitor check, resulting refresh rate: %f", + config_gfx->monitor_check); + + iidxhook_util_chart_patch_init( + IIDXHOOK_UTIL_CHART_PATCH_TIMEBASE_9_TO_13); + iidxhook_util_chart_patch_set_refresh_rate(config_gfx->monitor_check); + } + + iidxhook_util_d3d9_configure(&d3d9_config); + + hook_d3d9_init(iidxhook_d3d9_handlers, lengthof(iidxhook_d3d9_handlers)); +} + +static bool +_iidxhook2_main_init(HMODULE game_module, const bt_core_config_t *config) +{ + iidxhook_util_config_ezusb_t config_ezusb; + iidxhook_util_config_eamuse_t config_eamuse; + iidxhook_config_gfx_t config_gfx; + iidxhook_config_misc_t config_misc; + iidxhook_config_sec_t config_sec; + + log_info("iidxhook for DistorteD"); + log_info("build " __DATE__ " " __TIME__ ", gitrev " STRINGIFY(GITREV)); + + iidxhook_util_config_ezusb_get2(config, &config_ezusb); + iidxhook_util_config_eamuse_get2(config, &config_eamuse); + iidxhook_util_config_gfx_get2(config, &config_gfx); + iidxhook_util_config_misc_get2(config, &config_misc); + iidxhook_util_config_sec_get2(config, &config_sec); + + acp_hook_init(); + adapter_hook_init(); + eamuse_hook_init(); + settings_hook_init(); + + /* Round plug security */ + + ezusb_iidx_emu_node_security_plug_set_boot_version( + &config_sec.boot_version); + ezusb_iidx_emu_node_security_plug_set_boot_seeds(config_sec.boot_seeds); + ezusb_iidx_emu_node_security_plug_set_plug_black_mcode( + &config_sec.black_plug_mcode); + ezusb_iidx_emu_node_security_plug_set_pcbid(&config_eamuse.pcbid); + + /* eAmusement server IP */ + + eamuse_set_addr(&config_eamuse.server); + eamuse_check_connection(); + + /* Patch rteffect.dll calls */ + + // TODO this doesn't work, because it's too late to apply this here + // this needs to be launched before bm2dx.exe even loads the rteffect.dll + // and runs DllMain of it (see also how this is done with the proxy + // ezusb.dll) + if (config_misc.rteffect_stub) { + effector_hook_init(); + } + + /* Settings paths */ + + if (strlen(config_misc.settings_path) > 0) { + settings_hook_set_path(config_misc.settings_path); + } + + /* Direct3D and USER32 hooks */ + + iidxhook2_setup_d3d9_hooks(&config_gfx); + + /* Disable operator menu clock setting system clock time */ + + if (config_misc.disable_clock_set) { + iidxhook_util_clock_hook_init(); + } + + /* Start up IIDXIO.DLL */ + + log_info("Starting IIDX IO backend"); + + _iidxhook2_io_iidx_init(&_iidxhook2_module_io_iidx); + + if (!bt_io_iidx_init()) { + log_fatal("Initializing IIDX IO backend failed"); + } + + /* Start up EAMIO.DLL */ + + log_misc("Initializing card reader backend"); + + _iidxhook2_io_eam_init(&_iidxhook2_module_io_eam); + + if (!bt_io_eam_init()) { + log_fatal("Initializing card reader backend failed"); + } + + /* Set up IO emulation hooks _after_ IO API setup to allow + API implementations with real IO devices */ + iohook_push_handler(ezusb_emu_device_dispatch_irp); + iohook_push_handler(iidxhook_util_acio_dispatch_irp); + iohook_push_handler(iidxhook_util_chart_patch_dispatch_irp); + iohook_push_handler(settings_hook_dispatch_irp); + + hook_setupapi_init(&ezusb_emu_desc_device.setupapi); + ezusb_emu_device_hook_init( + ezusb_iidx_emu_msg_init(config_ezusb.io_board_type)); + + /* Card reader emulation, same issue with hooking as IO emulation */ + rs232_hook_init(); + + iidxhook_util_acio_init(true); + + return true; +} + +static void _iidxhook2_main_fini() +{ + // TODO cleanup +} + +void bt_module_core_config_api_set(const bt_core_config_api_t *api) +{ + bt_core_config_api_set(api); +} + +void bt_module_core_log_api_set(const bt_core_log_api_t *api) +{ + bt_core_log_api_set(api); +} + +void bt_module_core_thread_api_set(const bt_core_thread_api_t *api) +{ + bt_core_thread_api_set(api); +} + +void bt_module_hook_api_get(bt_hook_api_t *api) +{ + api->version = 1; + + api->v1.main_init = _iidxhook2_main_init; + api->v1.main_fini = _iidxhook2_main_fini; +} \ No newline at end of file diff --git a/src/main/iidxhook2/iidxhook2.def b/src/main/iidxhook2/iidxhook2.def index 76b59ec..df59a81 100644 --- a/src/main/iidxhook2/iidxhook2.def +++ b/src/main/iidxhook2/iidxhook2.def @@ -2,4 +2,9 @@ LIBRARY iidxhook2 EXPORTS DllMain@12 @1 NONAME - \ No newline at end of file + + ; Bemanitools 6 API + bt_module_core_config_api_set + bt_module_core_log_api_set + bt_module_core_thread_api_set + bt_module_hook_api_get \ No newline at end of file diff --git a/src/main/iidxhook2/iidxhook2.h b/src/main/iidxhook2/iidxhook2.h new file mode 100644 index 0000000..7f3a5ab --- /dev/null +++ b/src/main/iidxhook2/iidxhook2.h @@ -0,0 +1,9 @@ +#ifndef IIDXHOOK2_H +#define IIDXHOOK2_H + +#include "sdk/module/core/config.h" +#include "sdk/module/core/log.h" +#include "sdk/module/core/thread.h" +#include "sdk/module/hook.h" + +#endif \ No newline at end of file