forked from trumank/kismet-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kismet.cs
1212 lines (1143 loc) · 57.7 KB
/
Kismet.cs
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
namespace KismetAnalyzer;
using UAssetAPI;
using UAssetAPI.UnrealTypes;
using UAssetAPI.ExportTypes;
using UAssetAPI.FieldTypes;
using UAssetAPI.PropertyTypes.Objects;
using UAssetAPI.Kismet.Bytecode;
using UAssetAPI.Kismet.Bytecode.Expressions;
public class Kismet {
public static void Walk(UAsset asset, KismetExpression ex, Action<KismetExpression> func) {
uint offset = 0;
Walk(asset, ref offset, ex, (ex, offset) => func(ex));
}
public static void Walk(UAsset asset, ref uint offset, KismetExpression ex, Action<KismetExpression, uint> func) {
func(ex, offset);
offset++;
switch (ex) {
case EX_FieldPathConst e:
Walk(asset, ref offset, e.Value, func);
break;
case EX_SoftObjectConst e:
Walk(asset, ref offset, e.Value, func);
break;
case EX_AddMulticastDelegate e:
Walk(asset, ref offset, e.Delegate, func);
Walk(asset, ref offset, e.DelegateToAdd, func);
break;
case EX_ArrayConst e:
offset += 8;
foreach (var p in e.Elements) Walk(asset, ref offset, p, func);
break;
case EX_ArrayGetByRef e:
Walk(asset, ref offset, e.ArrayVariable, func);
Walk(asset, ref offset, e.ArrayIndex, func);
break;
case EX_Assert e:
offset += 3;
Walk(asset, ref offset, e.AssertExpression, func);
break;
case EX_BindDelegate e:
offset += 12;
Walk(asset, ref offset, e.Delegate, func);
Walk(asset, ref offset, e.ObjectTerm, func);
break;
case EX_CallMath e:
offset += 8;
foreach (var p in e.Parameters) Walk(asset, ref offset, p, func);
offset += 1;
break;
case EX_CallMulticastDelegate e:
offset += 8;
Walk(asset, ref offset, e.Delegate, func);
foreach (var p in e.Parameters) Walk(asset, ref offset, p, func);
offset += 1;
break;
case EX_ClearMulticastDelegate e:
Walk(asset, ref offset, e.DelegateToClear, func);
break;
case EX_ComputedJump e:
Walk(asset, ref offset, e.CodeOffsetExpression, func);
break;
case EX_Context e: // +EX_Context_FailSilent +EX_ClassContext
Walk(asset, ref offset, e.ObjectExpression, func);
offset += 12;
Walk(asset, ref offset, e.ContextExpression, func);
break;
case EX_CrossInterfaceCast e:
offset += 8;
Walk(asset, ref offset, e.Target, func);
break;
case EX_DynamicCast e:
offset += 8;
Walk(asset, ref offset, e.TargetExpression, func);
break;
case EX_FinalFunction e: // +EX_LocalFinalFunction
offset += 8;
foreach (var p in e.Parameters) Walk(asset, ref offset, p, func);
offset += 1;
break;
case EX_InterfaceContext e:
Walk(asset, ref offset, e.InterfaceValue, func);
break;
case EX_InterfaceToObjCast e:
offset += 8;
Walk(asset, ref offset, e.Target, func);
break;
case EX_JumpIfNot e:
offset += 4;
Walk(asset, ref offset, e.BooleanExpression, func);
break;
case EX_Let e:
offset += 8;
Walk(asset, ref offset, e.Variable, func);
Walk(asset, ref offset, e.Expression, func);
break;
case EX_LetBool e:
Walk(asset, ref offset, e.VariableExpression, func);
Walk(asset, ref offset, e.AssignmentExpression, func);
break;
case EX_LetDelegate e:
Walk(asset, ref offset, e.VariableExpression, func);
Walk(asset, ref offset, e.AssignmentExpression, func);
break;
case EX_LetMulticastDelegate e:
Walk(asset, ref offset, e.VariableExpression, func);
Walk(asset, ref offset, e.AssignmentExpression, func);
break;
case EX_LetObj e:
Walk(asset, ref offset, e.VariableExpression, func);
Walk(asset, ref offset, e.AssignmentExpression, func);
break;
case EX_LetValueOnPersistentFrame e:
offset += 8;
Walk(asset, ref offset, e.AssignmentExpression, func);
break;
case EX_LetWeakObjPtr e:
Walk(asset, ref offset, e.VariableExpression, func);
Walk(asset, ref offset, e.AssignmentExpression, func);
break;
case EX_VirtualFunction e: // +EX_LocalVirtualFunction
offset += 12;
foreach (var p in e.Parameters) Walk(asset, ref offset, p, func);
offset += 1;
break;
case EX_MapConst e:
offset += 20;
foreach (var p in e.Elements) Walk(asset, ref offset, p, func);
break;
case EX_MetaCast e:
offset += 8;
Walk(asset, ref offset, e.TargetExpression, func);
break;
case EX_ObjToInterfaceCast e:
offset += 8;
Walk(asset, ref offset, e.Target, func);
break;
case EX_PopExecutionFlowIfNot e:
Walk(asset, ref offset, e.BooleanExpression, func);
break;
case EX_PrimitiveCast e:
offset += 1;
Walk(asset, ref offset, e.Target, func);
break;
case EX_RemoveMulticastDelegate e:
Walk(asset, ref offset, e.Delegate, func);
Walk(asset, ref offset, e.DelegateToAdd, func);
break;
case EX_Return e:
Walk(asset, ref offset, e.ReturnExpression, func);
break;
case EX_SetArray e:
Walk(asset, ref offset, e.AssigningProperty, func);
foreach (var p in e.Elements) Walk(asset, ref offset, p, func);
offset += 1;
break;
case EX_SetConst e:
offset += 12;
foreach (var p in e.Elements) Walk(asset, ref offset, p, func);
offset += 1;
break;
case EX_SetMap e:
Walk(asset, ref offset, e.MapProperty, func);
offset += 4;
foreach (var p in e.Elements) Walk(asset, ref offset, p, func);
break;
case EX_SetSet e:
Walk(asset, ref offset, e.SetProperty, func);
offset += 4;
foreach (var p in e.Elements) Walk(asset, ref offset, p, func);
break;
case EX_Skip e:
offset += 4;
Walk(asset, ref offset, e.SkipExpression, func);
break;
case EX_StructConst e:
offset += 12;
foreach (var p in e.Value) Walk(asset, ref offset, p, func);
offset += 1;
break;
case EX_StructMemberContext e:
offset += 8;
Walk(asset, ref offset, e.StructExpression, func);
break;
case EX_SwitchValue e:
offset += 6;
Walk(asset, ref offset, e.IndexTerm, func);
foreach (var p in e.Cases) {
Walk(asset, ref offset, p.CaseIndexValueTerm, func);
offset += 4;
Walk(asset, ref offset, p.CaseTerm, func);
}
Walk(asset, ref offset, e.DefaultTerm, func);
break;
case EX_TextConst e:
offset += 1;
switch (e.Value.TextLiteralType) {
case EBlueprintTextLiteralType.Empty:
break;
case EBlueprintTextLiteralType.LocalizedText:
Walk(asset, ref offset, e.Value.LocalizedSource, func);
Walk(asset, ref offset, e.Value.LocalizedKey, func);
Walk(asset, ref offset, e.Value.LocalizedNamespace, func);
break;
case EBlueprintTextLiteralType.InvariantText:
Walk(asset, ref offset, e.Value.InvariantLiteralString, func);
break;
case EBlueprintTextLiteralType.LiteralString:
Walk(asset, ref offset, e.Value.LiteralString, func);
break;
case EBlueprintTextLiteralType.StringTableEntry:
offset += 8;
Walk(asset, ref offset, e.Value.StringTableId, func);
Walk(asset, ref offset, e.Value.StringTableKey, func);
break;
default:
throw new NotImplementedException();
};
break;
default:
offset += GetSize(asset, ex) - 1;
break;
}
}
public static uint GetSize(UAsset asset, KismetExpression exp) {
return 1 + exp switch
{
EX_PushExecutionFlow => 4,
EX_ComputedJump e => GetSize(asset, e.CodeOffsetExpression),
EX_Jump e => 4,
EX_JumpIfNot e => 4 + GetSize(asset, e.BooleanExpression),
EX_LocalVariable e => 8,
EX_DefaultVariable e => 8,
EX_ObjToInterfaceCast e => 8 + GetSize(asset, e.Target),
EX_Let e => 8 + GetSize(asset, e.Variable) + GetSize(asset, e.Expression),
EX_LetObj e => GetSize(asset, e.VariableExpression) + GetSize(asset, e.AssignmentExpression),
EX_LetBool e => GetSize(asset, e.VariableExpression) + GetSize(asset, e.AssignmentExpression),
EX_LetWeakObjPtr e => GetSize(asset, e.VariableExpression) + GetSize(asset, e.AssignmentExpression),
EX_LetValueOnPersistentFrame e => 8 + GetSize(asset, e.AssignmentExpression),
EX_StructMemberContext e => 8 + GetSize(asset, e.StructExpression),
EX_MetaCast e => 8 + GetSize(asset, e.TargetExpression),
EX_DynamicCast e => 8 + GetSize(asset, e.TargetExpression),
EX_PrimitiveCast e => 1 + e.ConversionType switch { ECastToken.ObjectToInterface => 8U, /* TODO InterfaceClass */ _ => 0U} + GetSize(asset, e.Target),
EX_PopExecutionFlow e => 0,
EX_PopExecutionFlowIfNot e => GetSize(asset, e.BooleanExpression),
EX_CallMath e => 8 + e.Parameters.Select(p => GetSize(asset, p)).Aggregate(0U, (acc, x) => x + acc) + 1,
EX_SwitchValue e => 6 + GetSize(asset, e.IndexTerm) + e.Cases.Select(c => GetSize(asset, c.CaseIndexValueTerm) + 4 + GetSize(asset, c.CaseTerm)).Aggregate(0U, (acc, x) => x + acc) + GetSize(asset, e.DefaultTerm),
EX_Self => 0,
EX_TextConst e =>
1 + e.Value.TextLiteralType switch
{
EBlueprintTextLiteralType.Empty => 0,
EBlueprintTextLiteralType.LocalizedText => GetSize(asset, e.Value.LocalizedSource) + GetSize(asset, e.Value.LocalizedKey) + GetSize(asset, e.Value.LocalizedNamespace),
EBlueprintTextLiteralType.InvariantText => GetSize(asset, e.Value.InvariantLiteralString),
EBlueprintTextLiteralType.LiteralString => GetSize(asset, e.Value.LiteralString),
EBlueprintTextLiteralType.StringTableEntry => 8 + GetSize(asset, e.Value.StringTableId) + GetSize(asset, e.Value.StringTableKey),
_ => throw new NotImplementedException(),
},
EX_ObjectConst e => 8,
EX_VectorConst e => asset.ObjectVersionUE5 >= ObjectVersionUE5.LARGE_WORLD_COORDINATES ? 24U : 12U,
EX_RotationConst e => asset.ObjectVersionUE5 >= ObjectVersionUE5.LARGE_WORLD_COORDINATES ? 24U : 12U,
EX_TransformConst e => asset.ObjectVersionUE5 >= ObjectVersionUE5.LARGE_WORLD_COORDINATES ? 80U : 40U,
EX_Context e => + GetSize(asset, e.ObjectExpression) + 4 + 8 + GetSize(asset, e.ContextExpression),
EX_CallMulticastDelegate e => 8 + GetSize(asset, e.Delegate) + e.Parameters.Select(p => GetSize(asset, p)).Aggregate(0U, (acc, x) => x + acc) + 1,
EX_LocalFinalFunction e => 8 + e.Parameters.Select(p => GetSize(asset, p)).Aggregate(0U, (acc, x) => x + acc) + 1,
EX_FinalFunction e => 8 + e.Parameters.Select(p => GetSize(asset, p)).Aggregate(0U, (acc, x) => x + acc) + 1,
EX_LocalVirtualFunction e => 12 + e.Parameters.Select(p => GetSize(asset, p)).Aggregate(0U, (acc, x) => x + acc) + 1,
EX_VirtualFunction e => 12 + e.Parameters.Select(p => GetSize(asset, p)).Aggregate(0U, (acc, x) => x + acc) + 1,
EX_InstanceVariable e => 8,
EX_AddMulticastDelegate e => GetSize(asset, e.Delegate) + GetSize(asset, e.DelegateToAdd),
EX_RemoveMulticastDelegate e => GetSize(asset, e.Delegate) + GetSize(asset, e.DelegateToAdd),
EX_ClearMulticastDelegate e => GetSize(asset, e.DelegateToClear),
EX_BindDelegate e => 12 + GetSize(asset, e.Delegate) + GetSize(asset, e.ObjectTerm),
EX_StructConst e => 8 + 4 + e.Value.Select(p => GetSize(asset, p)).Aggregate(0U, (acc, x) => x + acc) + 1,
EX_SetArray e => GetSize(asset, e.AssigningProperty) + e.Elements.Select(p => GetSize(asset, p)).Aggregate(0U, (acc, x) => x + acc) + 1,
EX_SetMap e => GetSize(asset, e.MapProperty) + 4 + e.Elements.Select(p => GetSize(asset, p)).Aggregate(0U, (acc, x) => x + acc) + 1,
EX_SetSet e => GetSize(asset, e.SetProperty) + 4 + e.Elements.Select(p => GetSize(asset, p)).Aggregate(0U, (acc, x) => x + acc) + 1,
EX_SoftObjectConst e => GetSize(asset, e.Value),
EX_ByteConst e => 1,
EX_IntConst e => 4,
EX_FloatConst e => 4,
EX_Int64Const e => 8,
EX_UInt64Const e => 8,
EX_NameConst e => 12,
EX_StringConst e => (uint) e.Value.Length + 1,
EX_UnicodeStringConst e => 2 * ((uint) e.Value.Length + 1),
EX_SkipOffsetConst e => 4,
EX_ArrayConst e => 12 + e.Elements.Select(p => GetSize(asset, p)).Aggregate(0U, (acc, x) => x + acc) + 1,
EX_Return e => GetSize(asset, e.ReturnExpression),
EX_LocalOutVariable e => 8,
EX_InterfaceContext e => GetSize(asset, e.InterfaceValue),
EX_InterfaceToObjCast e => 8 + GetSize(asset, e.Target),
EX_ArrayGetByRef e => GetSize(asset, e.ArrayVariable) + GetSize(asset, e.ArrayIndex),
EX_True e => 0,
EX_False e => 0,
EX_Nothing e => 0,
EX_NoObject e => 0,
EX_EndOfScript e => 0,
EX_Tracepoint e => 0,
EX_WireTracepoint e => 0,
_ => throw new NotImplementedException(exp.ToString()),
};
}
public static void ShiftAddressses(KismetExpression exp, int offset) {
switch (exp) {
case EX_PushExecutionFlow e:
{
e.PushingAddress = (uint) (e.PushingAddress + offset);
break;
}
case EX_ComputedJump e:
{
// TODO
break;
}
case EX_Jump e:
{
e.CodeOffset = (uint) (e.CodeOffset + offset);
break;
}
case EX_JumpIfNot e:
{
e.CodeOffset = (uint) (e.CodeOffset + offset);
break;
}
case EX_LocalFinalFunction e:
{
// TODO: Handle addressess in ubergraph
break;
}
case EX_StructConst e:
{
// TODO handle LatentActionInfo addresses (only can be in ubergraph)
break;
}
case EX_SkipOffsetConst e:
{
// referencedAddresses.Add(new Reference(e.Value, ReferenceType.Skip));
// TODO
break;
}
}
}
public static FPackageIndex? CopyExportTo((UAsset, FPackageIndex?) export, UAsset dst) {
if (export.Item2 == null) return null;
if (export.Item2.IsNull()) return export.Item2;
// could potentially check if there is an existing matchin export but that's complex
// maybe can check by name?
var exp = export.Item2.ToExport(export.Item1);
switch (exp) {
case FunctionExport e:
{
var src = export.Item1;
var fnDst = new FunctionExport() {
IsInheritedInstance = e.IsInheritedInstance,
GeneratePublicHash = e.GeneratePublicHash,
//StructExport
SuperStruct = CopyImportTo((src, e.SuperStruct), dst),
Children = new FPackageIndex[0],
LoadedProperties = new FProperty[0], // TODO
// ScriptBytecode = filled later
//FieldExport
Field = new UField(),
//NormalExport
Data = new List<PropertyData>(), // TODO?
//Export
ClassIndex = CopyImportTo((src, e.ClassIndex), dst),
SuperIndex = CopyImportTo((src, e.SuperIndex), dst),
TemplateIndex = CopyImportTo((src, e.TemplateIndex), dst),
ObjectFlags = e.ObjectFlags,
bForcedExport = e.bForcedExport,
bNotForClient = e.bNotForClient,
bNotForServer = e.bNotForServer,
PackageGuid = e.PackageGuid,
PackageFlags = e.PackageFlags,
bNotAlwaysLoadedForEditorGame = e.bNotAlwaysLoadedForEditorGame,
bIsAsset = e.bIsAsset,
SerializationBeforeSerializationDependencies = new List<FPackageIndex>(),
CreateBeforeSerializationDependencies = new List<FPackageIndex>(),
SerializationBeforeCreateDependencies = new List<FPackageIndex>(),
CreateBeforeCreateDependencies = new List<FPackageIndex>(),
Asset = dst,
//FObjectResource
ObjectName = e.ObjectName.Transfer(dst),
OuterIndex = FPackageIndex.FromExport(dst.Exports.IndexOf(dst.GetClassExport())),
//Export
Extras = e.Extras,
};
var fnPiDst = FPackageIndex.FromExport(dst.Exports.Count() - 1);
dst.Exports.Add(fnDst);
fnDst.ScriptBytecode = e.ScriptBytecode.Select(expression => CopyExpressionTo(expression, src, dst, e, fnDst)).ToArray();
return fnPiDst;
}
default:
throw new NotImplementedException($"Export {exp} not implemented");
}
/*
if (imp.OuterIndex.IsNull()) {
return asset.AddImport(new Import(imp.ClassPackage.ToString(), imp.ClassName.ToString(), FPackageIndex.FromRawIndex(0), imp.ObjectName.ToString(), false, asset));
} else {
return asset.AddImport(new Import(imp.ClassPackage.ToString(), imp.ClassName.ToString(), CopyImportTo((import.Item1, imp.OuterIndex), asset), imp.ObjectName.ToString(), false, asset));
}
*/
}
public static FPackageIndex? CopyImportTo((UAsset, FPackageIndex?) import, UAsset asset) {
if (import.Item2 == null) return null;
if (import.Item2.IsNull()) return import.Item2;
for (int i = 0; i < asset.Imports.Count; i++) {
var existing = FPackageIndex.FromImport(i);
if (AreImportsEqual(import, (asset, existing))) return existing;
}
var imp = import.Item2.ToImport(import.Item1);
if (imp.OuterIndex.IsNull()) {
return asset.AddImport(new Import(imp.ClassPackage.ToString(), imp.ClassName.ToString(), FPackageIndex.FromRawIndex(0), imp.ObjectName.ToString(), false, asset));
} else {
return asset.AddImport(new Import(imp.ClassPackage.ToString(), imp.ClassName.ToString(), CopyImportTo((import.Item1, imp.OuterIndex), asset), imp.ObjectName.ToString(), false, asset));
}
}
static bool AreImportsEqual((UAsset, FPackageIndex?) a, (UAsset, FPackageIndex?) b) {
if (a.Item2 == null || b.Item2 == null) {
return a.Item2 == null && b.Item2 == null;
} if (a.Item2.IsNull() || b.Item2.IsNull()) {
return a.Item2.IsNull() && b.Item2.IsNull();
}
var importA = a.Item2.ToImport(a.Item1);
var importB = b.Item2.ToImport(b.Item1);
return importA.ClassPackage == importB.ClassPackage
&& importA.ClassName == importB.ClassName
&& importA.ObjectName == importB.ObjectName
&& AreImportsEqual((a.Item1, importA.OuterIndex), (b.Item1, importB.OuterIndex));
}
public static FProperty? CopyProperty(FProperty? prop, UAsset src, UAsset dst) {
switch (prop) {
case FGenericProperty p:
{
return new FGenericProperty() {
ArrayDim = p.ArrayDim,
ElementSize = p.ElementSize,
PropertyFlags = p.PropertyFlags,
RepIndex = p.RepIndex,
RepNotifyFunc = p.RepNotifyFunc.Transfer(dst),
BlueprintReplicationCondition = p.BlueprintReplicationCondition,
RawValue = p.RawValue, // TODO is this ever not null?
SerializedType = p.SerializedType.Transfer(dst),
Name = p.Name.Transfer(dst),
Flags = p.Flags,
};
}
case FObjectProperty p:
{
return new FObjectProperty() {
PropertyClass = CopyImportTo((src, p.PropertyClass), dst),
ArrayDim = p.ArrayDim,
ElementSize = p.ElementSize,
PropertyFlags = p.PropertyFlags,
RepIndex = p.RepIndex,
RepNotifyFunc = p.RepNotifyFunc.Transfer(dst),
BlueprintReplicationCondition = p.BlueprintReplicationCondition,
RawValue = p.RawValue, // TODO is this ever not null?
SerializedType = p.SerializedType.Transfer(dst),
Name = p.Name.Transfer(dst),
Flags = p.Flags,
};
}
case FInterfaceProperty p:
{
return new FInterfaceProperty() {
InterfaceClass = CopyImportTo((src, p.InterfaceClass), dst),
ArrayDim = p.ArrayDim,
ElementSize = p.ElementSize,
PropertyFlags = p.PropertyFlags,
RepIndex = p.RepIndex,
RepNotifyFunc = p.RepNotifyFunc.Transfer(dst),
BlueprintReplicationCondition = p.BlueprintReplicationCondition,
RawValue = p.RawValue, // TODO is this ever not null?
SerializedType = p.SerializedType.Transfer(dst),
Name = p.Name.Transfer(dst),
Flags = p.Flags,
};
}
case FStructProperty p:
{
return new FStructProperty() {
Struct = CopyImportTo((src, p.Struct), dst),
ArrayDim = p.ArrayDim,
ElementSize = p.ElementSize,
PropertyFlags = p.PropertyFlags,
RepIndex = p.RepIndex,
RepNotifyFunc = p.RepNotifyFunc.Transfer(dst),
BlueprintReplicationCondition = p.BlueprintReplicationCondition,
RawValue = p.RawValue, // TODO is this ever not null?
SerializedType = p.SerializedType.Transfer(dst),
Name = p.Name.Transfer(dst),
Flags = p.Flags,
};
}
case FArrayProperty p:
{
return new FArrayProperty() {
Inner = CopyProperty(p.Inner, src, dst),
ArrayDim = p.ArrayDim,
ElementSize = p.ElementSize,
PropertyFlags = p.PropertyFlags,
RepIndex = p.RepIndex,
RepNotifyFunc = p.RepNotifyFunc.Transfer(dst),
BlueprintReplicationCondition = p.BlueprintReplicationCondition,
RawValue = p.RawValue, // TODO is this ever not null?
SerializedType = p.SerializedType.Transfer(dst),
Name = p.Name.Transfer(dst),
Flags = p.Flags,
};
}
case FBoolProperty p:
{
return new FBoolProperty() {
FieldSize = p.FieldSize,
ByteOffset = p.ByteOffset,
ByteMask = p.ByteMask,
FieldMask = p.FieldMask,
NativeBool = p.NativeBool,
Value = p.Value,
ArrayDim = p.ArrayDim,
ElementSize = p.ElementSize,
PropertyFlags = p.PropertyFlags,
RepIndex = p.RepIndex,
RepNotifyFunc = p.RepNotifyFunc.Transfer(dst),
BlueprintReplicationCondition = p.BlueprintReplicationCondition,
RawValue = p.RawValue, // TODO is this ever not null?
SerializedType = p.SerializedType.Transfer(dst),
Name = p.Name.Transfer(dst),
Flags = p.Flags,
};
}
}
throw new NotImplementedException($"FProperty {prop} not implemented");
}
public static UProperty CopyUProperty(UProperty prop, UAsset src, UAsset dst) {
switch (prop) {
case UObjectProperty p:
return new UObjectProperty() {
// UField
Next = null,
// UProperty
ArrayDim = p.ArrayDim,
ElementSize = p.ElementSize,
PropertyFlags = p.PropertyFlags,
RepNotifyFunc = p.RepNotifyFunc.Transfer(dst),
BlueprintReplicationCondition = p.BlueprintReplicationCondition,
RawValue = p.RawValue,
// UObjectProperty
PropertyClass = CopyImportTo((src, p.PropertyClass), dst),
};
case UStructProperty p:
return new UStructProperty() {
// UField
Next = null,
// UProperty
ArrayDim = p.ArrayDim,
ElementSize = p.ElementSize,
PropertyFlags = p.PropertyFlags,
RepNotifyFunc = p.RepNotifyFunc.Transfer(dst),
BlueprintReplicationCondition = p.BlueprintReplicationCondition,
RawValue = p.RawValue,
// UStructProperty
Struct = CopyImportTo((src, p.Struct), dst),
};
}
throw new NotImplementedException($"UProperty {prop} not implemented");
}
public static List<FPackageIndex> GetDependencies(UProperty prop) {
var dependencies = new List<FPackageIndex>();
switch (prop) {
case UObjectProperty p:
dependencies.Add(p.PropertyClass);
break;
case UStructProperty p:
dependencies.Add(p.Struct);
break;
}
return dependencies;
}
public static FFieldPath? CopyFieldPath(FFieldPath? p, UAsset src, UAsset dst, FunctionExport fnSrc, FunctionExport fnDst) {
if (p == null) return null;
if (p.ResolvedOwner.IsNull()) {
return new FFieldPath() {
Path = p.Path.Select(p => p.Transfer(dst)).ToArray(),
ResolvedOwner = FPackageIndex.FromRawIndex(0),
};
}
if (p.ResolvedOwner.IsImport()) {
return new FFieldPath() {
Path = p.Path.Select(p => p.Transfer(dst)).ToArray(),
ResolvedOwner = CopyImportTo((src, p.ResolvedOwner), dst),
};
}
if (p.Path.Length > 1) throw new NotImplementedException($"FFieldPath.Length > 1: {String.Join(", ", p.Path.Select(p => p.ToString()))}");
if (p.ResolvedOwner.ToExport(src) == fnSrc) {
var prop = fnSrc.LoadedProperties.First(l => l.Name.ToString() == p.Path[0].ToString());
if (prop == null) throw new NotImplementedException("Property missing");
var existing = fnDst.LoadedProperties.FirstOrDefault(l => l.Name.ToString() == p.Path[0].ToString(), null);
if (existing == null) { // prop doesn't already exist so copy it over
// TODO check type of prop == existing, only checking by name currently
fnDst.LoadedProperties = fnDst.LoadedProperties.Append(CopyProperty(prop, src, dst)).ToArray();
}
return new FFieldPath() {
Path = p.Path.Select(p => p.Transfer(dst)).ToArray(),
ResolvedOwner = FPackageIndex.FromExport(dst.Exports.IndexOf(fnDst)), // TODO avoid IndexOf
};
}
throw new NotImplementedException("FFieldPath points to an export that is not the source function");
}
public static FPackageIndex? CopyPropertyExport(FPackageIndex p, UAsset src, UAsset dst, FunctionExport fnSrc, FunctionExport fnDst) {
if (p == null) return null;
if (p.ToExport(src) is PropertyExport property) {
var fnPiSrc = FPackageIndex.FromExport(src.Exports.IndexOf(fnSrc)); // TODO avoid IndexOf
var fnPiDst = FPackageIndex.FromExport(dst.Exports.IndexOf(fnDst)); // TODO avoid IndexOf
var existing = dst.Exports.FindIndex(e => e is PropertyExport && e.OuterIndex.Equals(fnPiDst) && e.ObjectName.ToString() == property.ObjectName.ToString());
if (existing != -1) {
return FPackageIndex.FromExport(existing);
}
var uprop = CopyUProperty(property.Property, src, dst);
var newProperty = new PropertyExport() {
//PropertyExport
Property = uprop,
//NormalExport
Data = new List<PropertyData>(), // TODO?
//Export
ClassIndex = CopyImportTo((src, property.ClassIndex), dst),
SuperIndex = CopyImportTo((src, property.SuperIndex), dst),
TemplateIndex = CopyImportTo((src, property.TemplateIndex), dst),
ObjectFlags = property.ObjectFlags,
bForcedExport = property.bForcedExport,
bNotForClient = property.bNotForClient,
bNotForServer = property.bNotForServer,
PackageGuid = property.PackageGuid,
PackageFlags = property.PackageFlags,
bNotAlwaysLoadedForEditorGame = property.bNotAlwaysLoadedForEditorGame,
bIsAsset = property.bIsAsset,
SerializationBeforeSerializationDependencies = new List<FPackageIndex>(),
CreateBeforeSerializationDependencies = GetDependencies(uprop),
SerializationBeforeCreateDependencies = new List<FPackageIndex>(),
CreateBeforeCreateDependencies = new List<FPackageIndex>() {fnPiDst},
Asset = dst,
//FObjectResource
ObjectName = property.ObjectName.Transfer(dst),
OuterIndex = fnPiDst, // TODO don't assume this is owned by the function
//Export
Extras = property.Extras,
};
dst.Exports.Add(newProperty);
var pi = FPackageIndex.FromExport(dst.Exports.Count - 1);
fnDst.Children = fnDst.Children.Append(pi).ToArray();
fnDst.SerializationBeforeSerializationDependencies.Add(pi);
return pi;
} else {
throw new NotImplementedException("expected PropertyExport");
}
}
public static KismetPropertyPointer CopyKismetPropertyPointer(KismetPropertyPointer p, UAsset src, UAsset dst, FunctionExport fnSrc, FunctionExport fnDst) {
return new KismetPropertyPointer() {
Old = CopyPropertyExport(p.Old, src, dst, fnSrc, fnDst),
New = CopyFieldPath(p.New, src, dst, fnSrc, fnDst),
};
}
public static KismetExpression? CopyExpressionTo(KismetExpression? exp, UAsset src, UAsset dst, FunctionExport fnSrc, FunctionExport fnDst) {
if (exp == null) return null;
switch (exp) {
case EX_PushExecutionFlow e:
{
return new EX_PushExecutionFlow() {
PushingAddress = e.PushingAddress,
};
}
case EX_Context e:
{
return new EX_Context() {
ObjectExpression = CopyExpressionTo(e.ObjectExpression, src, dst, fnSrc, fnDst),
Offset = e.Offset,
RValuePointer = CopyKismetPropertyPointer(e.RValuePointer, src, dst, fnSrc, fnDst),
ContextExpression = CopyExpressionTo(e.ContextExpression, src, dst, fnSrc, fnDst),
};
}
case EX_ObjectConst e:
{
return new EX_ObjectConst() {
Value = CopyImportTo((src, e.Value), dst),
};
}
case EX_LocalVirtualFunction e:
{
return new EX_LocalVirtualFunction() {
VirtualFunctionName = e.VirtualFunctionName.Transfer(dst),
Parameters = e.Parameters.Select(p => CopyExpressionTo(p, src, dst, fnSrc, fnDst)).ToArray(),
};
}
case EX_SkipOffsetConst e:
{
return new EX_SkipOffsetConst() {
Value = e.Value,
};
}
case EX_ByteConst e:
{
return new EX_ByteConst() {
Value = e.Value,
};
}
case EX_IntConst e:
{
return new EX_IntConst() {
Value = e.Value,
};
}
case EX_FloatConst e:
{
return new EX_FloatConst() {
Value = e.Value,
};
}
case EX_DoubleConst e:
{
return new EX_DoubleConst() {
Value = e.Value,
};
}
case EX_VectorConst e:
{
return new EX_VectorConst() {
Value = e.Value,
};
}
case EX_RotationConst e:
{
return new EX_RotationConst() {
Value = e.Value,
};
}
case EX_StringConst e:
{
return new EX_StringConst() {
Value = e.Value,
};
}
case EX_NameConst e:
{
return new EX_NameConst() {
Value = e.Value.Transfer(dst),
};
}
case EX_TextConst e:
{
return new EX_TextConst() {
Value = new FScriptText() {
TextLiteralType = e.Value.TextLiteralType,
LocalizedSource = CopyExpressionTo(e.Value.LocalizedSource, src, dst, fnSrc, fnDst),
LocalizedKey = CopyExpressionTo(e.Value.LocalizedKey, src, dst, fnSrc, fnDst),
LocalizedNamespace = CopyExpressionTo(e.Value.LocalizedNamespace, src, dst, fnSrc, fnDst),
InvariantLiteralString = CopyExpressionTo(e.Value.InvariantLiteralString, src, dst, fnSrc, fnDst),
LiteralString = CopyExpressionTo(e.Value.LiteralString, src, dst, fnSrc, fnDst),
StringTableAsset = CopyImportTo((src, e.Value.StringTableAsset), dst), // TODO likely not actually an import
StringTableId = CopyExpressionTo(e.Value.StringTableId, src, dst, fnSrc, fnDst),
StringTableKey = CopyExpressionTo(e.Value.StringTableKey, src, dst, fnSrc, fnDst),
},
};
}
case EX_True e:
{
return new EX_True();
}
case EX_False e:
{
return new EX_False();
}
case EX_Self e:
{
return new EX_Self();
}
case EX_NoObject e:
{
return new EX_NoObject();
}
case EX_Nothing e:
{
return new EX_Nothing();
}
case EX_EndOfScript e:
{
return new EX_EndOfScript();
}
case EX_Let e:
{
return new EX_Let() {
Value = CopyKismetPropertyPointer(e.Value, src, dst, fnSrc, fnDst),
Variable = CopyExpressionTo(e.Variable, src, dst, fnSrc, fnDst),
Expression = CopyExpressionTo(e.Expression, src, dst, fnSrc, fnDst),
};
}
case EX_LocalVariable e:
{
return new EX_LocalVariable() {
Variable = CopyKismetPropertyPointer(e.Variable, src, dst, fnSrc, fnDst),
};
}
case EX_LocalOutVariable e:
{
return new EX_LocalOutVariable() {
Variable = CopyKismetPropertyPointer(e.Variable, src, dst, fnSrc, fnDst),
};
}
case EX_CallMath e:
{
return new EX_CallMath() {
StackNode = CopyImportTo((src, e.StackNode), dst),
Parameters = e.Parameters.Select(p => CopyExpressionTo(p, src, dst, fnSrc, fnDst)).ToArray(),
};
}
case EX_FinalFunction e:
{
return new EX_FinalFunction() {
StackNode = CopyImportTo((src, e.StackNode), dst),
Parameters = e.Parameters.Select(p => CopyExpressionTo(p, src, dst, fnSrc, fnDst)).ToArray(),
};
}
case EX_Return e:
{
return new EX_Return() {
ReturnExpression = CopyExpressionTo(e.ReturnExpression, src, dst, fnSrc, fnDst),
};
}
case EX_LetObj e:
{
return new EX_LetObj() {
VariableExpression = CopyExpressionTo(e.VariableExpression, src, dst, fnSrc, fnDst),
AssignmentExpression = CopyExpressionTo(e.AssignmentExpression, src, dst, fnSrc, fnDst),
};
}
case EX_LetBool e:
{
return new EX_LetBool() {
VariableExpression = CopyExpressionTo(e.VariableExpression, src, dst, fnSrc, fnDst),
AssignmentExpression = CopyExpressionTo(e.AssignmentExpression, src, dst, fnSrc, fnDst),
};
}
case EX_StructConst e:
{
return new EX_StructConst() {
Struct = CopyImportTo((src, e.Struct), dst),
StructSize = e.StructSize,
Value = e.Value.Select(p => CopyExpressionTo(p, src, dst, fnSrc, fnDst)).ToArray(),
};
}
case EX_StructMemberContext e:
{
return new EX_StructMemberContext() {
StructMemberExpression = CopyKismetPropertyPointer(e.StructMemberExpression, src, dst, fnSrc, fnDst),
StructExpression = CopyExpressionTo(e.StructExpression, src, dst, fnSrc, fnDst),
};
}
case EX_InterfaceContext e:
{
return new EX_InterfaceContext() {
InterfaceValue = CopyExpressionTo(e.InterfaceValue, src, dst, fnSrc, fnDst),
};
}
case EX_VirtualFunction e:
{
return new EX_VirtualFunction() {
VirtualFunctionName = e.VirtualFunctionName.Transfer(dst),
Parameters = e.Parameters.Select(p => CopyExpressionTo(p, src, dst, fnSrc, fnDst)).ToArray(),
};
}
case EX_SetArray e:
{
return new EX_SetArray() {
AssigningProperty = CopyExpressionTo(e.AssigningProperty, src, dst, fnSrc, fnDst),
ArrayInnerProp = CopyImportTo((src, e.ArrayInnerProp), dst),
Elements = e.Elements.Select(p => CopyExpressionTo(p, src, dst, fnSrc, fnDst)).ToArray(),
};
}
case EX_InstanceVariable e:
{
return new EX_InstanceVariable() {
Variable = CopyKismetPropertyPointer(e.Variable, src, dst, fnSrc, fnDst),
};
}
case EX_DynamicCast e:
{
return new EX_DynamicCast() {
ClassPtr = CopyImportTo((src, e.ClassPtr), dst),
TargetExpression = CopyExpressionTo(e.TargetExpression, src, dst, fnSrc, fnDst),
};
}
case EX_PrimitiveCast e:
{
return new EX_PrimitiveCast() {
ConversionType = e.ConversionType,
Target = CopyExpressionTo(e.Target, src, dst, fnSrc, fnDst),
};
}
case EX_JumpIfNot e:
{
return new EX_JumpIfNot() {
CodeOffset = e.CodeOffset, // TODO wtf to do about jumps
BooleanExpression = CopyExpressionTo(e.BooleanExpression, src, dst, fnSrc, fnDst),
};
}
case EX_PopExecutionFlow e:
{
return new EX_PopExecutionFlow();
}
case EX_Tracepoint e:
{
return new EX_Tracepoint();
}
case EX_WireTracepoint e:
{
return new EX_WireTracepoint();
}
default:
{
throw new NotImplementedException(exp.ToString());
}
}
}
public static IEnumerable<(uint, KismetExpression)> GetOffsets(UAsset asset, KismetExpression[] bytecode) {
var offsets = new List<(uint, KismetExpression)>();
uint offset = 0;
foreach (var inst in bytecode) {
offsets.Add((offset, inst));
offset += Kismet.GetSize(asset, inst);
}
return offsets;
}
public static void SpliceMissionTerminal(UAsset asset) {
foreach (var export in asset.Exports) {
if (export is FunctionExport ubergraph) {
if (!export.ObjectName.ToString().StartsWith("ExecuteUbergraph")) {
continue;
}
Console.WriteLine("found ubergraph");
var offsets = GetOffsets(asset, ubergraph.ScriptBytecode).ToDictionary(l => l.Item1, l => l.Item2);
var list = ubergraph.ScriptBytecode.ToList();
//list.RemoveAt(list.IndexOf(offsets[5179]));
list.Insert(list.IndexOf(offsets[5200]), new EX_Jump() {
CodeOffset = 5339
});
list.Insert(list.IndexOf(offsets[5200]), new EX_StringConst() {
Value = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
});
list.Remove(offsets[5200]);
list.Remove(offsets[5236]);
list.Remove(offsets[5257]);
list.Remove(offsets[5290]);
ubergraph.ScriptBytecode = list.ToArray();
var newOffsets = GetOffsets(asset, ubergraph.ScriptBytecode).ToDictionary(l => l.Item2, l => l.Item1);
foreach (var inst in ubergraph.ScriptBytecode) {
if (inst is EX_JumpIfNot jumpIfNot) {
jumpIfNot.CodeOffset = newOffsets[offsets[jumpIfNot.CodeOffset]];
} else if (inst is EX_Jump jump) {
jump.CodeOffset = newOffsets[offsets[jump.CodeOffset]];
} else if (inst is EX_PushExecutionFlow push) {