From 0a57ceeddfe579eecfbd85a09ae1676438cd0517 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Sun, 26 Jan 2025 23:58:58 -0500 Subject: [PATCH] [ROMM-911] Fix playing multi disc games in emujs --- backend/endpoints/rom.py | 73 ++++++++++++++++--- frontend/src/locales/de_DE/common.json | 1 + frontend/src/locales/en_GB/common.json | 1 + frontend/src/locales/en_US/common.json | 1 + frontend/src/locales/es_ES/common.json | 1 + frontend/src/locales/fr_FR/common.json | 3 +- frontend/src/locales/ko_KR/common.json | 1 + frontend/src/locales/pt_BR/common.json | 1 + frontend/src/locales/ru_RU/common.json | 3 +- frontend/src/locales/zh_CN/common.json | 1 + frontend/src/utils/index.ts | 4 +- frontend/src/views/Player/EmulatorJS/Base.vue | 27 ++++++- .../src/views/Player/EmulatorJS/Player.vue | 13 +++- frontend/src/views/Player/RuffleRS/Base.vue | 10 +-- 14 files changed, 117 insertions(+), 23 deletions(-) diff --git a/backend/endpoints/rom.py b/backend/endpoints/rom.py index 3ebbb73ec..716952f25 100644 --- a/backend/endpoints/rom.py +++ b/backend/endpoints/rom.py @@ -194,7 +194,6 @@ async def head_rom_content( request: Request, id: int, file_name: str, - file_ids: list[int] | None = None, ): """Head rom content endpoint @@ -213,13 +212,14 @@ async def head_rom_content( if not rom: raise RomNotFoundInDatabaseException(id) - file_ids = file_ids or [] + file_ids = request.query_params.get("file_ids") or "" + file_ids = [int(f) for f in file_ids.split(",") if f] files = db_rom_handler.get_rom_files(rom.id) files = [f for f in files if f.id in file_ids or not file_ids] - if not rom.multi: - # Serve the file directly in development mode for emulatorjs - if DEV_MODE: + # Serve the file directly in development mode for emulatorjs + if DEV_MODE: + if not rom.multi: rom_path = f"{LIBRARY_BASE_PATH}/{rom.full_path}" return FileResponse( path=rom_path, @@ -231,6 +231,28 @@ async def head_rom_content( }, ) + if len(files) == 1: + file = files[0] + rom_path = f"{LIBRARY_BASE_PATH}/{file.full_path}" + return FileResponse( + path=rom_path, + filename=file.file_name, + headers={ + "Content-Disposition": f'attachment; filename="{quote(file.file_name)}"', + "Content-Type": "application/octet-stream", + "Content-Length": str(file.file_size_bytes), + }, + ) + + return Response( + headers={ + "Content-Type": "application/zip", + "Content-Disposition": f'attachment; filename="{quote(file_name)}.zip"', + }, + ) + + # Otherwise proxy through nginx + if not rom.multi: return FileRedirectResponse( download_path=Path(f"/library/{rom.full_path}"), filename=rom.fs_name, @@ -258,7 +280,6 @@ async def get_rom_content( request: Request, id: int, file_name: str, - file_ids: list[int] | None = None, ): """Download rom endpoint (one single file or multiple zipped files for multi-part roms) @@ -266,7 +287,6 @@ async def get_rom_content( request (Request): Fastapi Request object id (int): Rom internal id file_name: Zip file output name - file_ids (list[int]): List of file ids to download for multi-part roms Returns: FileResponse: Returns one file for single file roms @@ -281,15 +301,16 @@ async def get_rom_content( if not rom: raise RomNotFoundInDatabaseException(id) - file_ids = file_ids or [] + file_ids = request.query_params.get("file_ids") or "" + file_ids = [int(f) for f in file_ids.split(",") if f] files = db_rom_handler.get_rom_files(rom.id) files = [f for f in files if f.id in file_ids or not file_ids] log.info(f"User {current_username} is downloading {rom.fs_name}") - if not rom.multi: - # Serve the file directly in development mode for emulatorjs - if DEV_MODE: + # Serve the file directly in development mode for emulatorjs + if DEV_MODE: + if not rom.multi: rom_path = f"{LIBRARY_BASE_PATH}/{rom.full_path}" return FileResponse( path=rom_path, @@ -301,6 +322,36 @@ async def get_rom_content( }, ) + if len(files) == 1: + file = files[0] + rom_path = f"{LIBRARY_BASE_PATH}/{file.full_path}" + return FileResponse( + path=rom_path, + filename=file.file_name, + headers={ + "Content-Disposition": f'attachment; filename="{quote(file.file_name)}"', + "Content-Type": "application/octet-stream", + "Content-Length": str(file.file_size_bytes), + }, + ) + + content_lines = [ + ZipContentLine( + crc32=f.crc_hash, + size_bytes=(await Path(LIBRARY_BASE_PATH, f.full_path).stat()).st_size, + encoded_location=quote(f"{LIBRARY_BASE_PATH}/{f.full_path}"), + filename=f.full_path.replace(rom.full_path, ""), + ) + for f in files + ] + + return ZipResponse( + content_lines=content_lines, + filename=f"{quote(file_name)}.zip", + ) + + # Otherwise proxy through nginx + if not rom.multi: return FileRedirectResponse( download_path=Path(f"/library/{rom.full_path}"), filename=rom.fs_name, diff --git a/frontend/src/locales/de_DE/common.json b/frontend/src/locales/de_DE/common.json index 7c14525e2..e7895723d 100644 --- a/frontend/src/locales/de_DE/common.json +++ b/frontend/src/locales/de_DE/common.json @@ -3,6 +3,7 @@ "platforms": "Plattformen", "platforms-n": "{n} Plattform | {n} Plattformen", "firmware": "Firmware", + "core": "Core", "games-n": "{n} Spiel | {n} Spiele", "collection": "Sammlung", "collections": "Sammlungen", diff --git a/frontend/src/locales/en_GB/common.json b/frontend/src/locales/en_GB/common.json index 441871e9c..14ce1775d 100644 --- a/frontend/src/locales/en_GB/common.json +++ b/frontend/src/locales/en_GB/common.json @@ -3,6 +3,7 @@ "platforms": "Platforms", "platforms-n": "{n} Platform | {n} Platforms", "firmware": "Firmware", + "core": "Core", "games-n": "{n} Game | {n} Games", "collection": "Collection", "collections": "Collections", diff --git a/frontend/src/locales/en_US/common.json b/frontend/src/locales/en_US/common.json index 441871e9c..14ce1775d 100644 --- a/frontend/src/locales/en_US/common.json +++ b/frontend/src/locales/en_US/common.json @@ -3,6 +3,7 @@ "platforms": "Platforms", "platforms-n": "{n} Platform | {n} Platforms", "firmware": "Firmware", + "core": "Core", "games-n": "{n} Game | {n} Games", "collection": "Collection", "collections": "Collections", diff --git a/frontend/src/locales/es_ES/common.json b/frontend/src/locales/es_ES/common.json index 0084f3151..4a1e12b32 100644 --- a/frontend/src/locales/es_ES/common.json +++ b/frontend/src/locales/es_ES/common.json @@ -3,6 +3,7 @@ "platforms": "Plataformas", "platforms-n": "{n} Plataforma | {n} Plataformas", "firmware": "Firmware", + "core": "Núcleo", "games-n": "{n} Juego | {n} Juegos", "collection": "Colección", "collections": "Colecciones", diff --git a/frontend/src/locales/fr_FR/common.json b/frontend/src/locales/fr_FR/common.json index ba73f1302..5b18020bb 100644 --- a/frontend/src/locales/fr_FR/common.json +++ b/frontend/src/locales/fr_FR/common.json @@ -2,7 +2,8 @@ "platform": "Plateforme", "platforms": "Plateformes", "platforms-n": "{n} Plateforme | {n} Plateformes", - "firmware": "Firmware", + "firmware": "Micrologiciel", + "core": "Noyau", "games-n": "{n} Jeu | {n} Jeux", "collection": "Collection", "collections": "Collections", diff --git a/frontend/src/locales/ko_KR/common.json b/frontend/src/locales/ko_KR/common.json index 3615e55fd..afe905358 100644 --- a/frontend/src/locales/ko_KR/common.json +++ b/frontend/src/locales/ko_KR/common.json @@ -3,6 +3,7 @@ "platforms": "플랫폼", "platforms-n": "{n}가지 플랫폼 | {n}가지 플랫폼", "firmware": "펌웨어", + "core": "코어", "games-n": "게임 {n}개 | 게임 {n}개", "collection": "모음집", "collections": "모음집", diff --git a/frontend/src/locales/pt_BR/common.json b/frontend/src/locales/pt_BR/common.json index 485a99ff5..d2f9e8b9a 100644 --- a/frontend/src/locales/pt_BR/common.json +++ b/frontend/src/locales/pt_BR/common.json @@ -3,6 +3,7 @@ "platforms": "Plataformas", "platforms-n": "{n} Plataforma | {n} Plataformas", "firmware": "Firmware", + "core": "Núcleo", "games-n": "{n} Jogo | {n} Jogos", "collection": "Coleção", "collections": "Coleções", diff --git a/frontend/src/locales/ru_RU/common.json b/frontend/src/locales/ru_RU/common.json index 6deae2bd1..d627e4f76 100644 --- a/frontend/src/locales/ru_RU/common.json +++ b/frontend/src/locales/ru_RU/common.json @@ -2,7 +2,8 @@ "platform": "Платформа", "platforms": "Платформы", "platforms-n": "{n} Платформа | {n} Платформы", - "firmware": "Firmware", + "firmware": "Прошивка", + "core": "Ядро", "games-n": "{n} Игра | {n} Игры", "collection": "Коллекция", "collections": "Коллекции", diff --git a/frontend/src/locales/zh_CN/common.json b/frontend/src/locales/zh_CN/common.json index 26b1c2559..15092a73a 100644 --- a/frontend/src/locales/zh_CN/common.json +++ b/frontend/src/locales/zh_CN/common.json @@ -3,6 +3,7 @@ "platforms": "平台", "platforms-n": "{n} 平台 | {n} 平台", "firmware": "固件", + "core": "核心", "games-n": "{n} 游戏 | {n} 游戏", "collection": "收藏", "collections": "收藏", diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index be0c4f48a..77994ddab 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -95,7 +95,9 @@ export function getDownloadPath({ }) { const queryParams = new URLSearchParams(); if (fileIDs.length > 0) { - fileIDs.forEach((fileId) => queryParams.append("files", fileId.toString())); + fileIDs.forEach((fileId) => + queryParams.append("file_ids", fileId.toString()), + ); } return `/api/roms/${rom.id}/content/${rom.fs_name}?${queryParams.toString()}`; } diff --git a/frontend/src/views/Player/EmulatorJS/Base.vue b/frontend/src/views/Player/EmulatorJS/Base.vue index c209f10fa..79dc027cb 100644 --- a/frontend/src/views/Player/EmulatorJS/Base.vue +++ b/frontend/src/views/Player/EmulatorJS/Base.vue @@ -4,7 +4,6 @@ import RomListItem from "@/components/common/Game/ListItem.vue"; import firmwareApi from "@/services/api/firmware"; import romApi from "@/services/api/rom"; import storeGalleryView from "@/stores/galleryView"; -import storeHeartbeat from "@/stores/heartbeat"; import type { DetailedRom } from "@/stores/roms"; import { formatBytes, formatTimestamp, getSupportedEJSCores } from "@/utils"; import Player from "@/views/Player/EmulatorJS/Player.vue"; @@ -21,13 +20,13 @@ const { t } = useI18n(); const route = useRoute(); const galleryViewStore = storeGalleryView(); const { defaultAspectRatioScreenshot } = storeToRefs(galleryViewStore); -const heartbeat = storeHeartbeat(); const rom = ref(null); const firmwareOptions = ref([]); const biosRef = ref(null); const saveRef = ref(null); const stateRef = ref(null); const coreRef = ref(null); +const discRef = ref(null); const supportedCores = ref([]); const gameRunning = ref(false); const storedFSOP = localStorage.getItem("fullScreenOnPlay"); @@ -117,6 +116,11 @@ onMounted(async () => { // Otherwise auto select first supported core coreRef.value = supportedCores.value[0]; } + + const storedDisc = localStorage.getItem(`player:${rom.value.id}:disc`); + if (storedDisc) { + discRef.value = parseInt(storedDisc); + } }); @@ -138,6 +142,7 @@ onMounted(async () => { :save="saveRef" :bios="biosRef" :core="coreRef" + :disc="discRef" /> @@ -157,6 +162,22 @@ onMounted(async () => { + { hide-details variant="outlined" clearable - label="Core" + :label="t('common.core')" :items=" supportedCores.map((c) => ({ title: c, diff --git a/frontend/src/views/Player/EmulatorJS/Player.vue b/frontend/src/views/Player/EmulatorJS/Player.vue index 0f60de963..a67681bb3 100644 --- a/frontend/src/views/Player/EmulatorJS/Player.vue +++ b/frontend/src/views/Player/EmulatorJS/Player.vue @@ -8,6 +8,7 @@ import { areThreadsRequiredForEJSCore, getSupportedEJSCores, getControlSchemeForPlatform, + getDownloadPath, } from "@/utils"; import { onBeforeUnmount, onMounted, ref } from "vue"; @@ -17,6 +18,7 @@ const props = defineProps<{ state: StateSchema | null; bios: FirmwareSchema | null; core: string | null; + disc: number | null; }>(); const romRef = ref(props.rom); const saveRef = ref(props.save); @@ -62,7 +64,10 @@ window.EJS_controlScheme = getControlSchemeForPlatform( ); window.EJS_threads = areThreadsRequiredForEJSCore(window.EJS_core); window.EJS_gameID = romRef.value.id; -window.EJS_gameUrl = `/api/roms/${romRef.value.id}/content/${romRef.value.fs_name}`; +window.EJS_gameUrl = getDownloadPath({ + rom: romRef.value, + fileIDs: props.disc ? [props.disc] : [], +}); window.EJS_biosUrl = props.bios ? `/api/firmware/${props.bios.id}/content/${props.bios.file_name}` : ""; @@ -114,6 +119,12 @@ onMounted(() => { } else { localStorage.removeItem(`player:${props.rom.platform_slug}:core`); } + + if (props.disc) { + localStorage.setItem(`player:${props.rom.id}:disc`, props.disc.toString()); + } else { + localStorage.removeItem(`player:${props.rom.id}:disc`); + } }); function buildStateName(): string { diff --git a/frontend/src/views/Player/RuffleRS/Base.vue b/frontend/src/views/Player/RuffleRS/Base.vue index 6c29fb6b6..1916e86ec 100644 --- a/frontend/src/views/Player/RuffleRS/Base.vue +++ b/frontend/src/views/Player/RuffleRS/Base.vue @@ -1,7 +1,6 @@