From ed617dc879c529c93934c2c39357dfe14c5ea813 Mon Sep 17 00:00:00 2001 From: Edo Date: Wed, 17 Aug 2022 21:53:52 +0200 Subject: [PATCH] [Filesystem] Use info from launcher (#440) * [Filesystem] Use info from launcher --- src/Components/Modules/FileSystem.cpp | 15 ++++++++++++--- src/Components/Modules/FileSystem.hpp | 2 ++ src/Utils/Utils.cpp | 15 ++++++++++----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/Components/Modules/FileSystem.cpp b/src/Components/Modules/FileSystem.cpp index ab6970f2..641714f9 100644 --- a/src/Components/Modules/FileSystem.cpp +++ b/src/Components/Modules/FileSystem.cpp @@ -243,9 +243,9 @@ namespace Components void FileSystem::RegisterFolder(const char* folder) { - std::string fs_cdpath = Dvar::Var("fs_cdpath").get(); - std::string fs_basepath = Dvar::Var("fs_basepath").get(); - std::string fs_homepath = Dvar::Var("fs_homepath").get(); + const auto fs_cdpath = Dvar::Var("fs_cdpath").get(); + const auto fs_basepath = Dvar::Var("fs_basepath").get(); + const auto fs_homepath = Dvar::Var("fs_homepath").get(); if (!fs_cdpath.empty()) Game::FS_AddLocalizedGameDirectory(fs_cdpath.data(), folder); if (!fs_basepath.empty()) Game::FS_AddLocalizedGameDirectory(fs_basepath.data(), folder); @@ -323,6 +323,12 @@ namespace Components Utils::Hook::Call(0x4291A0)(iwd); } + const char* FileSystem::Sys_DefaultInstallPath_Hk() + { + static auto current_path = std::filesystem::current_path().string(); + return current_path.data(); + } + FileSystem::FileSystem() { FileSystem::MemAllocator.clear(); @@ -370,6 +376,9 @@ namespace Components // Handle IWD freeing Utils::Hook(0x642F60, FileSystem::IwdFreeStub, HOOK_CALL).install()->quick(); + + // Set the working dir based on info from the Xlabs launcher + Utils::Hook(0x4326E0, FileSystem::Sys_DefaultInstallPath_Hk, HOOK_JUMP).install()->quick(); } FileSystem::~FileSystem() diff --git a/src/Components/Modules/FileSystem.hpp b/src/Components/Modules/FileSystem.hpp index 2a1064f5..50573b40 100644 --- a/src/Components/Modules/FileSystem.hpp +++ b/src/Components/Modules/FileSystem.hpp @@ -116,5 +116,7 @@ namespace Components static int LoadTextureSync(Game::GfxImageLoadDef **loadDef, Game::GfxImage *image); static void IwdFreeStub(Game::iwd_t* iwd); + + static const char* Sys_DefaultInstallPath_Hk(); }; } diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index 6d9cd4df..520faa9d 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -106,13 +106,18 @@ namespace Utils void SetEnvironment() { - wchar_t exeName[512]; - GetModuleFileNameW(GetModuleHandle(nullptr), exeName, sizeof(exeName) / sizeof(wchar_t)); + char* buffer{}; + std::size_t size{}; + if (_dupenv_s(&buffer, &size, "XLABS_MW2_INSTALL") != 0 || buffer == nullptr) + { + return; + } - auto* exeBaseName = wcsrchr(exeName, L'\\'); - exeBaseName[0] = L'\0'; + const auto _0 = gsl::finally([&] { std::free(buffer); }); - SetCurrentDirectoryW(exeName); + std::string dir{buffer, size}; + SetCurrentDirectoryA(dir.data()); + SetDllDirectoryA(dir.data()); } HMODULE GetNTDLL()