ResourceCache.h 7.5 KB

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