Ship S2 fixes: RequireDesigns refuses nulls explicitly, and a failing Compose falls back to the shipped catalog

- RequireDesigns names the null (entity, item, zone record, lot, lot ledger) instead of catching NullReferenceException.
- ResolveCatalog reports a Compose-level failure as an exclusion covering all mods and boots the shipped catalog.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-30 10:06:16 +02:00
commit 34319b0df1
5 changed files with 77 additions and 23 deletions

View file

@ -128,6 +128,7 @@ public static class EntitySerializer
// docking bay units, and their stored contents. The provenance ledger's GC root walk.
public static IEnumerable<ItemInstance> Items(EntityPack pack)
{
if (pack == null) throw new InvalidOperationException("This run's save is malformed: an entity is null. Start a new game.");
yield return pack.Hull;
foreach (var (_, item) in pack.Equipment) yield return item;
foreach (var (_, item) in pack.CargoBays) yield return item;

View file

@ -96,22 +96,21 @@ public static class RunSave
// built; the save is never edited to fit.
public static void RequireDesigns(CultCache cache, SavedGame game)
{
string[] named;
try
var designs = new List<CultRecordKey>();
foreach (var zoneRef in game.Zones ?? Array.Empty<CultRecordRef<SavedZone>>())
{
named = (game.Zones ?? Array.Empty<CultRecordRef<SavedZone>>())
.SelectMany(zone => cache.Get(zone)?.Contents?.Entities ?? new List<EntityPack>())
.SelectMany(EntitySerializer.Items).Select(item => item.Data.Key)
.Concat(Lots(cache).Lots.Values.Select(lot => lot.Design.Key))
.Where(key => key.IsSet()).Distinct()
.Where(key => cache.Get<ItemData>(key) == null)
.Select(key => key.Value).OrderBy(key => key, StringComparer.Ordinal).ToArray();
}
catch (NullReferenceException error)
{
// A save with a null entity, item or lot where one is required is malformed, and refused like a missing design.
throw new InvalidOperationException("This run's save is malformed: an entity, item or lot is missing where one is required. Start a new game.", error);
var where = zoneRef.Key.Value;
var zone = cache.Get(zoneRef) ?? throw MalformedSave($"zone {where} has no record");
// A zone never visited has no contents: legitimately null.
foreach (var pack in zone.Contents?.Entities ?? new List<EntityPack>())
foreach (var item in EntitySerializer.Items(pack))
designs.Add((item ?? throw MalformedSave($"an item in {where} is null")).Data.Key);
}
foreach (var (number, lot) in Lots(cache).Lots ?? throw MalformedSave("the lot ledger has no lots"))
designs.Add((lot ?? throw MalformedSave($"lot {number} is null")).Design.Key);
var named = designs.Where(key => key.IsSet()).Distinct()
.Where(key => cache.Get<ItemData>(key) == null)
.Select(key => key.Value).OrderBy(key => key, StringComparer.Ordinal).ToArray();
if (named.Length == 0) return;
const string modHull = "mod-hull:";
var mods = named.Where(key => key.StartsWith(modHull, StringComparison.Ordinal)).Select(key => key.Substring(modHull.Length)).ToArray();
@ -122,6 +121,9 @@ public static class RunSave
". Reinstall them, or start a new game.");
}
private static InvalidOperationException MalformedSave(string where) =>
new InvalidOperationException($"This run's save is malformed: {where}. Start a new game.");
// The stored ledger, or a fresh empty one when the run has minted nothing yet.
public static ProvenanceLedger Lots(CultCache cache) => cache.GetGlobal<ProvenanceLedger>() ?? new ProvenanceLedger();

View file

@ -158,7 +158,10 @@ public static class ShipModCatalog
if (!Directory.Exists(modsRoot) ||
!Directory.GetDirectories(modsRoot).Any(directory => File.Exists(Path.Combine(directory, "ship.cc"))))
return (shippedCatalog, Array.Empty<Exclusion>());
var composition = Compose(shippedCatalog, derivedCatalog, modsRoot);
Composition composition;
// A failure of the composition itself (an unwritable derived path, say) is still a mod problem, never a reason not to boot.
try { composition = Compose(shippedCatalog, derivedCatalog, modsRoot); }
catch (Exception error) { return (shippedCatalog, new[] { new Exclusion { Package = "(all mods)", Reason = error.Message } }); }
return (composition.Included.Length == 0 ? shippedCatalog : derivedCatalog, composition.Excluded);
}