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

@@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
@@ -120,12 +117,6 @@ namespace Server.Custom.Bridge
public string Sha256;
}
// ── imaging (§4.4) ───────────────────────────────────────────────────────────────────
private static bool _imagingChecked;
private static bool _imagingOk;
private static string _imagingReason;
public static void Initialize()
{
if (!BridgeConfig.Enabled)
@@ -583,93 +574,6 @@ namespace Server.Custom.Bridge
Accept(reqId, "assets.manifest", () => manifest(reqId, cursor));
}
/// <summary>
/// ARGB1555 to a PNG with a transparent background.
///
/// <c>Frame</c> writes 16-bit ARGB1555: a pixel the sprite does not cover is left as
/// zero and a pixel it does cover carries the top bit set. Saving that format straight
/// to PNG asks GDI+ to make the conversion, and what it does with a one-bit alpha
/// channel varies by platform — on Mono it is a different implementation entirely. A
/// sprite that came back with a black rectangle behind it would look fine in a test
/// that only checked the bytes decoded, and wrong on every page that showed it.
///
/// So the expansion is done here, explicitly: alpha bit clear becomes fully
/// transparent, and each 5-bit channel is widened to 8 bits by repeating its high bits
/// (<c>(c &lt;&lt; 3) | (c &gt;&gt; 2)</c>) rather than by shifting alone, which would
/// cap white at 248 and tint the whole catalogue.
/// </summary>
internal static byte[] BitmapToPng(Bitmap source)
{
var rect = new Rectangle(0, 0, source.Width, source.Height);
if (source.PixelFormat != PixelFormat.Format16bppArgb1555)
{
// Not what this library has ever produced. Save it rather than reinterpret it:
// guessing at an unknown layout is how a catalogue fills with confident nonsense.
using (var ms = new MemoryStream())
{
source.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
}
using (var target = new Bitmap(source.Width, source.Height, PixelFormat.Format32bppArgb))
{
BitmapData src = source.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format16bppArgb1555);
BitmapData dst = null;
try
{
dst = target.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
var line = new short[source.Width];
var outLine = new int[source.Width];
for (int y = 0; y < source.Height; y++)
{
Marshal.Copy(new IntPtr(src.Scan0.ToInt64() + ((long)y * src.Stride)),
line, 0, source.Width);
for (int x = 0; x < source.Width; x++)
{
int p = line[x] & 0xFFFF;
if ((p & 0x8000) == 0)
{
outLine[x] = 0;
continue;
}
int r = (p >> 10) & 0x1F;
int g = (p >> 5) & 0x1F;
int b = p & 0x1F;
outLine[x] = unchecked((int)0xFF000000)
| (((r << 3) | (r >> 2)) << 16)
| (((g << 3) | (g >> 2)) << 8)
| ((b << 3) | (b >> 2));
}
Marshal.Copy(outLine, 0, new IntPtr(dst.Scan0.ToInt64() + ((long)y * dst.Stride)),
source.Width);
}
}
finally
{
if (dst != null)
target.UnlockBits(dst);
source.UnlockBits(src);
}
using (var ms = new MemoryStream())
{
target.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
}
}
/// <summary>
/// Does a caller's asserted catalog id disagree with what this shard holds?
///
@@ -835,7 +739,11 @@ namespace Server.Custom.Bridge
.Bool("assetsEnabled", BridgeConfig.AssetsEnabled)
.Bool("treeEnabled", BridgeConfig.TreeEnabled);
WriteImaging(sb);
// No imaging/NO_IMAGING field (servuo-plugins #39, PR 2): every picture family — the
// body catalogue (PR 1) and item/land art (PR 2) — now decodes into ARGB1555 with no
// System.Drawing, so this shard serves pictures whether or not the host has libgdiplus.
// The source gate no longer reports an imaging state because there is no longer one that
// gates anything.
// §4.6: whichever of art.mul / artLegacyMUL.uop `FileIndex` would actually open. An
// operator who added custom graphics to art.mul while the UOP is present is getting
@@ -968,88 +876,6 @@ namespace Server.Custom.Bridge
return (long)(utc - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
// ── imaging (§4.4) ───────────────────────────────────────────────────────────────────
/// <summary>
/// Whether this host can turn a record into a picture at all.
///
/// ServUO targets net48, so a Linux shard runs it under Mono, where
/// <c>System.Drawing</c> is a thin layer over **libgdiplus** — and §4.2 put
/// <c>System.Drawing</c> in the *decode* path, not merely the encode: <c>Frame</c>
/// writes ARGB1555 through a <c>LockBits</c> pointer. Without that library a Linux
/// shard cannot read a sprite at all, while clilocs and the atlas are unaffected
/// because neither touches pixels.
///
/// It must never present as a stack trace or a 500. It is a named, actionable outcome
/// in the same family as the cliloc reader's `COMPRESSED`, and it is reported here —
/// on the *source gate*, the first call any import makes — so an operator learns it
/// while setting the shard up rather than from an empty bestiary weeks later.
/// </summary>
private static void WriteImaging(StringBuilder sb)
{
CheckImaging();
sb.Append(",\"imaging\":{\"ok\":").Append(_imagingOk ? "true" : "false");
if (!_imagingOk)
{
sb.Append(",\"code\":\"NO_IMAGING\",\"reason\":");
BridgeJson.Text(sb,
"This shard host cannot render images — Mono's System.Drawing needs libgdiplus. "
+ "Install it (apt-get install libgdiplus) and re-run the import. Cliloc and atlas "
+ "import are unaffected. (" + _imagingReason + ")");
}
sb.Append('}');
}
/// <summary>
/// Whether this host can produce a picture, for the families that produce pictures.
///
/// <see cref="WriteImaging"/> reports this on the source gate so an operator learns it
/// while setting the shard up. The catalogue needs the same answer as a *decision* —
/// it must refuse rather than throw a <c>DllNotFoundException</c> out of the middle of
/// a decode loop — so the check itself is shared and this is its one accessor.
/// </summary>
internal static bool ImagingOk(out string reason)
{
CheckImaging();
reason = _imagingReason;
return _imagingOk;
}
private static void CheckImaging()
{
if (_imagingChecked)
return;
_imagingChecked = true;
try
{
TouchImaging();
_imagingOk = true;
}
catch (Exception e)
{
// On a host with no libgdiplus this is a TypeInitializationException wrapping a
// DllNotFoundException, and it can surface as the method failing to JIT rather
// than as a throw from inside it — which is why the construction lives in its own
// method, so the failure is contained here instead of taking this class's
// static initialisation with it.
_imagingOk = false;
_imagingReason = e.GetType().Name + ": " + e.Message;
}
}
private static void TouchImaging()
{
using (var bmp = new System.Drawing.Bitmap(1, 1))
{
bmp.SetPixel(0, 0, System.Drawing.Color.Black);
}
}
// ── the hash cache (§6) ──────────────────────────────────────────────────────────────
private static string CachedHashFor(string path, long size, long mtime)