From 325119b72639e3542ccb75b3e7d66980a91948bd Mon Sep 17 00:00:00 2001 From: chexit Date: Wed, 4 Nov 2026 20:53:32 +0300 Subject: [PATCH] refactor: validate specs without a per-kind name list The armed/support check in load_specs named the ten armed kinds, so a new unit type would still have required a code edit and repeated the coupling #91 is removing. Drop it: optional missile/gun rows are still validated generically, and the existing kind-coverage check against content.json already ties specs to the catalog. validate_specs now takes the document, content identities and sound events, and a test shows a hypothetical unit kind is accepted from a specs row plus its content entry alone. No data or balance changes. Verified: gen_catalog --check fresh, test_gen_assets 10 tests, core release 195 passed + 8 scenarios (7 ignored), adapter 64 unit + 1 boundary + 5 scenarios, WASM contract 27 steps. --- tools/airdef_assets/catalog.py | 17 +++++++++-------- tools/test_gen_assets.py | 13 +++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/tools/airdef_assets/catalog.py b/tools/airdef_assets/catalog.py index 117d392..ae3f05f 100644 --- a/tools/airdef_assets/catalog.py +++ b/tools/airdef_assets/catalog.py @@ -165,10 +165,16 @@ def _rest_pitch(entry, where): def load_specs(): """Load the numeric profiles shared by Rust and Python, validating every reference.""" - data = json.loads(SPECS.read_text(encoding="utf-8")) + return validate_specs(json.loads(SPECS.read_text(encoding="utf-8")), load(), + {entry["event"] for entry in load_sounds()}) + + +def validate_specs(data, content, events): + """Check a specs document against the content identities and registered sound events. + + Nothing here names a unit or aircraft kind: a new type only needs its catalog entry. + """ _exact(data, {"shared", "drones", "units", "missiles", "named"}, "specs.json") - events = {entry["event"] for entry in load_sounds()} - content = load() shared = data["shared"] _exact(shared, {"mobility", "flight", "logistics"}, "shared") @@ -280,7 +286,6 @@ def load_specs(): unit_kinds = [] used_missiles = set() - armed = {"Patriot", "Gepard", "Pantsir", "S400", "S500", "IrisT", "Nasams", "Buk", "Tor", "P1sun"} for i, entry in enumerate(data["units"]): where = f"units[{i}]" _exact(entry, {"kind", "key", "name", "missile", "gun", "reload", "scale", "rest_pitch", @@ -334,10 +339,6 @@ def load_specs(): raise ValueError(f"{where}.gun.slew: expected [yaw, pitch]") for j, v in enumerate(gun["slew"]): _number(v, f"{where}.gun.slew[{j}]", 0) - if kind in armed and missile is None and gun is None: - raise ValueError(f"{where}: an armed unit needs missiles or a gun") - if kind not in armed and (missile is not None or gun is not None): - raise ValueError(f"{where}: a support unit has no weapons") if set(drone_kinds) != {entry["kind"] for entry in content["aircraft"]}: raise ValueError("specs.json drones and content.json aircraft disagree") diff --git a/tools/test_gen_assets.py b/tools/test_gen_assets.py index 990b4ae..22b6571 100644 --- a/tools/test_gen_assets.py +++ b/tools/test_gen_assets.py @@ -15,6 +15,7 @@ from PIL import Image TOOLS = Path(__file__).resolve().parent sys.path.insert(0, str(TOOLS)) +from airdef_assets import catalog from airdef_assets import pipeline from airdef_assets import icons import gen_tex @@ -33,6 +34,18 @@ class ContentCatalogTests(unittest.TestCase): self.assertEqual(language[f"item.airdef.{name}"], title) self.assertTrue((root / "airdef-mod/data/airdef/horizon/item" / (name + ".json")).is_file()) + def test_a_new_unit_kind_needs_only_catalog_entries(self): + # The validator must not carry its own list of kind names: a hypothetical unit is + # accepted from a specs row plus its content entry, with no code change. + data = json.loads(json.dumps(catalog.load_specs())) + content = json.loads(json.dumps(catalog.load())) + template = next(entry for entry in data["units"] if entry["gun"] is not None) + added = dict(template, kind="TestGun", key="testgun", name="Test gun") + data["units"].append(added) + content["units"].append({"kind": "TestGun", "item": None}) + events = {entry["event"] for entry in catalog.load_sounds()} + catalog.validate_specs(data, content, events) + class ManifestTests(unittest.TestCase): def test_manifest_rejects_traversal_and_duplicate_paths(self):