Resource.h 6.1 KB

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