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.

feat(sim): ContentLoader.Load(readFile, eventFiles) for platforms without a file system (I5)

eyalgolan465451ca32e2
diff --git a/sim/Statecraft.Sim.Tests/ContentLoaderTests.cs b/sim/Statecraft.Sim.Tests/ContentLoaderTests.cs
index b74ab6107..fad5be946 100644
--- a/sim/Statecraft.Sim.Tests/ContentLoaderTests.cs
+++ b/sim/Statecraft.Sim.Tests/ContentLoaderTests.cs
@@ -153,4 +153,41 @@ public class ContentLoaderTests
Assert.Equal("people.yaml", ex.File);
Assert.Equal("people[def_two].offices", ex.Field);
}
+
+ // I5: a WebGL build reads StreamingAssets through a URL, so the loader also takes a reader function.
+ static Func<string, string?> Reader(string dir) => rel =>
+ {
+ var path = Path.Combine(dir, rel);
+ return File.Exists(path) ? File.ReadAllText(path) : null;
+ };
+
+ static string[] EventFiles(string dir) =>
+ Array.ConvertAll(Directory.GetFiles(Path.Combine(dir, "events"), "*.yaml"), f => "events/" + Path.GetFileName(f));
+
+ [Fact]
+ public void ReaderAndDirectoryEntryPointsLoadTheSameContent()
+ {
+ var dir = Fixture.Write(events: new Dictionary<string, string> { ["drought.yaml"] = ValidationTests.Event });
+ var fromDir = ContentLoader.Load(dir);
+ var fromReader = ContentLoader.Load(Reader(dir), EventFiles(dir));
+ Assert.Single(fromReader.Events);
+ Assert.Equal("events/drought.yaml", fromReader.Events[0].File);
+ Assert.Equal(Simulation.NewGame(fromDir, 7, Mode.Historical).Hash(), Simulation.NewGame(fromReader, 7, Mode.Historical).Hash());
+ }
+
+ [Theory]
+ [InlineData("parties.yaml")]
+ [InlineData("events/drought.yaml")]
+ public void MissingFileGivesTheSameErrorThroughBothEntryPoints(string missing)
+ {
+ var dir = Fixture.Write(events: new Dictionary<string, string> { ["drought.yaml"] = ValidationTests.Event });
+ var events = EventFiles(dir);
+ File.Delete(Path.Combine(dir, missing));
+ var viaReader = Assert.Throws<ContentException>(() => ContentLoader.Load(Reader(dir), events));
+ Assert.Equal((missing, "(file)", "missing file"), (viaReader.File, viaReader.Field, viaReader.Message.Substring(viaReader.Message.LastIndexOf(": ") + 2)));
+ if (missing.StartsWith("events/")) return; // the directory entry point lists events itself, so it never asks for a deleted one
+ var viaDir = Assert.Throws<ContentException>(() => ContentLoader.Load(dir));
+ Assert.Equal(viaDir.Message, viaReader.Message);
+ Assert.Equal(viaDir.Field, viaReader.Field);
+ }
}
diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.cs
index 2af4d4f01..e49d18ac7 100644
--- a/sim/Statecraft.Sim/ContentLoader.cs
+++ b/sim/Statecraft.Sim/ContentLoader.cs
@@ -33,15 +33,16 @@ namespace Statecraft.Sim
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build();
- static T Read<T>(string root, string relPath)
+ static T Read<T>(Func<string, string?> readFile, string relPath)
{
- var path = Path.Combine(root, relPath);
- if (!File.Exists(path)) throw new ContentException(relPath, "(file)", "missing file");
- T result;
- try { result = Yaml.Deserialize<T>(File.ReadAllText(path)) ?? throw new ContentException(relPath, "(file)", "empty file"); }
- catch (YamlException e) { throw new ContentException(relPath, $"line {e.Start.Line}", e.InnerException?.Message ?? e.Message); }
+ string? text;
+ try { text = readFile(relPath); }
catch (IOException e) { throw new ContentException(relPath, "(file)", e.Message); }
catch (UnauthorizedAccessException e) { throw new ContentException(relPath, "(file)", e.Message); }
+ if (text == null) throw new ContentException(relPath, "(file)", "missing file");
+ T result;
+ try { result = Yaml.Deserialize<T>(text) ?? throw new ContentException(relPath, "(file)", "empty file"); }
+ catch (YamlException e) { throw new ContentException(relPath, $"line {e.Start.Line}", e.InnerException?.Message ?? e.Message); }
CheckNulls(relPath, result is GameEvent ev ? $"events[{ev.Id}]" : "", result!);
return result;
}
@@ -81,33 +82,50 @@ namespace Statecraft.Sim
static bool IsContentType(Type t) => t.IsClass && t != typeof(string) && t.Namespace == typeof(Content).Namespace;
- public static Content Load(string contentRoot)
+ /// <summary>Loads content through <paramref name="readFile"/>, which takes a path relative to the content root
+ /// ("people.yaml", "events/x.yaml") and returns the file's text, or null when it is missing. This is the
+ /// entry point for platforms without a file system (a WebGL build reads StreamingAssets through a URL).
+ /// <paramref name="eventFiles"/> are the event files, relative to the content root; they load in ordinal order.</summary>
+ public static Content Load(Func<string, string?> readFile, IEnumerable<string> eventFiles)
{
+ if (readFile == null) throw new ArgumentNullException(nameof(readFile));
+ if (eventFiles == null) throw new ArgumentNullException(nameof(eventFiles));
var c = new Content();
- c.Offices = Read<OfficesFile>(contentRoot, "offices.yaml").Offices;
- c.Parties = Read<PartiesFile>(contentRoot, "parties.yaml").Parties;
- c.People = Read<PeopleFile>(contentRoot, "people.yaml").People;
- c.Legislatures = Read<LegislaturesFile>(contentRoot, "legislatures.yaml").Legislatures;
- c.Governments = Read<GovernmentsFile>(contentRoot, "governments.yaml").Governments;
- var m = Read<ModelFile>(contentRoot, "model.yaml");
+ c.Offices = Read<OfficesFile>(readFile, "offices.yaml").Offices;
+ c.Parties = Read<PartiesFile>(readFile, "parties.yaml").Parties;
+ c.People = Read<PeopleFile>(readFile, "people.yaml").People;
+ c.Legislatures = Read<LegislaturesFile>(readFile, "legislatures.yaml").Legislatures;
+ c.Governments = Read<GovernmentsFile>(readFile, "governments.yaml").Governments;
+ var m = Read<ModelFile>(readFile, "model.yaml");
c.Indicators = m.Indicators; c.Policies = m.Policies; c.Effects = m.Effects;
c.Situations = m.Situations; c.VoterGroups = m.VoterGroups; c.Actors = m.Actors;
- c.Rules = Read<Rules>(contentRoot, "rules.yaml");
- c.Aliyah = Read<AliyahFile>(contentRoot, "aliyah.yaml").Series;
- c.PopulationAnchors = Read<PopulationFile>(contentRoot, "population.yaml").Anchors;
- var eventsDir = Path.Combine(contentRoot, "events");
- if (Directory.Exists(eventsDir))
- foreach (var f in Directory.GetFiles(eventsDir, "*.yaml").OrderBy(p => p, StringComparer.Ordinal))
- {
- var rel = "events/" + Path.GetFileName(f);
- var e = Read<GameEvent>(contentRoot, rel);
- e.File = rel;
- c.Events.Add(e);
- }
+ c.Rules = Read<Rules>(readFile, "rules.yaml");
+ c.Aliyah = Read<AliyahFile>(readFile, "aliyah.yaml").Series;
+ c.PopulationAnchors = Read<PopulationFile>(readFile, "population.yaml").Anchors;
+ foreach (var rel in eventFiles.OrderBy(p => p, StringComparer.Ordinal))
+ {
+ var e = Read<GameEvent>(readFile, rel);
+ e.File = rel;
+ c.Events.Add(e);
+ }
Validate(c);
return c;
}
+ /// <summary>Loads content from a directory: every events/*.yaml file is an event.</summary>
+ public static Content Load(string contentRoot)
+ {
+ var eventsDir = Path.Combine(contentRoot, "events");
+ var eventFiles = Directory.Exists(eventsDir)
+ ? Directory.GetFiles(eventsDir, "*.yaml").Select(f => "events/" + Path.GetFileName(f)).ToList()
+ : new List<string>();
+ return Load(rel =>
+ {
+ var path = Path.Combine(contentRoot, rel);
+ return File.Exists(path) ? File.ReadAllText(path) : null;
+ }, eventFiles);
+ }
+
internal const string IdRule = "must be lowercase letters, digits and underscores, starting with a letter";
/// <summary>Every id has the office-id shape (so it is safe in placeholders, CLI input and save files) and is unique in its list.</summary>