mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 02:24:09 +01:00
Merge pull request #4452 from rommapp/feat/emulatorjs-default-cores
feat(emulatorjs): set a default core per platform in config.yml
This commit is contained in:
@@ -476,6 +476,7 @@ class Config:
|
||||
EJS_ENABLE_AUTO_SAVE_SYNC: bool
|
||||
EJS_NETPLAY_ENABLED: bool
|
||||
EJS_NETPLAY_ICE_SERVERS: list[NetplayICEServer]
|
||||
EJS_DEFAULT_CORES: dict[str, str] # platform_slug -> core_name
|
||||
EJS_SETTINGS: dict[str, EjsOption] # core_name -> EjsOption
|
||||
EJS_CONTROLS: dict[str, EjsControls] # core_name -> EjsControls
|
||||
SCAN_METADATA_PRIORITY: list[str]
|
||||
@@ -763,6 +764,10 @@ class ConfigManager:
|
||||
EJS_NETPLAY_ICE_SERVERS=pydash.get(
|
||||
self._raw_config, "emulatorjs.netplay.ice_servers", []
|
||||
),
|
||||
EJS_DEFAULT_CORES=pydash.get(
|
||||
self._raw_config, "emulatorjs.default_cores", {}
|
||||
)
|
||||
or {},
|
||||
EJS_SETTINGS=pydash.get(self._raw_config, "emulatorjs.settings", {}),
|
||||
EJS_CONTROLS=self._get_ejs_controls(),
|
||||
SCAN_METADATA_PRIORITY=pydash.get(
|
||||
@@ -895,22 +900,22 @@ class ConfigManager:
|
||||
return yaml_controls
|
||||
|
||||
def _validated_platform_map(self, raw: Any, config_key: str) -> dict[str, str]:
|
||||
"""Check a folder name to slug mapping.
|
||||
"""Check a mapping of platform or folder names to non-empty strings.
|
||||
|
||||
Folder names are lowercased so lookups can ignore case.
|
||||
Keys are lowercased so lookups can ignore case.
|
||||
"""
|
||||
if not isinstance(raw, dict):
|
||||
log.critical(f"Invalid config.yml: {config_key} must be a dictionary")
|
||||
sys.exit(3)
|
||||
|
||||
normalized: dict[str, str] = {}
|
||||
for fs_slug, slug in raw.items():
|
||||
if not isinstance(slug, str) or not slug:
|
||||
for key, value in raw.items():
|
||||
if not isinstance(value, str) or not value:
|
||||
log.critical(
|
||||
f"Invalid config.yml: {config_key}.{fs_slug} must be a non-empty string"
|
||||
f"Invalid config.yml: {config_key}.{key} must be a non-empty string"
|
||||
)
|
||||
sys.exit(3)
|
||||
normalized[str(fs_slug).lower()] = slug
|
||||
normalized[str(key).lower()] = value
|
||||
|
||||
return normalized
|
||||
|
||||
@@ -1064,6 +1069,10 @@ class ConfigManager:
|
||||
)
|
||||
sys.exit(3)
|
||||
|
||||
self.config.EJS_DEFAULT_CORES = self._validated_platform_map(
|
||||
self.config.EJS_DEFAULT_CORES, "emulatorjs.default_cores"
|
||||
)
|
||||
|
||||
if not isinstance(self.config.EJS_SETTINGS, dict):
|
||||
log.critical("Invalid config.yml: emulatorjs.settings must be a dictionary")
|
||||
sys.exit(3)
|
||||
@@ -1335,6 +1344,7 @@ class ConfigManager:
|
||||
"enabled": self.config.EJS_NETPLAY_ENABLED,
|
||||
"ice_servers": self.config.EJS_NETPLAY_ICE_SERVERS,
|
||||
},
|
||||
"default_cores": self.config.EJS_DEFAULT_CORES,
|
||||
"settings": self.config.EJS_SETTINGS,
|
||||
"controls": self._format_ejs_controls_for_yaml(),
|
||||
},
|
||||
|
||||
@@ -154,6 +154,7 @@ def get_config(request: Request) -> ConfigResponse:
|
||||
cfg.EJS_NETPLAY_ICE_SERVERS if request.user.is_authenticated else []
|
||||
),
|
||||
EJS_CONTROLS=cfg.EJS_CONTROLS,
|
||||
EJS_DEFAULT_CORES=cfg.EJS_DEFAULT_CORES,
|
||||
EJS_SETTINGS=cfg.EJS_SETTINGS,
|
||||
SCAN_METADATA_PRIORITY=cfg.SCAN_METADATA_PRIORITY,
|
||||
SCAN_ARTWORK_PRIORITY=cfg.SCAN_ARTWORK_PRIORITY,
|
||||
|
||||
@@ -27,6 +27,7 @@ class ConfigResponse(TypedDict):
|
||||
EJS_ENABLE_AUTO_SAVE_SYNC: bool
|
||||
EJS_NETPLAY_ENABLED: bool
|
||||
EJS_NETPLAY_ICE_SERVERS: list[NetplayICEServer]
|
||||
EJS_DEFAULT_CORES: dict[str, str]
|
||||
EJS_SETTINGS: dict[str, dict[str, str]]
|
||||
EJS_CONTROLS: dict[str, EjsControls]
|
||||
SCAN_METADATA_PRIORITY: list[str]
|
||||
|
||||
@@ -81,6 +81,9 @@ emulatorjs:
|
||||
- urls: "turn:global.relay.metered.ca:80"
|
||||
username: "user"
|
||||
credential: "password"
|
||||
default_cores:
|
||||
nds: desmume
|
||||
Nintendo-DSi: melonds
|
||||
settings:
|
||||
parallel_n64:
|
||||
vsync: disabled
|
||||
|
||||
@@ -69,6 +69,10 @@ def test_config_loader():
|
||||
"credential": "password",
|
||||
},
|
||||
]
|
||||
assert loader.config.EJS_DEFAULT_CORES == {
|
||||
"nds": "desmume",
|
||||
"nintendo-dsi": "melonds",
|
||||
}
|
||||
assert loader.config.EJS_SETTINGS == {
|
||||
"parallel_n64": {"vsync": "disabled"},
|
||||
"snes9x": {"snes9x_region": "ntsc"},
|
||||
@@ -160,6 +164,7 @@ def test_empty_config_loader():
|
||||
assert not loader.config.EJS_ENABLE_AUTO_SAVE_SYNC
|
||||
assert not loader.config.EJS_NETPLAY_ENABLED
|
||||
assert loader.config.EJS_NETPLAY_ICE_SERVERS == []
|
||||
assert loader.config.EJS_DEFAULT_CORES == {}
|
||||
assert loader.config.EJS_SETTINGS == {}
|
||||
assert loader.config.EJS_CONTROLS == {}
|
||||
assert loader.config.SCAN_ARTWORK_PRIORITY_OVERRIDES == {}
|
||||
@@ -625,6 +630,26 @@ def test_platform_binding_lookup_ignores_case(tmp_path):
|
||||
assert loader.config.PLATFORMS_BINDING == {}
|
||||
|
||||
|
||||
def _write_emulatorjs_config(tmp_path: Path, emulatorjs_block: str) -> ConfigManager:
|
||||
config_file = tmp_path / "config.yml"
|
||||
config_file.write_text(f"emulatorjs:\n{emulatorjs_block}")
|
||||
return ConfigManager(str(config_file))
|
||||
|
||||
|
||||
def test_null_default_cores_block_means_empty(tmp_path):
|
||||
loader = _write_emulatorjs_config(tmp_path, " default_cores:\n")
|
||||
|
||||
assert loader.config.EJS_DEFAULT_CORES == {}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ['""', "5", "~"])
|
||||
def test_default_core_must_be_a_non_empty_string(tmp_path, value):
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
_write_emulatorjs_config(tmp_path, f" default_cores:\n nds: {value}\n")
|
||||
|
||||
assert excinfo.value.code == 3
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def critical(mocker):
|
||||
"""The last message the config manager logged before exiting."""
|
||||
|
||||
@@ -33,6 +33,7 @@ def test_config(client):
|
||||
DEFAULT_EXCLUDED_MULTI_FILE_DIRS
|
||||
)
|
||||
assert config.get("PLATFORMS_BINDING") == {}
|
||||
assert config.get("EJS_DEFAULT_CORES") == {}
|
||||
assert not config.get("SKIP_HASH_CALCULATION")
|
||||
assert config.get("GAMELIST_MEDIA_THUMBNAIL") == "box2d"
|
||||
assert config.get("GAMELIST_MEDIA_IMAGE") == "screenshot"
|
||||
|
||||
@@ -1696,6 +1696,8 @@ scan:
|
||||
|
||||
emulatorjs:
|
||||
debug: false
|
||||
default_cores:
|
||||
nds: desmume # platform slug → core preselected in the player
|
||||
netplay:
|
||||
enabled: false
|
||||
ice_servers:
|
||||
|
||||
@@ -182,6 +182,10 @@
|
||||
# disable_batch_bootup: false
|
||||
# disable_auto_unload: false
|
||||
# auto_save_sync: false # Upload the save to the server whenever it changes, without Save & Quit
|
||||
# default_cores: # Core preselected per platform, unless overridden on the device
|
||||
# # Use the exact core name, as listed for the platform in the docs
|
||||
# nds: desmume
|
||||
# nintendo-dsi: melonds
|
||||
# settings:
|
||||
# parallel_n64: # Use the exact core name
|
||||
# vsync: disabled
|
||||
|
||||
@@ -29,6 +29,7 @@ export type ConfigResponse = {
|
||||
EJS_ENABLE_AUTO_SAVE_SYNC: boolean;
|
||||
EJS_NETPLAY_ENABLED: boolean;
|
||||
EJS_NETPLAY_ICE_SERVERS: Array<NetplayICEServer>;
|
||||
EJS_DEFAULT_CORES: Record<string, string>;
|
||||
EJS_SETTINGS: Record<string, Record<string, string>>;
|
||||
EJS_CONTROLS: Record<string, EjsControls>;
|
||||
SCAN_METADATA_PRIORITY: Array<string>;
|
||||
|
||||
@@ -32,6 +32,7 @@ const defaultConfig = {
|
||||
EJS_DISABLE_BATCH_BOOTUP: false,
|
||||
EJS_ENABLE_AUTO_SAVE_SYNC: false,
|
||||
EJS_NETPLAY_ICE_SERVERS: [],
|
||||
EJS_DEFAULT_CORES: {},
|
||||
EJS_SETTINGS: {},
|
||||
EJS_CONTROLS: {},
|
||||
SCAN_METADATA_PRIORITY: [],
|
||||
@@ -93,6 +94,9 @@ export default defineStore("config", {
|
||||
isExclusionType(type: string): type is ExclusionType {
|
||||
return Object.keys(this.config).includes(type);
|
||||
},
|
||||
getEJSDefaultCore(platformSlug: string): string | null {
|
||||
return this.config.EJS_DEFAULT_CORES[platformSlug.toLowerCase()] ?? null;
|
||||
},
|
||||
getEJSCoreOptions(core: string | null): Record<string, string | boolean> {
|
||||
const defaultOptions = this.config.EJS_SETTINGS["default"] || {};
|
||||
if (!core) return defaultOptions;
|
||||
|
||||
@@ -315,7 +315,13 @@ onMounted(async () => {
|
||||
});
|
||||
firmwareOptions.value = firmwareResponse.data;
|
||||
|
||||
supportedCores.value = [...getSupportedEJSCores(rom.value.platform_slug)];
|
||||
const platformSlug = rom.value.platform_slug;
|
||||
supportedCores.value = [
|
||||
...getSupportedEJSCores(
|
||||
platformSlug,
|
||||
configStore.config.EJS_NETPLAY_ENABLED,
|
||||
),
|
||||
];
|
||||
|
||||
emitter?.on("saveSelected", selectSave);
|
||||
emitter?.on("stateSelected", selectState);
|
||||
@@ -333,6 +339,14 @@ onMounted(async () => {
|
||||
});
|
||||
}
|
||||
|
||||
// compatibleStates filters on selectedCore, so resolve the core first.
|
||||
selectedCore.value = resolveRememberedCore(
|
||||
rom.value.id,
|
||||
platformSlug,
|
||||
supportedCores.value,
|
||||
configStore.getEJSDefaultCore(platformSlug),
|
||||
);
|
||||
|
||||
// Default selection — save and state are independent, so both can be
|
||||
// armed at once. The bound save is the write-back target for "Save &
|
||||
// Quit" (PUT in place), so we only auto-bind it when the choice is
|
||||
@@ -340,13 +354,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.
|
||||
const initiallyCompatibleStates = rom.value.user_states.filter(
|
||||
(s) => !s.emulator || s.emulator === supportedCores.value[0],
|
||||
);
|
||||
const hasCompatibleState = initiallyCompatibleStates.length > 0;
|
||||
const hasCompatibleState = compatibleStates.value.length > 0;
|
||||
|
||||
if (hasCompatibleState) {
|
||||
selectedState.value = initiallyCompatibleStates[0];
|
||||
selectedState.value = compatibleStates.value[0];
|
||||
}
|
||||
const safeToBindSave =
|
||||
rom.value.user_saves.length === 1 || !hasCompatibleState;
|
||||
@@ -360,16 +371,8 @@ onMounted(async () => {
|
||||
bootableRomFiles.value,
|
||||
);
|
||||
|
||||
selectedCore.value = resolveRememberedCore(
|
||||
rom.value.id,
|
||||
rom.value.platform_slug,
|
||||
supportedCores.value,
|
||||
);
|
||||
|
||||
const coreOptions = configStore.getEJSCoreOptions(selectedCore.value);
|
||||
const storedBiosID = localStorage.getItem(
|
||||
`player:${rom.value.platform_slug}:bios_id`,
|
||||
);
|
||||
const storedBiosID = localStorage.getItem(`player:${platformSlug}:bios_id`);
|
||||
|
||||
selectedFirmware.value = resolveInitialFirmware({
|
||||
options: firmwareOptions.value,
|
||||
|
||||
@@ -50,6 +50,26 @@ describe("coreStorage", () => {
|
||||
expect(resolveRememberedCore(ROM_ID, SLUG, ARCADE_CORES)).toBe("mame2003");
|
||||
});
|
||||
|
||||
it("uses the instance default when nothing is remembered", () => {
|
||||
expect(resolveRememberedCore(ROM_ID, SLUG, ARCADE_CORES, "fbneo")).toBe(
|
||||
"fbneo",
|
||||
);
|
||||
});
|
||||
|
||||
it("prefers a remembered core over the instance default", () => {
|
||||
rememberCore(ROM_ID, SLUG, "mame2003_plus");
|
||||
|
||||
expect(resolveRememberedCore(ROM_ID, SLUG, ARCADE_CORES, "fbneo")).toBe(
|
||||
"mame2003_plus",
|
||||
);
|
||||
});
|
||||
|
||||
it("ignores an instance default the platform does not support", () => {
|
||||
expect(resolveRememberedCore(ROM_ID, SLUG, ARCADE_CORES, "melonds")).toBe(
|
||||
"mame2003",
|
||||
);
|
||||
});
|
||||
|
||||
it("forgets both keys when the selection is cleared", () => {
|
||||
rememberCore(ROM_ID, SLUG, "mame2003_plus");
|
||||
rememberCore(ROM_ID, SLUG, null);
|
||||
|
||||
@@ -4,20 +4,24 @@ 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.
|
||||
* The first remembered core the platform still supports, else the core the
|
||||
* instance configures for it, 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.
|
||||
* upstream, or gated behind netplay, or a typo in config.yml) falls through
|
||||
* instead of masking the next.
|
||||
*/
|
||||
export function resolveRememberedCore(
|
||||
romId: number,
|
||||
platformSlug: string,
|
||||
supportedCores: readonly string[],
|
||||
configuredCore?: string | null,
|
||||
): string {
|
||||
return (
|
||||
[
|
||||
localStorage.getItem(gameKey(romId)),
|
||||
localStorage.getItem(platformKey(platformSlug)),
|
||||
configuredCore,
|
||||
].find((core): core is string => !!core && supportedCores.includes(core)) ??
|
||||
supportedCores[0]
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user