fix(v2): remember the core a game was launched with

The v2 player read `player:<rom_id>:core` and `player:<platform_slug>:core` at
mount but never wrote either, so picking a core applied for that session only
and was forgotten on reload. The v1 player writes both on launch, so v2 was
missing that half.

Arcade is where this bites: its core list starts with mame2003, and resolution
falls back to the first entry, so a library that only runs under mame2003_plus
had no way to make the switch stick.

Both directions now go through one module, so the read and the write cannot
drift apart on the key format.

Fixes #4248

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-08-20 09:11:37 -04:00
parent 42e8043372
commit 4eb80a7e95
3 changed files with 104 additions and 9 deletions

View File

@@ -59,6 +59,7 @@ import {
} from "@/v2/utils/playerBezel";
import { resolveStoredDisc } from "@/v2/utils/playerDisc";
import { installIOSFullscreenShim } from "@/views/Player/EmulatorJS/utils";
import { rememberCore, resolveRememberedCore } from "./coreStorage";
// Reuse v1's heavy emulator integration — do NOT rewrite this. Lazy so the
// bundle doesn't pull in the EJS shims until we actually mount the player.
@@ -261,6 +262,9 @@ async function onPlay() {
removeIOSFullscreenShim.value?.();
removeIOSFullscreenShim.value = installIOSFullscreenShim();
if (rom.value) {
rememberCore(rom.value.id, rom.value.platform_slug, selectedCore.value);
}
gameRunning.value = true;
window.EJS_fullscreenOnLoaded = fullscreenOnPlay.value;
fullScreen.value = fullscreenOnPlay.value;
@@ -427,16 +431,11 @@ onMounted(async () => {
}
selectedDisc.value = discId;
// Prefer the core saved for this game, then the platform default, validating
// each candidate so a stale entry falls through instead of masking the next
const gameCore = localStorage.getItem(`player:${rom.value.id}:core`);
const platformCore = localStorage.getItem(
`player:${rom.value.platform_slug}:core`,
selectedCore.value = resolveRememberedCore(
rom.value.id,
rom.value.platform_slug,
supportedCores.value,
);
selectedCore.value =
[gameCore, platformCore].find(
(core): core is string => !!core && supportedCores.value.includes(core),
) ?? supportedCores.value[0];
const coreOptions = configStore.getEJSCoreOptions(selectedCore.value);
const storedBiosID = localStorage.getItem(

View File

@@ -0,0 +1,60 @@
import { beforeEach, describe, expect, it } from "vitest";
import { rememberCore, resolveRememberedCore } from "./coreStorage";
const ARCADE_CORES = ["mame2003", "mame2003_plus", "fbneo"];
const ROM_ID = 7;
const SLUG = "arcade";
describe("coreStorage", () => {
beforeEach(() => {
localStorage.clear();
});
it("falls back to the platform's first core when nothing is remembered", () => {
expect(resolveRememberedCore(ROM_ID, SLUG, ARCADE_CORES)).toBe("mame2003");
});
it("remembers a launch under both the game and the platform", () => {
rememberCore(ROM_ID, SLUG, "mame2003_plus");
expect(localStorage.getItem(`player:${ROM_ID}:core`)).toBe("mame2003_plus");
expect(localStorage.getItem(`player:${SLUG}:core`)).toBe("mame2003_plus");
});
it("applies the platform default to a game never launched before", () => {
rememberCore(ROM_ID, SLUG, "mame2003_plus");
expect(resolveRememberedCore(999, SLUG, ARCADE_CORES)).toBe(
"mame2003_plus",
);
});
it("prefers the game's own core over the platform default", () => {
rememberCore(ROM_ID, SLUG, "mame2003_plus");
localStorage.setItem(`player:${ROM_ID}:core`, "fbneo");
expect(resolveRememberedCore(ROM_ID, SLUG, ARCADE_CORES)).toBe("fbneo");
});
it("skips a remembered core the platform no longer supports", () => {
localStorage.setItem(`player:${ROM_ID}:core`, "mame2010");
localStorage.setItem(`player:${SLUG}:core`, "fbneo");
expect(resolveRememberedCore(ROM_ID, SLUG, ARCADE_CORES)).toBe("fbneo");
});
it("falls back to the first core when every remembered one is stale", () => {
localStorage.setItem(`player:${ROM_ID}:core`, "mame2010");
localStorage.setItem(`player:${SLUG}:core`, "mame2016");
expect(resolveRememberedCore(ROM_ID, SLUG, ARCADE_CORES)).toBe("mame2003");
});
it("forgets both keys when the selection is cleared", () => {
rememberCore(ROM_ID, SLUG, "mame2003_plus");
rememberCore(ROM_ID, SLUG, null);
expect(localStorage.getItem(`player:${ROM_ID}:core`)).toBeNull();
expect(localStorage.getItem(`player:${SLUG}:core`)).toBeNull();
});
});

View File

@@ -0,0 +1,36 @@
// The core a game is played with is remembered twice: under the game, and
// under its platform as the default for every other game on that platform.
const gameKey = (romId: number) => `player:${romId}:core`;
const platformKey = (platformSlug: string) => `player:${platformSlug}:core`;
/**
* The first remembered core the platform still supports, else its first core.
*
* Each candidate is validated so a core that is no longer offered (renamed
* upstream, or gated behind netplay) falls through instead of masking the next.
*/
export function resolveRememberedCore(
romId: number,
platformSlug: string,
supportedCores: readonly string[],
): string {
return (
[
localStorage.getItem(gameKey(romId)),
localStorage.getItem(platformKey(platformSlug)),
].find((core): core is string => !!core && supportedCores.includes(core)) ??
supportedCores[0]
);
}
/** Remember `core` for the game and its platform, or forget both when null. */
export function rememberCore(
romId: number,
platformSlug: string,
core: string | null,
): void {
for (const key of [gameKey(romId), platformKey(platformSlug)]) {
if (core) localStorage.setItem(key, core);
else localStorage.removeItem(key);
}
}