[FileSystem] Threadsafe filesystem interaction

Replace the ReadFile with a threadsafe variant.
Additionally, this includes a check for memory leaks.
!
This commit is contained in:
momo5502 2016-11-12 12:51:34 +01:00
parent 63bd148ac7
commit 72c19cb968
3 changed files with 41 additions and 1 deletions

View File

@ -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<std::string>();
@ -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<WORD>(0x49AACE, 0xC35E);
}
FileSystem::~FileSystem()
{
assert(FileSystem::MemAllocator.Empty());
}
}

View File

@ -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);

View File

@ -80,6 +80,11 @@ namespace Utils
return static_cast<T*>(this->Allocate(count * sizeof(T)));
}
bool Empty()
{
return (this->Pool.empty() && this->RefMemory.empty());
}
char* DuplicateString(std::string string)
{
char* data = Memory::DuplicateString(string);