mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-12-03 14:58:51 -06:00
refactor: move entry sources out of crossref
This commit is contained in:
1 parent
e9206e7cc8
commit
0232f0549b
13 files changed
+435
-247
No files matched your search
+9
-233
@@ -8,21 +8,15 @@ from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING, TypedDict
|
||||
|
||||
from model.alternatives import set_keys, set_requirements, settle_sets
|
||||
from model.database import get_mame_clone_map
|
||||
from model.declarations import OUTER_ROOTS
|
||||
from model.platforms import load_platform_config
|
||||
from model.profilekinds import is_emulator
|
||||
from model.registry import list_registered_platforms
|
||||
from model.systems import norm_system_id
|
||||
from packs.gaps import gap_destination
|
||||
from packs.supplemental import SupplementalIndex, data_hit, declared_content
|
||||
from packs.supplemental import SupplementalIndex
|
||||
from resolve.archives import archive_entry
|
||||
from resolve.content import content_fits, name_match_size_ok
|
||||
from resolve.directories import directory_members
|
||||
from resolve.launcherlayer import LauncherLayer, held_record
|
||||
from resolve.layout import system_dir_path
|
||||
from resolve.lookup import held_record_for
|
||||
from resolve.ownership import foreign_tree, owner_tokens
|
||||
from resolve.launches import PlatformLaunches
|
||||
from resolve.standalone import platform_layouts
|
||||
from resolve.entryscope import (
|
||||
@@ -37,6 +31,14 @@ from resolve.entryscope import (
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing_extensions import Unpack
|
||||
from packs.crossrefsource import (
|
||||
declared_as,
|
||||
entry_index,
|
||||
entry_source,
|
||||
first_hash,
|
||||
resolve_archive_source,
|
||||
slot_key,
|
||||
)
|
||||
|
||||
|
||||
def platform_configs(platforms_dir: str, platforms: list[str] | None = None) -> list[dict]:
|
||||
@@ -155,230 +157,6 @@ def _record_needing_platforms(
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
def _resolve_source(
|
||||
fname: str,
|
||||
by_name: dict[str, list],
|
||||
by_name_lower: dict[str, str],
|
||||
data_names: SupplementalIndex | None = None,
|
||||
by_path_suffix: dict | None = None,
|
||||
file_entry: dict | None = None,
|
||||
db_files: dict | None = None,
|
||||
bios_dir: str | None = None,
|
||||
) -> str | None:
|
||||
"""Return the source category for a file, or None if not found.
|
||||
|
||||
Returns ``"bios"`` (in database.json / bios/), ``"data"`` (in data/),
|
||||
or ``None`` (not available anywhere).
|
||||
|
||||
A name hit whose size contradicts a size the emulator verifies is not the
|
||||
file, so it does not count as available. Nor is one whose bytes carry
|
||||
none of the hashes the entry declares: a name never wins over a declared
|
||||
hash, and zquestclassic's ending.mid read as held because Hydra Castle
|
||||
Labyrinth has another file of that name.
|
||||
|
||||
Another casing of the name in another emulator's tree proves nothing, as
|
||||
in resolve_local_file's case-insensitive step: dosbox-x's et4000.bin read
|
||||
as held through pcem's et4000.BIN, which the pack and the installer
|
||||
refuse it.
|
||||
"""
|
||||
declared = declared_content(file_entry or {})
|
||||
owners = owner_tokens(file_entry or {})
|
||||
|
||||
def _can_be(sha1s: list | str) -> bool:
|
||||
if not file_entry or db_files is None:
|
||||
return True
|
||||
if not isinstance(sha1s, list):
|
||||
sha1s = [sha1s]
|
||||
return any(
|
||||
name_match_size_ok(file_entry, db_files.get(sha1, {}).get("size"))
|
||||
and content_fits(file_entry, db_files.get(sha1, {}).get("path", ""))
|
||||
and _record_carries(sha1, db_files.get(sha1, {}), declared)
|
||||
for sha1 in sha1s
|
||||
)
|
||||
|
||||
def _name_hit(name: str) -> bool:
|
||||
"""Whether the files indexed under *name* can be this entry."""
|
||||
return _can_be(by_name.get(name) or [])
|
||||
|
||||
def _folded_hit(name: str) -> bool:
|
||||
"""_name_hit for a name matched in another casing."""
|
||||
return _can_be([
|
||||
sha1 for sha1 in by_name.get(name) or []
|
||||
if db_files is None
|
||||
or not foreign_tree(db_files.get(sha1, {}).get("path", ""), owners)
|
||||
])
|
||||
|
||||
# bios/ via database.json by_name
|
||||
if fname in by_name and _name_hit(fname):
|
||||
return "bios"
|
||||
stripped = fname.rstrip("/")
|
||||
basename = stripped.rsplit("/", 1)[-1] if "/" in stripped else None
|
||||
if basename and basename in by_name and _name_hit(basename):
|
||||
return "bios"
|
||||
key = fname.lower()
|
||||
if key in by_name_lower and _folded_hit(by_name_lower[key]):
|
||||
return "bios"
|
||||
if basename and basename.lower() in by_name_lower and _folded_hit(
|
||||
by_name_lower[basename.lower()]
|
||||
):
|
||||
return "bios"
|
||||
# bios/ via by_path_suffix (regional variants)
|
||||
if by_path_suffix and fname in by_path_suffix and _can_be(by_path_suffix[fname]):
|
||||
return "bios"
|
||||
# bios/ under the canonical MAME set name, as resolve_local_file does:
|
||||
# a renamed archive is held once, under the name the dedup kept.
|
||||
canonical = get_mame_clone_map(bios_dir).get(fname)
|
||||
if canonical and canonical != fname:
|
||||
if canonical in by_name and _name_hit(canonical):
|
||||
return "bios"
|
||||
if canonical.lower() in by_name_lower and _folded_hit(
|
||||
by_name_lower[canonical.lower()]
|
||||
):
|
||||
return "bios"
|
||||
if data_names and data_hit(fname, basename, file_entry, data_names):
|
||||
return "data"
|
||||
return None
|
||||
|
||||
|
||||
|
||||
|
||||
def _record_carries(sha1: str, record: dict, declared: dict[str, set[str]]) -> bool:
|
||||
"""Whether a database record has one of the declared hashes, or none is declared."""
|
||||
if not declared:
|
||||
return True
|
||||
actual = {
|
||||
"sha1": sha1.lower(),
|
||||
**{field: str(record.get(field) or "").lower() for field in ("md5", "sha256", "crc32")},
|
||||
}
|
||||
return any(
|
||||
actual[field] and actual[field] in values
|
||||
for field, values in declared.items()
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def entry_index(db: dict, data_names: SupplementalIndex | None) -> dict:
|
||||
"""The collection indexes entry_source reads, one build for every caller."""
|
||||
indexes = db.get("indexes", {})
|
||||
by_name = indexes.get("by_name", {})
|
||||
return {
|
||||
"by_name": by_name,
|
||||
"by_name_lower": {k.lower(): k for k in by_name},
|
||||
"by_md5": indexes.get("by_md5", {}),
|
||||
"by_crc32": indexes.get("by_crc32", {}),
|
||||
"by_sha256": indexes.get("by_sha256", {}),
|
||||
"by_path_suffix": indexes.get("by_path_suffix", {}),
|
||||
"db_files": db.get("files", {}),
|
||||
"data_names": data_names,
|
||||
"bios_dir": db.get("bios_dir"),
|
||||
}
|
||||
|
||||
|
||||
def entry_source(f: dict, index: dict) -> str | None:
|
||||
"""Where the collection holds a profile entry, or None.
|
||||
|
||||
By name, path and alias, then by any hash the entry declares. The gap
|
||||
report and the site pages read this one function, so a file is held or
|
||||
missing in the same way on both.
|
||||
"""
|
||||
by_name, by_name_lower = index["by_name"], index["by_name_lower"]
|
||||
data_names, by_path_suffix = index.get("data_names"), index["by_path_suffix"]
|
||||
db_files = index["db_files"]
|
||||
fname = f.get("name", "")
|
||||
path_field = f.get("path") or ""
|
||||
# A directory is held when the collection holds files under it, the rule
|
||||
# the pack and verify --emulator expand it by, and by nothing else: a
|
||||
# bare-name lookup missed cdogs' .wolf3d/, whose 695 files every pack
|
||||
# carries, and called SuperTux's data/ held by MAME's plugins/data/.
|
||||
if f.get("type") == "directory" or fname.endswith("/"):
|
||||
members = directory_members(
|
||||
{"indexes": {"by_path_suffix": by_path_suffix}}, path_field or fname
|
||||
)
|
||||
return "bios" if members else None
|
||||
found = None
|
||||
for candidate in [fname, *([path_field] if path_field != fname else []),
|
||||
*(f.get("aliases") or [])]:
|
||||
if not candidate:
|
||||
continue
|
||||
source = _resolve_source(
|
||||
candidate, by_name, by_name_lower, data_names, by_path_suffix, f, db_files,
|
||||
index.get("bios_dir"),
|
||||
)
|
||||
if source == "bios":
|
||||
return source
|
||||
found = found or source
|
||||
|
||||
# The declared content anywhere in bios/ comes before a data/ copy, as in
|
||||
# the resolver: the hash steps run before the data directories, and the
|
||||
# held record is the one every declared hash agrees with.
|
||||
held = held_record_for(f, {
|
||||
"files": db_files,
|
||||
"indexes": {key: index[key] for key in ("by_md5", "by_sha256", "by_crc32")},
|
||||
})
|
||||
return "bios" if held is not None else found
|
||||
|
||||
|
||||
def resolve_archive_source(
|
||||
archive_name: str,
|
||||
by_name: dict[str, list],
|
||||
by_name_lower: dict[str, str],
|
||||
data_names: SupplementalIndex | None = None,
|
||||
by_path_suffix: dict | None = None,
|
||||
) -> str:
|
||||
"""Resolve source for an archive (ZIP) name, returning a source category string."""
|
||||
result = _resolve_source(
|
||||
archive_name, by_name, by_name_lower, data_names, by_path_suffix,
|
||||
)
|
||||
return result if result is not None else "missing"
|
||||
|
||||
|
||||
def first_hash(entry: dict) -> str:
|
||||
"""The first hash an entry declares, strongest first, or ''."""
|
||||
for field in ("sha1", "sha256", "md5", "crc32"):
|
||||
value = entry.get(field)
|
||||
if isinstance(value, list):
|
||||
value = value[0] if value else ""
|
||||
if value:
|
||||
return f"{field}:{str(value).lower()}"
|
||||
return ""
|
||||
|
||||
|
||||
def declared_as(f: dict, fname: str) -> dict[str, str]:
|
||||
"""The category and path the profile gives the entry a row stands for.
|
||||
|
||||
A row's own path is the destination the running build reads, so a
|
||||
reader that needs the profile's declaration takes it from here.
|
||||
"""
|
||||
return {
|
||||
"category": str(f.get("category") or "bios"),
|
||||
"profile_path": str(f.get("path") or fname),
|
||||
}
|
||||
|
||||
|
||||
def slot_key(f: dict, standalone: bool = False) -> tuple:
|
||||
"""The slot a profile entry fills: its entries on one slot are the
|
||||
versions its code accepts there, and holding any of them fills it.
|
||||
|
||||
The path is the one the running build reads: b2's standalone build
|
||||
reads MOS 3.20 and 3.50 from two directories, two slots, while the
|
||||
entries share a name and no `path:`."""
|
||||
fname = f.get("name", "")
|
||||
where = (f.get("standalone_path") if standalone else None) or f.get("path")
|
||||
return (
|
||||
fname,
|
||||
f.get("archive"),
|
||||
system_dir_path(f, where or fname),
|
||||
f.get("system"),
|
||||
f.get("selection_slot"),
|
||||
f.get("mode", "both"),
|
||||
read_from_save_dir(f, standalone),
|
||||
)
|
||||
|
||||
|
||||
# Why an entry is left out of the comparison, in the order reports print them.
|
||||
SET_ASIDE = ("other_versions", "not_read", "outside_scope", "launcher")
|
||||
|
||||
@@ -401,7 +179,6 @@ def gap_source_counts(data: dict) -> dict[str, int]:
|
||||
return counts
|
||||
|
||||
|
||||
|
||||
@dataclass
|
||||
class _ProfileComparison:
|
||||
"""One profile's entries against the platforms' declarations."""
|
||||
@@ -788,7 +565,6 @@ def _settle_launcher_places(
|
||||
data.update(_gap_counts(data["gap_details"]))
|
||||
|
||||
|
||||
|
||||
class ReportScope(TypedDict, total=False):
|
||||
"""What a cross_reference run compares against and answers for."""
|
||||
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
"""Where a profile entry resolves in the collection: its copy, hash and slot."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from model.database import get_mame_clone_map
|
||||
from packs.supplemental import SupplementalIndex, data_hit, declared_content
|
||||
from resolve.content import content_fits, name_match_size_ok
|
||||
from resolve.directories import directory_members
|
||||
from resolve.layout import system_dir_path
|
||||
from resolve.localfile import held_record_for
|
||||
from resolve.ownership import foreign_tree, owner_tokens
|
||||
from resolve.validation import read_from_save_dir
|
||||
|
||||
|
||||
def _resolve_source(
|
||||
fname: str,
|
||||
by_name: dict[str, list],
|
||||
by_name_lower: dict[str, str],
|
||||
data_names: SupplementalIndex | None = None,
|
||||
by_path_suffix: dict | None = None,
|
||||
file_entry: dict | None = None,
|
||||
db_files: dict | None = None,
|
||||
bios_dir: str | None = None,
|
||||
) -> str | None:
|
||||
"""Return the source category for a file, or None if not found.
|
||||
|
||||
Returns ``"bios"`` (in database.json / bios/), ``"data"`` (in data/),
|
||||
or ``None`` (not available anywhere).
|
||||
|
||||
A name hit whose size contradicts a size the emulator verifies is not the
|
||||
file, so it does not count as available. Nor is one whose bytes carry
|
||||
none of the hashes the entry declares: a name never wins over a declared
|
||||
hash, and zquestclassic's ending.mid read as held because Hydra Castle
|
||||
Labyrinth has another file of that name.
|
||||
|
||||
Another casing of the name in another emulator's tree proves nothing, as
|
||||
in resolve_local_file's case-insensitive step: dosbox-x's et4000.bin read
|
||||
as held through pcem's et4000.BIN, which the pack and the installer
|
||||
refuse it.
|
||||
"""
|
||||
declared = declared_content(file_entry or {})
|
||||
owners = owner_tokens(file_entry or {})
|
||||
|
||||
def _can_be(sha1s: list | str) -> bool:
|
||||
if not file_entry or db_files is None:
|
||||
return True
|
||||
if not isinstance(sha1s, list):
|
||||
sha1s = [sha1s]
|
||||
return any(
|
||||
name_match_size_ok(file_entry, db_files.get(sha1, {}).get("size"))
|
||||
and content_fits(file_entry, db_files.get(sha1, {}).get("path", ""))
|
||||
and _record_carries(sha1, db_files.get(sha1, {}), declared)
|
||||
for sha1 in sha1s
|
||||
)
|
||||
|
||||
def _name_hit(name: str) -> bool:
|
||||
"""Whether the files indexed under *name* can be this entry."""
|
||||
return _can_be(by_name.get(name) or [])
|
||||
|
||||
def _folded_hit(name: str) -> bool:
|
||||
"""_name_hit for a name matched in another casing."""
|
||||
return _can_be([
|
||||
sha1 for sha1 in by_name.get(name) or []
|
||||
if db_files is None
|
||||
or not foreign_tree(db_files.get(sha1, {}).get("path", ""), owners)
|
||||
])
|
||||
|
||||
# bios/ via database.json by_name
|
||||
if fname in by_name and _name_hit(fname):
|
||||
return "bios"
|
||||
stripped = fname.rstrip("/")
|
||||
basename = stripped.rsplit("/", 1)[-1] if "/" in stripped else None
|
||||
if basename and basename in by_name and _name_hit(basename):
|
||||
return "bios"
|
||||
key = fname.lower()
|
||||
if key in by_name_lower and _folded_hit(by_name_lower[key]):
|
||||
return "bios"
|
||||
if basename and basename.lower() in by_name_lower and _folded_hit(
|
||||
by_name_lower[basename.lower()]
|
||||
):
|
||||
return "bios"
|
||||
# bios/ via by_path_suffix (regional variants)
|
||||
if by_path_suffix and fname in by_path_suffix and _can_be(by_path_suffix[fname]):
|
||||
return "bios"
|
||||
# bios/ under the canonical MAME set name, as resolve_local_file does:
|
||||
# a renamed archive is held once, under the name the dedup kept.
|
||||
canonical = get_mame_clone_map(bios_dir).get(fname)
|
||||
if canonical and canonical != fname:
|
||||
if canonical in by_name and _name_hit(canonical):
|
||||
return "bios"
|
||||
if canonical.lower() in by_name_lower and _folded_hit(
|
||||
by_name_lower[canonical.lower()]
|
||||
):
|
||||
return "bios"
|
||||
if data_names and data_hit(fname, basename, file_entry, data_names):
|
||||
return "data"
|
||||
return None
|
||||
|
||||
|
||||
def _record_carries(sha1: str, record: dict, declared: dict[str, set[str]]) -> bool:
|
||||
"""Whether a database record has one of the declared hashes, or none is declared."""
|
||||
if not declared:
|
||||
return True
|
||||
actual = {
|
||||
"sha1": sha1.lower(),
|
||||
**{field: str(record.get(field) or "").lower() for field in ("md5", "sha256", "crc32")},
|
||||
}
|
||||
return any(
|
||||
actual[field] and actual[field] in values
|
||||
for field, values in declared.items()
|
||||
)
|
||||
|
||||
|
||||
def entry_index(db: dict, data_names: SupplementalIndex | None) -> dict:
|
||||
"""The collection indexes entry_source reads, one build for every caller."""
|
||||
indexes = db.get("indexes", {})
|
||||
by_name = indexes.get("by_name", {})
|
||||
return {
|
||||
"by_name": by_name,
|
||||
"by_name_lower": {k.lower(): k for k in by_name},
|
||||
"by_md5": indexes.get("by_md5", {}),
|
||||
"by_crc32": indexes.get("by_crc32", {}),
|
||||
"by_sha256": indexes.get("by_sha256", {}),
|
||||
"by_path_suffix": indexes.get("by_path_suffix", {}),
|
||||
"db_files": db.get("files", {}),
|
||||
"data_names": data_names,
|
||||
"bios_dir": db.get("bios_dir"),
|
||||
}
|
||||
|
||||
|
||||
def entry_source(f: dict, index: dict) -> str | None:
|
||||
"""Where the collection holds a profile entry, or None.
|
||||
|
||||
By name, path and alias, then by any hash the entry declares. The gap
|
||||
report and the site pages read this one function, so a file is held or
|
||||
missing in the same way on both.
|
||||
"""
|
||||
by_name, by_name_lower = index["by_name"], index["by_name_lower"]
|
||||
data_names, by_path_suffix = index.get("data_names"), index["by_path_suffix"]
|
||||
db_files = index["db_files"]
|
||||
fname = f.get("name", "")
|
||||
path_field = f.get("path") or ""
|
||||
# A directory is held when the collection holds files under it, the rule
|
||||
# the pack and verify --emulator expand it by, and by nothing else: a
|
||||
# bare-name lookup missed cdogs' .wolf3d/, whose 695 files every pack
|
||||
# carries, and called SuperTux's data/ held by MAME's plugins/data/.
|
||||
if f.get("type") == "directory" or fname.endswith("/"):
|
||||
members = directory_members(
|
||||
{"indexes": {"by_path_suffix": by_path_suffix}}, path_field or fname
|
||||
)
|
||||
return "bios" if members else None
|
||||
found = None
|
||||
for candidate in [fname, *([path_field] if path_field != fname else []),
|
||||
*(f.get("aliases") or [])]:
|
||||
if not candidate:
|
||||
continue
|
||||
source = _resolve_source(
|
||||
candidate, by_name, by_name_lower, data_names, by_path_suffix, f, db_files,
|
||||
index.get("bios_dir"),
|
||||
)
|
||||
if source == "bios":
|
||||
return source
|
||||
found = found or source
|
||||
|
||||
# The declared content anywhere in bios/ comes before a data/ copy, as in
|
||||
# the resolver: the hash steps run before the data directories, and the
|
||||
# held record is the one every declared hash agrees with.
|
||||
held = held_record_for(f, {
|
||||
"files": db_files,
|
||||
"indexes": {key: index[key] for key in ("by_md5", "by_sha256", "by_crc32")},
|
||||
})
|
||||
return "bios" if held is not None else found
|
||||
|
||||
|
||||
def resolve_archive_source(
|
||||
archive_name: str,
|
||||
by_name: dict[str, list],
|
||||
by_name_lower: dict[str, str],
|
||||
data_names: SupplementalIndex | None = None,
|
||||
by_path_suffix: dict | None = None,
|
||||
) -> str:
|
||||
"""Resolve source for an archive (ZIP) name, returning a source category string."""
|
||||
result = _resolve_source(
|
||||
archive_name, by_name, by_name_lower, data_names, by_path_suffix,
|
||||
)
|
||||
return result if result is not None else "missing"
|
||||
|
||||
|
||||
def first_hash(entry: dict) -> str:
|
||||
"""The first hash an entry declares, strongest first, or ''."""
|
||||
for field in ("sha1", "sha256", "md5", "crc32"):
|
||||
value = entry.get(field)
|
||||
if isinstance(value, list):
|
||||
value = value[0] if value else ""
|
||||
if value:
|
||||
return f"{field}:{str(value).lower()}"
|
||||
return ""
|
||||
|
||||
|
||||
def declared_as(f: dict, fname: str) -> dict[str, str]:
|
||||
"""The category and path the profile gives the entry a row stands for.
|
||||
|
||||
A row's own path is the destination the running build reads, so a
|
||||
reader that needs the profile's declaration takes it from here.
|
||||
"""
|
||||
return {
|
||||
"category": str(f.get("category") or "bios"),
|
||||
"profile_path": str(f.get("path") or fname),
|
||||
}
|
||||
|
||||
|
||||
def slot_key(f: dict, standalone: bool = False) -> tuple:
|
||||
"""The slot a profile entry fills: its entries on one slot are the
|
||||
versions its code accepts there, and holding any of them fills it.
|
||||
|
||||
The path is the one the running build reads: b2's standalone build
|
||||
reads MOS 3.20 and 3.50 from two directories, two slots, while the
|
||||
entries share a name and no `path:`."""
|
||||
fname = f.get("name", "")
|
||||
where = (f.get("standalone_path") if standalone else None) or f.get("path")
|
||||
return (
|
||||
fname,
|
||||
f.get("archive"),
|
||||
system_dir_path(f, where or fname),
|
||||
f.get("system"),
|
||||
f.get("selection_slot"),
|
||||
f.get("mode", "both"),
|
||||
read_from_save_dir(f, standalone),
|
||||
)
|
||||
@@ -15,7 +15,7 @@ from sitegen.runsections import system_name
|
||||
def _emulator_slots(files: list[dict]) -> list[list[dict]]:
|
||||
"""The entries of each slot, as verify --emulator and the gaps page
|
||||
count them, leaving out the slots the app ships."""
|
||||
from packs.crossref import slot_key
|
||||
from packs.crossrefsource import slot_key
|
||||
|
||||
slots: dict[tuple, list[dict]] = {}
|
||||
for f in files:
|
||||
|
||||
@@ -68,7 +68,7 @@ def availability_check(
|
||||
missing while the gap report called it held would describe a
|
||||
different collection on two pages of the same site.
|
||||
"""
|
||||
from packs.crossref import entry_index, entry_source
|
||||
from packs.crossrefsource import entry_index, entry_source
|
||||
|
||||
index = entry_index(db, data_names)
|
||||
|
||||
@@ -83,6 +83,187 @@ def availability_check(
|
||||
return _file_available
|
||||
|
||||
|
||||
||||||| parent of 068513702 (refactor: move entry sources out of crossref)
|
||||
def _emulator_slots(files: list[dict]) -> list[list[dict]]:
|
||||
"""The entries of each slot, as verify --emulator and the gaps page
|
||||
count them, leaving out the slots the app ships."""
|
||||
from packs.crossrefsource import slot_key
|
||||
|
||||
slots: dict[tuple, list[dict]] = {}
|
||||
for f in files:
|
||||
slots.setdefault(slot_key(f), []).append(f)
|
||||
return [group for group in slots.values() if not all(comes_with_app(f) for f in group)]
|
||||
|
||||
|
||||
def _set_names(value: object) -> list[str]:
|
||||
"""The sets an entry belongs to: group takes a name, a list or a map."""
|
||||
if isinstance(value, str):
|
||||
return [value]
|
||||
if isinstance(value, (list, dict)):
|
||||
return [str(name) for name in value]
|
||||
return []
|
||||
|
||||
|
||||
def _requirements(files: list[dict], groups: dict | None) -> list[list[dict]]:
|
||||
"""Each thing the emulator needs: the members of a required set, any one
|
||||
of which fills it (DuckStation's ps-bios, 105 images), or a required
|
||||
file of its own. load_emulator_profiles marks every member of a
|
||||
required set required, so counted per file DuckStation read "105
|
||||
required" for one BIOS."""
|
||||
required_sets = {name for name, spec in (groups or {}).items()
|
||||
if isinstance(spec, dict) and spec.get("required")}
|
||||
sets: dict[str, list[dict]] = {}
|
||||
alone: list[list[dict]] = []
|
||||
for f in files:
|
||||
if f.get("required") is not True or comes_with_app(f):
|
||||
continue
|
||||
names = [name for name in _set_names(f.get("group")) if name in required_sets]
|
||||
if names:
|
||||
for name in names:
|
||||
sets.setdefault(name, []).append(f)
|
||||
else:
|
||||
alone.append([f])
|
||||
return [*sets.values(), *alone]
|
||||
|
||||
|
||||
def _regional_set(requirements: list[list[dict]]) -> list[dict]:
|
||||
"""The one requirement, a set whose members the code picks by the game's
|
||||
region, or [] when there is no such single choice."""
|
||||
if len(requirements) != 1 or len(requirements[0]) < 2:
|
||||
return []
|
||||
members = requirements[0]
|
||||
if not all(f.get("region") for f in members):
|
||||
return []
|
||||
return members if len({region_words(f["region"]) for f in members}) >= 2 else []
|
||||
|
||||
|
||||
def _emulator_file_summary(files: list[dict], groups: dict | None = None) -> list[str]:
|
||||
"""What the emulator reads, above its file table, and its categories.
|
||||
|
||||
Counted by slot, as verify --emulator and the gaps page count: raze
|
||||
lists every GRP its code accepts under one name, and holding one fills
|
||||
the slot. Counted per entry, the page announced 46 missing where 12
|
||||
slots are empty. The files the app ships are counted apart: NetherSX2
|
||||
said "4 required" for one BIOS and three fonts in its package. A
|
||||
required set counts once (`groups`).
|
||||
"""
|
||||
slots = _emulator_slots(files)
|
||||
shipped = sum(1 for f in files if comes_with_app(f))
|
||||
requirements = _requirements(files, groups)
|
||||
in_sets = {id(f) for members in requirements for f in members}
|
||||
req_slots = sum(1 for group in slots if any(id(f) in in_sets for f in group))
|
||||
req_count = len(requirements)
|
||||
optional = len(slots) - req_slots
|
||||
hle_count = sum(1 for group in slots if any(f.get("hle_fallback") for f in group))
|
||||
|
||||
# Sentences, one number format: the line read "1,563 files | 164
|
||||
# required, 1399 optional | 1563 in repo".
|
||||
total = f"**{count_text(len(slots), 'file')}**"
|
||||
if sum(len(group) for group in slots) != len(slots):
|
||||
total += f" ({sum(len(group) for group in slots):,} accepted versions)"
|
||||
line = f"{total}: {req_count:,} required, {optional:,} optional."
|
||||
if any(len(members) > 1 for members in requirements):
|
||||
line += " Any one file of a required set fills it."
|
||||
regional = _regional_set(requirements) or (
|
||||
[f for members in requirements for f in members]
|
||||
if _regional_choice([f for members in requirements for f in members]) else [])
|
||||
if regional:
|
||||
# DuckStation takes the BIOS of the game's region: "3 required"
|
||||
# read as "get all three".
|
||||
line = f"{total}. One of {len(regional):,} is required, for the games' region."
|
||||
if optional:
|
||||
line += f" {optional:,} {'is' if optional == 1 else 'are'} optional."
|
||||
if hle_count:
|
||||
line += f" {hle_count:,} {'has' if hle_count == 1 else 'have'} an HLE fallback."
|
||||
if shipped:
|
||||
line += f" {count_text(shipped, 'more file')} {'comes' if shipped == 1 else 'come'} with the app."
|
||||
lines = [line, ""]
|
||||
|
||||
categories = [f.get("category", "bios") for f in files]
|
||||
if "game_data" in categories or "bios_zip" in categories:
|
||||
cats = [
|
||||
f"{categories.count(key):,} {label}"
|
||||
for key, label in (
|
||||
("bios", "BIOS"), ("bios_zip", "BIOS ZIPs"), ("game_data", "game data")
|
||||
)
|
||||
if categories.count(key)
|
||||
]
|
||||
lines.extend([f"By kind: {series(cats)}.", ""])
|
||||
return lines
|
||||
|
||||
|
||||
def _emulator_held_line(files: list[dict], available: Callable[[dict], bool]) -> list[str]:
|
||||
"""What this collection holds of them, one quiet line under the table:
|
||||
a reader comes for what the emulator needs, the project's counts after."""
|
||||
slots = _emulator_slots(files)
|
||||
if not slots:
|
||||
return []
|
||||
in_repo = sum(1 for group in slots if any(available(f) for f in group))
|
||||
unsourceable = sum(
|
||||
1 for group in slots
|
||||
if not any(available(f) for f in group) and all(f.get("unsourceable") for f in group)
|
||||
)
|
||||
held = [f"{in_repo:,} in repo", f"{len(slots) - in_repo - unsourceable:,} missing"]
|
||||
if unsourceable:
|
||||
held.append(f"{unsourceable:,} unsourceable")
|
||||
return [f"<p><small>In this collection: {series(held)}.</small></p>", ""]
|
||||
|
||||
|
||||
def _data_member_table(members: dict[str, dict], names: list[str], anchors: set[str]) -> list[str]:
|
||||
"""A data directory's files with their size and sha1, each sha1 an
|
||||
anchor the first time the page lists it (``anchors``, the page's ids)."""
|
||||
table = ["| File | Size | SHA1 |", "|------|-----:|------|"]
|
||||
for member in names:
|
||||
info = members[member]
|
||||
sha1 = str(info.get("sha1", ""))
|
||||
# One id per page: two members can hold the same bytes (every empty
|
||||
# file), and Dolphin's trees share Sys.
|
||||
anchor = card_anchor(sha1)
|
||||
span = "" if anchor in anchors else f'<span id="{anchor}"></span>'
|
||||
anchors.add(anchor)
|
||||
table.append(f"| {span}{code_span(member)} | {size_text(int(info.get('size', 0)))} | {code_span(sha1)} |")
|
||||
return table
|
||||
|
||||
|
||||
def _data_tree_page(ref: str, entry: dict, pages: list[tuple[str, list[str]]],
|
||||
index: int, registry: PageRegistry) -> str:
|
||||
"""One page of a large data directory's file list."""
|
||||
stem, names = pages[index]
|
||||
members = registry.data_members.get(ref) or {}
|
||||
part = f" (part {index + 1} of {len(pages)})" if len(pages) > 1 else ""
|
||||
read_by = series([f"[{escape_tags(display)}]({key}.md#data-directories)"
|
||||
for key, display in registry.data_readers.get(ref, [])])
|
||||
# A file list out of the search: "dolphin" found three parts of the
|
||||
# dolphin-sys tree, on their title, before the Dolphin page.
|
||||
front: dict = {"level": "reference", "search": "exclude"}
|
||||
# The reader the tree is named after (dolphin for dolphin-sys), else
|
||||
# the first by name.
|
||||
readers = registry.data_readers.get(ref) or []
|
||||
owner = max(readers, default=None,
|
||||
key=lambda reader: len(reader[0]) if ref.startswith(f"{reader[0]}-") else 0)
|
||||
if owner:
|
||||
head = _part_front_matter(
|
||||
(owner[1], f"{owner[0]}.md"),
|
||||
[(f"The {ref} data directory, part {n}", f"{other}.md")
|
||||
for n, (other, _names) in enumerate(pages, 1)], index, front)
|
||||
else:
|
||||
head = ["---", yaml.safe_dump(front, sort_keys=False, allow_unicode=True).rstrip("\n"),
|
||||
"---", ""]
|
||||
lines = [
|
||||
*head,
|
||||
f"# The {ref} data directory{part}", "",
|
||||
f"{entry.get('description') or 'A data directory'}: "
|
||||
f"{count_text(len(members), 'file')}, as the packs ship them."
|
||||
+ (f" Read by {read_by}." if read_by else ""), "",
|
||||
]
|
||||
if len(pages) > 1:
|
||||
lines.extend([", ".join(f"[part {i}]({other}.md)" if other != stem else f"part {i}"
|
||||
for i, (other, _names) in enumerate(pages, 1)), ""])
|
||||
lines.extend(search_excluded(_data_member_table(members, names, set())))
|
||||
lines.extend(["", date_line()])
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
||||
# An emulator page lists at most this many file cards: MAME's 1,564 made a
|
||||
# 3 MB page. Past it, the cards go to pages of their own and the emulator's
|
||||
# page keeps a table of every entry, each row holding the card's old
|
||||
|
||||
@@ -24,7 +24,7 @@ from romrecipes import ledger as romrecipes_ledger
|
||||
from resolve.launches import PlatformLaunches
|
||||
from resolve.standalone import platform_launches
|
||||
from model.profiles import unique_emulator_profiles
|
||||
from packs.crossref import first_hash
|
||||
from packs.crossrefsource import first_hash
|
||||
from core.hashing import cut_md5_owner, declared_hash_lists, parse_hash_list
|
||||
from resolve.review import LARGE_FILE_ISSUE_URL, Placement, placement
|
||||
from resolve.sendable import can_send, sendable
|
||||
|
||||
Reference in new issue
Block a user