Resource.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Copyright (c) 2008-2022 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. #include "../Precompiled.h"
  23. #include "../Core/Profiler.h"
  24. #include "../IO/File.h"
  25. #include "../IO/Log.h"
  26. #include "../Resource/Resource.h"
  27. #include "../Resource/XMLElement.h"
  28. namespace Urho3D
  29. {
  30. Resource::Resource(Context* context) :
  31. Object(context),
  32. memoryUse_(0),
  33. asyncLoadState_(ASYNC_DONE)
  34. {
  35. }
  36. bool Resource::Load(Deserializer& source)
  37. {
  38. // Because BeginLoad() / EndLoad() can be called from worker threads, where profiling would be a no-op,
  39. // create a type name -based profile block here
  40. #ifdef URHO3D_TRACY_PROFILING
  41. URHO3D_PROFILE_COLOR(Load, URHO3D_PROFILE_RESOURCE_COLOR);
  42. String profileBlockName("Load" + GetTypeName());
  43. URHO3D_PROFILE_STR(profileBlockName.CString(), profileBlockName.Length());
  44. #elif defined(URHO3D_PROFILING)
  45. String profileBlockName("Load" + GetTypeName());
  46. auto* profiler = GetSubsystem<Profiler>();
  47. if (profiler)
  48. profiler->BeginBlock(profileBlockName.CString());
  49. #endif
  50. // If we are loading synchronously in a non-main thread, behave as if async loading (for example use
  51. // GetTempResource() instead of GetResource() to load resource dependencies)
  52. SetAsyncLoadState(Thread::IsMainThread() ? ASYNC_DONE : ASYNC_LOADING);
  53. bool success = BeginLoad(source);
  54. if (success)
  55. success &= EndLoad();
  56. SetAsyncLoadState(ASYNC_DONE);
  57. #ifdef URHO3D_PROFILING
  58. if (profiler)
  59. profiler->EndBlock();
  60. #endif
  61. return success;
  62. }
  63. bool Resource::BeginLoad(Deserializer& source)
  64. {
  65. // This always needs to be overridden by subclasses
  66. return false;
  67. }
  68. bool Resource::EndLoad()
  69. {
  70. // If no GPU upload step is necessary, no override is necessary
  71. return true;
  72. }
  73. bool Resource::Save(Serializer& dest) const
  74. {
  75. URHO3D_LOGERROR("Save not supported for " + GetTypeName());
  76. return false;
  77. }
  78. bool Resource::LoadFile(const String& fileName)
  79. {
  80. File file(context_);
  81. return file.Open(fileName, FILE_READ) && Load(file);
  82. }
  83. bool Resource::SaveFile(const String& fileName) const
  84. {
  85. File file(context_);
  86. return file.Open(fileName, FILE_WRITE) && Save(file);
  87. }
  88. void Resource::SetName(const String& name)
  89. {
  90. name_ = name;
  91. nameHash_ = name;
  92. }
  93. void Resource::SetMemoryUse(unsigned size)
  94. {
  95. memoryUse_ = size;
  96. }
  97. void Resource::ResetUseTimer()
  98. {
  99. useTimer_.Reset();
  100. }
  101. void Resource::SetAsyncLoadState(AsyncLoadState newState)
  102. {
  103. asyncLoadState_ = newState;
  104. }
  105. unsigned Resource::GetUseTimer()
  106. {
  107. // If more references than the resource cache, return always 0 & reset the timer
  108. if (Refs() > 1)
  109. {
  110. useTimer_.Reset();
  111. return 0;
  112. }
  113. else
  114. return useTimer_.GetMSec(false);
  115. }
  116. void ResourceWithMetadata::AddMetadata(const String& name, const Variant& value)
  117. {
  118. bool exists;
  119. metadata_.Insert(MakePair(StringHash(name), value), exists);
  120. if (!exists)
  121. metadataKeys_.Push(name);
  122. }
  123. void ResourceWithMetadata::RemoveMetadata(const String& name)
  124. {
  125. metadata_.Erase(name);
  126. metadataKeys_.Remove(name);
  127. }
  128. void ResourceWithMetadata::RemoveAllMetadata()
  129. {
  130. metadata_.Clear();
  131. metadataKeys_.Clear();
  132. }
  133. const Urho3D::Variant& ResourceWithMetadata::GetMetadata(const String& name) const
  134. {
  135. const Variant* value = metadata_[name];
  136. return value ? *value : Variant::EMPTY;
  137. }
  138. bool ResourceWithMetadata::HasMetadata() const
  139. {
  140. return !metadata_.Empty();
  141. }
  142. void ResourceWithMetadata::LoadMetadataFromXML(const XMLElement& source)
  143. {
  144. for (XMLElement elem = source.GetChild("metadata"); elem; elem = elem.GetNext("metadata"))
  145. AddMetadata(elem.GetAttribute("name"), elem.GetVariant());
  146. }
  147. void ResourceWithMetadata::LoadMetadataFromJSON(const JSONArray& array)
  148. {
  149. for (unsigned i = 0; i < array.Size(); i++)
  150. {
  151. const JSONValue& value = array.At(i);
  152. AddMetadata(value.Get("name").GetString(), value.GetVariant());
  153. }
  154. }
  155. void ResourceWithMetadata::SaveMetadataToXML(XMLElement& destination) const
  156. {
  157. for (unsigned i = 0; i < metadataKeys_.Size(); ++i)
  158. {
  159. XMLElement elem = destination.CreateChild("metadata");
  160. elem.SetString("name", metadataKeys_[i]);
  161. elem.SetVariant(GetMetadata(metadataKeys_[i]));
  162. }
  163. }
  164. void ResourceWithMetadata::CopyMetadata(const ResourceWithMetadata& source)
  165. {
  166. metadata_ = source.metadata_;
  167. metadataKeys_ = source.metadataKeys_;
  168. }
  169. }