From 0af699b2674c8539a3653b41babb5a0e97d300fa Mon Sep 17 00:00:00 2001 From: MMagTech <64668236+MMagTech@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:31:42 -0500 Subject: [PATCH] fix(ra): hash an arcade set by its archive, not the CHD beside it RetroAchievements identifies an arcade game by its set's name. A folder holding a set and its disc was hashed by the CHD, which is named after the disc (gdl-0018.chd beside azumanga.zip). Co-Authored-By: Claude Opus 5.5 --- backend/handler/filesystem/roms_handler.py | 17 ++++++-- .../handler/filesystem/test_roms_handler.py | 40 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/backend/handler/filesystem/roms_handler.py b/backend/handler/filesystem/roms_handler.py index 1187f7091..3d84d1cb9 100644 --- a/backend/handler/filesystem/roms_handler.py +++ b/backend/handler/filesystem/roms_handler.py @@ -65,7 +65,11 @@ from utils.archives import ( read_tar_archive_files, read_zip_archive_files, ) -from utils.filesystem import ARCHIVE_FORMAT_BY_SUFFIX, iter_files +from utils.filesystem import ( + ARCHIVE_FORMAT_BY_SUFFIX, + COMPRESSED_FILE_SUFFIXES, + iter_files, +) from utils.hashing import crc32_to_hex from utils.platform_slugs import UniversalPlatformSlug as UPS from utils.rom_patcher import is_patch_file_name @@ -809,7 +813,7 @@ class FSRomsHandler(FSHandler): when its top level changed, so unchanged files are not reread. cnfg: The config to read, so a scan can load it once. """ - from adapters.services.rahasher import RAHasherService + from adapters.services.rahasher import ARCADE_RA_ID, RAHasherService from handler.metadata import meta_ra_handler # The rom's stored directory is the source of truth for its location, so @@ -933,9 +937,16 @@ class FSRomsHandler(FSHandler): 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) + # Arcade is hashed by the set's name, which its archive carries. The + # CHD beside it is named after the disc. + hash_archive = ra_platform["ra_id"] == ARCADE_RA_ID and any( + f_path == rom_dir + and file_name.lower().endswith(COMPRESSED_FILE_SUFFIXES) + for f_path, file_name, _ in entries + ) ra_path = ( str(largest_chd[1]) - if largest_chd + if largest_chd and not hash_archive else f"{abs_fs_path}/{rom.fs_name}/*" ) rom_ra_h = await RAHasherService().calculate_hash( diff --git a/backend/tests/handler/filesystem/test_roms_handler.py b/backend/tests/handler/filesystem/test_roms_handler.py index cf239fe6c..d079b2aad 100644 --- a/backend/tests/handler/filesystem/test_roms_handler.py +++ b/backend/tests/handler/filesystem/test_roms_handler.py @@ -1784,6 +1784,46 @@ class TestFSRomsHandler: "Final Fantasy VII.m3u": "", } + @pytest.mark.asyncio + @pytest.mark.parametrize( + ("slug", "file_names", "hashed"), + [ + ("arcade", ["azumanga.zip", "gdl-0018.chd"], "*"), + ("arcade", ["azumanga.7z", "gdl-0018.chd"], "*"), + ("arcade", ["gdl-0018.chd"], "gdl-0018.chd"), + ("psx", ["bonus.zip", "game.chd"], "game.chd"), + ], + ) + async def test_get_rom_files_folder_ra_hash_is_the_arcade_sets( + self, tmp_path: Path, slug: str, file_names: list[str], hashed: str + ): + """An arcade set's RA hash comes from its archive, not the CHD beside it.""" + platform = Platform(name=slug, slug=slug, fs_slug=slug) + game_dir = tmp_path / slug / "roms" / "azumanga" + game_dir.mkdir(parents=True) + for file_name in file_names: + (game_dir / file_name).write_bytes(b"MComprHD") + + test_handler = FSRomsHandler() + test_handler.base_path = tmp_path + + rom = Rom( + id=1, + fs_name="azumanga", + fs_extension="", + fs_path=f"{slug}/roms", + platform=platform, + ) + + with patch( + "adapters.services.rahasher.RAHasherService.calculate_hash", + return_value="", + ) 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}" + @staticmethod def _setup_archive_rom( tmp_path: Path, platform: Platform, fs_name: str, fs_extension: str, data: bytes