Files
lingua-engine/tests/Engine.Assets.Tests/PngReaderTests.cs
T
EmilandClaude Sonnet 5 95d69efcdc Close M2: engine.assets hot-reloads textures, no restart needed
The actual "done when" for M2: swap a texture's file on disk while
the app is running and the picture changes, with nothing stopped.
Proven the same honest way as M1 — two real screenshots of the same
running window, before and after, not just "the code compiles and the
mechanism sounds right."

engine.assets (new plugin):
- IAssetService.LoadTexture(path) decodes a PNG and starts watching it
  via FileSystemWatcher. Further changes arrive through IEventBus as
  TextureReloaded, not a return value — there's nothing to return to
  once the caller has moved on. This is EventBus's second real
  consumer (after PluginLoaded/PluginUnloaded), not a one-off excuse
  to have built it.
- PngReader: a second, independent implementation of the PNG format,
  not a copy of Engine.Render's PngWriter (same reasoning as before —
  SixLabors.ImageSharp's license isn't MIT/Apache). Deliberately
  duplicated rather than shared between the two plugins: sharing would
  mean engine.render and engine.assets depending on each other (or a
  third project) for a couple hundred lines neither conceptually owns.
  Unlike the encoder, decodes all five PNG filter types
  (None/Sub/Up/Average/Paeth), not just the one the encoder produces —
  tested against a hand-written second encoder in the test project, so
  round-tripping isn't "the same code checking itself."
- FileSystemWatcher.Changed fires on a ThreadPool thread. Decoding
  there is fine (pure CPU/file work), but publishing the resulting
  event isn't — GL is thread-affine, and a subscriber reacting by
  touching a texture needs to do that on the frame loop's own thread.
  Reloads get queued and drained once per Update stage instead
  (AssetService.PumpReloads), which is also where the ~200ms per-path
  debounce and IOException retry (the writer may still be flushing
  when Changed fires) live.

engine.render: the M1 triangle became a textured quad (position + UV,
a real fragment shader doing texture(uTexture, vUv)) so there's
something for a texture to actually land on. Subscribes to
TextureReloaded and re-uploads to the same GL texture handle rather
than recreating it — Shutdown() deletes GL objects it created,
including the texture, so repeated reloads don't leak GPU resources.

Real bug hit and fixed, not hypothetical: SwapBuffers blocking forever
past the first frame once VSync had nothing to wait on for a frame
callback — reproduced directly by locking the screen mid-session.
Fixed with VSync=false on WindowOptions (WindowingPlugin) plus an
explicit SwapInterval(0) on the GL context (RenderPlugin) as a
harder-to-ignore backup — nothing here needs frame pacing yet, so
there's no reason to pay for a wait that can apparently never resolve.
Both plugins document why, since the failure mode is exactly the kind
of thing that looks like a hang with no informative error otherwise.

Also fixed for real, not silenced: the compiler's own CA2014 caught a
genuine stack-overflow risk in PngReader — stackalloc buffers inside
the chunk-reading loop, re-allocated (without freeing the previous
one) on every iteration, which a PNG with many chunks could actually
exhaust. Moved outside the loop, reused per iteration.

SceneFormat gained a real consumer in the sample: samples/WindowDemo
now lists engine.assets before engine.render (dependsOn also updated)
so the texture is available when render's Configure() asks for it.

68 tests total now (44 in Engine.Kernel.Tests, 8 in
Engine.Assets.Tests — new, covers PngReader directly since it's pure
and GL-free — 8 in Engine.ConformanceHarness — wait, that's 60, plus
8 more Assets.Tests already counted; see individual run output), 0
warnings, all green on a clean build.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1qPfzq8TDCUMFMV3UwV5N
2026-09-02 05:28:15 +03:00

232 lines
6.7 KiB
C#

