| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566 |
- //
- // Urho3D Engine
- // Copyright (c) 2008-2011 Lasse Öörni
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Log.h"
- #include "PackageFile.h"
- #include "ResourceCache.h"
- #include "ResourceFactory.h"
- #include "StringUtils.h"
- #include "DebugNew.h"
- ResourceCache::ResourceCache()
- {
- LOGINFO("Resource cache created");
- }
- ResourceCache::~ResourceCache()
- {
- // At this point factories may be the only thing keeping subsystems alive; remove resources before factories so that
- // resources can be released cleanly
- mResourceGroups.clear();
- mFactories.clear();
-
- LOGINFO("Resource cache shut down");
- }
- void ResourceCache::addResourceFactory(ResourceFactory* factory)
- {
- if (!factory)
- return;
-
- mFactories.push_back(SharedPtr<ResourceFactory>(factory));
- }
- bool ResourceCache::addResourcePath(const std::string& path)
- {
- if (!directoryExists(path))
- {
- LOGERROR("Could not open directory " + path);
- return false;
- }
-
- std::string fixedPath = fixPath(path);
- std::string pathLower = toLower(fixedPath);
-
- // Check that the same path does not already exist
- for (unsigned i = 0; i < mResourcePaths.size(); ++i)
- {
- if (toLower(mResourcePaths[i]) == pathLower)
- return true;
- }
-
- mResourcePaths.push_back(fixedPath);
-
- // Scan the path for files recursively and add their hash-to-name mappings
- std::vector<std::string> fileNames;
- scanDirectory(fileNames, fixedPath, "*.*", SCAN_FILES, true);
- for (unsigned i = 0; i < fileNames.size(); ++i)
- registerHash(fileNames[i]);
-
- LOGINFO("Added resource path " + fixedPath);
- return true;
- }
- void ResourceCache::addPackageFile(PackageFile* package, bool addAsFirst)
- {
- if (!package)
- return;
-
- if (addAsFirst)
- mPackages.insert(mPackages.begin(), SharedPtr<PackageFile>(package));
- else
- mPackages.push_back(SharedPtr<PackageFile>(package));
-
- // Scan the package for files and add their hash-to-name mappings
- const std::map<std::string, PackageEntry>& entries = package->getEntries();
- for (std::map<std::string, PackageEntry>::const_iterator i = entries.begin(); i != entries.end(); ++i)
- registerHash(i->first);
-
- LOGINFO("Added resource package " + package->getName());
- }
- bool ResourceCache::addManualResource(Resource* resource)
- {
- if (!resource)
- {
- LOGERROR("Null manual resource");
- return false;
- }
-
- if (resource->getName().empty())
- {
- LOGERROR("Manual resource with empty name, can not add");
- return false;
- }
-
- resource->resetUseTimer();
- mResourceGroups[resource->getType()].mResources[resource->getNameHash()] = resource;
- updateResourceGroup(resource->getType());
- return true;
- }
- void ResourceCache::removeResourcePath(const std::string& path)
- {
- std::string fixedPath = toLower(fixPath(path));
- for (std::vector<std::string>::iterator i = mResourcePaths.begin(); i != mResourcePaths.end(); ++i)
- {
- if (toLower(*i) == fixedPath)
- {
- mResourcePaths.erase(i);
- return;
- }
- }
- }
- void ResourceCache::removePackageFile(PackageFile* package, bool releaseResources, bool forceRelease)
- {
- for (std::vector<SharedPtr<PackageFile> >::iterator i = mPackages.begin(); i != mPackages.end(); ++i)
- {
- if ((*i) == package)
- {
- if (releaseResources)
- releasePackageResources(*i, forceRelease);
- mPackages.erase(i);
- return;
- }
- }
- }
- void ResourceCache::removePackageFile(const std::string& fileName, bool releaseResources, bool forceRelease)
- {
- std::string fileNameLower = toLower(fileName);
-
- for (std::vector<SharedPtr<PackageFile> >::iterator i = mPackages.begin(); i != mPackages.end(); ++i)
- {
- if (toLower((*i)->getName()) == fileNameLower)
- {
- if (releaseResources)
- releasePackageResources(*i, forceRelease);
- mPackages.erase(i);
- return;
- }
- }
- }
- void ResourceCache::releaseResource(ShortStringHash type, const std::string& name, bool force)
- {
- releaseResource(type, StringHash(name), force);
- }
- void ResourceCache::releaseResource(ShortStringHash type, StringHash nameHash, bool force)
- {
- const SharedPtr<Resource>& existingRes = findResource(type, nameHash);
- if (!existingRes)
- return;
-
- // If other references exist, do not release, unless forced
- if ((existingRes.getRefCount() == 1) || (force))
- {
- mResourceGroups[type].mResources.erase(nameHash);
- updateResourceGroup(type);
- }
- }
- void ResourceCache::releaseResources(ShortStringHash type, bool force)
- {
- bool released = false;
-
- for (std::map<ShortStringHash, ResourceGroup>::iterator i = mResourceGroups.begin();
- i != mResourceGroups.end(); ++i)
- {
- if (i->first == type)
- {
- for (std::map<StringHash, SharedPtr<Resource> >::iterator j = i->second.mResources.begin();
- j != i->second.mResources.end();)
- {
- std::map<StringHash, SharedPtr<Resource> >::iterator current = j++;
- // If other references exist, do not release, unless forced
- if ((current->second.getRefCount() == 1) || (force))
- {
- i->second.mResources.erase(current);
- released = true;
- }
- }
- }
- }
-
- if (released)
- updateResourceGroup(type);
- }
- void ResourceCache::releaseResources(ShortStringHash type, const std::string& partialName, bool force)
- {
- std::string partialNameLower = toLower(partialName);
- bool released = false;
-
- for (std::map<ShortStringHash, ResourceGroup>::iterator i = mResourceGroups.begin();
- i != mResourceGroups.end(); ++i)
- {
- if (i->first == type)
- {
- for (std::map<StringHash, SharedPtr<Resource> >::iterator j = i->second.mResources.begin();
- j != i->second.mResources.end();)
- {
- std::map<StringHash, SharedPtr<Resource> >::iterator current = j++;
- if (current->second->getName().find(partialNameLower) != std::string::npos)
- {
- // If other references exist, do not release, unless forced
- if ((current->second.getRefCount() == 1) || (force))
- {
- i->second.mResources.erase(current);
- released = true;
- }
- }
- }
- }
- }
-
- if (released)
- updateResourceGroup(type);
- }
- void ResourceCache::releaseAllResources(bool force)
- {
- for (std::map<ShortStringHash, ResourceGroup>::iterator i = mResourceGroups.begin();
- i != mResourceGroups.end(); ++i)
- {
- bool released = false;
-
- for (std::map<StringHash, SharedPtr<Resource> >::iterator j = i->second.mResources.begin();
- j != i->second.mResources.end();)
- {
- std::map<StringHash, SharedPtr<Resource> >::iterator current = j++;
- // If other references exist, do not release, unless forced
- if ((current->second.getRefCount() == 1) || (force))
- {
- i->second.mResources.erase(current);
- released = true;
- }
- }
- if (released)
- updateResourceGroup(i->first);
- }
- }
- bool ResourceCache::reloadResource(Resource* resource)
- {
- if (!resource)
- return false;
-
- try
- {
- SharedPtr<File> file = getFile(resource->getName());
- resource->load(*(file.getPtr()), this);
- resource->resetUseTimer();
- updateResourceGroup(resource->getType());
- return true;
- }
- catch (...)
- {
- releaseResource(resource->getType(), resource->getNameHash());
- return false;
- }
- }
- void ResourceCache::setMemoryBudget(ShortStringHash type, unsigned budget)
- {
- mResourceGroups[type].mMemoryBudget = budget;
- }
- SharedPtr<File> ResourceCache::getFile(const std::string& name)
- {
- // Check first the packages
- for (unsigned i = 0; i < mPackages.size(); ++i)
- {
- if (mPackages[i]->exists(name))
- return SharedPtr<File>(new File(*mPackages[i], name));
- }
-
- // Then the filesystem
- for (unsigned i = 0; i < mResourcePaths.size(); ++i)
- {
- if (fileExists(mResourcePaths[i] + name))
- {
- // Construct the file first with full path, then rename it to not contain the resource path,
- // so that the file's name can be used in further getFile() calls (for example over the network)
- SharedPtr<File> file(new File(mResourcePaths[i] + name));
- file->setName(name);
- return file;
- }
- }
-
- EXCEPTION("Could not find resource " + name);
- }
- Resource* ResourceCache::getResource(ShortStringHash type, const std::string& name)
- {
- // Add the name to the hash map, so if this is an unknown resource, the error will not be unintelligible
- registerHash(name);
-
- return getResource(type, StringHash(name));
- }
- Resource* ResourceCache::getResource(ShortStringHash type, StringHash nameHash)
- {
- // If null hash, return null pointer immediately
- if (!nameHash)
- return 0;
-
- const SharedPtr<Resource>& existing = findResource(type, nameHash);
- if (existing)
- return existing;
-
- SharedPtr<Resource> resource;
- std::string name = hashToString(nameHash);
-
- for (unsigned i = 0; i < mFactories.size(); ++i)
- {
- resource = mFactories[i]->createResource(type, name);
- if (resource)
- break;
- }
-
- if (!resource)
- {
- LOGERROR("Could not load unknown resource type " + toString(type));
- return 0;
- }
-
- LOGDEBUG("Loading resource " + name);
-
- // Attempt to load the resource
- try
- {
- SharedPtr<File> file = getFile(name);
- resource->load(*(file.getPtr()), this);
- resource->resetUseTimer();
-
- // Store to cache
- mResourceGroups[type].mResources[nameHash] = resource;
- updateResourceGroup(type);
-
- return mResourceGroups[type].mResources[nameHash];
- }
- catch (...)
- {
- return 0;
- }
- }
- void ResourceCache::getResources(std::vector<Resource*>& result, ShortStringHash type) const
- {
- result.clear();
- std::map<ShortStringHash, ResourceGroup>::const_iterator i = mResourceGroups.find(type);
- if (i != mResourceGroups.end())
- {
- for (std::map<StringHash, SharedPtr<Resource> >::const_iterator j = i->second.mResources.begin();
- j != i->second.mResources.end(); ++j)
- result.push_back(j->second);
- }
- }
- bool ResourceCache::exists(const std::string& name) const
- {
- for (unsigned i = 0; i < mPackages.size(); ++i)
- {
- if (mPackages[i]->exists(name))
- return true;
- }
-
- for (unsigned i = 0; i < mResourcePaths.size(); ++i)
- {
- if (fileExists(mResourcePaths[i] + name))
- return true;
- }
-
- return false;
- }
- bool ResourceCache::exists(StringHash nameHash) const
- {
- std::string name = hashToString(nameHash);
- return exists(name);
- }
- unsigned ResourceCache::getMemoryBudget(ShortStringHash type) const
- {
- std::map<ShortStringHash, ResourceGroup>::const_iterator i = mResourceGroups.find(type);
- if (i != mResourceGroups.end())
- return i->second.mMemoryBudget;
- else
- return 0;
- }
- unsigned ResourceCache::getMemoryUse(ShortStringHash type) const
- {
- std::map<ShortStringHash, ResourceGroup>::const_iterator i = mResourceGroups.find(type);
- if (i != mResourceGroups.end())
- return i->second.mMemoryUse;
- else
- return 0;
- }
- unsigned ResourceCache::getTotalMemoryUse() const
- {
- unsigned total = 0;
- for (std::map<ShortStringHash, ResourceGroup>::const_iterator i = mResourceGroups.begin(); i != mResourceGroups.end(); ++i)
- total += i->second.mMemoryUse;
- return total;
- }
- std::string ResourceCache::getPreferredResourcePath(const std::string& path)
- {
- std::string fixedPath = fixPath(path);
-
- // Check for the existence of some known resource subdirectories to decide if we should add the parent directory instead
- static const std::string checkDirs[] = {
- "Fonts",
- "Materials",
- "Models",
- "Music",
- "Particle",
- "Physics",
- "Scripts",
- "Sounds",
- "Shaders",
- "Textures",
- "UI",
- ""
- };
-
- bool pathHasKnownDirs = false;
- bool parentHasKnownDirs = false;
-
- for (unsigned i = 0; !checkDirs[i].empty(); ++i)
- {
- if (directoryExists(fixedPath + checkDirs[i]))
- {
- pathHasKnownDirs = true;
- break;
- }
- }
- if (!pathHasKnownDirs)
- {
- std::string parentPath = getParentPath(fixedPath);
- for (unsigned i = 0; !checkDirs[i].empty(); ++i)
- {
- if (directoryExists(parentPath + checkDirs[i]))
- {
- parentHasKnownDirs = true;
- break;
- }
- }
- // If path does not have known subdirectories, but the parent path has, use the parent instead
- if (parentHasKnownDirs)
- fixedPath = parentPath;
- }
-
- return fixedPath;
- }
- const SharedPtr<Resource>& ResourceCache::findResource(ShortStringHash type, StringHash nameHash)
- {
- static const SharedPtr<Resource> noResource;
-
- std::map<ShortStringHash, ResourceGroup>::iterator i = mResourceGroups.find(type);
- if (i == mResourceGroups.end())
- return noResource;
- std::map<StringHash, SharedPtr<Resource> >::iterator j = i->second.mResources.find(nameHash);
- if (j == i->second.mResources.end())
- return noResource;
-
- return j->second;
- }
- void ResourceCache::releasePackageResources(PackageFile* package, bool force)
- {
- std::set<ShortStringHash> affectedGroups;
-
- const std::map<std::string, PackageEntry>& entries = package->getEntries();
- for (std::map<std::string, PackageEntry>::const_iterator i = entries.begin(); i != entries.end(); ++i)
- {
- StringHash nameHash(i->first);
-
- // We do not know the actual resource type, so search all type containers
- for (std::map<ShortStringHash, ResourceGroup>::iterator j = mResourceGroups.begin();
- j != mResourceGroups.end(); ++j)
- {
- std::map<StringHash, SharedPtr<Resource> >::iterator k = j->second.mResources.find(nameHash);
- if (k != j->second.mResources.end())
- {
- // If other references exist, do not release, unless forced
- if ((k->second.getRefCount() == 1) || (force))
- {
- j->second.mResources.erase(k);
- affectedGroups.insert(j->first);
- }
- break;
- }
- }
- }
-
- for (std::set<ShortStringHash>::iterator i = affectedGroups.begin(); i != affectedGroups.end(); ++i)
- updateResourceGroup(*i);
- }
- void ResourceCache::updateResourceGroup(ShortStringHash type)
- {
- std::map<ShortStringHash, ResourceGroup>::iterator i = mResourceGroups.find(type);
- if (i == mResourceGroups.end())
- return;
-
- for (;;)
- {
- unsigned totalSize = 0;
- unsigned oldestTimer = 0;
- std::map<StringHash, SharedPtr<Resource> >::iterator oldestResource = i->second.mResources.end();
-
- for (std::map<StringHash, SharedPtr<Resource> >::iterator j = i->second.mResources.begin();
- j != i->second.mResources.end(); ++j)
- {
- totalSize += j->second->getMemoryUse();
- unsigned useTimer = j->second->getUseTimer();
- if (useTimer > oldestTimer)
- {
- oldestTimer = useTimer;
- oldestResource = j;
- }
- }
-
- i->second.mMemoryUse = totalSize;
-
- // If memory budget defined and is exceeded, remove the oldest resource and loop again
- // (resources in use always return a zero timer and can not be removed)
- if ((i->second.mMemoryBudget) && (i->second.mMemoryUse > i->second.mMemoryBudget) &&
- (oldestResource != i->second.mResources.end()))
- {
- LOGDEBUG("Resource group " + oldestResource->second->getTypeName() + " over memory budget, releasing resource " +
- oldestResource->second->getName());
- i->second.mResources.erase(oldestResource);
- }
- else
- break;
- }
- }
|