From 4cf43e0d61e1f7acc84a4f1ff460a8dc6525aaa9 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sun, 12 Jun 2022 12:14:31 +0200 Subject: [PATCH] Fix invalid pointer for 'VPK_Mount_f' g_pFullFileSystem is a pointer to g_pFileSystem_Stdio in the compiled module. Inherit CBaseFileSystem for CFileSystem_Stdio (second VFTable pointer). Additional cleanup. --- r5dev/filesystem/basefilesystem.cpp | 6 +++--- r5dev/filesystem/basefilesystem.h | 27 ++++++++++++++++++++---- r5dev/filesystem/filesystem.h | 32 +++++++---------------------- r5dev/vstdlib/callback.cpp | 8 ++++---- 4 files changed, 37 insertions(+), 36 deletions(-) diff --git a/r5dev/filesystem/basefilesystem.cpp b/r5dev/filesystem/basefilesystem.cpp index dd3063c1..a9403cfe 100644 --- a/r5dev/filesystem/basefilesystem.cpp +++ b/r5dev/filesystem/basefilesystem.cpp @@ -107,7 +107,7 @@ void CBaseFileSystem::Warning(CBaseFileSystem* pFileSystem, FileWarningLevel_t l // *pszFilePath - // Output : Handle to file on success, NULL on failure //--------------------------------------------------------------------------------- -FileHandle_t CBaseFileSystem::ReadFromVPK(CBaseFileSystem* pFileSystem, std::int64_t* pResults, char* pszFilePath) +FileHandle_t CBaseFileSystem::ReadFromVPK(CBaseFileSystem* pFileSystem, FileHandle_t pResults, char* pszFilePath) { std::string svFilePath = ConvertToWinPath(pszFilePath); @@ -122,8 +122,8 @@ FileHandle_t CBaseFileSystem::ReadFromVPK(CBaseFileSystem* pFileSystem, std::int if (::FileExists(svFilePath.c_str()) /*|| ::FileExists(pszFilePath)*/) { - *pResults = -1; - return (void*)pResults; + *reinterpret_cast(pResults) = -1; + return pResults; } return CBaseFileSystem_LoadFromVPK(pFileSystem, pResults, pszFilePath); } diff --git a/r5dev/filesystem/basefilesystem.h b/r5dev/filesystem/basefilesystem.h index 0cec726b..b0479b59 100644 --- a/r5dev/filesystem/basefilesystem.h +++ b/r5dev/filesystem/basefilesystem.h @@ -1,5 +1,24 @@ #pragma once -#include "filesystem/filesystem.h" +typedef void* FileHandle_t; + +enum class SearchPathAdd_t : int +{ + PATH_ADD_TO_HEAD, // First path searched + PATH_ADD_TO_TAIL, // Last path searched + PATH_ADD_TO_TAIL_ATINDEX, // First path searched +}; + +enum class FileWarningLevel_t : int +{ + FILESYSTEM_WARNING = -1, // A problem! + FILESYSTEM_WARNING_QUIET = 0, // Don't print anything + FILESYSTEM_WARNING_REPORTUNCLOSED, // On shutdown, report names of files left unclosed + FILESYSTEM_WARNING_REPORTUSAGE, // Report number of times a file was opened, closed + FILESYSTEM_WARNING_REPORTALLACCESSES, // Report all open/close events to console ( !slow! ) + FILESYSTEM_WARNING_REPORTALLACCESSES_READ, // Report all open/close/read events to the console ( !slower! ) + FILESYSTEM_WARNING_REPORTALLACCESSES_READWRITE, // Report all open/close/read/write events to the console ( !slower! ) + FILESYSTEM_WARNING_REPORTALLACCESSES_ASYNC // Report all open/close/read/write events and all async I/O file events to the console ( !slower(est)! ) +}; class CBaseFileSystem { @@ -9,7 +28,7 @@ public: void Close(FileHandle_t file); bool FileExists(const char* pFileName, const char* pPathID); static void Warning(CBaseFileSystem* pFileSystem, FileWarningLevel_t level, const char* fmt, ...); - static FileHandle_t ReadFromVPK(CBaseFileSystem* pVpk, std::int64_t* pResults, char* pszFilePath); + static FileHandle_t ReadFromVPK(CBaseFileSystem* pVpk, FileHandle_t pResults, char* pszFilePath); static bool ReadFromCache(CBaseFileSystem* pFileSystem, char* pszFilePath, void* pResults); static void AddSearchPath(CBaseFileSystem* pFileSystem, const char* pPath, const char* pPathID, SearchPathAdd_t addType); static bool RemoveSearchPath(CBaseFileSystem* pFileSystem, const char* pPath, const char* pPathID); @@ -20,7 +39,7 @@ inline CMemory p_CBaseFileSystem_Warning; inline auto CBaseFileSystem_Warning = p_CBaseFileSystem_Warning.RCast(); inline CMemory p_CBaseFileSystem_LoadFromVPK; -inline auto CBaseFileSystem_LoadFromVPK = p_CBaseFileSystem_LoadFromVPK.RCast(); +inline auto CBaseFileSystem_LoadFromVPK = p_CBaseFileSystem_LoadFromVPK.RCast(); inline CMemory p_CBaseFileSystem_LoadFromCache; inline auto CBaseFileSystem_LoadFromCache = p_CBaseFileSystem_LoadFromCache.RCast(); @@ -59,7 +78,7 @@ class VBaseFileSystem : public IDetour p_CBaseFileSystem_RemoveSearchPath = g_mGameDll.FindPatternSIMD(reinterpret_cast("\x40\x53\x55\x56\x57\x41\x54\x41\x56\x41\x57\x48\x81\xEC\x00\x00\x00\x00\xC6\x44\x24\x00\x00"), "xxxxxxxxxxxxxx????xxx??"); CBaseFileSystem_Warning = p_CBaseFileSystem_Warning.RCast(); /*4C 89 4C 24 20 C3 ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? 48*/ - CBaseFileSystem_LoadFromVPK = p_CBaseFileSystem_LoadFromVPK.RCast(); /*48 89 5C 24 ?? 57 48 81 EC ?? ?? ?? ?? 49 8B C0 4C 8D 8C 24 ?? ?? ?? ??*/ + CBaseFileSystem_LoadFromVPK = p_CBaseFileSystem_LoadFromVPK.RCast(); /*48 89 5C 24 ?? 57 48 81 EC ?? ?? ?? ?? 49 8B C0 4C 8D 8C 24 ?? ?? ?? ??*/ CBaseFileSystem_LoadFromCache = p_CBaseFileSystem_LoadFromCache.RCast(); /*40 53 48 81 EC ?? ?? ?? ?? 80 3D ?? ?? ?? ?? ?? 49 8B D8*/ CBaseFileSystem_AddSearchPath = p_CBaseFileSystem_AddSearchPath.RCast(); /*44 89 4C 24 ?? 48 89 4C 24 ?? 55 57*/ CBaseFileSystem_RemoveSearchPath = p_CBaseFileSystem_RemoveSearchPath.RCast(); /*40 53 55 56 57 41 54 41 56 41 57 48 81 EC ?? ?? ?? ?? C6 44 24 ?? ??*/ diff --git a/r5dev/filesystem/filesystem.h b/r5dev/filesystem/filesystem.h index 4a22f5b4..62e90c46 100644 --- a/r5dev/filesystem/filesystem.h +++ b/r5dev/filesystem/filesystem.h @@ -1,43 +1,25 @@ #ifndef FILESYSTEM_H #define FILESYSTEM_H #include "vpklib/packedstore.h" +#include "filesystem/basefilesystem.h" #define GAMEINFOPATH_TOKEN "|gameinfo_path|" #define BASESOURCEPATHS_TOKEN "|all_source_engine_paths|" -typedef void* FileHandle_t; - -enum class SearchPathAdd_t : int -{ - PATH_ADD_TO_HEAD, // First path searched - PATH_ADD_TO_TAIL, // Last path searched - PATH_ADD_TO_TAIL_ATINDEX, // First path searched -}; - -enum class FileWarningLevel_t : int -{ - FILESYSTEM_WARNING = -1, // A problem! - FILESYSTEM_WARNING_QUIET = 0, // Don't print anything - FILESYSTEM_WARNING_REPORTUNCLOSED, // On shutdown, report names of files left unclosed - FILESYSTEM_WARNING_REPORTUSAGE, // Report number of times a file was opened, closed - FILESYSTEM_WARNING_REPORTALLACCESSES, // Report all open/close events to console ( !slow! ) - FILESYSTEM_WARNING_REPORTALLACCESSES_READ, // Report all open/close/read events to the console ( !slower! ) - FILESYSTEM_WARNING_REPORTALLACCESSES_READWRITE, // Report all open/close/read/write events to the console ( !slower! ) - FILESYSTEM_WARNING_REPORTALLACCESSES_ASYNC // Report all open/close/read/write events and all async I/O file events to the console ( !slower(est)! ) -}; - class IFileSystem { public: - void AddSearchPath(const char* pPath, const char* pathID, SearchPathAdd_t addType); + void AddSearchPath(const char* pPath, const char* pPathID, SearchPathAdd_t addType); bool RemoveSearchPath(const char* pPath, const char* pPathID); bool ReadFromCache(const char* pPath, void* pResult); - VPKData_t* MountVPK(const char* vpkPath); + VPKData_t* MountVPK(const char* pVpkPath); }; -class CFileSystem_Stdio : public IFileSystem + +class CFileSystem_Stdio : public IFileSystem, public CBaseFileSystem { }; -extern IFileSystem* g_pFullFileSystem; + +extern IFileSystem* g_pFullFileSystem; // Ptr to g_pFileSystem_Stdio. extern CFileSystem_Stdio* g_pFileSystem_Stdio; /////////////////////////////////////////////////////////////////////////////// diff --git a/r5dev/vstdlib/callback.cpp b/r5dev/vstdlib/callback.cpp index 520da0c6..92c8643d 100644 --- a/r5dev/vstdlib/callback.cpp +++ b/r5dev/vstdlib/callback.cpp @@ -687,12 +687,12 @@ void VPK_Mount_f(const CCommand& args) return; } - if (g_pFullFileSystem) + if (g_pFullFileSystem) // Initialized when g_pFileSystem_Stdio is initialized (global engine pointer). { - VPKData_t* pPakData = g_pFullFileSystem->MountVPK(args.Arg(1)); + VPKData_t* pPakData = g_pFileSystem_Stdio->MountVPK(args.Arg(1)); if (pPakData) { - DevMsg(eDLL_T::FS, "Mounted VPK file '%s' with handle '%d'\n", args.Arg(1), pPakData->m_nHandle); + DevMsg(eDLL_T::FS, "Mounted VPK file '%s' with handle '%i'\n", args.Arg(1), pPakData->m_nHandle); } else { @@ -701,7 +701,7 @@ void VPK_Mount_f(const CCommand& args) } else { - Warning(eDLL_T::FS, "Unable to mount VPK file '%s': '%s' is not initalized\n", args.Arg(1), VAR_NAME(g_pFileSystem)); + Warning(eDLL_T::FS, "Unable to mount VPK file '%s': '%s' is not initalized\n", args.Arg(1), VAR_NAME(g_pFullFileSystem)); } }