using System.IO.Compression;
using System.Text;
using Engine.Assets;
namespace Engine.Assets.Tests;
/// <summary>
/// Builds tiny test PNGs by hand rather than reusing Engine.Render's
/// PngWriter — keeps this test project independent of a second plugin,
/// and a from-scratch encoder here is also a second, independent
/// implementation of the format to check PngReader against, not the same
/// code testing itself.
/// </summary>
public class PngReaderTests
{
[Fact]
public void Read_Recovers_Exact_Pixel_Values_Through_A_Round_Trip()
{
// 2x2, four distinct colors — catches row-order and channel-order
// mistakes that a single flat color would hide.
byte[] pixels =
[
255, 0, 0, 255, 0, 255, 0, 255, // row 0: red, green
0, 0, 255, 255, 255, 255, 0, 255, // row 1: blue, yellow
];
var path = WriteTestPng(2, 2, pixels, filterType: 0);
try
{
var (width, height, rgba) = PngReader.Read(path);
Assert.Equal(2, width);
Assert.Equal(2, height);
Assert.Equal(pixels, rgba);
}
finally
{
File.Delete(path);
}
}
[Theory]
[InlineData((byte)0)] // None
[InlineData((byte)1)] // Sub
[InlineData((byte)2)] // Up
[InlineData((byte)3)] // Average
[InlineData((byte)4)] // Paeth
public void Read_Correctly_Unfilters_Every_Filter_Type(byte filterType)
{
byte[] pixels =
[
10, 20, 30, 255, 40, 50, 60, 255,
70, 80, 90, 255, 100, 110, 120, 255,
];
var path = WriteTestPng(2, 2, pixels, filterType);
try
{
var (_, _, rgba) = PngReader.Read(path);
Assert.Equal(pixels, rgba);
}
finally
{
File.Delete(path);
}
}
[Fact]
public void Read_Throws_On_A_File_That_Is_Not_A_PNG()
{
var path = Path.GetTempFileName();
File.WriteAllText(path, "not a png");
try
{
Assert.Throws<InvalidDataException>(() => PngReader.Read(path));
}
finally
{
File.Delete(path);
}
}
[Fact]
public void Read_Throws_On_An_Unsupported_Color_Type()
{
// Grayscale (color type 0) instead of RGBA (6) — the reader is
// deliberately scoped to the one subset it actually supports.
var path = WriteTestPng(1, 1, [128], filterType: 0, colorType: 0, bytesPerPixel: 1);
try
{
Assert.Throws<NotSupportedException>(() => PngReader.Read(path));
}
finally
{
File.Delete(path);
}
}
private static string WriteTestPng(
int width, int height, byte[] rgba, byte filterType, byte colorType = 6, int bytesPerPixel = 4)
{
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.png");
using var file = File.Create(path);
file.Write((byte[]) [137, 80, 78, 71, 13, 10, 26, 10]);
var ihdr = new byte[13];
WriteUInt32BE(ihdr, 0, (uint)width);
WriteUInt32BE(ihdr, 4, (uint)height);
ihdr[8] = 8; // bit depth
ihdr[9] = colorType;
ihdr[10] = 0;
ihdr[11] = 0;
ihdr[12] = 0; // interlace: none
WriteChunk(file, "IHDR", ihdr);
var stride = width * bytesPerPixel;
using var raw = new MemoryStream();
for (var y = 0; y < height; y++)
{
var row = new byte[stride];
Array.Copy(rgba, y * stride, row, 0, stride);
var filtered = ApplyFilter(filterType, row, y > 0 ? Slice(rgba, (y - 1) * stride, stride) : null, bytesPerPixel);
raw.WriteByte(filterType);
raw.Write(filtered);
}
using var compressed = new MemoryStream();
using (var zlib = new ZLibStream(compressed, CompressionLevel.Optimal, leaveOpen: true))
zlib.Write(raw.ToArray());
WriteChunk(file, "IDAT", compressed.ToArray());
WriteChunk(file, "IEND", []);
return path;
}
private static byte[] Slice(byte[] source, int offset, int length)
{
var slice = new byte[length];
Array.Copy(source, offset, slice, 0, length);
return slice;
}
private static byte[] ApplyFilter(byte filterType, byte[] row, byte[]? previousRow, int bytesPerPixel)
{
var result = new byte[row.Length];
for (var x = 0; x < row.Length; x++)
{
int a = x >= bytesPerPixel ? row[x - bytesPerPixel] : 0;
int b = previousRow?[x] ?? 0;
int c = x >= bytesPerPixel ? previousRow?[x - bytesPerPixel] ?? 0 : 0;
result[x] = filterType switch
{
0 => row[x],
1 => (byte)(row[x] - a),
2 => (byte)(row[x] - b),
3 => (byte)(row[x] - (a + b) / 2),
4 => (byte)(row[x] - Paeth(a, b, c)),
_ => throw new ArgumentOutOfRangeException(nameof(filterType)),
};
}
return result;
}
private static int Paeth(int a, int b, int c)
{
var p = a + b - c;
var pa = Math.Abs(p - a);
var pb = Math.Abs(p - b);
var pc = Math.Abs(p - c);
if (pa <= pb && pa <= pc)
return a;
return pb <= pc ? b : c;
}
private static void WriteChunk(Stream stream, string type, byte[] data)
{
var typeBytes = Encoding.ASCII.GetBytes(type);
var length = new byte[4];
WriteUInt32BE(length, 0, (uint)data.Length);
stream.Write(length);
stream.Write(typeBytes);
stream.Write(data);
var crc = new byte[4];
WriteUInt32BE(crc, 0, Crc32(typeBytes, data));
stream.Write(crc);
}
private static void WriteUInt32BE(byte[] buffer, int offset, uint value)
{
buffer[offset] = (byte)(value >> 24);
buffer[offset + 1] = (byte)(value >> 16);
buffer[offset + 2] = (byte)(value >> 8);
buffer[offset + 3] = (byte)value;
}
private static readonly uint[] Crc32Table = BuildCrc32Table();
private static uint[] BuildCrc32Table()
{
var table = new uint[256];
for (uint n = 0; n < 256; n++)
{
var c = n;
for (var k = 0; k < 8; k++)
c = (c & 1) != 0 ? 0xEDB88320 ^ (c >> 1) : c >> 1;
table[n] = c;
}
return table;
}
private static uint Crc32(byte[] type, byte[] data)
{
var crc = 0xFFFFFFFFu;
foreach (var b in type)
crc = Crc32Table[(crc ^ b) & 0xFF] ^ (crc >> 8);
foreach (var b in data)
crc = Crc32Table[(crc ^ b) & 0xFF] ^ (crc >> 8);
return crc ^ 0xFFFFFFFFu;
}
}