A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Sim: the state hash formats map placements and every number in the invariant culture (#2335)
Eyal Golan0d40b9cc41db
diff --git a/changelog.d/e21-hash-invariant-culture.md b/changelog.d/e21-hash-invariant-culture.mdnew file mode 100644index 000000000..a8331638d--- /dev/null+++ b/changelog.d/e21-hash-invariant-culture.md@@ -0,0 +1,4 @@+### Fixed++- Behind the scenes: the state hash and its readable text write map placements and every whole number in the+ invariant culture, so a machine set to Hebrew hashes a negative number the same way as an English one.diff --git a/sim/Statecraft.Sim.Tests/GameStateHashCultureTests.cs b/sim/Statecraft.Sim.Tests/GameStateHashCultureTests.csnew file mode 100644index 000000000..d3c919301--- /dev/null+++ b/sim/Statecraft.Sim.Tests/GameStateHashCultureTests.cs@@ -0,0 +1,46 @@+using System;+using System.Globalization;+using Statecraft.Sim;++/// <summary>The state hash and its readable text must not depend on the machine's culture: he-IL writes a negative+/// number with a left-to-right mark before the minus sign (U+200E U+002D), en-US with the minus sign alone.</summary>+public class GameStateHashCultureTests+{+ /// <summary>A legacy state whose every integer written by GameState's own text builder is negative, including a+ /// map placement on a negative hex.</summary>+ static GameState NegativeState()+ {+ var s = new GameState { Date = new DateTime(1948, 6, 1), RngState = 1, Turn = -3, Rung = -2, CeasefireWeeksLeft = -4, MapActionsThisTurn = -1 };+ s.Placements.Add(new Placement(MapActions.TransitCamp, -5, -7, s.Date));+ return s;+ }++ static (string Hash, string Text) Under(CultureInfo culture)+ {+ var prev = CultureInfo.CurrentCulture;+ try+ {+ CultureInfo.CurrentCulture = culture;+ var s = NegativeState();+ return (s.Hash(), s.Describe());+ }+ finally { CultureInfo.CurrentCulture = prev; }+ }++ /// <summary>he-IL with its negative sign set explicitly, so the test does not rely on this machine's ICU data.</summary>+ static CultureInfo Hebrew()+ {+ var he = (CultureInfo)new CultureInfo("he-IL").Clone();+ he.NumberFormat.NegativeSign = "-";+ return he;+ }++ [Fact]+ public void HashAndTextAreTheSameUnderHebrewAndEnglish()+ {+ var invariant = Under(CultureInfo.InvariantCulture);+ Assert.Contains("@-5,-7@", invariant.Text);+ Assert.Equal(invariant, Under(new CultureInfo("en-US")));+ Assert.Equal(invariant, Under(Hebrew()));+ }+}diff --git a/sim/Statecraft.Sim/GameState.cs b/sim/Statecraft.Sim/GameState.csindex ea93ed84c..e24a4cd4d 100644--- a/sim/Statecraft.Sim/GameState.cs+++ b/sim/Statecraft.Sim/GameState.cs@@ -182,7 +182,7 @@ namespace Statecraft.Sim{using var sha = SHA256.Create();var bytes = sha.ComputeHash(Encoding.UTF8.GetBytes(CanonicalText(false)));- return string.Concat(bytes.Select(b => b.ToString("x2")));+ return string.Concat(bytes.Select(b => b.ToString("x2", CultureInfo.InvariantCulture)));}/// <summary>The state in the order and form <see cref="Hash"/> hashes it, one line per section, with each number@@ -220,12 +220,12 @@ namespace Statecraft.Sim}void Sep() { if (!readable) sb.Append(';'); }sb.Append(Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)).Append(';')- .Append(RngState).Append(';').Append(Mode).Append(';').Append(Turn).Append(';');+ .Append(RngState.ToString(CultureInfo.InvariantCulture)).Append(';').Append(Mode).Append(';').Append(Turn.ToString(CultureInfo.InvariantCulture)).Append(';');Map("ind", Indicators); Map("pol", Policies); Map("eff", EffectContributions); Map("shift", IndicatorShifts);Map("bud", Budget); Map("pop", Population);List("sit", ActiveSituations);Num("treasury", Treasury); Num("debt", Debt); Num("fx", FxReserves);- Nl(); sb.Append("rung=").Append(Rung).Append(";cf=").Append(CeasefireWeeksLeft).Append(';');+ Nl(); sb.Append("rung=").Append(Rung.ToString(CultureInfo.InvariantCulture)).Append(";cf=").Append(CeasefireWeeksLeft.ToString(CultureInfo.InvariantCulture)).Append(';');Num("mob", Mobilization); Num("stab", CoalitionStability); if (usesAttention) Num("att", Attention);List("fired", FiredEvents);List("pending", PendingEvents);@@ -242,10 +242,10 @@ namespace Statecraft.SimNl(); sb.Append("map[");foreach (var p in Placements){- Entry(); sb.Append(p.Action).Append('@').Append(p.Q).Append(',').Append(p.R).Append('@').Append(p.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)); Sep();+ Entry(); sb.Append(p.Action).Append('@').Append(p.Q.ToString(CultureInfo.InvariantCulture)).Append(',').Append(p.R.ToString(CultureInfo.InvariantCulture)).Append('@').Append(p.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)); Sep();}Close(']');- Nl(); sb.Append("mapturn=").Append(MapActionsTurnDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)).Append(':').Append(MapActionsThisTurn).Append(';');+ Nl(); sb.Append("mapturn=").Append(MapActionsTurnDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)).Append(':').Append(MapActionsThisTurn.ToString(CultureInfo.InvariantCulture)).Append(';');Nl(); sb.Append("over=").Append(GameOver ?? "");// GDD v0.1 sections (spec 2026-10-12 section 2.4): each written only when set, so with the legacy loop the text// above is the whole text, byte for byte. The over= line has no separator after it, so each section adds one.