From 0682a46e28feb5c0315913144ec3c4ba76159fd4 Mon Sep 17 00:00:00 2001 From: Ethan O'Brien <77750390+ethanaobrien@users.noreply.github.com> Date: Mon, 29 Jan 2024 17:45:47 -0600 Subject: [PATCH] Cleanup --- data/GameManager.js | 54 +++++++++++++++++++++++++++------------------ data/emulator.js | 22 +++++++++--------- 2 files changed, 43 insertions(+), 33 deletions(-) diff --git a/data/GameManager.js b/data/GameManager.js index 8f85747..67937f1 100644 --- a/data/GameManager.js +++ b/data/GameManager.js @@ -50,31 +50,41 @@ class EJS_GameManager { }) } loadExternalFiles() { - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { if (this.EJS.config.externalFiles && this.EJS.config.externalFiles.constructor.name === 'Object') { for (const key in this.EJS.config.externalFiles) { - let path = key; - if (key.trim().endsWith("/")) { - const invalidCharacters = /[#<$+%>!`&*'|{}/\\?"=@:^\r\n]/ig; - let name = this.EJS.config.externalFiles[key].split("/").pop().split("#")[0].split("?")[0].replace(invalidCharacters, "").trim(); - if (!name) continue; - path += name; - } - this.EJS.downloadFile(this.EJS.config.externalFiles[key], (res) => { - if (res === -1) { - if (this.EJS.debug) console.warn("Failed to fetch file from '" + this.EJS.config.externalFiles[key] + "'. Make sure the file exists."); - return resolve(); - } - try { - this.writeFile(path, this.EJS.config.externalFiles[key]); - } catch(e) { - if (this.EJS.debug) console.warn("Failed to write file to '" + path + "'. Make sure there are no conflicting files."); - } - resolve(); - }, null, true, {responseType: "text", method: "GET"}); - + await new Promise(done => { + this.EJS.downloadFile(this.EJS.config.externalFiles[key], async (res) => { + if (res === -1) { + if (this.EJS.debug) console.warn("Failed to fetch file from '" + this.EJS.config.externalFiles[key] + "'. Make sure the file exists."); + return done(); + } + let path = key; + if (key.trim().endsWith("/")) { + const invalidCharacters = /[#<$+%>!`&*'|{}/\\?"=@:^\r\n]/ig; + let name = this.EJS.config.externalFiles[key].split("/").pop().split("#")[0].split("?")[0].replace(invalidCharacters, "").trim(); + if (!name) return done(); + const files = await this.EJS.checkCompression(new Uint8Array(res.data), this.EJS.localization("Decompress Game Assets")); + if (files["!!notCompressedData"]) { + path += name; + } else { + for (const k in files) { + this.writeFile(path+k, files[k]); + } + return done(); + } + } + try { + this.writeFile(path, res.data); + } catch(e) { + if (this.EJS.debug) console.warn("Failed to write file to '" + path + "'. Make sure there are no conflicting files."); + } + done(); + }, null, true, {responseType: "arraybuffer", method: "GET"}); + }) } - } else resolve(); + } + resolve(); }); } writeFile(path, data) { diff --git a/data/emulator.js b/data/emulator.js index 50f69f1..c8dfe07 100644 --- a/data/emulator.js +++ b/data/emulator.js @@ -494,21 +494,21 @@ class EmulatorJS { } return text; } + isCompressed(data) { //https://www.garykessler.net/library/file_sigs.html + //todo. Use hex instead of numbers + if ((data[0] === 80 && data[1] === 75) && ((data[2] === 3 && data[3] === 4) || (data[2] === 5 && data[3] === 6) || (data[2] === 7 && data[3] === 8))) { + return 'zip'; + } else if (data[0] === 55 && data[1] === 122 && data[2] === 188 && data[3] === 175 && data[4] === 39 && data[5] === 28) { + return '7z'; + } else if ((data[0] === 82 && data[1] === 97 && data[2] === 114 && data[3] === 33 && data[4] === 26 && data[5] === 7) && ((data[6] === 0) || (data[6] === 1 && data[7] == 0))) { + return 'rar'; + } + } checkCompression(data, msg, fileCbFunc) { if (msg) { this.textElem.innerText = msg; } //to be put in another file - function isCompressed(data) { //https://www.garykessler.net/library/file_sigs.html - //todo. Use hex instead of numbers - if ((data[0] === 80 && data[1] === 75) && ((data[2] === 3 && data[3] === 4) || (data[2] === 5 && data[3] === 6) || (data[2] === 7 && data[3] === 8))) { - return 'zip'; - } else if (data[0] === 55 && data[1] === 122 && data[2] === 188 && data[3] === 175 && data[4] === 39 && data[5] === 28) { - return '7z'; - } else if ((data[0] === 82 && data[1] === 97 && data[2] === 114 && data[3] === 33 && data[4] === 26 && data[5] === 7) && ((data[6] === 0) || (data[6] === 1 && data[7] == 0))) { - return 'rar'; - } - } const createWorker = (path) => { return new Promise((resolve, reject) => { this.downloadFile(path, (res) => { @@ -595,7 +595,7 @@ class EmulatorJS { }) }) } - const compression = isCompressed(data.slice(0, 10)); + const compression = this.isCompressed(data.slice(0, 10)); if (compression) { //Need to do zip and rar still if (compression === "7z") {