From f245c210e098e9fbdfc288d92b8c901fca8eff29 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 04:27:50 +0100 Subject: [PATCH] refactor: read retrobat es_systems.cfg once --- scripts/scraper/es_emulators.py | 25 +++++++-- scripts/scraper/retrobat_scraper.py | 87 ++++------------------------- tests/test_batocera_cores.py | 6 +- tests/test_save_root.py | 4 +- tests/test_scraper_contract.py | 25 +++++++-- 5 files changed, 58 insertions(+), 89 deletions(-) diff --git a/scripts/scraper/es_emulators.py b/scripts/scraper/es_emulators.py index 61efaea98..d83081749 100644 --- a/scripts/scraper/es_emulators.py +++ b/scripts/scraper/es_emulators.py @@ -28,12 +28,14 @@ def xml_systems(text: str) -> SystemEmulators: name = (system.findtext("name") or "").strip() if not name: continue - systems[name] = [ + # Two blocks may share a name (RetroBat's two `starship`): + # each still offers its emulators. + systems.setdefault(name, []).extend( (emulator.get("name"), - [core.text.strip() for core in emulator.iter("core") if core.text]) + [text for core in emulator.iter("core") if (text := (core.text or "").strip())]) for emulator in system.iter("emulator") if emulator.get("name") - ] + ) if not any(systems.values()): raise ValueError("es_systems.cfg names no emulator") return systems @@ -43,10 +45,10 @@ def json_systems(text: str) -> SystemEmulators: """Each system's emulators and cores, from an es_systems.json.""" systems: SystemEmulators = {} for system in json.loads(text).get("systems", []): - systems[system["name"]] = [ + systems.setdefault(system["name"], []).extend( (emulator["name"], [core["name"] for core in emulator.get("cores") or []]) for emulator in system.get("emulators") or [] - ] + ) if not any(systems.values()): raise ValueError("es_systems.json names no emulator") return systems @@ -60,6 +62,19 @@ def _pairs(systems: SystemEmulators) -> dict[str, set[str]]: return pairs +def libretro_systems( + systems: SystemEmulators, runner: str = "libretro" +) -> dict[str, list[str]]: + """libretro core -> the systems the list offers it for, under `runner`.""" + found: dict[str, set[str]] = {} + for system, emulators in systems.items(): + for emulator, cores in emulators: + if emulator == runner: + for core in cores: + found.setdefault(core, set()).add(system) + return {core: sorted(names) for core, names in sorted(found.items())} + + def xml_emulators(text: str) -> dict[str, set[str]]: """Emulator -> cores, from an es_systems.cfg.""" return _pairs(xml_systems(text)) diff --git a/scripts/scraper/retrobat_scraper.py b/scripts/scraper/retrobat_scraper.py index 4ac76f826..1f3b3358c 100644 --- a/scripts/scraper/retrobat_scraper.py +++ b/scripts/scraper/retrobat_scraper.py @@ -23,6 +23,7 @@ from .base_scraper import ( systems_from_requirements, ) from .batocera_scraper import SYSTEM_SLUG_MAP as BATOCERA_SLUGS +from .es_emulators import es_cores, libretro_systems, xml_systems from .retrobat_launchers import SOURCE_FILES as LAUNCHER_FILES from .retrobat_launchers import extract_launchers @@ -47,72 +48,6 @@ ES_SYSTEMS_URL = ( ) -def es_systems_dual(text: str) -> set[str]: - """Emulators es_systems.cfg offers both as a libretro core and standalone.""" - from model.safeparse import parse_untrusted_xml - - libretro: set[str] = set() - standalone: set[str] = set() - for emulator in parse_untrusted_xml(text, "es_systems.cfg").iter("emulator"): - name = (emulator.get("name") or "").strip() - if name == "libretro": - libretro |= {(core.text or "").strip() for core in emulator.iter("core")} - {""} - elif name: - standalone.add(name) - return libretro & standalone - - -def es_systems_cores(text: str) -> tuple[set[str], set[str]]: - """Every core RetroBat's ES offers, and the emulators it runs standalone. - - emulatorlauncher starts what es_systems.cfg lists: a libretro core by its - name under ````, any other emulator by its own - name. batocera-systems.json names BIOS files only, so the platform read - from it alone had no core list and relied on one kept by hand. - """ - from model.safeparse import parse_untrusted_xml - - root = parse_untrusted_xml(text, "es_systems.cfg") - cores: set[str] = set() - standalone: set[str] = set() - for emulator in root.iter("emulator"): - name = (emulator.get("name") or "").strip() - if not name: - continue - listed = {(core.text or "").strip() for core in emulator.iter("core")} - {""} - if name == "libretro": - cores |= listed - else: - cores.add(name) - standalone.add(name) - return cores, standalone - - -def es_systems_libretro_systems(text: str) -> dict[str, list[str]]: - """libretro core -> the ES systems es_systems.cfg offers it for. - - emulatorLauncher hands a libretro core saves\\ at each launch - (LibRetro.Generator.cs Configure: savefile_directory), so a file a core - reads below its save directory sits once in each of these system - folders. A commented-out system is no element and counts for nothing. - """ - from model.safeparse import parse_untrusted_xml - - found: dict[str, set[str]] = {} - for system in parse_untrusted_xml(text, "es_systems.cfg").iter("system"): - name = (system.findtext("name") or "").strip() - if not name: - continue - for emulator in system.iter("emulator"): - if (emulator.get("name") or "").strip() != "libretro": - continue - for core in emulator.iter("core"): - core_name = (core.text or "").strip() - if core_name: - found.setdefault(core_name, set()).add(name) - return {core: sorted(systems) for core, systems in sorted(found.items())} - - def pinned_revision(platform_file: Path = PLATFORM_FILE) -> str: """The emulatorLauncher commit platforms/retrobat.yml was read from. @@ -262,11 +197,13 @@ class Scraper(BaseScraper): version = fetch_github_latest_version(GITHUB_REPO) es_url = ES_SYSTEMS_URL.format(tag=version) es_text = fetch_text(es_url) - cores, standalone = es_systems_cores(es_text) - dual = es_systems_dual(es_text) - libretro_systems = es_systems_libretro_systems(es_text) - if not cores: - raise RuntimeError(f"es_systems.cfg at {version} names no core") + # emulatorLauncher starts a libretro core by its name under + # , any other emulator by its own name. + es_systems = xml_systems(es_text) + cores, standalone, dual = es_cores(es_systems, {"libretro": None}) + # emulatorLauncher hands a libretro core saves\\ at each launch + # (LibRetro.Generator.cs Configure: savefile_directory). + core_systems = libretro_systems(es_systems) return { "platform": "RetroBat", @@ -276,13 +213,13 @@ class Scraper(BaseScraper): "base_destination": "bios", "hash_type": "md5", "verification_mode": "md5", - "cores": sorted(cores), - "standalone_cores": sorted(standalone), + "cores": cores, + "standalone_cores": standalone, # Launched both ways: each layout gets its files. - **({"dual_mode_cores": sorted(dual)} if dual else {}), + **({"dual_mode_cores": dual} if dual else {}), "systems": systems, # The systems each libretro core runs for: its save folders. - "libretro_systems": libretro_systems, + "libretro_systems": core_systems, "launchers": fetch_launchers(required_revision(self.url, LAUNCHER_REPO, commit=True)), } diff --git a/tests/test_batocera_cores.py b/tests/test_batocera_cores.py index c68ad7124..337c46706 100644 --- a/tests/test_batocera_cores.py +++ b/tests/test_batocera_cores.py @@ -131,12 +131,12 @@ class AnEmulatorLaunchedBothWaysGetsBothLayouts(unittest.TestCase): def test_retrobat_and_recalbox_name_theirs(self): from scripts.scraper.recalbox_scraper import system_ini_cores, system_ini_libretro - from scripts.scraper.retrobat_scraper import es_systems_dual + from scripts.scraper.es_emulators import es_cores, xml_systems - cfg = ("" + cfg = ("gamecube" "dolphin" "") - self.assertEqual(es_systems_dual(cfg), {"dolphin"}) + self.assertEqual(es_cores(xml_systems(cfg), {"libretro": None})[2], ["dolphin"]) ini = "[core.0]\nemulator = libretro\ncore = dolphin\n[core.1]\nemulator = dolphin\ncore = dolphin\n" _cores, standalone = system_ini_cores(ini) self.assertEqual(system_ini_libretro(ini) & standalone, {"dolphin"}) diff --git a/tests/test_save_root.py b/tests/test_save_root.py index 6ccdd1f04..9a3313c58 100644 --- a/tests/test_save_root.py +++ b/tests/test_save_root.py @@ -811,7 +811,7 @@ class TheScrapersReadTheSystemLists(unittest.TestCase): {"dolphin": ["gamecube", "wii"], "stella": ["atari2600"]}) def test_retrobat_es_systems_cfg(self): - from scripts.scraper.retrobat_scraper import es_systems_libretro_systems + from scripts.scraper.es_emulators import libretro_systems, xml_systems text = ( "3ds" @@ -820,7 +820,7 @@ class TheScrapersReadTheSystemLists(unittest.TestCase): "citra-qt" "" ) - self.assertEqual(es_systems_libretro_systems(text), {"azahar": ["3ds"]}) + self.assertEqual(libretro_systems(xml_systems(text)), {"azahar": ["3ds"]}) def test_recalbox_system_ini(self): from scripts.scraper.recalbox_scraper import libretro_systems diff --git a/tests/test_scraper_contract.py b/tests/test_scraper_contract.py index 334b92388..56e22fad0 100644 --- a/tests/test_scraper_contract.py +++ b/tests/test_scraper_contract.py @@ -477,11 +477,28 @@ class RetroBatCoresFromItsES(unittest.TestCase): """ def test_libretro_cores_and_standalone_emulators(self): - from scraper.retrobat_scraper import es_systems_cores + from scraper.es_emulators import es_cores, xml_systems - cores, standalone = es_systems_cores(self.ES_SYSTEMS) - self.assertEqual(cores, {"snes9x", "bsnes", "simcoupe"}) - self.assertEqual(standalone, {"bsnes", "simcoupe"}) + cores, standalone, dual = es_cores(xml_systems(self.ES_SYSTEMS), {"libretro": None}) + self.assertEqual(cores, ["bsnes", "simcoupe", "snes9x"]) + self.assertEqual(standalone, ["bsnes", "simcoupe"]) + self.assertEqual(dual, ["bsnes"]) + + def test_same_named_systems_keep_every_emulator(self): + # RetroBat 8.3.1 names two blocks `starship`: ghostship + # sits under the first one only. + from scraper.es_emulators import es_cores, xml_systems + + text = ( + "" + "starship" + "" + "starship" + "" + "" + ) + _cores, standalone, _dual = es_cores(xml_systems(text), {"libretro": None}) + self.assertEqual(standalone, ["ghostship", "starship"]) def test_the_registry_leaves_the_list_to_the_scraper(self): import yaml