-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCfgDecodeCommand.cs
More file actions
553 lines (477 loc) · 20 KB
/
CfgDecodeCommand.cs
File metadata and controls
553 lines (477 loc) · 20 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
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace CFRezManager;
internal static partial class CfgDecodeCommand
{
private const int MaxConfigBytes = 8 * 1024 * 1024;
private const int MaxRezPhaseBytes = 8 * 1024;
private static readonly HashSet<string> KnownSystemConfigNames = new(StringComparer.OrdinalIgnoreCase)
{
"appinfo.cfg",
"commonconfig.cfg",
"dirsystemconfig.cfg",
"updateconfig.cfg"
};
private sealed record Options(string RootPath, string OutputDirectory, string? FailedListPath);
private sealed record DecodeResult(
string Path,
string RelativePath,
long SourceByteCount,
string Status,
string Method,
string OutputPath,
string Notes);
private sealed record StructuredTextCandidate(string Text, string EncodingName, int Score, string Method);
public static bool IsInvocation(string[] args)
{
return args.Any(arg =>
string.Equals(arg, "--decode-cfg", StringComparison.OrdinalIgnoreCase) ||
string.Equals(arg, "decode-cfg", StringComparison.OrdinalIgnoreCase));
}
public static int Run(string[] args)
{
try
{
Options options = ParseOptions(args);
List<string> files = ResolveInputFiles(options);
List<DecodeResult> results = files
.Select(path => DecodeFile(options, path))
.ToList();
string reportPath = Path.Combine(options.OutputDirectory, "_cfg_decode_report.txt");
string csvPath = Path.Combine(options.OutputDirectory, "_cfg_decode_report.csv");
WriteSummary(reportPath, csvPath, options, results);
Console.WriteLine($"Input CFG files: {results.Count.ToString(CultureInfo.InvariantCulture)}");
Console.WriteLine($"Decoded text: {results.Count(result => result.Status.StartsWith("decoded", StringComparison.OrdinalIgnoreCase)).ToString(CultureInfo.InvariantCulture)}");
Console.WriteLine($"Binary strip previews: {results.Count(result => result.Status == "binary-strip-preview").ToString(CultureInfo.InvariantCulture)}");
Console.WriteLine($"System encrypted: {results.Count(result => result.Status == "system-encrypted").ToString(CultureInfo.InvariantCulture)}");
Console.WriteLine($"Unresolved: {results.Count(result => result.Status == "unresolved").ToString(CultureInfo.InvariantCulture)}");
Console.WriteLine($"Output: {options.OutputDirectory}");
Console.WriteLine($"Report: {reportPath}");
Console.WriteLine($"Details: {csvPath}");
return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
return 1;
}
}
private static Options ParseOptions(string[] args)
{
string? rootPath = null;
string? outputDirectory = null;
string? failedListPath = null;
for (int index = 0; index < args.Length; index++)
{
string arg = args[index];
if (string.Equals(arg, "--decode-cfg", StringComparison.OrdinalIgnoreCase) ||
string.Equals(arg, "decode-cfg", StringComparison.OrdinalIgnoreCase))
{
continue;
}
if (TryReadOptionValue(args, ref index, "--root", out string? rootValue) ||
TryReadOptionValue(args, ref index, "--source-root", out rootValue) ||
TryReadOptionValue(args, ref index, "--input", out rootValue))
{
rootPath = rootValue;
continue;
}
if (TryReadOptionValue(args, ref index, "--output", out string? outputValue) ||
TryReadOptionValue(args, ref index, "-o", out outputValue))
{
outputDirectory = outputValue;
continue;
}
if (TryReadOptionValue(args, ref index, "--failed-list", out string? failedValue))
{
failedListPath = failedValue;
continue;
}
rootPath ??= arg;
}
if (string.IsNullOrWhiteSpace(rootPath))
{
throw new InvalidOperationException("Missing --root <directory>.");
}
rootPath = Path.GetFullPath(rootPath);
if (!Directory.Exists(rootPath))
{
throw new DirectoryNotFoundException($"CFG decode root does not exist: {rootPath}");
}
if (!string.IsNullOrWhiteSpace(failedListPath))
{
failedListPath = Path.GetFullPath(failedListPath);
if (!File.Exists(failedListPath))
{
throw new FileNotFoundException($"Failed-list file does not exist: {failedListPath}", failedListPath);
}
}
else
{
string defaultFailedList = Path.Combine(rootPath, "_cfg_failed_list.txt");
if (File.Exists(defaultFailedList))
{
failedListPath = defaultFailedList;
}
}
outputDirectory = string.IsNullOrWhiteSpace(outputDirectory)
? Path.Combine(rootPath, "_cfg_decode")
: Path.GetFullPath(outputDirectory);
Directory.CreateDirectory(outputDirectory);
Directory.CreateDirectory(Path.Combine(outputDirectory, "decoded"));
Directory.CreateDirectory(Path.Combine(outputDirectory, "strip_previews"));
return new Options(rootPath, outputDirectory, failedListPath);
}
private static bool TryReadOptionValue(string[] args, ref int index, string optionName, out string? value)
{
value = null;
string arg = args[index];
if (!string.Equals(arg, optionName, StringComparison.OrdinalIgnoreCase))
{
return false;
}
if (index + 1 >= args.Length)
{
throw new InvalidOperationException($"Missing value for {optionName}.");
}
index++;
value = args[index];
return true;
}
private static List<string> ResolveInputFiles(Options options)
{
IEnumerable<string> files;
if (!string.IsNullOrWhiteSpace(options.FailedListPath))
{
files = File.ReadLines(options.FailedListPath!)
.Select(line => line.Trim())
.Where(line => line.Length > 0)
.Select(line => Path.IsPathRooted(line)
? Path.GetFullPath(line)
: Path.GetFullPath(Path.Combine(options.RootPath, line)))
.Where(File.Exists);
}
else
{
files = Directory.EnumerateFiles(options.RootPath, "*.cfg", SearchOption.AllDirectories);
}
return files
.Distinct(StringComparer.OrdinalIgnoreCase)
.OrderBy(path => path, StringComparer.OrdinalIgnoreCase)
.ToList();
}
private static DecodeResult DecodeFile(Options options, string path)
{
string relativePath = Path.GetRelativePath(options.RootPath, path);
byte[] data = ReadPrefix(path, MaxConfigBytes);
byte[]? prepared = LzmaAloneDecoder.TryPrepareData(data, MaxConfigBytes);
bool lzmaExpanded = prepared is not null && !ReferenceEquals(prepared, data);
byte[] preparedBytes = prepared ?? data;
if (TryDecodeStructuredText(preparedBytes, lzmaExpanded ? "lzma-text" : "plain-text", out StructuredTextCandidate? plainCandidate))
{
StructuredTextCandidate candidate = plainCandidate!;
string outputPath = WriteDecodedText(options.OutputDirectory, relativePath, candidate.Text);
return new DecodeResult(
path,
relativePath,
data.Length,
lzmaExpanded ? "decoded-lzma" : "decoded-text",
$"{candidate.Method} / {candidate.EncodingName}",
outputPath,
$"score={candidate.Score.ToString(CultureInfo.InvariantCulture)}");
}
if (TryDecodeEncText(preparedBytes, out StructuredTextCandidate? encCandidate) ||
(lzmaExpanded && TryDecodeEncText(data, out encCandidate)))
{
StructuredTextCandidate candidate = encCandidate!;
string outputPath = WriteDecodedText(options.OutputDirectory, relativePath, candidate.Text);
return new DecodeResult(
path,
relativePath,
data.Length,
"decoded-enc",
$"{candidate.Method} / {candidate.EncodingName}",
outputPath,
$"score={candidate.Score.ToString(CultureInfo.InvariantCulture)}");
}
if (preparedBytes.Length <= MaxRezPhaseBytes &&
TryDecodeRezPhase(preparedBytes, out StructuredTextCandidate? rezCandidate))
{
StructuredTextCandidate candidate = rezCandidate!;
string outputPath = WriteDecodedText(options.OutputDirectory, relativePath, candidate.Text);
return new DecodeResult(
path,
relativePath,
data.Length,
"decoded-rez-phase",
$"{candidate.Method} / {candidate.EncodingName}",
outputPath,
$"score={candidate.Score.ToString(CultureInfo.InvariantCulture)}");
}
if (CfgBinaryStripDecoder.TryWritePng(
preparedBytes,
BuildPreviewPath(options.OutputDirectory, relativePath),
out CfgBinaryStripInfo stripInfo))
{
string outputPath = BuildPreviewPath(options.OutputDirectory, relativePath);
return new DecodeResult(
path,
relativePath,
data.Length,
"binary-strip-preview",
"raw-rgb-strip",
outputPath,
CfgBinaryStripDecoder.Describe(stripInfo));
}
if (LooksLikeSystemEncryptedConfig(relativePath, preparedBytes))
{
return new DecodeResult(
path,
relativePath,
data.Length,
"system-encrypted",
"launcher-or-protection-config",
string.Empty,
"High-entropy TCLS/TenProtect-style config; not a model material text CFG.");
}
return new DecodeResult(
path,
relativePath,
data.Length,
"unresolved",
"unknown",
string.Empty,
"No supported text decode succeeded.");
}
private static string WriteDecodedText(string outputRoot, string relativePath, string text)
{
string outputPath = Path.Combine(outputRoot, "decoded", NormalizeRelativePath(relativePath));
string? directory = Path.GetDirectoryName(outputPath);
if (!string.IsNullOrWhiteSpace(directory))
{
Directory.CreateDirectory(directory);
}
File.WriteAllText(outputPath, text, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
return outputPath;
}
private static string BuildPreviewPath(string outputRoot, string relativePath)
{
return Path.Combine(outputRoot, "strip_previews", NormalizeRelativePath(relativePath) + ".png");
}
private static string NormalizeRelativePath(string relativePath)
{
return relativePath
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)
.TrimStart(Path.DirectorySeparatorChar);
}
private static bool TryDecodeStructuredText(byte[] data, string method, out StructuredTextCandidate? candidate)
{
candidate = null;
if (!TextPreviewDecoder.TryDecode(data, preferKorean: false, out string text, out string encodingName))
{
return false;
}
int score = ScoreStructuredText(text);
if (score < 12)
{
return false;
}
candidate = new StructuredTextCandidate(text, encodingName, score, method);
return true;
}
private static bool TryDecodeEncText(byte[] data, out StructuredTextCandidate? candidate)
{
candidate = null;
if (!EncTextDecoder.TryDecode(data, out byte[] decoded))
{
return false;
}
return TryDecodeStructuredText(decoded, "enc-base64", out candidate);
}
private static bool TryDecodeRezPhase(byte[] data, out StructuredTextCandidate? candidate)
{
candidate = null;
StructuredTextCandidate? best = null;
for (int phase = 0; phase < RezCrypto.KeyLength; phase++)
{
if (TryDecodeRezPhaseVariant(data, phase, decode: true, out StructuredTextCandidate? decodedCandidate) &&
(best is null || decodedCandidate!.Score > best.Score))
{
best = decodedCandidate;
}
if (TryDecodeRezPhaseVariant(data, phase, decode: false, out StructuredTextCandidate? encodedCandidate) &&
(best is null || encodedCandidate!.Score > best.Score))
{
best = encodedCandidate;
}
if (best is not null && best.Score >= 100)
{
break;
}
}
candidate = best;
return candidate is not null;
}
private static bool TryDecodeRezPhaseVariant(byte[] data, int phase, bool decode, out StructuredTextCandidate? candidate)
{
candidate = null;
byte[] buffer = data.ToArray();
if (decode)
{
RezCrypto.Decode(buffer, phase);
}
else
{
RezCrypto.Encode(buffer, phase);
}
if (!TryDecodeStructuredText(buffer, decode ? $"rez-decode-phase-{phase}" : $"rez-encode-phase-{phase}", out StructuredTextCandidate? decoded))
{
return false;
}
candidate = decoded;
return true;
}
private static int ScoreStructuredText(string text)
{
if (string.IsNullOrWhiteSpace(text))
{
return 0;
}
int score = 0;
if (text.Contains("[Textures]", StringComparison.OrdinalIgnoreCase))
{
score += 120;
}
if (text.Contains("SpecularMapName", StringComparison.OrdinalIgnoreCase))
{
score += 80;
}
if (text.Contains("NormalMapName", StringComparison.OrdinalIgnoreCase) ||
text.Contains("EnvCubeMapName", StringComparison.OrdinalIgnoreCase) ||
text.Contains("DiffuseMappingEnabled", StringComparison.OrdinalIgnoreCase))
{
score += 30;
}
if (text.Contains("<?xml", StringComparison.OrdinalIgnoreCase))
{
score += 60;
}
if (Regex.IsMatch(text, @"(?m)^\s*\[[^\]\r\n]{1,80}\]\s*$"))
{
score += 24;
}
int assignmentCount = Regex.Matches(text, @"(?m)^\s*[^=\r\n]{1,120}=\s*.+$").Count;
score += Math.Min(40, assignmentCount * 4);
int textureReferenceCount = Regex.Matches(text, @"(?i)\.(png|dds|dtx|tga|jpg|jpeg|bmp)\b").Count;
score += Math.Min(30, textureReferenceCount * 3);
int lineCount = text.Count(ch => ch == '\n') + 1;
if (lineCount >= 4)
{
score += 8;
}
int suspicious = text.Count(ch => ch == '\uFFFD' || (char.IsControl(ch) && ch is not ('\r' or '\n' or '\t')));
score -= suspicious * 10;
return score;
}
private static bool LooksLikeSystemEncryptedConfig(string relativePath, byte[] data)
{
string fileName = NormalizeDuplicateSuffix(Path.GetFileName(relativePath));
if (KnownSystemConfigNames.Contains(fileName))
{
return true;
}
HashSet<byte> unique = [];
int printable = 0;
foreach (byte value in data)
{
unique.Add(value);
if (value is >= 0x20 and <= 0x7E or 0x09 or 0x0A or 0x0D)
{
printable++;
}
}
double printableRatio = data.Length == 0 ? 0 : printable / (double)data.Length;
return unique.Count >= 90 && printableRatio >= 0.25 && printableRatio <= 0.45;
}
private static string NormalizeDuplicateSuffix(string fileName)
{
return DuplicateSuffixRegex().Replace(fileName, string.Empty);
}
private static byte[] ReadPrefix(string path, int maxBytes)
{
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
int length = (int)Math.Min(stream.Length, maxBytes);
byte[] data = new byte[length];
int offset = 0;
while (offset < data.Length)
{
int read = stream.Read(data, offset, data.Length - offset);
if (read == 0)
{
break;
}
offset += read;
}
if (offset == data.Length)
{
return data;
}
Array.Resize(ref data, offset);
return data;
}
private static void WriteSummary(string reportPath, string csvPath, Options options, IReadOnlyList<DecodeResult> results)
{
using (var writer = new StreamWriter(reportPath, false, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)))
{
writer.WriteLine("CFG decode report");
writer.WriteLine();
writer.WriteLine($"Root: {options.RootPath}");
writer.WriteLine($"Output: {options.OutputDirectory}");
writer.WriteLine($"Failed list: {options.FailedListPath ?? "<all cfg files>"}");
writer.WriteLine($"Files: {results.Count.ToString(CultureInfo.InvariantCulture)}");
writer.WriteLine($"Decoded text: {results.Count(result => result.Status.StartsWith("decoded", StringComparison.OrdinalIgnoreCase)).ToString(CultureInfo.InvariantCulture)}");
writer.WriteLine($"Binary strip previews: {results.Count(result => result.Status == "binary-strip-preview").ToString(CultureInfo.InvariantCulture)}");
writer.WriteLine($"System encrypted: {results.Count(result => result.Status == "system-encrypted").ToString(CultureInfo.InvariantCulture)}");
writer.WriteLine($"Unresolved: {results.Count(result => result.Status == "unresolved").ToString(CultureInfo.InvariantCulture)}");
writer.WriteLine();
foreach (IGrouping<string, DecodeResult> group in results.GroupBy(result => result.Status))
{
writer.WriteLine(group.Key);
foreach (DecodeResult result in group.OrderBy(item => item.RelativePath, StringComparer.OrdinalIgnoreCase))
{
writer.WriteLine($"- {result.RelativePath}");
writer.WriteLine($" Method: {result.Method}");
if (!string.IsNullOrWhiteSpace(result.OutputPath))
{
writer.WriteLine($" Output: {result.OutputPath}");
}
if (!string.IsNullOrWhiteSpace(result.Notes))
{
writer.WriteLine($" Notes: {result.Notes}");
}
}
writer.WriteLine();
}
}
using var csv = new StreamWriter(csvPath, false, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
csv.WriteLine("RelativePath,SourceBytes,Status,Method,OutputPath,Notes");
foreach (DecodeResult result in results.OrderBy(item => item.RelativePath, StringComparer.OrdinalIgnoreCase))
{
csv.WriteLine(string.Join(",",
EscapeCsv(result.RelativePath),
result.SourceByteCount.ToString(CultureInfo.InvariantCulture),
EscapeCsv(result.Status),
EscapeCsv(result.Method),
EscapeCsv(result.OutputPath),
EscapeCsv(result.Notes)));
}
}
private static string EscapeCsv(string value)
{
return $"\"{value.Replace("\"", "\"\"", StringComparison.Ordinal)}\"";
}
[GeneratedRegex(@"\s+\(\d+\)(?=\.[^.]+$)", RegexOptions.CultureInvariant)]
private static partial Regex DuplicateSuffixRegex();
}