| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- #include "BsResources.h"
- #include "BsResource.h"
- #include "BsResourceManifest.h"
- #include "BsException.h"
- #include "BsFileSerializer.h"
- #include "BsFileSystem.h"
- #include "BsTaskScheduler.h"
- #include "BsUUID.h"
- #include "BsPath.h"
- #include "BsDebug.h"
- namespace BansheeEngine
- {
- Resources::Resources()
- {
- mDefaultResourceManifest = ResourceManifest::create("Default");
- mResourceManifests.push_back(mDefaultResourceManifest);
- }
- Resources::~Resources()
- {
- // Unload and invalidate all resources
- UnorderedMap<String, HResource> loadedResourcesCopy = mLoadedResources;
- for (auto& loadedResourcePair : loadedResourcesCopy)
- {
- unload(loadedResourcePair.second);
- // Invalidate the handle
- loadedResourcePair.second._setHandleData(nullptr, "");
- }
- }
- HResource Resources::load(const Path& filePath)
- {
- return loadInternal(filePath, true);
- }
- HResource Resources::loadAsync(const Path& filePath)
- {
- return loadInternal(filePath, false);
- }
- HResource Resources::loadFromUUID(const String& uuid)
- {
- Path filePath;
- bool foundPath = false;
- // Default manifest is at 0th index but all other take priority since Default manifest could
- // contain obsolete data.
- for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
- {
- if((*iter)->uuidToFilePath(uuid, filePath))
- {
- foundPath = true;
- break;
- }
- }
- if(!foundPath)
- {
- gDebug().logWarning("Cannot load resource. Resource with UUID '" + uuid + "' doesn't exist.");
- return HResource();
- }
- return load(filePath);
- }
- HResource Resources::loadFromUUIDAsync(const String& uuid)
- {
- Path filePath;
- bool foundPath = false;
- for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
- {
- if((*iter)->uuidToFilePath(uuid, filePath))
- {
- foundPath = true;
- break;
- }
- }
- if(!foundPath)
- {
- gDebug().logWarning("Cannot load resource. Resource with UUID '" + uuid + "' doesn't exist.");
- return HResource();
- }
- return loadAsync(filePath);
- }
- HResource Resources::loadInternal(const Path& filePath, bool synchronous)
- {
- String uuid;
- bool foundUUID = false;
- for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
- {
- if((*iter)->filePathToUUID(filePath, uuid))
- {
- foundUUID = true;
- break;
- }
- }
- if(!foundUUID)
- uuid = UUIDGenerator::instance().generateRandom();
- {
- BS_LOCK_MUTEX(mLoadedResourceMutex);
- auto iterFind = mLoadedResources.find(uuid);
- if(iterFind != mLoadedResources.end()) // Resource is already loaded
- {
- return iterFind->second;
- }
- }
- bool resourceLoadingInProgress = false;
- HResource existingResource;
- {
- BS_LOCK_MUTEX(mInProgressResourcesMutex);
- auto iterFind2 = mInProgressResources.find(uuid);
- if(iterFind2 != mInProgressResources.end())
- {
- existingResource = iterFind2->second;
- resourceLoadingInProgress = true;
- }
- }
- if(resourceLoadingInProgress) // We're already loading this resource
- {
- if(!synchronous)
- return existingResource;
- else
- {
- // Previously being loaded as async but now we want it synced, so we wait
- existingResource.blockUntilLoaded();
- return existingResource;
- }
- }
- if(!FileSystem::isFile(filePath))
- {
- gDebug().logWarning("Specified file: " + filePath.toString() + " doesn't exist.");
- return HResource();
- }
- HResource newResource(uuid);
- {
- BS_LOCK_MUTEX(mInProgressResourcesMutex);
- mInProgressResources[uuid] = newResource;
- }
- if(synchronous)
- {
- loadCallback(filePath, newResource);
- }
- else
- {
- String fileName = filePath.getFilename();
- String taskName = "Resource load: " + fileName;
- TaskPtr task = Task::create(taskName, std::bind(&Resources::loadCallback, this, filePath, newResource));
- TaskScheduler::instance().addTask(task);
- }
- return newResource;
- }
- ResourcePtr Resources::loadFromDiskAndDeserialize(const Path& filePath)
- {
- FileSerializer fs;
- std::shared_ptr<IReflectable> loadedData = fs.decode(filePath);
- if(loadedData == nullptr)
- BS_EXCEPT(InternalErrorException, "Unable to load resource.");
- if(!loadedData->isDerivedFrom(Resource::getRTTIStatic()))
- BS_EXCEPT(InternalErrorException, "Loaded class doesn't derive from Resource.");
- ResourcePtr resource = std::static_pointer_cast<Resource>(loadedData);
- return resource;
- }
- void Resources::unload(HResource resource)
- {
- if(!resource.isLoaded()) // If it's still loading wait until that finishes
- resource.blockUntilLoaded();
- // Call this before we actually destroy it
- onResourceDestroyed(resource);
- resource->destroy();
- {
- BS_LOCK_MUTEX(mLoadedResourceMutex);
- mLoadedResources.erase(resource.getUUID());
- }
- resource._setHandleData(nullptr, "");
- }
- void Resources::unloadAllUnused()
- {
- Vector<HResource> resourcesToUnload;
- {
- BS_LOCK_MUTEX(mLoadedResourceMutex);
- for(auto iter = mLoadedResources.begin(); iter != mLoadedResources.end(); ++iter)
- {
- if (iter->second.mData.unique()) // We just have this one reference, meaning nothing is using this resource
- resourcesToUnload.push_back(iter->second);
- }
- }
- for(auto iter = resourcesToUnload.begin(); iter != resourcesToUnload.end(); ++iter)
- {
- unload(*iter);
- }
- }
- void Resources::save(HResource resource, const Path& filePath, bool overwrite)
- {
- if(!resource.isLoaded())
- resource.blockUntilLoaded();
- bool fileExists = FileSystem::isFile(filePath);
- if(fileExists)
- {
- if(overwrite)
- FileSystem::remove(filePath);
- else
- BS_EXCEPT(InvalidParametersException, "Another file exists at the specified location.");
- }
- mDefaultResourceManifest->registerResource(resource.getUUID(), filePath);
- FileSerializer fs;
- fs.encode(resource.get(), filePath);
- }
- void Resources::registerResourceManifest(const ResourceManifestPtr& manifest)
- {
- if(manifest->getName() == "Default")
- return;
- auto findIter = std::find(mResourceManifests.begin(), mResourceManifests.end(), manifest);
- if(findIter == mResourceManifests.end())
- mResourceManifests.push_back(manifest);
- else
- *findIter = manifest;
- }
- ResourceManifestPtr Resources::getResourceManifest(const String& name) const
- {
- for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
- {
- if(name == (*iter)->getName())
- return (*iter);
- }
- return nullptr;
- }
- HResource Resources::_createResourceHandle(const ResourcePtr& obj)
- {
- String uuid = UUIDGenerator::instance().generateRandom();
- HResource newHandle(obj, uuid);
- {
- BS_LOCK_MUTEX(mLoadedResourceMutex);
- mLoadedResources[uuid] = newHandle;
- }
-
- return newHandle;
- }
- bool Resources::getFilePathFromUUID(const String& uuid, Path& filePath) const
- {
- for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
- {
- if((*iter)->uuidToFilePath(uuid, filePath))
- return true;
- }
- return false;
- }
- bool Resources::getUUIDFromFilePath(const Path& path, String& uuid) const
- {
- for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
- {
- if((*iter)->filePathToUUID(path, uuid))
- return true;
- }
- return false;
- }
- void Resources::loadCallback(const Path& filePath, HResource& resource)
- {
- ResourcePtr rawResource = loadFromDiskAndDeserialize(filePath);
- {
- BS_LOCK_MUTEX(mInProgressResourcesMutex);
- mInProgressResources.erase(resource.getUUID());
- }
- resource._setHandleData(rawResource, resource.getUUID());
- onResourceLoaded(resource);
- {
- BS_LOCK_MUTEX(mLoadedResourceMutex);
- mLoadedResources[resource.getUUID()] = resource;
- }
- }
- BS_CORE_EXPORT Resources& gResources()
- {
- return Resources::instance();
- }
- }
|