ship_cc: replace_visual writes ShipAuthoring's model asset and anchors, pinned to the C# keys
MODEL_ASSET_SLOT, ANCHORS_SLOT and ANCHOR_MEMBERS sit beside the other slot constants and ShipSchemaPinTests binds them to ShipAuthoring's and ShipAnchor's [Key]s. replace_visual carries an anchor's slots past Order by Id, refuses a blank or duplicate id, a wrong-length row and a changed ship id, and leaves the hull record and the lines alone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f98be434f0
commit
aca1d53e6f
3 changed files with 95 additions and 0 deletions
|
|
@ -17,6 +17,8 @@ public sealed class ShipSchemaPinTests
|
|||
private static readonly string Source = File.ReadAllText(Locate("tools", "blender", "aetheria_ships", "ship_cc.py"));
|
||||
|
||||
[Theory]
|
||||
[InlineData("MODEL_ASSET_SLOT", typeof(ShipAuthoring), nameof(ShipAuthoring.ModelAsset))]
|
||||
[InlineData("ANCHORS_SLOT", typeof(ShipAuthoring), nameof(ShipAuthoring.Anchors))]
|
||||
[InlineData("SCHEMATIC_LINES_SLOT", typeof(ShipAuthoring), nameof(ShipAuthoring.SchematicLines))]
|
||||
[InlineData("HULL_SHAPE_SLOT", typeof(HullData), nameof(HullData.Shape))]
|
||||
[InlineData("HULL_HARDPOINTS_SLOT", typeof(HullData), nameof(HullData.Hardpoints))]
|
||||
|
|
@ -56,6 +58,15 @@ public sealed class ShipSchemaPinTests
|
|||
Assert.Equal(slot, KeyOf(typeof(HardpointData), members[slot]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PythonAnchorMembersAreShipAnchorMembersAtTheirPositions()
|
||||
{
|
||||
var members = Strings("ANCHOR_MEMBERS");
|
||||
Assert.Equal(5, members.Length);
|
||||
for (var slot = 0; slot < members.Length; slot++)
|
||||
Assert.Equal(slot, KeyOf(typeof(ShipAnchor), members[slot]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PythonEnumNamesAreTheEnumsInValueOrder()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -21,12 +21,16 @@ HULL_SCHEMA = "aetheria.hulldata"
|
|||
# HardpointData). RETIRED_HULL_SLOT is the ShipAuthoring key that held the embedded hull before S1; no member owns it now. ShipSchemaPinTests in tests/Aetheria.Shared.Tests fails when any number or name below
|
||||
# disagrees with those types' [Key] attributes, so a renumbered or added member cannot drift silently.
|
||||
RETIRED_HULL_SLOT = 1
|
||||
MODEL_ASSET_SLOT = 2 # ShipAuthoring.ModelAsset
|
||||
ANCHORS_SLOT = 3 # ShipAuthoring.Anchors
|
||||
SCHEMATIC_LINES_SLOT = 4 # ShipAuthoring.SchematicLines
|
||||
HULL_SHAPE_SLOT = 5 # ItemData.Shape
|
||||
HULL_HARDPOINTS_SLOT = 23 # HullData.Hardpoints
|
||||
# HardpointData member names in slot order; a hardpoint row is one value per name, and any slot past the last
|
||||
# name belongs to a later schema and is carried through untouched.
|
||||
HARDPOINT_MEMBERS = ("Type", "Position", "Shape", "Transform", "Rotation", "Armor", "FiringArc")
|
||||
# ShipAnchor member names in slot order; as for hardpoints, slots past the last name are carried through by anchor Id.
|
||||
ANCHOR_MEMBERS = ("Id", "Role", "ModelNodeId", "ParentId", "Order")
|
||||
# Enum member names in value order (HardpointType, ItemRotation).
|
||||
HARDPOINT_TYPE_NAMES = ("Hull", "Tool", "Thermal", "Thruster", "WarpDrive", "Reactor", "Radiator",
|
||||
"Shield", "Sensors", "Energy", "Ballistic", "Launcher", "ControlModule", "AetherDrive")
|
||||
|
|
@ -109,6 +113,33 @@ def replace_lines(path: str, cultlib_packages: str, lines: list[list[Any]]) -> i
|
|||
return len(lines)
|
||||
|
||||
|
||||
def replace_visual(path: str, cultlib_packages: str, expected_id: str, model_asset: str,
|
||||
anchors: list[list[Any]]) -> int:
|
||||
"""Writes the visual's model asset and anchor list (ShipAuthoring slots 2 and 3) and nothing else. Each anchor row is
|
||||
ANCHOR_MEMBERS in order; the slots past Order that the same anchor Id carried before are appended to it unchanged."""
|
||||
file = read(path, cultlib_packages)
|
||||
if file.ship.body[0] != expected_id:
|
||||
raise ValueError("The bound ship ID changed; rebind the collection")
|
||||
known = len(ANCHOR_MEMBERS)
|
||||
ids = set()
|
||||
for anchor in anchors:
|
||||
if len(anchor) != known:
|
||||
raise ValueError(f"Each anchor row needs exactly {', '.join(ANCHOR_MEMBERS)}")
|
||||
anchor_id = anchor[0]
|
||||
if not isinstance(anchor_id, str) or not anchor_id.strip():
|
||||
raise ValueError("Every anchor needs an ID")
|
||||
if anchor_id in ids:
|
||||
raise ValueError(f"Anchor ID {anchor_id} is used twice")
|
||||
ids.add(anchor_id)
|
||||
previous = file.ship.body[ANCHORS_SLOT] if len(file.ship.body) > ANCHORS_SLOT else None
|
||||
later = {row[0]: row[known:] for row in previous or []
|
||||
if isinstance(row, list) and len(row) > known and isinstance(row[0], str)}
|
||||
file.ship.body[MODEL_ASSET_SLOT] = model_asset
|
||||
file.ship.body[ANCHORS_SLOT] = [list(anchor) + later.get(anchor[0], []) for anchor in anchors]
|
||||
file.store.push(_stamped(file.ship, file.msgpack))
|
||||
return len(anchors)
|
||||
|
||||
|
||||
def _hull_body(file: ShipFile) -> list:
|
||||
if len(file.hull.body) <= HULL_HARDPOINTS_SLOT:
|
||||
raise ValueError("Ship hull has an incompatible typed payload")
|
||||
|
|
|
|||
|
|
@ -230,6 +230,59 @@ class ReplaceLinesTests(ShipFileCase):
|
|||
self.assertEqual(before, self.bytes())
|
||||
|
||||
|
||||
class ReplaceVisualTests(ShipFileCase):
|
||||
def anchor(self, anchor_id, role="map-icon", parent=None, order=0):
|
||||
return [anchor_id, role, anchor_id, parent, order]
|
||||
|
||||
def test_writes_the_model_asset_and_anchor_rows_in_member_order(self):
|
||||
rows = [self.anchor("map"), self.anchor("gun", "weapon-mount"), self.anchor("gun.muzzle", "weapon-muzzle", "gun", 1)]
|
||||
self.assertEqual(3, ship_cc.replace_visual(self.path, PACKAGES, "mod.skiff", "ship.glb", rows))
|
||||
ship = self.ship()
|
||||
self.assertEqual("ship.glb", ship[ship_cc.MODEL_ASSET_SLOT])
|
||||
self.assertEqual(rows, ship[ship_cc.ANCHORS_SLOT])
|
||||
self.assertEqual(dict(zip(ship_cc.ANCHOR_MEMBERS, rows[2])),
|
||||
{"Id": "gun.muzzle", "Role": "weapon-muzzle", "ModelNodeId": "gun.muzzle", "ParentId": "gun", "Order": 1})
|
||||
|
||||
def test_replace_visual_carries_unknown_anchor_slots_by_id(self):
|
||||
ship = ship_body()
|
||||
ship[ship_cc.ANCHORS_SLOT] = [["map", "map-icon", "map", None, 0, "future-anchor-slot"],
|
||||
["shield", "shield", "shield", None, 0, "shield-future"]]
|
||||
self.write(ship, hull_body())
|
||||
# Reordered, with one new anchor: each known Id keeps its own tail, the new one gets none.
|
||||
ship_cc.replace_visual(self.path, PACKAGES, "mod.skiff", "ship.glb",
|
||||
[self.anchor("shield", "shield"), self.anchor("tractor", "tractor"), self.anchor("map")])
|
||||
self.assertEqual([["shield", "shield", "shield", None, 0, "shield-future"],
|
||||
["tractor", "tractor", "tractor", None, 0],
|
||||
["map", "map-icon", "map", None, 0, "future-anchor-slot"]], self.ship()[ship_cc.ANCHORS_SLOT])
|
||||
|
||||
def test_replace_visual_refuses_blank_or_duplicate_ids_and_a_changed_ship(self):
|
||||
before = self.bytes()
|
||||
cases = (("blank", "mod.skiff", [self.anchor(" ")], "needs an ID"),
|
||||
("none", "mod.skiff", [[None, "map-icon", "x", None, 0]], "needs an ID"),
|
||||
("duplicate", "mod.skiff", [self.anchor("map"), self.anchor("map", "shield")], "used twice"),
|
||||
("arity", "mod.skiff", [["map", "map-icon", "map", None]], "exactly"),
|
||||
("ship", "mod.other", [self.anchor("map")], "ship ID changed"))
|
||||
for name, ship_id, rows, message in cases:
|
||||
with self.subTest(name):
|
||||
with self.assertRaisesRegex(ValueError, message):
|
||||
ship_cc.replace_visual(self.path, PACKAGES, ship_id, "ship.glb", rows)
|
||||
self.assertEqual(before, self.bytes())
|
||||
|
||||
def test_replace_visual_touches_only_model_asset_and_anchors(self):
|
||||
hull_before = self.envelopes()[(ship_cc.HULL_SCHEMA, "mod-hull:mod.skiff")]
|
||||
ship_before = self.ship()
|
||||
ship_cc.replace_visual(self.path, PACKAGES, "mod.skiff", "ship.glb", [self.anchor("hull", "hull-collider")])
|
||||
after = self.envelopes()
|
||||
self.assertEqual(hull_before, after[(ship_cc.HULL_SCHEMA, "mod-hull:mod.skiff")])
|
||||
ship_after = self.ship()
|
||||
for slot, (old, new) in enumerate(zip(ship_before, ship_after)):
|
||||
if slot not in (ship_cc.MODEL_ASSET_SLOT, ship_cc.ANCHORS_SLOT):
|
||||
self.assertEqual(old, new, f"slot {slot}")
|
||||
self.assertEqual(len(ship_before), len(ship_after))
|
||||
self.assertEqual(LINES, ship_after[ship_cc.SCHEMATIC_LINES_SLOT])
|
||||
self.assertEqual(CATALOG.schema_id, after[(ship_cc.SCHEMA, "mod-ship:mod.skiff")].schema_id)
|
||||
|
||||
|
||||
class LayoutTests(ShipFileCase):
|
||||
def layout(self):
|
||||
return ship_cc.read_layout(self.path, PACKAGES)
|
||||
|
|
|
|||
Loading…
Reference in a new issue