using System.Text.RegularExpressions;
using RynthCore.Engine;
using RynthCore.Engine.Compatibility;
using RynthCore.Install;
namespace EngineRobustnessTests;
/// Checks for the action ring, the mutator gate, appraisal retention, the dump rules and the cast slot.
internal static partial class Program
{
private static void RunRingAndGateTests()
{
RingTakesAStopWhenFullOfStarts();
RingRefusesStartsDuringATransition();
RingStopRunsFirstAfterTheWindow();
CastStopFindsAnOutOfOrderStop();
GateTruthTable();
GateCallSitesAllUseTheSameShape();
RequestIdNeverMergesIntoAHeldEntry();
ReleasedDataKeptSoRecreateSkips();
ReleasedDataExpiresAndServerDeleteEvicts();
TeleportMessageStartsTheWindow();
OutOfMemoryBoxMatchesOnlyAcsBox();
LauncherHoldsTheKillDuringADump();
// The cast slot, the dump hold cap and the crash exits
CastRefusedDuringATransition();
RingPeekAtWalksWithoutRemoving();
EnqueueCastPassesTheTransitionState();
LauncherDumpHoldIsCapped();
CrashExitExcludesExitMinusOne();
CrashExitPathsFlushTheCrashLog();
}
// ---- the action ring (the real ActionRing, the one AcMainThreadQueue uses) --------
private readonly record struct Act(string What, long At, bool Teardown);
private static void RingTakesAStopWhenFullOfStarts()
{
_case = "ring reserve";
var ring = new ActionRing(256, 32);
int queued = 0, full = 0;
for (int i = 0; i < 300; i++)
{
var r = ring.TryEnqueue(new Act("start", 1000, false), teardown: false, inTransition: false);
if (r == ActionRing.Admit.Queued) queued++;
else if (r == ActionRing.Admit.Full) full++;
}
Check(queued == 224 && full == 76, $"300 starts: 224 taken, the rest refused at the reserve (got {queued}/{full})");
Check(ring.TryEnqueue(new Act("stop", 1001, true), teardown: true, inTransition: false) == ActionRing.Admit.Queued,
"a stop is still taken");
for (int i = 0; i < 31; i++)
ring.TryEnqueue(new Act("cancel", 1002, true), teardown: true, inTransition: false);
Check(ring.Count == 256, $"the reserve fills only with teardown (count {ring.Count})");
Check(ring.TryEnqueue(new Act("stop", 1003, true), teardown: true, inTransition: false) == ActionRing.Admit.Full,
"a completely full ring refuses even a stop");
}
private static void RingRefusesStartsDuringATransition()
{
_case = "ring transition";
var ring = new ActionRing(256, 32);
int rejected = 0;
for (int i = 0; i < 300; i++)
if (ring.TryEnqueue(new Act("start", 2000, false), teardown: false, inTransition: true) == ActionRing.Admit.RejectedTransition)
rejected++;
Check(rejected == 300 && ring.Count == 0, $"300 starts during a hold are refused, none stored (rejected {rejected}, count {ring.Count})");
Check(ring.TryEnqueue(new Act("stop", 2001, true), teardown: true, inTransition: true) == ActionRing.Admit.Queued,
"a stop during the hold is taken");
}
///
/// The flood scenario against the real ring: a bot floods starts before the transition is
/// seen (the ring fills to the reserve), the player dies, 300 more starts arrive during the
/// hold, then a stop. Nothing runs during the hold; after it the stale starts are dropped and
/// the stops are the first things to run.
///
private static void RingStopRunsFirstAfterTheWindow()
{
_case = "ring drain";
var ring = new ActionRing(256, 32);
var s = new WorldTransitionState();
s.Sample(900, false, false, 0x00AE);
for (int i = 0; i < 300; i++)
ring.TryEnqueue(new Act("start", 1000, false), false, s.InTransition(1000));
s.Sample(1500, true, false, 0x00AE); // dies
for (int i = 0; i < 300; i++)
ring.TryEnqueue(new Act("start-held", 2000, false), false, s.InTransition(2000));
var stopAdmit = ring.TryEnqueue(new Act("stop", 2500, true), true, s.InTransition(2500));
Check(stopAdmit == ActionRing.Admit.Queued, $"the stop is accepted ({stopAdmit})");
var ran = new List();
int dropped = 0;
bool heldClean = true;
for (long now = 1500; now < 20_000; now += 16)
{
s.Sample(now, now < 9000, false, 0x00AE);
int before = ring.Count;
if (s.CheckHolding(now)) { heldClean &= ring.Count == before; continue; } // Drain's early return
while (ring.TryDequeue(out Act e))
{
var v = s.Decide(now, e.At, e.Teardown);
if (v == WorldTransitionState.Verdict.Drop) dropped++;
else ran.Add(e.What);
}
}
Check(heldClean, "nothing leaves the ring while held");
Check(ran.Count > 0 && ran[0] == "stop", $"the stop runs first after the window (ran [{string.Join(",", ran.Take(3))}])");
Check(ran.Count == 1 && dropped == 224, $"the 224 stale starts are dropped, nothing else runs (ran {ran.Count}, dropped {dropped})");
}
// ---- the cast drain's stop check (the real CastStopRules over the real ActionRing) ------
///
/// A stop queued at or before the cast is found wherever it sits in the ring, including
/// behind an entry stamped after the cast (two producers can land out of time order).
///
private static void CastStopFindsAnOutOfOrderStop()
{
_case = "cast stop check";
static bool Any(ActionRing r, long at) =>
CastStopRules.AnyStopAtOrBefore(r, at, static e => e.At, static e => e.Teardown);
var empty = new ActionRing(8, 2);
Check(!Any(empty, 1000), "an empty ring has no stop");
var outOfOrder = new ActionRing(8, 2);
outOfOrder.TryEnqueue(new Act("start", 1200, false), false, false); // newer than the cast
outOfOrder.TryEnqueue(new Act("stop", 900, true), true, false); // older, queued behind it
Check(Any(outOfOrder, 1000), "a stop older than the cast, behind a newer entry, is found");
var onlyNewer = new ActionRing(8, 2);
onlyNewer.TryEnqueue(new Act("start", 500, false), false, false);
onlyNewer.TryEnqueue(new Act("stop", 1500, true), true, false);
Check(!Any(onlyNewer, 1000), "a stop queued after the cast does not count");
var sameTime = new ActionRing(8, 2);
sameTime.TryEnqueue(new Act("stop", 1000, true), true, false);
Check(Any(sameTime, 1000), "a stop queued at the cast's own time counts");
var noStop = new ActionRing(8, 2);
noStop.TryEnqueue(new Act("start", 100, false), false, false);
noStop.TryEnqueue(new Act("start", 200, false), false, false);
Check(!Any(noStop, 1000), "older entries that are not stops do not count");
Check(noStop.Count == 2 && outOfOrder.Count == 2, "the check removes nothing");
}
// ---- the mutator gate --------------------------------------------------------
private static void GateTruthTable()
{
_case = "mutator gate";
int asked = 0;
Func holding = () => { asked++; return true; };
Func clear = () => { asked++; return false; };
Check(MutatorGate.MustQueue(false, false, false, clear), "off the main thread: queue");
Check(MutatorGate.MustQueue(false, true, true, clear), "off the main thread, even teardown in a drain: queue");
Check(!MutatorGate.MustQueue(true, false, false, clear), "main thread, no transition: call now");
Check(MutatorGate.MustQueue(true, false, false, holding), "main thread in a transition (render-thread host action): queue");
Check(!MutatorGate.MustQueue(true, true, false, holding), "main thread in a transition, a stop: call now");
asked = 0;
Check(!MutatorGate.MustQueue(true, false, true, holding) && asked == 0, "called by a drain: call now, without asking the transition state");
asked = 0;
MutatorGate.MustQueue(false, false, false, holding);
Check(asked == 0, "off the main thread the (main-thread-only) transition state is never asked");
}
///
/// Every AC-mutating helper opens with the same gate: no helper may still decide with the old
/// bare IsOnMainThread() check before an AcMainThreadQueue.Enqueue*, and each stop/cancel
/// passes teardown: true while every other passes false (or !enabled for the two toggles).
///
private static void GateCallSitesAllUseTheSameShape()
{
_case = "gate call sites";
string? compat = FindRepoDir(Path.Combine("src", "RynthCore.Engine", "Compatibility"));
Check(compat != null, "found src\\RynthCore.Engine\\Compatibility by walking up");
if (compat == null) return;
string[] files = { "ClientCombatHooks.cs", "CommandInterpreterHooks.cs", "ClientHelperHooks.cs", "CombatActionHooks.cs",
"MovementActionHooks.cs", "PlayerPhysicsHooks.cs", "PlayerTrade.cs", "PlayerTraining.cs",
"DeleteObjectHooks.cs" };
var enqueue = new Regex(@"AcMainThreadQueue\.(Enqueue\w+)\(");
var gate = new Regex(@"AcMainThreadQueue\.MustQueue\(teardown: (true|false|!enabled)\)");
string[] teardownTrue = { "EnqueueStopCompletely", "EnqueueStopMovement", "EnqueueCancelAttack", "EnqueueCloseContainer" };
string[] toggles = { "EnqueueSetAutoRun", "EnqueueSetMotion" };
int sites = 0, bad = 0;
foreach (string f in files)
{
string[] lines = File.ReadAllLines(Path.Combine(compat, f));
for (int i = 0; i < lines.Length; i++)
{
Match m = enqueue.Match(lines[i]);
if (!m.Success) continue;
string name = m.Groups[1].Value;
if (name is "EnqueueWriteToChat" or "EnqueueChatCommand") continue;
sites++;
// The gate line is the nearest "if (" line above the enqueue (within 3 lines).
string? gateLine = null;
for (int j = i; j >= Math.Max(0, i - 3); j--)
if (lines[j].TrimStart().StartsWith("if (", StringComparison.Ordinal)) { gateLine = lines[j]; break; }
Match g = gateLine == null ? Match.Empty : gate.Match(gateLine);
string want = teardownTrue.Contains(name) ? "true" : toggles.Contains(name) ? "!enabled" : "false";
if (!g.Success || g.Groups[1].Value != want || gateLine!.Contains("IsOnMainThread"))
{
bad++;
Console.WriteLine($"[gate] {f}:{i + 1} {name}: gate line '{gateLine?.Trim()}' (want teardown: {want})");
}
}
}
// 45 in PR #8, + SalvageDirect (v82, ClientHelperHooks) + RemoveClientObject (v83, DeleteObjectHooks).
Check(sites == 47, $"47 gated mutator call sites found (got {sites})");
Check(bad == 0, $"every one opens with AcMainThreadQueue.MustQueue(teardown: ...) and the right teardown value ({bad} wrong)");
}
private static string? FindRepoDir(string relative)
{
string? dir = AppContext.BaseDirectory;
while (dir != null)
{
string candidate = Path.Combine(dir, relative);
if (Directory.Exists(candidate)) return candidate;
dir = Path.GetDirectoryName(dir);
}
return null;
}
// ---- no merge into an entry a transition will drop -------------------------------
private static void RequestIdNeverMergesIntoAHeldEntry()
{
_case = "requestid merge floor";
var q = new PendingIdQueue(8);
q.Enqueue(42, 1000, 1, 7_500);
// A transition held the drains up to 1500: the entry from 1000 will be dropped.
Check(q.Enqueue(42, 2000, 1, 7_500, mergeOnlyAfterMs: 1500) == PendingIdQueue.Result.Queued,
"a request after the window does not merge into the doomed entry");
Check(q.Count == 2, "so it survives the drop");
Check(q.Enqueue(42, 2100, 1, 7_500, mergeOnlyAfterMs: 1500) == PendingIdQueue.Result.Merged,
"it merges into the new one");
var r = new PendingIdQueue(8);
r.Enqueue(43, 1000, 1, 7_500);
Check(r.Enqueue(43, 2000, 1, 7_500, mergeOnlyAfterMs: 0) == PendingIdQueue.Result.Merged, "no transition: merges as before");
}
// ---- identify data kept across a client release ---------------------------------
///
/// Wired as the engine wires them: AppraisalHooks' cache (here a set) keeps an object while
/// ReleasedObjectRetention says so; AutoIdEligibility decides the re-create.
///
private static void ReleasedDataKeptSoRecreateSkips()
{
_case = "release keeps data";
var cache = new HashSet();
var keep = new ReleasedObjectRetention();
var elig = new AutoIdEligibility();
long t = 1000;
Check(elig.OnCreate(50, t, cache.Contains(50)) == AutoIdEligibility.CreateDecision.Appraise, "first create: appraised");
elig.OnSent(50, t + 100);
cache.Add(50); // the identify arrived
// Client release (BeingDeleted): AppraisalHooks.OnClientRelease
if (!keep.OnRelease(50, t + 60_000)) cache.Remove(50);
elig.OnClientRelease(50);
// Re-created 5 minutes later: AutoIdService.Enqueue (MarkLive, then the decision)
keep.Forget(50);
var d = elig.OnCreate(50, t + 5 * 60_000, cache.Contains(50));
Check(d == AutoIdEligibility.CreateDecision.SkipRecent, $"re-created within 10 min with the data kept: skipped ({d})");
Check(elig.SkippedRecentTotal == 1, $"SkippedRecentCount moves (got {elig.SkippedRecentTotal})");
}
private static void ReleasedDataExpiresAndServerDeleteEvicts()
{
_case = "release expiry";
var keep = new ReleasedObjectRetention(10 * 60_000, 4);
var expired = new List();
keep.OnRelease(1, 1000);
keep.OnRelease(2, 2000);
Check(keep.Expire(1000 + 10 * 60_000 - 1, expired) == 0, "kept inside the 10 minutes");
Check(keep.Expire(2000 + 10 * 60_000, expired) == 2 && expired.SequenceEqual(new uint[] { 1, 2 }), "both dropped once past it");
Check(keep.Count == 0, "and no longer tracked");
for (uint i = 10; i < 14; i++) keep.OnRelease(i, 5000);
Check(!keep.OnRelease(99, 5000), "at the bound a release is not kept (the caller evicts it at once)");
keep.Forget(10);
Check(keep.OnRelease(99, 5000), "room again once one is forgotten (re-created or server-deleted)");
}
// ---- 0xF751: the teleport message as its own signal ----------------------------------
private static void TeleportMessageStartsTheWindow()
{
_case = "teleport message";
var s = new WorldTransitionState();
s.Sample(1000, false, false, 0xA9B4);
Check(!s.InTransition(2000), "no transition before the message");
Check(s.OnTeleportMessage(2000), "PlayerTeleport received: the window starts");
// No portal flag, no landblock change, alive: only the message.
s.Sample(2016, false, false, 0xA9B4);
Check(s.InTransition(2000 + WorldTransitionState.SettleMs - 1), "held for the full settle on the message alone");
Check(s.Decide(3000, 2500, false) == WorldTransitionState.Verdict.Hold, "queued work held");
Check(!s.InTransition(2000 + WorldTransitionState.SettleMs), "window ends 5 s after it");
Check(s.Decide(2000 + WorldTransitionState.SettleMs, 2500, false) == WorldTransitionState.Verdict.Drop, "then stale work is dropped");
Check(!s.OnTeleportMessage(1500), "an older message never shortens a running window");
}
// ---- the OOM dump trigger ------------------------------------------------------
private static void OutOfMemoryBoxMatchesOnlyAcsBox()
{
_case = "oom box rule";
// The real text, from a real log line.
const string real = "OUT OF MEMORY: Failed to allocate -778718788 bytes.";
Check(OutOfMemoryBoxRule.IsOutOfMemoryBox(real, onMainThread: true), "AC's box on its main thread: dump");
Check(!OutOfMemoryBoxRule.IsOutOfMemoryBox(real, onMainThread: false), "the same text off the main thread: no dump");
Check(!OutOfMemoryBoxRule.IsOutOfMemoryBox("Plugin X: OUT OF MEMORY: Failed to allocate 4 bytes.", true), "prefix not at the start: no dump");
Check(!OutOfMemoryBoxRule.IsOutOfMemoryBox("out of memory: failed to allocate 4 bytes.", true), "other case: no dump (ordinal)");
Check(!OutOfMemoryBoxRule.IsOutOfMemoryBox("OUT OF MEMORY", true), "the bare words: no dump");
Check(!OutOfMemoryBoxRule.IsOutOfMemoryBox(null, true), "no text: no dump");
}
// ---- the launcher holds its kill while a dump is written ---------------------------
private static void LauncherHoldsTheKillDuringADump()
{
_case = "dump in progress";
var now = new DateTime(2026, 10, 10, 22, 18, 30, DateTimeKind.Utc);
string started = $"[18:18:16] {DumpInProgressRule.MarkerStarted} pid=123 - ...\n";
string ended = $"[18:20:01] {DumpInProgressRule.MarkerEnded} pid=123.\n";
Check(DumpInProgressRule.IsDumping("hb #1\n" + started, now.AddMinutes(-3), null, now), "marker 3 min old, no end: hold the kill");
Check(!DumpInProgressRule.IsDumping("hb #1\n" + started, now.AddMinutes(-11), null, now), "marker over 10 min old: a client that died mid-dump is killed");
Check(!DumpInProgressRule.IsDumping(started + ended, now.AddMinutes(-3), null, now), "dump ended: no hold");
Check(DumpInProgressRule.IsDumping(started + ended + started, now.AddMinutes(-1), null, now), "the LAST marker decides");
Check(DumpInProgressRule.IsDumping("hb #1\n", now.AddMinutes(-5), now.AddSeconds(-10), now), "no marker, but the dump file was written 10 s ago: hold");
Check(!DumpInProgressRule.IsDumping("hb #1\n", now.AddMinutes(-5), now.AddMinutes(-2), now), "a dump file quiet for 2 min: no hold");
Check(!DumpInProgressRule.IsDumping(null, now, null, now), "nothing at all: no hold");
}
// ---- the cast slot refuses during a transition (the real CastSlot) -------------
private static void CastRefusedDuringATransition()
{
_case = "cast slot transition";
var slot = new CastSlot();
Check(slot.TryEnqueue(0x50000001, 1234, 1000, inTransition: true) == CastSlot.Admit.RejectedTransition,
"an empty slot refuses a cast during a transition");
Check(slot.Pending == 0 && slot.TargetId == 0 && slot.SpellId == 0 && slot.QueuedAt == 0,
"and stays untouched");
Check(slot.TryEnqueue(0x50000002, 99, 2000, inTransition: false) == CastSlot.Admit.Queued, "outside one, a cast is queued");
Check(slot.TryEnqueue(0x50000003, 77, 2500, inTransition: true) == CastSlot.Admit.RejectedTransition,
"a transition refuses even with a cast pending (counted as a transition refusal, not busy)");
Check(slot.Pending == 1 && slot.TargetId == 0x50000002 && slot.SpellId == 99 && slot.QueuedAt == 2000,
"the pending cast is unchanged");
Check(slot.TryEnqueue(0x50000004, 55, 2600, inTransition: false) == CastSlot.Admit.Busy, "busy outside a transition");
slot.Pending = 0;
Check(slot.TryEnqueue(0x50000005, 66, 9000, inTransition: false) == CastSlot.Admit.Queued
&& slot.SpellId == 66 && slot.QueuedAt == 9000, "after the transition, casts queue again");
// The drain's once-per-cast stop-wait note belongs to the cast: a new cast clears it, a
// refused one leaves the pending cast's note alone.
slot.StopWaitNoted = true;
Check(slot.TryEnqueue(0x50000006, 67, 9100, inTransition: false) == CastSlot.Admit.Busy && slot.StopWaitNoted,
"a busy refusal keeps the pending cast's stop-wait note");
slot.Pending = 0;
Check(slot.TryEnqueue(0x50000007, 68, 9200, inTransition: false) == CastSlot.Admit.Queued && !slot.StopWaitNoted,
"a newly queued cast starts with no stop-wait note");
}
// ---- The cast drain's walk of the ring (ActionRing.TryPeekAt) --------------------------
private static void RingPeekAtWalksWithoutRemoving()
{
_case = "ring peek-at";
var ring = new ActionRing(8, 2);
Check(!ring.TryPeekAt(0, out _), "an empty ring has nothing at 0");
// Wrap the ring once so the walk crosses the end of the storage.
for (int i = 0; i < 5; i++) ring.TryEnqueue(new Act("old" + i, i, false), false, false);
for (int i = 0; i < 5; i++) ring.TryDequeue(out _);
for (int i = 0; i < 6; i++) ring.TryEnqueue(new Act("e" + i, 100 + i, i == 4), i == 4, false);
var seen = new List();
for (int i = 0; ring.TryPeekAt(i, out Act e); i++) seen.Add(e.What);
Check(string.Join(",", seen) == "e0,e1,e2,e3,e4,e5", $"oldest first across the wrap, got [{string.Join(",", seen)}]");
Check(ring.Count == 6, $"nothing removed (count {ring.Count})");
Check(!ring.TryPeekAt(6, out _) && !ring.TryPeekAt(-1, out _), "out of range is false");
Check(ring.TryDequeue(out Act head) && head.What == "e0" && ring.TryPeekAt(0, out Act next) && next.What == "e1",
"after a dequeue, offset 0 is the new oldest");
}
private static void EnqueueCastPassesTheTransitionState()
{
_case = "EnqueueCast wiring";
string? compat = FindRepoDir(Path.Combine("src", "RynthCore.Engine", "Compatibility"));
Check(compat != null, "found src\\RynthCore.Engine\\Compatibility by walking up");
if (compat == null) return;
string src = File.ReadAllText(Path.Combine(compat, "AcMainThreadQueue.cs"));
int start = src.IndexOf("public static bool EnqueueCast(", StringComparison.Ordinal);
int end = start < 0 ? -1 : src.IndexOf("\n }", start, StringComparison.Ordinal);
string body = start < 0 || end < 0 ? "" : src.Substring(start, end - start);
Check(body.Contains("_cast.TryEnqueue(targetId, spellId, NowMs, PlayerLifecycleLog.InWorldTransition)", StringComparison.Ordinal),
"EnqueueCast hands CastSlot the live transition state");
Check(Regex.IsMatch(body, @"RejectedTransition\)\s*Interlocked\.Increment\(ref _transitionRejected\)"),
"and counts a refusal in TransitionRejectedCount (the heartbeat's qrej)");
Check(!Regex.IsMatch(src, @"_cast\.(TargetId|SpellId|QueuedAt)\s*=[^=]"),
"nothing else writes the slot's cast fields");
}
// ---- the launcher's dump hold is capped per client --------------------------------
private static void LauncherDumpHoldIsCapped()
{
_case = "dump hold cap";
var now = new DateTime(2026, 10, 10, 22, 18, 30, DateTimeKind.Utc);
string open = $"hb #1\n[18:00:00] {DumpInProgressRule.MarkerStarted} pid=123 - ...\nhb #2\n";
DateTime freshLog = now.AddSeconds(-5);
Check(DumpInProgressRule.Decide(open, freshLog, null, null, now) == DumpInProgressRule.Hold.Holding,
"an open marker, first seen now: hold");
Check(DumpInProgressRule.Decide(open, freshLog, null, now.AddMinutes(-5), now) == DumpInProgressRule.Hold.Holding,
"held 5 min: still holding");
Check(DumpInProgressRule.Decide(open, freshLog, null, now.AddMinutes(-16), now) == DumpInProgressRule.Hold.Expired,
"fresh log, marker held over 15 min: the kill checks run");
Check(DumpInProgressRule.Decide("hb #1\n", freshLog, now.AddSeconds(-3), now.AddMinutes(-16), now) == DumpInProgressRule.Hold.Expired,
"a dump file still being written does not extend the cap either");
Check(DumpInProgressRule.Decide(open, freshLog, null, DateTime.MinValue, now) == DumpInProgressRule.Hold.Expired,
"the launcher's 'expired, logged' mark stays expired");
Check(DumpInProgressRule.Decide("hb #1\n", freshLog, null, now.AddMinutes(-16), now) == DumpInProgressRule.Hold.None,
"no sign of a dump: None (the launcher forgets the pid)");
}
// ---- which exit codes are crash exits ---------------------------------------------
private static void CrashExitExcludesExitMinusOne()
{
_case = "crash exit rule";
Check(CrashExitRule.IsCrashExit(0xC0000005u), "0xC0000005 access violation: crash");
Check(CrashExitRule.IsCrashExit(0xC0000409u), "0xC0000409 stack buffer overrun: crash");
Check(CrashExitRule.IsCrashExit(0xC0000374u), "0xC0000374 heap corruption: crash");
Check(CrashExitRule.IsCrashExit(0xC00000FDu), "0xC00000FD stack overflow: crash");
Check(CrashExitRule.IsCrashExit(0xE0434352u), "0xE0434352 CLR unhandled exception: crash (as before)");
Check(!CrashExitRule.IsCrashExit(0xFFFFFFFFu), "ExitProcess(-1): not a crash");
Check(!CrashExitRule.IsCrashExit(0u), "0: not a crash");
Check(!CrashExitRule.IsCrashExit(1u), "1: not a crash");
Check(!CrashExitRule.IsCrashExit(0x80000003u), "0x80000003 (warning severity): not a crash");
Check(!CrashExitRule.IsCrashExit(0x40010004u), "0x40010004 (informational): not a crash");
}
private static void CrashExitPathsFlushTheCrashLog()
{
_case = "crash exit flush";
string? compat = FindRepoDir(Path.Combine("src", "RynthCore.Engine", "Compatibility"));
Check(compat != null, "found src\\RynthCore.Engine\\Compatibility by walking up");
if (compat == null) return;
string src = File.ReadAllText(Path.Combine(compat, "ProcessExitHooks.cs"));
Check(src.Contains("private static bool IsCrashExit(uint code) => CrashExitRule.IsCrashExit(code);", StringComparison.Ordinal),
"ProcessExitHooks uses CrashExitRule");
foreach (string detour in new[] { "ExitProcessDetour", "TerminateProcessDetour", "RtlExitUserProcessDetour", "NtTerminateProcessDetour" })
{
int start = src.IndexOf($"private static {(detour.Contains("Terminate") ? (detour.StartsWith("Nt") ? "uint" : "bool") : "void")} {detour}(", StringComparison.Ordinal);
int end = start < 0 ? -1 : src.IndexOf("\n }", start, StringComparison.Ordinal);
string body = start < 0 || end < 0 ? "" : src.Substring(start, end - start);
int flush = body.IndexOf("SehTrampoline.FlushCrashLog(", StringComparison.Ordinal);
int orig = body.IndexOf("Original!(", StringComparison.Ordinal);
Check(flush >= 0 && orig > flush, $"{detour}: a crash exit flushes the crash log before the original");
}
}
}