This commit is contained in:
Ethan O'Brien 2024-01-29 17:45:47 -06:00
parent 0fc2c55971
commit 0682a46e28
2 changed files with 43 additions and 33 deletions

View file

@ -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) {
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) continue;
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();
}
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]);
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.");
}
resolve();
}, null, true, {responseType: "text", method: "GET"});
done();
}, null, true, {responseType: "arraybuffer", method: "GET"});
})
}
} else resolve();
}
resolve();
});
}
writeFile(path, data) {

View file

@ -494,12 +494,7 @@ class EmulatorJS {
}
return text;
}
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
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';
@ -509,6 +504,11 @@ class EmulatorJS {
return 'rar';
}
}
checkCompression(data, msg, fileCbFunc) {
if (msg) {
this.textElem.innerText = msg;
}
//to be put in another file
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") {