using System.Numerics; using System.Text.Json; using GamePort.Assets; namespace GamePort.Application; /// Original screens.omt laboratory objectives and PracticeYesNo; positions from EXE 402C40/407070. public sealed class SceneHud { private sealed record Quad(int Canvas, int VertexOffset, uint FirstIndex, uint Width, uint Height); private readonly List quads = []; private static readonly Dictionary Dimensions = new() { [120] = (256, 29), [121] = (256, 29), [122] = (256, 29), [156] = (320, 173), [169] = (207, 100), [83] = (8, 8), [84] = (8, 8), [85] = (8, 8) }; public Mesh Mesh { get; } public SceneHud(Mesh combined, string manifest) { var full = Path.GetFullPath(manifest); if (new FileInfo(full).Length > 64 * 1024) throw new InvalidDataException("UI manifest exceeds limit"); var directory = Path.GetDirectoryName(full)!; using var doc = JsonDocument.Parse(File.ReadAllBytes(full), new JsonDocumentOptions { MaxDepth = 12 }); var root = doc.RootElement; if (root.GetProperty("version").GetInt32() != 1 || root.GetProperty("referenceWidth").GetInt32() != 640 || root.GetProperty("referenceHeight").GetInt32() != 480) throw new InvalidDataException("Unsupported prepared UI format"); var recovered = new Dictionary(); var entries = root.GetProperty("textures"); if (entries.GetArrayLength() != Dimensions.Count) throw new InvalidDataException("Unexpected UI texture count"); foreach (var entry in entries.EnumerateArray()) { var id = entry.GetProperty("id").GetInt32(); var width = entry.GetProperty("width").GetUInt32(); var height = entry.GetProperty("height").GetUInt32(); if (!Dimensions.TryGetValue(id, out var expected) || expected != (width, height)) throw new InvalidDataException("Unexpected UI canvas identity/dimensions"); var path = Path.GetFullPath(Path.Combine(directory, entry.GetProperty("path").GetString()!)); if (!path.StartsWith(directory + Path.DirectorySeparatorChar, StringComparison.Ordinal)) throw new InvalidDataException("UI resource escapes prepared directory"); var bytes = checked((long)width * height * 4); if (new FileInfo(path).Length != bytes) throw new InvalidDataException("Invalid prepared UI pixel count"); if (!recovered.TryAdd(id, new(width, height, File.ReadAllBytes(path)))) throw new InvalidDataException("Duplicate prepared UI canvas"); } var vertices = combined.Vertices.ToList(); var indices = combined.Indices.ToList(); var draws = combined.Draws.ToList(); if (!combined.HasExplicitDraws && draws.Count == 0 && indices.Count != 0) draws.Add(new(0, (uint)indices.Count, -1)); var textures = new Dictionary(combined.Textures); // Pad precedes writing; modal follows the HUD, exactly as layered screen elements. foreach (var id in new[] { 156, 120, 121, 122, 83, 84, 85, 169 }) { var image = recovered[id]; var texture = int.MinValue; while (textures.ContainsKey(texture)) texture++; textures.Add(texture, image); var first = (uint)vertices.Count; quads.Add(new(id, vertices.Count, (uint)indices.Count, image.Width, image.Height)); vertices.AddRange(new[] { new MeshVertex(Vector3.Zero, new(0, 0), Vector3.One), new MeshVertex(Vector3.Zero, new(1, 0), Vector3.One), new MeshVertex(Vector3.Zero, new(1, 1), Vector3.One), new MeshVertex(Vector3.Zero, new(0, 1), Vector3.One) }); draws.Add(new((uint)indices.Count, 6, texture) { ScreenSpace = true, Visible = false }); indices.AddRange(new[] { first, first + 1, first + 2, first, first + 2, first + 3 }); } Mesh = new(vertices.ToArray(), indices.ToArray()) { Draws = draws.ToArray(), HasExplicitDraws = true, Textures = textures, CollisionPolygons = combined.CollisionPolygons }; } /// Writes the final merged buffer, including when later visual composers copied these vertices. public void Update(Mesh destination, int currentObjective, bool objectivesVisible, bool practiceVisible, float objectiveYOffset = -69, bool objectiveContentVisible = true, uint viewportWidth = 640, uint viewportHeight = 480) { if (!float.IsFinite(objectiveYOffset) || objectiveYOffset < -480 || objectiveYOffset > 480) throw new ArgumentOutOfRangeException(nameof(objectiveYOffset)); if (destination.Vertices.Length < Mesh.Vertices.Length || destination.Indices.Length < Mesh.Indices.Length) throw new ArgumentException("Destination omits HUD ranges", nameof(destination)); var viewport = new UiViewport(viewportWidth, viewportHeight); foreach (var quad in quads) { var draw = destination.Draws.FirstOrDefault(d => d.FirstIndex == quad.FirstIndex && d.ScreenSpace && d.IndexCount == 6) ?? throw new ArgumentException("Destination omits HUD draws", nameof(destination)); draw.Visible = quad.Canvas == 169 ? practiceVisible : objectivesVisible && currentObjective is >= 1 and <= 3 && (quad.Canvas == 156 || objectiveContentVisible && (quad.Canvas == 119 + currentObjective || quad.Canvas == 82 + currentObjective)); var digit = quad.Canvas is >= 83 and <= 85; var x = quad.Canvas == 169 ? 210f : digit ? 393f : 384f; var y = quad.Canvas == 169 ? 210f : (quad.Canvas == 156 ? 380f : digit ? 450f : 448f) + objectiveYOffset; var anchor = quad.Canvas == 169 ? viewport.CenterOffset : viewport.BottomRightOffset; Vector3 Position(float px, float py) => viewport.ToNdc(new Vector2(px, py) + anchor); destination.Vertices[quad.VertexOffset].Position = Position(x, y); destination.Vertices[quad.VertexOffset + 1].Position = Position(x + quad.Width, y); destination.Vertices[quad.VertexOffset + 2].Position = Position(x + quad.Width, y + quad.Height); destination.Vertices[quad.VertexOffset + 3].Position = Position(x, y + quad.Height); } } }