From 434382ce4b848c1768407703c03ff8be024c86f8 Mon Sep 17 00:00:00 2001 From: Kevin Trocolli Date: Sun, 7 Apr 2024 17:55:38 -0400 Subject: [PATCH] vfs: hook System_getAppRootPath for unity games --- platform/platform.c | 2 +- platform/vfs.c | 34 +++++++++++++++++++++++++++++++++- platform/vfs.h | 2 +- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/platform/platform.c b/platform/platform.c index bf44800..51279f9 100644 --- a/platform/platform.c +++ b/platform/platform.c @@ -75,7 +75,7 @@ HRESULT platform_hook_init( return hr; } - hr = vfs_hook_init(&cfg->vfs); + hr = vfs_hook_init(&cfg->vfs, game_id); if (FAILED(hr)) { return hr; diff --git a/platform/vfs.c b/platform/vfs.c index befc87d..e02114f 100644 --- a/platform/vfs.c +++ b/platform/vfs.c @@ -8,6 +8,8 @@ #include "hooklib/path.h" #include "hooklib/reg.h" +#include "hook/procaddr.h" +#include "hook/table.h" #include "platform/vfs.h" @@ -31,6 +33,18 @@ static HRESULT vfs_path_hook_option( static HRESULT vfs_reg_read_amfs(void *bytes, uint32_t *nbytes); static HRESULT vfs_reg_read_appdata(void *bytes, uint32_t *nbytes); +static wchar_t* my_System_getAppRootPath(); +static wchar_t* (*next_System_getAppRootPath)(); + +static const struct hook_symbol amdaemon_syms[] = { + { + .name = "System_getAppRootPath", + .patch = my_System_getAppRootPath, + .link = (void **) &next_System_getAppRootPath, + }, +}; + +static wchar_t game[5] = {0}; static wchar_t vfs_nthome_real[MAX_PATH]; static const wchar_t vfs_nthome[] = L"C:\\Documents and Settings\\AppUser"; static const size_t vfs_nthome_len = _countof(vfs_nthome) - 1; @@ -55,7 +69,7 @@ static const struct reg_hook_val vfs_reg_vals[] = { static struct vfs_config vfs_config; -HRESULT vfs_hook_init(const struct vfs_config *config) +HRESULT vfs_hook_init(const struct vfs_config *config, const char* game_id) { wchar_t temp[MAX_PATH]; size_t nthome_len; @@ -68,6 +82,8 @@ HRESULT vfs_hook_init(const struct vfs_config *config) return S_FALSE; } + mbstowcs(game, game_id, 4); + if (config->amfs[0] == L'\0') { dprintf("Vfs: FATAL: AMFS path not specified in INI file\n"); @@ -175,6 +191,13 @@ HRESULT vfs_hook_init(const struct vfs_config *config) return hr; } + proc_addr_table_push( + NULL, + "amdaemon_api.dll", + amdaemon_syms, + _countof(amdaemon_syms) + ); + return S_OK; } @@ -473,3 +496,12 @@ static HRESULT vfs_reg_read_appdata(void *bytes, uint32_t *nbytes) { return reg_hook_read_wstr(bytes, nbytes, vfs_config.appdata); } + +static wchar_t* my_System_getAppRootPath() +{ + wchar_t *path = malloc(sizeof(wchar_t) * MAX_PATH); + wcscpy_s(path, MAX_PATH, vfs_config.appdata); + wcscat_s(path, MAX_PATH, game); + wcscat_s(path, MAX_PATH, L"\\"); + return path; +} diff --git a/platform/vfs.h b/platform/vfs.h index 8767305..a7e513f 100644 --- a/platform/vfs.h +++ b/platform/vfs.h @@ -12,4 +12,4 @@ struct vfs_config { wchar_t option[MAX_PATH]; }; -HRESULT vfs_hook_init(const struct vfs_config *config); +HRESULT vfs_hook_init(const struct vfs_config *config, const char* game_id);