-
Notifications
You must be signed in to change notification settings - Fork 29
/
UPKUtils.cpp
1080 lines (1031 loc) · 37.9 KB
/
UPKUtils.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
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "UPKUtils.h"
#include "LzoUtils.h"
#include <cstring>
#include <sstream>
uint8_t PatchUPKhash [] = {0x7A, 0xA0, 0x56, 0xC9,
0x60, 0x5F, 0x7B, 0x31,
0x72, 0x5D, 0x4B, 0xC4,
0x7C, 0xD2, 0x4D, 0xD9 };
UPKUtils::UPKUtils(const char* filename)
{
UPKUtils::Read(filename);
}
UPKUtils::~UPKUtils()
{
///ugly, but for compatibility reasons: don't want to rewrite other tools yet
if (AutosaveOn)
UPKUtils::SaveOnDisk();
}
bool UPKUtils::Read(const char* filename)
{
UPKFileName = filename;
std::ifstream file(UPKFileName, std::ios::binary);
if (!file)
return false;
UPKFile.str("");
UPKFile << file.rdbuf();
file.close();
return UPKUtils::Reload();
}
bool UPKUtils::SaveFileAs(const char* filename)
{
UPKFileName = filename;
return UPKUtils::SaveOnDisk();
}
bool UPKUtils::SaveOnDisk()
{
std::ofstream file(UPKFileName, std::ios::binary);
if (!file)
return false;
file.write(UPKFile.str().data(), UPKFile.str().size());
file.close();
return true;
}
bool UPKUtils::Reload()
{
if (!IsLoaded())
return false;
UPKFile.clear();
UPKFile.seekg(0, std::ios::end);
UPKFileSize = UPKFile.tellg();
UPKFile.seekg(0);
if (!UPKInfo::Read(UPKFile))
return UPKInfo::ReadCompressedHeader(UPKFile);
return true;
}
bool UPKUtils::DecompressPackage(const char* decompFileName)
{
if (!IsLoaded())
return false;
if (!IsLzoCompressed())
return false;
unsigned int NumCompressedChunks = Summary.NumCompressedChunks;
std::stringstream decompressed_stream;
if (IsFullyCompressed())
NumCompressedChunks = 1;
else
{
///reset flags
Summary.CompressionFlags = 0;
Summary.PackageFlags &= ~((uint32_t)UPackageFlags::Compressed);
Summary.NumCompressedChunks = 0;
///write new summary
std::vector<char> summaryData = SerializeSummary();
decompressed_stream.write(summaryData.data(), summaryData.size());
}
///decompress
for (unsigned int i = 0; i < NumCompressedChunks; ++i)
{
if (IsFullyCompressed())
UPKFile.seekg(0);
else
UPKFile.seekg(Summary.CompressedChunks[i].CompressedOffset);
std::vector<char> dataChunk;
DecompressLzoCompressedDataFromStream(UPKFile, dataChunk);
///write decompressed data
decompressed_stream.write(dataChunk.data(), dataChunk.size());
}
///reset and reload the package
if (std::string(decompFileName) != "")
UPKFileName = decompFileName;
UPKFile.str("");
UPKFile << decompressed_stream.str();
return Reload();
}
std::vector<char> UPKUtils::GetExportData(uint32_t idx)
{
std::vector<char> data;
if (idx < 1 || idx >= ExportTable.size())
return data;
data.resize(ExportTable[idx].SerialSize);
UPKFile.seekg(ExportTable[idx].SerialOffset);
UPKFile.read(data.data(), data.size());
LastAccessedExportObjIdx = idx;
return data;
}
std::string UPKUtils::GetUObjectSerializedData(uint32_t idx)
{
///returns just the UObject part of export data: next object ref + defaultproperties list
if (idx < 1 || idx >= ExportTable.size() || NoneIdx <= 0)
return "";
std::vector<char> exportData = GetExportData(idx);
std::string exportDataStr(exportData.data(), exportData.size());
if (Summary.Version == VER_BATMAN_CITY) ///for BatmanAC def props are serialized by offset, so need to actually deserialize def props list to find out where it ends
{
UObject* tmpObj = DeserializeObjectByRef(idx, true);
uint32_t sz = tmpObj->GetUObjExportEntrySize();
if (sz > 0 && sz <= ExportTable[idx].SerialSize)
exportDataStr.resize(sz);
else
exportDataStr = "";
if (tmpObj)
delete tmpObj;
}
else ///for normal packages def prop list ends with None name index, which is easy to locate by search
{
UNameIndex NameIdx = NoneToUNameIndex();
std::string noneStr(reinterpret_cast<char*>(&NameIdx), 8);
size_t pos = exportDataStr.find(noneStr);
if (pos == std::string::npos)
return "";
exportDataStr.resize(pos + 8);
}
return exportDataStr;
}
void UPKUtils::SaveExportData(uint32_t idx)
{
if (idx < 1 || idx >= ExportTable.size())
return;
std::string filename = ExportTable[idx].FullName + "." + ExportTable[idx].Type;
std::vector<char> dataChunk = GetExportData(idx);
std::ofstream out(filename.c_str(), std::ios::binary);
out.write(dataChunk.data(), dataChunk.size());
}
/// relatively safe behavior (old realization)
bool UPKUtils::MoveExportData(uint32_t idx, uint32_t newObjectSize)
{
if (idx < 1 || idx >= ExportTable.size())
return false;
std::vector<char> data = GetExportData(idx);
UPKFile.seekg(0, std::ios::end);
uint32_t newObjectOffset = UPKFile.tellg();
bool isFunction = (ExportTable[idx].Type == "Function");
if (newObjectSize > ExportTable[idx].SerialSize)
{
UPKFile.seekp(ExportTable[idx].EntryOffset + sizeof(uint32_t)*8);
UPKFile.write(reinterpret_cast<char*>(&newObjectSize), sizeof(newObjectSize));
unsigned int diffSize = newObjectSize - data.size();
if (isFunction == false)
{
for (unsigned int i = 0; i < diffSize; ++i)
data.push_back(0x00);
}
else
{
uint32_t oldMemSize = 0;
uint32_t oldFileSize = 0;
memcpy(&oldMemSize, data.data() + 0x28, 0x4); /// copy function memory size
memcpy(&oldFileSize, data.data() + 0x2C, 0x4); /// and file size
uint32_t newMemSize = oldMemSize + diffSize; /// find new sizes
uint32_t newFileSize = oldFileSize + diffSize;
uint32_t headSize = 0x30 + oldFileSize - 1; /// head size (all data before 0x53)
uint32_t tailSize = ExportTable[idx].SerialSize - headSize; /// tail size (0x53 and all data after)
std::vector<char> newData(newObjectSize);
memset(newData.data(), 0x0B, newObjectSize); /// fill new data with 0x0B
memcpy(newData.data(), data.data(), headSize); /// copy all data before 0x53
memcpy(newData.data() + 0x28, &newMemSize, 0x4); /// set new memory size
memcpy(newData.data() + 0x2C, &newFileSize, 0x4);/// and file size
memcpy(newData.data() + headSize + diffSize, data.data() + headSize, tailSize); /// copy 0x53 and all data after
data = newData;
}
}
UPKFile.seekp(ExportTable[idx].EntryOffset + sizeof(uint32_t)*9);
UPKFile.write(reinterpret_cast<char*>(&newObjectOffset), sizeof(newObjectOffset));
UPKFile.seekp(newObjectOffset);
UPKFile.write(data.data(), data.size());
/// write backup info
UPKFile.write(reinterpret_cast<char*>(&PatchUPKhash[0]), 16);
UPKFile.write(reinterpret_cast<char*>(&ExportTable[idx].SerialSize), sizeof(ExportTable[idx].SerialSize));
UPKFile.write(reinterpret_cast<char*>(&ExportTable[idx].SerialOffset), sizeof(ExportTable[idx].SerialOffset));
/// reload package
UPKUtils::Reload();
return true;
}
bool UPKUtils::UndoMoveExportData(uint32_t idx)
{
if (idx < 1 || idx >= ExportTable.size())
return false;
UPKFile.seekg(ExportTable[idx].SerialOffset + ExportTable[idx].SerialSize);
uint8_t readHash [16];
UPKFile.read(reinterpret_cast<char*>(&readHash[0]), 16);
if (memcmp(readHash, PatchUPKhash, 16) != 0)
return false;
uint32_t oldObjectFileSize, oldObjectOffset;
UPKFile.read(reinterpret_cast<char*>(&oldObjectFileSize), sizeof(oldObjectFileSize));
UPKFile.read(reinterpret_cast<char*>(&oldObjectOffset), sizeof(oldObjectOffset));
UPKFile.seekp(ExportTable[idx].EntryOffset + sizeof(uint32_t)*8);
UPKFile.write(reinterpret_cast<char*>(&oldObjectFileSize), sizeof(oldObjectFileSize));
UPKFile.write(reinterpret_cast<char*>(&oldObjectOffset), sizeof(oldObjectOffset));
/// reload package
UPKUtils::Reload();
return true;
}
std::vector<char> UPKUtils::GetResizedDataChunk(uint32_t idx, int newObjectSize, int resizeAt)
{
std::vector<char> data;
if (idx < 1 || idx >= ExportTable.size())
return data;
/// get export object serial data
data = GetExportData(idx);
/// if object needs resizing
if (newObjectSize > 0 && (unsigned)newObjectSize != data.size())
{
/// if resizing occurs in the middle of an object
if (resizeAt > 0 && resizeAt < newObjectSize)
{
int diff = newObjectSize - data.size();
std::vector<char> newData(newObjectSize);
memset(newData.data(), 0, newObjectSize); /// fill with zeros
memcpy(newData.data(), data.data(), resizeAt); /// copy head
if (diff > 0) /// if expanding
memcpy(newData.data() + resizeAt + diff, data.data() + resizeAt, data.size() - resizeAt);
else /// if shrinking
memcpy(newData.data() + resizeAt, data.data() + resizeAt - diff, data.size() - (resizeAt - diff));
data = newData;
}
else
{
data.resize(newObjectSize, 0);
}
}
return data;
}
bool UPKUtils::MoveResizeObject(uint32_t idx, int newObjectSize, int resizeAt)
{
if (idx < 1 || idx >= ExportTable.size())
return false;
std::vector<char> data = GetResizedDataChunk(idx, newObjectSize, resizeAt);
/// move write pointer to the end of file
UPKFile.seekg(0, std::ios::end);
uint32_t newObjectOffset = UPKFile.tellg();
/// if object needs resizing
if (ExportTable[idx].SerialSize != data.size())
{
/// write new SerialSize to ExportTable entry
if (Summary.Version == VER_BATMAN_CITY)
UPKFile.seekp(ExportTable[idx].EntryOffset + sizeof(uint32_t)*9);
else
UPKFile.seekp(ExportTable[idx].EntryOffset + sizeof(uint32_t)*8);
UPKFile.write(reinterpret_cast<char*>(&newObjectSize), sizeof(newObjectSize));
}
/// write new SerialOffset to ExportTable entry
if (Summary.Version == VER_BATMAN_CITY)
UPKFile.seekp(ExportTable[idx].EntryOffset + sizeof(uint32_t)*10);
else
UPKFile.seekp(ExportTable[idx].EntryOffset + sizeof(uint32_t)*9);
UPKFile.write(reinterpret_cast<char*>(&newObjectOffset), sizeof(newObjectOffset));
/// write new SerialData
if (data.size() > 0)
{
UPKFile.seekp(newObjectOffset);
UPKFile.write(data.data(), data.size());
}
/// write backup info
UPKFile.write(reinterpret_cast<char*>(&PatchUPKhash[0]), 16);
UPKFile.write(reinterpret_cast<char*>(&ExportTable[idx].SerialSize), sizeof(ExportTable[idx].SerialSize));
UPKFile.write(reinterpret_cast<char*>(&ExportTable[idx].SerialOffset), sizeof(ExportTable[idx].SerialOffset));
/// reload package
UPKUtils::Reload();
return true;
}
bool UPKUtils::UndoMoveResizeObject(uint32_t idx)
{
return UndoMoveExportData(idx);
}
std::string UPKUtils::Deserialize(UObjectReference ObjRef, bool TryUnsafe, bool QuickMode)
{
if (ObjRef < 1 || ObjRef >= (int)ExportTable.size())
return "Bad object reference!\n";
UObject* Obj;
if (ExportTable[ObjRef].ObjectFlagsH & (uint32_t)UObjectFlagsH::PropertiesObject)
{
Obj = UObjectFactory::Create(GlobalType::UObject);
}
else
{
Obj = UObjectFactory::Create(ExportTable[ObjRef].Type);
}
if (Obj == nullptr)
return "Can't create object of given type!\n";
std::string res;
UPKFile.seekg(ExportTable[ObjRef].SerialOffset);
Obj->SetRef(ObjRef);
Obj->SetUnsafe(TryUnsafe);
Obj->SetQuickMode(QuickMode);
res = Obj->Deserialize(UPKFile, *dynamic_cast<UPKInfo*>(this));
delete Obj;
return res;
}
UObject* UPKUtils::DeserializeObjectByRef(UObjectReference ObjRef, bool DoNotReadTFC)
{
if (ObjRef < 1 || ObjRef >= (int)ExportTable.size())
return nullptr;
UObject* ExportObject;
if (ExportTable[ObjRef].ObjectFlagsH & (uint32_t)UObjectFlagsH::PropertiesObject)
{
ExportObject = UObjectFactory::Create(GlobalType::UObject);
}
else
{
ExportObject = UObjectFactory::Create(ExportTable[ObjRef].Type);
}
if (ExportObject == nullptr)
return nullptr;
UPKFile.seekg(ExportTable[ObjRef].SerialOffset);
ExportObject->SetRef(ObjRef);
ExportObject->SetUnsafe(false);
ExportObject->SetQuickMode(false);
if (dynamic_cast<UTexture2D*>(ExportObject) != nullptr)
dynamic_cast<UTexture2D*>(ExportObject)->SetDoNotReadTFC(DoNotReadTFC);
ExportObject->Deserialize(UPKFile, *dynamic_cast<UPKInfo*>(this));
return ExportObject;
}
bool UPKUtils::CheckValidFileOffset(size_t offset)
{
if (IsLoaded() == false)
{
return false;
}
/// does not allow to change package signature and version
return (offset >= 8 && offset < UPKFileSize);
}
bool UPKUtils::WriteExportData(uint32_t idx, std::vector<char> data, std::vector<char> *backupData)
{
if (idx < 1 || idx >= ExportTable.size())
return false;
if (!IsLoaded())
return false;
if (ExportTable[idx].SerialSize != data.size())
return false;
if (backupData != nullptr)
{
backupData->clear();
backupData->resize(data.size());
UPKFile.seekg(ExportTable[idx].SerialOffset);
UPKFile.read(backupData->data(), backupData->size());
}
UPKFile.seekp(ExportTable[idx].SerialOffset);
UPKFile.write(data.data(), data.size());
return true;
}
bool UPKUtils::WriteNameTableName(uint32_t idx, std::string name)
{
if (idx < 1 || idx >= NameTable.size())
return false;
if (!IsLoaded())
return false;
if ((unsigned)(NameTable[idx].NameLength - 1) != name.length())
return false;
UPKFile.seekp(NameTable[idx].EntryOffset + sizeof(NameTable[idx].NameLength));
UPKFile.write(name.c_str(), name.length());
/// reload package
UPKUtils::Reload();
return true;
}
bool UPKUtils::WriteData(size_t offset, std::vector<char> data, std::vector<char> *backupData)
{
if (!CheckValidFileOffset(offset))
return false;
if (backupData != nullptr)
{
backupData->clear();
backupData->resize(data.size());
UPKFile.seekg(offset);
UPKFile.read(backupData->data(), backupData->size());
}
UPKFile.seekp(offset);
UPKFile.write(data.data(), data.size());
/// if changed header
if (offset < Summary.SerialOffset)
{
/// reload package
UPKUtils::Reload();
}
return true;
}
std::vector<char> UPKUtils::GetBulkData(size_t offset, std::vector<char> data)
{
UBulkDataMirror DataMirror;
DataMirror.SetBulkDataRaw(data);
DataMirror.SetFileOffset(offset + DataMirror.GetBulkDataRelOffset());
std::string mirrorStr = DataMirror.Serialize();
std::vector<char> mirrorVec(mirrorStr.size());
memcpy(mirrorVec.data(), mirrorStr.data(), mirrorStr.size());
return mirrorVec;
}
size_t UPKUtils::FindDataChunk(std::vector<char> data, size_t beg, size_t limit)
{
if (limit != 0 && (limit - beg + 1 < data.size() || limit < beg))
return 0;
size_t offset = 0, idx = beg;
bool found = false;
std::vector<char> fileBuf((limit == 0 ? UPKFileSize : limit) - beg + 1);
UPKFile.seekg(beg);
UPKFile.read(fileBuf.data(), fileBuf.size());
char* pFileBuf = fileBuf.data();
char* pData = data.data();
for (char* p = pFileBuf; p != pFileBuf + fileBuf.size() - data.size() + 1; ++p)
{
if (memcmp(p, pData, data.size()) == 0)
{
found = true;
offset = idx;
break;
}
++idx;
}
if (found == false)
offset = 0;
UPKFile.clear();
UPKFile.seekg(0);
return offset;
}
size_t UPKUtils::GetScriptSize(uint32_t idx)
{
if (idx < 1 || idx >= ExportTable.size())
return 0;
UObject* Obj;
Obj = UObjectFactory::Create(ExportTable[idx].Type);
if (Obj == nullptr)
return 0;
UPKFile.seekg(ExportTable[idx].SerialOffset);
Obj->SetRef(idx);
Obj->SetUnsafe(false);
Obj->SetQuickMode(true);
Obj->Deserialize(UPKFile, *dynamic_cast<UPKInfo*>(this));
if (Obj->IsStructure() == false)
{
delete Obj;
return 0;
}
UStruct* St = dynamic_cast<UStruct*>(Obj);
if (St == nullptr)
{
delete Obj;
return 0;
}
size_t ScriptSize = St->GetScriptSerialSize();
delete Obj;
return ScriptSize;
}
size_t UPKUtils::GetScriptMemSize(uint32_t idx)
{
if (idx < 1 || idx >= ExportTable.size())
return 0;
UObject* Obj;
Obj = UObjectFactory::Create(ExportTable[idx].Type);
if (Obj == nullptr)
return 0;
UPKFile.seekg(ExportTable[idx].SerialOffset);
Obj->SetRef(idx);
Obj->SetUnsafe(false);
Obj->SetQuickMode(true);
Obj->Deserialize(UPKFile, *dynamic_cast<UPKInfo*>(this));
if (Obj->IsStructure() == false)
{
delete Obj;
return 0;
}
UStruct* St = dynamic_cast<UStruct*>(Obj);
if (St == nullptr)
{
delete Obj;
return 0;
}
size_t ScriptMemSize = St->GetScriptMemorySize();
delete Obj;
return ScriptMemSize;
}
size_t UPKUtils::GetScriptRelOffset(uint32_t idx)
{
if (idx < 1 || idx >= ExportTable.size())
return 0;
UObject* Obj;
Obj = UObjectFactory::Create(ExportTable[idx].Type);
if (Obj == nullptr)
return 0;
UPKFile.seekg(ExportTable[idx].SerialOffset);
Obj->SetRef(idx);
Obj->SetUnsafe(false);
Obj->SetQuickMode(true);
Obj->Deserialize(UPKFile, *dynamic_cast<UPKInfo*>(this));
if (Obj->IsStructure() == false)
{
delete Obj;
return 0;
}
UStruct* St = dynamic_cast<UStruct*>(Obj);
if (St == nullptr)
{
delete Obj;
return 0;
}
size_t ScriptRelOffset = St->GetScriptOffset() - ExportTable[idx].SerialOffset;
delete Obj;
return ScriptRelOffset;
}
bool UPKUtils::ReplacePropertyValue(UDefaultProperty prop, uint32_t idx, std::string& exportDataStr)
{
std::string propString = prop.Serialize(*dynamic_cast<UPKInfo*>(this));
std::string propHeaderString(propString.begin(), propString.begin() + prop.GetInnerValueOffset(GetVersion()));
size_t pos = exportDataStr.find(propHeaderString);
if (pos == std::string::npos)
return false;
pos += propHeaderString.size();
std::string propValueString(propString.begin() + propHeaderString.size(), propString.end());
exportDataStr.replace(pos, propValueString.size(), propValueString);
return true;
}
bool UPKUtils::ReplaceProperty(UDefaultProperty oldProp, UDefaultProperty newProp, uint32_t idx, std::string& exportDataStr)
{
std::string oldPropString, newPropString;
if (Summary.Version == VER_BATMAN_CITY)
{
oldPropString = oldProp.SerializeBatmanAC(*dynamic_cast<UPKInfo*>(this));
newPropString = newProp.SerializeBatmanAC(*dynamic_cast<UPKInfo*>(this));
}
else
{
oldPropString = oldProp.Serialize(*dynamic_cast<UPKInfo*>(this));
newPropString = newProp.Serialize(*dynamic_cast<UPKInfo*>(this));
}
///so far only props of the same size can be replaced
if (oldPropString.size() != newPropString.size())
return false;
size_t pos = exportDataStr.find(oldPropString);
if (pos == std::string::npos)
return false;
exportDataStr.replace(pos, newPropString.size(), newPropString);
return true;
}
bool UPKUtils::RemoveProperty(UDefaultProperty prop, uint32_t idx, std::string& exportDataStr)
{
std::string propString = prop.Serialize(*dynamic_cast<UPKInfo*>(this));
std::string propHeaderString(propString.begin(), propString.begin() + prop.GetInnerValueOffset(GetVersion()));
size_t pos = exportDataStr.find(propHeaderString);
if (pos == std::string::npos)
return false;
exportDataStr.erase(pos, propString.size());
return true;
}
bool UPKUtils::InsertProperty(UDefaultProperty prop, UDefaultProperty beforeProp, uint32_t idx, std::string& exportDataStr)
{
std::string propString = prop.Serialize(*dynamic_cast<UPKInfo*>(this));
std::string beforePropString = beforeProp.Serialize(*dynamic_cast<UPKInfo*>(this));
std::string propHeaderString(beforePropString.begin(), beforePropString.begin() + beforeProp.GetInnerValueOffset(GetVersion()));
size_t pos = exportDataStr.find(propHeaderString);
if (pos == std::string::npos)
return false;
exportDataStr.insert(pos, propString);
return true;
}
bool UPKUtils::InsertProperty(UDefaultProperty prop, uint32_t idx, std::string& exportDataStr)
{
UDefaultProperty beforeProp;
beforeProp.MakeNameProperty("None", "", *dynamic_cast<UPKInfo*>(this));
return InsertProperty(prop, beforeProp, idx, exportDataStr);
}
bool UPKUtils::ResizeInPlace(uint32_t idx, int newObjectSize, int resizeAt)
{
if (!UPKFile.good())
{
return false;
}
if (idx < 1 || idx >= ExportTable.size())
return false;
std::vector<char> data = GetResizedDataChunk(idx, newObjectSize, resizeAt);
int diffSize = data.size() - ExportTable[idx].SerialSize;
/// increase offsets
for (unsigned i = 1; i <= Summary.ExportCount; ++i)
{
if (i != idx && ExportTable[i].SerialOffset > ExportTable[idx].SerialOffset)
{
ExportTable[i].SerialOffset += diffSize;
}
}
/// backup serialized export data into memory
UPKFile.clear();
UPKFile.seekg(Summary.SerialOffset);
std::vector<char> serializedDataBeforeIdx(ExportTable[idx].SerialOffset - UPKFile.tellg());
if (serializedDataBeforeIdx.size() > 0)
{
UPKFile.read(serializedDataBeforeIdx.data(), serializedDataBeforeIdx.size());
}
UPKFile.seekg(ExportTable[idx].SerialOffset + ExportTable[idx].SerialSize);
std::vector<char> serializedDataAfterIdx(UPKFileSize - UPKFile.tellg());
if (serializedDataAfterIdx.size() > 0)
{
UPKFile.read(serializedDataAfterIdx.data(), serializedDataAfterIdx.size());
}
/// save new serial size
ExportTable[idx].SerialSize = newObjectSize;
/// serialize header
std::vector<char> serializedHeader = SerializeHeader();
/// rewrite package
UPKFile.str("");
/// write serialized header
UPKFile.write(serializedHeader.data(), serializedHeader.size());
/// write serialized export data before resized object
if (serializedDataBeforeIdx.size() > 0)
{
UPKFile.write(serializedDataBeforeIdx.data(), serializedDataBeforeIdx.size());
}
/// write resized export object data
UPKFile.write(data.data(), data.size());
/// write serialized export data after resized object
if (serializedDataAfterIdx.size() > 0)
{
UPKFile.write(serializedDataAfterIdx.data(), serializedDataAfterIdx.size());
}
/// reload package
return Reload();
}
bool UPKUtils::AddNameEntry(FNameEntry Entry)
{
if (!UPKFile.good())
{
return false;
}
size_t oldSerialOffset = Summary.SerialOffset;
/// increase header size
Summary.HeaderSize += Entry.EntrySize;
/// add entry
++Summary.NameCount;
NameTable.push_back(Entry);
/// increase offsets
Summary.ImportOffset += Entry.EntrySize;
Summary.ExportOffset += Entry.EntrySize;
Summary.DependsOffset += Entry.EntrySize;
Summary.SerialOffset += Entry.EntrySize;
for (unsigned i = 1; i <= Summary.ExportCount; ++i)
{
ExportTable[i].SerialOffset += Entry.EntrySize;
}
/// backup serialized export data into memory
UPKFile.clear();
UPKFile.seekg(oldSerialOffset);
std::vector<char> serializedData(UPKFileSize - oldSerialOffset);
UPKFile.read(serializedData.data(), serializedData.size());
/// rewrite package
UPKFile.seekp(0);
/// serialize header
std::vector<char> serializedHeader = SerializeHeader();
/// write serialized header
UPKFile.write(serializedHeader.data(), serializedHeader.size());
/// write serialized export data
UPKFile.write(serializedData.data(), serializedData.size());
/// reload package
UPKUtils::Reload();
return true;
}
bool UPKUtils::AddImportEntry(FObjectImport Entry)
{
if (!UPKFile.good())
{
return false;
}
size_t oldSerialOffset = Summary.SerialOffset;
/// increase header size
Summary.HeaderSize += Entry.EntrySize;
/// add entry
++Summary.ImportCount;
ImportTable.push_back(Entry);
/// increase offsets
Summary.ExportOffset += Entry.EntrySize;
Summary.DependsOffset += Entry.EntrySize;
Summary.SerialOffset += Entry.EntrySize;
for (unsigned i = 1; i <= Summary.ExportCount; ++i)
{
ExportTable[i].SerialOffset += Entry.EntrySize;
}
/// backup serialized export data into memory
UPKFile.clear();
UPKFile.seekg(oldSerialOffset);
std::vector<char> serializedData(UPKFileSize - oldSerialOffset);
UPKFile.read(serializedData.data(), serializedData.size());
/// rewrite package
UPKFile.seekp(0);
/// serialize header
std::vector<char> serializedHeader = SerializeHeader();
/// write serialized header
UPKFile.write(serializedHeader.data(), serializedHeader.size());
/// write serialized export data
UPKFile.write(serializedData.data(), serializedData.size());
/// reload package
UPKUtils::Reload();
return true;
}
bool UPKUtils::AddExportEntry(FObjectExport Entry)
{
if (!UPKFile.good())
{
return false;
}
unsigned oldExportCount = Summary.ExportCount;
size_t oldSerialOffset = Summary.SerialOffset;
/// increase header size
Summary.HeaderSize += Entry.EntrySize;
/// add entry
++Summary.ExportCount;
if (Entry.SerialSize < 16) /// PrevObject + NoneIdx + NextRef
{
Entry.SerialSize = 16;
}
Entry.SerialOffset = UPKFileSize + Entry.EntrySize;
ExportTable.push_back(Entry);
/// increase offsets
Summary.DependsOffset += Entry.EntrySize;
Summary.SerialOffset += Entry.EntrySize;
for (unsigned i = 1; i <= oldExportCount; ++i)
{
ExportTable[i].SerialOffset += Entry.EntrySize;
}
/// backup serialized export data into memory
UPKFile.clear();
UPKFile.seekg(oldSerialOffset);
std::vector<char> serializedData(UPKFileSize - oldSerialOffset);
UPKFile.read(serializedData.data(), serializedData.size());
/// rewrite package
UPKFile.seekp(0);
/// serialize header
std::vector<char> serializedHeader = SerializeHeader();
/// write serialized header
UPKFile.write(serializedHeader.data(), serializedHeader.size());
/// write serialized export data
UPKFile.write(serializedData.data(), serializedData.size());
/// write new export serialized data
std::vector<char> serializedEntry(Entry.SerialSize);
UObjectReference PrevObjRef = oldExportCount;
memcpy(serializedEntry.data(), reinterpret_cast<char*>(&PrevObjRef), sizeof(PrevObjRef));
memcpy(serializedEntry.data() + sizeof(PrevObjRef), reinterpret_cast<char*>(&NoneIdx), sizeof(NoneIdx));
UPKFile.write(serializedEntry.data(), serializedEntry.size());
/// reload package
UPKUtils::Reload();
/// link export object to owner
LinkChild(Entry.OwnerRef, Summary.ExportCount);
return true;
}
bool UPKUtils::LinkChild(UObjectReference OwnerRef, UObjectReference ChildRef)
{
if (OwnerRef < 1 || OwnerRef >= (int)ExportTable.size())
return false;
UObject* Obj;
/// deserialize owner object to get first child
Obj = UObjectFactory::Create(ExportTable[OwnerRef].Type);
if (Obj == nullptr || Obj->IsStructure() == false)
{
return false;
}
UPKFile.seekg(ExportTable[OwnerRef].SerialOffset);
Obj->SetRef(OwnerRef);
Obj->SetUnsafe(false);
Obj->SetQuickMode(true);
Obj->Deserialize(UPKFile, *dynamic_cast<UPKInfo*>(this));
UStruct* StructObj = dynamic_cast<UStruct*>(Obj);
if (StructObj == nullptr)
{
delete Obj;
return false;
}
UObjectReference FirstChildRef = StructObj->GetFirstChildRef();
/// owner has no children
if (FirstChildRef == 0)
{
/// link child to owner
UPKFile.seekg(StructObj->GetFirstChildRefOffset());
UPKFile.write(reinterpret_cast<char*>(&ChildRef), sizeof(ChildRef));
delete Obj;
return true;
}
delete Obj;
/// find last child
UObjectReference NextRef = FirstChildRef;
size_t LastRefOffset = 0;
while (NextRef != 0)
{
Obj = UObjectFactory::Create(GlobalType::UField);
if (Obj == nullptr)
{
return false;
}
UPKFile.seekg(ExportTable[NextRef].SerialOffset);
Obj->SetRef(NextRef);
Obj->SetUnsafe(false);
Obj->SetQuickMode(true);
Obj->Deserialize(UPKFile, *dynamic_cast<UPKInfo*>(this));
UField* FieldObj = dynamic_cast<UField*>(Obj);
if (FieldObj == nullptr)
{
delete Obj;
return false;
}
NextRef = FieldObj->GetNextRef();
LastRefOffset = FieldObj->GetNextRefOffset();
delete Obj;
}
/// link new child to last child
UPKFile.seekg(LastRefOffset);
UPKFile.write(reinterpret_cast<char*>(&ChildRef), sizeof(ChildRef));
return true;
}
bool UPKUtils::Deserialize(FNameEntry& entry, std::vector<char>& data)
{
if (data.size() < 12)
{
return false;
}
std::stringstream ss;
ss.write(data.data(), data.size());
ss.read(reinterpret_cast<char*>(&entry.NameLength), 4);
if (data.size() != 12U + entry.NameLength)
{
return false;
}
if (entry.NameLength > 0)
{
getline(ss, entry.Name, '\0');
}
else
{
entry.Name = "";
}
ss.read(reinterpret_cast<char*>(&entry.NameFlagsL), 4);
ss.read(reinterpret_cast<char*>(&entry.NameFlagsH), 4);
/// memory variables
entry.EntrySize = data.size();
return true;
}
bool UPKUtils::Deserialize(FObjectImport& entry, std::vector<char>& data)
{
if (data.size() != 28)
{
return false;
}
std::stringstream ss;
ss.write(data.data(), data.size());
ss.read(reinterpret_cast<char*>(&entry.PackageIdx), sizeof(entry.PackageIdx));
ss.read(reinterpret_cast<char*>(&entry.TypeIdx), sizeof(entry.TypeIdx));
ss.read(reinterpret_cast<char*>(&entry.OwnerRef), sizeof(entry.OwnerRef));
ss.read(reinterpret_cast<char*>(&entry.NameIdx), sizeof(entry.NameIdx));
/// memory variables
entry.EntrySize = data.size();
entry.Name = IndexToName(entry.NameIdx);
entry.FullName = entry.Name;
if (entry.OwnerRef != 0)
{
entry.FullName = ResolveFullName(entry.OwnerRef) + "." + entry.Name;
}
entry.Type = IndexToName(entry.TypeIdx);
if (entry.Type == "")
{
entry.Type = "Class";
}
return true;
}
bool UPKUtils::Deserialize(FObjectExport& entry, std::vector<char>& data)
{
if (data.size() < 68)
{
return false;
}
std::stringstream ss;
ss.write(data.data(), data.size());
ss.read(reinterpret_cast<char*>(&entry.TypeRef), sizeof(entry.TypeRef));
ss.read(reinterpret_cast<char*>(&entry.ParentClassRef), sizeof(entry.ParentClassRef));
ss.read(reinterpret_cast<char*>(&entry.OwnerRef), sizeof(entry.OwnerRef));
ss.read(reinterpret_cast<char*>(&entry.NameIdx), sizeof(entry.NameIdx));
ss.read(reinterpret_cast<char*>(&entry.ArchetypeRef), sizeof(entry.ArchetypeRef));
ss.read(reinterpret_cast<char*>(&entry.ObjectFlagsH), sizeof(entry.ObjectFlagsH));
ss.read(reinterpret_cast<char*>(&entry.ObjectFlagsL), sizeof(entry.ObjectFlagsL));
ss.read(reinterpret_cast<char*>(&entry.SerialSize), sizeof(entry.SerialSize));
ss.read(reinterpret_cast<char*>(&entry.SerialOffset), sizeof(entry.SerialOffset));
ss.read(reinterpret_cast<char*>(&entry.ExportFlags), sizeof(entry.ExportFlags));
ss.read(reinterpret_cast<char*>(&entry.NetObjectCount), sizeof(entry.NetObjectCount));
ss.read(reinterpret_cast<char*>(&entry.GUID), sizeof(entry.GUID));
ss.read(reinterpret_cast<char*>(&entry.Unknown1), sizeof(entry.Unknown1));
entry.NetObjects.resize(entry.NetObjectCount);
if (data.size() != 68 + entry.NetObjectCount)
{
return false;
}
if (entry.NetObjectCount > 0)
{
ss.read(reinterpret_cast<char*>(entry.NetObjects.data()), entry.NetObjects.size()*4);
}
/// memory variables
entry.EntrySize = data.size();
entry.Name = IndexToName(entry.NameIdx);
entry.FullName = entry.Name;
if (entry.OwnerRef != 0)
{
entry.FullName = ResolveFullName(entry.OwnerRef) + "." + entry.Name;
}
entry.Type = ObjRefToName(entry.TypeRef);
if (entry.Type == "")
{
entry.Type = "Class";
}
return true;
}
std::vector<char> UPKUtils::SerializeSummary()
{
std::stringstream ss;
ss.write(reinterpret_cast<char*>(&Summary.Signature), 4);
int32_t Ver = (Summary.LicenseeVersion << 16) + Summary.Version;
ss.write(reinterpret_cast<char*>(&Ver), 4);
ss.write(reinterpret_cast<char*>(&Summary.HeaderSize), 4);
ss.write(reinterpret_cast<char*>(&Summary.FolderNameLength), 4);
if (Summary.FolderNameLength > 0)
{
ss.write(Summary.FolderName.c_str(), Summary.FolderNameLength);
}
ss.write(reinterpret_cast<char*>(&Summary.PackageFlags), 4);
ss.write(reinterpret_cast<char*>(&Summary.NameCount), 4);
ss.write(reinterpret_cast<char*>(&Summary.NameOffset), 4);
ss.write(reinterpret_cast<char*>(&Summary.ExportCount), 4);
ss.write(reinterpret_cast<char*>(&Summary.ExportOffset), 4);
ss.write(reinterpret_cast<char*>(&Summary.ImportCount), 4);
ss.write(reinterpret_cast<char*>(&Summary.ImportOffset), 4);
ss.write(reinterpret_cast<char*>(&Summary.DependsOffset), 4);
if (Summary.Version >= VER_BATMAN_CITY)
{
ss.write(reinterpret_cast<char*>(&Summary.SerialOffset), 4);
ss.write(reinterpret_cast<char*>(&Summary.Unknown2), 4);
ss.write(reinterpret_cast<char*>(&Summary.Unknown3), 4);
ss.write(reinterpret_cast<char*>(&Summary.Unknown4), 4);
}
ss.write(reinterpret_cast<char*>(&Summary.GUID), sizeof(Summary.GUID));
ss.write(reinterpret_cast<char*>(&Summary.GenerationsCount), 4);
for (unsigned i = 0; i < Summary.GenerationsCount; ++i)
{
FGenerationInfo Entry = Summary.Generations[i];
ss.write(reinterpret_cast<char*>(&Entry.ExportCount), 4);