From 92b2bf696ceb765adb5b2194e0689f09ebc4b7e9 Mon Sep 17 00:00:00 2001 From: Ethan O'Brien <77750390+ethanaobrien@users.noreply.github.com> Date: Mon, 26 Jun 2023 11:39:31 -0500 Subject: [PATCH] bios support, fix canvas size bugs --- src/emulator.js | 82 +++++++++++++++++++++++++++++++++++++------------ src/loader.js | 5 +-- 2 files changed, 66 insertions(+), 21 deletions(-) diff --git a/src/emulator.js b/src/emulator.js index b7c3be9..2ed98fc 100644 --- a/src/emulator.js +++ b/src/emulator.js @@ -227,7 +227,7 @@ class EmulatorJS { return new Promise((resolve, reject) => { const files = {}; const onMessage = (data) => { - console.log(data); + //console.log(data); if (!data.data) return; //data.data.t/ 4=progress, 2 is file, 1 is zip done if (data.data.t === 4 && msg) { @@ -262,7 +262,7 @@ class EmulatorJS { return decompressRar(data); } } else { - return new Promise(resolve => resolve({file: data})); + return new Promise(resolve => resolve({"!!notCompressedData": data})); } } @@ -336,6 +336,42 @@ class EmulatorJS { } return options[core] || core; } + getBaseFileName() { + //Only once game and core is loaded + if (!this.started) return null; + let parts = this.fileName.split("."); + parts.splice(parts.length-1, 1); + return parts.join("."); + } + downloadBios() { + if (!this.config.biosUrl) { + this.startGame(); + return; + } + this.textElem.innerText = this.localization("Download Game BIOS"); + this.downloadFile(this.config.biosUrl, (res) => { + if (res === -1) { + this.textElem.innerText = "Error"; + this.textElem.style.color = "red"; + return; + } + this.checkCompression(new Uint8Array(res.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]); + break; + } + if (k.endsWith('/')) continue; + console.log(k.split('/').pop()); + FS.writeFile(k.split('/').pop(), data[k]); + } + this.startGame(); + }); + }, (progress) => { + this.textElem.innerText = this.localization("Download Game Data") + progress; + }, true, {responseType: "arraybuffer", method: "GET"}); + + } downloadRom() { this.gameManager = new window.EJS_GameManager(this.Module); @@ -348,11 +384,20 @@ class EmulatorJS { } this.checkCompression(new Uint8Array(res.data), this.localization("Decompress Game Data")).then((data) => { for (const k in data) { + if (k === "!!notCompressedData") { + this.fileName = this.config.gameUrl.split('/').pop().split("#")[0].split("?")[0]; + FS.writeFile(this.fileName, data[k]); + break; + } + if (k.endsWith('/')) { + FS.mkdir(k); + continue; + } this.fileName = k; - FS.writeFile(k, data[k]); //needs to be cleaned up - break; + console.log(k); + FS.writeFile(k, data[k]); } - this.startGame(); + this.downloadBios(); }); }, (progress) => { this.textElem.innerText = this.localization("Download Game Data") + progress; @@ -399,6 +444,7 @@ class EmulatorJS { const args = []; if (this.debug) args.push('-v'); args.push('/'+this.fileName); + console.log(args); this.Module.callMain(args); this.Module.resumeMainLoop(); this.started = true; @@ -407,14 +453,7 @@ class EmulatorJS { this.virtualGamepad.style.display = ""; } - //this needs to be fixed... - setInterval(() => { - if (document.fullscreenElement !== null) { - this.Module.setCanvasSize(this.canvas.getBoundingClientRect().width-10, this.canvas.getBoundingClientRect().height-10); - } else { - this.Module.setCanvasSize(800, 600); - } - }, 100) + this.handleResize(); } bindListeners() { this.createContextMenu(); @@ -422,6 +461,7 @@ class EmulatorJS { this.createControlSettingMenu(); this.setVirtualGamepad(); this.addEventListener(document, "keydown keyup", this.keyChange.bind(this)); + this.addEventListener(window, "resize", this.handleResize.bind(this)); //this.gamepad = new GamepadHandler(); //https://github.com/ethanaobrien/Gamepad //keyboard, etc... } @@ -469,7 +509,8 @@ class EmulatorJS { screenshotUrl = URL.createObjectURL(blob); const a = this.createElement("a"); a.href = screenshotUrl; - a.download = "screenshot.png"; + const date = new Date(); + a.download = this.getBaseFileName()+"-"+date.getMonth()+"-"+date.getDate()+"-"+date.getFullYear()+".png"; a.click(); hideMenu(); }); @@ -635,7 +676,7 @@ class EmulatorJS { stateUrl = URL.createObjectURL(blob); const a = this.createElement("a"); a.href = stateUrl; - a.download = "game.state"; + a.download = this.getBaseFileName()+".state"; a.click(); }); addButton("Load State", '', async () => { @@ -1367,9 +1408,12 @@ class EmulatorJS { }); }) - - //todo - zone and dpad (and input) - - + } + handleResize() { + const dpr = window.devicePixelRatio || 1; + const positionInfo = this.game.getBoundingClientRect(); + const width = positionInfo.width * dpr; + const height = (positionInfo.height * dpr); + Module.setCanvasSize(width, height); } } diff --git a/src/loader.js b/src/loader.js index bcf16e1..8e26b3d 100644 --- a/src/loader.js +++ b/src/loader.js @@ -41,9 +41,10 @@ await loadStyle('css/main.css'); } const config = {}; - config.gameUrl = EJS_gameUrl; + config.gameUrl = window.EJS_gameUrl; config.dataPath = scriptPath; - 'undefined' != typeof EJS_core && (config.system = EJS_core); + config.system = window.EJS_core; + config.biosUrl = window.EJS_biosUrl; new EmulatorJS(EJS_player, config);