182 lines
11 KiB
C#
182 lines
11 KiB
C#
using GamePort.Assets;
|
|
using GamePort.Application;
|
|
using GamePort.Game;
|
|
using System.Numerics;
|
|
|
|
var root = args.Length == 1 ? Path.GetFullPath(args[0])
|
|
: Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../../../../Original"));
|
|
var files = Directory.GetFiles(Path.Combine(root, "gam"), "*.gam");
|
|
Require(files.Length == 35, $"Expected all 35 original level files at {root}");
|
|
var entities = 0;
|
|
foreach (var file in files)
|
|
{
|
|
var level = GamLevel.Load(file);
|
|
entities += level.Entities.Count;
|
|
Require(level.Player is not null, $"Missing player in {file}");
|
|
Require(level.FindStart() is not null, $"Missing matching player startpoint in {file}");
|
|
}
|
|
Require(entities == 3299, "Original entity total differs");
|
|
var initial = GamLevel.Load(Path.Combine(root, "gam/level1b.gam"));
|
|
var saves = Directory.GetFiles(root, "*.tsk");
|
|
Require(saves.Length == 12, "Expected original save corpus");
|
|
foreach (var file in saves)
|
|
{
|
|
var raw = File.ReadAllBytes(file);
|
|
var save = OriginalSave.Parse(raw);
|
|
Require(save.Encode().SequenceEqual(raw), $"TSK byte round-trip differs: {file}");
|
|
foreach (var length in new[] { 0, 4, 18, 16000, raw.Length - 1 }) RejectSave(raw[..length]);
|
|
RejectSave([.. raw, 0]);
|
|
}
|
|
var newGame = OriginalSave.Load(Path.Combine(root, "NewGame.tsk"));
|
|
Require(newGame.LevelCode == "LV1B" && newGame.StartPoint == "STARTEXP", "Wrong new-game resume data");
|
|
var tasks = new TaskStates(newGame.Tasks);
|
|
Require(tasks.Get("scene") == 30 && tasks.Get("missing") == 0, "Original task query semantics differ");
|
|
var assignments = 0;
|
|
tasks.Assigned += (_, _) => assignments++;
|
|
tasks.SetExisting("ScEnE", 35);
|
|
tasks.SetExisting("missing", 77);
|
|
tasks.SetExisting("SCENE", 35);
|
|
Require(tasks.Get("SCENE") == 35 && tasks.Get("missing") == 0 && tasks.Snapshot().Length == 12 && assignments == 3,
|
|
"Task assignment must broadcast without creating missing entries");
|
|
var changed = newGame with { Score = 15, Tasks = tasks.Snapshot() };
|
|
Require(OriginalSave.Parse(changed.Encode()).Score == 15 && OriginalSave.Parse(changed.Encode()).Tasks[3].State == 35,
|
|
"Modified original save did not persist recovered fields");
|
|
Console.WriteLine("PASS: all12 original TSK files byte-round-trip, malformed saves reject, task query/assignment and mutations persist.");
|
|
Require(initial.Code == "LV1B" && initial.Entities.Count == 129, "Wrong initial scene");
|
|
Require(initial.Player!.Position.X == 5071.93310546875f, "Original player X changed");
|
|
Require(initial.FindStart()!.CameraPosition.Y == 235.64761352539062f, "Original camera Y changed");
|
|
Require(initial.FindStart()!.Text("StartTrigger") == "LABEXP", "Initial trigger differs");
|
|
Require(initial.FindStart()!.Text("MusicDatabase") == "musicsecretlab.omt", "Initial music differs");
|
|
|
|
var sample = File.ReadAllBytes(Path.Combine(root, "gam/VR01.gam"));
|
|
foreach (var length in new[] { 0, 3, 7, 8, 30, sample.Length - 1 })
|
|
Reject(sample[..length]);
|
|
Reject([.. sample, 0]);
|
|
var corruptCount = (byte[])sample.Clone();
|
|
Array.Fill(corruptCount, (byte)0xff, 4, 4);
|
|
Reject(corruptCount);
|
|
var corruptString = (byte[])sample.Clone();
|
|
corruptString[0x22] = 100; // first property's Pascal payload length
|
|
Reject(corruptString);
|
|
Console.WriteLine($"PASS: C# decoded {files.Length} original GAM files, {entities} entities; original startup and corruption checks passed.");
|
|
|
|
var velocity = Vector3.Zero;
|
|
var impulse = new Vector3(0, RecoveredMotion.JumpImpulse, 0);
|
|
var displacement = RecoveredMotion.Integrate(ref velocity, ref impulse, Vector3.Zero, RecoveredMotion.Gravity, 0.125f);
|
|
Require(velocity.Y == 450 && displacement.Y == 56.25f && impulse == Vector3.Zero,
|
|
"Recovered jump/gravity order or impulse consumption differs");
|
|
displacement = RecoveredMotion.Integrate(ref velocity, ref impulse, Vector3.Zero, RecoveredMotion.Gravity, 0.125f);
|
|
Require(velocity.Y == 350 && displacement.Y == 43.75f, "Impulse was incorrectly repeated");
|
|
var speed = 0f;
|
|
for (var i = 0; i < 30; i++) speed = RecoveredMotion.AddSpeed(speed);
|
|
Require(speed == 600, "Original speed cap differs");
|
|
for (var i = 0; i < 30; i++) speed = RecoveredMotion.ReleaseSpeed(speed);
|
|
Require(speed == 0 && RecoveredMotion.ReleaseSpeed(-10) == 0, "Speed decay crossed zero");
|
|
var rng = new OriginalRandom(uint.MaxValue);
|
|
Require(rng.Next() == 65511 && rng.State == 4293335538u, "LCG uint32 overflow differs");
|
|
Console.WriteLine("PASS: recovered impulse/gravity integration, speed limits and wrapping RNG regression checks.");
|
|
|
|
var project = Path.GetFullPath(Path.Combine(root, "../GamePort"));
|
|
var scene = Mesh.LoadSceneJson(Path.Combine(project, "artifacts/extracted/level1b/scene.json"));
|
|
Require(scene.HasExplicitDraws && scene.Draws.Length > 0, "Missing original material draw ranges");
|
|
Require(scene.Draws.Sum(d => (long)d.IndexCount) < scene.Indices.Length,
|
|
"Null-material collision proxy geometry was incorrectly rendered");
|
|
var baseMesh = AseMesh.Load(AssetPath.Resolve(root, "ase/JIMSTOP.ASE"));
|
|
Require(baseMesh.Indices.Length == 2442, "Original Jimmy topology differs");
|
|
Require(baseMesh.Vertices.Any(v => v.Uv != Vector2.Zero), "Original base UVs missing");
|
|
var animation = new MorphAnimation(Path.Combine(project, "artifacts/extracted/animations/jimrun/animation.json"));
|
|
var a = new MeshVertex[animation.Base.Vertices.Length];
|
|
var b = new MeshVertex[a.Length];
|
|
var half = new MeshVertex[a.Length];
|
|
animation.Sample(0, a, Matrix4x4.Identity);
|
|
animation.Sample(animation.SampleInterval, b, Matrix4x4.Identity);
|
|
animation.Sample(animation.SampleInterval / 2, half, Matrix4x4.Identity);
|
|
Require(a.Where((v, i) => v.Position != b[i].Position).Any(), "Original animation samples do not change positions");
|
|
for (var i = 0; i < a.Length; i++)
|
|
{
|
|
Require(Vector3.Distance(half[i].Position, (a[i].Position + b[i].Position) / 2) < 0.0001f,
|
|
"Original morph interpolation differs");
|
|
Require(a[i].Uv == b[i].Uv && a[i].Uv == half[i].Uv, "Morph changed retained base UV");
|
|
}
|
|
var combined = animation.AppendTo(scene);
|
|
Require(combined.Indices.Length == scene.Indices.Length + 2442 && combined.Textures.Count == 34,
|
|
"Original scene/character resource merge differs");
|
|
Require(combined.Indices.All(i => i < combined.Vertices.Length), "Combined scene contains invalid index");
|
|
Console.WriteLine("PASS: null-material visibility, case-insensitive ASE, original morph interpolation/base UV, and scene/character merge.");
|
|
|
|
var start = initial.FindStart()!;
|
|
var placement = PreviewCharacterPlacement.Create(scene, baseMesh, start.Position, start.Rotation.Y);
|
|
Require(placement.FloorAtOrigin is > 0.5f and < 0.6f, "Wrong original lab floor selected (ceiling/proxy?)");
|
|
Require(placement.VerticalCorrection is > 38 and < 40, "Original lab floor penetration was not corrected");
|
|
Require(placement.SourcePosition == start.Position && placement.Position.X == start.Position.X
|
|
&& placement.Position.Z == start.Position.Z, "Preview grounding changed the original GAM data or horizontal position");
|
|
var correctedSole = baseMesh.Vertices.Min(v => Vector3.Transform(v.Position, placement.Model).Y);
|
|
Require(correctedSole >= placement.FloorAtOrigin!.Value - 0.05f, "Standing soles still penetrate the lab floor");
|
|
|
|
Mesh FlatFloors(bool drawProxy)
|
|
{
|
|
var vertices = new List<MeshVertex>();
|
|
foreach (var y in new[] { 10f, 25f, 500f })
|
|
foreach (var p in new[] { new Vector3(-500,y,-500),new Vector3(500,y,-500),new Vector3(500,y,500),
|
|
new Vector3(-500,y,-500),new Vector3(500,y,500),new Vector3(-500,y,500) })
|
|
vertices.Add(new MeshVertex(p, Vector2.Zero, Vector3.One));
|
|
return new Mesh(vertices.ToArray(), Enumerable.Range(0, vertices.Count).Select(i => (uint)i).ToArray())
|
|
{ HasExplicitDraws = true, Draws = drawProxy ? [new MeshDraw(0, 18, -1)] : [new MeshDraw(0, 6, -1), new MeshDraw(12, 6, -1)] };
|
|
}
|
|
var flat = PreviewCharacterPlacement.Create(FlatFloors(false), baseMesh, new Vector3(0,30,0), 0);
|
|
Require(flat.FloorAtOrigin == 10, "Invisible proxy or overhead ceiling selected as preview floor");
|
|
Require(MathF.Abs(baseMesh.Vertices.Min(v => Vector3.Transform(v.Position, flat.Model).Y) - 10) < 0.001f,
|
|
"Standing model does not rest on actual visible floor");
|
|
var raised = PreviewCharacterPlacement.Create(FlatFloors(true), baseMesh, new Vector3(0,30,0), 0);
|
|
Require(raised.FloorAtOrigin == 25, "Nearest visible floor below origin was not selected");
|
|
var noFloor = PreviewCharacterPlacement.Create(new Mesh([], []), baseMesh, start.Position, 0);
|
|
Require(noFloor.VerticalCorrection == 0 && noFloor.FloorAtOrigin == null, "Missing floor caused an invented world offset");
|
|
var clips = new PreviewAnimationLibrary(Path.Combine(project, "artifacts/extracted/animations/jimstop/animation.json"));
|
|
Require(!clips.Current.HasVertexMotion, "Supplied idle unexpectedly has vertex motion");
|
|
foreach (var key in new[] { 31u, 32u, 33u })
|
|
Require(clips.Select(key) && clips.Current.HasVertexMotion, "Animation preview key did not select moving original samples");
|
|
Require(clips.Select(30) && !clips.Current.HasVertexMotion, "Idle selection did not restore original still pose");
|
|
Console.WriteLine($"PASS: original lab grounding (+{placement.VerticalCorrection:F3} Y), ceiling/proxy rejection, and explicit idle/run/jump/scratch selection.");
|
|
|
|
var preparedFixture = Path.Combine(Path.GetTempPath(), "neutron-bank-" + Guid.NewGuid().ToString("N"));
|
|
Directory.CreateDirectory(preparedFixture);
|
|
try
|
|
{
|
|
string WaveManifest(string name, string database, string payload)
|
|
{
|
|
File.WriteAllText(Path.Combine(preparedFixture, name + ".wav"), payload);
|
|
var path = Path.Combine(preparedFixture, name + ".json");
|
|
File.WriteAllText(path, System.Text.Json.JsonSerializer.Serialize(new { resources = new[] {
|
|
new { database, type = "Wave", id = 59, path = name + ".wav", audio = new { seconds = 1.0 } } } }));
|
|
return path;
|
|
}
|
|
var first = WaveManifest("scene", "omt/soundeffects.omt", "same-source-riff");
|
|
var second = WaveManifest("bank", "SoundEffects.omt", "same-source-riff");
|
|
var merged = new PreparedEntityResources(first, second);
|
|
Require(merged.Wave("SOUNDEFFECTS.OMT", 59)?.Seconds == 1, "Explicit bank/scene identity did not merge");
|
|
File.WriteAllText(Path.Combine(preparedFixture, "bank.wav"), "different-source-riff");
|
|
try { _ = new PreparedEntityResources(first, second); throw new Exception("Conflicting audio identities accepted"); }
|
|
catch (InvalidDataException) { }
|
|
Console.WriteLine("PASS: explicit bank/scene resource merge accepts identical waves and rejects conflicting identities.");
|
|
}
|
|
finally { Directory.Delete(preparedFixture, true); }
|
|
|
|
static void Require(bool condition, string message)
|
|
{
|
|
if (!condition) throw new Exception(message);
|
|
}
|
|
|
|
static void Reject(byte[] data)
|
|
{
|
|
try { GamLevel.Parse(data); }
|
|
catch (InvalidDataException) { return; }
|
|
throw new Exception("Malformed GAM was accepted");
|
|
}
|
|
|
|
static void RejectSave(byte[] data)
|
|
{
|
|
try { OriginalSave.Parse(data); }
|
|
catch (InvalidDataException) { return; }
|
|
throw new Exception("Malformed TSK was accepted");
|
|
}
|