From 250ae6438ceab955fbc372c1010711cf045b944f Mon Sep 17 00:00:00 2001 From: BlizzHacker Date: Sun, 29 Nov 2026 00:28:35 +0000 Subject: [PATCH] fix(library): take a folder library's platform from the folder under the root A folder library made a game of every file with a ROM extension anywhere under the root and named its platform after the folder directly above it. A Switch release that bundles an emulator NAND therefore produced one "game" per .nca content file, titled by its content id, under platforms called 00000000, 00000001 ... on the Games page and on every friend's shelf. ROMarr's own hidden .romarr-collection-spool folder showed up as a platform as well. - The platform is the first folder under the root, however deep the file (snes/USA/Game.smc is snes; switch/Game/update/x.nsp is switch). RomM's roms/ layout still reads as . - The walk skips hidden folders and the NAND content stores Contents/registered and Contents/placehld. A user folder that happens to be called "registered" elsewhere is still read. Fixes #324 Co-Authored-By: Claude Opus 5.5 --- romarr/libraries.py | 33 +++++++++++++++-- tests/test_folder_library_nested.py | 57 +++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 tests/test_folder_library_nested.py diff --git a/romarr/libraries.py b/romarr/libraries.py index c0905a57..f28d50c7 100644 --- a/romarr/libraries.py +++ b/romarr/libraries.py @@ -770,6 +770,31 @@ def _directory_entries(directory): raise time.sleep((0.05, 0.15)[attempt]) + # Folders under `Contents/` that hold an emulator's own storage rather than + # games. A Switch release that bundles Yuzu or Ryujinx carries its NAND, and + # every file in `Contents/registered` is an .nca named by a content id -- + # read as games they surface as hash titles under platforms called `00000000`. + NAND_CONTENT_DIRS = frozenset({"registered", "placehld"}) + + @classmethod + def _skipped_dir(cls, name: str, parent) -> bool: + """Hidden folders (ROMarr's own collection spool among them) and NAND stores.""" + return name.startswith(".") or (name.lower() in cls.NAND_CONTENT_DIRS + and parent.name.lower() == "contents") + + @staticmethod + def _platform_of(parts: tuple[str, ...]) -> str: + """The platform folder under the root, however deep the file sits. + + `snes/USA/Game.smc` and `switch/Game/update/Game.nsp` are snes and + switch, not USA and update. RomM's own layout nests every platform + under `roms/`, so that folder is skipped when it is not the platform. + """ + folders = parts[:-1] + if len(folders) > 1 and folders[0] == "roms": + folders = folders[1:] + return folders[0] if folders else "" + def _walk(self, limit: int): """Stream ROM-looking paths breadth-first, without sorting the whole tree.""" if not self.configured or limit <= 0: @@ -784,7 +809,8 @@ def _walk(self, limit: int): for entry in self._directory_entries(directory): try: if self._fs_call(lambda: entry.is_dir(follow_symlinks=False)): - pending.append(Path(entry.path)) + if not self._skipped_dir(entry.name, directory): + pending.append(Path(entry.path)) elif (os.path.splitext(entry.name)[1].lower() in exts and self._fs_call(entry.is_file)): seen += 1 @@ -800,9 +826,10 @@ def _walk(self, limit: int): def iter_games(self, limit: int | None = None): """Read a folder once for progressive refresh; propagate incomplete reads.""" + depth = len(self._root.parts) for path in self._walk(self.MAX_SCAN if limit is None else limit): yield Game(id=str(path), name=path.stem, - platform=path.parent.name if path.parent != self._root else "", + platform=self._platform_of(path.parts[depth:]), extension=path.suffix.lower(), file_name=path.name, local_path=str(path)) @@ -852,7 +879,7 @@ def games(self, limit: int = 60, offset: int = 0, timeout: int | None = None) -> list[Game]: """One game per ROM file. - The platform is the containing directory, because that is the layout + The platform is the folder under the root, because that is the layout every one of these frontends imposes -- and it is the same layout the importer writes, so a ROM filed by ROMarr lands where it will be found. """ diff --git a/tests/test_folder_library_nested.py b/tests/test_folder_library_nested.py new file mode 100644 index 00000000..18f81134 --- /dev/null +++ b/tests/test_folder_library_nested.py @@ -0,0 +1,57 @@ +"""Files deeper than / keep their platform and are not games +when they belong to an emulator's own storage (issue #324).""" +from romarr.libraries import FolderConfig, FolderLibrary + +NCA = "6b37623b1b74cc73e13d1a13c0bb87d0.nca" + + +def library(root, *files): + for rel in files: + path = root / rel + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(b"rom") + return FolderLibrary(FolderConfig(root=str(root))) + + +def platforms(lib): + return {g.name: g.platform for g in lib.iter_games()} + + +def test_platform_is_the_folder_under_the_root_however_deep_the_file(tmp_path): + lib = library(tmp_path, "snes/USA/Super Metroid.smc", + "switch/Zelda/update/Zelda v1.2.nsp", "nes/Contra.nes") + assert platforms(lib) == {"Super Metroid": "snes", + "Zelda v1.2": "switch", "Contra": "nes"} + + +def test_romm_roms_folder_is_not_a_platform(tmp_path): + """RomM's library layout puts every platform under roms/.""" + lib = library(tmp_path, "roms/snes/Super Metroid.smc", "roms/gba/Advance Wars.gba") + assert platforms(lib) == {"Super Metroid": "snes", "Advance Wars": "gba"} + + +def test_a_file_at_the_root_has_no_platform(tmp_path): + lib = library(tmp_path, "Loose.sfc") + assert platforms(lib) == {"Loose": ""} + + +def test_switch_nand_content_inside_a_game_folder_is_not_a_game(tmp_path): + game = "switch/Some Game [PC]" + lib = library(tmp_path, f"{game}/Some Game.nsp", + f"{game}/user/nand/user/Contents/registered/00000000/{NCA}", + f"{game}/user/nand/system/Contents/registered/0000000C/{NCA}", + f"{game}/user/nand/user/Contents/placehld/{NCA}") + assert platforms(lib) == {"Some Game": "switch"} + assert lib.count() == 1 + + +def test_hidden_folders_such_as_the_collection_spool_are_not_read(tmp_path): + lib = library(tmp_path, ".romarr-collection-spool/job1/Chrono Trigger.sfc", + "snes/.trash/Old.sfc", "snes/Chrono Trigger.sfc") + assert platforms(lib) == {"Chrono Trigger": "snes"} + + +def test_a_user_folder_named_registered_is_still_read(tmp_path): + """Only the NAND store under Contents/ is skipped, not any folder of that name.""" + lib = library(tmp_path, "snes/registered/Super Metroid.smc") + assert platforms(lib) == {"Super Metroid": "snes"}