refactor: Move all hook and IO libraries to use btools log and thread api

Remove usages of AVS threads and logging functions from the bemanitools backend. Use the bemanitools 6 API instead
which propagates interfaces to call thread and log APIs.

Prior design decisions for using these were:
- have a single log stream through the AVS logging system for “neatness”, log output redirection options in launcher, and theoretically logging to an xrpc endpoint
- IIDX 19 to IIDX 24 crash the AVS logging engine in libavs due to IO hook code running in a non AVS thread (threads in ezusb library) causing a stackoverflow when trying to acquire a mutex (see dev journal 2018-02-10-logging-breakdown-avs.md for details)

With a flexible log sink architecture in the core of bemanitools, and a separate writer function that is hooked up as a log writer to AVS, log streams are still unified on a sink level. This takes care of having all log messaged sinked to the same targets no matter how many logging engines are using them.

This avoids going through the AVS logging engine with the IO related hooking code in iidx, which just uses the bemanitools logging engine instead.

Furthermore, this removes any needs to having to switch thread and logging implementations once AVS is booted which significantly simplifies the runtime orchestration during bootstrapping.

The log-server, which was specifically implemented for iidx to solve the threading issue, can also be removed now. Unfortunately, this also caused performance issues such as stuttering due to it’s rather simplistic implementation.
This commit is contained in:
icex2
2024-08-15 11:34:31 +02:00
parent 57e68f928a
commit e61dfb3977
48 changed files with 693 additions and 1055 deletions
+5
View File
@@ -2,3 +2,8 @@ LIBRARY ddrhook1
EXPORTS
DllMain@12 @1 NONAME
; Bemanitools 6 API
bt_module_core_log_api_set
bt_module_core_thread_api_set
bt_module_hook_api_get
+35 -44
View File
@@ -2,9 +2,6 @@
#include <stdbool.h>
#include "avs-ext/log.h"
#include "avs-ext/thread.h"
#include "cconfig/cconfig-hook.h"
#include "core/log-bt-ext.h"
@@ -31,7 +28,6 @@
#include "hook/iohook.h"
#include "hook/table.h"
#include "hooklib/app.h"
#include "hooklib/rs232.h"
#include "iface-core/log.h"
@@ -40,13 +36,15 @@
#include "iface-io/ddr.h"
#include "iface-io/eam.h"
#include "imports/avs.h"
#include "module/io-ext.h"
#include "module/io.h"
#include "p3ioemu/emu.h"
#include "sdk/module/core/log.h"
#include "sdk/module/core/thread.h"
#include "sdk/module/hook.h"
#include "security/rp-sign-key.h"
#include "util/cmdline.h"
@@ -65,24 +63,9 @@ static const hook_d3d9_irp_handler_t ddrhook1_d3d9_handlers[] = {
gfx_d3d9_irp_handler,
};
static DWORD STDCALL
my_GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize);
static DWORD(STDCALL *real_GetModuleFileNameA)(
HMODULE hModule, LPSTR lpFilename, DWORD nSize);
static bool ddrhook1_init_check = false;
static module_io_t *_ddrhook1_module_io_ddr;
static module_io_t *_ddrhook1_module_io_eam;
static const struct hook_symbol init_hook_syms[] = {
{
.name = "GetModuleFileNameA",
.patch = my_GetModuleFileNameA,
.link = (void **) &real_GetModuleFileNameA,
},
};
static void _ddrhook1_io_ddr_init(module_io_t **module)
{
bt_io_ddr_api_t api;
@@ -103,8 +86,8 @@ static void _ddrhook1_io_eam_init(module_io_t **module)
bt_io_eam_api_set(&api);
}
static DWORD STDCALL
my_GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
static bool
_ddrhook1_main_init(HMODULE game_module, const bt_core_config_t *config_)
{
bool ok;
struct cconfig *config;
@@ -114,13 +97,14 @@ my_GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
struct ddrhook1_config_gfx config_gfx;
struct ddrhook1_config_security config_security;
if (ddrhook1_init_check)
goto skip;
ddrhook1_master_insert_hooks(NULL);
ddrhook1_filesystem_hook_init();
hook_d3d9_init(
ddrhook1_d3d9_handlers, lengthof(ddrhook1_d3d9_handlers));
log_info("--- Begin ddrhook1 main ---");
ddrhook1_init_check = true;
config = cconfig_init();
ddrhook1_config_ddrhook1_init(config);
@@ -214,26 +198,33 @@ my_GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
log_info("--- End ddrhook1 main ---");
skip:
return real_GetModuleFileNameA(hModule, lpFilename, nSize);
return true;
}
static void _ddrhook1_main_fini()
{
// TODO cleanup
}
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 = _ddrhook1_main_init;
api->v1.main_fini = _ddrhook1_main_fini;
}
BOOL WINAPI DllMain(HMODULE self, DWORD reason, void *ctx)
{
if (reason == DLL_PROCESS_ATTACH) {
// Use AVS APIs
avs_ext_log_core_api_set();
avs_ext_thread_core_api_set();
hook_table_apply(
NULL, "kernel32.dll", init_hook_syms, lengthof(init_hook_syms));
ddrhook1_master_insert_hooks(NULL);
ddrhook1_filesystem_hook_init();
hook_d3d9_init(
ddrhook1_d3d9_handlers, lengthof(ddrhook1_d3d9_handlers));
}
return TRUE;
}