ResourceCache.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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. #pragma once
  24. #include "File.h"
  25. #include "HashMap.h"
  26. #include "Resource.h"
  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 either the resource load paths or from inside a package file. 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 whether automatic resource reloading is enabled.
  115. bool GetAutoReloadResources() const { return autoReloadResources_; }
  116. /// Return either the path itself or its parent, based on which of them has recognized resource subdirectories.
  117. String GetPreferredResourceDir(const String& path);
  118. /// Remove unsupported constructs from the resource name to prevent ambiguity.
  119. String SanitateResourceName(const String& name);
  120. /// Store a hash-to-name mapping.
  121. void StoreNameHash(const String& name);
  122. private:
  123. /// Find a resource.
  124. const SharedPtr<Resource>& FindResource(ShortStringHash type, StringHash nameHash);
  125. /// Release resources loaded from a package file.
  126. void ReleasePackageResources(PackageFile* package, bool force = false);
  127. /// Update a resource group. Recalculate memory use and release resources if over memory budget.
  128. void UpdateResourceGroup(ShortStringHash type);
  129. /// Handle begin frame event. Automatic resource reloads are processed here.
  130. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  131. /// Resources by type.
  132. HashMap<ShortStringHash, ResourceGroup> resourceGroups_;
  133. /// Resource load directories.
  134. Vector<String> resourceDirs_;
  135. /// File watchers for resource directories, if automatic reloading enabled.
  136. Vector<SharedPtr<FileWatcher> > fileWatchers_;
  137. /// Package files.
  138. Vector<SharedPtr<PackageFile> > packages_;
  139. /// Mapping of hashes to filenames.
  140. HashMap<StringHash, String> hashToName_;
  141. /// Automatic resource reloading flag.
  142. bool autoReloadResources_;
  143. };
  144. template <class T> T* ResourceCache::GetResource(const String& name)
  145. {
  146. ShortStringHash type = T::GetTypeStatic();
  147. return static_cast<T*>(GetResource(type, name));
  148. }
  149. template <class T> T* ResourceCache::GetResource(StringHash nameHash)
  150. {
  151. ShortStringHash type = T::GetTypeStatic();
  152. return static_cast<T*>(GetResource(type, nameHash));
  153. }
  154. template <class T> void ResourceCache::GetResources(PODVector<T*>& result) const
  155. {
  156. PODVector<Resource*>& resources = reinterpret_cast<PODVector<Resource*>&>(result);
  157. ShortStringHash type = T::GetTypeStatic();
  158. GetResources(resources, type);
  159. // Perform conversion of the returned pointers
  160. for (unsigned i = 0; i < result.Size(); ++i)
  161. {
  162. Resource* resource = resources[i];
  163. result[i] = static_cast<T*>(resource);
  164. }
  165. }
  166. /// Register Resource library subsystems and objects.
  167. void RegisterResourceLibrary(Context* context);