diff --git a/src/Components/Modules/FileSystem.cpp b/src/Components/Modules/FileSystem.cpp index c42a57d1..332ff343 100644 --- a/src/Components/Modules/FileSystem.cpp +++ b/src/Components/Modules/FileSystem.cpp @@ -3,6 +3,7 @@ namespace Components { std::mutex FileSystem::Mutex; + std::recursive_mutex FileSystem::FSMutex; Utils::Memory::Allocator FileSystem::MemAllocator; void FileSystem::File::read() @@ -253,6 +254,24 @@ namespace Components return !File(execFilename).exists(); } + void FileSystem::FsRestartSync(int a1, int a2) + { + std::lock_guard _(FileSystem::FSMutex); + return Utils::Hook::Call(0x461A50)(a1, a2); // FS_Restart + } + + void FileSystem::DelayLoadImagesSync() + { + std::lock_guard _(FileSystem::FSMutex); + return Utils::Hook::Call(0x494060)(); // DB_LoadDelayedImages + } + + int FileSystem::LoadTextureSync(Game::GfxImageLoadDef **loadDef, Game::GfxImage *image) + { + std::lock_guard _(FileSystem::FSMutex); + return Game::Load_Texture(loadDef, image); + } + FileSystem::FileSystem() { FileSystem::MemAllocator.clear(); @@ -277,6 +296,15 @@ namespace Components // Ignore bad magic, when trying to free hunk when it's already cleared Utils::Hook::Set(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() diff --git a/src/Components/Modules/FileSystem.hpp b/src/Components/Modules/FileSystem.hpp index 3157f794..c2db0827 100644 --- a/src/Components/Modules/FileSystem.hpp +++ b/src/Components/Modules/FileSystem.hpp @@ -96,6 +96,7 @@ namespace Components private: static std::mutex Mutex; + static std::recursive_mutex FSMutex; static Utils::Memory::Allocator MemAllocator; static int ReadFile(const char* path, char** buffer); @@ -107,5 +108,9 @@ namespace Components static void RegisterFolders(); static void StartupStub(); 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); }; }