Resource.cpp 5.2 KB

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