Files
servuo-plugins/overlay/Scripts/Custom/Bridge/BridgeArtMul.cs
wtclaude cac66a6b83 fix(bridge): decode item and land art without GDI+ (#39, 2 of 2)
On a Linux/Mono shard the Asset Bridge's item and land art failed: the vendored
Art.LoadStatic/LoadLand build a 16bpp Bitmap (Settings.PixelFormat) and
libgdiplus 6.1 cannot create one, so every static and land picture threw
[GDI+ NotImplemented] before a pixel was read. PR 1 moved the body catalogue off
System.Drawing; this finishes the migration for the art plane, so Windows and
Linux now share one managed path and the whole Asset Bridge needs no libgdiplus.

- BridgeArtMul: a hand-written art.mul static + land decoder to ARGB1555,
  mirroring Art.LoadStatic's run loop and LoadLand's 44x44 diamond, with the
  record/canvas bounds the vendored writers lack (no System.Drawing, no Bitmap).
- BridgeArt: Render now decodes through BridgeArtMul and hues through BridgeHue
  (over hues.mul + the tiledata PartialHue flag) into BridgePng, via a shared
  TryArtPixels helper the parity harness drives. The GDI+ Bitmap/ApplyTo/dispose
  path, the imaging gate, and the Files.CacheData re-check in TryHue are gone,
  because a key now decodes its own fresh buffer that nothing else aliases.
- BridgeAssets: retire BitmapToPng (its last caller) and the imaging probe/field
  machinery; assets.sources no longer carries an imaging field. No decode goes
  through System.Drawing any more, so the NO_IMAGING refusal is retired with the
  GDI+ it guarded. Files.CacheData stays off, now only so the parity oracle's
  vendored decode hands back fresh bitmaps.
- BridgeArtParity: a [BridgeArtParity] GM command / BRIDGE_ART_PARITY=1 boot run
  that compares the managed decode+hue against the vendored one pixel for pixel.

Overlay-only; no wire change beyond the retired imaging field, no PROTOCOL_VERSION
bump. The website tolerates the field's absence (its NO_IMAGING branch is guarded
by `sources.imaging &&`); the dead NO_IMAGING UI/model in module-uo and the
installer doctor libgdiplus row are removed in a tracked follow-up.

Verified on this machine. Windows (vendored GDI+ oracle available): the parity
harness compared every served picture and every one is pixel-identical to the
vendored decode — 43,433/43,433 (39,189 statics + 4,244 land) with no hue, then
39,189/39,189 again hued in each item's PartialHue mode, 0 mismatched. On WSL2
Ubuntu 24.04 / Mono 6.8 / libgdiplus 6.1 (the live-host match) the vendored
oracle threw NotImplemented and the managed path produced all 43,433 + 39,189.
dotnet build 0/0.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014HJEmcaKvWQ8LsnXHKha5y
2026-11-26 02:13:59 -06:00

