diff --git a/frontend/src/utils/emulatorjsFirmware.test.ts b/frontend/src/utils/emulatorjsFirmware.test.ts index 74cb47fd0..dff52f85b 100644 --- a/frontend/src/utils/emulatorjsFirmware.test.ts +++ b/frontend/src/utils/emulatorjsFirmware.test.ts @@ -1,15 +1,15 @@ import { describe, expect, it } from "vitest"; -import { puaeFirmwareFiles } from "./emulatorjsFirmware"; +import { firmwareExternalFiles } from "./emulatorjsFirmware"; -const firmware = [ - { id: 1, file_name: "kick34005.A500", missing_from_fs: false }, - { id: 2, file_name: "kick40068.A1200", missing_from_fs: false }, - { id: 3, file_name: "gone.rom", missing_from_fs: true }, -]; +const kick13 = { id: 1, file_name: "kick34005.A500", missing_from_fs: false }; +const kick31 = { id: 2, file_name: "kick40068.A1200", missing_from_fs: false }; +const missing = { id: 3, file_name: "gone.rom", missing_from_fs: true }; +const bundle = { id: 4, file_name: "bios.zip", missing_from_fs: false }; +const firmware = [kick13, kick31, missing, bundle]; -describe("puaeFirmwareFiles", () => { - it("places every available Kickstart at its own name in the system directory", () => { - expect(puaeFirmwareFiles("puae", firmware)).toEqual({ +describe("firmwareExternalFiles", () => { + it("places every available Kickstart in the PUAE system directory", () => { + expect(firmwareExternalFiles("puae", firmware, null)).toEqual({ "/home/web_user/retroarch/userdata/system/kick34005.A500": "/api/firmware/1/content/kick34005.A500", "/home/web_user/retroarch/userdata/system/kick40068.A1200": @@ -17,16 +17,34 @@ describe("puaeFirmwareFiles", () => { }); }); - it("does not change firmware loading for another core", () => { - expect(puaeFirmwareFiles("pcsx_rearmed", firmware)).toEqual({}); + it("leaves the selected file to EJS_biosUrl for PUAE", () => { + expect(firmwareExternalFiles("puae", firmware, kick13)).toEqual({ + "/home/web_user/retroarch/userdata/system/kick40068.A1200": + "/api/firmware/2/content/kick40068.A1200", + }); + }); + + it("passes every file next to the game for other cores when none is selected", () => { + expect(firmwareExternalFiles("melonds", firmware, null)).toEqual({ + "/kick34005.A500": "/api/firmware/1/content/kick34005.A500", + "/kick40068.A1200": "/api/firmware/2/content/kick40068.A1200", + }); + }); + + it("passes nothing extra for other cores once a file is selected", () => { + expect(firmwareExternalFiles("pcsx_rearmed", firmware, kick13)).toEqual({}); }); it("ignores unsafe filenames", () => { expect( - puaeFirmwareFiles("puae", [ - { id: 1, file_name: "../other.rom", missing_from_fs: false }, - { id: 2, file_name: "folder\\file.rom", missing_from_fs: false }, - ]), + firmwareExternalFiles( + "puae", + [ + { id: 1, file_name: "../other.rom", missing_from_fs: false }, + { id: 2, file_name: "folder\\file.rom", missing_from_fs: false }, + ], + null, + ), ).toEqual({}); }); }); diff --git a/frontend/src/utils/emulatorjsFirmware.ts b/frontend/src/utils/emulatorjsFirmware.ts index 9ef3855d4..fc6f2ff28 100644 --- a/frontend/src/utils/emulatorjsFirmware.ts +++ b/frontend/src/utils/emulatorjsFirmware.ts @@ -1,28 +1,42 @@ -interface FirmwareFile { - id: number; - file_name: string; - missing_from_fs: boolean; -} +import type { FirmwareSchema } from "@/__generated__"; + +type FirmwareFile = Pick< + FirmwareSchema, + "id" | "file_name" | "missing_from_fs" +>; const SYSTEM_DIRECTORY = "/home/web_user/retroarch/userdata/system"; +// Where EmulatorJS writes EJS_biosUrl, next to the game. +const CONTENT_DIRECTORY = ""; +// Cores that pick among several firmware files by name for each game. +const FILENAME_SELECTING_CORES = new Set(["puae"]); +// EJS_externalFiles writes archives unextracted; they still load through EJS_biosUrl. +const ARCHIVE_EXTENSION = /\.(zip|7z|rar)$/i; -export function puaeFirmwareFiles( +/** Firmware to load beside EJS_biosUrl: always all for filename-selecting cores, otherwise all only when none is selected. */ +export function firmwareExternalFiles( core: string, firmware: readonly FirmwareFile[], + selected: FirmwareFile | null, ): Record { - if (core !== "puae") return {}; + let directory: string; + if (FILENAME_SELECTING_CORES.has(core)) directory = SYSTEM_DIRECTORY; + else if (!selected) directory = CONTENT_DIRECTORY; + else return {}; return Object.fromEntries( firmware .filter( - ({ file_name, missing_from_fs }) => + ({ id, file_name, missing_from_fs }) => + id !== selected?.id && !missing_from_fs && file_name !== "." && file_name !== ".." && - !/[\\/]/.test(file_name), + !/[\\/]/.test(file_name) && + !ARCHIVE_EXTENSION.test(file_name), ) .map(({ id, file_name }) => [ - `${SYSTEM_DIRECTORY}/${file_name}`, + `${directory}/${file_name}`, `/api/firmware/${id}/content/${encodeURIComponent(file_name)}`, ]), ); diff --git a/frontend/src/views/Player/EmulatorJS/Player.vue b/frontend/src/views/Player/EmulatorJS/Player.vue index 20bf1de04..3b247158a 100644 --- a/frontend/src/views/Player/EmulatorJS/Player.vue +++ b/frontend/src/views/Player/EmulatorJS/Player.vue @@ -37,7 +37,7 @@ import { getControlSchemeForPlatform, getDownloadPath, } from "@/utils"; -import { puaeFirmwareFiles } from "@/utils/emulatorjsFirmware"; +import { firmwareExternalFiles } from "@/utils/emulatorjsFirmware"; import { useSnackbar, type SnackbarTone } from "@/v2/composables/useSnackbar"; import { saveSave, @@ -336,7 +336,11 @@ window.EJS_gameUrl = getDownloadPath({ window.EJS_biosUrl = props.bios ? `/api/firmware/${props.bios.id}/content/${props.bios.file_name}` : ""; -window.EJS_externalFiles = puaeFirmwareFiles(window.EJS_core, props.firmware); +window.EJS_externalFiles = firmwareExternalFiles( + window.EJS_core, + props.firmware, + props.bios, +); window.EJS_player = "#game"; window.EJS_color = "#A453FF"; window.EJS_alignStartButton = "center";