Triforce v0.8.5

A Randomizer page in the launcher, a new title screen with Mods and Save Select tabs, scaling changes applied in game, the Jabu-Jabu bars fixed, renderer frame-time work and the brightness-flash fix, and fixes from the 0.8.4 reports.
This commit is contained in:
ruckus7382sfgsgdf
2026-11-28 10:54:41 -06:00
parent 2df08c751e
commit b527e29adb
203 changed files with 18821 additions and 2051 deletions

View File

@@ -1540,9 +1540,10 @@ public static class Astc
{
if (blockWidth < 4 || blockWidth > 12 || blockHeight < 4 || blockHeight > 12)
throw new NotSupportedException("ASTC block " + blockWidth + "x" + blockHeight);
ImageLimits.Check(width, height);
var rows = (height + blockHeight - 1) / blockHeight;
var cols = (width + blockWidth - 1) / blockWidth;
if (data.Length < rows * cols * 16)
if (data.Length < (long)rows * cols * 16)
throw new InvalidDataException("ASTC data is shorter than the image");
var output = new byte[width * height * 4];
Parallel.For(0, rows, () => new Scratch(), (yIndex, _, scratch) =>

View File

@@ -42,6 +42,11 @@ public static class BlockLinear
{
if (bytesPerElement is not (1 or 2 or 4 or 8 or 16))
throw new NotSupportedException(bytesPerElement + " bytes per element");
// ((Triforce, 11-26) the sizes checked before anything is allocated: a header could ask for 2 GB, then be refused)
ImageLimits.Check(width, height);
if ((long)width * bytesPerElement * height > int.MaxValue || blockHeightLog2 is < 0 or > 5) throw new InvalidDataException("a texture larger than the game has");
if (Size(width, height, bytesPerElement, blockHeightLog2) > input.Length)
throw new InvalidDataException("the texture's data is shorter than its size calls for");
var pitch = width * bytesPerElement;
var output = new byte[pitch * height];
var gobsInX = (pitch + 63) >> GobSizeXShift;
@@ -49,9 +54,6 @@ public static class BlockLinear
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);

View File

@@ -40,14 +40,22 @@ public sealed class Bntx
throw new InvalidDataException("not a BNTX archive");
var count = U32(0x24);
var table = (long)U64(0x28);
// ((Triforce, 11-26) the count and the names as the file says them, checked: a few KB that named 131072 textures, each
// with a 64K-character name, asked for about 17 GB)
if (count > 4096 || table < 0 || table > data.Length || table + 8L * count > data.Length)
throw new InvalidDataException("BNTX texture table out of the file");
var list = new List<BntxTexture>();
var seen = new HashSet<long>();
for (var i = 0; i < count; i++)
{
var p = (long)U64(table + 8 * i);
if (p < 0 || p + 0x80 > data.Length || !seen.Add(p)) throw new InvalidDataException("BNTX texture " + i + " is out of the file, or named twice");
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);
if (nameAt < 0 || nameAt + 2 > data.Length) throw new InvalidDataException("BNTX texture " + i + "'s name is out of the file");
var nameLength = U16(nameAt);
if (nameLength > 1024 || nameAt + 2 + nameLength > data.Length) throw new InvalidDataException("BNTX texture " + i + "'s name is too long");
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),
@@ -188,6 +196,7 @@ public static class BlockCompression
var bytesPerBlock = kind is Kind.BC1 or Kind.BC4 ? 8 : 16;
var wBlocks = (width + 3) / 4;
var hBlocks = (height + 3) / 4;
ImageLimits.Check(width, height);
var output = new byte[width * height * 4];
Span<byte> texels = stackalloc byte[64];
Span<byte> channel = stackalloc byte[16];

View File

@@ -0,0 +1,16 @@
// ((Triforce, 11-26) The sizes a texture of the game may have. A texture's width and height come from its file's header: before
// they size an output buffer (width * height * 4, in int), they are checked, so a damaged or crafted file is refused with a
// reason instead of asking for gigabytes, wrapping to a negative size, or throwing OverflowException.
namespace Triforce.Core.GameFiles;
public static class ImageLimits
{
/// <summary>The widest and tallest texture taken (the game's largest are far smaller; 16384 x 16384 x 4 bytes fits an int).</summary>
public const int MaxSide = 16384;
public static void Check(int width, int height)
{
if (width <= 0 || height <= 0 || width > MaxSide || height > MaxSide)
throw new InvalidDataException("a texture of " + width + " x " + height + ", which no texture of the game is");
}
}

View File

@@ -127,6 +127,8 @@ public sealed class NupSource : IGameSource
string Name(uint offset, uint count)
{
// ((Triforce, 11-26) a name inside the strings area and of a name's length: a damaged index made each read a megabyte)
if (count > 1024 || stringsAt + (long)offset + count > index.Capacity) throw new InvalidDataException("the package's index names something outside its names");
var bytes = new byte[count];
index.ReadArray(stringsAt + offset, bytes, 0, (int)count);
return Encoding.UTF8.GetString(bytes);
@@ -145,6 +147,10 @@ public sealed class NupSource : IGameSource
return known;
var first = index.ReadUInt32(dirsAt + 24L * dir + 8);
var count = index.ReadUInt32(dirsAt + 24L * dir + 12);
// ((Triforce, 11-26) the children inside the children's table (between kidsAt and the names), before anything is
// allocated for them: a 300-byte file asked for tens of GB here)
if ((long)first + count > (stringsAt - kidsAt) / 4 || count > dirCount + fileCount)
throw new InvalidDataException("the package's index lists children outside its table");
var kids = new uint[count];
index.ReadArray(kidsAt + 4L * first, kids, 0, (int)count);
var names = new Dictionary<string, uint>((int)count, StringComparer.Ordinal);
@@ -241,6 +247,7 @@ public sealed class NupSource : IGameSource
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);
if (size > Zs.MaxUnpacked) throw new InvalidDataException(romfsPath + ": larger than any file of the game");
var output = new byte[size];
using var z = new Decompressor();
if (z.Unwrap(frame, output) != size)

View File

@@ -48,14 +48,19 @@ public sealed class Zs
return Decompress(data, dict);
}
/// <summary>((Triforce, 11-26) The most one frame may unpack to: the game's largest file is far smaller. A frame that says more,
/// or unpacks to more, is refused: a header of a few bytes could ask for 2 GB, and a frame that names no size grew without end.</summary>
public const int MaxUnpacked = 1 << 30;
/// <summary>One zstd frame unpacked, with a dictionary or none.</summary>
public static byte[] Decompress(ReadOnlySpan<byte> data, byte[]? dictionary)
public static byte[] Decompress(ReadOnlySpan<byte> data, byte[]? dictionary, int max = MaxUnpacked)
{
using var z = new Decompressor();
if (dictionary != null)
z.LoadDictionary(dictionary);
var size = Decompressor.GetDecompressedSize(data);
if (size > 0 && size <= int.MaxValue)
if ((ulong)size > (ulong)max) throw new InvalidDataException("a zstd frame that unpacks to " + size + " bytes, more than a game file is");
if (size > 0)
{
var output = new byte[size];
var n = z.Unwrap(data, output);
@@ -66,7 +71,12 @@ public sealed class Zs
if (dictionary != null)
stream.LoadDictionary(dictionary);
using var into = new MemoryStream();
stream.CopyTo(into);
var buffer = new byte[1 << 16];
for (int n; (n = stream.Read(buffer, 0, buffer.Length)) > 0;)
{
into.Write(buffer, 0, n);
if (into.Length > max) throw new InvalidDataException("a zstd frame that unpacks to more than " + max + " bytes, more than a game file is");
}
return into.ToArray();
}