diff --git a/frontend/src/v2/utils/playerDisc.test.ts b/frontend/src/v2/utils/playerDisc.test.ts index 2c11bb1e7..f4fc7cff9 100644 --- a/frontend/src/v2/utils/playerDisc.test.ts +++ b/frontend/src/v2/utils/playerDisc.test.ts @@ -1,17 +1,25 @@ import { beforeEach, describe, expect, it } from "vitest"; +import type { RomFileCategory } from "@/__generated__"; import { ALL_DISCS, bootDiscId, + bootableFiles, defaultDisc, rememberDisc, resolveRememberedDisc, resolveStoredDisc, } from "./playerDisc"; -// Only `id` and `file_name` are read off each file; minimal stubs stand in for -// RomFileSchema. +// Only `id`, `file_name` and `category` are read off each file; minimal stubs +// stand in for RomFileSchema. +const file = ( + id: number, + file_name: string, + category: RomFileCategory | null = null, +) => ({ id, file_name, category }); + const files = (...ids: number[]) => - ids.map((id) => ({ id, file_name: `Game (Disc ${id}).chd` })); + ids.map((id) => file(id, `Game (Disc ${id}).chd`)); describe("resolveStoredDisc", () => { it("keeps a stored id that still belongs to the rom", () => { @@ -74,17 +82,12 @@ describe("resolveStoredDisc", () => { describe("defaultDisc", () => { it("boots every file together when the set ships its own playlist", () => { - expect( - defaultDisc([...files(1, 2), { id: 99, file_name: "Game.m3u" }]), - ).toBe(ALL_DISCS); + expect(defaultDisc([...files(1, 2), file(99, "Game.m3u")])).toBe(ALL_DISCS); }); it("matches the playlist extension case-insensitively", () => { expect( - defaultDisc([ - { id: 1, file_name: "Game (Disc 1).chd" }, - { id: 2, file_name: "Game.M3U" }, - ]), + defaultDisc([file(1, "Game (Disc 1).chd"), file(2, "Game.M3U")]), ).toBe(ALL_DISCS); }); @@ -93,12 +96,57 @@ describe("defaultDisc", () => { }); it("boots the only file of a single-file rom carrying a playlist", () => { - expect(defaultDisc([{ id: 1, file_name: "Game.m3u" }])).toBe(1); + expect(defaultDisc([file(1, "Game.m3u")])).toBe(1); }); it("returns null for a rom with no files", () => { expect(defaultDisc([])).toBeNull(); }); + + it("skips media that sorts before the game file", () => { + expect( + defaultDisc([ + file(1, "artwork.png", "screenshot"), + file(2, "Game.chd", "game"), + ]), + ).toBe(2); + }); + + it("still boots a file scanned before categories existed", () => { + expect(defaultDisc([file(1, "Game.chd", null)])).toBe(1); + }); + + it("boots media rather than nothing when that is all the rom has", () => { + expect(defaultDisc([file(1, "manual.pdf", "manual")])).toBe(1); + }); +}); + +describe("bootableFiles", () => { + it("drops every category a core cannot boot", () => { + const game = file(1, "Game.chd", "game"); + + expect( + bootableFiles([ + file(2, "manual.pdf", "manual"), + file(3, "guide.txt", "walkthrough"), + file(4, "shot.png", "screenshot"), + file(5, "track.mp3", "soundtrack"), + file(6, "fix.ips", "patch"), + file(7, "codes.cht", "cheat"), + game, + ]), + ).toEqual([game]); + }); + + it("keeps the parts of a multi-file release", () => { + const release = [ + file(1, "Game.nsp", "game"), + file(2, "Game.nsz", "update"), + file(3, "Game DLC.nsp", "dlc"), + ]; + + expect(bootableFiles(release)).toEqual(release); + }); }); describe("bootDiscId", () => { @@ -160,4 +208,15 @@ describe("rememberDisc / resolveRememberedDisc", () => { rememberDisc(ROM_ID, 2); expect(resolveRememberedDisc(99, files(1, 2))).toBe(1); }); + + it("drops a remembered media file once the caller filters media out", () => { + const bootable = bootableFiles([ + file(1, "manual.pdf", "manual"), + file(2, "Game.chd", "game"), + ]); + rememberDisc(ROM_ID, 1); + + expect(resolveRememberedDisc(ROM_ID, bootable)).toBe(2); + expect(localStorage.getItem(`player:${ROM_ID}:disc-selection`)).toBeNull(); + }); }); diff --git a/frontend/src/v2/utils/playerDisc.ts b/frontend/src/v2/utils/playerDisc.ts index 884f8ef34..c0fba6c81 100644 --- a/frontend/src/v2/utils/playerDisc.ts +++ b/frontend/src/v2/utils/playerDisc.ts @@ -10,6 +10,8 @@ // Asking the download endpoint for no file in particular instead returns every // file zipped with a generated .m3u, which EmulatorJS boots with its in-game // disc switcher (issue #3985). That choice is stored as `ALL_DISCS`. +import type { RomFileCategory } from "@/__generated__"; + export const ALL_DISCS = "all"; // A file id to boot on its own, ALL_DISCS to boot every file together, or null @@ -19,6 +21,32 @@ export type DiscSelection = number | typeof ALL_DISCS | null; interface DiscFile { id: number; file_name: string; + category?: RomFileCategory | null; +} + +// Media uploaded into a folder rom's subdirectory becomes one of the rom's +// files, so Play would otherwise default to a manual (issue #4074). +const NON_BOOTABLE_CATEGORIES: readonly RomFileCategory[] = [ + "manual", + "walkthrough", + "screenshot", + "soundtrack", + "patch", + "cheat", +]; + +function isBootableFile(file: DiscFile): boolean { + // A file scanned before categories existed carries none, so absence boots. + return !file.category || !NON_BOOTABLE_CATEGORIES.includes(file.category); +} + +// Falls back to every file when media is all the rom has, so the player is +// never left with nothing to boot. +export function bootableFiles( + files: readonly T[], +): readonly T[] { + const bootable = files.filter(isBootableFile); + return bootable.length > 0 ? bootable : files; } export interface ResolvedDisc { @@ -51,7 +79,7 @@ export function resolveStoredDisc( // anything else boots one file to avoid pulling hundreds of unused MB. export function defaultDisc(files: readonly DiscFile[]): DiscSelection { if (files.length > 1 && files.some(isM3uFile)) return ALL_DISCS; - return files[0]?.id ?? null; + return bootableFiles(files)[0]?.id ?? null; } // The file id to download, or null to download the rom whole. diff --git a/frontend/src/v2/views/Player/EmulatorJS.vue b/frontend/src/v2/views/Player/EmulatorJS.vue index b256d2213..fffd71469 100644 --- a/frontend/src/v2/views/Player/EmulatorJS.vue +++ b/frontend/src/v2/views/Player/EmulatorJS.vue @@ -66,6 +66,7 @@ import { import { ALL_DISCS, bootDiscId, + bootableFiles, rememberDisc, resolveRememberedDisc, type DiscSelection, @@ -138,9 +139,11 @@ const compatibleStates = computed( ) ?? [], ); +const bootableRomFiles = computed(() => bootableFiles(rom.value?.files ?? [])); + const discItems = computed<{ title: string; value: DiscSelection }[]>(() => [ { title: t("play.all-discs"), value: ALL_DISCS }, - ...(rom.value?.files ?? []).map((f) => ({ + ...bootableRomFiles.value.map((f) => ({ title: f.file_name, value: f.id, })), @@ -345,7 +348,10 @@ onMounted(async () => { } isSavesTabSelected.value = !hasCompatibleState; - selectedDisc.value = resolveRememberedDisc(rom.value.id, rom.value.files); + selectedDisc.value = resolveRememberedDisc( + rom.value.id, + bootableRomFiles.value, + ); selectedCore.value = resolveRememberedCore( rom.value.id, @@ -590,7 +596,7 @@ const selectedAsset = computed(() =>
(rom.value?.files ?? []).some((f) => fileExtension(f.file_name) === "m3u"), ); +const bootableRomFiles = computed(() => bootableFiles(rom.value?.files ?? [])); + // Mirrors the download endpoint's playlist filtering: when .cue files are // present only those are valid swap targets (raw .bin tracks are not), and // the .m3u itself is never something to swap to. const discOptions = computed(() => { - const files = (rom.value?.files ?? []).filter( + const files = bootableRomFiles.value.filter( (f) => fileExtension(f.file_name) !== "m3u", ); const cueFiles = files.filter((f) => fileExtension(f.file_name) === "cue"); @@ -275,7 +278,7 @@ const canSwapDisc = computed( const showManualDiscHint = computed( () => capabilities.value.hasManualDiscSwap && - (rom.value?.files?.length ?? 0) > 1 && + bootableRomFiles.value.length > 1 && !isJoining, );