--- a/core/upstream/dolphin_web_core.cpp 2026-11-16 10:40:31 +++ b/core/upstream/dolphin_web_core.cpp 2026-11-16 10:40:31 @@ -1318,10 +1318,15 @@ int SetEmulationSpeed(float factor) { - if (s_runtime_initialized || factor < 0.0f || factor > 5.0f) + if (factor < 0.0f || factor > 5.0f) return 0; s_emulation_speed = factor; + // PlayAgain 0108: a running game follows the change (fast-forward), as it + // follows the desktop's speed setting; the throttle picks it up on the CPU + // thread (CoreTimingManager::RefreshConfig). + if (s_runtime_initialized) + Config::SetBaseOrCurrent(Config::MAIN_EMULATION_SPEED, factor); return 1; } --- a/src/upstream-discio-worker.js 2026-11-16 10:40:31 +++ b/src/upstream-discio-worker.js 2026-11-16 10:40:31 @@ -2350,6 +2350,10 @@ case "importMemcard": case "exportSaveState": case "importSaveState": + // PlayAgain 0108: the running game's own save files in and out, and the speed. + case "paSaveFiles": + case "paPutSaveFiles": + case "paSetSpeed": return handleSavesMessage(type, payload); case "streamCapture": if (!(payload?.writable instanceof WritableStream)) throw new Error("streamCapture needs a WritableStream"); @@ -4864,10 +4868,126 @@ await new Promise((resolve) => setTimeout(resolve, 50)); } return false; +} + +// PlayAgain 0108. A game's own save, out of and into Dolphin's user +// directory: a GameCube game's saves on memory card A as one raw card image +// (what Dolphin itself opens), a Wii title's data folder in the NAND as its +// files, with paths relative to the user directory. +const PA_WII_SAVE_PATH = /^Wii\/title\/0001000[0-9a-f]\/[0-9a-f]{8}\/data\/(?:[^/]+\/)*[^/]+$/; +const PA_SAVE_MAX_BYTES = 64 * 1024 * 1024; + +function paWiiPathOk(path) { + return typeof path === "string" && !path.split("/").includes("..") && PA_WII_SAVE_PATH.test(path); +} + +function paWalk(dir, out) { + const fs = moduleInstance.FS; + let names = []; + try { + names = fs.readdir(dir).filter((name) => name !== "." && name !== ".."); + } catch { + return out; + } + for (const name of names.sort()) { + const path = `${dir}/${name}`; + if (fs.isDir(fs.stat(path).mode)) paWalk(path, out); + else out.push(path); + } + return out; +} + +function paTitleFolders(gameId) { + const hex = [...gameId.slice(0, 4)].map((c) => c.charCodeAt(0).toString(16).padStart(2, "0")).join(""); + let kinds = []; + try { + kinds = moduleInstance.FS.readdir(`${USER_DATA_ROOT}/Wii/title`).filter((name) => /^0001000[0-9a-f]$/.test(name)); + } catch { + return []; + } + return kinds.map((kind) => `${USER_DATA_ROOT}/Wii/title/${kind}/${hex}/data`); +} + +// The card folder a save belongs in, from its game code's region letter (as Dolphin picks a disc's region). +function paCardFolderOf(gameCode) { + const letter = gameCode[3]; + const region = letter === "E" ? "NTSC-U" : letter === "J" || letter === "K" || letter === "W" ? "NTSC-J" : "PAL"; + return gciFolderPath(USER_DATA_ROOT, region, "A"); +} + +function paSaveFiles() { + const identity = saveIdentity(); + if (!coreStopping) api?.flushMemoryCards?.(); + const fs = moduleInstance.FS; + const transfer = []; + const code = identity.gameId.slice(0, 4); + const folder = cardFolder(identity, "A"); + const gcis = listCardSaves(folder) + .filter((save) => !save.error && save.gameCode === code) + .map((save) => fs.readFile(`${folder}/${save.fileName}`)); + let card = null; + if (gcis.length > 0) { + card = transferable(buildRawCard(gcis)).buffer; + transfer.push(card); + } + const files = []; + let total = 0; + for (const dir of paTitleFolders(identity.gameId)) { + for (const full of paWalk(dir, [])) { + const path = full.slice(USER_DATA_ROOT.length + 1); + if (!paWiiPathOk(path)) continue; + const bytes = fs.readFile(full); + total += bytes.byteLength; + if (total > PA_SAVE_MAX_BYTES) throw new Error("paSaveFiles: the game's save is larger than 64 MB"); + const copy = transferable(bytes).buffer; + files.push({ path, bytes: copy }); + transfer.push(copy); + } + } + return { gameId: identity.gameId, region: identity.region, card, files, transfer }; } +// Before the boot only: Dolphin reads the card folder and the NAND when the game starts. +async function paPutSaveFiles(payload) { + if (coreBoot.accepted) throw new Error("paPutSaveFiles: a game is running"); + const files = Array.isArray(payload?.files) ? payload.files : []; + let total = 0; + for (const file of files) { + if (!paWiiPathOk(file?.path)) throw new Error(`paPutSaveFiles: ${String(file?.path)} is not a save path`); + total += file.bytes?.byteLength ?? 0; + } + if (total > PA_SAVE_MAX_BYTES) throw new Error("paPutSaveFiles: more than 64 MB"); + // Everything is read and checked before anything is written. + const saves = payload?.card ? parseRawCard(importBytes({ bytes: payload.card }, MAX_CARD_BYTES, "paPutSaveFiles")).saves : []; + for (const save of saves) { + const folder = paCardFolderOf(save.gameCode); + const same = listCardSaves(folder).find((held) => held.identity === memcardSaveIdentity(save)); + mkdirTreeFs(folder); + moduleInstance.FS.writeFile(`${folder}/${same ? same.fileName : gciFileName(save)}`, save.bytes); + } + for (const file of files) { + const full = `${USER_DATA_ROOT}/${file.path}`; + mkdirTreeFs(full.slice(0, full.lastIndexOf("/"))); + moduleInstance.FS.writeFile(full, new Uint8Array(file.bytes)); + } + return { cardSaves: saves.length, files: files.length, persisted: await flushUserDataReported() }; +} + async function handleSavesMessage(type, payload) { switch (type) { + case "paSaveFiles": + // `tree` (harness): every file under the NAND too, to see where a title keeps its save. + return payload?.tree + ? { ...paSaveFiles(), tree: paWalk(`${USER_DATA_ROOT}/Wii`, []).map((path) => path.slice(USER_DATA_ROOT.length + 1)) } + : paSaveFiles(); + case "paPutSaveFiles": + return paPutSaveFiles(payload); + case "paSetSpeed": { + const factor = Math.max(0.1, Math.min(4, Number(payload?.factor) || 1)); + if (!api?.setEmulationSpeed) return { supported: false }; + api.setEmulationSpeed(factor); + return { supported: true, factor }; + } case "flushUserData": return flushUserData(); case "listUserData": {