forked from Hay1tsme/segatools
CRLF -> LF
This commit is contained in:
@ -1,12 +1,12 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
void unity_config_load(struct unity_config *cfg, const wchar_t *filename) {
|
void unity_config_load(struct unity_config *cfg, const wchar_t *filename) {
|
||||||
GetPrivateProfileStringW(
|
GetPrivateProfileStringW(
|
||||||
L"unity",
|
L"unity",
|
||||||
L"targetAssembly",
|
L"targetAssembly",
|
||||||
L"",
|
L"",
|
||||||
cfg->target_assembly,
|
cfg->target_assembly,
|
||||||
_countof(cfg->target_assembly),
|
_countof(cfg->target_assembly),
|
||||||
filename
|
filename
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,173 +1,173 @@
|
|||||||
// A simplified version of NeighTools' UnityDoorstop, allowing mod loaders
|
// A simplified version of NeighTools' UnityDoorstop, allowing mod loaders
|
||||||
// like BepInEx to be loaded into Unity games.
|
// like BepInEx to be loaded into Unity games.
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: CC0
|
// SPDX-License-Identifier: CC0
|
||||||
// https://github.com/NeighTools/UnityDoorstop
|
// https://github.com/NeighTools/UnityDoorstop
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <pathcch.h>
|
#include <pathcch.h>
|
||||||
#include <psapi.h>
|
#include <psapi.h>
|
||||||
|
|
||||||
#include "hooklib/procaddr.h"
|
#include "hooklib/procaddr.h"
|
||||||
#include "util/dprintf.h"
|
#include "util/dprintf.h"
|
||||||
|
|
||||||
#include "doorstop.h"
|
#include "doorstop.h"
|
||||||
#include "mono.h"
|
#include "mono.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
static void * my_mono_jit_init_version(const char *root_domain_name, const char *runtime_version);
|
static void * my_mono_jit_init_version(const char *root_domain_name, const char *runtime_version);
|
||||||
void doorstop_invoke(void *domain);
|
void doorstop_invoke(void *domain);
|
||||||
|
|
||||||
static char module_name[MAX_PATH];
|
static char module_name[MAX_PATH];
|
||||||
static bool doorstop_hook_initted;
|
static bool doorstop_hook_initted;
|
||||||
static struct unity_config unity_config;
|
static struct unity_config unity_config;
|
||||||
static struct hook_symbol unity_mono_syms[] = {
|
static struct hook_symbol unity_mono_syms[] = {
|
||||||
{
|
{
|
||||||
.name = "mono_jit_init_version",
|
.name = "mono_jit_init_version",
|
||||||
.patch = my_mono_jit_init_version,
|
.patch = my_mono_jit_init_version,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void doorstop_mono_hook_init(const struct unity_config *cfg, HINSTANCE module) {
|
void doorstop_mono_hook_init(const struct unity_config *cfg, HINSTANCE module) {
|
||||||
if (doorstop_hook_initted || cfg->target_assembly[0] == 0) {
|
if (doorstop_hook_initted || cfg->target_assembly[0] == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
GetModuleBaseNameA(GetCurrentProcess(), module, module_name, MAX_PATH);
|
GetModuleBaseNameA(GetCurrentProcess(), module, module_name, MAX_PATH);
|
||||||
|
|
||||||
memcpy(&unity_config, cfg, sizeof(*cfg));
|
memcpy(&unity_config, cfg, sizeof(*cfg));
|
||||||
load_mono_functions(module);
|
load_mono_functions(module);
|
||||||
proc_addr_table_push(module_name, unity_mono_syms, _countof(unity_mono_syms));
|
proc_addr_table_push(module_name, unity_mono_syms, _countof(unity_mono_syms));
|
||||||
|
|
||||||
doorstop_hook_initted = true;
|
doorstop_hook_initted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void * my_mono_jit_init_version(const char *root_domain_name, const char *runtime_version) {
|
static void * my_mono_jit_init_version(const char *root_domain_name, const char *runtime_version) {
|
||||||
dprintf("Unity: Starting Mono domain \"%s\"\n", root_domain_name);
|
dprintf("Unity: Starting Mono domain \"%s\"\n", root_domain_name);
|
||||||
|
|
||||||
SetEnvironmentVariableW(L"DOORSTOP_DLL_SEARCH_DIRS", widen(mono_assembly_getrootdir()));
|
SetEnvironmentVariableW(L"DOORSTOP_DLL_SEARCH_DIRS", widen(mono_assembly_getrootdir()));
|
||||||
|
|
||||||
void* domain = mono_jit_init_version(root_domain_name, runtime_version);
|
void* domain = mono_jit_init_version(root_domain_name, runtime_version);
|
||||||
|
|
||||||
doorstop_invoke(domain);
|
doorstop_invoke(domain);
|
||||||
return domain;
|
return domain;
|
||||||
}
|
}
|
||||||
|
|
||||||
void doorstop_invoke(void* domain) {
|
void doorstop_invoke(void* domain) {
|
||||||
if (GetEnvironmentVariableW(L"DOORSTOP_INITIALIZED", NULL, 0) != 0) {
|
if (GetEnvironmentVariableW(L"DOORSTOP_INITIALIZED", NULL, 0) != 0) {
|
||||||
dprintf("Unity: Doorstop is already initialized.\n");
|
dprintf("Unity: Doorstop is already initialized.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
SetEnvironmentVariableW(L"DOORSTOP_INITIALIZED", L"TRUE");
|
SetEnvironmentVariableW(L"DOORSTOP_INITIALIZED", L"TRUE");
|
||||||
|
|
||||||
mono_thread_set_main(mono_thread_current());
|
mono_thread_set_main(mono_thread_current());
|
||||||
|
|
||||||
if (mono_domain_set_config) {
|
if (mono_domain_set_config) {
|
||||||
#define CONFIG_EXT L".config"
|
#define CONFIG_EXT L".config"
|
||||||
|
|
||||||
wchar_t exe_path[MAX_PATH];
|
wchar_t exe_path[MAX_PATH];
|
||||||
size_t exe_path_len = GetModuleFileNameW(NULL, exe_path, MAX_PATH);
|
size_t exe_path_len = GetModuleFileNameW(NULL, exe_path, MAX_PATH);
|
||||||
wchar_t *folder_name = wcsdup(exe_path);
|
wchar_t *folder_name = wcsdup(exe_path);
|
||||||
|
|
||||||
PathCchRemoveFileSpec(folder_name, exe_path_len + 1);
|
PathCchRemoveFileSpec(folder_name, exe_path_len + 1);
|
||||||
|
|
||||||
char *exe_path_n = narrow(exe_path);
|
char *exe_path_n = narrow(exe_path);
|
||||||
char *folder_name_n = narrow(folder_name);
|
char *folder_name_n = narrow(folder_name);
|
||||||
|
|
||||||
dprintf("Unity: Setting config paths: base dir: %s; config path: %s\n", folder_name_n, exe_path_n);
|
dprintf("Unity: Setting config paths: base dir: %s; config path: %s\n", folder_name_n, exe_path_n);
|
||||||
|
|
||||||
mono_domain_set_config(domain, folder_name_n, exe_path_n);
|
mono_domain_set_config(domain, folder_name_n, exe_path_n);
|
||||||
|
|
||||||
free(folder_name);
|
free(folder_name);
|
||||||
free(exe_path_n);
|
free(exe_path_n);
|
||||||
free(folder_name_n);
|
free(folder_name_n);
|
||||||
|
|
||||||
#undef CONFIG_EXT
|
#undef CONFIG_EXT
|
||||||
}
|
}
|
||||||
|
|
||||||
SetEnvironmentVariableW(L"DOORSTOP_INVOKE_DLL_PATH", unity_config.target_assembly);
|
SetEnvironmentVariableW(L"DOORSTOP_INVOKE_DLL_PATH", unity_config.target_assembly);
|
||||||
|
|
||||||
char *assembly_dir = mono_assembly_getrootdir();
|
char *assembly_dir = mono_assembly_getrootdir();
|
||||||
dprintf("Unity: Assembly directory: %s\n", assembly_dir);
|
dprintf("Unity: Assembly directory: %s\n", assembly_dir);
|
||||||
|
|
||||||
SetEnvironmentVariableA("DOORSTOP_MANAGED_FOLDER_DIR", assembly_dir);
|
SetEnvironmentVariableA("DOORSTOP_MANAGED_FOLDER_DIR", assembly_dir);
|
||||||
|
|
||||||
wchar_t app_path[MAX_PATH];
|
wchar_t app_path[MAX_PATH];
|
||||||
GetModuleFileNameW(NULL, app_path, MAX_PATH);
|
GetModuleFileNameW(NULL, app_path, MAX_PATH);
|
||||||
SetEnvironmentVariableW(L"DOORSTOP_PROCESS_PATH", app_path);
|
SetEnvironmentVariableW(L"DOORSTOP_PROCESS_PATH", app_path);
|
||||||
|
|
||||||
char* dll_path = narrow(unity_config.target_assembly);
|
char* dll_path = narrow(unity_config.target_assembly);
|
||||||
|
|
||||||
dprintf("Unity: Loading assembly: %s\n", dll_path);
|
dprintf("Unity: Loading assembly: %s\n", dll_path);
|
||||||
|
|
||||||
void* assembly = mono_domain_assembly_open(domain, dll_path);
|
void* assembly = mono_domain_assembly_open(domain, dll_path);
|
||||||
|
|
||||||
if (!assembly) {
|
if (!assembly) {
|
||||||
dprintf("Unity: Failed to load assembly\n");
|
dprintf("Unity: Failed to load assembly\n");
|
||||||
free(dll_path);
|
free(dll_path);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *image = mono_assembly_get_image(assembly);
|
void *image = mono_assembly_get_image(assembly);
|
||||||
|
|
||||||
if (!image) {
|
if (!image) {
|
||||||
dprintf("Unity: Assembly image doesn't exist\n");
|
dprintf("Unity: Assembly image doesn't exist\n");
|
||||||
free(dll_path);
|
free(dll_path);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *desc = mono_method_desc_new("*:Main", FALSE);
|
void *desc = mono_method_desc_new("*:Main", FALSE);
|
||||||
void *method = mono_method_desc_search_in_image(desc, image);
|
void *method = mono_method_desc_search_in_image(desc, image);
|
||||||
|
|
||||||
if (!method) {
|
if (!method) {
|
||||||
dprintf("Unity: Assembly does not have a valid entrypoint.\n");
|
dprintf("Unity: Assembly does not have a valid entrypoint.\n");
|
||||||
free(dll_path);
|
free(dll_path);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *signature = mono_method_signature(method);
|
void *signature = mono_method_signature(method);
|
||||||
UINT32 params = mono_signature_get_param_count(signature);
|
UINT32 params = mono_signature_get_param_count(signature);
|
||||||
void **args = NULL;
|
void **args = NULL;
|
||||||
|
|
||||||
if (params == 1) {
|
if (params == 1) {
|
||||||
// If there is a parameter, it's most likely a string[].
|
// If there is a parameter, it's most likely a string[].
|
||||||
void *args_array = mono_array_new(domain, mono_get_string_class(), 0);
|
void *args_array = mono_array_new(domain, mono_get_string_class(), 0);
|
||||||
args = malloc(sizeof(void*) * 1);
|
args = malloc(sizeof(void*) * 1);
|
||||||
args[0] = args_array;
|
args[0] = args_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
dprintf("Unity: Invoking method %p\n", method);
|
dprintf("Unity: Invoking method %p\n", method);
|
||||||
|
|
||||||
void *exc = NULL;
|
void *exc = NULL;
|
||||||
mono_runtime_invoke(method, NULL, args, &exc);
|
mono_runtime_invoke(method, NULL, args, &exc);
|
||||||
|
|
||||||
if (exc) {
|
if (exc) {
|
||||||
dprintf("Unity: Error invoking method!\n");
|
dprintf("Unity: Error invoking method!\n");
|
||||||
|
|
||||||
void *ex_class = mono_get_exception_class();
|
void *ex_class = mono_get_exception_class();
|
||||||
void *to_string_desc = mono_method_desc_new("*:ToString()", FALSE);
|
void *to_string_desc = mono_method_desc_new("*:ToString()", FALSE);
|
||||||
void* to_string_method = mono_method_desc_search_in_class(to_string_desc, ex_class);
|
void* to_string_method = mono_method_desc_search_in_class(to_string_desc, ex_class);
|
||||||
|
|
||||||
mono_method_desc_free(to_string_desc);
|
mono_method_desc_free(to_string_desc);
|
||||||
|
|
||||||
if (to_string_method) {
|
if (to_string_method) {
|
||||||
void* real_to_string_method = mono_object_get_virtual_method(exc, to_string_method);
|
void* real_to_string_method = mono_object_get_virtual_method(exc, to_string_method);
|
||||||
void* exc2 = NULL;
|
void* exc2 = NULL;
|
||||||
void* str = mono_runtime_invoke(real_to_string_method, exc, NULL, &exc2);
|
void* str = mono_runtime_invoke(real_to_string_method, exc, NULL, &exc2);
|
||||||
|
|
||||||
if (!exc2) {
|
if (!exc2) {
|
||||||
char* exc_str = mono_string_to_utf8(str);
|
char* exc_str = mono_string_to_utf8(str);
|
||||||
dprintf("Unity: Error message: %s\n", exc_str);
|
dprintf("Unity: Error message: %s\n", exc_str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mono_method_desc_free(desc);
|
mono_method_desc_free(desc);
|
||||||
free(dll_path);
|
free(dll_path);
|
||||||
|
|
||||||
if (args) {
|
if (args) {
|
||||||
free(args);
|
free(args);
|
||||||
args = NULL;
|
args = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
220
unityhook/hook.c
220
unityhook/hook.c
@ -1,110 +1,110 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "hook/table.h"
|
#include "hook/table.h"
|
||||||
#include "hooklib/path.h"
|
#include "hooklib/path.h"
|
||||||
#include "util/dprintf.h"
|
#include "util/dprintf.h"
|
||||||
|
|
||||||
#include "doorstop.h"
|
#include "doorstop.h"
|
||||||
#include "hook.h"
|
#include "hook.h"
|
||||||
|
|
||||||
static bool unity_hook_initted;
|
static bool unity_hook_initted;
|
||||||
static struct unity_config unity_config;
|
static struct unity_config unity_config;
|
||||||
|
|
||||||
static const wchar_t *target_modules[] = {
|
static const wchar_t *target_modules[] = {
|
||||||
L"mono.dll",
|
L"mono.dll",
|
||||||
L"mono-2.0-bdwgc.dll",
|
L"mono-2.0-bdwgc.dll",
|
||||||
L"cri_ware_unity.dll",
|
L"cri_ware_unity.dll",
|
||||||
};
|
};
|
||||||
static const size_t target_modules_len = _countof(target_modules);
|
static const size_t target_modules_len = _countof(target_modules);
|
||||||
|
|
||||||
static void dll_hook_insert_hooks(HMODULE target);
|
static void dll_hook_insert_hooks(HMODULE target);
|
||||||
|
|
||||||
static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name);
|
static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name);
|
||||||
static HMODULE (WINAPI *next_LoadLibraryW)(const wchar_t *name);
|
static HMODULE (WINAPI *next_LoadLibraryW)(const wchar_t *name);
|
||||||
|
|
||||||
static const struct hook_symbol unity_kernel32_syms[] = {
|
static const struct hook_symbol unity_kernel32_syms[] = {
|
||||||
{
|
{
|
||||||
.name = "LoadLibraryW",
|
.name = "LoadLibraryW",
|
||||||
.patch = my_LoadLibraryW,
|
.patch = my_LoadLibraryW,
|
||||||
.link = (void **) &next_LoadLibraryW,
|
.link = (void **) &next_LoadLibraryW,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
void unity_hook_init(const struct unity_config *cfg, HINSTANCE self) {
|
void unity_hook_init(const struct unity_config *cfg, HINSTANCE self) {
|
||||||
assert(cfg != NULL);
|
assert(cfg != NULL);
|
||||||
|
|
||||||
if (unity_hook_initted) {
|
if (unity_hook_initted) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(&unity_config, cfg, sizeof(*cfg));
|
memcpy(&unity_config, cfg, sizeof(*cfg));
|
||||||
dll_hook_insert_hooks(NULL);
|
dll_hook_insert_hooks(NULL);
|
||||||
|
|
||||||
unity_hook_initted = true;
|
unity_hook_initted = true;
|
||||||
dprintf("Unity: Hook enabled.\n");
|
dprintf("Unity: Hook enabled.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dll_hook_insert_hooks(HMODULE target) {
|
static void dll_hook_insert_hooks(HMODULE target) {
|
||||||
hook_table_apply(
|
hook_table_apply(
|
||||||
target,
|
target,
|
||||||
"kernel32.dll",
|
"kernel32.dll",
|
||||||
unity_kernel32_syms,
|
unity_kernel32_syms,
|
||||||
_countof(unity_kernel32_syms));
|
_countof(unity_kernel32_syms));
|
||||||
}
|
}
|
||||||
|
|
||||||
static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name) {
|
static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name) {
|
||||||
const wchar_t *name_end;
|
const wchar_t *name_end;
|
||||||
const wchar_t *target_module;
|
const wchar_t *target_module;
|
||||||
bool already_loaded;
|
bool already_loaded;
|
||||||
HMODULE result;
|
HMODULE result;
|
||||||
size_t name_len;
|
size_t name_len;
|
||||||
size_t target_module_len;
|
size_t target_module_len;
|
||||||
|
|
||||||
if (name == NULL) {
|
if (name == NULL) {
|
||||||
SetLastError(ERROR_INVALID_PARAMETER);
|
SetLastError(ERROR_INVALID_PARAMETER);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the module is already loaded
|
// Check if the module is already loaded
|
||||||
already_loaded = GetModuleHandleW(name) != NULL;
|
already_loaded = GetModuleHandleW(name) != NULL;
|
||||||
|
|
||||||
// Must call the next handler so the DLL reference count is incremented
|
// Must call the next handler so the DLL reference count is incremented
|
||||||
result = next_LoadLibraryW(name);
|
result = next_LoadLibraryW(name);
|
||||||
|
|
||||||
if (!already_loaded && result != NULL) {
|
if (!already_loaded && result != NULL) {
|
||||||
name_len = wcslen(name);
|
name_len = wcslen(name);
|
||||||
|
|
||||||
// mono entrypoint for injecting target_assembly
|
// mono entrypoint for injecting target_assembly
|
||||||
if (GetProcAddress(result, "mono_jit_init_version")) {
|
if (GetProcAddress(result, "mono_jit_init_version")) {
|
||||||
doorstop_mono_hook_init(&unity_config, result);
|
doorstop_mono_hook_init(&unity_config, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < target_modules_len; i++) {
|
for (size_t i = 0; i < target_modules_len; i++) {
|
||||||
target_module = target_modules[i];
|
target_module = target_modules[i];
|
||||||
target_module_len = wcslen(target_module);
|
target_module_len = wcslen(target_module);
|
||||||
|
|
||||||
// Check if the newly loaded library is at least the length of
|
// Check if the newly loaded library is at least the length of
|
||||||
// the name of the target module
|
// the name of the target module
|
||||||
if (name_len < target_module_len) {
|
if (name_len < target_module_len) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
name_end = &name[name_len - target_module_len];
|
name_end = &name[name_len - target_module_len];
|
||||||
|
|
||||||
// Check if the name of the newly loaded library is one of the
|
// Check if the name of the newly loaded library is one of the
|
||||||
// modules the path hooks should be injected into
|
// modules the path hooks should be injected into
|
||||||
if (_wcsicmp(name_end, target_module) != 0) {
|
if (_wcsicmp(name_end, target_module) != 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
dprintf("Unity: Loaded %S\n", target_module);
|
dprintf("Unity: Loaded %S\n", target_module);
|
||||||
|
|
||||||
dll_hook_insert_hooks(result);
|
dll_hook_insert_hooks(result);
|
||||||
path_hook_insert_hooks(result);
|
path_hook_insert_hooks(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user