Ability to set gameurl to a blob

This commit is contained in:
Ethan O'Brien 2023-07-22 11:40:40 -05:00
parent 66249ba1da
commit b5285f687a
2 changed files with 52 additions and 12 deletions

View file

@ -3,6 +3,7 @@
# 4.0.4 # 4.0.4
- Fix cheats "x" button - Fix cheats "x" button
- Optimize memory usage - Optimize memory usage
- Added ability to set urls to blob/arraybuffer/uint8array if needed
# 4.0.3 [View Tree](https://github.com/EmulatorJS/EmulatorJS/tree/5219ab51227bc0fb60cbc7b60e476b0145c932c9) # 4.0.3 [View Tree](https://github.com/EmulatorJS/EmulatorJS/tree/5219ab51227bc0fb60cbc7b60e476b0145c932c9)
- Remove RetroArch messages - Remove RetroArch messages

View file

@ -92,6 +92,19 @@ class EmulatorJS {
} }
} }
downloadFile(path, cb, progressCB, notWithPath, opts) { downloadFile(path, cb, progressCB, notWithPath, opts) {
const data = this.toData(path);//check other data types
if (data) {
data.then((game) => {
if (opts.method === 'HEAD') {
cb({headers:{}});
return;
} else {
cb({headers:{}, data:game});
return;
}
})
return;
}
const basePath = notWithPath ? '' : this.config.dataPath; const basePath = notWithPath ? '' : this.config.dataPath;
path = basePath + path; path = basePath + path;
if (!notWithPath && this.config.filePaths) { if (!notWithPath && this.config.filePaths) {
@ -156,6 +169,20 @@ class EmulatorJS {
})(); })();
} }
} }
toData(data, rv) {
if (!(data instanceof ArrayBuffer) && !(data instanceof Uint8Array) && !(data instanceof Blob)) return null;
if (rv) return true;
return new Promise(async (resolve) => {
if (data instanceof ArrayBuffer) {
resolve(new Uint8Array(data));
} else if (data instanceof Uint8Array) {
resolve(data);
} else if (data instanceof Blob) {
resolve(new Uint8Array(await data.arrayBuffer()));
}
resolve();
})
}
checkForUpdates() { checkForUpdates() {
fetch('https://raw.githack.com/EmulatorJS/EmulatorJS/main/data/version.json').then(response => { fetch('https://raw.githack.com/EmulatorJS/EmulatorJS/main/data/version.json').then(response => {
if (response.ok) { if (response.ok) {
@ -594,7 +621,7 @@ class EmulatorJS {
} }
downloadStartState() { downloadStartState() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof this.config.loadState !== "string") { if (typeof this.config.loadState !== "string" && !this.toData(this.config.loadState, true)) {
resolve(); resolve();
return; return;
} }
@ -619,7 +646,7 @@ class EmulatorJS {
} }
downloadGamePatch() { downloadGamePatch() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof this.config.gamePatchUrl !== "string" || !this.config.gamePatchUrl.trim()) { if ((typeof this.config.gamePatchUrl !== "string" || !this.config.gamePatchUrl.trim()) && !this.toData(this.config.gamePatchUrl, true)) {
resolve(); resolve();
return; return;
} }
@ -650,9 +677,12 @@ class EmulatorJS {
this.textElem.style.color = "red"; this.textElem.style.color = "red";
return; return;
} }
if (this.toData(this.config.gamePatchUrl, true)) {
this.config.gamePatchUrl = "game";
}
gotData(res.data); gotData(res.data);
const limit = (typeof this.config.cacheLimit === "number") ? this.config.cacheLimit : 1073741824; const limit = (typeof this.config.cacheLimit === "number") ? this.config.cacheLimit : 1073741824;
if (parseFloat(res.headers['content-length']) < limit && this.saveInBrowserSupported()) { if (parseFloat(res.headers['content-length']) < limit && this.saveInBrowserSupported() && this.config.gamePatchUrl !== "game") {
this.storage.rom.put(this.config.gamePatchUrl.split("/").pop(), { this.storage.rom.put(this.config.gamePatchUrl.split("/").pop(), {
"content-length": res.headers['content-length'], "content-length": res.headers['content-length'],
data: res.data data: res.data
@ -667,7 +697,7 @@ class EmulatorJS {
} }
downloadGameParent() { downloadGameParent() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof this.config.gameParentUrl !== "string" || !this.config.gameParentUrl.trim()) { if ((typeof this.config.gameParentUrl !== "string" || !this.config.gameParentUrl.trim()) && !this.toData(this.config.gameParentUrl, true)) {
resolve(); resolve();
return; return;
} }
@ -698,9 +728,12 @@ class EmulatorJS {
this.textElem.style.color = "red"; this.textElem.style.color = "red";
return; return;
} }
if (this.toData(this.config.gameParentUrl, true)) {
this.config.gameParentUrl = "game";
}
gotData(res.data); gotData(res.data);
const limit = (typeof this.config.cacheLimit === "number") ? this.config.cacheLimit : 1073741824; const limit = (typeof this.config.cacheLimit === "number") ? this.config.cacheLimit : 1073741824;
if (parseFloat(res.headers['content-length']) < limit && this.saveInBrowserSupported()) { if (parseFloat(res.headers['content-length']) < limit && this.saveInBrowserSupported() && this.config.gameParentUrl !== "game") {
this.storage.rom.put(this.config.gameParentUrl.split("/").pop(), { this.storage.rom.put(this.config.gameParentUrl.split("/").pop(), {
"content-length": res.headers['content-length'], "content-length": res.headers['content-length'],
data: res.data data: res.data
@ -715,7 +748,7 @@ class EmulatorJS {
} }
downloadBios() { downloadBios() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof this.config.biosUrl !== "string" || !this.config.biosUrl.trim()) { if ((typeof this.config.biosUrl !== "string" || !this.config.biosUrl.trim()) && !this.toData(this.config.biosUrl, true)) {
resolve(); resolve();
return; return;
} }
@ -751,8 +784,11 @@ class EmulatorJS {
this.textElem.style.color = "red"; this.textElem.style.color = "red";
return; return;
} }
if (this.toData(this.config.biosUrl, true)) {
this.config.biosUrl = "game";
}
gotBios(res.data); gotBios(res.data);
if (this.saveInBrowserSupported()) { if (this.saveInBrowserSupported() && this.config.biosUrl !== "game") {
this.storage.bios.put(this.config.biosUrl.split("/").pop(), { this.storage.bios.put(this.config.biosUrl.split("/").pop(), {
"content-length": res.headers['content-length'], "content-length": res.headers['content-length'],
data: res.data data: res.data
@ -779,7 +815,7 @@ class EmulatorJS {
} }
let resData = {}; let resData = {};
const altName = this.config.gameUrl.startsWith("blob:") ? this.config.gameName || "game" : this.config.gameUrl.split('/').pop().split("#")[0].split("?")[0]; const altName = this.config.gameUrl.startsWith("blob:") ? (this.config.gameName || "game") : this.config.gameUrl.split('/').pop().split("#")[0].split("?")[0];
this.checkCompression(new Uint8Array(data), this.localization("Decompress Game Data"), (fileName, fileData) => { this.checkCompression(new Uint8Array(data), this.localization("Decompress Game Data"), (fileName, fileData) => {
if (fileName.includes("/")) { if (fileName.includes("/")) {
const paths = fileName.split("/"); const paths = fileName.split("/");
@ -844,15 +880,15 @@ class EmulatorJS {
}); });
} }
this.downloadFile(this.config.gameUrl, (res) => { this.downloadFile(this.config.gameUrl, (res) => {
if (res === -1) { if (res === -1) {
this.textElem.innerText = this.localization('Network Error'); this.textElem.innerText = this.localization('Network Error');
this.textElem.style.color = "red"; this.textElem.style.color = "red";
return; return;
} }
this.storage.rom.get(this.config.gameUrl.split("/").pop()).then((result) => { const name = (typeof this.config.gameUrl === "string") ? this.config.gameUrl.split('/').pop() : "game";
if (result && result['content-length'] === res.headers['content-length'] && !this.debug) { this.storage.rom.get(name).then((result) => {
if (result && result['content-length'] === res.headers['content-length'] && !this.debug && name !== "game") {
gotGameData(result.data); gotGameData(result.data);
return; return;
} }
@ -862,9 +898,12 @@ class EmulatorJS {
this.textElem.style.color = "red"; this.textElem.style.color = "red";
return; return;
} }
if (this.toData(this.config.gameUrl, true)) {
this.config.gameUrl = "game";
}
gotGameData(res.data); gotGameData(res.data);
const limit = (typeof this.config.cacheLimit === "number") ? this.config.cacheLimit : 1073741824; const limit = (typeof this.config.cacheLimit === "number") ? this.config.cacheLimit : 1073741824;
if (parseFloat(res.headers['content-length']) < limit && this.saveInBrowserSupported()) { if (parseFloat(res.headers['content-length']) < limit && this.saveInBrowserSupported() && this.config.gameUrl !== "game") {
this.storage.rom.put(this.config.gameUrl.split("/").pop(), { this.storage.rom.put(this.config.gameUrl.split("/").pop(), {
"content-length": res.headers['content-length'], "content-length": res.headers['content-length'],
data: res.data data: res.data