Files
romm/backend/utils/m3u.py
Sam Dornan 14fc8d9930 fix: treat every disc descriptor as a disc, not just .cue (#4532)
* Treat every disc descriptor as a disc, not just .cue

playlist_files keyed on .cue alone, so a Dreamcast .gdi set, a CloneCD
.ccd set and an Alcohol .mds set all fell through to "every file is a
disc" -- the descriptor and the raw tracks it cannot boot without were
listed side by side.

The three readers of the rule each got that wrong in their own way: the
playlist shipped inside the download zip named non-bootable tracks, and
the in-browser disc swapper offered them as discs.

Key on a set of descriptor extensions instead, so no one sheet format is
privileged over the others. The shape is unchanged -- descriptors if any,
else everything -- and so is the download zip, which lists every file
regardless.

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

* Keep a disc that stands on its own beside a descriptor

Selecting descriptors and discarding everything else assumed a multi-file
ROM is all one format. It is not: a .chd sitting beside a .gdi boots on
its own, and returning only the .gdi dropped it from the playlist and
from the swapper's targets.

Exclude the raw tracks a sheet points at instead of keeping only sheets.
The tracks are still not discs, so every case the previous commit fixed
stays fixed, and a disc no sheet describes now survives.

This also settles the same bug on the .cue path, which predates this
branch: disc1.cue + disc1.bin + disc2.chd used to list the cue alone.

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

* Count a sheet's audio tracks as tracks

A .cue names its CDDA tracks the same way it names its data track, so a
.wav or .flac beside one is data, not a disc. The playlist listed them.

Brings the list in step with the shell's own in src/main/discs/m3u.ts,
which has carried the audio formats since it was written.

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

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-16 11:05:03 -05:00

46 lines
1.8 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from models.rom import RomFile
# Sheet formats that describe a disc by pointing at raw track files: .cue for
# BIN/CUE, .gdi for Dreamcast, .ccd for CloneCD and .mds for Alcohol. No one
# format is privileged over the others.
DESCRIPTOR_EXTENSIONS = frozenset({"cue", "gdi", "ccd", "mds"})
# The raw tracks those sheets point at, data and CDDA audio alike. None of them
# is loadable on its own, so a sheet in the set means its tracks are data rather
# than discs -- but only those tracks. Anything else present stays a disc in its
# own right. Kept in step with the shell's own list in src/main/discs/m3u.ts.
COMPANION_EXTENSIONS = frozenset(
{"bin", "raw", "img", "sub", "mdf", "wav", "ogg", "flac", "mp3"}
)
def playlist_files(files: list[RomFile]) -> list[RomFile]:
"""The files of a multi-file ROM that name a playable disc.
The .m3u itself is never one. Where a descriptor is present the raw tracks
it references are not discs either, since they are not loadable alone --
but a set is not required to be all one format, so a disc that stands on
its own (a .chd beside a .gdi) is kept. One home for the rule: the
playlist, the download endpoint and the disc swapper all have to agree on
which files are discs.
"""
discs = [f for f in files if f.file_extension.lower() != "m3u"]
if not any(f.file_extension.lower() in DESCRIPTOR_EXTENSIONS for f in discs):
return discs
return [f for f in discs if f.file_extension.lower() not in COMPANION_EXTENSIONS]
def generate_m3u_content(
files: list[RomFile],
hidden_folder: bool,
) -> bytes:
"""Generate M3U playlist content for multi-file ROMs."""
return "\n".join(
f.file_name_for_download(hidden_folder) for f in playlist_files(files)
).encode()