224 lines
10 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
namespace Server.Custom.Bridge
{
/// <summary>
/// **The item-and-land art reader** (servuo-plugins #39, PR 2): the half of the GDI+-free
/// migration <see cref="BridgeMul"/> (animations) and <see cref="BridgeUop"/> (UOP bodies)
/// left for the art plane. On a Linux/Mono shard the vendored <c>Ultima.Art.LoadStatic</c>
/// and <c>LoadLand</c> build a 16bpp <c>Bitmap</c> (<c>Settings.PixelFormat</c>) and
/// libgdiplus 6.1 answers <c>NotImplemented</c>, so every item and land picture the Asset
/// Bridge serves fails before a pixel is read. This decodes the same bytes into an ARGB1555
/// <c>ushort[]</c> with no <c>Bitmap</c> anywhere, for <see cref="BridgePng.FromArgb1555"/>
/// to encode and <see cref="BridgeHue"/> to tint.
///
/// ── **Faithful to the vendored run loops, with the bounds they lack** ──
///
/// <c>LoadStatic</c>'s inner loop advances a raw pointer by a run header's
/// <c>(xOffset, xRun)</c> pair and writes <c>xRun</c> palette-less ARGB1555 pixels
/// (<c>stored ^ 0x8000</c>), stopping a row on the <c>(0,0)</c> terminator or when a pair
/// would leave the scanline. It guards neither the read cursor nor the write against the
/// record — <see cref="BridgeAssetValidator.StaticRecordSane"/> is the shipped gate that
/// walks the same table bounded, and this reader re-checks every read and every write
/// against the known array length anyway rather than writing through a pointer the way the
/// library does. Two behaviours differ from a raw pointer, and they differ from each other:
///
/// - **Row-stop (truncate).** `xOffset > width` or `xOffset + xRun > width` *stops the row*
/// and moves on — exactly what <c>LoadStatic</c> does against the padded 16bpp stride
/// (`delta`), and what <c>StaticRecordSane</c> does against `width`. Because this reader
/// bounds against the real `width` rather than `delta`, the two can differ by one case: a
/// run whose last pixel lands in a scanline's invisible pad column (only possible for an
/// odd width) — the library writes it into the pad, this truncates the row. No record this
/// client's own packer produces reaches it, and the pixel is invisible either way.
/// - **Canvas-overflow (refuse).** If the cumulative column plus a run would cross into the
/// next scanline (`col + xRun > width`), the library bleeds a visible run into the next
/// row through its pointer; this *refuses the whole record* (returns false). A refusal is
/// surfaced to the merge-gate harness as a possible miss rather than silently dropped.
///
/// <c>LoadLand</c> is fixed geometry: a 44×44 diamond of 1,012 stored ARGB1555 values
/// (<c>stored | 0x8000</c>), two triangular halves, read straight off the record with no run
/// headers. <see cref="BridgeAssetValidator.LandLengthSane"/> guarantees the 2,024 bytes the
/// vendored reader consumes regardless of the declared length are present.
/// </summary>
public static class BridgeArtMul
{
/// <summary>
/// Decodes one static item record (<c>art.mul</c> / <c>artLegacyMUL.uop</c>) into
/// ARGB1555. <paramref name="record"/> is the raw, verdata-patched record exactly as
/// <see cref="BridgeAssetValidator.RecordReader.ReadRecordCopy"/> hands it over.
///
/// Returns false with <paramref name="empty"/> set for a 0×0 (or non-positive) record —
/// the same early return <c>LoadStatic</c> makes before it builds a bitmap, which the
/// Bridge treats as "no picture here" rather than damage. Any other false is a record
/// this reader refused, with <paramref name="reason"/> set.
/// </summary>
public static bool TryDecodeStatic(byte[] record, out ushort[] argb1555,
out int width, out int height, out bool empty, out string reason)
{
argb1555 = null;
width = 0;
height = 0;
empty = false;
reason = null;
// The 8-byte header: a 4-byte flag, then width and height as ushorts.
if (record == null || record.Length < 8)
{
reason = "static record is too short to hold an 8-byte header";
return false;
}
int w = BitConverter.ToUInt16(record, 4);
int h = BitConverter.ToUInt16(record, 6);
// LoadStatic returns null here rather than misbehaving: no art, not damage.
if (w <= 0 || h <= 0)
{
empty = true;
reason = "static is " + w + "x" + h;
return false;
}
if (w > BridgeAssetValidator.MaxArtDimension || h > BridgeAssetValidator.MaxArtDimension)
{
reason = "static declares " + w + "x" + h;
return false;
}
int words = record.Length / 2;
// The row-lookup table: h ushorts starting at word 4.
if (4 + h > words)
{
reason = "row table (" + h + " entries) does not fit in a " + record.Length + "-byte record";
return false;
}
int start = h + 4;
var canvas = new ushort[w * h];
for (int y = 0; y < h; y++)
{
// GetStatic's `lookups[y] = start + bindata[4 + y]`, in ushort units.
int count = start + BitConverter.ToUInt16(record, (4 + y) * 2);
int rowBase = y * w;
int col = 0; // cur - line, in pixels — cumulative across this row's pairs
while (true)
{
// Two ushorts for the run header, both inside the record.
if (count < 0 || count + 1 >= words)
{
reason = "row " + y + " reads at word " + count + ", past the record's " + words;
return false;
}
int xOffset = BitConverter.ToUInt16(record, count * 2);
int xRun = BitConverter.ToUInt16(record, (count + 1) * 2);
count += 2;
// The (0,0) terminator LoadStatic stops the row on.
if (xOffset + xRun == 0)
break;
// LoadStatic stops the row here (against the scanline); the read cursor stops
// with it. StaticRecordSane makes the identical test against width.
if (xOffset > w || xOffset + xRun > w)
break;
col += xOffset;
// The run's pixels must be inside the record...
if (count + xRun > words)
{
reason = "row " + y + " declares a " + xRun + "-pixel run running past the record";
return false;
}
// ...and inside the row. The vendored writer bounds each pair but not the
// cumulative column; no record this client packs crosses the scanline, and one
// that did would have the library bleed into the next row rather than refuse.
if (col < 0 || col + xRun > w)
{
reason = "row " + y + " writes a run to column " + col + "+" + xRun + ", past " + w;
return false;
}
int at = rowBase + col;
for (int i = 0; i < xRun; i++)
canvas[at + i] = (ushort)(BitConverter.ToUInt16(record, (count + i) * 2) ^ 0x8000);
count += xRun;
col += xRun;
}
}
argb1555 = canvas;
width = w;
height = h;
return true;
}
/// <summary>The land diamond is always 44×44 — <c>LoadLand</c> hard-codes it.</summary>
private const int LandSize = 44;
/// <summary>
/// Decodes one land tile record into a 44×44 ARGB1555 diamond, mirroring
/// <c>LoadLand</c>: two triangular halves of stored values, <c>stored | 0x8000</c>, read
/// straight off the record. The four corners outside the diamond are never written and
/// stay transparent, exactly as the vendored fresh bitmap leaves them.
/// </summary>
public static bool TryDecodeLand(byte[] record, out ushort[] argb1555,
out int width, out int height, out string reason)
{
argb1555 = null;
width = 0;
height = 0;
reason = null;
// LoadLand reads 2,024 bytes whatever the declared length; LandLengthSane is the gate
// that refuses a shorter record, and this re-states the bound on the known array.
if (record == null || record.Length < BridgeAssetValidator.LandRecordBytes)
{
reason = "land record is "
+ (record == null ? 0 : record.Length)
+ " bytes; LoadLand always reads " + BridgeAssetValidator.LandRecordBytes;
return false;
}
var canvas = new ushort[LandSize * LandSize];
int src = 0; // byte offset of the next stored ushort
// Top half: rows 0..21 widen from 2 to 44, centred.
int xOffset = 21;
int xRun = 2;
for (int y = 0; y < 22; y++, xOffset--, xRun += 2)
{
int at = (y * LandSize) + xOffset;
for (int i = 0; i < xRun; i++, src += 2)
canvas[at + i] = (ushort)(BitConverter.ToUInt16(record, src) | 0x8000);
}
// Bottom half: rows 22..43 narrow from 44 back to 2.
xOffset = 0;
xRun = 44;
for (int y = 22; y < 44; y++, xOffset++, xRun -= 2)
{
int at = (y * LandSize) + xOffset;
for (int i = 0; i < xRun; i++, src += 2)
canvas[at + i] = (ushort)(BitConverter.ToUInt16(record, src) | 0x8000);
}
argb1555 = canvas;
width = LandSize;
height = LandSize;
return true;
}
}
}