diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index ae2bc9c6..34f8e018 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -2,8 +2,7 @@ namespace Components { - std::recursive_mutex AssetHandler::BypassMutex; - std::vector AssetHandler::BypassThreads; + thread_local bool AssetHandler::BypassState; std::map AssetHandler::AssetInterfaces; std::map> AssetHandler::TypeCallbacks; wink::signal> AssetHandler::RestrictSignal; @@ -56,7 +55,7 @@ namespace Components if (filename) { // Allow call DB_FindXAssetHeader within the hook - AssetHandler::SetThreadBypass(); + AssetHandler::BypassState = true; if (AssetHandler::TypeCallbacks.find(type) != AssetHandler::TypeCallbacks.end()) { @@ -64,7 +63,7 @@ namespace Components } // Disallow calling DB_FindXAssetHeader ;) - AssetHandler::ClearThreadBypass(); + AssetHandler::BypassState = false; } return header; @@ -72,33 +71,7 @@ namespace Components int AssetHandler::HasThreadBypass() { - std::lock_guard _(AssetHandler::BypassMutex); - return (std::find(AssetHandler::BypassThreads.begin(), AssetHandler::BypassThreads.end(), std::this_thread::get_id()) != AssetHandler::BypassThreads.end()) & 1; - } - - void AssetHandler::SetThreadBypass() - { - std::lock_guard _(AssetHandler::BypassMutex); - - if (!AssetHandler::HasThreadBypass()) - { - AssetHandler::BypassThreads.push_back(std::this_thread::get_id()); - } - } - - void AssetHandler::ClearThreadBypass() - { - std::lock_guard _(AssetHandler::BypassMutex); - - while (AssetHandler::HasThreadBypass()) - { - auto i = std::find(AssetHandler::BypassThreads.begin(), AssetHandler::BypassThreads.end(), std::this_thread::get_id()); - - if (i != AssetHandler::BypassThreads.end()) - { - AssetHandler::BypassThreads.erase(i); - } - } + return AssetHandler::BypassState & 1; } __declspec(naked) void AssetHandler::FindAssetStub() @@ -339,13 +312,11 @@ namespace Components Game::XAssetHeader AssetHandler::FindOriginalAsset(Game::XAssetType type, const char* filename) { - std::lock_guard _(AssetHandler::BypassMutex); - int originalState = AssetHandler::HasThreadBypass(); - AssetHandler::SetThreadBypass(); + AssetHandler::BypassState = true; Game::XAssetHeader header = Game::DB_FindXAssetHeader(type, filename); - if (!originalState) AssetHandler::ClearThreadBypass(); + if (!originalState) AssetHandler::BypassState = false; return header; } diff --git a/src/Components/Modules/AssetHandler.hpp b/src/Components/Modules/AssetHandler.hpp index 623d359d..63427db4 100644 --- a/src/Components/Modules/AssetHandler.hpp +++ b/src/Components/Modules/AssetHandler.hpp @@ -40,8 +40,7 @@ namespace Components static void StoreTemporaryAsset(Game::XAssetType type, Game::XAssetHeader asset); private: - static std::recursive_mutex BypassMutex; - static std::vector BypassThreads; + static thread_local bool BypassState; static std::map TemporaryAssets[Game::XAssetType::ASSET_TYPE_COUNT]; @@ -68,8 +67,6 @@ namespace Components static void ModifyAsset(Game::XAssetType type, Game::XAssetHeader asset, std::string name); static int HasThreadBypass(); - static void SetThreadBypass(); - static void ClearThreadBypass(); }; }