forked from TES5Edit/TES5Edit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wbLocalization.pas
646 lines (561 loc) · 17 KB
/
wbLocalization.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
{*******************************************************************************
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
*******************************************************************************}
unit wbLocalization;
{$I wbDefines.inc}
interface
uses
Classes, SysUtils, StrUtils, Math,
wbInterface, wbBSA;
const
sStringID = 'STRINGID:';
type
TwbLStringType = (
lsDLString,
lsILString,
lsString
);
TwbLocalizationFile = class
private
fName : string;
fFileName : string;
fFileType : TwbLStringType;
fStrings : TStrings;
fModified : boolean;
fNextID : Cardinal;
procedure Init;
function FileStringType(aFileName: string): TwbLStringType;
function ReadZString(aStream: TMemoryStream): AnsiString;
function ReadLenZString(aStream: TMemoryStream): AnsiString;
procedure WriteZString(aStream: TMemoryStream; aString: AnsiString);
procedure WriteLenZString(aStream: TMemoryStream; aString: AnsiString);
procedure ReadDirectory(aStream: TMemoryStream);
protected
function Get(Index: Cardinal): string;
procedure Put(Index: Cardinal; const S: string);
public
property Strings[Index: Cardinal]: string read Get write Put; default;
property Items: TStrings read fStrings;
property Name: string read fName;
property FileName: string read fFileName;
property Modified: boolean read fModified write fModified;
property NextID: Cardinal read fNextID;
constructor Create(const aFileName: string); overload;
constructor Create(const aFileName: string; aData: TBytes); overload;
destructor Destroy; override;
function Count: Integer;
function IndexToID(Index: Integer): Cardinal;
function IDExists(ID: Cardinal): boolean;
function AddString(ID: Integer; const S: string): boolean;
procedure WriteToStream(const aStream: TStream);
procedure ExportToFile(const aFileName: string);
end;
TwbLocalizationHandler = class
private
lFiles : TStrings;
fReuseDup : boolean;
protected
function Get(Index: Integer): TwbLocalizationFile;
function GetStringsPath: string;
public
NoTranslate: boolean;
property _Files[Index: Integer]: TwbLocalizationFile read Get; default;
property StringsPath: string read GetStringsPath;
property ReuseDup: Boolean read fReuseDup write fReuseDup;
constructor Create;
destructor Destroy; override;
procedure Clear;
function Count: Integer;
function LocalizedValueDecider(aElement: IwbElement): TwbLStringType;
function AvailableLanguages: TStringList;
function AvailableLocalizationFiles: TStringList;
procedure LoadForFile(aFileName: string);
function AddLocalization(const aFileName: string): TwbLocalizationFile; overload;
function AddLocalization(const aFileName: string; aData: TBytes): TwbLocalizationFile; overload;
function GetValue(ID: Cardinal; aElement: IwbElement): string;
function SetValue(ID: Cardinal; aElement: IwbElement; aValue: string): Cardinal;
function AddValue(aValue: string; aElement: IwbElement): Cardinal;
function GetLocalizationFileNameByElement(aElement: IwbElement): string;
function GetLocalizationFileNameByType(aPluginFile: string; ls: TwbLStringType): string;
procedure GetStringsFromFile(aFileName: string; const aList: TStrings);
end;
const
wbLocalizationExtension: array [TwbLStringType] of string = (
'.DLSTRINGS',
'.ILSTRINGS',
'.STRINGS'
);
var
wbLocalizationHandler: TwbLocalizationHandler;
implementation
uses
WideStrUtils;
constructor TwbLocalizationFile.Create(const aFileName: string);
var
fs: TFileStream;
fStream: TMemoryStream;
Buffer: PByte;
begin
fFileName := aFileName;
Init;
// cache file in mem
fStream := TMemoryStream.Create;
try
fs := TFileStream.Create(aFileName, fmOpenRead or fmShareDenyNone);
GetMem(Buffer, fs.Size);
try
fs.ReadBuffer(Buffer^, fs.Size);
fStream.WriteBuffer(Buffer^, fs.Size);
fStream.Position := 0;
ReadDirectory(fStream);
finally
if Assigned(Buffer) then FreeMem(Buffer);
end;
finally
FreeAndNil(fs);
FreeAndNil(fStream);
end;
end;
constructor TwbLocalizationFile.Create(const aFileName: string; aData: TBytes);
var
fStream: TMemoryStream;
begin
fFileName := aFileName;
Init;
fStream := TMemoryStream.Create;
try
fStream.WriteBuffer(aData[0], length(aData));
fStream.Position := 0;
ReadDirectory(fStream);
finally
FreeAndNil(fStream);
end;
end;
destructor TwbLocalizationFile.Destroy;
begin
FreeAndNil(fStrings);
inherited;
end;
procedure TwbLocalizationFile.Init;
begin
fModified := false;
fName := ExtractFileName(fFileName);
fFileType := FileStringType(fFileName);
fStrings := TwbFastStringList.Create;
fNextID := 1;
end;
function TwbLocalizationFile.FileStringType(aFileName: string): TwbLStringType;
var
ext: string;
i: TwbLStringType;
begin
Result := lsString;
ext := ExtractFileExt(aFileName);
for i := Low(TwbLStringType) to High(TwbLStringType) do
if SameText(ext, wbLocalizationExtension[i]) then
Result := i;
end;
function TwbLocalizationFile.ReadZString(aStream: TMemoryStream): AnsiString;
var
Position : Integer;
begin
Position := aStream.Position;
Result := PAnsiChar(@PByte(aStream.Memory)[Position]);
aStream.Position := aStream.Position + Succ(Length(Result));
end;
function TwbLocalizationFile.ReadLenZString(aStream: TMemoryStream): AnsiString;
var
Len: Cardinal;
begin
aStream.ReadBuffer(Len, 4);
Dec(Len); // trailing null
SetLength(Result, Len);
if Len > 0 then
aStream.ReadBuffer(Result[1], Len);
end;
procedure TwbLocalizationFile.WriteZString(aStream: TMemoryStream; aString: AnsiString);
const
z: Byte = 0;
begin
aStream.WriteBuffer(aString[1], Length(aString));
aStream.WriteBuffer(z, SizeOf(z));
end;
procedure TwbLocalizationFile.WriteLenZString(aStream: TMemoryStream; aString: AnsiString);
const
z: Byte = 0;
var
l: Cardinal;
begin
l := Length(aString) + SizeOf(z);
aStream.WriteBuffer(l, SizeOf(Cardinal));
aStream.WriteBuffer(aString[1], Length(aString));
aStream.WriteBuffer(z, SizeOf(z));
end;
procedure TwbLocalizationFile.ReadDirectory(aStream: TMemoryStream);
var
i: integer;
scount, id, offset: Cardinal;
oldPos: int64;
s: AnsiString;
begin
if aStream.Size < 8 then
Exit;
aStream.Read(scount, 4); // number of strings
aStream.Position := aStream.Position + 4; // skip dataSize
if scount > 0 then
for i := 0 to scount - 1 do begin
aStream.Read(id, 4); // string ID
aStream.Read(offset, 4); // offset of string relative to data (header + dirsize)
oldPos := aStream.Position;
aStream.Position := 8 + scount*8 + offset; // header + dirsize + offset
if fFileType = lsString then
s := ReadZString(aStream)
else
s := ReadLenZString(aStream);
fStrings.AddObject(wbAnsiToString(s, nil), pointer(id));
if Succ(id) > fNextID then
fNextID := Succ(id);
aStream.Position := oldPos;
end;
end;
procedure TwbLocalizationFile.WriteToStream(const aStream: TStream);
var
dir, data: TMemoryStream;
i: integer;
c: Cardinal;
begin
dir := TMemoryStream.Create;
data := TMemoryStream.Create;
c := fStrings.Count;
dir.WriteBuffer(c, SizeOf(c)); // number of strings
dir.WriteBuffer(c, SizeOf(c)); // dataSize, will overwrite later
try
for i := 0 to Pred(fStrings.Count) do begin
c := Cardinal(fStrings.Objects[i]);
dir.WriteBuffer(c, SizeOf(c)); // ID
c := data.Position;
dir.WriteBuffer(c, SizeOf(c)); // relative position
if fFileType = lsString then
WriteZString(data, wbStringToAnsi(fStrings[i], nil))
else
WriteLenZString(data, wbStringToAnsi(fStrings[i], nil));
end;
c := data.Size;
dir.Position := 4;
dir.WriteBuffer(c, SizeOf(c)); // dataSize
aStream.CopyFrom(dir, 0);
aStream.CopyFrom(data, 0);
finally
FreeAndNil(dir);
FreeAndNil(data);
end;
end;
function TwbLocalizationFile.Count: Integer;
begin
Result := fStrings.Count;
end;
function TwbLocalizationFile.IndexToID(Index: Integer): Cardinal;
begin
if Index < Count then
Result := Cardinal(fStrings.Objects[Index])
else
Result := 0;
end;
function TwbLocalizationFile.IDExists(ID: Cardinal): boolean;
begin
Result := fStrings.IndexOfObject(Pointer(ID)) <> -1;
end;
function TwbLocalizationFile.Get(Index: Cardinal): string;
var
idx: integer;
begin
Result := '';
idx := fStrings.IndexOfObject(Pointer(Index));
if idx <> -1 then
Result := fStrings[idx]
else
Result := '<Error: Unknown lstring ID ' + IntToHex(Index, 8) + '>';
end;
procedure TwbLocalizationFile.Put(Index: Cardinal; const S: string);
var
idx: integer;
begin
idx := fStrings.IndexOfObject(Pointer(Index));
if idx <> -1 then
if fStrings[idx] <> S then begin
fStrings[idx] := S;
fModified := true;
end;
end;
function TwbLocalizationFile.AddString(ID: Integer; const S: string): boolean;
begin
Result := false;
if ID < NextID then
Exit;
fStrings.AddObject(S, Pointer(ID));
fNextID := Succ(ID);
fModified := true;
Result := true;
end;
procedure TwbLocalizationFile.ExportToFile(const aFileName: string);
var
i: integer;
sl: TStringList;
begin
sl := TStringList.Create;
try
for i := 0 to Pred(fStrings.Count) do begin
sl.Add('[' + IntToHex(Integer(fStrings.Objects[i]), 8) + ']');
sl.Add(fStrings[i]);
end;
sl.SaveToFile(aFileName);
finally
FreeAndNil(sl);
end;
end;
constructor TwbLocalizationHandler.Create;
begin
lFiles := TwbFastStringListCS.CreateSorted;
fReuseDup := false;
NoTranslate := false;
end;
destructor TwbLocalizationHandler.Destroy;
begin
Clear;
FreeAndNil(lFiles);
end;
function TwbLocalizationHandler.Count: Integer;
begin
Result := lFiles.Count;
end;
procedure TwbLocalizationHandler.Clear;
var
i: integer;
begin
for i := 0 to Pred(Count) do
_Files[i].Destroy;
lFiles.Clear;
end;
function TwbLocalizationHandler.Get(Index: Integer): TwbLocalizationFile;
begin
if Index < Count then
Result := TwbLocalizationFile(lFiles.Objects[Index])
else
Result := nil;
end;
function TwbLocalizationHandler.AddLocalization(const aFileName: string): TwbLocalizationFile;
begin
Result := TwbLocalizationFile.Create(aFileName);
lFiles.AddObject(ExtractFileName(aFileName), Result);
end;
function TwbLocalizationHandler.AddLocalization(const aFileName: string; aData: TBytes): TwbLocalizationFile;
begin
Result := TwbLocalizationFile.Create(aFileName, aData);
lFiles.AddObject(ExtractFileName(aFileName), Result);
end;
function TwbLocalizationHandler.LocalizedValueDecider(aElement: IwbElement): TwbLStringType;
var
sigElement, sigRecord: TwbSignature;
aRecord: IwbSubRecord;
begin
if Supports(aElement, IwbSubRecord, aRecord) then
sigElement := aRecord.Signature
else
sigElement := '';
sigRecord := aElement.ContainingMainRecord.Signature;
if (sigRecord <> 'LSCR') and (sigElement = 'DESC') then Result := lsDLString else // DESC always from dlstrings except LSCR
if (sigRecord = 'QUST') and (sigElement = 'CNAM') then Result := lsDLString else // quest log entry
if (sigRecord = 'BOOK') and (sigElement = 'CNAM') then Result := lsDLString else // Book CNAM description
if (sigRecord = 'INFO') and (sigElement <> 'RNAM') then Result := lsILString else // dialog, RNAM are lsString, others lsILString
Result := lsString; // others
end;
function TwbLocalizationHandler.GetStringsPath: string;
begin
Result := wbDataPath + 'Strings\';
end;
function TwbLocalizationHandler.AvailableLanguages: TStringList;
var
F: TSearchRec;
p: integer;
s: string;
begin
Result := TStringList.Create;
if FindFirst(StringsPath + '*.*STRINGS', faAnyFile, F) = 0 then try
repeat
s := LowerCase(ChangeFileExt(F.Name, ''));
p := LastDelimiter('_', s);
if p > 0 then begin
s := Copy(s, p + 1, length(s));
if s = '' then Continue;
s := AnsiUpperCase(s[1]) + Copy(s, 2, Length(s));
if Result.IndexOf(s) = -1 then
Result.Add(s);
end;
until FindNext(F) <> 0;
finally
FindClose(F);
end;
end;
function TwbLocalizationHandler.AvailableLocalizationFiles: TStringList;
var
F: TSearchRec;
begin
Result := TStringList.Create;
if FindFirst(StringsPath + '*.*STRINGS', faAnyFile, F) = 0 then try
repeat
Result.Add(F.Name);
until FindNext(F) <> 0;
finally
FindClose(F);
end;
end;
procedure TwbLocalizationHandler.LoadForFile(aFileName: string);
var
ls: TwbLStringType;
s: string;
res: TDynResources;
begin
if not Assigned(wbContainerHandler) then
Exit;
for ls := Low(TwbLStringType) to High(TwbLStringType) do begin
s := wbLocalizationHandler.GetLocalizationFileNameByType(aFileName, ls);
if lFiles.IndexOf(ExtractFileName(s)) = -1 then begin
res := wbContainerHandler.OpenResource(s);
if length(res) > 0 then begin
//wbProgressCallback('[' + s + '] Loading Localization.');
wbLocalizationHandler.AddLocalization(wbDataPath + s, res[High(res)].GetData);
end;
end;
end;
end;
function TwbLocalizationHandler.GetLocalizationFileNameByType(aPluginFile: string; ls: TwbLStringType): string;
begin
Result := Format('%s_%s%s', [
ChangeFileExt(aPluginFile, ''),
wbLanguage,
wbLocalizationExtension[ls]
]);
// relative path to Data folder
Result := 'Strings\' + Result;
end;
function TwbLocalizationHandler.GetLocalizationFileNameByElement(aElement: IwbElement): string;
begin
Result := '';
if not Assigned(aElement) then
Exit;
Result := GetLocalizationFileNameByType(aElement._File.FileName, LocalizedValueDecider(aElement));
end;
// create a new lstring from aValue for aElement
function TwbLocalizationHandler.AddValue(aValue: string; aElement: IwbElement): Cardinal;
var
ls: TwbLStringType;
FileName: string;
wblf: array [TwbLStringType] of TwbLocalizationFile;
idx: integer;
data: TBytes;
ID: Cardinal;
begin
Result := 0;
if not Assigned(aElement) then
Exit;
if aValue = '' then
Exit;
// create localization files if absent
try
ID := 1;
for ls := Low(TwbLStringType) to High(TwbLStringType) do begin
FileName := GetLocalizationFileNameByType(aElement._File.FileName, ls);
idx := lFiles.IndexOf(ExtractFileName(FileName));
if idx = -1 then begin
wblf[ls] := AddLocalization(wbDataPath + FileName, data);
wblf[ls].Modified := true;
end else
wblf[ls] := _Files[idx];
if wblf[ls].NextID > ID then
ID := wblf[ls].NextID;
end;
ls := LocalizedValueDecider(aElement);
// detect a duplicate string
if ReuseDup then begin
idx := wblf[ls].fStrings.IndexOf(aValue);
if idx <> -1 then ID := Cardinal(wblf[ls].fStrings.Objects[idx]) else
wblf[ls].AddString(ID, aValue);
end else
wblf[ls].AddString(ID, aValue);
Result := ID;
finally
end;
end;
function TwbLocalizationHandler.SetValue(ID: Cardinal; aElement: IwbElement; aValue: string): Cardinal;
var
idx: integer;
FileName: string;
begin
Result := ID;
if not Assigned(aElement) then
Exit;
FileName := GetLocalizationFileNameByElement(aElement);
idx := lFiles.IndexOf(ExtractFileName(FileName));
if (idx = -1) or (ID = 0) then begin
// new string
Result := AddValue(aValue, aElement);
Exit;
end;
if not _Files[idx].IDExists(ID) then
// string doesn't exist, create new
Result := AddValue(aValue, aElement)
else
// modify existing
_Files[idx][ID] := aValue;
end;
function TwbLocalizationHandler.GetValue(ID: Cardinal; aElement: IwbElement): string;
var
lFileName: string;
idx: integer;
begin
Result := '';
if NoTranslate then begin
Result := IntToHex(ID, 8);
Exit;
end;
if ID = 0 then
Exit;
lFileName := ExtractFileName(GetLocalizationFileNameByElement(aElement));
if lFileName = '' then
Exit;
idx := lFiles.IndexOf(lFileName);
// load strings files if absent
if idx = - 1 then
LoadForFile(aElement._File.FileName);
// get file again
idx := lFiles.IndexOf(lFileName);
if idx = - 1 then begin
Result := '<Error: No strings file for lstring ID ' + IntToHex(ID, 8) + '>';
Exit;
end;
Result := _Files[idx][ID];
end;
procedure TwbLocalizationHandler.GetStringsFromFile(aFileName: string; const aList: TStrings);
var
i: integer;
begin
if not Assigned(aList) then
Exit;
for i := 0 to Pred(lFiles.Count) do
if SameText(lFiles[i], aFileName) then begin
aList.Assign(_Files[i].fStrings);
Break;
end;
end;
initialization
wbLocalizationHandler := TwbLocalizationHandler.Create;
finalization
FreeAndNil(wbLocalizationHandler);
end.