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(save): preserve earlier rejected autosave files (#1996)

Eyal Golanaf83ced3cdf0
diff --git a/changelog.d/release-save-preservation.md b/changelog.d/release-save-preservation.md
new file mode 100644
index 000000000..c5a1aab6b
--- /dev/null
+++ b/changelog.d/release-save-preservation.md
@@ -0,0 +1,3 @@
+### Fixed
+
+- Preserve earlier rejected autosaves when another unreadable autosave or temporary save is set aside, keeping each file for diagnosis.
diff --git a/unity/Assets/Scripts/SaveStore.cs b/unity/Assets/Scripts/SaveStore.cs
index 70a64c77d..0c2317151 100644
--- a/unity/Assets/Scripts/SaveStore.cs
+++ b/unity/Assets/Scripts/SaveStore.cs
@@ -53,13 +53,14 @@ namespace Statecraft.Game
/// <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>
+ /// note, on every start). Existing rejected files are kept; later rejections use unique filenames. The Guid names
+ /// disk evidence only and never enters simulation state. File.Move does not overwrite an existing destination.</summary>
public virtual void SetAsideAutosave()
{
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);
+ if (File.Exists(bad)) bad = Path.Combine(Directory, "autosave.bad." + System.Guid.NewGuid().ToString("N") + ".txt");
File.Move(source, bad);
}
diff --git a/unity/Assets/Tests/EditMode/SaveUiTests.cs b/unity/Assets/Tests/EditMode/SaveUiTests.cs
index f6a7eeb59..ef251c13d 100644
--- a/unity/Assets/Tests/EditMode/SaveUiTests.cs
+++ b/unity/Assets/Tests/EditMode/SaveUiTests.cs
@@ -127,6 +127,44 @@ public class SaveUiTests
finally { Application.logMessageReceived -= Logged; }
}
+ [TestCase(false)]
+ [TestCase(true)]
+ public void ASecondRejectedFilePreservesBothSetsOfBytes(bool lingeringTmp)
+ {
+ var store = TempStore();
+ Directory.CreateDirectory(store.Directory);
+ var first = new byte[] { 0, 13, 10, 255, 65 };
+ var second = new byte[] { 66, 0, 254 };
+ File.WriteAllBytes(store.AutosavePath, first);
+ store.SetAsideAutosave();
+ var source = store.AutosavePath + (lingeringTmp ? ".tmp" : "");
+ File.WriteAllBytes(source, second);
+ store.SetAsideAutosave();
+ var original = Path.Combine(store.Directory, "autosave.bad.txt");
+ CollectionAssert.AreEqual(first, File.ReadAllBytes(original));
+ var later = Directory.GetFiles(store.Directory).Where(path => path != original).ToArray();
+ Assert.AreEqual(1, later.Length);
+ CollectionAssert.AreEqual(second, File.ReadAllBytes(later[0]));
+ Assert.IsFalse(File.Exists(source));
+ }
+
+ [Test]
+ public void AStartThatRejectsASavePreservesAnEarlierBadFile()
+ {
+ var store = TempStore();
+ Directory.CreateDirectory(store.Directory);
+ var original = Path.Combine(store.Directory, "autosave.bad.txt");
+ var first = new byte[] { 0, 13, 10, 255, 65 };
+ File.WriteAllBytes(original, first);
+ store.WriteAutosave("statecraft-save 9\ncontent x\nseed 1\nmode Historical\nend x\n");
+ var notes = new List<string>();
+ Assert.AreEqual(0, GameStart.Begin(C, store, () => 5UL, notes, Text).State.Turn);
+ Assert.AreEqual(Text["save.refused." + SaveException.NewerVersion], notes[0]);
+ CollectionAssert.AreEqual(first, File.ReadAllBytes(original));
+ Assert.AreEqual(2, Directory.GetFiles(store.Directory).Length);
+ Assert.IsFalse(File.Exists(store.AutosavePath));
+ }
+
[Test]
public void AnUnreadableAutosaveIsSetAsideAndANewGameStarts()
{