-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathApus.Engine.UDict.pas
536 lines (497 loc) · 11.5 KB
/
Apus.Engine.UDict.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
// This file is licensed under the terms of BSD-3 license (see license.txt)
// This file is a part of the Apus Game Engine (http://apus-software.com/engine/)
unit Apus.Engine.UDict;
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
interface
uses Apus.Common;
var
languageID:integer;
langCode,langName,langNameEn,langFileName:string;
unicode,translated:boolean;
langCreatedBy:UTF8String; // Creator/contributor name
langBaseDir:UTF8String; // base directory for the language-specific files (default = langCode)
procedure DictInit(fname:UTF8String='';fname2:UTF8String='');
function Translate(s:String8):String8; overload;
function Translate(s:String16):String16; overload;
function Tr(s:UTF8String):UTF8String;
function Simply(s:UTF8String):UTF8String;
// Decode UTF8String from UTF8 to 8bit if dictionary is in UTF8
function Decode(s:String8):string8;
function Simplify(s:String8):string8; overload;
function Simplify(s:String16):String16; overload;
implementation
uses SysUtils;
type
tconv=object
def,new:string8;
defW,newW:string16;
age:integer;
end;
var conv:array of tconv;
replaceconv:array of tconv;
tcv:tconv;
numconv,curconv,numreplaceconv:integer;
sorttime:int64;
curage:integer;
function Simply(s:UTF8String):UTF8String;
var q:integer;
c:AnsiChar;
begin
q:=1;
while q<=length(s) do
begin
c:=s[q];
if (c=' ')or(c='''')or(c='^')or(c='-')or(c=#0)or(c=#9) then
delete(s,q,1)
else
inc(q);
end;
Result:=s;
end;
procedure checknew(num:integer);
begin
if num>0 then if conv[num].def>conv[(num-1) div 2].def then
begin
tcv:=conv[num];
conv[num]:=conv[(num-1) div 2];
conv[(num-1) div 2]:=tcv;
checknew((num-1) div 2);
end;
end;
procedure checkold(num:integer);
var needchange:integer;
begin
needchange:=0;
if (num*2+2<=curconv-1)and(conv[num*2+1].def<conv[num*2+2].def) then
needchange:=num*2+2
else
if num*2+1<=curconv-1 then needchange:=num*2+1
else
exit;
if conv[num].def<conv[needchange].def then
begin
tcv:=conv[num];
conv[num]:=conv[needchange];
conv[needchange]:=tcv;
checkold(needchange);
end;
end;
procedure processString(s:RawByteString);
var s1,s2:RawByteString;
i,q,w,e:integer;
sa:StringArr;
begin
inc(curage);
if IsUTF8(s) then begin
unicode:=true;
delete(s,1,3);
end;
w:=pos('//',s);
if w=1 then exit;
if w>0 then begin
q:=0;
for i:=1 to w-1 do
if s[i]='''' then inc(q);
if q mod 2=0 then delete(s,w,length(s)); // comment
end;
if uppercase(copy(s,1,10))='LANGUAGEID' then
begin
q:=pos('=',s);
if q>0 then
begin
s1:=chop(copy(s,q+1,255));
sa:=Split(';',s1);
try
languageID:=strtoint(sa[0]);
if (length(sa)>1) and (sa[1]<>'') then langCode:=chop(sa[1]);
if (length(sa)>2) and (sa[2]<>'') then langNameEn:=chop(sa[2]);
if (length(sa)>3) and (sa[3]<>'') then langName:=chop(sa[3]);
except
languageID:=0;
end;
end;
end;
if uppercase(copy(s,1,9))='CREATEDBY' then
begin
q:=pos('=',s);
if q>0 then langCreatedBy:=chop(copy(s,q+1,255));
end;
if uppercase(copy(s,1,7))='BASEDIR' then
begin
q:=pos('=',s);
if q>0 then langBaseDir:=chop(copy(s,q+1,255));
end;
q:=pos(''''+'''',s);
while q>0 do
begin
s:=copy(s,1,q-1)+'^'+copy(s,q+2);
q:=pos(''''+'''',s);
end;
q:=pos('''',s);
if q<>0 then
begin
s:=copy(s,q+1,2555);
q:=pos('''',s);
if q<>0 then
begin
s1:=copy(s,1,q-1);
s:=copy(s,q+1,2555);
q:=pos('''',s);
if q<>0 then
begin
w:=pos('=',s);
if (w<>0)and(w<q) then
begin
s:=copy(s,q+1,2555);
q:=pos('''',s);
if q<>0 then
begin
s2:=copy(s,1,q-1);
if (numconv mod 128=0) then
begin
setlength(conv,numconv+128);
end;
q:=pos('^',s1);
while q>0 do
begin
s1:=copy(s1,1,q-1)+''''{+''''}+copy(s1,q+1);
q:=pos('^',s1);
end;
q:=pos('^',s2);
while q>0 do
begin
s2:=copy(s2,1,q-1)+''''{+''''}+copy(s2,q+1);
q:=pos('^',s2);
end;
conv[numconv].def:=s1;
conv[numconv].new:=s2;
if unicode then begin
conv[numconv].defW:=DecodeUTF8(s1);
conv[numconv].newW:=DecodeUTF8(s2);
end else begin
conv[numconv].defW:=s1;
conv[numconv].newW:=s2;
end;
conv[numconv].age:=curage;
checknew(numconv);
inc(numconv);
end
end else
begin
w:=pos('->',s);
if (w<>0)and(w<q) then
begin
s:=copy(s,q+1,2555);
q:=pos('''',s);
if q<>0 then
begin
s2:=copy(s,1,q-1);
if (numreplaceconv mod 32=0) then
begin
setlength(replaceconv,numreplaceconv+32);
end;
q:=pos('^',s1);
while q>0 do
begin
s1:=copy(s1,1,q-1)+''''{+''''}+copy(s1,q+1);
q:=pos('^',s1);
end;
s1:={' '+}s1;
s2:={' '+}s2;
replaceconv[numreplaceconv].def:=s1;
replaceconv[numreplaceconv].new:=s2;
if unicode then begin
replaceconv[numreplaceconv].defW:=DecodeUTF8(s1);
replaceconv[numreplaceconv].newW:=DecodeUTF8(s2);
end else begin
replaceconv[numreplaceconv].defW:=s1;
replaceconv[numreplaceconv].newW:=s2;
end;
replaceconv[numreplaceconv].age:=curage;
inc(numreplaceconv);
end;
end;
end;
end;
end;
end;
end;
procedure DictInit(fname:UTF8String='';fname2:UTF8String='');
var f:text;
s:String8;
q,w,e:integer;
begin
LogMessage('DictInit: '+fname);
sorttime:=MyTickCount;
languageID:=0;
langCreatedBy:='';
langBaseDir:='';
langFileName:='';
numconv:=0;
unicode:=false;
curage:=0;
if fname='' then fname:='language.dic';
if fileexists(fname) then
begin
langFileName:=ExtractFileName(fname);
assign(f,fname);
SetTextCodePage(f,CP_UTF8);
reset(f);
while not eof(f) do
begin
readln(f,s);
processstring(s);
end;
close(f);
end;
if fileexists(fname2) then
begin
assign(f,fname2);
SetTextCodePage(f,CP_UTF8);
reset(f);
while not eof(f) do
begin
readln(f,s);
processstring(s);
end;
close(f);
end;
for q:=numconv-1 downto 1 do
begin
tcv:=conv[0];
conv[0]:=conv[q];
conv[q]:=tcv;
curconv:=q;
checkold(0);
end;
q:=0;
w:=0;
while q<numconv do
begin
e:=q;
if (q+1<numconv)and(conv[q].def=conv[q+1].def)and(conv[q].age<conv[q+1].age) then inc(q);
if (q-1>0)and(conv[q].def=conv[q-1].def)and(conv[q].age<conv[q-1].age) then inc(q);
if e=q then
begin
if q<>w then
conv[w]:=conv[q];
inc(q);
inc(w);
end;
end;
numconv:=w;
sorttime:=MyTickCount-sorttime;
if langBaseDir='' then langBaseDir:=langCode;
end;
function SimpleTranslate(s:UTF8String):UTF8String;
var s1,s2:UTF8String;
q:integer;
function subtranslate(s:UTF8String):UTF8String;
var q,cur,min,max:integer;
begin
if numconv>0 then
begin
min:=0;
max:=numconv-1;
while min<=max do
begin
cur:=(min+max) div 2;
if s=conv[cur].def then
begin
result:=conv[cur].new;
translated:=true;
exit;
end;
if s>conv[cur].def then
min:=cur+1
else
max:=cur-1;
end;
end;
result:=s;
q:=pos('`',s);
if q>0 then
begin
result:=copy(s,1,q-1);
while (q<length(s))and(s[q]<>' ')and(s[q]<>',')and(s[q]<>'.')and(s[q]<>'!')and(s[q]<>':')and(s[q]<>'(')and(s[q]<>')') do
inc(q);
if q<length(s) then
result:=result+copy(s,q,50000);
end;
end;
begin
q:=pos('^',s);
if q>0 then
begin
s1:=copy(s,1,q-1);
s2:=copy(s,q+1,2555);
result:=subtranslate(s1)+simpletranslate(s2);
end else
result:=subtranslate(s);
end;
function SimpleTranslateW(s:widestring):widestring;
var s1,s2:widestring;
q:integer;
function subtranslate(s:widestring):widestring;
var q,cur,min,max:integer;
begin
if numconv>0 then
begin
min:=0;
max:=numconv-1;
while min<=max do
begin
cur:=(min+max) div 2;
if s=conv[cur].defW then
begin
result:=conv[cur].newW;
translated:=true;
exit;
end;
if s>conv[cur].defW then
min:=cur+1
else
max:=cur-1;
end;
end;
result:=s;
q:=pos('`',s);
if q>0 then
begin
result:=copy(s,1,q-1);
while (q<length(s))and(s[q]<>' ')and(s[q]<>',')and(s[q]<>'.')and(s[q]<>'!')and(s[q]<>':')and(s[q]<>'(')and(s[q]<>')') do
inc(q);
if q<length(s) then
result:=result+copy(s,q,50000);
end;
end;
begin
q:=pos('^',s);
if q>0 then
begin
s1:=copy(s,1,q-1);
s2:=copy(s,q+1,2555);
result:=subtranslate(s1)+simpletranslateW(s2);
end else
result:=subtranslate(s);
// result:=s;
end;
function Simplify(s:string8):string8;
begin
while (length(s)>0)and(s[1]='^') do
s:=copy(s,2,16384);
while (length(s)>0)and(s[length(s)]='^') do
s:=copy(s,1,length(s)-1);
result:=s;
end;
function Simplify(s:String16):String16;
begin
while (length(s)>0)and(s[1]='^') do
s:=copy(s,2,16384);
while (length(s)>0)and(s[length(s)]='^') do
s:=copy(s,1,length(s)-1);
result:=s;
end;
function Translate(s:string8):string8;
var ss:array[0..5] of String8;
q,w:integer;
begin
// logmessage('translate: '+s);
for q:=0 to 5 do ss[q]:='';
w:=0;
q:=length(s);
if (q>0)and(s[q]='%') then s:=s+#0; // fake padding to split '%%%' - must be removed later
s:=s+'%%';
q:=pos('%%',s);
while q>0 do
begin
ss[w]:=Simplify(copy(s,1,q-1));
inc(w);
s:=copy(s,q+2,16384);
q:=pos('%%',s);
end;
s:=simpletranslate(ss[0]);
for q:=1 to 5 do
begin
w:=pos('%'+inttostr(q),s);
if w>0 then
begin
ss[0]:=copy(s,w+2,16384);
if (length(ss[0])>1)and(ss[0,1]='`')and(ss[0,2] in ['1'..'9']) then
begin
ss[q]:=ss[q]+copy(ss[0],1,2);
ss[0]:=copy(ss[0],3,16384);
end;
s:=copy(s,1,w-1)+simpletranslate(ss[q])+ss[0];
end;
end;
for q:=0 to numreplaceconv-1 do
begin
w:=pos(replaceconv[q].def,s);
if (w>0)and((w=1)or(s[w-1]=' ')or(s[w-1]='.')or(s[w-1]=',')) then
s:=copy(s,1,w-1)+replaceconv[q].new+copy(s,w+length(replaceconv[q].def),16384);
end;
result:=s;
end;
function Translate(s:String16):String16; overload;
var ss:array[0..5] of String16;
q,w,l:integer;
begin
// logmessage('translate: '+s);
for q:=0 to 5 do ss[q]:='';
w:=0;
q:=length(s);
if (q>0)and(s[q]='%') then s:=s+#0; // fake padding to split '%%%' - must be removed later
s:=s+'%%';
q:=pos(String16('%%'),s);
while q>0 do
begin
ss[w]:=Simplify(copy(s,1,q-1));
l:=length(ss[w]);
if (l>0) and (ss[w][l]=#0) then SetLength(ss[w],l-1);
inc(w);
s:=copy(s,q+2,16384);
q:=pos(String16('%%'),s);
end;
s:=simpletranslateW(ss[0]);
for q:=1 to 5 do
begin
w:=pos(String16('%'+inttostr(q)),s);
if w>0 then
begin
ss[0]:=copy(s,w+2,16384);
if (length(ss[0])>1)and(ss[0,1]='`')and(ss[0,2] in ['1'..'9']) then
begin
ss[q]:=ss[q]+copy(ss[0],1,2);
ss[0]:=copy(ss[0],3,16384);
end;
s:=copy(s,1,w-1)+simpletranslateW(ss[q])+ss[0];
end;
end;
for q:=0 to numreplaceconv-1 do
begin
w:=pos(replaceconv[q].defW,s);
if (w>0)and((w=1)or(s[w-1]=' ')or(s[w-1]='.')or(s[w-1]=',')) then
s:=copy(s,1,w-1)+replaceconv[q].newW+copy(s,w+length(replaceconv[q].defW),16384);
end;
result:=s;
end;
function Tr(s:string8):string8;
begin
// result:=Translate('^'+s+'^');
result:=Translate(s);
end;
function Decode(s:string8):string8;
var
wst:WideString;
begin
if unicode then begin
wst:=DecodeUTF8(s);
result:=wst;
end else
result:=s;
end;
initialization
end.