From 7c0c5414053e364ed8e1faf3e0cf37c092918147 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sun, 12 Jun 2022 12:40:26 +0200 Subject: [PATCH] FileExist cleanup Use fs::path constructor for all strings. Renamed CPakFile::AsyncLoad to CPakFile::LoadAsync. Renamed CPakFile::Load to CPakFile::LoadPak. --- r5dev/engine/cmodel_bsp.cpp | 14 +++++++------- r5dev/engine/sys_utils.cpp | 2 +- r5dev/filesystem/basefilesystem.cpp | 4 ++-- r5dev/materialsystem/cmaterialsystem.cpp | 2 +- r5dev/public/include/utility.h | 2 +- r5dev/public/utility.cpp | 4 ++-- r5dev/rtech/rtech_game.cpp | 18 +++++++++--------- r5dev/rtech/rtech_game.h | 24 ++++++++++++------------ r5dev/vstdlib/callback.cpp | 12 ++++++------ 9 files changed, 41 insertions(+), 41 deletions(-) diff --git a/r5dev/engine/cmodel_bsp.cpp b/r5dev/engine/cmodel_bsp.cpp index 8970e785..f35c2a96 100644 --- a/r5dev/engine/cmodel_bsp.cpp +++ b/r5dev/engine/cmodel_bsp.cpp @@ -211,7 +211,7 @@ void MOD_ProcessPakQueue() } if (!v14 || v13 == 9) { - g_pakLoadApi->Unload(*(_DWORD*)v10); + g_pakLoadApi->UnloadPak(*(_DWORD*)v10); MOD_UnloadPakFile(); } if (v13 && (unsigned int)(v13 - 13) > 1) @@ -292,17 +292,17 @@ void MOD_ProcessPakQueue() s_bLevelResourceInitialized = true; MOD_PreloadPakFile(g_svLevelName); } - *(_DWORD*)v15 = g_pakLoadApi->AsyncLoad(v17, g_pMallocPool.GetPtr(), 4, 0); + *(_DWORD*)v15 = g_pakLoadApi->LoadAsync(v17, g_pMallocPool.GetPtr(), 4, 0); if (strcmp(v17, "common_mp.rpak") == 0 || strcmp(v17, "common_sp.rpak") == 0 || strcmp(v17, "common_pve.rpak") == 0) { - RPakHandle_t pakHandle = g_pakLoadApi->AsyncLoad("common_sdk.rpak", g_pMallocPool.GetPtr(), 4, 0); + RPakHandle_t pakHandle = g_pakLoadApi->LoadAsync("common_sdk.rpak", g_pMallocPool.GetPtr(), 4, 0); if (pakHandle != -1) g_vLoadedPakHandle.push_back(pakHandle); } if (strcmp(v17, "ui_mp.rpak") == 0) { - RPakHandle_t pakHandle = g_pakLoadApi->AsyncLoad("ui_sdk.rpak", g_pMallocPool.GetPtr(), 4, 0); + RPakHandle_t pakHandle = g_pakLoadApi->LoadAsync("ui_sdk.rpak", g_pMallocPool.GetPtr(), 4, 0); if (pakHandle != -1) g_vLoadedPakHandle.push_back(pakHandle); } @@ -349,7 +349,7 @@ void MOD_PreloadPakFile(const string& svLevelName) ostream << "platform\\scripts\\levels\\settings\\" << svLevelName << ".json"; fs::path fsPath = fs::current_path() /= ostream.str(); - if (FileExists(fsPath.string().c_str())) + if (FileExists(fsPath)) { nlohmann::json jsIn; try @@ -368,7 +368,7 @@ void MOD_PreloadPakFile(const string& svLevelName) if (it.is_string()) { string svToLoad = it.get() + ".rpak"; - RPakHandle_t nPakId = g_pakLoadApi->AsyncLoad(svToLoad.c_str(), g_pMallocPool.GetPtr(), 4, 0); + RPakHandle_t nPakId = g_pakLoadApi->LoadAsync(svToLoad.c_str(), g_pMallocPool.GetPtr(), 4, 0); if (nPakId == -1) Error(eDLL_T::ENGINE, "%s: unable to load pak '%s' results '%d'\n", __FUNCTION__, svToLoad.c_str(), nPakId); @@ -396,7 +396,7 @@ void MOD_UnloadPakFile(void) { if (it >= 0) { - g_pakLoadApi->Unload(it); + g_pakLoadApi->UnloadPak(it); } } g_vLoadedPakHandle.clear(); diff --git a/r5dev/engine/sys_utils.cpp b/r5dev/engine/sys_utils.cpp index eb63baa7..77d73414 100644 --- a/r5dev/engine/sys_utils.cpp +++ b/r5dev/engine/sys_utils.cpp @@ -107,7 +107,7 @@ void* HSys_LoadAssetHelper(const CHAR* lpFileName, std::int64_t a2, LARGE_INTEGE base_file.erase(0, 11); // Erase 'base_dir'. mod_file = mod_dir + base_file; // Prepend 'mod_dir'. - if (FileExists(mod_file.c_str())) + if (FileExists(mod_file)) { // Load decompressed pak files from 'mod_dir'. return Sys_LoadAssetHelper(mod_file.c_str(), a2, a3); diff --git a/r5dev/filesystem/basefilesystem.cpp b/r5dev/filesystem/basefilesystem.cpp index a9403cfe..7ee0c907 100644 --- a/r5dev/filesystem/basefilesystem.cpp +++ b/r5dev/filesystem/basefilesystem.cpp @@ -120,7 +120,7 @@ FileHandle_t CBaseFileSystem::ReadFromVPK(CBaseFileSystem* pFileSystem, FileHand // TODO: obtain 'mod' SearchPath's instead. svFilePath.insert(0, "platform\\"); - if (::FileExists(svFilePath.c_str()) /*|| ::FileExists(pszFilePath)*/) + if (::FileExists(svFilePath) /*|| ::FileExists(pszFilePath)*/) { *reinterpret_cast(pResults) = -1; return pResults; @@ -148,7 +148,7 @@ bool CBaseFileSystem::ReadFromCache(CBaseFileSystem* pFileSystem, char* pszFileP // TODO: obtain 'mod' SearchPath's instead. svFilePath.insert(0, "platform\\"); - if (::FileExists(svFilePath.c_str()) /*|| ::FileExists(pszFilePath)*/) + if (::FileExists(svFilePath) /*|| ::FileExists(pszFilePath)*/) { return false; } diff --git a/r5dev/materialsystem/cmaterialsystem.cpp b/r5dev/materialsystem/cmaterialsystem.cpp index 32bd50b0..b62bad18 100644 --- a/r5dev/materialsystem/cmaterialsystem.cpp +++ b/r5dev/materialsystem/cmaterialsystem.cpp @@ -18,7 +18,7 @@ void StreamDB_Init(const char* pszStreamDBFile) ostream << "platform\\scripts\\levels\\settings\\" << pszStreamDBFile << ".json"; fs::path fsPath = fs::current_path() /= ostream.str(); - if (FileExists(fsPath.string().c_str())) + if (FileExists(fsPath)) { nlohmann::json jsIn; try diff --git a/r5dev/public/include/utility.h b/r5dev/public/include/utility.h index ae06ab7c..6ba952db 100644 --- a/r5dev/public/include/utility.h +++ b/r5dev/public/include/utility.h @@ -3,7 +3,7 @@ ///////////////////////////////////////////////////////////////////////////// // Internals BOOL IsBadReadPtrV2(void* ptr); -BOOL FileExists(const char* szPath); +BOOL FileExists(const fs::path& svFilePath); MODULEINFO GetModuleInfo(const char* szModule); DWORD64 FindPatternSIMD(const char* szModule, const unsigned char* szPattern, const char* szMask); diff --git a/r5dev/public/utility.cpp b/r5dev/public/utility.cpp index e68d80d3..955fa100 100644 --- a/r5dev/public/utility.cpp +++ b/r5dev/public/utility.cpp @@ -8,9 +8,9 @@ /////////////////////////////////////////////////////////////////////////////// // For checking if a specific file exists. -BOOL FileExists(const char* szPath) +BOOL FileExists(const fs::path& svFilePath) { - return fs::exists(szPath); + return fs::exists(svFilePath); } /////////////////////////////////////////////////////////////////////////////// diff --git a/r5dev/rtech/rtech_game.cpp b/r5dev/rtech/rtech_game.cpp index 745f4c20..029f89a3 100644 --- a/r5dev/rtech/rtech_game.cpp +++ b/r5dev/rtech/rtech_game.cpp @@ -20,7 +20,7 @@ vector g_vLoadedPakHandle; // bUnk - // Output : pak file handle on success, -1 (INVALID_PAK_HANDLE) on failure //----------------------------------------------------------------------------- -RPakHandle_t CPakFile::AsyncLoad(const char* szPakFileName, uintptr_t pMalloc, int nIdx, bool bUnk) +RPakHandle_t CPakFile::LoadAsync(const char* szPakFileName, uintptr_t pMalloc, int nIdx, bool bUnk) { RPakHandle_t pakHandle = -1; #ifdef DEDICATED @@ -44,10 +44,10 @@ RPakHandle_t CPakFile::AsyncLoad(const char* szPakFileName, uintptr_t pMalloc, i string svPakFilePathMod = "paks\\Win32\\" + string(szPakFileName); string svPakFilePathBase = "paks\\Win64\\" + string(szPakFileName); - if (FileExists(svPakFilePathMod.c_str()) || FileExists(svPakFilePathBase.c_str())) + if (FileExists(svPakFilePathMod) || FileExists(svPakFilePathBase)) { DevMsg(eDLL_T::RTECH, "Loading pak file: '%s'\n", szPakFileName); - pakHandle = CPakFile_AsyncLoad(szPakFileName, pMalloc, nIdx, bUnk); + pakHandle = CPakFile_LoadAsync(szPakFileName, pMalloc, nIdx, bUnk); if (pakHandle == -1) { @@ -66,7 +66,7 @@ RPakHandle_t CPakFile::AsyncLoad(const char* szPakFileName, uintptr_t pMalloc, i // Purpose: unloads loaded pak files // Input : handle - //----------------------------------------------------------------------------- -void CPakFile::Unload(RPakHandle_t handle) +void CPakFile::UnloadPak(RPakHandle_t handle) { RPakLoadedInfo_t* pakInfo = g_pRTech->GetPakLoadedInfo(handle); @@ -81,19 +81,19 @@ void CPakFile::Unload(RPakHandle_t handle) } } - CPakFile_Unload(handle); + CPakFile_UnloadPak(handle); } void RTech_Game_Attach() { - DetourAttach((LPVOID*)&CPakFile_AsyncLoad, &CPakFile::AsyncLoad); - DetourAttach((LPVOID*)&CPakFile_Unload, &CPakFile::Unload); + DetourAttach((LPVOID*)&CPakFile_LoadAsync, &CPakFile::LoadAsync); + DetourAttach((LPVOID*)&CPakFile_UnloadPak, &CPakFile::UnloadPak); } void RTech_Game_Detach() { - DetourDetach((LPVOID*)&CPakFile_AsyncLoad, &CPakFile::AsyncLoad); - DetourDetach((LPVOID*)&CPakFile_Unload, &CPakFile::Unload); + DetourDetach((LPVOID*)&CPakFile_LoadAsync, &CPakFile::LoadAsync); + DetourDetach((LPVOID*)&CPakFile_UnloadPak, &CPakFile::UnloadPak); } // Symbols taken from R2 dll's. diff --git a/r5dev/rtech/rtech_game.h b/r5dev/rtech/rtech_game.h index e6aacbae..def90028 100644 --- a/r5dev/rtech/rtech_game.h +++ b/r5dev/rtech/rtech_game.h @@ -24,20 +24,20 @@ enum class ePakStatus : int }; /* ==== RTECH_GAME ====================================================================================================================================================== */ +inline CMemory p_CPakFile_LoadAsync; +inline auto CPakFile_LoadAsync = p_CPakFile_LoadAsync.RCast(); + inline CMemory p_CPakFile_LoadPak; inline auto CPakFile_LoadPak = p_CPakFile_LoadPak.RCast(); -inline CMemory p_CPakFile_AsyncLoad; -inline auto CPakFile_AsyncLoad = p_CPakFile_AsyncLoad.RCast(); - inline CMemory p_CPakFile_UnloadPak; -inline auto CPakFile_Unload = p_CPakFile_UnloadPak.RCast(); +inline auto CPakFile_UnloadPak = p_CPakFile_UnloadPak.RCast(); class CPakFile { public: - static RPakHandle_t AsyncLoad(const char* szPakFileName, uintptr_t pMalloc = g_pMallocPool.GetPtr(), int nIdx = NULL, bool bUnk = false); - static void Unload(RPakHandle_t handle); + static RPakHandle_t LoadAsync(const char* szPakFileName, uintptr_t pMalloc = g_pMallocPool.GetPtr(), int nIdx = NULL, bool bUnk = false); + static void UnloadPak(RPakHandle_t handle); }; extern CPakFile* g_pakLoadApi; @@ -50,7 +50,7 @@ class VRTechGame : public IDetour { virtual void GetAdr(void) const { - spdlog::debug("| FUN: CPakFile::AsyncLoad : {:#18x} |\n", p_CPakFile_AsyncLoad.GetPtr()); + spdlog::debug("| FUN: CPakFile::LoadAsync : {:#18x} |\n", p_CPakFile_LoadAsync.GetPtr()); spdlog::debug("| FUN: CPakFile::LoadPak : {:#18x} |\n", p_CPakFile_LoadPak.GetPtr()); spdlog::debug("| FUN: CPakFile::UnloadPak : {:#18x} |\n", p_CPakFile_UnloadPak.GetPtr()); spdlog::debug("+----------------------------------------------------------------+\n"); @@ -65,14 +65,14 @@ class VRTechGame : public IDetour CPakFile_LoadPak = p_CPakFile_LoadPak.RCast(); #endif #if defined (GAMEDLL_S0) || defined (GAMEDLL_S1) || defined (GAMEDLL_S2) - p_CPakFile_AsyncLoad = g_mGameDll.FindPatternSIMD(reinterpret_cast("\x40\x53\x48\x83\xEC\x40\x48\x89\x6C\x24\x00\x41\x8B\xE8"), "xxxxxxxxxx?xxx"); - CPakFile_AsyncLoad = p_CPakFile_AsyncLoad.RCast(); /*40 53 48 83 EC 40 48 89 6C 24 ? 41 8B E8*/ + p_CPakFile_LoadAsync = g_mGameDll.FindPatternSIMD(reinterpret_cast("\x40\x53\x48\x83\xEC\x40\x48\x89\x6C\x24\x00\x41\x8B\xE8"), "xxxxxxxxxx?xxx"); + CPakFile_LoadAsync = p_CPakFile_LoadAsync.RCast(); /*40 53 48 83 EC 40 48 89 6C 24 ? 41 8B E8*/ #elif defined (GAMEDLL_S3) - p_CPakFile_AsyncLoad = g_mGameDll.FindPatternSIMD(reinterpret_cast("\x40\x53\x48\x83\xEC\x40\x48\x89\x6C\x24\x00\x41\x0F\xB6\xE9"), "xxxxxxxxxx?xxxx"); - CPakFile_AsyncLoad = p_CPakFile_AsyncLoad.RCast(); /*40 53 48 83 EC 40 48 89 6C 24 ? 41 0F B6 E9*/ + p_CPakFile_LoadAsync = g_mGameDll.FindPatternSIMD(reinterpret_cast("\x40\x53\x48\x83\xEC\x40\x48\x89\x6C\x24\x00\x41\x0F\xB6\xE9"), "xxxxxxxxxx?xxxx"); + CPakFile_LoadAsync = p_CPakFile_LoadAsync.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_Unload = p_CPakFile_UnloadPak.RCast(); /*48 89 5C 24 ? 48 89 74 24 ? 57 48 83 EC 30 8B C1*/ + CPakFile_UnloadPak = 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/callback.cpp b/r5dev/vstdlib/callback.cpp index 92c8643d..76859c56 100644 --- a/r5dev/vstdlib/callback.cpp +++ b/r5dev/vstdlib/callback.cpp @@ -383,7 +383,7 @@ void Pak_RequestUnload_f(const CCommand& args) string pakName = pakInfo->m_pszFileName; !pakName.empty() ? DevMsg(eDLL_T::RTECH, "Requested pak unload for '%s'\n", pakName.c_str()) : DevMsg(eDLL_T::RTECH, "Requested Pak Unload for '%d'\n", nPakId); - g_pakLoadApi->Unload(nPakId); + g_pakLoadApi->UnloadPak(nPakId); } else { @@ -394,7 +394,7 @@ void Pak_RequestUnload_f(const CCommand& args) } DevMsg(eDLL_T::RTECH, "Requested pak unload for '%s'\n", args.Arg(1)); - g_pakLoadApi->Unload(pakInfo->m_nPakId); + g_pakLoadApi->UnloadPak(pakInfo->m_nPakId); } } catch (std::exception& e) @@ -411,7 +411,7 @@ Pak_RequestLoad_f */ void Pak_RequestLoad_f(const CCommand& args) { - g_pakLoadApi->AsyncLoad(args.Arg(1)); + g_pakLoadApi->LoadAsync(args.Arg(1)); } @@ -453,12 +453,12 @@ void Pak_Swap_f(const CCommand& args) !pakName.empty() ? DevMsg(eDLL_T::RTECH, "Requested pak swap for '%s'\n", pakName.c_str()) : DevMsg(eDLL_T::RTECH, "Requested pak swap for '%d'\n", nPakId); - g_pakLoadApi->Unload(nPakId); + g_pakLoadApi->UnloadPak(nPakId); while (pakInfo->m_nStatus != RPakStatus_t::PAK_STATUS_FREED) // Wait till this slot gets free'd. std::this_thread::sleep_for(std::chrono::seconds(1)); - g_pakLoadApi->AsyncLoad(pakName.c_str()); + g_pakLoadApi->LoadAsync(pakName.c_str()); } catch (std::exception& e) { @@ -512,7 +512,7 @@ void RTech_Decompress_f(const CCommand& args) DevMsg(eDLL_T::RTECH, "______________________________________________________________\n"); DevMsg(eDLL_T::RTECH, "-+ RTech decompress ------------------------------------------\n"); - if (!FileExists(pakNameIn.c_str())) + if (!FileExists(pakNameIn)) { Error(eDLL_T::RTECH, "Error: pak file '%s' does not exist!\n", pakNameIn.c_str()); return;