forked from kenygia/MooseEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LsbObject.h
745 lines (671 loc) · 18.8 KB
/
LsbObject.h
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
#ifndef LSBOBJECT_H
#define LSBOBJECT_H
#include <vector>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
#pragma pack(push,1)
struct HEADER_LSB {
long magic;
long fileLength;
long endianness;
long reserved1;
long reserved2;
long fileTimestamp;
long verMajor;
long verMinor;
long verBuild;
long verRevision;
long tagCount;
};
struct TAG_LSB {
long tagLength;
char* tag = 0;
long index;
TAG_LSB() {
tagLength = -1;
}
~TAG_LSB() {
delete tag;
}
TAG_LSB(const TAG_LSB& other) {
this->tagLength = other.tagLength;
if (other.tagLength >= 0) {
long strLen = strlen(other.tag);
this->tag = new char[strLen + 1];
strcpy(this->tag, other.tag);
this->index = other.index;
}
}
};
struct ENTITY_HEADER_LSB {
long id;
long dataOffset;
};
#pragma pack(pop)
class LsbObject {
std::string name;
long index;
long type;
char *data = 0;
long dataSize = 0;
std::vector<LsbObject*> children;
long childId = -1;
long itemsLeft = 0;
long directoriesLeft = 0;
long childrenCount = 0;
long entityId = 0;
long entitySize = 0;
bool bIsDirectory = false;
LsbObject *parent = 0;
std::vector<TAG_LSB *> *tagList = 0;
std::string localized1;
std::string localized2;
public:
LsbObject *lookupByUniquePath(const char *path);
static LsbObject *lookupByUniquePath(std::vector<LsbObject *>& entities, const char *path);
static std::vector<LsbObject *> lookupAllEntitiesWithName(LsbObject *object, const char *name);
static std::vector<LsbObject *> extractPropertyForEachListItem(std::vector<LsbObject *>& list, const char *propertyName);
static std::vector<LsbObject *> findItemsByAttribute(std::vector<LsbObject *>& list, const char *attributeName, const char *attributeValue, int valueLength);
static LsbObject *getObjectCreator(LsbObject *object);
static LsbObject *getObjectFromCreator(LsbObject *creator, const char *objectGroupName);
~LsbObject() {
if (data != 0) {
delete[] data;
}
for (int i=0; i<children.size(); ++i) {
delete children[i];
}
}
LsbObject() {
;
}
LsbObject(bool isDirectory, long index, const char *tag, long type, LsbObject *parent, std::vector<TAG_LSB *> *tagList = 0) {
this->setIsDirectory(isDirectory);
this->setItemsLeft(0);
this->setDirectoriesLeft(0);
this->setChildrenCount(-1);
this->setIndex(index);
this->setName(tag);
this->setType(type);
this->setParent(parent);
this->setTagList(tagList);
if (parent != 0) {
this->setChildId(parent->getChildren().size());
}
}
LsbObject(const LsbObject& other) {
this->name = other.name;
this->index = other.index;
this->type = other.type;
this->data = new char[other.dataSize];
memcpy(this->data, other.data, other.dataSize);
this->dataSize = other.dataSize;
for (int i=0; i<other.children.size(); ++i) {
LsbObject *newChild = new LsbObject(*other.children[i]);
this->addChild(newChild);
}
this->childId = other.childId;
this->itemsLeft = other.itemsLeft;
this->directoriesLeft = other.directoriesLeft;
this->childrenCount = other.childrenCount;
this->entityId = other.entityId;
this->entitySize = other.entitySize;
this->bIsDirectory = other.bIsDirectory;
this->parent = other.parent;
this->tagList = other.tagList;
this->localized1 = other.localized1;
this->localized2 = other.localized2;
}
static TAG_LSB *getTagByName(const char *name, std::vector<TAG_LSB *> *tagList)
{
std::string nameText = name;
for (int i=0; i<tagList->size(); ++i) {
if (tagList->at(i)->tag == nameText) {
return tagList->at(i);
}
}
return 0;
}
static TAG_LSB *createTagIfNeeded(const char *name, std::vector<TAG_LSB *> *tagList)
{
TAG_LSB *tag = getTagByName(name, tagList);
if (tag != 0) {
return tag;
}
tag = new TAG_LSB();
tag->index = LsbObject::getNextFreeTagIndex(tagList);
long tagLength = strlen(name) + 1;
char *tagAlloc = new char[tagLength];
strcpy(tagAlloc, name);
tag->tag = tagAlloc;
tag->tagLength = tagLength;
tagList->push_back(tag);
return tag;
}
void retag(std::vector<TAG_LSB *> *newTagList) {
std::stack<LsbObject *> dirStack;
dirStack.push(this);
while (!dirStack.empty()) {
LsbObject *currentDirectory = dirStack.top();
dirStack.pop();
std::string dirTag = currentDirectory->getName();
TAG_LSB *newTag = LsbObject::createTagIfNeeded(dirTag.c_str(), newTagList);
currentDirectory->setIndex(newTag->index);
std::vector<LsbObject *> items = currentDirectory->getItemsOnly();
std::vector<LsbObject *> dirs = currentDirectory->getDirectoriesOnly();
for (int j=0; j<items.size(); ++j) {
LsbObject *item = items[j];
std::string itemTag = item->getName();
TAG_LSB *newTag = LsbObject::createTagIfNeeded(itemTag.c_str(), newTagList);
item->setIndex(newTag->index);
}
for (int j=dirs.size() - 1; j>=0; --j) {
dirStack.push(dirs[j]);
}
}
}
static void stripUnusedTags(std::vector<TAG_LSB *> *tagList, std::vector<LsbObject *> objects) {
typedef std::map<int, bool> TagMap;
TagMap tagMap;
for (int i=0; i<objects.size(); ++i) {
std::stack<LsbObject *> dirStack;
dirStack.push(objects[i]);
while (!dirStack.empty()) {
LsbObject *currentDirectory = dirStack.top();
dirStack.pop();
tagMap[currentDirectory->getIndex()] = true;
std::vector<LsbObject *> items = currentDirectory->getItemsOnly();
std::vector<LsbObject *> dirs = currentDirectory->getDirectoriesOnly();
for (int j=0; j<items.size(); ++j) {
LsbObject *item = items[j];
tagMap[item->getIndex()] = true;
}
for (int j=dirs.size() - 1; j>=0; --j) {
dirStack.push(dirs[j]);
}
}
}
for (int i=0; i<tagList->size(); ++i) {
TAG_LSB *tag = tagList->at(i);
if (tagMap.find(tag->index) == tagMap.end()) {
tagList->erase(tagList->begin() + i);
delete tag;
--i;
}
}
}
static long getNextFreeTagIndex(std::vector<TAG_LSB *> *tagList) {
long nextFree = 0;
for (int i=0; i<tagList->size(); ++i) {
TAG_LSB *tag = tagList->at(i);
if (tag->index >= nextFree) {
nextFree = tag->index + 1;
}
}
return nextFree;
}
std::vector<TAG_LSB *> *getTagList() {
return this->tagList;
}
void setTagList(std::vector<TAG_LSB *> *tagList) {
this->tagList = tagList;
}
bool replaceChild(LsbObject *oldChild, LsbObject *newChild) {
for (int i=0; i<children.size(); ++i) {
if (children[i] == oldChild) {
children[i] = newChild;
delete oldChild;
return true;
}
}
return false;
}
bool removeChild(LsbObject *targetChild) {
for (int i=0; i<this->getChildren().size(); ++i) {
LsbObject *child = this->getChildren()[i];
if (child == targetChild) {
this->getChildren().erase(this->getChildren().begin() + i);
delete child;
return true;
}
}
return false;
}
long getChildrenCount() {
return this->childrenCount;
}
void setChildrenCount(long childrenCount) {
this->childrenCount = childrenCount;
}
long getChildId() {
return this->childId;
}
void setChildId(long childId) {
this->childId = childId;
}
std::vector<LsbObject *> getItemsOnly() {
std::vector<LsbObject *> items;
for (int i=0; i<children.size(); ++i) {
if (!children[i]->isDirectory()) {
items.push_back(children[i]);
}
}
return items;
}
std::vector<LsbObject *> getDirectoriesOnly() {
std::vector<LsbObject *> dirs;
for (int i=0; i<children.size(); ++i) {
if (children[i]->isDirectory()) {
dirs.push_back(children[i]);
}
}
return dirs;
}
LsbObject *getParent() {
return parent;
}
void setParent(LsbObject *parent) {
this->parent = parent;
}
long getTotalNodesRemaining() {
return directoriesLeft + itemsLeft;
}
bool isDirectory() {
return bIsDirectory;
}
void setIsDirectory(bool isDirectory) {
this->bIsDirectory = isDirectory;
}
long getEntitySize() {
return entitySize;
}
void setEntitySize(long entitySize) {
this->entitySize = entitySize;
}
bool isEntity() {
return entityId != 0;
}
void setEntityId(long entityId) {
this->entityId = entityId;
}
long getEntityId() {
return entityId;
}
long decrementItemsLeft() {
if (itemsLeft > 0) {
--itemsLeft;
}
return itemsLeft;
}
long decrementDirectoriesLeft() {
if (directoriesLeft > 0) {
--directoriesLeft;
}
return directoriesLeft;
}
void setItemsLeft(long itemsLeft) {
this->itemsLeft = itemsLeft;
}
long getItemsLeft() {
return itemsLeft;
}
void setDirectoriesLeft(long directoriesLeft) {
this->directoriesLeft = directoriesLeft;
}
long getDirectoriesLeft() {
return directoriesLeft;
}
void setIndex(long index) {
this->index = index;
}
long getIndex() {
return index;
}
void setType(long type) {
this->type = type;
}
long getType() {
return type;
}
LsbObject *addChild(LsbObject *obj) {
children.push_back(obj);
obj->setParent(this);
return children.back();
}
LsbObject *insertChild(LsbObject *obj, unsigned index) {
if (index < children.size()) {
children.insert(children.begin() + index, obj);
obj->setParent(this);
return obj;
} else {
return addChild(obj);
}
}
bool insertLast(LsbObject *object, const char *objectType) {
bool found = false;
for (int i=0; i<this->getChildren().size(); ++i) {
LsbObject *child = this->getChildren()[i];
if (found && child->getName() != objectType) {
//assumed to be continuous
insertChild(object, i);
return true;
}
if (child->getName() == objectType) {
found = true;
}
}
if (!found) {
this->addChild(object);
}
return false;
}
std::vector<LsbObject*>& getChildren() {
return children;
}
void setName(std::string name) {
this->name = name;
}
std::string getName() {
return name;
}
void setData(const char *data, int size) {
if (this->data != 0) {
delete[] this->data;
}
this->data = new char[size];
memcpy(this->data, data, size);
dataSize = size;
}
char *getData() {
return data;
}
long getDataSize() {
return dataSize;
}
long getIntData() {
return *((long *)data);
}
char getByteData() {
return *((char *)data);
}
void fromString(std::string &text) {
long type = this->getType();
if (type >= 0x14 && type <= 0x19) {
this->setData(text.c_str(), text.length() + 1);
} else if (type == 0x1D || type == 0x1E) {
unsigned int maxCount = text.length() + 1;
wchar_t *wide = new wchar_t[maxCount];
mbstowcs(wide, text.c_str(), maxCount);
this->setData((char *)wide, maxCount * 2);
delete[] wide;
} else if (type == 0x04) {
long value = 0;
try {
value = boost::lexical_cast<long>(text);
} catch (const boost::bad_lexical_cast& e) {
}
this->setData((char *)&value, sizeof(long));
} else if (type == 0x05) {
unsigned long value = 0;
try {
value = boost::lexical_cast<unsigned long>(text);
}
catch (const boost::bad_lexical_cast& e) {
;
}
this->setData((char *)&value, sizeof(unsigned long));
} else if (type == 0x06) {
float value = 0;
try {
value = boost::lexical_cast<float>(text);
} catch (const boost::bad_lexical_cast& e) {
}
this->setData((char *)&value, sizeof(float));
} else if (type == 0x07) {
double value = 0;
try {
value = boost::lexical_cast<double>(text);
} catch (const boost::bad_lexical_cast& e) {
}
this->setData((char *)&value, sizeof(double));
} else if (type == 0x13) {
bool value = false;
if (boost::to_lower_copy(text) == "true") {
value = true;
}
this->setData((char *)&value, sizeof(bool));
} else if (type == 0x0C) {
std::vector<float> floatValues;
std::vector<std::string> values;
boost::split(values, text, boost::is_any_of(","));
for (int i=0; i<values.size(); ++i) {
std::string value = values[i];
boost::trim(value);
bool success = true;
float floatValue;
try {
floatValue = boost::lexical_cast<float>(value);
} catch (const boost::bad_lexical_cast& e) {
success = false;
}
if (success) {
floatValues.push_back(floatValue);
}
}
if (floatValues.size() == 3) {
long newDataLen = sizeof(float) * 3;
char *newData = new char[newDataLen];
memcpy(newData + sizeof(float) * 0, &floatValues[0], sizeof(float));
memcpy(newData + sizeof(float) * 1, &floatValues[1], sizeof(float));
memcpy(newData + sizeof(float) * 2, &floatValues[2], sizeof(float));
this->setData(newData, newDataLen);
delete[] newData;
}
} else if (type == 0x01) {
unsigned char value = 0;
try {
value = (unsigned char)boost::lexical_cast<unsigned short>(text);
} catch (const boost::bad_lexical_cast& e) {
}
this->setData((char *)&value, sizeof(unsigned char));
}
}
std::string toString () {
std::ostringstream ss;
char *dataPtr = this->getData();
switch (type) {
case 0x0:{
ss<<"null";
break;}
case 0x1:{
unsigned char value = *((unsigned char *)dataPtr);
unsigned long temp = value;
ss<<temp;
break;}
case 0x2:{
signed short value = *((signed short *)dataPtr);
ss<<value;
break;}
case 0x3:{
unsigned short value = *((unsigned short *)dataPtr);
ss<<value;
break;}
case 0x4:{
signed long value = *((signed long *)dataPtr);
ss<<value;
break;}
case 0x5:{
unsigned long value = *((unsigned long *)dataPtr);
ss<<value;
break;}
case 0x6:{
float value = *((float *)dataPtr);
ss<<value;
break;}
case 0x7:{
double value = *((double *)dataPtr);
ss<<value;
break;}
case 0x8:{
signed long value = *((signed long *)dataPtr);
signed long value2 = *((signed long *)(dataPtr + 4));
ss<<value<<", "<<value2;
break;}
case 0x9:{
signed long value = *((signed long *)dataPtr);
signed long value2 = *((signed long *)dataPtr + 1);
signed long value3 = *((signed long *)dataPtr + 2);
ss<<value<<", "<<value2<<", "<<value3;
break;}
case 0x0A:{
signed long value = *((signed long *)dataPtr);
signed long value2 = *((signed long *)dataPtr + 1);
signed long value3 = *((signed long *)dataPtr + 2);
signed long value4 = *((signed long *)dataPtr + 3);
ss<<value<<", "<<value2<<", "<<value3<<", "<<value4;
break;}
case 0x0B:{
float value = *((float *)dataPtr);
float value2 = *((float *)dataPtr + 1);
ss<<value<<", "<<value2;
break;}
case 0x0C:{
float value = *((float *)dataPtr);
float value2 = *((float *)dataPtr + 1);
float value3 = *((float *)dataPtr + 2);
ss<<value<<", "<<value2<<", "<<value3;
break;}
case 0x0D:{
float value = *((float *)dataPtr);
float value2 = *((float *)dataPtr + 1);
float value3 = *((float *)dataPtr + 2);
float value4 = *((float *)dataPtr + 3);
ss<<value<<", "<<value2<<", "<<value3<<", "<<value4;
break;}
case 0x0E:{
float value = *((float *)dataPtr);
float value2 = *((float *)dataPtr + 1);
float value3 = *((float *)dataPtr + 2);
float value4 = *((float *)dataPtr + 3);
ss<<"{"<<value<<", "<<value2<<"},"
<<" {"<<value3<<", "<<value4<<"}";
break;}
case 0x0F:{
float value = *((float *)dataPtr);
float value2 = *((float *)dataPtr + 1);
float value3 = *((float *)dataPtr + 2);
float value4 = *((float *)dataPtr + 3);
float value5 = *((float *)dataPtr + 4);
float value6 = *((float *)dataPtr + 5);
float value7 = *((float *)dataPtr + 6);
float value8 = *((float *)dataPtr + 7);
float value9 = *((float *)dataPtr + 8);
ss<<"{"<<value<<", "<<value2<<", "<<value3<<"},"
<<" {"<<value4<<", "<<value5<<","<<value6<<"},"
<<" {"<<value7<<", "<<value8<<", "<<value9<<"}";
break;}
case 0x10:{
float value = *((float *)dataPtr);
float value2 = *((float *)dataPtr + 1);
float value3 = *((float *)dataPtr + 2);
float value4 = *((float *)dataPtr + 3);
float value5 = *((float *)dataPtr + 4);
float value6 = *((float *)dataPtr + 5);
float value7 = *((float *)dataPtr + 6);
float value8 = *((float *)dataPtr + 7);
float value9 = *((float *)dataPtr + 8);
float value10 = *((float *)dataPtr + 9);
float value11 = *((float *)dataPtr + 10);
float value12 = *((float *)dataPtr + 11);
ss<<"{"<<value<<", "<<value2<<", "<<value3<<", "<<value4<<"},"
<<" {"<<value5<<", "<<value6<<", "<<value7<<", "<<value8<<"},"
<<" {"<<value9<<", "<<value10<<", "<<value11<<", "<<value12<<"}";
break;}
case 0x11:{
float value = *((float *)dataPtr);
float value2 = *((float *)dataPtr + 1);
float value3 = *((float *)dataPtr + 2);
float value4 = *((float *)dataPtr + 3);
float value5 = *((float *)dataPtr + 4);
float value6 = *((float *)dataPtr + 5);
float value7 = *((float *)dataPtr + 6);
float value8 = *((float *)dataPtr + 7);
float value9 = *((float *)dataPtr + 8);
float value10 = *((float *)dataPtr + 9);
float value11 = *((float *)dataPtr + 10);
float value12 = *((float *)dataPtr + 11);
ss<<"{"<<value<<", "<<value2<<", "<<value3<<"},"
<<" {"<<value4<<", "<<value5<<", "<<value6<<"},"
<<" {"<<value7<<", "<<value8<<", "<<value9<<"},"
<<" {"<<value10<<", "<<value11<<", "<<value12<<"}";
break;}
case 0x12:{
float value = *((float *)dataPtr);
float value2 = *((float *)dataPtr + 1);
float value3 = *((float *)dataPtr + 2);
float value4 = *((float *)dataPtr + 3);
float value5 = *((float *)dataPtr + 4);
float value6 = *((float *)dataPtr + 5);
float value7 = *((float *)dataPtr + 6);
float value8 = *((float *)dataPtr + 7);
float value9 = *((float *)dataPtr + 8);
float value10 = *((float *)dataPtr + 9);
float value11 = *((float *)dataPtr + 10);
float value12 = *((float *)dataPtr + 11);
float value13 = *((float *)dataPtr + 12);
float value14 = *((float *)dataPtr + 13);
float value15 = *((float *)dataPtr + 14);
float value16 = *((float *)dataPtr + 15);
ss<<"{"<<value<<", "<<value2<<", "<<value3<<", "<<value4<<"},"
<<" {"<<value5<<", "<<value6<<", "<<value7<<", "<<value8<<"},"
<<" {"<<value9<<", "<<value10<<", "<<value11<<", "<<value12<<"},"
<<" {"<<value13<<", "<<value14<<", "<<value15<<", "<<value16<<"}";
break;}
case 0x13:{
unsigned char value = *((unsigned char *)dataPtr);
ss<<(value == 1 ? "true" : "false");
break;}
case 0x14:
case 0x15:
case 0x16:
case 0x17:
case 0x18:
case 0x19:{
ss.write(dataPtr, this->getDataSize());
ss<<'\0';
break;}
case 0x1A:{
unsigned long long value = *((unsigned long long *)dataPtr);
ss<<value;
break;}
case 0x1B:{
signed char value = *((signed char *)dataPtr);
signed long temp = value;
ss<<temp;
break;}
case 0x1C:{
ss.write(dataPtr, strlen(dataPtr) + 1);
break;}
case 0x1D:
case 0x1E:{
char *alloc = new char[this->getDataSize() / 2];
wcstombs(alloc, (const wchar_t*)dataPtr, this->getDataSize());
ss<<alloc;
delete []alloc;
break;}
}
return ss.str();
}
std::string &getLocalized1();
void setLocalized1(const std::string &value);
std::string &getLocalized2();
void setLocalized2(const std::string &value);
};
#endif // LSBOBJECT_H