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 { /// /// **The #39 merge gate**: the GM command [BridgeAnimParity], which proves the new /// GDI+-free legacy decode ( + , driven by /// ) is pixel-identical to the vendored /// Ultima.Frame path it replaces. /// /// This is the one file left that still touches System.Drawing, and it does so only /// to be the oracle: on Windows it runs the vendored GetAnimation, 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 /// NotImplemented (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. /// 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); } } } /// /// The vendored decode for one key, read back to ARGB1555. False with /// still true means "no art here"; false with it set false /// means the vendored path cannot run at all on this host (libgdiplus /// NotImplemented), which is exactly the #39 fault and tells the harness to stop /// asking the oracle. /// 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. } } } } /// /// Reads a 16bpp ARGB1555 bitmap into a ushort[] with Marshal.Copy — no /// unsafe block, exactly as BridgeAssets.BitmapToPng reads its source. /// 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); } } } }