forked from dheerendra1/dava.framework
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathKeyedArchive.cpp
685 lines (591 loc) · 17.8 KB
/
KeyedArchive.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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
#include "FileSystem/KeyedArchive.h"
#include "FileSystem/File.h"
#include "FileSystem/DynamicMemoryFile.h"
#include "FileSystem/UnmanagedMemoryFile.h"
#include "FileSystem/YamlParser.h"
#include "FileSystem/YamlNode.h"
#include "FileSystem/VariantType.h"
#include "FileSystem/YamlEmitter.h"
#include "FileSystem/Private/KeyedArchiveReflection.h"
#include "Reflection/ReflectionRegistrator.h"
#include "Logger/Logger.h"
namespace DAVA
{
VariantType PrepareValueForKeyedArchive(const Any& value, VariantType::eVariantType resultType)
{
return PrepareValueForKeyedArchiveImpl(value, resultType);
}
DAVA_VIRTUAL_REFLECTION_IMPL(KeyedArchive)
{
ReflectionRegistrator<KeyedArchive>::Begin(std::make_unique<KeyedArchiveStructureWrapper>())
.End();
}
KeyedArchive::KeyedArchive()
{
}
KeyedArchive::KeyedArchive(const KeyedArchive& arc)
{
for (const auto& obj : arc.GetArchieveData())
{
SetVariant(obj.first, *obj.second);
}
}
KeyedArchive& KeyedArchive::operator=(const KeyedArchive& arc)
{
if (this != &arc)
{
DeleteAllKeys();
for (const auto& obj : arc.GetArchieveData())
{
SetVariant(obj.first, *obj.second);
}
}
return *this;
}
KeyedArchive::~KeyedArchive()
{
DeleteAllKeys();
}
bool KeyedArchive::Load(const FilePath& pathName)
{
File* archive = File::Create(pathName, File::OPEN | File::READ);
if (nullptr == archive)
{
return false;
}
bool ret = Load(archive);
SafeRelease(archive);
return ret;
}
bool KeyedArchive::Load(File* archive)
{
DAVA::Array<char, 2> header;
uint32 wasRead = archive->Read(header.data(), 2);
if (wasRead != 2)
{
Logger::Error("[KeyedArchive] error loading keyed archive from file: %s, filesize: %d", archive->GetFilename().GetAbsolutePathname().c_str(), archive->GetSize());
return false;
}
else if ((header[0] != 'K') || (header[1] != 'A'))
{
const bool seekResult = archive->Seek(0, File::SEEK_FROM_START);
if (!seekResult)
{
Logger::Error("[KeyedArchive] seek failed from file: %s, filesize: %d", archive->GetFilename().GetAbsolutePathname().c_str(), archive->GetSize());
return false;
}
while (!archive->IsEof())
{
VariantType key;
if (archive->IsEof())
{
break;
}
if (!key.Read(archive))
{
return false;
}
VariantType value;
if (!value.Read(archive))
{
return false;
}
SetVariant(key.AsString(), std::move(value));
}
return true;
}
uint16 version = 0;
if (2 != archive->Read(&version, 2))
{
return false;
}
if (version != 1)
{
Logger::Error("[KeyedArchive] error loading keyed archive, because version is incorrect");
return false;
}
uint32 numberOfItems = 0;
if (4 != archive->Read(&numberOfItems, 4))
{
return false;
}
for (uint32 item = 0; item < numberOfItems; ++item)
{
VariantType key;
if (archive->IsEof())
{
break;
}
if (!key.Read(archive))
{
return false;
}
VariantType value;
if (!value.Read(archive))
{
return false;
}
SetVariant(key.AsString(), std::move(value));
}
return true;
}
bool KeyedArchive::Save(const FilePath& pathName) const
{
File* archive = File::Create(pathName, File::CREATE | File::WRITE);
if (nullptr == archive)
{
return false;
}
bool ret = Save(archive);
SafeRelease(archive);
return ret;
}
bool KeyedArchive::Save(File* archive) const
{
Map<UnderlyingMap::key_type, UnderlyingMap::mapped_type> orderedMap;
orderedMap.insert(objectMap.begin(), objectMap.end());
Array<char, 2> header;
uint16 version = 1;
uint32 size = static_cast<uint32>(orderedMap.size());
header[0] = 'K';
header[1] = 'A';
if (2 != archive->Write(header.data(), 2)
|| 2 != archive->Write(&version, 2)
|| 4 != archive->Write(&size, 4))
{
return false;
}
for (const auto& obj : orderedMap)
{
VariantType key;
key.SetString(obj.first);
if (!key.Write(archive)
|| !obj.second->Write(archive))
{
return false;
}
}
return archive->Flush();
}
uint32 KeyedArchive::Save(uint8* data, uint32 size) const
{
ScopedPtr<DynamicMemoryFile> buffer(DynamicMemoryFile::Create(File::CREATE | File::WRITE));
if (!Save(buffer))
{
Logger::Error("failed to write KeyedArchive to memory_file");
}
auto archieveSize = buffer->GetSize();
if ((nullptr != data) && (size >= archieveSize))
{ // if data is null, we just return requested size for data
Memcpy(data, buffer->GetData(), static_cast<size_t>(archieveSize));
}
return static_cast<uint32>(archieveSize);
}
bool KeyedArchive::Load(const uint8* data, uint32 size)
{
if (nullptr == data || 0 == size)
{
return false;
}
ScopedPtr<DynamicMemoryFile> buffer(DynamicMemoryFile::Create(File::CREATE | File::WRITE | File::READ));
auto written = buffer->Write(data, size);
DVASSERT(written == size);
buffer->Seek(0, File::SEEK_FROM_START);
return Load(buffer);
}
bool KeyedArchive::LoadFromYamlFile(const FilePath& pathName)
{
RefPtr<YamlParser> parser(YamlParser::Create(pathName));
return (parser.Get() != nullptr) && LoadFromYamlNode(parser->GetRootNode());
}
bool KeyedArchive::LoadFromYamlNode(const YamlNode* rootNode)
{
if (nullptr == rootNode)
{
return false;
}
const YamlNode* archieveNode = rootNode->Get(VariantType::TYPENAME_KEYED_ARCHIVE);
if (nullptr == archieveNode)
{
return false;
}
int32 count = archieveNode->GetCount();
for (int32 i = 0; i < count; ++i)
{
const YamlNode* node = archieveNode->Get(i);
const String& variableNameToArchMap = archieveNode->GetItemKeyName(i);
VariantType value(node->AsVariantType());
if (value.GetType() == VariantType::TYPE_NONE)
{
continue;
}
SetVariant(variableNameToArchMap, std::move(value));
}
return true;
}
bool KeyedArchive::SaveToYamlFile(const FilePath& pathName) const
{
RefPtr<YamlNode> node(YamlNode::CreateMapNode());
node->Set(VariantType::TYPENAME_KEYED_ARCHIVE, VariantType(const_cast<KeyedArchive*>(this)));
return YamlEmitter::SaveToYamlFile(pathName, node.Get());
}
void KeyedArchive::SetBool(const String& key, bool value)
{
SetVariant(key, value, &VariantType::SetBool);
}
void KeyedArchive::SetInt32(const String& key, int32 value)
{
SetVariant(key, value, &VariantType::SetInt32);
}
void KeyedArchive::SetUInt32(const String& key, uint32 value)
{
SetVariant(key, value, &VariantType::SetUInt32);
}
void KeyedArchive::SetFloat(const String& key, float32 value)
{
SetVariant(key, value, &VariantType::SetFloat);
}
void KeyedArchive::SetFloat64(const String& key, float64 value)
{
SetVariant(key, value, &VariantType::SetFloat64);
}
void KeyedArchive::SetString(const String& key, const String& value)
{
SetVariant(key, value, &VariantType::SetString);
}
void KeyedArchive::SetWideString(const String& key, const WideString& value)
{
SetVariant(key, value, &VariantType::SetWideString);
}
void KeyedArchive::SetFastName(const String& key, const FastName& value)
{
SetVariant(key, value, &VariantType::SetFastName);
}
void KeyedArchive::SetByteArray(const String& key, const uint8* value, int32 arraySize)
{
auto iter = objectMap.find(key);
if (iter != objectMap.end())
{
(iter->second->SetByteArray)(value, arraySize);
}
else
{
objectMap[key] = new VariantType(value, arraySize);
}
}
void KeyedArchive::SetVariant(const String& key, const VariantType& value)
{
auto iter = objectMap.find(key);
if (iter != objectMap.end())
{
*iter->second = value;
}
else
{
objectMap[key] = new VariantType(value);
}
}
void KeyedArchive::SetVariant(const String& key, VariantType&& value)
{
auto iter = objectMap.find(key);
if (iter != objectMap.end())
{
*iter->second = std::move(value);
}
else
{
objectMap[key] = new VariantType(std::move(value));
}
}
void KeyedArchive::SetByteArrayFromArchive(const String& key, KeyedArchive* archive)
{
//DVWARNING(false, "Method is depriceted! Use SetArchive()");
DynamicMemoryFile* file = DynamicMemoryFile::Create(File::CREATE | File::WRITE);
if (!archive->Save(file))
{
Logger::Error("failed to write KeyedArchive to memory_file");
}
SetByteArray(key, file->GetData(), static_cast<uint32>(file->GetSize()));
SafeRelease(file);
}
void KeyedArchive::SetArchive(const String& key, KeyedArchive* archive)
{
SetVariant(key, archive, &VariantType::SetKeyedArchive);
}
void KeyedArchive::SetInt64(const String& key, const int64& value)
{
SetVariant(key, value, &VariantType::SetInt64);
}
void KeyedArchive::SetUInt64(const String& key, const uint64& value)
{
SetVariant(key, value, &VariantType::SetUInt64);
}
void KeyedArchive::SetVector2(const String& key, const Vector2& value)
{
SetVariant(key, value, &VariantType::SetVector2);
}
void KeyedArchive::SetVector3(const String& key, const Vector3& value)
{
SetVariant(key, value, &VariantType::SetVector3);
}
void KeyedArchive::SetVector4(const String& key, const Vector4& value)
{
SetVariant(key, value, &VariantType::SetVector4);
}
void KeyedArchive::SetMatrix2(const String& key, const Matrix2& value)
{
SetVariant(key, value, &VariantType::SetMatrix2);
}
void KeyedArchive::SetMatrix3(const String& key, const Matrix3& value)
{
SetVariant(key, value, &VariantType::SetMatrix3);
}
void KeyedArchive::SetMatrix4(const String& key, const Matrix4& value)
{
SetVariant(key, value, &VariantType::SetMatrix4);
}
void KeyedArchive::SetColor(const String& key, const Color& value)
{
SetVariant(key, value, &VariantType::SetColor);
}
bool KeyedArchive::IsKeyExists(const String& key) const
{
auto it = objectMap.find(key);
if (it != objectMap.end())
{
return true;
}
return false;
}
bool KeyedArchive::GetBool(const String& key, bool defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsBool() : defaultValue;
}
int32 KeyedArchive::GetInt32(const String& key, int32 defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsInt32() : defaultValue;
}
uint32 KeyedArchive::GetUInt32(const String& key, uint32 defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsUInt32() : defaultValue;
}
float32 KeyedArchive::GetFloat(const String& key, float32 defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsFloat() : defaultValue;
}
float64 KeyedArchive::GetFloat64(const String& key, float64 defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsFloat64() : defaultValue;
}
String KeyedArchive::GetString(const String& key, const String& defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsString() : defaultValue;
}
WideString KeyedArchive::GetWideString(const String& key, const WideString& defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsWideString() : defaultValue;
}
FastName KeyedArchive::GetFastName(const String& key, const FastName& defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsFastName() : defaultValue;
}
const uint8* KeyedArchive::GetByteArray(const String& key, const uint8* defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsByteArray() : defaultValue;
}
int32 KeyedArchive::GetByteArraySize(const String& key, int32 defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsByteArraySize() : defaultValue;
}
KeyedArchive* KeyedArchive::GetArchiveFromByteArray(const String& key) const
{
//DVWARNING(false, "Method is depriceted! Use GetArchive()");
int32 size = GetByteArraySize(key);
if (size == 0)
{
return nullptr;
}
KeyedArchive* archive = new KeyedArchive;
ScopedPtr<UnmanagedMemoryFile> file(new UnmanagedMemoryFile(GetByteArray(key), size));
if (!archive->Load(file))
{
SafeRelease(archive);
return nullptr;
}
return archive;
}
KeyedArchive* KeyedArchive::GetArchive(const String& key, KeyedArchive* defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsKeyedArchive() : defaultValue;
}
VariantType* KeyedArchive::GetVariant(const String& key) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second : nullptr;
}
int64 KeyedArchive::GetInt64(const String& key, int64 defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsInt64() : defaultValue;
}
uint64 KeyedArchive::GetUInt64(const String& key, uint64 defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsUInt64() : defaultValue;
}
Vector2 KeyedArchive::GetVector2(const String& key, const Vector2& defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsVector2() : defaultValue;
}
Vector3 KeyedArchive::GetVector3(const String& key, const Vector3& defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsVector3() : defaultValue;
}
Vector4 KeyedArchive::GetVector4(const String& key, const Vector4& defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsVector4() : defaultValue;
}
Matrix2 KeyedArchive::GetMatrix2(const String& key, const Matrix2& defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsMatrix2() : defaultValue;
}
Matrix3 KeyedArchive::GetMatrix3(const String& key, const Matrix3& defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsMatrix3() : defaultValue;
}
Matrix4 KeyedArchive::GetMatrix4(const String& key, const Matrix4& defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsMatrix4() : defaultValue;
}
Color KeyedArchive::GetColor(const String& key, const Color& defaultValue) const
{
auto it = objectMap.find(key);
return it != objectMap.end() ? it->second->AsColor() : defaultValue;
}
void KeyedArchive::DeleteKey(const String& key)
{
auto it = objectMap.find(key);
if (it != objectMap.end())
{
delete it->second;
objectMap.erase(key);
}
}
void KeyedArchive::DeleteAllKeys()
{
for (const auto& obj : objectMap)
{
delete obj.second;
}
objectMap.clear();
}
uint32 KeyedArchive::Count(const String& key) const
{
if (key.empty())
{
return static_cast<uint32>(objectMap.size());
}
else
{
return static_cast<uint32>(objectMap.count(key));
}
}
void KeyedArchive::Dump() const
{
Logger::FrameworkDebug("============================================================");
Logger::FrameworkDebug("--------------- Archive Currently contain ----------------");
for (const auto& obj : objectMap)
{
switch (obj.second->GetType())
{
case VariantType::TYPE_BOOLEAN:
{
if (obj.second->boolValue)
{
Logger::FrameworkDebug("%s : true", obj.first.c_str());
}
else
{
Logger::FrameworkDebug("%s : false", obj.first.c_str());
}
}
break;
case VariantType::TYPE_INT8:
{
Logger::FrameworkDebug("%s : %hhd", obj.first.c_str(), obj.second->int8Value);
}
break;
case VariantType::TYPE_UINT8:
{
Logger::FrameworkDebug("%s : %hu", obj.first.c_str(), obj.second->uint8Value);
}
break;
case VariantType::TYPE_INT16:
{
Logger::FrameworkDebug("%s : %hd", obj.first.c_str(), obj.second->int16Value);
}
break;
case VariantType::TYPE_UINT16:
{
Logger::FrameworkDebug("%s : %hu", obj.first.c_str(), obj.second->uint16Value);
}
break;
case VariantType::TYPE_INT32:
{
Logger::FrameworkDebug("%s : %d", obj.first.c_str(), obj.second->int32Value);
}
break;
case VariantType::TYPE_UINT32:
{
Logger::FrameworkDebug("%s : %u", obj.first.c_str(), obj.second->uint32Value);
}
break;
case VariantType::TYPE_FLOAT:
{
Logger::FrameworkDebug("%s : %f", obj.first.c_str(), obj.second->floatValue);
}
break;
case VariantType::TYPE_STRING:
{
Logger::FrameworkDebug("%s : %s", obj.first.c_str(), obj.second->stringValue->c_str());
}
break;
case VariantType::TYPE_WIDE_STRING:
{
Logger::FrameworkDebug("%s : %S", obj.first.c_str(), obj.second->wideStringValue->c_str());
}
break;
default:
break;
}
}
Logger::FrameworkDebug("============================================================");
}
const KeyedArchive::UnderlyingMap& KeyedArchive::GetArchieveData() const
{
return objectMap;
}
const char* KeyedArchive::GenKeyFromIndex(uint32 index)
{
static char tmpKey[32];
sprintf(tmpKey, "%04u", index);
return tmpKey;
}
};