[FileSystem] Synchronize filesystem access throughout the application

This commit is contained in:
momo5502 2017-01-11 19:16:19 +01:00
parent 3cf8c30864
commit 0fa33e609f
2 changed files with 33 additions and 0 deletions

View File

@ -3,6 +3,7 @@
namespace Components namespace Components
{ {
std::mutex FileSystem::Mutex; std::mutex FileSystem::Mutex;
std::recursive_mutex FileSystem::FSMutex;
Utils::Memory::Allocator FileSystem::MemAllocator; Utils::Memory::Allocator FileSystem::MemAllocator;
void FileSystem::File::read() void FileSystem::File::read()
@ -253,6 +254,24 @@ namespace Components
return !File(execFilename).exists(); return !File(execFilename).exists();
} }
void FileSystem::FsRestartSync(int a1, int a2)
{
std::lock_guard<std::recursive_mutex> _(FileSystem::FSMutex);
return Utils::Hook::Call<void(int, int)>(0x461A50)(a1, a2); // FS_Restart
}
void FileSystem::DelayLoadImagesSync()
{
std::lock_guard<std::recursive_mutex> _(FileSystem::FSMutex);
return Utils::Hook::Call<void()>(0x494060)(); // DB_LoadDelayedImages
}
int FileSystem::LoadTextureSync(Game::GfxImageLoadDef **loadDef, Game::GfxImage *image)
{
std::lock_guard<std::recursive_mutex> _(FileSystem::FSMutex);
return Game::Load_Texture(loadDef, image);
}
FileSystem::FileSystem() FileSystem::FileSystem()
{ {
FileSystem::MemAllocator.clear(); FileSystem::MemAllocator.clear();
@ -277,6 +296,15 @@ 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);
// Synchronize filesystem restarts
Utils::Hook(0x4A745B, FileSystem::FsRestartSync, HOOK_CALL).install()->quick(); // SV_SpawnServer
Utils::Hook(0x4C8609, FileSystem::FsRestartSync, HOOK_CALL).install()->quick(); // FS_ConditionalRestart
Utils::Hook(0x5AC68E, FileSystem::FsRestartSync, HOOK_CALL).install()->quick();
// Synchronize db image loading
Utils::Hook(0x415AB8, FileSystem::DelayLoadImagesSync, HOOK_CALL).install()->quick();
Utils::Hook(0x4D32BC, FileSystem::LoadTextureSync, HOOK_CALL).install()->quick();
} }
FileSystem::~FileSystem() FileSystem::~FileSystem()

View File

@ -96,6 +96,7 @@ namespace Components
private: private:
static std::mutex Mutex; static std::mutex Mutex;
static std::recursive_mutex FSMutex;
static Utils::Memory::Allocator MemAllocator; static Utils::Memory::Allocator MemAllocator;
static int ReadFile(const char* path, char** buffer); static int ReadFile(const char* path, char** buffer);
@ -107,5 +108,9 @@ namespace Components
static void RegisterFolders(); static void RegisterFolders();
static void StartupStub(); static void StartupStub();
static int ExecIsFSStub(const char* execFilename); static int ExecIsFSStub(const char* execFilename);
static void FsRestartSync(int a1, int a2);
static void DelayLoadImagesSync();
static int LoadTextureSync(Game::GfxImageLoadDef **loadDef, Game::GfxImage *image);
}; };
} }