forked from TES5Edit/TES5Edit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wbSaveInterface.pas
475 lines (406 loc) · 14.5 KB
/
wbSaveInterface.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
unit wbSaveInterface;
interface
uses
SysUtils,
wbInterface;
var
dtArrays : set of TwbDefType = [
dtSubRecordArray,
dtArray
];
function wbVMType : IwbIntegerDefFormater;
function wbVMHandle : IwbIntegerDefFormater;
function wbVMObjectHandle : IwbIntegerDefFormater;
function wbVMArrayHandle : IwbIntegerDefFormater;
function wbSaveWorldspaceIndex : IwbIntegerDefFormater;
procedure InitializeVMTypeArray(aContainer: IwbContainer);
procedure InitializeVMObjectArray(aContainer: IwbContainer);
procedure InitializeVMObjectDetachedArray(aContainer: IwbContainer);
procedure InitializeVMArrayTable(aContainer: IwbContainer);
procedure InitializeSaveWorldspaceArray(aContainer: IwbContainer);
procedure InitializeSaveRefIDArray(aContainer: IwbContainer);
function GetSaveRefID(aIndex: Cardinal): Cardinal;
function QueryCountForVMArrayHandle(anArrayHandle: Int64): Int64;
function wbFindSaveElement(aName: String; aElement: IwbElement): IwbElement;
function wbDontShowBranch(const aElement: IwbElement): Boolean;
// CoSave shared data
var
wbCoSaveArrayTypeEnum : IwbEnumDef;
function wbCoSaveChapterOtherCounter(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Cardinal;
function wbCoSavePluginCounter(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Cardinal;
function wbCoSaveChunkCounter(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Cardinal;
function wbCoSaveChunkType(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Integer;
function wbCoSaveChunkTypeName(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): String;
function wbCoSaveArrayKeyElementDecider(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Integer;
function wbCoSaveArrayDataElementDecider(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Integer;
function wbCoSaveArrayType(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Integer;
function wbCoSaveArrayTypeName(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): String;
// Place holder during decoding...
function ToBeDeterminedDecider(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Integer;
function ToBeDeterminedCounter(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Cardinal;
function ToBeDeterminedCountCallback(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Cardinal;
implementation
{ wbVMType }
var
sifVMTypeArray : array of String = nil;
procedure InitializeVMTypeArray(aContainer: IwbContainer);
var
i : Integer;
begin
if Assigned(aContainer) and not assigned(sifVMTypeArray) then begin
SetLength(sifVMTypeArray, aContainer.ElementCount);
for i := 0 to Pred(aContainer.ElementCount) do
sifVMTypeArray[i] := aContainer.Elements[i].NativeValue;
end;
end;
function TwbVMTypeFormaterToString(aInt: Int64; const aElement: IwbElement; aType: TwbCallbackType): string;
begin
if aType = ctToSortKey then
Result := IntToHex64(aInt, 8)
else if (aInt >= 0) and (aInt < Length(sifVMTypeArray)) then
Result := '[' + IntToHex64(aInt, 8) + '] '+ sifVMTypeArray[aInt]
else
Result := '[' + IntToHex64(aInt, 8) + '] <no such string>';
end;
{ wbVMObjectHandle }
type
ohfVMObjectHandleRecord = record
Handle: Int64;
VMType: Integer;
end;
var
ohfVMObjectHandleTable : array of ohfVMObjectHandleRecord = nil; // stores VMType
ohfVMObjectDetachedHandleTable : array of ohfVMObjectHandleRecord = nil; // stores VMType
procedure InitializeVMObjectArray(aContainer: IwbContainer);
var
i : Integer;
Container : IwbContainer;
begin
if Assigned(aContainer) and not Assigned(ohfVMObjectHandleTable) then begin
SetLength(ohfVMObjectHandleTable, aContainer.ElementCount);
for i := 0 to Pred(aContainer.ElementCount) do begin
Container := (aContainer.Elements[i] as IwbContainer);
ohfVMObjectHandleTable[i].Handle := Container.ElementByName['Object Handle'].NativeValue;
ohfVMObjectHandleTable[i].VMType := Container.ElementByName['Name'].NativeValue;
end;
end;
end;
procedure InitializeVMObjectDetachedArray(aContainer: IwbContainer);
var
i : Integer;
Container : IwbContainer;
begin
if Assigned(aContainer) and not Assigned(ohfVMObjectDetachedHandleTable) and (aContainer.ElementCount>0) then begin
SetLength(ohfVMObjectDetachedHandleTable, aContainer.ElementCount);
for i := 0 to Pred(aContainer.ElementCount) do begin
Container := (aContainer.Elements[i] as IwbContainer);
ohfVMObjectDetachedHandleTable[i].Handle := Container.ElementByName['Object Handle'].NativeValue;
ohfVMObjectDetachedHandleTable[i].VMType := Container.ElementByName['Name'].NativeValue;
end;
end;
end;
function ReadObjectName(aInt: Int64): String;
var
VMType : Integer;
i : Integer;
begin
Result := '';
VMType := -1;
for i := 0 to High(ohfVMObjectHandleTable) do
if ohfVMObjectHandleTable[i].Handle=aInt then begin
VMType := ohfVMObjectHandleTable[i].VMType;
end;
if VMType<0 then
for i := 0 to High(ohfVMObjectDetachedHandleTable) do
if ohfVMObjectDetachedHandleTable[i].Handle=aInt then begin
VMType := ohfVMObjectDetachedHandleTable[i].VMType;
end;
if VMType >= 0 then
Result := '[' + IntToHex64(aInt, 8) + '] '+ sifVMTypeArray[VMType];
end;
function TwbHandleFormaterToString(aInt: Int64; const aElement: IwbElement; aType: TwbCallbackType): string;
begin
Result := IntToHex64(aInt, 16)
end;
function TwbObjectHandleFormaterToString(aInt: Int64; const aElement: IwbElement; aType: TwbCallbackType): string;
begin
if aType = ctToSortKey then
Result := IntToHex64(aInt, 8)
else begin
Result := ReadObjectName(aInt);
if Result = '' then
if aInt = 0 then
Result := '[' + IntToHex64(aInt, 8) + '] [empty]'
else
Result := '[' + IntToHex64(aInt, 8) + '] <no such object>';
end;
end;
{ wbVMArrayHandle }
type
ahfVMArrayHandleRecord = record
Handle : Int64;
Count : Int64;
end;
var
ahfVMArrayHandleTable : array of ahfVMArrayHandleRecord = nil; // stores VMType
procedure InitializeVMArrayTable(aContainer: IwbContainer);
var
i : Integer;
Container : IwbContainer;
begin
if Assigned(aContainer) and not Assigned(ahfVMArrayHandleTable) then begin
SetLength(ahfVMArrayHandleTable, aContainer.ElementCount);
for i := 0 to Pred(aContainer.ElementCount) do begin
Container := (aContainer.Elements[i] as IwbContainer);
ahfVMArrayHandleTable[i].Handle := Container.ElementByName['Array Handle'].NativeValue;
ahfVMArrayHandleTable[i].Count := Container.ElementByName['Count'].NativeValue;
end;
end;
end;
function QueryCountForVMArrayHandle(anArrayHandle: Int64): Int64;
var
i : Integer;
begin
Result := -1;
for i := 0 to High(ahfVMArrayHandleTable) do
if ahfVMArrayHandleTable[i].Handle = anArrayHandle then begin
Result := ahfVMArrayHandleTable[i].Count;
Break;
end;
if result < 0 then
result := 0;
end;
function TwbVMArrayHandleFormaterToString(aInt: Int64; const aElement: IwbElement; aType: TwbCallbackType): string;
var
Count : Int64;
begin
if aType = ctToSortKey then
Result := IntToHex64(aInt, 8)
else begin
Count := QueryCountForVMArrayHandle(aInt);
Result := '[' + IntToHex64(aInt, 8) + '] Count = '+ IntToStr(Count);
end;
end;
{ wbSaveWorldspaceIndex }
var
sifSaveWorldspaceArray : array of IwbElement = nil;
procedure InitializeSaveWorldspaceArray(aContainer: IwbContainer);
var
i : Integer;
begin
if Assigned(aContainer) and not assigned(sifSaveWorldspaceArray) then begin
SetLength(sifSaveWorldspaceArray, aContainer.ElementCount);
for i := 0 to Pred(aContainer.ElementCount) do
sifSaveWorldspaceArray[i] := aContainer.Elements[i];
end;
end;
function TwbSaveWorldspaceIndexFormaterToString(aInt: Int64; const aElement: IwbElement; aType: TwbCallbackType): string;
begin
if aType = ctToSortKey then
Result := IntToHex64(aInt, 8)
else if (aInt > 0) and (aInt <= Length(sifSaveWorldspaceArray)) then begin
Result := '[' + IntToHex64(aInt, 8) + '] '+ sifSaveWorldspaceArray[aInt-1].Value
end else
Result := '[' + IntToHex64(aInt, 8) + '] <no such worldspace>';
end;
var
SaveRefIDArray: TwbRefIDArray = nil;
procedure InitializeSaveRefIDArray(aContainer: IwbContainer);
var
i : Integer;
begin
if Assigned(aContainer) and not assigned(SaveRefIDArray) then begin
SetLength(SaveRefIDArray, aContainer.ElementCount);
for i := 0 to Pred(aContainer.ElementCount) do
SaveRefIDArray[i] := aContainer.Elements[i].NativeValue;
InitializeRefIDArray(SaveRefIDArray);
end;
end;
function GetSaveRefID(aIndex: Cardinal): Cardinal;
begin
if (aIndex>0) and (aIndex<Length(SaveRefIDArray)) then
Result := SaveRefIDArray[aIndex-1]
else
Result := 0;
end;
function wbVMType : IwbIntegerDefFormater;
begin
Result := wbCallback(@TwbVMTypeFormaterToString, nil);
end;
function wbVMHandle : IwbIntegerDefFormater; overload;
begin
Result := wbCallback(@TwbHandleFormaterToString, nil);
end;
function wbVMObjectHandle : IwbIntegerDefFormater; overload;
begin
Result := wbCallback(@TwbObjectHandleFormaterToString, nil);
end;
function wbVMArrayHandle : IwbIntegerDefFormater;
begin
Result := wbCallback(@TwbVMArrayHandleFormaterToString, nil);
end;
function wbSaveWorldspaceIndex : IwbIntegerDefFormater;
begin
Result := wbCallback(@TwbSaveWorldspaceIndexFormaterToString, nil);
end;
function wbFindSaveElement(aName: String; aElement: IwbElement): IwbElement;
var
Container : IwbContainer;
function FindOurself(aName: String; aContainer: IwbContainer; var aElement: IwbElement): Boolean;
var
i : Integer;
tContainer : IwbContainer;
begin
Result := False;
for i := 0 to Pred(aContainer.ElementCount) do
if SameText(aContainer.Elements[i].BaseName, aName) then begin
aElement := aContainer.Elements[i];
Result := True;
break;
end else if Supports(aContainer.Elements[i], IwbContainer, tContainer) then
if FindOurself(aName, tContainer, aElement) then begin
Result := True;
break;
end;
end;
begin
Result := aElement;
while (not SameText(aName, Result.BaseName)) and Assigned(Result.Container) do
Result := Result.Container;
if (not SameText(aName, Result.BaseName)) then begin // try again in reverse
Result := aElement;
if Supports(Result, IwbContainer, Container) then
FindOurself(aName, Container, Result);
end;
end;
function wbDontShowBranch(const aElement: IwbElement): Boolean;
begin
Result := wbHideNeverShow;
end;
function wbCoSaveChapterOtherCounter(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Cardinal;
var
Element : IwbElement;
Container: IwbDataContainer;
begin
Result := 0;
if not Assigned(aElement) then Exit;
Element := wbFindSaveElement('Chunk', aElement);
if Supports(Element, IwbDataContainer, Container) then begin
Element := Container.ElementByName['Length'];
if Assigned(Element) then
Result := Element.NativeValue;
end;
end;
function wbCoSavePluginCounter(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Cardinal;
var
Element : IwbElement;
Container : IwbDataContainer;
aFile : IwbFile;
begin
Result := 0;
if not Assigned(aElement) then Exit;
aFile := aElement.GetFile;
if not Assigned(aFile) then Exit;
Element := aFile.ElementByName['CoSave File Header'];
if Supports(Element, IwbDataContainer, Container) then begin
Element := Container.ElementByName['Plugins count'];
if Assigned(Element) then
Result := Element.NativeValue;
end;
end;
function wbCoSaveChunkCounter(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Cardinal;
var
Element : IwbElement;
Container: IwbDataContainer;
begin
Result := 0;
if not Assigned(aElement) then Exit;
Element := wbFindSaveElement('Plugin', aElement);
if Supports(Element, IwbDataContainer, Container) then begin
Element := Container.ElementByName['Chunks count'];
if Assigned(Element) then
Result := Element.NativeValue;
end;
end;
function wbCoSaveChunkType(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Integer;
begin
Result := -1;
if not Assigned(aBasePtr) then Exit;
Result := PCardinal(aBasePtr)^;
end;
function wbCoSaveChunkTypeName(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): String;
begin
Result := '';
if not Assigned(aBasePtr) then Exit;
Result := wbStr4ToString(PCardinal(aBasePtr)^);
end;
function wbCoSaveArrayKeyElementDecider(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Integer;
var
Element : IwbElement;
Container: IwbDataContainer;
begin
Result := 0;
if not Assigned(aElement) then Exit;
Element := wbFindSaveElement('Array_var', aElement);
if Supports(Element, IwbDataContainer, Container) then begin
Element := Container.ElementByName['Key Type'];
if Assigned(Element) then
case Element.NativeValue of
1: Result := 1;
else
Result := 2;
end;
end;
end;
function wbCoSaveArrayDataElementDecider(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Integer;
var
Element : IwbElement;
Container: IwbDataContainer;
begin
Result := 0;
if not Assigned(aElement) then Exit;
Element := wbFindSaveElement('Element', aElement);
if Supports(Element, IwbDataContainer, Container) then begin
Element := Container.ElementByName['Data Type'];
if Assigned(Element) then
case Element.NativeValue of
1: Result := 1; // Numeric
2: Result := 2; // Form
3: Result := 3; // String
4: Result := 4; // Array
end;
end;
end;
function wbCoSaveArrayType(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Integer;
begin
Result := -1;
if not Assigned(aBasePtr) then Exit;
Result := PCardinal(aBasePtr)^;
end;
function wbCoSaveArrayTypeName(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): String;
begin
Result := '';
if not Assigned(aBasePtr) then Exit;
Result := IntToHex(PCardinal(aBasePtr)^, 8);
end;
function ToBeDeterminedDecider(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Integer;
begin
Result := 0;
end;
function ToBeDeterminedCounter(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Cardinal;
begin
Result := 0;
end;
function ToBeDeterminedCountCallback(aBasePtr: Pointer; aEndPtr: Pointer; const aElement: IwbElement): Cardinal;
begin
Result := 0;
end;
initialization
wbCoSaveArrayTypeEnum := wbEnum([
'Invalid',
'Numeric',
'Form',
'String',
'Array'
]);
end.