mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 18:43:23 +01:00
* fix(scan): skip extracted Switch files when reading title ids Sigil reads a Switch title id only from an NSP or XCI container, so no file under an extracted title's exefs or romfs folder can yield one. A scan still handed every such file to sigil, and the Switch family never reuses stored ids while a file lacks one, so every quick scan re-read an extracted game's whole tree (one Switch 2 game in the report holds ~160k files) and kept each path in the scan's needs-key set for the summary. Files inside an exefs or romfs folder of a Switch ROM are no longer title id sources. On a 50k-file extracted game, a quick rescan goes from 50,002 sigil reads and 50,002 retained needs-key paths to none, and its peak allocation drops from 193 MB to 110 MB. Refs #5706 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(scan): check row reuse by identity in the extracted Switch rescan test Unsaved rows all have a None id, so comparing ids could not tell reused rows from rebuilt ones. Addresses Greptile's review. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
72 lines
2.7 KiB
Python
72 lines
2.7 KiB
Python
"""Switch title-id knowledge: recognising a base-game id, recovering one from an
|
|
update or DLC id, and mapping a content type to a file category."""
|
|
|
|
import re
|
|
from pathlib import PurePath
|
|
from typing import Final
|
|
|
|
from models.rom import RomFileCategory, RomIdentity, SaveTargetLayout
|
|
|
|
# Maps sigil's Switch CNMT content type to the RomFile category. Authoritative
|
|
# over folder-derived categories when the binary parse succeeds.
|
|
CONTENT_TYPE_CATEGORIES: Final[dict[str, RomFileCategory]] = {
|
|
"application": RomFileCategory.GAME,
|
|
"patch": RomFileCategory.UPDATE,
|
|
"addon": RomFileCategory.DLC,
|
|
}
|
|
|
|
# The folders an extracted title unpacks into. Sigil reads a Switch title id only
|
|
# from an NSP or XCI container, so no file under them can yield one.
|
|
EXTRACTED_DIR_NAMES: Final = frozenset({"exefs", "romfs"})
|
|
|
|
TITLE_ID_REGEX: Final = re.compile(r"[0-9A-Fa-f]{16}")
|
|
# An already-embedded id, e.g. [0100F4700B2E0000].
|
|
TITLE_ID_BRACKET_REGEX: Final = re.compile(rf"\[{TITLE_ID_REGEX.pattern}\]")
|
|
|
|
|
|
def is_extracted_content(rel_path: PurePath) -> bool:
|
|
"""Whether a file, relative to its ROM folder, sits in an extracted title's
|
|
exefs or romfs folder."""
|
|
return any(part.lower() in EXTRACTED_DIR_NAMES for part in rel_path.parts[:-1])
|
|
|
|
|
|
def derive_base_title_id(title_id: str) -> str | None:
|
|
"""Derive the base-game title id from an update/DLC id: clear the low 12
|
|
bits and decrement the program-index nibble when it is odd."""
|
|
if len(title_id) < 4:
|
|
return None
|
|
nibble_char = title_id[-4]
|
|
try:
|
|
nibble = int(nibble_char, 16)
|
|
except ValueError:
|
|
return None
|
|
if nibble % 2 == 1:
|
|
nibble -= 1
|
|
formatted = format(nibble, "x" if nibble_char.islower() else "X")
|
|
return f"{title_id[:-4]}{formatted}000"
|
|
|
|
|
|
def is_base_title_id(title_id: str) -> bool:
|
|
"""A base-game id is its own derived base."""
|
|
return derive_base_title_id(title_id) == title_id
|
|
|
|
|
|
def normalize_identity(is_switch: bool, identity: RomIdentity) -> RomIdentity:
|
|
"""Force a Switch identity onto the base game's, leaving others untouched.
|
|
|
|
Every writer of `Rom.title_id` goes through here: reassociating a renamed
|
|
non-hashable ROM matches on that column, which only works while it holds
|
|
the base game's id. A non-derivable id is left as read rather than dropped.
|
|
"""
|
|
title_id = identity.title_id
|
|
derived = derive_base_title_id(title_id) if is_switch and title_id else None
|
|
if derived is None or (derived == title_id and identity.save_target):
|
|
return identity
|
|
|
|
# Switch saves are keyed by the base title id itself.
|
|
return RomIdentity(
|
|
title_id=derived,
|
|
save_target=derived,
|
|
save_target_layout=SaveTargetLayout.FOLDER_EXACT,
|
|
)
|