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(cli): reject malformed numbers instead of crashing

eyalgolan25f1a5f218ea
diff --git a/sim/Statecraft.Cli/Program.cs b/sim/Statecraft.Cli/Program.cs
index 2c9e26bfd..f107f5b65 100644
--- a/sim/Statecraft.Cli/Program.cs
+++ b/sim/Statecraft.Cli/Program.cs
@@ -20,9 +20,14 @@ Content content;
try { content = ContentLoader.Load(Arg("--content") ?? FindContent()); }
catch (ContentException e) { Console.Error.WriteLine($"Content error: {e.Message}"); return 2; }
-var state = Simulation.NewGame(content, ulong.Parse(Arg("--seed") ?? "1948"), Mode.Historical);
+ulong seed = 1948;
+int turns = 1000;
+var seedText = Arg("--seed");
+if (seedText != null && !InputParse.TryULong(seedText, out seed)) { Console.Error.WriteLine($"Invalid value for --seed: '{seedText}'"); return 1; }
+var turnsText = Arg("--turns");
+if (turnsText != null && !InputParse.TryInt(turnsText, out turns)) { Console.Error.WriteLine($"Invalid value for --turns: '{turnsText}'"); return 1; }
+var state = Simulation.NewGame(content, seed, Mode.Historical);
bool auto = Arg("--auto") == "historical";
-int turns = int.Parse(Arg("--turns") ?? "1000");
for (int t = 0; t < turns && state.GameOver == null; t++)
{
@@ -63,9 +68,15 @@ static List<Command>? ReadCommands()
case "q": return null;
case "n": return list;
case "c" when parts.Length == 3: list.Add(new ChooseEventOption(parts[1], parts[2])); break;
- case "p" when parts.Length == 3: list.Add(new SetPolicy(parts[1], double.Parse(parts[2]))); break;
- case "m" when parts.Length == 2: list.Add(new SetMobilization(double.Parse(parts[1]))); break;
- case "s" when parts.Length == 2: list.Add(new SecurityOrder(int.Parse(parts[1]))); break;
+ case "p" when parts.Length == 3:
+ if (InputParse.TryDouble(parts[2], out var level)) list.Add(new SetPolicy(parts[1], level)); else Console.WriteLine($"Invalid number: '{parts[2]}'");
+ break;
+ case "m" when parts.Length == 2:
+ if (InputParse.TryDouble(parts[1], out var mob)) list.Add(new SetMobilization(mob)); else Console.WriteLine($"Invalid number: '{parts[1]}'");
+ break;
+ case "s" when parts.Length == 2:
+ if (InputParse.TryInt(parts[1], out var delta)) list.Add(new SecurityOrder(delta)); else Console.WriteLine($"Invalid number: '{parts[1]}'");
+ break;
default: Console.WriteLine("commands: c EVENT OPTION | p POLICY LEVEL | m LEVEL | s DELTA | n | q"); break;
}
}
diff --git a/sim/Statecraft.Sim.Tests/CoreTests.cs b/sim/Statecraft.Sim.Tests/CoreTests.cs
index 2587fd8af..614eec130 100644
--- a/sim/Statecraft.Sim.Tests/CoreTests.cs
+++ b/sim/Statecraft.Sim.Tests/CoreTests.cs
@@ -74,3 +74,40 @@ public class CoreTests
Assert.NotEqual(a.Hash(), b.Hash());
}
}
+
+public class InputParseTests
+{
+ [Theory]
+ [InlineData("0.9", 0.9)] [InlineData("1e0", 1.0)] [InlineData("-2", -2.0)]
+ public void ParsesInvariantDoubles(string text, double expected)
+ {
+ Assert.True(Statecraft.Sim.InputParse.TryDouble(text, out var v)); Assert.Equal(expected, v);
+ }
+
+ [Theory]
+ [InlineData("abc")] [InlineData("")] [InlineData("0,9")] [InlineData("NaN")] [InlineData("Infinity")]
+ public void RejectsBadDoubles(string text) => Assert.False(Statecraft.Sim.InputParse.TryDouble(text, out _));
+
+ [Fact]
+ public void IntAndULongRejectJunkAndNegativeSeed()
+ {
+ Assert.True(Statecraft.Sim.InputParse.TryInt("-1", out var i)); Assert.Equal(-1, i);
+ Assert.False(Statecraft.Sim.InputParse.TryInt("x", out _));
+ Assert.True(Statecraft.Sim.InputParse.TryULong("1948", out var u)); Assert.Equal(1948UL, u);
+ Assert.False(Statecraft.Sim.InputParse.TryULong("-5", out _));
+ Assert.False(Statecraft.Sim.InputParse.TryULong("x", out _));
+ }
+
+ [Fact]
+ public void ParsingIsCultureIndependent()
+ {
+ var old = System.Globalization.CultureInfo.CurrentCulture;
+ try
+ {
+ System.Globalization.CultureInfo.CurrentCulture = new System.Globalization.CultureInfo("de-DE");
+ Assert.True(Statecraft.Sim.InputParse.TryDouble("0.9", out var v)); Assert.Equal(0.9, v);
+ Assert.False(Statecraft.Sim.InputParse.TryDouble("0,9", out _));
+ }
+ finally { System.Globalization.CultureInfo.CurrentCulture = old; }
+ }
+}
diff --git a/sim/Statecraft.Sim/InputParse.cs b/sim/Statecraft.Sim/InputParse.cs
new file mode 100644
index 000000000..53002d5bd
--- /dev/null
+++ b/sim/Statecraft.Sim/InputParse.cs
@@ -0,0 +1,17 @@
+using System.Globalization;
+
+namespace Statecraft.Sim
+{
+ /// <summary>Culture-invariant parsing for text front ends; failures are values, never exceptions.</summary>
+ public static class InputParse
+ {
+ public static bool TryDouble(string text, out double value) =>
+ double.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out value) && !double.IsNaN(value) && !double.IsInfinity(value);
+
+ public static bool TryInt(string text, out int value) =>
+ int.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out value);
+
+ public static bool TryULong(string text, out ulong value) =>
+ ulong.TryParse(text, NumberStyles.None, CultureInfo.InvariantCulture, out value);
+ }
+}