diff --git a/include/Engine/Core/ResourceManager.h b/include/Engine/Core/ResourceManager.h index cb78ce13..15a551e7 100644 --- a/include/Engine/Core/ResourceManager.h +++ b/include/Engine/Core/ResourceManager.h @@ -65,6 +65,7 @@ private: ResourceManager(); public: + static bool UseThreading; /*static ResourceManager& Instance() { static ResourceManager s; @@ -94,7 +95,7 @@ public: @param resourceName Fully qualified name of the resource to load. */ template - static T* Load(std::string resourceName, Resource* parent = nullptr); + static T* Load(const std::string& resourceName, Resource* parent = nullptr); /** Reloads an already loaded resource, keeping its resource ID intact. @@ -141,9 +142,9 @@ private: static unsigned int GetNewResourceID(unsigned int typeID); // Internal: Create a resource and cache it - static Resource* createResourceThrowing(std::string resourceType, std::string resourceName, Resource* parent); - static Resource* createResource(std::string resourceType, std::string resourceName, Resource* parent, std::exception_ptr& exception); - static Resource* cacheResource(Resource* resource, std::string resourceType, std::string resourceName, Resource* parent); + static Resource* createResourceThrowing(const std::string& resourceType, const std::string& resourceName, Resource* parent); + static Resource* createResource(const std::string& resourceType, const std::string& resourceName, Resource* parent, std::exception_ptr& exception); + static Resource* cacheResource(Resource* resource, const std::string& resourceType, const std::string& resourceName, Resource* parent); static bool IsMainThread(); }; @@ -156,7 +157,7 @@ void ResourceManager::RegisterType(std::string typeName) } template -static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = nullptr */) +static T* ResourceManager::Load(const std::string& resourceName, Resource* parent /* = nullptr */) { auto resourceTypename = typeid(T).name(); auto iter = m_CompilerTypenameToResourceType.find(resourceTypename); @@ -176,7 +177,7 @@ static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = decltype(m_ResourceCache)::iterator it; //If a thread has already been launched to load this resource. auto tIt = m_LoadingThreads.find(cacheKey); - if (tIt != m_LoadingThreads.end()) { + if (UseThreading && tIt != m_LoadingThreads.end()) { if (async) { //Throw StillLoadingException if the thread is still working. if (!tIt->second.try_join_for(boost::chrono::nanoseconds(1))) { @@ -209,9 +210,8 @@ static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = } } - Resource* res = nullptr; //If resource is not cached.. - if (async) { + if (UseThreading && async) { if (mustNotLoadInThread) { try { return static_cast(createResourceThrowing(resourceType, resourceName, parent)); diff --git a/resources/DefaultConfig.ini b/resources/DefaultConfig.ini index 3ab402ad..d84b15bb 100644 --- a/resources/DefaultConfig.ini +++ b/resources/DefaultConfig.ini @@ -16,4 +16,7 @@ StartNetwork=false IsServer=false Name=Bob Address=127.0.0.1 -Port=13 \ No newline at end of file +Port=13 + +[Multithreading] +ResourceLoading=true diff --git a/src/Engine/Core/ResourceManager.cpp b/src/Engine/Core/ResourceManager.cpp index ed79e4f7..54228efa 100644 --- a/src/Engine/Core/ResourceManager.cpp +++ b/src/Engine/Core/ResourceManager.cpp @@ -9,6 +9,7 @@ std::unordered_map, Resource*> ResourceManag std::unordered_map ResourceManager::m_ResourceFromName; std::unordered_map ResourceManager::m_ResourceParents; unsigned int ResourceManager::m_CurrentResourceTypeID = 0; +bool ResourceManager::UseThreading = false; std::unordered_map ResourceManager::m_ResourceTypeIDs; std::unordered_map ResourceManager::m_ResourceCount; FileWatcher ResourceManager::m_FileWatcher; @@ -79,7 +80,7 @@ void ResourceManager::Update() m_FileWatcher.Check(); } -Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent, std::exception_ptr& exception) +Resource* ResourceManager::createResource(const std::string& resourceType, const std::string& resourceName, Resource* parent, std::exception_ptr& exception) { auto facIt = m_FactoryFunctions.find(resourceType); if (facIt == m_FactoryFunctions.end()) { @@ -102,7 +103,7 @@ Resource* ResourceManager::createResource(std::string resourceType, std::string } -Resource* ResourceManager::createResourceThrowing(std::string resourceType, std::string resourceName, Resource* parent) +Resource* ResourceManager::createResourceThrowing(const std::string& resourceType, const std::string& resourceName, Resource* parent) { std::exception_ptr exception; Resource* res = createResource(resourceType, resourceName, parent, exception); @@ -112,7 +113,7 @@ Resource* ResourceManager::createResourceThrowing(std::string resourceType, std: return res; } -Resource* ResourceManager::cacheResource(Resource* resource, std::string resourceType, std::string resourceName, Resource* parent) +Resource* ResourceManager::cacheResource(Resource* resource, const std::string& resourceType, const std::string& resourceName, Resource* parent) { //Lock the mutex immediately, and unlock it when leaving the code block. boost::lock_guard guard(m_Mutex); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index efa773f5..e6589676 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -14,6 +14,7 @@ Game::Game(int argc, char* argv[]) ResourceManager::RegisterType("EntityFile"); m_Config = ResourceManager::Load("Config.ini"); + ResourceManager::UseThreading = m_Config->Get("Multithreading.ResourceLoading", true); LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get("Debug.LogLevel", 1)); // Create the core event broker