-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKM_TextManager.pas
More file actions
763 lines (592 loc) · 18.9 KB
/
KM_TextManager.pas
File metadata and controls
763 lines (592 loc) · 18.9 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
unit KM_TextManager;
{$I KM_CompilerDirectives.inc}
interface
uses
System.Classes, Vcl.Dialogs, System.Math, Vcl.Clipbrd, System.Generics.Defaults,
System.StrUtils, System.SysUtils, System.Types,
KM_ResLocales,
KM_TextLines;
type
TKMTargetGame = (tgUnknown, tgKaMRemake, tgKnightsProvince);
TKMLibraryType = (
ltGame,
ltMissions
);
TKMClipboardExport = (
ceSimple, // Just columns with locales
ceLastChanged // Include LastChanged for each locale
);
TKMTextManager = class
private const
COL_NAME_TAG = 'Tag';
COL_NAME_NEEDS_UPDATE = 'Needs update';
COL_NAME_DESC = 'Description';
private class var
LOCALE_DEFAULT: Integer;
private
fLocales: TKMResLocales;
fWorkDir: string;
fTextPath: string;
fTagsPath: string; // We use consts only for ingame library, others don't need them
fMetaPath: string; // We use meta only for ingame library, others don't need it
fLines: TKMLines;
fTargetGame: TKMTargetGame;
fLibType: TKMLibraryType;
// For copy/paste
fBuffer: array of string;
fHasChanges: Boolean;
function GetCount: Integer;
function GetItem(aIndex: Integer): TKMLine;
procedure LoadMeta(const aFilename: string);
procedure SaveMeta(const aFilename: string);
procedure TagsAutoName(const aPath: string);
function ToClipboardHeader(aLocales: TByteSet; aExport: TKMClipboardExport): string;
function ToClipboardBody(aLocales: TByteSet; aExport: TKMClipboardExport): string;
procedure ClipboardColumnsToLocIndex(aLine: string; out aLocIndex: TArray<Integer>);
public const
// Path matches for KMR and KP
GAME_TEXT_PATH = 'data\text\text.%s.libx';
constructor Create(aTargetGame: TKMTargetGame; aLocales: TKMResLocales; const aWorkDir, aTagsPath, aMetaPath: string);
destructor Destroy; override;
procedure Load4(const aTextPath: string; aLocales: TByteSet);
procedure Save(aSortById: Boolean);
property Count: Integer read GetCount;
property Items[aIndex: Integer]: TKMLine read GetItem; default;
property HasChanges: Boolean read fHasChanges write fHasChanges;
procedure TextCopy(aIndex: Integer);
procedure TextPaste(aIndex: Integer);
procedure EraseAllStringsButEng(aIndex: Integer);
procedure Delete(aIndex: Integer);
procedure Insert(aIndex: Integer);
procedure InsertSeparator(aIndex: Integer);
procedure MoveUp(aIndex: Integer);
procedure MoveDown(aIndex: Integer);
procedure SortByIndex;
procedure SortByTag;
procedure CompactIndexes;
procedure ToClipboard(aLocales: TByteSet; aExport: TKMClipboardExport);
procedure ToClipboardAll(aList: TStringList; aLocales: TByteSet);
procedure FromClipboard;
procedure FromClipboardAll(aSortById: Boolean);
procedure ListMismatchingAll(aFolders: TStringList; aList: TStringList; aLocales: TByteSet);
end;
implementation
uses
KromStringUtils,
KM_IoXml;
{ TKMTextManager }
constructor TKMTextManager.Create(aTargetGame: TKMTargetGame; aLocales: TKMResLocales; const aWorkDir, aTagsPath, aMetaPath: string);
begin
inherited Create;
fLines := TKMLines.Create;
fTargetGame := aTargetGame;
fLocales := aLocales;
fWorkDir := aWorkDir;
fTagsPath := fWorkDir + aTagsPath;
fMetaPath := fWorkDir + aMetaPath;
TKMLine.LOCALE_COUNT := fLocales.Count;
LOCALE_DEFAULT := fLocales.IndexByCode(TKMResLocales.DEFAULT_LOCALE);
TKMLine.LOCALE_DEFAULT := LOCALE_DEFAULT;
end;
destructor TKMTextManager.Destroy;
begin
FreeAndNil(fLines);
inherited;
end;
procedure TKMTextManager.Load4(const aTextPath: string; aLocales: TByteSet);
var
I: Integer;
begin
fTextPath := fWorkDir + aTextPath;
fLines.Clear;
// See if this is a game libx or a mission
if Pos(GAME_TEXT_PATH, fTextPath) <> 0 then
fLibType := ltGame
else
fLibType := ltMissions;
// If we have consts - good, use them
if fLibType = ltGame then
fLines.LoadGameConsts(fTagsPath);
for I := 0 to fLocales.Count - 1 do
if (fTargetGame = tgKaMRemake)
or ((fTargetGame = tgKnightsProvince) and (fLibType = ltGame)) then
fLines.LoadLibx(Format(fTextPath, [fLocales[I].Code]), I)
else
fLines.LoadLibxKP(Format(fTextPath, [fLocales[I].Code]), I);
if fLibType = ltGame then
LoadMeta(fMetaPath);
if (fTargetGame = tgKaMRemake) and (fLibType = ltMissions) then
TagsAutoName(aTextPath); // Name tags just for UI, they wont be saved
fHasChanges := False;
end;
procedure TKMTextManager.Save(aSortById: Boolean);
var
I: Integer;
fname: string;
begin
// Save Libx files
for I := 0 to fLocales.Count - 1 do
begin
fname := Format(fTextPath, [fLocales[I].Code]);
if (fTargetGame = tgKaMRemake)
or ((fTargetGame = tgKnightsProvince) and (fLibType = ltGame)) then
fLines.SaveLibx(fname, I, (fLibType = ltMissions), aSortById)
else
fLines.SaveLibxKP(fname, I, (fLibType = ltGame));
end;
// For the game also save the consts and meta
if fLibType = ltGame then
begin
fLines.SaveGameConsts(fTagsPath);
SaveMeta(fMetaPath);
end;
fHasChanges := False;
end;
procedure TKMTextManager.LoadMeta(const aFilename: string);
var
I, K, L: Integer;
xml: TKMXMLDocument;
nTags, nLine, nLastChanged, nDesc, nLastEng: TXmlNode;
tagName: string;
lastEng: string;
tagExists: Boolean;
begin
if not FileExists(aFilename) then Exit;
xml := TKMXMLDocument.Create;
try
xml.LoadFromFile(aFilename);
nTags := xml.Root.Find('Tags');
for I := 0 to nTags.ChildNodes.Count - 1 do
begin
nLine := nTags.ChildNodes[I];
tagName := nLine.Name;
nLastChanged := nLine.Find('LastChanged');
L := fLines.IndexOfTag(tagName);
tagExists := L <> -1;
if tagExists then
begin
if nLastChanged <> nil then
for K := 0 to fLocales.Count - 1 do
if nLastChanged.HasAttribute(fLocales[K].Code) then
fLines[L].SetLastChanged(K, nLastChanged.Attributes[fLocales[K].Code].AsString);
nDesc := nLine.Find('Description');
fLines[L].Description := nDesc.Attributes['Value'].AsString;
nLastEng := nLine.Find('LastEng');
lastEng := nLastEng.Attributes['Value'].AsString;
if lastEng <> fLines[L].Strings[LOCALE_DEFAULT] then
fLines[L].LastChanged[LOCALE_DEFAULT] := Now;
end;
end;
finally
xml.Free;
end;
end;
procedure TKMTextManager.SaveMeta(const aFilename: string);
var
I, K: Integer;
xml: TKMXMLDocument;
nTags, nLine, nLastChanged, nDesc, nLastEng: TXmlNode;
begin
xml := TKMXMLDocument.Create;
try
nTags := xml.Root.AddChild('Tags');
for I := 0 to fLines.Count - 1 do
if not fLines[I].IsSpacer then
begin
nLine := nTags.AddChild(fLines[I].Tag);
nLastChanged := nLine.AddChild('LastChanged');
for K := 0 to fLocales.Count - 1 do
nLastChanged.Attributes[fLocales[K].Code] := fLines[I].GetLastChanged(K);
nDesc := nLine.AddChild('Description');
nDesc.Attributes['Value'] := fLines[I].Description;
nLastEng := nLine.AddChild('LastEng');
nLastEng.Attributes['Value'] := fLines[I].Strings[LOCALE_DEFAULT];
end;
xml.SaveToFile(aFilename);
finally
xml.Free;
end;
end;
procedure TKMTextManager.SortByIndex;
begin
fLines.Sort(TComparer<TKMLine>.Construct(
function (const aLeft, aRight: TKMLine): Integer
begin
Result := CompareValue(aLeft.Id, aRight.Id);
end));
fHasChanges := True;
end;
procedure TKMTextManager.SortByTag;
begin
fLines.Sort(TComparer<TKMLine>.Construct(
function (const aLeft, aRight: TKMLine): Integer
begin
Result := CompareStr(aLeft.Tag, aRight.Tag);
end));
fHasChanges := True;
end;
// Our goal is to keep everything in place but update the Ids
procedure TKMTextManager.CompactIndexes;
const
SPACING = 5; // Handy for adding new strings
var
I, K: Integer;
begin
if fLibType = ltMissions then Exit;
K := 0;
for I := 0 to fLines.Count - 1 do
if not fLines[I].IsSpacer then
begin
fLines[I].Id := K;
Inc(K);
end else
Inc(K, SPACING);
fHasChanges := True;
end;
function TKMTextManager.GetCount: Integer;
begin
Result := fLines.Count;
end;
function TKMTextManager.GetItem(aIndex: Integer): TKMLine;
begin
Result := fLines[aIndex];
end;
procedure TKMTextManager.TagsAutoName(const aPath: string);
begin
fLines.TagsAutoName(aPath);
fHasChanges := True;
end;
procedure TKMTextManager.TextCopy(aIndex: Integer);
var
I: Integer;
begin
SetLength(fBuffer, fLocales.Count);
for I := 0 to fLocales.Count - 1 do
fBuffer[I] := fLines[aIndex].Strings[I];
end;
procedure TKMTextManager.TextPaste(aIndex: Integer);
var
I: Integer;
begin
Assert(Length(fBuffer) = fLocales.Count);
for I := 0 to fLocales.Count - 1 do
fLines[aIndex].Strings[I] := fBuffer[I];
fHasChanges := True;
end;
procedure TKMTextManager.EraseAllStringsButEng(aIndex: Integer);
var
I: Integer;
begin
for I := 0 to fLocales.Count - 1 do
if I <> LOCALE_DEFAULT then
fLines[aIndex].Strings[I] := '';
fHasChanges := True;
end;
procedure TKMTextManager.Insert(aIndex: Integer);
begin
fLines.Insert(aIndex, TKMLine.Create(fLines.Last.Id + 1, 'TX_NEW' + IntToStr(fLines.Last.Id + 1)));
end;
procedure TKMTextManager.Delete(aIndex: Integer);
begin
fLines.Delete(aIndex);
fHasChanges := True;
end;
procedure TKMTextManager.InsertSeparator(aIndex: Integer);
begin
fLines.Insert(aIndex, TKMLine.CreateSpacer);
fHasChanges := True;
end;
procedure TKMTextManager.MoveUp(aIndex: Integer);
begin
if aIndex > 0 then
// Can't move the top item up
fLines.Move(aIndex, aIndex - 1);
fHasChanges := True;
end;
procedure TKMTextManager.MoveDown(aIndex: Integer);
begin
if aIndex < fLines.Count - 1 then
// Can't move the bottom item down
fLines.Move(aIndex, aIndex + 1);
fHasChanges := True;
end;
function TKMTextManager.ToClipboardHeader(aLocales: TByteSet; aExport: TKMClipboardExport): string;
var
I: Integer;
begin
// First line, list lang names
Result := COL_NAME_TAG;
for I := 0 to fLocales.Count - 1 do
if (aLocales = []) or (I in aLocales) then
Result := Result + #9 + fLocales[I].Code;
if aExport = ceLastChanged then
Result := Result + #9 + COL_NAME_NEEDS_UPDATE;
Result := Result + #9 + COL_NAME_DESC;
Result := Result + sLineBreak;
end;
function TKMTextManager.ToClipboardBody(aLocales: TByteSet; aExport: TKMClipboardExport): string;
var
I, K: Integer;
s: string;
begin
Result := '';
// Do the export
for I := 0 to fLines.Count - 1 do
begin
if not fLines[I].IsSpacer then
begin
Result := Result + fLines[I].Tag;
for K := 0 to fLocales.Count - 1 do
if (aLocales = []) or (K in aLocales) then
begin
s := fLines[I].Strings[K];
s := EscapeTextForGoogleSheets(s);
Result := Result + #9 + s;
if (fLibType = ltGame) and (aExport = ceLastChanged) and (K <> LOCALE_DEFAULT) then
begin
if (fLines[I].LastChanged[LOCALE_DEFAULT] = 0) then
Result := Result + #9 + '' // New line (not saved in Meta)
else
if (fLines[I].LastChanged[K] = 0) then
Result := Result + #9 + '' // New localization (not saved in Meta)
else
if (fLines[I].LastChanged[K] < fLines[I].LastChanged[LOCALE_DEFAULT]) then
Result := Result + #9 + 'UPDATED'
else
Result := Result + #9 + '';
end;
end;
Result := Result + #9 + fLines[I].Description;
end
else
Result := Result + '-';
Result := Result + sLineBreak;
end;
end;
// Copy to clipboard to be pasted into Excel/Sheets
// Tag Text Text Text
procedure TKMTextManager.ToClipboard(aLocales: TByteSet; aExport: TKMClipboardExport);
begin
Clipboard.AsText := ToClipboardHeader(aLocales, aExport) + ToClipboardBody(aLocales, aExport);
end;
// Copy to clipboard to be pasted into Excel/Sheets
// Tag Text Text Text
procedure TKMTextManager.ToClipboardAll(aList: TStringList; aLocales: TByteSet);
function TranslationPercent(aLineFrom, aLineTo: Integer): string;
var
I, K: Integer;
fromStr, toStr: string;
begin
Result := '-';
K := 0;
fromStr := IntToStr(aLineFrom);
toStr := IntToStr(aLineTo);
for I := 0 to fLocales.Count - 1 do
if (aLocales = []) or (I in aLocales) then
begin
Result := Result + #9;
// Add % and a hint for localizations that can reference Eng
if I <> LOCALE_DEFAULT then
Result := Result + '=ROUND(COUNTA('+Chr(Ord('B')+K)+fromStr+':'+Chr(Ord('B')+K)+toStr+')/COUNTA(B'+fromStr+':B'+toStr+')*100,2)' + '&"% (use <== when translation is not needed)"';
Inc(K);
end;
Result := Result + #9 + 'Conditional formatting range is `C3:C2600` and formula is `=AND(ISBLANK(C3), NOT (ISBLANK(B3)))`';
end;
var
localesCount: Integer;
I: Integer;
s: string;
lineCount: Integer;
exportMode: TKMClipboardExport;
begin
// How many locales are we exporting (except Eng)
localesCount := 0;
for I := 0 to fLocales.Count - 1 do
if (I <> LOCALE_DEFAULT) and ((aLocales = []) or (I in aLocales)) then
Inc(localesCount);
if localesCount = 1 then
exportMode := ceLastChanged
else
exportMode := ceSimple;
s := '';
lineCount := 1;
for I := 0 to aList.Count - 1 do
begin
s := s + '> ' + aList[I] + sLineBreak;
Inc(lineCount);
Load4(aList[I], aLocales);
s := s + ToClipboardBody(aLocales, exportMode);
Inc(lineCount, fLines.Count);
end;
Clipboard.AsText := ToClipboardHeader(aLocales, exportMode) + TranslationPercent(3, lineCount+1) + sLineBreak + s;
end;
procedure TKMTextManager.ClipboardColumnsToLocIndex(aLine: string; out aLocIndex: TArray<Integer>);
var
ss: TStringDynArray;
I: Integer;
begin
ss := SplitString(aLine, #9);
Assert(Length(ss) > 1, 'Column names line needs to have at least 2 columns (Tag and one Locale)');
// Always skip first column (Tag)
SetLength(aLocIndex, Length(ss) - 1);
for I := 1 to High(ss) do
if (ss[I] <> '')
and (ss[I] <> TKMTextManager.COL_NAME_NEEDS_UPDATE)
and (ss[I] <> TKMTextManager.COL_NAME_DESC) then
aLocIndex[I - 1] := fLocales.IndexByCode(ss[I])
else
begin
SetLength(aLocIndex, I-1);
Break;
end;
end;
// Update translations
procedure TKMTextManager.FromClipboard;
var
sl: TStringList;
locIndex: TArray<Integer>;
I, K: Integer;
tag: string;
txt: array of string;
idLine, idLoc: Integer;
changesCount: Integer;
s: string;
begin
sl := TStringList.Create;
sl.Text := Clipboard.AsText;
// First line, get lang names
ClipboardColumnsToLocIndex(sl[0], locIndex);
SetLength(txt, fLocales.Count);
// Other lines, get the strings
changesCount := 0;
for I := 1 to sl.Count - 1 do
if StringFromString(sl[I], #9, 0) <> '-' then
begin
// Source data
tag := StringFromString(sl[I], #9, 0);
for K := 0 to High(locIndex) do
begin
s := StringFromString(sl[I], #9, K+1);
if StartsStr(#39'=', s) then
s := LeftStr(s, Length(s) - 1);
txt[K] := s;
end;
// Now we have all data to update
idLine := fLines.IndexOfTag(tag);
for K := 0 to High(locIndex) do
begin
idLoc := locIndex[K];
if fLines[idLine].Strings[idLoc] <> txt[K] then
begin
fLines[idLine].Strings[idLoc] := txt[K];
Inc(changesCount);
end;
end;
end;
ShowMessage(Format('%d lines updated', [changesCount]));
sl.Free;
fHasChanges := True;
end;
procedure TKMTextManager.FromClipboardAll(aSortById: Boolean);
var
sl: TStringList;
locIndex: TArray<Integer>;
I, K: Integer;
tag: string;
txt: array of string;
idLine, idLoc: Integer;
changesCount: Integer;
s: string;
fname: string;
filesCount: Integer;
dt: TDateTime;
begin
dt := Now; // Query once, so we dont get on a "second change" moment
sl := TStringList.Create;
sl.Text := Clipboard.AsText;
// First line, get lang names
ClipboardColumnsToLocIndex(sl[0], locIndex);
SetLength(txt, fLocales.Count);
// Other lines, get the strings
filesCount := 0;
changesCount := 0;
// Line 0 - Column names
// Line 1 - Translation percentage
for I := 2 to sl.Count - 1 do
if StartsStr('>', sl[I]) then
begin
// Save previous
if I > 2 then
Save(aSortById);
// Take 1st cell and trim the header
fname := StringFromString(sl[I], #9, 0);
fname := StringReplace(fname, '> ', '', [rfReplaceAll]);
Load4(fname, []);
Inc(filesCount);
end else
if StringFromString(sl[I], #9, 0) <> '-' then
begin
// Source data
tag := StringFromString(sl[I], #9, 0);
if tag = '' then Continue;
for K := 0 to High(locIndex) do
begin
s := StringFromString(sl[I], #9, K+1);
if StartsStr(#39'=', s) then
s := LeftStr(s, Length(s) - 1);
txt[K] := s;
end;
// Now we have all data to update
idLine := fLines.IndexOfTag(tag);
if idLine = -1 then
Continue; // Tag was removed from the game
for K := 0 to High(locIndex) do
begin
idLoc := locIndex[K];
if fLines[idLine].Strings[idLoc] <> txt[K] then
begin
fLines[idLine].Strings[idLoc] := txt[K];
fLines[idLine].LastChanged[idLoc] := dt;
Inc(changesCount);
end;
end;
end;
Save(aSortById);
ShowMessage(Format('%d files updated. %d lines updated', [filesCount, changesCount]));
sl.Free;
end;
procedure TKMTextManager.ListMismatchingAll(aFolders: TStringList; aList: TStringList; aLocales: TByteSet);
var
I, K: Integer;
newFile: Boolean;
procedure Append(aString: string);
begin
if newFile then
begin
if aList.Count > 0 then
aList.Append('');
aList.Append(aFolders[I] + ': ');
newFile := False;
end;
aList.Append(aString);
end;
begin
for I := 0 to aFolders.Count - 1 do
begin
Load4(aFolders[I], aLocales);
newFile := True;
// Linebreaks
for K := 0 to fLines.Count - 1 do
if not fLines[K].CheckMatchingForCharCount(['|'], aLocales) then
Append(' | in ' + fLines[K].Tag);
// Formats
for K := 0 to fLines.Count - 1 do
if not fLines[K].CheckMatchingForCharCount(['%'], aLocales) then
Append(' % in ' + fLines[K].Tag);
// Colors
for K := 0 to fLines.Count - 1 do
if not fLines[K].CheckMatchingForCharCount(['[$'], aLocales) then
Append(' [$ in ' + fLines[K].Tag);
end;
end;
end.