feat(player): sync in-game saves seconds after the game writes them

The periodic save upload only ran on EmulatorJS' "System Save interval"
tick (5 minutes by default, never when the player disabled it), and was
off unless emulatorjs.auto_save_sync was set. The player now flushes the
SRAM itself every 5 seconds while the game runs, so the existing tracker
sees a new in-game save on the next two agreeing ticks and uploads it,
and the config flag defaults to on.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MozJiij1oQbu12GvtAPBFR
This commit is contained in:
zurdi
2026-09-16 19:34:39 +02:00
parent 1444b0454a
commit 24cd55abe5
6 changed files with 25 additions and 12 deletions

View File

@@ -756,7 +756,7 @@ class ConfigManager:
self._raw_config, "emulatorjs.disable_batch_bootup", False
),
EJS_ENABLE_AUTO_SAVE_SYNC=pydash.get(
self._raw_config, "emulatorjs.auto_save_sync", False
self._raw_config, "emulatorjs.auto_save_sync", True
),
EJS_NETPLAY_ENABLED=pydash.get(
self._raw_config, "emulatorjs.netplay.enabled", False

View File

@@ -73,7 +73,7 @@ emulatorjs:
cache_limit: 1000
disable_auto_unload: true
disable_batch_bootup: true
auto_save_sync: true
auto_save_sync: false
netplay:
enabled: true
ice_servers:

View File

@@ -58,7 +58,7 @@ def test_config_loader():
assert loader.config.EJS_DEBUG
assert loader.config.EJS_DISABLE_AUTO_UNLOAD
assert loader.config.EJS_DISABLE_BATCH_BOOTUP
assert loader.config.EJS_ENABLE_AUTO_SAVE_SYNC
assert not loader.config.EJS_ENABLE_AUTO_SAVE_SYNC
assert loader.config.EJS_CACHE_LIMIT == 1000
assert loader.config.EJS_NETPLAY_ENABLED
assert loader.config.EJS_NETPLAY_ICE_SERVERS == [
@@ -161,7 +161,7 @@ def test_empty_config_loader():
assert loader.config.EJS_CACHE_LIMIT is None
assert not loader.config.EJS_DISABLE_AUTO_UNLOAD
assert not loader.config.EJS_DISABLE_BATCH_BOOTUP
assert not loader.config.EJS_ENABLE_AUTO_SAVE_SYNC
assert 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 == {}

View File

@@ -182,7 +182,7 @@
# cache_limit: null # Cache limit per ROM (in bytes)
# disable_batch_bootup: false
# disable_auto_unload: false
# auto_save_sync: false # Upload the save to the server whenever it changes, without Save & Quit
# auto_save_sync: true # Upload the save to the server seconds after the game writes it, 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

View File

@@ -30,7 +30,7 @@ const defaultConfig = {
EJS_CACHE_LIMIT: null,
EJS_DISABLE_AUTO_UNLOAD: false,
EJS_DISABLE_BATCH_BOOTUP: false,
EJS_ENABLE_AUTO_SAVE_SYNC: false,
EJS_ENABLE_AUTO_SAVE_SYNC: true,
EJS_NETPLAY_ICE_SERVERS: [],
EJS_DEFAULT_CORES: {},
EJS_SETTINGS: {},

View File

@@ -294,7 +294,7 @@ onMounted(() => {
});
onBeforeUnmount(async () => {
autoSaveSyncEmulator = null;
uninstallAutoSaveSync();
emitter?.off("saveSelected", loadSave);
emitter?.off("stateSelected", loadState);
window.EJS_emulator?.callEvent("exit");
@@ -344,10 +344,15 @@ async function waitForGameManager(timeoutMs = 5000): Promise<boolean> {
// frames rendered before loadState takes cleanly.
const STATE_APPLY_SETTLE_MS = 500;
// Periodic save upload on EmulatorJS' "System Save interval" tick (see
// createSaveSyncTracker). EmulatorJS has no `off`, so the handler stays
// subscribed and this slot is what tells it the component still owns it.
// Periodic save upload on the "saveSaveFiles" tick (see createSaveSyncTracker).
// EmulatorJS flushes the SRAM only on its "System Save interval" (5 minutes by
// default), so a faster flush here gets an in-game save to the server seconds
// after the game writes it.
const SAVE_SYNC_POLL_MS = 5000;
// EmulatorJS has no `off`: the handler stays subscribed and this slot is what
// tells it the component still owns it.
let autoSaveSyncEmulator: object | null = null;
let autoSaveSyncTimer: ReturnType<typeof setInterval> | null = null;
function installAutoSaveSync() {
const emulator = window.EJS_emulator;
if (!emulator?.gameManager || autoSaveSyncEmulator === emulator) return;
@@ -373,6 +378,14 @@ function installAutoSaveSync() {
uploading = false;
}
});
autoSaveSyncTimer = setInterval(() => {
if (emulator.started) emulator.gameManager.saveSaveFiles();
}, SAVE_SYNC_POLL_MS);
}
function uninstallAutoSaveSync() {
autoSaveSyncEmulator = null;
if (autoSaveSyncTimer) clearInterval(autoSaveSyncTimer);
autoSaveSyncTimer = null;
}
// Saves management
@@ -585,7 +598,7 @@ window.EJS_onGameStart = async () => {
const exitEmulation = createExitEmulationButton();
exitEmulation.addEventListener("click", async () => {
autoSaveSyncEmulator = null;
uninstallAutoSaveSync();
if (!romRef.value || !window.EJS_emulator) return immediateExit();
romsStore.update(romRef.value);
immediateExit();
@@ -593,7 +606,7 @@ window.EJS_onGameStart = async () => {
const saveAndQuit = createSaveQuitButton();
saveAndQuit.addEventListener("click", async () => {
autoSaveSyncEmulator = null;
uninstallAutoSaveSync();
if (!romRef.value || !window.EJS_emulator) return immediateExit();
// Grab the screenshot while the game is still running (EmulatorJS reads