Files
libretro/scripts/packs/platformpack.py
T
Abdessamad Derraz 5b3f0c3f4c fix: settle a platform declaration in one place
The pack builder and the manifest each coded the copy a declaration
ships: resolve, agnostic substitute, frontend verdict, then the cores'
choice. The builder skipped the cores' choice once an earlier
declaration of the path was refused, and shipped the copy the name found
while the installer placed the variant the core accepts.
DeclarationJudge in packs/declsettle.py now settles it for both, and the
cores choose on every copy shipped.
2026-11-23 21:02:46 +01:00

1311 lines
53 KiB
Python

"""Building a platform pack, whole or cut per system: plan, variant choice, slot settlement, the archive."""
from __future__ import annotations
import contextlib
import os
import tempfile
import zipfile
from collections.abc import Callable, Iterable
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, TypedDict
from model.registry import load_platform_registry
from core.hashing import compute_hashes
from core.options import reject_unknown
from core.paths import EMULATORS_DIR, declared_destination
from core.ziptools import is_archive_path
from model.alternatives import core_file_spares
from model.declarations import OUTER_ROOTS, declared_root
from model.platforms import load_platform_config
from model.profiles import installed_data_refs_in, load_emulator_profiles
from model.releaserecord import PACK_SUFFIX
from packs import slots
from packs.archive import (
CASE_FOLDING_OS,
PackRecord,
Placed,
add_pack_member,
add_zip_to_pack,
extract_zip_to_archive,
same_bytes,
write_generated_member,
writing_pack,
)
from packs.datatrees import (
core_data_trees,
data_directory_members,
pack_markers,
)
from packs.declsettle import DeclarationJudge, Settlement
from packs.naming import system_display_name
from packs.extras import (
ExtraResolver,
collect_emulator_extras,
detect_extras_prefix,
extra_full_destination,
extra_region_key,
extras_for_systems as _extras_for_systems,
platform_region_groups,
undeclared_report,
)
from packs.naming import file_name_part, pack_narrowings, platform_pack_name, platform_pack_stem
from packs.packcounts import PackCounts, pack_counts
from packs.paths import flat_arcname, platform_destination, save_layout
from packs.platformreport import print_platform_pack, print_split_packs
from packs.readme import build_agnostic_rename_readme, build_readme, save_place
from resolve import packfiles as packresolve, regions as resolve_regions, slot as slot_mod
from resolve.contentlayout import TARGET_KEY
from resolve.cores import platform_scope_systems, system_scope_error
from resolve.gamedata import GameDataCut, without_game_data
from resolve.nativemode import (
hash_mismatch_excludes_file, platform_mode, reads_file_contents,
)
from resolve.packfiles import (
download_external,
preferred_entries as _preferred_entries,
resolve_file,
)
from resolve.validation import (
build_validation_index,
parse_validation,
destination_owners,
existence_discrepancy,
extra_alternatives,
)
if TYPE_CHECKING:
from typing_extensions import Unpack
def _case_twin_pairs(
twins: list[str], seen_destinations: set[str], base_dest: str, flatten: bool,
) -> list[tuple[str, str]]:
"""Each spelling left out, with the spelling the pack holds."""
held = {
flat_arcname(dest, base_dest, flatten).lower(): flat_arcname(dest, base_dest, flatten)
for dest in seen_destinations
}
return sorted({(twin, held[twin.lower()]) for twin in twins if twin.lower() in held})
class _VariantOptions(TypedDict, total=False):
"""The keyword options of _select_variants."""
regions: list[str] | None
one_per_slot: bool
required_only: bool
undeclared: list[dict] | None
zip_contents: dict | None
data_registry: dict | None
slot_overrides: dict[str, str] | None
def _select_variants(
config: dict,
pack_systems: dict,
emulators_dir: str,
db: dict,
base_dest: str,
emu_profiles: dict | None,
target_cores: set[str] | None,
source: str,
**options: Unpack[_VariantOptions],
) -> tuple[set, list, list]:
"""Which regional and slot alternatives this pack leaves out.
Both reductions are decided once, over the platform baseline and the
core extras together: deciding them separately is how the report and
the pack came to withdraw different files for the same request.
Returns the destinations to drop, the groups kept only as a
fallback, and the slots no profile puts in order.
"""
reject_unknown("_select_variants", options, _VariantOptions.__optional_keys__)
regions = options.get("regions")
one_per_slot = options.get("one_per_slot", False)
region_drops: set[str] = set()
region_fallbacks: list[str] = []
# One grouping for both passes: a file judged in its own system by one
# pass must not land in every system of its profile for the other.
region_groups: dict[str, list[tuple[str, str]]] = {}
held: set[str] = set()
if regions or one_per_slot:
region_groups, _extra_dests = platform_region_groups(
config,
pack_systems,
emulators_dir,
db,
base_dest,
emu_profiles,
target_cores=target_cores,
include_extras=(source != "platform"),
include_all=(source == "truth"),
required_only=options.get("required_only", False),
held_out=held,
undeclared=options.get("undeclared"),
zip_contents=options.get("zip_contents"),
data_registry=options.get("data_registry"),
slot_overrides=options.get("slot_overrides"),
)
if regions:
region_index = resolve_regions.platform_region_index(
config, emu_profiles or {}, target_cores
)
region_drops = resolve_regions.resolve_region_drops(
region_groups, region_index, regions, held
)
region_fallbacks = resolve_regions.fallback_groups(
region_groups, region_index, regions
)
slot_undecidable: list[str] = []
if one_per_slot:
slot_drops, slot_undecidable = one_per_slot_drops(
config, emu_profiles, target_cores, region_groups, region_drops, held
)
region_drops = region_drops | slot_drops
return region_drops, region_fallbacks, slot_undecidable
def one_per_slot_drops(
config: dict,
emu_profiles: dict | None,
target_cores: set[str] | None,
region_groups: dict[str, list[tuple[str, str]]],
region_drops: set[str],
held: set[str],
) -> tuple[set[str], list[str]]:
"""What --one-per-slot withdraws once the regions have, and the slots no
declared order decides. The pack and the manifest both read it."""
slot_groups = {
sys_id: [(d, n) for d, n in members if d not in region_drops]
for sys_id, members in region_groups.items()
}
return slot_mod.resolve_slot_drops(
slot_groups,
slot_mod.platform_slot_index(config, emu_profiles or {}, target_cores),
held,
)
def _matches_an_alternative(fe: dict, local_path: str, profiles: dict) -> bool:
"""Whether the copy is another version the same profile accepts there.
A profile lists every version its code takes under one name (each GRP
of Raze's table, eduke32's demo and retail NWINTER.GRP). The extra
carries the first, and the held copy matching a sibling is no mismatch.
"""
siblings = extra_alternatives(fe, profiles)
if len(siblings) < 2:
return False
# A sibling the code takes without a check (Raze's "BLOOD: Unknown
# Version") accepts whatever copy fills the slot.
if any(not parse_validation(entry.get("validation")) for entry in siblings):
return True
actual = compute_hashes(local_path, frozenset({"sha1", "md5", "crc32"}))
for entry in siblings:
for algorithm in ("sha1", "md5", "crc32"):
declared = entry.get(algorithm)
values = declared if isinstance(declared, list) else [declared]
if any(v and str(v).lower() == actual.get(algorithm, "") for v in values):
return True
return False
def _left_out_lines(cut: GameDataCut, systems: set[str], cores: bool) -> list[str]:
"""The game data a --no-game-data build left out, and why, one line each.
``systems`` limits the platform layer to the systems this pack covers,
and ``cores`` says whether this pack collected the core files itself:
a split part does not, its run names them once.
"""
kept = [
item for item in cut.left_out
if (item.layer == "platform" and item.system in systems)
or (item.layer == "core" and cores)
]
return GameDataCut(cut.config, cut.profiles, kept).report_lines()
class PackNarrowing(TypedDict, total=False):
"""How a platform pack is narrowed, shared by a pack and its split parts."""
emulators_dir: str
zip_contents: dict | None
data_registry: dict | None
emu_profiles: dict | None
target_cores: set[str] | None
required_only: bool
source: str
regions: list[str] | None
target_name: str | None
one_per_slot: bool
offline: bool | None
no_game_data: bool
class _PartScope(TypedDict, total=False):
"""What one split part keeps of the platform."""
system_filter: list[str] | None
precomputed_extras: list[dict] | None
extras_only: bool
class PackOptions(PackNarrowing, _PartScope, total=False):
"""The keyword options of generate_pack."""
flatten: bool
pack_name: str | None
record: PackRecord | None
within: dict[str, str] | None
uncached_out: set[str] | None
counts: list[PackCounts] | None
@dataclass(frozen=True)
class _PackSettings:
"""PackOptions with their defaults."""
emulators_dir: str = EMULATORS_DIR
zip_contents: dict | None = None
data_registry: dict | None = None
emu_profiles: dict | None = None
target_cores: set[str] | None = None
required_only: bool = False
system_filter: list[str] | None = None
precomputed_extras: list[dict] | None = None
extras_only: bool = False
source: str = "full"
flatten: bool = True
regions: list[str] | None = None
target_name: str | None = None
one_per_slot: bool = False
offline: bool | None = None
pack_name: str | None = None
no_game_data: bool = False
record: PackRecord | None = None
within: dict[str, str] | None = None
uncached_out: set[str] | None = None
counts: list[PackCounts] | None = None
@dataclass
class _PlatformPlan:
"""What a platform pack is built from, settled before its first member."""
platform_name: str
platforms_dir: str
db: dict
bios_dir: str
opts: _PackSettings
config: dict
emu_profiles: dict
zip_contents: dict
cut: GameDataCut | None
narrowings: list[tuple[str, str]]
zip_path: str
validation_index: dict
platform_profiles: dict[str, dict]
validation_owners: dict
slot_overrides: dict[str, str]
pack_systems: dict
preferred_entries: dict[str, int] = field(default_factory=dict)
undeclared: list[dict] | None = None
region_drops: set[str] = field(default_factory=set)
region_fallbacks: list = field(default_factory=list)
slot_undecidable: list[str] = field(default_factory=list)
@property
def verification_mode(self) -> str:
return platform_mode(self.config)
@property
def platform_display(self) -> str:
return self.config.get("platform", self.platform_name)
@property
def base_dest(self) -> str:
return self.config.get("base_destination", "")
@property
def case_insensitive(self) -> bool:
# Case-insensitive dedup only for platforms targeting Windows/macOS.
# Linux-only platforms (Batocera, Recalbox, RetroDECK, Lakka, RomM)
# are case-sensitive and may have distinct files like DISK.ROM vs disk.rom.
return self.config.get("case_insensitive_fs", False)
def _plan_platform_pack(
platform_name: str,
platforms_dir: str,
db: dict,
bios_dir: str,
output_dir: str,
opts: _PackSettings,
) -> _PlatformPlan | str:
"""The configuration, scope and checks a platform pack reads, or the
reason the systems asked for leave nothing to build."""
config = load_platform_config(platform_name, platforms_dir)
if opts.target_name:
config = {**config, TARGET_KEY: opts.target_name}
zip_contents = {} if opts.zip_contents is None else opts.zip_contents
emu_profiles = opts.emu_profiles
if emu_profiles is None:
# The pipeline always passes them, but a caller that does not must get the
# same pack, not one built without validation, slots or agnostic cores.
emu_profiles = load_emulator_profiles(opts.emulators_dir)
# Cut from the full config and profiles: the category of a platform
# entry is read from the profiles' game data entries.
cut = (
without_game_data(config, emu_profiles, opts.data_registry, opts.target_cores)
if opts.no_game_data else None
)
if cut is not None:
config, emu_profiles = cut.config, cut.profiles
narrowings = pack_narrowings(
opts.source, opts.regions, opts.target_name, opts.one_per_slot, opts.required_only,
no_game_data=opts.no_game_data,
)
zip_path = os.path.join(
output_dir,
platform_pack_name(
platform_name, platforms_dir, narrowings, opts.system_filter, opts.pack_name
),
)
if opts.record is None:
os.makedirs(output_dir, exist_ok=True)
# Build emulator-level validation index from the platform's own
# emulators (same scope as verify.py)
from resolve.cores import resolve_platform_cores
validation_index = {}
platform_profiles: dict[str, dict] = {}
if emu_profiles:
platform_profiles = {
name: emu_profiles[name]
for name in resolve_platform_cores(config, emu_profiles)
}
validation_index = build_validation_index(platform_profiles)
validation_owners = destination_owners(platform_profiles)
slot_overrides = slots.pack_overrides(
config, emu_profiles or {}, db, zip_contents, opts.data_registry,
target_cores=opts.target_cores, source=opts.source,
)
pack_systems = platform_scope_systems(
config, emu_profiles or {}, opts.target_cores, opts.system_filter
)
if opts.system_filter and not pack_systems:
return system_scope_error(config, platform_name, opts.system_filter, opts.target_name)
if opts.extras_only:
# The part of a split that carries the core extras no platform system
# owns: the platform's own files and trees went to the other parts.
pack_systems = {}
plan = _PlatformPlan(
platform_name, platforms_dir, db, bios_dir, opts, config, emu_profiles,
zip_contents, cut, narrowings, zip_path, validation_index, platform_profiles,
validation_owners, slot_overrides, pack_systems,
)
_settle_pack_choices(plan)
return plan
def _settle_pack_choices(plan: _PlatformPlan) -> None:
"""The declaration each destination takes and the variants left out."""
opts = plan.opts
if opts.source != "truth":
# Decided among the declarations this pack keeps: a preferred entry
# that --required-only drops must not silence its required twin, or
# the destination leaves the pack without a trace (RetroDECK's four
# Atari 5200 ROMs, optional under atari-400-800 and required under
# atari-5200). No data directories: the choice is about the baseline.
plan.preferred_entries = _preferred_entries(
plan.pack_systems, plan.db, plan.bios_dir, plan.base_dest, opts.required_only,
plan.zip_contents, opts.offline,
)
# Region selection is decided once, over both the platform baseline and the
# core extras, so the two phases below consult a single set. Grouping uses a
# dedicated extras pass with an empty seen set: it only needs group
# membership, never destinations, and a superset of members is the safe
# direction. Runs only when --region is given.
# The gap report both extras passes read, computed once: the region
# grouping and the core complement below ask it the same question.
plan.undeclared = (
undeclared_report(
plan.config, opts.emulators_dir, plan.db, plan.emu_profiles, opts.target_cores,
include_all=(opts.source == "truth"),
)
if opts.source != "platform" and opts.precomputed_extras is None
else None
)
plan.region_drops, plan.region_fallbacks, plan.slot_undecidable = _select_variants(
plan.config, plan.pack_systems, opts.emulators_dir, plan.db, plan.base_dest,
plan.emu_profiles, opts.target_cores, opts.source, regions=opts.regions,
one_per_slot=opts.one_per_slot, required_only=opts.required_only,
undeclared=plan.undeclared, zip_contents=plan.zip_contents,
data_registry=opts.data_registry, slot_overrides=plan.slot_overrides,
)
@dataclass
class PlatformPackOutcome:
"""What one platform pack build came to, as print_platform_pack reports it.
``refusal`` is set, and ``zip_path`` None, when the systems asked for
leave nothing to build.
"""
zip_path: str | None
refusal: str = ""
source: str = "full"
verification_mode: str = ""
total_files: int = 0
core_count: int = 0
tree_count: int = 0
file_status: dict[str, str] = field(default_factory=dict)
file_reasons: dict[str, str] = field(default_factory=dict)
core_discrepancies: list[str] = field(default_factory=list)
slot_undecidable: list[str] = field(default_factory=list)
uncached: list[tuple[str, str]] = field(default_factory=list)
left_out: list[str] = field(default_factory=list)
@dataclass
class _PlatformPackBuild:
"""One platform pack being written: its members and what each slot came to.
A platform file is settled by the DeclarationJudge the install manifest
reads too, and a core extra by the lookup build_platform_pack binds.
"""
plan: _PlatformPlan
zf: zipfile.ZipFile | PackRecord
declarations: DeclarationJudge
resolve_extra: Callable[[dict, str], tuple[str | None, str, str]]
placed: Placed = field(init=False)
total_files: int = 0
core_count: int = 0
save_count: int = 0
tree_count: int = 0
# Per-destination status. ``excluded`` means a same-named local payload
# exists but contradicts the declaration and is intentionally not shipped.
# It is distinct from a genuine coverage gap (``missing``).
file_status: dict[str, str] = field(default_factory=dict)
file_reasons: dict[str, str] = field(default_factory=dict)
# Core extras whose local copy contradicts the profile hash: packed,
# reported, and never counted against the platform's own file total.
core_discrepancies: list[str] = field(default_factory=list)
# Spellings left out as case twins of a packed file, for the README.
case_twins: list[str] = field(default_factory=list)
# (registry key, cache path) of each data directory the cache lacks.
uncached: list[tuple[str, str]] = field(default_factory=list)
def __post_init__(self) -> None:
self.placed = Placed(self.plan.case_insensitive)
def _flat(self, path: str) -> str:
return flat_arcname(path, self.plan.base_dest, self.plan.opts.flatten)
def run(self) -> None:
if self.plan.opts.source != "truth":
for sys_id, system in sorted(self.plan.pack_systems.items()):
for file_entry in system.get("files", []):
self._pack_declared(sys_id, file_entry)
# Core requirements: files platform's cores need but YAML doesn't declare
self._pack_core_files()
self._pack_trees()
self._write_readme()
def _judge_conflict(self, file_entry: dict, full_dest: str) -> None:
# Content ships under the conflicting shape (file vs dir,
# e.g. SGB1.sfc). verify.py resolves each declaration on
# its own, so this one is counted by what it resolves to:
# an unresolved one counted OK had the pack claim a file
# verify reports missing.
plan = self.plan
conflict_path, conflict_status = resolve_file(
file_entry, plan.db, plan.bios_dir, plan.zip_contents,
data_dir_registry=plan.opts.data_registry, offline=plan.opts.offline,
)
# The verdict of the main path: a zipped_file archive is
# judged by the member inside, and a frontend that hashes
# one digest accepts a file another declared hash
# contradicts, as verify judges it.
conflict_status, refusal = self.declarations.verdict(
file_entry, conflict_path, conflict_status,
)
if conflict_status == "not_found":
self.file_status[full_dest] = "missing"
self.file_reasons[full_dest] = "not found"
elif refusal:
self.file_status[full_dest] = "excluded"
self.file_reasons[full_dest] = refusal
else:
self.file_status.setdefault(full_dest, "ok")
def _pack_declared(self, sys_id: str, file_entry: dict) -> None:
plan, opts = self.plan, self.plan.opts
if opts.required_only and file_entry.get("required") is False:
return
dest, full_dest = platform_destination(file_entry, plan.base_dest)
if plan.region_drops and dest in plan.region_drops:
return
if not dest:
self._judge_unnamed(sys_id, file_entry)
return
already_packed = self.placed.holds(full_dest)
preferred = plan.preferred_entries.get(full_dest)
if preferred is not None and id(file_entry) != preferred:
return
if self.placed.conflicts(full_dest):
self._judge_conflict(file_entry, full_dest)
return
storage = file_entry.get("storage", "embedded")
if storage == "user_provided":
if not already_packed:
self._write_instructions(file_entry, full_dest)
return
settled = self.declarations.settle(
file_entry, sys_id, dest, plan.slot_overrides.get(full_dest),
)
if settled.status == "external":
self._pack_external(file_entry, full_dest)
return
if settled.status == "not_found":
if not already_packed:
self.file_status[full_dest] = "missing"
self.file_reasons[full_dest] = "not found"
return
if settled.agnostic_dir and settled.resolved:
self._note_rename(file_entry, settled.resolved, settled.agnostic_dir)
if self._settle_declared(file_entry, full_dest, settled, already_packed) and settled.path:
self._ship_declared(file_entry, full_dest, settled.path, already_packed)
def _judge_unnamed(self, sys_id: str, file_entry: dict) -> None:
# EmuDeck-style entries (system:md5 whitelist, no filename).
# Judged as verify judges it: resolved on disk, the md5
# list parsed once (case, comma lists).
plan = self.plan
fkey = f"{sys_id}/{file_entry.get('name', '')}"
held, held_status = resolve_file(
file_entry, plan.db, plan.bios_dir, plan.zip_contents,
data_dir_registry=plan.opts.data_registry, offline=plan.opts.offline,
)
if held_status == "not_found" or not held:
self.file_status[fkey] = "missing"
elif self.declarations.verdict(file_entry, held, held_status)[1]:
self.file_status[fkey] = "excluded"
else:
self.file_status.setdefault(fkey, "ok")
def _write_instructions(self, file_entry: dict, full_dest: str) -> None:
base_dest = self.plan.base_dest
self.placed.take(full_dest)
self.file_status.setdefault(full_dest, "ok")
instructions = file_entry.get(
"instructions", "Please provide this file manually."
)
instr_name = f"INSTRUCTIONS_{file_entry['name']}.txt"
instr_path = (
f"{base_dest}/{instr_name}" if base_dest else instr_name
)
write_generated_member(
self.zf,
self._flat(instr_path),
f"File needed: {file_entry['name']}\n\n{instructions}\n",
)
self.total_files += 1
def _pack_external(self, file_entry: dict, full_dest: str) -> None:
file_ext = os.path.splitext(file_entry["name"])[1] or ""
with tempfile.NamedTemporaryFile(
delete=False, suffix=file_ext, dir=packresolve.scratch_dir()
) as tmp:
tmp_path = tmp.name
# The slot is the platform's file whether the download
# arrives or not: verify cannot fetch, and a core file
# filling it on a failed download made the two differ.
self.placed.take(full_dest)
try:
if self.plan.opts.record is None and download_external(file_entry, tmp_path):
extract = file_entry.get("extract", False)
if extract and tmp_path.endswith(".zip"):
extract_zip_to_archive(tmp_path, self._flat(full_dest), self.zf)
else:
add_pack_member(self.zf, tmp_path, self._flat(full_dest))
self.file_status.setdefault(full_dest, "ok")
self.total_files += 1
else:
self.file_status[full_dest] = "missing"
self.file_reasons[full_dest] = "external download failed"
finally:
if os.path.exists(tmp_path):
os.unlink(tmp_path)
def _note_rename(self, file_entry: dict, local_path: str, agnostic_path: str) -> None:
# A filename-agnostic core's substitute ships under the declared
# name: a note says which image it is.
plan = self.plan
original_name = os.path.basename(local_path)
dest_name = file_entry.get("name", "")
if original_name == dest_name:
return
alt_names = [
entry.get("name", "")
for entry in plan.db.get("files", {}).values()
if entry.get("path", "").startswith(agnostic_path)
and entry.get("name", "") and entry.get("name", "") != original_name
]
readme_text = build_agnostic_rename_readme(dest_name, original_name, alt_names)
readme_name = f"RENAMED_{dest_name}.txt"
readme_full = f"{plan.base_dest}/{readme_name}" if plan.base_dest else readme_name
if readme_full not in self.placed.destinations:
write_generated_member(self.zf, self._flat(readme_full), readme_text)
self.placed.destinations.add(readme_full)
def _settle_declared(
self, file_entry: dict, full_dest: str, settled: Settlement, already_packed: bool,
) -> bool:
"""Record what a declaration came to, and whether its copy ships."""
plan = self.plan
if settled.refusal:
# An earlier declaration of this path shipped a file this
# one refuses: it is in the pack, untested as verify
# counts it, not excluded.
self.file_status[full_dest] = "untested" if already_packed else "excluded"
self.file_reasons[full_dest] = settled.refusal
return False
if not hash_mismatch_excludes_file(plan.verification_mode) and (
settled.status == "hash_mismatch" and settled.resolved
):
# Existence platforms accept any file at the declared path:
# their code never reads the bytes. An upstream hash that
# contradicts a local dump is reported, not acted on.
self.file_reasons.setdefault(
full_dest,
existence_discrepancy(file_entry, settled.resolved, plan.platform_display),
)
if self.file_status.get(full_dest) == "missing":
# An earlier declaration of this path found nothing, and this
# one fills it, as verify counts it.
del self.file_status[full_dest]
self.file_reasons.pop(full_dest, None)
self.file_status.setdefault(full_dest, "ok")
# Emulator-level validation: informational only for platform packs.
# Platform verification (existence/md5) is the authority for pack
# status. The copy the cores chose ships whatever an earlier
# declaration of this path came to, as the installer places it.
if settled.disagreement and self.file_status.get(full_dest) == "ok":
self.file_reasons.setdefault(
full_dest, f"{plan.platform_display} says OK but {settled.disagreement}"
)
return True
def _ship_declared(
self, file_entry: dict, full_dest: str, local_path: str, already_packed: bool,
) -> None:
within = self.plan.opts.within
if already_packed:
if full_dest not in self.placed.destinations:
self.case_twins.append(self._flat(full_dest))
return
if within is not None and not same_bytes(local_path, within.get(self._flat(full_dest))):
# The full pack holds another system's file here.
return
if self.file_status.get(full_dest) == "excluded":
# An earlier declaration refused the file this one ships.
self.file_status[full_dest] = "untested"
self.placed.take(full_dest)
extract = file_entry.get("extract", False)
flat_dest = self._flat(full_dest)
if extract and is_archive_path(local_path):
extract_zip_to_archive(local_path, flat_dest, self.zf)
elif is_archive_path(local_path):
add_zip_to_pack(local_path, flat_dest, self.zf, file_entry)
else:
add_pack_member(self.zf, local_path, flat_dest)
self.total_files += 1
def _region_dropped(self, fe: dict) -> bool:
drops = self.plan.region_drops
return bool(drops) and extra_region_key(fe, declared_root(self.plan.config)) in drops
def _spares(self, files: list[dict]) -> set[int]:
if not self.plan.opts.required_only:
return set()
return core_file_spares(files, self._region_dropped)
def _core_files(self) -> list[dict]:
plan, opts = self.plan, self.plan.opts
if opts.source == "platform":
return []
if opts.precomputed_extras is not None:
return opts.precomputed_extras
if opts.system_filter:
# The systems asked for, with what their cores need: the split
# packs get exactly this, and a full pack of one system had none.
return _extras_for_systems(
collect_emulator_extras(
plan.config, opts.emulators_dir, plan.db, set(), plan.base_dest,
plan.emu_profiles, target_cores=opts.target_cores,
include_all=(opts.source == "truth"), undeclared=plan.undeclared,
),
opts.system_filter,
)
return collect_emulator_extras(
plan.config,
opts.emulators_dir,
plan.db,
self.placed.destinations,
plan.base_dest,
plan.emu_profiles,
target_cores=opts.target_cores,
include_all=(opts.source == "truth"),
undeclared=plan.undeclared,
)
def _pack_core_files(self) -> None:
core_files = self._core_files()
spares = self._spares(core_files)
for fe in core_files:
if self.plan.opts.required_only and (fe.get("required") is False or id(fe) in spares):
continue
self._pack_core_file(fe)
def _pack_core_file(self, fe: dict) -> None:
plan = self.plan
dest = declared_destination(fe)
if self._region_dropped(fe):
return
if not dest:
return
if fe.get("root") in OUTER_ROOTS:
# Read below the roms directory or in an emulator folder: the
# pack is extracted into the BIOS folder and cannot reach
# them, the installer places these files.
return
# Core extras: collect_emulator_extras already adjusted
# destinations for slug-based platforms. Apply the effective
# prefix (base_dest, or inferred from YAML when base_dest is
# empty, as RetroDECK infers "bios"). A file read below the save
# directory goes to the pack's save folder instead.
full_dest = extra_full_destination(
fe, detect_extras_prefix(plan.config, plan.base_dest)
)
if not full_dest:
return
# Taken, a case twin on a case-insensitive filesystem, or a
# file/directory conflict (SGB1.sfc vs SGB1.sfc/): verify replays
# the same rule (packextras.hidden_extras).
block = self.placed.block(full_dest)
if block:
if block == "case_twin" and self.resolve_extra(fe, dest)[0]:
self.case_twins.append(self._flat(full_dest))
return
# The emulator's own check picks the copy, as for a declared file
# and in the emulator pack: the name led RetroArch's GC/dsp_rom.bin
# to Dolphin's obsolete v0.3.1 free ROM, beside Nintendo's coef
# ROM, a pair Dolphin warns about at every start.
local_path, status, disagreement = self.resolve_extra(fe, dest)
if not local_path:
return
self._note_core_discrepancy(fe, full_dest, local_path, status, disagreement)
flat_dest = self._flat(full_dest)
within = plan.opts.within
if within is not None and not same_bytes(local_path, within.get(flat_dest)):
return
if is_archive_path(local_path):
add_zip_to_pack(local_path, flat_dest, self.zf, fe)
else:
add_pack_member(self.zf, local_path, flat_dest)
if self.file_status.get(full_dest) in ("missing", "excluded"):
self._core_fills_declaration(fe, full_dest)
self.placed.take(full_dest)
self.core_count += 1
if fe.get("root") == "save":
self.save_count += 1
self.total_files += 1
def _note_core_discrepancy(
self, fe: dict, full_dest: str, local_path: str, status: str, disagreement: str,
) -> None:
if disagreement:
self.core_discrepancies.append(f"{full_dest}: {disagreement}")
if status == "hash_mismatch" and not _matches_an_alternative(
fe, local_path, self.plan.emu_profiles or {}
):
# The core's declared hash comes from its source, the local dump
# is what the collection holds. Shipping it keeps the emulator
# working when the code never reads the hash, and the divergence is
# reported so it can be resolved at the profile or the dump.
# file_status tracks platform declarations only, so the report
# goes to its own list and never moves the pack's own count.
self.core_discrepancies.append(
f"{full_dest}: declared hash of "
f"{fe.get('source_profile') or fe.get('source_emulator', 'core profile')}"
" does not match the packed copy"
)
def _core_fills_declaration(self, fe: dict, full_dest: str) -> None:
source_name = fe.get("source_profile") or fe.get(
"source_emulator", "core profile"
)
if reads_file_contents(self.plan.verification_mode):
# The frontend hashes what sits at the path, and the core's
# copy is not the hash it declares: the file ships for the
# core, the platform's own check still fails, as verify says.
self.file_reasons[full_dest] = (
"platform-declared hash unavailable; "
f"packed the validated {source_name} requirement, "
"which the platform check does not accept"
)
else:
# An existence frontend is satisfied by any file at the path.
self.file_status[full_dest] = "ok"
self.file_reasons.pop(full_dest, None)
def _pack_trees(self) -> None:
plan, opts = self.plan, self.plan.opts
# The trees the cores read follow them as their extras do: by the
# systems asked for, and those of no system in the part of their own.
core_trees = (
[] if opts.source == "platform"
else core_data_trees(
plan.config, plan.emu_profiles, plan.platform_name, opts.data_registry,
opts.target_cores,
)
)
if opts.extras_only:
owned = list(platform_scope_systems(plan.config, plan.emu_profiles, opts.target_cores))
core_trees = [t for t in core_trees if not _extras_for_systems([t], owned)]
elif opts.system_filter:
core_trees = _extras_for_systems(core_trees, opts.system_filter)
# Counted apart: the trees a core reads are not on the platform's
# list. MyMinUI declares nothing and its line read "6944 baseline".
self.total_files += self._pack_data_directories(plan.pack_systems, ())
self.tree_count = self._pack_data_directories({}, core_trees)
self.total_files += self.tree_count
def _pack_data_directories(self, pack_systems: dict, core_trees: Iterable[dict]) -> int:
"""Add the data directories to the pack. Returns how many files went in."""
plan = self.plan
required_only = plan.opts.required_only
added = 0
uncached: list[tuple[str, str]] = []
for src, full in data_directory_members(
pack_systems, plan.opts.data_registry, plan.platform_name, plan.base_dest,
self.placed, required_only=required_only,
core_trees=core_trees, uncached_out=uncached,
installed=installed_data_refs_in(plan.platform_name, plan.emu_profiles or {}),
):
# The installer writes a data tree member without the bit.
add_pack_member(self.zf, src, self._flat(full), executable=False)
if isinstance(self.zf, PackRecord):
self.zf.trees.append(self._flat(full))
added += 1
self.uncached.extend(uncached)
if plan.opts.uncached_out is not None:
plan.opts.uncached_out.update(ref_key for ref_key, _path in uncached)
return added
def _unfolded_twins(self) -> None:
# The spellings a folded platform counts as declared never reach
# the extras: collected as the filesystem that keeps case sees
# them, as the manifest's case_variants are (Mesen-CE's bios.col
# beside the declared BIOS.col).
plan, opts = self.plan, self.plan.opts
unfolded = collect_emulator_extras(
{**plan.config, "case_insensitive_fs": False}, opts.emulators_dir, plan.db, set(),
plan.base_dest, plan.emu_profiles, target_cores=opts.target_cores,
include_all=(opts.source == "truth"),
)
if opts.system_filter:
unfolded = _extras_for_systems(unfolded, opts.system_filter)
prefix = detect_extras_prefix(plan.config, plan.base_dest)
unfolded_spares = self._spares(unfolded)
for fe in unfolded:
if opts.required_only and (fe.get("required") is False or id(fe) in unfolded_spares):
continue
if self._region_dropped(fe):
continue
full = extra_full_destination(fe, prefix)
if (
full and self.placed.block(full) == "case_twin"
and self.resolve_extra(fe, declared_destination(fe))[0]
):
self.case_twins.append(self._flat(full))
def _write_readme(self) -> None:
# README.txt for users: personalized step-by-step per platform
plan, opts = self.plan, self.plan.opts
registry = _platform_registry(plan.platforms_dir).get(plan.platform_name, {})
install = registry.get("install", {}) or {}
keeps_case = plan.case_insensitive and any(
d.get("os") not in CASE_FOLDING_OS for d in install.get("detect", [])
)
if keeps_case and opts.source != "platform" and opts.precomputed_extras is None:
self._unfolded_twins()
save_count = self.save_count
readme_text = build_readme(
plan.platform_name, plan.platform_display, plan.base_dest, self.total_files,
len(plan.pack_systems),
source=opts.source,
contributors=registry.get("contributed_by", []),
regions=opts.regions,
fallback_systems=plan.region_fallbacks,
one_per_slot=opts.one_per_slot,
undecidable_slots=plan.slot_undecidable,
narrowings=plan.narrowings,
system_filter=opts.system_filter,
detect=install.get("detect", []),
standalone_copies=install.get("standalone_copies", []),
launch_folders=len(plan.config.get("core_system_dirs") or {}),
save_folder=(save_layout(plan.config).in_pack if save_count else ""),
save_where=(
save_place(
str(registry.get("extract_to") or ""),
save_layout(plan.config).folder, plan.base_dest,
) if save_count else ""
),
save_by_system=bool(save_count and save_layout(plan.config).by_system),
case_twins=(
_case_twin_pairs(
self.case_twins, self.placed.destinations, plan.base_dest, opts.flatten
)
if keeps_case else []
),
)
write_generated_member(self.zf, "README.txt", readme_text)
for marker in pack_markers(plan.platform_name, opts.target_name, plan.platforms_dir):
write_generated_member(
self.zf,
self._flat(f"{plan.base_dest}/{marker}" if plan.base_dest else marker),
"",
)
def outcome(self) -> PlatformPackOutcome:
plan, opts = self.plan, self.plan.opts
return PlatformPackOutcome(
zip_path=plan.zip_path,
source=opts.source,
verification_mode=plan.verification_mode,
total_files=self.total_files,
core_count=self.core_count,
tree_count=self.tree_count,
file_status=self.file_status,
file_reasons=self.file_reasons,
core_discrepancies=self.core_discrepancies,
slot_undecidable=plan.slot_undecidable,
uncached=self.uncached,
left_out=(
_left_out_lines(
plan.cut,
systems=set(plan.pack_systems) if opts.source != "truth" else set(),
cores=opts.source != "platform" and opts.precomputed_extras is None,
)
if plan.cut is not None else []
),
)
def _platform_registry(platforms_dir: str) -> dict:
"""The platforms of _registry.yml, or nothing when the file is absent."""
return load_platform_registry(platforms_dir)
def build_platform_pack(
platform_name: str,
platforms_dir: str,
db: dict,
bios_dir: str,
output_dir: str,
**options: Unpack[PackOptions],
) -> PlatformPackOutcome:
"""Build a ZIP pack for a platform. Its report is print_platform_pack's.
``no_game_data`` leaves out the platform's entries categorized as game
data and the cores' ``category: game_data`` files, and names each one.
``pack_name`` replaces the derived file name: a split part is named after
its group, and a name built from every system of a hundred-system group
is longer than a file name can be.
``counts``, when given, receives the pack's PackCounts once it is built.
``record`` builds the pack in memory (PackRecord): no archive is written,
no external file fetched, and the paths are kept on it.
``within`` is the full pack's PackRecord.sources, given to a split part:
a declared file or core extra is written only where the full pack writes
the same bytes. A part sees neither the other systems' declarations nor
their extras, so a path the full pack settles (a file/directory conflict,
a case twin, a path another system's file takes) was written again.
``uncached_out`` receives the data directories the pack carries and the
cache does not hold: the pack is written without them.
The outcome carries the ZIP path, or the refusal when the systems asked
for leave nothing to build.
"""
reject_unknown("generate_pack", options, PackOptions.__optional_keys__)
opts = _PackSettings(**options)
plan = _plan_platform_pack(platform_name, platforms_dir, db, bios_dir, output_dir, opts)
if isinstance(plan, str):
return PlatformPackOutcome(zip_path=None, refusal=plan)
declarations = DeclarationJudge(
db, bios_dir, plan.zip_contents, opts.data_registry, opts.offline,
plan.verification_mode, plan.config.get("hash_rule", ""), plan.platform_profiles,
plan.validation_index, plan.validation_owners,
)
extra_resolver = ExtraResolver(
db, bios_dir, plan.zip_contents, opts.data_registry, plan.validation_index,
plan.validation_owners, opts.offline, profiles=plan.emu_profiles,
)
def resolve_extra(fe: dict, dest: str) -> tuple[str | None, str, str]:
return extra_resolver.resolve(fe, dest)
with (
contextlib.nullcontext(opts.record) if opts.record is not None
else writing_pack(plan.zip_path)
) as zf:
build = _PlatformPackBuild(plan, zf, declarations, resolve_extra)
build.run()
if opts.counts is not None:
opts.counts.append(pack_counts(plan.zip_path, build.file_status, build.core_count))
return build.outcome()
def generate_pack(
platform_name: str,
platforms_dir: str,
db: dict,
bios_dir: str,
output_dir: str,
**options: Unpack[PackOptions],
) -> str | None:
"""Build a platform pack (build_platform_pack) and print its report.
Returns the path to the generated ZIP, or None when nothing was built.
"""
outcome = build_platform_pack(platform_name, platforms_dir, db, bios_dir, output_dir, **options)
print_platform_pack(outcome)
return outcome.zip_path
def _group_systems_by_manufacturer(
systems: dict[str, dict],
db: dict,
bios_dir: str,
) -> dict[str, list[str]]:
"""Group system IDs by manufacturer for --split --group-by manufacturer."""
from model.systems import derive_manufacturer
groups: dict[str, list[str]] = {}
for sid, sys_data in systems.items():
mfr = derive_manufacturer(sid, sys_data)
groups.setdefault(mfr, []).append(sid)
return groups
class SplitOptions(PackNarrowing, total=False):
"""The keyword options of generate_split_packs."""
records: dict[str, PackRecord] | None
uncached_out: set[str] | None
def _split_extras(
config: dict, db: dict, narrowing: PackNarrowing,
) -> tuple[list[dict], list[str]]:
"""The core extras of every part, collected once (expensive: scans 260+
emulator profiles), then distributed per group by system overlap, and
the core game data the run leaves out, named once for all parts."""
emu_profiles = narrowing["emu_profiles"] or {}
source = narrowing["source"]
target_cores = narrowing["target_cores"]
base_dest = config.get("base_destination", "")
# Each part cuts the platform's own entries from the full profiles. The
# extras are collected once here, already without the cores' game data.
cut = (
without_game_data(config, emu_profiles, narrowing["data_registry"], target_cores)
if narrowing["no_game_data"] else None
)
if source == "platform":
all_extras = []
elif emu_profiles:
all_extras = collect_emulator_extras(
cut.config if cut else config, narrowing["emulators_dir"], db,
set(), base_dest, cut.profiles if cut else emu_profiles,
target_cores=target_cores, include_all=(source == "truth"),
)
else:
all_extras = []
left_out = (
_left_out_lines(cut, systems=set(), cores=True)
if cut is not None and source != "platform" else []
)
return all_extras, left_out
@dataclass
class SplitOutcome:
"""What a --split build came to: the core game data left out, then each part."""
left_out: list[str] = field(default_factory=list)
parts: list[PlatformPackOutcome] = field(default_factory=list)
@property
def zip_paths(self) -> list[str]:
return [part.zip_path for part in self.parts if part.zip_path]
def build_split_packs(
platform_name: str,
platforms_dir: str,
db: dict,
bios_dir: str,
output_dir: str,
group_by: str = "system",
**options: Unpack[SplitOptions],
) -> SplitOutcome:
"""Build split packs (one ZIP per system or manufacturer), printing nothing.
The parts add up to the full pack: each is built against the full pack's
own placement (``within``). ``records`` builds the parts in memory, keyed
by part name, with no archive written.
"""
reject_unknown("generate_split_packs", options, SplitOptions.__optional_keys__)
config = load_platform_config(platform_name, platforms_dir)
records = options.get("records")
narrowing: PackNarrowing = {
"emulators_dir": options.get("emulators_dir", "emulators"),
"zip_contents": options.get("zip_contents"),
"data_registry": options.get("data_registry"),
"emu_profiles": options.get("emu_profiles"),
"target_cores": options.get("target_cores"),
"required_only": options.get("required_only", False),
"source": options.get("source", "full"),
"regions": options.get("regions"),
"target_name": options.get("target_name"),
"one_per_slot": options.get("one_per_slot", False),
"offline": options.get("offline"),
"no_game_data": options.get("no_game_data", False),
}
# The split directory carries the same tags as the packs inside it, or two
# differently narrowed runs write into one another.
split_tags = "".join(
tag
for tag, _label in pack_narrowings(
narrowing["source"], narrowing["regions"], narrowing["target_name"],
narrowing["one_per_slot"], narrowing["required_only"],
no_game_data=narrowing["no_game_data"],
)
)
# Two groupings write different files. Without the tag they accumulate in
# one directory under one SHA256SUMS.txt.
group_tag = "" if group_by == "system" else f"_By{group_by.title()}"
# The parts carry the version, so the directory does too: a rescrape that
# moves the version would otherwise leave the old parts beside the new
# under one SHA256SUMS.txt.
# Named by the stem the full pack reads: assembled here by hand, a '/' in
# a version nested the directory where the check never looked for it.
part_prefix = (
f"{platform_pack_stem([platform_name], platform_name, platforms_dir)}{split_tags}"
)
split_dir = os.path.join(output_dir, f"{part_prefix}{group_tag}_Split")
if records is None:
os.makedirs(split_dir, exist_ok=True)
emu_profiles = narrowing["emu_profiles"]
if emu_profiles is None:
emu_profiles = load_emulator_profiles(narrowing["emulators_dir"])
narrowing["emu_profiles"] = emu_profiles
full = PackRecord()
# The parts add up to the full pack: its trees are the run's account.
build_platform_pack(
platform_name, platforms_dir, db, bios_dir, split_dir,
record=full, uncached_out=options.get("uncached_out"), **narrowing,
)
def part(pack_name: str, scope: _PartScope) -> PlatformPackOutcome:
record = PackRecord() if records is not None else None
built = build_platform_pack(
platform_name, platforms_dir, db, bios_dir, split_dir,
pack_name=pack_name, record=record, within=full.sources,
**scope, **narrowing,
)
if records is not None and record is not None and built.zip_path:
records[pack_name] = record
return built
# The systems a core of the target runs: a part per declared system sent
# each one the target drops to a refusal naming it as unknown.
systems = platform_scope_systems(config, emu_profiles, narrowing["target_cores"])
if group_by == "manufacturer":
groups = _group_systems_by_manufacturer(systems, db, bios_dir)
else:
groups = {system_display_name(sid): [sid] for sid in systems}
all_extras, left_out = _split_extras(config, db, narrowing)
outcome = SplitOutcome(left_out=left_out)
for group_name, group_system_ids in sorted(groups.items()):
group_extras = _extras_for_systems(all_extras, group_system_ids)
outcome.parts.append(part(
f"{part_prefix}_{file_name_part(group_name, '_')}{PACK_SUFFIX}",
{"system_filter": group_system_ids, "precomputed_extras": group_extras},
))
# Extras whose system the platform does not declare, or that name none,
# belong to no group: they go in a part of their own, or the parts would
# not add up to the full pack (4507 of RetroArch's 5430 were left out).
distributed = {
id(fe)
for fe in _extras_for_systems(
all_extras, [s for ids in groups.values() for s in ids]
)
}
undistributed = [fe for fe in all_extras if id(fe) not in distributed]
if undistributed:
outcome.parts.append(part(
f"{part_prefix}_Other_Cores{PACK_SUFFIX}",
{"precomputed_extras": undistributed, "extras_only": True},
))
return outcome
def generate_split_packs(
platform_name: str,
platforms_dir: str,
db: dict,
bios_dir: str,
output_dir: str,
group_by: str = "system",
**options: Unpack[SplitOptions],
) -> list[str]:
"""Build split packs (build_split_packs) and print each part's report.
Returns the path of each part written.
"""
outcome = build_split_packs(
platform_name, platforms_dir, db, bios_dir, output_dir, group_by, **options,
)
print_split_packs(outcome)
return outcome.zip_paths