diff --git a/src/Components/Modules/FileSystem.cpp b/src/Components/Modules/FileSystem.cpp index 8331e824..d6a813e7 100644 --- a/src/Components/Modules/FileSystem.cpp +++ b/src/Components/Modules/FileSystem.cpp @@ -2,6 +2,8 @@ namespace Components { + Utils::Memory::Allocator FileSystem::MemAllocator; + void FileSystem::File::Read() { char* buffer = nullptr; @@ -32,7 +34,7 @@ namespace Components bool FileSystem::FileReader::Exists() { - return (this->Size > 0); + return (this->Size >= 0); } std::string FileSystem::FileReader::GetName() @@ -40,6 +42,11 @@ namespace Components return this->Name; } + int FileSystem::FileReader::GetSize() + { + return this->Size; + } + std::string FileSystem::FileReader::GetBuffer() { Utils::Memory::Allocator allocator; @@ -152,6 +159,16 @@ namespace Components Game::FS_Remove(path); } + void* FileSystem::AllocateFile(int size) + { + return FileSystem::MemAllocator.Allocate(size); + } + + void FileSystem::FreeFile(void* buffer) + { + FileSystem::MemAllocator.Free(buffer); + } + void FileSystem::RegisterFolder(const char* folder) { std::string fs_cdpath = Dvar::Var("fs_cdpath").Get(); @@ -197,6 +214,13 @@ namespace Components FileSystem::FileSystem() { + FileSystem::MemAllocator.Clear(); + + // Thread safe file system interaction + Utils::Hook(0x4F4BFF, FileSystem::AllocateFile, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x4F4BC0, Game::FS_FOpenFileReadCurrentThread, HOOK_CALL).Install()->Quick(); + Utils::Hook(Game::FS_FreeFile, FileSystem::FreeFile, HOOK_JUMP).Install()->Quick(); + // Filesystem config checks Utils::Hook(0x6098FD, FileSystem::ExecIsFSStub, HOOK_CALL).Install()->Quick(); @@ -213,4 +237,9 @@ namespace Components // Ignore bad magic, when trying to free hunk when it's already cleared Utils::Hook::Set(0x49AACE, 0xC35E); } + + FileSystem::~FileSystem() + { + assert(FileSystem::MemAllocator.Empty()); + } } diff --git a/src/Components/Modules/FileSystem.hpp b/src/Components/Modules/FileSystem.hpp index 37b43f44..6120869a 100644 --- a/src/Components/Modules/FileSystem.hpp +++ b/src/Components/Modules/FileSystem.hpp @@ -31,6 +31,7 @@ namespace Components bool Exists(); std::string GetName(); std::string GetBuffer(); + int GetSize(); bool Read(void* buffer, size_t size); void Seek(int offset, int origin); @@ -57,6 +58,7 @@ namespace Components }; FileSystem(); + ~FileSystem(); #if defined(DEBUG) || defined(FORCE_UNIT_TESTS) const char* GetName() { return "FileSystem"; }; @@ -67,6 +69,10 @@ namespace Components static void DeleteFile(std::string folder, std::string file); private: + static Utils::Memory::Allocator MemAllocator; + + static void* AllocateFile(int size); + static void FreeFile(void* buffer); static void RegisterFolder(const char* folder); diff --git a/src/Utils/Memory.hpp b/src/Utils/Memory.hpp index d6b98bb6..87b18f51 100644 --- a/src/Utils/Memory.hpp +++ b/src/Utils/Memory.hpp @@ -80,6 +80,11 @@ namespace Utils return static_cast(this->Allocate(count * sizeof(T))); } + bool Empty() + { + return (this->Pool.empty() && this->RefMemory.empty()); + } + char* DuplicateString(std::string string) { char* data = Memory::DuplicateString(string);