From 20e416e752e89cbbf010f12a0ce0dadd1c934e77 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Thu, 30 Jul 2026 21:03:54 -0400 Subject: [PATCH 1/2] fix(player): resume from the picked asset instead of always the state The v2 player armed a save and a state independently and handed both to , which applied the save and then the state. A savestate restores the whole machine (SRAM included), so the save was always discarded when any compatible state existed. Split the two roles the `save` prop was serving: it stays the write-back target for "Save & Quit", while a new `resumeFrom` prop says which asset actually boots. The v2 shell passes the visible tab, so the AssetPreview ("Resume from save/state") and what loads can no longer disagree. v1 keeps save and state mutually exclusive, so it leaves `resumeFrom` unset and falls back to the previous precedence. Fixes #4010 Co-Authored-By: Claude Opus 5 (1M context) --- frontend/src/v2/views/Player/EmulatorJS.vue | 8 +++ .../src/views/Player/EmulatorJS/Player.vue | 16 +++++- .../src/views/Player/EmulatorJS/utils.test.ts | 51 ++++++++++++++++++- frontend/src/views/Player/EmulatorJS/utils.ts | 23 +++++++++ 4 files changed, 95 insertions(+), 3 deletions(-) diff --git a/frontend/src/v2/views/Player/EmulatorJS.vue b/frontend/src/v2/views/Player/EmulatorJS.vue index 2b0d37ba6..86dac08d5 100644 --- a/frontend/src/v2/views/Player/EmulatorJS.vue +++ b/frontend/src/v2/views/Player/EmulatorJS.vue @@ -403,6 +403,10 @@ onMounted(async () => { // there are multiple saves, since loading the state injects a different // SRAM timeline that would overwrite an arbitrary save the user never // picked. In that case the user must select the save slot explicitly. + // + // Binding a save is not the same as resuming from it. The visible tab + // decides that (see `activeAssetTab`), so opening on States never means + // the bound save is the one that boots. const initiallyCompatibleStates = rom.value.user_states.filter( (s) => !s.emulator || s.emulator === supportedCores.value[0], ); @@ -539,6 +543,9 @@ const platformLabel = computed( ); type AssetTab = "save" | "state"; +// Doubles as the resume source handed to . The tab owns both the +// ("Resume from save/state") and what actually boots, so the +// preview can never promise a save while an armed state loads over it (#4010). const activeAssetTab = computed(() => isSavesTabSelected.value ? "save" : "state", ); @@ -786,6 +793,7 @@ const selectedAsset = computed(() => :rom="rom" :state="selectedState" :save="selectedSave" + :resume-from="activeAssetTab" :bios="selectedFirmware" :core="selectedCore" :disc="selectedDisc" diff --git a/frontend/src/views/Player/EmulatorJS/Player.vue b/frontend/src/views/Player/EmulatorJS/Player.vue index be8e20df8..d41caf1d6 100644 --- a/frontend/src/views/Player/EmulatorJS/Player.vue +++ b/frontend/src/views/Player/EmulatorJS/Player.vue @@ -34,6 +34,8 @@ import { createQuickLoadButton, createSaveQuitButton, createExitEmulationButton, + resolveResumeSource, + type ResumeSource, } from "./utils"; const INVALID_CHARS_REGEX = /[#<$+%>!`&*'|{}/\\?"=@:^\r\n]/gi; @@ -47,11 +49,15 @@ const router = useRouter(); const props = defineProps<{ rom: DetailedRom; + // The bound save slot: the write-back target "Save & Quit" PUTs into. It is + // only applied at boot when `resumeFrom` says so, so a save can stay bound + // while the session resumes from a state. save: SaveSchema | null; state: StateSchema | null; bios: FirmwareSchema | null; core: string | null; disc: number | null; + resumeFrom?: ResumeSource | null; }>(); const romRef = ref(props.rom); const saveRef = ref(props.save); @@ -432,8 +438,14 @@ window.EJS_onGameStart = async () => { if (!ready) { console.warn("Game manager not ready for save/state injection"); } else { - if (props.save) await loadSave(props.save); - if (props.state) { + const resumeSource = resolveResumeSource({ + resumeFrom: props.resumeFrom, + hasSave: !!props.save, + hasState: !!props.state, + }); + if (resumeSource === "save" && props.save) { + await loadSave(props.save); + } else if (resumeSource === "state" && props.state) { await new Promise((resolve) => setTimeout(resolve, STATE_APPLY_SETTLE_MS), ); diff --git a/frontend/src/views/Player/EmulatorJS/utils.test.ts b/frontend/src/views/Player/EmulatorJS/utils.test.ts index 04d4210bb..a81665cef 100644 --- a/frontend/src/views/Player/EmulatorJS/utils.test.ts +++ b/frontend/src/views/Player/EmulatorJS/utils.test.ts @@ -1,5 +1,5 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest"; -import { installEJSDefaultOptionsTrap } from "./utils"; +import { installEJSDefaultOptionsTrap, resolveResumeSource } from "./utils"; const STORAGE_KEY = "ejs-7-n64-Test Game-settings"; @@ -133,3 +133,52 @@ describe("installEJSDefaultOptionsTrap", () => { expect(emulator.preGetSetting).toBe(patched); }); }); + +describe("resolveResumeSource", () => { + it("honours an explicit save choice while a state is armed", () => { + expect( + resolveResumeSource({ + resumeFrom: "save", + hasSave: true, + hasState: true, + }), + ).toBe("save"); + }); + + it("honours an explicit state choice while a save is bound", () => { + expect( + resolveResumeSource({ + resumeFrom: "state", + hasSave: true, + hasState: true, + }), + ).toBe("state"); + }); + + it("boots fresh when the chosen side has nothing selected", () => { + expect( + resolveResumeSource({ + resumeFrom: "save", + hasSave: false, + hasState: true, + }), + ).toBeNull(); + expect( + resolveResumeSource({ + resumeFrom: "state", + hasSave: true, + hasState: false, + }), + ).toBeNull(); + }); + + it("prefers the state when no choice is given", () => { + expect(resolveResumeSource({ hasSave: true, hasState: true })).toBe( + "state", + ); + expect(resolveResumeSource({ hasSave: true, hasState: false })).toBe( + "save", + ); + expect(resolveResumeSource({ hasSave: false, hasState: false })).toBeNull(); + }); +}); diff --git a/frontend/src/views/Player/EmulatorJS/utils.ts b/frontend/src/views/Player/EmulatorJS/utils.ts index 877386382..f1e98167b 100644 --- a/frontend/src/views/Player/EmulatorJS/utils.ts +++ b/frontend/src/views/Player/EmulatorJS/utils.ts @@ -154,6 +154,29 @@ export function loadEmulatorJSState(state: Uint8Array) { window.EJS_emulator.gameManager.loadState(state); } +export type ResumeSource = "save" | "state"; + +/** + * Which asset the emulator applies at boot. A state restores the whole + * machine (SRAM included), so applying both would silently discard the save. + * Callers that keep a save bound as the write-back target while resuming from + * a state pass `resumeFrom` to say which one they actually mean; without it, + * a state wins (v1 keeps the two mutually exclusive, so only one is ever set). + */ +export function resolveResumeSource({ + resumeFrom, + hasSave, + hasState, +}: { + resumeFrom?: ResumeSource | null; + hasSave: boolean; + hasState: boolean; +}): ResumeSource | null { + const source = resumeFrom ?? (hasState ? "state" : "save"); + if (source === "state") return hasState ? "state" : null; + return hasSave ? "save" : null; +} + export function invalidateEmulatorJSRomCacheIfRenamed(rom: { id: number; fs_name: string; From 702aa00a2d85e91ce54504af4f7016c2aad9332d Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Thu, 30 Jul 2026 21:43:51 -0400 Subject: [PATCH 2/2] fix(player): drop the resumeFrom prop and let the state win Keep the save prop as the write-back target for "Save & Quit" without adding a second prop to say which asset boots: when both a save and a state are selected, the session resumes from the state. The save is no longer loaded right before a state overwrites it, so the outcome is the same but without the discarded SRAM injection. Co-Authored-By: Claude Opus 5 (1M context) --- frontend/src/v2/views/Player/EmulatorJS.vue | 8 --- .../src/views/Player/EmulatorJS/Player.vue | 19 ++----- .../src/views/Player/EmulatorJS/utils.test.ts | 51 +------------------ frontend/src/views/Player/EmulatorJS/utils.ts | 23 --------- 4 files changed, 6 insertions(+), 95 deletions(-) diff --git a/frontend/src/v2/views/Player/EmulatorJS.vue b/frontend/src/v2/views/Player/EmulatorJS.vue index 86dac08d5..2b0d37ba6 100644 --- a/frontend/src/v2/views/Player/EmulatorJS.vue +++ b/frontend/src/v2/views/Player/EmulatorJS.vue @@ -403,10 +403,6 @@ onMounted(async () => { // there are multiple saves, since loading the state injects a different // SRAM timeline that would overwrite an arbitrary save the user never // picked. In that case the user must select the save slot explicitly. - // - // Binding a save is not the same as resuming from it. The visible tab - // decides that (see `activeAssetTab`), so opening on States never means - // the bound save is the one that boots. const initiallyCompatibleStates = rom.value.user_states.filter( (s) => !s.emulator || s.emulator === supportedCores.value[0], ); @@ -543,9 +539,6 @@ const platformLabel = computed( ); type AssetTab = "save" | "state"; -// Doubles as the resume source handed to . The tab owns both the -// ("Resume from save/state") and what actually boots, so the -// preview can never promise a save while an armed state loads over it (#4010). const activeAssetTab = computed(() => isSavesTabSelected.value ? "save" : "state", ); @@ -793,7 +786,6 @@ const selectedAsset = computed(() => :rom="rom" :state="selectedState" :save="selectedSave" - :resume-from="activeAssetTab" :bios="selectedFirmware" :core="selectedCore" :disc="selectedDisc" diff --git a/frontend/src/views/Player/EmulatorJS/Player.vue b/frontend/src/views/Player/EmulatorJS/Player.vue index d41caf1d6..ac6bd2feb 100644 --- a/frontend/src/views/Player/EmulatorJS/Player.vue +++ b/frontend/src/views/Player/EmulatorJS/Player.vue @@ -34,8 +34,6 @@ import { createQuickLoadButton, createSaveQuitButton, createExitEmulationButton, - resolveResumeSource, - type ResumeSource, } from "./utils"; const INVALID_CHARS_REGEX = /[#<$+%>!`&*'|{}/\\?"=@:^\r\n]/gi; @@ -49,15 +47,11 @@ const router = useRouter(); const props = defineProps<{ rom: DetailedRom; - // The bound save slot: the write-back target "Save & Quit" PUTs into. It is - // only applied at boot when `resumeFrom` says so, so a save can stay bound - // while the session resumes from a state. save: SaveSchema | null; state: StateSchema | null; bios: FirmwareSchema | null; core: string | null; disc: number | null; - resumeFrom?: ResumeSource | null; }>(); const romRef = ref(props.rom); const saveRef = ref(props.save); @@ -438,18 +432,15 @@ window.EJS_onGameStart = async () => { if (!ready) { console.warn("Game manager not ready for save/state injection"); } else { - const resumeSource = resolveResumeSource({ - resumeFrom: props.resumeFrom, - hasSave: !!props.save, - hasState: !!props.state, - }); - if (resumeSource === "save" && props.save) { - await loadSave(props.save); - } else if (resumeSource === "state" && props.state) { + // A state restores the whole machine, SRAM included, so a save applied + // alongside it would be discarded: the state wins when both are set. + if (props.state) { await new Promise((resolve) => setTimeout(resolve, STATE_APPLY_SETTLE_MS), ); await loadState(props.state); + } else if (props.save) { + await loadSave(props.save); } } diff --git a/frontend/src/views/Player/EmulatorJS/utils.test.ts b/frontend/src/views/Player/EmulatorJS/utils.test.ts index a81665cef..04d4210bb 100644 --- a/frontend/src/views/Player/EmulatorJS/utils.test.ts +++ b/frontend/src/views/Player/EmulatorJS/utils.test.ts @@ -1,5 +1,5 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest"; -import { installEJSDefaultOptionsTrap, resolveResumeSource } from "./utils"; +import { installEJSDefaultOptionsTrap } from "./utils"; const STORAGE_KEY = "ejs-7-n64-Test Game-settings"; @@ -133,52 +133,3 @@ describe("installEJSDefaultOptionsTrap", () => { expect(emulator.preGetSetting).toBe(patched); }); }); - -describe("resolveResumeSource", () => { - it("honours an explicit save choice while a state is armed", () => { - expect( - resolveResumeSource({ - resumeFrom: "save", - hasSave: true, - hasState: true, - }), - ).toBe("save"); - }); - - it("honours an explicit state choice while a save is bound", () => { - expect( - resolveResumeSource({ - resumeFrom: "state", - hasSave: true, - hasState: true, - }), - ).toBe("state"); - }); - - it("boots fresh when the chosen side has nothing selected", () => { - expect( - resolveResumeSource({ - resumeFrom: "save", - hasSave: false, - hasState: true, - }), - ).toBeNull(); - expect( - resolveResumeSource({ - resumeFrom: "state", - hasSave: true, - hasState: false, - }), - ).toBeNull(); - }); - - it("prefers the state when no choice is given", () => { - expect(resolveResumeSource({ hasSave: true, hasState: true })).toBe( - "state", - ); - expect(resolveResumeSource({ hasSave: true, hasState: false })).toBe( - "save", - ); - expect(resolveResumeSource({ hasSave: false, hasState: false })).toBeNull(); - }); -}); diff --git a/frontend/src/views/Player/EmulatorJS/utils.ts b/frontend/src/views/Player/EmulatorJS/utils.ts index f1e98167b..877386382 100644 --- a/frontend/src/views/Player/EmulatorJS/utils.ts +++ b/frontend/src/views/Player/EmulatorJS/utils.ts @@ -154,29 +154,6 @@ export function loadEmulatorJSState(state: Uint8Array) { window.EJS_emulator.gameManager.loadState(state); } -export type ResumeSource = "save" | "state"; - -/** - * Which asset the emulator applies at boot. A state restores the whole - * machine (SRAM included), so applying both would silently discard the save. - * Callers that keep a save bound as the write-back target while resuming from - * a state pass `resumeFrom` to say which one they actually mean; without it, - * a state wins (v1 keeps the two mutually exclusive, so only one is ever set). - */ -export function resolveResumeSource({ - resumeFrom, - hasSave, - hasState, -}: { - resumeFrom?: ResumeSource | null; - hasSave: boolean; - hasState: boolean; -}): ResumeSource | null { - const source = resumeFrom ?? (hasState ? "state" : "save"); - if (source === "state") return hasState ? "state" : null; - return hasSave ? "save" : null; -} - export function invalidateEmulatorJSRomCacheIfRenamed(rom: { id: number; fs_name: string;