-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebugUTLKFile.pas
More file actions
1164 lines (943 loc) · 41.5 KB
/
debugUTLKFile.pas
File metadata and controls
1164 lines (943 loc) · 41.5 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 UTLKFile;
interface
uses SysUtils, Windows, Classes;
// STRING FLAG CONSTANTS
const TEXT_PRESENT = $0001;
const SND_PRESENT = $0002;
const SNDLENGTH_PRESENT = $0004;
type
// CUSTOM TYPE DEFINITIONS
float = single;
TResRef = array[0..15] of Char;
T4Char = array[0..3] of Char;
// EXCEPTION FOR PASSING ERROR MESSAGES
EHell = Class(Exception);
// TTLKString ==================================================================
TTLKString = Class(TObject)
private
Flags : dword;
SoundResref : TResRef;
VolumeVariance : dword;
PitchVariance : dword;
OffsetToString : dword;
StringSize : dword;
SoundLength : float;
StringStrRef : dword;
StringEntry : string;
CustomEntry : boolean;
public
function GetFlags() : dword;
procedure SetFlags(nFlags : dword);
function GetSound() : TResRef;
procedure SetSound(resref : TResRef);
function GetVolume() : dword;
procedure SetVolume(nVolume : dword);
function GetPitch() : dword;
procedure SetPitch(nPitch : dword);
function GetOffset() : dword;
procedure SetOffset(nOffset : dword);
function GetSize() : dword;
procedure SetSize(nSize : dword);
function GetSndLength() : float;
procedure SetSndLength(fLength : float);
function GetText() : string;
procedure SetText(sText : string);
function GetStrRef() : dword;
procedure SetStrRef(iStrRef : dword);
function GetCustom() : boolean;
procedure SetCustom(bCustom : boolean);
function GetSoundString() : string;
procedure Clone(oClone : TTLKString);
constructor Create(); overload;
constructor Create(oClone : TTLKString); overload;
destructor Destroy(); override;
property strflags : dword read GetFlags write SetFlags;
property strsound : TResRef read GetSound write SetSound;
property sndvolume : dword read GetVolume write SetVolume;
property sndpitch : dword read GetPitch write SetPitch;
property stroffset : dword read GetOffset write SetOffset;
property strsize : dword read GetSize write SetSize;
property sndlength : float read GetSndLength write SetSndLength;
property strtext : string read GetText write SetText;
property strref : dword read GetStrRef write SetStrRef;
property iscustom : boolean read GetCustom write SetCustom;
property soundstring : string read GetSoundString;
end;
// TStringData =================================================================
TStringData = Class(TObject)
private
next : TStringData;
prev : TStringData;
data : TTLKString;
public
function GetData() : TTLKString;
procedure SetData(oData : TTLKString);
function GetNext() : TStringData;
procedure SetNext(oData : TStringData);
function GetPrev() : TStringData;
procedure SetPrev(oData : TStringData);
constructor Create();
destructor Destroy(); override;
property stringdata : TTLKString read GetData write SetData;
property listnext : TStringData read GetNext write SetNext;
property listprev : TStringData read GetPrev write SetPrev;
end;
// TStringDataList =============================================================
TStringDataList = Class(TObject)
private
p_oFirst : TStringData;
p_oLast : TStringData;
p_oCurrent : TStringData;
p_iCount : integer;
public
function first() : TTLKString;
function next() : TTLKString;
function current() : TTLKString;
function isempty(): boolean;
function eol(): boolean;
procedure Insert(oData : TTLKString);
procedure Delete(bDelete : boolean = false);
constructor Create;
destructor Destroy; override;
end;
// TTLKFileHandler ============================================================
TTLKFileHandler = Class(TObject)
private
// Header information
FileType : T4Char;
FileVersion : T4Char;
LanguageId : dword;
StringCount : dword;
StringEntriesOffset : dword;
// The string entries
StringList : TStringDataList;
l_filename : string; // ADDED(2005-09-15)
// Keep track of if data has been written properly.
bDirty : boolean;
bFileOpen : boolean;
l_modified : boolean; // ADDED(2005-09-15)
public
property strings : TStringDataList read StringList;
property count : dword read StringCount;
property fileid : T4Char read FileType;
property version : T4Char read FileVersion;
property fileexists : boolean read bFileOpen;
property language : dword read LanguageId write LanguageId;
property filename : string read l_filename;
property modified : boolean read l_modified;
procedure AddEntry(oEntry : TTLKString);
procedure ReplaceEntry(oEntry : TTLKString);
procedure DeleteEntry(iStrRef : DWORD);
procedure NewTlkFile();
procedure LoadTlkFile(sFilename : string);
procedure SaveTlkFile(sFilename : string = '');
procedure Reset();
constructor Create(); overload;
constructor Create(sFilename : string); overload;
destructor Destroy(); override;
end;
var
fileDebug : TextFile;
procedure DBP(sText : string);
implementation
uses Forms;
procedure DBP(sText : string);
begin
Writeln(fileDebug, sText);
Flush(fileDebug);
end;
// TSTRINGDATA =================================================================
// Class defining an individual node in the String Entry linked list.
// Keeps track of the previous and next nodes in the list in relation to it.
// =============================================================================
// -----------------------------------------------------------------------------
// Constructor - create a new node for the string data list.
// -----------------------------------------------------------------------------
constructor TStringData.Create();
begin
inherited Create;
end;
// -----------------------------------------------------------------------------
// Destructor - Destroy this node.
// -----------------------------------------------------------------------------
destructor TStringData.Destroy();
begin
inherited Destroy;
// ST: Remember - don't destroy the data here. Just because you want to
// remove it from the list doesn't mean you no longer want the data.
end;
// -----------------------------------------------------------------------------
// Data control function - store data in this node.
// -----------------------------------------------------------------------------
procedure TStringData.SetData(oData : TTLKString);
begin
data := oData;
end;
// -----------------------------------------------------------------------------
// Data control function - get the data stored in this node.
// -----------------------------------------------------------------------------
function TStringData.GetData() : TTLKString;
begin
Result := data;
end;
// -----------------------------------------------------------------------------
// Data control function - return the next node in the linked list related
// to this node.
// -----------------------------------------------------------------------------
function TStringData.GetNext() : TStringData;
begin
result := next;
end;
// -----------------------------------------------------------------------------
// Data control function - Set the next node in the linked list related to
// this node.
// -----------------------------------------------------------------------------
procedure TStringData.SetNext(oData : TStringData);
begin
next := oData;
end;
// -----------------------------------------------------------------------------
// Data control function - return the previous node in the linked list related
// to this node.
// -----------------------------------------------------------------------------
function TStringData.GetPrev() : TStringData;
begin
result := prev;
end;
// -----------------------------------------------------------------------------
// Data control function - Set the previous node in the linked list related to
// this node.
// -----------------------------------------------------------------------------
procedure TStringData.SetPrev(oData : TStringData);
begin
prev := oData;
end;
// TSTRINGDATALIST =============================================================
// Linked list containing all of the string entries from the dialog.tlk file.
// =============================================================================
// -----------------------------------------------------------------------------
// Constructor - initialize a new linked list.
// -----------------------------------------------------------------------------
constructor TStringDataList.Create();
begin
inherited Create;
p_iCount := 0;
p_oFirst := nil;
end;
// -----------------------------------------------------------------------------
// Destructor - Destroy the list and all content kept within it.
// -----------------------------------------------------------------------------
destructor TStringDataList.Destroy();
var
oTmp : TStringData;
oTmpData : TTLKString;
begin
// Destroy all nodes in the linked list before destroying the
// list object itself.
p_oCurrent := p_oFirst;
while (p_oCurrent <> nil) do
begin
oTmp := p_oCurrent;
oTmpData := p_oCurrent.stringdata;
p_oCurrent := p_oCurrent.listnext;
oTmpData.free();
oTmp.free();
p_iCount := p_iCount - 1; // TODO: Throw exception if this ends up
// bigger than 0 at the end of the loop?
end;
inherited Destroy;
end;
// -----------------------------------------------------------------------------
// Move the list pointer to the first position and return the object stored
// there, if any.
// -----------------------------------------------------------------------------
function TStringDataList.first() : TTLKString;
begin
p_oCurrent := p_oFirst;
if (p_oFirst <> nil) then
result := p_oCurrent.stringdata
else
result := nil;
end;
// -----------------------------------------------------------------------------
// MReturn the object stored at the current list position, if any.
// -----------------------------------------------------------------------------
function TStringDataList.current() : TTLKString;
begin
if (p_oCurrent <> nil) then
result := p_oCurrent.stringdata
else
result := nil;
end;
// -----------------------------------------------------------------------------
// Move the list pointer to the next position and return the object stored
// there, if any.
// -----------------------------------------------------------------------------
function TStringDataList.next() : TTLKString;
begin
if ((p_oCurrent <> p_oLast) and (p_oCurrent.listnext <> nil)) then
begin
p_oCurrent := p_oCurrent.listnext;
result := p_oCurrent.stringdata;
end
else begin
p_oCurrent := nil; // FIX?(2005-09-15) Added this...
result := nil;
end;
end;
// -----------------------------------------------------------------------------
// Returns TRUE if the list is empty and FALSE if it is not.
// -----------------------------------------------------------------------------
function TStringDataList.isempty(): boolean;
begin
if (p_oFirst = nil) then
result := true
else
result := false;
end;
// -----------------------------------------------------------------------------
// Returns TRUE if the list pointer has reached the end of the list.
// -----------------------------------------------------------------------------
function TStringDataList.eol(): boolean;
begin
if (p_oCurrent = nil) then
result := true
else
result := false;
end;
// -----------------------------------------------------------------------------
// Inserts a new entry at the end of the linked list.
// -----------------------------------------------------------------------------
procedure TStringDataList.Insert(oData : TTLKString);
var
oTmp : TStringData;
begin
oTmp := TStringData.Create();
oTmp.stringdata := oData;
// ST: List is empty, insert at first position
if (p_oFirst = nil) then
begin
p_oFirst := oTmp;
p_oFirst.listnext := nil;
p_oFirst.listprev := nil;
p_oCurrent := p_oFirst;
p_oLast := p_oFirst;
end
else
begin
// ST: Insert at the end of the list...
p_oLast.listnext := oTmp;
oTmp.listnext := nil;
oTmp.listprev := p_oLast;
p_oLast := oTmp;
p_oCurrent := p_oLast;
end;
p_iCount := p_iCount + 1;
end;
// -----------------------------------------------------------------------------
// Delete an entry from the linked list. Be VERY cautious about using this since
// Dialog TLK is indexed by position. Removing one entry that's not at the end
// of the list will mess up the indexing for all following entries.
// The list pointer will be set to the next entry in the list following the
// currently deleted one.
// If bDelete is TRUE the data contained within the list entry will be deleted
// as well. If FALSE, only the list entry will be deleted. Make sure the data
// is referenced elsewhere before using this.
// -----------------------------------------------------------------------------
procedure TStringDataList.Delete(bDelete : boolean);
var
oPrev : TStringData;
oNext : TStringData;
oData : TTLKString;
begin
oPrev := p_oCurrent.listprev;
oNext := p_oCurrent.listnext;
if (oNext <> nil) then
oNext.listprev := oPrev;
if (oPrev <> nil) then
oPrev.listnext := oNext;
// FIX(2005-09-15) Set the First pointer to next object if first object is deleted.
if (p_oCurrent = p_oFirst) then
p_oFirst := oNext;
// FIX(2005-09-15) Set the Last pointer to previous object if first object is deleted.
if (p_oCurrent = p_oLast) then
p_oLast := oPrev;
if (bDelete) then
begin
oData := p_oCurrent.stringdata;
oData.free();
end;
p_oCurrent.free();
// FIX(2005-09-15) Decrement the list counter as an entry has been deleted.
p_iCount := p_iCount - 1;
p_oCurrent := oNext;
end;
// TTLKSTRING ==================================================================
// A data holding class that stores the data belonging to a dialog.tlk entry.
// =============================================================================
// -----------------------------------------------------------------------------
// Standard constructor, create a new talkstring object.
// -----------------------------------------------------------------------------
constructor TTLKString.Create();
begin
inherited Create;
end;
// -----------------------------------------------------------------------------
// Copy Constructor - create a copy of an existing talkstring object.
// -----------------------------------------------------------------------------
constructor TTLKString.Create(oClone : TTLKString);
begin
inherited Create();
Flags := oClone.strflags;
SoundResref := oClone.strsound;
VolumeVariance := oClone.sndvolume;
PitchVariance := oClone.sndpitch;
OffsetToString := oClone.stroffset;
StringSize := oClone.strsize;
SoundLength := oClone.sndlength;
StringStrRef := oClone.strref;
StringEntry := oClone.strtext;
CustomEntry := oClone.iscustom;
end;
// -----------------------------------------------------------------------------
// Destructor - destroy this talkstring object.
// -----------------------------------------------------------------------------
destructor TTLKString.Destroy();
begin
inherited Destroy();
end;
// -----------------------------------------------------------------------------
// Make this object hold data identical to that of the specified object.
// -----------------------------------------------------------------------------
procedure TTLKString.Clone(oClone : TTLKString);
begin
Flags := oClone.strflags;
SoundResref := oClone.strsound;
VolumeVariance := oClone.sndvolume;
PitchVariance := oClone.sndpitch;
OffsetToString := oClone.stroffset;
StringSize := oClone.strsize;
SoundLength := oClone.sndlength;
StringStrRef := oClone.strref;
StringEntry := oClone.strtext;
CustomEntry := oClone.iscustom;
end;
// -----------------------------------------------------------------------------
// Get any bitfield flags set for this string, use bit-and with above constants
// to check if a specific flag is set..
// -----------------------------------------------------------------------------
function TTLKString.GetFlags() : dword;
begin
result := Flags;
end;
// -----------------------------------------------------------------------------
// Set bitfield flags for this string, use bit-or to set multiple flags.
// -----------------------------------------------------------------------------
procedure TTLKString.SetFlags(nFlags : dword);
begin
Flags := nFlags;
end;
// -----------------------------------------------------------------------------
// Get the StrRef for the sound associated with this string, if any.
// -----------------------------------------------------------------------------
function TTLKString.GetSound() : TResRef;
begin
result := SoundResref;
end;
// -----------------------------------------------------------------------------
// Set the StrRef of the sound associated with this string.
// -----------------------------------------------------------------------------
procedure TTLKString.SetSound(resref : TResRef);
begin
SoundResref := resref;
end;
// -----------------------------------------------------------------------------
// Get the volume variance for the sound associated with this string.
// -----------------------------------------------------------------------------
function TTLKString.GetVolume() : dword;
begin
result := VolumeVariance;
end;
// -----------------------------------------------------------------------------
// Set the volume variance for the sound associated with this string.
// -----------------------------------------------------------------------------
procedure TTLKString.SetVolume(nVolume : dword);
begin
VolumeVariance := nVolume;
end;
// -----------------------------------------------------------------------------
// Get the sound pitch variance for the sound associated with this string.
// -----------------------------------------------------------------------------
function TTLKString.GetPitch() : dword;
begin
result := PitchVariance;
end;
// -----------------------------------------------------------------------------
// Set the sound pitch variance for the sound associated with this string.
// -----------------------------------------------------------------------------
procedure TTLKString.SetPitch(nPitch : dword);
begin
PitchVariance := nPitch;
end;
// -----------------------------------------------------------------------------
// Get the offset for the string within the current dialog.tlk file.
// Don't write this when rebuilding a tlk file, it's only used for reading.
// -----------------------------------------------------------------------------
function TTLKString.GetOffset() : dword;
begin
result := OffsetToString;
end;
// -----------------------------------------------------------------------------
// Set the offset for the string within the current dialog.tlk file.
// -----------------------------------------------------------------------------
procedure TTLKString.SetOffset(nOffset : dword);
begin
OffsetToString := nOffset;
end;
// -----------------------------------------------------------------------------
// Get the length of the current string, to be read from the offset. This is
// only used for reading dialog.tlk, it's probably better to re-calculate string
// length from what we have when writing.
// -----------------------------------------------------------------------------
function TTLKString.GetSize() : dword;
begin
result := StringSize;
end;
// -----------------------------------------------------------------------------
// Get the size of the string in dialog.tlk. Used with offset for reading in
// data from an existing dialog.tlk file.
// -----------------------------------------------------------------------------
procedure TTLKString.SetSize(nSize : dword);
begin
StringSize := nSize;
end;
// -----------------------------------------------------------------------------
// Get the duration of the sound associated with this string.
// -----------------------------------------------------------------------------
function TTLKString.GetSndLength() : float;
begin
result := SoundLength;
end;
// -----------------------------------------------------------------------------
// Set the duration of the sound associated with this string.
// -----------------------------------------------------------------------------
procedure TTLKString.SetSndLength(fLength : float);
begin
SoundLength := fLength;
end;
// -----------------------------------------------------------------------------
// Get the text string for this entry.
// -----------------------------------------------------------------------------
function TTLKString.GetText() : string;
begin
result := StringEntry;
end;
// -----------------------------------------------------------------------------
// Set the text string for this entry.
// -----------------------------------------------------------------------------
procedure TTLKString.SetText(sText : string);
begin
// IMPORTANT!!!! MAKE SURE THAT THE LENGTH OF THE NEW STRING IS SET WITH
// THE SETSIZE PROPERTY AS WELL WHEN THIS IS MODIFIED
// MANUALLY! CAN'T DO IT HERE SINCE IT'D OVERRIDE THE VALUE
// READ FROM FILE WHEN LOADING A TLK FILE!
StringEntry := sText;
// StringSize := length(StringEntry);
end;
// -----------------------------------------------------------------------------
// Get the Dialog.tlk index for this particular string.
// -----------------------------------------------------------------------------
function TTLKString.GetStrRef() : dword;
begin
result := StringStrRef;
end;
// -----------------------------------------------------------------------------
// Store the Dialog.tlk index for this particular string.
// -----------------------------------------------------------------------------
procedure TTLKString.SetStrRef(iStrRef : dword);
begin
StringStrRef := iStrRef;
end;
// -----------------------------------------------------------------------------
// Get the status of the Custom flag. This is set for entries that have not been
// read from dialog.tlk but added by this program. This should not be written.
// -----------------------------------------------------------------------------
function TTLKString.GetCustom() : boolean;
begin
result := CustomEntry;
end;
// -----------------------------------------------------------------------------
// Set the custom flag for this entry. This should be set for new entries that
// have not been read from a dialog.tlk file.
// -----------------------------------------------------------------------------
procedure TTLKString.SetCustom(bCustom : boolean);
begin
CustomEntry := bCustom;
end;
function TTLKString.GetSoundString() : string;
var
sOut : string;
i : integer;
begin
sOut := '';
for i := Low(SoundResref) to High(SoundResref) do begin
if (SoundResref[i] <> #0) then
sOut := sOut + SoundResref[i];
end;
result := sOut;
end;
// TTLKFILEHANDLER =============================================================
// Class that handles reading in data from a dialog.tlk file and writing out
// data to a new dialog.tlk file.
// =============================================================================
// -----------------------------------------------------------------------------
// Standard constructor, create a new TLK file handler.
// -----------------------------------------------------------------------------
constructor TTLKFileHandler.Create();
begin
inherited Create();
Reset();
end;
// -----------------------------------------------------------------------------
// Shortcut constructor, create a new TLK file handler and load the TLK file
// specified as a parameter.
// -----------------------------------------------------------------------------
constructor TTLKFileHandler.Create(sFilename : string);
begin
inherited Create();
// FIX(2005-09-15) Removed Reset call since LoadTlkFile() does this anyway.
LoadTlkFile(sFileName);
end;
// -----------------------------------------------------------------------------
// Destructor, destroy the TLK file handler.
// -----------------------------------------------------------------------------
destructor TTLKFileHandler.Destroy();
begin
StringList.free();
inherited Destroy();
end;
// -----------------------------------------------------------------------------
// Add a new entry to the end of the stringlist.
// -----------------------------------------------------------------------------
procedure TTLKFileHandler.AddEntry(oEntry : TTLKString);
begin
if (bFileOpen = False) then
raise EHell.Create('Unable to add new entry. No TLK file is open!');
// ADD a new entry AT THE END of the TLK data...
// CHECK THIS CAREFULLY IF LENGTH() CAN BE USED TO GET THE PROPER LENGTH
// OF THE STRING THAT IS NEEDED FOR STRINGSIZE.
oEntry.strsize := Length(oEntry.strtext);
oEntry.iscustom := True;
oEntry.strref := StringCount;
StringList.Insert(oEntry);
StringCount := StringCount + 1;
l_modified := True;
end;
// -----------------------------------------------------------------------------
// ADDED(2005-09-15) Delete an entry from the stringlist.
// -----------------------------------------------------------------------------
procedure TTLKFileHandler.DeleteEntry(iStrRef : DWORD);
var
oCheck : TTLKString;
bFound : boolean;
iCount : DWORD;
begin
if (bFileOpen = False) then
raise EHell.Create('Unable to add new entry. No TLK file is open!');
if (Self.strings.isempty()) then
raise EHell.Create('Unable to delete selected entry. The entry list is already empty!');
if (iStrRef >= StringCount) then
raise EHell.Create('Invalid StrRef of entry to delete! Specified value exceeds the end of the entry list!');
iCount := 0;
bFound := False;
oCheck := Self.strings.first();
while (oCheck <> nil) and not self.strings.eol() do begin
// If an entry has been deleted, re-index the StrRefs of all following entries.
if bFound then begin
oCheck.strref := iCount;
inc(iCount);
oCheck := Self.strings.next();
end
// Found an entry with a matching StrRef. Since strRefs should be unique, delete it.
else if (oCheck.strref = iStrRef) and (oCheck.strref = iCount) then begin
Self.strings.delete(True);
oCheck := Self.strings.current();
StringCount := StringCount - 1;
l_modified := True;
bFound := True;
end
else begin
inc(iCount);
oCheck := Self.strings.next();
end;
end;
end;
// -----------------------------------------------------------------------------
// REPLACE the entry with the specified STRREF with the provided entry.
// Just a wrapper setting some fields in the entry...
// -----------------------------------------------------------------------------
procedure TTLKFileHandler.ReplaceEntry(oEntry : TTLKString);
begin
if (bFileOpen = False) then
raise EHell.Create('Unable to modify entry. No TLK file is currently open!');
oEntry.iscustom := true;
oEntry.strsize := Length(oEntry.strtext);
l_modified := True;
end;
// -----------------------------------------------------------------------------
// Initialize the handler as if a blank TLK file has been loaded.
// -----------------------------------------------------------------------------
procedure TTLKFileHandler.NewTlkFile();
begin
Reset();
bFileOpen := True;
FileType := 'TLK ';
FileVersion := 'V3.0';
l_filename := 'untitled.tlk';
l_modified := True;
end;
// -----------------------------------------------------------------------------
// [?] Load the TLK file specified by the parameter.
// -----------------------------------------------------------------------------
procedure TTLKFileHandler.LoadTlkFile(sFilename : string);
var
inFile : TFileStream;
oData : TTLKString;
iCount : integer;
iIdx : integer;
nTemp : dword;
tmpfloat : float;
tmpResref : TResRef;
stringBuf : array[0..4095] of Char;
nOffset : dword;
begin
DBP('LoadTlkFile - START');
DBP('LoadTlkFile - Resetting object attributes...');
Reset();
DBP('LoadTlkFile - Object attributes reset.');
// ADDED(2005-09-15) Added check if specified file exists before trying to open it.
if not Sysutils.FileExists(sFilename) then
raise EHell.Create('Unable to load specified TLK file since it could not be found!');
l_filename := sFilename;
DBP('LoadTlkFile - Loading file ' + l_filename);
inFile := TFileStream.Create(sFilename, fmOpenRead or fmShareDenyWrite);
try
DBP('LoadTlkFile - FileStream opened in Read mode...');
// Read in file header information...
DBP('LoadTlkFile - Reading Type and Version from header....');
inFile.Read(FileType, sizeof(FileType));
inFile.Read(FileVersion, sizeof(FileVersion));
DBP('LoadTlkFile - Type and Version read from header.');
if not (FileType = 'TLK ') then
raise EHell.Create('Type mismatch. Specified file is not a valid TLK file!');
if not (FileVersion = 'V3.0') then
raise EHell.Create('Version mismatch. File is not a valid v3.0 TLK file!');
// Read the rest of the TLK header info...
DBP('LoadTlkFile - Reading rest of header data...');
inFile.Read(LanguageId, sizeof(LanguageId));
inFile.Read(StringCount, sizeof(StringCount));
inFile.Read(StringEntriesOffset, sizeof(StringEntriesOffset));
DBP('LoadTlkFile - Header data loaded.');
if (StringCount < 1) then
raise EHell.Create('No entries found in the specified TLK file!');
// Read in all the entries in the String Data Table...
DBP('LoadTlkFile - Starting loop to read entries in String Data Table...');
for iCount := 0 to (StringCount-1) do
begin
if (longword(inFile.Position) >= StringEntriesOffset) then
raise EHell.Create('Error reading string data table. Overflow into entry data table!');
// Read a string data entry and store in an object...
DBP('LoadTlkFile - Creating new Entry object (' + IntToStr(iCount) + ')...');
oData := TTLKString.Create();
//DBP('LoadTlkFile - Reading Str Flags');
inFile.Read(nTemp, sizeof(nTemp));
oData.strflags := nTemp;
//DBP('LoadTlkFile - Reading Sound StrRef...');
inFile.Read(tmpResref, sizeof(tmpResref));
oData.strsound := tmpResref;
//DBP('LoadTlkFile - Reading Sound Volume...');
inFile.Read(nTemp, sizeof(nTemp));
oData.sndvolume := nTemp;
//DBP('LoadTlkFile - Reading Sound Pitch...');
inFile.Read(nTemp, sizeof(nTemp));
oData.sndpitch := nTemp;
//DBP('LoadTlkFile - Reading offset to string data...');
inFile.Read(nTemp, sizeof(nTemp));
oData.stroffset := nTemp;
//DBP('LoadTlkFile - Reading string length...');
inFile.Read(nTemp, sizeof(nTemp));
oData.strsize := nTemp;
//DBP('LoadTlkFile - Reading sound length...');
inFile.Read(tmpfloat, sizeof(tmpfloat));
oData.sndlength := tmpfloat;
//DBP('LoadTlkFile - Setting entry StrRef...');
oData.strref := iCount;
// Clear the buffer of any leftover characters....
// FIX(2006-10-02) Use FillChar instead of doing it manually, it's much faster.
//DBP('LoadTlkFile - Clearing string buffer...');
FillChar(stringBuf, 4096, #0);
//for iIdx := 0 to 4095 do
// stringBuf[iIdx] := #0;
// Read the text string from the entry table...
DBP('LoadTlkFile - Reading text string...');
nOffset := longword(inFile.Position);
inFile.Seek(StringEntriesOffset + oData.stroffset, soFromBeginning);
inFile.Read(stringBuf, oData.strsize);
oData.strtext := stringBuf;
inFile.Seek(nOffset, soFromBeginning);
DBP('LoadTlkFile - Adding new entry to the List...');
StringList.Insert(oData);
end;
DBP('LoadTlkFile - End of loop loading string data table...');
bFileOpen := True;
l_modified := false;
finally
DBP('LoadTlkFile - Closing FileStream...');
inFile.free();
end;
DBP('LoadTlkFile - END.');
end;
// -----------------------------------------------------------------------------
// [?] Save a new TLK file with the name and path specified by the parameter.
// -----------------------------------------------------------------------------
procedure TTLKFileHandler.SaveTlkFile(sFilename : string = '');
var
stringBuf : array[0..4095] of Char;
outFile : TFileStream;
oData : TTLKString;
iOffsetEntry : dword;
iTmpOffset : dword;
iCount : integer;
iIdx : integer;
nTmpInt : dword;
sTmpResRef : TResRef;
fTmpFloat : float;