forked from MGuffin/xTranslator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTESVT_Fuz.pas
More file actions
603 lines (556 loc) · 15.7 KB
/
TESVT_Fuz.pas
File metadata and controls
603 lines (556 loc) · 15.7 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
{
Fallout4translator - TESVTranslator - sseTranslator by McGuffin mguffin[arobase]gmail.com
main distribution here: http://www.nexusmods.com/skyrimspecialedition/mods/134?
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.
Note on extra Components used by this Tool:
-SynEdit: https://github.com/SynEdit/SynEdit
-VirtualStringTree: written by Mike Lischke (www.soft-gems.net).
-Diff: Search Heuristic compare is using the Diff Unit http://www.angusj.com/delphi/textdiff.html credit to Angus Johnson
-HtmlViewer: https://github.com/BerndGabriel/HtmlViewer
-ZLibex: 1.2.3 http://www.dellapasqua.com and xEdit https://github.com/TES5Edit
-LZ4 https://github.com/atelierw/LZ4Delphi and xEdit https://github.com/TES5Edit
-Part of the code for Bsa/ba2/Stream loading from xEdit https://github.com/TES5Edit
-OmniXML: https://github.com/mremec/omnixml
-PCRE Regex library: http://www.regular-expressions.info/delphi.html (unicode version)
-Hunspell Module is distributed under the Mozilla Public License Version 1.1 https://github.com/hunspell/hunspell/releases
-xmw to wav converter: xWMAEncode.exe from directX SDK/microsoft
}
unit TESVT_Fuz;
interface
uses classes, sysutils, TESVT_Const, TESVT_Utils, TESVT_FastSearch, ioutils, regularexpressions, TESVT_TypeDef, TESVT_EspDefinition;
type
tfuzExport = class
bsaId: integer;
bIsCurrent: byte; // 0 = neutral, 1= current, 2= loose file
index, indexFd, masterIndex: integer;
responseID: integer;
voiceIndex: integer;
used: boolean;
sVoiceID: string;
hVoiceID: Cardinal;
inFilename: string;
inExt: string;
inFolder: string;
function getSize: Cardinal;
constructor Create(folder, filename: string; i, j: Cardinal; bsaIndex: integer); overload;
end;
tFuz = class
List: tlist;
BsaList, voiceType: tstringList;
current: Cardinal;
espLoader: tEspLoader;
private
function getIndex(formID: Cardinal): byte;
public
function getFromRec(r: rdialInfo): boolean; overload;
function getFromRec(r: rdialInfo; t: tstrings): boolean; overload;
function getFromRec(r: rdialInfo; var index: integer): boolean; overload;
function getVoiceFromRec(r: rdialInfo; voiceIndex: integer): boolean;
procedure verifyFromRec(r: rdialInfo);
procedure drawVoiceList(l: tstrings; filter: string = '');
procedure sanitize(mList: tstringList);
procedure cleanUnused(aList: tAlist);
procedure buildVoiceType;
procedure clear;
constructor Create;
destructor Destroy; override;
end;
procedure FindVoiceBSAEx(addonlist: tstringList; Path: String; FBSAList: tstringList);
function compareFuzForSorting(p1, p2: pointer): integer;
function compareFuz(p1, p2: pointer): integer;
function getExpressionString(espLoader: tEspLoader; rEx: Cardinal): string;
procedure getFuzFromFile(folder, filename: string);
procedure getXmwFromFile(folder, filename: string);
procedure getWemFromFile(folder, filename: string);
procedure getFuzFromBuffer(buffer: tbytes);
procedure getXmvFromBuffer(buffer: tbytes);
procedure getFuzFromLooseFiles(l: tlist);
procedure getWemFromBuffer(buffer: tbytes);
var
fuz: tFuz;
lFuzBSA: tstringList;
implementation
constructor tfuzExport.Create(folder, filename: string; i, j: Cardinal; bsaIndex: integer);
var
k: integer;
begin
bsaId := bsaIndex;
inFolder := folder;
used := false;
inExt := ansilowercase(extractfileext(filename));
inFilename := trim(removefileext(filename));
responseID := 0;
sVoiceID := '';
hVoiceID := 0;
masterIndex := 0;
if bGameIsSF then
begin
sVoiceID := ansilowercase(inFilename); // investigate for override
hVoiceID := stringHash(sVoiceID);
responseID := 1;
end
else
for k := length(inFilename) downto 1 do
if inFilename[k] = '_' then
if (k + 1 <= length(inFilename)) and ((k - 1) >= 1) then
begin
responseID := strtointdef(copy(inFilename, k + 1, 3), 0);
if responseID > 0 then
begin
if bGameIsSkyrim then
begin
if k - 8 > 0 then
sVoiceID := ansilowercase(copy(inFilename, k - 8, 8));
end
else
sVoiceID := ansilowercase(copy(inFilename, 1, k - 1));
hVoiceID := stringHash(sVoiceID);
end;
break;
end;
index := i;
indexFd := j;
end;
function tfuzExport.getSize: Cardinal;
begin
Result := retCardinal(instancesize + length(inFilename) + length(inExt) + length(inFolder)) * 2;
end;
// ==========tFuz===================
constructor tFuz.Create;
begin
List := tlist.Create;
espLoader := nil;
voiceType := tstringList.Create;
voiceType.sorted := true;
voiceType.Duplicates := dupIgnore;
BsaList := tstringList.Create;
BsaList.Duplicates := dupIgnore;
BsaList.sorted := true;
current := 0;
end;
function tFuz.getIndex(formID: Cardinal): byte;
begin
if assigned(espLoader) then
Result := espLoader.getMasterIndex(formID)
else
begin
// temp hack for first light / medium support for starfield.
// Need fixing for extended medium master inheritance
// works only with the base game medium esm for now.
// starfield Light and medium master support
if (byte(formID shr 24) = $FE) or (byte(formID shr 24) = $FD) then
Result := 1
else
Result := byte(formID shr 24);
end;
end;
destructor tFuz.Destroy;
begin
clear;
List.free;
BsaList.free;
voiceType.free;
inherited;
end;
procedure tFuz.clear;
var
i: integer;
begin
for i := 0 to pred(List.count) do
tfuzExport(List[i]).free;
List.clear;
voiceType.clear;
BsaList.clear;
current := 0;
espLoader := nil;
end;
procedure tFuz.drawVoiceList(l: tstrings; filter: string = '');
var
i: integer;
begin
l.BeginUpdate;
l.clear;
for i := 0 to pred(voiceType.count) do
begin
if (filter = '') or (pos(filter, ansilowercase(voiceType[i])) > 0) then
l.addObject(voiceType[i], tobject(i));
end;
l.endUpdate;
end;
procedure tFuz.sanitize(mList: tstringList);
var
i: integer;
a: sArray;
tf: tfuzExport;
begin
// delete unsupported fuz
for i := pred(List.count) downto 0 do
if (tfuzExport(List[i]).sVoiceID = '') or (tfuzExport(List[i]).responseID = 0) then
begin
// doFeedback(tfuzExport(List[i]).inFilename);
tfuzExport(List[i]).free;
List.delete(i);
end;
// get ID master
//for i := pred(List.count) downto 0 do
for i := 0 to pred(List.count) do
begin
tf := List[i];
explode(tf.inFolder, a, saSlash);
if length(a) >= 2 then
tf.masterIndex := mList.indexof(ansilowercase(a[2]))
else
tf.masterIndex := 0;
if tf.bsaId < 0 then
tf.bIsCurrent := 2
else if pos(RemoveFileExt(mList[pred(mList.count)]), BsaList[tf.bsaId]) = 1 then
tf.bIsCurrent := 1;
end;
List.sort(compareFuzForSorting);
setlength(a, 0);
end;
procedure tFuz.cleanUnused(aList: tAlist);
var
i: integer;
sk: tskyStr;
tf: tfuzExport;
rinfo: rdialInfo;
// tmplist: tlist;
begin
// check if the fuz is used
for i := 0 to pred(aList[2].count) do // INFO are only into list = 2
begin
sk := aList[2][i];
if sk.isEspAssigned then
begin
rinfo := sk.GetResponseInfo;
verifyFromRec(rinfo);
finalize(rinfo);
end;
end;
// delete unused fuz
if bCleanFuz then // set in dev Const
begin
{ tmplist := tlist.Create;
for i := 0 to pred(List.count) do
begin
tf := List[i];
if not tf.used then
tf.free
else
tmplist.Add(tf);
end;
List.clear;
List.assign(tmplist);
tmplist.free; }
for i := pred(List.count) downto 0 do
begin
tf := List[i];
if not tf.used then
begin
tf.free;
List.delete(i);
end;
end;
end;
end;
procedure tFuz.buildVoiceType;
var
i: integer;
a: sArray;
tf: tfuzExport;
begin
for i := 0 to pred(List.count) do
begin
tf := List[i];
explode(tf.inFolder, a, saSlash);
if length(a) > 3 then
voiceType.Add(a[3]);
end;
// dispatch voiceType Index
for i := 0 to pred(List.count) do
begin
tf := List[i];
explode(tf.inFolder, a, saSlash);
if length(a) > 3 then
voiceType.Find(a[3], tf.voiceIndex)
end;
end;
function tFuz.getVoiceFromRec(r: rdialInfo; voiceIndex: integer): boolean;
var
i, index: integer;
tf: tfuzExport;
begin
Result := false;
if getFromRec(r, index) then
begin
for i := index to pred(List.count) do
begin
tf := List[i];
if (r.hVoiceID <> tf.hVoiceID) or (tf.masterIndex <> getIndex(r.rformID)) then
break;
if (tf.responseID = r.rID) then
begin
if voiceIndex = tf.voiceIndex then
exit(true);
end;
end;
end;
end;
procedure tFuz.verifyFromRec(r: rdialInfo);
var
i, index: integer;
tf: tfuzExport;
begin
if getFromRec(r, index) then
begin
for i := index to pred(List.count) do
begin
tf := List[i];
if (r.hVoiceID <> tf.hVoiceID) or (tf.masterIndex <> getIndex(r.rformID)) then
break;
if (tf.responseID = r.rID) then
tf.used := true;
end;
end;
end;
function tFuz.getFromRec(r: rdialInfo; t: tstrings): boolean;
var
i, index: integer;
tf: tfuzExport;
begin
Result := getFromRec(r, index);
if Result then
begin
for i := index to pred(List.count) do
begin
tf := List[i];
if (r.hVoiceID <> tf.hVoiceID) or (tf.masterIndex <> getIndex(r.rformID)) then
break;
if (tf.responseID = r.rID) then
begin
if tf.bsaId < 0 then
t.addObject(format('[LooseFile] %s\%s%s', [tf.inFolder, tf.inFilename, tf.inExt]), tf)
else
t.addObject(format('[%s] %s\%s%s', [BsaList[tf.bsaId], tf.inFolder, tf.inFilename, tf.inExt]), tf);
end;
end;
end;
end;
function tFuz.getFromRec(r: rdialInfo; var index: integer): boolean;
var
tfSearch: tfuzExport;
begin
tfSearch := tfuzExport.Create;
tfSearch.masterIndex := getIndex(r.rformID); // byte(r.rformID shr 24);
tfSearch.responseID := r.rID;
tfSearch.sVoiceID := r.sVoiceID;
tfSearch.hVoiceID := r.hVoiceID;
Result := FastListSearch(List, compareFuz, tfSearch, index, true);
tfSearch.free;
end;
function tFuz.getFromRec(r: rdialInfo): boolean;
var
index: integer;
begin
Result := getFromRec(r, index);
end;
procedure getFuzFromFile(folder, filename: string);
var
f, fOut: tmemorystream;
header: Cardinal;
offset: Cardinal;
size: int64;
begin
f := tmemorystream.Create;
fOut := tmemorystream.Create;
try
f.loadFromFile(folder + filename);
f.position := 0;
f.read(header, sizeof(header));
f.read(header, sizeof(header));
f.read(offset, sizeof(offset));
f.position := f.position + offset;
size := f.size - f.position;
fOut.CopyFrom(f, size);
fOut.savetofile(tmpFuzPath + fuzTMPXMW);
finally
fOut.free;
f.free;
end;
end;
procedure getXmwFromFile(folder, filename: string);
var
f: tmemorystream;
begin
f := tmemorystream.Create;
try
f.loadFromFile(folder + filename);
f.position := 0;
f.savetofile(tmpFuzPath + fuzTMPXMW);
finally
f.free;
end;
end;
procedure getWemFromFile(folder, filename: string);
var
f: tmemorystream;
begin
f := tmemorystream.Create;
try
f.loadFromFile(folder + filename);
f.position := 0;
f.savetofile(tmpFuzPath + fuzTMPWEM);
finally
f.free;
end;
end;
procedure getFuzFromBuffer(buffer: tbytes);
var
f, fOut: tmemorystream;
header: Cardinal;
offset: Cardinal;
size: int64;
begin
f := tmemorystream.Create;
fOut := tmemorystream.Create;
try
f.WriteBuffer(buffer[0], length(buffer));
f.position := 0;
f.read(header, sizeof(header));
f.read(header, sizeof(header));
f.read(offset, sizeof(offset));
f.position := f.position + offset;
size := f.size - f.position;
fOut.CopyFrom(f, size);
fOut.savetofile(tmpFuzPath + fuzTMPXMW);
finally
fOut.free;
f.free;
end;
end;
procedure getXmvFromBuffer(buffer: tbytes);
var
f: tmemorystream;
begin
f := tmemorystream.Create;
try
f.WriteBuffer(buffer[0], length(buffer));
f.position := 0;
f.savetofile(tmpFuzPath + fuzTMPXMW);
finally
f.free;
end;
end;
procedure getWemFromBuffer(buffer: tbytes);
var
f: tmemorystream;
begin
f := tmemorystream.Create;
try
f.WriteBuffer(buffer[0], length(buffer));
f.position := 0;
f.savetofile(tmpFuzPath + fuzTMPWEM);
finally
f.free;
end;
end;
// ==============emote===============
function getExpressionString(espLoader: tEspLoader; rEx: Cardinal): string;
begin
if bGameIsSF and assigned(espLoader) then
Result := espLoader.findkeyword(rEx, nil, true)
else
Result := EmoteDefinitionList.values[inttohex(rEx, 8)];
if (Result = '') then
Result := EmoteDefinitionList.values['FFFFFFFF'];
end;
// ==============folder===============
function isFuzBSA(sSearch, sFile: string): boolean;
var
r: TRegEx;
begin
r := TRegEx.Create(sSearch);
Result := false;
try
Result := r.ismatch(sFile);
except
end;
end;
procedure FindVoiceBSAEx(addonlist: tstringList; Path: String; FBSAList: tstringList);
var
searchResult: TSearchRec;
tmpResult, tmpSearch: string;
i, j: integer;
begin
FBSAList.clear;
Path := IncludeTrailingPathDelimiter(Path);
try
if (FindFirst(Path + '*.*', faAnyFile - faDirectory, searchResult) = 0) then
begin
repeat
tmpResult := ansilowercase(searchResult.Name);
for i := 0 to pred(addonlist.count) do
begin
if AnsiPos(extractfileext(tmpResult), '.ba2;.bsa') > 0 then
begin
for j := 0 to pred(lFuzBSA.count) do
begin
tmpSearch := ansilowercase(stringReplace(lFuzBSA[j], '%basename%', removefileext(addonlist[i]), [rfIgnoreCase]));
if isFuzBSA(tmpSearch, removefileext(tmpResult)) then
FBSAList.Add(tmpResult)
end;
end;
end;
until FindNext(searchResult) <> 0;
end;
finally
FindClose(searchResult);
end;
end;
procedure getFuzFromLooseFiles(l: tlist);
var
Path, pathLower: string;
begin
if directoryExists(Game_CacheDataFolder + fuzLooseFolder) then
for Path in TDirectory.GetFiles(Game_CacheDataFolder + fuzLooseFolder, '*.*', TSearchOption.soAllDirectories) do
begin
pathLower := ansilowercase(copy(Path, length(Game_CacheDataFolder) + 1, length(Path)));
if pos(extractfileext(pathLower), sfuzExt) > 0 then
l.Add(tfuzExport.Create(extractFileDir(pathLower), extractFileName(pathLower), 0, 0, -1));
end;
end;
// -----------
// =FUZ quick sort & search=
function compareFuzHashID(p1, p2: pointer): integer;
begin
Result := compareCardinal(tfuzExport(p1).hVoiceID, tfuzExport(p2).hVoiceID);
if Result = 0 then
Result := AnsiCompareText(tfuzExport(p1).sVoiceID, tfuzExport(p2).sVoiceID);
end;
function compareFuzForSorting(p1, p2: pointer): integer;
begin
Result := compareFuzHashID(p1, p2);
if Result = 0 then
Result := tfuzExport(p1).responseID - tfuzExport(p2).responseID;
if Result = 0 then
Result := tfuzExport(p1).masterIndex - tfuzExport(p2).masterIndex;
if Result = 0 then
Result := tfuzExport(p2).bIsCurrent - tfuzExport(p1).bIsCurrent;
end;
function compareFuz(p1, p2: pointer): integer;
begin
Result := compareFuzHashID(p1, p2);
if Result = 0 then
Result := tfuzExport(p1).responseID - tfuzExport(p2).responseID;
if Result = 0 then
Result := tfuzExport(p1).masterIndex - tfuzExport(p2).masterIndex;
end;
end.