From 24cd55abe57338fe75e4e4cd3550bca47c99ed28 Mon Sep 17 00:00:00 2001 From: zurdi Date: Wed, 16 Sep 2026 19:34:39 +0200 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01MozJiij1oQbu12GvtAPBFR --- backend/config/config_manager.py | 2 +- .../tests/config/fixtures/config/config.yml | 2 +- backend/tests/config/test_config_loader.py | 4 +-- examples/config.example.yml | 2 +- frontend/src/stores/config.ts | 2 +- .../src/views/Player/EmulatorJS/Player.vue | 25 ++++++++++++++----- 6 files changed, 25 insertions(+), 12 deletions(-) diff --git a/backend/config/config_manager.py b/backend/config/config_manager.py index ce6714425..c29fecfb4 100644 --- a/backend/config/config_manager.py +++ b/backend/config/config_manager.py @@ -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 diff --git a/backend/tests/config/fixtures/config/config.yml b/backend/tests/config/fixtures/config/config.yml index daee39102..5bde2c5d6 100644 --- a/backend/tests/config/fixtures/config/config.yml +++ b/backend/tests/config/fixtures/config/config.yml @@ -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: diff --git a/backend/tests/config/test_config_loader.py b/backend/tests/config/test_config_loader.py index b5030c55a..325741c33 100644 --- a/backend/tests/config/test_config_loader.py +++ b/backend/tests/config/test_config_loader.py @@ -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 == {} diff --git a/examples/config.example.yml b/examples/config.example.yml index 5bac71808..e4a87cde1 100644 --- a/examples/config.example.yml +++ b/examples/config.example.yml @@ -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 diff --git a/frontend/src/stores/config.ts b/frontend/src/stores/config.ts index ecb9dd307..aa395e6a9 100644 --- a/frontend/src/stores/config.ts +++ b/frontend/src/stores/config.ts @@ -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: {}, diff --git a/frontend/src/views/Player/EmulatorJS/Player.vue b/frontend/src/views/Player/EmulatorJS/Player.vue index fec1dd753..957676973 100644 --- a/frontend/src/views/Player/EmulatorJS/Player.vue +++ b/frontend/src/views/Player/EmulatorJS/Player.vue @@ -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 { // 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 | 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