A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
fix(ui): portrait import decodes everything before writing, stronger portrait tests
eyalgolan746a68dcd561
diff --git a/unity/Assets/Editor/PortraitImport.cs b/unity/Assets/Editor/PortraitImport.csindex 0d8c5d951..c79095917 100644--- a/unity/Assets/Editor/PortraitImport.cs+++ b/unity/Assets/Editor/PortraitImport.cs@@ -21,7 +21,10 @@ namespace Statecraft.Game.EditorToolspublic const string CatalogPath = Folder + "/PortraitCatalog.asset";public const int MaxEdge = 512;- public static void Run()+ public static void Run() => Import(f => File.ReadAllBytes(f));++ /// <summary>Run with a different way of reading source files (tests inject a corrupt one).</summary>+ public static void Import(System.Func<string, byte[]> readSource){var content = ContentLoader.Load(Paths.Content);var people = content.People.Where(p => p.Portrait != null && p.Portrait.File != "").ToList();@@ -35,6 +38,16 @@ namespace Statecraft.Game.EditorToolsif (!File.Exists(source)) throw new FileNotFoundException($"people.yaml: {p.Id} portrait file missing", source);}+ // Decode and downscale everything in memory before touching the folder or the catalog: an unreadable image+ // must leave both exactly as they were.+ var images = new Dictionary<string, byte[]>();+ foreach (var p in people)+ {+ try { images[p.Id] = Downscale(readSource(Path.Combine(Paths.RepoRoot, p.Portrait!.File))); }+ catch (System.Exception e) when (!(e is System.InvalidOperationException && e.Message.StartsWith("people.yaml")))+ { throw new System.InvalidOperationException($"people.yaml: {p.Id} portrait '{p.Portrait!.File}' cannot be read: {e.Message}", e); }+ }+Directory.CreateDirectory(Folder);var wanted = new HashSet<string>(people.Select(p => $"{Folder}/{p.Id}.jpg"));foreach (var stale in Directory.GetFiles(Folder, "*.jpg").Select(f => f.Replace('\\', '/')).Where(f => !wanted.Contains(f)).ToList())@@ -47,7 +60,7 @@ namespace Statecraft.Game.EditorToolsforeach (var p in people){var target = $"{Folder}/{p.Id}.jpg";- var bytes = Downscale(File.ReadAllBytes(Path.Combine(Paths.RepoRoot, p.Portrait!.File)));+ var bytes = images[p.Id];if (!File.Exists(target) || !File.ReadAllBytes(target).SequenceEqual(bytes)){File.WriteAllBytes(target, bytes);diff --git a/unity/Assets/Tests/EditMode/EventCardTests.cs b/unity/Assets/Tests/EditMode/EventCardTests.csindex 97b34912e..0dad010cb 100644--- a/unity/Assets/Tests/EditMode/EventCardTests.cs+++ b/unity/Assets/Tests/EditMode/EventCardTests.cs@@ -56,17 +56,22 @@ public class EventCardTestsusing var ui = new UiTestKit();var bridge = AtArmistice();var real = AssetDatabase.LoadAssetAtPath<PortraitCatalog>(CatalogPath);- var catalog = Object.Instantiate(real); // the real catalog, minus the holder's entry+ var catalog = Object.Instantiate(real); // the real catalog; its entries are edited between renderstry{+ var card = new EventCardPresenter(ui.Root, bridge, Text, catalog);+ var image = ui.Q<VisualElement>("event-image");+ Assert.IsNotNull(catalog.Get("ben_gurion"));+ Assert.AreEqual(catalog.Get("ben_gurion"), image.style.backgroundImage.value.texture, "with the portrait, the texture is set");+ Assert.AreEqual(DisplayStyle.Flex, image.style.display.value);+catalog.entries.RemoveAll(e => e.personId == "ben_gurion");- Assert.IsNull(catalog.Get("ben_gurion"));Assert.Greater(catalog.entries.Count, 0);- new EventCardPresenter(ui.Root, bridge, Text, catalog);- Assert.AreEqual(DisplayStyle.None, ui.Q<VisualElement>("event-image").style.display.value);- // Unity reads a background set to StyleKeyword.None back as Null (verified in a probe); a set texture reads Undefined.- Assert.AreEqual(StyleKeyword.Null, ui.Q<VisualElement>("event-image").style.backgroundImage.keyword);- Assert.IsNull(ui.Q<VisualElement>("event-image").style.backgroundImage.value.texture);+ card.Render();+ // Unity reads a background set to StyleKeyword.None back as Null (verified in a probe), so Null plus no texture is the observable "cleared".+ Assert.IsNull(image.style.backgroundImage.value.texture, "the texture is gone");+ Assert.AreEqual(StyleKeyword.Null, image.style.backgroundImage.keyword);+ Assert.AreEqual(DisplayStyle.None, image.style.display.value);}finally { Object.DestroyImmediate(catalog); }}diff --git a/unity/Assets/Tests/EditMode/PortraitImportTests.cs b/unity/Assets/Tests/EditMode/PortraitImportTests.csindex f03c3d7da..1341f0c40 100644--- a/unity/Assets/Tests/EditMode/PortraitImportTests.cs+++ b/unity/Assets/Tests/EditMode/PortraitImportTests.cs@@ -4,6 +4,8 @@ using System.Linq;using System.Reflection;using System.Security.Cryptography;using NUnit.Framework;+using Statecraft.Game;+using Statecraft.Sim;using UnityEditor;using UnityEngine;@@ -12,6 +14,9 @@ public class PortraitImportTests{const string Folder = "Assets/UI/Portraits";+ static int ExpectedCount() => ContentLoader.Load(Paths.Content).People.Count(p => p.Portrait != null && p.Portrait.File != ""+ && (p.Portrait.License == "Public domain" || p.Portrait.License == "CC0"));+static void RunImport(){var t = Type.GetType("Statecraft.Game.EditorTools.PortraitImport, Assembly-CSharp-Editor");@@ -40,7 +45,8 @@ public class PortraitImportTestspublic void NoPortraitIsLargerThan512PixelsOnAnyTarget(){var paths = Directory.GetFiles(Folder, "*.jpg");- Assert.AreEqual(13, paths.Length);+ Assert.AreEqual(ExpectedCount(), paths.Length);+ Assert.Greater(paths.Length, 0);foreach (var path in paths.Select(p => p.Replace('\\', '/'))){var imp = (TextureImporter)AssetImporter.GetAtPath(path);@@ -52,4 +58,23 @@ public class PortraitImportTestsAssert.LessOrEqual(Mathf.Max(tex.width, tex.height), 512, path + " source size");}}++ [Test]+ public void AnUnreadableSourceLeavesTheFolderAndCatalogUntouched()+ {+ var stale = Folder + "/zz_stale.jpg";+ File.WriteAllBytes(stale, new byte[] { 1, 2, 3 }); // would be deleted by a half-run import+ try+ {+ var before = Snapshot();+ var t = Type.GetType("Statecraft.Game.EditorTools.PortraitImport, Assembly-CSharp-Editor")!;+ int calls = 0;+ Func<string, byte[]> corruptSecond = f => ++calls == 2 ? new byte[] { 0, 1, 2, 3, 4 } : File.ReadAllBytes(f);+ var ex = Assert.Throws<TargetInvocationException>(() => t.GetMethod("Import", BindingFlags.Public | BindingFlags.Static)!.Invoke(null, new object[] { corruptSecond }));+ StringAssert.Contains("cannot be read", ex!.InnerException!.Message);+ Assert.AreEqual(before, Snapshot());+ Assert.IsTrue(File.Exists(stale), "nothing was deleted");+ }+ finally { File.Delete(stale); File.Delete(stale + ".meta"); }+ }}