refactor: split romfolders.es by format family

ES-DE moves to romfolders.esde, the formats built from package scripts
(elec-conf, Recalbox, RetroPie) to romfolders.packages.
This commit is contained in:
Abdessamad Derraz committed 2026-11-24 06:20:52 +01:00
1 parent 6e9c06850f
commit 1a33cb6e8a
5 files changed
+622 -599

No files matched your search

+1 -1
View File
@@ -57,7 +57,7 @@ from .base_scraper import (
systems_from_requirements,
)
from .retrodeck_launchers import extract_launchers
from .romfolders.es import command_core, esde_systems
from .romfolders.esde import command_core, esde_systems
PLATFORM_NAME = "retrodeck"
COMPONENTS_REPO = "RetroDECK/components"
+1 -1
View File
@@ -150,7 +150,7 @@ def load_formats() -> dict[str, Parser]:
"""Import every parser module of this package once.
Testing FORMATS for emptiness stopped short once a scraper had imported
one parser module on its own (retrodeck_scraper reads romfolders.es):
one parser module on its own (retrodeck_scraper reads romfolders.esde):
the other formats were never registered. import_module returns a module
already loaded at once, so every call goes through all of them.
"""
-597
View File
@@ -27,15 +27,10 @@ from __future__ import annotations
import html
import json
import re
import shlex
from collections import Counter
from collections.abc import Iterator
from model.romfolders import DEFAULT_ROW_SOURCE, format_ref
from model.safeparse import parse_untrusted_xml, yaml_load_upstream
from ..retropie_modules import PLATFORM_FLAGS, module_enabled, module_fields
from ..recalbox_romfs import Defconfig, defconfig_boards, read_system_ini
from . import Fetcher, SourceError, register
@@ -368,595 +363,3 @@ def es_json(spec: dict, fetcher: Fetcher) -> list[dict]:
"note": row.get("note"),
})
return rows
# ---------------------------------------------------------------------------
# ROCKNIX family: config/emulators/<system>.conf and the emulators package
def _conf_values(text: str) -> tuple[dict[str, str], dict[str, int]]:
values: dict[str, str] = {}
lines: dict[str, int] = {}
for number, line in enumerate(text.splitlines(), 1):
match = re.match(r"^\s*(SYSTEM_[A-Z_]+)=(.*)$", line)
if not match:
continue
raw = match.group(2).strip()
try:
value = " ".join(shlex.split(raw))
except ValueError:
value = raw.strip('"')
values[match.group(1)] = value
lines[match.group(1)] = number
return values, lines
def _nesting(line: str, stack: list[list[str]]) -> bool:
"""Follow case and if nesting. True when the line is consumed."""
match = re.match(r'case\s+"?\$\{?(\w+)\}?"?\s+in', line)
if match:
stack.append(["case", match.group(1), ""])
return True
if line == "esac":
stack.pop()
return True
if line.startswith("if ") or line.startswith("[") and "&&" not in line and line.endswith("then"):
stack.append(["if", line, ""])
return True
if line in ("fi",):
if stack and stack[-1][0] == "if":
stack.pop()
return True
return line in ("then", "else") or line.startswith("elif ")
def _record_call(
words: list[str], number: int, stack: list[list[str]],
cores: list[dict], systems: dict[str, list[int]],
) -> None:
branches = [(s[1], s[2]) for s in stack]
if words[:1] == ["add_emu_core"] and len(words) >= 4:
cores.append({
"system": words[1], "emulator": words[2], "core": words[3],
"default": len(words) > 4 and words[4] == "true",
"branches": branches, "line": number,
})
elif words[:1] == ["add_es_system"] and len(words) >= 2:
systems.setdefault(words[1], []).append(number)
def _package_calls(text: str) -> tuple[list[dict], dict[str, list[int]]]:
"""add_emu_core and add_es_system calls of makeinstall_target, each
with the `case` branches it sits under as (variable, pattern) pairs.
An `if` block nests a condition whose truth the build decides: its
calls carry it as a branch the default choice does not count as
unconditional."""
cores: list[dict] = []
systems: dict[str, list[int]] = {}
stack: list[list[str]] = []
inside = False
for number, raw in enumerate(text.splitlines(), 1):
line = raw.split("#", 1)[0].strip()
if raw.startswith("makeinstall_target()"):
inside = True
continue
if not inside or not line:
continue
if raw.startswith("}"):
break
if _nesting(line, stack):
continue
ends = line.endswith(";;")
if ends:
line = line[:-2].strip()
if stack and stack[-1][0] == "case":
match = re.match(r"^([^()\s][^()]*)\)\s*(.*)$", line)
if match and not line.startswith(("add_", "[", "sed", "cp", "mkdir")):
stack[-1][2] = match.group(1).strip()
line = match.group(2).strip()
_record_call(line.split(), number, stack, cores, systems)
if ends and stack and stack[-1][0] == "case":
stack[-1][2] = ""
return cores, systems
def _board_key(branches: list[tuple[str, str]]) -> str | None:
"""A default chosen under one `case` on the device names that device."""
if len(branches) == 1 and branches[0][1] and branches[0][1] != "*":
variable, pattern = branches[0]
if not variable.startswith("["):
return f"{variable}={pattern}"
return None
@register("elec-conf")
def elec_conf(spec: dict, fetcher: Fetcher) -> list[dict]:
"""A system ES lists is one add_es_system writes from its conf file
(SYSTEM_PATH, SYSTEM_EXTENSION). The default is the core whose
add_emu_core says true: unconditional, else the `*)` branch of a
device case, the branches for named devices going to board_defaults."""
params = spec["params"]
key = params.get("source", DEFAULT_ROW_SOURCE)
package = params["package"]
cores, systems = _package_calls(fetcher.text(key, package))
if not systems:
raise SourceError(f"{package}: no add_es_system call")
conf_dir = params["conf_dir"]
rows: list[dict] = []
for name, lines in systems.items():
conf_path = f"{conf_dir}/{name}.conf"
conf_text = fetcher.optional_text(key, conf_path)
if conf_text is None:
continue
values, value_lines = _conf_values(conf_text)
folder = _strip_prefix(values.get("SYSTEM_PATH", ""), params["path_prefixes"])
if not folder:
continue
mine = [c for c in cores if c["system"] == name]
flagged = [c for c in mine if c["default"]]
plain = [c for c in flagged if not c["branches"]]
star = [c for c in flagged if c["branches"] and c["branches"][-1][1] == "*"]
candidates = plain or star or flagged
pick: dict | None = candidates[0] if candidates else None
if pick is None:
pick = next((c for c in mine if not c["branches"]), mine[0] if mine else None)
board_defaults = {}
for c in flagged:
board = _board_key(c["branches"])
if c is not pick and board and (c["emulator"], c["core"]) != (
pick and pick["emulator"], pick and pick["core"]):
board_defaults[board] = _board(c["core"], c["emulator"], format_ref(key, package, c["line"]))
refs = [
format_ref(key, conf_path, value_lines.get("SYSTEM_PATH"), value_lines.get("SYSTEM_EXTENSION")),
format_ref(key, package, *lines),
]
if pick:
refs.append(format_ref(key, package, pick["line"]))
note = None
if pick and pick["branches"] and not _board_key(pick["branches"]) and pick["branches"][-1][1] != "*":
note = "default core chosen under a build condition: " + ", ".join(
f"{v} {p}".strip() for v, p in pick["branches"])
rows.append({
"folder": folder,
"name": name,
"extensions": _extensions(values.get("SYSTEM_EXTENSION")),
"core": pick["core"] if pick else None,
"emulator": pick["emulator"] if pick else None,
"source_ref": "; ".join(refs),
"board_defaults": board_defaults,
"offers": _offers([
{"emulator": c["emulator"], "core": c["core"],
"when": ", ".join(f"{v} {p}".strip() for v, p in c["branches"]),
"source_ref": format_ref(key, package, c["line"])}
for c in mine
], pick["core"] if pick else None, pick["emulator"] if pick else None),
"arcade": values.get("SYSTEM_HARDWARE") == "arcade" or None,
"note": note,
})
return rows
# ---------------------------------------------------------------------------
# ES-DE (EmuDeck, RetroDECK)
_RA_CORE = re.compile(r"%CORE_RETROARCH%[/\\]([\w.+-]+?)_libretro")
_EMULATOR = re.compile(r"%EMULATOR_([A-Za-z0-9_-]+)%")
def command_core(command: str) -> tuple[str | None, str | None]:
match = _RA_CORE.search(command)
if match:
return "retroarch", match.group(1)
match = _EMULATOR.search(command)
if match:
name = match.group(1).lower()
return name, name
return None, None
def esde_systems(text: str, path: str) -> Iterator[tuple[str, str, list[str], list[tuple[str, str]]]]:
root = parse_untrusted_xml(_lenient_xml(text), path)
for system in root.iter("system"):
name = (system.findtext("name") or "").strip()
rom_path = (system.findtext("path") or "").strip()
commands = [((c.get("label") or "").strip(), (c.text or "").strip())
for c in system.findall("command")]
yield name, rom_path, _extensions(system.findtext("extension")), commands
def _alternative_emulators(text: str, function: str) -> dict[str, tuple[str, int]]:
"""`ESDE_setEmu '<label>' <system>` calls inside one function."""
out: dict[str, tuple[str, int]] = {}
inside = False
for number, line in enumerate(text.splitlines(), 1):
if re.search(rf"\b{re.escape(function)}\b\s*\(\)|function\s+{re.escape(function)}\b", line):
inside = True
continue
if not inside:
continue
if line.rstrip() == "}":
break
match = re.search(r"ESDE_setEmu\s+['\"]([^'\"]+)['\"]\s+([\w.-]+)", line)
if match:
out[match.group(2)] = (match.group(1), number)
return out
_ROMPATH_OPTIONS = ("-rompath", "-rp")
def _core_words(command: str) -> tuple[str, list[str]] | None:
"""The core and the words of the content RetroArch hands it.
ES-DE starts the libretro MAME core with a quoted command line as its
content (`-L .../mame_libretro.so "apple2e -rompath \\"...\\" ..."`): popen
runs it through the shell (es-core/src/utils/PlatformUtil.cpp:231, 257),
and the core splits the content on spaces and quotes when it is no file
(retro_init.cpp parse_cmdline, mmain2).
"""
core = _RA_CORE.search(command)
if core is None:
return None
try:
args = shlex.split(command)
at = args.index("-L")
content = shlex.split(args[at + 2]) if at + 2 < len(args) else []
except ValueError:
return None
return core.group(1), content
def _searched_by(commands: list[tuple[str, str, int | None]], key: str, path: str) -> list[dict]:
"""The ROM folders each emulator's commands pass it, by emulator and core.
ES-DE starts standalone MAME with `-rompath %GAMEDIR%\\;%ROMPATH%/<system>`
and a machine, or %BASENAME% where the game is the machine. The command
line outranks mame.ini. The libretro MAME core gets the same option in
the command line it is handed as content, after the machine. %GAMEDIR%
is the game's own folder, no folder a pack fills.
"""
found: dict[tuple[str, str], dict] = {}
for _label, command, line in commands:
libretro = _core_words(command)
if libretro is not None:
core, words = libretro
if words[:1] == [core]:
# A first word naming the core is dropped (mmain2).
words = words[1:]
name = "retroarch"
at = next((i for i, w in enumerate(words) if w in _ROMPATH_OPTIONS), None)
machine = words[0] if words and at else ""
else:
emulator = _EMULATOR.search(command)
if emulator is None:
continue
core, name = "", emulator.group(1).lower()
words = command.replace("\\;", ";").split()
at = next((i for i, w in enumerate(words) if w in _ROMPATH_OPTIONS), None)
machine = words[at + 2] if at is not None and at + 2 < len(words) else ""
if at is None or at + 1 >= len(words):
continue
reads = [
folder for part in words[at + 1].split(";")
if (folder := _strip_prefix(part, ["%ROMPATH%/", "%ROMPATH%\\"]))
]
if not reads:
continue
record = found.setdefault((name, core), {"emulator": name, "core": core, "reads": [],
"machines": [], "any_machine": False,
"lines": []})
record["reads"] += [r for r in reads if r not in record["reads"]]
if not machine or machine.startswith("%"):
record["any_machine"] = True
elif machine not in record["machines"]:
record["machines"].append(machine)
record["lines"].append(line)
rows = []
for record in found.values():
row = {"emulator": record["emulator"]}
if record["core"]:
row["core"] = record["core"]
row["reads"] = record["reads"]
if record["machines"]:
row["machines"] = record["machines"]
if record["any_machine"]:
row["any_machine"] = True
row["source_ref"] = format_ref(key, path, *record["lines"])
rows.append(row)
return rows
def _command_lines(text: str) -> dict[str, list[int]]:
"""Command text -> the lines it is written on, in file order."""
out: dict[str, list[int]] = {}
for number, line in enumerate(text.splitlines(), 1):
match = re.search(r"<command\b[^>]*>([^<]*)</command>", line)
if match:
out.setdefault(match.group(1).strip(), []).append(number)
return out
@register("esde")
def esde(spec: dict, fetcher: Fetcher) -> list[dict]:
"""Each system of ES-DE's list whose path is %ROMPATH%/<folder>.
The default emulator is the first <command> (FileData.cpp), unless an
alternativeEmulator label is written for the system: an installer that
sets one names a command of that system by its label. A custom system
file replaces the bundled definition of the same name.
"""
params = spec["params"]
systems: dict[str, dict] = {}
for item in [params["systems"], *params.get("custom", [])]:
key, path = item["source"], item["path"]
text = _lenient_xml(fetcher.text(key, path))
lines = _first_lines(text, r"<name>([^<]+)</name>")
command_lines = _command_lines(text)
for name, rom_path, extensions, commands in esde_systems(text, path):
folder = _strip_prefix(rom_path, ["%ROMPATH%/", "%ROMPATH%\\"])
if not name or not folder:
continue
start = lines.get(name) or 0
placed = [
(label, command, next(
(n for n in command_lines.get(command, []) if n >= start), None))
for label, command in commands
]
systems[name] = {
"folder": folder, "extensions": extensions, "commands": commands,
"ref": format_ref(key, path, lines.get(name)),
"searched_by": _searched_by(placed, key, path),
"placed": [(*c, key, path) for c in placed],
}
alternatives: dict[str, tuple[str, int]] = {}
alt = params.get("alternatives")
if alt:
alternatives = _alternative_emulators(fetcher.text(alt["source"], alt["path"]), alt["function"])
if not alternatives:
raise SourceError(f"{alt['path']}: no ESDE_setEmu call in {alt['function']}")
rows: list[dict] = []
for name, system in systems.items():
commands = system["commands"]
refs = [system["ref"]]
chosen = commands[0] if commands else ("", "")
note = None
if name in alternatives:
label, line = alternatives[name]
match = next((c for c in commands if c[0] == label), None)
refs.append(format_ref(alt["source"], alt["path"], line))
if match:
chosen = match
else:
note = f"alternative emulator '{label}' names no command of this system"
emulator, core = command_core(chosen[1])
offers = _offers([
{"emulator": e, "core": c, "source_ref": format_ref(key_, path_, line)}
for _label, command, line, key_, path_ in system["placed"]
for e, c in [command_core(command)] if e
], core, emulator)
extensions = system["extensions"]
if "." in extensions:
# ES-DE's "." is a file without an extension (SystemData.cpp
# getExtension), not a suffix the schema can hold.
extensions = [e for e in extensions if e != "."]
note = "; ".join(filter(None, [note, "files without an extension are games too ('.' in the list)"]))
rows.append({
"folder": system["folder"],
"name": name,
"extensions": extensions,
"core": core,
"emulator": emulator,
"source_ref": "; ".join(refs),
"note": note,
"offers": offers,
"searched_by": system["searched_by"] or None,
})
return rows
# ---------------------------------------------------------------------------
# Recalbox
# FolderData.cpp IsMatching: a list opening with files: holds whole file
# names, each bounded by a space or an end of the list.
_FILES_PREFIX = "files:"
def _recalbox_names(listed: str) -> list[str]:
"""The whole names of a files: list. A name may hold spaces
("prince of persia (1990).zip"): a list with a word that has no dot
is one name, not several."""
words = listed.split()
if words and all("." in word for word in words):
return words
return [listed.strip()] if listed.strip() else []
def _recalbox_boards(fetcher: Fetcher, key: str, configs_dir: str) -> dict[str, tuple[str, Defconfig]]:
"""Each board and the defconfig that builds it, with the lines of the
packages it enables."""
fetcher.cache_directory(key, configs_dir)
found = defconfig_boards(
(path, fetcher.text(key, path))
for path in fetcher.entries(key, configs_dir) if path.endswith("_defconfig")
)
if not found:
raise SourceError(f"{configs_dir}: no defconfig names a Recalbox target")
return found
@register("recalbox")
def recalbox(spec: dict, fetcher: Fetcher) -> list[dict]:
"""One system.ini per system, built once per board. A core is kept on
a board whose defconfig enables its `package`, and a system left with
none is not built there (romfs2). The default core is the lowest
`priority` (EmulatorManager), the last listed on a tie. The row names the
default most boards share, and `board_defaults` every board that differs.
A `.ext!` extension is an archive the frontend hands over unopened, and
a `files:` list names whole files instead of extensions."""
params = spec["params"]
key = params.get("source", DEFAULT_ROW_SOURCE)
boards = _recalbox_boards(fetcher, key, params["configs_dir"])
rows: list[dict] = []
fetcher.cache_directory(key, params["systems_dir"])
for entry in fetcher.entries(key, params["systems_dir"]):
path = f"{entry}/system.ini"
text = fetcher.optional_text(key, path)
if text is None:
continue
ini = read_system_ini(text)
sections, lines = ini.sections, ini.lines
system = sections.get("system", {})
folder = _strip_prefix(system.get("roms.folder", ""), ["%ROOT%/"])
if not folder:
continue
picks: dict[str, dict] = {}
extensions: set[str] = set()
names: set[str] = set()
for board, (config_path, config) in sorted(boards.items()):
enabled = config.enabled
cores = []
for section in ini.slots(board):
package, _ = ini.get(section, "package", board)
if package not in enabled:
continue
priority, priority_line = ini.get(section, "priority", board)
core, core_line = ini.get(section, "core", board)
emulator, _ = ini.get(section, "emulator", board)
exts, _ = ini.get(section, "extensions", board)
listed = exts or ""
if listed.startswith(_FILES_PREFIX):
names.update(_recalbox_names(listed[len(_FILES_PREFIX):]))
else:
extensions.update(listed.split())
cores.append({
"core": core, "emulator": emulator, "priority": int(priority or "255"),
"source_ref": "; ".join([
format_ref(key, path, priority_line, core_line),
format_ref(key, config_path, enabled[package]),
]),
})
if cores:
picks[board] = min(reversed(cores), key=lambda c: c["priority"])
if not picks:
continue
shared = Counter((pick["core"], pick["emulator"]) for pick in picks.values())
core, emulator = max(shared, key=lambda pair: shared[pair])
first = next(pick for pick in picks.values() if (pick["core"], pick["emulator"]) == (core, emulator))
board_defaults = {
board: _board(pick["core"], pick["emulator"], pick["source_ref"])
for board, pick in picks.items() if (pick["core"], pick["emulator"]) != (core, emulator)
}
archive = any(e.endswith("!") for e in extensions)
rows.append({
"folder": folder,
"name": system.get("name"),
"extensions": sorted({e.rstrip("!") for e in extensions}),
"files": sorted(names),
"core": core,
"emulator": emulator,
"source_ref": "; ".join([format_ref(key, path, lines.get("system/roms.folder")), first["source_ref"]]),
"board_defaults": board_defaults,
"arcade": sections.get("properties", {}).get("type") == "arcade" or None,
"note": "an extension ending in ! is an archive the frontend does not open" if archive else None,
})
return rows
# ---------------------------------------------------------------------------
# RetroPie
_ADD_EMULATOR = re.compile(r'^\s*addEmulator\s+(\S+)\s+("[^"]*"|\S+)\s+("[^"]*"|\S+)')
_LIST = re.compile(r'(?:local\s+)?(\w+)=\(([^)]*)\)')
_LOOP = re.compile(r'for (\w+) in (.+?); do')
def _emulator_calls(text: str) -> Iterator[tuple[int, str, str, list[str]]]:
"""(line, default flag, emulator id, systems) of each addEmulator call,
a system variable expanded through the loop or array that sets it."""
lines = text.splitlines()
arrays = {m.group(1): m.group(2).replace('"', "").split() for m in _LIST.finditer(text)}
loops: list[tuple[int, str, list[str]]] = []
for index, line in enumerate(lines):
match = _LOOP.search(line)
if match:
items = match.group(2).strip()
array = re.fullmatch(r'"?\$\{(\w+)\[@\]\}"?', items)
loops.append((index, match.group(1),
arrays.get(array.group(1), []) if array else items.replace('"', "").split()))
for index, line in enumerate(lines):
match = _ADD_EMULATOR.match(line)
if not match or "function addEmulator" in line:
continue
flag, emu, system = match.group(1), match.group(2).strip('"'), match.group(3).strip('"')
systems = [system]
var = re.fullmatch(r"\$\{?(\w+)\}?", system)
if var:
found = [items for i, name, items in loops if i < index and name == var.group(1)]
if found:
systems = found[-1]
elif var.group(1) in arrays:
systems = arrays[var.group(1)]
else:
continue
yield index + 1, flag, emu, systems
@register("retropie")
def retropie(spec: dict, fetcher: Fetcher) -> list[dict]:
"""Folders are `<system>` under the roms root (helpers.sh addSystem),
with `<system>_exts` from platforms.cfg. The default is set once, by
the first installed module whose addEmulator passes 1: a basic install
installs the core then the main section in registration order
(setup.sh basic_install_setup, packages.sh rp_registerAllModules). A
system no basic-install module serves gets its folder from an optional
module, so the first opt, then exp, module stands as its default."""
params = spec["params"]
key = params.get("source", DEFAULT_ROW_SOURCE)
cfg_path = params["platforms_cfg"]
cfg = fetcher.text(key, cfg_path)
exts: dict[str, tuple[str, int]] = {}
for number, cfg_line in enumerate(cfg.splitlines(), 1):
match = re.match(r'^([\w-]+)_exts="([^"]*)"', cfg_line)
if match:
exts[match.group(1)] = (match.group(2), number)
platform = PLATFORM_FLAGS[params["platform"]]
modules: list[dict] = []
for kind in params["module_dirs"]:
for path in fetcher.tree(key, f"scriptmodules/{kind}/"):
if not path.endswith(".sh") or path.count("/") != 2:
continue
text = fetcher.text(key, path)
fields = module_fields(text)
if not module_enabled(fields.get("flags", "").split(), platform):
continue
modules.append({"path": path, "id": fields.get("id", ""),
"section": fields.get("section", ""), "text": text})
order = [m for section in params["install_sections"] for m in modules if m["section"] == section]
defaults: dict[str, tuple[str, str, int]] = {}
for module in order:
for line, flag, emu, systems in _emulator_calls(module["text"]):
if flag != "1":
continue
emu = emu.replace("${md_id}", module["id"]).replace("$md_id", module["id"])
for system in systems:
defaults.setdefault(system, (emu, module["path"], line))
rows: list[dict] = []
for system, (raw, line) in sorted(exts.items()):
refs = [format_ref(key, cfg_path, line)]
core = None
if system in defaults:
core, path, call = defaults[system]
refs.append(format_ref(key, path, call))
emulator = "retroarch" if core and core.startswith("lr-") else core
rows.append({
"folder": system,
"extensions": _extensions(raw),
"core": core,
"emulator": emulator,
"source_ref": "; ".join(refs),
})
return rows
+232
View File
@@ -0,0 +1,232 @@
"""ES-DE: es_systems.xml with the alternative emulators and the commands an installer writes over it."""
from __future__ import annotations
import re
import shlex
from collections.abc import Iterator
from model.romfolders import format_ref
from model.safeparse import parse_untrusted_xml
from . import Fetcher, SourceError, register
from .es import _extensions, _first_lines, _lenient_xml, _offers, _strip_prefix
# ---------------------------------------------------------------------------
# ES-DE (EmuDeck, RetroDECK)
_RA_CORE = re.compile(r"%CORE_RETROARCH%[/\\]([\w.+-]+?)_libretro")
_EMULATOR = re.compile(r"%EMULATOR_([A-Za-z0-9_-]+)%")
def command_core(command: str) -> tuple[str | None, str | None]:
match = _RA_CORE.search(command)
if match:
return "retroarch", match.group(1)
match = _EMULATOR.search(command)
if match:
name = match.group(1).lower()
return name, name
return None, None
def esde_systems(text: str, path: str) -> Iterator[tuple[str, str, list[str], list[tuple[str, str]]]]:
root = parse_untrusted_xml(_lenient_xml(text), path)
for system in root.iter("system"):
name = (system.findtext("name") or "").strip()
rom_path = (system.findtext("path") or "").strip()
commands = [((c.get("label") or "").strip(), (c.text or "").strip())
for c in system.findall("command")]
yield name, rom_path, _extensions(system.findtext("extension")), commands
def _alternative_emulators(text: str, function: str) -> dict[str, tuple[str, int]]:
"""`ESDE_setEmu '<label>' <system>` calls inside one function."""
out: dict[str, tuple[str, int]] = {}
inside = False
for number, line in enumerate(text.splitlines(), 1):
if re.search(rf"\b{re.escape(function)}\b\s*\(\)|function\s+{re.escape(function)}\b", line):
inside = True
continue
if not inside:
continue
if line.rstrip() == "}":
break
match = re.search(r"ESDE_setEmu\s+['\"]([^'\"]+)['\"]\s+([\w.-]+)", line)
if match:
out[match.group(2)] = (match.group(1), number)
return out
_ROMPATH_OPTIONS = ("-rompath", "-rp")
def _core_words(command: str) -> tuple[str, list[str]] | None:
"""The core and the words of the content RetroArch hands it.
ES-DE starts the libretro MAME core with a quoted command line as its
content (`-L .../mame_libretro.so "apple2e -rompath \\"...\\" ..."`): popen
runs it through the shell (es-core/src/utils/PlatformUtil.cpp:231, 257),
and the core splits the content on spaces and quotes when it is no file
(retro_init.cpp parse_cmdline, mmain2).
"""
core = _RA_CORE.search(command)
if core is None:
return None
try:
args = shlex.split(command)
at = args.index("-L")
content = shlex.split(args[at + 2]) if at + 2 < len(args) else []
except ValueError:
return None
return core.group(1), content
def _searched_by(commands: list[tuple[str, str, int | None]], key: str, path: str) -> list[dict]:
"""The ROM folders each emulator's commands pass it, by emulator and core.
ES-DE starts standalone MAME with `-rompath %GAMEDIR%\\;%ROMPATH%/<system>`
and a machine, or %BASENAME% where the game is the machine. The command
line outranks mame.ini. The libretro MAME core gets the same option in
the command line it is handed as content, after the machine. %GAMEDIR%
is the game's own folder, no folder a pack fills.
"""
found: dict[tuple[str, str], dict] = {}
for _label, command, line in commands:
libretro = _core_words(command)
if libretro is not None:
core, words = libretro
if words[:1] == [core]:
# A first word naming the core is dropped (mmain2).
words = words[1:]
name = "retroarch"
at = next((i for i, w in enumerate(words) if w in _ROMPATH_OPTIONS), None)
machine = words[0] if words and at else ""
else:
emulator = _EMULATOR.search(command)
if emulator is None:
continue
core, name = "", emulator.group(1).lower()
words = command.replace("\\;", ";").split()
at = next((i for i, w in enumerate(words) if w in _ROMPATH_OPTIONS), None)
machine = words[at + 2] if at is not None and at + 2 < len(words) else ""
if at is None or at + 1 >= len(words):
continue
reads = [
folder for part in words[at + 1].split(";")
if (folder := _strip_prefix(part, ["%ROMPATH%/", "%ROMPATH%\\"]))
]
if not reads:
continue
record = found.setdefault((name, core), {"emulator": name, "core": core, "reads": [],
"machines": [], "any_machine": False,
"lines": []})
record["reads"] += [r for r in reads if r not in record["reads"]]
if not machine or machine.startswith("%"):
record["any_machine"] = True
elif machine not in record["machines"]:
record["machines"].append(machine)
record["lines"].append(line)
rows = []
for record in found.values():
row = {"emulator": record["emulator"]}
if record["core"]:
row["core"] = record["core"]
row["reads"] = record["reads"]
if record["machines"]:
row["machines"] = record["machines"]
if record["any_machine"]:
row["any_machine"] = True
row["source_ref"] = format_ref(key, path, *record["lines"])
rows.append(row)
return rows
def _command_lines(text: str) -> dict[str, list[int]]:
"""Command text -> the lines it is written on, in file order."""
out: dict[str, list[int]] = {}
for number, line in enumerate(text.splitlines(), 1):
match = re.search(r"<command\b[^>]*>([^<]*)</command>", line)
if match:
out.setdefault(match.group(1).strip(), []).append(number)
return out
@register("esde")
def esde(spec: dict, fetcher: Fetcher) -> list[dict]:
"""Each system of ES-DE's list whose path is %ROMPATH%/<folder>.
The default emulator is the first <command> (FileData.cpp), unless an
alternativeEmulator label is written for the system: an installer that
sets one names a command of that system by its label. A custom system
file replaces the bundled definition of the same name.
"""
params = spec["params"]
systems: dict[str, dict] = {}
for item in [params["systems"], *params.get("custom", [])]:
key, path = item["source"], item["path"]
text = _lenient_xml(fetcher.text(key, path))
lines = _first_lines(text, r"<name>([^<]+)</name>")
command_lines = _command_lines(text)
for name, rom_path, extensions, commands in esde_systems(text, path):
folder = _strip_prefix(rom_path, ["%ROMPATH%/", "%ROMPATH%\\"])
if not name or not folder:
continue
start = lines.get(name) or 0
placed = [
(label, command, next(
(n for n in command_lines.get(command, []) if n >= start), None))
for label, command in commands
]
systems[name] = {
"folder": folder, "extensions": extensions, "commands": commands,
"ref": format_ref(key, path, lines.get(name)),
"searched_by": _searched_by(placed, key, path),
"placed": [(*c, key, path) for c in placed],
}
alternatives: dict[str, tuple[str, int]] = {}
alt = params.get("alternatives")
if alt:
alternatives = _alternative_emulators(fetcher.text(alt["source"], alt["path"]), alt["function"])
if not alternatives:
raise SourceError(f"{alt['path']}: no ESDE_setEmu call in {alt['function']}")
rows: list[dict] = []
for name, system in systems.items():
commands = system["commands"]
refs = [system["ref"]]
chosen = commands[0] if commands else ("", "")
note = None
if name in alternatives:
label, line = alternatives[name]
match = next((c for c in commands if c[0] == label), None)
refs.append(format_ref(alt["source"], alt["path"], line))
if match:
chosen = match
else:
note = f"alternative emulator '{label}' names no command of this system"
emulator, core = command_core(chosen[1])
offers = _offers([
{"emulator": e, "core": c, "source_ref": format_ref(key_, path_, line)}
for _label, command, line, key_, path_ in system["placed"]
for e, c in [command_core(command)] if e
], core, emulator)
extensions = system["extensions"]
if "." in extensions:
# ES-DE's "." is a file without an extension (SystemData.cpp
# getExtension), not a suffix the schema can hold.
extensions = [e for e in extensions if e != "."]
note = "; ".join(filter(None, [note, "files without an extension are games too ('.' in the list)"]))
rows.append({
"folder": system["folder"],
"name": name,
"extensions": extensions,
"core": core,
"emulator": emulator,
"source_ref": "; ".join(refs),
"note": note,
"offers": offers,
"searched_by": system["searched_by"] or None,
})
return rows
+388
View File
@@ -0,0 +1,388 @@
"""Folder lists built from package scripts: elec-conf emulator packages, Recalbox system.ini, RetroPie scriptmodules."""
from __future__ import annotations
import re
import shlex
from collections import Counter
from collections.abc import Iterator
from model.romfolders import DEFAULT_ROW_SOURCE, format_ref
from ..recalbox_romfs import Defconfig, defconfig_boards, read_system_ini
from ..retropie_modules import PLATFORM_FLAGS, module_enabled, module_fields
from . import Fetcher, SourceError, register
from .es import _board, _extensions, _offers, _strip_prefix
# ---------------------------------------------------------------------------
# ROCKNIX family: config/emulators/<system>.conf and the emulators package
def _conf_values(text: str) -> tuple[dict[str, str], dict[str, int]]:
values: dict[str, str] = {}
lines: dict[str, int] = {}
for number, line in enumerate(text.splitlines(), 1):
match = re.match(r"^\s*(SYSTEM_[A-Z_]+)=(.*)$", line)
if not match:
continue
raw = match.group(2).strip()
try:
value = " ".join(shlex.split(raw))
except ValueError:
value = raw.strip('"')
values[match.group(1)] = value
lines[match.group(1)] = number
return values, lines
def _nesting(line: str, stack: list[list[str]]) -> bool:
"""Follow case and if nesting. True when the line is consumed."""
match = re.match(r'case\s+"?\$\{?(\w+)\}?"?\s+in', line)
if match:
stack.append(["case", match.group(1), ""])
return True
if line == "esac":
stack.pop()
return True
if line.startswith("if ") or line.startswith("[") and "&&" not in line and line.endswith("then"):
stack.append(["if", line, ""])
return True
if line in ("fi",):
if stack and stack[-1][0] == "if":
stack.pop()
return True
return line in ("then", "else") or line.startswith("elif ")
def _record_call(
words: list[str], number: int, stack: list[list[str]],
cores: list[dict], systems: dict[str, list[int]],
) -> None:
branches = [(s[1], s[2]) for s in stack]
if words[:1] == ["add_emu_core"] and len(words) >= 4:
cores.append({
"system": words[1], "emulator": words[2], "core": words[3],
"default": len(words) > 4 and words[4] == "true",
"branches": branches, "line": number,
})
elif words[:1] == ["add_es_system"] and len(words) >= 2:
systems.setdefault(words[1], []).append(number)
def _package_calls(text: str) -> tuple[list[dict], dict[str, list[int]]]:
"""add_emu_core and add_es_system calls of makeinstall_target, each
with the `case` branches it sits under as (variable, pattern) pairs.
An `if` block nests a condition whose truth the build decides: its
calls carry it as a branch the default choice does not count as
unconditional."""
cores: list[dict] = []
systems: dict[str, list[int]] = {}
stack: list[list[str]] = []
inside = False
for number, raw in enumerate(text.splitlines(), 1):
line = raw.split("#", 1)[0].strip()
if raw.startswith("makeinstall_target()"):
inside = True
continue
if not inside or not line:
continue
if raw.startswith("}"):
break
if _nesting(line, stack):
continue
ends = line.endswith(";;")
if ends:
line = line[:-2].strip()
if stack and stack[-1][0] == "case":
match = re.match(r"^([^()\s][^()]*)\)\s*(.*)$", line)
if match and not line.startswith(("add_", "[", "sed", "cp", "mkdir")):
stack[-1][2] = match.group(1).strip()
line = match.group(2).strip()
_record_call(line.split(), number, stack, cores, systems)
if ends and stack and stack[-1][0] == "case":
stack[-1][2] = ""
return cores, systems
def _board_key(branches: list[tuple[str, str]]) -> str | None:
"""A default chosen under one `case` on the device names that device."""
if len(branches) == 1 and branches[0][1] and branches[0][1] != "*":
variable, pattern = branches[0]
if not variable.startswith("["):
return f"{variable}={pattern}"
return None
@register("elec-conf")
def elec_conf(spec: dict, fetcher: Fetcher) -> list[dict]:
"""A system ES lists is one add_es_system writes from its conf file
(SYSTEM_PATH, SYSTEM_EXTENSION). The default is the core whose
add_emu_core says true: unconditional, else the `*)` branch of a
device case, the branches for named devices going to board_defaults."""
params = spec["params"]
key = params.get("source", DEFAULT_ROW_SOURCE)
package = params["package"]
cores, systems = _package_calls(fetcher.text(key, package))
if not systems:
raise SourceError(f"{package}: no add_es_system call")
conf_dir = params["conf_dir"]
rows: list[dict] = []
for name, lines in systems.items():
conf_path = f"{conf_dir}/{name}.conf"
conf_text = fetcher.optional_text(key, conf_path)
if conf_text is None:
continue
values, value_lines = _conf_values(conf_text)
folder = _strip_prefix(values.get("SYSTEM_PATH", ""), params["path_prefixes"])
if not folder:
continue
mine = [c for c in cores if c["system"] == name]
flagged = [c for c in mine if c["default"]]
plain = [c for c in flagged if not c["branches"]]
star = [c for c in flagged if c["branches"] and c["branches"][-1][1] == "*"]
candidates = plain or star or flagged
pick: dict | None = candidates[0] if candidates else None
if pick is None:
pick = next((c for c in mine if not c["branches"]), mine[0] if mine else None)
board_defaults = {}
for c in flagged:
board = _board_key(c["branches"])
if c is not pick and board and (c["emulator"], c["core"]) != (
pick and pick["emulator"], pick and pick["core"]):
board_defaults[board] = _board(c["core"], c["emulator"], format_ref(key, package, c["line"]))
refs = [
format_ref(key, conf_path, value_lines.get("SYSTEM_PATH"), value_lines.get("SYSTEM_EXTENSION")),
format_ref(key, package, *lines),
]
if pick:
refs.append(format_ref(key, package, pick["line"]))
note = None
if pick and pick["branches"] and not _board_key(pick["branches"]) and pick["branches"][-1][1] != "*":
note = "default core chosen under a build condition: " + ", ".join(
f"{v} {p}".strip() for v, p in pick["branches"])
rows.append({
"folder": folder,
"name": name,
"extensions": _extensions(values.get("SYSTEM_EXTENSION")),
"core": pick["core"] if pick else None,
"emulator": pick["emulator"] if pick else None,
"source_ref": "; ".join(refs),
"board_defaults": board_defaults,
"offers": _offers([
{"emulator": c["emulator"], "core": c["core"],
"when": ", ".join(f"{v} {p}".strip() for v, p in c["branches"]),
"source_ref": format_ref(key, package, c["line"])}
for c in mine
], pick["core"] if pick else None, pick["emulator"] if pick else None),
"arcade": values.get("SYSTEM_HARDWARE") == "arcade" or None,
"note": note,
})
return rows
# ---------------------------------------------------------------------------
# Recalbox
# FolderData.cpp IsMatching: a list opening with files: holds whole file
# names, each bounded by a space or an end of the list.
_FILES_PREFIX = "files:"
def _recalbox_names(listed: str) -> list[str]:
"""The whole names of a files: list. A name may hold spaces
("prince of persia (1990).zip"): a list with a word that has no dot
is one name, not several."""
words = listed.split()
if words and all("." in word for word in words):
return words
return [listed.strip()] if listed.strip() else []
def _recalbox_boards(fetcher: Fetcher, key: str, configs_dir: str) -> dict[str, tuple[str, Defconfig]]:
"""Each board and the defconfig that builds it, with the lines of the
packages it enables."""
fetcher.cache_directory(key, configs_dir)
found = defconfig_boards(
(path, fetcher.text(key, path))
for path in fetcher.entries(key, configs_dir) if path.endswith("_defconfig")
)
if not found:
raise SourceError(f"{configs_dir}: no defconfig names a Recalbox target")
return found
@register("recalbox")
def recalbox(spec: dict, fetcher: Fetcher) -> list[dict]:
"""One system.ini per system, built once per board. A core is kept on
a board whose defconfig enables its `package`, and a system left with
none is not built there (romfs2). The default core is the lowest
`priority` (EmulatorManager), the last listed on a tie. The row names the
default most boards share, and `board_defaults` every board that differs.
A `.ext!` extension is an archive the frontend hands over unopened, and
a `files:` list names whole files instead of extensions."""
params = spec["params"]
key = params.get("source", DEFAULT_ROW_SOURCE)
boards = _recalbox_boards(fetcher, key, params["configs_dir"])
rows: list[dict] = []
fetcher.cache_directory(key, params["systems_dir"])
for entry in fetcher.entries(key, params["systems_dir"]):
path = f"{entry}/system.ini"
text = fetcher.optional_text(key, path)
if text is None:
continue
ini = read_system_ini(text)
sections, lines = ini.sections, ini.lines
system = sections.get("system", {})
folder = _strip_prefix(system.get("roms.folder", ""), ["%ROOT%/"])
if not folder:
continue
picks: dict[str, dict] = {}
extensions: set[str] = set()
names: set[str] = set()
for board, (config_path, config) in sorted(boards.items()):
enabled = config.enabled
cores = []
for section in ini.slots(board):
package, _ = ini.get(section, "package", board)
if package not in enabled:
continue
priority, priority_line = ini.get(section, "priority", board)
core, core_line = ini.get(section, "core", board)
emulator, _ = ini.get(section, "emulator", board)
exts, _ = ini.get(section, "extensions", board)
listed = exts or ""
if listed.startswith(_FILES_PREFIX):
names.update(_recalbox_names(listed[len(_FILES_PREFIX):]))
else:
extensions.update(listed.split())
cores.append({
"core": core, "emulator": emulator, "priority": int(priority or "255"),
"source_ref": "; ".join([
format_ref(key, path, priority_line, core_line),
format_ref(key, config_path, enabled[package]),
]),
})
if cores:
picks[board] = min(reversed(cores), key=lambda c: c["priority"])
if not picks:
continue
shared = Counter((pick["core"], pick["emulator"]) for pick in picks.values())
core, emulator = max(shared, key=lambda pair: shared[pair])
first = next(pick for pick in picks.values() if (pick["core"], pick["emulator"]) == (core, emulator))
board_defaults = {
board: _board(pick["core"], pick["emulator"], pick["source_ref"])
for board, pick in picks.items() if (pick["core"], pick["emulator"]) != (core, emulator)
}
archive = any(e.endswith("!") for e in extensions)
rows.append({
"folder": folder,
"name": system.get("name"),
"extensions": sorted({e.rstrip("!") for e in extensions}),
"files": sorted(names),
"core": core,
"emulator": emulator,
"source_ref": "; ".join([format_ref(key, path, lines.get("system/roms.folder")), first["source_ref"]]),
"board_defaults": board_defaults,
"arcade": sections.get("properties", {}).get("type") == "arcade" or None,
"note": "an extension ending in ! is an archive the frontend does not open" if archive else None,
})
return rows
# ---------------------------------------------------------------------------
# RetroPie
_ADD_EMULATOR = re.compile(r'^\s*addEmulator\s+(\S+)\s+("[^"]*"|\S+)\s+("[^"]*"|\S+)')
_LIST = re.compile(r'(?:local\s+)?(\w+)=\(([^)]*)\)')
_LOOP = re.compile(r'for (\w+) in (.+?); do')
def _emulator_calls(text: str) -> Iterator[tuple[int, str, str, list[str]]]:
"""(line, default flag, emulator id, systems) of each addEmulator call,
a system variable expanded through the loop or array that sets it."""
lines = text.splitlines()
arrays = {m.group(1): m.group(2).replace('"', "").split() for m in _LIST.finditer(text)}
loops: list[tuple[int, str, list[str]]] = []
for index, line in enumerate(lines):
match = _LOOP.search(line)
if match:
items = match.group(2).strip()
array = re.fullmatch(r'"?\$\{(\w+)\[@\]\}"?', items)
loops.append((index, match.group(1),
arrays.get(array.group(1), []) if array else items.replace('"', "").split()))
for index, line in enumerate(lines):
match = _ADD_EMULATOR.match(line)
if not match or "function addEmulator" in line:
continue
flag, emu, system = match.group(1), match.group(2).strip('"'), match.group(3).strip('"')
systems = [system]
var = re.fullmatch(r"\$\{?(\w+)\}?", system)
if var:
found = [items for i, name, items in loops if i < index and name == var.group(1)]
if found:
systems = found[-1]
elif var.group(1) in arrays:
systems = arrays[var.group(1)]
else:
continue
yield index + 1, flag, emu, systems
@register("retropie")
def retropie(spec: dict, fetcher: Fetcher) -> list[dict]:
"""Folders are `<system>` under the roms root (helpers.sh addSystem),
with `<system>_exts` from platforms.cfg. The default is set once, by
the first installed module whose addEmulator passes 1: a basic install
installs the core then the main section in registration order
(setup.sh basic_install_setup, packages.sh rp_registerAllModules). A
system no basic-install module serves gets its folder from an optional
module, so the first opt, then exp, module stands as its default."""
params = spec["params"]
key = params.get("source", DEFAULT_ROW_SOURCE)
cfg_path = params["platforms_cfg"]
cfg = fetcher.text(key, cfg_path)
exts: dict[str, tuple[str, int]] = {}
for number, cfg_line in enumerate(cfg.splitlines(), 1):
match = re.match(r'^([\w-]+)_exts="([^"]*)"', cfg_line)
if match:
exts[match.group(1)] = (match.group(2), number)
platform = PLATFORM_FLAGS[params["platform"]]
modules: list[dict] = []
for kind in params["module_dirs"]:
for path in fetcher.tree(key, f"scriptmodules/{kind}/"):
if not path.endswith(".sh") or path.count("/") != 2:
continue
text = fetcher.text(key, path)
fields = module_fields(text)
if not module_enabled(fields.get("flags", "").split(), platform):
continue
modules.append({"path": path, "id": fields.get("id", ""),
"section": fields.get("section", ""), "text": text})
order = [m for section in params["install_sections"] for m in modules if m["section"] == section]
defaults: dict[str, tuple[str, str, int]] = {}
for module in order:
for line, flag, emu, systems in _emulator_calls(module["text"]):
if flag != "1":
continue
emu = emu.replace("${md_id}", module["id"]).replace("$md_id", module["id"])
for system in systems:
defaults.setdefault(system, (emu, module["path"], line))
rows: list[dict] = []
for system, (raw, line) in sorted(exts.items()):
refs = [format_ref(key, cfg_path, line)]
core = None
if system in defaults:
core, path, call = defaults[system]
refs.append(format_ref(key, path, call))
emulator = "retroarch" if core and core.startswith("lr-") else core
rows.append({
"folder": system,
"extensions": _extensions(raw),
"core": core,
"emulator": emulator,
"source_ref": "; ".join(refs),
})
return rows