Resource.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // Copyright (c) 2008-2020 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. /// \file
  23. #pragma once
  24. #include "../Core/Object.h"
  25. #include "../Core/Timer.h"
  26. #include "../Resource/JSONValue.h"
  27. namespace Urho3D
  28. {
  29. class Deserializer;
  30. class Serializer;
  31. class XMLElement;
  32. /// Asynchronous loading state of a resource.
  33. enum AsyncLoadState
  34. {
  35. /// No async operation in progress.
  36. ASYNC_DONE = 0,
  37. /// Queued for asynchronous loading.
  38. ASYNC_QUEUED = 1,
  39. /// In progress of calling BeginLoad() in a worker thread.
  40. ASYNC_LOADING = 2,
  41. /// BeginLoad() succeeded. EndLoad() can be called in the main thread.
  42. ASYNC_SUCCESS = 3,
  43. /// BeginLoad() failed.
  44. ASYNC_FAIL = 4
  45. };
  46. /// Base class for resources.
  47. /// @templateversion
  48. class URHO3D_API Resource : public Object
  49. {
  50. URHO3D_OBJECT(Resource, Object);
  51. public:
  52. /// Construct.
  53. explicit Resource(Context* context);
  54. /// Load resource synchronously. Call both BeginLoad() & EndLoad() and return true if both succeeded.
  55. bool Load(Deserializer& source);
  56. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  57. virtual bool BeginLoad(Deserializer& source);
  58. /// Finish resource loading. Always called from the main thread. Return true if successful.
  59. virtual bool EndLoad();
  60. /// Save resource. Return true if successful.
  61. virtual bool Save(Serializer& dest) const;
  62. /// Load resource from file.
  63. /// @alias{Load}
  64. bool LoadFile(const String& fileName);
  65. /// Save resource to file.
  66. /// @alias{Save}
  67. virtual bool SaveFile(const String& fileName) const;
  68. /// Set name.
  69. /// @property
  70. void SetName(const String& name);
  71. /// Set memory use in bytes, possibly approximate.
  72. void SetMemoryUse(unsigned size);
  73. /// Reset last used timer.
  74. void ResetUseTimer();
  75. /// Set the asynchronous loading state. Called by ResourceCache. Resources in the middle of asynchronous loading are not normally returned to user.
  76. void SetAsyncLoadState(AsyncLoadState newState);
  77. /// Return name.
  78. /// @property
  79. const String& GetName() const { return name_; }
  80. /// Return name hash.
  81. StringHash GetNameHash() const { return nameHash_; }
  82. /// Return memory use in bytes, possibly approximate.
  83. /// @property
  84. unsigned GetMemoryUse() const { return memoryUse_; }
  85. /// Return time since last use in milliseconds. If referred to elsewhere than in the resource cache, returns always zero.
  86. /// @property
  87. unsigned GetUseTimer();
  88. /// Return the asynchronous loading state.
  89. AsyncLoadState GetAsyncLoadState() const { return asyncLoadState_; }
  90. private:
  91. /// Name.
  92. String name_;
  93. /// Name hash.
  94. StringHash nameHash_;
  95. /// Last used timer.
  96. Timer useTimer_;
  97. /// Memory use in bytes.
  98. unsigned memoryUse_;
  99. /// Asynchronous loading state.
  100. AsyncLoadState asyncLoadState_;
  101. };
  102. /// Base class for resources that support arbitrary metadata stored. Metadata serialization shall be implemented in derived classes.
  103. class URHO3D_API ResourceWithMetadata : public Resource
  104. {
  105. URHO3D_OBJECT(ResourceWithMetadata, Resource);
  106. public:
  107. /// Construct.
  108. explicit ResourceWithMetadata(Context* context) : Resource(context) {}
  109. /// Add new metadata variable or overwrite old value.
  110. /// @property{set_metadata}
  111. void AddMetadata(const String& name, const Variant& value);
  112. /// Remove metadata variable.
  113. void RemoveMetadata(const String& name);
  114. /// Remove all metadata variables.
  115. void RemoveAllMetadata();
  116. /// Return metadata variable.
  117. /// @property
  118. const Variant& GetMetadata(const String& name) const;
  119. /// Return whether the resource has metadata.
  120. /// @property
  121. bool HasMetadata() const;
  122. protected:
  123. /// Load metadata from <metadata> children of XML element.
  124. void LoadMetadataFromXML(const XMLElement& source);
  125. /// Load metadata from JSON array.
  126. void LoadMetadataFromJSON(const JSONArray& array);
  127. /// Save as <metadata> children of XML element.
  128. void SaveMetadataToXML(XMLElement& destination) const;
  129. /// Copy metadata from another resource.
  130. void CopyMetadata(const ResourceWithMetadata& source);
  131. private:
  132. /// Animation metadata variables.
  133. VariantMap metadata_;
  134. /// Animation metadata keys.
  135. StringVector metadataKeys_;
  136. };
  137. inline const String& GetResourceName(Resource* resource)
  138. {
  139. return resource ? resource->GetName() : String::EMPTY;
  140. }
  141. inline StringHash GetResourceType(Resource* resource, StringHash defaultType)
  142. {
  143. return resource ? resource->GetType() : defaultType;
  144. }
  145. inline ResourceRef GetResourceRef(Resource* resource, StringHash defaultType)
  146. {
  147. return ResourceRef(GetResourceType(resource, defaultType), GetResourceName(resource));
  148. }
  149. template <class T> Vector<String> GetResourceNames(const Vector<SharedPtr<T> >& resources)
  150. {
  151. Vector<String> ret(resources.Size());
  152. for (unsigned i = 0; i < resources.Size(); ++i)
  153. ret[i] = GetResourceName(resources[i]);
  154. return ret;
  155. }
  156. template <class T> ResourceRefList GetResourceRefList(const Vector<SharedPtr<T> >& resources)
  157. {
  158. return ResourceRefList(T::GetTypeStatic(), GetResourceNames(resources));
  159. }
  160. }