ResourceCache.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 all resources of a specific type.
  70. void ReleaseResources(ShortStringHash type, bool force = false);
  71. /// Release resources of a specific type and partial name.
  72. void ReleaseResources(ShortStringHash type, const String& partialName, bool force = false);
  73. /// Release resources of all types by partial name.
  74. void ReleaseResources(const String& partialName, bool force = false);
  75. /// Release all resources. When called with the force flag false, releases all currently unused 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 all loaded resources of a specific type.
  90. void GetResources(PODVector<Resource*>& result, ShortStringHash type) const;
  91. /// Return all loaded resources.
  92. const HashMap<ShortStringHash, ResourceGroup>& GetAllResources() const { return resourceGroups_; }
  93. /// Return added resource load directories.
  94. const Vector<String>& GetResourceDirs() const { return resourceDirs_; }
  95. /// Return added package files.
  96. const Vector<SharedPtr<PackageFile> >& GetPackageFiles() const { return packages_; }
  97. /// Template version of returning a resource by name.
  98. template <class T> T* GetResource(const String& name);
  99. /// Template version of returning a resource by name.
  100. template <class T> T* GetResource(const char* name);
  101. /// Template version of returning loaded resources of a specific type.
  102. template <class T> void GetResources(PODVector<T*>& result) const;
  103. /// Return whether a file exists by name.
  104. bool Exists(const String& name) const;
  105. /// Return memory budget for a resource type.
  106. unsigned GetMemoryBudget(ShortStringHash type) const;
  107. /// Return total memory use for a resource type.
  108. unsigned GetMemoryUse(ShortStringHash type) const;
  109. /// Return total memory use for all resources.
  110. unsigned GetTotalMemoryUse() const;
  111. /// Return full absolute file name of resource if possible.
  112. String GetResourceFileName(const String& name) const;
  113. /// Return whether automatic resource reloading is enabled.
  114. bool GetAutoReloadResources() const { return autoReloadResources_; }
  115. /// Return either the path itself or its parent, based on which of them has recognized resource subdirectories.
  116. String GetPreferredResourceDir(const String& path) const;
  117. /// Remove unsupported constructs from the resource name to prevent ambiguity, and normalize absolute filename to resource path relative if possible.
  118. String SanitateResourceName(const String& name) const;
  119. /// Store a dependency for a resource. If a dependency file changes, the resource will be reloaded.
  120. void StoreResourceDependency(Resource* resource, const String& dependency);
  121. /// Reset dependencies for a resource.
  122. void ResetDependencies(Resource* resource);
  123. private:
  124. /// Find a resource.
  125. const SharedPtr<Resource>& FindResource(ShortStringHash type, StringHash nameHash);
  126. /// Find a resource by name only. Searches all type groups.
  127. const SharedPtr<Resource>& FindResource(StringHash nameHash);
  128. /// Release resources loaded from a package file.
  129. void ReleasePackageResources(PackageFile* package, bool force = false);
  130. /// Update a resource group. Recalculate memory use and release resources if over memory budget.
  131. void UpdateResourceGroup(ShortStringHash type);
  132. /// Handle begin frame event. Automatic resource reloads are processed here.
  133. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  134. /// Resources by type.
  135. HashMap<ShortStringHash, ResourceGroup> resourceGroups_;
  136. /// Resource load directories.
  137. Vector<String> resourceDirs_;
  138. /// File watchers for resource directories, if automatic reloading enabled.
  139. Vector<SharedPtr<FileWatcher> > fileWatchers_;
  140. /// Package files.
  141. Vector<SharedPtr<PackageFile> > packages_;
  142. /// Dependent resources.
  143. HashMap<StringHash, HashSet<StringHash> > dependentResources_;
  144. /// Automatic resource reloading flag.
  145. bool autoReloadResources_;
  146. };
  147. template <class T> T* ResourceCache::GetResource(const String& name)
  148. {
  149. ShortStringHash type = T::GetTypeStatic();
  150. return static_cast<T*>(GetResource(type, name));
  151. }
  152. template <class T> T* ResourceCache::GetResource(const char* name)
  153. {
  154. ShortStringHash type = T::GetTypeStatic();
  155. return static_cast<T*>(GetResource(type, name));
  156. }
  157. template <class T> void ResourceCache::GetResources(PODVector<T*>& result) const
  158. {
  159. PODVector<Resource*>& resources = reinterpret_cast<PODVector<Resource*>&>(result);
  160. ShortStringHash type = T::GetTypeStatic();
  161. GetResources(resources, type);
  162. // Perform conversion of the returned pointers
  163. for (unsigned i = 0; i < result.Size(); ++i)
  164. {
  165. Resource* resource = resources[i];
  166. result[i] = static_cast<T*>(resource);
  167. }
  168. }
  169. /// Register Resource library subsystems and objects.
  170. void URHO3D_API RegisterResourceLibrary(Context* context);
  171. }