Files
romm/backend/utils/platform_aliases.py
T
54fecf2500 fix(setup): resolve folder aliases when identifying platforms in the setup wizard (#5473)
* fix(setup): resolve folder aliases when identifying platforms in the setup wizard

The setup library endpoint returned raw folder names, so folders such as
gamecube or a config-bound nintendo-snes showed as Unidentified and their
platform stayed in the supported list. Report the resolved platform slug
next to each folder and match on it in the wizard.

Fixes #5465

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* fix(setup): match supported platforms on resolved slug, one detected row per folder

pr-ready step 2 (code-review xhigh --fix): compare supported platforms by
slug so a DB row with an alias fs_slug still matches, keep each folder's own
name and rom count, read config once per request, black-format the new test,
and add a SetupStepPlatforms unit test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(setup): fold detected-row branches, drop redundant selection guards

pr-ready step 3 (simplify): build identified and unidentified rows from one
object, drop the detected checks in togglePlatform and newSlugsInGroup (every
caller already lists only supportedAvailable), and merge the duplicated chip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(setup): assert the resolved slug in every setup/library entry

pr-ready step 4 (review-polish): compare whole existing_platforms entries in
the older setup/library tests so they cover slug too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(setup): tighten detected-row comment

pr-ready step 5 (humanizer).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(settings): resolve built-in folder aliases in folder mappings

Folder Mappings matched folders to platforms on the client by exact slug,
so alias folders like gamecube or megadrive showed as unmapped even though
the scan resolves them. The heartbeat now returns FS_PLATFORM_SLUGS, each
folder's slug from its name or built-in alias, and the table reads it for
auto-detected rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: drop response schema field comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
2026-11-08 22:25:29 -05:00

228 lines
6.9 KiB
Python

from __future__ import annotations
from collections.abc import Iterable
from typing import TYPE_CHECKING, Final
from utils.platform_slugs import UniversalPlatformSlug as UPS
if TYPE_CHECKING:
from config.config_manager import Config
# Folder names used by Batocera, RetroBat and ES-DE that differ from RomM slugs.
PLATFORM_FS_ALIASES: Final[dict[str, UPS]] = {
"adam": UPS.COLECOADAM,
"advision": UPS.ADVENTURE_VISION,
"amiga1200": UPS.AMIGA,
"amiga4000": UPS.AMIGA,
"amiga500": UPS.AMIGA,
"amiga600": UPS.AMIGA,
"amigacd32": UPS.AMIGA_CD32,
"amigacdtv": UPS.AMIGA_CD32,
"amstradcpc": UPS.ACPC,
"apfm1000": UPS.APF,
"apple2": UPS.APPLEII,
"apple2gs": UPS.APPLE_IIGS,
"arcadia": UPS.ARCADIA_2001,
"archimedes": UPS.ACORN_ARCHIMEDES,
"astrocde": UPS.ASTROCADE,
"atarijaguar": UPS.JAGUAR,
"atarijaguarcd": UPS.ATARI_JAGUAR_CD,
"atarilynx": UPS.LYNX,
"atarist": UPS.ATARI_ST,
"atarixe": UPS.ATARI_XEGS,
"bbc": UPS.BBCMICRO,
"c20": UPS.VIC_20,
"camplynx": UPS.CAMPUTERS_LYNX,
"cdi": UPS.PHILIPS_CD_I,
"cdimono1": UPS.PHILIPS_CD_I,
"cdtv": UPS.COMMODORE_CDTV,
"channelf": UPS.FAIRCHILD_CHANNEL_F,
"coco": UPS.TRS_80_COLOR_COMPUTER,
"consolearcade": UPS.ARCADE,
"cplus4": UPS.C_PLUS_4,
"cps": UPS.ARCADE,
"cps1": UPS.ARCADE,
"cps2": UPS.ARCADE,
"cps3": UPS.ARCADE,
"crvision": UPS.CREATIVISION,
"dragon32": UPS.DRAGON_32_SLASH_64,
"dreamcast": UPS.DC,
"easyrpg": UPS.RPG_MAKER,
"electron": UPS.ACORN_ELECTRON,
"flash": UPS.BROWSER,
"fm7": UPS.FM_7,
"fmtowns": UPS.FM_TOWNS,
"gameandwatch": UPS.G_AND_W,
"gamecom": UPS.GAME_DOT_COM,
"gamecube": UPS.NGC,
"gamepock": UPS.EPOCH_GAME_POCKET_COMPUTER,
"gb2players": UPS.GB,
"gba2players": UPS.GBA,
"gbc2players": UPS.GBC,
"gc": UPS.NGC,
"gmaster": UPS.HARTUNG,
"gx4000": UPS.AMSTRAD_GX4000,
"gzdoom": UPS.DOOM,
"jaguarcd": UPS.ATARI_JAGUAR_CD,
"laserdisc": UPS.DAPHNE,
"lcdgames": UPS.HANDHELD_ELECTRONIC_LCD,
"macintosh": UPS.MAC,
"mark3": UPS.SMS,
"mastersystem": UPS.SMS,
"megacd": UPS.SEGACD,
"megacdjp": UPS.SEGACD,
"megadrive": UPS.GENESIS,
"megadrive-msu": UPS.GENESIS,
"megadrivejp": UPS.GENESIS,
"megaduck": UPS.MEGA_DUCK_SLASH_COUGAR_BOY,
"model1": UPS.ARCADE,
"msu-md": UPS.GENESIS,
"msx1": UPS.MSX,
"msx2+": UPS.MSX2PLUS,
"msxturbor": UPS.MSX_TURBO,
"mz2500": UPS.SHARP_MZ_80B20002500,
"mz700": UPS.SHARP_MZ_80K7008001500,
"mz800": UPS.SHARP_MZ_80K7008001500,
"mz80k": UPS.SHARP_MZ_80K7008001500,
"n3ds": UPS.N3DS,
"n64dd": UPS.N64DD,
"namco22": UPS.ARCADE,
"neogeo": UPS.NEOGEOAES,
"neogeocd": UPS.NEO_GEO_CD,
"ngp": UPS.NEO_GEO_POCKET,
"ngpc": UPS.NEO_GEO_POCKET_COLOR,
"odyssey2": UPS.ODYSSEY_2,
"oricatmos": UPS.ATMOS,
"palm": UPS.PALM_OS,
"pc": UPS.WIN,
"pc88": UPS.PC_8800_SERIES,
"pc98": UPS.PC_9800_SERIES,
"pcengine": UPS.TG16,
"pcenginecd": UPS.TURBOGRAFX_CD,
"pcfx": UPS.PC_FX,
"pcw": UPS.AMSTRAD_PCW,
"pet": UPS.CPET,
"plugnplay": UPS.PLUG_AND_PLAY,
"pokemini": UPS.POKEMON_MINI,
"prboom": UPS.DOOM,
"ps": UPS.PSX,
"ps3-psn": UPS.PS3,
"pspminis": UPS.PSP_MINIS,
"pv1000": UPS.CASIO_PV_1000,
"samcoupe": UPS.SAM_COUPE,
"scv": UPS.EPOCH_SUPER_CASSETTE_VISION,
"sega32x": UPS.SEGA32,
"segastv": UPS.STV,
"sfc": UPS.SFAM,
"sg-1000": UPS.SG1000,
"sgb": UPS.GBC,
"sgb-msu1": UPS.GBC,
"snes-msu1": UPS.SNES,
"sufami": UPS.SUFAMI_TURBO,
"supracan": UPS.SUPER_ACAN,
"system-32": UPS.ARCADE,
"system16": UPS.ARCADE,
"system32": UPS.ARCADE,
"tg-cd": UPS.TURBOGRAFX_CD,
"thomson": UPS.THOMSON_MO5,
"ti99": UPS.TI_99,
"tic80": UPS.TIC_80,
"tutor": UPS.TOMY_TUTOR,
"typex": UPS.TYPE_X,
"vc4000": UPS.VC_4000,
"vic20": UPS.VIC_20,
"videopac": UPS.VIDEOPAC_G7400,
"videopacplus": UPS.VIDEOPAC_G7400,
"vpinball": UPS.PINBALL,
"wasm4": UPS.WASM_4,
"wiiware": UPS.WII,
"windows": UPS.WIN,
"windows3x": UPS.WIN3X,
"windows9x": UPS.WIN9X,
"wonderswancolor": UPS.WONDERSWAN_COLOR,
"wswan": UPS.WONDERSWAN,
"wswanc": UPS.WONDERSWAN_COLOR,
"x68000": UPS.SHARP_X68000,
"xegs": UPS.ATARI_XEGS,
"zc210": UPS.ZELDA_CLASSIC,
"zc250": UPS.ZELDA_CLASSIC,
"zmachine": UPS.Z_MACHINE,
"zxspectrum": UPS.ZXS,
}
def _unambiguous_folders() -> dict[str, str]:
folders_by_slug: dict[UPS, list[str]] = {}
for fs_slug, slug in PLATFORM_FS_ALIASES.items():
folders_by_slug.setdefault(slug, []).append(fs_slug)
return {
slug.value: folders[0]
for slug, folders in folders_by_slug.items()
if len(folders) == 1
}
# Only a slug reached by exactly one folder name can be resolved back to it.
PLATFORM_SLUG_FOLDERS: Final[dict[str, str]] = _unambiguous_folders()
def _to_slug(name: str) -> str:
key = name.lower()
if key in UPS:
return key
alias = PLATFORM_FS_ALIASES.get(key)
return alias.value if alias else name
def resolve_builtin_platform_slug(fs_slug: str) -> str | None:
"""Resolve a folder name to a platform slug by its name or built-in alias.
Returns:
The platform slug, or None when the folder needs a config mapping.
"""
slug = _to_slug(fs_slug)
return slug if slug in UPS else None
def resolve_platform_slug(fs_slug: str, config: Config) -> str:
"""Resolve a platform folder name to a RomM platform slug."""
key = fs_slug.lower()
bound = config.PLATFORMS_BINDING.get(key) or config.PLATFORMS_VERSIONS.get(key)
# A bound name resolves like a folder name, so it may be an alias too.
return _to_slug(bound or fs_slug)
def resolve_fs_slug(slug: str, config: Config) -> str | None:
"""Resolve a RomM platform slug back to the folder name it came from.
Returns:
The folder name, or None when no folder maps to the slug or several
alias folders collapse onto it.
"""
for mapping in (config.PLATFORMS_BINDING, config.PLATFORMS_VERSIONS):
for fs_slug, bound in mapping.items():
if _to_slug(bound) == slug:
return fs_slug
return PLATFORM_SLUG_FOLDERS.get(slug)
def resolve_fs_folder(fs_slug: str, fs_platforms: Iterable[str]) -> str | None:
"""Resolve a folder name to the one on disk it names, ignoring case.
Config keys are lowercased, but paths are built from the folder's own
casing. An exact match wins, so on a case-sensitive filesystem two folders
differing only by case each still resolve to themselves.
Returns:
The folder as it is spelled on disk, or None when none matches.
"""
folded = fs_slug.lower()
fallback: str | None = None
for folder in fs_platforms:
if folder == fs_slug:
return folder
if fallback is None and folder.lower() == folded:
fallback = folder
return fallback