EmulatorJS/data/loader.js

159 lines
6.9 KiB
JavaScript
Raw Normal View History

(async function() {
2024-07-27 20:00:29 +00:00
const scripts = [
"emulator.js",
"nipplejs.js",
"shaders.js",
"storage.js",
"gamepad.js",
"GameManager.js",
"socket.io.min.js"
];
const folderPath = (path) => path.substring(0, path.length - path.split('/').pop().length);
2023-08-26 15:49:05 +00:00
let scriptPath = (typeof window.EJS_pathtodata === "string") ? window.EJS_pathtodata : folderPath((new URL(document.currentScript.src)).pathname);
if (!scriptPath.endsWith('/')) scriptPath+='/';
//console.log(scriptPath);
function loadScript(file) {
return new Promise(function (resolve, reject) {
let script = document.createElement('script');
script.src = function() {
2023-07-03 15:49:28 +00:00
if ('undefined' != typeof EJS_paths && typeof EJS_paths[file] === 'string') {
return EJS_paths[file];
2024-07-27 20:05:54 +00:00
} else if (file.endsWith("emulator.min.js")) {
2024-07-27 20:00:29 +00:00
return scriptPath + file;
} else {
2024-07-27 20:00:29 +00:00
return scriptPath + "src/" + file;
}
}();
script.onload = resolve;
2024-01-16 18:39:16 +00:00
script.onerror = () => {
filesmissing(file).then(e => resolve());
}
document.head.appendChild(script);
})
}
function loadStyle(file) {
return new Promise(function(resolve, reject) {
let css = document.createElement('link');
css.rel = 'stylesheet';
css.href = function() {
2023-07-03 15:49:28 +00:00
if ('undefined' != typeof EJS_paths && typeof EJS_paths[file] === 'string') {
return EJS_paths[file];
} else {
return scriptPath+file;
}
}();
css.onload = resolve;
2024-01-16 18:39:16 +00:00
css.onerror = () => {
filesmissing(file).then(e => resolve());
}
document.head.appendChild(css);
})
}
2024-01-16 18:39:16 +00:00
async function filesmissing(file) {
console.error("Failed to load " + file);
let minifiedFailed = file.includes(".min.") && !file.includes("socket");
console[minifiedFailed?"warn":"error"]("Failed to load " + file + " beacuse it's likly that the minified files are missing.\nTo fix this you have 3 options:\n1. You can download the zip from the latest release here: https://github.com/EmulatorJS/EmulatorJS/releases/latest - Stable\n2. You can download the zip from here: https://cdn.emulatorjs.org/latest/data/emulator.min.zip and extract it to the data/ folder. (easiest option) - Beta\n3. You can build the files by running `npm i && npm run build` in the data/minify folder. (hardest option) - Beta\nNote: you will probably need to do the same for the cores, extract them to the data/cores/ folder.");
if (minifiedFailed) {
console.log("Attempting to load non-minified files");
if (file === "emulator.min.js") {
2024-07-27 20:00:29 +00:00
for (let i=0; i<scripts.length; i++) {
await loadScript(scripts[i]);
}
2024-01-16 18:39:16 +00:00
} else {
await loadStyle('emulator.css');
}
}
}
2023-07-18 16:10:21 +00:00
if (('undefined' != typeof EJS_DEBUG_XX && true === EJS_DEBUG_XX)) {
2024-07-27 20:00:29 +00:00
for (let i=0; i<scripts.length; i++) {
await loadScript(scripts[i]);
}
2023-07-18 15:56:27 +00:00
await loadStyle('emulator.css');
2023-07-18 16:10:21 +00:00
} else {
await loadScript('emulator.min.js');
await loadStyle('emulator.min.css');
}
const config = {};
2023-06-26 16:39:31 +00:00
config.gameUrl = window.EJS_gameUrl;
config.dataPath = scriptPath;
2023-06-26 16:39:31 +00:00
config.system = window.EJS_core;
config.biosUrl = window.EJS_biosUrl;
2023-07-01 18:15:26 +00:00
config.gameName = window.EJS_gameName;
config.color = window.EJS_color;
2023-07-01 18:59:39 +00:00
config.adUrl = window.EJS_AdUrl;
config.adMode = window.EJS_AdMode;
2023-07-18 15:56:27 +00:00
config.adTimer = window.EJS_AdTimer;
config.adSize = window.EJS_AdSize;
2023-08-14 23:19:17 +00:00
config.alignStartButton = window.EJS_alignStartButton;
2023-07-01 19:06:16 +00:00
config.VirtualGamepadSettings = window.EJS_VirtualGamepadSettings;
2023-07-01 19:15:00 +00:00
config.buttonOpts = window.EJS_Buttons;
2023-07-03 14:34:48 +00:00
config.volume = window.EJS_volume;
2023-07-03 15:49:28 +00:00
config.defaultControllers = window.EJS_defaultControls;
config.startOnLoad = window.EJS_startOnLoaded;
config.fullscreenOnLoad = window.EJS_fullscreenOnLoaded;
2023-07-03 15:49:28 +00:00
config.filePaths = window.EJS_paths;
2023-07-03 16:01:26 +00:00
config.loadState = window.EJS_loadStateURL;
2023-07-03 16:16:44 +00:00
config.cacheLimit = window.EJS_CacheLimit;
2023-07-03 16:25:22 +00:00
config.cheats = window.EJS_cheats;
2023-07-03 16:43:59 +00:00
config.defaultOptions = window.EJS_defaultOptions;
2023-07-11 16:30:27 +00:00
config.gamePatchUrl = window.EJS_gamePatchUrl;
config.gameParentUrl = window.EJS_gameParentUrl;
2023-07-15 20:10:56 +00:00
config.netplayUrl = window.EJS_netplayServer;
2023-07-17 13:07:52 +00:00
config.gameId = window.EJS_gameID;
2023-07-18 14:59:31 +00:00
config.backgroundImg = window.EJS_backgroundImage;
config.backgroundBlur = window.EJS_backgroundBlur;
config.backgroundColor = window.EJS_backgroundColor;
config.controlScheme = window.EJS_controlScheme;
2023-08-24 16:57:46 +00:00
config.threads = window.EJS_threads;
2023-08-30 18:42:50 +00:00
config.disableCue = window.EJS_disableCue;
config.startBtnName = window.EJS_startButtonName;
2023-10-03 00:41:16 +00:00
config.softLoad = window.EJS_softLoad;
config.screenRecording = window.EJS_screenRecording;
config.externalFiles = window.EJS_externalFiles;
2024-02-01 19:31:36 +00:00
config.disableDatabases = window.EJS_disableDatabases;
2024-02-05 18:36:19 +00:00
config.disableLocalStorage = window.EJS_disableLocalStorage;
2024-02-18 18:13:19 +00:00
config.forceLegacyCores = window.EJS_forceLegacyCores;
2024-02-28 18:32:27 +00:00
config.noAutoFocus = window.EJS_noAutoFocus;
config.shaders = Object.assign({}, window.EJS_SHADERS, window.EJS_shaders ? window.EJS_shaders : {});
2023-07-03 16:37:43 +00:00
if (typeof window.EJS_language === "string" && window.EJS_language !== "en-US") {
try {
let path;
if ('undefined' != typeof EJS_paths && typeof EJS_paths[window.EJS_language] === 'string') {
path = EJS_paths[window.EJS_language];
} else {
path = scriptPath+"localization/"+window.EJS_language+".json";
}
config.language = window.EJS_language;
config.langJson = JSON.parse(await (await fetch(path)).text());
} catch(e) {
config.langJson = {};
}
}
2023-07-18 15:56:27 +00:00
window.EJS_emulator = new EmulatorJS(EJS_player, config);
window.EJS_adBlocked = (url, del) => window.EJS_emulator.adBlocked(url, del);
if (typeof window.EJS_ready === "function") {
window.EJS_emulator.on("ready", window.EJS_ready);
}
2023-07-18 15:56:27 +00:00
if (typeof window.EJS_onGameStart === "function") {
window.EJS_emulator.on("start", window.EJS_onGameStart);
}
if (typeof window.EJS_onLoadState === "function") {
window.EJS_emulator.on("loadState", window.EJS_onLoadState);
2023-07-18 15:56:27 +00:00
}
if (typeof window.EJS_onSaveState === "function") {
window.EJS_emulator.on("saveState", window.EJS_onSaveState);
}
if (typeof window.EJS_onLoadSave === "function") {
window.EJS_emulator.on("loadSave", window.EJS_onLoadSave);
}
if (typeof window.EJS_onSaveSave === "function") {
window.EJS_emulator.on("saveSave", window.EJS_onSaveSave);
2023-07-18 15:56:27 +00:00
}
2023-08-24 16:57:46 +00:00
})();