Added a few debug messages to ResourceManager.
No longer warns about hot-loading when a resource which causes a load chain is preloaded.
This commit is contained in:
+14
-4
@@ -9,10 +9,6 @@ Resource* ResourceManager::CreateResource(std::string resourceType, std::string
|
|||||||
LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": Type not registered", resourceName.c_str(), resourceType.c_str());
|
LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": Type not registered", resourceName.c_str(), resourceType.c_str());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto resIt = m_ResourceCache.find(resourceName);
|
|
||||||
if (resIt != m_ResourceCache.end())
|
|
||||||
return resIt->second;
|
|
||||||
|
|
||||||
// Call the factory function
|
// Call the factory function
|
||||||
Resource* resource = facIt->second(resourceName);
|
Resource* resource = facIt->second(resourceName);
|
||||||
@@ -32,7 +28,16 @@ void ResourceManager::RegisterType(std::string resourceType, std::function<Resou
|
|||||||
|
|
||||||
void ResourceManager::Preload(std::string resourceType, std::string resourceName)
|
void ResourceManager::Preload(std::string resourceType, std::string resourceName)
|
||||||
{
|
{
|
||||||
|
if (IsResourceLoaded(resourceName))
|
||||||
|
{
|
||||||
|
LOG_WARNING("Attempted to preload resource \"%s\" multiple times!", resourceName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_Preloading = true;
|
||||||
|
LOG_INFO("Preloading resource \"%s\"", resourceName.c_str());
|
||||||
CreateResource(resourceType, resourceName);
|
CreateResource(resourceType, resourceName);
|
||||||
|
m_Preloading = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int ResourceManager::GetTypeID(std::string resourceType)
|
unsigned int ResourceManager::GetTypeID(std::string resourceType)
|
||||||
@@ -48,3 +53,8 @@ unsigned int ResourceManager::GetNewResourceID(unsigned int typeID)
|
|||||||
{
|
{
|
||||||
return m_ResourceCount[typeID]++;
|
return m_ResourceCount[typeID]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ResourceManager::IsResourceLoaded(std::string resourceName)
|
||||||
|
{
|
||||||
|
return m_ResourceCache.find(resourceName) != m_ResourceCache.end();
|
||||||
|
}
|
||||||
|
|||||||
+23
-4
@@ -19,14 +19,17 @@ class ResourceManager
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ResourceManager()
|
ResourceManager()
|
||||||
: m_CurrentResourceTypeID(0) { }
|
: m_CurrentResourceTypeID(0), m_Preloading(false) { }
|
||||||
|
|
||||||
// Registers the factory function of a resource type
|
// Registers the factory function of a resource type
|
||||||
void RegisterType(std::string resourceType, std::function<Resource*(std::string)> factoryFunction);
|
void RegisterType(std::string resourceType, std::function<Resource*(std::string)> factoryFunction);
|
||||||
|
|
||||||
// Loads a resource and caches it for future use
|
// Loads a resource and caches it for future use
|
||||||
void Preload(std::string resourceType, std::string resourceName);
|
void Preload(std::string resourceType, std::string resourceName);
|
||||||
|
|
||||||
|
// Checks if a resource is in cache
|
||||||
|
bool IsResourceLoaded(std::string resourceName);
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
// Hot-loads a resource and caches it for future use
|
// Hot-loads a resource and caches it for future use
|
||||||
T* Load(std::string resourceType, std::string resourceName);
|
T* Load(std::string resourceType, std::string resourceName);
|
||||||
@@ -44,6 +47,8 @@ private:
|
|||||||
std::unordered_map<std::string, unsigned int> m_ResourceTypeIDs;
|
std::unordered_map<std::string, unsigned int> m_ResourceTypeIDs;
|
||||||
// Number of resources of a type. Doubles as local ID.
|
// Number of resources of a type. Doubles as local ID.
|
||||||
std::unordered_map<unsigned int, unsigned int> m_ResourceCount;
|
std::unordered_map<unsigned int, unsigned int> m_ResourceCount;
|
||||||
|
// Flag to suppress hot-load warnings when a preloading resource chain loads another resource
|
||||||
|
bool m_Preloading;
|
||||||
|
|
||||||
unsigned int GetTypeID(std::string resourceType);
|
unsigned int GetTypeID(std::string resourceType);
|
||||||
unsigned int GetNewResourceID(unsigned int typeID);
|
unsigned int GetNewResourceID(unsigned int typeID);
|
||||||
@@ -55,20 +60,34 @@ private:
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
T* ResourceManager::Load(std::string resourceType, std::string resourceName)
|
T* ResourceManager::Load(std::string resourceType, std::string resourceName)
|
||||||
{
|
{
|
||||||
|
auto it = m_ResourceCache.find(resourceName);
|
||||||
|
if (it != m_ResourceCache.end())
|
||||||
|
return static_cast<T*>(it->second);
|
||||||
|
|
||||||
|
if (m_Preloading)
|
||||||
|
{
|
||||||
|
LOG_INFO("Preloading resource \"%s\"", resourceName.c_str());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LOG_WARNING("Hot-loading resource \"%s\"", resourceName.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
return static_cast<T*>(CreateResource(resourceType, resourceName));
|
return static_cast<T*>(CreateResource(resourceType, resourceName));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T* ResourceManager::Fetch(std::string resourceName) const
|
T* ResourceManager::Fetch(std::string resourceName) const
|
||||||
{
|
{
|
||||||
if (m_ResourceCache.find(resourceName) == m_ResourceCache.end())
|
auto it = m_ResourceCache.find(resourceName);
|
||||||
|
if (it == m_ResourceCache.end())
|
||||||
{
|
{
|
||||||
LOG_ERROR("Failed to fetch resource \"%s\": Resource not loaded!", resourceName.c_str());
|
LOG_ERROR("Failed to fetch resource \"%s\": Resource not loaded!", resourceName.c_str());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return static_cast<T*>(m_ResourceCache.at(resourceName));
|
return static_cast<T*>(it->second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user