ResourceCache.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef RESOURCE_RESOURCECACHE_H
  24. #define RESOURCE_RESOURCECACHE_H
  25. #include "File.h"
  26. #include "Resource.h"
  27. #include "SharedPtr.h"
  28. #include <map>
  29. #include <string>
  30. #include <vector>
  31. class PackageFile;
  32. class ResourceFactory;
  33. //! A container of resources with specific type
  34. struct ResourceGroup
  35. {
  36. //! Construct with defaults
  37. ResourceGroup() :
  38. mMemoryBudget(0),
  39. mMemoryUse(0)
  40. {
  41. }
  42. //! Memory budget
  43. unsigned mMemoryBudget;
  44. //! Current memory use
  45. unsigned mMemoryUse;
  46. //! Resources
  47. std::map<StringHash, SharedPtr<Resource> > mResources;
  48. };
  49. //! Loads resources when requested and holds them for later access
  50. class ResourceCache : public RefCounted
  51. {
  52. public:
  53. //! Construct
  54. ResourceCache();
  55. //! Destruct. Free all resources
  56. virtual ~ResourceCache();
  57. //! Add a resource factory
  58. void addResourceFactory(ResourceFactory* factory);
  59. //! Add a resource load path
  60. void addResourcePath(const std::string& path);
  61. //! Add a package file for loading resources from
  62. void addPackageFile(PackageFile* package, bool addAsFirst = false);
  63. //! Add a manually created resource. Must be uniquely named
  64. bool addManualResource(Resource* resource);
  65. //! Remove a resource load path
  66. void removeResourcePath(const std::string& path);
  67. //! Remove a package file. Optionally release the resources loaded from it
  68. void removePackageFile(PackageFile* package, bool releaseResources = true, bool forceRelease = false);
  69. //! Remove a package file by name. Optionally release the resources loaded from it
  70. void removePackageFile(const std::string& fileName, bool releaseResources = true, bool forceRelease = false);
  71. //! Release a resource by name
  72. void releaseResource(ShortStringHash type, const std::string& name, bool force = false);
  73. //! Release a resource by name hash
  74. void releaseResource(ShortStringHash type, StringHash nameHash, bool force = false);
  75. //! Release all resources of a specific type
  76. void releaseResources(ShortStringHash type, bool force = false);
  77. //! Release resources of a specific type and partial name
  78. void releaseResources(ShortStringHash type, const std::string& partialName, bool force = false);
  79. //! Reload a resource. Return false and release it if fails
  80. bool reloadResource(Resource* resource);
  81. //! Set memory budget for a specific resource type, default 0 is unlimited
  82. void setMemoryBudget(ShortStringHash type, unsigned budget);
  83. //! Open and return a file from either the resource load paths or from inside a package file. Throw an exception if fails
  84. SharedPtr<File> getFile(const std::string& name);
  85. //! Return a resource by type and name. Load if not loaded yet. Return null if fails
  86. Resource* getResource(ShortStringHash type, const std::string& name);
  87. //! Return a resource by type and name hash. Load if not loaded yet. Return null if fails
  88. Resource* getResource(ShortStringHash type, StringHash nameHash);
  89. //! Return all resources of a specific type
  90. std::vector<Resource*> getResources(ShortStringHash type);
  91. //! Return all resources
  92. const std::map<ShortStringHash, ResourceGroup>& getAllResources() const { return mResourceGroups; }
  93. //! Return added resource load paths
  94. const std::vector<std::string>& getResourcePaths() const { return mResourcePaths; }
  95. //! Return added package files
  96. const std::vector<SharedPtr<PackageFile> >& getPackageFiles() const { return mPackages; }
  97. //! Template version of returning a resource by name
  98. template <class T> T* getResource(const std::string& name);
  99. //! Template version of returning a resource by name hash
  100. template <class T> T* getResource(StringHash nameHash);
  101. //! Template version of returning resources of a specific type
  102. template <class T> std::vector<T*> getResources();
  103. //! Return whether a file exists by name
  104. bool exists(const std::string& name) const;
  105. //! Return whether a file exists by name hash
  106. bool exists(StringHash nameHash) const;
  107. //! Return memory budget for a resource type
  108. unsigned getMemoryBudget(ShortStringHash type) const;
  109. //! Return total memory use for a resource type
  110. unsigned getMemoryUse(ShortStringHash type) const;
  111. //! Return total memory use for all resources
  112. unsigned getTotalMemoryUse() const;
  113. private:
  114. //! Find a resource
  115. const SharedPtr<Resource>& findResource(ShortStringHash type, StringHash nameHash);
  116. //! Release resources loaded from a package file
  117. void releasePackageResources(PackageFile* package, bool force = false);
  118. //! Update a resource group. Recalculate memory use and release resources if over memory budget
  119. void updateResourceGroup(ShortStringHash type);
  120. //! Resources by type
  121. std::map<ShortStringHash, ResourceGroup> mResourceGroups;
  122. //! Resource factories
  123. std::vector<SharedPtr<ResourceFactory> > mFactories;
  124. //! Resource load paths
  125. std::vector<std::string> mResourcePaths;
  126. //! Package files
  127. std::vector<SharedPtr<PackageFile> > mPackages;
  128. };
  129. template <class T> T* ResourceCache::getResource(const std::string& name)
  130. {
  131. ShortStringHash type = T::getTypeStatic();
  132. return static_cast<T*>(getResource(type, name));
  133. }
  134. template <class T> T* ResourceCache::getResource(StringHash nameHash)
  135. {
  136. ShortStringHash type = T::getTypeStatic();
  137. return static_cast<T*>(getResource(type, nameHash));
  138. }
  139. template <class T> std::vector<T*> ResourceCache::getResources()
  140. {
  141. ShortStringHash type = T::getTypeStatic();
  142. std::vector<Resource*> resources = getResources(type);
  143. std::vector<T*> ret;
  144. for (std::vector<Resource*>::const_iterator i = resources.begin(); i != resources.end(); ++i)
  145. ret.push_back(static_cast<T*>(*i));
  146. return ret;
  147. }
  148. #endif // RESOURCE_RESOURCECACHE_H