ResourceManager::Load never returns null, throws exceptions instead.
This commit is contained in:
@@ -220,7 +220,7 @@ bool attachAABBComponentFromModel(World* world, EntityID id)
|
||||
}
|
||||
ComponentWrapper model = world->GetComponent(id, "Model");
|
||||
ComponentWrapper collision = world->AttachComponent(id, "AABB");
|
||||
Model* modelRes = ResourceManager::Load<Model, true>(model["Resource"]);
|
||||
Model* modelRes = ResourceManager::Load<Model>(model["Resource"]);
|
||||
if (modelRes == nullptr) {
|
||||
return false;
|
||||
}
|
||||
@@ -247,7 +247,7 @@ bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox)
|
||||
{
|
||||
ComponentWrapper& cTrans = world->GetComponent(AABBComponent.EntityID, "Transform");
|
||||
ComponentWrapper model = world->GetComponent(AABBComponent.EntityID, "Model");
|
||||
Model* modelRes = ResourceManager::Load<Model, true>(model["Resource"]);
|
||||
Model* modelRes = ResourceManager::Load<Model>(model["Resource"]);
|
||||
outBox.CreateFromCenter(AABBComponent["BoxCenter"], AABBComponent["BoxSize"]);
|
||||
glm::vec3 mini = outBox.MinCorner();
|
||||
glm::vec3 maxi = outBox.MaxCorner();
|
||||
|
||||
@@ -13,8 +13,8 @@ std::unordered_map<std::string, unsigned int> ResourceManager::m_ResourceTypeIDs
|
||||
std::unordered_map<unsigned int, unsigned int> ResourceManager::m_ResourceCount;
|
||||
FileWatcher ResourceManager::m_FileWatcher;
|
||||
std::unordered_map<std::pair<std::string, std::string>, boost::thread> ResourceManager::m_LoadingThreads;
|
||||
std::unordered_map<std::pair<std::string, std::string>, std::exception_ptr> ResourceManager::m_LoadingThreadExceptions;
|
||||
boost::recursive_mutex ResourceManager::m_Mutex;
|
||||
const ResourceManager::SpecialResourcePointer ResourceManager::m_StillLoading;
|
||||
|
||||
unsigned int ResourceManager::GetTypeID(std::string resourceType)
|
||||
{
|
||||
@@ -79,47 +79,61 @@ void ResourceManager::Update()
|
||||
m_FileWatcher.Check();
|
||||
}
|
||||
|
||||
Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent)
|
||||
Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent, std::exception_ptr& exception)
|
||||
{
|
||||
auto facIt = m_FactoryFunctions.find(resourceType);
|
||||
if (facIt == m_FactoryFunctions.end()) {
|
||||
LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceType.c_str());
|
||||
return nullptr;
|
||||
cacheResource(nullptr, resourceType, resourceName, parent);
|
||||
//This basically throws an exception.
|
||||
exception = std::make_exception_ptr(Resource::FailedLoadingException()); return nullptr;
|
||||
}
|
||||
|
||||
// Call the factory function
|
||||
Resource* resource = nullptr;
|
||||
try {
|
||||
resource = facIt->second(resourceName);
|
||||
} catch (const ThreadUnsafeResource::StillLoadingException&) {
|
||||
resource = m_StillLoading;
|
||||
return cacheResource(facIt->second(resourceName), resourceType, resourceName, parent);
|
||||
} catch (const Resource::StillLoadingException&) {
|
||||
exception = std::current_exception(); return nullptr;
|
||||
} catch (const std::exception& e) {
|
||||
LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": %s", resourceName.c_str(), resourceType.c_str(), e.what());
|
||||
cacheResource(nullptr, resourceType, resourceName, parent);
|
||||
exception = std::current_exception(); return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Resource* ResourceManager::createResourceThrowing(std::string resourceType, std::string resourceName, Resource* parent)
|
||||
{
|
||||
std::exception_ptr exception;
|
||||
Resource* res = createResource(resourceType, resourceName, parent, exception);
|
||||
if (exception) {
|
||||
std::rethrow_exception(exception);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
Resource* ResourceManager::cacheResource(Resource* resource, std::string resourceType, std::string resourceName, Resource* parent)
|
||||
{
|
||||
//Lock the mutex immediately, and unlock it when leaving the code block.
|
||||
boost::lock_guard<decltype(m_Mutex)> guard(m_Mutex);
|
||||
if (resource != nullptr) {
|
||||
// Store IDs
|
||||
resource->TypeID = GetTypeID(resourceType);
|
||||
resource->ResourceID = GetNewResourceID(resource->TypeID);
|
||||
}
|
||||
|
||||
{
|
||||
//Lock the mutex immediately, and unlock it when leaving the code block.
|
||||
boost::lock_guard<decltype(m_Mutex)> guard(m_Mutex);
|
||||
if (resource != nullptr) {
|
||||
// Store IDs
|
||||
resource->TypeID = GetTypeID(resourceType);
|
||||
resource->ResourceID = GetNewResourceID(resource->TypeID);
|
||||
}
|
||||
|
||||
// Cache
|
||||
m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource;
|
||||
m_ResourceFromName[resourceName] = resource;
|
||||
if (parent != nullptr) {
|
||||
m_ResourceParents[resource] = parent;
|
||||
}
|
||||
|
||||
if (!boost::filesystem::is_directory(resourceName)) {
|
||||
LOG_DEBUG("Adding watch for %s", resourceName.c_str());
|
||||
m_FileWatcher.AddWatch(resourceName, fileWatcherCallback);
|
||||
}
|
||||
// Cache
|
||||
m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource;
|
||||
m_ResourceFromName[resourceName] = resource;
|
||||
if (parent != nullptr) {
|
||||
m_ResourceParents[resource] = parent;
|
||||
}
|
||||
|
||||
return resource;
|
||||
//if (!boost::filesystem::is_directory(resourceName)) {
|
||||
// LOG_DEBUG("Adding watch for %s", resourceName.c_str());
|
||||
// m_FileWatcher.AddWatch(resourceName, fileWatcherCallback);
|
||||
//}
|
||||
return resource;
|
||||
}
|
||||
|
||||
bool ResourceManager::IsMainThread()
|
||||
|
||||
@@ -2,12 +2,8 @@
|
||||
|
||||
Model::Model(std::string fileName)
|
||||
{
|
||||
//Load the RawModel asyncronously, this will be done on a separate thread in the background.
|
||||
//Try loading the model asyncronously, if it throws any exceptions then let it propagate back to caller.
|
||||
m_RawModel = ResourceManager::Load<RawModel, true>(fileName);
|
||||
//If it is null, the model is not done yet, so tell resourceManager to try constructing me again later.
|
||||
if (m_RawModel == nullptr) {
|
||||
throw StillLoadingException();
|
||||
}
|
||||
|
||||
for (auto& group : m_RawModel->MaterialGroups) {
|
||||
if (!group.TexturePath.empty()) {
|
||||
|
||||
@@ -84,13 +84,21 @@ void RenderSystem::fillModels(std::list<std::shared_ptr<RenderJob>>& jobs, World
|
||||
continue;
|
||||
}
|
||||
|
||||
Model* model = ResourceManager::Load<::Model, true>(resource);
|
||||
if (model == nullptr) {
|
||||
model = ResourceManager::Load<::Model>("Models/Core/Error.obj");
|
||||
Model* model;
|
||||
try {
|
||||
model = ResourceManager::Load<::Model, true>(resource);
|
||||
} catch (const Resource::StillLoadingException&) {
|
||||
//continue;
|
||||
model = ResourceManager::Load<::Model>("Models/Core/UnitRaptor.obj");
|
||||
} catch (const std::exception&) {
|
||||
try {
|
||||
model = ResourceManager::Load<::Model>("Models/Core/Error.obj");
|
||||
} catch (const std::exception&) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
glm::mat4 modelMatrix = Transform::ModelMatrix(modelComponent.EntityID, world);
|
||||
|
||||
for (auto matGroup : model->MaterialGroups()) {
|
||||
std::shared_ptr<ModelJob> modelJob = std::shared_ptr<ModelJob>(new ModelJob(model, m_Camera, modelMatrix, matGroup, modelComponent, world));
|
||||
jobs.push_back(modelJob);
|
||||
|
||||
Reference in New Issue
Block a user