Save file support

This commit is contained in:
Ethan O'Brien 2023-07-07 12:22:20 -05:00
parent 0f96beabf4
commit 36d2d61740
2 changed files with 27 additions and 10 deletions

View file

@ -21,7 +21,8 @@ class EJS_GameManager {
setCurrentDisk: this.Module.cwrap('set_current_disk', 'null', ['number']), setCurrentDisk: this.Module.cwrap('set_current_disk', 'null', ['number']),
getSaveFilePath: this.Module.cwrap('save_file_path', 'string', []), getSaveFilePath: this.Module.cwrap('save_file_path', 'string', []),
saveSaveFiles: this.Module.cwrap('cmd_savefiles', '', []), saveSaveFiles: this.Module.cwrap('cmd_savefiles', '', []),
supportsStates: this.Module.cwrap('supports_states', 'number', []) supportsStates: this.Module.cwrap('supports_states', 'number', []),
loadSaveFiles: this.Module.cwrap('refresh_save_files', 'null', [])
} }
this.mkdir("/home"); this.mkdir("/home");
this.mkdir("/home/web_user"); this.mkdir("/home/web_user");
@ -33,7 +34,15 @@ class EJS_GameManager {
this.FS.writeFile("/home/web_user/retroarch/userdata/retroarch.cfg", this.getRetroArchCfg()); this.FS.writeFile("/home/web_user/retroarch/userdata/retroarch.cfg", this.getRetroArchCfg());
this.FS.mount(IDBFS, {}, '/data/saves');
this.FS.syncfs(true, () => {});
this.initShaders(); this.initShaders();
this.EJS.addEventListener(window, "beforeunload", () => {
this.saveSaveFiles();
this.FS.syncfs(() => {});
})
} }
mkdir(path) { mkdir(path) {
try { try {
@ -41,7 +50,15 @@ class EJS_GameManager {
} catch(e) {} } catch(e) {}
} }
getRetroArchCfg() { getRetroArchCfg() {
return "autosave_interval = 10\nsavefile_directory = \"/data/saves\"\n"; return "autosave_interval = 60\n" +
"screenshot_directory = /\n" +
"block_sram_overwrite = false\n" +
"video_font_enable = false\n" +
"video_scale = 1.0\n" +
"video_gpu_screenshot = false\n" +
"audio_latency = 96\n" +
"video_vsync = true\n" +
"savefile_directory = \"/data/saves\"\n";
} }
initShaders() { initShaders() {
if (!window.EJS_SHADERS) return; if (!window.EJS_SHADERS) return;
@ -217,18 +234,18 @@ class EJS_GameManager {
} }
saveSaveFiles() { saveSaveFiles() {
this.functions.saveSaveFiles(); this.functions.saveSaveFiles();
this.FS.syncfs(false, () => {});
} }
supportsStates() { supportsStates() {
return !!this.functions.supportsStates(); return !!this.functions.supportsStates();
} }
getSaveFile() { getSaveFile() {
return new Promise((resolve) => { this.saveSaveFiles();
this.saveSaveFiles(); const exists = FS.analyzePath(this.getSaveFilePath()).exists;
setTimeout(() => { return (exists ? FS.readFile(this.getSaveFilePath()) : null);
const exists = FS.analyzePath(this.getSaveFilePath()).exists; }
resolve(exists ? FS.readFile(this.getSaveFilePath()) : null); loadSaveFiles() {
}, 250); this.functions.loadSaveFiles();
})
} }
} }

View file

@ -1107,7 +1107,6 @@ class EmulatorJS {
const state = new Uint8Array(await file.arrayBuffer()); const state = new Uint8Array(await file.arrayBuffer());
this.gameManager.loadState(state); this.gameManager.loadState(state);
} }
this.gameManager.saveSaveFiles();
}); });
const controlMenu = addButton("Control Settings", '<svg viewBox="0 0 640 512"><path fill="currentColor" d="M480 96H160C71.6 96 0 167.6 0 256s71.6 160 160 160c44.8 0 85.2-18.4 114.2-48h91.5c29 29.6 69.5 48 114.2 48 88.4 0 160-71.6 160-160S568.4 96 480 96zM256 276c0 6.6-5.4 12-12 12h-52v52c0 6.6-5.4 12-12 12h-40c-6.6 0-12-5.4-12-12v-52H76c-6.6 0-12-5.4-12-12v-40c0-6.6 5.4-12 12-12h52v-52c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v52h52c6.6 0 12 5.4 12 12v40zm184 68c-26.5 0-48-21.5-48-48s21.5-48 48-48 48 21.5 48 48-21.5 48-48 48zm80-80c-26.5 0-48-21.5-48-48s21.5-48 48-48 48 21.5 48 48-21.5 48-48 48z"/></svg>', () => { const controlMenu = addButton("Control Settings", '<svg viewBox="0 0 640 512"><path fill="currentColor" d="M480 96H160C71.6 96 0 167.6 0 256s71.6 160 160 160c44.8 0 85.2-18.4 114.2-48h91.5c29 29.6 69.5 48 114.2 48 88.4 0 160-71.6 160-160S568.4 96 480 96zM256 276c0 6.6-5.4 12-12 12h-52v52c0 6.6-5.4 12-12 12h-40c-6.6 0-12-5.4-12-12v-52H76c-6.6 0-12-5.4-12-12v-40c0-6.6 5.4-12 12-12h52v-52c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v52h52c6.6 0 12 5.4 12 12v40zm184 68c-26.5 0-48-21.5-48-48s21.5-48 48-48 48 21.5 48 48-21.5 48-48 48zm80-80c-26.5 0-48-21.5-48-48s21.5-48 48-48 48 21.5 48 48-21.5 48-48 48z"/></svg>', () => {
this.controlMenu.style.display = ""; this.controlMenu.style.display = "";
@ -1144,6 +1143,7 @@ class EmulatorJS {
} }
if (FS.analyzePath(path).exists) FS.unlink(path); if (FS.analyzePath(path).exists) FS.unlink(path);
FS.writeFile(path, sav); FS.writeFile(path, sav);
this.gameManager.loadSaveFiles();
}); });
const spacer = this.createElement("span"); const spacer = this.createElement("span");