Resource.h 5.1 KB

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