mai2/mu3/cm: Integrate UnityDoorstop with segatools (#11)

[UnityDoorstop](https://github.com/NeighTools/UnityDoorstop) is a tool to execute managed code (.NET DLLs) before Unity does, useful for modding frameworks such as BepInEx.

This PR integrates parts of its code into segatools, so loading BepInEx is as simple as adding 2 lines to `segatools.ini`:
```ini
[unity]
targetAssembly=BepInEx\core\BepInEx.Preloader.dll
```

This PR also factors out the Unity path redirection hooks to its own module.

Reviewed-on: Dniel97/segatools#11
Co-authored-by: beerpsi <beerpsi@duck.com>
Co-committed-by: beerpsi <beerpsi@duck.com>
This commit is contained in:
2024-04-15 19:30:28 +00:00
committed by Dniel97
parent 47a65e5e51
commit 4041844ea9
30 changed files with 436 additions and 232 deletions

View File

@ -42,4 +42,5 @@ void mu3_hook_config_load(
gfx_config_load(&cfg->gfx, filename);
vfd_config_load(&cfg->vfd, filename);
mu3_dll_config_load(&cfg->dll, filename);
unity_config_load(&cfg->unity, filename);
}

View File

@ -13,6 +13,8 @@
#include "platform/config.h"
#include "unityhook/config.h"
struct mu3_hook_config {
struct platform_config platform;
struct aime_config aime;
@ -22,6 +24,7 @@ struct mu3_hook_config {
// struct led15093_config led15093;
struct vfd_config vfd;
struct mu3_dll_config dll;
struct unity_config unity;
};
void mu3_dll_config_load(

View File

@ -2,7 +2,6 @@
#include <stdlib.h>
#include "board/io4.h"
#include "board/sg-reader.h"
#include "board/vfd.h"
@ -20,10 +19,12 @@
#include "mu3hook/config.h"
#include "mu3hook/io4.h"
#include "mu3hook/mu3-dll.h"
#include "mu3hook/unity.h"
#include "platform/platform.h"
#include "unityhook/config.h"
#include "unityhook/hook.h"
#include "util/dprintf.h"
static HMODULE mu3_hook_mod;
@ -99,7 +100,7 @@ static DWORD CALLBACK mu3_pre_startup(void)
There seems to be an issue with other DLL hooks if `LoadLibraryW` is
hooked earlier in the `mu3hook` initialization. */
unity_hook_init();
unity_hook_init(&mu3_hook_cfg.unity, mu3_hook_mod);
/* Initialize debug helpers */

View File

@ -17,6 +17,7 @@ shared_library(
hooklib_lib,
mu3io_lib,
platform_lib,
unityhook_lib,
util_lib,
],
sources : [
@ -27,7 +28,5 @@ shared_library(
'io4.h',
'mu3-dll.c',
'mu3-dll.h',
'unity.h',
'unity.c',
],
)

View File

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

View File

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