diff --git a/scripts/scraper/arcade_parent_importer.py b/scripts/scraper/arcade_parent_importer.py index 83d7f129a..9de40edaf 100644 --- a/scripts/scraper/arcade_parent_importer.py +++ b/scripts/scraper/arcade_parent_importer.py @@ -57,6 +57,7 @@ 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 model.mamerelease import mame_tag @@ -247,7 +248,7 @@ def _fbneo_candidates(entry: SetEntry, names: dict[str, set[str]]) -> list[str]: 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 n.split("_", 1)[1] == 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]: diff --git a/scripts/scraper/fbneo_hash_scraper.py b/scripts/scraper/fbneo_hash_scraper.py index fffc2fadc..5e683d3cc 100644 --- a/scripts/scraper/fbneo_hash_scraper.py +++ b/scripts/scraper/fbneo_hash_scraper.py @@ -193,13 +193,11 @@ def run( diff = compute_diff(str(path), str(CACHE_PATH), mode="fbneo") print(_format_diff(path.stem, diff, show_added=is_main)) - # New entries are never written. A non-arcade FBNeo driver opens an - # archive whose name has the system prefix stripped by its own - # GetZipName (cv_coleco -> coleco.zip, gba_gba -> gba.zip), and the - # parser reads the driver struct name instead. Adding those would - # create phantom archives the core never opens, next to the correctly - # named entries already curated. Refreshing existing entries is safe: - # they are keyed by the archive name the profile already declares. + # New entries are never written: the parser knows one line per + # driver, the struct, while a curated entry cites the RomDesc line of + # its own ROM. Existing entries, console archives included (the + # parser keys each board ROM set by the archive GetZipName opens), + # are refreshed in place. if not dry_run and diff["updated"]: merge_fbneo_profile(str(path), str(CACHE_PATH), write=True, add_new=False) log.info("merged changes into %s", path.name) diff --git a/scripts/scraper/fbneo_parser.py b/scripts/scraper/fbneo_parser.py index 900867a74..1205330c5 100644 --- a/scripts/scraper/fbneo_parser.py +++ b/scripts/scraper/fbneo_parser.py @@ -2,7 +2,8 @@ Parses BurnRomInfo structs (static ROM arrays) and BurnDriver structs (driver registration) from FBNeo C source files. BIOS sets are identified -by the BDF_BOARDROM flag in BurnDriver definitions. +by the BDF_BOARDROM flag in BurnDriver definitions, and keyed by the +archive the driver opens. """ from __future__ import annotations @@ -13,8 +14,9 @@ from pathlib import Path from core.lines import LineIndex +# Sizes are written in hex or decimal (d_snes.cpp: ``{ "dsp1.bin", 10240, ...``). _ROM_ENTRY_RE = re.compile( - r'\{\s*"([^"]+)"\s*,\s*(0x[\da-fA-F]+)\s*,\s*(0x[\da-fA-F]+)\s*,\s*([^}]+)\}', + r'\{\s*"([^"]+)"\s*,\s*(0x[\da-fA-F]+|\d+)\s*,\s*(0x[\da-fA-F]+)\s*,\s*([^}]+)\}', ) _BURN_DRIVER_RE = re.compile( @@ -22,40 +24,67 @@ _BURN_DRIVER_RE = re.compile( re.DOTALL, ) -_ROM_DESC_RE = re.compile( - r"static\s+struct\s+BurnRomInfo\s+(\w+)RomDesc\s*\[\s*\]\s*=\s*\{(.*?)\};", - re.DOTALL, -) +# The first three fields of a BurnDriver: name, parent, board ROM. +_DRIVER_HEAD_RE = re.compile(r'\s*("[^"]*"|NULL)\s*,\s*("[^"]*"|NULL)\s*,\s*("[^"]*"|NULL)') + +# The GetZipName field of a BurnDriver and its RomInfo, which names the +# RomDesc array (BurnDrvngpc_ngp is "ngp_ngp" and reads ngpc_ngpRomDesc). +_ZIP_AND_ROMS_RE = re.compile(r"(\w+)\s*,\s*(\w+)RomInfo\s*,") + + +def zip_name(driver: str) -> str: + """The archive a driver opens through a system GetZipName. + + Every non-arcade GetZipName cuts the system prefix up to its underscore + (MSXGetZipName in d_msx.cpp: ``msx_msx`` opens msx.zip, ``msx_1942`` + opens 1942.zip). The arcade drivers have none and open their own name. + """ + return driver.split("_", 1)[1] if "_" in driver else driver + + +def _quoted(field: str) -> str: + return field.strip('"') if field != "NULL" else "" def find_bios_sets(source: str, filename: str) -> dict[str, dict]: """Find BDF_BOARDROM drivers in source code. - Returns a dict mapping set name to metadata: - {set_name: {"source_file": str, "source_line": int}} + A board ROM set is keyed by the archive the games open it under: their + GetZipName reads the board ROM name too (``i == 1``), so ``ngp_ngp``, + whose own field is NULL, is looked up as ngp.zip by every NGP game. + + Returns a dict mapping that archive to metadata: + {archive: {"source_file": str, "source_line": int, "rom_desc": str}} """ - results: dict[str, dict] = {} lines = LineIndex(source) + boards: list[tuple[str, int, bool, str]] = [] + zipped_boards: set[str] = set() for match in _BURN_DRIVER_RE.finditer(source): body = match.group(2) + head = _DRIVER_HEAD_RE.match(body) + fields = _ZIP_AND_ROMS_RE.search(body) + own_zip = fields is not None and fields.group(1) != "NULL" + if head and own_zip and _quoted(head.group(3)): + zipped_boards.add(_quoted(head.group(3))) if "BDF_BOARDROM" not in body: continue - # Set name is the first quoted string in the struct body name_match = re.search(r'"([^"]+)"', body) if not name_match: continue + driver = name_match.group(1) + rom_desc = fields.group(2) if fields else driver + boards.append((driver, lines.line(match.start()), own_zip, rom_desc)) - set_name = name_match.group(1) - line_num = lines.line(match.start()) - - results[set_name] = { + return { + (zip_name(driver) if own_zip or driver in zipped_boards else driver): { "source_file": filename, - "source_line": line_num, + "source_line": line, + "rom_desc": rom_desc, } - - return results + for driver, line, own_zip, rom_desc in boards + } def parse_rom_info(source: str, set_name: str) -> list[dict]: @@ -81,7 +110,7 @@ def parse_rom_info(source: str, set_name: str) -> list[dict]: name = entry.group(1) if not name: continue - size = int(entry.group(2), 16) + size = int(entry.group(2), 0) crc32 = format(int(entry.group(3), 16), "08x") roms.append( @@ -101,7 +130,7 @@ def parse_fbneo_source_tree(base_path: str) -> dict[str, dict]: Scans .cpp files under src/burn/drv/ for BDF_BOARDROM drivers, then parses their associated BurnRomInfo arrays. - Returns a dict mapping set name to: + Returns a dict mapping the archive each driver opens to: {source_file, source_line, roms: [{name, size, crc32}, ...]} """ drv_path = Path(base_path) / "src" / "burn" / "drv" @@ -120,9 +149,9 @@ def parse_fbneo_source_tree(base_path: str) -> dict[str, dict]: rel_path = str(filepath.relative_to(base_path)) bios_sets = find_bios_sets(source, rel_path) - for set_name, meta in bios_sets.items(): - roms = parse_rom_info(source, set_name) - results[set_name] = { + for archive, meta in bios_sets.items(): + roms = parse_rom_info(source, meta["rom_desc"]) + results[archive] = { "source_file": meta["source_file"], "source_line": meta["source_line"], "roms": roms, diff --git a/tests/test_fbneo_parser.py b/tests/test_fbneo_parser.py index 328839f5e..44fc74830 100644 --- a/tests/test_fbneo_parser.py +++ b/tests/test_fbneo_parser.py @@ -12,6 +12,7 @@ from scripts.scraper.fbneo_parser import ( find_bios_sets, parse_fbneo_source_tree, parse_rom_info, + zip_name, ) NEOGEO_FIXTURE = """\ @@ -83,8 +84,72 @@ struct BurnDriver BurnDrvmslug = { }; """ +# d_snes.cpp: a console driver with its own GetZipName and a decimal size. +SNES_FIXTURE = """\ +static struct BurnRomInfo snes_dsp1RomDesc[] = { + { "dsp1.bin", 10240, 0x2838f9f5, BRF_ESS | BRF_PRG }, +}; + +struct BurnDriver BurnDrvsnes_dsp1 = { + "snes_dsp1", NULL, NULL, NULL, "x", + "DSP-1\\0", NULL, "DSP microcode", "SNES / Super Famicom", + NULL, NULL, NULL, NULL, + BDF_BOARDROM, 1, HARDWARE_SNES, GBF_BIOS, 0, + SNESGetZipName, snes_dsp1RomInfo, snes_dsp1RomName, NULL, NULL, NULL, NULL, SNESInputInfo, SNESDIPInfo, + DrvInit, DrvExit, DrvFrame, DrvDraw, DrvScan, &DrvRecalc, 0x8000, + 512, 448, 4, 3 +}; +""" + +# d_ngp.cpp: the board ROM driver has no GetZipName of its own, its games +# open it through NgpGetZipName, and its RomDesc is not named after it. +NGP_FIXTURE = """\ +static struct BurnRomInfo ngpc_ngpRomDesc[] = { + { "SNK Neo-Geo Pocket BIOS (1998)(SNK)(en-ja).bin", 0x10000, 0x6232df8d, BRF_PRG | BRF_BIOS }, + { "SNK Neo-Geo Pocket Color BIOS (1999)(SNK)(en-ja).bin", 0x10000, 0x6EEB6F40, BRF_PRG | BRF_BIOS }, +}; + +struct BurnDriver BurnDrvngpc_ngp = { + "ngp_ngp", NULL, NULL, NULL, "1998", + "NeoGeo Pocket (Bios)\\0", "BIOS only", "SNK", "NeoGeo Pocket", + NULL, NULL, NULL, NULL, + BDF_BOARDROM, 0, HARDWARE_SNK_NGP | HARDWARE_SNK_NGPC, GBF_BIOS, 0, + NULL, ngpc_ngpRomInfo, ngpc_ngpRomName, NULL, NULL, NULL, NULL, NgpInputInfo, NgpDIPInfo, + DrvInit, DrvExit, DrvFrame, k1geDraw, DrvScan, &BurnRecalc, 0x1000, + 160, 152, 4, 3 +}; + +struct BurnDriver BurnDrvngpc_bakumats = { + "ngp_bakumats", "ngp_lastblad", "ngp_ngp", NULL, "2000", + "Bakumatsu Rouman\\0", NULL, "SNK", "NeoGeo Pocket Color", + NULL, NULL, NULL, NULL, + BDF_GAME_WORKING, 1, HARDWARE_SNK_NGPC, GBF_VSFIGHT, 0, + NgpGetZipName, ngpc_bakumatsRomInfo, ngpc_bakumatsRomName, NULL, NULL, NULL, NULL, NgpInputInfo, NgpDIPInfo, + DrvInit, DrvExit, DrvFrame, k1geDraw, DrvScan, &BurnRecalc, 0x1000, + 160, 152, 4, 3 +}; +""" + + +class TestZipName(unittest.TestCase): + def test_cuts_the_system_prefix(self) -> None: + self.assertEqual(zip_name("msx_msx"), "msx") + self.assertEqual(zip_name("msx_1942"), "1942") + self.assertEqual(zip_name("snes_st010"), "st010") + + def test_name_without_prefix_is_kept(self) -> None: + self.assertEqual(zip_name("neogeo"), "neogeo") + class TestFindBiosSets(unittest.TestCase): + def test_console_driver_keyed_by_its_archive(self) -> None: + result = find_bios_sets(SNES_FIXTURE, "d_snes.cpp") + self.assertEqual(list(result), ["dsp1"]) + + def test_board_rom_keyed_by_what_its_games_open(self) -> None: + result = find_bios_sets(NGP_FIXTURE, "d_ngp.cpp") + self.assertEqual(list(result), ["ngp"]) + def test_detects_neogeo(self) -> None: result = find_bios_sets(NEOGEO_FIXTURE, "d_neogeo.cpp") self.assertIn("neogeo", result) @@ -142,6 +207,10 @@ class TestParseRomInfo(unittest.TestCase): self.assertEqual(bios["name"], "pgm_p01s.rom") self.assertEqual(bios["crc32"], "e42b166e") + def test_decimal_size(self) -> None: + roms = parse_rom_info(SNES_FIXTURE, "snes_dsp1") + self.assertEqual(roms, [{"name": "dsp1.bin", "size": 10240, "crc32": "2838f9f5"}]) + def test_unknown_set_returns_empty(self) -> None: roms = parse_rom_info(NEOGEO_FIXTURE, "nonexistent") self.assertEqual(roms, []) @@ -158,6 +227,15 @@ class TestParseSourceTree(unittest.TestCase): self.assertIn("neogeo", result) self.assertEqual(len(result["neogeo"]["roms"]), 5) + def test_reads_the_rom_desc_the_driver_names(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + drv_dir = Path(tmpdir) / "src" / "burn" / "drv" + drv_dir.mkdir(parents=True) + (drv_dir / "d_ngp.cpp").write_text(NGP_FIXTURE) + + result = parse_fbneo_source_tree(tmpdir) + self.assertEqual([r["crc32"] for r in result["ngp"]["roms"]], ["6232df8d", "6eeb6f40"]) + def test_skips_non_cpp(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: drv_dir = Path(tmpdir) / "src" / "burn" / "drv"