mirror of
https://github.com/EmulatorJS/EmulatorJS.git
synced 2026-02-06 11:17:36 +00:00
Remove legacy download methods
This commit is contained in:
parent
d6b6952761
commit
5ff7245936
@ -885,6 +885,12 @@ class EmulatorJS {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Download a file, with caching and File object support
|
||||
* @param {*} url The URL or File object to download
|
||||
* @param {*} type The download type (from this.downloadType)
|
||||
* @returns
|
||||
*/
|
||||
download(url, type) {
|
||||
if (url === undefined || url === null || url === "") {
|
||||
if (this.debug) console.log("[EJS " + type.name.toUpperCase() + "] No URL provided, skipping download.");
|
||||
@ -1019,281 +1025,9 @@ class EmulatorJS {
|
||||
resolve(returnData);
|
||||
});
|
||||
}
|
||||
|
||||
downloadGameFile(assetUrl, type, progressMessage, decompressProgressMessage) {
|
||||
if (assetUrl === undefined || assetUrl === null) {
|
||||
return new Promise((resolve) => {
|
||||
resolve(assetUrl);
|
||||
});
|
||||
}
|
||||
console.log("[EJS " + type.name.toUpperCase() + "] Requested download for " + assetUrl);
|
||||
return new Promise(async (resolve, reject) => {
|
||||
if ((typeof assetUrl !== "string" || !assetUrl.trim()) && !this.toData(assetUrl, true) && !(assetUrl instanceof File)) {
|
||||
return resolve(assetUrl);
|
||||
}
|
||||
const gotData = async (input) => {
|
||||
const coreFilename = "/" + this.fileName;
|
||||
const coreFilePath = coreFilename.substring(0, coreFilename.length - coreFilename.split("/").pop().length);
|
||||
if (this.config.dontExtractBIOS === true) {
|
||||
this.gameManager.FS.writeFile(coreFilePath + assetUrl.split("/").pop(), new Uint8Array(input));
|
||||
return resolve(assetUrl);
|
||||
}
|
||||
|
||||
// Decompress if needed
|
||||
if (!this.compression) {
|
||||
this.compression = new window.EJS_COMPRESSION(this);
|
||||
}
|
||||
|
||||
const assetFilename = assetUrl.split("/").pop().split("#")[0].split("?")[0];
|
||||
this.textElem.innerText = decompressProgressMessage;
|
||||
|
||||
const data = await this.compression.decompress(new Uint8Array(input), (m, appendMsg) => {
|
||||
this.textElem.innerText = appendMsg ? (decompressProgressMessage + m) : m;
|
||||
}, null);
|
||||
|
||||
for (const k in data) {
|
||||
if (k === "!!notCompressedData") {
|
||||
this.gameManager.FS.writeFile(coreFilePath + assetUrl.split("/").pop().split("#")[0].split("?")[0], data[k]);
|
||||
break;
|
||||
}
|
||||
if (k.endsWith("/")) continue;
|
||||
this.gameManager.FS.writeFile(coreFilePath + k.split("/").pop(), data[k]);
|
||||
}
|
||||
}
|
||||
|
||||
console.log("[EJS " + type.name.toUpperCase() + "] Downloading " + assetUrl);
|
||||
this.textElem.innerText = progressMessage;
|
||||
|
||||
const res = await this.downloadFile(assetUrl, type.name, (progress) => {
|
||||
this.textElem.innerText = progressMessage + progress;
|
||||
}, true, { responseType: "arraybuffer", method: "GET" }, false, type.dontCache);
|
||||
if (res === -1) {
|
||||
this.startGameError(this.localization("Network Error"));
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
if (assetUrl instanceof File) {
|
||||
assetUrl = assetUrl.name;
|
||||
} else if (this.toData(assetUrl, true)) {
|
||||
assetUrl = "game";
|
||||
}
|
||||
await gotData(res.data);
|
||||
resolve(assetUrl);
|
||||
console.log("[EJS " + type.name.toUpperCase() + "] Download and decompression complete");
|
||||
});
|
||||
}
|
||||
downloadGamePatch() {
|
||||
return new Promise(async (resolve) => {
|
||||
this.config.gamePatchUrl = await this.downloadGameFile(this.config.gamePatchUrl, this.downloadType.patch, this.localization("Download Game Patch"), this.localization("Decompress Game Patch"));
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
downloadGameParent() {
|
||||
return new Promise(async (resolve) => {
|
||||
this.config.gameParentUrl = await this.downloadGameFile(this.config.gameParentUrl, this.downloadType.parent, this.localization("Download Game Parent"), this.localization("Decompress Game Parent"));
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
downloadBios() {
|
||||
return new Promise(async (resolve) => {
|
||||
this.config.biosUrl = await this.downloadGameFile(this.config.biosUrl, this.downloadType.bios, this.localization("Download Game BIOS"), this.localization("Decompress Game BIOS"));
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
downloadRom() {
|
||||
const supportsExt = (ext) => {
|
||||
const core = this.getCore();
|
||||
if (!this.extensions) return false;
|
||||
return this.extensions.includes(ext);
|
||||
};
|
||||
|
||||
return new Promise(resolve => {
|
||||
this.textElem.innerText = this.localization("Download Game Data");
|
||||
|
||||
// List of cores to generate a CUE file for, if it doesn't exist.
|
||||
const cueGeneration = ["mednafen_psx_hw"];
|
||||
const prioritizeExtensions = ["cue", "ccd", "toc", "m3u"];
|
||||
|
||||
let createCueFile = cueGeneration.includes(this.getCore());
|
||||
if (this.config.disableCue === true) {
|
||||
createCueFile = false;
|
||||
}
|
||||
|
||||
const writeFilesToFS = (fileName, fileData, altName) => {
|
||||
if (fileName.includes("/")) {
|
||||
const paths = fileName.split("/");
|
||||
let cp = "";
|
||||
for (let i = 0; i < paths.length - 1; i++) {
|
||||
if (paths[i] === "") continue;
|
||||
cp += `/${paths[i]}`;
|
||||
if (!this.gameManager.FS.analyzePath(cp).exists) {
|
||||
this.gameManager.FS.mkdir(cp);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (fileName.endsWith("/")) {
|
||||
this.gameManager.FS.mkdir(fileName);
|
||||
return null;
|
||||
}
|
||||
if (fileName === "!!notCompressedData") {
|
||||
this.gameManager.FS.writeFile(altName, fileData);
|
||||
return altName;
|
||||
} else {
|
||||
this.gameManager.FS.writeFile(`/${fileName}`, fileData);
|
||||
return fileName;
|
||||
}
|
||||
};
|
||||
|
||||
const selectRomFile = (fileNames, coreName, disableCue) => {
|
||||
let isoFile = null;
|
||||
let supportedFile = null;
|
||||
let cueFile = null;
|
||||
let selectedCueExt = null;
|
||||
fileNames.forEach(fileName => {
|
||||
const ext = fileName.split(".").pop().toLowerCase();
|
||||
if (supportedFile === null && supportsExt(ext)) {
|
||||
supportedFile = fileName;
|
||||
}
|
||||
if (isoFile === null && ["iso", "cso", "chd", "elf"].includes(ext)) {
|
||||
isoFile = fileName;
|
||||
}
|
||||
if (prioritizeExtensions.includes(ext)) {
|
||||
const currentCueExt = (cueFile === null) ? null : cueFile.split(".").pop().toLowerCase();
|
||||
if (coreName === "psx") {
|
||||
// Always prefer m3u files for psx cores
|
||||
if (currentCueExt !== "m3u") {
|
||||
if (cueFile === null || ext === "m3u") {
|
||||
cueFile = fileName;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const priority = ["cue", "ccd"]
|
||||
// Prefer cue or ccd files over toc or m3u
|
||||
if (!priority.includes(currentCueExt)) {
|
||||
if (cueFile === null || priority.includes(ext)) {
|
||||
cueFile = fileName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
if (supportedFile !== null) {
|
||||
this.fileName = supportedFile;
|
||||
} else {
|
||||
this.fileName = fileNames[0];
|
||||
}
|
||||
if (isoFile !== null && supportsExt(isoFile.split(".").pop().toLowerCase())) {
|
||||
this.fileName = isoFile;
|
||||
}
|
||||
if (cueFile !== null && supportsExt(cueFile.split(".").pop().toLowerCase())) {
|
||||
this.fileName = cueFile;
|
||||
} else if (createCueFile && supportsExt("m3u") && supportsExt("cue")) {
|
||||
this.fileName = this.gameManager.createCueFile(fileNames);
|
||||
}
|
||||
if (this.getCore(true) === "dos" && !this.config.disableBatchBootup) {
|
||||
this.fileName = this.gameManager.writeBootupBatchFile();
|
||||
}
|
||||
resolve();
|
||||
};
|
||||
|
||||
const gotGameData = async (data) => {
|
||||
const coreName = this.getCore(true);
|
||||
const altName = this.getBaseFileName(true);
|
||||
|
||||
if (["arcade", "mame"].includes(coreName) || this.config.dontExtractRom === true) {
|
||||
this.fileName = altName;
|
||||
this.gameManager.FS.writeFile(this.fileName, new Uint8Array(data));
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
let disableCue = false;
|
||||
if (["pcsx_rearmed", "genesis_plus_gx", "picodrive", "mednafen_pce", "smsplus", "vice_x64", "vice_x64sc", "vice_x128", "vice_xvic", "vice_xpet", "puae"].includes(coreName) && this.config.disableCue === undefined) {
|
||||
disableCue = true;
|
||||
console.log("DISABLING CUE!");
|
||||
} else {
|
||||
disableCue = this.config.disableCue;
|
||||
}
|
||||
|
||||
const romFilename = this.getBaseFileName(true);
|
||||
const cacheKey = `rom-${coreName}-${romFilename}`;
|
||||
|
||||
// Check the cache for decompressed content
|
||||
const cached = await this.storageCache.get(cacheKey, false);
|
||||
|
||||
if (cached && cached.files) {
|
||||
// Cache hit - write cached files to FS
|
||||
if (this.debug) console.log("[EJS ROM] Using cached decompressed files");
|
||||
const fileNames = [];
|
||||
for (const file of cached.files) {
|
||||
const writtenName = writeFilesToFS(file.filename, file.bytes, altName);
|
||||
if (writtenName) {
|
||||
fileNames.push(writtenName);
|
||||
}
|
||||
}
|
||||
selectRomFile(fileNames, coreName, disableCue);
|
||||
return;
|
||||
}
|
||||
|
||||
// Cache miss - decompress and cache the result
|
||||
let fileNames = [];
|
||||
|
||||
if (!this.compression) {
|
||||
this.compression = new window.EJS_COMPRESSION(this);
|
||||
}
|
||||
|
||||
this.compression.decompress(new Uint8Array(data), (m, appendMsg) => {
|
||||
this.textElem.innerText = appendMsg ? (this.localization("Decompress Game Data") + m) : m;
|
||||
}, (fileName, fileData) => {
|
||||
const writtenName = writeFilesToFS(fileName, fileData, altName);
|
||||
if (writtenName) {
|
||||
fileNames.push(writtenName);
|
||||
}
|
||||
}).then(async (decompressedFiles) => {
|
||||
// Store decompressed files in cache
|
||||
const cacheFiles = fileNames.map(name => {
|
||||
const data = this.gameManager.FS.readFile(`/${name}`);
|
||||
return new window.EJS_FileItem(name, data);
|
||||
});
|
||||
const cacheItem = new window.EJS_CacheItem(
|
||||
cacheKey,
|
||||
cacheFiles,
|
||||
Date.now(),
|
||||
this.downloadType.rom.name,
|
||||
"arraybuffer",
|
||||
romFilename,
|
||||
romFilename,
|
||||
Date.now() + 5 * 24 * 60 * 60 * 1000
|
||||
);
|
||||
await this.storageCache.put(cacheItem);
|
||||
|
||||
selectRomFile(fileNames, coreName, disableCue);
|
||||
return;
|
||||
});
|
||||
}
|
||||
const downloadFile = async () => {
|
||||
console.log("[EJS ROM] Downloading ROM " + this.config.gameUrl.name);
|
||||
// create new download class
|
||||
const res = await this.downloadFile(this.config.gameUrl, this.downloadType.rom.name, (progress) => {
|
||||
this.textElem.innerText = this.localization("Download Game Data") + progress;
|
||||
}, true, { responseType: "arraybuffer", method: "GET" }, false, this.downloadType.rom.dontCache);
|
||||
if (res === -1) {
|
||||
this.startGameError(this.localization("Network Error"));
|
||||
return;
|
||||
}
|
||||
if (this.config.gameUrl instanceof File) {
|
||||
this.config.gameUrl = this.config.gameUrl.name;
|
||||
} else if (this.toData(this.config.gameUrl, true)) {
|
||||
this.config.gameUrl = "game";
|
||||
}
|
||||
gotGameData(res.data);
|
||||
if (this.debug) console.log("[EJS ROM] Download and decompression complete");
|
||||
}
|
||||
|
||||
// download the content
|
||||
downloadFile();
|
||||
})
|
||||
}
|
||||
/**
|
||||
* Download all necessary files and start the game
|
||||
*/
|
||||
downloadFiles() {
|
||||
(async () => {
|
||||
this.gameManager = new window.EJS_GameManager(this.Module, this);
|
||||
@ -1308,11 +1042,6 @@ class EmulatorJS {
|
||||
await this.downloadStartState();
|
||||
const parentData = await this.download(this.config.gameParentUrl, this.downloadType.parent);
|
||||
const patchData = await this.download(this.config.gamePatchUrl, this.downloadType.patch);
|
||||
// await this.downloadRom();
|
||||
// await this.downloadBios();
|
||||
// await this.downloadStartState();
|
||||
// await this.downloadGameParent();
|
||||
// await this.downloadGamePatch();
|
||||
|
||||
// Select rom file for start up
|
||||
// List of cores to generate a CUE file for, if it doesn't exist.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user