-
Notifications
You must be signed in to change notification settings - Fork 1
/
wbDataFormatNif.pas
8008 lines (7177 loc) · 289 KB
/
wbDataFormatNif.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
unit wbDataFormatNif;
interface
uses
Types, SysUtils, StrUtils, Classes, Variants, wbDataFormat, JsonDataObjects,
wbNifMath;
type
TwbNifVersion = (nfUnknown, nfTES3, nfTES4, nfFO3, nfTES5, nfSSE, nfFO4);
TwbNifOption = (nfoCollapseLinkArrays, nfoRemoveUnusedStrings);
TwbNifOptions = set of TwbNifOption;
TwbNiRefDef = class(TdfIntegerDef)
private
FTemplate: string;
FPtr: Boolean;
public
function CreateElement(const aParent: TdfElement): TdfElement; override;
function Clone: TdfDef; override;
procedure Assign(const aDef: TdfDef); override;
property Template: string read FTemplate write FTemplate;
// False - NiRef (points to previous blocks)
// True - NiPtr (points to following blocks)
property Ptr: Boolean read FPtr write FPtr;
end;
TwbNiRef = class(TdfInteger)
protected
function GetTemplate: string;
function GetPtr: Boolean;
public
property Template: string read GetTemplate;
property Ptr: Boolean read GetPtr;
end;
TwbNifBlock = class;
TwbNifBlocks = array of TwbNifBlock;
TwbNifFile = class;
TwbNifBlockDef = class(TdfStructDef)
function CreateElement(const aParent: TdfElement): TdfElement; override;
end;
TwbNifBlock = class(TdfStruct)
private
// NiRef and NiPtr elements in this block
FRefs: TList;
// indexed string elements in this block
FStrings: TList;
protected
function GetName: string; override;
function GetBlockType: string;
function GetNifFile: TwbNifFile;
function GetRef(Index: Integer): TwbNiRef;
function GetRefsCount: Integer;
function GetString(Index: Integer): TdfElement;
function GetStringsCount: Integer;
public
constructor Create(const aDef: TdfDef; const aParent: TdfElement); override;
destructor Destroy; override;
function Index: Integer; override;
procedure AddRef(const aElement: TdfElement);
procedure RemoveRef(const aElement: TdfElement);
procedure AddString(const aElement: TdfElement);
procedure RemoveString(const aElement: TdfElement);
function IsNiObject(const aTemplate: string; aInherited: Boolean = True): Boolean;
function AddChild(const aBlockType: string): TwbNifBlock;
function AddExtraData(const aBlockType: string): TwbNifBlock;
function AddProperty(const aBlockType: string): TwbNifBlock;
function PropertyByName(const aName: string): TwbNifBlock;
function ExtraDataByName(const aName: string): TwbNifBlock;
function ChildrenByType(const aBlockType: string; aInherited: Boolean = False): TwbNifBlocks;
function ChildByType(const aBlockType: string; aInherited: Boolean = False): TwbNifBlock;
function PropertiesByType(const aBlockType: string; aInherited: Boolean = False): TwbNifBlocks;
function PropertyByType(const aBlockType: string; aInherited: Boolean = False): TwbNifBlock;
function ExtraDatasByType(const aBlockType: string; aInherited: Boolean = False): TwbNifBlocks;
function ExtraDataByType(const aBlockType: string; aInherited: Boolean = False): TwbNifBlock;
function GetVertices(aElement: TdfElement = nil): TVector3Array;
function GetNormals(aElement: TdfElement = nil): TVector3Array;
function GetTexCoord(aElement: TdfElement = nil): TVector2Array;
function GetTriangles(aElement: TdfElement = nil): TTriangleArray;
function GetStrips(aElement: TdfElement = nil): TStripArray;
function UpdateBounds: Boolean;
function UpdateNormals(aAddIfMissing: Boolean = True): Boolean;
function UpdateTangents(aAddIfMissing: Boolean = True): Boolean;
function GetAssetsList: TStringDynArray;
property BlockType: string read GetBlockType;
property NifFile: TwbNifFile read GetNifFile;
property RefsCount: Integer read GetRefsCount;
property StringsCount: Integer read GetStringsCount;
property Refs[Index: Integer]: TwbNiRef read GetRef;
property Strings[Index: Integer]: TdfElement read GetString;
end;
TwbNifFile = class(TdfContainer)
private
FNifVersion: TwbNifVersion;
FOptions: TwbNifOptions;
FInternalUpdates: Boolean;
FStopAtIndex: Integer;
function ReadBlock(const aBlockType: string; const aDataStart, aDataEnd: Pointer): Integer;
protected
function GetHeader: TwbNifBlock;
function GetFooter: TwbNifBlock;
function GetBlocksCount: Integer;
function GetBlock(Index: Integer): TwbNifBlock;
procedure SetNifVersion(aVersion: TwbNifVersion);
procedure RemapBlocks(const aMap: array of Integer);
public
Version, UserVersion, UserVersion2: Cardinal;
constructor Create; reintroduce; overload;
function DataSize: Integer; override;
function UnSerialize(const aDataStart, aDataEnd: Pointer; const aDataSize: Integer): Integer; override;
function Serialize(const aDataStart, aDataEnd: Pointer): Integer; override;
procedure SerializeToJSON(const aJSON: TJSONBaseObject); override;
procedure UnSerializeFromJSON(const aJSON: TJSONBaseObject); override;
procedure Delete(Index: Integer); override;
procedure Move(CurIndex, NewIndex: Integer); override;
procedure UpdateNifVersion;
procedure UpdateHeader;
function AddBlock(const aBlockType: string): TwbNifBlock;
function InsertBlock(Index: Integer; const aBlockType: string): TwbNifBlock;
procedure ConvertBlock(Index: Integer; const aBlockType: string);
function CopyBlock(Index: Integer): TwbNifBlock;
function BlocksByType(const aBlockType: string; aInherited: Boolean = False): TwbNifBlocks;
function BlockByType(const aBlockType: string; aInherited: Boolean = False): TwbNifBlock;
function BlockByName(const aBlockName: string): TwbNifBlock;
function SpellTriangulate: Boolean;
function SpellFaceNormals: Boolean;
function SpellUpdateTangents: Boolean;
function SpellAddUpdateTangents: Boolean;
function GetAssetsList: TStringDynArray;
property NifVersion: TwbNifVersion read FNifVersion write SetNifVersion;
property Options: TwbNifOptions read FOptions write FOptions;
property Header: TwbNifBlock read GetHeader;
property Footer: TwbNifBlock read GetFooter;
property BlocksCount: Integer read GetBlocksCount;
property Blocks[Index: Integer]: TwbNifBlock read GetBlock;
property InternalUpdates: Boolean read FInternalUpdates write FInternalUpdates;
property StopAtIndex: Integer read FStopAtIndex write FStopAtIndex;
end;
{
function wbNiObjectExists(const aNiObject: string): Boolean;
procedure wbNiObjectCheckDups;
}
implementation
uses
wbDataFormatNifTypes, Math;
const
sNifMagicGamebryo = 'Gamebryo File Format, Version ';
sNifMagicNetImmerse = 'NetImmerse File Format, Version ';
type
TNiObjectInfo = record
Def: TdfDef;
Name: string;
Inherit: string;
InheritIndex: Integer;
NameHash: Cardinal;
IsAbstract: Boolean;
end;
TNiObjectInfos = record
NiObjects: array of TNiObjectInfo;
procedure Add(const aInherit: string; aIsAbstract: Boolean; aDef: TdfDef; aInsertIndex: Integer);
function IndexOf(const aName: string): Integer;
procedure FreeDefs;
end;
var
NiObjectInfos: TNiObjectInfos;
v4002 : Cardinal;
v4101 : Cardinal;
v41012 : Cardinal;
v4202 : Cardinal;
v4210 : Cardinal;
v4220 : Cardinal;
v10010 : Cardinal;
v10012 : Cardinal;
v10013 : Cardinal;
v10100 : Cardinal;
v1010106 : Cardinal;
v10200 : Cardinal;
v10401 : Cardinal;
v20103 : Cardinal;
v20004 : Cardinal;
v20005 : Cardinal;
v20007 : Cardinal;
v20101 : Cardinal;
v20205 : Cardinal;
v20207 : Cardinal;
procedure wbDefineNif; forward;
{ Helper functions }
function wbNifVersionToInt(const aVersion: string): Cardinal;
var
i: integer;
j: Cardinal;
begin
Result := 0;
with TStringList.Create do try
Delimiter := '.';
StrictDelimiter := True;
DelimitedText := aVersion;
for i := 0 to 3 do begin
if i < Count then
j := StrToIntDef(Strings[i], 0)
else
j := 0;
Result := Result or (j shl (24 - i*8));
end;
finally
Free;
end;
end;
function wbIntToNifVersion(aVersion: Cardinal): string;
begin
Result := Format('%d.%d.%d.%d', [
aVersion shr 24 and $ff,
aVersion shr 16 and $ff,
aVersion shr 8 and $ff,
aVersion and $ff
]);
end;
function nif(const e: TdfElement): TwbNifFile; inline;
begin
Result := TwbNifFile(e.Root);
end;
function nifblk(const e: TdfElement): TwbNifBlock; inline;
var
Element: TdfElement;
begin
Element := e.Parent;
// get to the containing nif block
while Assigned(Element) and not (Element is TwbNifBlock) do
Element := Element.Parent;
Result := TwbNifBlock(Element);
end;
procedure wbGetVector2(var Vector: TVector2; const aElement: TdfElement; asText: Boolean = False); inline;
begin
if asText then begin
Vector.v[0] := dfStrToFloat(aElement.EditValues['[0]']);
Vector.v[1] := dfStrToFloat(aElement.EditValues['[1]']);
end
else begin
Vector.v[0] := aElement.NativeValues['[0]'];
Vector.v[1] := aElement.NativeValues['[1]'];
end;
end;
procedure wbSetVector2(const Vector: TVector2; const aElement: TdfElement; asText: Boolean = False); inline;
begin
if asText then begin
aElement.EditValues['[0]'] := dfFloatToStr(Vector.v[0]);
aElement.EditValues['[1]'] := dfFloatToStr(Vector.v[1]);
end
else begin
aElement.NativeValues['[0]'] := Vector.v[0];
aElement.NativeValues['[1]'] := Vector.v[1];
end;
end;
procedure wbGetVector3(var Vector: TVector3; const aElement: TdfElement; asText: Boolean = False); inline;
begin
if asText then begin
Vector.v[0] := dfStrToFloat(aElement.EditValues['[0]']);
Vector.v[1] := dfStrToFloat(aElement.EditValues['[1]']);
Vector.v[2] := dfStrToFloat(aElement.EditValues['[2]']);
end
else begin
Vector.v[0] := aElement.NativeValues['[0]'];
Vector.v[1] := aElement.NativeValues['[1]'];
Vector.v[2] := aElement.NativeValues['[2]'];
end;
end;
procedure wbSetVector3(const Vector: TVector3; const aElement: TdfElement; asText: Boolean = False); inline;
begin
if asText then begin
aElement.EditValues['[0]'] := dfFloatToStr(Vector.v[0]);
aElement.EditValues['[1]'] := dfFloatToStr(Vector.v[1]);
aElement.EditValues['[2]'] := dfFloatToStr(Vector.v[2]);
end
else begin
aElement.NativeValues['[0]'] := Vector.v[0];
aElement.NativeValues['[1]'] := Vector.v[1];
aElement.NativeValues['[2]'] := Vector.v[2];
end;
end;
procedure wbGetTriangle(var Tri: TTriangle; const aElement: TdfElement); inline;
begin
Tri[0] := aElement.NativeValues['[0]'];
Tri[1] := aElement.NativeValues['[1]'];
Tri[2] := aElement.NativeValues['[2]'];
end;
procedure wbSetTriangle(const Tri: TTriangle; const aElement: TdfElement); inline;
begin
aElement.NativeValues['[0]'] := Tri[0];
aElement.NativeValues['[1]'] := Tri[1];
aElement.NativeValues['[2]'] := Tri[2];
end;
{ NiObjectInfos }
procedure TNiObjectInfos.Add(const aInherit: string; aIsAbstract: Boolean; aDef: TdfDef; aInsertIndex: Integer);
var
i: integer;
begin
if IndexOf(aDef.Name) <> -1 then
raise Exception.Create('Definition of ' + aDef.Name + ' already exists');
if aInherit <> '' then begin
i := IndexOf(aInherit);
if i = -1 then
raise Exception.Create('Unknown NiObject to inherit from: ' + aInherit);
aDef.InsertDefsFrom(NiObjects[i].Def, aInsertIndex);
end else
i := -1;
SetLength(NiObjects, Succ(Length(NiObjects)));
with NiObjects[Pred(Length(NiObjects))] do begin
Def := aDef;
Name := aDef.Name;
NameHash := dfCalcHash(Name);
Inherit := aInherit;
InheritIndex := i;
IsAbstract := aIsAbstract;
end;
end;
function TNiObjectInfos.IndexOf(const aName: string): Integer;
var
i: integer;
h: Cardinal;
begin
Result := -1;
h := dfCalcHash(aName);
for i := Low(NiObjects) to High(NiObjects) do
if NiObjects[i].NameHash = h then begin
Result := i;
Break;
end;
end;
procedure TNiObjectInfos.FreeDefs;
var
i: integer;
begin
for i := Low(NiObjects) to High(NiObjects) do
NiObjects[i].Def.Free;
end;
procedure FreeNiObjectDefs;
begin
NiObjectInfos.FreeDefs;
end;
procedure wbNiObject(aDef: TdfDef; const aInherit: string; aIsAbstract: Boolean; aInsertIndex: Integer = 0); overload;
begin
NiObjectInfos.Add(aInherit, aIsAbstract, aDef, aInsertIndex);
end;
procedure wbNiObject(aDef: TdfDef); overload;
begin
NiObjectInfos.Add('', False, aDef, 0);
end;
{
function wbNiObjectExists(const aNiObject: string): Boolean;
begin
Result := NiObjectInfos.IndexOf(aNiObject) <> -1;
end;
procedure wbNiObjectCheckDups;
procedure Check(d: TdfDef; const aParent: string);
var
i: integer;
sl: TStringList;
begin
if d.DataType <> dtStruct then Exit;
sl := TStringList.Create;
for i := Low(d.Defs) to High(d.Defs) do begin
if not (Assigned(d.Defs[i].OnGetEnabled)) and (d.Defs[i].Name <> '') then
if sl.IndexOf(d.Defs[i].Name) <> -1 then
WriteLn(aParent + ' \ ' + d.Defs[i].Name)
else
sl.Add(d.Defs[i].Name);
Check(d.Defs[i], aParent + ' \ ' + d.Name);
end;
sl.Free;
end;
var
i: integer;
begin
for i := Low(NiObjectInfos.NiObjects) to High(NiObjectInfos.NiObjects) do
Check(NiObjectInfos.NiObjects[i].Def, NiObjectInfos.NiObjects[i].Def.Name);
end;
}
function wbNiObjectDef(const aNiObject: string): TdfDef;
var
i: integer;
begin
if Length(NiObjectInfos.NiObjects) = 0 then
wbDefineNif;
i := NiObjectInfos.IndexOf(aNiObject);
if i = -1 then
raise Exception.Create('Unknown NiObject: ' + aNiObject);
if NiObjectInfos.NiObjects[i].IsAbstract then
raise Exception.Create('Can not initialize from asbtract NiObject: ' + aNiObject);
Result := NiObjectInfos.NiObjects[i].Def;
end;
function wbIsNiObject(const aNiObject, aTemplate: string): Boolean; overload;
var
i: Integer;
h: Cardinal;
begin
h := dfCalcHash(aTemplate);
i := NiObjectInfos.IndexOf(aNiObject);
Result := False;
repeat
if NiObjectInfos.NiObjects[i].NameHash = h then begin
Result := True;
Exit;
end;
i := NiObjectInfos.NiObjects[i].InheritIndex;
until i = -1;
end;
function wbIsNiObject(aElement: TdfElement; const aTemplate: string): Boolean; overload;
begin
// get to the containing nif block
while Assigned(aElement) and not (aElement is TwbNifBlock) do
aElement := aElement.Parent;
if Assigned(aElement) then
Result := wbIsNiObject(aElement.Def.Name, aTemplate)
else
Result := False;
end;
{ TwbNifBlock }
function wbNifBlock(
const aName: string;
const aDefs: array of TdfDef;
const aEvents: array of const
): TwbNifBlockDef; overload;
begin
Result := TwbNifBlockDef.Create(aName, dtStruct, aDefs);
Result.AssignEvents(aEvents);
end;
function wbNifBlock(const aName: string; const aDefs: array of TdfDef): TwbNifBlockDef; overload;
begin
Result := wbNifBlock(aName, aDefs, []);
end;
function wbNifBlock(const aName: string): TwbNifBlockDef; overload;
begin
Result := wbNifBlock(aName, [], []);
end;
function TwbNifBlockDef.CreateElement(const aParent: TdfElement): TdfElement;
begin
Result := TwbNifBlock.Create(Self, aParent);
end;
constructor TwbNifBlock.Create(const aDef: TdfDef; const aParent: TdfElement);
begin
inherited;
FRefs := TList.Create;
FStrings := TList.Create;
end;
destructor TwbNifBlock.Destroy;
begin
inherited;
FRefs.Free;
FStrings.Free;
end;
function TwbNifBlock.GetName: string;
begin
Result := BlockType;
if (Result <> 'NiHeader') and (Result <> 'NiFooter') then
Result := IntToStr(Index) + ' ' + Result;
end;
function TwbNifBlock.GetBlockType: string;
begin
Result := Def.Name;
end;
function TwbNifBlock.GetNifFile: TwbNifFile;
begin
Result := nif(Self);
end;
function TwbNifBlock.Index: Integer;
begin
Result := inherited;
// -1 to exclude NiHeader
if BlockType <> 'NiHeader' then
Dec(Result);
end;
function TwbNifBlock.GetRef(Index: Integer): TwbNiRef;
begin
Result := TwbNiRef(FRefs[Index]);
end;
function TwbNifBlock.GetRefsCount: Integer;
begin
Result := FRefs.Count;
end;
function TwbNifBlock.GetString(Index: Integer): TdfElement;
begin
Result := TdfElement(FStrings[Index]);
end;
function TwbNifBlock.GetStringsCount: Integer;
begin
Result := FStrings.Count;
end;
procedure TwbNifBlock.AddRef(const aElement: TdfElement);
begin
FRefs.Add(aElement);
end;
procedure TwbNifBlock.RemoveRef(const aElement: TdfElement);
begin
if not (dsDestroying in DataState) then
FRefs.Remove(aElement);
end;
procedure TwbNifBlock.AddString(const aElement: TdfElement);
begin
FStrings.Add(aElement);
end;
procedure TwbNifBlock.RemoveString(const aElement: TdfElement);
begin
if not (dsDestroying in DataState) then
FStrings.Remove(aElement);
end;
function TwbNifBlock.IsNiObject(const aTemplate: string; aInherited: Boolean = True): Boolean;
begin
Result := (not aInherited and (BlockType = aTemplate)) or (aInherited and wbIsNiObject(BlockType, aTemplate));
end;
function TwbNifBlock.AddChild(const aBlockType: string): TwbNifBlock;
var
Children, Child: TdfElement;
i: integer;
begin
Children := Elements['Children'];
if not Assigned(Children) then
DoException('Can not have children');
// find existing None entry if any
Child := nil;
for i := 0 to Pred(Children.Count) do
if Children[i].NativeValue = -1 then begin
Child := Children[i];
Break;
end;
// or add a new one
if not Assigned(Child) then
Child := Children.Add;
Result := NifFile.AddBlock(aBlockType);
Child.NativeValue := Result.Index;
end;
function TwbNifBlock.AddExtraData(const aBlockType: string): TwbNifBlock;
var
Datas, Data: TdfElement;
i: integer;
begin
Datas := Elements['Extra Data List'];
if not Assigned(Datas) then
Datas := Elements['Extra Data'];
if not Assigned(Datas) then
DoException('Can not have extra data');
// if a single value (Morrowind meshes)
if Datas is TdfValue then
Data := Datas
else begin
// find existing None entry if any
Data := nil;
for i := 0 to Pred(Datas.Count) do
if Datas[i].NativeValue = -1 then begin
Data := Datas[i];
Break;
end;
// or add a new one
if not Assigned(Data) then
Data := Datas.Add;
end;
if Data.NativeValue <> -1 then
DoException('Can not add more extra data');
Result := NifFile.AddBlock(aBlockType);
Data.NativeValue := Result.Index;
end;
function TwbNifBlock.AddProperty(const aBlockType: string): TwbNifBlock;
var
Props, Prop: TdfElement;
i: integer;
begin
Prop := Elements['Shader Property'];
if Assigned(Prop) and wbIsNiObject(aBlockType, 'BSShaderProperty') then begin
if Prop.LinksTo <> nil then
raise Exception.Create('Block already has a shader property, can not add a new one');
Result := NifFile.AddBlock(aBlockType);
Prop.NativeValue := Result.Index;
Exit;
end;
Prop := Elements['Alpha Property'];
if Assigned(Prop) and wbIsNiObject(aBlockType, 'NiAlphaProperty') then begin
if Prop.LinksTo <> nil then
raise Exception.Create('Block already has an alpha property, can not add a new one');
Result := NifFile.AddBlock(aBlockType);
Prop.NativeValue := Result.Index;
Exit;
end;
Props := Elements['Properties'];
if not Assigned(Props) then
DoException('Block can not have properties');
// find existing None entry if any
Prop := nil;
for i := 0 to Pred(Props.Count) do
if Props[i].NativeValue = -1 then begin
Prop := Props[i];
Break;
end;
// or add a new one
if not Assigned(Prop) then
Prop := Props.Add;
Result := NifFile.AddBlock(aBlockType);
Prop.NativeValue := Result.Index;
end;
function TwbNifBlock.PropertyByName(const aName: string): TwbNifBlock;
var
Props: TdfElement;
i: integer;
begin
Props := Elements['Shader Property'];
if Assigned(Props) and (Props.LinksTo <> nil) and (Props.LinksTo.EditValues['Name'] = aName) then begin
Result := TwbNifBlock(Props.LinksTo);
Exit;
end;
Props := Elements['Alpha Property'];
if Assigned(Props) and (Props.LinksTo <> nil) and (Props.LinksTo.EditValues['Name'] = aName) then begin
Result := TwbNifBlock(Props.LinksTo);
Exit;
end;
Props := Elements['Properties'];
if Assigned(Props) then
for i := 0 to Pred(Props.Count) do begin
Result := TwbNifBlock(Props[i].LinksTo);
if Assigned(Result) and (Result.EditValues['Name'] = aName) then
Exit;
end;
Result := nil;
end;
function TwbNifBlock.ExtraDataByName(const aName: string): TwbNifBlock;
var
Datas: TdfElement;
i: integer;
begin
Datas := Elements['Extra Data List'];
if Assigned(Datas) then begin
for i := 0 to Pred(Datas.Count) do begin
Result := TwbNifBlock(Datas[i].LinksTo);
if Assigned(Result) and (Result.EditValues['Name'] = aName) then
Exit;
end;
end
else begin
Datas := Elements['Extra Data'];
if Assigned(Datas) then begin
Result := TwbNifBlock(Datas.LinksTo);
if Assigned(Result) and (Result.EditValues['Name'] = aName) then
Exit;
end;
end;
Result := nil;
end;
function TwbNifBlock.ChildrenByType(const aBlockType: string; aInherited: Boolean = False): TwbNifBlocks;
var
Children: TdfElement;
Child: TwbNifBlock;
i: integer;
begin
Children := Elements['Children'];
if not Assigned(Children) then
Exit;
for i := 0 to Pred(Children.Count) do begin
Child := TwbNifBlock(Children[i].LinksTo);
if Assigned(Child) and Child.IsNiObject(aBlockType, aInherited) then begin
SetLength(Result, Succ(Length(Result)));
Result[Pred(Length(Result))] := Child;
end;
end;
end;
function TwbNifBlock.ChildByType(const aBlockType: string; aInherited: Boolean = False): TwbNifBlock;
var
Children: TwbNifBlocks;
begin
Children := ChildrenByType(aBlockType, aInherited);
if Length(Children) <> 0 then
Result := Children[0]
else
Result := nil;
end;
function TwbNifBlock.PropertiesByType(const aBlockType: string; aInherited: Boolean = False): TwbNifBlocks;
var
Props: TdfElement;
Prop: TwbNifBlock;
i: integer;
begin
Props := Elements['Shader Property'];
if Assigned(Props) then begin
Prop := TwbNifBlock(Props.LinksTo);
if Assigned(Prop) and Prop.IsNiObject(aBlockType, aInherited) then begin
SetLength(Result, Succ(Length(Result)));
Result[Pred(Length(Result))] := Prop;
end;
end;
Props := Elements['Alpha Property'];
if Assigned(Props) then begin
Prop := TwbNifBlock(Props.LinksTo);
if Assigned(Prop) and Prop.IsNiObject(aBlockType, aInherited) then begin
SetLength(Result, Succ(Length(Result)));
Result[Pred(Length(Result))] := Prop;
end;
end;
Props := Elements['Properties'];
if Assigned(Props) then
for i := 0 to Pred(Props.Count) do begin
Prop := TwbNifBlock(Props[i].LinksTo);
if Assigned(Prop) and Prop.IsNiObject(aBlockType, aInherited) then begin
SetLength(Result, Succ(Length(Result)));
Result[Pred(Length(Result))] := Prop;
end;
end;
end;
function TwbNifBlock.PropertyByType(const aBlockType: string; aInherited: Boolean = False): TwbNifBlock;
var
Props: TwbNifBlocks;
begin
Props := PropertiesByType(aBlockType, aInherited);
if Length(Props) <> 0 then
Result := Props[0]
else
Result := nil;
end;
function TwbNifBlock.ExtraDatasByType(const aBlockType: string; aInherited: Boolean = False): TwbNifBlocks;
var
Datas: TdfElement;
Data: TwbNifBlock;
i: integer;
begin
Datas := Elements['Extra Data List'];
if Assigned(Datas) then begin
for i := 0 to Pred(Datas.Count) do begin
Data := TwbNifBlock(Datas[i].LinksTo);
if Assigned(Data) and Data.IsNiObject(aBlockType, aInherited) then begin
SetLength(Result, Succ(Length(Result)));
Result[Pred(Length(Result))] := Data;
end;
end;
end
else begin
Datas := Elements['Extra Data'];
if Assigned(Datas) then begin
Data := TwbNifBlock(Datas.LinksTo);
if Assigned(Data) and Data.IsNiObject(aBlockType, aInherited) then begin
SetLength(Result, Succ(Length(Result)));
Result[Pred(Length(Result))] := Data;
end;
end;
end;
end;
function TwbNifBlock.ExtraDataByType(const aBlockType: string; aInherited: Boolean = False): TwbNifBlock;
var
Datas: TwbNifBlocks;
begin
Datas := ExtraDatasByType(aBlockType, aInherited);
if Length(Datas) <> 0 then
Result := Datas[0]
else
Result := nil;
end;
function TwbNifBlock.GetVertices(aElement: TdfElement = nil): TVector3Array;
var
i: integer;
Entries: TdfElement;
begin
if not Assigned(aElement) then
aElement := Self;
if IsNiObject('BSTriShape') then begin
if not aElement.NativeValues['VertexDesc\VF\VF_VERTEX'] then
Exit;
Entries := aElement.Elements['Vertex Data'];
if not Assigned(Entries) then
Exit;
SetLength(Result, Entries.Count);
for i := 0 to Pred(Entries.Count) do
wbGetVector3(Result[i], Entries[i][0], True); // Elements['Vertex'];
end
else if IsNiObject('NiTriBasedGeomData') then begin
Entries := aElement.Elements['Vertices'];
if not Assigned(Entries) then
Exit;
SetLength(Result, Entries.Count);
for i := 0 to Pred(Entries.Count) do
wbGetVector3(Result[i], Entries[i]);
end;
end;
function TwbNifBlock.GetNormals(aElement: TdfElement = nil): TVector3Array;
var
i: integer;
Entries: TdfElement;
begin
if not Assigned(aElement) then
aElement := Self;
if IsNiObject('BSTriShape') then begin
if not aElement.NativeValues['VertexDesc\VF\VF_NORMAL'] then
Exit;
Entries := aElement.Elements['Vertex Data'];
if not Assigned(Entries) then
Exit;
SetLength(Result, Entries.Count);
for i := 0 to Pred(Entries.Count) do
wbGetVector3(Result[i], Entries[i][4], True); // Elements['Normal'];
end
else if IsNiObject('NiTriBasedGeomData') then begin
Entries := aElement.Elements['Normals'];
if not Assigned(Entries) then
Exit;
SetLength(Result, Entries.Count);
for i := 0 to Pred(Entries.Count) do
wbGetVector3(Result[i], Entries[i]);
end;
end;
function TwbNifBlock.GetTexCoord(aElement: TdfElement = nil): TVector2Array;
var
i: integer;
Entries: TdfElement;
begin
if not Assigned(aElement) then
aElement := Self;
if IsNiObject('BSTriShape') then begin
if not aElement.NativeValues['VertexDesc\VF\VF_UV'] then
Exit;
Entries := aElement.Elements['Vertex Data'];
if not Assigned(Entries) then
Exit;
SetLength(Result, Entries.Count);
for i := 0 to Pred(Entries.Count) do
wbGetVector2(Result[i], Entries[i][3], True); // Elements['UV'];
end
else if IsNiObject('NiTriBasedGeomData') then begin
Entries := aElement.Elements['UV Sets'];
if Assigned(Entries) and (Entries.Count <> 0) then begin
Entries := Entries[0];
SetLength(Result, Entries.Count);
for i := 0 to Pred(Entries.Count) do
wbGetVector2(Result[i], Entries[i]);
end;
end;
end;
function TwbNifBlock.GetTriangles(aElement: TdfElement = nil): TTriangleArray;
var
i: integer;
Entries: TdfElement;
begin
if not Assigned(aElement) then
aElement := Self;
Entries := aElement.Elements['Triangles'];
if Assigned(Entries) then begin
SetLength(Result, Entries.Count);
for i := 0 to Pred(Entries.Count) do
wbGetTriangle(Result[i], Entries[i]);
end
else
Result := Triangulate(GetStrips);
end;
function TwbNifBlock.GetStrips(aElement: TdfElement = nil): TStripArray;
var
i, j: integer;
Entries, e: TdfElement;
begin
if not Assigned(aElement) then
aElement := Self;
Entries := aElement.Elements['Strips'];
if Assigned(Entries) then begin
for i := 0 to Pred(Entries.Count) do begin
SetLength(Result, Succ(Length(Result)));
e := Entries[i];
SetLength(Result[i], e.Count);
for j := 0 to Pred(e.Count) do
Result[i][j] := e[j].NativeValue;
end;
end;
end;
function TwbNifBlock.UpdateBounds: Boolean;
var
verts: TVector3Array;
center: TVector3;
r: Extended;
e: TdfElement;
begin
Result := False;
verts := GetVertices;
if Length(verts) = 0 then
Exit;
CalculateCenterRadius(verts, center, r,