fix(bridge): decode item and land art without GDI+ (#39, 2 of 2)
On a Linux/Mono shard the Asset Bridge's item and land art failed: the vendored Art.LoadStatic/LoadLand build a 16bpp Bitmap (Settings.PixelFormat) and libgdiplus 6.1 cannot create one, so every static and land picture threw [GDI+ NotImplemented] before a pixel was read. PR 1 moved the body catalogue off System.Drawing; this finishes the migration for the art plane, so Windows and Linux now share one managed path and the whole Asset Bridge needs no libgdiplus. - BridgeArtMul: a hand-written art.mul static + land decoder to ARGB1555, mirroring Art.LoadStatic's run loop and LoadLand's 44x44 diamond, with the record/canvas bounds the vendored writers lack (no System.Drawing, no Bitmap). - BridgeArt: Render now decodes through BridgeArtMul and hues through BridgeHue (over hues.mul + the tiledata PartialHue flag) into BridgePng, via a shared TryArtPixels helper the parity harness drives. The GDI+ Bitmap/ApplyTo/dispose path, the imaging gate, and the Files.CacheData re-check in TryHue are gone, because a key now decodes its own fresh buffer that nothing else aliases. - BridgeAssets: retire BitmapToPng (its last caller) and the imaging probe/field machinery; assets.sources no longer carries an imaging field. No decode goes through System.Drawing any more, so the NO_IMAGING refusal is retired with the GDI+ it guarded. Files.CacheData stays off, now only so the parity oracle's vendored decode hands back fresh bitmaps. - BridgeArtParity: a [BridgeArtParity] GM command / BRIDGE_ART_PARITY=1 boot run that compares the managed decode+hue against the vendored one pixel for pixel. Overlay-only; no wire change beyond the retired imaging field, no PROTOCOL_VERSION bump. The website tolerates the field's absence (its NO_IMAGING branch is guarded by `sources.imaging &&`); the dead NO_IMAGING UI/model in module-uo and the installer doctor libgdiplus row are removed in a tracked follow-up. Verified on this machine. Windows (vendored GDI+ oracle available): the parity harness compared every served picture and every one is pixel-identical to the vendored decode — 43,433/43,433 (39,189 statics + 4,244 land) with no hue, then 39,189/39,189 again hued in each item's PartialHue mode, 0 mismatched. On WSL2 Ubuntu 24.04 / Mono 6.8 / libgdiplus 6.1 (the live-host match) the vendored oracle threw NotImplemented and the managed path produced all 43,433 + 39,189. dotnet build 0/0. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014HJEmcaKvWQ8LsnXHKha5y
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
@@ -51,19 +50,26 @@ namespace Server.Custom.Bridge
|
||||
/// the same trade §2.1 already refused. So hue is part of the key, and the key is resolved
|
||||
/// where the files are.
|
||||
///
|
||||
/// ── **The trap this phase existed to find** ──
|
||||
/// ── **The trap this phase existed to find, and why it is gone** ──
|
||||
///
|
||||
/// <c>Art.GetStatic</c> memoises into a static <c>Bitmap[0xFFFF]</c> and returns **the same
|
||||
/// instance** every time; <c>Hue.ApplyTo</c> repaints a bitmap **in place**. Hue a static
|
||||
/// once and the library's own copy is hued from then on — the plain key comes back hued,
|
||||
/// and the next hue stacks on the last. It is §4.5's failure mode (a confident, plausible,
|
||||
/// wrong picture that every success count agrees with) reached through a door §4.5 never
|
||||
/// looked at, because phase 0 was auditing *records* and this is the library's *cache*.
|
||||
/// The hue is applied in place, and the picture it is applied to must be this call's alone.
|
||||
/// The vendored path made that hard: <c>Art.GetStatic</c> memoised into a static
|
||||
/// <c>Bitmap[0xFFFF]</c> and returned **the same instance** every time, while
|
||||
/// <c>Hue.ApplyTo</c> repainted a bitmap **in place** — so hueing a static once left the
|
||||
/// library's own copy hued from then on, the plain key came back hued, and the next hue
|
||||
/// stacked on the last. It was §4.5's failure mode (a confident, plausible, wrong picture
|
||||
/// that every success count agrees with) reached through the library's *cache* rather than
|
||||
/// a *record*. Phase 5 defused it by turning <c>Files.CacheData</c> off for the process and
|
||||
/// re-checking that invariant before every hue.
|
||||
///
|
||||
/// <see cref="BridgeAssets.Initialize"/> turns <c>Files.CacheData</c> off for the life of
|
||||
/// the process, which makes every bitmap this file receives its own. That invariant is
|
||||
/// load-bearing enough that <see cref="Render"/> **re-checks it** before applying a hue and
|
||||
/// refuses rather than risk it: an invariant nothing verifies is a comment.
|
||||
/// As of #39 PR 2 the trap cannot be set at all: <see cref="BridgeArtMul"/> decodes each key
|
||||
/// into **its own fresh <c>ushort[]</c>** off the raw record (no <c>Bitmap</c>, no library
|
||||
/// cache), and <see cref="BridgeHue.Apply"/> repaints that private buffer, so the plain key
|
||||
/// and the hued key never share pixels no matter what <c>Files.CacheData</c> says. The whole
|
||||
/// path is GDI+-free, which is the point — a Linux/Mono shard serves item and land art it
|
||||
/// could not decode before. (<see cref="BridgeAssets.Initialize"/> still turns
|
||||
/// <c>Files.CacheData</c> off, now only so the <c>[BridgeArtParity]</c> oracle's vendored
|
||||
/// decode hands back fresh bitmaps.)
|
||||
///
|
||||
/// ── **What is validated, and against what** ──
|
||||
///
|
||||
@@ -161,18 +167,11 @@ namespace Server.Custom.Bridge
|
||||
/// </summary>
|
||||
private static void ReplyFetch(string reqId, List<string> keys, string expected, string cursor)
|
||||
{
|
||||
string imagingReason;
|
||||
|
||||
if (!BridgeAssets.ImagingOk(out imagingReason))
|
||||
{
|
||||
// §17.9: a flat refusal, not a partial answer. Every picture in this family needs
|
||||
// a decoder that goes through GDI+, so there is no half of it to serve.
|
||||
BridgeAssets.Fail(reqId, "UNAVAILABLE",
|
||||
"this shard host cannot render images - Mono's System.Drawing needs "
|
||||
+ "libgdiplus. (" + imagingReason + ")");
|
||||
return;
|
||||
}
|
||||
|
||||
// No imaging gate (servuo-plugins #39, PR 2): item and land art now decode through the
|
||||
// managed BridgeArtMul -> BridgeHue -> BridgePng path, with no System.Drawing anywhere,
|
||||
// so a Linux/Mono host with no libgdiplus serves this family exactly as Windows does.
|
||||
// The flat NO_IMAGING refusal this family carried until #39 is gone with the GDI+ it
|
||||
// guarded.
|
||||
string id = SourceId();
|
||||
|
||||
if (BridgeAssets.CatalogMismatch(expected, id))
|
||||
@@ -363,17 +362,78 @@ namespace Server.Custom.Bridge
|
||||
if (!TryParseKey(key, out land, out id, out hue))
|
||||
return Unsupported(key, "not a key this shard serves");
|
||||
|
||||
int at = land ? id : StaticBase + id;
|
||||
|
||||
ushort[] pixels;
|
||||
int width, height;
|
||||
bool partial, decodeRefused;
|
||||
string reason;
|
||||
|
||||
ArtOutcome outcome = TryArtPixels(readers, land, id, at, hue,
|
||||
out pixels, out width, out height, out partial, out decodeRefused, out reason);
|
||||
|
||||
if (outcome == ArtOutcome.Unsupported)
|
||||
return Unsupported(key, reason);
|
||||
|
||||
if (outcome != ArtOutcome.Ok)
|
||||
return Absent(key, reason ?? "the decoder returned no picture");
|
||||
|
||||
byte[] png = BridgePng.FromArgb1555(pixels, width, height);
|
||||
|
||||
if (png == null)
|
||||
return Absent(key, "the picture could not be encoded");
|
||||
|
||||
return new Rendered
|
||||
{
|
||||
Key = key,
|
||||
Status = "ok",
|
||||
Sha256 = BridgeAssets.Sha256Hex(png),
|
||||
Png = png,
|
||||
Width = width,
|
||||
Height = height,
|
||||
Hue = hue,
|
||||
PartialHue = partial,
|
||||
Source = readers.Source
|
||||
};
|
||||
}
|
||||
|
||||
private enum ArtOutcome { Ok, Absent, Unsupported }
|
||||
|
||||
/// <summary>
|
||||
/// The gate, the managed decode and the hue for one art key, to a hued ARGB1555 buffer:
|
||||
/// the exact path <see cref="Render"/> encodes and <see cref="RunArtParity"/> proves, so
|
||||
/// a green parity run is evidence about the code that ships rather than a copy of it.
|
||||
///
|
||||
/// <paramref name="at"/> is the index entry — a land id, or <c>0x4000 + id</c> for a
|
||||
/// static. <see cref="ArtOutcome.Unsupported"/> is a key this shard does not serve (a
|
||||
/// hue it cannot resolve, an id past the index); <see cref="ArtOutcome.Absent"/> is a
|
||||
/// key it serves but has no picture for (the §4.5 phantoms, a damaged record, a 0×0
|
||||
/// static); <see cref="ArtOutcome.Ok"/> fills <paramref name="pixels"/>.
|
||||
/// </summary>
|
||||
private static ArtOutcome TryArtPixels(Readers readers, bool land, int id, int at, int hue,
|
||||
out ushort[] pixels, out int width, out int height, out bool partial,
|
||||
out bool decodeRefused, out string reason)
|
||||
{
|
||||
pixels = null;
|
||||
width = 0;
|
||||
height = 0;
|
||||
partial = false;
|
||||
decodeRefused = false;
|
||||
reason = null;
|
||||
|
||||
FileIndex index = readers.Index;
|
||||
|
||||
if (index == null || index.Index == null)
|
||||
return Absent(key, "this shard has no art file");
|
||||
|
||||
int at = land ? id : StaticBase + id;
|
||||
{
|
||||
reason = "this shard has no art file";
|
||||
return ArtOutcome.Absent;
|
||||
}
|
||||
|
||||
if (at < 0 || at >= index.Index.Length)
|
||||
return Unsupported(key, "id " + id + " is past the end of this client's art index");
|
||||
|
||||
string reason;
|
||||
{
|
||||
reason = "id " + id + " is past the end of this client's art index";
|
||||
return ArtOutcome.Unsupported;
|
||||
}
|
||||
|
||||
BridgeAssetValidator.Verdict verdict =
|
||||
BridgeAssetValidator.CheckEntry(index, at, readers.DataLength, readers.VerdataLength, out reason);
|
||||
@@ -383,106 +443,97 @@ namespace Server.Custom.Bridge
|
||||
// The 9,962 statics and 12,140 land tiles of §4.5: an index entry that reads
|
||||
// `lookup 0, length 0`, which the library treats as a hit and answers with the
|
||||
// previous asset's pixels. Absent is the true answer and the only safe one.
|
||||
return Absent(key, reason);
|
||||
return ArtOutcome.Absent;
|
||||
}
|
||||
|
||||
if (verdict != BridgeAssetValidator.Verdict.Ok)
|
||||
{
|
||||
// A damaged record rather than a missing one. Still absent to the website — there
|
||||
// is no picture either way — but the reason is worth carrying, because this one an
|
||||
// operator can act on.
|
||||
Console.WriteLine("[Bridge] art: {0} refused: {1}", key, reason);
|
||||
return Absent(key, reason);
|
||||
// A damaged record rather than a missing one. Still absent to the website — there is
|
||||
// no picture either way — but the reason is worth carrying.
|
||||
Console.WriteLine("[Bridge] art: entry {0} refused: {1}", at, reason);
|
||||
return ArtOutcome.Absent;
|
||||
}
|
||||
|
||||
if (land)
|
||||
{
|
||||
if (!BridgeAssetValidator.LandLengthSane(index, at, out reason))
|
||||
{
|
||||
Console.WriteLine("[Bridge] art: {0} refused: {1}", key, reason);
|
||||
return Absent(key, reason);
|
||||
Console.WriteLine("[Bridge] art: land {0} refused: {1}", id, reason);
|
||||
return ArtOutcome.Absent;
|
||||
}
|
||||
}
|
||||
else if (readers.Reader == null || !readers.Reader.StaticSane(index, at, out reason))
|
||||
{
|
||||
Console.WriteLine("[Bridge] art: {0} refused: {1}",
|
||||
key, reason ?? "the art record could not be read");
|
||||
Console.WriteLine("[Bridge] art: static {0} refused: {1}",
|
||||
id, reason ?? "the art record could not be read");
|
||||
|
||||
return Absent(key, reason ?? "the art record could not be read");
|
||||
return ArtOutcome.Absent;
|
||||
}
|
||||
|
||||
// A hue is resolved BEFORE anything is decoded, so a bad one costs no pixels and, more
|
||||
// to the point, cannot half-apply to a picture that then gets cached and served.
|
||||
Ultima.Hue applied = null;
|
||||
bool partial = false;
|
||||
// A hue is resolved BEFORE anything is decoded, so a bad one costs no pixels and cannot
|
||||
// half-apply to a picture that then gets served.
|
||||
short[] hueColors = null;
|
||||
|
||||
if (hue > 0)
|
||||
{
|
||||
if (!TryHue(id, hue, out applied, out partial, out reason))
|
||||
return Unsupported(key, reason);
|
||||
if (!TryHue(id, hue, out hueColors, out partial, out reason))
|
||||
return ArtOutcome.Unsupported;
|
||||
}
|
||||
|
||||
Bitmap bitmap = land
|
||||
? Art.GetLand(id)
|
||||
// `checkmaxid: false` on purpose (§4.5): the default maps an out-of-range id to 0
|
||||
// and returns ITEM ZERO'S PICTURE. The id is already bounded against the index
|
||||
// that was actually opened, so this can only be loud.
|
||||
: Art.GetStatic(id, false);
|
||||
|
||||
// **Whether this bitmap is ours to dispose is the same question as whether it is ours
|
||||
// to hue**, and it has the same answer. With the library's cache off — which
|
||||
// `BridgeAssets.Initialize` guarantees and `TryHue` re-checks — every call decodes a
|
||||
// fresh instance that nothing else holds, so not disposing it would leak one bitmap per
|
||||
// fetched key. With the cache on, that instance is the library's own copy and disposing
|
||||
// it would leave a disposed `Bitmap` in a static array for the next caller to fault on.
|
||||
// Both mistakes are silent; the flag decides, once, here.
|
||||
bool owned = !Files.CacheData;
|
||||
|
||||
try
|
||||
if (readers.Reader == null)
|
||||
{
|
||||
if (bitmap == null || bitmap.Width <= 0 || bitmap.Height <= 0)
|
||||
return Absent(key, "the decoder returned no picture");
|
||||
reason = "this shard cannot read the art file";
|
||||
return ArtOutcome.Absent;
|
||||
}
|
||||
|
||||
if (applied != null)
|
||||
applied.ApplyTo(bitmap, partial);
|
||||
byte[] record = readers.Reader.ReadRecordCopy(index, at, out reason);
|
||||
|
||||
byte[] png = BridgeAssets.BitmapToPng(bitmap);
|
||||
if (record == null)
|
||||
{
|
||||
reason = reason ?? "the art record could not be read";
|
||||
return ArtOutcome.Absent;
|
||||
}
|
||||
|
||||
if (png == null)
|
||||
return Absent(key, "the picture could not be encoded");
|
||||
// Raw record bytes -> ARGB1555 ushort[], no System.Drawing (servuo-plugins #39, PR 2).
|
||||
// Each key decodes its own fresh buffer that nothing else holds, so the hue below
|
||||
// repaints a private array — the vendored path's shared-Bitmap aliasing hazard (see the
|
||||
// class summary) cannot occur, and there is nothing to dispose.
|
||||
bool empty = false;
|
||||
|
||||
return new Rendered
|
||||
bool decoded = land
|
||||
? BridgeArtMul.TryDecodeLand(record, out pixels, out width, out height, out reason)
|
||||
: BridgeArtMul.TryDecodeStatic(record, out pixels, out width, out height, out empty, out reason);
|
||||
|
||||
if (!decoded)
|
||||
{
|
||||
// A 0×0 static is no art rather than damage — the vendored decoder returns early on
|
||||
// the same condition — so it is silently absent. Anything else is the managed
|
||||
// decoder refusing a record the gate admitted (a cumulative-column overflow, a run
|
||||
// past the record): worth a line, and — because the vendored path might still render
|
||||
// it — surfaced to the parity harness through `decodeRefused` so it cannot hide in
|
||||
// the skip bucket.
|
||||
if (!empty)
|
||||
{
|
||||
Key = key,
|
||||
Status = "ok",
|
||||
Sha256 = BridgeAssets.Sha256Hex(png),
|
||||
Png = png,
|
||||
Width = bitmap.Width,
|
||||
Height = bitmap.Height,
|
||||
Hue = hue,
|
||||
PartialHue = partial,
|
||||
Source = readers.Source
|
||||
};
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (owned && bitmap != null)
|
||||
bitmap.Dispose();
|
||||
decodeRefused = true;
|
||||
Console.WriteLine("[Bridge] art: {0} {1} refused: {2}", land ? "land" : "static", id, reason);
|
||||
}
|
||||
|
||||
return ArtOutcome.Absent;
|
||||
}
|
||||
|
||||
if (hueColors != null)
|
||||
BridgeHue.Apply(pixels, hueColors, partial);
|
||||
|
||||
return ArtOutcome.Ok;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves one wire hue onto a ramp, and decides whether it repaints the whole sprite
|
||||
/// or only its grey pixels.
|
||||
///
|
||||
/// Four things have to hold, and every one of them has a way of not holding that
|
||||
/// Three things have to hold, and every one of them has a way of not holding that
|
||||
/// produces a picture rather than an error:
|
||||
///
|
||||
/// **The library's cache is off.** Re-checked here because <c>ApplyTo</c> repaints in
|
||||
/// place: with the cache on, this would edit the copy <c>Art</c> hands to everyone
|
||||
/// else. <see cref="BridgeAssets.Initialize"/> turns it off at boot and this refuses
|
||||
/// if it somehow did not, because the failure is invisible and permanent.
|
||||
///
|
||||
/// **`hues.mul` is present.** When it is missing <c>Hues.Initialize</c> does not throw
|
||||
/// — it fills all 3,000 slots with a <c>new Hue(index)</c> whose ramp is **all zeroes**,
|
||||
/// and applying one of those paints the sprite black. An all-zero ramp is therefore
|
||||
@@ -499,19 +550,12 @@ namespace Server.Custom.Bridge
|
||||
/// **Land has no such flag**, which is why <see cref="TryParseKey"/> does not accept a
|
||||
/// hue on a land key at all rather than guessing a mode for it.
|
||||
/// </summary>
|
||||
private static bool TryHue(int id, int hue, out Ultima.Hue applied, out bool partial, out string reason)
|
||||
private static bool TryHue(int id, int hue, out short[] colors, out bool partial, out string reason)
|
||||
{
|
||||
applied = null;
|
||||
colors = null;
|
||||
partial = false;
|
||||
reason = null;
|
||||
|
||||
if (Files.CacheData)
|
||||
{
|
||||
reason = "this shard's art cache is on, so a hue cannot be applied safely";
|
||||
Console.WriteLine("[Bridge] art: refusing hue {0}: {1}", hue, reason);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (hue < 1 || hue > MaxHue)
|
||||
{
|
||||
reason = "hue " + hue + " is outside 1-" + MaxHue;
|
||||
@@ -537,7 +581,7 @@ namespace Server.Custom.Bridge
|
||||
if (!TryPartialHue(id, out partial, out reason))
|
||||
return false;
|
||||
|
||||
applied = candidate;
|
||||
colors = candidate.Colors;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -838,5 +882,269 @@ namespace Server.Custom.Bridge
|
||||
_cache.Id, _cache.ByKey.Count, _cache.Bytes, BridgeConfig.AssetArtCacheBytes);
|
||||
}
|
||||
}
|
||||
|
||||
// ── the #39 PR 2 merge gate: managed decode/hue vs the vendored GDI+ path ──────────────
|
||||
|
||||
/// <summary>
|
||||
/// The vendored decode for one art key, read back to ARGB1555, as the oracle for
|
||||
/// <see cref="RunArtParity"/>. Supplied by <c>BridgeArtParity</c> — the only art file
|
||||
/// that still touches <c>System.Drawing</c> — so this file stays GDI+-free.
|
||||
/// <paramref name="hue"/> is 0 for the decode pass or a wire hue for the hue pass, and
|
||||
/// <paramref name="partial"/> is the mode the shipping path chose (from
|
||||
/// <c>tiledata.mul</c>), so the vendored <c>Hue.ApplyTo</c> tints identically.
|
||||
/// <paramref name="available"/> false means the vendored bitmap cannot run on this host
|
||||
/// (libgdiplus <c>NotImplemented</c> — the very #39 fault), which drops the harness into
|
||||
/// candidate-only (does-it-render) mode for the rest of the walk.
|
||||
/// </summary>
|
||||
public delegate bool ArtParityOracle(bool land, int id, int hue, bool partial,
|
||||
out ushort[] pixels, out int width, out int height, out bool available);
|
||||
|
||||
/// <summary>A real, non-empty hue on every client — the hue pass tints each served static with it.</summary>
|
||||
private const int ParityHue = 33;
|
||||
|
||||
private const int ParityMaxShown = 20;
|
||||
|
||||
private sealed class ParityCounts
|
||||
{
|
||||
public long Resolved, Matched, Mismatched, Missed, Extra, CandidateOnly, Skipped;
|
||||
public readonly StringBuilder Detail = new StringBuilder();
|
||||
public int Shown;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares the shipping managed decode+hue (<see cref="TryArtPixels"/>) against the
|
||||
/// vendored GDI+ path, pixel for pixel, across every served item and land id. The #39
|
||||
/// PR 2 merge gate: on Windows the vendored path is the oracle and every served picture
|
||||
/// must match it; on a Linux/Mono host the oracle is unavailable (the 16bpp
|
||||
/// <c>Bitmap</c> throws <c>NotImplemented</c>) and the harness instead confirms the
|
||||
/// managed path still renders where the art index says it should.
|
||||
///
|
||||
/// Two passes. The first decodes every served static and land id with no hue — the bulk
|
||||
/// parity. The second tints every served static with <see cref="ParityHue"/> in the mode
|
||||
/// its <c>tiledata.mul</c> <c>PartialHue</c> flag dictates, which is the only pass that
|
||||
/// exercises the grey-only branch on the ~13,259 ids that need it. Land takes no hue.
|
||||
/// </summary>
|
||||
public static string RunArtParity(ArtParityOracle oracle)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine("[Bridge] item/land art GDI+-free parity (servuo-plugins #39, PR 2)");
|
||||
|
||||
if (oracle == null)
|
||||
{
|
||||
sb.AppendLine(" no oracle supplied");
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
using (var readers = new Readers())
|
||||
{
|
||||
FileIndex index = readers.Index;
|
||||
|
||||
if (index == null || index.Index == null)
|
||||
{
|
||||
sb.AppendLine(" this shard has no art index");
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
// Statics address 0x4000 + id; the addressable ceiling is the smaller of the item
|
||||
// range (0xBFFF) and what this client's index actually holds.
|
||||
int staticMax = Math.Min(0xBFFF, index.Index.Length - StaticBase - 1);
|
||||
|
||||
bool oracleUp = true;
|
||||
|
||||
oracleUp = Walk(sb, "decode (no hue)", readers, oracle, oracleUp, staticMax, false, 0);
|
||||
Walk(sb, "hue " + ParityHue, readers, oracle, oracleUp, staticMax, true, ParityHue);
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One pass: every served static (and, when <paramref name="staticsOnly"/> is false,
|
||||
/// every served land id) at <paramref name="hue"/>, compared against the oracle. Returns
|
||||
/// the oracle's availability so the caller does not keep probing a Linux host that has
|
||||
/// already answered <c>NotImplemented</c> once.
|
||||
/// </summary>
|
||||
private static bool Walk(StringBuilder sb, string label, Readers readers, ArtParityOracle oracle,
|
||||
bool oracleUp, int staticMax, bool staticsOnly, int hue)
|
||||
{
|
||||
var c = new ParityCounts();
|
||||
|
||||
for (int id = 0; id <= staticMax; id++)
|
||||
{
|
||||
if ((id & 0x1FFF) == 0)
|
||||
Console.WriteLine("[Bridge] art parity ({0}): static {1}/{2}", label, id, staticMax);
|
||||
|
||||
oracleUp = Compare(readers, oracle, oracleUp, false, id, StaticBase + id, hue, c);
|
||||
}
|
||||
|
||||
if (!staticsOnly)
|
||||
{
|
||||
for (int id = 0; id < LandCount; id++)
|
||||
oracleUp = Compare(readers, oracle, oracleUp, true, id, id, hue, c);
|
||||
}
|
||||
|
||||
if (c.CandidateOnly > 0 && c.Matched == 0 && c.Mismatched == 0)
|
||||
{
|
||||
sb.AppendFormat(CultureInfo.InvariantCulture,
|
||||
" {0}: oracle unavailable (GDI+ NotImplemented — a Linux/Mono host): the managed path produced {1} pictures\n",
|
||||
label, c.CandidateOnly);
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.AppendFormat(CultureInfo.InvariantCulture,
|
||||
" {0}: compared {1} matched {2} MISMATCHED {3} MISSED {4} EXTRA {5} (skipped {6} ids the Bridge does not serve)\n",
|
||||
label, c.Resolved, c.Matched, c.Mismatched, c.Missed, c.Extra, c.Skipped);
|
||||
sb.AppendLine(c.Mismatched == 0 && c.Missed == 0 && c.Extra == 0
|
||||
? " RESULT: PASS — managed decode is pixel-identical to the vendored path"
|
||||
: " RESULT: FAIL — see the records below");
|
||||
}
|
||||
|
||||
if (c.Detail.Length > 0)
|
||||
{
|
||||
sb.AppendLine(" first discrepancies:");
|
||||
sb.Append(c.Detail);
|
||||
}
|
||||
|
||||
return oracleUp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One key. A key the Bridge gate does not admit a picture for — a §4.5 phantom, a
|
||||
/// damaged record, a 0×0 static, an unresolvable hue — is skipped, not a parity case:
|
||||
/// comparing it against raw <c>Get*</c> (which has no §4.5 gate) would re-measure the
|
||||
/// phantoms the validator exists to stop.
|
||||
///
|
||||
/// But a record the gate ADMITS and the managed decoder then refuses (a cumulative-column
|
||||
/// overflow, a run past the record — conditions <c>StaticSane</c> does not all catch) is
|
||||
/// the one regression this gate exists to find, because the vendored path might still
|
||||
/// render it. <see cref="TryArtPixels"/> flags that case through <c>decodeRefused</c>, and
|
||||
/// it is checked against the oracle and counted <c>MISSED</c> rather than left to hide in
|
||||
/// the skip bucket.
|
||||
/// </summary>
|
||||
private static bool Compare(Readers readers, ArtParityOracle oracle, bool oracleUp,
|
||||
bool land, int id, int at, int hue, ParityCounts c)
|
||||
{
|
||||
ushort[] cand;
|
||||
int cw, ch;
|
||||
bool partial, decodeRefused;
|
||||
string reason;
|
||||
|
||||
if (TryArtPixels(readers, land, id, at, hue,
|
||||
out cand, out cw, out ch, out partial, out decodeRefused, out reason)
|
||||
!= ArtOutcome.Ok)
|
||||
{
|
||||
// The gate admitted the record but the managed decoder refused it: the vendored path
|
||||
// may still render it, so this is a MISSED candidate, not a skip.
|
||||
if (decodeRefused && oracleUp)
|
||||
{
|
||||
ushort[] orcM;
|
||||
int owM, ohM;
|
||||
bool availM;
|
||||
|
||||
if (oracle(land, id, hue, partial, out orcM, out owM, out ohM, out availM))
|
||||
{
|
||||
if (!availM)
|
||||
{
|
||||
c.CandidateOnly++;
|
||||
return false;
|
||||
}
|
||||
|
||||
c.Missed++;
|
||||
|
||||
if (c.Shown++ < ParityMaxShown)
|
||||
c.Detail.AppendFormat(CultureInfo.InvariantCulture,
|
||||
" MISSED {0} {1} hue {2}: vendored has {3}x{4}, managed refused ({5})\n",
|
||||
land ? "land" : "static", id, hue, owM, ohM, reason);
|
||||
|
||||
return oracleUp;
|
||||
}
|
||||
|
||||
// Both agree there is no servable art here — not a miss.
|
||||
}
|
||||
|
||||
c.Skipped++;
|
||||
return oracleUp;
|
||||
}
|
||||
|
||||
if (!oracleUp)
|
||||
{
|
||||
c.CandidateOnly++;
|
||||
return oracleUp;
|
||||
}
|
||||
|
||||
ushort[] orc;
|
||||
int ow, oh;
|
||||
bool available;
|
||||
|
||||
bool haveOrc = oracle(land, id, hue, partial, out orc, out ow, out oh, out available);
|
||||
|
||||
if (!available)
|
||||
{
|
||||
// A Linux/Mono host: the vendored bitmap ctor cannot run. Switch to candidate-only.
|
||||
c.CandidateOnly++;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!haveOrc)
|
||||
{
|
||||
c.Extra++;
|
||||
|
||||
if (c.Shown++ < ParityMaxShown)
|
||||
c.Detail.AppendFormat(CultureInfo.InvariantCulture,
|
||||
" EXTRA {0} {1} hue {2}: managed has {3}x{4}, vendored has none\n",
|
||||
land ? "land" : "static", id, hue, cw, ch);
|
||||
|
||||
return oracleUp;
|
||||
}
|
||||
|
||||
c.Resolved++;
|
||||
|
||||
int diffAt = -1;
|
||||
|
||||
if (cw == ow && ch == oh && SamePixels(cand, orc, out diffAt))
|
||||
{
|
||||
c.Matched++;
|
||||
}
|
||||
else
|
||||
{
|
||||
c.Mismatched++;
|
||||
|
||||
if (c.Shown++ < ParityMaxShown)
|
||||
{
|
||||
if (cw != ow || ch != oh)
|
||||
c.Detail.AppendFormat(CultureInfo.InvariantCulture,
|
||||
" DIM {0} {1} hue {2}: managed {3}x{4} vs vendored {5}x{6}\n",
|
||||
land ? "land" : "static", id, hue, cw, ch, ow, oh);
|
||||
else
|
||||
c.Detail.AppendFormat(CultureInfo.InvariantCulture,
|
||||
" PIXEL {0} {1} hue {2}: {3}x{4}, first diff at {5}: managed {6:X4} vs vendored {7:X4}\n",
|
||||
land ? "land" : "static", id, hue, cw, ch, diffAt, cand[diffAt], orc[diffAt]);
|
||||
}
|
||||
}
|
||||
|
||||
return oracleUp;
|
||||
}
|
||||
|
||||
private static bool SamePixels(ushort[] a, ushort[] b, out int diffAt)
|
||||
{
|
||||
diffAt = -1;
|
||||
|
||||
if (a == null || b == null || a.Length != b.Length)
|
||||
{
|
||||
diffAt = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < a.Length; i++)
|
||||
{
|
||||
if (a[i] != b[i])
|
||||
{
|
||||
diffAt = i;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user