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 PR 2 merge gate**: the GM command [BridgeArtParity], which proves the /// new GDI+-free item/land decode ( + , /// driven by ) is pixel-identical to the vendored /// Ultima.Art + Hue.ApplyTo path it replaces. /// /// Like for the catalogue, this is the one art file that /// still touches System.Drawing, and it does so only to be the oracle: on /// Windows it runs the vendored Art.GetStatic/GetLand (and, for the hue pass, /// Hue.ApplyTo), 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 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); } } } /// /// The vendored decode for one art key, hued the way the shipping path chose and 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(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. } } } } /// /// Reads a 16bpp ARGB1555 bitmap into a ushort[] with Marshal.Copy — no /// unsafe block, exactly as 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); } } } }