-
Notifications
You must be signed in to change notification settings - Fork 130
/
Serializable.cpp
665 lines (551 loc) · 19 KB
/
Serializable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//
// Copyright (c) 2008-2022 the Urho3D project.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include "../Precompiled.h"
#include "../Core/Context.h"
#include "../IO/Archive.h"
#include "../IO/ArchiveSerialization.h"
#include "../IO/Deserializer.h"
#include "../IO/FileSystem.h"
#include "../IO/Log.h"
#include "../IO/Serializer.h"
#include "../Resource/XMLElement.h"
#include "../Resource/XMLFile.h"
#include "../Resource/JSONFile.h"
#include "../Resource/JSONValue.h"
#include "../Resource/ResourceCache.h"
#include "../Scene/SceneEvents.h"
#include "../Scene/NodePrefab.h"
#include "../Scene/Serializable.h"
#include <EASTL/fixed_vector.h>
#include "../DebugNew.h"
namespace Urho3D
{
static const unsigned MAX_STACK_ATTRIBUTE_COUNT = 128;
static unsigned RemapAttributeIndex(const ea::vector<AttributeInfo>* attributes, const AttributeInfo& netAttr, unsigned netAttrIndex)
{
if (!attributes)
return netAttrIndex; // Could not remap
for (unsigned i = 0; i < attributes->size(); ++i)
{
const AttributeInfo& attr = attributes->at(i);
// Compare accessor to avoid name string compare
if (attr.accessor_ && attr.accessor_ == netAttr.accessor_.Get())
return i;
}
return netAttrIndex; // Could not remap
}
Serializable::Serializable(Context* context) :
Object(context),
setInstanceDefault_(false),
temporary_(false)
{
}
Serializable::~Serializable() = default;
void Serializable::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
{
// TODO: may be could use an observer pattern here
if (setInstanceDefault_)
SetInstanceDefault(attr.name_, src);
attr.accessor_->Set(this, src);
}
void Serializable::OnGetAttribute(const AttributeInfo& attr, Variant& dest) const
{
attr.accessor_->Get(this, dest);
}
ObjectReflection* Serializable::GetReflection() const
{
return context_->GetReflection(GetType());
}
const ea::vector<AttributeInfo>* Serializable::GetAttributes() const
{
return context_->GetAttributes(GetType());
}
bool Serializable::Load(Deserializer& source)
{
const ea::vector<AttributeInfo>* attributes = GetAttributes();
if (!attributes)
return true;
for (unsigned i = 0; i < attributes->size(); ++i)
{
const AttributeInfo& attr = attributes->at(i);
if (!attr.ShouldLoad())
continue;
if (source.IsEof())
{
URHO3D_LOGERROR("Could not load " + GetTypeName() + ", stream not open or at end");
return false;
}
Variant varValue = source.ReadVariant(attr.type_, context_);
OnSetAttribute(attr, varValue);
}
return true;
}
bool Serializable::Save(Serializer& dest) const
{
const ea::vector<AttributeInfo>* attributes = GetAttributes();
if (!attributes)
return true;
Variant value;
for (unsigned i = 0; i < attributes->size(); ++i)
{
const AttributeInfo& attr = attributes->at(i);
if (!attr.ShouldSave())
continue;
OnGetAttribute(attr, value);
if (!dest.WriteVariantData(value))
{
URHO3D_LOGERROR("Could not save " + GetTypeName() + ", writing to stream failed");
return false;
}
}
return true;
}
bool Serializable::LoadXML(const XMLElement& source)
{
if (source.IsNull())
{
URHO3D_LOGERROR("Could not load " + GetTypeName() + ", null source element");
return false;
}
const ea::vector<AttributeInfo>* attributes = GetAttributes();
if (!attributes)
return true;
XMLElement attrElem = source.GetChild("attribute");
unsigned startIndex = 0;
while (attrElem)
{
ea::string name = attrElem.GetAttribute("name");
unsigned i = startIndex;
unsigned attempts = attributes->size();
while (attempts)
{
const AttributeInfo& attr = attributes->at(i);
if (attr.ShouldLoad() && !attr.name_.compare(name))
{
Variant varValue;
// If enums specified, do enum lookup and int assignment. Otherwise assign the variant directly
if (!attr.enumNames_.empty() && attr.type_ == VAR_INT)
{
ea::string value = attrElem.GetAttribute("value");
const unsigned enumValue = attr.ConvertEnumToUInt(value);
if (enumValue != M_MAX_UNSIGNED)
varValue = enumValue;
else
URHO3D_LOGWARNING("Unknown enum value " + value + " in attribute " + attr.name_);
}
else
varValue = attrElem.GetVariantValue(attr.type_, context_);
if (!varValue.IsEmpty())
OnSetAttribute(attr, varValue);
startIndex = (i + 1) % attributes->size();
break;
}
else
{
i = (i + 1) % attributes->size();
--attempts;
}
}
if (!attempts)
URHO3D_LOGWARNING("Unknown attribute " + name + " in XML data");
attrElem = attrElem.GetNext("attribute");
}
return true;
}
bool Serializable::LoadJSON(const JSONValue& source)
{
if (source.IsNull())
{
URHO3D_LOGERROR("Could not load " + GetTypeName() + ", null JSON source element");
return false;
}
const ea::vector<AttributeInfo>* attributes = GetAttributes();
if (!attributes)
return true;
// Get attributes value
JSONValue attributesValue = source.Get("attributes");
if (attributesValue.IsNull())
return true;
// Warn if the attributes value isn't an object
if (!attributesValue.IsObject())
{
URHO3D_LOGWARNING("'attributes' object is present in " + GetTypeName() + " but is not a JSON object; skipping load");
return true;
}
const JSONObject& attributesObject = attributesValue.GetObject();
for (const AttributeInfo& attr : *attributes)
{
if (attr.ShouldLoad())
{
const JSONValue& value = attributesValue[attr.name_];
if (value.GetValueType() == JSON_NULL)
continue;
Variant varValue;
// If enums specified, do enum lookup ad int assignment. Otherwise assign variant directly
if (!attr.enumNames_.empty() && attr.type_ == VAR_INT)
{
const ea::string& valueStr = value.GetString();
const unsigned enumValue = attr.ConvertEnumToUInt(valueStr);
if (enumValue != M_MAX_UNSIGNED)
varValue = enumValue;
else
URHO3D_LOGWARNING("Unknown enum value " + valueStr + " in attribute " + attr.name_);
}
else
varValue = value.GetVariantValue(attr.type_, context_);
if (!varValue.IsEmpty())
OnSetAttribute(attr, varValue);
}
}
// Report missing attributes.
for (const auto& pair : attributesObject)
{
bool found = false;
for (int i = 0; i < attributes->size() && !found; i++)
found |= attributes->at(i).name_ == pair.first;
if (!found)
URHO3D_LOGWARNING("Unknown attribute {} in JSON data", pair.first);
}
return true;
}
bool Serializable::SaveXML(XMLElement& dest) const
{
if (dest.IsNull())
{
URHO3D_LOGERROR("Could not save " + GetTypeName() + ", null destination element");
return false;
}
const ea::vector<AttributeInfo>* attributes = GetAttributes();
if (!attributes)
return true;
Variant value;
for (unsigned i = 0; i < attributes->size(); ++i)
{
const AttributeInfo& attr = attributes->at(i);
if (!attr.ShouldSave())
continue;
OnGetAttribute(attr, value);
Variant defaultValue(GetAttributeDefault(i));
// In XML serialization default values can be skipped. This will make the file easier to read or edit manually
if (value == defaultValue && !SaveDefaultAttributes(attr))
continue;
XMLElement attrElem = dest.CreateChild("attribute");
attrElem.SetAttribute("name", attr.name_);
// If enums specified, set as an enum string. Otherwise set directly as a Variant
if (!attr.enumNames_.empty() && attr.type_ == VAR_INT)
{
int enumValue = value.GetInt();
attrElem.SetAttribute("value", attr.enumNames_[enumValue]);
}
else
attrElem.SetVariantValue(value);
}
return true;
}
bool Serializable::SaveJSON(JSONValue& dest) const
{
const ea::vector<AttributeInfo>* attributes = GetAttributes();
if (!attributes)
return true;
Variant value;
JSONValue attributesValue;
for (unsigned i = 0; i < attributes->size(); ++i)
{
const AttributeInfo& attr = attributes->at(i);
if (!attr.ShouldSave())
continue;
OnGetAttribute(attr, value);
Variant defaultValue(GetAttributeDefault(i));
// In JSON serialization default values can be skipped. This will make the file easier to read or edit manually
if (value == defaultValue && !SaveDefaultAttributes(attr))
continue;
JSONValue attrVal;
// If enums specified, set as an enum string. Otherwise set directly as a Variant
if (!attr.enumNames_.empty() && attr.type_ == VAR_INT)
{
int enumValue = value.GetInt();
attrVal = attr.enumNames_[enumValue];
}
else
attrVal.SetVariantValue(value, context_);
attributesValue.Set(attr.name_, attrVal);
}
dest.Set("attributes", attributesValue);
return true;
}
bool Serializable::Load(const ea::string& resourceName)
{
AbstractFilePtr file(GetSubsystem<ResourceCache>()->GetFile(resourceName, false));
if (file)
return Load(*file);
return false;
}
bool Serializable::LoadXML(const ea::string& resourceName)
{
SharedPtr<XMLFile> file(GetSubsystem<ResourceCache>()->GetResource<XMLFile>(resourceName, false));
if (file)
return LoadXML(file->GetRoot());
return false;
}
bool Serializable::LoadJSON(const ea::string& resourceName)
{
SharedPtr<JSONFile> file(GetSubsystem<ResourceCache>()->GetResource<JSONFile>(resourceName, false));
if (file)
return LoadJSON(file->GetRoot());
return false;
}
bool Serializable::LoadFile(const ea::string& resourceName)
{
ResourceCache* cache = GetSubsystem<ResourceCache>();
// Router may redirect to different file.
auto realResourceId = FileIdentifier::FromUri(resourceName);
cache->RouteResourceName(realResourceId);
const ea::string realResourceName = realResourceId.ToUri();
ea::string extension = GetExtension(realResourceName);
if (extension == ".xml")
return LoadXML(realResourceName);
if (extension == ".json")
return LoadJSON(realResourceName);
return Load(realResourceName);
}
void Serializable::SerializeInBlock(Archive& archive)
{
SerializeInBlock(archive, false);
}
void Serializable::SerializeInBlock(Archive& archive, bool serializeTemporary)
{
const bool compactSave = !archive.IsHumanReadable();
const PrefabArchiveFlags flags = PrefabArchiveFlag::IgnoreSerializableId | PrefabArchiveFlag::IgnoreSerializableType
| (serializeTemporary ? PrefabArchiveFlag::SerializeTemporary : PrefabArchiveFlag::None);
SerializablePrefab prefab;
if (!archive.IsInput())
prefab.Import(this, serializeTemporary ? PrefabSaveFlag::SaveTemporary : PrefabSaveFlag::None);
prefab.SerializeInBlock(archive, flags, compactSave);
if (archive.IsInput())
prefab.Export(this);
ApplyAttributes();
}
bool Serializable::SetAttribute(unsigned index, const Variant& value)
{
const ea::vector<AttributeInfo>* attributes = GetAttributes();
if (!attributes)
{
URHO3D_LOGERROR(GetTypeName() + " has no attributes");
return false;
}
if (index >= attributes->size())
{
URHO3D_LOGERROR("Attribute index out of bounds");
return false;
}
const AttributeInfo& attr = attributes->at(index);
// Check that the new value's type matches the attribute type
if (value.GetType() == attr.type_)
{
OnSetAttribute(attr, value);
return true;
}
else
{
URHO3D_LOGERROR("Could not set attribute " + attr.name_ + ": expected type " + Variant::GetTypeName(attr.type_) +
" but got " + value.GetTypeName());
return false;
}
}
bool Serializable::SetAttribute(const ea::string& name, const Variant& value)
{
const ea::vector<AttributeInfo>* attributes = GetAttributes();
if (!attributes)
{
URHO3D_LOGERROR(GetTypeName() + " has no attributes");
return false;
}
for (auto i = attributes->begin(); i != attributes->end(); ++i)
{
if (!i->name_.compare(name))
{
// Check that the new value's type matches the attribute type
if (value.GetType() == i->type_)
{
OnSetAttribute(*i, value);
return true;
}
else
{
URHO3D_LOGERROR("Could not set attribute " + i->name_ + ": expected type " + Variant::GetTypeName(i->type_)
+ " but got " + value.GetTypeName());
return false;
}
}
}
URHO3D_LOGERROR("Could not find attribute " + name + " in " + GetTypeName());
return false;
}
void Serializable::ResetToDefault()
{
const ea::vector<AttributeInfo>* attributes = GetAttributes();
if (!attributes)
return;
for (unsigned i = 0; i < attributes->size(); ++i)
{
const AttributeInfo& attr = attributes->at(i);
if (attr.mode_ & (AM_NOEDIT | AM_NODEID | AM_COMPONENTID | AM_NODEIDVECTOR))
continue;
Variant defaultValue = GetInstanceDefault(attr.name_);
if (defaultValue.IsEmpty())
defaultValue = attr.defaultValue_;
OnSetAttribute(attr, defaultValue);
}
}
void Serializable::RemoveInstanceDefault()
{
instanceDefaultValues_.reset();
}
void Serializable::SetTemporary(bool enable)
{
if (enable != temporary_)
{
temporary_ = enable;
using namespace TemporaryChanged;
VariantMap& eventData = GetEventDataMap();
eventData[P_SERIALIZABLE] = this;
SendEvent(E_TEMPORARYCHANGED, eventData);
}
}
Variant Serializable::GetAttribute(unsigned index) const
{
Variant ret;
const ea::vector<AttributeInfo>* attributes = GetAttributes();
if (!attributes)
{
URHO3D_LOGERROR(GetTypeName() + " has no attributes");
return ret;
}
if (index >= attributes->size())
{
URHO3D_LOGERROR("Attribute index out of bounds");
return ret;
}
OnGetAttribute(attributes->at(index), ret);
return ret;
}
Variant Serializable::GetAttribute(const ea::string& name) const
{
Variant ret;
const ea::vector<AttributeInfo>* attributes = GetAttributes();
if (!attributes)
{
URHO3D_LOGERROR(GetTypeName() + " has no attributes");
return ret;
}
for (auto i = attributes->begin(); i != attributes->end(); ++i)
{
if (!i->name_.compare(name))
{
OnGetAttribute(*i, ret);
return ret;
}
}
URHO3D_LOGERROR("Could not find attribute " + name + " in " + GetTypeName());
return ret;
}
Variant Serializable::GetAttributeDefault(unsigned index) const
{
const ea::vector<AttributeInfo>* attributes = GetAttributes();
if (!attributes)
{
URHO3D_LOGERROR(GetTypeName() + " has no attributes");
return Variant::EMPTY;
}
if (index >= attributes->size())
{
URHO3D_LOGERROR("Attribute index out of bounds");
return Variant::EMPTY;
}
AttributeInfo attr = attributes->at(index);
Variant defaultValue = GetInstanceDefault(attr.name_);
return defaultValue.IsEmpty() ? attr.defaultValue_ : defaultValue;
}
Variant Serializable::GetAttributeDefault(const ea::string& name) const
{
Variant defaultValue = GetInstanceDefault(name);
if (!defaultValue.IsEmpty())
return defaultValue;
const ea::vector<AttributeInfo>* attributes = GetAttributes();
if (!attributes)
{
URHO3D_LOGERROR(GetTypeName() + " has no attributes");
return Variant::EMPTY;
}
for (auto i = attributes->begin(); i != attributes->end(); ++i)
{
if (!i->name_.compare(name))
return i->defaultValue_;
}
URHO3D_LOGERROR("Could not find attribute " + name + " in " + GetTypeName());
return Variant::EMPTY;
}
unsigned Serializable::GetNumAttributes() const
{
const ea::vector<AttributeInfo>* attributes = GetAttributes();
return attributes ? attributes->size() : 0;
}
void Serializable::CopyAttributes(const Serializable* source, bool resetToDefault)
{
URHO3D_ASSERT(source);
if (resetToDefault)
ResetToDefault();
const auto* sourceAttributes = source->GetAttributes();
const auto* destAttributes = GetAttributes();
if (!sourceAttributes || !destAttributes)
return;
if (sourceAttributes != destAttributes)
{
URHO3D_LOGERROR("Source and destination objects have different types");
return;
}
for (unsigned i = 0; i < sourceAttributes->size(); ++i)
{
const AttributeInfo& attr = sourceAttributes->at(i);
Variant value;
source->OnGetAttribute(attr, value);
OnSetAttribute(attr, value);
}
}
void Serializable::SetInstanceDefault(const ea::string& name, const Variant& defaultValue)
{
// Allocate the instance level default value
if (!instanceDefaultValues_)
instanceDefaultValues_ = ea::make_unique<VariantMap>();
instanceDefaultValues_->operator [](name) = defaultValue;
}
Variant Serializable::GetInstanceDefault(const ea::string& name) const
{
if (instanceDefaultValues_)
{
auto i = instanceDefaultValues_->find(name);
if (i != instanceDefaultValues_->end())
return i->second;
}
return Variant::EMPTY;
}
}