refactor: split games into index, regions, folders

This commit is contained in:
Abdessamad Derraz committed 2026-11-24 06:21:13 +01:00
1 parent 9098fba1bf
commit 935eab63e2
14 files changed
+990 -948

No files matched your search

+1 -1
View File
@@ -16,7 +16,7 @@
# and Invoke-WebRequest throws on a cut transfer before the hash is read:
# the site's build rewrites this line there (scripts/sitebootstrap.py).
$mismatchAdvice = "The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that."
$defaultInstallSha256 = "7b6e3083d33d9b2fd7735df41ba56a0e3f7c1e98cf56327277566a0da05d6464"
$defaultInstallSha256 = "e62e2621eccc43ddbc56b9227db16ef5ddf918d8846c15ff0d6fbbe9e059da98"
# The pin above refuses any changed byte: this cap only stops a body that
# cannot be the installer (a sign-in portal's page) before it is hashed.
$maximumInstallerBytes = 8MB
+201 -195
View File
@@ -8807,6 +8807,129 @@ def read_game(facts: GameFacts, system_id: str, system: SystemData, readers: set
_read_game_headers(facts, system, wanted, packed)
# --- installer/game_regions.py -----------------------------------------------
def core_regions(facts: GameFacts, system_id: str, system: SystemData,
profile: str) -> frozenset | None:
"""The regions a game is proven to need under one core: the catalog's
tag of the content matched, the region the core's own readers compute
from its bytes, or both when they agree. None for no proof or for two
proofs that disagree."""
if facts.system != system_id:
return None
core = system.cores.get(profile) or {}
header: frozenset | None = None
order = [name for name in core.get("order") or [] if isinstance(name, str)]
all_read = bool(order)
for name in order:
value = facts.readings.get(name)
if value is None:
all_read = False
continue
if isinstance(value, frozenset):
header = value
break
otherwise = core.get("otherwise")
if header is None and all_read and isinstance(otherwise, list) and otherwise:
header = frozenset(str(region) for region in otherwise)
proofs = [proof for proof in (facts.dat_regions, header) if proof]
if not proofs:
return None
if len(proofs) == 2 and not any(_region_comparable(a, b)
for a in proofs[0] for b in proofs[1]):
return None
return frozenset().union(*proofs)
# Cases of games/regions.yml a proven region does not settle whatever the
# game: device state (an option, a per-game setting) or the code itself.
_ALWAYS_WIDE = ("always", "option", "user-state")
def region_rule_keeps_all(rule: dict | None, games: list[GameFacts]) -> bool:
"""Whether the core keeps every region whatever its games prove: no rule
read at its pin, or one of the cases games/regions.yml names (a region
option, an override BIOS, a fallback across regions, a disc database)."""
if not isinstance(rule, dict):
return True
for case in rule.get("keep_all_regions") or []:
if not isinstance(case, dict):
return True
name, condition = case.get("case"), case.get("condition")
if condition in _ALWAYS_WIDE:
return True
if name == "first-disc-region":
if any(_suffix(game.path.name) == ".m3u" for game in games):
return True
elif name not in ("multi-area-disc", "gamecube-unknown-region", "saturn-kr-ipl"):
return True
return False
def needed_regions(rule: dict, proofs: list[frozenset | None]) -> frozenset | None:
"""The regions the games of one core prove, routed as the core routes
them, None when one game proves none."""
if not proofs or any(proof is None for proof in proofs):
return None
routes = rule.get("routes") or {}
needed = frozenset(str(routes.get(region, region))
for proof in proofs for region in proof or ())
wide = {str(case.get("case")) for case in rule.get("keep_all_regions") or []
if isinstance(case, dict)}
if "saturn-kr-ipl" in wide and needed & {"asia-ntsc", "south-korea"}:
return None
return needed
def keep_regions(dests: list[str], entries: dict[str, dict], needed: frozenset,
region_free: set[str]) -> list[str]:
"""The files of a set the proven regions keep: every file with no region
tag, a world or region-free file, and those whose tag a needed region
matches. A needed region no file matches keeps the whole set."""
def regions(dest: str) -> list[str]:
entry = entries.get(dest) or {}
if Path(dest).name.casefold() in region_free:
return []
tags = [str(r) for r in entry.get("regions") or []]
return [] if WORLD_REGION in tags else tags
tagged = {dest: regions(dest) for dest in dests if regions(dest)}
for region in needed:
if not any(_region_comparable(region, tag) for tags in tagged.values() for tag in tags):
return list(dests)
return [dest for dest in dests
if dest not in tagged
or any(_region_comparable(region, tag) for region in needed for tag in tagged[dest])]
def _region_kept(files: list[str], games: list[GameFacts], entries: dict[str, dict],
system: str, info: SystemData,
profile: str) -> tuple[list[str], bool, frozenset | None]:
"""The files of a set the games' regions keep, whether every region is
kept, and the regions it was narrowed to (None when not narrowed)."""
rule = info.rules.get(profile)
region_free = {str(name).casefold() for name in (rule or {}).get("region_free") or []}
tagged = any((entries.get(dest) or {}).get("regions") for dest in files)
if not tagged:
return list(files), False, None
if region_rule_keeps_all(rule, games):
return list(files), True, None
proofs = [core_regions(game, system, info, profile) for game in games]
needed = needed_regions(rule or {}, proofs)
if needed is None:
for game, proof in zip(games, proofs):
if proof is None and not game.reason:
game.reason = ("not_read" if not game.read
else "not_recognised" if not game.recognised
else "region_unknown")
return list(files), True, None
kept = keep_regions(files, entries, needed, region_free)
if len(kept) < len(files):
return kept, False, needed
return kept, True, None
# --- installer/lists.py ------------------------------------------------------
NETWORK_ADVICE = (" Check the connection, and HTTPS_PROXY if this network needs a "
@@ -19142,7 +19265,7 @@ def finish_app(
sys.exit(1)
# --- installer/games.py ------------------------------------------------------
# --- installer/games_index.py ------------------------------------------------
# --- Games on the card -------------------------------------------------------
#
@@ -19354,99 +19477,7 @@ def games_setups(index: dict, plat: str, bios_path: Path, os_type: str = "") ->
return found
def core_regions(facts: GameFacts, system_id: str, system: SystemData,
profile: str) -> frozenset | None:
"""The regions a game is proven to need under one core: the catalog's
tag of the content matched, the region the core's own readers compute
from its bytes, or both when they agree. None for no proof or for two
proofs that disagree."""
if facts.system != system_id:
return None
core = system.cores.get(profile) or {}
header: frozenset | None = None
order = [name for name in core.get("order") or [] if isinstance(name, str)]
all_read = bool(order)
for name in order:
value = facts.readings.get(name)
if value is None:
all_read = False
continue
if isinstance(value, frozenset):
header = value
break
otherwise = core.get("otherwise")
if header is None and all_read and isinstance(otherwise, list) and otherwise:
header = frozenset(str(region) for region in otherwise)
proofs = [proof for proof in (facts.dat_regions, header) if proof]
if not proofs:
return None
if len(proofs) == 2 and not any(_region_comparable(a, b)
for a in proofs[0] for b in proofs[1]):
return None
return frozenset().union(*proofs)
# Cases of games/regions.yml a proven region does not settle whatever the
# game: device state (an option, a per-game setting) or the code itself.
_ALWAYS_WIDE = ("always", "option", "user-state")
def region_rule_keeps_all(rule: dict | None, games: list[GameFacts]) -> bool:
"""Whether the core keeps every region whatever its games prove: no rule
read at its pin, or one of the cases games/regions.yml names (a region
option, an override BIOS, a fallback across regions, a disc database)."""
if not isinstance(rule, dict):
return True
for case in rule.get("keep_all_regions") or []:
if not isinstance(case, dict):
return True
name, condition = case.get("case"), case.get("condition")
if condition in _ALWAYS_WIDE:
return True
if name == "first-disc-region":
if any(_suffix(game.path.name) == ".m3u" for game in games):
return True
elif name not in ("multi-area-disc", "gamecube-unknown-region", "saturn-kr-ipl"):
return True
return False
def needed_regions(rule: dict, proofs: list[frozenset | None]) -> frozenset | None:
"""The regions the games of one core prove, routed as the core routes
them, None when one game proves none."""
if not proofs or any(proof is None for proof in proofs):
return None
routes = rule.get("routes") or {}
needed = frozenset(str(routes.get(region, region))
for proof in proofs for region in proof or ())
wide = {str(case.get("case")) for case in rule.get("keep_all_regions") or []
if isinstance(case, dict)}
if "saturn-kr-ipl" in wide and needed & {"asia-ntsc", "south-korea"}:
return None
return needed
def keep_regions(dests: list[str], entries: dict[str, dict], needed: frozenset,
region_free: set[str]) -> list[str]:
"""The files of a set the proven regions keep: every file with no region
tag, a world or region-free file, and those whose tag a needed region
matches. A needed region no file matches keeps the whole set."""
def regions(dest: str) -> list[str]:
entry = entries.get(dest) or {}
if Path(dest).name.casefold() in region_free:
return []
tags = [str(r) for r in entry.get("regions") or []]
return [] if WORLD_REGION in tags else tags
tagged = {dest: regions(dest) for dest in dests if regions(dest)}
for region in needed:
if not any(_region_comparable(region, tag) for tags in tagged.values() for tag in tags):
return list(dests)
return [dest for dest in dests
if dest not in tagged
or any(_region_comparable(region, tag) for region in needed for tag in tagged[dest])]
# --- installer/game_folders.py -----------------------------------------------
class GamesPlan:
"""What the games of a card need, and what was read to say so."""
@@ -19553,80 +19584,6 @@ def _named_dests(names: Iterable[str], pool: Iterable[str]) -> list[str]:
return [dest for dest in pool if Path(dest).name.casefold() in wanted]
def plan_games(plat: str, bios_path: Path, manifest: dict, folder: Path | None = None,
term: Terminal | None = None, data: GamesData | None = None,
os_type: str = "", network: bool = True) -> GamesPlan | None:
"""What the games of the card (or of folder) need, from install/games/.
None when the card has no rom folder this setup reads and no folder was
named. Raises GamesUnavailable when the index cannot be read, or does
not describe this list (a destination it names is not in the list).
"""
data = data or GAMES
index = data.load_index(network)
fallback = data.doc("fallback", plat, network)
if fallback is None:
raise GamesUnavailable("no_sets", f"install/games/ holds no BIOS sets for {plat}")
setup, root = _games_setup_root(data, index, plat, bios_path, folder, os_type, network)
if root is None:
return None
folders = game_folders(setup, index, root, folder is not None)
plan = GamesPlan(root, folder is None)
entries = {str(entry.get("dest")): entry for entry in manifest.get("files") or []}
listed = set(entries)
found = _found_games(plan, folders, setup)
docs = _GamesDocs(data, network)
cache_root = cache_folder()
cache = _ScanCache(cache_root / GAMES_SCAN_FILE if cache_root is not None else None)
progress = ReadCounter(sum(len(games) for _folder, games in found), term, "games_reading",
" games")
read = [(game_folder, _read_folder_games(plan, game_folder, games, docs, cache, progress))
for game_folder, games in found]
progress.end()
cache.save()
_plan_read_folders(plan, read, fallback, entries, docs)
missing = sorted((plan.keep | plan.required) - listed)
if missing:
raise GamesUnavailable("stale", "the games index does not describe this file list "
f"({missing[0]} is not in it)")
return plan
def _games_setup_root(data: GamesData, index: dict, plat: str, bios_path: Path,
folder: Path | None, os_type: str,
network: bool) -> tuple[dict | None, Path | None]:
"""The rom folder setup the card follows, and the root of its games."""
setup = None
root = folder
for key in games_setups(index, plat, bios_path, os_type):
candidate = data.doc("setups", key, network)
if candidate is None:
continue
setup = setup or candidate
if folder is None:
found_root = games_root(candidate, bios_path)
if found_root is not None:
setup, root = candidate, found_root
break
return setup, root
def _found_games(plan: GamesPlan, folders: list[GameFolder],
setup: dict | None) -> list[tuple[GameFolder, list[Path]]]:
"""The games of each folder, up to MAX_GAME_FILES in all."""
budget = MAX_GAME_FILES
found: list[tuple[GameFolder, list[Path]]] = []
for game_folder in folders:
games, others = folder_games(game_folder, setup, budget)
budget -= len(games)
plan.not_games += others
found.append((game_folder, games))
if budget <= 0:
plan.truncated = True
break
return found
def _arcade_lookup(doc: dict) -> dict:
"""An arcade document as the lookups of _read_arcade read it."""
parents: dict[str, list[str]] = {}
@@ -19836,33 +19793,6 @@ def _plan_arcade_folder(plan: GamesPlan, games: list[GameFacts], entries: dict[s
plan.required.update(dest for dest in files if dest not in optional)
def _region_kept(files: list[str], games: list[GameFacts], entries: dict[str, dict],
system: str, info: SystemData,
profile: str) -> tuple[list[str], bool, frozenset | None]:
"""The files of a set the games' regions keep, whether every region is
kept, and the regions it was narrowed to (None when not narrowed)."""
rule = info.rules.get(profile)
region_free = {str(name).casefold() for name in (rule or {}).get("region_free") or []}
tagged = any((entries.get(dest) or {}).get("regions") for dest in files)
if not tagged:
return list(files), False, None
if region_rule_keeps_all(rule, games):
return list(files), True, None
proofs = [core_regions(game, system, info, profile) for game in games]
needed = needed_regions(rule or {}, proofs)
if needed is None:
for game, proof in zip(games, proofs):
if proof is None and not game.reason:
game.reason = ("not_read" if not game.read
else "not_recognised" if not game.recognised
else "region_unknown")
return list(files), True, None
kept = keep_regions(files, entries, needed, region_free)
if len(kept) < len(files):
return kept, False, needed
return kept, True, None
def _plan_set_extras(plan: GamesPlan, info: SystemData, profile: str, games: list[GameFacts],
files: list[str], kept: list[str], entries: dict[str, dict],
profiles: list[str]) -> None:
@@ -19933,6 +19863,82 @@ def _extra_fires(extra: dict, games: list[GameFacts]) -> bool:
return any(_suffix(game.path.name) in extensions for game in games)
# --- installer/games.py ------------------------------------------------------
def plan_games(plat: str, bios_path: Path, manifest: dict, folder: Path | None = None,
term: Terminal | None = None, data: GamesData | None = None,
os_type: str = "", network: bool = True) -> GamesPlan | None:
"""What the games of the card (or of folder) need, from install/games/.
None when the card has no rom folder this setup reads and no folder was
named. Raises GamesUnavailable when the index cannot be read, or does
not describe this list (a destination it names is not in the list).
"""
data = data or GAMES
index = data.load_index(network)
fallback = data.doc("fallback", plat, network)
if fallback is None:
raise GamesUnavailable("no_sets", f"install/games/ holds no BIOS sets for {plat}")
setup, root = _games_setup_root(data, index, plat, bios_path, folder, os_type, network)
if root is None:
return None
folders = game_folders(setup, index, root, folder is not None)
plan = GamesPlan(root, folder is None)
entries = {str(entry.get("dest")): entry for entry in manifest.get("files") or []}
listed = set(entries)
found = _found_games(plan, folders, setup)
docs = _GamesDocs(data, network)
cache_root = cache_folder()
cache = _ScanCache(cache_root / GAMES_SCAN_FILE if cache_root is not None else None)
progress = ReadCounter(sum(len(games) for _folder, games in found), term, "games_reading",
" games")
read = [(game_folder, _read_folder_games(plan, game_folder, games, docs, cache, progress))
for game_folder, games in found]
progress.end()
cache.save()
_plan_read_folders(plan, read, fallback, entries, docs)
missing = sorted((plan.keep | plan.required) - listed)
if missing:
raise GamesUnavailable("stale", "the games index does not describe this file list "
f"({missing[0]} is not in it)")
return plan
def _games_setup_root(data: GamesData, index: dict, plat: str, bios_path: Path,
folder: Path | None, os_type: str,
network: bool) -> tuple[dict | None, Path | None]:
"""The rom folder setup the card follows, and the root of its games."""
setup = None
root = folder
for key in games_setups(index, plat, bios_path, os_type):
candidate = data.doc("setups", key, network)
if candidate is None:
continue
setup = setup or candidate
if folder is None:
found_root = games_root(candidate, bios_path)
if found_root is not None:
setup, root = candidate, found_root
break
return setup, root
def _found_games(plan: GamesPlan, folders: list[GameFolder],
setup: dict | None) -> list[tuple[GameFolder, list[Path]]]:
"""The games of each folder, up to MAX_GAME_FILES in all."""
budget = MAX_GAME_FILES
found: list[tuple[GameFolder, list[Path]]] = []
for game_folder in folders:
games, others = folder_games(game_folder, setup, budget)
budget -= len(games)
plan.not_games += others
found.append((game_folder, games))
if budget <= 0:
plan.truncated = True
break
return found
def games_lines(plan: GamesPlan, manifest: dict | None, every: bool = False) -> list[str]:
"""The games read, in words: the head of the plan and of --check."""
count = len(plan.games)
+1 -1
View File
@@ -23,7 +23,7 @@ DEFAULT_INSTALL_URL="https://raw.githubusercontent.com/Abdess/retrobios/main/ins
# curl or wget stop on a cut transfer before the hash is read: the site's
# build rewrites this line there (scripts/sitebootstrap.py).
MISMATCH_ADVICE="The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that."
DEFAULT_INSTALL_SHA256="7b6e3083d33d9b2fd7735df41ba56a0e3f7c1e98cf56327277566a0da05d6464"
DEFAULT_INSTALL_SHA256="e62e2621eccc43ddbc56b9227db16ef5ddf918d8846c15ff0d6fbbe9e059da98"
# The pin above refuses any changed byte: this cap only stops a body that
# cannot be the installer (a sign-in portal's page) before it is hashed.
MAX_INSTALLER_BYTES=8388608
+8 -8
View File
@@ -27,8 +27,8 @@
{
"dest": "roms/ports/retrobios/install.py",
"repo_path": "install.py",
"size": 1502616,
"sha256": "7b6e3083d33d9b2fd7735df41ba56a0e3f7c1e98cf56327277566a0da05d6464",
"size": 1502859,
"sha256": "e62e2621eccc43ddbc56b9227db16ef5ddf918d8846c15ff0d6fbbe9e059da98",
"executable": false
},
{
@@ -71,8 +71,8 @@
"muos": {
"archive": {
"dest": "ARCHIVE/RetroBIOS.muxapp",
"size": 1556241,
"sha256": "ee4e0aeca4b193f4e7d9b7221c89ec49eb3019434f27d8312d5168693ae74a0d"
"size": 1556484,
"sha256": "fe8690eb14484257995f027036fdea687d1c3810f4b0ebf034e2cb55bccd6a48"
},
"members": [
{
@@ -92,8 +92,8 @@
{
"name": "RetroBIOS/install.py",
"repo_path": "install.py",
"size": 1502616,
"sha256": "7b6e3083d33d9b2fd7735df41ba56a0e3f7c1e98cf56327277566a0da05d6464",
"size": 1502859,
"sha256": "e62e2621eccc43ddbc56b9227db16ef5ddf918d8846c15ff0d6fbbe9e059da98",
"executable": false
},
{
@@ -166,8 +166,8 @@
{
"dest": ".config/modules/retrobios/install.py",
"repo_path": "install.py",
"size": 1502616,
"sha256": "7b6e3083d33d9b2fd7735df41ba56a0e3f7c1e98cf56327277566a0da05d6464",
"size": 1502859,
"sha256": "e62e2621eccc43ddbc56b9227db16ef5ddf918d8846c15ff0d6fbbe9e059da98",
"executable": false
},
{
+1 -1
View File
@@ -27,7 +27,7 @@ from installer.remote import DEFAULT_RELEASE_REF, active_remote
from installer.text import _plural
if TYPE_CHECKING:
from installer.games import GamesPlan
from installer.game_folders import GamesPlan
def recorded_axes(files: list[dict], omitted: Iterable[dict] = ()) -> frozenset:
+1 -1
View File
@@ -40,7 +40,7 @@ from installer.safe_replace import KEPT, _MEMBER_ERRORS, _NotKept, _kept_slot, u
if TYPE_CHECKING:
from installer.console import Progress
from installer.games import GamesPlan
from installer.game_folders import GamesPlan
def _archive_validator(headers: http.client.HTTPMessage | Mapping[str, str] | None) -> str:
+408
View File
@@ -0,0 +1,408 @@
"""Reading the game folders of a card into the systems and BIOS sets its games need."""
from __future__ import annotations
import os
from collections.abc import Callable, Collection, Iterable
from pathlib import Path
from typing import TYPE_CHECKING
from installer.console import ReadCounter
from installer.game_files import (
ARCADE_SUFFIXES,
GameFacts,
SystemData,
_ScanCache,
_suffix,
read_game,
)
from installer.game_regions import _region_kept
from installer.games_index import GamesData
from installer.platforms import entry_systems
if TYPE_CHECKING:
from installer.game_files import GameFolder
class GamesPlan:
"""What the games of a card need, and what was read to say so."""
def __init__(self, root: Path, card: bool) -> None:
self.root = root
self.card = card
self.games: list[GameFacts] = []
self.not_games = 0
self.support = 0
# Per system: folder names, game count, recognised, regions kept
# (empty for every region), and the cores whose sets were read.
self.systems: dict[str, dict] = {}
self.keep: set[str] = set()
self.required: set[str] = set()
self.unknown_core: list[str] = []
self.truncated = False
# The cores the games run on (a folder's default and its boards'),
# and the systems of a folder no setup names a core for.
self.profiles: set[str] = set()
self.any_core: set[str] = set()
@property
def fallback(self) -> list[GameFacts]:
return [game for game in self.games if game.reason]
def where(self) -> str:
return "on this card" if self.card else "in this folder"
def wanted(self, required_only: bool) -> set[str]:
return self.required if required_only else self.keep
def apply(self, files: list[dict], omitted: list[dict],
required_only: bool) -> tuple[list[dict], list[dict]]:
"""The files and omissions of the list the games need."""
wanted = self.wanted(required_only)
folded = {dest.casefold() for dest in wanted}
kept = [entry for entry in files if entry.get("dest") in wanted
or (entry.get("case_variant") and str(entry.get("dest")).casefold() in folded)]
systems = {system for entry in kept for system in entry_systems(entry)}
kept_omitted = [entry for entry in omitted
if (entry.get("dest") in wanted
or bool(set(entry_systems(entry)) & systems))
and (entry.get("required") or not required_only)]
return kept, kept_omitted
def event_systems(self) -> list[dict]:
return [{"system": system, "folder": ", ".join(info["folders"]),
"games": info["games"], "recognised": info["recognised"],
"regions": sorted(info["regions"] or ())}
for system, info in sorted(self.systems.items())]
def event_fallback(self) -> list[dict]:
return [{"system": game_system(game), "folder": game.folder.path.name,
"name": game.shown, "reason": game.reason} for game in self.fallback]
def game_system(game: GameFacts) -> str:
systems = game.folder.row.get("systems") or []
return str(systems[0]) if systems else ""
def _folder_profiles(row: dict) -> list[str | None]:
"""The cores a folder's games may run on: its default, and each board's
(a card moves between boards, and is often read on a computer)."""
found: list[str | None] = [row.get("profile")]
for spec in (row.get("board_defaults") or {}).values():
if isinstance(spec, dict) and spec.get("profile") not in found:
found.append(spec.get("profile"))
return found
def _folder_set_keys(row: dict, fallback: dict) -> list[str]:
"""The fallback sets of a folder: its own and its boards', else, for a
folder no setup names a core for, every set of its systems."""
keys = list(row.get("fallback") or [])
for spec in (row.get("board_defaults") or {}).values():
if isinstance(spec, dict):
keys += [key for key in spec.get("fallback") or [] if key not in keys]
if keys or "fallback" in row:
return keys
systems = set(row.get("systems") or [])
return sorted(key for key in fallback.get("sets") or {}
if key.split("|", 1)[0] in systems)
def _set_rows(fallback: dict, key: str) -> tuple[list[str], set[str]]:
"""A fallback set's destinations and those the declaring layer marks optional."""
table = fallback.get("files") or []
chosen = (fallback.get("sets") or {}).get(key) or {}
def dest(index: object) -> str | None:
if isinstance(index, int) and 0 <= index < len(table):
return str(table[index][0])
return None
files = [d for d in (dest(i) for i in chosen.get("files") or []) if d]
optional = {d for d in (dest(i) for i in chosen.get("optional") or []) if d}
return files, optional
def _named_dests(names: Iterable[str], pool: Iterable[str]) -> list[str]:
wanted = {name.casefold() for name in names}
return [dest for dest in pool if Path(dest).name.casefold() in wanted]
def _arcade_lookup(doc: dict) -> dict:
"""An arcade document as the lookups of _read_arcade read it."""
parents: dict[str, list[str]] = {}
for root_set, members in (doc.get("bios") or {}).items():
for member in members:
parents.setdefault(member, []).append(root_set)
devices: dict[str, list[str]] = {}
for device, members in (doc.get("devices") or {}).items():
for member in members:
devices.setdefault(member, []).append(device)
known = set(parents) | set(devices) | set(doc.get("no_bios") or [])
# FBNeo keys a console game by its driver (msx_1942): the
# MSX, NES and Arcade lists each hold a 1942.
consoles: dict[str, list[str]] = {}
for key in known:
driver, cut, stem = key.partition("_")
if cut and stem:
consoles.setdefault(stem, []).append(driver)
return {
"parents": parents, "devices": devices, "consoles": consoles,
"plain": set(doc.get("no_bios") or []),
"support": (set(doc.get("bios_sets") or []) | set(doc.get("bios") or {})
| set(doc.get("devices") or {})),
}
class _GamesDocs:
"""The system and arcade documents of install/games/, each read once."""
def __init__(self, data: GamesData, network: bool) -> None:
self.data = data
self.network = network
self.systems: dict[str, SystemData] = {}
self.arcades: dict[str, dict] = {}
def system(self, system: str) -> SystemData:
if system not in self.systems:
self.systems[system] = SystemData(self.data.doc("systems", system, self.network))
return self.systems[system]
def arcade(self, name: str) -> dict | None:
if name not in self.arcades:
doc = self.data.doc("arcade", name, self.network)
self.arcades[name] = {} if doc is None else _arcade_lookup(doc)
return self.arcades[name] or None
def _folder_readers(row_systems: list[str], profiles: list[str | None],
docs: _GamesDocs) -> set[str]:
"""The cores of a folder's profiles that read its first system."""
readers: set[str] = set()
if row_systems:
cores = docs.system(row_systems[0]).cores
for profile in profiles:
readers |= {name for name in (cores.get(profile or "") or {}).get("order") or []
if isinstance(name, str)}
return readers
def _count_game(plan: GamesPlan, game_folder: GameFolder, row_systems: list[str],
facts: GameFacts) -> None:
"""Count a game under each system of its folder."""
for system in row_systems:
info = plan.systems.setdefault(system, {"folders": [], "games": 0,
"recognised": 0, "regions": None})
if game_folder.path.name not in info["folders"]:
info["folders"].append(game_folder.path.name)
info["games"] += 1
info["recognised"] += int(facts.recognised)
def _read_folder_games(plan: GamesPlan, game_folder: GameFolder, games: list[Path],
docs: _GamesDocs, cache: _ScanCache,
progress: ReadCounter) -> list[GameFacts]:
"""Read the games of one folder: the ones that are not support sets."""
row = game_folder.row
row_systems = [str(s) for s in row.get("systems") or []]
arcade = row.get("arcade") if isinstance(row.get("arcade"), str) else None
profiles = _folder_profiles(row)
if row_systems and not row.get("profile") and "fallback" not in row:
plan.unknown_core += [s for s in row_systems if s not in plan.unknown_core]
readers = _folder_readers(row_systems, profiles, docs)
group: list[GameFacts] = []
for path in games:
shown = os.path.relpath(path, plan.root).replace(os.sep, "/")
facts = GameFacts(path, shown, game_folder)
progress.done += 1
if row.get("arcade"):
_read_arcade(facts, docs.arcade(arcade) if arcade else None,
_console_hints(game_folder))
elif row_systems:
read_game(facts, row_systems[0], docs.system(row_systems[0]), readers, cache)
progress.step()
if facts.kind == "support":
plan.support += 1
continue
plan.games.append(facts)
group.append(facts)
_count_game(plan, game_folder, row_systems, facts)
return group
def _plan_read_folders(plan: GamesPlan, read: list[tuple[GameFolder, list[GameFacts]]],
fallback: dict, entries: dict[str, dict], docs: _GamesDocs) -> None:
"""Add to the plan what the games read in each folder need."""
for game_folder, group in read:
if group:
for profile in _folder_profiles(game_folder.row):
if profile:
plan.profiles.add(str(profile))
else:
plan.any_core.update(str(s) for s in game_folder.row.get("systems") or [])
arcade = game_folder.row.get("arcade")
sets = (docs.arcade(arcade) or {}).get("support", set()) \
if isinstance(arcade, str) else set()
_plan_game_folder(plan, game_folder, group, fallback, entries, docs.system, sets)
# Systems whose folder an arcade core reads as arcade sets, by their bare
# set name. Any other system's sets are console games, keyed by driver.
_ARCADE_SYSTEMS = frozenset({"arcade", "neogeo", "snk-neogeo", "sega-naomi", "sega-naomi2",
"sammy-atomiswave", "sega-stv", "cps1", "cps2", "cps3"})
def _console_hints(folder: GameFolder) -> tuple[str, ...] | None:
"""None for an arcade folder, else the words of its systems and its
name, which tell one console's driver prefix from another's."""
systems = [str(s) for s in folder.row.get("systems") or []]
if not systems or any(s in _ARCADE_SYSTEMS or s.startswith("arcade") for s in systems):
return None
words = {word for system in systems for word in system.split("-")}
words.add(folder.path.name.lower())
return tuple(sorted(words))
def _read_arcade(facts: GameFacts, index: dict | None,
console: tuple[str, ...] | None = None) -> None:
"""An arcade set: a BIOS or device set (not a game), a game and the BIOS
sets its romof chain reaches, or a name the core's DAT does not hold. In
a console's folder (console, the words of its systems) a set is looked
for under the core's driver name for it (msx_1942): the bare name is an
arcade set's."""
if _suffix(facts.path.name) not in ARCADE_SUFFIXES:
facts.kind = "support"
return
name = os.path.splitext(facts.path.name)[0].lower()
if index is None:
facts.reason = "not_recognised"
return
if name in index["support"]:
facts.kind = "support"
return
keys = [name]
if console is not None:
drivers = index.get("consoles", {}).get(name, [])
hinted = [d for d in drivers if d in console]
# A prefix no word of the folder names: every console's driver of
# that name, so that the right one is among them.
keys = [f"{d}_{name}" for d in sorted(hinted or drivers)] or [name]
parents = {p for key in keys for p in index["parents"].get(key, ())}
devices = {d for key in keys for d in index["devices"].get(key, ())}
facts.parents, facts.devices = tuple(sorted(parents)), tuple(sorted(devices))
if parents or devices or any(key in index["plain"] for key in keys):
facts.recognised = True
facts.title = keys[0] if len(keys) == 1 else name
else:
facts.reason = "unknown_set"
def _read_by_profiles(entries: dict[str, dict], dest: str, profiles: list[str]) -> bool:
"""Whether one of a folder's profiles reads dest, every core when the
entry names none."""
cores = (entries.get(dest) or {}).get("cores")
return cores is None or any(name in cores for name in profiles)
def _plan_arcade_folder(plan: GamesPlan, games: list[GameFacts], entries: dict[str, dict],
profiles: list[str], sets: dict[str, tuple[list[str], set[str]]],
arcade_sets: Collection[str]) -> None:
"""Add to the plan the BIOS and device sets an arcade folder's games need."""
# A BIOS set is looked for in the whole list, as the platform's own
# declaration (neogeo.zip) and as the core's (fbneo/neogeo.zip).
usable = [dest for dest in entries if _read_by_profiles(entries, dest, profiles)]
whole = False
for game in games:
if game.reason:
whole = True
continue
names = set(game.parents + game.devices)
needed = _named_dests([f"{name}.zip" for name in names], usable)
held = {Path(dest).name.casefold() for dest in needed}
if any(f"{name}.zip".casefold() not in held for name in names):
game.reason = "no_parent"
whole = True
continue
plan.keep.update(needed)
plan.required.update(needed)
if whole:
# Every set of the folder, and every BIOS set the core reads.
own = _named_dests([f"{name}.zip" for name in arcade_sets],
[dest for dest in usable
if (entries.get(dest) or {}).get("cores") is not None])
plan.keep.update(own)
plan.required.update(own)
for files, optional in sets.values():
plan.keep.update(files)
plan.required.update(dest for dest in files if dest not in optional)
def _plan_set_extras(plan: GamesPlan, info: SystemData, profile: str, games: list[GameFacts],
files: list[str], kept: list[str], entries: dict[str, dict],
profiles: list[str]) -> None:
"""Add to the plan the games/extras.yml files of a set that fire."""
for extra in info.extras:
if extra.get("profile") != profile or not _extra_fires(extra, games):
continue
if extra.get("region_files"):
named = [dest for dest in kept if (entries.get(dest) or {}).get("regions")]
else:
names = extra.get("files") or []
named = _named_dests(names, files) or _named_dests(
names, [dest for dest in entries if _read_by_profiles(entries, dest, profiles)])
plan.keep.update(named)
if extra.get("required") or extra.get("filename_override"):
plan.required.update(named)
def _plan_game_folder(plan: GamesPlan, game_folder: GameFolder, games: list[GameFacts],
fallback: dict, entries: dict[str, dict],
system_data: Callable[[str], SystemData],
arcade_sets: Collection[str] = ()) -> None:
"""Add to the plan what one folder's games need. arcade_sets are the
BIOS and device sets of the folder's arcade index."""
row = game_folder.row
profiles = [name for name in _folder_profiles(row) if name]
if not row.get("systems"):
# A port's own folder, read by one core and named by no system: what
# that core loads.
named = [dest for dest, entry in entries.items()
if any(name in (entry.get("cores") or []) for name in profiles)]
plan.keep.update(named)
plan.required.update(named)
return
keys = _folder_set_keys(row, fallback)
sets: dict[str, tuple[list[str], set[str]]] = {key: _set_rows(fallback, key) for key in keys}
if row.get("arcade"):
_plan_arcade_folder(plan, games, entries, profiles, sets, arcade_sets)
return
narrowed: dict[str, set[str]] = {}
wide: set[str] = set()
for key, (files, optional) in sets.items():
system, _, profile = key.partition("|")
info = system_data(system)
kept, every, narrowed_to = _region_kept(files, games, entries, system, info, profile)
if every:
wide.add(system)
elif narrowed_to is not None:
narrowed.setdefault(system, set()).update(narrowed_to)
plan.keep.update(kept)
plan.required.update(dest for dest in kept if dest not in optional)
_plan_set_extras(plan, info, profile, games, files, kept, entries, profiles)
for system, regions in narrowed.items():
state = plan.systems.get(system)
# Said only where no core of the folder keeps the other regions.
if state is not None and system not in wide:
state["regions"] = sorted(set(state["regions"] or ()) | regions)
def _extra_fires(extra: dict, games: list[GameFacts]) -> bool:
"""Whether a games/extras.yml entry applies to these games. A trigger on
header bytes this file does not read applies: the file stays."""
trigger = extra.get("trigger") or {}
if trigger.get("every_game") or trigger.get("header"):
return True
extensions = {str(ext).lower() for ext in trigger.get("extensions") or []}
return any(_suffix(game.path.name) in extensions for game in games)
+129
View File
@@ -0,0 +1,129 @@
"""Which BIOS regions the games on a card need, and when a rule keeps every region."""
from __future__ import annotations
from pathlib import Path
from installer.game_files import GameFacts, SystemData, _suffix
from installer.regions import WORLD_REGION, _region_comparable
def core_regions(facts: GameFacts, system_id: str, system: SystemData,
profile: str) -> frozenset | None:
"""The regions a game is proven to need under one core: the catalog's
tag of the content matched, the region the core's own readers compute
from its bytes, or both when they agree. None for no proof or for two
proofs that disagree."""
if facts.system != system_id:
return None
core = system.cores.get(profile) or {}
header: frozenset | None = None
order = [name for name in core.get("order") or [] if isinstance(name, str)]
all_read = bool(order)
for name in order:
value = facts.readings.get(name)
if value is None:
all_read = False
continue
if isinstance(value, frozenset):
header = value
break
otherwise = core.get("otherwise")
if header is None and all_read and isinstance(otherwise, list) and otherwise:
header = frozenset(str(region) for region in otherwise)
proofs = [proof for proof in (facts.dat_regions, header) if proof]
if not proofs:
return None
if len(proofs) == 2 and not any(_region_comparable(a, b)
for a in proofs[0] for b in proofs[1]):
return None
return frozenset().union(*proofs)
# Cases of games/regions.yml a proven region does not settle whatever the
# game: device state (an option, a per-game setting) or the code itself.
_ALWAYS_WIDE = ("always", "option", "user-state")
def region_rule_keeps_all(rule: dict | None, games: list[GameFacts]) -> bool:
"""Whether the core keeps every region whatever its games prove: no rule
read at its pin, or one of the cases games/regions.yml names (a region
option, an override BIOS, a fallback across regions, a disc database)."""
if not isinstance(rule, dict):
return True
for case in rule.get("keep_all_regions") or []:
if not isinstance(case, dict):
return True
name, condition = case.get("case"), case.get("condition")
if condition in _ALWAYS_WIDE:
return True
if name == "first-disc-region":
if any(_suffix(game.path.name) == ".m3u" for game in games):
return True
elif name not in ("multi-area-disc", "gamecube-unknown-region", "saturn-kr-ipl"):
return True
return False
def needed_regions(rule: dict, proofs: list[frozenset | None]) -> frozenset | None:
"""The regions the games of one core prove, routed as the core routes
them, None when one game proves none."""
if not proofs or any(proof is None for proof in proofs):
return None
routes = rule.get("routes") or {}
needed = frozenset(str(routes.get(region, region))
for proof in proofs for region in proof or ())
wide = {str(case.get("case")) for case in rule.get("keep_all_regions") or []
if isinstance(case, dict)}
if "saturn-kr-ipl" in wide and needed & {"asia-ntsc", "south-korea"}:
return None
return needed
def keep_regions(dests: list[str], entries: dict[str, dict], needed: frozenset,
region_free: set[str]) -> list[str]:
"""The files of a set the proven regions keep: every file with no region
tag, a world or region-free file, and those whose tag a needed region
matches. A needed region no file matches keeps the whole set."""
def regions(dest: str) -> list[str]:
entry = entries.get(dest) or {}
if Path(dest).name.casefold() in region_free:
return []
tags = [str(r) for r in entry.get("regions") or []]
return [] if WORLD_REGION in tags else tags
tagged = {dest: regions(dest) for dest in dests if regions(dest)}
for region in needed:
if not any(_region_comparable(region, tag) for tags in tagged.values() for tag in tags):
return list(dests)
return [dest for dest in dests
if dest not in tagged
or any(_region_comparable(region, tag) for region in needed for tag in tagged[dest])]
def _region_kept(files: list[str], games: list[GameFacts], entries: dict[str, dict],
system: str, info: SystemData,
profile: str) -> tuple[list[str], bool, frozenset | None]:
"""The files of a set the games' regions keep, whether every region is
kept, and the regions it was narrowed to (None when not narrowed)."""
rule = info.rules.get(profile)
region_free = {str(name).casefold() for name in (rule or {}).get("region_free") or []}
tagged = any((entries.get(dest) or {}).get("regions") for dest in files)
if not tagged:
return list(files), False, None
if region_rule_keeps_all(rule, games):
return list(files), True, None
proofs = [core_regions(game, system, info, profile) for game in games]
needed = needed_regions(rule or {}, proofs)
if needed is None:
for game, proof in zip(games, proofs):
if proof is None and not game.reason:
game.reason = ("not_read" if not game.read
else "not_recognised" if not game.recognised
else "region_unknown")
return list(files), True, None
kept = keep_regions(files, entries, needed, region_free)
if len(kept) < len(files):
return kept, False, needed
return kept, True, None
+6 -737
View File
@@ -1,461 +1,36 @@
"""Games on the card: the BIOS their systems and regions need, from install/games/."""
"""Games on the card: the plan of BIOS they need, its lines and events, and the run that reads them."""
from __future__ import annotations
import argparse
import hashlib
import http.client
import json
import os
import re
import urllib.error
from collections.abc import Callable, Collection, Iterable
from pathlib import Path
from typing import TYPE_CHECKING
from installer.cards import games_part
from installer.console import ReadCounter, detail, fold_line
from installer.destination import card_root_above
from installer.detect_os import detect_os, detected_os
from installer.events import EVENTS
from installer.game_files import (
ARCADE_SUFFIXES,
GAMES_SCAN_FILE,
MAX_GAME_FILES,
GameFacts,
SystemData,
_ScanCache,
_cache_write,
_may_hold_games,
_suffix,
cache_folder,
folder_games,
game_folders,
games_root,
read_game,
)
from installer.handheld_app import card_app
from installer.host_fs import _is_dir
from installer.manifest import _SHA256_RE
from installer.network import _fetch_json, _read_capped
from installer.offline import LOCAL, _Offline, offline_words
from installer.offline import LOCAL
from installer.paths import shown_path
from installer.platforms import entry_systems, system_title
from installer.platforms import system_title
from installer.refusal import Refusal
from installer.regions import WORLD_REGION, _region_comparable, region_title
from installer.remote import active_remote
from installer.regions import region_title
from installer.text import _plural, agree
if TYPE_CHECKING:
from installer.game_files import GameFolder
from installer.terminal import Terminal
# --- Games on the card -------------------------------------------------------
#
# --for-games keeps what the games of a card need, read from install/games/
# (scripts/generate_games_index.py): per system the files its default core
# reads (a fallback set), one region where the game's own bytes prove it,
# and for an arcade set the BIOS sets its romof chain reaches. Any doubt
# keeps the wider set: the game must run.
GAMES_FORMAT = 1
def games_url(path: str) -> str:
"""Where install/games/<path> is read from in this run."""
return f"{active_remote().base}/install/games/{path}"
MAX_GAMES_INDEX_BYTES = 4 * 1024 * 1024
MAX_GAMES_DOC_BYTES = 32 * 1024 * 1024
_GAMES_DOC_NAME = re.compile(r"^(setups|fallback|systems|arcade)/[A-Za-z0-9_.+-]+\.json$")
# Why a game got every BIOS of its system: said with its name.
GAMES_REASONS = {
"not_recognised": "not recognised",
"not_read": "format not read",
"region_unknown": "region not proven",
"unknown_set": "set unknown to the core",
"no_parent": "BIOS set not in the list",
}
class GamesUnavailable(Exception):
"""install/games/ could not be read: the run keeps the wider set."""
def __init__(self, reason: str, words: str) -> None:
super().__init__(words)
self.reason = reason
self.words = words
class GamesData:
"""install/games/ for this run: index.json, then each document it lists,
checked against the sha256 the index gives. Read from the games folder
beside --manifest, the cache, or the network, in that order."""
def __init__(self) -> None:
self.index: dict | None = None
self.docs: dict[str, dict] = {}
def reset(self) -> None:
self.index = None
self.docs = {}
@staticmethod
def beside() -> Path | None:
return LOCAL.manifest.parent / "games" if LOCAL.manifest is not None else None
@staticmethod
def cached(name: str) -> Path | None:
root = cache_folder()
return root / "games" / name if root is not None else None
def load_index(self, network: bool = True, attempts: int = 2) -> dict:
if self.index is not None:
return self.index
raw = None
beside = self.beside()
if beside is not None:
raw = _read_capped(beside / "index.json", MAX_GAMES_INDEX_BYTES)
cached = self.cached("index.json")
reason = "offline"
failure = ""
if raw is None and network:
try:
raw = _fetch_json(games_url("index.json"), MAX_GAMES_INDEX_BYTES,
"games index", attempts=attempts, raw=True)
except _Offline:
reason = "offline"
except (urllib.error.URLError, OSError, ValueError,
http.client.HTTPException) as exc:
reason = "network"
failure = str(exc)
else:
if cached is not None:
_cache_write(cached, raw)
if raw is None and cached is not None:
raw = _read_capped(cached, MAX_GAMES_INDEX_BYTES)
if raw is None:
raise GamesUnavailable(reason, (
f"the games index is not on this machine and {offline_words()}"
if reason == "offline"
else f"the games index could not be downloaded ({failure})"))
try:
data = json.loads(raw.decode("utf-8"))
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
raise GamesUnavailable("invalid", f"the games index is not valid ({exc})") from exc
if (not isinstance(data, dict) or data.get("format_version") != GAMES_FORMAT
or not all(isinstance(data.get(kind), dict)
for kind in ("setups", "fallback", "systems", "arcade"))):
raise GamesUnavailable("invalid", "the games index is of another format")
self.index = data
return data
@staticmethod
def _doc_held(beside: Path | None, cached: Path | None, name: str, digest: str,
limit: int) -> bytes | None:
"""A listed document beside --manifest or in the cache, of its sha256."""
for source in (beside / name if beside is not None else None, cached):
if source is None:
continue
found = _read_capped(source, limit)
if found is not None and hashlib.sha256(found).hexdigest() == digest:
return found
return None
@staticmethod
def _doc_fetched(kind: str, name: str, digest: str, limit: int, attempts: int,
cached: Path | None) -> bytes | None:
"""A listed document downloaded and cached, None when offline."""
try:
fetched = _fetch_json(games_url(name), limit, f"games {kind}",
attempts=attempts, raw=True)
except _Offline:
return None
except (urllib.error.URLError, OSError, ValueError,
http.client.HTTPException) as exc:
raise GamesUnavailable(
"network", f"install/games/{name} could not be downloaded ({exc})") from exc
if hashlib.sha256(fetched).hexdigest() != digest:
raise GamesUnavailable("invalid", f"install/games/{name} is not the one "
"the games index lists")
if cached is not None:
_cache_write(cached, fetched)
return fetched
def doc(self, kind: str, key: str, network: bool = True,
attempts: int = 2) -> dict | None:
"""One listed document, None when the index lists none of that name."""
entry = self.load_index(network, attempts).get(kind, {}).get(key)
if not isinstance(entry, dict):
return None
name = str(entry.get("file", ""))
digest = str(entry.get("sha256", ""))
if not _GAMES_DOC_NAME.match(name) or not _SHA256_RE.match(digest):
raise GamesUnavailable("invalid", f"the games index names {name!r}")
if name in self.docs:
return self.docs[name]
limit = min(int(entry.get("size") or 0), MAX_GAMES_DOC_BYTES)
beside = self.beside()
cached = self.cached(f"{digest}.json")
raw = self._doc_held(beside, cached, name, digest, limit)
if raw is None and network:
raw = self._doc_fetched(kind, name, digest, limit, attempts, cached)
if raw is None:
raise GamesUnavailable("offline", f"install/games/{name} is not on this machine "
f"and {offline_words()}")
try:
data = json.loads(raw.decode("utf-8"))
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
raise GamesUnavailable("invalid", f"install/games/{name} is not valid") from exc
if not isinstance(data, dict):
raise GamesUnavailable("invalid", f"install/games/{name} is not valid")
self.docs[name] = data
return data
GAMES = GamesData()
# --- Where the games are -----------------------------------------------------
def _setup_key(name: str) -> str:
return re.sub(r"[^a-z0-9]+", "-", name.lower()).strip("-")
def games_setups(index: dict, plat: str, bios_path: Path, os_type: str = "") -> list[str]:
"""The setups whose rom folders this BIOS folder's games may sit in,
best first: the system detection named (muOS, ArkOS), else the
platform's own."""
setups = index.get("setups") or {}
named = detected_os(plat, bios_path)
if not named:
above = card_root_above(bios_path)
named = above[0] if above else ""
if not named:
# --platform names the list, not the card: the full screen passes
# --platform=retroarch for a muOS card, detection never runs in that
# run, and the card's ROMS/ was never read. The card's own files
# name its system, as card_app reads them for the app.
card = card_app(bios_path)
named = card.system if card else ""
if not named:
# --dest on a handheld's games part from a computer (ArkOS's
# EASYROMS/bios): detection never ran there, and the card's own rom
# folders sit beside the BIOS folder.
part = games_part(bios_path.parent)
named = part.name if part else ""
key = _setup_key(named) if named else ""
found = [name for name in sorted(setups) if key and (name == key or name.startswith(key + "-"))]
if not found and plat in setups:
found = [plat]
windows = f"{plat}-windows"
if (os_type or detect_os()) == "windows" and windows in setups:
found.insert(0, windows)
return found
def core_regions(facts: GameFacts, system_id: str, system: SystemData,
profile: str) -> frozenset | None:
"""The regions a game is proven to need under one core: the catalog's
tag of the content matched, the region the core's own readers compute
from its bytes, or both when they agree. None for no proof or for two
proofs that disagree."""
if facts.system != system_id:
return None
core = system.cores.get(profile) or {}
header: frozenset | None = None
order = [name for name in core.get("order") or [] if isinstance(name, str)]
all_read = bool(order)
for name in order:
value = facts.readings.get(name)
if value is None:
all_read = False
continue
if isinstance(value, frozenset):
header = value
break
otherwise = core.get("otherwise")
if header is None and all_read and isinstance(otherwise, list) and otherwise:
header = frozenset(str(region) for region in otherwise)
proofs = [proof for proof in (facts.dat_regions, header) if proof]
if not proofs:
return None
if len(proofs) == 2 and not any(_region_comparable(a, b)
for a in proofs[0] for b in proofs[1]):
return None
return frozenset().union(*proofs)
# Cases of games/regions.yml a proven region does not settle whatever the
# game: device state (an option, a per-game setting) or the code itself.
_ALWAYS_WIDE = ("always", "option", "user-state")
def region_rule_keeps_all(rule: dict | None, games: list[GameFacts]) -> bool:
"""Whether the core keeps every region whatever its games prove: no rule
read at its pin, or one of the cases games/regions.yml names (a region
option, an override BIOS, a fallback across regions, a disc database)."""
if not isinstance(rule, dict):
return True
for case in rule.get("keep_all_regions") or []:
if not isinstance(case, dict):
return True
name, condition = case.get("case"), case.get("condition")
if condition in _ALWAYS_WIDE:
return True
if name == "first-disc-region":
if any(_suffix(game.path.name) == ".m3u" for game in games):
return True
elif name not in ("multi-area-disc", "gamecube-unknown-region", "saturn-kr-ipl"):
return True
return False
def needed_regions(rule: dict, proofs: list[frozenset | None]) -> frozenset | None:
"""The regions the games of one core prove, routed as the core routes
them, None when one game proves none."""
if not proofs or any(proof is None for proof in proofs):
return None
routes = rule.get("routes") or {}
needed = frozenset(str(routes.get(region, region))
for proof in proofs for region in proof or ())
wide = {str(case.get("case")) for case in rule.get("keep_all_regions") or []
if isinstance(case, dict)}
if "saturn-kr-ipl" in wide and needed & {"asia-ntsc", "south-korea"}:
return None
return needed
def keep_regions(dests: list[str], entries: dict[str, dict], needed: frozenset,
region_free: set[str]) -> list[str]:
"""The files of a set the proven regions keep: every file with no region
tag, a world or region-free file, and those whose tag a needed region
matches. A needed region no file matches keeps the whole set."""
def regions(dest: str) -> list[str]:
entry = entries.get(dest) or {}
if Path(dest).name.casefold() in region_free:
return []
tags = [str(r) for r in entry.get("regions") or []]
return [] if WORLD_REGION in tags else tags
tagged = {dest: regions(dest) for dest in dests if regions(dest)}
for region in needed:
if not any(_region_comparable(region, tag) for tags in tagged.values() for tag in tags):
return list(dests)
return [dest for dest in dests
if dest not in tagged
or any(_region_comparable(region, tag) for region in needed for tag in tagged[dest])]
class GamesPlan:
"""What the games of a card need, and what was read to say so."""
def __init__(self, root: Path, card: bool) -> None:
self.root = root
self.card = card
self.games: list[GameFacts] = []
self.not_games = 0
self.support = 0
# Per system: folder names, game count, recognised, regions kept
# (empty for every region), and the cores whose sets were read.
self.systems: dict[str, dict] = {}
self.keep: set[str] = set()
self.required: set[str] = set()
self.unknown_core: list[str] = []
self.truncated = False
# The cores the games run on (a folder's default and its boards'),
# and the systems of a folder no setup names a core for.
self.profiles: set[str] = set()
self.any_core: set[str] = set()
@property
def fallback(self) -> list[GameFacts]:
return [game for game in self.games if game.reason]
def where(self) -> str:
return "on this card" if self.card else "in this folder"
def wanted(self, required_only: bool) -> set[str]:
return self.required if required_only else self.keep
def apply(self, files: list[dict], omitted: list[dict],
required_only: bool) -> tuple[list[dict], list[dict]]:
"""The files and omissions of the list the games need."""
wanted = self.wanted(required_only)
folded = {dest.casefold() for dest in wanted}
kept = [entry for entry in files if entry.get("dest") in wanted
or (entry.get("case_variant") and str(entry.get("dest")).casefold() in folded)]
systems = {system for entry in kept for system in entry_systems(entry)}
kept_omitted = [entry for entry in omitted
if (entry.get("dest") in wanted
or bool(set(entry_systems(entry)) & systems))
and (entry.get("required") or not required_only)]
return kept, kept_omitted
def event_systems(self) -> list[dict]:
return [{"system": system, "folder": ", ".join(info["folders"]),
"games": info["games"], "recognised": info["recognised"],
"regions": sorted(info["regions"] or ())}
for system, info in sorted(self.systems.items())]
def event_fallback(self) -> list[dict]:
return [{"system": game_system(game), "folder": game.folder.path.name,
"name": game.shown, "reason": game.reason} for game in self.fallback]
def game_system(game: GameFacts) -> str:
systems = game.folder.row.get("systems") or []
return str(systems[0]) if systems else ""
def _folder_profiles(row: dict) -> list[str | None]:
"""The cores a folder's games may run on: its default, and each board's
(a card moves between boards, and is often read on a computer)."""
found: list[str | None] = [row.get("profile")]
for spec in (row.get("board_defaults") or {}).values():
if isinstance(spec, dict) and spec.get("profile") not in found:
found.append(spec.get("profile"))
return found
def _folder_set_keys(row: dict, fallback: dict) -> list[str]:
"""The fallback sets of a folder: its own and its boards', else, for a
folder no setup names a core for, every set of its systems."""
keys = list(row.get("fallback") or [])
for spec in (row.get("board_defaults") or {}).values():
if isinstance(spec, dict):
keys += [key for key in spec.get("fallback") or [] if key not in keys]
if keys or "fallback" in row:
return keys
systems = set(row.get("systems") or [])
return sorted(key for key in fallback.get("sets") or {}
if key.split("|", 1)[0] in systems)
def _set_rows(fallback: dict, key: str) -> tuple[list[str], set[str]]:
"""A fallback set's destinations and those the declaring layer marks optional."""
table = fallback.get("files") or []
chosen = (fallback.get("sets") or {}).get(key) or {}
def dest(index: object) -> str | None:
if isinstance(index, int) and 0 <= index < len(table):
return str(table[index][0])
return None
files = [d for d in (dest(i) for i in chosen.get("files") or []) if d]
optional = {d for d in (dest(i) for i in chosen.get("optional") or []) if d}
return files, optional
def _named_dests(names: Iterable[str], pool: Iterable[str]) -> list[str]:
wanted = {name.casefold() for name in names}
return [dest for dest in pool if Path(dest).name.casefold() in wanted]
from installer.game_folders import GamesPlan, _GamesDocs, _plan_read_folders, _read_folder_games
from installer.games_index import GAMES, GAMES_REASONS, GamesData, GamesUnavailable, games_setups
def plan_games(plat: str, bios_path: Path, manifest: dict, folder: Path | None = None,
@@ -532,312 +107,6 @@ def _found_games(plan: GamesPlan, folders: list[GameFolder],
return found
def _arcade_lookup(doc: dict) -> dict:
"""An arcade document as the lookups of _read_arcade read it."""
parents: dict[str, list[str]] = {}
for root_set, members in (doc.get("bios") or {}).items():
for member in members:
parents.setdefault(member, []).append(root_set)
devices: dict[str, list[str]] = {}
for device, members in (doc.get("devices") or {}).items():
for member in members:
devices.setdefault(member, []).append(device)
known = set(parents) | set(devices) | set(doc.get("no_bios") or [])
# FBNeo keys a console game by its driver (msx_1942): the
# MSX, NES and Arcade lists each hold a 1942.
consoles: dict[str, list[str]] = {}
for key in known:
driver, cut, stem = key.partition("_")
if cut and stem:
consoles.setdefault(stem, []).append(driver)
return {
"parents": parents, "devices": devices, "consoles": consoles,
"plain": set(doc.get("no_bios") or []),
"support": (set(doc.get("bios_sets") or []) | set(doc.get("bios") or {})
| set(doc.get("devices") or {})),
}
class _GamesDocs:
"""The system and arcade documents of install/games/, each read once."""
def __init__(self, data: GamesData, network: bool) -> None:
self.data = data
self.network = network
self.systems: dict[str, SystemData] = {}
self.arcades: dict[str, dict] = {}
def system(self, system: str) -> SystemData:
if system not in self.systems:
self.systems[system] = SystemData(self.data.doc("systems", system, self.network))
return self.systems[system]
def arcade(self, name: str) -> dict | None:
if name not in self.arcades:
doc = self.data.doc("arcade", name, self.network)
self.arcades[name] = {} if doc is None else _arcade_lookup(doc)
return self.arcades[name] or None
def _folder_readers(row_systems: list[str], profiles: list[str | None],
docs: _GamesDocs) -> set[str]:
"""The cores of a folder's profiles that read its first system."""
readers: set[str] = set()
if row_systems:
cores = docs.system(row_systems[0]).cores
for profile in profiles:
readers |= {name for name in (cores.get(profile or "") or {}).get("order") or []
if isinstance(name, str)}
return readers
def _count_game(plan: GamesPlan, game_folder: GameFolder, row_systems: list[str],
facts: GameFacts) -> None:
"""Count a game under each system of its folder."""
for system in row_systems:
info = plan.systems.setdefault(system, {"folders": [], "games": 0,
"recognised": 0, "regions": None})
if game_folder.path.name not in info["folders"]:
info["folders"].append(game_folder.path.name)
info["games"] += 1
info["recognised"] += int(facts.recognised)
def _read_folder_games(plan: GamesPlan, game_folder: GameFolder, games: list[Path],
docs: _GamesDocs, cache: _ScanCache,
progress: ReadCounter) -> list[GameFacts]:
"""Read the games of one folder: the ones that are not support sets."""
row = game_folder.row
row_systems = [str(s) for s in row.get("systems") or []]
arcade = row.get("arcade") if isinstance(row.get("arcade"), str) else None
profiles = _folder_profiles(row)
if row_systems and not row.get("profile") and "fallback" not in row:
plan.unknown_core += [s for s in row_systems if s not in plan.unknown_core]
readers = _folder_readers(row_systems, profiles, docs)
group: list[GameFacts] = []
for path in games:
shown = os.path.relpath(path, plan.root).replace(os.sep, "/")
facts = GameFacts(path, shown, game_folder)
progress.done += 1
if row.get("arcade"):
_read_arcade(facts, docs.arcade(arcade) if arcade else None,
_console_hints(game_folder))
elif row_systems:
read_game(facts, row_systems[0], docs.system(row_systems[0]), readers, cache)
progress.step()
if facts.kind == "support":
plan.support += 1
continue
plan.games.append(facts)
group.append(facts)
_count_game(plan, game_folder, row_systems, facts)
return group
def _plan_read_folders(plan: GamesPlan, read: list[tuple[GameFolder, list[GameFacts]]],
fallback: dict, entries: dict[str, dict], docs: _GamesDocs) -> None:
"""Add to the plan what the games read in each folder need."""
for game_folder, group in read:
if group:
for profile in _folder_profiles(game_folder.row):
if profile:
plan.profiles.add(str(profile))
else:
plan.any_core.update(str(s) for s in game_folder.row.get("systems") or [])
arcade = game_folder.row.get("arcade")
sets = (docs.arcade(arcade) or {}).get("support", set()) \
if isinstance(arcade, str) else set()
_plan_game_folder(plan, game_folder, group, fallback, entries, docs.system, sets)
# Systems whose folder an arcade core reads as arcade sets, by their bare
# set name. Any other system's sets are console games, keyed by driver.
_ARCADE_SYSTEMS = frozenset({"arcade", "neogeo", "snk-neogeo", "sega-naomi", "sega-naomi2",
"sammy-atomiswave", "sega-stv", "cps1", "cps2", "cps3"})
def _console_hints(folder: GameFolder) -> tuple[str, ...] | None:
"""None for an arcade folder, else the words of its systems and its
name, which tell one console's driver prefix from another's."""
systems = [str(s) for s in folder.row.get("systems") or []]
if not systems or any(s in _ARCADE_SYSTEMS or s.startswith("arcade") for s in systems):
return None
words = {word for system in systems for word in system.split("-")}
words.add(folder.path.name.lower())
return tuple(sorted(words))
def _read_arcade(facts: GameFacts, index: dict | None,
console: tuple[str, ...] | None = None) -> None:
"""An arcade set: a BIOS or device set (not a game), a game and the BIOS
sets its romof chain reaches, or a name the core's DAT does not hold. In
a console's folder (console, the words of its systems) a set is looked
for under the core's driver name for it (msx_1942): the bare name is an
arcade set's."""
if _suffix(facts.path.name) not in ARCADE_SUFFIXES:
facts.kind = "support"
return
name = os.path.splitext(facts.path.name)[0].lower()
if index is None:
facts.reason = "not_recognised"
return
if name in index["support"]:
facts.kind = "support"
return
keys = [name]
if console is not None:
drivers = index.get("consoles", {}).get(name, [])
hinted = [d for d in drivers if d in console]
# A prefix no word of the folder names: every console's driver of
# that name, so that the right one is among them.
keys = [f"{d}_{name}" for d in sorted(hinted or drivers)] or [name]
parents = {p for key in keys for p in index["parents"].get(key, ())}
devices = {d for key in keys for d in index["devices"].get(key, ())}
facts.parents, facts.devices = tuple(sorted(parents)), tuple(sorted(devices))
if parents or devices or any(key in index["plain"] for key in keys):
facts.recognised = True
facts.title = keys[0] if len(keys) == 1 else name
else:
facts.reason = "unknown_set"
def _read_by_profiles(entries: dict[str, dict], dest: str, profiles: list[str]) -> bool:
"""Whether one of a folder's profiles reads dest, every core when the
entry names none."""
cores = (entries.get(dest) or {}).get("cores")
return cores is None or any(name in cores for name in profiles)
def _plan_arcade_folder(plan: GamesPlan, games: list[GameFacts], entries: dict[str, dict],
profiles: list[str], sets: dict[str, tuple[list[str], set[str]]],
arcade_sets: Collection[str]) -> None:
"""Add to the plan the BIOS and device sets an arcade folder's games need."""
# A BIOS set is looked for in the whole list, as the platform's own
# declaration (neogeo.zip) and as the core's (fbneo/neogeo.zip).
usable = [dest for dest in entries if _read_by_profiles(entries, dest, profiles)]
whole = False
for game in games:
if game.reason:
whole = True
continue
names = set(game.parents + game.devices)
needed = _named_dests([f"{name}.zip" for name in names], usable)
held = {Path(dest).name.casefold() for dest in needed}
if any(f"{name}.zip".casefold() not in held for name in names):
game.reason = "no_parent"
whole = True
continue
plan.keep.update(needed)
plan.required.update(needed)
if whole:
# Every set of the folder, and every BIOS set the core reads.
own = _named_dests([f"{name}.zip" for name in arcade_sets],
[dest for dest in usable
if (entries.get(dest) or {}).get("cores") is not None])
plan.keep.update(own)
plan.required.update(own)
for files, optional in sets.values():
plan.keep.update(files)
plan.required.update(dest for dest in files if dest not in optional)
def _region_kept(files: list[str], games: list[GameFacts], entries: dict[str, dict],
system: str, info: SystemData,
profile: str) -> tuple[list[str], bool, frozenset | None]:
"""The files of a set the games' regions keep, whether every region is
kept, and the regions it was narrowed to (None when not narrowed)."""
rule = info.rules.get(profile)
region_free = {str(name).casefold() for name in (rule or {}).get("region_free") or []}
tagged = any((entries.get(dest) or {}).get("regions") for dest in files)
if not tagged:
return list(files), False, None
if region_rule_keeps_all(rule, games):
return list(files), True, None
proofs = [core_regions(game, system, info, profile) for game in games]
needed = needed_regions(rule or {}, proofs)
if needed is None:
for game, proof in zip(games, proofs):
if proof is None and not game.reason:
game.reason = ("not_read" if not game.read
else "not_recognised" if not game.recognised
else "region_unknown")
return list(files), True, None
kept = keep_regions(files, entries, needed, region_free)
if len(kept) < len(files):
return kept, False, needed
return kept, True, None
def _plan_set_extras(plan: GamesPlan, info: SystemData, profile: str, games: list[GameFacts],
files: list[str], kept: list[str], entries: dict[str, dict],
profiles: list[str]) -> None:
"""Add to the plan the games/extras.yml files of a set that fire."""
for extra in info.extras:
if extra.get("profile") != profile or not _extra_fires(extra, games):
continue
if extra.get("region_files"):
named = [dest for dest in kept if (entries.get(dest) or {}).get("regions")]
else:
names = extra.get("files") or []
named = _named_dests(names, files) or _named_dests(
names, [dest for dest in entries if _read_by_profiles(entries, dest, profiles)])
plan.keep.update(named)
if extra.get("required") or extra.get("filename_override"):
plan.required.update(named)
def _plan_game_folder(plan: GamesPlan, game_folder: GameFolder, games: list[GameFacts],
fallback: dict, entries: dict[str, dict],
system_data: Callable[[str], SystemData],
arcade_sets: Collection[str] = ()) -> None:
"""Add to the plan what one folder's games need. arcade_sets are the
BIOS and device sets of the folder's arcade index."""
row = game_folder.row
profiles = [name for name in _folder_profiles(row) if name]
if not row.get("systems"):
# A port's own folder, read by one core and named by no system: what
# that core loads.
named = [dest for dest, entry in entries.items()
if any(name in (entry.get("cores") or []) for name in profiles)]
plan.keep.update(named)
plan.required.update(named)
return
keys = _folder_set_keys(row, fallback)
sets: dict[str, tuple[list[str], set[str]]] = {key: _set_rows(fallback, key) for key in keys}
if row.get("arcade"):
_plan_arcade_folder(plan, games, entries, profiles, sets, arcade_sets)
return
narrowed: dict[str, set[str]] = {}
wide: set[str] = set()
for key, (files, optional) in sets.items():
system, _, profile = key.partition("|")
info = system_data(system)
kept, every, narrowed_to = _region_kept(files, games, entries, system, info, profile)
if every:
wide.add(system)
elif narrowed_to is not None:
narrowed.setdefault(system, set()).update(narrowed_to)
plan.keep.update(kept)
plan.required.update(dest for dest in kept if dest not in optional)
_plan_set_extras(plan, info, profile, games, files, kept, entries, profiles)
for system, regions in narrowed.items():
state = plan.systems.get(system)
# Said only where no core of the folder keeps the other regions.
if state is not None and system not in wide:
state["regions"] = sorted(set(state["regions"] or ()) | regions)
def _extra_fires(extra: dict, games: list[GameFacts]) -> bool:
"""Whether a games/extras.yml entry applies to these games. A trigger on
header bytes this file does not read applies: the file stays."""
trigger = extra.get("trigger") or {}
if trigger.get("every_game") or trigger.get("header"):
return True
extensions = {str(ext).lower() for ext in trigger.get("extensions") or []}
return any(_suffix(game.path.name) in extensions for game in games)
def games_lines(plan: GamesPlan, manifest: dict | None, every: bool = False) -> list[str]:
"""The games read, in words: the head of the plan and of --check."""
count = len(plan.games)
+230
View File
@@ -0,0 +1,230 @@
"""The games index under install/games/: where it lives, its bounds, the setups it covers."""
from __future__ import annotations
import hashlib
import http.client
import json
import re
import urllib.error
from pathlib import Path
from installer.cards import games_part
from installer.destination import card_root_above
from installer.detect_os import detect_os, detected_os
from installer.game_files import _cache_write, cache_folder
from installer.handheld_app import card_app
from installer.manifest import _SHA256_RE
from installer.network import _fetch_json, _read_capped
from installer.offline import LOCAL, _Offline, offline_words
from installer.remote import active_remote
# --- Games on the card -------------------------------------------------------
#
# --for-games keeps what the games of a card need, read from install/games/
# (scripts/generate_games_index.py): per system the files its default core
# reads (a fallback set), one region where the game's own bytes prove it,
# and for an arcade set the BIOS sets its romof chain reaches. Any doubt
# keeps the wider set: the game must run.
GAMES_FORMAT = 1
def games_url(path: str) -> str:
"""Where install/games/<path> is read from in this run."""
return f"{active_remote().base}/install/games/{path}"
MAX_GAMES_INDEX_BYTES = 4 * 1024 * 1024
MAX_GAMES_DOC_BYTES = 32 * 1024 * 1024
_GAMES_DOC_NAME = re.compile(r"^(setups|fallback|systems|arcade)/[A-Za-z0-9_.+-]+\.json$")
# Why a game got every BIOS of its system: said with its name.
GAMES_REASONS = {
"not_recognised": "not recognised",
"not_read": "format not read",
"region_unknown": "region not proven",
"unknown_set": "set unknown to the core",
"no_parent": "BIOS set not in the list",
}
class GamesUnavailable(Exception):
"""install/games/ could not be read: the run keeps the wider set."""
def __init__(self, reason: str, words: str) -> None:
super().__init__(words)
self.reason = reason
self.words = words
class GamesData:
"""install/games/ for this run: index.json, then each document it lists,
checked against the sha256 the index gives. Read from the games folder
beside --manifest, the cache, or the network, in that order."""
def __init__(self) -> None:
self.index: dict | None = None
self.docs: dict[str, dict] = {}
def reset(self) -> None:
self.index = None
self.docs = {}
@staticmethod
def beside() -> Path | None:
return LOCAL.manifest.parent / "games" if LOCAL.manifest is not None else None
@staticmethod
def cached(name: str) -> Path | None:
root = cache_folder()
return root / "games" / name if root is not None else None
def load_index(self, network: bool = True, attempts: int = 2) -> dict:
if self.index is not None:
return self.index
raw = None
beside = self.beside()
if beside is not None:
raw = _read_capped(beside / "index.json", MAX_GAMES_INDEX_BYTES)
cached = self.cached("index.json")
reason = "offline"
failure = ""
if raw is None and network:
try:
raw = _fetch_json(games_url("index.json"), MAX_GAMES_INDEX_BYTES,
"games index", attempts=attempts, raw=True)
except _Offline:
reason = "offline"
except (urllib.error.URLError, OSError, ValueError,
http.client.HTTPException) as exc:
reason = "network"
failure = str(exc)
else:
if cached is not None:
_cache_write(cached, raw)
if raw is None and cached is not None:
raw = _read_capped(cached, MAX_GAMES_INDEX_BYTES)
if raw is None:
raise GamesUnavailable(reason, (
f"the games index is not on this machine and {offline_words()}"
if reason == "offline"
else f"the games index could not be downloaded ({failure})"))
try:
data = json.loads(raw.decode("utf-8"))
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
raise GamesUnavailable("invalid", f"the games index is not valid ({exc})") from exc
if (not isinstance(data, dict) or data.get("format_version") != GAMES_FORMAT
or not all(isinstance(data.get(kind), dict)
for kind in ("setups", "fallback", "systems", "arcade"))):
raise GamesUnavailable("invalid", "the games index is of another format")
self.index = data
return data
@staticmethod
def _doc_held(beside: Path | None, cached: Path | None, name: str, digest: str,
limit: int) -> bytes | None:
"""A listed document beside --manifest or in the cache, of its sha256."""
for source in (beside / name if beside is not None else None, cached):
if source is None:
continue
found = _read_capped(source, limit)
if found is not None and hashlib.sha256(found).hexdigest() == digest:
return found
return None
@staticmethod
def _doc_fetched(kind: str, name: str, digest: str, limit: int, attempts: int,
cached: Path | None) -> bytes | None:
"""A listed document downloaded and cached, None when offline."""
try:
fetched = _fetch_json(games_url(name), limit, f"games {kind}",
attempts=attempts, raw=True)
except _Offline:
return None
except (urllib.error.URLError, OSError, ValueError,
http.client.HTTPException) as exc:
raise GamesUnavailable(
"network", f"install/games/{name} could not be downloaded ({exc})") from exc
if hashlib.sha256(fetched).hexdigest() != digest:
raise GamesUnavailable("invalid", f"install/games/{name} is not the one "
"the games index lists")
if cached is not None:
_cache_write(cached, fetched)
return fetched
def doc(self, kind: str, key: str, network: bool = True,
attempts: int = 2) -> dict | None:
"""One listed document, None when the index lists none of that name."""
entry = self.load_index(network, attempts).get(kind, {}).get(key)
if not isinstance(entry, dict):
return None
name = str(entry.get("file", ""))
digest = str(entry.get("sha256", ""))
if not _GAMES_DOC_NAME.match(name) or not _SHA256_RE.match(digest):
raise GamesUnavailable("invalid", f"the games index names {name!r}")
if name in self.docs:
return self.docs[name]
limit = min(int(entry.get("size") or 0), MAX_GAMES_DOC_BYTES)
beside = self.beside()
cached = self.cached(f"{digest}.json")
raw = self._doc_held(beside, cached, name, digest, limit)
if raw is None and network:
raw = self._doc_fetched(kind, name, digest, limit, attempts, cached)
if raw is None:
raise GamesUnavailable("offline", f"install/games/{name} is not on this machine "
f"and {offline_words()}")
try:
data = json.loads(raw.decode("utf-8"))
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
raise GamesUnavailable("invalid", f"install/games/{name} is not valid") from exc
if not isinstance(data, dict):
raise GamesUnavailable("invalid", f"install/games/{name} is not valid")
self.docs[name] = data
return data
GAMES = GamesData()
# --- Where the games are -----------------------------------------------------
def _setup_key(name: str) -> str:
return re.sub(r"[^a-z0-9]+", "-", name.lower()).strip("-")
def games_setups(index: dict, plat: str, bios_path: Path, os_type: str = "") -> list[str]:
"""The setups whose rom folders this BIOS folder's games may sit in,
best first: the system detection named (muOS, ArkOS), else the
platform's own."""
setups = index.get("setups") or {}
named = detected_os(plat, bios_path)
if not named:
above = card_root_above(bios_path)
named = above[0] if above else ""
if not named:
# --platform names the list, not the card: the full screen passes
# --platform=retroarch for a muOS card, detection never runs in that
# run, and the card's ROMS/ was never read. The card's own files
# name its system, as card_app reads them for the app.
card = card_app(bios_path)
named = card.system if card else ""
if not named:
# --dest on a handheld's games part from a computer (ArkOS's
# EASYROMS/bios): detection never ran there, and the card's own rom
# folders sit beside the BIOS folder.
part = games_part(bios_path.parent)
named = part.name if part else ""
key = _setup_key(named) if named else ""
found = [name for name in sorted(setups) if key and (name == key or name.startswith(key + "-"))]
if not found and plat in setups:
found = [plat]
windows = f"{plat}-windows"
if (os_type or detect_os()) == "windows" and windows in setups:
found.insert(0, windows)
return found
+1 -1
View File
@@ -53,7 +53,7 @@ from installer.terminal import terminal_size
from installer.text import _no_widow, _pad, _plural, _wrap, agree, cell_width, format_size, screen_text
if TYPE_CHECKING:
from installer.games import GamesPlan
from installer.game_folders import GamesPlan
from installer.tally import Tally
+1 -1
View File
@@ -94,7 +94,7 @@ from installer.text import _plural, format_size, screen_text
if TYPE_CHECKING:
from installer.from_folders import FolderIndex, LocalCopy, Unreadable
from installer.games import GamesPlan
from installer.game_folders import GamesPlan
from installer.run_setups import _InstallRun
+1 -1
View File
@@ -12,7 +12,7 @@ from installer.datadirs import select_data_directories
from installer.destination import dest_drive, shown_bios_folder
from installer.embedded import CARD_PLATFORMS
from installer.frontend_folders import SAVE_NOT_KNOWN, save_directory_for, save_where_words
from installer.games import GAMES_REASONS
from installer.games_index import GAMES_REASONS
from installer.manifest import OTHER_SYSTEMS, entry_copies
from installer.narrowing import narrow
from installer.paths import SavePlace, spelled
+1 -1
View File
@@ -503,7 +503,7 @@ those it imports:
| Files and folders | `host_fs`, `paths`, `filesystems`, `config_values`, `local_check`, `safe_replace` |
| This machine | `detection_notes`, `detect_os`, `detect_frontends`, `cards`, `device`, `cores`, `frontend_folders`, `setups` |
| Sources of files | `offline`, `network`, `lists`, `pack_source`, `from_folders`, `diagnosis`, `download`, `datadirs`, `standalone` |
| What a run keeps | `narrowing`, `choices`, `romm`, `install_record`, `game_files`, `games`, `tally`, `space`, `fetch_plan` |
| What a run keeps | `narrowing`, `choices`, `romm`, `install_record`, `game_files`, `games_index`, `game_regions`, `game_folders`, `games`, `tally`, `space`, `fetch_plan` |
| Line mode | `destination`, `line_folder`, `line_menu`, `plan_words`, `plan`, `failure_causes`, `outcome` |
| Runs and actions | `arguments`, `run_setups`, `run_files`, `run`, `pack`, `removal_plan`, `removal`, `backups`, `handheld_app`, `cli` |
| Full screen | `tui_state`, `tui_routes`, `tui_commands`, `tui_keys`, `tui_paint`, `tui_choice`, `tui_folder_scan`, `tui_rows`, `tui_frame`, `tui_plan_view`, `tui_outcome`, `tui_where_views`, `tui_screens`, `tui_help`, `tui_steps`, `tui_folder_actions`, `tui_actions`, `tui_input`, `tui_receive`, `tui_list_words`, `tui_engine`, `tui_first_screen`, `tui_terminal`, `tui_session` |