diff --git a/backend/handler/metadata/gamelist_handler.py b/backend/handler/metadata/gamelist_handler.py
index c23d6c897..7bf1c9e48 100644
--- a/backend/handler/metadata/gamelist_handler.py
+++ b/backend/handler/metadata/gamelist_handler.py
@@ -1,10 +1,9 @@
-import glob
import os
import re
import uuid
from collections.abc import Iterator
from pathlib import Path
-from typing import Final, NotRequired, TypedDict
+from typing import Final, Literal, NotRequired, TypedDict
from xml.etree.ElementTree import Element # trunk-ignore(bandit/B405)
import pydash
@@ -90,7 +89,36 @@ class GamelistRom(BaseRom):
gamelist_metadata: NotRequired[GamelistMetadata]
-ESDE_MEDIA_MAP: Final = {
+MediaUrlKey = Literal[
+ "box2d_url",
+ "box2d_back_url",
+ "box3d_url",
+ "fanart_url",
+ "image_url",
+ "manual_url",
+ "marquee_url",
+ "miximage_url",
+ "miximage_v2_url",
+ "physical_url",
+ "screenshot_url",
+ "thumbnail_url",
+ "title_screen_url",
+ "video_url",
+]
+
+MediaPathKey = Literal[
+ "box2d_back_path",
+ "box3d_path",
+ "fanart_path",
+ "miximage_path",
+ "miximage_v2_path",
+ "physical_path",
+ "marquee_path",
+ "title_screen_path",
+ "video_path",
+]
+
+ESDE_MEDIA_MAP: Final[dict[MediaUrlKey, str]] = {
"image_url": PLATFORM_MEDIA_DIRS["image"],
"box2d_url": PLATFORM_MEDIA_DIRS["box2d"],
"box2d_back_url": PLATFORM_MEDIA_DIRS["box2d_back"],
@@ -107,7 +135,7 @@ ESDE_MEDIA_MAP: Final = {
"video_url": PLATFORM_MEDIA_DIRS["video"],
}
-XML_TAG_MAP: Final = {
+XML_TAG_MAP: Final[dict[MediaUrlKey, str]] = {
"image_url": "image",
"box2d_url": "cover",
"box2d_back_url": "backcover",
@@ -157,8 +185,73 @@ def _split_comma_separated_values(value: str | None) -> list[str]:
return pydash.compact([item.strip() for item in split_values])
+MediaFileIndex = dict[MediaUrlKey, dict[str, str]]
+
+# The order ES-DE probes extensions in when a ROM has media in several formats
+ESDE_MEDIA_EXTENSION_PRIORITY: Final = (
+ ".png",
+ ".jpg",
+ ".webp",
+ ".mp4",
+ ".mkv",
+ ".avi",
+ ".wmv",
+ ".mov",
+ ".webm",
+ ".m4v",
+ ".pdf",
+)
+
+
+_ESDE_MEDIA_EXTENSION_RANK: Final = {
+ extension: rank for rank, extension in enumerate(ESDE_MEDIA_EXTENSION_PRIORITY)
+}
+
+
+def _esde_media_rank(file_name: str) -> tuple[int, str]:
+ extension = os.path.splitext(file_name)[1].lower()
+ return (
+ _ESDE_MEDIA_EXTENSION_RANK.get(extension, len(_ESDE_MEDIA_EXTENSION_RANK)),
+ file_name,
+ )
+
+
+def build_media_file_index(platform: Platform) -> MediaFileIndex:
+ """Index each ES-DE media folder's files by stem, listing each folder once.
+
+ Returns:
+ The stem-to-URI mapping for every ESDE_MEDIA_MAP key.
+ """
+ platform_dir = fs_platform_handler.get_platform_fs_structure(platform.fs_slug)
+ index: MediaFileIndex = {}
+
+ for media_key, folder_name in ESDE_MEDIA_MAP.items():
+ # Media is only a fallback, so an unusable folder must not fail the parse
+ try:
+ folder = fs_platform_handler.validate_path(
+ os.path.join(platform_dir, folder_name)
+ )
+ uri_prefix = f"file://{folder.relative_to(fs_platform_handler.base_path)}"
+ with os.scandir(folder) as it:
+ file_names = sorted(
+ (entry.name for entry in it if entry.is_file()),
+ key=_esde_media_rank,
+ )
+ except OSError, ValueError:
+ file_names = []
+
+ stems: dict[str, str] = {}
+ for file_name in file_names:
+ stems.setdefault(
+ os.path.splitext(file_name)[0], f"{uri_prefix}/{file_name}"
+ )
+ index[media_key] = stems
+
+ return index
+
+
def extract_media_from_gamelist_rom(
- game: Element, platform: Platform
+ game: Element, platform: Platform, media_files: MediaFileIndex
) -> GamelistMetadataMedia:
platform_dir = fs_platform_handler.get_platform_fs_structure(platform.fs_slug)
@@ -183,30 +276,52 @@ def extract_media_from_gamelist_rom(
for media_key, xml_tag in XML_TAG_MAP.items():
elem = game.find(xml_tag)
if elem is not None and elem.text:
- gamelist_media[media_key] = _make_file_uri(platform_dir, elem.text) # type: ignore[literal-required]
+ try:
+ gamelist_media[media_key] = _make_file_uri(platform_dir, elem.text)
+ except ValueError as e:
+ log.debug(f"Skipping gamelist <{xml_tag}> outside the library: {e}")
- # Fallback to searching media folders by ROM basename
+ # Fallback to the media folders' files named after the ROM
path_elem = game.find("path")
if path_elem is not None and path_elem.text:
- rom_stem = os.path.splitext(os.path.basename(path_elem.text))[0]
+ rom_name = os.path.basename(path_elem.text)
+ rom_stem = os.path.splitext(rom_name)[0]
+ is_directory: bool | None = None
- for media_key, folder_name in ESDE_MEDIA_MAP.items():
- if gamelist_media[media_key]: # type: ignore[literal-required]
+ for media_key in ESDE_MEDIA_MAP:
+ if gamelist_media[media_key]:
continue
- search_pattern = os.path.join(platform_dir, folder_name, f"{rom_stem}.*")
- search_path = fs_platform_handler.validate_path(search_pattern)
- found_files = glob.glob(str(search_path))
- if found_files:
- gamelist_media[media_key] = ( # type: ignore[literal-required]
- f"file://{str(Path(found_files[0]).relative_to(fs_platform_handler.base_path))}"
- )
+ files = media_files[media_key]
+ if rom_name in files and rom_stem in files:
+ # ES-DE names a directory's media after its full name, a file's
+ # after its stem, so only a name both could match needs the disk.
+ if is_directory is None:
+ is_directory = _is_directory_entry(
+ game, platform_dir, path_elem.text
+ )
+ name = rom_name if is_directory else rom_stem
+ else:
+ name = rom_name if rom_name in files else rom_stem
+ gamelist_media[media_key] = files.get(name)
return gamelist_media
+def _is_directory_entry(game: Element, platform_dir: str, raw_path: str) -> bool:
+ """Whether a gamelist entry names a directory rather than a file."""
+ if game.tag == "folder":
+ return True
+ try:
+ return fs_platform_handler.validate_path(
+ os.path.join(platform_dir, gamelist_path_to_rel_path(raw_path))
+ ).is_dir()
+ except ValueError:
+ return False
+
+
def extract_metadata_from_gamelist_rom(
- game: Element, platform: Platform
+ game: Element, platform: Platform, media_files: MediaFileIndex
) -> GamelistMetadata:
rating_elem = game.find("rating")
releasedate_elem = game.find("releasedate")
@@ -271,18 +386,18 @@ def extract_metadata_from_gamelist_rom(
marquee_path=None,
title_screen_path=None,
video_path=None,
- **extract_media_from_gamelist_rom(game, platform),
+ **extract_media_from_gamelist_rom(game, platform, media_files),
)
def populate_rom_specific_paths(
rom_metadata: GamelistMetadata, rom: Rom
-) -> dict[str, str]:
+) -> dict[MediaPathKey, str]:
"""Populate ROM-specific paths after retrieving metadata from cache"""
preferred_media_types = get_preferred_media_types()
# Create a copy of the metadata to avoid modifying the cached version
- updated_metadata: dict[str, str] = {}
+ updated_metadata: dict[MediaPathKey, str] = {}
# Set paths for media types that are preferred
if MetadataMediaType.BOX2D_BACK in preferred_media_types and rom_metadata.get(
@@ -435,6 +550,7 @@ class GamelistHandler(MetadataHandler):
ambiguous_filenames: set[str] = set()
try:
+ media_files = build_media_file_index(platform)
for game in self._iter_game_elements(gamelist_path):
if game.tag not in ("game", "folder"):
continue
@@ -476,7 +592,9 @@ class GamelistHandler(MetadataHandler):
)
# Build ROM data
- rom_metadata = extract_metadata_from_gamelist_rom(game, platform)
+ rom_metadata = extract_metadata_from_gamelist_rom(
+ game, platform, media_files
+ )
name_sort_key = compute_name_sort_key(sort_name) if sort_name else None
rom_data = GamelistRom(
gamelist_id=str(uuid.uuid4()),
@@ -564,7 +682,8 @@ class GamelistHandler(MetadataHandler):
# Populate ROM-specific paths using the actual rom object
if gamelist_metadata:
rom_specific_paths = populate_rom_specific_paths(gamelist_metadata, rom)
- gamelist_metadata.update(**rom_specific_paths) # type: ignore[call-arg]
+ for path_key, path in rom_specific_paths.items():
+ gamelist_metadata[path_key] = path
matched_rom["gamelist_metadata"] = gamelist_metadata
return matched_rom
diff --git a/backend/tests/handler/metadata/test_gamelist_handler.py b/backend/tests/handler/metadata/test_gamelist_handler.py
index bd8558cc4..9c48762df 100644
--- a/backend/tests/handler/metadata/test_gamelist_handler.py
+++ b/backend/tests/handler/metadata/test_gamelist_handler.py
@@ -10,6 +10,9 @@ from config.config_manager import MetadataMediaType
from handler.filesystem import fs_platform_handler
from handler.metadata.gamelist_handler import (
GamelistHandler,
+ GamelistMetadataMedia,
+ build_media_file_index,
+ extract_media_from_gamelist_rom,
extract_metadata_from_gamelist_rom,
populate_rom_specific_paths,
)
@@ -242,11 +245,193 @@ def test_extract_metadata_from_gamelist_rom_includes_sort_name(platform: Platfor
"handler.metadata.gamelist_handler.extract_media_from_gamelist_rom",
return_value=MOCK_MEDIA,
):
- metadata = extract_metadata_from_gamelist_rom(game, platform)
+ metadata = extract_metadata_from_gamelist_rom(game, platform, {})
assert metadata["sort_name"] == "Akumajou Dracula"
+def _platform_dir(platform: Platform) -> str:
+ return fs_platform_handler.get_platform_fs_structure(platform.fs_slug)
+
+
+def _write_media(root: Path, platform: Platform, folder: str, name: str) -> None:
+ media_dir = root / _platform_dir(platform) / folder
+ media_dir.mkdir(parents=True, exist_ok=True)
+ (media_dir / name).write_bytes(b"")
+
+
+def _extract_media(
+ tmp_path: Path, platform: Platform, game_xml: str
+) -> GamelistMetadataMedia:
+ with patch.object(fs_platform_handler, "base_path", tmp_path):
+ return extract_media_from_gamelist_rom(
+ ET.fromstring(game_xml), platform, build_media_file_index(platform)
+ )
+
+
+def _write_gamelist(tmp_path: Path, platform: Platform, games_xml: str) -> Path:
+ gamelist_path = tmp_path / _platform_dir(platform) / "gamelist.xml"
+ gamelist_path.parent.mkdir(parents=True, exist_ok=True)
+ gamelist_path.write_text(
+ f'\n{games_xml}', encoding="utf-8"
+ )
+ return gamelist_path
+
+
+def test_build_media_file_index_maps_stems_to_uris(tmp_path: Path, platform: Platform):
+ _write_media(tmp_path, platform, "covers", "Game.png")
+ _write_media(tmp_path, platform, "videos", "Game.mp4")
+ platform_dir = _platform_dir(platform)
+ (tmp_path / platform_dir / "covers" / "Other.d").mkdir()
+
+ with patch.object(fs_platform_handler, "base_path", tmp_path):
+ index = build_media_file_index(platform)
+
+ assert index["box2d_url"] == {"Game": f"file://{platform_dir}/covers/Game.png"}
+ assert index["video_url"] == {"Game": f"file://{platform_dir}/videos/Game.mp4"}
+ assert index["manual_url"] == {}
+
+
+def test_build_media_file_index_follows_esde_extension_order(
+ tmp_path: Path, platform: Platform
+):
+ for name in ("Game.jpg", "Game.png", "Game.webp"):
+ _write_media(tmp_path, platform, "covers", name)
+ for name in ("Game.avi", "Game.mkv", "Game.mp4"):
+ _write_media(tmp_path, platform, "videos", name)
+ platform_dir = _platform_dir(platform)
+
+ with patch.object(fs_platform_handler, "base_path", tmp_path):
+ index = build_media_file_index(platform)
+
+ assert index["box2d_url"]["Game"] == f"file://{platform_dir}/covers/Game.png"
+ assert index["video_url"]["Game"] == f"file://{platform_dir}/videos/Game.mp4"
+
+
+@pytest.mark.parametrize(
+ "media_names, game_xml, expected",
+ [
+ # A name with a dot in it isn't a prefix of a longer title
+ (
+ ["Super Mario Bros. 3.png"],
+ "./Super Mario Bros.zip",
+ None,
+ ),
+ (
+ ["Super Mario Bros. 3.png"],
+ "./Super Mario Bros. 3.zip",
+ "covers/Super Mario Bros. 3.png",
+ ),
+ # Directories are named in full
+ (
+ ["Final Fantasy VII.m3u.png"],
+ "./Final Fantasy VII.m3u",
+ "covers/Final Fantasy VII.m3u.png",
+ ),
+ (
+ ["Dr.png", "Dr. Mario.png"],
+ "./Dr. Mario",
+ "covers/Dr. Mario.png",
+ ),
+ # A file keeps its stem when media exists under both names
+ (
+ ["Game.png", "Game.zip.png"],
+ "./Game.zip",
+ "covers/Game.png",
+ ),
+ # An explicit tag wins over the media folder
+ (
+ ["Game.png"],
+ "./Game.zip./art/box.png",
+ "art/box.png",
+ ),
+ ],
+)
+def test_extract_media_matches_the_rom_name(
+ tmp_path: Path,
+ platform: Platform,
+ media_names: list[str],
+ game_xml: str,
+ expected: str | None,
+):
+ for name in media_names:
+ _write_media(tmp_path, platform, "covers", name)
+
+ media = _extract_media(tmp_path, platform, game_xml)
+
+ assert media["box2d_url"] == (
+ f"file://{_platform_dir(platform)}/{expected}" if expected else None
+ )
+
+
+def test_extract_media_names_a_game_directory_in_full(
+ tmp_path: Path, platform: Platform
+):
+ for name in ("Final Fantasy VII.png", "Final Fantasy VII.m3u.png"):
+ _write_media(tmp_path, platform, "covers", name)
+ (tmp_path / _platform_dir(platform) / "Final Fantasy VII.m3u").mkdir()
+
+ media = _extract_media(
+ tmp_path, platform, "./Final Fantasy VII.m3u"
+ )
+
+ assert media["box2d_url"] == (
+ f"file://{_platform_dir(platform)}/covers/Final Fantasy VII.m3u.png"
+ )
+
+
+def test_parse_gamelist_xml_indexes_media_once(tmp_path: Path, platform: Platform):
+ for name in ("One", "Two", "Three"):
+ _write_media(tmp_path, platform, "covers", f"{name}.png")
+ gamelist_path = _write_gamelist(
+ tmp_path,
+ platform,
+ "./One.zip"
+ "./Two.zip"
+ "./Three.zip",
+ )
+
+ with (
+ patch.object(fs_platform_handler, "base_path", tmp_path),
+ patch(
+ "handler.metadata.gamelist_handler.get_preferred_media_types",
+ return_value=[],
+ ),
+ patch(
+ "handler.metadata.gamelist_handler.build_media_file_index",
+ wraps=build_media_file_index,
+ ) as build_index,
+ ):
+ roms_data = GamelistHandler()._parse_gamelist_xml(gamelist_path, platform)
+
+ build_index.assert_called_once_with(platform)
+ assert roms_data["Two.zip"].get("url_cover", "").endswith("/covers/Two.png")
+
+
+def test_parse_gamelist_xml_skips_media_tags_outside_the_library(
+ tmp_path: Path, platform: Platform
+):
+ _write_media(tmp_path, platform, "covers", "One.png")
+ gamelist_path = _write_gamelist(
+ tmp_path,
+ platform,
+ "./One.zip/home/pi/covers/One.png"
+ "./Two.zip",
+ )
+
+ with (
+ patch.object(fs_platform_handler, "base_path", tmp_path),
+ patch(
+ "handler.metadata.gamelist_handler.get_preferred_media_types",
+ return_value=[],
+ ),
+ ):
+ roms_data = GamelistHandler()._parse_gamelist_xml(gamelist_path, platform)
+
+ assert roms_data["One.zip"].get("url_cover", "").endswith("/covers/One.png")
+ assert "Two.zip" in roms_data
+
+
@pytest.mark.parametrize(
"url_key, media_type, expected_path_key, expected_filename",
[