From 6e9c06850fa180bdf1d084b1232eba3a76dd013b Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 24 Nov 2026 05:59:41 +0100 Subject: [PATCH] refactor: split the arcade parent importer Set readers for DATs, FBNeo's driver list and flycast's table move to arcade_sets, the old MAME driver code reader to arcade_drivers. --- scripts/scraper/arcade_drivers.py | 443 ++++++++++++++ scripts/scraper/arcade_parent_importer.py | 670 +--------------------- scripts/scraper/arcade_sets.py | 234 ++++++++ tests/test_arcade_parents.py | 17 +- 4 files changed, 699 insertions(+), 665 deletions(-) create mode 100644 scripts/scraper/arcade_drivers.py create mode 100644 scripts/scraper/arcade_sets.py diff --git a/scripts/scraper/arcade_drivers.py b/scripts/scraper/arcade_drivers.py new file mode 100644 index 000000000..76d5721c2 --- /dev/null +++ b/scripts/scraper/arcade_drivers.py @@ -0,0 +1,443 @@ +"""Game sets read from the driver code of the MAME versions whose DAT the code moved past.""" + +from __future__ import annotations + +import ast +import functools +import hashlib +import re +from dataclasses import dataclass +from pathlib import Path + +from .arcade_sets import SetEntry +from .base_scraper import BlobMismatchError, download +from .clex import DriverSourceError, blank_comments, call_args, call_span +from .dat_fetch import api_json +from core.hashing import git_blob_sha + + +# --- MAME 0.37 and 0.78 driver source (mame2000, mame2003, mame2003-plus) ---- + +# driver.h at each pin: NAME is argument 1 and PARENT argument 2 in every +# form, and a BIOS set carries NOT_A_DRIVER in its flags. +_GAME_MACRO = re.compile(r"\b(GAME|GAMEX|GAMEC|GAMECX|GAMEB|GAMEBX)\s*\(") +_DRIVER_ENTRY = re.compile(r"\bDRIVER\s*\(\s*(\w+)\s*\)") +_ROM_START = re.compile(r"\bROM_START\s*\(\s*(\w+)\s*\)(.*?)\bROM_END\b", re.S) +_ROM_LOAD = re.compile(r"\bROMX?_LOAD\w*\s*\(") +_DISK_IMAGE = re.compile(r"\bDISK_IMAGE\w*\s*\(") +_ROM_START_HEAD = re.compile(r"\bROM_START\s*\(") +_DIRECTIVE = re.compile(r"^[ \t]*#[ \t]*(\w+)[ \t]*(.*)$") +_MACRO_HEAD = re.compile(r"(\w+)(\([^)]*\))?[ \t]*(.*)") +_CONDITION_TOKEN = re.compile( + r"\s*(?:(0[xX][0-9a-fA-F]+|\d+)[uUlL]*|(defined)|([A-Za-z_]\w*)" + r"|(&&|\|\||==|!=|<=|>=|<<|>>|[!<>()+\-*/%~&|^]))" +) +_IDENTIFIER = re.compile(r"\b[A-Za-z_]\w*\b") +_INCLUDE = re.compile(r'"([^"]+)"') + + +def _condition_value(expr: str, defines: dict[str, str], depth: int = 0) -> int: + """An ``#if`` expression the way the C preprocessor reads it: a name not + defined is 0, a defined name is its value.""" + python: list[str] = [] + position = 0 + expr = expr.strip() + while position < len(expr): + match = _CONDITION_TOKEN.match(expr, position) + if not match: + raise DriverSourceError(f"#if {expr}: cannot read") + number, defined, name, operator = match.groups() + position = match.end() + if number: + python.append(str(int(number, 0))) + elif defined: + target = re.match(r"\s*\(?\s*(\w+)\s*\)?", expr[position:]) + if not target: + raise DriverSourceError(f"#if {expr}: defined without a name") + python.append("1" if target.group(1) in defines else "0") + position += target.end() + elif name: + value = defines.get(name, "0").strip() or "1" + if depth > 8: + raise DriverSourceError(f"#if {expr}: {name} does not resolve") + python.append(str(_condition_value(value, defines, depth + 1))) + else: + python.append( + {"&&": " and ", "||": " or ", "!": " not ", "/": "//"}.get(operator, operator) + ) + try: + return int(_arithmetic(ast.parse("".join(python).strip(), mode="eval").body)) + except (SyntaxError, ZeroDivisionError, TypeError) as exc: + raise DriverSourceError(f"#if {expr}: {exc}") from exc + + +_BINARY = { + ast.Add: lambda a, b: a + b, ast.Sub: lambda a, b: a - b, + ast.Mult: lambda a, b: a * b, ast.FloorDiv: lambda a, b: a // b, + ast.Mod: lambda a, b: a % b, ast.LShift: lambda a, b: a << b, + ast.RShift: lambda a, b: a >> b, ast.BitAnd: lambda a, b: a & b, + ast.BitOr: lambda a, b: a | b, ast.BitXor: lambda a, b: a ^ b, +} +_COMPARE = { + ast.Eq: lambda a, b: a == b, ast.NotEq: lambda a, b: a != b, + ast.Lt: lambda a, b: a < b, ast.LtE: lambda a, b: a <= b, + ast.Gt: lambda a, b: a > b, ast.GtE: lambda a, b: a >= b, +} + + +def _arithmetic(node: ast.AST) -> int: + """Integer arithmetic of an ``#if``, nothing else evaluated.""" + if isinstance(node, ast.Constant) and isinstance(node.value, int): + return node.value + if isinstance(node, ast.UnaryOp): + value = _arithmetic(node.operand) + if isinstance(node.op, ast.Not): + return int(not value) + if isinstance(node.op, ast.USub): + return -value + if isinstance(node.op, ast.UAdd): + return value + if isinstance(node.op, ast.Invert): + return ~value + if isinstance(node, ast.BoolOp): + values = [_arithmetic(v) for v in node.values] + return int(all(values) if isinstance(node.op, ast.And) else any(values)) + if isinstance(node, ast.BinOp) and type(node.op) in _BINARY: + return _BINARY[type(node.op)](_arithmetic(node.left), _arithmetic(node.right)) + if isinstance(node, ast.Compare): + left = _arithmetic(node.left) + for op, right_node in zip(node.ops, node.comparators): + right = _arithmetic(right_node) + if type(op) not in _COMPARE or not _COMPARE[type(op)](left, right): + return 0 + left = right + return 1 + raise TypeError(f"{type(node).__name__} in a condition") + + +class _Preprocessor: + """Conditional compilation over src/, enough to tell which GAME and + ROM_START a build compiles. Headers are read for their definitions.""" + + def __init__(self, headers: dict[str, str]) -> None: + self.headers = headers + self._by_basename: dict[str, list[str]] = {} + for path in headers: + self._by_basename.setdefault(path.rsplit("/", 1)[-1], []).append(path) + + def _resolve(self, name: str, including: str) -> str | None: + folder = including.rsplit("/", 1)[0] + for candidate in (f"{folder}/{name}", f"src/{name}", f"src/includes/{name}"): + if candidate in self.headers: + return candidate + found = self._by_basename.get(name.rsplit("/", 1)[-1], []) + return found[0] if len(found) == 1 else None + + def run( + self, path: str, text: str, defines: dict[str, str], + local: dict[str, tuple[list[str] | None, str]] | None, seen: set[str], + ) -> str: + text = blank_comments(text, keep_lines=False).replace("\\\r\n", " ").replace("\\\n", " ") + out: list[str] = [] + # Each level: (enclosing level active, a branch already taken, this branch active). + stack: list[tuple[bool, bool, bool]] = [] + active = True + for line in text.splitlines(): + directive = _DIRECTIVE.match(line) + if not directive: + if active: + out.append(line) + continue + word, rest = directive.groups() + if word in _CONDITIONALS: + active = _branch(path, word, rest, defines, stack, active) + elif active: + self._define(path, word, rest, defines, local, seen) + if stack: + raise DriverSourceError(f"{path}: #if without #endif") + return "\n".join(out) + + def _define( + self, path: str, word: str, rest: str, defines: dict[str, str], + local: dict[str, tuple[list[str] | None, str]] | None, seen: set[str], + ) -> None: + """#define, #undef and #include in an active region.""" + if word == "define": + head = _MACRO_HEAD.match(rest) + if head: + defines[head.group(1)] = head.group(3) + if local is not None: + params = head.group(2) + local[head.group(1)] = ( + [a.strip() for a in params[1:-1].split(",")] if params else None, + head.group(3), + ) + elif word == "undef": + defines.pop(rest.strip(), None) + elif word == "include": + target = _INCLUDE.match(rest) + header = self._resolve(target.group(1), path) if target else None + if header and header not in seen: + seen.add(header) + self.run(header, self.headers[header], defines, None, seen) + + +_CONDITIONALS = frozenset({"if", "ifdef", "ifndef", "elif", "else", "endif"}) + + +def _branch( + path: str, word: str, rest: str, defines: dict[str, str], + stack: list[tuple[bool, bool, bool]], active: bool, +) -> bool: + """Apply one conditional directive to *stack*. Returns the new active state.""" + if word in ("if", "ifdef", "ifndef"): + if not active: + stack.append((False, True, False)) + return active + if word == "if": + value = bool(_condition_value(rest, defines)) + else: + value = (rest.split()[0] in defines) == (word == "ifdef") + stack.append((True, value, value)) + return value + if word in ("elif", "else"): + if not stack: + raise DriverSourceError(f"{path}: #{word} without #if") + outer, taken, _ = stack[-1] + value = ( + outer and not taken + and (word == "else" or bool(_condition_value(rest, defines))) + ) + stack[-1] = (outer, taken or value, value) + return value + if not stack: + raise DriverSourceError(f"{path}: #endif without #if") + stack.pop() + return stack[-1][2] if stack else True + + +def _dumped(args: list[str]) -> bool: + """A ROM_LOAD whose ROM was dumped: 0.78 writes ``NO_DUMP`` in the hash + argument, 0.37 a zero CRC.""" + if any("NO_DUMP" in a for a in args): + return False + crc = args[3] if len(args) > 3 else "" + return not re.fullmatch(r"0[xX]0+|0", crc.strip()) + + +def _argument(values: dict[str, str], match: re.Match[str]) -> str: + return values.get(match.group(0), match.group(0)) + + +def _expand_declarations(text: str, local: dict[str, tuple[list[str] | None, str]]) -> str: + """Calls of a file's own function-like macros that write a ROM_START or a + GAME line, expanded (pgm.c builds every DoDonPachi II set through + ``DDP2_SET(name, program, asic)``).""" + writers = { + name: (params, body) + for name, (params, body) in local.items() + if params is not None and (_ROM_START_HEAD.search(body) or _GAME_MACRO.search(body)) + } + if not writers: + return text + call = re.compile(r"\b(" + "|".join(map(re.escape, writers)) + r")\s*\(") + out: list[str] = [] + position = 0 + for match in call.finditer(text): + if match.start() < position: + continue + params, body = writers[match.group(1)] + args, end = call_span(text, match.end()) + if len(args) != len(params): + raise DriverSourceError(f"{match.group(1)} called with {len(args)} arguments") + values = dict(zip(params, args)) + out.append(text[position : match.start()]) + out.append(_IDENTIFIER.sub(functools.partial(_argument, values), body)) + position = end + out.append(text[position:]) + return "".join(out) + + +def compiled_drivers(driver_c: str, preprocessor: _Preprocessor) -> list[str]: + """The DRIVER() entries driver.c compiles into the driver list.""" + # driver.c is read twice by the build, the second time with + # DRIVER_RECURSIVE defined: that pass is the list. + text = preprocessor.run( + "src/driver.c", driver_c, {"DRIVER_RECURSIVE": "", "DRIVER": ""}, None, set() + ) + return [n for n in _DRIVER_ENTRY.findall(text) if n != "NAME"] + + +def _holds_roms( + body: str, local: dict[str, tuple[list[str] | None, str]], known: dict[str, bool], + seen: frozenset[str] = frozenset(), +) -> bool: + for match in _ROM_LOAD.finditer(body): + if _dumped(call_args(body, match.end())): + return True + # A ROM list shared through a macro of the same file (a BIOS + # block every game of the board pulls in). + for word in set(_IDENTIFIER.findall(body)) - seen: + if word in local: + if word not in known: + known[word] = _holds_roms(local[word][1], local, known, seen | {word}) + if known[word]: + return True + return False + + +def _read_driver( + path: str, source: str, preprocessor: _Preprocessor, + declared: dict[str, tuple[str | None, bool]], roms: dict[str, tuple[bool, bool]], +) -> None: + """The ROM_START blocks and GAME declarations of one driver file.""" + local: dict[str, tuple[list[str] | None, str]] = {} + text = _expand_declarations(preprocessor.run(path, source, {}, local, set()), local) + known: dict[str, bool] = {} + for match in _ROM_START.finditer(text): + body = match.group(2) + roms.setdefault( + match.group(1), (_holds_roms(body, local, known), bool(_DISK_IMAGE.search(body))) + ) + for match in _GAME_MACRO.finditer(text): + args = call_args(text, match.end()) + if len(args) < 9: + raise DriverSourceError(f"{path}: {match.group(1)} with {len(args)} arguments") + name, parent = args[1], args[2] + if name in declared: + raise DriverSourceError(f"{path}: {name} declared twice") + declared[name] = ( + None if parent == "0" else parent, + any("NOT_A_DRIVER" in a for a in args[3:]), + ) + + +def driver_source_sets( + driver_c: str, drivers: dict[str, str], headers: dict[str, str] +) -> dict[str, SetEntry]: + """Every compiled set and every set on its parent chain, from the source. + + *drivers* maps each path under src/drivers/ to its text and *headers* + every header under src/. A set is declared by a GAME-family macro whose + ROMs are ``ROM_START(name)``, and a set flagged ``NOT_A_DRIVER`` is a BIOS + set, the flag the 0.37 and 0.78 info.c use to tell a resource from a game. + """ + preprocessor = _Preprocessor(headers) + declared: dict[str, tuple[str | None, bool]] = {} + roms: dict[str, tuple[bool, bool]] = {} + for path, source in sorted(drivers.items()): + _read_driver(path, source, preprocessor, declared, roms) + compiled = compiled_drivers(driver_c, preprocessor) + undeclared = [n for n in compiled if n not in declared] + if undeclared: + raise DriverSourceError(f"driver.c lists undeclared sets: {', '.join(undeclared[:10])}") + sets: dict[str, SetEntry] = {} + pending = list(compiled) + while pending: + name = pending.pop() + if name in sets or name not in declared: + continue + parent, bios = declared[name] + has_roms, disks = roms.get(name, (False, False)) + sets[name] = SetEntry( + name=name, romof=parent, isbios=bios, runnable=not bios, + has_roms=has_roms, disks=disks, + ) + if parent: + pending.append(parent) + return sets + + +def git_tree_sha(entries: dict[str, tuple[str, str]]) -> str: + """The sha git gives a directory, from ``{path: (mode, blob sha)}`` of + every file under it, paths relative to it.""" + children: dict[str, dict[str, tuple[str, str]]] = {} + rows: list[tuple[str, str, str]] = [] + for path, (mode, sha) in entries.items(): + head, sep, tail = path.partition("/") + if sep: + children.setdefault(head, {})[tail] = (mode, sha) + else: + rows.append((head, mode, sha)) + rows += [(name, "40000", git_tree_sha(sub)) for name, sub in children.items()] + # git orders a tree by name, a directory compared as if it ended in "/". + rows.sort(key=lambda row: row[0].encode() + (b"/" if row[1] == "40000" else b"")) + body = b"".join( + mode.encode() + b" " + name.encode() + b"\0" + bytes.fromhex(sha) + for name, mode, sha in rows + ) + return hashlib.sha1(f"tree {len(body)}\0".encode() + body).hexdigest() + + +@dataclass +class DriverSource: + """What the reader needs from src/ at one commit, and the tree sha of + src/ computed from every file of it.""" + + driver_c: str + drivers: dict[str, str] + headers: dict[str, str] + tree: str + + +def read_driver_source(tarball: Path) -> DriverSource: + """src/ of a GitHub tarball: the texts the reader needs, and the git + tree sha of the whole directory.""" + import tarfile + + entries: dict[str, tuple[str, str]] = {} + texts: dict[str, str] = {} + with tarfile.open(tarball, "r:gz") as archive: + for member in archive: + parts = member.name.split("/", 1) + if len(parts) < 2 or not parts[1].startswith("src/"): + continue + path = parts[1] + if member.issym(): + entries[path[4:]] = ("120000", git_blob_sha(member.linkname.encode())) + elif member.isfile(): + handle = archive.extractfile(member) + data = handle.read() if handle else b"" + mode = "100755" if member.mode & 0o111 else "100644" + entries[path[4:]] = (mode, git_blob_sha(data)) + if path == "src/driver.c" or path.endswith(".h") or ( + path.startswith("src/drivers/") and path.endswith(".c") + ): + texts[path] = data.decode("latin-1") + if "src/driver.c" not in texts: + raise FileNotFoundError(f"{tarball} has no src/driver.c") + return DriverSource( + driver_c=texts["src/driver.c"], + drivers={p: t for p, t in texts.items() if p.startswith("src/drivers/") and p.endswith(".c")}, + headers={p: t for p, t in texts.items() if p.endswith(".h")}, + tree=git_tree_sha(entries), + ) + + +def fetch_driver_source( + slug: str, commit: str, cache_dir: Path, offline: bool = False +) -> DriverSource: + """src/ at a commit, its tree sha checked against the repository's own + when online.""" + tarball = cache_dir / "src" / slug / f"{commit}.tar.gz" + if offline: + if not tarball.is_file(): + raise FileNotFoundError(f"{tarball} is not cached") + else: + download(f"https://codeload.github.com/{slug}/tar.gz/{commit}", tarball, + skip_existing=True) + source = read_driver_source(tarball) + if not offline: + listing = api_json(f"https://api.github.com/repos/{slug}/git/trees/{commit}") + listed = next( + ( + item.get("sha") + for item in (listing.get("tree", []) if isinstance(listing, dict) else []) + if item.get("path") == "src" + ), + None, + ) + if listed != source.tree: + tarball.unlink() + raise BlobMismatchError(f"{slug}@{commit[:8]}:src is tree {listed}, not {source.tree}") + return source diff --git a/scripts/scraper/arcade_parent_importer.py b/scripts/scraper/arcade_parent_importer.py index ed4d963bc..c2e7a4543 100644 --- a/scripts/scraper/arcade_parent_importer.py +++ b/scripts/scraper/arcade_parent_importer.py @@ -36,17 +36,11 @@ on a card that none of the lists holds is one the core does not know. from __future__ import annotations import argparse -import ast -import functools -import hashlib import json -import re import sys import urllib.parse import zipfile -from collections import Counter from collections.abc import Iterator -from dataclasses import dataclass, field from pathlib import Path from typing import IO @@ -56,12 +50,20 @@ from model.profiles import load_emulator_profiles from core.cli import add_repo_inputs, run_cli from core.hashing import compute_hashes, git_blob_sha from core.upstream import parse_repo -from .base_scraper import BlobMismatchError, download, fetch_verified_blob -from .fbneo_parser import zip_name -from .clex import DriverSourceError, blank_comments, call_args, call_span -from .listxml import Opener, attrs, iter_elements, open_listxml, rom_members +from .base_scraper import fetch_verified_blob +from .listxml import Opener, open_listxml from model.mamerelease import mame_tag from .dat_fetch import MAME_LISTXML_URL, api_json, cached_pack, fetch_pack +from .arcade_drivers import driver_source_sets, fetch_driver_source +from .arcade_sets import ( + FBNEO_DRIVERLIST, + SetEntry, + fbneo_drivers, + fbneo_sets, + flycast_index, + is_bios, + iter_sets, +) SCHEMA_VERSION = 1 DEFAULT_OUTPUT = Path("games") / "arcade" @@ -110,49 +112,6 @@ FLOORS: dict[str, int] = { } -@dataclass -class SetEntry: - """What the index needs from one machine or game element.""" - - name: str - romof: str | None = None - isbios: bool = False - isdevice: bool = False - runnable: bool = True - has_roms: bool = False - disks: bool = False - devices: list[str] = field(default_factory=list) - source: str | None = None - - -_DEVICE_REF = re.compile(rb']*\bname="([^"]+)"') -_DISK = re.compile(rb"]*)>") - - -def iter_sets(stream: IO[bytes]) -> Iterator[SetEntry]: - """Every machine or game element of a DAT or -listxml, streamed.""" - for element in iter_elements(stream, ("machine", "game")): - found, body = element.attrs, element.body - disks = [attrs(d) for d in _DISK.findall(body)] - yield SetEntry( - name=found.get("name", ""), - romof=found.get("romof") or None, - isbios=found.get("isbios") == "yes", - isdevice=found.get("isdevice") == "yes", - runnable=found.get("runnable", "yes") != "no", - has_roms=bool(rom_members(body)), - disks=any(d.get("status") != "nodump" for d in disks), - devices=[d.decode() for d in _DEVICE_REF.findall(body)], - source=found.get("sourcefile"), - ) - - -def is_bios(entry: SetEntry, marker: str) -> bool: - if entry.isbios: - return True - return marker == "runnable" and not entry.runnable and not entry.isdevice - - def build_index( sets: dict[str, SetEntry], marker: str, archives: dict[str, str] | None = None ) -> dict: @@ -223,611 +182,6 @@ def build_index( } -# --- FBNeo: one namespace per DAT --------------------------------------------- - -# Generated at build time from every driver and committed: the set name and the -# source file of each driver. -FBNEO_DRIVERLIST = "src/dep/generated/driverlist.h" -_FBNEO_DRIVER = re.compile(r'\{\s*"([^"]+)",\s*"([^"]+\.cpp)"\s*\}') - - -def fbneo_drivers(listing: str) -> dict[str, set[str]]: - """Driver names per source file, from driverlist.h.""" - names: dict[str, set[str]] = {} - for name, path in _FBNEO_DRIVER.findall(listing): - names.setdefault(path, set()).add(name) - if not names: - raise ValueError(f"{FBNEO_DRIVERLIST} lists no driver") - return names - - -def _fbneo_candidates(entry: SetEntry, names: dict[str, set[str]]) -> list[str]: - """The drivers a DAT set can be. A console DAT writes the archive name, - which the driver's GetZipName() takes by cutting the system prefix: - ``1942`` in the MSX DAT is the driver ``msx_1942``.""" - drivers = names.get(entry.source or "", set()) - if entry.name in drivers: - return [entry.name] - return sorted(n for n in drivers if "_" in n and zip_name(n) == entry.name) - - -def _fbneo_driver_names(entries: list[SetEntry], names: dict[str, set[str]]) -> dict[str, str]: - """Driver name per set of one DAT. Where a source file holds two systems - (d_nes.cpp builds the NES and the FDS drivers), the prefix the rest of - the DAT carries decides.""" - candidates = {e.name: _fbneo_candidates(e, names) for e in entries} - prefixes = Counter( - found[0].split("_", 1)[0] - for name, found in candidates.items() - if len(found) == 1 and found[0] != name - ) - driver: dict[str, str] = {} - for entry in entries: - found = candidates[entry.name] - if len(found) > 1 and prefixes: - dominant = prefixes.most_common(1)[0][0] - found = [n for n in found if n.split("_", 1)[0] == dominant] - if len(found) != 1: - raise ValueError(f"{entry.source}: {entry.name} matches drivers {found}") - driver[entry.name] = found[0] - return driver - - -def fbneo_sets( - openers: list[Opener], names: dict[str, set[str]] -) -> tuple[dict[str, SetEntry], dict[str, str]]: - """Every set of FBNeo's DATs keyed by its driver, and the archive of each - set whose driver name is not its archive name. - - Each DAT is its own namespace: the Arcade, MSX, NES and ZX Spectrum DATs - all carry a ``1942``, and a ``romof`` inside a DAT names a set of that DAT. - Merged by archive name, the MSX ``1942`` that needs msx.zip was the - arcade one, which needs nothing. The Arcade and Neo Geo DATs both carry - the Neo Geo drivers: a driver read twice keeps its first reading. - """ - sets: dict[str, SetEntry] = {} - archives: dict[str, str] = {} - for opener in openers: - with opener() as stream: - entries = [e for e in iter_sets(stream) if e.name] - driver = _fbneo_driver_names(entries, names) - for entry in entries: - name = driver[entry.name] - if name in sets: - continue - if name != entry.name: - archives[name] = entry.name - if entry.romof: - entry.romof = driver.get(entry.romof, entry.romof) - entry.name = name - sets[name] = entry - return sets, archives - - -# --- C table reader (flycast naomi_roms.cpp) --------------------------------- - -_TOKEN = re.compile(r'"(?:\\.|[^"\\])*"|[{},]|[^\s{},"]+') - - -def _initializer(text: str, array: str) -> list: - """The brace initializer of ``const [] = {...}`` as nested - lists of tokens, each element split on its top-level commas.""" - match = re.search(rf"\b{re.escape(array)}\s*\[\s*\]\s*=\s*\{{", text) - if not match: - raise ValueError(f"no initializer for {array}[]") - tokens = _TOKEN.findall(text[match.end() - 1 :]) - stack: list[list] = [] - item: list = [] - for token in tokens: - if token == "{": - stack.append(item) - item = [] - stack.append([]) - elif token == "}": - elements = stack.pop() - if item: - elements.append(item if len(item) > 1 else item[0]) - item = stack.pop() - item.append(elements) - if not stack: - return item[0] - elif token == ",": - elements = stack[-1] - if item: - elements.append(item if len(item) > 1 else item[0]) - item = [] - else: - item.append(token) - raise ValueError(f"unterminated initializer for {array}[]") - - -def _string(token: object) -> str | None: - if isinstance(token, str) and token.startswith('"'): - return token[1:-1] - return None - - -def flycast_index(source: str) -> dict: - """BIOS archive per NAOMI/Atomiswave game from naomi_roms.cpp.""" - text = blank_comments(source) - archive_of: dict[str, str] = {} - for element in _initializer(text, "BIOS"): - if not isinstance(element, list) or not element: - continue - name = _string(element[0]) - if name is None: - continue - # BIOS_t: name, blobs[], filename (naomi_roms.h:53-66). - filename = _string(element[2]) if len(element) > 2 else None - archive_of[name] = filename or name - bios: dict[str, set[str]] = {} - chd: list[str] = [] - unknown: dict[str, str] = {} - games: set[str] = set() - for element in _initializer(text, "Games"): - if not isinstance(element, list) or not element: - continue - name = _string(element[0]) - if name is None: - continue - # shors2k2 is declared twice at e88b0ced: a name counts once. - games.add(name) - # Game: name, parent_name, description, size, key, bios, cart_type, - # rotation, blobs[], gdrom_name, ... (naomi_roms.h:71-94). - named = _string(element[5]) if len(element) > 5 else None - if named is not None and named not in archive_of: - unknown[name] = named - continue - bios.setdefault(archive_of[named or "naomi"], set()).add(name) - if len(element) > 9 and _string(element[9]): - chd.append(name) - return { - "bios": {k: sorted(v) for k, v in sorted(bios.items())}, - "devices": {}, - "chd": sorted(chd), - "missing_parents": dict(sorted(unknown.items())), - "no_bios": [], - "bios_sets": sorted(set(archive_of.values())), - "counts": { - "sets": len(games) + len(archive_of), - "games": len(games), - "games_with_bios": len({g for v in bios.values() for g in v}), - "bios_sets": len(archive_of), - "bios_roots_used": len(bios), - "games_with_devices": 0, - "chd_games": len(chd), - "missing_parents": len(unknown), - }, - } - - -# --- MAME 0.37 and 0.78 driver source (mame2000, mame2003, mame2003-plus) ---- - -# driver.h at each pin: NAME is argument 1 and PARENT argument 2 in every -# form, and a BIOS set carries NOT_A_DRIVER in its flags. -_GAME_MACRO = re.compile(r"\b(GAME|GAMEX|GAMEC|GAMECX|GAMEB|GAMEBX)\s*\(") -_DRIVER_ENTRY = re.compile(r"\bDRIVER\s*\(\s*(\w+)\s*\)") -_ROM_START = re.compile(r"\bROM_START\s*\(\s*(\w+)\s*\)(.*?)\bROM_END\b", re.S) -_ROM_LOAD = re.compile(r"\bROMX?_LOAD\w*\s*\(") -_DISK_IMAGE = re.compile(r"\bDISK_IMAGE\w*\s*\(") -_ROM_START_HEAD = re.compile(r"\bROM_START\s*\(") -_DIRECTIVE = re.compile(r"^[ \t]*#[ \t]*(\w+)[ \t]*(.*)$") -_MACRO_HEAD = re.compile(r"(\w+)(\([^)]*\))?[ \t]*(.*)") -_CONDITION_TOKEN = re.compile( - r"\s*(?:(0[xX][0-9a-fA-F]+|\d+)[uUlL]*|(defined)|([A-Za-z_]\w*)" - r"|(&&|\|\||==|!=|<=|>=|<<|>>|[!<>()+\-*/%~&|^]))" -) -_IDENTIFIER = re.compile(r"\b[A-Za-z_]\w*\b") -_INCLUDE = re.compile(r'"([^"]+)"') - - -def _condition_value(expr: str, defines: dict[str, str], depth: int = 0) -> int: - """An ``#if`` expression the way the C preprocessor reads it: a name not - defined is 0, a defined name is its value.""" - python: list[str] = [] - position = 0 - expr = expr.strip() - while position < len(expr): - match = _CONDITION_TOKEN.match(expr, position) - if not match: - raise DriverSourceError(f"#if {expr}: cannot read") - number, defined, name, operator = match.groups() - position = match.end() - if number: - python.append(str(int(number, 0))) - elif defined: - target = re.match(r"\s*\(?\s*(\w+)\s*\)?", expr[position:]) - if not target: - raise DriverSourceError(f"#if {expr}: defined without a name") - python.append("1" if target.group(1) in defines else "0") - position += target.end() - elif name: - value = defines.get(name, "0").strip() or "1" - if depth > 8: - raise DriverSourceError(f"#if {expr}: {name} does not resolve") - python.append(str(_condition_value(value, defines, depth + 1))) - else: - python.append( - {"&&": " and ", "||": " or ", "!": " not ", "/": "//"}.get(operator, operator) - ) - try: - return int(_arithmetic(ast.parse("".join(python).strip(), mode="eval").body)) - except (SyntaxError, ZeroDivisionError, TypeError) as exc: - raise DriverSourceError(f"#if {expr}: {exc}") from exc - - -_BINARY = { - ast.Add: lambda a, b: a + b, ast.Sub: lambda a, b: a - b, - ast.Mult: lambda a, b: a * b, ast.FloorDiv: lambda a, b: a // b, - ast.Mod: lambda a, b: a % b, ast.LShift: lambda a, b: a << b, - ast.RShift: lambda a, b: a >> b, ast.BitAnd: lambda a, b: a & b, - ast.BitOr: lambda a, b: a | b, ast.BitXor: lambda a, b: a ^ b, -} -_COMPARE = { - ast.Eq: lambda a, b: a == b, ast.NotEq: lambda a, b: a != b, - ast.Lt: lambda a, b: a < b, ast.LtE: lambda a, b: a <= b, - ast.Gt: lambda a, b: a > b, ast.GtE: lambda a, b: a >= b, -} - - -def _arithmetic(node: ast.AST) -> int: - """Integer arithmetic of an ``#if``, nothing else evaluated.""" - if isinstance(node, ast.Constant) and isinstance(node.value, int): - return node.value - if isinstance(node, ast.UnaryOp): - value = _arithmetic(node.operand) - if isinstance(node.op, ast.Not): - return int(not value) - if isinstance(node.op, ast.USub): - return -value - if isinstance(node.op, ast.UAdd): - return value - if isinstance(node.op, ast.Invert): - return ~value - if isinstance(node, ast.BoolOp): - values = [_arithmetic(v) for v in node.values] - return int(all(values) if isinstance(node.op, ast.And) else any(values)) - if isinstance(node, ast.BinOp) and type(node.op) in _BINARY: - return _BINARY[type(node.op)](_arithmetic(node.left), _arithmetic(node.right)) - if isinstance(node, ast.Compare): - left = _arithmetic(node.left) - for op, right_node in zip(node.ops, node.comparators): - right = _arithmetic(right_node) - if type(op) not in _COMPARE or not _COMPARE[type(op)](left, right): - return 0 - left = right - return 1 - raise TypeError(f"{type(node).__name__} in a condition") - - -class _Preprocessor: - """Conditional compilation over src/, enough to tell which GAME and - ROM_START a build compiles. Headers are read for their definitions.""" - - def __init__(self, headers: dict[str, str]) -> None: - self.headers = headers - self._by_basename: dict[str, list[str]] = {} - for path in headers: - self._by_basename.setdefault(path.rsplit("/", 1)[-1], []).append(path) - - def _resolve(self, name: str, including: str) -> str | None: - folder = including.rsplit("/", 1)[0] - for candidate in (f"{folder}/{name}", f"src/{name}", f"src/includes/{name}"): - if candidate in self.headers: - return candidate - found = self._by_basename.get(name.rsplit("/", 1)[-1], []) - return found[0] if len(found) == 1 else None - - def run( - self, path: str, text: str, defines: dict[str, str], - local: dict[str, tuple[list[str] | None, str]] | None, seen: set[str], - ) -> str: - text = blank_comments(text, keep_lines=False).replace("\\\r\n", " ").replace("\\\n", " ") - out: list[str] = [] - # Each level: (enclosing level active, a branch already taken, this branch active). - stack: list[tuple[bool, bool, bool]] = [] - active = True - for line in text.splitlines(): - directive = _DIRECTIVE.match(line) - if not directive: - if active: - out.append(line) - continue - word, rest = directive.groups() - if word in _CONDITIONALS: - active = _branch(path, word, rest, defines, stack, active) - elif active: - self._define(path, word, rest, defines, local, seen) - if stack: - raise DriverSourceError(f"{path}: #if without #endif") - return "\n".join(out) - - def _define( - self, path: str, word: str, rest: str, defines: dict[str, str], - local: dict[str, tuple[list[str] | None, str]] | None, seen: set[str], - ) -> None: - """#define, #undef and #include in an active region.""" - if word == "define": - head = _MACRO_HEAD.match(rest) - if head: - defines[head.group(1)] = head.group(3) - if local is not None: - params = head.group(2) - local[head.group(1)] = ( - [a.strip() for a in params[1:-1].split(",")] if params else None, - head.group(3), - ) - elif word == "undef": - defines.pop(rest.strip(), None) - elif word == "include": - target = _INCLUDE.match(rest) - header = self._resolve(target.group(1), path) if target else None - if header and header not in seen: - seen.add(header) - self.run(header, self.headers[header], defines, None, seen) - - -_CONDITIONALS = frozenset({"if", "ifdef", "ifndef", "elif", "else", "endif"}) - - -def _branch( - path: str, word: str, rest: str, defines: dict[str, str], - stack: list[tuple[bool, bool, bool]], active: bool, -) -> bool: - """Apply one conditional directive to *stack*. Returns the new active state.""" - if word in ("if", "ifdef", "ifndef"): - if not active: - stack.append((False, True, False)) - return active - if word == "if": - value = bool(_condition_value(rest, defines)) - else: - value = (rest.split()[0] in defines) == (word == "ifdef") - stack.append((True, value, value)) - return value - if word in ("elif", "else"): - if not stack: - raise DriverSourceError(f"{path}: #{word} without #if") - outer, taken, _ = stack[-1] - value = ( - outer and not taken - and (word == "else" or bool(_condition_value(rest, defines))) - ) - stack[-1] = (outer, taken or value, value) - return value - if not stack: - raise DriverSourceError(f"{path}: #endif without #if") - stack.pop() - return stack[-1][2] if stack else True - - -def _dumped(args: list[str]) -> bool: - """A ROM_LOAD whose ROM was dumped: 0.78 writes ``NO_DUMP`` in the hash - argument, 0.37 a zero CRC.""" - if any("NO_DUMP" in a for a in args): - return False - crc = args[3] if len(args) > 3 else "" - return not re.fullmatch(r"0[xX]0+|0", crc.strip()) - - -def _argument(values: dict[str, str], match: re.Match[str]) -> str: - return values.get(match.group(0), match.group(0)) - - -def _expand_declarations(text: str, local: dict[str, tuple[list[str] | None, str]]) -> str: - """Calls of a file's own function-like macros that write a ROM_START or a - GAME line, expanded (pgm.c builds every DoDonPachi II set through - ``DDP2_SET(name, program, asic)``).""" - writers = { - name: (params, body) - for name, (params, body) in local.items() - if params is not None and (_ROM_START_HEAD.search(body) or _GAME_MACRO.search(body)) - } - if not writers: - return text - call = re.compile(r"\b(" + "|".join(map(re.escape, writers)) + r")\s*\(") - out: list[str] = [] - position = 0 - for match in call.finditer(text): - if match.start() < position: - continue - params, body = writers[match.group(1)] - args, end = call_span(text, match.end()) - if len(args) != len(params): - raise DriverSourceError(f"{match.group(1)} called with {len(args)} arguments") - values = dict(zip(params, args)) - out.append(text[position : match.start()]) - out.append(_IDENTIFIER.sub(functools.partial(_argument, values), body)) - position = end - out.append(text[position:]) - return "".join(out) - - -def compiled_drivers(driver_c: str, preprocessor: _Preprocessor) -> list[str]: - """The DRIVER() entries driver.c compiles into the driver list.""" - # driver.c is read twice by the build, the second time with - # DRIVER_RECURSIVE defined: that pass is the list. - text = preprocessor.run( - "src/driver.c", driver_c, {"DRIVER_RECURSIVE": "", "DRIVER": ""}, None, set() - ) - return [n for n in _DRIVER_ENTRY.findall(text) if n != "NAME"] - - -def _holds_roms( - body: str, local: dict[str, tuple[list[str] | None, str]], known: dict[str, bool], - seen: frozenset[str] = frozenset(), -) -> bool: - for match in _ROM_LOAD.finditer(body): - if _dumped(call_args(body, match.end())): - return True - # A ROM list shared through a macro of the same file (a BIOS - # block every game of the board pulls in). - for word in set(_IDENTIFIER.findall(body)) - seen: - if word in local: - if word not in known: - known[word] = _holds_roms(local[word][1], local, known, seen | {word}) - if known[word]: - return True - return False - - -def _read_driver( - path: str, source: str, preprocessor: _Preprocessor, - declared: dict[str, tuple[str | None, bool]], roms: dict[str, tuple[bool, bool]], -) -> None: - """The ROM_START blocks and GAME declarations of one driver file.""" - local: dict[str, tuple[list[str] | None, str]] = {} - text = _expand_declarations(preprocessor.run(path, source, {}, local, set()), local) - known: dict[str, bool] = {} - for match in _ROM_START.finditer(text): - body = match.group(2) - roms.setdefault( - match.group(1), (_holds_roms(body, local, known), bool(_DISK_IMAGE.search(body))) - ) - for match in _GAME_MACRO.finditer(text): - args = call_args(text, match.end()) - if len(args) < 9: - raise DriverSourceError(f"{path}: {match.group(1)} with {len(args)} arguments") - name, parent = args[1], args[2] - if name in declared: - raise DriverSourceError(f"{path}: {name} declared twice") - declared[name] = ( - None if parent == "0" else parent, - any("NOT_A_DRIVER" in a for a in args[3:]), - ) - - -def driver_source_sets( - driver_c: str, drivers: dict[str, str], headers: dict[str, str] -) -> dict[str, SetEntry]: - """Every compiled set and every set on its parent chain, from the source. - - *drivers* maps each path under src/drivers/ to its text and *headers* - every header under src/. A set is declared by a GAME-family macro whose - ROMs are ``ROM_START(name)``, and a set flagged ``NOT_A_DRIVER`` is a BIOS - set, the flag the 0.37 and 0.78 info.c use to tell a resource from a game. - """ - preprocessor = _Preprocessor(headers) - declared: dict[str, tuple[str | None, bool]] = {} - roms: dict[str, tuple[bool, bool]] = {} - for path, source in sorted(drivers.items()): - _read_driver(path, source, preprocessor, declared, roms) - compiled = compiled_drivers(driver_c, preprocessor) - undeclared = [n for n in compiled if n not in declared] - if undeclared: - raise DriverSourceError(f"driver.c lists undeclared sets: {', '.join(undeclared[:10])}") - sets: dict[str, SetEntry] = {} - pending = list(compiled) - while pending: - name = pending.pop() - if name in sets or name not in declared: - continue - parent, bios = declared[name] - has_roms, disks = roms.get(name, (False, False)) - sets[name] = SetEntry( - name=name, romof=parent, isbios=bios, runnable=not bios, - has_roms=has_roms, disks=disks, - ) - if parent: - pending.append(parent) - return sets - - -def git_tree_sha(entries: dict[str, tuple[str, str]]) -> str: - """The sha git gives a directory, from ``{path: (mode, blob sha)}`` of - every file under it, paths relative to it.""" - children: dict[str, dict[str, tuple[str, str]]] = {} - rows: list[tuple[str, str, str]] = [] - for path, (mode, sha) in entries.items(): - head, sep, tail = path.partition("/") - if sep: - children.setdefault(head, {})[tail] = (mode, sha) - else: - rows.append((head, mode, sha)) - rows += [(name, "40000", git_tree_sha(sub)) for name, sub in children.items()] - # git orders a tree by name, a directory compared as if it ended in "/". - rows.sort(key=lambda row: row[0].encode() + (b"/" if row[1] == "40000" else b"")) - body = b"".join( - mode.encode() + b" " + name.encode() + b"\0" + bytes.fromhex(sha) - for name, mode, sha in rows - ) - return hashlib.sha1(f"tree {len(body)}\0".encode() + body).hexdigest() - - -@dataclass -class DriverSource: - """What the reader needs from src/ at one commit, and the tree sha of - src/ computed from every file of it.""" - - driver_c: str - drivers: dict[str, str] - headers: dict[str, str] - tree: str - - -def read_driver_source(tarball: Path) -> DriverSource: - """src/ of a GitHub tarball: the texts the reader needs, and the git - tree sha of the whole directory.""" - import tarfile - - entries: dict[str, tuple[str, str]] = {} - texts: dict[str, str] = {} - with tarfile.open(tarball, "r:gz") as archive: - for member in archive: - parts = member.name.split("/", 1) - if len(parts) < 2 or not parts[1].startswith("src/"): - continue - path = parts[1] - if member.issym(): - entries[path[4:]] = ("120000", git_blob_sha(member.linkname.encode())) - elif member.isfile(): - handle = archive.extractfile(member) - data = handle.read() if handle else b"" - mode = "100755" if member.mode & 0o111 else "100644" - entries[path[4:]] = (mode, git_blob_sha(data)) - if path == "src/driver.c" or path.endswith(".h") or ( - path.startswith("src/drivers/") and path.endswith(".c") - ): - texts[path] = data.decode("latin-1") - if "src/driver.c" not in texts: - raise FileNotFoundError(f"{tarball} has no src/driver.c") - return DriverSource( - driver_c=texts["src/driver.c"], - drivers={p: t for p, t in texts.items() if p.startswith("src/drivers/") and p.endswith(".c")}, - headers={p: t for p, t in texts.items() if p.endswith(".h")}, - tree=git_tree_sha(entries), - ) - - -def fetch_driver_source( - slug: str, commit: str, cache_dir: Path, offline: bool = False -) -> DriverSource: - """src/ at a commit, its tree sha checked against the repository's own - when online.""" - tarball = cache_dir / "src" / slug / f"{commit}.tar.gz" - if offline: - if not tarball.is_file(): - raise FileNotFoundError(f"{tarball} is not cached") - else: - download(f"https://codeload.github.com/{slug}/tar.gz/{commit}", tarball, - skip_existing=True) - source = read_driver_source(tarball) - if not offline: - listing = api_json(f"https://api.github.com/repos/{slug}/git/trees/{commit}") - listed = next( - ( - item.get("sha") - for item in (listing.get("tree", []) if isinstance(listing, dict) else []) - if item.get("path") == "src" - ), - None, - ) - if listed != source.tree: - tarball.unlink() - raise BlobMismatchError(f"{slug}@{commit[:8]}:src is tree {listed}, not {source.tree}") - return source - - def dat_gaps(code: dict[str, SetEntry], dat: dict[str, SetEntry], marker: str) -> dict: """Where the DAT a repository carries contradicts its own code.""" both = code.keys() & dat.keys() diff --git a/scripts/scraper/arcade_sets.py b/scripts/scraper/arcade_sets.py new file mode 100644 index 000000000..928e983a1 --- /dev/null +++ b/scripts/scraper/arcade_sets.py @@ -0,0 +1,234 @@ +"""Game sets and their BIOS parents as a DAT, FBNeo's driver list and flycast's table declare them.""" + +from __future__ import annotations + +import re +from collections import Counter +from collections.abc import Iterator +from dataclasses import dataclass, field +from typing import IO + +from .clex import blank_comments +from .fbneo_parser import zip_name +from .listxml import Opener, attrs, iter_elements, rom_members + + +@dataclass +class SetEntry: + """What the index needs from one machine or game element.""" + + name: str + romof: str | None = None + isbios: bool = False + isdevice: bool = False + runnable: bool = True + has_roms: bool = False + disks: bool = False + devices: list[str] = field(default_factory=list) + source: str | None = None + + +_DEVICE_REF = re.compile(rb']*\bname="([^"]+)"') +_DISK = re.compile(rb"]*)>") + + +def iter_sets(stream: IO[bytes]) -> Iterator[SetEntry]: + """Every machine or game element of a DAT or -listxml, streamed.""" + for element in iter_elements(stream, ("machine", "game")): + found, body = element.attrs, element.body + disks = [attrs(d) for d in _DISK.findall(body)] + yield SetEntry( + name=found.get("name", ""), + romof=found.get("romof") or None, + isbios=found.get("isbios") == "yes", + isdevice=found.get("isdevice") == "yes", + runnable=found.get("runnable", "yes") != "no", + has_roms=bool(rom_members(body)), + disks=any(d.get("status") != "nodump" for d in disks), + devices=[d.decode() for d in _DEVICE_REF.findall(body)], + source=found.get("sourcefile"), + ) + + +def is_bios(entry: SetEntry, marker: str) -> bool: + if entry.isbios: + return True + return marker == "runnable" and not entry.runnable and not entry.isdevice + + +# --- FBNeo: one namespace per DAT --------------------------------------------- + +# Generated at build time from every driver and committed: the set name and the +# source file of each driver. +FBNEO_DRIVERLIST = "src/dep/generated/driverlist.h" +_FBNEO_DRIVER = re.compile(r'\{\s*"([^"]+)",\s*"([^"]+\.cpp)"\s*\}') + + +def fbneo_drivers(listing: str) -> dict[str, set[str]]: + """Driver names per source file, from driverlist.h.""" + names: dict[str, set[str]] = {} + for name, path in _FBNEO_DRIVER.findall(listing): + names.setdefault(path, set()).add(name) + if not names: + raise ValueError(f"{FBNEO_DRIVERLIST} lists no driver") + return names + + +def _fbneo_candidates(entry: SetEntry, names: dict[str, set[str]]) -> list[str]: + """The drivers a DAT set can be. A console DAT writes the archive name, + which the driver's GetZipName() takes by cutting the system prefix: + ``1942`` in the MSX DAT is the driver ``msx_1942``.""" + drivers = names.get(entry.source or "", set()) + if entry.name in drivers: + return [entry.name] + return sorted(n for n in drivers if "_" in n and zip_name(n) == entry.name) + + +def _fbneo_driver_names(entries: list[SetEntry], names: dict[str, set[str]]) -> dict[str, str]: + """Driver name per set of one DAT. Where a source file holds two systems + (d_nes.cpp builds the NES and the FDS drivers), the prefix the rest of + the DAT carries decides.""" + candidates = {e.name: _fbneo_candidates(e, names) for e in entries} + prefixes = Counter( + found[0].split("_", 1)[0] + for name, found in candidates.items() + if len(found) == 1 and found[0] != name + ) + driver: dict[str, str] = {} + for entry in entries: + found = candidates[entry.name] + if len(found) > 1 and prefixes: + dominant = prefixes.most_common(1)[0][0] + found = [n for n in found if n.split("_", 1)[0] == dominant] + if len(found) != 1: + raise ValueError(f"{entry.source}: {entry.name} matches drivers {found}") + driver[entry.name] = found[0] + return driver + + +def fbneo_sets( + openers: list[Opener], names: dict[str, set[str]] +) -> tuple[dict[str, SetEntry], dict[str, str]]: + """Every set of FBNeo's DATs keyed by its driver, and the archive of each + set whose driver name is not its archive name. + + Each DAT is its own namespace: the Arcade, MSX, NES and ZX Spectrum DATs + all carry a ``1942``, and a ``romof`` inside a DAT names a set of that DAT. + Merged by archive name, the MSX ``1942`` that needs msx.zip was the + arcade one, which needs nothing. The Arcade and Neo Geo DATs both carry + the Neo Geo drivers: a driver read twice keeps its first reading. + """ + sets: dict[str, SetEntry] = {} + archives: dict[str, str] = {} + for opener in openers: + with opener() as stream: + entries = [e for e in iter_sets(stream) if e.name] + driver = _fbneo_driver_names(entries, names) + for entry in entries: + name = driver[entry.name] + if name in sets: + continue + if name != entry.name: + archives[name] = entry.name + if entry.romof: + entry.romof = driver.get(entry.romof, entry.romof) + entry.name = name + sets[name] = entry + return sets, archives + + +# --- C table reader (flycast naomi_roms.cpp) --------------------------------- + +_TOKEN = re.compile(r'"(?:\\.|[^"\\])*"|[{},]|[^\s{},"]+') + + +def _initializer(text: str, array: str) -> list: + """The brace initializer of ``const [] = {...}`` as nested + lists of tokens, each element split on its top-level commas.""" + match = re.search(rf"\b{re.escape(array)}\s*\[\s*\]\s*=\s*\{{", text) + if not match: + raise ValueError(f"no initializer for {array}[]") + tokens = _TOKEN.findall(text[match.end() - 1 :]) + stack: list[list] = [] + item: list = [] + for token in tokens: + if token == "{": + stack.append(item) + item = [] + stack.append([]) + elif token == "}": + elements = stack.pop() + if item: + elements.append(item if len(item) > 1 else item[0]) + item = stack.pop() + item.append(elements) + if not stack: + return item[0] + elif token == ",": + elements = stack[-1] + if item: + elements.append(item if len(item) > 1 else item[0]) + item = [] + else: + item.append(token) + raise ValueError(f"unterminated initializer for {array}[]") + + +def _string(token: object) -> str | None: + if isinstance(token, str) and token.startswith('"'): + return token[1:-1] + return None + + +def flycast_index(source: str) -> dict: + """BIOS archive per NAOMI/Atomiswave game from naomi_roms.cpp.""" + text = blank_comments(source) + archive_of: dict[str, str] = {} + for element in _initializer(text, "BIOS"): + if not isinstance(element, list) or not element: + continue + name = _string(element[0]) + if name is None: + continue + # BIOS_t: name, blobs[], filename (naomi_roms.h:53-66). + filename = _string(element[2]) if len(element) > 2 else None + archive_of[name] = filename or name + bios: dict[str, set[str]] = {} + chd: list[str] = [] + unknown: dict[str, str] = {} + games: set[str] = set() + for element in _initializer(text, "Games"): + if not isinstance(element, list) or not element: + continue + name = _string(element[0]) + if name is None: + continue + # shors2k2 is declared twice at e88b0ced: a name counts once. + games.add(name) + # Game: name, parent_name, description, size, key, bios, cart_type, + # rotation, blobs[], gdrom_name, ... (naomi_roms.h:71-94). + named = _string(element[5]) if len(element) > 5 else None + if named is not None and named not in archive_of: + unknown[name] = named + continue + bios.setdefault(archive_of[named or "naomi"], set()).add(name) + if len(element) > 9 and _string(element[9]): + chd.append(name) + return { + "bios": {k: sorted(v) for k, v in sorted(bios.items())}, + "devices": {}, + "chd": sorted(chd), + "missing_parents": dict(sorted(unknown.items())), + "no_bios": [], + "bios_sets": sorted(set(archive_of.values())), + "counts": { + "sets": len(games) + len(archive_of), + "games": len(games), + "games_with_bios": len({g for v in bios.values() for g in v}), + "bios_sets": len(archive_of), + "bios_roots_used": len(bios), + "games_with_devices": 0, + "chd_games": len(chd), + "missing_parents": len(unknown), + }, + } diff --git a/tests/test_arcade_parents.py b/tests/test_arcade_parents.py index 194f2b2d3..11a8b20ff 100644 --- a/tests/test_arcade_parents.py +++ b/tests/test_arcade_parents.py @@ -12,6 +12,9 @@ import zipfile from pathlib import Path from scripts.scraper import arcade_parent_importer as api +from scripts.scraper import arcade_drivers +from scripts.scraper import arcade_sets +from scripts.scraper import clex from scripts.scraper import listxml ROOT = Path(__file__).resolve().parent.parent @@ -94,7 +97,7 @@ class RunnableMarker(unittest.TestCase): class FlycastTable(unittest.TestCase): def setUp(self) -> None: - self.index = api.flycast_index((FIXTURES / "naomi_roms.cpp").read_text()) + self.index = arcade_sets.flycast_index((FIXTURES / "naomi_roms.cpp").read_text()) def test_null_bios_is_naomi_and_filename_names_the_archive(self) -> None: self.assertEqual(self.index["bios"]["naomi"], ["gram2000", "ikaruga", "jpgame"]) @@ -298,7 +301,7 @@ class DriverSource(unittest.TestCase): def test_the_source_is_named_by_its_tree(self) -> None: tree = self.doc["source"]["files"]["src"] - source = api.read_driver_source(self.cache / "src" / "libretro" / "mame2003-libretro" / f"{COMMIT}.tar.gz") + source = arcade_drivers.read_driver_source(self.cache / "src" / "libretro" / "mame2003-libretro" / f"{COMMIT}.tar.gz") self.assertEqual(tree, source.tree) self.assertEqual(list(self.doc["source"]["files"]), ["src"]) @@ -309,11 +312,11 @@ class DriverSource(unittest.TestCase): self.assertIn("built1", gaps["code_only"]) def test_an_undeclared_driver_is_refused(self) -> None: - with self.assertRaises(api.DriverSourceError): - api.driver_source_sets("DRIVER( nosuch )", {"src/drivers/a.c": ""}, {}) + with self.assertRaises(clex.DriverSourceError): + arcade_drivers.driver_source_sets("DRIVER( nosuch )", {"src/drivers/a.c": ""}, {}) def test_the_empty_tree(self) -> None: - self.assertEqual(api.git_tree_sha({}), "4b825dc642cb6eb9a060e54bf8d69288fbee4904") + self.assertEqual(arcade_drivers.git_tree_sha({}), "4b825dc642cb6eb9a060e54bf8d69288fbee4904") @unittest.skipIf(Draft202012Validator is None, "jsonschema not installed") def test_output_matches_the_schema(self) -> None: @@ -346,7 +349,7 @@ class FbneoNamespaces(unittest.TestCase): """Each FBNeo DAT is its own namespace, a console set is its driver.""" def setUp(self) -> None: - names = api.fbneo_drivers(FBNEO_LIST) + names = arcade_sets.fbneo_drivers(FBNEO_LIST) arcade = _dat(_game("1942", "pre90s/d_1942.cpp")) msx = _dat( _game("1942", "msx/d_msx.cpp", "msx"), @@ -354,7 +357,7 @@ class FbneoNamespaces(unittest.TestCase): '', ) nes = _dat(_game("zelda", "nes/d_nes.cpp"), _game("smb", "nes/d_nes.cpp")) - sets, self.archives = api.fbneo_sets( + sets, self.archives = arcade_sets.fbneo_sets( [lambda d=d: io.BytesIO(d) for d in (arcade, msx, nes)], names ) self.sets = sets