refactor: read retrobat es_systems.cfg once

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 06:01:53 +01:00
1 parent 3eb6bd81a1
commit f245c210e0
5 files changed
+58 -89

No files matched your search

+20 -5
View File
@@ -28,12 +28,14 @@ def xml_systems(text: str) -> SystemEmulators:
name = (system.findtext("name") or "").strip()
if not name:
continue
systems[name] = [
# Two <system> 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))
+12 -75
View File
@@ -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 ``<emulator name="libretro">``, 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\\<system> 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
# <emulator name="libretro">, 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\\<system> 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)),
}
+3 -3
View File
@@ -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 = ("<systemList><system><emulators>"
cfg = ("<systemList><system><name>gamecube</name><emulators>"
"<emulator name=\"libretro\"><cores><core>dolphin</core></cores></emulator>"
"<emulator name=\"dolphin\"/></emulators></system></systemList>")
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"})
+2 -2
View File
@@ -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 = (
"<systemList><system><name>3ds</name><emulators>"
@@ -820,7 +820,7 @@ class TheScrapersReadTheSystemLists(unittest.TestCase):
"<emulator name=\"citra-qt\"><cores><core>citra-qt</core></cores></emulator>"
"</emulators></system></systemList>"
)
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
+21 -4
View File
@@ -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 <system> blocks `starship`: ghostship
# sits under the first one only.
from scraper.es_emulators import es_cores, xml_systems
text = (
"<systemList>"
"<system><name>starship</name><emulators>"
"<emulator name=\"ghostship\"/></emulators></system>"
"<system><name>starship</name><emulators>"
"<emulator name=\"starship\"/></emulators></system>"
"</systemList>"
)
_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