From 41c7850dfa71925667b1e1cb8f83cf7d5f334a69 Mon Sep 17 00:00:00 2001 From: Ethan O'Brien Date: Sat, 3 Aug 2024 20:54:05 -0500 Subject: [PATCH] Modularize EmulatorJS --- data/src/GameManager.js | 24 ++++++------- data/src/emulator.js | 76 ++++++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 48 deletions(-) diff --git a/data/src/GameManager.js b/data/src/GameManager.js index 76c78ab..99adce9 100644 --- a/data/src/GameManager.js +++ b/data/src/GameManager.js @@ -39,8 +39,8 @@ class EJS_GameManager { this.writeFile("/home/web_user/retroarch/userdata/retroarch.cfg", this.getRetroArchCfg()); - this.FS.mount(IDBFS, {}, '/data/saves'); - this.FS.syncfs(true, () => {}); + this.FS.mount(this.FS.filesystems.IDBFS, {autoPersist: true}, '/data/saves'); + //this.FS.syncfs(true, () => {}); this.initShaders(); @@ -48,10 +48,8 @@ class EJS_GameManager { this.toggleMainLoop(0); this.functions.saveSaveFiles(); setTimeout(() => { - this.FS.syncfs(() => { - try {window.abort()} catch(e){}; - }); - }, 500); + try {window.abort()} catch(e){}; + }, 1000); }) } loadExternalFiles() { @@ -164,7 +162,7 @@ class EJS_GameManager { return new Promise(async resolve => { while (1) { try { - FS.stat("/screenshot.png"); + this.FS.stat("/screenshot.png"); return resolve(this.FS.readFile("/screenshot.png")); } catch(e) {} @@ -273,14 +271,14 @@ class EJS_GameManager { } for (let i=0; i 1) { let contents = ""; for (let i=0; i {}); + //this.FS.syncfs(false, () => {}); } supportsStates() { return !!this.functions.supportsStates(); } getSaveFile() { this.saveSaveFiles(); - const exists = FS.analyzePath(this.getSaveFilePath()).exists; - return (exists ? FS.readFile(this.getSaveFilePath()) : null); + const exists = this.FS.analyzePath(this.getSaveFilePath()).exists; + return (exists ? this.FS.readFile(this.getSaveFilePath()) : null); } loadSaveFiles() { this.clearEJSResetTimer(); diff --git a/data/src/emulator.js b/data/src/emulator.js index 7121a8d..41c7c01 100644 --- a/data/src/emulator.js +++ b/data/src/emulator.js @@ -662,11 +662,11 @@ class EmulatorJS { let legacy = (this.supportsWebgl2 && this.webgl2Enabled ? "" : "-legacy"); let filename = this.getCore()+(this.config.threads ? "-thread" : "")+legacy+"-wasm.data"; this.storage.core.get(filename).then((result) => { - if (result && result.version === rep.buildStart && !this.debug) { - gotCore(result.data); - return; - } - const corePath = 'cores/'+filename; + if (result && result.version === rep.buildStart && !this.debug) { + gotCore(result.data); + return; + } + const corePath = 'cores/'+filename; this.downloadFile(corePath, (res) => { if (res === -1) { console.log("File not found, attemping to fetch from emulatorjs cdn"); @@ -702,9 +702,11 @@ class EmulatorJS { }, null, false, {responseType: "text", method: "GET"}); } initGameCore(js, wasm, thread) { - this.initModule(wasm, thread); let script = this.createElement("script"); script.src = URL.createObjectURL(new Blob([js], {type: "application/javascript"})); + script.addEventListener("load", () => { + this.initModule(wasm, thread); + }); document.body.appendChild(script); } getBaseFileName(force) { @@ -773,11 +775,11 @@ class EmulatorJS { this.checkCompression(new Uint8Array(data), this.localization("Decompress Game Patch")).then((data) => { for (const k in data) { if (k === "!!notCompressedData") { - FS.writeFile(this.config.gamePatchUrl.split('/').pop().split("#")[0].split("?")[0], data[k]); + this.gameManager.FS.writeFile(this.config.gamePatchUrl.split('/').pop().split("#")[0].split("?")[0], data[k]); break; } if (k.endsWith('/')) continue; - FS.writeFile("/" + k.split('/').pop(), data[k]); + this.gameManager.FS.writeFile("/" + k.split('/').pop(), data[k]); } resolve(); }) @@ -823,11 +825,11 @@ class EmulatorJS { this.checkCompression(new Uint8Array(data), this.localization("Decompress Game Parent")).then((data) => { for (const k in data) { if (k === "!!notCompressedData") { - FS.writeFile(this.config.gameParentUrl.split('/').pop().split("#")[0].split("?")[0], data[k]); + this.gameManager.FS.writeFile(this.config.gameParentUrl.split('/').pop().split("#")[0].split("?")[0], data[k]); break; } if (k.endsWith('/')) continue; - FS.writeFile("/" + k.split('/').pop(), data[k]); + this.gameManager.FS.writeFile("/" + k.split('/').pop(), data[k]); } resolve(); }) @@ -873,11 +875,11 @@ class EmulatorJS { this.checkCompression(new Uint8Array(data), this.localization("Decompress Game BIOS")).then((data) => { for (const k in data) { if (k === "!!notCompressedData") { - FS.writeFile(this.config.biosUrl.split('/').pop().split("#")[0].split("?")[0], data[k]); + this.gameManager.FS.writeFile(this.config.biosUrl.split('/').pop().split("#")[0].split("?")[0], data[k]); break; } if (k.endsWith('/')) continue; - FS.writeFile("/" + k.split('/').pop(), data[k]); + this.gameManager.FS.writeFile("/" + k.split('/').pop(), data[k]); } resolve(); }) @@ -928,7 +930,7 @@ class EmulatorJS { const gotGameData = (data) => { if (['arcade', 'mame'].includes(this.getCore(true))) { this.fileName = this.getBaseFileName(true); - FS.writeFile(this.fileName, new Uint8Array(data)); + this.gameManager.FS.writeFile(this.fileName, new Uint8Array(data)); resolve(); return; } @@ -950,20 +952,20 @@ class EmulatorJS { for (let i=0; i { @@ -1067,26 +1069,28 @@ class EmulatorJS { })(); } initModule(wasmData, threadData) { - window.Module = { - 'noInitialRun': true, - 'onRuntimeInitialized': this.downloadFiles.bind(this), - 'arguments': [], - 'preRun': [], - 'postRun': [], - 'canvas': this.canvas, - 'print': (msg) => { + window.EJS_Runtime({ + noInitialRun: true, + onRuntimeInitialized: null, + arguments: [], + preRun: [], + postRun: [], + canvas: this.canvas, + print: (msg) => { if (this.debug) { console.log(msg); } }, - 'printErr': (msg) => { + printErr: (msg) => { if (this.debug) { console.log(msg); + } + }, - 'totalDependencies': 0, - 'monitorRunDependencies': () => {}, - 'locateFile': function(fileName) { + totalDependencies: 0, + monitorRunDependencies: () => {}, + locateFile: function(fileName) { if (this.debug) console.log(fileName); if (fileName.endsWith(".wasm")) { return URL.createObjectURL(new Blob([wasmData], {type: "application/wasm"})); @@ -1094,8 +1098,10 @@ class EmulatorJS { return URL.createObjectURL(new Blob([threadData], {type: "application/javascript"})); } } - }; - this.Module = window.Module; + }).then(module => { + this.Module = module; + this.downloadFiles(); + }); } startGame() { try { @@ -1815,10 +1821,10 @@ class EmulatorJS { for (let i=0; i', async () => {