mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-19 16:20:01 -04:00
Triforce v0.5.2
The new launcher (Windows and Linux: the game's own look, music and settings, saves and mods pages, smaller updates in parts), benchmark mode (Find my best settings: the game tours four places by itself and writes its results), Quit and Restart in the pause menu under Mods, Squadala! in the Mods menu, battle music above 60 fps, menu text, the controller in the menus with the game in the background, the whole GameBanana list, RX 580 and RX 5000/6000 shader fixes, the pipeline cache kept through an update, async present black flashes, and faster builds.
This commit is contained in:
1570
launcher/Core/GameFiles/Astc.cs
Normal file
1570
launcher/Core/GameFiles/Astc.cs
Normal file
File diff suppressed because it is too large
Load Diff
70
launcher/Core/GameFiles/BlockLinear.cs
Normal file
70
launcher/Core/GameFiles/BlockLinear.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2025 suyu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
// (Triforce) The Tegra GPU's block-linear texture layout undone, ported from suyu's video_core/textures/decoders.cpp
|
||||
// (SwizzleImpl with TO_LINEAR false, depth 1). A texture is stored in GOBs (64 bytes by 8 rows, 512 bytes), stacked
|
||||
// 2^blockHeight GOBs tall into blocks; blocks run left to right, then the next row of blocks.
|
||||
//
|
||||
// Inside a GOB this is the Switch 2's order, as in our suyu's decoders.h: the Tegra X1's (x bits 0-3, 5, 8 / y bits 4,
|
||||
// 6, 7) with offset bits 5 and 6 swapped, so x bit 4 lands on bit 6 and y bit 1 on bit 5. (The X1's order, which most
|
||||
// Switch tools use, scrambles this game's textures in 4x4-block patches.)
|
||||
namespace Triforce.Core.GameFiles;
|
||||
|
||||
/// <summary>Tegra block-linear to linear.</summary>
|
||||
public static class BlockLinear
|
||||
{
|
||||
const int GobSizeXShift = 6; // 64 bytes
|
||||
const int GobSizeYShift = 3; // 8 rows
|
||||
const int GobSizeShift = GobSizeXShift + GobSizeYShift; // 512 bytes
|
||||
|
||||
/// <summary>The position of a byte inside its GOB: pdep of x into SWIZZLE_X_BITS 0b1_0100_1111 and of y into
|
||||
/// SWIZZLE_Y_BITS 0b0_1011_0000.</summary>
|
||||
static int SwizzleX(int x) => (x & 15) | (x & 16) << 2 | (x & 32) << 3;
|
||||
static int SwizzleY(int y) => (y & 3) << 4 | (y & 4) << 5;
|
||||
|
||||
/// <summary>The size a block-linear image takes, in bytes.</summary>
|
||||
public static long Size(int width, int height, int bytesPerElement, int blockHeightLog2)
|
||||
{
|
||||
var alignedWidth = (width * bytesPerElement + 63) & ~63;
|
||||
var rows = 1 << (GobSizeYShift + blockHeightLog2);
|
||||
var alignedHeight = (height + rows - 1) / rows * rows;
|
||||
return (long)alignedWidth * alignedHeight;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A block-linear image made linear. width and height count elements (pixels, or blocks of a compressed format);
|
||||
/// bytesPerElement is 1, 2, 4, 8 or 16. The result is width * height * bytesPerElement bytes, rows one after another.
|
||||
/// </summary>
|
||||
public static byte[] Deswizzle(ReadOnlySpan<byte> input, int width, int height, int bytesPerElement, int blockHeightLog2)
|
||||
{
|
||||
if (bytesPerElement is not (1 or 2 or 4 or 8 or 16))
|
||||
throw new NotSupportedException(bytesPerElement + " bytes per element");
|
||||
var pitch = width * bytesPerElement;
|
||||
var output = new byte[pitch * height];
|
||||
var gobsInX = (pitch + 63) >> GobSizeXShift;
|
||||
var blockSize = gobsInX << (GobSizeShift + blockHeightLog2);
|
||||
var blockHeightMask = (1 << blockHeightLog2) - 1;
|
||||
var xShift = GobSizeShift + blockHeightLog2;
|
||||
|
||||
if (Size(width, height, bytesPerElement, blockHeightLog2) > input.Length)
|
||||
throw new InvalidDataException("the texture's data is shorter than its size calls for");
|
||||
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
var swizzledY = SwizzleY(y);
|
||||
var blockY = y >> GobSizeYShift;
|
||||
var offsetY = (blockY >> blockHeightLog2) * blockSize + ((blockY & blockHeightMask) << GobSizeShift);
|
||||
var row = output.AsSpan(y * pitch, pitch);
|
||||
for (var x = 0; x < pitch; x += bytesPerElement)
|
||||
{
|
||||
var offsetX = (x >> GobSizeXShift) << xShift;
|
||||
var at = offsetY + offsetX + (SwizzleX(x & 63) | swizzledY);
|
||||
input.Slice(at, bytesPerElement).CopyTo(row.Slice(x, bytesPerElement));
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
317
launcher/Core/GameFiles/Bntx.cs
Normal file
317
launcher/Core/GameFiles/Bntx.cs
Normal file
@@ -0,0 +1,317 @@
|
||||
// BNTX: the Switch's texture archive (the game's UI art is in these, inside .bntx.zs files). Offsets as the lab's
|
||||
// research/scratch/t09_bntx.py reads them, little-endian:
|
||||
//
|
||||
// BNTX header 0x24 u32 texture count, 0x28 u64 offset of the table of BRTI offsets
|
||||
// BRTI 0x12 u16 tile mode (0 block-linear, 1 linear), 0x16 u16 mip count, 0x1C u32 format,
|
||||
// 0x24 u32 width, 0x28 u32 height, 0x34 u32 block height (log2, in GOBs), 0x50 u32 image size,
|
||||
// 0x58 u8[4] channel sources (R, G, B, A: 0 zero, 1 one, 2 red, 3 green, 4 blue, 5 alpha),
|
||||
// 0x60 u64 name offset (u16 length, then the name), 0x70 u64 offset of the mip offsets (u64 each)
|
||||
//
|
||||
// The format's high byte is the kind (0x0b RGBA8, 0x1a-0x1e BC1-BC5, 0x2d-0x3a ASTC 4x4 to 12x12), the low byte the
|
||||
// number type (1 unorm, 6 sRGB...). Only mip 0 of the first layer is decoded, as the launcher shows the art at full size.
|
||||
using System.Buffers.Binary;
|
||||
using System.Text;
|
||||
|
||||
namespace Triforce.Core.GameFiles;
|
||||
|
||||
/// <summary>One texture of a BNTX archive.</summary>
|
||||
public sealed record BntxTexture(string Name, int Width, int Height, uint Format, int MipCount, int TileMode,
|
||||
int BlockHeightLog2, long DataOffset, long ImageSize, byte[] ChannelSources)
|
||||
{
|
||||
/// <summary>The format's kind (high byte), e.g. 0x2d for ASTC 4x4.</summary>
|
||||
public int Kind => (int)(Format >> 8);
|
||||
|
||||
/// <summary>A readable format name.</summary>
|
||||
public string FormatName => Bntx.FormatName(Format);
|
||||
}
|
||||
|
||||
/// <summary>A parsed BNTX archive, with decoding of its textures to RGBA8.</summary>
|
||||
public sealed class Bntx
|
||||
{
|
||||
readonly byte[] data;
|
||||
readonly Dictionary<string, BntxTexture> byName = new(StringComparer.Ordinal);
|
||||
|
||||
public IReadOnlyList<BntxTexture> Textures { get; }
|
||||
|
||||
public Bntx(byte[] data)
|
||||
{
|
||||
this.data = data;
|
||||
if (data.Length < 0x30 || data[0] != 'B' || data[1] != 'N' || data[2] != 'T' || data[3] != 'X')
|
||||
throw new InvalidDataException("not a BNTX archive");
|
||||
var count = U32(0x24);
|
||||
var table = (long)U64(0x28);
|
||||
var list = new List<BntxTexture>();
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var p = (long)U64(table + 8 * i);
|
||||
if (data[p] != 'B' || data[p + 1] != 'R' || data[p + 2] != 'T' || data[p + 3] != 'I')
|
||||
throw new InvalidDataException("BNTX texture " + i + " has no BRTI header");
|
||||
var nameAt = (long)U64(p + 0x60);
|
||||
var nameLength = U16(nameAt);
|
||||
var name = Encoding.UTF8.GetString(data, (int)nameAt + 2, nameLength);
|
||||
var mips = (long)U64(p + 0x70);
|
||||
var tex = new BntxTexture(name, (int)U32(p + 0x24), (int)U32(p + 0x28), U32(p + 0x1C), U16(p + 0x16), U16(p + 0x12),
|
||||
(int)(U32(p + 0x34) & 7), (long)U64(mips), U32(p + 0x50), data[(int)(p + 0x58)..(int)(p + 0x5C)]);
|
||||
list.Add(tex);
|
||||
byName.TryAdd(name, tex);
|
||||
}
|
||||
Textures = list;
|
||||
}
|
||||
|
||||
ushort U16(long at) => BinaryPrimitives.ReadUInt16LittleEndian(data.AsSpan((int)at));
|
||||
uint U32(long at) => BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan((int)at));
|
||||
ulong U64(long at) => BinaryPrimitives.ReadUInt64LittleEndian(data.AsSpan((int)at));
|
||||
|
||||
/// <summary>The texture of that name, or null.</summary>
|
||||
public BntxTexture? Find(string name) => byName.GetValueOrDefault(name);
|
||||
|
||||
/// <summary>
|
||||
/// A texture's first mip decoded to RGBA8 (Width * Height * 4 bytes). The texels are as stored: an sRGB texture is not
|
||||
/// made linear, and a one-channel format (BC4, R8) comes out grey and opaque. With applyChannelSources the texture's own
|
||||
/// channel sources are applied as the GPU would (BC4 art in this game is then white, with the channel as alpha).
|
||||
/// </summary>
|
||||
public byte[] DecodeRgba(BntxTexture t, bool applyChannelSources = false)
|
||||
{
|
||||
var (blockW, blockH, bytesPerBlock) = BlockInfo(t.Format);
|
||||
var wBlocks = (t.Width + blockW - 1) / blockW;
|
||||
var hBlocks = (t.Height + blockH - 1) / blockH;
|
||||
var source = data.AsSpan((int)t.DataOffset, (int)Math.Min(t.ImageSize, data.Length - t.DataOffset));
|
||||
var linear = t.TileMode == 1
|
||||
? source[..(wBlocks * hBlocks * bytesPerBlock)].ToArray()
|
||||
: BlockLinear.Deswizzle(source, wBlocks, hBlocks, bytesPerBlock, t.BlockHeightLog2);
|
||||
|
||||
byte[] rgba = (t.Format >> 8) switch
|
||||
{
|
||||
>= 0x2d and <= 0x3a => Astc.Decompress(linear, t.Width, t.Height, blockW, blockH),
|
||||
0x1a => BlockCompression.Decode(linear, t.Width, t.Height, BlockCompression.Kind.BC1),
|
||||
0x1b => BlockCompression.Decode(linear, t.Width, t.Height, BlockCompression.Kind.BC2),
|
||||
0x1c => BlockCompression.Decode(linear, t.Width, t.Height, BlockCompression.Kind.BC3),
|
||||
0x1d => BlockCompression.Decode(linear, t.Width, t.Height, BlockCompression.Kind.BC4),
|
||||
0x1e => BlockCompression.Decode(linear, t.Width, t.Height, BlockCompression.Kind.BC5),
|
||||
0x0b => linear,
|
||||
0x0c => Swap(linear, 2, 1, 0, 3),
|
||||
0x02 => Expand(linear, 1),
|
||||
0x09 => Expand(linear, 2),
|
||||
_ => throw new NotSupportedException(t.Name + ": texture format " + FormatName(t.Format) + " is not supported"),
|
||||
};
|
||||
if (applyChannelSources)
|
||||
ApplySources(rgba, t.ChannelSources);
|
||||
return rgba;
|
||||
}
|
||||
|
||||
/// <summary>The block size and bytes per block of a format (1x1 for an uncompressed one).</summary>
|
||||
static (int W, int H, int Bytes) BlockInfo(uint format) => (format >> 8) switch
|
||||
{
|
||||
0x02 => (1, 1, 1),
|
||||
0x09 => (1, 1, 2),
|
||||
0x0b or 0x0c => (1, 1, 4),
|
||||
0x1a or 0x1d => (4, 4, 8),
|
||||
0x1b or 0x1c or 0x1e => (4, 4, 16),
|
||||
var k and >= 0x2d and <= 0x3a => (AstcBlocks[k - 0x2d].W, AstcBlocks[k - 0x2d].H, 16),
|
||||
_ => throw new NotSupportedException("texture format " + FormatName(format) + " is not supported"),
|
||||
};
|
||||
|
||||
static readonly (int W, int H)[] AstcBlocks =
|
||||
{
|
||||
(4, 4), (5, 4), (5, 5), (6, 5), (6, 6), (8, 5), (8, 6), (8, 8), (10, 5), (10, 6), (10, 8), (10, 10), (12, 10), (12, 12),
|
||||
};
|
||||
|
||||
public static string FormatName(uint format)
|
||||
{
|
||||
var kind = (int)(format >> 8);
|
||||
var type = (format & 0xFF) switch { 1 => "unorm", 2 => "snorm", 5 => "float", 6 => "srgb", var n => "type" + n };
|
||||
var name = kind switch
|
||||
{
|
||||
0x02 => "R8",
|
||||
0x09 => "R8G8",
|
||||
0x0b => "R8G8B8A8",
|
||||
0x0c => "B8G8R8A8",
|
||||
>= 0x1a and <= 0x20 => "BC" + (kind - 0x19),
|
||||
>= 0x2d and <= 0x3a => "ASTC_" + AstcBlocks[kind - 0x2d].W + "x" + AstcBlocks[kind - 0x2d].H,
|
||||
_ => "0x" + kind.ToString("x2"),
|
||||
};
|
||||
return name + "_" + type + " (0x" + format.ToString("x4") + ")";
|
||||
}
|
||||
|
||||
static byte[] Swap(byte[] p, int r, int g, int b, int a)
|
||||
{
|
||||
var o = new byte[p.Length];
|
||||
for (var i = 0; i < p.Length; i += 4)
|
||||
{
|
||||
o[i] = p[i + r];
|
||||
o[i + 1] = p[i + g];
|
||||
o[i + 2] = p[i + b];
|
||||
o[i + 3] = p[i + a];
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
/// <summary>R8 to grey (R, R, R, 255); R8G8 to (R, G, 0, 255).</summary>
|
||||
static byte[] Expand(byte[] p, int channels)
|
||||
{
|
||||
var n = p.Length / channels;
|
||||
var o = new byte[n * 4];
|
||||
for (var i = 0; i < n; i++)
|
||||
{
|
||||
var r = p[i * channels];
|
||||
o[4 * i] = r;
|
||||
o[4 * i + 1] = channels == 1 ? r : p[i * channels + 1];
|
||||
o[4 * i + 2] = channels == 1 ? r : (byte)0;
|
||||
o[4 * i + 3] = 255;
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
/// <summary>The texture's channel sources applied (0 zero, 1 one, 2-5 red, green, blue, alpha of the decoded texel).</summary>
|
||||
static void ApplySources(byte[] rgba, byte[] sources)
|
||||
{
|
||||
if (sources.Length < 4 || (sources[0] == 2 && sources[1] == 3 && sources[2] == 4 && sources[3] == 5))
|
||||
return;
|
||||
Span<byte> texel = stackalloc byte[4];
|
||||
for (var i = 0; i < rgba.Length; i += 4)
|
||||
{
|
||||
rgba.AsSpan(i, 4).CopyTo(texel);
|
||||
// (one-channel formats decode grey here: their "red" is that grey)
|
||||
for (var c = 0; c < 4; c++)
|
||||
rgba[i + c] = sources[c] switch { 0 => 0, 1 => 255, >= 2 and <= 5 => texel[sources[c] - 2], _ => texel[c] };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>BC1-BC5 (DXT1, DXT3, DXT5, RGTC1, RGTC2) decoding to RGBA8. BC4 decodes grey and opaque, BC5 to red and green.</summary>
|
||||
public static class BlockCompression
|
||||
{
|
||||
public enum Kind { BC1, BC2, BC3, BC4, BC5 }
|
||||
|
||||
public static byte[] Decode(ReadOnlySpan<byte> blocks, int width, int height, Kind kind)
|
||||
{
|
||||
var bytesPerBlock = kind is Kind.BC1 or Kind.BC4 ? 8 : 16;
|
||||
var wBlocks = (width + 3) / 4;
|
||||
var hBlocks = (height + 3) / 4;
|
||||
var output = new byte[width * height * 4];
|
||||
Span<byte> texels = stackalloc byte[64];
|
||||
Span<byte> channel = stackalloc byte[16];
|
||||
for (var by = 0; by < hBlocks; by++)
|
||||
for (var bx = 0; bx < wBlocks; bx++)
|
||||
{
|
||||
var block = blocks.Slice((by * wBlocks + bx) * bytesPerBlock, bytesPerBlock);
|
||||
switch (kind)
|
||||
{
|
||||
case Kind.BC1:
|
||||
Color(block, texels, true);
|
||||
break;
|
||||
case Kind.BC2:
|
||||
Color(block[8..], texels, false);
|
||||
for (var i = 0; i < 16; i++)
|
||||
{
|
||||
var nibble = (block[i / 2] >> (4 * (i & 1))) & 15;
|
||||
texels[4 * i + 3] = (byte)(nibble * 17);
|
||||
}
|
||||
break;
|
||||
case Kind.BC3:
|
||||
Color(block[8..], texels, false);
|
||||
Channel(block, channel);
|
||||
for (var i = 0; i < 16; i++)
|
||||
texels[4 * i + 3] = channel[i];
|
||||
break;
|
||||
case Kind.BC4:
|
||||
Channel(block, channel);
|
||||
for (var i = 0; i < 16; i++)
|
||||
{
|
||||
texels[4 * i] = texels[4 * i + 1] = texels[4 * i + 2] = channel[i];
|
||||
texels[4 * i + 3] = 255;
|
||||
}
|
||||
break;
|
||||
case Kind.BC5:
|
||||
Channel(block, channel);
|
||||
for (var i = 0; i < 16; i++)
|
||||
texels[4 * i] = channel[i];
|
||||
Channel(block[8..], channel);
|
||||
for (var i = 0; i < 16; i++)
|
||||
{
|
||||
texels[4 * i + 1] = channel[i];
|
||||
texels[4 * i + 2] = 0;
|
||||
texels[4 * i + 3] = 255;
|
||||
}
|
||||
break;
|
||||
}
|
||||
for (var y = 0; y < 4 && by * 4 + y < height; y++)
|
||||
{
|
||||
var count = Math.Min(4, width - bx * 4);
|
||||
texels.Slice(y * 16, count * 4).CopyTo(output.AsSpan(((by * 4 + y) * width + bx * 4) * 4));
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>A BC1 color block: two RGB565 endpoints and 2-bit indexes. With punchThrough (BC1), c0 <= c1 selects the
|
||||
/// three-color mode whose fourth color is transparent black.</summary>
|
||||
static void Color(ReadOnlySpan<byte> b, Span<byte> texels, bool punchThrough)
|
||||
{
|
||||
var c0 = BinaryPrimitives.ReadUInt16LittleEndian(b);
|
||||
var c1 = BinaryPrimitives.ReadUInt16LittleEndian(b[2..]);
|
||||
Span<int> pal = stackalloc int[16];
|
||||
Rgb565(c0, pal[..4]);
|
||||
Rgb565(c1, pal.Slice(4, 4));
|
||||
if (c0 > c1 || !punchThrough)
|
||||
{
|
||||
for (var k = 0; k < 3; k++)
|
||||
{
|
||||
pal[8 + k] = (2 * pal[k] + pal[4 + k] + 1) / 3;
|
||||
pal[12 + k] = (pal[k] + 2 * pal[4 + k] + 1) / 3;
|
||||
}
|
||||
pal[11] = pal[15] = 255;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var k = 0; k < 3; k++)
|
||||
{
|
||||
pal[8 + k] = (pal[k] + pal[4 + k] + 1) / 2;
|
||||
pal[12 + k] = 0;
|
||||
}
|
||||
pal[11] = 255;
|
||||
pal[15] = 0;
|
||||
}
|
||||
var bits = BinaryPrimitives.ReadUInt32LittleEndian(b[4..]);
|
||||
for (var i = 0; i < 16; i++)
|
||||
{
|
||||
var idx = (int)(bits >> (2 * i)) & 3;
|
||||
for (var k = 0; k < 4; k++)
|
||||
texels[4 * i + k] = (byte)pal[4 * idx + k];
|
||||
}
|
||||
}
|
||||
|
||||
static void Rgb565(ushort c, Span<int> into)
|
||||
{
|
||||
int r = (c >> 11) & 31, g = (c >> 5) & 63, b = c & 31;
|
||||
into[0] = (r << 3) | (r >> 2);
|
||||
into[1] = (g << 2) | (g >> 4);
|
||||
into[2] = (b << 3) | (b >> 2);
|
||||
into[3] = 255;
|
||||
}
|
||||
|
||||
/// <summary>A BC4/BC3-alpha block: two 8-bit endpoints and 3-bit indexes over 6 or 4 interpolated values, rounded
|
||||
/// to nearest (as the reference decoder the launcher's art was approved with).</summary>
|
||||
static void Channel(ReadOnlySpan<byte> b, Span<byte> values)
|
||||
{
|
||||
int a0 = b[0], a1 = b[1];
|
||||
Span<int> pal = stackalloc int[8];
|
||||
pal[0] = a0;
|
||||
pal[1] = a1;
|
||||
if (a0 > a1)
|
||||
for (var k = 1; k <= 6; k++)
|
||||
pal[k + 1] = ((7 - k) * a0 + k * a1 + 3) / 7;
|
||||
else
|
||||
{
|
||||
for (var k = 1; k <= 4; k++)
|
||||
pal[k + 1] = ((5 - k) * a0 + k * a1 + 2) / 5;
|
||||
pal[6] = 0;
|
||||
pal[7] = 255;
|
||||
}
|
||||
ulong bits = 0;
|
||||
for (var k = 0; k < 6; k++)
|
||||
bits |= (ulong)b[2 + k] << (8 * k);
|
||||
for (var i = 0; i < 16; i++)
|
||||
values[i] = (byte)pal[(int)(bits >> (3 * i)) & 7];
|
||||
}
|
||||
}
|
||||
221
launcher/Core/GameFiles/Bwav.cs
Normal file
221
launcher/Core/GameFiles/Bwav.cs
Normal file
@@ -0,0 +1,221 @@
|
||||
// BWAV: the game's sound files (romfs "Sound/Resource/Stream/<name>.bwav"). Ported from setup/Sound.cs (Bwav.Decode,
|
||||
// from NX2navi's runtime/menu_sound.c) and the lab's research/scratch/bwav2wav.py and mixwav.py, which made the sounds the
|
||||
// launcher's design was approved with; this decodes and mixes the same way, sample for sample.
|
||||
//
|
||||
// Layout: "BWAV", a byte order mark, a version, a CRC32, a prefetch flag (u16) and the channel count (u16 at 0x0E), then
|
||||
// 0x4C bytes per channel: codec (u16: 0 PCM16, 1 DSP-ADPCM), pan (u16: 0 left, 1 right, 2 middle), sample rate (u32),
|
||||
// sample count (u32), the count again, 16 DSP coefficients (s16), the place of the samples in the file (u32 at 0x30), ...
|
||||
// DSP-ADPCM is frames of 8 bytes: a header (the coefficient pair's index, the scale's exponent) and 14 samples of four bits.
|
||||
// All numbers little-endian. Other codecs (the title music's codec 2) are not decoded.
|
||||
namespace Triforce.Core.GameFiles;
|
||||
|
||||
/// <summary>16-bit stereo sound: left and right samples in turn.</summary>
|
||||
public sealed class Pcm
|
||||
{
|
||||
public int Rate { get; init; }
|
||||
public short[] Samples { get; init; } = Array.Empty<short>();
|
||||
public int Frames => Samples.Length / 2;
|
||||
public double Seconds => Rate > 0 ? (double)Frames / Rate : 0;
|
||||
|
||||
/// <summary>The loudest sample's size (0 for silence).</summary>
|
||||
public int Peak
|
||||
{
|
||||
get
|
||||
{
|
||||
var peak = 0;
|
||||
foreach (var s in Samples)
|
||||
peak = Math.Max(peak, Math.Abs((int)s));
|
||||
return peak;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Bwav
|
||||
{
|
||||
/// <summary>The share of full scale the loudest sample is set to by Mix and Normalize (as mixwav.py: 70 %).</summary>
|
||||
public const double DefaultPeak = 0.7;
|
||||
|
||||
static int U16(byte[] b, long at) => b[at] | b[at + 1] << 8;
|
||||
static long U32(byte[] b, long at) => b[at] | (long)b[at + 1] << 8 | (long)b[at + 2] << 16 | (long)b[at + 3] << 24;
|
||||
|
||||
/// <summary>
|
||||
/// A BWAV file decoded to 16-bit stereo, as bwav2wav.py does at gain 1: each channel goes to its side by its pan (left,
|
||||
/// right, or both), a one-channel sound is lowered by 0.7071 to sound as loud in both, and each sample is cut toward
|
||||
/// zero to a whole number and clamped. Throws InvalidDataException for anything that is not a whole BWAV file of one or
|
||||
/// two channels of PCM16 or DSP-ADPCM.
|
||||
/// </summary>
|
||||
public static Pcm Decode(byte[] b)
|
||||
{
|
||||
if (b.Length < 0x10 || b[0] != 'B' || b[1] != 'W' || b[2] != 'A' || b[3] != 'V')
|
||||
throw new InvalidDataException("not a BWAV file");
|
||||
var channels = U16(b, 0x0E);
|
||||
if (channels < 1 || channels > 2 || 0x10 + channels * 0x4C > b.Length)
|
||||
throw new InvalidDataException("BWAV with " + channels + " channels (one or two are decoded)");
|
||||
long rate = U32(b, 0x10 + 4), frames = U32(b, 0x10 + 8);
|
||||
if (rate < 8000 || rate > 96000 || frames == 0 || frames > rate * 3600)
|
||||
throw new InvalidDataException("BWAV with a rate of " + rate + " and " + frames + " samples");
|
||||
for (var c = 0; c < channels; c++)
|
||||
if (!Whole(b, 0x10 + c * 0x4C, frames))
|
||||
throw new InvalidDataException("BWAV channel " + c + " is cut off or of an unknown codec (" + U16(b, 0x10 + c * 0x4C) + ")");
|
||||
|
||||
// (the sides summed as whole numbers, as bwav2wav.py does: two channels panned to the same side add up)
|
||||
var left = new int[frames];
|
||||
var right = new int[frames];
|
||||
for (var c = 0; c < channels; c++)
|
||||
Channel(b, 0x10 + c * 0x4C, left, right, frames);
|
||||
|
||||
var samples = new short[frames * 2];
|
||||
for (long i = 0; i < frames; i++)
|
||||
{
|
||||
double l = left[i], r = right[i];
|
||||
if (channels == 1)
|
||||
{
|
||||
l *= 0.7071;
|
||||
r *= 0.7071;
|
||||
}
|
||||
samples[2 * i] = (short)Math.Clamp((int)l, -32768, 32767);
|
||||
samples[2 * i + 1] = (short)Math.Clamp((int)r, -32768, 32767);
|
||||
}
|
||||
return new Pcm { Rate = (int)rate, Samples = samples };
|
||||
}
|
||||
|
||||
/// <summary>Is the channel one of the two codecs, with all of its samples (as many as the sound has, at most) inside the
|
||||
/// file?</summary>
|
||||
static bool Whole(byte[] b, long info, long frames)
|
||||
{
|
||||
var codec = U16(b, info);
|
||||
long count = Math.Min(U32(b, info + 8), frames), at = U32(b, info + 0x30);
|
||||
// (DSP-ADPCM: the last frame may be cut to the bytes its samples use, its header and one byte per two samples; the
|
||||
// game's music files end that way, so asking for the whole 8 bytes would refuse them)
|
||||
var rest = count % 14;
|
||||
var bytes = count / 14 * 8 + (rest == 0 ? 0 : 1 + (rest + 1) / 2);
|
||||
return codec == 0 ? at + count * 2 <= b.Length : codec == 1 && at + bytes <= b.Length;
|
||||
}
|
||||
|
||||
/// <summary>One channel added into its side: pan 0 left, 1 right, anything else both.</summary>
|
||||
static void Channel(byte[] b, long info, int[] left, int[] right, long frames)
|
||||
{
|
||||
int codec = U16(b, info), pan = U16(b, info + 2);
|
||||
long count = Math.Min(U32(b, info + 8), frames), at = U32(b, info + 0x30);
|
||||
if (codec == 0)
|
||||
{
|
||||
for (long i = 0; i < count; i++)
|
||||
{
|
||||
int v = (short)U16(b, at + 2 * i);
|
||||
if (pan != 1) left[i] += v;
|
||||
if (pan != 0) right[i] += v;
|
||||
}
|
||||
return;
|
||||
}
|
||||
var pairs = new int[16];
|
||||
for (var i = 0; i < 16; i++)
|
||||
pairs[i] = (short)U16(b, info + 0x10 + 2 * i);
|
||||
long h1 = 0, h2 = 0;
|
||||
for (long i = 0; i < count; i++)
|
||||
{
|
||||
var frame = at + i / 14 * 8;
|
||||
var k = (int)(i % 14);
|
||||
int index = b[frame] >> 4 & 7, shift = b[frame] & 15;
|
||||
var nibble = b[frame + 1 + k / 2] >> ((k & 1) != 0 ? 0 : 4) & 15;
|
||||
if (nibble >= 8)
|
||||
nibble -= 16;
|
||||
var s = ((long)nibble << shift << 11) + pairs[2 * index] * h1 + pairs[2 * index + 1] * h2;
|
||||
var v = (int)Math.Max(-32768, Math.Min(32767, (s + 1024) >> 11));
|
||||
h2 = h1;
|
||||
h1 = v;
|
||||
if (pan != 1) left[i] += v;
|
||||
if (pan != 0) right[i] += v;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tracks of the same rate summed sample by sample (a shorter one is silence after its end), then scaled so the loudest
|
||||
/// sample is at `peak` of full scale, each sample cut toward zero: mixwav.py. One track in gives that track normalized.
|
||||
/// Saria's Song is the eight BGM_LostWood_0 .. _7 layers mixed this way.
|
||||
/// </summary>
|
||||
public static Pcm Mix(double peak, params Pcm[] tracks)
|
||||
{
|
||||
if (tracks.Length == 0)
|
||||
throw new ArgumentException("nothing to mix");
|
||||
var length = tracks.Max(t => t.Samples.Length);
|
||||
var rate = tracks[^1].Rate;
|
||||
var sum = new int[length];
|
||||
foreach (var t in tracks)
|
||||
{
|
||||
if (t.Rate != rate)
|
||||
throw new ArgumentException("tracks of different rates (" + t.Rate + ", " + rate + ") cannot be mixed");
|
||||
var s = t.Samples;
|
||||
for (var i = 0; i < s.Length; i++)
|
||||
sum[i] += s[i];
|
||||
}
|
||||
var loudest = 1;
|
||||
foreach (var v in sum)
|
||||
loudest = Math.Max(loudest, Math.Abs(v));
|
||||
var gain = peak * 32767 / loudest;
|
||||
var output = new short[length];
|
||||
for (var i = 0; i < length; i++)
|
||||
output[i] = (short)Math.Clamp((int)(sum[i] * gain), -32768, 32767);
|
||||
return new Pcm { Rate = rate, Samples = output };
|
||||
}
|
||||
|
||||
/// <summary>Mix at the default peak (70 % of full scale).</summary>
|
||||
public static Pcm Mix(params Pcm[] tracks) => Mix(DefaultPeak, tracks);
|
||||
|
||||
/// <summary>One track with its loudest sample at `peak` of full scale (Mix of one track).</summary>
|
||||
public static Pcm Normalize(Pcm track, double peak = DefaultPeak) => Mix(peak, track);
|
||||
|
||||
/// <summary>One track made louder or quieter by a factor, clamped at full scale.</summary>
|
||||
public static Pcm Gain(Pcm track, double gain)
|
||||
{
|
||||
var s = track.Samples;
|
||||
var output = new short[s.Length];
|
||||
for (var i = 0; i < s.Length; i++)
|
||||
output[i] = (short)Math.Clamp((int)(s[i] * gain), -32768, 32767);
|
||||
return new Pcm { Rate = track.Rate, Samples = output };
|
||||
}
|
||||
|
||||
/// <summary>The sound as a WAV file's bytes: 16-bit stereo PCM with the usual 44-byte header.</summary>
|
||||
public static byte[] ToWav(Pcm pcm)
|
||||
{
|
||||
var data = pcm.Samples.Length * 2;
|
||||
var wav = new byte[44 + data];
|
||||
void Tag(int at, string tag) { for (var i = 0; i < 4; i++) wav[at + i] = (byte)tag[i]; }
|
||||
void Put(int at, int value, int bytes) { for (var i = 0; i < bytes; i++) wav[at + i] = (byte)(value >> 8 * i); }
|
||||
Tag(0, "RIFF"); Put(4, 36 + data, 4); Tag(8, "WAVE");
|
||||
Tag(12, "fmt "); Put(16, 16, 4); Put(20, 1, 2); Put(22, 2, 2); Put(24, pcm.Rate, 4); Put(28, pcm.Rate * 4, 4); Put(32, 4, 2); Put(34, 16, 2);
|
||||
Tag(36, "data"); Put(40, data, 4);
|
||||
System.Runtime.InteropServices.MemoryMarshal.AsBytes(pcm.Samples.AsSpan()).CopyTo(wav.AsSpan(44));
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
for (var i = 44; i + 1 < wav.Length; i += 2)
|
||||
(wav[i], wav[i + 1]) = (wav[i + 1], wav[i]);
|
||||
return wav;
|
||||
}
|
||||
|
||||
/// <summary>A 16-bit stereo WAV file read back (for comparing with references; the data chunk is found by its tag).</summary>
|
||||
public static Pcm FromWav(byte[] wav)
|
||||
{
|
||||
if (wav.Length < 12 || wav[0] != 'R' || wav[8] != 'W')
|
||||
throw new InvalidDataException("not a WAV file");
|
||||
int rate = 0, at = 12;
|
||||
while (at + 8 <= wav.Length)
|
||||
{
|
||||
var tag = System.Text.Encoding.ASCII.GetString(wav, at, 4);
|
||||
var n = (int)U32(wav, at + 4);
|
||||
if (tag == "fmt ")
|
||||
{
|
||||
if (U16(wav, at + 8 + 2) != 2 || U16(wav, at + 8 + 14) != 16)
|
||||
throw new NotSupportedException("only 16-bit stereo WAV files are read");
|
||||
rate = (int)U32(wav, at + 8 + 4);
|
||||
}
|
||||
else if (tag == "data")
|
||||
{
|
||||
n = Math.Min(n, wav.Length - at - 8);
|
||||
var samples = new short[n / 2];
|
||||
System.Runtime.InteropServices.MemoryMarshal.Cast<byte, short>(wav.AsSpan(at + 8, n / 2 * 2)).CopyTo(samples);
|
||||
return new Pcm { Rate = rate, Samples = samples };
|
||||
}
|
||||
at += 8 + n + (n & 1);
|
||||
}
|
||||
throw new InvalidDataException("WAV without data");
|
||||
}
|
||||
}
|
||||
30
launcher/Core/GameFiles/Caption.cs
Normal file
30
launcher/Core/GameFiles/Caption.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
// A save slot's picture: each slot folder of the game's saves (slot_00 .. slot_05) has a caption.sav that holds, among
|
||||
// the slot's details, a JPEG of the place the player saved (research/scratch/t09_caption.py). The JPEG is taken as the
|
||||
// bytes from its first start marker (FF D8 FF) to its last end marker (FF D9), which needs no knowledge of the .sav layout.
|
||||
namespace Triforce.Core.GameFiles;
|
||||
|
||||
public static class Caption
|
||||
{
|
||||
/// <summary>The JPEG inside a caption.sav's bytes, or null when there is none.</summary>
|
||||
public static byte[]? Jpeg(ReadOnlySpan<byte> captionSav)
|
||||
{
|
||||
ReadOnlySpan<byte> start = stackalloc byte[] { 0xFF, 0xD8, 0xFF };
|
||||
ReadOnlySpan<byte> end = stackalloc byte[] { 0xFF, 0xD9 };
|
||||
var from = captionSav.IndexOf(start);
|
||||
if (from < 0)
|
||||
return null;
|
||||
var to = captionSav.LastIndexOf(end);
|
||||
if (to < from)
|
||||
return null;
|
||||
return captionSav[from..(to + 2)].ToArray();
|
||||
}
|
||||
|
||||
/// <summary>The JPEG of a caption.sav file (read only, shared with the game), or null when there is none.</summary>
|
||||
public static byte[]? JpegFromFile(string captionSavPath)
|
||||
{
|
||||
using var f = new FileStream(captionSavPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
|
||||
var data = new byte[f.Length];
|
||||
f.ReadExactly(data);
|
||||
return Jpeg(data);
|
||||
}
|
||||
}
|
||||
59
launcher/Core/GameFiles/FolderSource.cs
Normal file
59
launcher/Core/GameFiles/FolderSource.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
// A game the player keeps unpacked: the folder that holds romfs/ (and exefs/), or the romfs folder itself.
|
||||
namespace Triforce.Core.GameFiles;
|
||||
|
||||
/// <summary>Reads romfs files straight from a folder. Never writes to it.</summary>
|
||||
public sealed class FolderSource : IGameSource
|
||||
{
|
||||
/// <summary>The romfs folder in use.</summary>
|
||||
public string Romfs { get; }
|
||||
|
||||
/// <param name="path">The game folder holding romfs/, or the romfs folder itself (it is taken as one when it has the
|
||||
/// folders every romfs of this game has: Pack and Sound).</param>
|
||||
public FolderSource(string path)
|
||||
{
|
||||
var full = Path.GetFullPath(path);
|
||||
if (Directory.Exists(Path.Combine(full, "romfs")))
|
||||
Romfs = Path.Combine(full, "romfs");
|
||||
else if (Directory.Exists(Path.Combine(full, "Pack")) || Directory.Exists(Path.Combine(full, "Sound")))
|
||||
Romfs = full;
|
||||
else
|
||||
throw new DirectoryNotFoundException("No romfs folder in " + full + " (choose the game folder that holds romfs and exefs)");
|
||||
}
|
||||
|
||||
public string Describe => "folder " + Romfs;
|
||||
|
||||
string Full(string romfsPath) => Path.Combine(Romfs, GameSource.Clean(romfsPath).Replace('/', Path.DirectorySeparatorChar));
|
||||
|
||||
public bool Exists(string romfsPath) => File.Exists(Full(romfsPath));
|
||||
|
||||
public byte[] Read(string romfsPath)
|
||||
{
|
||||
var file = Full(romfsPath);
|
||||
// (FileShare.ReadWrite: another program, the game itself, may have the file open; we only ever read)
|
||||
using var f = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, 1, FileOptions.SequentialScan);
|
||||
var data = new byte[f.Length];
|
||||
f.ReadExactly(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
public IEnumerable<string> List(string dir)
|
||||
{
|
||||
var clean = GameSource.Clean(dir);
|
||||
var folder = clean.Length == 0 ? Romfs : Full(clean);
|
||||
if (!Directory.Exists(folder))
|
||||
yield break;
|
||||
var prefix = clean.Length == 0 ? "" : clean + "/";
|
||||
foreach (var d in Directory.EnumerateDirectories(folder))
|
||||
yield return prefix + Path.GetFileName(d) + "/";
|
||||
foreach (var f in Directory.EnumerateFiles(folder))
|
||||
yield return prefix + Path.GetFileName(f);
|
||||
}
|
||||
|
||||
public string? Stamp(string romfsPath)
|
||||
{
|
||||
var info = new FileInfo(Full(romfsPath));
|
||||
return info.Exists ? info.Length + "-" + info.LastWriteTimeUtc.Ticks : null;
|
||||
}
|
||||
|
||||
public void Dispose() { }
|
||||
}
|
||||
150
launcher/Core/GameFiles/GameMedia.cs
Normal file
150
launcher/Core/GameFiles/GameMedia.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
// What the launcher shows and plays from the player's own game: the title art as PNG and the music and interface sounds as
|
||||
// WAV, decoded at run time from their .nup or game folder (DESIGN.md section 10 lists the sounds and where each plays).
|
||||
// Results are kept as files in a cache folder the caller gives (in the user's own app data, never the repository or the
|
||||
// install folder), keyed by a cheap identity of the source files, so the second start of the launcher reads them back at
|
||||
// once and a changed game file (another dump, an update, a mod) is decoded again.
|
||||
using System.Collections.Concurrent;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Triforce.Core.GameFiles;
|
||||
|
||||
/// <summary>The game's art and audio for the launcher, decoded on first use and cached. Thread-safe: two calls for the
|
||||
/// same item decode it once; different items decode side by side.</summary>
|
||||
public sealed class GameMedia
|
||||
{
|
||||
/// <summary>Changes whenever a decoder's output would change, so older cache files are not used.</summary>
|
||||
const string CacheVersion = "gamemedia-1";
|
||||
|
||||
/// <summary>The title screen's texture archive.</summary>
|
||||
public const string TitleArchive = "UI/Layout2Archive/Title.Product.100.Nin_Ounce_NVN2.bntx.zs";
|
||||
|
||||
/// <summary>The textures the launcher shows (the logo set): the logo, its aura, the button ornament and the logo's glow.</summary>
|
||||
public static readonly IReadOnlyList<string> LogoSet = new[] { "Logo_USEU_00", "TitleLogoAura_00", "TitleBtnDeco_00", "Logo_GlowL_USEU_00" };
|
||||
|
||||
/// <summary>The sounds by the launcher's name for them, each the game's stream file(s) mixed (DESIGN.md section 10).</summary>
|
||||
public static readonly IReadOnlyDictionary<string, string[]> Sounds = new Dictionary<string, string[]>
|
||||
{
|
||||
// music
|
||||
["saria"] = Enumerable.Range(0, 8).Select(i => "BGM_LostWood_" + i).ToArray(), // the 8 layers at once: the whole song
|
||||
["fairy"] = new[] { "BGM_GreatFairyFountain" },
|
||||
["temple"] = new[] { "BGM_ShrineOfTime_Child" },
|
||||
["itemget"] = new[] { "BGM_ItemGet_Large" },
|
||||
// interface sounds
|
||||
["cursor"] = new[] { "SE_Sys_SysMenu_Cursor00" },
|
||||
["tab_l"] = new[] { "SE_Sys_MinusMenu_TabMove_L00" },
|
||||
["tab_r"] = new[] { "SE_Sys_MinusMenu_TabMove_R00" },
|
||||
["decide"] = new[] { "SE_Sys_SysMenu_DecideS00" },
|
||||
["decide_l"] = new[] { "SE_Sys_SysMenu_DecideL00" },
|
||||
["cancel"] = new[] { "SE_Sys_Cancel00" },
|
||||
["open"] = new[] { "SE_Sys_PlusMenu_Open00" },
|
||||
["close"] = new[] { "SE_Sys_PlusMenu_Close00" },
|
||||
["success"] = new[] { "SE_Sys_OcarinaWindow_Success00" },
|
||||
["error"] = new[] { "SE_Sys_SysMenu_Error00" },
|
||||
};
|
||||
|
||||
/// <summary>A stream file's romfs path.</summary>
|
||||
public static string StreamPath(string name) => "Sound/Resource/Stream/" + name + ".bwav";
|
||||
|
||||
readonly IGameSource source;
|
||||
readonly string cacheDir;
|
||||
readonly Zs zs;
|
||||
readonly ConcurrentDictionary<string, Lazy<Bntx>> archives = new(StringComparer.Ordinal);
|
||||
// (one lock per cache file, for the whole process: two instances on the same cache folder decode an item once)
|
||||
static readonly ConcurrentDictionary<string, object> gates = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public GameMedia(IGameSource source, string cacheDir)
|
||||
{
|
||||
this.source = source;
|
||||
this.cacheDir = Path.GetFullPath(cacheDir);
|
||||
zs = new Zs(source);
|
||||
}
|
||||
|
||||
public IGameSource Source => source;
|
||||
|
||||
/// <summary>A texture of the title archive as a PNG file's bytes.</summary>
|
||||
public byte[] Png(string textureName) => Png(TitleArchive, textureName);
|
||||
|
||||
/// <summary>A texture of any BNTX archive in the romfs (a .bntx.zs or a plain .bntx) as a PNG file's bytes.</summary>
|
||||
public byte[] Png(string archive, string textureName) => File.ReadAllBytes(PngFile(archive, textureName));
|
||||
|
||||
/// <summary>A sound (one of <see cref="Sounds"/>' names) as a WAV file's bytes.</summary>
|
||||
public byte[] Wav(string id) => File.ReadAllBytes(WavFile(id));
|
||||
|
||||
/// <summary>The cache file holding the texture's PNG, decoded first when it is not there (for a page that loads it by
|
||||
/// path or URL).</summary>
|
||||
public string PngFile(string archive, string textureName) =>
|
||||
Cached(Safe(textureName) + ".png", new[] { archive, Zs.DictionaryPack }, () =>
|
||||
{
|
||||
var bntx = Archive(archive);
|
||||
var texture = bntx.Find(textureName)
|
||||
?? throw new KeyNotFoundException("The game's " + archive + " has no texture " + textureName);
|
||||
var rgba = bntx.DecodeRgba(texture);
|
||||
return GameFiles.Png.Encode(rgba, texture.Width, texture.Height);
|
||||
});
|
||||
|
||||
public string PngFile(string textureName) => PngFile(TitleArchive, textureName);
|
||||
|
||||
/// <summary>The cache file holding the sound's WAV, decoded first when it is not there.</summary>
|
||||
public string WavFile(string id)
|
||||
{
|
||||
if (!Sounds.TryGetValue(id, out var names))
|
||||
throw new KeyNotFoundException("No sound called " + id + " (known: " + string.Join(", ", Sounds.Keys) + ")");
|
||||
var paths = names.Select(StreamPath).ToArray();
|
||||
return Cached(Safe(id) + ".wav", paths, () =>
|
||||
{
|
||||
// (the layers of a mixed song are read and decoded side by side)
|
||||
var tracks = new Pcm[paths.Length];
|
||||
Parallel.For(0, paths.Length, i => tracks[i] = Bwav.Decode(source.Read(paths[i])));
|
||||
return Bwav.ToWav(Bwav.Mix(tracks));
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>The names of the textures in an archive (to check a name, or to pick another piece of art).</summary>
|
||||
public IReadOnlyList<BntxTexture> Textures(string archive = TitleArchive) => Archive(archive).Textures;
|
||||
|
||||
/// <summary>Lets go of the unpacked archives kept for further textures (about 30 MB for the title archive).</summary>
|
||||
public void Trim() => archives.Clear();
|
||||
|
||||
Bntx Archive(string archive) => archives.GetOrAdd(archive, a => new Lazy<Bntx>(() =>
|
||||
new Bntx(a.EndsWith(".zs", StringComparison.Ordinal) ? zs.Unpack(a) : source.Read(a)))).Value;
|
||||
|
||||
/// <summary>
|
||||
/// The cache file for an item: "<name>-<key>.<ext>", the key a hash of the decoders' version and the
|
||||
/// source files' stamps. Made by `make` when it is not there (written beside and then moved into place, so a reader
|
||||
/// never sees half a file); other versions of the same item are removed then.
|
||||
/// </summary>
|
||||
string Cached(string fileName, string[] inputs, Func<byte[]> make)
|
||||
{
|
||||
var identity = new StringBuilder(CacheVersion).Append('|').Append(fileName);
|
||||
foreach (var input in inputs)
|
||||
identity.Append('|').Append(input).Append('=').Append(source.Stamp(input)
|
||||
?? throw new FileNotFoundException("The game has no " + input + " (" + source.Describe + ")", input));
|
||||
var key = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(identity.ToString())), 0, 8).ToLowerInvariant();
|
||||
var stem = Path.GetFileNameWithoutExtension(fileName);
|
||||
var ext = Path.GetExtension(fileName);
|
||||
var path = Path.Combine(cacheDir, stem + "-" + key + ext);
|
||||
|
||||
lock (gates.GetOrAdd(path, _ => new object()))
|
||||
{
|
||||
if (File.Exists(path))
|
||||
return path;
|
||||
var bytes = make();
|
||||
Directory.CreateDirectory(cacheDir);
|
||||
var part = path + "." + Environment.ProcessId + ".part";
|
||||
File.WriteAllBytes(part, bytes);
|
||||
File.Move(part, path, overwrite: true);
|
||||
foreach (var old in Directory.EnumerateFiles(cacheDir, stem + "-*" + ext))
|
||||
if (!string.Equals(old, path, StringComparison.OrdinalIgnoreCase) && Path.GetFileName(old).Length == Path.GetFileName(path).Length)
|
||||
try { File.Delete(old); } catch (IOException) { } catch (UnauthorizedAccessException) { }
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A name made safe as a file name.</summary>
|
||||
static string Safe(string name)
|
||||
{
|
||||
var chars = name.Select(c => char.IsAsciiLetterOrDigit(c) || c is '_' or '.' ? c : '_').ToArray();
|
||||
return new string(chars);
|
||||
}
|
||||
}
|
||||
46
launcher/Core/GameFiles/IGameSource.cs
Normal file
46
launcher/Core/GameFiles/IGameSource.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
// The player's own game files, wherever they are: a .nup dump (NupSource) or a folder holding romfs/ and exefs/
|
||||
// (FolderSource). Everything the launcher shows from the game (title art, music, sounds) is read through this, at run time,
|
||||
// from the player's copy. Nothing read here is ever written into the repository or a release.
|
||||
namespace Triforce.Core.GameFiles;
|
||||
|
||||
/// <summary>
|
||||
/// A read-only view of a game's romfs. Paths are romfs paths with "/" between the parts and no leading slash, for example
|
||||
/// "UI/Layout2Archive/Title.Product.100.Nin_Ounce_NVN2.bntx.zs". All members are safe to call from several threads.
|
||||
/// </summary>
|
||||
public interface IGameSource : IDisposable
|
||||
{
|
||||
/// <summary>Whether the romfs has this file.</summary>
|
||||
bool Exists(string romfsPath);
|
||||
|
||||
/// <summary>The file's bytes, as the game sees them (a .zs file stays zstd: see <see cref="Zs"/>). Throws
|
||||
/// FileNotFoundException when there is no such file.</summary>
|
||||
byte[] Read(string romfsPath);
|
||||
|
||||
/// <summary>What a romfs folder holds, one level deep, as romfs paths: files as they are, folders with a trailing "/".
|
||||
/// "" lists the romfs root. Empty for a folder that does not exist.</summary>
|
||||
IEnumerable<string> List(string dir);
|
||||
|
||||
/// <summary>A short description for logs: what kind of source, and where.</summary>
|
||||
string Describe { get; }
|
||||
|
||||
/// <summary>A cheap identity of one file (size and time, or where it sits in the package): it changes when the file
|
||||
/// can have changed. Used to key caches. Null when there is no such file.</summary>
|
||||
string? Stamp(string romfsPath);
|
||||
}
|
||||
|
||||
/// <summary>Opening a source from whatever the player dropped or chose.</summary>
|
||||
public static class GameSource
|
||||
{
|
||||
/// <summary>A .nup file, a game folder (holding romfs/) or a romfs folder itself.</summary>
|
||||
public static IGameSource Open(string path)
|
||||
{
|
||||
if (File.Exists(path))
|
||||
return new NupSource(path);
|
||||
if (Directory.Exists(path))
|
||||
return new FolderSource(path);
|
||||
throw new FileNotFoundException("No game file or folder at " + path, path);
|
||||
}
|
||||
|
||||
/// <summary>A romfs path made uniform: "/" between the parts, no leading or trailing "/".</summary>
|
||||
internal static string Clean(string romfsPath) => romfsPath.Replace('\\', '/').Trim('/');
|
||||
}
|
||||
367
launcher/Core/GameFiles/NupSource.cs
Normal file
367
launcher/Core/GameFiles/NupSource.cs
Normal file
@@ -0,0 +1,367 @@
|
||||
// A .nup package: a decrypted Switch 2 dump in one file, holding one or more titles (the game, its update), each with an
|
||||
// exefs/ and a romfs/ tree. Ported from nx2/nup.py (the reader the build uses); the layout, as worked out there
|
||||
// (little-endian):
|
||||
//
|
||||
// 0x000 "NUP\x01"; u64 TOC offset at +0x08, u64 TOC size at +0x10, u64 blob offset at +0x38, u64 file size at +0x40
|
||||
// 0x080 "TOC\x01", u32 titles, u64 dirs, u64 files, u64 strings offset (from 0x80), u64 strings size
|
||||
// 0x0b0 titles, 0x40 each: u64 title id, u32 root dir, u32 exefs dir, u32 romfs dir, u32 version, ...
|
||||
// then dirs, 24 bytes each: u32 name offset, u32 name length, u32 first child, u32 children, u64 0
|
||||
// then files, 80 bytes each: u32 name offset, u32 name length, u64 size, u64 data offset, u64 stored size,
|
||||
// 32-byte hash, u8 codec (0 raw, 1 zstd), u8 flags, u16 reserved, 12 bytes reserved
|
||||
// then the child list, u32 each: a node index; below the dir count a dir, else (index - dirs) a file
|
||||
// then the name strings (not 0-terminated)
|
||||
//
|
||||
// A file's data is stored as is at its data offset, or as a zstd frame whose content is the file (codec 1; in older
|
||||
// packages codec 0 with a frame whose stated content size is the file's size). A romfs .zs file is zstd in the game too:
|
||||
// that one is not unpacked here (Zs does it, with the game's dictionaries).
|
||||
//
|
||||
// The package is opened for reading only (FileAccess.Read, FileShare.Read) and never written. Opening reads the header and
|
||||
// maps the index; no file data is read until a file is asked for.
|
||||
using System.IO.MemoryMappedFiles;
|
||||
using System.Text;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
using ZstdSharp;
|
||||
|
||||
namespace Triforce.Core.GameFiles;
|
||||
|
||||
/// <summary>One title in a .nup package: the base game or an update.</summary>
|
||||
public readonly record struct NupTitle(int Index, ulong Id, uint Version)
|
||||
{
|
||||
/// <summary>The title id as the Switch writes it (16 hex digits).</summary>
|
||||
public string IdHex => Id.ToString("x16");
|
||||
public override string ToString() => IdHex + " v" + Version;
|
||||
}
|
||||
|
||||
/// <summary>Reads romfs files out of a .nup package. The newest title (the update) is looked in first, then the others.</summary>
|
||||
public sealed class NupSource : IGameSource
|
||||
{
|
||||
static readonly byte[] ZstdMagic = { 0x28, 0xB5, 0x2F, 0xFD };
|
||||
const uint None = 0xFFFFFFFF;
|
||||
|
||||
readonly FileStream file;
|
||||
readonly SafeFileHandle handle;
|
||||
readonly MemoryMappedFile map;
|
||||
readonly MemoryMappedViewAccessor index;
|
||||
readonly long length;
|
||||
readonly DateTime written;
|
||||
readonly uint dirCount, fileCount;
|
||||
readonly long dirsAt, filesAt, kidsAt, stringsAt;
|
||||
readonly (uint Root, uint Exefs, uint Romfs)[] trees;
|
||||
// (each folder's children by name, made the first time the folder is looked into)
|
||||
readonly Dictionary<uint, Dictionary<string, uint>> children = new();
|
||||
|
||||
/// <summary>The package's file.</summary>
|
||||
public string Path { get; }
|
||||
|
||||
/// <summary>The titles in the package, newest version first (the update before the base game).</summary>
|
||||
public IReadOnlyList<NupTitle> Titles { get; }
|
||||
|
||||
/// <summary>The newest title's version: the game version the player has (for this game, 131072 is 2.0.0).</summary>
|
||||
public uint Version => Titles[0].Version;
|
||||
|
||||
public NupSource(string path)
|
||||
{
|
||||
Path = System.IO.Path.GetFullPath(path);
|
||||
file = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read, 1, FileOptions.RandomAccess);
|
||||
try
|
||||
{
|
||||
handle = file.SafeFileHandle;
|
||||
var info = new FileInfo(Path);
|
||||
length = file.Length;
|
||||
written = info.LastWriteTimeUtc;
|
||||
|
||||
var head = new byte[0xB0];
|
||||
if (RandomAccess.Read(handle, head, 0) != head.Length || !head.AsSpan(0, 4).SequenceEqual("NUP\x01"u8))
|
||||
throw new InvalidDataException(Path + " is not a .nup package");
|
||||
var declared = (long)BitConverter.ToUInt64(head, 0x40);
|
||||
// (a package that ends early, a download or copy that did not finish: said now, with the sizes)
|
||||
if (declared != 0 && length < declared)
|
||||
throw new InvalidDataException(string.Format("The .nup file is incomplete: it is {0} MB, its header says {1} MB. " +
|
||||
"Copy or download it again.", length >> 20, declared >> 20));
|
||||
if (!head.AsSpan(0x80, 4).SequenceEqual("TOC\x01"u8))
|
||||
throw new InvalidDataException(Path + ": no table of contents at 0x80");
|
||||
var titleCount = BitConverter.ToUInt32(head, 0x84);
|
||||
var nd = BitConverter.ToUInt64(head, 0x88);
|
||||
var nf = BitConverter.ToUInt64(head, 0x90);
|
||||
var stringsOffset = BitConverter.ToUInt64(head, 0x98);
|
||||
var stringsSize = BitConverter.ToUInt64(head, 0xA0);
|
||||
if (titleCount == 0 || titleCount > 64 || nd > int.MaxValue || nf > int.MaxValue)
|
||||
throw new InvalidDataException(Path + ": the table of contents makes no sense");
|
||||
dirCount = (uint)nd;
|
||||
fileCount = (uint)nf;
|
||||
dirsAt = 0xB0 + 0x40L * titleCount;
|
||||
filesAt = dirsAt + 24L * dirCount;
|
||||
kidsAt = filesAt + 80L * fileCount;
|
||||
stringsAt = 0x80 + (long)stringsOffset;
|
||||
var indexEnd = stringsAt + (long)stringsSize;
|
||||
if (stringsAt < kidsAt || indexEnd > length)
|
||||
throw new InvalidDataException(Path + ": the table of contents runs past the end of the file");
|
||||
|
||||
// (the index is mapped, not read: only the pages a lookup touches are read from the disk)
|
||||
map = MemoryMappedFile.CreateFromFile(file, null, 0, MemoryMappedFileAccess.Read, HandleInheritability.None, leaveOpen: true);
|
||||
index = map.CreateViewAccessor(0, indexEnd, MemoryMappedFileAccess.Read);
|
||||
|
||||
var titles = new List<NupTitle>();
|
||||
trees = new (uint, uint, uint)[titleCount];
|
||||
for (var i = 0; i < titleCount; i++)
|
||||
{
|
||||
var at = 0xB0 + 0x40L * i;
|
||||
titles.Add(new NupTitle(i, index.ReadUInt64(at), index.ReadUInt32(at + 20)));
|
||||
trees[i] = (index.ReadUInt32(at + 8), index.ReadUInt32(at + 12), index.ReadUInt32(at + 16));
|
||||
}
|
||||
titles.Sort((a, b) => b.Version.CompareTo(a.Version));
|
||||
Titles = titles;
|
||||
}
|
||||
catch
|
||||
{
|
||||
index?.Dispose();
|
||||
map?.Dispose();
|
||||
file.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public string Describe => string.Format("nup {0} ({1:0.00} GB; titles {2})", Path, length / 1e9, string.Join(", ", Titles));
|
||||
|
||||
// ---- the index ----
|
||||
|
||||
string Name(uint offset, uint count)
|
||||
{
|
||||
var bytes = new byte[count];
|
||||
index.ReadArray(stringsAt + offset, bytes, 0, (int)count);
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
|
||||
string NodeName(uint node) => node < dirCount
|
||||
? Name(index.ReadUInt32(dirsAt + 24L * node), index.ReadUInt32(dirsAt + 24L * node + 4))
|
||||
: Name(index.ReadUInt32(filesAt + 80L * (node - dirCount)), index.ReadUInt32(filesAt + 80L * (node - dirCount) + 4));
|
||||
|
||||
/// <summary>A folder's children by name (node indexes).</summary>
|
||||
Dictionary<string, uint> Children(uint dir)
|
||||
{
|
||||
lock (children)
|
||||
{
|
||||
if (children.TryGetValue(dir, out var known))
|
||||
return known;
|
||||
var first = index.ReadUInt32(dirsAt + 24L * dir + 8);
|
||||
var count = index.ReadUInt32(dirsAt + 24L * dir + 12);
|
||||
var kids = new uint[count];
|
||||
index.ReadArray(kidsAt + 4L * first, kids, 0, (int)count);
|
||||
var names = new Dictionary<string, uint>((int)count, StringComparer.Ordinal);
|
||||
foreach (var kid in kids)
|
||||
if (kid < dirCount + fileCount)
|
||||
names.TryAdd(NodeName(kid), kid);
|
||||
children[dir] = names;
|
||||
return names;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The node a romfs path names in one title's romfs (a dir below dirCount, a file above), or None.</summary>
|
||||
uint Find(uint romfsDir, string romfsPath)
|
||||
{
|
||||
var node = romfsDir;
|
||||
var clean = GameSource.Clean(romfsPath);
|
||||
if (clean.Length == 0)
|
||||
return node;
|
||||
foreach (var part in clean.Split('/'))
|
||||
{
|
||||
if (node >= dirCount || !Children(node).TryGetValue(part, out node))
|
||||
return None;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <summary>The file record for a romfs path, from the newest title that has it.</summary>
|
||||
bool FindFile(string romfsPath, out uint fileIndex)
|
||||
{
|
||||
foreach (var t in Titles)
|
||||
{
|
||||
var romfs = trees[t.Index].Romfs;
|
||||
if (romfs == None || romfs >= dirCount)
|
||||
continue;
|
||||
var node = Find(romfs, romfsPath);
|
||||
if (node != None && node >= dirCount)
|
||||
{
|
||||
fileIndex = node - dirCount;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
fileIndex = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Exists(string romfsPath) => FindFile(romfsPath, out _);
|
||||
|
||||
public IEnumerable<string> List(string dir)
|
||||
{
|
||||
var clean = GameSource.Clean(dir);
|
||||
var prefix = clean.Length == 0 ? "" : clean + "/";
|
||||
var seen = new SortedSet<string>(StringComparer.Ordinal);
|
||||
foreach (var t in Titles)
|
||||
{
|
||||
var romfs = trees[t.Index].Romfs;
|
||||
if (romfs == None || romfs >= dirCount)
|
||||
continue;
|
||||
var node = Find(romfs, clean);
|
||||
if (node == None || node >= dirCount)
|
||||
continue;
|
||||
foreach (var (name, kid) in Children(node))
|
||||
seen.Add(prefix + name + (kid < dirCount ? "/" : ""));
|
||||
}
|
||||
return seen;
|
||||
}
|
||||
|
||||
public string? Stamp(string romfsPath)
|
||||
{
|
||||
if (!FindFile(romfsPath, out var i))
|
||||
return null;
|
||||
var at = filesAt + 80L * i;
|
||||
return string.Format("{0}-{1}-{2}-{3}", length, written.Ticks, index.ReadUInt64(at + 16), index.ReadUInt64(at + 8));
|
||||
}
|
||||
|
||||
// ---- the data ----
|
||||
|
||||
public byte[] Read(string romfsPath)
|
||||
{
|
||||
if (!FindFile(romfsPath, out var i))
|
||||
throw new FileNotFoundException("The game has no " + romfsPath + " (" + Path + ")", romfsPath);
|
||||
var at = filesAt + 80L * i;
|
||||
var size = (long)index.ReadUInt64(at + 8);
|
||||
var offset = (long)index.ReadUInt64(at + 16);
|
||||
var stored = (long)index.ReadUInt64(at + 24);
|
||||
var codec = index.ReadByte(at + 64);
|
||||
if (size == 0)
|
||||
return Array.Empty<byte>();
|
||||
if (size > int.MaxValue || offset < 0 || offset > length)
|
||||
throw new InvalidDataException(romfsPath + ": the package's record for it makes no sense");
|
||||
|
||||
if (codec == 1)
|
||||
{
|
||||
// (the package states the frame's own size: exactly that is read)
|
||||
if (stored <= 0 || stored > length - offset)
|
||||
throw new InvalidDataException(romfsPath + ": its compressed data runs past the end of the package");
|
||||
var frame = ReadAt(offset, (int)stored);
|
||||
var output = new byte[size];
|
||||
using var z = new Decompressor();
|
||||
if (z.Unwrap(frame, output) != size)
|
||||
throw new InvalidDataException(romfsPath + ": it unpacked to the wrong size");
|
||||
return output;
|
||||
}
|
||||
if (IsPackedLegacy(romfsPath, offset, size))
|
||||
return UnpackLegacy(romfsPath, offset, size);
|
||||
if (size > length - offset)
|
||||
throw new InvalidDataException(romfsPath + ": its data runs past the end of the package (the .nup is incomplete)");
|
||||
return ReadAt(offset, (int)size);
|
||||
}
|
||||
|
||||
byte[] ReadAt(long offset, int count)
|
||||
{
|
||||
var data = new byte[count];
|
||||
var done = 0;
|
||||
while (done < count)
|
||||
{
|
||||
var n = RandomAccess.Read(handle, data.AsSpan(done), offset + done);
|
||||
if (n <= 0)
|
||||
throw new EndOfStreamException("The .nup file ends early: it is incomplete. Copy or download it again.");
|
||||
done += n;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>The content size a zstd frame header states, or -1 when it states none (nup.py frame_content_size).</summary>
|
||||
static long FrameContentSize(ReadOnlySpan<byte> head)
|
||||
{
|
||||
if (head.Length < 18 || !head[..4].SequenceEqual(ZstdMagic))
|
||||
return -1;
|
||||
var fhd = head[4];
|
||||
var single = (fhd >> 5) & 1;
|
||||
var at = 5 + (single != 0 ? 0 : 1) + new[] { 0, 1, 2, 4 }[fhd & 3];
|
||||
var n = new[] { single != 0 ? 1 : 0, 2, 4, 8 }[fhd >> 6];
|
||||
if (n == 0)
|
||||
return -1;
|
||||
long size = 0;
|
||||
for (var k = n - 1; k >= 0; k--)
|
||||
size = size << 8 | head[at + k];
|
||||
return n == 2 ? size + 256 : size;
|
||||
}
|
||||
|
||||
/// <summary>Older packages (codec 0) may still keep a file as a zstd frame: nup.py is_packed, the same rules. A .zs file
|
||||
/// is zstd in the game too; it counts as packed only when what the frame holds is zstd again.</summary>
|
||||
bool IsPackedLegacy(string name, long offset, long size)
|
||||
{
|
||||
var head = new byte[18];
|
||||
if (RandomAccess.Read(handle, head, offset) < 18 || !head.AsSpan(0, 4).SequenceEqual(ZstdMagic))
|
||||
return false;
|
||||
var content = FrameContentSize(head);
|
||||
if (name.EndsWith(".zs", StringComparison.Ordinal) && (content < 0 || content == size))
|
||||
return content == size && InnerIsZstd(offset, size);
|
||||
if (content >= 0)
|
||||
return content == size;
|
||||
try
|
||||
{
|
||||
return UnpackLegacy(name, offset, size).Length == size;
|
||||
}
|
||||
catch (Exception e) when (e is ZstdException or InvalidDataException or IOException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool InnerIsZstd(long offset, long size)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var s = new DecompressionStream(new WindowStream(handle, offset, Math.Min(size, 1 << 20)));
|
||||
var four = new byte[4];
|
||||
return s.ReadAtLeast(four, 4, throwOnEndOfStream: false) == 4 && four.AsSpan().SequenceEqual(ZstdMagic);
|
||||
}
|
||||
catch (Exception e) when (e is ZstdException or IOException)
|
||||
{
|
||||
return false; // (made with one of the game's dictionaries: the game's own file)
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A frame of unknown length: decompressed until the file's size is reached (the frame cannot be longer than
|
||||
/// the file plus 64 KB, as nup.py reads it).</summary>
|
||||
byte[] UnpackLegacy(string name, long offset, long size)
|
||||
{
|
||||
using var s = new DecompressionStream(new WindowStream(handle, offset, Math.Min(size + (1 << 16), length - offset)));
|
||||
var output = new byte[size];
|
||||
var got = s.ReadAtLeast(output, output.Length, throwOnEndOfStream: false);
|
||||
if (got != size)
|
||||
throw new InvalidDataException(string.Format("{0}: unpacked to {1} bytes, {2} expected", name, got, size));
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>A read-only stream over part of the package, read by position (safe beside other threads' reads).</summary>
|
||||
sealed class WindowStream(SafeFileHandle handle, long start, long count) : Stream
|
||||
{
|
||||
long position;
|
||||
public override bool CanRead => true;
|
||||
public override bool CanSeek => false;
|
||||
public override bool CanWrite => false;
|
||||
public override long Length => count;
|
||||
public override long Position { get => position; set => throw new NotSupportedException(); }
|
||||
public override int Read(byte[] buffer, int offset, int n) => Read(buffer.AsSpan(offset, n));
|
||||
public override int Read(Span<byte> buffer)
|
||||
{
|
||||
var n = (int)Math.Min(buffer.Length, count - position);
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
n = RandomAccess.Read(handle, buffer[..n], start + position);
|
||||
position += n;
|
||||
return n;
|
||||
}
|
||||
public override void Flush() { }
|
||||
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
|
||||
public override void SetLength(long value) => throw new NotSupportedException();
|
||||
public override void Write(byte[] buffer, int offset, int n) => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
index.Dispose();
|
||||
map.Dispose();
|
||||
file.Dispose();
|
||||
}
|
||||
}
|
||||
149
launcher/Core/GameFiles/Png.cs
Normal file
149
launcher/Core/GameFiles/Png.cs
Normal file
@@ -0,0 +1,149 @@
|
||||
// A minimal PNG writer and reader for 8-bit RGBA images (the reader is only for comparing with reference images and for
|
||||
// tests: it takes the PNGs this writer and common tools make, colour type 6 or 2, 8 bits, not interlaced).
|
||||
using System.Buffers.Binary;
|
||||
using System.IO.Compression;
|
||||
|
||||
namespace Triforce.Core.GameFiles;
|
||||
|
||||
public static class Png
|
||||
{
|
||||
static readonly byte[] Signature = { 0x89, (byte)'P', (byte)'N', (byte)'G', 0x0D, 0x0A, 0x1A, 0x0A };
|
||||
|
||||
/// <summary>An RGBA8 image (width * height * 4 bytes, rows top to bottom) as a PNG file's bytes. Each row is filtered
|
||||
/// with "Sub", which keeps the zlib step quick and the file reasonably small.</summary>
|
||||
public static byte[] Encode(ReadOnlySpan<byte> rgba, int width, int height, CompressionLevel level = CompressionLevel.Fastest)
|
||||
{
|
||||
if (rgba.Length < (long)width * height * 4)
|
||||
throw new ArgumentException("the image is smaller than width * height * 4 bytes");
|
||||
var stride = width * 4;
|
||||
var raw = new byte[(stride + 1) * height];
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
var row = rgba.Slice(y * stride, stride);
|
||||
var at = y * (stride + 1);
|
||||
raw[at] = 1; // (filter: Sub)
|
||||
row[..4].CopyTo(raw.AsSpan(at + 1));
|
||||
for (var x = 4; x < stride; x++)
|
||||
raw[at + 1 + x] = (byte)(row[x] - row[x - 4]);
|
||||
}
|
||||
using var idat = new MemoryStream();
|
||||
using (var z = new ZLibStream(idat, level, leaveOpen: true))
|
||||
z.Write(raw);
|
||||
|
||||
using var png = new MemoryStream();
|
||||
png.Write(Signature);
|
||||
Span<byte> header = stackalloc byte[13];
|
||||
BinaryPrimitives.WriteInt32BigEndian(header, width);
|
||||
BinaryPrimitives.WriteInt32BigEndian(header[4..], height);
|
||||
header[8] = 8; // bit depth
|
||||
header[9] = 6; // RGBA
|
||||
header[10] = 0; // deflate
|
||||
header[11] = 0; // adaptive filtering
|
||||
header[12] = 0; // not interlaced
|
||||
Chunk(png, "IHDR", header);
|
||||
Chunk(png, "IDAT", idat.GetBuffer().AsSpan(0, (int)idat.Length));
|
||||
Chunk(png, "IEND", ReadOnlySpan<byte>.Empty);
|
||||
return png.ToArray();
|
||||
}
|
||||
|
||||
static void Chunk(Stream s, string type, ReadOnlySpan<byte> payload)
|
||||
{
|
||||
Span<byte> four = stackalloc byte[4];
|
||||
BinaryPrimitives.WriteInt32BigEndian(four, payload.Length);
|
||||
s.Write(four);
|
||||
Span<byte> tag = stackalloc byte[4];
|
||||
for (var i = 0; i < 4; i++)
|
||||
tag[i] = (byte)type[i];
|
||||
s.Write(tag);
|
||||
s.Write(payload);
|
||||
var crc = Crc(Crc(0xFFFFFFFF, tag), payload) ^ 0xFFFFFFFF;
|
||||
BinaryPrimitives.WriteUInt32BigEndian(four, crc);
|
||||
s.Write(four);
|
||||
}
|
||||
|
||||
static readonly uint[] CrcTable = Enumerable.Range(0, 256).Select(n =>
|
||||
{
|
||||
var c = (uint)n;
|
||||
for (var k = 0; k < 8; k++)
|
||||
c = (c & 1) != 0 ? 0xEDB88320 ^ (c >> 1) : c >> 1;
|
||||
return c;
|
||||
}).ToArray();
|
||||
|
||||
/// <summary>The CRC-32 PNG chunks carry (zlib's), continued over more bytes.</summary>
|
||||
static uint Crc(uint crc, ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
foreach (var b in bytes)
|
||||
crc = CrcTable[(crc ^ b) & 0xFF] ^ (crc >> 8);
|
||||
return crc;
|
||||
}
|
||||
|
||||
/// <summary>A PNG decoded to RGBA8 (8-bit RGBA or RGB, not interlaced).</summary>
|
||||
public static (int Width, int Height, byte[] Rgba) Decode(byte[] png)
|
||||
{
|
||||
if (png.Length < 33 || !png.AsSpan(0, 8).SequenceEqual(Signature))
|
||||
throw new InvalidDataException("not a PNG file");
|
||||
int width = 0, height = 0, channels = 4;
|
||||
using var idat = new MemoryStream();
|
||||
for (var at = 8; at + 8 <= png.Length;)
|
||||
{
|
||||
var n = BinaryPrimitives.ReadInt32BigEndian(png.AsSpan(at));
|
||||
var type = System.Text.Encoding.ASCII.GetString(png, at + 4, 4);
|
||||
var body = png.AsSpan(at + 8, n);
|
||||
if (type == "IHDR")
|
||||
{
|
||||
width = BinaryPrimitives.ReadInt32BigEndian(body);
|
||||
height = BinaryPrimitives.ReadInt32BigEndian(body[4..]);
|
||||
if (body[8] != 8 || (body[9] != 6 && body[9] != 2) || body[12] != 0)
|
||||
throw new NotSupportedException("only 8-bit RGB/RGBA PNGs without interlacing are read");
|
||||
channels = body[9] == 6 ? 4 : 3;
|
||||
}
|
||||
else if (type == "IDAT")
|
||||
idat.Write(body);
|
||||
else if (type == "IEND")
|
||||
break;
|
||||
at += 12 + n;
|
||||
}
|
||||
idat.Position = 0;
|
||||
var stride = width * channels;
|
||||
var raw = new byte[(stride + 1) * height];
|
||||
using (var z = new ZLibStream(idat, CompressionMode.Decompress))
|
||||
z.ReadExactly(raw);
|
||||
|
||||
var pixels = new byte[stride * height];
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
var filter = raw[y * (stride + 1)];
|
||||
var src = raw.AsSpan(y * (stride + 1) + 1, stride);
|
||||
var dst = pixels.AsSpan(y * stride, stride);
|
||||
var up = y > 0 ? pixels.AsSpan((y - 1) * stride, stride) : new byte[stride];
|
||||
for (var x = 0; x < stride; x++)
|
||||
{
|
||||
int a = x >= channels ? dst[x - channels] : 0, b = up[x], c = x >= channels ? up[x - channels] : 0;
|
||||
dst[x] = (byte)(src[x] + filter switch
|
||||
{
|
||||
0 => 0,
|
||||
1 => a,
|
||||
2 => b,
|
||||
3 => (a + b) / 2,
|
||||
4 => Paeth(a, b, c),
|
||||
_ => throw new InvalidDataException("PNG filter " + filter),
|
||||
});
|
||||
}
|
||||
}
|
||||
if (channels == 4)
|
||||
return (width, height, pixels);
|
||||
var rgba = new byte[width * height * 4];
|
||||
for (var i = 0; i < width * height; i++)
|
||||
{
|
||||
pixels.AsSpan(i * 3, 3).CopyTo(rgba.AsSpan(i * 4));
|
||||
rgba[i * 4 + 3] = 255;
|
||||
}
|
||||
return (width, height, rgba);
|
||||
}
|
||||
|
||||
static int Paeth(int a, int b, int c)
|
||||
{
|
||||
int p = a + b - c, pa = Math.Abs(p - a), pb = Math.Abs(p - b), pc = Math.Abs(p - c);
|
||||
return pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
|
||||
}
|
||||
}
|
||||
123
launcher/Core/GameFiles/Zs.cs
Normal file
123
launcher/Core/GameFiles/Zs.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
// The game's .zs files: zstd frames made with one of three dictionaries that the game keeps in its romfs, in
|
||||
// "Pack/ZsDic.pack.zs" (a zstd frame without a dictionary holding a SARC archive of the dictionaries). Ported from the
|
||||
// lab's research/scratch/t09_common.py (unzs, sarc).
|
||||
using System.Buffers.Binary;
|
||||
using System.Text;
|
||||
using ZstdSharp;
|
||||
|
||||
namespace Triforce.Core.GameFiles;
|
||||
|
||||
/// <summary>Unpacks romfs .zs files with the game's own dictionaries. Thread-safe.</summary>
|
||||
public sealed class Zs
|
||||
{
|
||||
public const string DictionaryPack = "Pack/ZsDic.pack.zs";
|
||||
|
||||
readonly IGameSource source;
|
||||
readonly Lazy<Dictionary<string, byte[]>> dictionaries;
|
||||
|
||||
public Zs(IGameSource source)
|
||||
{
|
||||
this.source = source;
|
||||
dictionaries = new Lazy<Dictionary<string, byte[]>>(() => Sarc(Decompress(source.Read(DictionaryPack), null)));
|
||||
}
|
||||
|
||||
/// <summary>The game's dictionaries by name (zs.zsdic, pack.zsdic, bcett.byml.zsdic).</summary>
|
||||
public IReadOnlyDictionary<string, byte[]> Dictionaries => dictionaries.Value;
|
||||
|
||||
/// <summary>The dictionary the game uses for a file, by its name (t09_common.unzs): bcett files have their own, .pack.zs
|
||||
/// archives theirs, everything else the general one.</summary>
|
||||
public static string DictionaryFor(string romfsPath)
|
||||
{
|
||||
var name = romfsPath[(romfsPath.LastIndexOf('/') + 1)..];
|
||||
return name.Contains("bcett", StringComparison.Ordinal) ? "bcett.byml.zsdic"
|
||||
: name.EndsWith(".pack.zs", StringComparison.Ordinal) ? "pack.zsdic" : "zs.zsdic";
|
||||
}
|
||||
|
||||
/// <summary>A romfs .zs file, read and unpacked.</summary>
|
||||
public byte[] Unpack(string romfsPath) => Unpack(romfsPath, source.Read(romfsPath));
|
||||
|
||||
/// <summary>A .zs file's bytes unpacked. The dictionary is the one the name calls for; when the frame names another
|
||||
/// dictionary by its id, that one is used instead.</summary>
|
||||
public byte[] Unpack(string romfsPath, byte[] data)
|
||||
{
|
||||
var dicts = dictionaries.Value;
|
||||
dicts.TryGetValue(DictionaryFor(romfsPath), out var dict);
|
||||
var wanted = FrameDictionaryId(data);
|
||||
if (wanted != 0 && (dict == null || DictionaryId(dict) != wanted))
|
||||
dict = dicts.Values.FirstOrDefault(d => DictionaryId(d) == wanted) ?? dict;
|
||||
return Decompress(data, dict);
|
||||
}
|
||||
|
||||
/// <summary>One zstd frame unpacked, with a dictionary or none.</summary>
|
||||
public static byte[] Decompress(ReadOnlySpan<byte> data, byte[]? dictionary)
|
||||
{
|
||||
using var z = new Decompressor();
|
||||
if (dictionary != null)
|
||||
z.LoadDictionary(dictionary);
|
||||
var size = Decompressor.GetDecompressedSize(data);
|
||||
if (size > 0 && size <= int.MaxValue)
|
||||
{
|
||||
var output = new byte[size];
|
||||
var n = z.Unwrap(data, output);
|
||||
return n == output.Length ? output : output[..n];
|
||||
}
|
||||
// (a frame that does not say how large it is: unpacked as a stream)
|
||||
using var stream = new DecompressionStream(new MemoryStream(data.ToArray()));
|
||||
if (dictionary != null)
|
||||
stream.LoadDictionary(dictionary);
|
||||
using var into = new MemoryStream();
|
||||
stream.CopyTo(into);
|
||||
return into.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>The dictionary id a zstd frame header names, 0 for none.</summary>
|
||||
static uint FrameDictionaryId(ReadOnlySpan<byte> frame)
|
||||
{
|
||||
if (frame.Length < 6 || BinaryPrimitives.ReadUInt32LittleEndian(frame) != 0xFD2FB528)
|
||||
return 0;
|
||||
var fhd = frame[4];
|
||||
var single = (fhd >> 5) & 1;
|
||||
var idBytes = new[] { 0, 1, 2, 4 }[fhd & 3];
|
||||
var at = 5 + (single != 0 ? 0 : 1);
|
||||
uint id = 0;
|
||||
for (var k = idBytes - 1; k >= 0; k--)
|
||||
id = id << 8 | (at + k < frame.Length ? frame[at + k] : 0u);
|
||||
return id;
|
||||
}
|
||||
|
||||
/// <summary>A zstd dictionary's id (after its magic 0xEC30A437), 0 for raw content.</summary>
|
||||
static uint DictionaryId(byte[] dict) =>
|
||||
dict.Length >= 8 && BinaryPrimitives.ReadUInt32LittleEndian(dict) == 0xEC30A437 ? BinaryPrimitives.ReadUInt32LittleEndian(dict.AsSpan(4)) : 0;
|
||||
|
||||
/// <summary>A SARC archive's files by name (by hash, as 8 hex digits, for one without a name). Little-endian SARC only,
|
||||
/// as this game's are.</summary>
|
||||
public static Dictionary<string, byte[]> Sarc(byte[] b)
|
||||
{
|
||||
if (b.Length < 0x20 || b[0] != 'S' || b[1] != 'A' || b[2] != 'R' || b[3] != 'C')
|
||||
throw new InvalidDataException("not a SARC archive");
|
||||
var dataAt = BinaryPrimitives.ReadInt32LittleEndian(b.AsSpan(0x0C));
|
||||
const int sfat = 0x14;
|
||||
var count = BinaryPrimitives.ReadUInt16LittleEndian(b.AsSpan(sfat + 6));
|
||||
var namesAt = sfat + 12 + 16 * count + 8;
|
||||
var files = new Dictionary<string, byte[]>(StringComparer.Ordinal);
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var node = b.AsSpan(sfat + 12 + 16 * i);
|
||||
var hash = BinaryPrimitives.ReadUInt32LittleEndian(node);
|
||||
var attr = BinaryPrimitives.ReadUInt32LittleEndian(node[4..]);
|
||||
var start = BinaryPrimitives.ReadInt32LittleEndian(node[8..]);
|
||||
var end = BinaryPrimitives.ReadInt32LittleEndian(node[12..]);
|
||||
string name;
|
||||
if (attr >> 24 != 0)
|
||||
{
|
||||
var at = namesAt + (int)(attr & 0xFFFF) * 4;
|
||||
var zero = Array.IndexOf(b, (byte)0, at);
|
||||
name = Encoding.UTF8.GetString(b, at, (zero < 0 ? b.Length : zero) - at);
|
||||
}
|
||||
else
|
||||
name = hash.ToString("x8");
|
||||
files[name] = b[(dataAt + start)..(dataAt + end)];
|
||||
}
|
||||
return files;
|
||||
}
|
||||
}
|
||||
13
launcher/Core/Triforce.Core.csproj
Normal file
13
launcher/Core/Triforce.Core.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ZstdSharp.Port" Version="0.8.8" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user