Files
servuo-plugins/overlay/Scripts/Custom/Bridge/BridgeMul.cs
wtclaude 5dd5780f42 fix(bridge): decode legacy animation art without GDI+ (#39, 1 of 2)
On a Linux/Mono shard the Asset Bridge's legacy-MUL catalogue art failed with
[GDI+ status: NotImplemented]: the vendored Ultima.Frame ctor builds a 16bpp
Bitmap (Settings.PixelFormat) and libgdiplus cannot create one. This finishes,
for the legacy animation path, the GDI+-free migration §4.4 began for UOP —
Windows and Linux now share one managed path rather than a Linux workaround.

- BridgeMul: a hand-written anim*.mul first-frame decoder to ARGB1555, mirroring
  Ultima.Frame's run loop with BridgeUop's bounds (no System.Drawing, no Bitmap).
- BridgeHue: Hue.ApplyTo reproduced over a ushort[], reusing the vendored
  Hues.List[i].Colors table so a body.def-hued creature tints identically.
- BridgeAssetValidator: RecordReader.ReadRecordCopy hands the verdata-patched
  record bytes to the managed decoder; a ResolveAnimation overload surfaces the
  body.def hue it already computes and discarded.
- BridgeCatalog: the legacy path now decodes through BridgeMul + BridgeHue ->
  BridgePng; the imaging gate is removed from assets.manifest/fetch, since the
  body catalogue (legacy + UOP) no longer touches System.Drawing. The record
  gate (ResolveAnimation/CheckEntry/AnimationSane) is unchanged.
- BridgeAnimParity: a [BridgeAnimParity] GM command / BRIDGE_ANIM_PARITY=1 boot
  run that compares the managed decode against the vendored one pixel for pixel.

Overlay-only; no wire change, no PROTOCOL_VERSION bump. The art/land path
(BridgeArt) and its imaging gate are the second PR. The art family's imaging.ok
status is unchanged meanwhile.

Verified on this machine's ServUO + live client: the parity harness compared all
40,341 records the Bridge serves and every one is pixel-identical to the vendored
GDI+ decode (0 mismatched, 0 missed); 102,923 out-of-band/invalid records the
Bridge refuses were correctly skipped.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014HJEmcaKvWQ8LsnXHKha5y
2026-11-25 21:12:24 -06:00

179 lines
7.2 KiB
C#

using System;
namespace Server.Custom.Bridge
{
/// <summary>
/// **The legacy-MUL animation reader** (servuo-plugins #39): the half of the GDI+-free
/// migration that <see cref="BridgeUop"/> left undone. §4.4 moved the UOP path off
/// <c>System.Drawing</c> because libgdiplus was archived in March 2025; this does the same
/// for <c>anim*.mul</c>, and for a sharper reason — on a Linux/Mono shard the vendored
/// <c>Ultima.Frame</c> ctor asks libgdiplus to create a 16bpp <c>Bitmap</c>
/// (<c>Settings.PixelFormat</c>) and libgdiplus answers <c>NotImplemented</c>, so the whole
/// catalogue's legacy art fails before a single pixel is read.
///
/// This decodes the same bytes <c>Ultima.Frame</c> does, into an ARGB1555 <c>ushort[]</c>
/// with no <c>Bitmap</c> anywhere, for <see cref="BridgePng.FromArgb1555"/> to encode. The
/// run loop is <c>Frame</c>'s (<c>Animations.cs</c>), with the two bounds it does not have —
/// exactly as <see cref="BridgeUop.Group.TryDecode"/> added them to the UOP run loop: a run
/// that would leave the canvas, or read past the record, refuses the frame rather than
/// writing through a raw pointer the way the library does.
///
/// ── **Legacy layout vs UOP** ──
///
/// The pixel format is identical (shared palette of ARGB1555, <c>DoubleXor</c> run headers,
/// the <c>0x7FFF7FFF</c> terminator), but the container is not. A legacy record is a single
/// <b>shared</b> palette of 0x100 entries at the very start, then a frame count and a lookup
/// table, then the frames; a UOP record carries a <b>per-frame</b> palette after each
/// frame's header. So this reads the palette once and seeks the first frame through the
/// lookup table — it does not reuse <see cref="BridgeUop"/>'s offset math.
/// </summary>
public static class BridgeMul
{
/// <summary>The xor <c>Ultima.Frame</c> applies to every run header, and so must this.</summary>
private const int DoubleXor = (0x200 << 22) | (0x200 << 12);
/// <summary>The shared palette: 0x100 ARGB1555 entries at the head of every record.</summary>
private const int PaletteBytes = 0x100 * 2;
/// <summary>
/// Decodes the first frame of a legacy animation record (what a
/// <c>FirstFrame: true</c> <c>GetAnimation</c> call produces) into ARGB1555.
///
/// <paramref name="record"/> is the raw record, verdata-patched, exactly as
/// <see cref="BridgeAssetValidator.RecordReader.ReadRecordCopy"/> hands it over — this
/// reads bytes only, never a stream, so every bound is a bound on a known array length.
/// A 0x0 frame returns false with <paramref name="empty"/> set, which is how the
/// vendored ctor treats it too (an early return, not damage).
/// </summary>
public static bool TryDecodeFirstFrame(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;
if (record == null || record.Length < PaletteBytes + 4)
{
reason = "record is too short to hold a palette and frame count";
return false;
}
var palette = new ushort[0x100];
for (int i = 0; i < 0x100; i++)
{
// The library's own xor: the stored entry has its alpha bit clear and every
// palette colour is opaque. A pixel no run covers stays zero, which is the
// sprite's transparent background.
palette[i] = (ushort)(BitConverter.ToUInt16(record, i * 2) ^ 0x8000);
}
int start = PaletteBytes; // GetAnimation's `start`, after the palette
int frameCount = BitConverter.ToInt32(record, start);
if (frameCount <= 0 || frameCount > BridgeAssetValidator.MaxAnimFrames)
{
reason = "record declares " + frameCount + " frames";
return false;
}
// Only the first frame is decoded, so only its lookup slot has to be inside the record.
int lookupAt = start + 4;
if ((long)lookupAt + 4 > record.Length)
{
reason = "frame lookup table runs past the record";
return false;
}
long frameAt = (long)start + BitConverter.ToInt32(record, lookupAt);
// The frame header is xCenter(i16), yCenter(i16), width(u16), height(u16) = 8 bytes.
if (frameAt < 0 || frameAt + 8 > record.Length)
{
reason = "frame points outside the record";
return false;
}
int at = (int)frameAt;
int xCenter = BitConverter.ToInt16(record, at);
int yCenter = BitConverter.ToInt16(record, at + 2);
int w = BitConverter.ToUInt16(record, at + 4);
int h = BitConverter.ToUInt16(record, at + 6);
if (w == 0 || h == 0)
{
// No art, exactly as Frame's ctor returns early on width==0 || height==0.
empty = true;
reason = "frame is " + w + "x" + h;
return false;
}
if (w > BridgeAssetValidator.MaxArtDimension || h > BridgeAssetValidator.MaxArtDimension)
{
reason = "frame declares " + w + "x" + h;
return false;
}
var canvas = new ushort[w * h];
int p = at + 8;
int xBase = xCenter - 0x200;
int yBase = (yCenter + h) - 0x200;
while (true)
{
if (p + 4 > record.Length)
{
reason = "frame has no terminator inside the record";
return false;
}
int header = BitConverter.ToInt32(record, p);
p += 4;
if (header == 0x7FFF7FFF)
break;
header ^= DoubleXor;
int x = ((header >> 22) & 0x3FF) + xBase;
int y = ((header >> 12) & 0x3FF) + yBase;
int run = header & 0xFFF;
if (run == 0)
continue;
if (p + run > record.Length)
{
reason = "frame has a run past the end of the record";
return false;
}
if (y < 0 || y >= h || x < 0 || x + run > w)
{
reason = "frame has a run at " + x + "," + y + " of " + run
+ " outside " + w + "x" + h;
return false;
}
int cursor = (y * w) + x;
for (int i = 0; i < run; i++)
canvas[cursor + i] = palette[record[p + i]];
p += run;
}
argb1555 = canvas;
width = w;
height = h;
return true;
}
}
}