From d1402a74657233aa4c2e3184011200ba18a5b605 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Mon, 16 May 2022 20:44:19 +0200 Subject: [PATCH] General cleanup and improvements Hook CPakFile::Unload() and track when mp_lobby is unloaded. Removed duplicate pointers for MOD_ProcessPakQueue(). Use DWORD for thread id comparison in MOD_ProcessPakQueue(). Use RPakHandle_t for all RPak handles. --- r5dev/engine/cmodel_bsp.cpp | 19 ++++++++++--------- r5dev/engine/cmodel_bsp.h | 10 ++-------- r5dev/engine/sys_utils.cpp | 5 ----- r5dev/filesystem/filesystem.h | 4 ++-- r5dev/rtech/rtech_game.cpp | 7 +++++-- r5dev/rtech/rtech_game.h | 4 ++-- r5dev/vstdlib/completion.cpp | 2 +- 7 files changed, 22 insertions(+), 29 deletions(-) diff --git a/r5dev/engine/cmodel_bsp.cpp b/r5dev/engine/cmodel_bsp.cpp index 751f113b..7c203f11 100644 --- a/r5dev/engine/cmodel_bsp.cpp +++ b/r5dev/engine/cmodel_bsp.cpp @@ -13,6 +13,7 @@ #include "rtech/rtech_utils.h" #include "rtech/rtech_game.h" #include "datacache/mdlcache.h" +#include "filesystem/filesystem.h" string g_svLevelName; bool s_bLevelResourceInitialized = false; @@ -127,7 +128,7 @@ void MOD_ProcessPakQueue() { return; } - if ((*(unsigned __int8(__fastcall**)(__int64))(*(_QWORD*)*g_FileSystem + 696i64))(*g_FileSystem) && !*dword_1634F445C) + if ((*(unsigned __int8(__fastcall**)(__int64))(*(_QWORD*)*(_QWORD*)g_pFileSystem_Stdio + 696i64))(*(_QWORD*)g_pFileSystem_Stdio) && !*dword_1634F445C) { v1 = &*off_141874660; for (i = 0; i < 5; ++i) @@ -163,9 +164,9 @@ void MOD_ProcessPakQueue() v11 = *(_DWORD*)v10; v12 = *(_DWORD*)v10 & 0x1FF; v10[4] = 1; - if (*((_DWORD*)&*unk_167D40B70 + 46 * v12) == v11) + if (*((_DWORD*)&*g_pLoadedPakInfo + 46 * v12) == v11) { - v13 = *((_DWORD*)&*unk_167D40B70 + 46 * v12 + 1); + v13 = *((_DWORD*)&*g_pLoadedPakInfo + 46 * v12 + 1); v14 = v10[4]; } else @@ -240,8 +241,8 @@ void MOD_ProcessPakQueue() JT_ReleaseFifoLock((JobFifoLock_s*)&*qword_167ED7BE0); v23 = *qword_1671061C8; } - (*(void(__fastcall**)(__int64, __int64))(*(_QWORD*)*g_FileSystem + 656i64))(*g_FileSystem, 256i64); - (*(void(__fastcall**)(__int64, __int64))(*(_QWORD*)*g_FileSystem + 648i64))(*g_FileSystem, v23); + (*(void(__fastcall**)(__int64, __int64))(*(_QWORD*)*(_QWORD*)g_pFileSystem_Stdio + 656i64))(*(_QWORD*)g_pFileSystem_Stdio, 256i64); + (*(void(__fastcall**)(__int64, __int64))(*(_QWORD*)*(_QWORD*)g_pFileSystem_Stdio + 648i64))(*(_QWORD*)g_pFileSystem_Stdio, v23); } } } @@ -264,7 +265,7 @@ void MOD_ProcessPakQueue() if (*(_DWORD*)v15 != -1) { v22 = 184i64 * (v21 & 0x1FF); - if (*(_DWORD*)((char*)&*unk_167D40B70 + v22) != v21 || ((*(_DWORD*)((char*)&*unk_167D40B70 + v22 + 4) - 9) & 0xFFFFFFFB) != 0) + if (*(_DWORD*)((char*)&*g_pLoadedPakInfo + v22) != v21 || ((*(_DWORD*)((char*)&*g_pLoadedPakInfo + v22 + 4) - 9) & 0xFFFFFFFB) != 0) { *byte_16709DDDF = 0; return; } @@ -274,14 +275,14 @@ void MOD_ProcessPakQueue() } //----------------------------------------------------------------------------- -// Purpose: load assets for level with fifolock (still not reliable enough). +// Purpose: load assets for level with fifolock. // Input : *szLevelName - // Output : true on success, false on failure //----------------------------------------------------------------------------- bool MOD_LoadPakForMap(const char* szLevelName) { - if (MOD_LevelHasChanged(szLevelName)) - s_bLevelResourceInitialized = false; + if (MOD_LevelHasChanged(szLevelName)) + s_bLevelResourceInitialized = false; g_svLevelName = szLevelName; return v_MOD_LoadPakForMap(szLevelName); diff --git a/r5dev/engine/cmodel_bsp.h b/r5dev/engine/cmodel_bsp.h index 57b2b357..b206ef59 100644 --- a/r5dev/engine/cmodel_bsp.h +++ b/r5dev/engine/cmodel_bsp.h @@ -14,10 +14,8 @@ inline int32_t * dword_1634F445C; inline void** qword_167ED7BB8; inline void** qword_14180A098; inline bool* byte_16709DDDF; -inline int64_t * g_FileSystem; inline char** off_141874660; inline void** unk_141874555; -inline void** unk_167D40B70; inline void** unk_1418749B0; inline void** unk_141874550; inline int64_t* qword_1671061C8; @@ -25,7 +23,7 @@ inline int64_t* qword_167ED7BC0; inline int64_t* qword_167ED7C68; inline int64_t* qword_167ED7BE0; inline int64_t* qword_14045C070; -inline int32_t* dword_1641E443C; +inline DWORD* dword_1641E443C; inline bool* byte_167208B0C; inline auto sub_1401F9C10 = p_MOD_ProcessPakQueue.RCast<__int64(*)(char* a1, char* a2, __int64 a3)>(); @@ -60,10 +58,8 @@ class VModel_BSP : public IDetour spdlog::debug("| VAR: qword_167ED7BB8 : {:#18x} |\n", reinterpret_cast(qword_167ED7BB8)); spdlog::debug("| VAR: qword_14180A098 : {:#18x} |\n", reinterpret_cast(qword_14180A098)); spdlog::debug("| VAR: byte_16709DDDF : {:#18x} |\n", reinterpret_cast(byte_16709DDDF)); - spdlog::debug("| VAR: g_FileSystem : {:#18x} |\n", reinterpret_cast(g_FileSystem)); spdlog::debug("| VAR: off_141874660 : {:#18x} |\n", reinterpret_cast(off_141874660)); spdlog::debug("| VAR: unk_141874555 : {:#18x} |\n", reinterpret_cast(unk_141874555)); - spdlog::debug("| VAR: unk_167D40B70 : {:#18x} |\n", reinterpret_cast(unk_167D40B70)); spdlog::debug("| VAR: unk_1418749B0 : {:#18x} |\n", reinterpret_cast(unk_1418749B0)); spdlog::debug("| VAR: unk_141874550 : {:#18x} |\n", reinterpret_cast(unk_141874550)); spdlog::debug("| VAR: qword_1671061C8 : {:#18x} |\n", reinterpret_cast(qword_1671061C8)); @@ -105,10 +101,8 @@ class VModel_BSP : public IDetour qword_167ED7BB8 = p_MOD_ProcessPakQueue.Offset(0x10).FindPattern("48 83").ResolveRelativeAddressSelf(0x3, 0x8).RCast(); qword_14180A098 = p_MOD_ProcessPakQueue.Offset(0x20).FindPattern("83 3D").ResolveRelativeAddressSelf(0x2, 0x7).RCast(); byte_16709DDDF = p_MOD_ProcessPakQueue.Offset(0x20).FindPattern("88 1D").ResolveRelativeAddressSelf(0x2, 0x6).RCast(); - g_FileSystem = p_MOD_ProcessPakQueue.Offset(0x20).FindPattern("48 8B").ResolveRelativeAddressSelf(0x3, 0x7).RCast(); off_141874660 = p_MOD_ProcessPakQueue.Offset(0x40).FindPattern("4C 8D 15").ResolveRelativeAddressSelf(0x3, 0x7).RCast(); unk_141874555 = p_MOD_ProcessPakQueue.Offset(0x40).FindPattern("4C 8D 1D").ResolveRelativeAddressSelf(0x3, 0x7).RCast(); - unk_167D40B70 = p_MOD_ProcessPakQueue.Offset(0xA0).FindPattern("4C 8D 35").ResolveRelativeAddressSelf(0x3, 0x7).RCast(); unk_1418749B0 = p_MOD_ProcessPakQueue.Offset(0xA0).FindPattern("48 8D 1D").ResolveRelativeAddressSelf(0x3, 0x7).RCast(); unk_141874550 = p_MOD_ProcessPakQueue.Offset(0x150).FindPattern("48 8D 2D").ResolveRelativeAddressSelf(0x3, 0x7).RCast(); qword_1671061C8 = p_MOD_ProcessPakQueue.Offset(0x200).FindPattern("48 8B 1D").ResolveRelativeAddressSelf(0x3, 0x7).RCast(); @@ -116,7 +110,7 @@ class VModel_BSP : public IDetour qword_167ED7C68 = p_MOD_ProcessPakQueue.Offset(0x200).FindPattern("0F B7 05").ResolveRelativeAddressSelf(0x3, 0x7).RCast(); qword_167ED7BE0 = p_MOD_ProcessPakQueue.Offset(0x250).FindPattern("48 8D 0D").ResolveRelativeAddressSelf(0x3, 0x7).RCast(); qword_14045C070 = p_MOD_ProcessPakQueue.Offset(0x2A0).FindPattern("48 8D 0D").ResolveRelativeAddressSelf(0x3, 0x7).RCast(); - dword_1641E443C = p_MOD_ProcessPakQueue.Offset(0x2A0).FindPattern("3B 05").ResolveRelativeAddressSelf(0x2, 0x6).RCast(); + dword_1641E443C = p_MOD_ProcessPakQueue.Offset(0x2A0).FindPattern("3B 05").ResolveRelativeAddressSelf(0x2, 0x6).RCast(); byte_167208B0C = p_MOD_ProcessPakQueue.Offset(0x2A0).FindPattern("C6 05").ResolveRelativeAddressSelf(0x2, 0x7).RCast(); (*((char**)(&qword_167ED7C68))) -= 6; diff --git a/r5dev/engine/sys_utils.cpp b/r5dev/engine/sys_utils.cpp index 914f8d9b..bfed0bd1 100644 --- a/r5dev/engine/sys_utils.cpp +++ b/r5dev/engine/sys_utils.cpp @@ -372,14 +372,9 @@ void* HSys_LoadAssetHelper(const CHAR* lpFileName, std::int64_t a2, LARGE_INTEGE if (FileExists(mod_file.c_str())) { // Load decompressed pak files from 'mod_dir'. - DevMsg(eDLL_T::RTECH, "Loading pak: '%s'\n", mod_file.c_str()); return Sys_LoadAssetHelper(mod_file.c_str(), a2, a3); } } - if (strstr(lpFileName, base_dir.c_str())) - { - DevMsg(eDLL_T::RTECH, "Loading pak: '%s'\n", lpFileName); - } return Sys_LoadAssetHelper(lpFileName, a2, a3); } diff --git a/r5dev/filesystem/filesystem.h b/r5dev/filesystem/filesystem.h index 7ed2f286..299e4c4b 100644 --- a/r5dev/filesystem/filesystem.h +++ b/r5dev/filesystem/filesystem.h @@ -43,8 +43,8 @@ class VFileSystem_Stdio : public IDetour virtual void GetFun(void) const { } virtual void GetVar(void) const { - g_pFileSystem_Stdio = g_mGameDll.FindPatternSIMD(reinterpret_cast("\x40\x53\x48\x83\xEC\x20\x48\x8B\xD9\xFF\x15\x00\x00\x00\x00\x48\x8D\x05\x00\x00\x00\x00"), - "xxxxxxxxxxx????xxx????").FindPatternSelf("48 8D", CMemory::Direction::DOWN).ResolveRelativeAddressSelf(0x3, 0x7).RCast(); + g_pFileSystem_Stdio = g_mGameDll.FindPatternSIMD(reinterpret_cast("\x48\x8B\x0D\x00\x00\x00\x00\x45\x33\xC0\x48\x83\xC1\x08\x48\x8B\x01"), + "xxx????xxxxxxxxxx").ResolveRelativeAddressSelf(0x3, 0x7).RCast(); } virtual void GetCon(void) const { } virtual void Attach(void) const { } diff --git a/r5dev/rtech/rtech_game.cpp b/r5dev/rtech/rtech_game.cpp index 5fb205cf..dff37905 100644 --- a/r5dev/rtech/rtech_game.cpp +++ b/r5dev/rtech/rtech_game.cpp @@ -47,6 +47,7 @@ RPakHandle_t CPakFile::AsyncLoad(const char* szPakFileName, uintptr_t pMalloc, i if (FileExists(svPakFilePathMod.c_str()) || FileExists(svPakFilePathBase.c_str())) { + DevMsg(eDLL_T::RTECH, "Loading pak file: '%s'\n", szPakFileName); pakHandle = CPakFile_AsyncLoad(szPakFileName, pMalloc, nIdx, bUnk); if (pakHandle == -1) @@ -72,23 +73,25 @@ void CPakFile::Unload(RPakHandle_t handle) if (pakInfo.m_pszFileName) { - DevMsg(eDLL_T::RTECH, "%s - Unloading PakFile '%s'\n", __FUNCTION__, pakInfo.m_pszFileName); + DevMsg(eDLL_T::RTECH, "Unloading pak file: '%s'\n", pakInfo.m_pszFileName); if (strcmp(pakInfo.m_pszFileName, "mp_lobby.rpak") == 0) s_bBasePaksInitialized = false; } - CPakFile_UnloadPak(handle); + CPakFile_Unload(handle); } void RTech_Game_Attach() { DetourAttach((LPVOID*)&CPakFile_AsyncLoad, &CPakFile::AsyncLoad); + DetourAttach((LPVOID*)&CPakFile_Unload, &CPakFile::Unload); } void RTech_Game_Detach() { DetourDetach((LPVOID*)&CPakFile_AsyncLoad, &CPakFile::AsyncLoad); + DetourDetach((LPVOID*)&CPakFile_Unload, &CPakFile::Unload); } // Symbols taken from R2 dll's. diff --git a/r5dev/rtech/rtech_game.h b/r5dev/rtech/rtech_game.h index 6dd17ac2..0cdec3d5 100644 --- a/r5dev/rtech/rtech_game.h +++ b/r5dev/rtech/rtech_game.h @@ -36,7 +36,7 @@ inline CMemory p_CPakFile_AsyncLoad; inline auto CPakFile_AsyncLoad = p_CPakFile_AsyncLoad.RCast(); inline CMemory p_CPakFile_UnloadPak; -inline auto CPakFile_UnloadPak = p_CPakFile_UnloadPak.RCast(); +inline auto CPakFile_Unload = p_CPakFile_UnloadPak.RCast(); class CPakFile { @@ -81,7 +81,7 @@ class VRTechGame : public IDetour CPakFile_AsyncLoad = p_CPakFile_AsyncLoad.RCast(); /*40 53 48 83 EC 40 48 89 6C 24 ? 41 0F B6 E9*/ #endif p_CPakFile_UnloadPak = g_mGameDll.FindPatternSIMD(reinterpret_cast("\x48\x89\x5C\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xEC\x30\x8B\xC1"), "xxxx?xxxx?xxxxxxx"); - CPakFile_UnloadPak = p_CPakFile_UnloadPak.RCast(); /*48 89 5C 24 ? 48 89 74 24 ? 57 48 83 EC 30 8B C1*/ + CPakFile_Unload = p_CPakFile_UnloadPak.RCast(); /*48 89 5C 24 ? 48 89 74 24 ? 57 48 83 EC 30 8B C1*/ } virtual void GetVar(void) const { } virtual void GetCon(void) const { } diff --git a/r5dev/vstdlib/completion.cpp b/r5dev/vstdlib/completion.cpp index 889b1554..8bb01230 100644 --- a/r5dev/vstdlib/completion.cpp +++ b/r5dev/vstdlib/completion.cpp @@ -366,7 +366,7 @@ void _Pak_RequestUnload_f_CompletionFunc(const CCommand& args) { if (args.HasOnlyDigits(1)) { - int nPakId = std::stoi(args.Arg(1)); + RPakHandle_t nPakId = std::stoi(args.Arg(1)); RPakLoadedInfo_t pakInfo = g_pRTech->GetPakLoadedInfo(nPakId); pakInfo.m_pszFileName ? DevMsg(eDLL_T::RTECH, "Requested Pak Unload for '%s'\n", pakInfo.m_pszFileName) : DevMsg(eDLL_T::RTECH, "Requested Pak Unload for '%d'\n", nPakId); g_pakLoadApi->Unload(nPakId);