-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUGFFFile.pas
More file actions
3526 lines (2879 loc) · 128 KB
/
UGFFFile.pas
File metadata and controls
3526 lines (2879 loc) · 128 KB
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 UGFFFile;
// =============================================================================
// CLASS FOR READING, HANDLING AND WRITING BIOWARE GFF V3.2 FORMAT FILES.
// =============================================================================
// Main class: TGFFFile
// Last changed: 2006-01-09
// Version: 1.0.1a
// -----------------------------------------------------------------------------
//
// KNOWN PROBLEMS:
// --------------
// * Currently no support for modifying DWORD64 fields. I *really* should use a
// newer version of Delphi that has this data type... :/
//
// * Currently no support for modifying VOID/Binary values, because I am lazy...
//
// * Root struct Field iterator only supports one concurrent loop. Use the root
// and fields properties for now if additional loops are needed.
//
// * Internal data structure is poorly protected, but since I'm the only one using
// this I'll have to manually take care to modify things the correct way. :)
//
//
// Version history: (as far as I remember to update it)
// ----------------
// 1.0.1a (2006-01-09)
// * Added ChangeFieldValue() shortcut function to allow quickly changing the
// value of most types of fields as a string regardless of what kind of datatype
// the field is. This is mostly meant for TSLPatcher and thus uses that syntax
// for changing EcoLocStrings, Positions and Orientations.
//
// 1.0 (2005-10-08)
// * Handler and support classes rewritten from scratch to get rid of the
// horribly awkward internal data handling of the previous version.
//
// * Changed internal data keeping model into a logical Tree structure.
//
// * Rewritten all logic for reading and writing files from to accomodate
// the new internal file format.
//
// * Added functionality to fetch a field by label-path.
//
// * Added functionality for adding new fields to the tree structure.
//
// * Added functionality for deleting fields/sections of the tree structure.
//
// * Added functionality to TGFF_CExoLocString for adding and removing substrings.
//
// * Added Clone() method to the TGFF_Field superclass which will clone the field
// and any sub-fields in case of a LIST or STRUCT field. (Not very pretty right
// now since it isn't virtual to be overridden in the subclasses. The superclass
// currently does all the work.)
interface
uses SysUtils, Classes, Windows, UStrTok, UST_Common;
// Size of buffers used for some read/write operations...
const BYTE_BUFFER_SIZE = 4096;
const TEXT_BUFFER_SIZE = 32768;
// GFFField type definition constants...
const FIELD_TYPE_BYTE = 0;
const FIELD_TYPE_CHAR = 1;
const FIELD_TYPE_WORD = 2;
const FIELD_TYPE_SHORT = 3;
const FIELD_TYPE_DWORD = 4;
const FIELD_TYPE_INT = 5;
const FIELD_TYPE_DWORD64 = 6;
const FIELD_TYPE_INT64 = 7;
const FIELD_TYPE_FLOAT = 8;
const FIELD_TYPE_DOUBLE = 9;
const FIELD_TYPE_CEXOSTRING = 10;
const FIELD_TYPE_RESREF = 11;
const FIELD_TYPE_CEXOLOCSTRING = 12;
const FIELD_TYPE_VOID = 13;
const FIELD_TYPE_STRUCT = 14;
const FIELD_TYPE_LIST = 15;
const FIELD_TYPE_ORIENTATION = 16;
const FIELD_TYPE_POSITION = 17;
type
// -----------------------------------------------------------------------------
// SOME TYPE DEFINITIONS
// -----------------------------------------------------------------------------
// Fixed length types...
T4Char = array [0..3] of Char;
T16Char = array [0..15] of Char;
T8Bytes = array [0..7] of Byte;
TBuffer = array [0..TEXT_BUFFER_SIZE] of Char;
// Variable length types...
TChars = array of Char;
TBytes = array of Byte;
TFieldIndex = array of DWORD;
// -----------------------------------------------------------------------------
// SUPPORT CLASSES USED WHEN SAVING/LOADING FILES
// -----------------------------------------------------------------------------
TGFF_Header = class(TObject)
filetype : T4Char; // Filetype, ie GFF, UTI, UTC, DLG etc...
fileversion : T4Char; // Version of GFF format, should be V3.2
structoffset : DWORD; // Absolute offset where stuct array begins
structcount : DWORD; // Number of elements in the Struct array
fieldoffset : DWORD; // Absolute offset where field array begind
fieldcount : DWORD; // Number of elements in the field array
labeloffset : DWORD;
labelcount : DWORD; // Number of elements in the labels array
fielddataoffset : DWORD;
fielddatacount : DWORD; // Bytes occupied by the field data block
fieldindexoffset : DWORD;
fieldindexcount : DWORD; // Bytes occupied by the field index array
listindexoffset : DWORD;
listindexcount : DWORD; // Bytes occupied by listindex array
end;
TGFF_SaveData = class(TObject)
// Data needed to build file header
StructCount : DWORD;
FieldCount : DWORD;
FieldIndexCount : DWORD;
ListIndexCount : DWORD;
ListCount : DWORD;
DataBlockSize : DWORD;
FieldLabels : array of T16Char;
// Data to keep track of write progress in the sections
CurrStructIndex : DWORD;
CurrStructOffset : DWORD;
CurrFieldIndex : DWORD;
CurrFieldOffset : DWORD;
CurrFieldDataOffset : DWORD;
CurrFieldIndexOffset : DWORD;
CurrListIndexOffset : DWORD;
procedure AddLabel(sLabel : T16Char);
constructor Create();
destructor Destroy(); override;
end;
// -----------------------------------------------------------------------------
// EXCEPTION TYPE USED BY THESE CLASSES....
// -----------------------------------------------------------------------------
EGFFError = class(Exception);
// -----------------------------------------------------------------------------
// GFF CLASS TYPE DATACARRIERS
// -----------------------------------------------------------------------------
TGFFField = class(TObject) // ABSTRACT super-class for all GFF data types.
private
l_label : T16Char;
l_type : DWORD;
public
procedure SetLabel(sLabel : string);
procedure SetLabelRaw(sLabel : T16Char);
function GetLabel() : string;
function GetLabelRaw() : T16Char;
function GetString() : string;
function Clone() : TGFFField; // This really should be virtual+abstract but I can't be bothered right now :)
constructor Create(); overload;
constructor Create(sLabel : string); overload;
destructor Destroy(); override;
property fieldtype : DWORD read l_type write l_type;
property fieldlabel : string read GetLabel write SetLabel;
property fieldlabelraw : T16Char read GetLabelRaw write SetLabelRaw;
property text : string read GetString;
end;
TGFFStruct = class(TGFFField)
private
l_fields : array of TGFFField;
l_type : DWORD; // The custom, programmer-set Struct Type-ID.
l_count : DWORD; // The number of fields in this Struct.
function GetField(i : integer) : TGFFField;
public
procedure AddField(oField : TGFFField);
procedure DeleteField(sLabel : string);
procedure ReplaceField(i : integer; oField : TGFFField);
function GetFieldByLabel(sLabel : string) : TGFFField;
constructor Create(); overload;
constructor Create(sLabel : string); overload;
Destructor Destroy(); override;
property typeid : DWORD read l_type write l_type;
property count : DWORD read l_count;
property fields[i : integer] : TGFFField read GetField;
end;
TGFFList = Class(TGFFField)
private
l_count : DWORD;
l_structs : array of TGFFStruct;
function GetStruct(iIndex : integer) : TGFFStruct;
public
procedure AddStruct(oStruct : TGFFStruct);
procedure DeleteStruct(iIndex : DWORD);
constructor Create(); overload;
constructor Create(sLabel : string); overload;
destructor Destroy(); override;
property count : DWORD read l_count;
property structs[i : integer] : TGFFStruct read GetStruct;
end;
// -----------------------------------------------------------------------------
// GFF DATATYPE WRAPPER CLASSES
// -----------------------------------------------------------------------------
TGFF_SByte = class(TGFFField)
value : Byte;
constructor Create(); overload;
constructor Create(sLabel : string; iData : Byte); overload;
end;
TGFF_SChar = class(TGFFField)
value : Char;
constructor Create(); overload;
constructor Create(sLabel : string; cData : Char); overload;
end;
TGFF_SWord = class(TGFFField)
value : Word;
constructor Create(); overload;
constructor Create(sLabel : string; iData : Word); overload;
end;
TGFF_SShort = class(TGFFField)
value : SmallInt;
constructor Create(); overload;
constructor Create(sLabel : string; iData : SmallInt); overload;
end;
TGFF_SDWORD = class(TGFFField)
value : DWORD;
constructor Create(); overload;
constructor Create(sLabel : string; iData : DWORD); overload;
end;
TGFF_SInt = class(TGFFField)
value : LongInt;
constructor Create(); overload;
constructor Create(sLabel : string; iData : LongInt); overload;
end;
TGFF_CDWORD64 = class(TGFFField)
value : T8Bytes;
constructor Create(); overload;
constructor Create(sLabel : string; aData : T8Bytes); overload;
end;
TGFF_CInt64 = class(TGFFField)
value : Int64;
constructor Create(); overload;
constructor Create(sLabel : string; iData : Int64); overload;
end;
TGFF_SFloat = class(TGFFField)
value : Single;
constructor Create(); overload;
constructor Create(sLabel : string; fData : Single); overload;
end;
TGFF_CDouble = class(TGFFField)
value : Double;
constructor Create(); overload;
constructor Create(sLabel : string; fData : Double); overload;
end;
TGFF_CExoString = class(TGFFField)
size : DWORD;
text : TChars;
constructor Create(); overload;
constructor Create(sLabel : string; sData : string); overload;
destructor Destroy(); override;
procedure SetString(sText : string);
function GetString() : string;
// Use this property rather than the Get/Set methods to access the text...
property textstring : string read GetString write SetString;
end;
TGFF_CResRef = class(TGFFField)
size : Byte;
text : TChars;
constructor Create(); overload;
constructor Create(sLabel : string; sData : string); overload;
destructor Destroy(); override;
procedure SetString(sText : string);
function GetString() : string;
// Use this property rather than the Get/Set methods to access the text...
property textstring : string read GetString write SetString;
end;
// Support class for CExoLocString
TGFF_CSubString = class (TObject)
stringid : LongInt;
stringlength : LongInt;
text : TChars;
constructor Create(); overload;
constructor Create(nID : LongInt; sText : string); overload;
destructor Destroy(); override;
procedure SetString(sText : string);
function GetString() : string;
// Use this property rather than the Get/Set methods to access the text...
property textstring : string read GetString write SetString;
end;
TGFF_CExoLocString = class(TGFFField)
bytesize : DWORD; // Size of whole structure, excluding this field.
strref : DWORD;
stringcount : DWORD;
substrings : array of TGFF_CSubString;
constructor Create(); overload;
constructor Create(sLabel : string; iStrRef : DWORD); overload;
destructor Destroy(); override;
procedure AddString(iLangID : integer; sText : string);
procedure SetString(iIndex : integer; sText : string);
procedure DeleteString(iIndex : integer);
procedure DeleteStringByID(iLangID : integer);
procedure SetStringByID(iLangID : integer; sText : string);
function GetStringById(iLangID : integer) : string;
function GetString(iIndex : integer) : string;
function GetLocString(iIndex : integer) : TGFF_CSubString;
property strings[i:integer] : string read GetString write SetString;
property locstrings[i:integer] : TGFF_CSubString read GetLocString;
end;
TGFF_CVoid = class(TGFFField)
bytesize : DWORD;
data : TBytes;
constructor Create(); overload;
constructor Create(sLabel : string; aData : TBytes); overload;
destructor Destroy(); override;
end;
// FIX(2005-05-31) Undocumented type added in KotOR...
TGFF_COrientation = class(TGFFField)
value : array [0..3] of Single;
constructor Create(); overload;
constructor Create(sLabel : string; fData1 : Single; fData2 : Single; fData3 : Single; fData4 : Single); overload;
end;
// FIX(2005-05-31) Undocumented type added in KotOR...
TGFF_CPosition = class(TGFFField)
value : array [0..2] of Single;
constructor Create(); overload;
constructor Create(sLabel : string; fX : Single; fY : Single; fZ : Single); overload;
end;
// -----------------------------------------------------------------------------
// MAIN GFF HANDLER CLASS
// -----------------------------------------------------------------------------
TGFFFile = class(TObject)
private
l_filetype : T4Char; // 4 character file type (UTI, DLG etc...)
l_fileversion : T4Char; // 4 character version info (e.g. V1.0)
l_filename : string; // Path/Name of the currently loaded file.
l_rootstruct : TGFFStruct; // Root struct containing the data in this file
l_currfield : DWORD; // Iterator for fetching the fields in the Root Struct.
l_isloaded : boolean; // Set to TRUE when a file has been loaded into this object
l_isdirty : boolean; // Set to TRUE if the GFF data has been changed since the file has been loaded.
l_file : TFileStream; // File handle, used for reading and writing from the file on disk.
l_header : TGFF_Header; // Reference to File Header object. Used when loading a file.
l_savedata : TGFF_SaveData; // Temporary container used to collect data when saving a file...
procedure ResetAll();
// Get/Set methods for the Type and Version properties.
procedure SetType(sType : string);
procedure SetVersion(sVersion : string);
function GetType() : string;
function GetVersion() : string;
// Functions used to load file data...
function LoadFileStruct(iOffset : DWORD) : TGFFStruct;
function LoadFileField(iOffset : DWORD) : TGFFField;
function LoadComplexField(iType : DWORD; iDataOrOffset : DWORD) : TGFFField;
// Functions used to save the data to a file...
function SaveGetLabelIndex(sLabel : T16Char) : DWORD;
function SaveProcessList(oList : TGFFList) : DWORD;
function SaveProcessComplexFieldData(oField : TGFFField) : DWORD;
function SaveProcessField(oField : TGFFField) : DWORD;
function SaveProcessStruct(oStruct : TGFFStruct) : DWORD;
function GetIsComplexField(oField : TGFFField) : boolean;
function GetFieldDataSize(oField : TGFFField) : DWORD;
procedure SaveParseList(oList : TGFFList);
procedure SaveParseStruct(oStruct : TGFFStruct);
procedure SaveProcessLabels();
public
// Functions for retrieving the data fields
function GetFieldByLabel(sFieldPath : string) : TGFFField;
function GetFirstRootField() : TGFFField;
function GetNextRootField() : TGFFField;
// functions for adding new fields or deleting existing ones
procedure AddField(oField : TGFFField; sPath : string);
procedure DeleteField(sFieldPath : string);
function ChangeFieldValue(sPath : string; sValue : string) : boolean;
// Functions for creating/loading/saving a GFF file.
procedure NewFile(sType : string; sFilename : string = '');
procedure LoadFile(sFilename : string);
procedure SaveFile(sFilename : string = '');
constructor Create();
destructor Destroy(); override;
property filetype : string read GetType write SetType;
property version : string read GetVersion write SetVersion;
property filename : string read l_filename;
property loaded : boolean read l_isloaded;
property dirty : boolean read l_isdirty write l_isdirty;
property root : TGFFStruct read l_rootstruct;
end;
implementation
// =============================================================================
// CLASS FUNCTIONS: TGFFFile
// =============================================================================
// -----------------------------------------------------------------------------
// CONSTRUCTOR - Initializes the GFF File Handler
// -----------------------------------------------------------------------------
constructor TGFFFile.Create();
var
i : integer;
begin
inherited Create();
for i := Low(l_filetype) to High(l_filetype) do
l_filetype[i] := #0;
for i := Low(l_fileversion) to High(l_fileversion) do
l_fileversion[i] := #0;
l_filename := '';
l_currfield := 0;
l_rootstruct := nil;
l_isloaded := False;
l_isdirty := False;
l_file := nil;
l_header := nil;
l_savedata := nil;
end;
// -----------------------------------------------------------------------------
// DESTRUCTOR - Destroys the GFF File Handler and all data it has loaded.
// -----------------------------------------------------------------------------
destructor TGFFFile.Destroy();
begin
ResetAll();
inherited Destroy();
end;
// -----------------------------------------------------------------------------
// Deletes all stored data, if any, and restores the GFF Handler to its
// starting state.
// -----------------------------------------------------------------------------
procedure TGFFFile.ResetAll();
var
i : integer;
begin
l_isloaded := False;
l_isdirty := False;
l_filename := '';
// Destroy the Root Struct and all GFF fields below it in the hierarchy
if (l_rootstruct <> nil) then begin
l_rootstruct.free();
l_rootstruct := nil;
end;
// Reset the file type
for i:= Low(l_filetype) to High(l_filetype) do
l_filetype[i] := #0;
// Reset the file version
for i := Low(l_fileversion) to High(l_fileversion) do
l_fileversion[i] := #0;
// If the FileStream hasn't been destroyed, do it.
if (l_file <> nil) then begin
l_file.free();
l_file := nil;
end;
// If the header exists, destroy it.
if (l_header <> nil) then begin
l_header.free();
l_header := nil;
end;
end;
//// FILE LOADING METHODS //////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// Creates a new blank GFF file of the specified type (DLG, GIT, UTC etc...).
// -----------------------------------------------------------------------------
procedure TGFFFile.NewFile(sType : string; sFilename : string = '');
begin
ResetAll();
l_filename := sFilename;
Self.filetype := sType;
Self.version := 'V3.2';
l_rootstruct := TGFFStruct.Create();
l_rootstruct.typeid := $FFFFFFFF;
l_isloaded := True;
l_isdirty := False;
end;
// -----------------------------------------------------------------------------
// Main function for loading a new GFF file into this object. The sFilename must
// contain the path and name to a GFF V3.2 format file to load.
// -----------------------------------------------------------------------------
procedure TGFFFile.LoadFile(sFilename : string);
begin
if not FileExists(sFilename) then
raise EGFFError.CreateHelp('Specified file ' + sFilename + ' could not be found to be opened!', 0);
ResetAll();
l_header := TGFF_Header.Create();
l_file := TFileStream.Create(sFilename, fmOpenRead or fmShareDenyWrite);
try
if (l_file = nil) then
raise EGFFError.CreateHelp('Unable to load Header information, no file has been opened!', 0);
// READ THE HEADER INFORMATION!
l_file.read(l_header.filetype, 4);
l_file.read(l_header.fileversion, 4);
// Format version mismatch, unable to continue.
if (l_header.fileversion <> 'V3.2') then begin
ResetAll();
raise EGFFError.CreateHelp('Invalid file version. Loaded file is not in GFF V3.2 format!', 1);
end;
l_filename := sFilename;
l_filetype := l_header.filetype;
l_fileversion := l_header.fileversion;
// Read in the Struct Array offset & count
l_file.Read(l_header.structoffset, sizeof(l_header.structoffset));
l_file.Read(l_header.structcount, sizeof(l_header.structcount));
// Read in the Field Array offset & count
l_file.Read(l_header.fieldoffset, sizeof(l_header.fieldoffset));
l_file.Read(l_header.fieldcount, sizeof(l_header.fieldcount));
// Read in the Label Array offset & count
l_file.Read(l_header.labeloffset, sizeof(l_header.labeloffset));
l_file.Read(l_header.labelcount, sizeof(l_header.labelcount));
// Read in the File Data Block offset & count
l_file.Read(l_header.fielddataoffset, sizeof(l_header.fielddataoffset));
l_file.Read(l_header.fielddatacount, sizeof(l_header.fielddatacount));
// Read in the Field Index Array offset & count
l_file.Read(l_header.fieldindexoffset, sizeof(l_header.fieldindexoffset));
l_file.Read(l_header.fieldindexcount, sizeof(l_header.fieldindexcount));
// Read in the List Index Array offset & count
l_file.Read(l_header.listindexoffset, sizeof(l_header.listindexoffset));
l_file.Read(l_header.listindexcount, sizeof(l_header.listindexcount));
// READ ALL THE GFF FIELDS INTO A TREE STRUCTURE UNDER THE ROOT STRUCT...
l_rootstruct := LoadFileStruct(l_header.structoffset);
// Everything should have been loaded if we've gotten this far...
l_isloaded := True;
finally
if (l_file <> nil) then
l_file.free();
if (l_header <> nil) then
l_header.free();
l_file := nil;
l_header := nil;
l_isdirty := False;
end;
end;
// -----------------------------------------------------------------------------
// Loads the data for a Complex data type from the file and stores it in a new
// TGFFField sub-object, which the function then returns. This function does
// not do anything with the STRUCT and LIST types.
// -----------------------------------------------------------------------------
function TGFFFile.LoadComplexField(iType : DWORD; iDataOrOffset : DWORD) : TGFFField;
var
i, n : integer;
iNext : integer;
iReadBuffer : integer;
iReadBlock : integer;
sBuffer : TBuffer;
iBuffer : array [0..BYTE_BUFFER_SIZE] of Byte;
oField : TGFFField;
oExoString : TGFF_CExoString;
oResref : TGFF_CResRef;
oExoLocString : TGFF_CExoLocString;
oSubString : TGFF_CSubString;
oVoid : TGFF_CVoid;
oOrientation : TGFF_COrientation;
oPosition : TGFF_CPosition;
begin
l_file.Seek(l_header.fielddataoffset + iDataOrOffset, soFromBeginning);
case (iType) of
FIELD_TYPE_DWORD64: begin
oField := TGFF_CDWORD64.Create();
l_file.read((oField as TGFF_CDWORD64).value, sizeof((oField as TGFF_CDWORD64).value));
end;
FIELD_TYPE_INT64: begin
oField := TGFF_CInt64.Create();
l_file.read((oField as TGFF_CInt64).value, sizeof((oField as TGFF_CInt64).value));
end;
FIELD_TYPE_DOUBLE: begin
oField := TGFF_CDouble.Create();
l_file.read((oField as TGFF_CDouble).value, sizeof((oField as TGFF_CDouble).value));
end;
FIELD_TYPE_CEXOSTRING: begin
oExoString := TGFF_CExoString.Create();
l_file.read(oExoString.size, sizeof(oExoString.size));
SetLength(oExoString.text, oExoString.size);
// REWRITE: Support ExoStrings of arbitrary length rather than have a fixed
// read buffer like before. Check that this works!
iNext := Low(oExoString.text);
iReadBuffer := oExoString.size;
while (iReadBuffer > 0) do begin
iReadBlock := iReadBuffer;
if (iReadBlock > TEXT_BUFFER_SIZE) then
iReadBlock := TEXT_BUFFER_SIZE;
l_file.read(sBuffer, iReadBlock);
for i := Low(sBuffer) to (iReadBlock-1) do begin
if (iNext > High(oExoString.text)) then
raise EGFFError.CreateHelp('Error loading CExoString, overflow when loading text!', 6);
oExoString.text[iNext] := sBuffer[i];
inc(iNext);
end;
iReadBuffer := iReadBuffer - iReadBlock;
end;
oField := oExoString;
end;
FIELD_TYPE_RESREF: begin
oResRef := TGFF_CResRef.Create();
l_file.read(oResRef.size, sizeof(oResRef.size));
// Error! Text is longer than the max 16 character length of a ResRef
if (oResRef.size > 16) then
raise EGFFError.CreateHelp('Error loading CResRef field, string is too long!', 8);
l_file.read(sBuffer, oResRef.size);
SetLength(oResRef.text, oResRef.size);
for i := Low(oResRef.text) to High(oResRef.text) do
oResRef.text[i] := sBuffer[i];
oField := oResRef;
end;
FIELD_TYPE_CEXOLOCSTRING: begin
oExoLocString := TGFF_CExoLocString.Create();
l_file.Read(oExoLocString.bytesize, sizeof(oExoLocString.bytesize));
l_file.Read(oExoLocString.strref, sizeof(oExoLocString.strref));
l_file.Read(oExoLocString.stringcount, sizeof(oExoLocString.stringcount));
if (oExoLocString.stringcount > 0) then begin
SetLength(oExoLocString.substrings, oExoLocString.stringcount);
for i := 0 to (oExoLocString.stringcount - 1) do begin
// This is UGLY... The ExoLocStr class should handle its substrings instead...
// ...some time when I feel like doing things the proper way... :)
oSubString := TGFF_CSubString.Create();
l_file.Read(oSubString.stringid, sizeof(oSubString.stringid));
l_file.Read(oSubString.stringlength, sizeof(oSubString.stringlength));
if (oSubString.stringlength > 0) then begin
SetLength(oSubString.text, oSubString.stringlength);
// REWRITE: Support Substrings of arbitrary length rather than have a fixed
// read buffer like before. Check that this works!
iNext := 0;
iReadBuffer := oSubString.stringlength;
while (iReadBuffer > 0) do begin
iReadBlock := iReadBuffer;
if (iReadBlock > TEXT_BUFFER_SIZE) then
iReadBlock := TEXT_BUFFER_SIZE;
l_file.read(sBuffer, iReadBlock);
for n := 0 to (iReadBlock-1) do begin
if (iNext > (oSubString.stringlength-1)) then
raise EGFFError.CreateHelp('Error loading CExoLocSubstring, overflow when loading text!', 10);
oSubString.text[iNext] := sBuffer[n];
inc(iNext);
end;
iReadBuffer := iReadBuffer - iReadBlock;
end;
end;
oExoLocString.substrings[i] := oSubString;
end;
end;
oField := oExoLocString;
end;
FIELD_TYPE_VOID: begin
oVoid := TGFF_CVoid.Create();
l_file.read(oVoid.bytesize, sizeof(oVoid.bytesize));
SetLength(oVoid.data, oVoid.bytesize);
// REWRITE: Support binary data of arbitrary length rather than have a fixed
// read buffer like before. Check that this works!
iNext := Low(oVoid.data);
iReadBuffer := oVoid.bytesize;
while (iReadBuffer > 0) do begin
iReadBlock := iReadBuffer;
if (iReadBlock > BYTE_BUFFER_SIZE) then
iReadBlock := BYTE_BUFFER_SIZE;
l_file.read(iBuffer, iReadBlock);
for i := Low(iBuffer) to (iReadBlock-1) do begin
if (iNext > High(oVoid.data)) then
raise EGFFError.CreateHelp('Error loading Binary/Void data, attempted read past end of data space.', 9);
oVoid.data[iNext] := iBuffer[i];
inc(iNext);
end;
iReadBuffer := iReadBuffer - iReadBlock;
end;
oField := oVoid;
end;
FIELD_TYPE_ORIENTATION: begin
// Added this, new undocumented KotOR field type...
oOrientation := TGFF_COrientation.Create();
for i := 0 to 3 do
l_file.read(oOrientation.value[i], 4);
oField := oOrientation;
end;
FIELD_TYPE_POSITION: begin
// Added this, new undocumented KotOR field type, looks like a Vector...
oPosition := TGFF_CPosition.Create();
for i := 0 to 2 do
l_file.read(oPosition.value[i], 4);
oField := oPosition;
end;
else
raise EGFFError.CreateHelp('Invalid field type encountered when reading field ' + IntToStr(iType) + ' data!', 5);
end;
Result := oField;
end;
// -----------------------------------------------------------------------------
// Loads the data for a GFF Field from the file and stores it in a new TGFFField
// object, which the function then returns.
// -----------------------------------------------------------------------------
function TGFFFile.LoadFileField(iOffset : DWORD) : TGFFField;
var
iType : DWORD;
iLabelIndex : DWORD;
iDataOrOffset : DWORD;
oField : TGFFField;
oList : TGFFList;
sLabel : T16Char;
iFieldOffset : DWORD;
iListCount : DWORD;
iStructIndex : DWORD;
i : integer;
begin
oField := nil;
l_file.Seek(iOffset, soFromBeginning);
l_file.read(iType, sizeof(iType));
l_file.read(iLabelIndex, sizeof(iLabelIndex));
// Read the field data depending on datatype.
case (iType) of
FIELD_TYPE_BYTE: begin
oField := TGFF_SByte.Create();
l_file.read((oField as TGFF_SByte).value, sizeof((oField as TGFF_SByte).value));
end;
FIELD_TYPE_CHAR: begin
oField := TGFF_SChar.Create();
l_file.read((oField as TGFF_SChar).value, sizeof((oField as TGFF_SChar).value));
end;
FIELD_TYPE_WORD: begin
oField := TGFF_SWord.Create();
l_file.read((oField as TGFF_SWord).value, sizeof((oField as TGFF_SWord).value));
end;
FIELD_TYPE_SHORT: begin
oField := TGFF_SShort.Create();
l_file.read((oField as TGFF_SShort).value, sizeof((oField as TGFF_SShort).value));
end;
FIELD_TYPE_DWORD: begin
oField := TGFF_SDWORD.Create();
l_file.read((oField as TGFF_SDWORD).value, sizeof((oField as TGFF_SDWORD).value));
end;
FIELD_TYPE_INT: begin
oField := TGFF_SInt.Create();
l_file.read((oField as TGFF_SInt).value, sizeof((oField as TGFF_SInt).value));
end;
FIELD_TYPE_FLOAT: begin
oField := TGFF_SFloat.Create();
l_file.read((oField as TGFF_SFloat).value, sizeof((oField as TGFF_SFloat).value));
end;
FIELD_TYPE_DWORD64, FIELD_TYPE_INT64, FIELD_TYPE_DOUBLE..FIELD_TYPE_VOID, FIELD_TYPE_ORIENTATION, FIELD_TYPE_POSITION: begin
l_file.read(iDataOrOffset, sizeof(iDataOrOffset));
oField := LoadComplexField(iType, iDataOrOffset);
end;
FIELD_TYPE_STRUCT: begin
l_file.read(iDataOrOffset, sizeof(iDataOrOffset));
iFieldOffset := l_header.structoffset + (iDataOrOffset * 12);
oField := LoadFileStruct(iFieldOffset);
end;
FIELD_TYPE_LIST: begin
l_file.read(iDataOrOffset, sizeof(iDataOrOffset));
l_file.Seek(l_header.listindexoffset + iDataOrOffset, soFromBeginning);
l_file.read(iListCount, sizeof(iListCount));
oList := TGFFList.Create();
for i := 1 to iListCount do begin
iFieldOffset := l_header.listindexoffset + iDataOrOffset + (DWORD(i) * 4);
l_file.Seek(iFieldOffset, soFromBeginning);
l_file.read(iStructIndex, sizeof(iStructIndex));
oField := LoadFileStruct(l_header.structoffset + (iStructIndex * 12));
oList.AddStruct((oField as TGFFStruct));
end;
oField := oList;
end;
end;
// Get and store the field label...
if (oField <> nil) then begin
l_file.Seek(l_header.labeloffset + (iLabelIndex * 16), soFromBeginning);
l_file.read(sLabel, sizeof(sLabel));
oField.fieldlabelraw := sLabel;
end;
oField.fieldtype := iType;
Result := oField;
end;
// -----------------------------------------------------------------------------
// Loads a STRUCT data type from the file, creates a new TGFFStruct object to
// store it in, retrieves any fields it contains and stores them in it. This
// struct object is then returned by the function.
// -----------------------------------------------------------------------------
function TGFFFile.LoadFileStruct(iOffset : DWORD) : TGFFStruct;
var
oStruct : TGFFStruct;
oField : TGFFField;
iType : DWORD;
iDataOrOffset : DWORD;
iFieldCount : DWORD;
iFieldOffset : DWORD;
iFieldIndex : DWORD;
i : integer;
begin
l_file.Seek(iOffset, soFromBeginning);
l_file.read(iType, sizeof(iType));
l_file.read(iDataOrOffset, sizeof(iDataOrOffset));
l_file.read(iFieldCount, sizeof(iFieldCount));
// Create the new STRUCT object...
oStruct := TGFFStruct.Create();
oStruct.typeid := iType;
// Struct only has one field. Fetch it directly from the FieldArray
if (iFieldCount = 1) then begin
iFieldOffset := l_header.fieldoffset + (iDataOrOffset * 12);
oField := LoadFileField(iFieldOffset);
oStruct.AddField(oField);
end
// Struct has many fields. Get their indexes from the FieldIndexarray and look them up.
else if (iFieldCount > 1) then begin
for i := 0 to (iFieldCount-1) do begin
// Fetch a FieldArray index from the FieldIndexArray
iFieldOffset := l_header.fieldindexoffset + iDataOrOffset + (DWORD(i) * 4);
l_file.Seek(iFieldOffset, soFromBeginning);
l_file.read(iFieldIndex, sizeof(iFieldIndex));
// Look up the retrieved index in the FieldArray
iFieldOffset := l_header.fieldoffset + (iFieldIndex * 12);
oField := LoadFileField(iFieldOffset);
oStruct.AddField(oField);
end;
end;
// NOTE: A STRUCT without fields is a valid condition. It will be setup properly
// by the TGFFStruct constructor for that case, so just return the "empty" object.