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. std::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 path
  53. bool AddResourcePath(const std::string& path);
  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 path
  59. void RemoveResourcePath(const std::string& path);
  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 std::string& fileName, bool ReleaseResources = true, bool forceRelease = false);
  64. /// Release a resource by name
  65. void ReleaseResource(ShortStringHash type, const std::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 std::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 std::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 std::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(std::vector<Resource*>& result, ShortStringHash type) const;
  86. /// Return all loaded resources
  87. const std::map<ShortStringHash, ResourceGroup>& GetAllResources() const { return resourceGroups_; }
  88. /// Return added resource load paths
  89. const std::vector<std::string>& GetResourcePaths() const { return resourcePaths_; }
  90. /// Return added package files
  91. const std::vector<SharedPtr<PackageFile> >& GetPackageFiles() const { return packages_; }
  92. /// Template version of returning a resource by name
  93. template <class T> T* GetResource(const std::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(std::vector<T*>& result) const;
  98. /// Return whether a file exists by name
  99. bool Exists(const std::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 std::string& GetResourceName(StringHash nameHash) const;
  110. /// Return either the path itself or its parent, based on which of them has recognized resource subdirectories
  111. std::string GetPreferredResourcePath(const std::string& path);
  112. private:
  113. /// Find a resource
  114. const SharedPtr<Resource>& FindResource(ShortStringHash type, StringHash nameHash);
  115. /// Release resources loaded from a package file
  116. void ReleasePackageResources(PackageFile* package, bool force = false);
  117. /// Update a resource group. Recalculate memory use and release resources if over memory budget
  118. void UpdateResourceGroup(ShortStringHash type);
  119. /// Store a hash-to-name mapping
  120. void StoreNameHash(const std::string& name);
  121. /// Resources by type
  122. std::map<ShortStringHash, ResourceGroup> resourceGroups_;
  123. /// Resource load paths
  124. std::vector<std::string> resourcePaths_;
  125. /// Package files
  126. std::vector<SharedPtr<PackageFile> > packages_;
  127. /// Mapping of hashes to filenames
  128. std::map<StringHash, std::string> hashToName_;
  129. };
  130. template <class T> T* ResourceCache::GetResource(const std::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(std::vector<T*>& result) const
  141. {
  142. std::vector<Resource*>& resources = reinterpret_cast<std::vector<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);