-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFmodBankDecoder.cs
More file actions
700 lines (604 loc) · 22.9 KB
/
FmodBankDecoder.cs
File metadata and controls
700 lines (604 loc) · 22.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
using System.Buffers.Binary;
using System.Globalization;
using System.IO;
using System.Text;
namespace CFRezManager;
internal sealed record FmodBankFsbBlock(
int Index,
int Offset,
int Length,
int MetadataLength,
uint HeaderVersion,
uint StreamCount,
uint SampleHeaderSize,
uint NameTableSize,
uint SampleDataSize,
uint ModeFlags,
IReadOnlyList<string> SampleNames,
IReadOnlyList<FmodBankFsbSample> Samples);
internal sealed record FmodBankFsbSample(
int HeaderOffset,
int HeaderLength,
int DataOffset,
int DataLength);
internal sealed record FmodBankDocument(
string Text,
string StorageDescription,
int SourceByteCount,
int DecodedByteCount,
int FsbBlockCount,
int StreamCount);
internal sealed record FmodBankExportResult(
string DecodedBankPath,
IReadOnlyList<string> FsbBlockPaths,
int DecodedByteCount);
internal static class FmodBankDecoder
{
public const int MaxSourceBytes = 768 * 1024 * 1024;
public const int MaxDecodedBytes = 768 * 1024 * 1024;
public const int MaxThumbnailSourceBytes = 96 * 1024 * 1024;
private const int FsbHeaderLength = 0x3C;
private const int MaxPreviewNamesPerBlock = 40;
private const int MaxSampleNamesPerBlock = 100_000;
private const int MaxWholeBankStrings = 120;
public static bool IsCandidate(string extension)
{
return string.Equals(extension, "bank", StringComparison.OrdinalIgnoreCase);
}
public static bool TryDecode(
byte[] data,
string fileName,
out FmodBankDocument? document,
out string? errorMessage)
{
document = null;
errorMessage = null;
if (!TryPrepareDecodedData(data, out byte[]? bankData, out bool compressed, out long decodedBytes, out errorMessage) ||
bankData is null)
{
return false;
}
if (!IsFmodBankData(bankData))
{
errorMessage = "BANK data is not a recognized FMOD RIFF/FEV bank.";
return false;
}
IReadOnlyList<FmodBankFsbBlock> fsbBlocks = ExtractFsbBlocks(bankData);
int streamCount = fsbBlocks.Sum(block => block.StreamCount > int.MaxValue ? int.MaxValue : (int)block.StreamCount);
string storage = compressed
? "LZMA-compressed FMOD bank"
: "FMOD bank";
string text = BuildPreviewText(fileName, data.Length, bankData, storage, fsbBlocks);
document = new FmodBankDocument(
text,
storage,
data.Length,
checked((int)Math.Min(decodedBytes, int.MaxValue)),
fsbBlocks.Count,
streamCount);
return true;
}
public static FmodBankExportResult ExportDecodedFiles(byte[] data, string fileName, string outputDirectory)
{
if (!TryPrepareDecodedData(data, out byte[]? bankData, out _, out _, out string? errorMessage) ||
bankData is null)
{
throw new InvalidDataException(errorMessage ?? "BANK compression could not be decoded.");
}
if (!IsFmodBankData(bankData))
{
throw new InvalidDataException("BANK data is not a recognized FMOD RIFF/FEV bank.");
}
Directory.CreateDirectory(outputDirectory);
string baseName = MakeSafeFileName(Path.GetFileNameWithoutExtension(fileName));
string decodedBankPath = MakeUniquePath(Path.Combine(outputDirectory, $"{baseName}.decoded.bank"));
File.WriteAllBytes(decodedBankPath, bankData);
var fsbPaths = new List<string>();
foreach (FmodBankFsbBlock block in ExtractFsbBlocks(bankData))
{
string fsbPath = MakeUniquePath(Path.Combine(outputDirectory, $"{baseName}.fsb{block.Index:D2}.fsb"));
using FileStream output = File.Create(fsbPath);
output.Write(bankData.AsSpan(block.Offset, block.Length));
fsbPaths.Add(fsbPath);
}
return new FmodBankExportResult(decodedBankPath, fsbPaths, bankData.Length);
}
public static bool IsCompressedBank(byte[] data)
{
return LzmaAloneDecoder.IsCompressed(data);
}
public static bool TryPrepareDecodedData(
byte[] data,
out byte[]? bankData,
out bool compressed,
out long decodedBytes,
out string? errorMessage)
{
bankData = null;
errorMessage = null;
compressed = LzmaAloneDecoder.IsCompressed(data);
decodedBytes = data.Length;
if (!compressed)
{
bankData = data;
return true;
}
if (!LzmaAloneDecoder.TryGetDecodedByteCount(data, out decodedBytes) ||
decodedBytes <= 0 ||
decodedBytes > MaxDecodedBytes ||
decodedBytes > int.MaxValue)
{
errorMessage = $"BANK decoded size is too large or invalid: {decodedBytes:N0} bytes.";
return false;
}
bankData = LzmaAloneDecoder.TryPrepareData(data, MaxDecodedBytes);
if (bankData is null)
{
errorMessage = "BANK compression could not be decoded.";
return false;
}
return true;
}
private static bool IsFmodBankData(byte[] data)
{
return data.Length >= 12 &&
data.AsSpan(0, 4).SequenceEqual("RIFF"u8) &&
(data.AsSpan(8, 4).SequenceEqual("FEV "u8) ||
data.AsSpan(8, 4).SequenceEqual("FSB5"u8));
}
public static IReadOnlyList<FmodBankFsbBlock> ExtractFsbBlocks(byte[] data, bool includeMetadataOnlyBlocks = false)
{
return ExtractFsbBlocks(data, data.Length, includeMetadataOnlyBlocks);
}
public static IReadOnlyList<FmodBankFsbBlock> ExtractFsbBlocks(
byte[] data,
int dataLength,
bool includeMetadataOnlyBlocks = false)
{
var blocks = new List<FmodBankFsbBlock>();
dataLength = Math.Clamp(dataLength, 0, data.Length);
int offset = 0;
while ((offset = IndexOf(data, dataLength, "FSB5"u8, offset)) >= 0)
{
if (offset + FsbHeaderLength > dataLength)
{
offset += 4;
continue;
}
uint headerVersion = ReadUInt32(data, offset + 4);
uint streamCount = ReadUInt32(data, offset + 8);
uint sampleHeaderSize = ReadUInt32(data, offset + 12);
uint nameTableSize = ReadUInt32(data, offset + 16);
uint sampleDataSize = ReadUInt32(data, offset + 20);
uint modeFlags = ReadUInt32(data, offset + 24);
ulong blockLength = (ulong)FsbHeaderLength + sampleHeaderSize + nameTableSize + sampleDataSize;
ulong metadataLength = (ulong)FsbHeaderLength + sampleHeaderSize + nameTableSize;
if (blockLength > int.MaxValue ||
metadataLength > int.MaxValue ||
streamCount > int.MaxValue)
{
offset += 4;
continue;
}
bool blockComplete = offset + (long)blockLength <= dataLength;
bool metadataComplete = offset + (long)metadataLength <= dataLength;
if (!blockComplete && (!includeMetadataOnlyBlocks || !metadataComplete))
{
offset += 4;
continue;
}
int nameOffset = checked(offset + FsbHeaderLength + (int)sampleHeaderSize);
int sampleNameLimit = Math.Min((int)streamCount, MaxSampleNamesPerBlock);
IReadOnlyList<string> sampleNames = Array.Empty<string>();
if (nameTableSize > 0 && nameOffset + (long)nameTableSize <= dataLength)
{
sampleNames = ExtractFsbNameTable(data.AsSpan(nameOffset, (int)nameTableSize), (int)streamCount, sampleNameLimit);
}
IReadOnlyList<FmodBankFsbSample> samples = TryExtractFsbSamples(
data.AsSpan(offset + FsbHeaderLength, (int)sampleHeaderSize),
(int)streamCount,
checked((int)sampleDataSize));
blocks.Add(new FmodBankFsbBlock(
blocks.Count,
offset,
(int)blockLength,
(int)metadataLength,
headerVersion,
streamCount,
sampleHeaderSize,
nameTableSize,
sampleDataSize,
modeFlags,
sampleNames,
samples));
if (!blockComplete)
{
break;
}
offset += Math.Max(4, (int)blockLength);
}
return blocks;
}
private static IReadOnlyList<string> TryExtractFmodSampleNames(byte[] data, int offset, int length, int maxCount)
{
if (maxCount <= 0 || offset < 0 || length <= 0 || offset + length > data.Length)
{
return Array.Empty<string>();
}
try
{
byte[] fsbData = data.AsSpan(offset, length).ToArray();
Fmod5Sharp.FmodTypes.FmodSoundBank soundBank = Fmod5Sharp.FsbLoader.LoadFsbFromByteArray(fsbData);
var names = new List<string>(Math.Min(soundBank.Samples.Count, maxCount));
foreach (Fmod5Sharp.FmodTypes.FmodSample sample in soundBank.Samples)
{
if (names.Count >= maxCount)
{
break;
}
names.Add(sample.Name ?? string.Empty);
}
return names;
}
catch
{
return Array.Empty<string>();
}
}
private static IReadOnlyList<FmodBankFsbSample> TryExtractFsbSamples(
ReadOnlySpan<byte> sampleHeaders,
int streamCount,
int sampleDataSize)
{
if (streamCount <= 0 || sampleHeaders.Length < streamCount * 8)
{
return Array.Empty<FmodBankFsbSample>();
}
try
{
var offsets = new List<int>(streamCount);
int position = 0;
for (int i = 0; i < streamCount; i++)
{
if (position + 8 > sampleHeaders.Length)
{
return Array.Empty<FmodBankFsbSample>();
}
uint first = BinaryPrimitives.ReadUInt32LittleEndian(sampleHeaders[position..]);
uint flags = first & 0x3F;
uint dataOffset = first >> 6;
long sampleOffset = dataOffset * 16L;
if (sampleOffset < 0 || sampleOffset > sampleDataSize)
{
return Array.Empty<FmodBankFsbSample>();
}
int headerOffset = position;
offsets.Add((int)sampleOffset);
position += 8;
if ((flags & 0x01) != 0)
{
bool hasNextChunk = true;
while (hasNextChunk)
{
if (position + 4 > sampleHeaders.Length)
{
return Array.Empty<FmodBankFsbSample>();
}
uint chunkHeader = BinaryPrimitives.ReadUInt32LittleEndian(sampleHeaders[position..]);
hasNextChunk = (chunkHeader & 0x01) != 0;
int chunkSize = checked((int)((chunkHeader >> 1) & 0x00FF_FFFF));
position += 4;
if (chunkSize < 0 || position + chunkSize > sampleHeaders.Length)
{
return Array.Empty<FmodBankFsbSample>();
}
position += chunkSize;
}
}
int headerLength = position - headerOffset;
if (headerLength <= 0)
{
return Array.Empty<FmodBankFsbSample>();
}
}
var samples = new List<FmodBankFsbSample>(streamCount);
int headerPosition = 0;
for (int i = 0; i < offsets.Count; i++)
{
int headerOffset = headerPosition;
if (headerOffset + 8 > sampleHeaders.Length)
{
return Array.Empty<FmodBankFsbSample>();
}
uint first = BinaryPrimitives.ReadUInt32LittleEndian(sampleHeaders[headerOffset..]);
uint flags = first & 0x3F;
headerPosition += 8;
if ((flags & 0x01) != 0)
{
bool hasNextChunk = true;
while (hasNextChunk)
{
if (headerPosition + 4 > sampleHeaders.Length)
{
return Array.Empty<FmodBankFsbSample>();
}
uint chunkHeader = BinaryPrimitives.ReadUInt32LittleEndian(sampleHeaders[headerPosition..]);
hasNextChunk = (chunkHeader & 0x01) != 0;
int chunkSize = checked((int)((chunkHeader >> 1) & 0x00FF_FFFF));
headerPosition += 4 + chunkSize;
if (headerPosition > sampleHeaders.Length)
{
return Array.Empty<FmodBankFsbSample>();
}
}
}
int headerLength = headerPosition - headerOffset;
int start = offsets[i];
int end = i + 1 < offsets.Count ? offsets[i + 1] : sampleDataSize;
if (end < start)
{
return Array.Empty<FmodBankFsbSample>();
}
samples.Add(new FmodBankFsbSample(headerOffset, headerLength, start, end - start));
}
return samples;
}
catch
{
return Array.Empty<FmodBankFsbSample>();
}
}
private static IReadOnlyList<string> ExtractFsbNameTable(ReadOnlySpan<byte> data, int streamCount, int maxCount)
{
if (data.Length < streamCount * sizeof(uint) || streamCount <= 0 || maxCount <= 0)
{
return Array.Empty<string>();
}
var names = new List<string>(Math.Min(streamCount, maxCount));
for (int i = 0; i < streamCount && names.Count < maxCount; i++)
{
int nameOffset = checked((int)BinaryPrimitives.ReadUInt32LittleEndian(data.Slice(i * sizeof(uint), sizeof(uint))));
if (nameOffset < streamCount * sizeof(uint) || nameOffset >= data.Length)
{
names.Add(string.Empty);
continue;
}
int end = nameOffset;
while (end < data.Length && data[end] != 0)
{
end++;
}
if (end <= nameOffset)
{
names.Add(string.Empty);
continue;
}
string value = DecodePrintable(data[nameOffset..end]).Trim();
names.Add(IsLikelySampleName(value) ? value : string.Empty);
}
return names;
}
private static string BuildPreviewText(
string fileName,
int sourceByteCount,
byte[] bankData,
string storage,
IReadOnlyList<FmodBankFsbBlock> fsbBlocks)
{
var builder = new StringBuilder();
builder.AppendLine($"FMOD bank: {fileName}");
builder.AppendLine($"Storage: {storage}, {sourceByteCount.ToString("N0", CultureInfo.CurrentCulture)} bytes -> {bankData.Length.ToString("N0", CultureInfo.CurrentCulture)} bytes");
builder.AppendLine($"Container: {ReadAscii(bankData, 0, 4)} / {ReadAscii(bankData, 8, 4)}");
if (bankData.Length >= 8)
{
uint riffBodyBytes = ReadUInt32(bankData, 4);
builder.AppendLine($"RIFF declared body: {riffBodyBytes.ToString("N0", CultureInfo.CurrentCulture)} bytes");
}
int totalStreams = fsbBlocks.Sum(block => block.StreamCount > int.MaxValue ? int.MaxValue : (int)block.StreamCount);
long totalSampleBytes = fsbBlocks.Sum(block => (long)block.SampleDataSize);
builder.AppendLine($"FSB5 blocks: {fsbBlocks.Count.ToString("N0", CultureInfo.CurrentCulture)}");
builder.AppendLine($"Audio streams: {totalStreams.ToString("N0", CultureInfo.CurrentCulture)}");
builder.AppendLine($"Sample data: {totalSampleBytes.ToString("N0", CultureInfo.CurrentCulture)} bytes");
builder.AppendLine();
if (fsbBlocks.Count == 0)
{
builder.AppendLine("No embedded FSB5 audio block was found.");
}
else
{
builder.AppendLine("Embedded FSB5 blocks:");
foreach (FmodBankFsbBlock block in fsbBlocks)
{
builder.AppendLine(
FormattableString.Invariant(
$"#{block.Index:D2} offset=0x{block.Offset:X} bytes={block.Length} streams={block.StreamCount} names={block.NameTableSize} sampleData={block.SampleDataSize} mode=0x{block.ModeFlags:X} version={block.HeaderVersion}"));
if (block.SampleNames.Count > 0)
{
builder.AppendLine(" Sample names:");
foreach (string name in block.SampleNames.Take(MaxPreviewNamesPerBlock))
{
builder.AppendLine($" - {name}");
}
if (block.SampleNames.Count > MaxPreviewNamesPerBlock)
{
builder.AppendLine($" ... {block.SampleNames.Count - MaxPreviewNamesPerBlock:N0} more names");
}
}
}
}
IReadOnlyList<string> visibleStrings = ExtractPrintableStrings(bankData, MaxWholeBankStrings);
if (visibleStrings.Count > 0)
{
builder.AppendLine();
builder.AppendLine("Readable strings:");
foreach (string value in visibleStrings)
{
builder.AppendLine($"- {value}");
}
}
return builder.ToString();
}
private static IReadOnlyList<string> ExtractNullTerminatedStrings(ReadOnlySpan<byte> data, int maxCount)
{
if (data.IsEmpty || maxCount <= 0)
{
return Array.Empty<string>();
}
var strings = new List<string>();
int start = 0;
for (int i = 0; i <= data.Length; i++)
{
bool atEnd = i == data.Length;
if (!atEnd && data[i] != 0)
{
continue;
}
if (i > start)
{
string value = DecodePrintable(data[start..i]).Trim();
if (value.Length >= 2)
{
strings.Add(value);
if (strings.Count >= maxCount)
{
break;
}
}
}
start = i + 1;
}
return strings;
}
private static IReadOnlyList<string> ExtractPrintableStrings(byte[] data, int maxCount)
{
int scanLength = Math.Min(data.Length, 4 * 1024 * 1024);
var strings = new List<string>();
int start = -1;
for (int i = 0; i < scanLength; i++)
{
if (IsPrintable(data[i]))
{
if (start < 0)
{
start = i;
}
}
else if (start >= 0)
{
AddPrintableString(data.AsSpan(start, i - start), strings, maxCount);
if (strings.Count >= maxCount)
{
break;
}
start = -1;
}
}
if (start >= 0 && strings.Count < maxCount)
{
AddPrintableString(data.AsSpan(start, scanLength - start), strings, maxCount);
}
return strings;
}
private static void AddPrintableString(ReadOnlySpan<byte> value, List<string> strings, int maxCount)
{
if (value.Length < 4 || strings.Count >= maxCount)
{
return;
}
string text = DecodePrintable(value).Trim();
if (text.Length >= 4 && !strings.Contains(text, StringComparer.Ordinal))
{
strings.Add(text);
}
}
private static string DecodePrintable(ReadOnlySpan<byte> value)
{
Span<char> chars = value.Length <= 512 ? stackalloc char[value.Length] : new char[value.Length];
int count = 0;
foreach (byte b in value)
{
chars[count++] = IsPrintable(b) ? (char)b : ' ';
}
return new string(chars[..count]);
}
private static bool IsPrintable(byte value)
{
return value is >= 0x20 and <= 0x7E;
}
private static bool IsLikelySampleName(string value)
{
if (value.Length < 2)
{
return false;
}
foreach (char ch in value)
{
if (char.IsLetterOrDigit(ch) || ch is '_' or '-' or '/' or '\\' or '.')
{
return true;
}
}
return false;
}
private static int IndexOf(byte[] data, ReadOnlySpan<byte> signature, int start)
{
for (int i = start; i <= data.Length - signature.Length; i++)
{
if (data.AsSpan(i, signature.Length).SequenceEqual(signature))
{
return i;
}
}
return -1;
}
private static int IndexOf(byte[] data, int dataLength, ReadOnlySpan<byte> signature, int start)
{
dataLength = Math.Clamp(dataLength, 0, data.Length);
for (int i = start; i <= dataLength - signature.Length; i++)
{
if (data.AsSpan(i, signature.Length).SequenceEqual(signature))
{
return i;
}
}
return -1;
}
private static uint ReadUInt32(byte[] data, int offset)
{
return BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(offset, sizeof(uint)));
}
private static string ReadAscii(byte[] data, int offset, int length)
{
if (offset < 0 || length <= 0 || offset + length > data.Length)
{
return string.Empty;
}
return Encoding.ASCII.GetString(data, offset, length);
}
private static string MakeSafeFileName(string value)
{
foreach (char invalid in Path.GetInvalidFileNameChars())
{
value = value.Replace(invalid, '_');
}
return string.IsNullOrWhiteSpace(value) ? "bank" : value;
}
private static string MakeUniquePath(string path)
{
if (!File.Exists(path))
{
return path;
}
string directory = Path.GetDirectoryName(path) ?? string.Empty;
string fileName = Path.GetFileNameWithoutExtension(path);
string extension = Path.GetExtension(path);
for (int index = 1; ; index++)
{
string candidate = Path.Combine(directory, $"{fileName} ({index}){extension}");
if (!File.Exists(candidate))
{
return candidate;
}
}
}
}