46 lines
1.8 KiB
Python
46 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate Rust content entries from the shared catalog; --check detects drift."""
|
|
import argparse
|
|
from pathlib import Path
|
|
import re
|
|
|
|
from airdef_assets.catalog import load
|
|
|
|
OUTPUT = Path(__file__).resolve().parents[1] / "airdef-mod/src/sim/catalog_entries.rs"
|
|
|
|
|
|
def generate(data):
|
|
lines = ["// Generated by tools/gen_catalog.py from catalog/content.json. Do not edit.",
|
|
"pub const AIRCRAFT: &[Aircraft] = &["]
|
|
for entry in data["aircraft"]:
|
|
kind, item = entry["kind"], entry["item"]
|
|
if not re.fullmatch(r"[A-Z][A-Za-z0-9]*", kind):
|
|
raise ValueError(f"invalid Rust kind {kind!r}")
|
|
if item is not None and not re.fullmatch(r"[a-z0-9_]+", item):
|
|
raise ValueError(f"invalid item ID {item!r}")
|
|
item_expr = f'Some("airdef:{item}")' if item is not None else "None"
|
|
lines.append(f' Aircraft {{ kind: {kind}, launch_item: {item_expr}, raid: {str(entry["raid"]).lower()} }},')
|
|
lines += ["];", "", "pub const UNITS: &[Unit] = &["]
|
|
for entry in data["units"]:
|
|
kind, item = entry["kind"], entry["item"]
|
|
if not re.fullmatch(r"[A-Z][A-Za-z0-9]*", kind) or not re.fullmatch(r"[a-z0-9_]+", item):
|
|
raise ValueError(f"invalid unit {entry!r}")
|
|
lines.append(f' Unit {{ kind: SamKind::{kind}, deploy_item: "airdef:{item}" }},')
|
|
return "\n".join(lines + ["];", ""])
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--check", action="store_true")
|
|
args = parser.parse_args()
|
|
generated = generate(load())
|
|
if args.check:
|
|
if not OUTPUT.exists() or OUTPUT.read_text(encoding="utf-8") != generated:
|
|
parser.exit(1, f"{OUTPUT} is stale; run python3 tools/gen_catalog.py\n")
|
|
else:
|
|
OUTPUT.write_text(generated, encoding="utf-8")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|