using System; namespace Server.Custom.Bridge { /// /// **The legacy-MUL animation reader** (servuo-plugins #39): the half of the GDI+-free /// migration that left undone. §4.4 moved the UOP path off /// System.Drawing because libgdiplus was archived in March 2025; this does the same /// for anim*.mul, and for a sharper reason — on a Linux/Mono shard the vendored /// Ultima.Frame ctor asks libgdiplus to create a 16bpp Bitmap /// (Settings.PixelFormat) and libgdiplus answers NotImplemented, so the whole /// catalogue's legacy art fails before a single pixel is read. /// /// This decodes the same bytes Ultima.Frame does, into an ARGB1555 ushort[] /// with no Bitmap anywhere, for to encode. The /// run loop is Frame's (Animations.cs), with the two bounds it does not have — /// exactly as added them to the UOP run loop: a run /// that would leave the canvas, or read past the record, refuses the frame rather than /// writing through a raw pointer the way the library does. /// /// ── **Legacy layout vs UOP** ── /// /// The pixel format is identical (shared palette of ARGB1555, DoubleXor run headers, /// the 0x7FFF7FFF terminator), but the container is not. A legacy record is a single /// shared palette of 0x100 entries at the very start, then a frame count and a lookup /// table, then the frames; a UOP record carries a per-frame palette after each /// frame's header. So this reads the palette once and seeks the first frame through the /// lookup table — it does not reuse 's offset math. /// public static class BridgeMul { /// The xor Ultima.Frame applies to every run header, and so must this. private const int DoubleXor = (0x200 << 22) | (0x200 << 12); /// The shared palette: 0x100 ARGB1555 entries at the head of every record. private const int PaletteBytes = 0x100 * 2; /// /// Decodes the first frame of a legacy animation record (what a /// FirstFrame: true GetAnimation call produces) into ARGB1555. /// /// is the raw record, verdata-patched, exactly as /// hands it over — this /// reads bytes only, never a stream, so every bound is a bound on a known array length. /// A 0x0 frame returns false with set, which is how the /// vendored ctor treats it too (an early return, not damage). /// public static bool TryDecodeFirstFrame(byte[] record, out ushort[] argb1555, out int width, out int height, out bool empty, out string reason) { argb1555 = null; width = 0; height = 0; empty = false; reason = null; if (record == null || record.Length < PaletteBytes + 4) { reason = "record is too short to hold a palette and frame count"; return false; } var palette = new ushort[0x100]; for (int i = 0; i < 0x100; i++) { // The library's own xor: the stored entry has its alpha bit clear and every // palette colour is opaque. A pixel no run covers stays zero, which is the // sprite's transparent background. palette[i] = (ushort)(BitConverter.ToUInt16(record, i * 2) ^ 0x8000); } int start = PaletteBytes; // GetAnimation's `start`, after the palette int frameCount = BitConverter.ToInt32(record, start); if (frameCount <= 0 || frameCount > BridgeAssetValidator.MaxAnimFrames) { reason = "record declares " + frameCount + " frames"; return false; } // Only the first frame is decoded, so only its lookup slot has to be inside the record. int lookupAt = start + 4; if ((long)lookupAt + 4 > record.Length) { reason = "frame lookup table runs past the record"; return false; } long frameAt = (long)start + BitConverter.ToInt32(record, lookupAt); // The frame header is xCenter(i16), yCenter(i16), width(u16), height(u16) = 8 bytes. if (frameAt < 0 || frameAt + 8 > record.Length) { reason = "frame points outside the record"; return false; } int at = (int)frameAt; int xCenter = BitConverter.ToInt16(record, at); int yCenter = BitConverter.ToInt16(record, at + 2); int w = BitConverter.ToUInt16(record, at + 4); int h = BitConverter.ToUInt16(record, at + 6); if (w == 0 || h == 0) { // No art, exactly as Frame's ctor returns early on width==0 || height==0. empty = true; reason = "frame is " + w + "x" + h; return false; } if (w > BridgeAssetValidator.MaxArtDimension || h > BridgeAssetValidator.MaxArtDimension) { reason = "frame declares " + w + "x" + h; return false; } var canvas = new ushort[w * h]; int p = at + 8; int xBase = xCenter - 0x200; int yBase = (yCenter + h) - 0x200; while (true) { if (p + 4 > record.Length) { reason = "frame has no terminator inside the record"; return false; } int header = BitConverter.ToInt32(record, p); p += 4; if (header == 0x7FFF7FFF) break; header ^= DoubleXor; int x = ((header >> 22) & 0x3FF) + xBase; int y = ((header >> 12) & 0x3FF) + yBase; int run = header & 0xFFF; if (run == 0) continue; if (p + run > record.Length) { reason = "frame has a run past the end of the record"; return false; } if (y < 0 || y >= h || x < 0 || x + run > w) { reason = "frame has a run at " + x + "," + y + " of " + run + " outside " + w + "x" + h; return false; } int cursor = (y * w) + x; for (int i = 0; i < run; i++) canvas[cursor + i] = palette[record[p + i]]; p += run; } argb1555 = canvas; width = w; height = h; return true; } } }