#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Fetch game .spc sets for rung 3 into reference/dsp/spc/games/, per run. Game .spc files are never committed (docs/dsp/handoff-brief.md, decision 5). This fetches Zophar's per-game "(EMU)" zips, extracts only the .spc members into reference/dsp/spc/games// and records every file's SHA-256 in reference/dsp/spc/games/pins.json, which stays local with the files. python -I scripts/dsp_fetch_games.py [SLUG ...] ``GAMES`` names each set's sound driver. The attribution is from general knowledge of the games' publishers and programmers, a labelling for coverage, not a claim any test checks: replay needs no knowledge of the driver. """ from __future__ import annotations import hashlib import json import re import subprocess import sys import tempfile import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] GAMES_DIR = ROOT / "reference" / "dsp" / "spc" / "games" USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) Gecko/20100101 Firefox/128.0" PAGE = "https://www.zophar.net/music/nintendo-snes-spc/{slug}" #: (Zophar slug, sound driver family) GAMES = [ ("super-mario-world", "Nintendo N-SPC"), ("legend-of-zelda-the-a-link-to-the-past", "Nintendo N-SPC"), ("super-metroid", "Nintendo N-SPC"), ("final-fantasy-vi", "Square (Akao)"), ("chrono-trigger", "Square (Akao)"), ("secret-of-mana", "Square (Seiken Densetsu 2)"), ("super-castlevania-iv", "Konami"), ("contra-iii-the-alien-wars", "Konami"), ("mega-man-x", "Capcom"), ("super-ghouls-n-ghosts", "Capcom"), ("donkey-kong-country", "Rare"), ("donkey-kong-country-2-diddys-kong-quest", "Rare"), ("super-turrican", "Factor 5"), ("indiana-jones-greatest-adventures", "Factor 5"), ("actraiser", "Quintet/Enix"), ("tales-of-phantasia", "Namco (Wolf Team)"), ("super-bomberman", "Hudson"), ("kirby-super-star", "HAL"), ("wild-guns", "Natsume"), ("earthbound", "Nintendo N-SPC (Mother 2)"), ] def curl(url: str, target: Path) -> None: subprocess.run( ["curl", "-sSfL", "--max-time", "300", "-A", USER_AGENT, "-o", str(target), url], check=True, capture_output=True, ) def fetch(slug: str, driver: str, pins: dict) -> str: directory = GAMES_DIR / slug with tempfile.TemporaryDirectory() as work: page = Path(work) / "page.html" curl(PAGE.format(slug=slug), page) found = re.findall(r'href="(https://[^"]*%28EMU%29[^"]*\.zip)"', page.read_text("latin-1")) if not found: return "no (EMU) zip on the page" archive = Path(work) / "set.zip" curl(found[0], archive) directory.mkdir(parents=True, exist_ok=True) count = 0 with zipfile.ZipFile(archive) as members: for member in members.infolist(): name = Path(member.filename).name if not name.lower().endswith(".spc") or member.file_size > 0x20000: continue data = members.read(member) (directory / name).write_bytes(data) pins.setdefault(slug, {"driver": driver, "url": found[0], "files": {}}) pins[slug]["files"][name] = hashlib.sha256(data).hexdigest() count += 1 return f"{count} files" def main() -> int: wanted = set(sys.argv[1:]) GAMES_DIR.mkdir(parents=True, exist_ok=True) pins_path = GAMES_DIR / "pins.json" pins = json.loads(pins_path.read_text()) if pins_path.exists() else {} for slug, driver in GAMES: if wanted and slug not in wanted: continue try: result = fetch(slug, driver, pins) except (subprocess.CalledProcessError, zipfile.BadZipFile) as error: result = f"FAILED: {error}" print(f"{slug}: {result}", flush=True) pins_path.write_text(json.dumps(pins, indent=1) + "\n") return 0 if __name__ == "__main__": sys.exit(main())