using System; namespace Server.Custom.Bridge { /// /// **The item-and-land art reader** (servuo-plugins #39, PR 2): the half of the GDI+-free /// migration (animations) and (UOP bodies) /// left for the art plane. On a Linux/Mono shard the vendored Ultima.Art.LoadStatic /// and LoadLand build a 16bpp Bitmap (Settings.PixelFormat) and /// libgdiplus 6.1 answers NotImplemented, so every item and land picture the Asset /// Bridge serves fails before a pixel is read. This decodes the same bytes into an ARGB1555 /// ushort[] with no Bitmap anywhere, for /// to encode and to tint. /// /// ── **Faithful to the vendored run loops, with the bounds they lack** ── /// /// LoadStatic's inner loop advances a raw pointer by a run header's /// (xOffset, xRun) pair and writes xRun palette-less ARGB1555 pixels /// (stored ^ 0x8000), stopping a row on the (0,0) terminator or when a pair /// would leave the scanline. It guards neither the read cursor nor the write against the /// record — is the shipped gate that /// walks the same table bounded, and this reader re-checks every read and every write /// against the known array length anyway rather than writing through a pointer the way the /// library does. Two behaviours differ from a raw pointer, and they differ from each other: /// /// - **Row-stop (truncate).** `xOffset > width` or `xOffset + xRun > width` *stops the row* /// and moves on — exactly what LoadStatic does against the padded 16bpp stride /// (`delta`), and what StaticRecordSane does against `width`. Because this reader /// bounds against the real `width` rather than `delta`, the two can differ by one case: a /// run whose last pixel lands in a scanline's invisible pad column (only possible for an /// odd width) — the library writes it into the pad, this truncates the row. No record this /// client's own packer produces reaches it, and the pixel is invisible either way. /// - **Canvas-overflow (refuse).** If the cumulative column plus a run would cross into the /// next scanline (`col + xRun > width`), the library bleeds a visible run into the next /// row through its pointer; this *refuses the whole record* (returns false). A refusal is /// surfaced to the merge-gate harness as a possible miss rather than silently dropped. /// /// LoadLand is fixed geometry: a 44×44 diamond of 1,012 stored ARGB1555 values /// (stored | 0x8000), two triangular halves, read straight off the record with no run /// headers. guarantees the 2,024 bytes the /// vendored reader consumes regardless of the declared length are present. /// public static class BridgeArtMul { /// /// Decodes one static item record (art.mul / artLegacyMUL.uop) into /// ARGB1555. is the raw, verdata-patched record exactly as /// hands it over. /// /// Returns false with set for a 0×0 (or non-positive) record — /// the same early return LoadStatic makes before it builds a bitmap, which the /// Bridge treats as "no picture here" rather than damage. Any other false is a record /// this reader refused, with set. /// public static bool TryDecodeStatic(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; // The 8-byte header: a 4-byte flag, then width and height as ushorts. if (record == null || record.Length < 8) { reason = "static record is too short to hold an 8-byte header"; return false; } int w = BitConverter.ToUInt16(record, 4); int h = BitConverter.ToUInt16(record, 6); // LoadStatic returns null here rather than misbehaving: no art, not damage. if (w <= 0 || h <= 0) { empty = true; reason = "static is " + w + "x" + h; return false; } if (w > BridgeAssetValidator.MaxArtDimension || h > BridgeAssetValidator.MaxArtDimension) { reason = "static declares " + w + "x" + h; return false; } int words = record.Length / 2; // The row-lookup table: h ushorts starting at word 4. if (4 + h > words) { reason = "row table (" + h + " entries) does not fit in a " + record.Length + "-byte record"; return false; } int start = h + 4; var canvas = new ushort[w * h]; for (int y = 0; y < h; y++) { // GetStatic's `lookups[y] = start + bindata[4 + y]`, in ushort units. int count = start + BitConverter.ToUInt16(record, (4 + y) * 2); int rowBase = y * w; int col = 0; // cur - line, in pixels — cumulative across this row's pairs while (true) { // Two ushorts for the run header, both inside the record. if (count < 0 || count + 1 >= words) { reason = "row " + y + " reads at word " + count + ", past the record's " + words; return false; } int xOffset = BitConverter.ToUInt16(record, count * 2); int xRun = BitConverter.ToUInt16(record, (count + 1) * 2); count += 2; // The (0,0) terminator LoadStatic stops the row on. if (xOffset + xRun == 0) break; // LoadStatic stops the row here (against the scanline); the read cursor stops // with it. StaticRecordSane makes the identical test against width. if (xOffset > w || xOffset + xRun > w) break; col += xOffset; // The run's pixels must be inside the record... if (count + xRun > words) { reason = "row " + y + " declares a " + xRun + "-pixel run running past the record"; return false; } // ...and inside the row. The vendored writer bounds each pair but not the // cumulative column; no record this client packs crosses the scanline, and one // that did would have the library bleed into the next row rather than refuse. if (col < 0 || col + xRun > w) { reason = "row " + y + " writes a run to column " + col + "+" + xRun + ", past " + w; return false; } int at = rowBase + col; for (int i = 0; i < xRun; i++) canvas[at + i] = (ushort)(BitConverter.ToUInt16(record, (count + i) * 2) ^ 0x8000); count += xRun; col += xRun; } } argb1555 = canvas; width = w; height = h; return true; } /// The land diamond is always 44×44 — LoadLand hard-codes it. private const int LandSize = 44; /// /// Decodes one land tile record into a 44×44 ARGB1555 diamond, mirroring /// LoadLand: two triangular halves of stored values, stored | 0x8000, read /// straight off the record. The four corners outside the diamond are never written and /// stay transparent, exactly as the vendored fresh bitmap leaves them. /// public static bool TryDecodeLand(byte[] record, out ushort[] argb1555, out int width, out int height, out string reason) { argb1555 = null; width = 0; height = 0; reason = null; // LoadLand reads 2,024 bytes whatever the declared length; LandLengthSane is the gate // that refuses a shorter record, and this re-states the bound on the known array. if (record == null || record.Length < BridgeAssetValidator.LandRecordBytes) { reason = "land record is " + (record == null ? 0 : record.Length) + " bytes; LoadLand always reads " + BridgeAssetValidator.LandRecordBytes; return false; } var canvas = new ushort[LandSize * LandSize]; int src = 0; // byte offset of the next stored ushort // Top half: rows 0..21 widen from 2 to 44, centred. int xOffset = 21; int xRun = 2; for (int y = 0; y < 22; y++, xOffset--, xRun += 2) { int at = (y * LandSize) + xOffset; for (int i = 0; i < xRun; i++, src += 2) canvas[at + i] = (ushort)(BitConverter.ToUInt16(record, src) | 0x8000); } // Bottom half: rows 22..43 narrow from 44 back to 2. xOffset = 0; xRun = 44; for (int y = 22; y < 44; y++, xOffset++, xRun -= 2) { int at = (y * LandSize) + xOffset; for (int i = 0; i < xRun; i++, src += 2) canvas[at + i] = (ushort)(BitConverter.ToUInt16(record, src) | 0x8000); } argb1555 = canvas; width = LandSize; height = LandSize; return true; } } }