ResourceCache.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "File.h"
  24. #include "HashSet.h"
  25. #include "Resource.h"
  26. namespace Urho3D
  27. {
  28. class FileWatcher;
  29. class PackageFile;
  30. /// Container of resources with specific type.
  31. struct ResourceGroup
  32. {
  33. /// Construct with defaults.
  34. ResourceGroup() :
  35. memoryBudget_(0),
  36. memoryUse_(0)
  37. {
  38. }
  39. /// Memory budget.
  40. unsigned memoryBudget_;
  41. /// Current memory use.
  42. unsigned memoryUse_;
  43. /// Resources.
  44. HashMap<StringHash, SharedPtr<Resource> > resources_;
  45. };
  46. /// %Resource cache subsystem. Loads resources on demand and stores them for later access.
  47. class URHO3D_API ResourceCache : public Object
  48. {
  49. OBJECT(ResourceCache);
  50. public:
  51. /// Construct.
  52. ResourceCache(Context* context);
  53. /// Destruct. Free all resources.
  54. virtual ~ResourceCache();
  55. /// Add a resource load directory.
  56. bool AddResourceDir(const String& pathName);
  57. /// Add a package file for loading resources from.
  58. void AddPackageFile(PackageFile* package, bool addAsFirst = false);
  59. /// Add a manually created resource. Must be uniquely named.
  60. bool AddManualResource(Resource* resource);
  61. /// Remove a resource load directory.
  62. void RemoveResourceDir(const String& pathName);
  63. /// Remove a package file. Optionally release the resources loaded from it.
  64. void RemovePackageFile(PackageFile* package, bool releaseResources = true, bool forceRelease = false);
  65. /// Remove a package file by name. Optionally release the resources loaded from it.
  66. void RemovePackageFile(const String& fileName, bool releaseResources = true, bool forceRelease = false);
  67. /// Release a resource by name.
  68. void ReleaseResource(ShortStringHash type, const String& name, bool force = false);
  69. /// Release a resource by name hash.
  70. void ReleaseResource(ShortStringHash type, StringHash nameHash, bool force = false);
  71. /// Release all resources of a specific type.
  72. void ReleaseResources(ShortStringHash type, bool force = false);
  73. /// Release resources of a specific type and partial name.
  74. void ReleaseResources(ShortStringHash type, const String& partialName, bool force = false);
  75. /// Release all resources.
  76. void ReleaseAllResources(bool force = false);
  77. /// Reload a resource. Return false and release it if fails.
  78. bool ReloadResource(Resource* resource);
  79. /// Set memory budget for a specific resource type, default 0 is unlimited.
  80. void SetMemoryBudget(ShortStringHash type, unsigned budget);
  81. /// Enable or disable automatic reloading of resources as files are modified.
  82. void SetAutoReloadResources(bool enable);
  83. /// Open and return a file from the resource load paths or from inside a package file. If not found, use a fallback search with absolute path. Return null if fails.
  84. SharedPtr<File> GetFile(const 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 String& name);
  87. /// Return a resource by type and name. Load if not loaded yet. Return null if fails.
  88. Resource* GetResource(ShortStringHash type, const char* name);
  89. /// Return a resource by type and name hash. Load if not loaded yet. Return null if fails.
  90. Resource* GetResource(ShortStringHash type, StringHash nameHash);
  91. /// Return all loaded resources of a specific type.
  92. void GetResources(PODVector<Resource*>& result, ShortStringHash type) const;
  93. /// Return all loaded resources.
  94. const HashMap<ShortStringHash, ResourceGroup>& GetAllResources() const { return resourceGroups_; }
  95. /// Return added resource load directories.
  96. const Vector<String>& GetResourceDirs() const { return resourceDirs_; }
  97. /// Return added package files.
  98. const Vector<SharedPtr<PackageFile> >& GetPackageFiles() const { return packages_; }
  99. /// Template version of returning a resource by name.
  100. template <class T> T* GetResource(const String& name);
  101. /// Template version of returning a resource by name.
  102. template <class T> T* GetResource(const char* name);
  103. /// Template version of returning a resource by name hash.
  104. template <class T> T* GetResource(StringHash nameHash);
  105. /// Template version of returning loaded resources of a specific type.
  106. template <class T> void GetResources(PODVector<T*>& result) const;
  107. /// Return whether a file exists by name.
  108. bool Exists(const String& name) const;
  109. /// Return whether a file exists by name hash.
  110. bool Exists(StringHash nameHash) const;
  111. /// Return memory budget for a resource type.
  112. unsigned GetMemoryBudget(ShortStringHash type) const;
  113. /// Return total memory use for a resource type.
  114. unsigned GetMemoryUse(ShortStringHash type) const;
  115. /// Return total memory use for all resources.
  116. unsigned GetTotalMemoryUse() const;
  117. /// Return resource name from hash, or empty if not found.
  118. const String& GetResourceName(StringHash nameHash) const;
  119. /// Return full absolute file name of resource if possible.
  120. String GetResourceFileName(const String& name) const;
  121. /// Return whether automatic resource reloading is enabled.
  122. bool GetAutoReloadResources() const { return autoReloadResources_; }
  123. /// Return either the path itself or its parent, based on which of them has recognized resource subdirectories.
  124. String GetPreferredResourceDir(const String& path) const;
  125. /// Remove unsupported constructs from the resource name to prevent ambiguity, and normalize absolute filename to resource path relative if possible.
  126. String SanitateResourceName(const String& name) const;
  127. /// Store a hash-to-name mapping.
  128. void StoreNameHash(const String& name);
  129. /// Store a dependency for a resource. If a dependency file changes, the resource will be reloaded.
  130. void StoreResourceDependency(Resource* resource, const String& dependency);
  131. /// Reset dependencies for a resource.
  132. void ResetDependencies(Resource* resource);
  133. private:
  134. /// Find a resource.
  135. const SharedPtr<Resource>& FindResource(ShortStringHash type, StringHash nameHash);
  136. /// Find a resource by name only. Searches all type groups.
  137. const SharedPtr<Resource>& FindResource(StringHash nameHash);
  138. /// Release resources loaded from a package file.
  139. void ReleasePackageResources(PackageFile* package, bool force = false);
  140. /// Update a resource group. Recalculate memory use and release resources if over memory budget.
  141. void UpdateResourceGroup(ShortStringHash type);
  142. /// Handle begin frame event. Automatic resource reloads are processed here.
  143. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  144. /// Resources by type.
  145. HashMap<ShortStringHash, ResourceGroup> resourceGroups_;
  146. /// Resource load directories.
  147. Vector<String> resourceDirs_;
  148. /// File watchers for resource directories, if automatic reloading enabled.
  149. Vector<SharedPtr<FileWatcher> > fileWatchers_;
  150. /// Package files.
  151. Vector<SharedPtr<PackageFile> > packages_;
  152. /// Mapping of hashes to filenames.
  153. HashMap<StringHash, String> hashToName_;
  154. /// Dependent resources.
  155. HashMap<StringHash, HashSet<StringHash> > dependentResources_;
  156. /// Automatic resource reloading flag.
  157. bool autoReloadResources_;
  158. };
  159. template <class T> T* ResourceCache::GetResource(const String& name)
  160. {
  161. ShortStringHash type = T::GetTypeStatic();
  162. return static_cast<T*>(GetResource(type, name));
  163. }
  164. template <class T> T* ResourceCache::GetResource(const char* name)
  165. {
  166. ShortStringHash type = T::GetTypeStatic();
  167. return static_cast<T*>(GetResource(type, name));
  168. }
  169. template <class T> T* ResourceCache::GetResource(StringHash nameHash)
  170. {
  171. ShortStringHash type = T::GetTypeStatic();
  172. return static_cast<T*>(GetResource(type, nameHash));
  173. }
  174. template <class T> void ResourceCache::GetResources(PODVector<T*>& result) const
  175. {
  176. PODVector<Resource*>& resources = reinterpret_cast<PODVector<Resource*>&>(result);
  177. ShortStringHash type = T::GetTypeStatic();
  178. GetResources(resources, type);
  179. // Perform conversion of the returned pointers
  180. for (unsigned i = 0; i < result.Size(); ++i)
  181. {
  182. Resource* resource = resources[i];
  183. result[i] = static_cast<T*>(resource);
  184. }
  185. }
  186. /// Register Resource library subsystems and objects.
  187. void RegisterResourceLibrary(Context* context);
  188. }