59 lines
2.5 KiB
Python
59 lines
2.5 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, load_sounds
|
|
|
|
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 generate_sound_ids(entries):
|
|
lines = ["// Generated by tools/gen_catalog.py from catalog/sounds.json. Do not edit."]
|
|
for entry in entries:
|
|
event = entry["event"]
|
|
name = event.replace(".", "_").upper()
|
|
lines.append(f'pub const {name}: &str = "airdef:{event}";')
|
|
if entry["far"]:
|
|
lines.append(f'pub const {name}_FAR: &str = "airdef:{event}_far";')
|
|
return "\n".join(lines + [""])
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--check", action="store_true")
|
|
args = parser.parse_args()
|
|
sound_output = OUTPUT.with_name("catalog_sound_ids.rs")
|
|
outputs = ((OUTPUT, generate(load())), (sound_output, generate_sound_ids(load_sounds())))
|
|
for path, generated in outputs:
|
|
if args.check:
|
|
if not path.exists() or path.read_text(encoding="utf-8") != generated:
|
|
parser.exit(1, f"{path} is stale; run python3 tools/gen_catalog.py\n")
|
|
else:
|
|
path.write_text(generated, encoding="utf-8")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|