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) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-07-30 21:43:51 -04:00
parent 20e416e752
commit 702aa00a2d
4 changed files with 6 additions and 95 deletions

View File

@@ -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 <Player>. The tab owns both the
// <AssetPreview> ("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<AssetTab>(() =>
isSavesTabSelected.value ? "save" : "state",
);
@@ -793,7 +786,6 @@ const selectedAsset = computed<SaveSchema | StateSchema | null>(() =>
:rom="rom"
:state="selectedState"
:save="selectedSave"
:resume-from="activeAssetTab"
:bios="selectedFirmware"
:core="selectedCore"
:disc="selectedDisc"

View File

@@ -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<DetailedRom>(props.rom);
const saveRef = ref<SaveSchema | null>(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);
}
}

View File

@@ -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();
});
});

View File

@@ -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;