-
Notifications
You must be signed in to change notification settings - Fork 2
/
PexToJson.pas
1433 lines (1247 loc) · 50.7 KB
/
PexToJson.pas
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
{
Poor man's PEX "decompiler" (more of a disassembler)
Version 2.0
Sources:
- http://f4se.silverlock.org/
The source code of scriptdump was the basis for most of this.
- https://en.uesp.net/wiki/Tes5Mod:Compiled_Script_File_Format
More of a honorable mention, since it's for Skyrim, which is sufficiently different.
Usage:
- Call either readPexResource, pexReadFile, or pexReadStream. It will return a TJsonObject. Clean it up manually.
Output Json structure:
- "header": object
- "majorVersion": int
- "minorVersion": int
- "gameId": int
- "compileTime1": uint
- "compileTime2": uint
- "sourceName": string
- "userName": string
- "machineName": string
- "debug": object
- "timestamp": uint
The unix timestamp of when this was compiled.
- "functions": array of PEXDebugFunction
- "groups": array of PEXDebugGroup
- "structs: array of PEXDebugStruct
- "userFlags": object
Contains this:
"0": "hidden",
"1": "conditional",
"2": "default",
"3": "collapsedonref",
"4": "collapsedonbase",
"5": "mandatory"
The 0-5 numbers are the leftshift argument, that is, "5" for "mandatory" means it's actually 1 shl 5 = 32.
These are also represented by the PEX_FLAG_* constants.
- "objects": array of PEXObject
The objects represent the PEXObjects ("classes") contained within the pex file. Usually only one,
but it seems like the format supports multiple, too.
PEXObject structure:
- "name": string
- "extends": string
- "docblock": string
- "const": bool
- "userFlags": int
- "autoStateName": string
If no auto state is defined, this will be "".
- "structs": array of PEXStruct
- "variables": array of PEXVariable
- "properties": array of PEXProperty
- "states": array of PEXState
PEXStruct structure:
- "name": string
- "members": array of PEXStructMember
PEXStructMember structure:
- "name": string
- "type": string
- "userFlags": int
- "value": PEXValue
This represents the default value of the variable.
- "const": boolean
- "docblock": string
PEXVariable structure:
- "name": string
If this begins with "::" and ends with "_var", then it's an auto-generated local variable of a property.
- "type": string
- "userFlags": int
- "value": PEXValue
This represents the default value of the variable.
- "const": bool
PEXProperty structure:
- "name": string
- "type": string
- "docblock": string
- "userFlags": int
- "flags": int
PEXState structure:
- "name": string
This is "" for the "empty state".
- "functions": array of PEXFunction
PEXFunction structure:
- "name": string
- "data": object
- "returnType": string
- "docBlock": string
- "userFlags": int
- "flags": int
- "params": PEXParam
- "locals": PEXParam
- "code": array of PEXCodeValue
PEXCode structure:
- "op": string
One of "nop", "iadd", "fadd", "isub", "fsub", "imul", "fmul", "idiv", "fdiv", "imod", "not", "ineg", "fneg", "assign", "cast", "cmp_eq", "cmp_lt", "cmp_le", "cmp_gt", "cmp_ge", "jmp", "jmpt", "jmpf", "callmethod", "callparent", "callstatic", "return", "strcat", "propget", "propset", "array_create", "array_length", "array_getelement", "array_setelement", "array_findelement", "array_rfindelement", "is", "struct_create", "struct_get", "struct_set", "array_findstruct", "array_rfindstruct", "array_add", "array_insert", "array_removelast", "array_remove", "array_clear".
- "args": array of PEXValue
PEXParam structure:
- "name": string
- "type": string
The actual type as written in the script, custom types will be here, too.
PEXValue structure:
- "name": string
- "type": string
One of "null", "identifier", "string", "integer", "float", "bool".
PEXCodeValue structure:
- "name": string
- "type": string
One of "null", "identifier", "string", "integer", "float", "bool".
- "argCode": string
One of "S", "L", "I", "F", "A", "Q", "u", "N", "T", "*".
PEXDebugFunction structure:
- "object": string
The object this function belongs to, should be the same as the script name.
- "state": string
The state in which this function is defined, emptystring if no state.
- "function": string
The name of the function.
- "type": int
Unknown.
- "lineNumbers": array of integer
Seems to be line numbers of the original source file, in the same order as the corresponding instructions in PEXFunction's "code" array?
PEXDebugGroup structure:
- "object": string
The object this group belongs to, should be the same as the script name.
- "group": string
Name of the property group. Can be empty, as it seems like the default "empty" group is also added here.
- "docblock": string
The docblock string of this group.
- "flags": uint
Flags of the group. "collapsed" seems to be 24
- "properties": array of string
Names of the properties within this group, in the "proper" order.
PEXDebugStruct structure:
- "object": string
The object this struct belongs to, should be the same as the script name.
- "struct": string
Name of this struct.
- "members": array of string
Names of the struct members, in the "proper" order.
}
unit PexToJson;
const
PEX_FLAG_HIDDEN = 1;
PEX_FLAG_CONDITIONAL = 2;
PEX_FLAG_DEFAULT = 4;
PEX_FLAG_COLLAPSEDONREF = 8;
PEX_FLAG_COLLAPSEDONBASE = 16;
PEX_FLAG_MANDATORY = 32;
var
pexStringTable: TStringList;
scriptTypeBlackList: TStringList;
pexBr: TBinaryReader;
pexCurrentStream: TStream;
function readPexScriptName(scriptName: string): TJsonObject;
begin
Result := readPexResource(_scriptNameToPexPath(scriptName));
end;
function readPexResource(filename: string): TJsonObject;
var
containers, assets: TStringList;
i: integer;
foundContainer: string;
byteStream: TFileStream;
begin
Result := nil;
// this is the shitty part...
// So, there is ResourceExists. It checks whenever the thing exists in any container, but doesn't tell you in which.
// There is also ResourceOpenData. This one REQUIRES the container.
// So, I guess I have to go find the container manually...
containers := TStringList.create();
ResourceContainerList(containers);
for i:=containers.count-1 downto 0 do begin
assets := TStringList.create();
containers.CaseSensitive := false;
ResourceList(containers[i], assets);
if(assets.indexOf(filename) >= 0) then begin
foundContainer := containers[i];
assets.free();
break;
end;
assets.free();
end;
containers.free();
if(foundContainer = '') then exit;
// ok try loading
byteStream := TBytesStream.Create(ResourceOpenData(foundContainer, filename));
Result := pexReadStream(byteStream);
byteStream.free();
end;
{
filename MUST be within DataPath!
}
function pexReadFile(filename: string): TJsonObject;
var
byteStream: TFileStream;
begin
Result := nil;
if(not FileExists(DataPath+filename)) then begin
exit;
end;
//pexContainedObjects := TStringList.create;
//pexExtendedObjects := TStringList.create;
try
byteStream := TBytesStream.Create(ResourceOpenData('', filename));
Result := pexReadStream(byteStream);
finally
if(assigned(byteStream)) then begin
byteStream.free();
end;
end;
end;
{
byteStream can be made by either
TBytesStream.Create(ResourceOpenData(...))
or
TFileStream.Create(fileName, fmOpenRead or fmShareDenyWrite)
}
function pexReadStream(byteStream: TFileStream): TJsonObject;
begin
Result := nil;
//pexContainedObjects := TStringList.create;
//pexExtendedObjects := TStringList.create;
pexCurrentStream := byteStream;
pexBr := TBinaryReader.Create(byteStream);
Result := _pexDoParse();
_pexCleanUp();
end;
{
analyzes a previously parsed pex, and returns a list of all script names which are used in it.
}
function getUsedScripts(pexJson: TJsonObject): TStringList;
var
i: integer;
curObj: TJsonObject;
begin
createBlackList();
Result := TStringList.create;
Result.Duplicates := dupIgnore;
Result.CaseSensitive := false;
Result.Sorted := true;
for i:=0 to pexJson.A['objects'].count-1 do begin
curObj := pexJson.A['objects'].O[i];
addScriptToList(Result, curObj.S['extends']);
// structs
addScriptsFromStructs(Result, curObj.A['structs']);
// variables
addScriptsFromVariables(Result, curObj.A['variables']);
// props, they are similar enough
addScriptsFromVariables(Result, curObj.A['properties']);
// the hard part, aka states
addScriptsFromStates(Result, curObj.A['states']);
end;
cleanupBlackList();
end;
// ==========================================================================
// === END of "public" functions. Please do not call anything below manually.
// ==========================================================================
procedure createBlackList();
begin
scriptTypeBlackList := TStringList.create;
scriptTypeBlackList.Duplicates := dupIgnore;
scriptTypeBlackList.CaseSensitive := false;
scriptTypeBlackList.Sorted := true;
scriptTypeBlackList.add('float');
scriptTypeBlackList.add('int');
scriptTypeBlackList.add('string');
scriptTypeBlackList.add('bool');
scriptTypeBlackList.add('var');
scriptTypeBlackList.add('none');
scriptTypeBlackList.add('debug');
scriptTypeBlackList.add('f4se');
scriptTypeBlackList.add('game');
scriptTypeBlackList.add('input');
scriptTypeBlackList.add('inputenablelayer');
scriptTypeBlackList.add('instancedata');
scriptTypeBlackList.add('math');
scriptTypeBlackList.add('ui');
scriptTypeBlackList.add('utility');
scriptTypeBlackList.add('shout');
scriptTypeBlackList.add('wordofpower');
scriptTypeBlackList.add('leveledspell');
scriptTypeBlackList.add('scroll');
scriptTypeBlackList.add('soulgem');
scriptTypeBlackList.add('scriptobject');
scriptTypeBlackList.add('form');
scriptTypeBlackList.add('objectreference');
scriptTypeBlackList.add('actor');
scriptTypeBlackList.add('alias');
scriptTypeBlackList.add('referencealias');
scriptTypeBlackList.add('locationalias');
scriptTypeBlackList.add('refcollectionalias');
scriptTypeBlackList.add('activemagiceffect');
scriptTypeBlackList.add('action');
scriptTypeBlackList.add('activator');
scriptTypeBlackList.add('flora');
scriptTypeBlackList.add('furniture');
scriptTypeBlackList.add('talkingactivator');
scriptTypeBlackList.add('actorbase');
scriptTypeBlackList.add('actorvalue');
scriptTypeBlackList.add('ammo');
scriptTypeBlackList.add('armor');
scriptTypeBlackList.add('associationtype');
scriptTypeBlackList.add('book');
scriptTypeBlackList.add('camerashot');
scriptTypeBlackList.add('cell');
scriptTypeBlackList.add('class');
scriptTypeBlackList.add('combatstyle');
scriptTypeBlackList.add('component');
scriptTypeBlackList.add('container');
scriptTypeBlackList.add('door');
scriptTypeBlackList.add('defaultobject');
scriptTypeBlackList.add('effectshader');
scriptTypeBlackList.add('enchantment');
scriptTypeBlackList.add('encounterzone');
scriptTypeBlackList.add('equipslot');
scriptTypeBlackList.add('explosion');
scriptTypeBlackList.add('faction');
scriptTypeBlackList.add('formlist');
scriptTypeBlackList.add('globalvariable');
scriptTypeBlackList.add('hazard');
scriptTypeBlackList.add('headpart');
scriptTypeBlackList.add('holotape');
scriptTypeBlackList.add('idle');
scriptTypeBlackList.add('idlemarker');
scriptTypeBlackList.add('imagespacemodifier');
scriptTypeBlackList.add('impactdataset');
scriptTypeBlackList.add('ingredient');
scriptTypeBlackList.add('instancenamingrules');
scriptTypeBlackList.add('keyword');
scriptTypeBlackList.add('locationreftype');
scriptTypeBlackList.add('leveledactor');
scriptTypeBlackList.add('leveleditem');
scriptTypeBlackList.add('leveledspell');
scriptTypeBlackList.add('light');
scriptTypeBlackList.add('location');
scriptTypeBlackList.add('magiceffect');
scriptTypeBlackList.add('message');
scriptTypeBlackList.add('miscobject');
scriptTypeBlackList.add('constructibleobject');
scriptTypeBlackList.add('key');
scriptTypeBlackList.add('soulgem');
scriptTypeBlackList.add('musictype');
scriptTypeBlackList.add('objectmod');
scriptTypeBlackList.add('outfit');
scriptTypeBlackList.add('outputmodel');
scriptTypeBlackList.add('package');
scriptTypeBlackList.add('perk');
scriptTypeBlackList.add('potion');
scriptTypeBlackList.add('projectile');
scriptTypeBlackList.add('quest');
scriptTypeBlackList.add('race');
scriptTypeBlackList.add('scene');
scriptTypeBlackList.add('scroll');
scriptTypeBlackList.add('shaderparticlegeometry');
scriptTypeBlackList.add('shout');
scriptTypeBlackList.add('sound');
scriptTypeBlackList.add('soundcategory');
scriptTypeBlackList.add('soundcategorysnapshot');
scriptTypeBlackList.add('spell');
scriptTypeBlackList.add('static');
scriptTypeBlackList.add('movablestatic');
scriptTypeBlackList.add('terminal');
scriptTypeBlackList.add('textureset');
scriptTypeBlackList.add('topic');
scriptTypeBlackList.add('topicinfo');
scriptTypeBlackList.add('visualeffect');
scriptTypeBlackList.add('voicetype');
scriptTypeBlackList.add('watertype');
scriptTypeBlackList.add('weapon');
scriptTypeBlackList.add('weather');
scriptTypeBlackList.add('wordofpower');
scriptTypeBlackList.add('worldspace');
end;
procedure cleanupBlackList();
begin
scriptTypeBlackList.free();
scriptTypeBlackList := nil;
end;
function isBuiltInType(typeStr: string): boolean;
var
typeLc: string;
begin
Result := (scriptTypeBlackList.indexOf(typeStr) >= 0);
end;
procedure addScriptToList(outList: TStringList; typeStr: string);
var
prefix, suffix: string;
hashPos: integer;
begin
if (typeStr = '') or isBuiltInType(typeStr) then begin
exit;
end;
suffix := copy(typeStr, length(typeStr)-1, 2); // returns the last two chars
if(suffix = '[]') then begin
prefix := copy(typeStr, 1, length(typeStr)-2); // cuts off the last two chars
addScriptToList(outList, prefix);
exit;
//if (prefix = '') or isBuiltInType(prefix) then begin
//exit;
//end;
//outList.Add(prefix);
end;
hashPos := Pos('#', typeStr);
if(hashPos > 0) then begin
// cut it
prefix := copy(typeStr, 1, hashPos-1);
addScriptToList(outList, prefix);
exit;
end;
outList.Add(typeStr);
end;
procedure addScriptsFromStates(outList: TStringList; states: TJsonArray);
var
i, j: integer;
curFunc: TJsonObject;
functions: TJsonArray;
begin
for i:=0 to states.count-1 do begin
functions := states.O[i].A['functions'];
for j:=0 to functions.count-1 do begin
curFunc := functions.O[j];
// params
addScriptsFromVariables(outList, curFunc.O['data'].A['params']);
// locals
addScriptsFromVariables(outList, curFunc.O['data'].A['locals']);
// code
addScriptsFromCode(outList, curFunc.O['data'].A['code']);
end;
end;
end;
procedure addScriptsFromCode(outList: TStringList; code: TJsonArray);
var
i: integer;
curEntry, curArg: TJsonObject;
curOp: string;
begin
for i:=0 to code.count-1 do begin
curEntry := code.O[i];
curOp := curEntry.S['op'];
// only check "callstatic" and "is"
if(curOp = 'callstatic') then begin
//AddMessage('Found a callstatic');
// NSS* -> the first N arg is the name
curArg := curEntry.A['args'].O[0];
addScriptToList(outList, curArg.S['value']);
end else if (curOp = 'is') then begin
// SAT -> third T arg
curArg := curEntry.A['args'].O[2];
addScriptToList(outList, curArg.S['value']);
end;
end;
end;
procedure addScriptsFromVariables(outList: TStringList; vars: TJsonArray);
var
i: integer;
members: TJsonArray;
curType: string;
begin
for i:=0 to vars.count-1 do begin
curType := vars.O[i].S['type'];
addScriptToList(outList, curType);
end;
end;
// for getUsedScripts begin
procedure addScriptsFromStructs(outList: TStringList; structs: TJsonArray);
var
i, j: integer;
members: TJsonArray;
curType: string;
begin
for i:=0 to structs.count-1 do begin
// we care about the types of the members
members := structs.O[i].A['members'];
for j:=0 to members.count-1 do begin
curType := members.O[j].S['type'];
addScriptToList(outList, curType);
end;
end;
end;
// for getUsedScripts end
// TODO: also port checkScriptExtends and findScriptInElementByName
procedure _pexCleanUp();
begin
if(assigned(pexStringTable)) then begin
pexStringTable.free();
pexStringTable := nil;
end;
if(assigned(pexBr)) then begin
pexBr.free();
pexBr := nil;
end;
end;
function _pexGetStringTableEntry(i: integer): string;
begin
if(i<pexStringTable.count) then begin
Result := pexStringTable[i];
exit;
end;
Result := '';
end;
function _pexReadHeader(): TJsonObject;
var
magic: cardinal;
pexMajorVersion, pexMinorVersion, pexGameId: integer;
pexCompileTime1: cardinal;
pexCompileTime2: cardinal;
pexSourceName, pexUserName, pexMachineName: string;
begin
Result := TJsonObject.create();
magic := pexBr.readUInt32(); // magic
// Result.U['magic'] := magic;
//pexBr.seek(4);//seek(4); // soBegin, soCurrent, soEnd
pexMajorVersion := pexBr.readByte(); // just read = 1 byte
pexMinorVersion := pexBr.readByte();
pexGameId := pexBr.ReadUInt16();
Result.I['majorVersion'] := pexMajorVersion;
Result.I['minorVersion'] := pexMinorVersion;
Result.I['gameId'] := pexGameId;
// it seems pascal doesn't support 64bit ints
pexCompileTime1 := pexBr.ReadUInt32();
pexCompileTime2 := pexBr.ReadUInt32();
Result.U['compileTime1'] := pexCompileTime1;
Result.U['compileTime2'] := pexCompileTime2;
pexSourceName := _pexReadString();
pexUserName := _pexReadString();
pexMachineName := _pexReadString();
Result.S['sourceName'] := pexSourceName;
Result.S['userName'] := pexUserName;
Result.S['machineName'] := pexMachineName;
end;
procedure _pexReadStringTable();
var
numStrings, i: integer;
curString: string;
begin
pexStringTable := TStringList.create;
numStrings := pexBr.ReadUInt16();
for i:=0 to numStrings-1 do begin
curString := _pexReadString();
//AddMessage('Found string '+curString);
pexStringTable.add(curString);
end;
end;
function _pexReadDebugFunction(appendTo: TJsonArray): TJsonObject;
var
objectNameIndex, stateNameIndex, functionNameIndex, functionType, instructionCount, i: integer;
instructions: TJsonArray;
begin
if(nil <> appendTo) then begin
Result := appendTo.addObject();
end else begin
Result := TJsonObject.create;
end;
objectNameIndex := pexBr.ReadUInt16();
stateNameIndex := pexBr.ReadUInt16();
functionNameIndex := pexBr.ReadUInt16();
functionType := pexBr.readByte();
instructionCount := pexBr.ReadUInt16();
//skipBytes(instructionCount * 2);
Result.S['object'] := _pexGetStringTableEntry(objectNameIndex);
Result.S['state'] := _pexGetStringTableEntry(stateNameIndex);
Result.S['function'] := _pexGetStringTableEntry(functionNameIndex);
Result.I['type'] := IntToStr(functionType);
instructions := Result.A['lineNumbers'];
// AddMessage('objectNameIndex '+pexStringTable[objectNameIndex]+', stateNameIndex '+pexStringTable[stateNameIndex]+', functionNameIndex '+pexStringTable[functionNameIndex]);
//AddMessage('NumInstr '+IntToStr(instructionCount));
for i:=0 to instructionCount-1 do begin
instructions.add(pexBr.ReadUInt16());
// potentially do something with the info? or just skip 2*instructionCount right away?
// AddMessage(' Line NR: '+IntToStr(pexBr.ReadUInt16()));
end;
end;
function _pexReadPropertyGroups(): TJsonArray;
var
cnt, objName, groupName, groupDoc, nameCount, i, j: integer;
userFlags: cardinal;
curGroupName: string;
curEntry: TJsonObject;
propNames: TJsonArray;
begin
Result := TJsonArray.create;
// Seems to be Groups? taken from the source code of scriptdump.exe from http://f4se.silverlock.org/
cnt := pexBr.ReadUInt16();
for i:=0 to cnt-1 do begin
if(_isEOF()) then begin
exit;
end;
// the names seem to be more UInt16 indices
objName := pexBr.ReadUInt16();
if(_isEOF()) then exit;
groupName := pexBr.ReadUInt16();
if(_isEOF()) then exit;
groupDoc := pexBr.ReadUInt16();
if(_isEOF()) then exit;
userFlags := pexBr.ReadUInt32();
if(_isEOF()) then exit;
nameCount := pexBr.ReadUInt16();
curEntry := Result.addObject();
curEntry.S['object'] := _pexGetStringTableEntry(objName);
curEntry.S['group'] := _pexGetStringTableEntry(groupName);
curEntry.S['docblock'] := _pexGetStringTableEntry(groupDoc);
curEntry.U['flags'] := userFlags;
propNames := curEntry.A['properties'];
for j:=0 to nameCount-1 do begin
if(_isEOF()) then exit;
curGroupName := _pexGetStringTableEntry(pexBr.ReadUInt16());
propNames.add(curGroupName);
end;
end;
end;
function _pexReadStructOrder(): TJsonArray;
var
cnt, i, j, objName, orderName, varCount, curVarName: integer;
curObj: TJsonObject;
curNames: TJsonArray;
begin
Result := TJsonArray.create;
// Seems to be structs? taken from the source code of scriptdump.exe from http://f4se.silverlock.org/
cnt := pexBr.ReadUInt16();
for i:=0 to cnt-1 do begin
if(_isEOF()) then exit;
curObj := Result.addObject();
objName := pexBr.ReadUInt16();
orderName := pexBr.ReadUInt16();
varCount := pexBr.ReadUInt16();
curObj.S['object'] := _pexGetStringTableEntry(objName);
curObj.S['struct'] := _pexGetStringTableEntry(orderName);
// what are these?
// skipBytes(varCount * 2);
curNames := curObj.A['members'];
for j:=0 to varCount-1 do begin
curVarName := pexBr.ReadUInt16();
curNames.add(_pexGetStringTableEntry(curVarName));
end;
end;
end;
function _pexReadDebugInfo(): TJsonObject;
var
hasDebugInfo, modTime1, modTime2, numFuncs, i: integer;
pexHasDebugInfo: boolean;
modTime: cardinal;
debugFunctions: TJsonArray;
begin
Result := TJsonObject.create();
hasDebugInfo := pexBr.readByte();
//AddMessage('hasDebugInfo '+IntToStr(hasDebugInfo));
pexHasDebugInfo := (hasDebugInfo <> 0);
// apparently if you don't have debug, it just skips the other fields, and is literally just the one byte?
if(not pexHasDebugInfo) then begin
exit;
end;
modTime1 := pexBr.ReadUInt32();
modTime2 := pexBr.ReadUInt32();
numFuncs := pexBr.ReadUInt16();
// I think I can just (modTime2 << something) + modTime1, and then it's a timestamp
// FFFFFFFF
// expected: 1707253497 0 -> Tue Feb 06 22:04:57 2024
modTime := (modTime2 shl $FFFFFFFF) + modTime1;
// AddMessage('ModTime: '+IntToStr(modTime));
Result.U['timestamp'] := modTime;
//AddMessage('Num Funcs '+IntToStr(numFuncs));
debugFunctions := Result.A['functions'];
for i:=0 to numFuncs-1 do begin
//AddMessage('Processing debug func');
_pexReadDebugFunction(debugFunctions);
end;
if(_isEOF()) then exit;
Result.A['groups'] := _pexReadPropertyGroups();
if(_isEOF()) then exit;
Result.A['structs'] := _pexReadStructOrder();
end;
function _pexReadUserFlags(): TJsonObject;
var
userFlagCount, curNameIndex, curFlagIndex, i: integer;
begin
Result := TJsonObject.create();
userFlagCount := pexBr.ReadUInt16();
// just skip them for now
// skipBytes(userFlagCount*3);
// not sure what these are even for?
// AddMessage(' == userFlagCount = '+IntToStr(userFlagCount));
for i:=0 to userFlagCount-1 do begin
curNameIndex := pexBr.ReadUInt16();
curFlagIndex := pexBr.readByte();
// AddMessage('Read Flag '+IntToStr(curNameIndex)+' -> '+IntToStr(curFlagIndex));
// AddMessage('Read Flag '+pexStringTable[curNameIndex]+' -> '+IntToStr(curFlagIndex));
Result.S[IntToStr(curFlagIndex)] := _pexGetStringTableEntry(curNameIndex);
end;
end;
function _pexReadObject(appendTo: TJsonArray): TJsonObject;
var
i, nameIndex, parentClassIndex, docstring, autoStateName, constFlag: integer;
size, userFlags, skipTo, numVars, numProps, numStructs, numStates: cardinal;
temp: TJsonObject;
begin
if(nil <> appendTo) then begin
Result := appendTo.addObject();
end else begin
Result := TJsonObject.create;
end;
// getStringTableEntry
nameIndex := pexBr.ReadUInt16();
Result.S['name'] := _pexGetStringTableEntry(nameIndex);
size := pexBr.ReadUInt32()-4;
// s
// AddMessage('Size is '+IntToStr(size));
if(size > 0) then begin
skipTo := size + pexCurrentStream.position;
parentClassIndex := pexBr.ReadUInt16();
docstring := pexBr.ReadUInt16();
constFlag := pexBr.ReadByte();// added for F4
userFlags := pexBr.ReadUInt32();
autoStateName := pexBr.ReadUInt16();
numStructs := pexBr.ReadUInt16();
//pexContainedObjects.add(_pexGetStringTableEntry(nameIndex));
//pexExtendedObjects.add(_pexGetStringTableEntry(parentClassIndex));
Result.S['extends'] := _pexGetStringTableEntry(parentClassIndex);
Result.S['docblock'] := _pexGetStringTableEntry(docstring);
Result.B['const'] := (constFlag <> 0);
Result.U['userFlags'] := userFlags;
Result.S['autoStateName'] := _pexGetStringTableEntry(autoStateName);
// AddMessage('Found object '+_pexGetStringTableEntry(nameIndex)+' extends '+_pexGetStringTableEntry(parentClassIndex));
// AddMessage('Auto state name: '+_pexGetStringTableEntry(autoStateName)+', numStructs? '+IntToStr(numStructs));
// try to parse structs
for i:=0 to numStructs-1 do begin
_pexReadStruct(Result.A['structs']);
end;
// next, variables
numVars := pexBr.ReadUInt16();
for i:=0 to numVars-1 do begin
_pexReadVar(Result.A['variables']);
end;
// properties
numProps := pexBr.ReadUInt16();
for i:=0 to numProps-1 do begin
_pexReadProp(Result.A['properties']);
end;
numStates := pexBr.ReadUInt16();
for i:=0 to numStates-1 do begin
_pexReadState(Result.A['states']);
end;
// skip rest
pexCurrentStream.position := skipTo;
//pexCurrentStream.seek(skipTo, soFromBeginning);
end;
end;
function _pexReadState(appendTo: TJsonArray): TJsonObject;
var
numFuncs, i, stateNameIndex, funcNameIndex: cardinal;
funcs: TJsonArray;
funcObj: TJsonObject;
begin
if(nil <> appendTo) then begin
Result := appendTo.addObject();
end else begin
Result := TJsonObject.create;
end;
stateNameIndex := pexBr.ReadUInt16();
// AddMessage('State: '+_pexGetStringTableEntry(stateNameIndex));
Result.S['name'] := _pexGetStringTableEntry(stateNameIndex);
numFuncs := pexBr.ReadUInt16();
funcs := Result.A['functions'];
for i:=0 to numFuncs-1 do begin
// name and function
funcNameIndex := pexBr.ReadUInt16();
funcObj := funcs.AddObject();
// AddMessage('Function: '+_pexGetStringTableEntry(funcNameIndex));
funcObj.S['name'] := _pexGetStringTableEntry(funcNameIndex);
// funcs.add(_pexReadFunction());
funcObj.O['data'] := _pexReadFunction(nil);
end;
end;
function _pexReadProp(appendTo: TJsonArray): TJsonObject;
const
flagRead = 1; // 1 shl 0
flagWrite = 2; // 1 shl 1
flagAutoVar = 4;// 1 shl 2;
var
nameIndex, typeIndex, docIndex, autoVarIndex, userFlags, flags: cardinal;
begin
if(nil <> appendTo) then begin
Result := appendTo.addObject();
end else begin
Result := TJsonObject.create;
end;
// damn props are complex
nameIndex := pexBr.ReadUInt16();
typeIndex := pexBr.ReadUInt16();
docIndex := pexBr.ReadUInt16();
userFlags := pexBr.ReadUInt32();
flags := pexBr.ReadByte();
Result.S['name'] := _pexGetStringTableEntry(nameIndex);
Result.S['type'] := _pexGetStringTableEntry(typeIndex);
Result.S['docblock'] := _pexGetStringTableEntry(docIndex);
Result.U['userFlags'] := userFlags;
Result.I['flags'] := flags;
autoVarIndex := 0;
//kFlags_AutoVar = 1 << 2,
if ((flags and flagAutoVar) <> 0) then begin
autoVarIndex := pexBr.ReadUInt16();
end;
// AddMessage(_pexGetStringTableEntry(typeIndex)+' property '+_pexGetStringTableEntry(nameIndex)+' '+IntToStr(userFlags)+' '+IntToStr(flags));
// now depending on read/write, do something?
// ooh these are functions already. :vaultsweat:
if ((flags and flagAutoVar) = 0) then begin
if((flags and flagRead) <> 0) then begin
Result.O['readFunction'] := _pexReadFunction(nil);
end;
if((flags and flagWrite) <> 0) then begin
Result.O['writeFunction'] := _pexReadFunction(nil);
end;
end;
end;
function _pexReadFunction(appendTo: TJsonArray): TJsonObject;
var
i, returnTypeIndex, dockIndex, userFlags, flags, numParams, codeLength: cardinal;
params, locals, code: TJsonArray;
curObj: TJsonObject;
begin
if(nil <> appendTo) then begin
Result := appendTo.addObject();
end else begin
Result := TJsonObject.create;
end;
returnTypeIndex := pexBr.ReadUInt16();
dockIndex := pexBr.ReadUInt16();
userFlags := pexBr.ReadUInt32();
flags := pexBr.ReadByte();
numParams := pexBr.ReadUInt16();
Result.S['returnType'] := _pexGetStringTableEntry(returnTypeIndex);
Result.S['docBlock'] := _pexGetStringTableEntry(dockIndex);
Result.U['userFlags'] := userFlags;
Result.I['flags'] := flags;
params := Result.A['params'];
for i:=0 to numParams-1 do begin
_pexReadParam(params);
end;
numParams := pexBr.ReadUInt16();
// UInt16 numLocals = src->Read16();
locals := Result.A['locals'];
// AddMessage('Locals: '+IntToStr(numParams));
for i:=0 to numParams-1 do begin
_pexReadParam(locals);
end;
code := Result.A['code'];
codeLength := pexBr.ReadUInt16();
// AddMessage('codeLength: '+IntToStr(codeLength));
// unfortunately it seems like we can't calculate how much code to skip
for i:=0 to codeLength-1 do begin
_pexReadCode(code);
end;
end;
function _pexReadCode(appendTo: TJsonArray): TJsonObject;
var
opcode, numArgs, i, j, curValue: cardinal;
argDesc, curChar, opName: string;
varVal, varVal2: TJsonObject;
args, tempArgs: TJsonArray;
begin
if(nil <> appendTo) then begin
Result := appendTo.addObject();