-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add RAHasher and util to calculate RetroAchievements hashes
Build and include the `RAHasher` binary in the Docker image, to calculate hashes for RetroAchievements. Also, add a service to run `RAHasher` from Python. Example usage: ```python from adapters.services.rahasher import RAHasherError, RAHasherService rahasher = RAHasherService() try: hash = await rahasher.calculate_hash("nes", Path("path/to/rom.nes")) except RAHasherError: # Handle error hash = None ```
- Loading branch information
1 parent
8a13611
commit cf087b3
Showing
4 changed files
with
161 additions
and
6 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import asyncio | ||
import re | ||
from pathlib import Path | ||
|
||
from logger.logger import log | ||
|
||
RAHASHER_VALID_HASH_REGEX = re.compile(r"^[0-9a-f]{32}$") | ||
|
||
# TODO: Centralize standarized platform slugs using StrEnum. | ||
PLATFORM_SLUG_TO_RETROACHIEVEMENTS_ID: dict[str, int] = { | ||
"3do": 43, | ||
"cpc": 37, | ||
"acpc": 37, | ||
"apple2": 38, | ||
"appleii": 38, | ||
"arcade": 27, | ||
"arcadia-2001": 73, | ||
"arduboy": 71, | ||
"atari-2600": 25, | ||
"atari2600": 25, | ||
"atari-7800": 51, | ||
"atari7800": 51, | ||
"atari-jaguar-cd": 77, | ||
"colecovision": 44, | ||
"dreamcast": 40, | ||
"dc": 40, | ||
"gameboy": 4, | ||
"gb": 4, | ||
"gameboy-advance": 5, | ||
"gba": 5, | ||
"gameboy-color": 6, | ||
"gbc": 6, | ||
"game-gear": 15, | ||
"gamegear": 15, | ||
"gamecube": 16, | ||
"ngc": 14, | ||
"genesis": 1, | ||
"genesis-slash-megadrive": 16, | ||
"intellivision": 45, | ||
"jaguar": 17, | ||
"lynx": 13, | ||
"msx": 29, | ||
"mega-duck-slash-cougar-boy": 69, | ||
"nes": 7, | ||
"famicom": 7, | ||
"neo-geo-cd": 56, | ||
"neo-geo-pocket": 14, | ||
"neo-geo-pocket-color": 14, | ||
"n64": 2, | ||
"nintendo-ds": 18, | ||
"nds": 18, | ||
"nintendo-dsi": 78, | ||
"odyssey-2": 23, | ||
"pc-8000": 47, | ||
"pc-8800-series": 47, | ||
"pc-fx": 49, | ||
"psp": 41, | ||
"playstation": 12, | ||
"ps": 12, | ||
"ps2": 21, | ||
"pokemon-mini": 24, | ||
"saturn": 39, | ||
"sega-32x": 10, | ||
"sega32": 10, | ||
"sega-cd": 9, | ||
"segacd": 9, | ||
"sega-master-system": 11, | ||
"sms": 11, | ||
"sg-1000": 33, | ||
"snes": 3, | ||
"turbografx-cd": 76, | ||
"turbografx-16-slash-pc-engine-cd": 76, | ||
"turbo-grafx": 8, | ||
"turbografx16--1": 8, | ||
"vectrex": 26, | ||
"virtual-boy": 28, | ||
"virtualboy": 28, | ||
"watara-slash-quickshot-supervision": 63, | ||
"wonderswan": 53, | ||
"wonderswan-color": 53, | ||
} | ||
|
||
|
||
class RAHasherError(Exception): ... | ||
|
||
|
||
class RAHasherService: | ||
"""Service to calculate RetroAchievements hashes using RAHasher.""" | ||
|
||
async def calculate_hash(self, platform_slug: str, file_path: Path) -> str: | ||
platform_id = PLATFORM_SLUG_TO_RETROACHIEVEMENTS_ID.get(platform_slug) | ||
if not platform_id: | ||
raise ValueError( | ||
f"Platform not supported by RetroAchievements. {platform_slug=}" | ||
) | ||
|
||
args = (str(platform_id), str(file_path)) | ||
log.debug("Executing RAHasher with args: %s", args) | ||
|
||
proc = await asyncio.create_subprocess_exec( | ||
"RAHasher", | ||
*args, | ||
stdout=asyncio.subprocess.PIPE, | ||
stderr=asyncio.subprocess.PIPE, | ||
) | ||
return_code = await proc.wait() | ||
if return_code != 1: | ||
stderr = (await proc.stderr.read()).decode("utf-8") | ||
Check failure on line 108 in backend/adapters/services/rahasher.py Trunk.io / Trunk Checkmypy(union-attr)
|
||
raise RAHasherError(f"RAHasher failed with code {return_code}. {stderr=}") | ||
|
||
file_hash = (await proc.stdout.read()).decode("utf-8").strip() | ||
Check failure on line 111 in backend/adapters/services/rahasher.py Trunk.io / Trunk Checkmypy(union-attr)
|
||
if not file_hash: | ||
raise RAHasherError( | ||
f"RAHasher returned an empty hash. {platform_id=}, {file_path=}" | ||
) | ||
if not RAHASHER_VALID_HASH_REGEX.match(file_hash): | ||
raise RAHasherError( | ||
f"RAHasher returned an invalid hash: {file_hash=}, {platform_id=}, {file_path=}" | ||
) | ||
|
||
return file_hash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters