ResourceCache.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "Resource.h"
  25. namespace Urho3D
  26. {
  27. class FileWatcher;
  28. class PackageFile;
  29. /// Container of resources with specific type.
  30. struct ResourceGroup
  31. {
  32. /// Construct with defaults.
  33. ResourceGroup() :
  34. memoryBudget_(0),
  35. memoryUse_(0)
  36. {
  37. }
  38. /// Memory budget.
  39. unsigned memoryBudget_;
  40. /// Current memory use.
  41. unsigned memoryUse_;
  42. /// Resources.
  43. HashMap<StringHash, SharedPtr<Resource> > resources_;
  44. };
  45. /// %Resource cache subsystem. Loads resources on demand and stores them for later access.
  46. class ResourceCache : public Object
  47. {
  48. OBJECT(ResourceCache);
  49. public:
  50. /// Construct.
  51. ResourceCache(Context* context);
  52. /// Destruct. Free all resources.
  53. virtual ~ResourceCache();
  54. /// Add a resource load directory.
  55. bool AddResourceDir(const String& pathName);
  56. /// Add a package file for loading resources from.
  57. void AddPackageFile(PackageFile* package, bool addAsFirst = false);
  58. /// Add a manually created resource. Must be uniquely named.
  59. bool AddManualResource(Resource* resource);
  60. /// Remove a resource load directory.
  61. void RemoveResourceDir(const String& pathName);
  62. /// Remove a package file. Optionally release the resources loaded from it.
  63. void RemovePackageFile(PackageFile* package, bool releaseResources = true, bool forceRelease = false);
  64. /// Remove a package file by name. Optionally release the resources loaded from it.
  65. void RemovePackageFile(const String& fileName, bool releaseResources = true, bool forceRelease = false);
  66. /// Release a resource by name.
  67. void ReleaseResource(ShortStringHash type, const String& name, bool force = false);
  68. /// Release a resource by name hash.
  69. void ReleaseResource(ShortStringHash type, StringHash nameHash, bool force = false);
  70. /// Release all resources of a specific type.
  71. void ReleaseResources(ShortStringHash type, bool force = false);
  72. /// Release resources of a specific type and partial name.
  73. void ReleaseResources(ShortStringHash type, const String& partialName, bool force = false);
  74. /// Release all resources.
  75. void ReleaseAllResources(bool force = false);
  76. /// Reload a resource. Return false and release it if fails.
  77. bool ReloadResource(Resource* resource);
  78. /// Set memory budget for a specific resource type, default 0 is unlimited.
  79. void SetMemoryBudget(ShortStringHash type, unsigned budget);
  80. /// Enable or disable automatic reloading of resources as files are modified.
  81. void SetAutoReloadResources(bool enable);
  82. /// 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.
  83. SharedPtr<File> GetFile(const String& name);
  84. /// Return a resource by type and name. Load if not loaded yet. Return null if fails.
  85. Resource* GetResource(ShortStringHash type, const String& name);
  86. /// Return a resource by type and name hash. Load if not loaded yet. Return null if fails.
  87. Resource* GetResource(ShortStringHash type, StringHash nameHash);
  88. /// Return all loaded resources of a specific type.
  89. void GetResources(PODVector<Resource*>& result, ShortStringHash type) const;
  90. /// Return all loaded resources.
  91. const HashMap<ShortStringHash, ResourceGroup>& GetAllResources() const { return resourceGroups_; }
  92. /// Return added resource load directories.
  93. const Vector<String>& GetResourceDirs() const { return resourceDirs_; }
  94. /// Return added package files.
  95. const Vector<SharedPtr<PackageFile> >& GetPackageFiles() const { return packages_; }
  96. /// Template version of returning a resource by name.
  97. template <class T> T* GetResource(const String& name);
  98. /// Template version of returning a resource by name hash.
  99. template <class T> T* GetResource(StringHash nameHash);
  100. /// Template version of returning loaded resources of a specific type.
  101. template <class T> void GetResources(PODVector<T*>& result) const;
  102. /// Return whether a file exists by name.
  103. bool Exists(const String& name) const;
  104. /// Return whether a file exists by name hash.
  105. bool Exists(StringHash nameHash) const;
  106. /// Return memory budget for a resource type.
  107. unsigned GetMemoryBudget(ShortStringHash type) const;
  108. /// Return total memory use for a resource type.
  109. unsigned GetMemoryUse(ShortStringHash type) const;
  110. /// Return total memory use for all resources.
  111. unsigned GetTotalMemoryUse() const;
  112. /// Return resource name from hash, or empty if not found.
  113. const String& GetResourceName(StringHash nameHash) const;
  114. /// Return full absolute file name of resource if possible.
  115. String GetResourceFileName(const String& name) const;
  116. /// Return whether automatic resource reloading is enabled.
  117. bool GetAutoReloadResources() const { return autoReloadResources_; }
  118. /// Return either the path itself or its parent, based on which of them has recognized resource subdirectories.
  119. String GetPreferredResourceDir(const String& path) const;
  120. /// Remove unsupported constructs from the resource name to prevent ambiguity.
  121. String SanitateResourceName(const String& name) const;
  122. /// Store a hash-to-name mapping.
  123. void StoreNameHash(const String& name);
  124. private:
  125. /// Find a resource.
  126. const SharedPtr<Resource>& FindResource(ShortStringHash type, StringHash nameHash);
  127. /// Release resources loaded from a package file.
  128. void ReleasePackageResources(PackageFile* package, bool force = false);
  129. /// Update a resource group. Recalculate memory use and release resources if over memory budget.
  130. void UpdateResourceGroup(ShortStringHash type);
  131. /// Handle begin frame event. Automatic resource reloads are processed here.
  132. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  133. /// Resources by type.
  134. HashMap<ShortStringHash, ResourceGroup> resourceGroups_;
  135. /// Resource load directories.
  136. Vector<String> resourceDirs_;
  137. /// File watchers for resource directories, if automatic reloading enabled.
  138. Vector<SharedPtr<FileWatcher> > fileWatchers_;
  139. /// Package files.
  140. Vector<SharedPtr<PackageFile> > packages_;
  141. /// Mapping of hashes to filenames.
  142. HashMap<StringHash, String> hashToName_;
  143. /// Automatic resource reloading flag.
  144. bool autoReloadResources_;
  145. };
  146. template <class T> T* ResourceCache::GetResource(const String& name)
  147. {
  148. ShortStringHash type = T::GetTypeStatic();
  149. return static_cast<T*>(GetResource(type, name));
  150. }
  151. template <class T> T* ResourceCache::GetResource(StringHash nameHash)
  152. {
  153. ShortStringHash type = T::GetTypeStatic();
  154. return static_cast<T*>(GetResource(type, nameHash));
  155. }
  156. template <class T> void ResourceCache::GetResources(PODVector<T*>& result) const
  157. {
  158. PODVector<Resource*>& resources = reinterpret_cast<PODVector<Resource*>&>(result);
  159. ShortStringHash type = T::GetTypeStatic();
  160. GetResources(resources, type);
  161. // Perform conversion of the returned pointers
  162. for (unsigned i = 0; i < result.Size(); ++i)
  163. {
  164. Resource* resource = resources[i];
  165. result[i] = static_cast<T*>(resource);
  166. }
  167. }
  168. /// Register Resource library subsystems and objects.
  169. void RegisterResourceLibrary(Context* context);
  170. }