diff --git a/frontend/src/utils/index.test.ts b/frontend/src/utils/index.test.ts index fa9015a50..95284baef 100644 --- a/frontend/src/utils/index.test.ts +++ b/frontend/src/utils/index.test.ts @@ -2,7 +2,11 @@ import { describe, expect, it } from "vitest"; import type { Config } from "@/stores/config"; import type { Heartbeat } from "@/stores/heartbeat"; import type { SimpleRom } from "@/stores/roms"; -import { getDownloadPath, isJsDosEmulationSupported } from "./index"; +import { + getDownloadPath, + isJsDosBundle, + isJsDosEmulationSupported, +} from "./index"; function makeRom(overrides: Partial): SimpleRom { return { @@ -142,3 +146,25 @@ describe("isJsDosEmulationSupported", () => { ).toBe(true); }); }); + +describe("isJsDosBundle", () => { + const withExt = (fs_extension: string) => + makeRom({ fs_extension } as Partial); + + it("accepts a .jsdos bundle regardless of case", () => { + expect(isJsDosBundle(withExt("jsdos"))).toBe(true); + expect(isJsDosBundle(withExt("JSDOS"))).toBe(true); + }); + + // js-dos panics with "Broken bundle" on anything that is not its own format. + it("rejects plain archives, bare executables and folders", () => { + expect(isJsDosBundle(withExt("zip"))).toBe(false); + expect(isJsDosBundle(withExt("exe"))).toBe(false); + expect(isJsDosBundle(withExt(""))).toBe(false); + }); + + it("rejects a missing rom", () => { + expect(isJsDosBundle(null)).toBe(false); + expect(isJsDosBundle(undefined)).toBe(false); + }); +}); diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index 0bb315279..9ca29dfd9 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -702,6 +702,9 @@ export function isRuffleEmulationSupported( return ["flash", "browser"].includes(slug.toLowerCase()); } +/** File extension of a js-dos bundle. */ +export const JSDOS_BUNDLE_EXTENSION = "jsdos"; + /** * Check if js-dos emulation is supported for a given platform. * @@ -721,6 +724,21 @@ export function isJsDosEmulationSupported( return ["win3x", "win9x"].includes(slug.toLowerCase()); } +/** + * Check if a ROM file is a js-dos bundle. + * + * js-dos boots only its own bundle format: an archive carrying + * `.jsdos/dosbox.conf`. Handed a bare game folder or a plain archive it panics + * with "Broken bundle", so the play routes gate on the extension rather than + * offering a player that cannot start. + * + * @param rom The ROM to check. + * @returns True if the file is a js-dos bundle, false otherwise. + */ +export function isJsDosBundle(rom: SimpleRom | null | undefined) { + return rom?.fs_extension.toLowerCase() === JSDOS_BUNDLE_EXTENSION; +} + export type PlayingStatus = RomUserStatus | "backlogged" | "now_playing" | "hidden"; diff --git a/frontend/src/v2/composables/useCanPlay/index.test.ts b/frontend/src/v2/composables/useCanPlay/index.test.ts index 072a6d7c5..65ff389dc 100644 --- a/frontend/src/v2/composables/useCanPlay/index.test.ts +++ b/frontend/src/v2/composables/useCanPlay/index.test.ts @@ -9,6 +9,9 @@ const support = vi.hoisted(() => ({ ejs: vi.fn(() => false), jsDos: vi.fn(() => false), ruffle: vi.fn(() => false), + // js-dos also demands its own bundle format; on by default so the engine + // stubs stay the only variable. + jsDosBundle: vi.fn(() => true), })); const heartbeat = ref({}); @@ -22,6 +25,7 @@ vi.mock("@/utils", () => ({ isEJSEmulationSupported: support.ejs, isJsDosEmulationSupported: support.jsDos, isRuffleEmulationSupported: support.ruffle, + isJsDosBundle: support.jsDosBundle, })); function makeRom(overrides: Partial = {}): SimpleRom { @@ -38,9 +42,11 @@ beforeEach(() => { support.ejs.mockReturnValue(false); support.jsDos.mockReturnValue(false); support.ruffle.mockReturnValue(false); + support.jsDosBundle.mockReturnValue(true); support.ejs.mockClear(); support.jsDos.mockClear(); support.ruffle.mockClear(); + support.jsDosBundle.mockClear(); }); describe("useCanPlay", () => { @@ -85,6 +91,17 @@ describe("useCanPlay", () => { expect(canPlayRuffle.value).toBe(false); }); + // A bare game folder or plain archive makes js-dos panic with + // "Broken bundle", so offering Play would hand the user a dead player. + it("refuses js-dos for a rom that is not a bundle", () => { + support.jsDos.mockReturnValue(true); + support.jsDosBundle.mockReturnValue(false); + const { canPlay, canPlayJsDos } = useCanPlay(() => makeRom()); + + expect(canPlayJsDos.value).toBe(false); + expect(canPlay.value).toBe(false); + }); + it("stays unplayable when no engine supports the platform", () => { const { canPlay } = useCanPlay(() => makeRom()); diff --git a/frontend/src/v2/composables/useCanPlay/index.ts b/frontend/src/v2/composables/useCanPlay/index.ts index 6c5ebfeba..0f3342ea8 100644 --- a/frontend/src/v2/composables/useCanPlay/index.ts +++ b/frontend/src/v2/composables/useCanPlay/index.ts @@ -6,8 +6,9 @@ // "Playable" means EJS, js-dos, or Ruffle can run the platform on this // server (admin toggles + platform support + WebGL availability), and // there is a file to boot: a physical game or one missing from the -// filesystem has nothing to hand the emulator. The individual flags are -// exposed so the play action can pick the right route. +// filesystem has nothing to hand the emulator. js-dos additionally needs +// the file to be one of its own bundles. The individual flags are exposed +// so the play action can pick the right route. import { storeToRefs } from "pinia"; import { computed, type ComputedRef } from "vue"; import storeConfig from "@/stores/config"; @@ -15,6 +16,7 @@ import storeHeartbeat from "@/stores/heartbeat"; import type { SimpleRom } from "@/stores/roms"; import { isEJSEmulationSupported, + isJsDosBundle, isJsDosEmulationSupported, isRuffleEmulationSupported, } from "@/utils"; @@ -37,9 +39,15 @@ export function useCanPlay(getRom: () => SimpleRom | null | undefined): { }); const canPlayEJS = supportedBy(isEJSEmulationSupported); - const canPlayJsDos = supportedBy(isJsDosEmulationSupported); const canPlayRuffle = supportedBy(isRuffleEmulationSupported); + // js-dos boots only its own `.jsdos` bundle, so the platform alone would + // offer Play on files the player panics on. + const onJsDosPlatform = supportedBy(isJsDosEmulationSupported); + const canPlayJsDos = computed( + () => onJsDosPlatform.value && isJsDosBundle(getRom()), + ); + const canPlay = computed( () => canPlayEJS.value || canPlayJsDos.value || canPlayRuffle.value, );