Files
libretro/scripts/packs/manifestselect.py
T

194 lines
8.0 KiB
Python

"""What an install manifest records about each file so the installer can narrow it.
The installer runs without the profiles or the database, yet a narrowed
install must hold the same files as a pack built with the same narrowing.
Each entry therefore carries the platform systems it belongs to and, when
the code selects it by territory, its regions and the groups in which it
competes. The installer replays region.resolve_region_drops over them.
The pack's `--system X` keeps the platform's declarations under X and every
core extra owned by X, then groups regions over the declarations it kept and
over every core extra of the platform. A group membership is therefore either
unconditional (a core extra) or present only when the declaring system is
kept (a platform declaration), and the manifest records the two apart.
"""
from __future__ import annotations
from dataclasses import dataclass
from resolve import regions as resolve_regions
from resolve import slot as slot_mod
from model.systems import norm_system_id
from core.paths import declared_destination
from packs.extras import (
kept_entries,
serving_system_ids,
extra_region_groups,
extra_region_key,
system_spellings,
)
@dataclass(frozen=True)
class SelectionIndex:
"""System and region facts for one platform, as the pack builder reads them."""
region_index: dict[str, dict]
competing: frozenset[str]
systems_by_dest: dict[str, frozenset[str]]
declared_by: dict[str, frozenset[str]]
extra_groups: dict[str, frozenset[str]]
slots: dict[str, dict]
@classmethod
def build(
cls,
region_groups: dict[str, list[tuple[str, str]]],
region_index: dict[str, dict],
pack_systems: dict[str, dict],
extras: list[dict],
required_only: bool = False,
extras_prefix: str = "",
slot_claims: dict[str, dict[str, dict]] | None = None,
) -> SelectionIndex:
by_norm: dict[str, set[str]] = {}
for sys_id in pack_systems:
by_norm.setdefault(norm_system_id(sys_id), set()).add(sys_id)
declared_by: dict[str, set[str]] = {}
for sys_id, system in pack_systems.items():
for file_entry in kept_entries(system.get("files", []), required_only):
dest = declared_destination(file_entry)
if dest:
declared_by.setdefault(dest, set()).add(sys_id)
owned_by: dict[str, set[str]] = {}
extra_groups: dict[str, set[str]] = {}
spellings = system_spellings(pack_systems)
for extra in extras:
# Keyed as platform_region_groups keys it (extra_region_key).
dest = extra_region_key(extra, extras_prefix)
if not dest:
continue
extra_groups.setdefault(dest, set()).update(
extra_region_groups(extra, spellings)
)
# A profile system the platform does not have is no system the
# pack's --system accepts: it would offer what the pack refuses.
for sys_id in serving_system_ids(extra):
owned_by.setdefault(dest, set()).update(
by_norm.get(norm_system_id(sys_id), ())
)
systems_by_dest = {
dest: frozenset(declared_by.get(dest, set()) | owned_by.get(dest, set()))
for dest in set(declared_by) | set(owned_by)
}
return cls(
region_index,
frozenset(
dest for members in region_groups.values() for dest, _name in members
),
systems_by_dest,
{dest: frozenset(ids) for dest, ids in declared_by.items()},
{dest: frozenset(ids) for dest, ids in extra_groups.items()},
_slot_fields(region_groups, slot_claims or {}),
)
def fields(self, dest: str, name: str) -> dict:
"""What an entry carries for the installer's selection.
`systems` drives --system. A regional entry adds its regions,
`region_groups` it always competes in as a core extra, and
`region_system_groups` it competes in only when that platform system
is kept, and `priority` the rank the code searches it at. An entry that never competes carries no region field: an
untagged file always survives a region filter.
"""
out: dict = {}
systems = self.systems_by_dest.get(dest)
if systems:
out["systems"] = sorted(systems)
if dest not in self.competing:
return out
if dest in self.slots:
out["slot"] = self.slots[dest]
regions = resolve_regions.lookup_regions(self.region_index, dest, name)
if not regions:
return out
out["regions"] = sorted(regions)
always = self.extra_groups.get(dest)
if always:
out["region_groups"] = sorted(always)
declared = self.declared_by.get(dest)
if declared:
out["region_system_groups"] = sorted(declared)
# The code's search rank decides whether a world file may withdraw
# this one when no region matches.
priority = resolve_regions.lookup_priority(self.region_index, dest, name)
if priority is not None:
out["priority"] = priority
return out
def _slot_fields(
region_groups: dict[str, list[tuple[str, str]]],
claims: dict[str, dict[str, dict]],
) -> dict[str, dict]:
"""Per destination, the groups it competes in for a slot and the claims
at each key slot.lookup_slot would try.
The builder looks each member up by its own name, so a destination a
platform declares under one name and a core under another is looked up
twice: the first name's groups and keys head the field, each other
name's go in `also`. A name no emulator claims has no keys, which keeps
the destination as the builder keeps it. Only a group where some
emulator ranks a member can drop a file, so only its members are
written, each with all its groups. A key whose claims equal the
previous one's answers the same and is left out.
"""
# Each time a group lists the destination: two cores filing one file in
# a group make two candidates of one rank, which no order separates.
chains: dict[str, dict[str, list[str]]] = {}
for group_id, members in region_groups.items():
for dest, name in members:
chains.setdefault(dest, {}).setdefault(name, []).append(group_id)
def keys_of(dest: str, name: str) -> list[str]:
return [k for k in slot_mod.slot_lookup_keys(dest, name) if k in claims]
def ranked(dest: str) -> bool:
return any(rank is not None for name in chains.get(dest, {})
for key in keys_of(dest, name)
for claim in claims[key].values() for rank in claim["ranks"])
# Every group of such a destination: one where it stays keeps it.
written: set[str] = set()
for members in region_groups.values():
if any(ranked(member) for member, _name in members):
written.update(member for member, name in members
if any(keys_of(member, other) for other in chains[member]))
def chain(dest: str, name: str, group_ids: list[str]) -> dict:
kept: list[dict] = []
for key in keys_of(dest, name):
by_emulator = {
emulator: {
"ranks": claim["ranks"],
**({"regions": sorted(claim["regions"])} if claim["regions"] else {}),
**({"sets": sorted(g for _owner, g in claim["sets"])}
if claim["sets"] else {}),
}
for emulator, claim in sorted(claims[key].items())
}
if not kept or kept[-1] != by_emulator:
kept.append(by_emulator)
return {"groups": sorted(group_ids), "keys": kept}
fields: dict[str, dict] = {}
for dest in sorted(written):
first, *others = [chain(dest, name, group_ids)
for name, group_ids in chains[dest].items()]
fields[dest] = {**first, **({"also": others} if others else {})}
return fields