fix(scan): skip extracted Switch files when reading title ids (#5839)

* 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>
This commit is contained in:
Georges-Antoine AssiandClaude Opus 5.5 authored and GitHub committed 2026-12-02 08:23:01 -05:00
1 parent 37af026d21
commit a3f45158e1
4 files changed
+97

No files matched your search

@@ -869,6 +869,9 @@ class FSRomsHandler(FSHandler):
sigil_platform
and _may_hold_title_id(path, rom_file.category)
and not rom_file.n64_md5
and not (
is_switch and switch.is_extracted_content(path.relative_to(rom_dir))
)
):
title_id_sources.append(_TitleIdSource(path, rom_file))
@@ -2467,6 +2467,72 @@ class TestSigilTitleIdExtraction:
# Only the ROM file itself is worth a native parse.
assert mock_extract.await_count == 1
@pytest.mark.asyncio
async def test_extracted_switch_files_are_not_read(
self, tmp_path: Path, sigil_config: Config
):
handler = make_sigil_handler(tmp_path)
rom = make_multi_part_rom(
tmp_path,
SWITCH_PLATFORM,
"Zelda",
[
"Zelda [update].nsp",
"exefs/main",
"exefs/main.npdm",
"romfs/Model/Link.bfres",
"base/RomFS/Sound/Bgm.bfstm",
],
)
mock_extract = AsyncMock(side_effect=switch_family_extract)
with patch(SIGIL_PATCH_TARGET, mock_extract):
parsed = await handler.get_rom_files(rom)
read = [Path(call.args[1]).name for call in mock_extract.await_args_list]
assert read == ["Zelda [update].nsp"]
assert len(parsed.rom_files) == 5
@pytest.mark.asyncio
async def test_extracted_switch_rom_is_not_reread_on_rescan(
self, tmp_path: Path, sigil_config: Config
):
handler = make_sigil_handler(tmp_path)
rom = make_multi_part_rom(
tmp_path,
SWITCH_PLATFORM,
"Zelda",
["exefs/main", "exefs/main.npdm", "romfs/Model/Link.bfres"],
)
first = await handler.get_rom_files(rom)
mock_extract = AsyncMock(return_value=None)
with patch(SIGIL_PATCH_TARGET, mock_extract):
parsed = await handler.get_rom_files(
rom, existing_files=first.rom_files, reuse_title_ids=True
)
mock_extract.assert_not_awaited()
assert all(
new is old
for new, old in zip(parsed.rom_files, first.rom_files, strict=True)
)
@pytest.mark.asyncio
async def test_romfs_folder_is_read_outside_the_switch_family(
self, tmp_path: Path, sigil_config: Config, stub_ra_hasher: None
):
handler = make_sigil_handler(tmp_path)
rom = make_multi_part_rom(tmp_path, PS2_PLATFORM, "Game", ["romfs/Game.iso"])
mock_extract = AsyncMock(return_value=None)
with patch(SIGIL_PATCH_TARGET, mock_extract):
await handler.get_rom_files(rom)
mock_extract.assert_awaited_once()
@pytest.mark.asyncio
async def test_sidecar_files_are_not_read(
self, tmp_path: Path, sigil_config: Config
+17
View File
@@ -1,3 +1,5 @@
from pathlib import PurePath
import pytest
from models.rom import RomIdentity, SaveTargetLayout
@@ -26,6 +28,21 @@ class TestIsBaseTitleId:
assert not switch.is_base_title_id(title_id)
class TestIsExtractedContent:
@pytest.mark.parametrize(
"rel_path",
["romfs/data/a.bfres", "exefs/main", "ExeFS/main.npdm", "v65536/RomFS/b.arc"],
)
def test_files_in_an_extracted_tree(self, rel_path: str):
assert switch.is_extracted_content(PurePath(rel_path))
@pytest.mark.parametrize(
"rel_path", ["Game.nsp", "updates/Game.nsp", "romfs", "romfs.nsp", "."]
)
def test_files_outside_an_extracted_tree(self, rel_path: str):
assert not switch.is_extracted_content(PurePath(rel_path))
class TestDeriveBaseTitleId:
def test_update_resolves_to_its_base(self):
assert switch.derive_base_title_id("0100ABCD12340800") == "0100ABCD12340000"
+11
View File
@@ -2,6 +2,7 @@
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
@@ -14,11 +15,21 @@ CONTENT_TYPE_CATEGORIES: Final[dict[str, RomFileCategory]] = {
"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."""