#!/usr/bin/env python3 """Classify native RS field scripts and movements by their first source macro.""" import argparse import re from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def generate(pret, out): event = (pret / "include/macros/event.inc").read_text() movement = (pret / "include/macros/movement.inc").read_text() scripts = set(re.findall(r"^\s*\.macro\s+(\w+)", event, re.M)) moves = set(re.findall(r"^\s*create_movement_action\s+(\w+)", movement, re.M)) moves.update(re.findall(r"^\s*\.macro\s+(\w+)", movement, re.M)) classified = {"scripts": set(), "movements": set(), "data": set()} paths = set() def include(path): if path in paths: return paths.add(path) for rel in re.findall(r'^\s*\.include\s+"(data/[^\"]+)"', path.read_text(), re.M): include(pret / rel) include(pret / "data/event_scripts.s") for path in sorted(paths): pending = [] for raw in path.read_text(errors="replace").splitlines(): line = raw.split("@", 1)[0].strip() if not line or line.startswith("#"): continue label = re.fullmatch(r"(\w+)::?", line) if label: pending.append(label[1]) continue token = line.split()[0] if token in (".align", ".balign"): continue kind = ("data" if token in ("brailleformat", ".byte", ".2byte", ".4byte", ".string", ".incbin") else "scripts" if token in scripts else "movements" if token in moves else None) if kind: classified[kind].update(pending) pending = [] lines = ["-- Generated by tools/gen_rs_script_catalog.py from pokeruby source macros.", "return {"] for kind, names in classified.items(): lines.append(" " + kind + " = {") lines.extend(' "' + name + '",' for name in sorted(names)) lines.append(" },") lines.append("}") out.parent.mkdir(parents=True, exist_ok=True) out.write_text("\n".join(lines) + "\n") print(f"Classified {len(classified['scripts'])} script labels and {len(classified['movements'])} movement labels") if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--pret", type=Path, default=ROOT.parent / "pokeruby") parser.add_argument("--out", type=Path, default=ROOT / "src/import/gba/versions_scripts_rs.lua") args = parser.parse_args() generate(args.pret, args.out)