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