[FileSystem] Threadsafe filesystem interaction
Replace the ReadFile with a threadsafe variant. Additionally, this includes a check for memory leaks. !
This commit is contained in:
parent
63bd148ac7
commit
72c19cb968
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Components
|
namespace Components
|
||||||
{
|
{
|
||||||
|
Utils::Memory::Allocator FileSystem::MemAllocator;
|
||||||
|
|
||||||
void FileSystem::File::Read()
|
void FileSystem::File::Read()
|
||||||
{
|
{
|
||||||
char* buffer = nullptr;
|
char* buffer = nullptr;
|
||||||
@ -32,7 +34,7 @@ namespace Components
|
|||||||
|
|
||||||
bool FileSystem::FileReader::Exists()
|
bool FileSystem::FileReader::Exists()
|
||||||
{
|
{
|
||||||
return (this->Size > 0);
|
return (this->Size >= 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string FileSystem::FileReader::GetName()
|
std::string FileSystem::FileReader::GetName()
|
||||||
@ -40,6 +42,11 @@ namespace Components
|
|||||||
return this->Name;
|
return this->Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int FileSystem::FileReader::GetSize()
|
||||||
|
{
|
||||||
|
return this->Size;
|
||||||
|
}
|
||||||
|
|
||||||
std::string FileSystem::FileReader::GetBuffer()
|
std::string FileSystem::FileReader::GetBuffer()
|
||||||
{
|
{
|
||||||
Utils::Memory::Allocator allocator;
|
Utils::Memory::Allocator allocator;
|
||||||
@ -152,6 +159,16 @@ namespace Components
|
|||||||
Game::FS_Remove(path);
|
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)
|
void FileSystem::RegisterFolder(const char* folder)
|
||||||
{
|
{
|
||||||
std::string fs_cdpath = Dvar::Var("fs_cdpath").Get<std::string>();
|
std::string fs_cdpath = Dvar::Var("fs_cdpath").Get<std::string>();
|
||||||
@ -197,6 +214,13 @@ namespace Components
|
|||||||
|
|
||||||
FileSystem::FileSystem()
|
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
|
// Filesystem config checks
|
||||||
Utils::Hook(0x6098FD, FileSystem::ExecIsFSStub, HOOK_CALL).Install()->Quick();
|
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
|
// Ignore bad magic, when trying to free hunk when it's already cleared
|
||||||
Utils::Hook::Set<WORD>(0x49AACE, 0xC35E);
|
Utils::Hook::Set<WORD>(0x49AACE, 0xC35E);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FileSystem::~FileSystem()
|
||||||
|
{
|
||||||
|
assert(FileSystem::MemAllocator.Empty());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@ namespace Components
|
|||||||
bool Exists();
|
bool Exists();
|
||||||
std::string GetName();
|
std::string GetName();
|
||||||
std::string GetBuffer();
|
std::string GetBuffer();
|
||||||
|
int GetSize();
|
||||||
bool Read(void* buffer, size_t size);
|
bool Read(void* buffer, size_t size);
|
||||||
void Seek(int offset, int origin);
|
void Seek(int offset, int origin);
|
||||||
|
|
||||||
@ -57,6 +58,7 @@ namespace Components
|
|||||||
};
|
};
|
||||||
|
|
||||||
FileSystem();
|
FileSystem();
|
||||||
|
~FileSystem();
|
||||||
|
|
||||||
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
|
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
|
||||||
const char* GetName() { return "FileSystem"; };
|
const char* GetName() { return "FileSystem"; };
|
||||||
@ -67,6 +69,10 @@ namespace Components
|
|||||||
static void DeleteFile(std::string folder, std::string file);
|
static void DeleteFile(std::string folder, std::string file);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static Utils::Memory::Allocator MemAllocator;
|
||||||
|
|
||||||
|
static void* AllocateFile(int size);
|
||||||
|
static void FreeFile(void* buffer);
|
||||||
|
|
||||||
static void RegisterFolder(const char* folder);
|
static void RegisterFolder(const char* folder);
|
||||||
|
|
||||||
|
@ -80,6 +80,11 @@ namespace Utils
|
|||||||
return static_cast<T*>(this->Allocate(count * sizeof(T)));
|
return static_cast<T*>(this->Allocate(count * sizeof(T)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Empty()
|
||||||
|
{
|
||||||
|
return (this->Pool.empty() && this->RefMemory.empty());
|
||||||
|
}
|
||||||
|
|
||||||
char* DuplicateString(std::string string)
|
char* DuplicateString(std::string string)
|
||||||
{
|
{
|
||||||
char* data = Memory::DuplicateString(string);
|
char* data = Memory::DuplicateString(string);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user