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
This commit is contained in:
2026-11-26 02:13:59 -06:00
parent db87b20fae
commit cac66a6b83
5 changed files with 888 additions and 284 deletions

View File

@@ -0,0 +1,235 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using Server;
using Server.Commands;
using Ultima;
namespace Server.Custom.Bridge
{
/// <summary>
/// **The #39 PR 2 merge gate**: the GM command <c>[BridgeArtParity]</c>, which proves the
/// new GDI+-free item/land decode (<see cref="BridgeArtMul"/> + <see cref="BridgeHue"/>,
/// driven by <see cref="BridgeArt.RunArtParity"/>) is pixel-identical to the vendored
/// <c>Ultima.Art</c> + <c>Hue.ApplyTo</c> path it replaces.
///
/// Like <see cref="BridgeAnimParity"/> for the catalogue, this is the one art file that
/// still touches <c>System.Drawing</c>, and it does so only to be the <b>oracle</b>: on
/// Windows it runs the vendored <c>Art.GetStatic</c>/<c>GetLand</c> (and, for the hue pass,
/// <c>Hue.ApplyTo</c>), reads the resulting 16bpp bitmap back to ARGB1555, and hands those
/// pixels to the harness to compare against the managed decode. On a real Linux/Mono shard
/// the vendored bitmap ctor throws <c>NotImplemented</c> (the very bug #39 fixes), the
/// oracle reports itself unavailable, and the harness falls back to confirming the managed
/// path still renders — so the same command is meaningful on both platforms. It ships in the
/// overlay because it needs both decoders in one process; it changes nothing the Bridge
/// serves.
/// </summary>
public static class BridgeArtParity
{
public static void Initialize()
{
CommandSystem.Register("BridgeArtParity", AccessLevel.Administrator, OnCommand);
// Headless gate run (servuo-plugins #39): with BRIDGE_ART_PARITY=1 in the environment,
// run the parity once the world is up and write parity-art.txt, no client needed. For
// verification/CI only — it is never set on a production shard.
if (Environment.GetEnvironmentVariable("BRIDGE_ART_PARITY") == "1")
EventSink.ServerStarted += OnServerStartedParity;
}
private static void OnServerStartedParity()
{
Console.WriteLine("[Bridge] art parity: BRIDGE_ART_PARITY=1 — running at boot");
Run(null);
}
private static void OnCommand(CommandEventArgs e)
{
Run(e.Mobile);
}
private static void Run(Mobile from)
{
if (from != null)
from.SendMessage(
"Running item/land art GDI+ parity across the art index — the shard will pause for a bit.");
Console.WriteLine("[Bridge] art parity: starting");
string report;
try
{
report = BridgeArt.RunArtParity(Oracle);
}
catch (Exception ex)
{
report = "[Bridge] art parity crashed: " + ex;
}
Console.WriteLine(report);
string path = null;
try
{
path = Path.Combine(Core.BaseDirectory, "parity-art.txt");
File.WriteAllText(path, report);
}
catch (Exception ex)
{
Console.WriteLine("[Bridge] art parity: cannot write file: " + ex.Message);
path = null;
}
if (from != null)
{
from.SendMessage("Art parity done."
+ (path != null ? " Report: " + path : " (see the console)"));
// The summary head, not the full detail list — the console and the file hold it all.
string[] lines = report.Split('\n');
for (int i = 0; i < lines.Length && i < 8; i++)
{
string line = lines[i].TrimEnd();
if (line.Length > 0)
from.SendMessage(line);
}
}
}
/// <summary>
/// The vendored decode for one art key, hued the way the shipping path chose and read
/// back to ARGB1555. False with <paramref name="available"/> still true means "no art
/// here"; false with it set false means the vendored path cannot run at all on this host
/// (libgdiplus <c>NotImplemented</c>), which is exactly the #39 fault and tells the
/// harness to stop asking the oracle.
/// </summary>
private static bool Oracle(bool land, int id, int hue, bool partial,
out ushort[] pixels, out int width, out int height, out bool available)
{
pixels = null;
width = 0;
height = 0;
available = true;
Bitmap bmp = null;
// With Files.CacheData off (BridgeAssets.Initialize guarantees it) GetStatic/GetLand
// decode a fresh instance that nothing else holds, so it is ours to hue in place and to
// dispose. The managed side's §4.5-equivalent re-check is why this flag is trusted.
bool owned = !Files.CacheData;
try
{
// `checkmaxid: false` matches the Bridge's own GetStatic call: the id is already
// bounded against the opened index, and the default would map an out-of-range id to
// item zero's picture.
bmp = land ? Art.GetLand(id) : Art.GetStatic(id, false);
if (bmp == null || bmp.Width <= 0 || bmp.Height <= 0)
{
bmp = null; // a null bitmap is not ours to dispose
return false;
}
if (hue > 0)
{
Hue[] list = Hues.List;
if (list != null && hue - 1 < list.Length && list[hue - 1] != null)
list[hue - 1].ApplyTo(bmp, partial);
}
width = bmp.Width;
height = bmp.Height;
pixels = ToArgb1555(bmp);
return pixels != null;
}
catch (NotImplementedException)
{
// libgdiplus 6.1 cannot create/lock the 16bpp bitmap — the #39 condition itself.
available = false;
return false;
}
catch (DllNotFoundException)
{
// No libgdiplus at all on this host: System.Drawing cannot run. Same verdict — the
// oracle is unavailable, not "this id has no art".
available = false;
return false;
}
catch (TypeInitializationException)
{
// The System.Drawing static ctor failed (typically wrapping the DllNotFoundException
// above). Still "oracle unavailable", not an EXTRA per id.
available = false;
return false;
}
catch
{
// Any other vendored failure: treat as no art for this key, keep the oracle alive.
return false;
}
finally
{
if (owned && bmp != null)
{
try
{
bmp.Dispose();
}
catch
{
// A fresh, uncached bitmap; nothing else holds it.
}
}
}
}
/// <summary>
/// Reads a 16bpp ARGB1555 bitmap into a <c>ushort[]</c> with <c>Marshal.Copy</c> — no
/// <c>unsafe</c> block, exactly as <see cref="BridgeAnimParity"/> reads its source.
/// </summary>
private static ushort[] ToArgb1555(Bitmap bmp)
{
var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData data = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format16bppArgb1555);
try
{
int w = bmp.Width;
int h = bmp.Height;
var pixels = new ushort[w * h];
var line = new short[w];
long scan = data.Scan0.ToInt64();
for (int y = 0; y < h; y++)
{
Marshal.Copy(new IntPtr(scan + ((long)y * data.Stride)), line, 0, w);
int rowBase = y * w;
for (int x = 0; x < w; x++)
pixels[rowBase + x] = (ushort)line[x];
}
return pixels;
}
finally
{
bmp.UnlockBits(data);
}
}
}
}