diff --git a/backend/handler/filesystem/roms_handler.py b/backend/handler/filesystem/roms_handler.py index 10da938ff..4000e75a2 100644 --- a/backend/handler/filesystem/roms_handler.py +++ b/backend/handler/filesystem/roms_handler.py @@ -71,6 +71,7 @@ from utils.filesystem import ( iter_files, ) from utils.hashing import crc32_to_hex +from utils.m3u import disc_name_order from utils.platform_slugs import UniversalPlatformSlug as UPS from utils.rom_patcher import is_patch_file_name @@ -379,6 +380,39 @@ def _arcade_set_archive( return picked[1] if picked else None +# Disc sheets RAHasher reads with its default CD reader. +RA_DISC_SHEET_SUFFIXES: Final = (".cue", ".gdi") + + +def _folder_ra_hash_file( + entries: Sequence[DirEntry], rom_dir: Path, set_name: str, is_arcade: bool +) -> Path | None: + """Return the file in a folder ROM that RAHasher should hash, or None if it has none. + + RAHasher takes one file and, on Linux, does not expand a wildcard. + """ + if is_arcade and (set_archive := _arcade_set_archive(entries, rom_dir, set_name)): + return set_archive + + top_level = [ + (st.st_size, Path(f_path, file_name)) + for f_path, file_name, st in entries + if f_path == rom_dir and not is_patch_file_name(file_name) + ] + chds = [f for f in top_level if is_chd_file(f[1])] + if chds: + return max(chds, key=lambda f: f[0])[1] + + sheets = [ + f[1] for f in top_level if f[1].name.lower().endswith(RA_DISC_SHEET_SUFFIXES) + ] + if sheets: + return min(sheets, key=lambda p: disc_name_order(p.name)) + + largest = max(top_level, key=lambda f: f[0], default=None) + return largest[1] if largest else None + + # File categories that never hold a ROM binary, so sigil has nothing to read. NON_BINARY_FILE_CATEGORIES: Final = DOCUMENT_CATEGORIES | { RomFileCategory.SOUNDTRACK, @@ -945,32 +979,16 @@ class FSRomsHandler(FSHandler): if calculate_hashes and top_level_changed: ra_platform = meta_ra_handler.get_platform(rom.platform_slug) if ra_platform and ra_platform["ra_id"]: - # Arcade is hashed by the set's name, which its archive carries. The - # CHD beside it is named after the disc. - set_archive = ( - _arcade_set_archive(entries, rom_dir, rom.fs_name) - if ra_platform["ra_id"] == ARCADE_RA_ID - else None - ) - # RAHasher can't process CHD files via the /* wildcard and instead expects - # track files (bin/cue/etc.). For a folder holding a CHD, pass the largest - # one directly, matching single-file CHD behaviour. - top_level_chds = [ - (st.st_size, Path(f_path, file_name)) - for f_path, file_name, st in entries - if f_path == rom_dir and is_chd_file(Path(f_path, file_name)) - ] - largest_chd = max(top_level_chds, key=lambda c: c[0], default=None) - if set_archive is not None: - ra_path = str(set_archive) - elif largest_chd: - ra_path = str(largest_chd[1]) - else: - ra_path = f"{abs_fs_path}/{rom.fs_name}/*" - rom_ra_h = await RAHasherService().calculate_hash( - ra_platform, - ra_path, + ra_file = _folder_ra_hash_file( + entries, + rom_dir, + rom.fs_name, + is_arcade=ra_platform["ra_id"] == ARCADE_RA_ID, ) + if ra_file: + rom_ra_h = await RAHasherService().calculate_hash( + ra_platform, str(ra_file) + ) for f_path, file_name, st in entries: is_top_level = f_path == rom_dir diff --git a/backend/tests/handler/filesystem/test_roms_handler.py b/backend/tests/handler/filesystem/test_roms_handler.py index 5f4a5fead..d23a6783c 100644 --- a/backend/tests/handler/filesystem/test_roms_handler.py +++ b/backend/tests/handler/filesystem/test_roms_handler.py @@ -1798,12 +1798,25 @@ class TestFSRomsHandler: ("arcade", {"azumanga.zip": 4, "naomi.zip": 32}, "azumanga.zip"), ("arcade", {"gdl-0018.chd": 64}, "gdl-0018.chd"), ("psx", {"bonus.zip": 4, "game.chd": 64}, "game.chd"), + ("psx", {"game.cue": 4, "game.bin": 64}, "game.cue"), + ( + "psx", + {"game (Disc 2).cue": 4, "game (Disc 1).cue": 4, "game.bin": 64}, + "game (Disc 1).cue", + ), + ("dc", {"game.gdi": 4, "track01.bin": 64}, "game.gdi"), + ("nes", {"game.nes": 32, "readme.txt": 4, "fix.ips": 64}, "game.nes"), + ("nes", {}, None), ], ) - async def test_get_rom_files_folder_ra_hash_is_the_arcade_sets( - self, tmp_path: Path, slug: str, file_sizes: dict[str, int], hashed: str + async def test_get_rom_files_folder_ra_hash_file( + self, + tmp_path: Path, + slug: str, + file_sizes: dict[str, int], + hashed: str | None, ): - """An arcade set's RA hash comes from its archive, not the CHD or BIOS beside it.""" + """A folder ROM's RA hash comes from one file in it, never a wildcard.""" platform = Platform(name=slug, slug=slug, fs_slug=slug) game_dir = tmp_path / slug / "roms" / "azumanga" game_dir.mkdir(parents=True) @@ -1828,8 +1841,11 @@ class TestFSRomsHandler: ) as mock_calculate: await test_handler.get_rom_files(rom) - mock_calculate.assert_called_once() - assert mock_calculate.call_args.args[1] == f"{game_dir}/{hashed}" + if hashed is None: + mock_calculate.assert_not_called() + else: + mock_calculate.assert_called_once() + assert mock_calculate.call_args.args[1] == f"{game_dir}/{hashed}" @staticmethod def _setup_archive_rom( diff --git a/backend/utils/m3u.py b/backend/utils/m3u.py index 8e28ab3a9..42f06dfa3 100644 --- a/backend/utils/m3u.py +++ b/backend/utils/m3u.py @@ -52,19 +52,27 @@ DISC_TAG_REGEX = re.compile( ) -def disc_number(file: RomFile) -> int | None: - """The disc a file's name claims to be (A counts as 1), or None.""" - match = DISC_TAG_REGEX.search(file.file_name) +def _name_disc_number(file_name: str) -> int | None: + match = DISC_TAG_REGEX.search(file_name) if not match: return None number, letter = match.groups() return int(number) if number else ord(letter.lower()) - ord("a") + 1 -def _disc_order(file: RomFile) -> tuple[bool, int, str]: +def disc_number(file: RomFile) -> int | None: + """The disc a file's name claims to be (A counts as 1), or None.""" + return _name_disc_number(file.file_name) + + +def disc_name_order(file_name: str) -> tuple[bool, int, str]: """Sort numbered discs first and in order, then the rest by name.""" - number = disc_number(file) - return (number is None, number or 0, file.file_name) + number = _name_disc_number(file_name) + return (number is None, number or 0, file_name) + + +def _disc_order(file: RomFile) -> tuple[bool, int, str]: + return disc_name_order(file.file_name) def _playlist_entries(m3u_path: Path) -> list[list[Path]] | None: