Reconstruct lab story, interactions, patrol and jet pack practice flow

This commit is contained in:
UnixNight committed 2026-10-24 20:33:03 -04:00
1 parent 58424120bb
commit c929f942e1
84 files changed
+7957 -305

No files matched your search

+17 -2
View File
@@ -14,6 +14,7 @@ public struct MeshVertex(Vector3 position, Vector2 uv, Vector3 color)
public sealed record Mesh(MeshVertex[] Vertices, uint[] Indices)
{
public MeshPolygonSource[] CollisionPolygons { get; init; } = [];
public MeshDraw[] Draws { get; init; } = [];
public bool HasExplicitDraws { get; init; }
public Dictionary<int,PngImage> Textures { get; init; } = [];
@@ -38,6 +39,14 @@ public sealed record Mesh(MeshVertex[] Vertices, uint[] Indices)
var indices = root.GetProperty("indices").EnumerateArray().Select(p => p.GetUInt32()).ToArray();
if (indices.Length % 3 != 0 || indices.Any(i => i >= vertices.Length)) throw new InvalidDataException("Invalid scene triangle indices.");
if (vertices.Length == 0 || indices.Length == 0) throw new InvalidDataException("Empty scene mesh.");
var collisionPolygons = root.TryGetProperty("collisionPolygons", out var polygonList)
? polygonList.EnumerateArray().Select(p => new MeshPolygonSource(p.GetProperty("shapeId").GetInt32(),
p.GetProperty("polygonIndex").GetInt32(),
new Vector3(p.GetProperty("normal")[0].GetSingle(), p.GetProperty("normal")[1].GetSingle(), p.GetProperty("normal")[2].GetSingle()),
p.GetProperty("materialName").GetString())).ToArray() : [];
if (collisionPolygons.Length != 0 && (collisionPolygons.Length != indices.Length / 3
|| collisionPolygons.Any(p => !float.IsFinite(p.Normal.X + p.Normal.Y + p.Normal.Z))))
throw new InvalidDataException("Invalid source collision polygon metadata");
var textures = new Dictionary<int,PngImage>();
if(root.TryGetProperty("textures",out var textureList)) foreach(var t in textureList.EnumerateArray())
{
@@ -71,8 +80,14 @@ public sealed record Mesh(MeshVertex[] Vertices, uint[] Indices)
for(var i=first;i<first+count;i++)vertices[indices[i]].Color=material.Item2;
draws.Add(new MeshDraw(first,count,material.Item1));
}
return new Mesh(vertices, indices){Draws=draws.ToArray(),HasExplicitDraws=true,Textures=textures};
return new Mesh(vertices, indices){Draws=draws.ToArray(),HasExplicitDraws=true,Textures=textures,CollisionPolygons=collisionPolygons};
}
}
public sealed record MeshDraw(uint FirstIndex,uint IndexCount,int TextureId);
public readonly record struct MeshPolygonSource(int ShapeId, int PolygonIndex, Vector3 Normal, string? MaterialName);
public sealed record MeshDraw(uint FirstIndex,uint IndexCount,int TextureId)
{
public bool Visible { get; set; } = true;
public bool ScreenSpace { get; init; }
}
+4 -3
View File
@@ -72,18 +72,19 @@ public sealed class MorphAnimation
}
}
public Mesh AppendTo(Mesh scene)
public Mesh AppendTo(Mesh scene, int textureId = int.MinValue)
{
var vertices = new MeshVertex[scene.Vertices.Length + Base.Vertices.Length];
scene.Vertices.CopyTo(vertices, 0);
Base.Vertices.CopyTo(vertices, scene.Vertices.Length);
var indices = scene.Indices.Concat(Base.Indices.Select(i => checked(i + (uint)scene.Vertices.Length))).ToArray();
var draws = scene.HasExplicitDraws ? scene.Draws : [new MeshDraw(0, (uint)scene.Indices.Length, -1)];
var textures = new Dictionary<int, PngImage>(scene.Textures) { [int.MinValue] = Texture };
if (scene.Textures.ContainsKey(textureId)) throw new InvalidDataException("Actor texture identity already in use");
var textures = new Dictionary<int, PngImage>(scene.Textures) { [textureId] = Texture };
return new Mesh(vertices, indices)
{
HasExplicitDraws = true,
Draws = [.. draws, new MeshDraw((uint)scene.Indices.Length, (uint)Base.Indices.Length, int.MinValue)],
Draws = [.. draws, new MeshDraw((uint)scene.Indices.Length, (uint)Base.Indices.Length, textureId)],
Textures = textures
};
}
+114
View File
@@ -0,0 +1,114 @@
using System.Buffers.Binary;
using System.Numerics;
using System.Text;
namespace GamePort.Assets;
public sealed record SavedTask(string Name, int State);
/// <summary>Original TSK wire layout recovered at EXE0x45FEE0/0x4603F0; unknown fields retained.</summary>
public sealed record OriginalSave(string LevelCode, float PlayerValue, short Counter,
int Score, int OtherCounter, int[] ItemStates, int[] LevelCounters,
string StartPoint, string ReservedName, string ReturnLevel, string ReturnStart,
Vector3 StoredVector, int[] InventoryStates, SavedTask[] Tasks)
{
public const int ItemCount = 4200, LevelCounterCount = 29, InventoryCount = 19;
public static OriginalSave Load(string path) => Parse(File.ReadAllBytes(path));
public static OriginalSave Parse(ReadOnlySpan<byte> bytes)
{
var reader = new Reader(bytes);
var code = reader.Text(4);
if (code.Any(c => !char.IsAsciiLetterOrDigit(c))) throw new InvalidDataException("Invalid TSK level code");
var playerValue = reader.Float();
var counter = reader.Short();
var score = reader.Integer();
var otherCounter = reader.Integer();
var items = reader.Integers(ItemCount);
var levels = reader.Integers(LevelCounterCount);
var start = reader.Pascal();
var reserved = reader.Pascal();
var returnLevel = reader.Pascal();
var returnStart = reader.Pascal();
var vector = new Vector3(reader.Float(), reader.Float(), reader.Float());
var inventory = reader.Integers(InventoryCount);
var count = reader.Integer();
if (count < 0 || count > reader.Remaining / 5) throw new InvalidDataException("Invalid TSK task count");
var tasks = new SavedTask[count];
for (var i = 0; i < count; i++) tasks[i] = new(reader.Pascal(), reader.Integer());
if (reader.Remaining != 0) throw new InvalidDataException("Unexpected TSK trailing bytes");
return new(code, playerValue, counter, score, otherCounter, items, levels, start, reserved,
returnLevel, returnStart, vector, inventory, tasks);
}
public byte[] Encode()
{
using var output = new MemoryStream();
void Integer(int value)
{
Span<byte> raw = stackalloc byte[4];
BinaryPrimitives.WriteInt32BigEndian(raw, value);
output.Write(raw);
}
void Float(float value)
{
if (!float.IsFinite(value)) throw new InvalidDataException("Nonfinite TSK float");
Integer(BitConverter.SingleToInt32Bits(value));
}
void Text(string value, bool pascal)
{
if (value.Any(c => c > 255) || pascal && value.Length > 255)
throw new InvalidDataException("TSK text must fit original Latin1 Pascal string");
if (pascal) output.WriteByte((byte)value.Length);
output.Write(Encoding.Latin1.GetBytes(value));
}
void Table(int[] values, int length)
{
if (values.Length != length) throw new InvalidDataException("Invalid fixed TSK table length");
foreach (var value in values) Integer(value);
}
if (LevelCode.Length != 4 || LevelCode.Any(c => !char.IsAsciiLetterOrDigit(c)))
throw new InvalidDataException("Invalid TSK level code");
Text(LevelCode, false);
Float(PlayerValue);
Span<byte> shortBytes = stackalloc byte[2];
BinaryPrimitives.WriteInt16BigEndian(shortBytes, Counter);
output.Write(shortBytes);
Integer(Score); Integer(OtherCounter);
Table(ItemStates, ItemCount); Table(LevelCounters, LevelCounterCount);
Text(StartPoint, true); Text(ReservedName, true); Text(ReturnLevel, true); Text(ReturnStart, true);
Float(StoredVector.X); Float(StoredVector.Y); Float(StoredVector.Z);
Table(InventoryStates, InventoryCount);
Integer(Tasks.Length);
foreach (var task in Tasks) { Text(task.Name, true); Integer(task.State); }
return output.ToArray();
}
private ref struct Reader(ReadOnlySpan<byte> data)
{
private readonly ReadOnlySpan<byte> bytes = data;
private int offset;
public int Remaining => bytes.Length - offset;
private ReadOnlySpan<byte> Take(int count)
{
if (count < 0 || count > Remaining) throw new InvalidDataException($"Truncated TSK at0x{offset:X}");
var value = bytes.Slice(offset, count); offset += count; return value;
}
public string Text(int count) => Encoding.Latin1.GetString(Take(count));
public string Pascal() => Text(Take(1)[0]);
public short Short() => BinaryPrimitives.ReadInt16BigEndian(Take(2));
public int Integer() => BinaryPrimitives.ReadInt32BigEndian(Take(4));
public float Float()
{
var value = BitConverter.Int32BitsToSingle(Integer());
return float.IsFinite(value) ? value : throw new InvalidDataException("Nonfinite TSK float");
}
public int[] Integers(int count)
{
if (count > Remaining / 4) throw new InvalidDataException("Truncated fixed TSK table");
var values = new int[count];
for (var i = 0; i < count; i++) values[i] = Integer();
return values;
}
}
}
@@ -0,0 +1,60 @@
using System.Text.Json;
namespace GamePort.Assets;
public sealed record PreparedWave(string Path, double Seconds);
/// <summary>Original database/type/id resource identities retained by entities.py.</summary>
public sealed class PreparedEntityResources
{
private readonly Dictionary<(string Database, int Id), PreparedWave> waves = [];
private readonly Dictionary<(string Database, int Id), PngImage> canvases = [];
public PreparedEntityResources(params string[] manifests)
{
foreach (var manifest in manifests) Load(manifest);
}
private void Load(string manifest)
{
var directory = Path.GetDirectoryName(Path.GetFullPath(manifest))!;
string Resolve(string relative)
{
var full = Path.GetFullPath(Path.Combine(directory, relative));
if (!full.StartsWith(directory + Path.DirectorySeparatorChar, StringComparison.Ordinal))
throw new InvalidDataException("Prepared resource escapes its directory");
return full;
}
using var doc = JsonDocument.Parse(File.ReadAllText(manifest));
foreach (var resource in doc.RootElement.GetProperty("resources").EnumerateArray())
{
if (!resource.TryGetProperty("path", out var path)) continue;
var key = (Normalize(resource.GetProperty("database").GetString()!), resource.GetProperty("id").GetInt32());
switch (resource.GetProperty("type").GetString())
{
case "Wave":
var seconds = resource.GetProperty("audio").GetProperty("seconds").GetDouble();
if (!double.IsFinite(seconds) || seconds < 0) throw new InvalidDataException("Invalid prepared audio duration");
var resolved = Resolve(path.GetString()!);
if (waves.TryGetValue(key, out var previous))
{
if (previous.Seconds != seconds || !File.ReadAllBytes(previous.Path).AsSpan().SequenceEqual(File.ReadAllBytes(resolved)))
throw new InvalidDataException("Conflicting prepared wave identity");
}
else waves.Add(key, new(resolved, seconds));
break;
case "Canv":
var width = resource.GetProperty("width").GetUInt32();
var height = resource.GetProperty("height").GetUInt32();
var rgba = File.ReadAllBytes(Resolve(path.GetString()!));
if (width == 0 || height == 0 || width > 8192 || height > 8192 || rgba.Length != checked((int)(width * height * 4)))
throw new InvalidDataException("Invalid prepared canvas");
canvases.Add(key, new(width, height, rgba));
break;
}
}
}
private static string Normalize(string name) => Path.GetFileName(name.Replace('\\', '/')).ToUpperInvariant();
public PreparedWave? Wave(string database, int id) => waves.GetValueOrDefault((Normalize(database), id));
public PngImage? Canvas(string database, int id) => canvases.GetValueOrDefault((Normalize(database), id));
public double Duration(string database, int id) => Wave(database, id)?.Seconds
?? throw new FileNotFoundException($"Prepare original speech resource {database}:Wave:{id}");
}