Ship S1 fixes: refuse the retired embedded hull, one body per hull, closed validator gaps

Load reads the raw ship authoring payload and refuses a non-nil retired key 1 (the
pre-S1 embedded hull) naming the old shape, so a stale hull is no longer silently
ignored; ship_cc.py refuses the same file on read and therefore on every save.

A hull naming both a Unity prefab and a visual is refused by one predicate
(ShipAuthoringStore.RequireOneBody), called by Validate and by Compose over every hull
in the derived catalog, shipped ones included; a whitespace-only Prefab is unset. This
replaces the mod-ship-only "cannot name a Unity prefab" check.

The validator also refuses non-finite or negative hardpoint Armor and FiringArc, an
undefined HardpointType or ItemRotation, and an anchor Role outside the eight the
schema documents.

Load wraps MessagePack decode failures with the file path. Python replace_layout
refuses non-boolean layout and hardpoint-footprint cells.

Tests: exact Visual key comparison, HullData.Visual at key 32, a save refreshing
stored_at, plus one case per new refusal. HullData.Visual is now [Inspectable] like
Prefab (Unity editor only; not compiled here).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-30 06:18:20 +02:00
commit f470c1b775
9 changed files with 316 additions and 10 deletions

View file

@ -532,7 +532,7 @@ public class HullData : EquippableItemData
// The hull's visual: a ShipAuthoring record holding its model package, or unset for a Unity-prefab hull. A hull
// names exactly one of Prefab and Visual. Keys 30 and 31 are EquippableItemData's, so 32 is the next free key.
[JsonProperty("visual"), Key(32)]
[Inspectable, JsonProperty("visual"), Key(32)]
public CultRecordRef<ShipAuthoring> Visual;
[IgnoreMember]

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using GameCult.Caching;
using GameCult.Caching.MessagePack;
using MessagePack;
@ -49,6 +50,12 @@ public sealed class ShipPolyline
public static class ShipAuthoringStore
{
// ShipAnchor.Role's vocabulary.
private static readonly HashSet<string> Roles = new HashSet<string>(StringComparer.Ordinal)
{
"map-icon", "hull-collider", "shield", "tractor", "thruster-emitter", "weapon-muzzle", "radiator-mesh", "articulation"
};
public static CultCache Open(string path, bool writable = false)
{
var cache = new CultCache();
@ -74,6 +81,49 @@ public static class ShipAuthoringStore
// The structure of a ship file, without judging its content (drafts load): exactly one hull and one visual, each at
// its deterministic key, the hull naming that visual by its typed ref.
public static (HullData Hull, ShipAuthoring Visual) Load(string path)
{
RefuseLegacyEmbeddedHull(path);
try { return LoadRecords(path); }
catch (MessagePackSerializationException error)
{
throw new InvalidOperationException($"{path}: a record does not decode as its schema: {error.Message}", error);
}
}
// Key 1 of ShipAuthoring held the whole hull before S1. The type no longer has that member, so a deserialized record
// shows nothing of it, and a file that still carries one would load as a ship silently ignoring its old hull. The
// raw payload is the only place the old shape is visible.
private static void RefuseLegacyEmbeddedHull(string path)
{
if (!File.Exists(path)) return;
var bytes = File.ReadAllBytes(path);
if (bytes.Length == 0) return;
CultPersistedStoreSnapshot snapshot;
try { snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(bytes); }
catch (MessagePackSerializationException error)
{
throw new InvalidOperationException($"{path}: not a readable ship file: {error.Message}", error);
}
var schema = typeof(ShipAuthoring).GetCustomAttribute<CultDocumentAttribute>().SchemaName;
foreach (var record in snapshot.Records)
{
if (snapshot.SchemaCatalog.FirstOrDefault(entry => entry.SchemaId == record.SchemaId)?.SchemaName != schema) continue;
var reader = new MessagePackReader(record.Payload);
if (reader.NextMessagePackType != MessagePackType.Array) continue;
var slots = reader.ReadArrayHeader();
for (var slot = 0; slot < slots; slot++)
{
if (slot == RetiredHullKey && !reader.TryReadNil())
throw new InvalidOperationException($"{path}: {record.Key}: legacy embedded hull at retired key {RetiredHullKey} of the ship authoring record. " +
$"Migrate it: write the hull as its own hull record at mod-hull:<id> naming the visual, and rewrite the ship authoring record without key {RetiredHullKey}.");
if (slot != RetiredHullKey) reader.Skip();
}
}
}
private const int RetiredHullKey = 1;
private static (HullData Hull, ShipAuthoring Visual) LoadRecords(string path)
{
using var cache = Open(path);
var hulls = cache.GetAll<HullData>().ToArray();
@ -96,6 +146,15 @@ public static class ShipAuthoringStore
return (hull, visual);
}
// A hull names one body: a Unity prefab (shipped hulls) or a ShipAuthoring record (mod ships), never both, so no
// consumer needs a precedence rule. A whitespace-only Prefab is unset. Every path that validates or composes hulls
// calls this.
public static void RequireOneBody(HullData hull, string label)
{
if (!string.IsNullOrWhiteSpace(hull.Prefab) && hull.Visual.IsSet())
throw new InvalidOperationException($"{label}: a hull names one body, but this one names both a Unity prefab and a visual record.");
}
// The one semantic check every path shares: the hull and its visual, judged together.
public static void Validate(HullData hull, ShipAuthoring ship)
{
@ -108,9 +167,7 @@ public static class ShipAuthoringStore
if (string.IsNullOrWhiteSpace(hull.Name)) throw new InvalidOperationException($"{ship.Id}: hull name is required.");
if (hull.Shape?.Cells == null || !hull.Shape.Cells.Cast<bool>().Any(occupied => occupied))
throw new InvalidOperationException($"{ship.Id}: schematic must contain at least one cell.");
// A hull names exactly one visual: a Unity prefab (shipped hulls) or a ShipAuthoring record (mod ships).
if (!string.IsNullOrEmpty(hull.Prefab))
throw new InvalidOperationException($"{ship.Id}: a mod ship cannot name a Unity prefab.");
RequireOneBody(hull, ship.Id);
if (!hull.Visual.Key.Equals(ShipModCatalog.AuthoringKey(ship.Id)))
throw new InvalidOperationException($"{ship.Id}: the hull must name its visual record {ShipModCatalog.AuthoringKey(ship.Id).Value}.");
if (string.IsNullOrWhiteSpace(ship.ModelAsset) || Path.IsPathRooted(ship.ModelAsset) ||
@ -129,6 +186,9 @@ public static class ShipAuthoringStore
if (!nodeIds.Add(anchor.ModelNodeId))
throw new InvalidOperationException($"{ship.Id}: model node {anchor.ModelNodeId} is claimed by more than one anchor.");
}
foreach (var anchor in anchors)
if (!Roles.Contains(anchor.Role))
throw new InvalidOperationException($"{ship.Id}: anchor {anchor.Id} has unknown role '{anchor.Role}'.");
foreach (var role in new[] { "map-icon", "hull-collider", "shield", "tractor" })
if (anchors.Count(anchor => anchor.Role == role) != 1)
throw new InvalidOperationException($"{ship.Id}: exactly one {role} anchor is required.");
@ -140,6 +200,12 @@ public static class ShipAuthoringStore
if (hardpoint == null || string.IsNullOrWhiteSpace(hardpoint.Transform) ||
!mounts.Add(hardpoint.Transform))
throw new InvalidOperationException($"{ship.Id}: hardpoint IDs must be present and unique.");
if (!Enum.IsDefined(typeof(HardpointType), hardpoint.Type))
throw new InvalidOperationException($"{ship.Id}: hardpoint {hardpoint.Transform} has an unknown type {(int)hardpoint.Type}.");
if (!Enum.IsDefined(typeof(ItemRotation), hardpoint.Rotation))
throw new InvalidOperationException($"{ship.Id}: hardpoint {hardpoint.Transform} has an unknown rotation {(int)hardpoint.Rotation}.");
if (!float.IsFinite(hardpoint.Armor) || hardpoint.Armor < 0 || !float.IsFinite(hardpoint.FiringArc) || hardpoint.FiringArc < 0)
throw new InvalidOperationException($"{ship.Id}: hardpoint {hardpoint.Transform} armor and firing arc must be finite and not negative.");
if (hardpoint.Shape?.Cells == null || !hardpoint.Shape.Cells.Cast<bool>().Any(occupied => occupied))
throw new InvalidOperationException($"{ship.Id}: hardpoint {hardpoint.Transform} has no cells.");
for (var cellX = 0; cellX < hardpoint.Shape.Width; cellX++)

View file

@ -70,6 +70,9 @@ public static class ShipModCatalog
// Verbatim: the mod store's own two records, under the keys it already holds them at.
ShipAuthoringStore.Write(cache, hull, ship);
}
// Shipped hulls too: the derived catalog must not hold a hull that names two bodies.
foreach (var hull in cache.GetAll<HullData>())
ShipAuthoringStore.RequireOneBody(hull, hull.Name);
cache.FlushAsync().GetAwaiter().GetResult();
}
if (File.Exists(output)) File.Replace(temporary, output, null);