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
This commit is contained in:
213
overlay/Scripts/Custom/Bridge/BridgeAnimParity.cs
Normal file
213
overlay/Scripts/Custom/Bridge/BridgeAnimParity.cs
Normal file
@@ -0,0 +1,213 @@
|
||||
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 merge gate**: the GM command <c>[BridgeAnimParity]</c>, which proves the new
|
||||
/// GDI+-free legacy decode (<see cref="BridgeMul"/> + <see cref="BridgeHue"/>, driven by
|
||||
/// <see cref="BridgeCatalog.RunAnimParity"/>) is pixel-identical to the vendored
|
||||
/// <c>Ultima.Frame</c> path it replaces.
|
||||
///
|
||||
/// This is the one file left 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>GetAnimation</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 BridgeAnimParity
|
||||
{
|
||||
public static void Initialize()
|
||||
{
|
||||
CommandSystem.Register("BridgeAnimParity", AccessLevel.Administrator, OnCommand);
|
||||
|
||||
// Headless gate run (servuo-plugins #39): with BRIDGE_ANIM_PARITY=1 in the environment,
|
||||
// run the parity once the world is up and write parity-anim.txt, no client needed. For
|
||||
// verification/CI only — it is never set on a production shard.
|
||||
if (Environment.GetEnvironmentVariable("BRIDGE_ANIM_PARITY") == "1")
|
||||
EventSink.ServerStarted += OnServerStartedParity;
|
||||
}
|
||||
|
||||
private static void OnServerStartedParity()
|
||||
{
|
||||
Console.WriteLine("[Bridge] anim parity: BRIDGE_ANIM_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 legacy anim GDI+ parity across the catalogue — the shard will pause for a bit.");
|
||||
|
||||
Console.WriteLine("[Bridge] anim parity: starting");
|
||||
|
||||
string report;
|
||||
|
||||
try
|
||||
{
|
||||
report = BridgeCatalog.RunAnimParity(Oracle);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
report = "[Bridge] anim parity crashed: " + ex;
|
||||
}
|
||||
|
||||
Console.WriteLine(report);
|
||||
|
||||
string path = null;
|
||||
|
||||
try
|
||||
{
|
||||
path = Path.Combine(Core.BaseDirectory, "parity-anim.txt");
|
||||
File.WriteAllText(path, report);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("[Bridge] anim parity: cannot write file: " + ex.Message);
|
||||
path = null;
|
||||
}
|
||||
|
||||
if (from != null)
|
||||
{
|
||||
from.SendMessage("Anim 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 < 6; i++)
|
||||
{
|
||||
string line = lines[i].TrimEnd();
|
||||
|
||||
if (line.Length > 0)
|
||||
from.SendMessage(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The vendored decode for one key, 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(int body, int action, int direction,
|
||||
out ushort[] pixels, out int width, out int height, out bool available)
|
||||
{
|
||||
pixels = null;
|
||||
width = 0;
|
||||
height = 0;
|
||||
available = true;
|
||||
|
||||
Bitmap bmp = null;
|
||||
|
||||
try
|
||||
{
|
||||
int hue = 0;
|
||||
|
||||
// Exactly the call the old BridgeCatalog.Decode made: preserveHue false, first
|
||||
// frame. The vendored path applies the body.def hue to the bitmap internally, so
|
||||
// these pixels are already hued — matching what BridgeHue does on the managed side.
|
||||
Frame[] frames = Animations.GetAnimation(body, action, direction, ref hue, false, true);
|
||||
|
||||
if (frames == null || frames.Length == 0 || frames[0] == null)
|
||||
return false;
|
||||
|
||||
bmp = frames[0].Bitmap;
|
||||
|
||||
if (bmp == null || bmp.Width <= 0 || bmp.Height <= 0)
|
||||
{
|
||||
bmp = null; // Frame.Empty / a null bitmap is not ours to dispose.
|
||||
return false;
|
||||
}
|
||||
|
||||
width = bmp.Width;
|
||||
height = bmp.Height;
|
||||
pixels = ToArgb1555(bmp);
|
||||
|
||||
return pixels != null;
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
{
|
||||
// libgdiplus cannot create/lock the 16bpp bitmap — the #39 condition itself.
|
||||
available = false;
|
||||
return false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Any other vendored failure: treat as no art for this key, keep the oracle alive.
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (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 <c>BridgeAssets.BitmapToPng</c> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user