Skip to content

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): final Track B residuals (Tasks 20, 21)

eyalgolanc64864119c64
diff --git a/unity/Assets/Scripts/SaveStore.cs b/unity/Assets/Scripts/SaveStore.cs
index 93901d6be..120ed7639 100644
--- a/unity/Assets/Scripts/SaveStore.cs
+++ b/unity/Assets/Scripts/SaveStore.cs
@@ -25,7 +25,7 @@ namespace Statecraft.Game
public string AutosavePath => Path.Combine(Directory, AutosaveName);
- public virtual void WriteAutosave(string text)
+ public void WriteAutosave(string text)
{
System.IO.Directory.CreateDirectory(Directory);
var tmp = AutosavePath + ".tmp";
@@ -35,8 +35,10 @@ namespace Statecraft.Game
}
/// <summary>The autosave text, or null. WriteAutosave deletes the old file only after the .tmp is completely written, so
- /// a crash between the delete and the move leaves just the .tmp, which is complete: it is read and promoted. When both
- /// exist the .tmp may be partial and is ignored.</summary>
+ /// a crash between the delete and the move leaves just the .tmp, and then the .tmp is complete: it is read and promoted.
+ /// (A crash in the middle of the very first write can leave a partial .tmp and no autosave.txt; that ends safely too:
+ /// the load throws, the file is set aside and a new game starts with a note.) When both files exist the .tmp may be
+ /// partial and is ignored.</summary>
public virtual string? ReadAutosave()
{
if (File.Exists(AutosavePath)) return File.ReadAllText(AutosavePath);
@@ -48,15 +50,23 @@ namespace Statecraft.Game
return text;
}
- /// <summary>Moves an unreadable autosave aside (autosave.bad.txt) so it is kept for a bug report.</summary>
+ /// <summary>Moves an unreadable autosave aside (autosave.bad.txt) so it is kept for a bug report. With no autosave.txt it
+ /// sets aside a lingering .tmp instead (a .tmp that could not be promoted or read would otherwise be rejected, with a
+ /// note, on every start).</summary>
public virtual void SetAsideAutosave()
{
- if (!File.Exists(AutosavePath)) return;
+ var source = File.Exists(AutosavePath) ? AutosavePath : File.Exists(AutosavePath + ".tmp") ? AutosavePath + ".tmp" : null;
+ if (source == null) return;
var bad = Path.Combine(Directory, "autosave.bad.txt");
if (File.Exists(bad)) File.Delete(bad);
- File.Move(AutosavePath, bad);
+ File.Move(source, bad);
}
- public virtual void DeleteAutosave() { if (File.Exists(AutosavePath)) File.Delete(AutosavePath); }
+ /// <summary>Removes the autosave and any lingering .tmp (a leftover would be resurrected by the next start).</summary>
+ public virtual void DeleteAutosave()
+ {
+ if (File.Exists(AutosavePath)) File.Delete(AutosavePath);
+ if (File.Exists(AutosavePath + ".tmp")) File.Delete(AutosavePath + ".tmp");
+ }
}
}
diff --git a/unity/Assets/Tests/EditMode/MapActionUiTests.cs b/unity/Assets/Tests/EditMode/MapActionUiTests.cs
index b774e7cf8..9a986fcfe 100644
--- a/unity/Assets/Tests/EditMode/MapActionUiTests.cs
+++ b/unity/Assets/Tests/EditMode/MapActionUiTests.cs
@@ -214,9 +214,20 @@ public class MapActionUiTests
Assert.LessOrEqual(view.style.left.value.value + view.style.width.value.value, 100f);
Assert.LessOrEqual(view.style.top.value.value + view.style.height.value.value, 100f);
// a view at the very edge stays inside the picture
- mini.SetView(f, f.ToWorld(b.lon0, b.lat0, 0), 20000f);
- Assert.GreaterOrEqual(view.style.left.value.value, 0f);
- Assert.GreaterOrEqual(view.style.top.value.value, 0f);
+ foreach (var (lon, lat) in new[] { (b.lon0, b.lat0), (b.lon1, b.lat0), (b.lon0, b.lat1), (b.lon1, b.lat1) })
+ {
+ mini.SetView(f, f.ToWorld(lon, lat, 0), 20000f);
+ yield return null;
+ Assert.GreaterOrEqual(view.style.left.value.value, 0f);
+ Assert.GreaterOrEqual(view.style.top.value.value, 0f);
+ Assert.LessOrEqual(view.style.left.value.value + view.style.width.value.value, 100.001f, "right edge");
+ Assert.LessOrEqual(view.style.top.value.value + view.style.height.value.value, 100.001f, "bottom edge");
+ if (view.layout.width > 0)
+ {
+ Assert.LessOrEqual(view.layout.xMax, image.layout.width + 0.5f);
+ Assert.LessOrEqual(view.layout.yMax, image.layout.height + 0.5f);
+ }
+ }
Assert.AreSame(image, view.parent, "the frame is positioned in the picture's own coordinates");
}
@@ -347,4 +358,18 @@ public class MapActionUiTests
mini.Dispose();
Assert.IsTrue(tex == null, "the texture is destroyed");
}
+
+ [Test]
+ public void AClickBeforeLayoutRaisesNothing()
+ {
+ using var ui = new UiTestKit();
+ using var mini = new MinimapPresenter(ui.Root, Hexes);
+ var image = ui.Q<VisualElement>("minimap-image");
+ Assert.IsFalse(image.layout.width > 0, "no layout has run yet");
+ int clicks = 0;
+ mini.Clicked += (lon, lat) => clicks++;
+ using (var e = PointerDownEvent.GetPooled(new Event { type = EventType.MouseDown, button = 0, mousePosition = new Vector2(10, 10) }))
+ { e.target = image; image.SendEvent(e); }
+ Assert.AreEqual(0, clicks);
+ }
}
diff --git a/unity/Assets/Tests/EditMode/SaveUiTests.cs b/unity/Assets/Tests/EditMode/SaveUiTests.cs
index 8240e47d4..4b9f3cba3 100644
--- a/unity/Assets/Tests/EditMode/SaveUiTests.cs
+++ b/unity/Assets/Tests/EditMode/SaveUiTests.cs
@@ -304,4 +304,56 @@ public class SaveUiTests
Assert.AreEqual("Turn 0", ui.Q<Label>("turn").text);
Assert.AreEqual(DisplayStyle.None, ui.Q<Label>("toast").style.display.value);
}
+
+ [Test]
+ public void ALingeringTmpIsSetAsideWhenThereIsNoAutosave()
+ {
+ // Promotion failed and the .tmp cannot be read: autosave.txt is missing, so the .tmp itself must be set aside,
+ // or it would be found, rejected and noted on every start.
+ var store = TempStore();
+ Directory.CreateDirectory(store.Directory);
+ File.WriteAllText(store.AutosavePath + ".tmp", "half a sav");
+ store.SetAsideAutosave();
+ Assert.IsFalse(File.Exists(store.AutosavePath + ".tmp"));
+ Assert.IsTrue(File.Exists(Path.Combine(store.Directory, "autosave.bad.txt")));
+ var notes = new List<string>();
+ Assert.AreEqual(0, GameStart.Begin(C, store, () => 1UL, notes, Text).State.Turn);
+ Assert.IsEmpty(notes, "nothing is left to reject on the next start");
+ }
+
+ [Test]
+ public void ABrokenFirstWriteEndsSafely()
+ {
+ // A crash in the middle of the very first write leaves a partial .tmp and no autosave.txt.
+ var store = TempStore();
+ var good = GameSession.New(C, 9, Mode.Historical).Save.Write();
+ Directory.CreateDirectory(store.Directory);
+ File.WriteAllText(store.AutosavePath + ".tmp", good.Substring(0, good.Length / 2));
+ var notes = new List<string>();
+ LogAssert.Expect(LogType.Warning, new System.Text.RegularExpressions.Regex("Autosave rejected"));
+ var session = GameStart.Begin(C, store, () => 1UL, notes, Text);
+ Assert.AreEqual(0, session.State.Turn);
+ Assert.AreEqual(1, notes.Count);
+ Assert.IsFalse(File.Exists(store.AutosavePath + ".tmp"));
+ Assert.IsFalse(File.Exists(store.AutosavePath));
+ Assert.IsTrue(File.Exists(Path.Combine(store.Directory, "autosave.bad.txt")));
+ }
+
+ [Test]
+ public void NewGameAlsoRemovesALingeringTmp()
+ {
+ using var ui = new UiTestKit();
+ var store = TempStore();
+ var played = new SimBridge(GameSession.New(C, 3, Mode.Historical), Text);
+ played.EndTurn();
+ Directory.CreateDirectory(store.Directory);
+ File.WriteAllText(store.AutosavePath + ".tmp", played.Session.Save.Write()); // a complete save, only the .tmp
+ var toast = new ToastPresenter(ui.Root, played, Text);
+ new NewGamePresenter(ui.Root, played, store, toast, Text, () => 4UL);
+ ui.Press("new-game"); ui.Press("new-game");
+ Assert.IsFalse(File.Exists(store.AutosavePath + ".tmp"));
+ var notes = new List<string>();
+ Assert.AreEqual(0, GameStart.Begin(C, store, () => 1UL, notes, Text).State.Turn, "the next start does not resurrect the abandoned game");
+ Assert.IsEmpty(notes);
+ }
}