Resource.cpp 5.3 KB

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