using System.Buffers.Binary; using System.Numerics; using GamePort.Assets; using GamePort.Game; var original = Path.GetFullPath(args.Length == 1 ? args[0] : "../Original"); var source = File.ReadAllBytes(Path.Combine(original, "Neutron.exe")); void Bytes(int va, string hex) { var expected = Convert.FromHexString(hex); Require(source.AsSpan(va - 0x400000, expected.Length).SequenceEqual(expected), $"Source instructions differ at0x{va:X}"); } int Pointer(int va) => BinaryPrimitives.ReadInt32LittleEndian(source.AsSpan(va - 0x400000, 4)); Require(Pointer(0x4a3bdc + 0x168) == 0x43b5a0, "Jimmy transition vtable target"); Require(Pointer(0x4a3bdc + 0x1e8) == 0x42af00, "Separate Jimmy checkpoint vtable target"); Bytes(0x42af1b, "898E481E0000"); Bytes(0x42af30, "81C6381E0000"); Bytes(0x43b78e, "8A83A509000084C07537"); Bytes(0x43b7b1, "8D8BA80900008993A80900008B50048951048B50088951088B400C89410C"); Bytes(0x458014, "88905409000074088D8340FFFFFFEB0233C085DB8890A5090000"); Bytes(0x423a90, "8A85E508000084C00F8486000000C685E508000000"); Bytes(0x423abf, "FF926C010000"); Bytes(0x423ae3, "8B8DE80800008BC48964242089088B95EC0800008950048B8DF00800008948088B95F408000089500C"); Bytes(0x423b0c, "8B855C0900008D88C00000008B80C0000000FF9068010000"); Require(Pointer(0x49f480 + 0x168) == 0x471680 && Pointer(0x49f480 + 0x16c) == 0x471630, "Goddard root setter retains unchanged XYZ"); Bytes(0x471680, "8B54240C8B01528B54240C528B54240C52FF906C010000C21000"); Bytes(0x4427f2, "8A905409000084D2741D85C9740F8D8140FFFFFFC6805409000000EB21"); Bytes(0x442887, "8A905409000084D2741C85C974108D8140FFFFFF5F5EC6805409000000"); Bytes(0x46008b, "8D87E80800008D4C241C50FFD58D8FEC080000518D4C2420FFD581C7F00800008D4C241C57FFD5"); Bytes(0x460ba3, "898DA80900008B5424688995AC0900008B44246C8B55008985B0090000"); Console.WriteLine("PASS source departure/RETURN root, start-trigger suppression and TSK vector identity"); var initial = OriginalSave.Load(Path.Combine(original, "NewGame.tsk")); var departingRoot = new Vector3(3321.25f, 76.5f, -456.75f); var tutorialRoot = new Vector3(-31, 211, 584); var departure = RuntimeCheckpoint.Stage(initial.StoredVector, departingRoot, false); Require(!departure.Returning && departure.ActivateStartTrigger, "Ordinary departure retains destination start trigger"); Require(departure.Root == departingRoot, "Departure captures live actor root, not STRT position"); var tutorialSave = initial with { LevelCode = "VR04", StartPoint = "PHONEBOOTH", ReturnLevel = "level1b.gam", ReturnStart = "STARTEXP", StoredVector = departure.Root }; var reloaded = OriginalSave.Parse(tutorialSave.Encode()); var returning = RuntimeCheckpoint.Stage(reloaded.StoredVector, tutorialRoot, true); Require(returning.Returning && !returning.ActivateStartTrigger, "RETURN suppresses destination start trigger"); Require(returning.Root == departingRoot && returning.Root != tutorialRoot, "RETURN preserves departure root across TSK encoding"); Require(reloaded.ReturnLevel == "level1b.gam" && reloaded.ReturnStart == "STARTEXP", "Return strings stay separate from captured root"); var nextDeparture = RuntimeCheckpoint.Stage(returning.Root, tutorialRoot, false); Require(nextDeparture.Root == tutorialRoot && nextDeparture.ActivateStartTrigger, "RETURN suppression is per transition, not permanent"); Console.WriteLine("PASS lab departure -> serialized tutorial -> RETURN preserves root and suppresses intro once"); var saves = Directory.EnumerateFiles(original).Where(p => p.EndsWith(".tsk", StringComparison.OrdinalIgnoreCase)).ToArray(); Require(saves.Length == 12, "Expected all12 supplied saves"); foreach (var path in saves) { var bytes = File.ReadAllBytes(path); var save = OriginalSave.Parse(bytes); Require(save.Encode().SequenceEqual(bytes), $"Wire layout changed for {path}"); var returnStage = RuntimeCheckpoint.Stage(save.StoredVector, tutorialRoot, true); Require(returnStage.Root == save.StoredVector, $"RETURN did not preserve original vector in {path}"); var freshLoad = RuntimeCheckpoint.Stage(save.StoredVector, tutorialRoot, false); Require(!freshLoad.Returning && freshLoad.ActivateStartTrigger, "Loaded TSK vector alone does not imply pending RETURN"); } Console.WriteLine("PASS all12 original TSK vectors remain byte-identical and distinct from pending RETURN state"); foreach (var bad in new[] { new Vector3(float.NaN, 0, 0), new Vector3(0, float.PositiveInfinity, 0), new Vector3(0, 0, float.NegativeInfinity) }) { Reject(() => RuntimeCheckpoint.Stage(bad, departingRoot, true)); Reject(() => RuntimeCheckpoint.Stage(departingRoot, bad, false)); } // Large but finite roots are valid serialized evidence; avoid inventing world bounds. Require(RuntimeCheckpoint.Stage(Vector3.Zero, new(float.MaxValue, 0, -float.MaxValue), false).Root.X == float.MaxValue, "Finite original coordinates must not be normalized or truncated"); Console.WriteLine("PASS nonfinite input rejection without invented coordinate bounds"); static void Require(bool condition, string message) { if (!condition) throw new InvalidOperationException(message); } static void Reject(Action action) { try { action(); } catch (ArgumentOutOfRangeException) { return; } throw new InvalidOperationException("Expected nonfinite root rejection"); }