2020-11-23 12:13:52 +01:00
|
|
|
const Engine = (function () {
|
|
|
|
const preloader = new Preloader();
|
2020-03-11 11:55:28 +01:00
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
let wasmExt = '.wasm';
|
|
|
|
let unloadAfterInit = true;
|
|
|
|
let loadPath = '';
|
|
|
|
let loadPromise = null;
|
|
|
|
let initPromise = null;
|
|
|
|
let stderr = null;
|
|
|
|
let stdout = null;
|
|
|
|
let progressFunc = null;
|
2020-03-11 11:55:28 +01:00
|
|
|
|
|
|
|
function load(basePath) {
|
|
|
|
if (loadPromise == null) {
|
|
|
|
loadPath = basePath;
|
|
|
|
loadPromise = preloader.loadPromise(basePath + wasmExt);
|
|
|
|
preloader.setProgressFunc(progressFunc);
|
|
|
|
requestAnimationFrame(preloader.animateProgress);
|
|
|
|
}
|
|
|
|
return loadPromise;
|
2020-11-23 12:13:52 +01:00
|
|
|
}
|
2020-03-11 11:55:28 +01:00
|
|
|
|
|
|
|
function unload() {
|
|
|
|
loadPromise = null;
|
2020-11-23 12:13:52 +01:00
|
|
|
}
|
2020-03-11 11:55:28 +01:00
|
|
|
|
|
|
|
/** @constructor */
|
2020-11-19 16:54:07 +01:00
|
|
|
function Engine() { // eslint-disable-line no-shadow
|
2020-05-01 14:36:41 +02:00
|
|
|
this.canvas = null;
|
|
|
|
this.executableName = '';
|
|
|
|
this.rtenv = null;
|
|
|
|
this.customLocale = null;
|
|
|
|
this.resizeCanvasOnStart = false;
|
2020-05-01 14:45:45 +02:00
|
|
|
this.onExecute = null;
|
2020-05-01 14:36:41 +02:00
|
|
|
this.onExit = null;
|
2020-10-23 18:33:20 +02:00
|
|
|
this.persistentPaths = ['/userfs'];
|
2020-12-05 00:37:41 +01:00
|
|
|
this.gdnativeLibs = [];
|
2020-11-23 12:13:52 +01:00
|
|
|
}
|
2020-03-11 11:55:28 +01:00
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.init = /** @param {string=} basePath */ function (basePath) {
|
2020-03-11 11:55:28 +01:00
|
|
|
if (initPromise) {
|
|
|
|
return initPromise;
|
|
|
|
}
|
2020-05-01 14:36:41 +02:00
|
|
|
if (loadPromise == null) {
|
2020-03-11 11:55:28 +01:00
|
|
|
if (!basePath) {
|
2020-11-23 12:13:52 +01:00
|
|
|
initPromise = Promise.reject(new Error('A base path must be provided when calling `init` and the engine is not loaded.'));
|
2020-03-11 11:55:28 +01:00
|
|
|
return initPromise;
|
|
|
|
}
|
|
|
|
load(basePath);
|
|
|
|
}
|
2020-11-23 12:13:52 +01:00
|
|
|
let config = {};
|
|
|
|
if (typeof stdout === 'function') {
|
2020-03-11 11:55:28 +01:00
|
|
|
config.print = stdout;
|
2020-11-23 12:13:52 +01:00
|
|
|
}
|
|
|
|
if (typeof stderr === 'function') {
|
2020-03-11 11:55:28 +01:00
|
|
|
config.printErr = stderr;
|
2020-11-23 12:13:52 +01:00
|
|
|
}
|
|
|
|
const me = this;
|
|
|
|
initPromise = new Promise(function (resolve, reject) {
|
2020-05-01 14:36:41 +02:00
|
|
|
config['locateFile'] = Utils.createLocateRewrite(loadPath);
|
|
|
|
config['instantiateWasm'] = Utils.createInstantiatePromise(loadPromise);
|
2020-10-24 16:02:09 +02:00
|
|
|
// Emscripten configuration.
|
|
|
|
config['thisProgram'] = me.executableName;
|
|
|
|
config['noExitRuntime'] = true;
|
2021-01-25 04:48:11 +01:00
|
|
|
config['dynamicLibraries'] = [`${me.executableName}.side.wasm`].concat(me.gdnativeLibs);
|
2020-11-23 12:13:52 +01:00
|
|
|
Godot(config).then(function (module) {
|
|
|
|
module['initFS'](me.persistentPaths).then(function (fs_err) {
|
2020-09-18 17:25:05 +02:00
|
|
|
me.rtenv = module;
|
|
|
|
if (unloadAfterInit) {
|
|
|
|
unload();
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
config = null;
|
|
|
|
});
|
2020-03-11 11:55:28 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return initPromise;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @type {function(string, string):Object} */
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.preloadFile = function (file, path) {
|
2020-03-11 11:55:28 +01:00
|
|
|
return preloader.preload(file, path);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @type {function(...string):Object} */
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.start = function () {
|
2020-03-11 11:55:28 +01:00
|
|
|
// Start from arguments.
|
2020-11-23 12:13:52 +01:00
|
|
|
const args = [];
|
|
|
|
for (let i = 0; i < arguments.length; i++) {
|
2020-03-11 11:55:28 +01:00
|
|
|
args.push(arguments[i]);
|
|
|
|
}
|
2020-11-23 12:13:52 +01:00
|
|
|
const me = this;
|
|
|
|
return me.init().then(function () {
|
2020-05-01 14:36:41 +02:00
|
|
|
if (!me.rtenv) {
|
2020-05-08 16:55:01 +02:00
|
|
|
return Promise.reject(new Error('The engine must be initialized before it can be started'));
|
2020-05-01 14:36:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(me.canvas instanceof HTMLCanvasElement)) {
|
|
|
|
me.canvas = Utils.findCanvas();
|
2020-11-19 16:54:07 +01:00
|
|
|
if (!me.canvas) {
|
|
|
|
return Promise.reject(new Error('No canvas found in page'));
|
|
|
|
}
|
2020-05-01 14:36:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Canvas can grab focus on click, or key events won't work.
|
|
|
|
if (me.canvas.tabIndex < 0) {
|
|
|
|
me.canvas.tabIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Browser locale, or custom one if defined.
|
2020-11-23 12:13:52 +01:00
|
|
|
let locale = me.customLocale;
|
2020-05-01 14:36:41 +02:00
|
|
|
if (!locale) {
|
|
|
|
locale = navigator.languages ? navigator.languages[0] : navigator.language;
|
|
|
|
locale = locale.split('.')[0];
|
|
|
|
}
|
2020-10-23 18:33:20 +02:00
|
|
|
// Godot configuration.
|
|
|
|
me.rtenv['initConfig']({
|
|
|
|
'resizeCanvasOnStart': me.resizeCanvasOnStart,
|
|
|
|
'canvas': me.canvas,
|
|
|
|
'locale': locale,
|
2020-11-23 12:13:52 +01:00
|
|
|
'onExecute': function (p_args) {
|
2020-10-23 18:33:20 +02:00
|
|
|
if (me.onExecute) {
|
|
|
|
me.onExecute(p_args);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
},
|
2020-11-23 12:13:52 +01:00
|
|
|
'onExit': function (p_code) {
|
2020-10-23 18:33:20 +02:00
|
|
|
me.rtenv['deinitFS']();
|
|
|
|
if (me.onExit) {
|
|
|
|
me.onExit(p_code);
|
|
|
|
}
|
|
|
|
me.rtenv = null;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
preloader.preloadedFiles.forEach(function (file) {
|
2020-05-08 16:55:01 +02:00
|
|
|
me.rtenv['copyToFS'](file.path, file.buffer);
|
2020-03-11 11:55:28 +01:00
|
|
|
});
|
2020-05-01 14:36:41 +02:00
|
|
|
preloader.preloadedFiles.length = 0; // Clear memory
|
|
|
|
me.rtenv['callMain'](args);
|
|
|
|
initPromise = null;
|
|
|
|
resolve();
|
2020-03-11 11:55:28 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.startGame = function (execName, mainPack, extraArgs) {
|
2020-03-11 11:55:28 +01:00
|
|
|
// Start and init with execName as loadPath if not inited.
|
2020-05-01 14:36:41 +02:00
|
|
|
this.executableName = execName;
|
2020-11-23 12:13:52 +01:00
|
|
|
const me = this;
|
2020-03-11 11:55:28 +01:00
|
|
|
return Promise.all([
|
|
|
|
this.init(execName),
|
2020-11-23 12:13:52 +01:00
|
|
|
this.preloadFile(mainPack, mainPack),
|
|
|
|
]).then(function () {
|
|
|
|
let args = ['--main-pack', mainPack];
|
|
|
|
if (extraArgs) {
|
2020-05-01 14:36:41 +02:00
|
|
|
args = args.concat(extraArgs);
|
2020-11-23 12:13:52 +01:00
|
|
|
}
|
2020-05-01 14:36:41 +02:00
|
|
|
return me.start.apply(me, args);
|
2020-03-11 11:55:28 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.setWebAssemblyFilenameExtension = function (override) {
|
2020-03-11 11:55:28 +01:00
|
|
|
if (String(override).length === 0) {
|
|
|
|
throw new Error('Invalid WebAssembly filename extension override');
|
|
|
|
}
|
|
|
|
wasmExt = String(override);
|
|
|
|
};
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.setUnloadAfterInit = function (enabled) {
|
2020-03-11 11:55:28 +01:00
|
|
|
unloadAfterInit = enabled;
|
|
|
|
};
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.setCanvas = function (canvasElem) {
|
2020-05-01 14:36:41 +02:00
|
|
|
this.canvas = canvasElem;
|
2020-03-11 11:55:28 +01:00
|
|
|
};
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.setCanvasResizedOnStart = function (enabled) {
|
2020-05-01 14:36:41 +02:00
|
|
|
this.resizeCanvasOnStart = enabled;
|
2020-03-11 11:55:28 +01:00
|
|
|
};
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.setLocale = function (locale) {
|
2020-05-01 14:36:41 +02:00
|
|
|
this.customLocale = locale;
|
2020-03-11 11:55:28 +01:00
|
|
|
};
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.setExecutableName = function (newName) {
|
2020-05-01 14:36:41 +02:00
|
|
|
this.executableName = newName;
|
2020-03-11 11:55:28 +01:00
|
|
|
};
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.setProgressFunc = function (func) {
|
2020-03-11 11:55:28 +01:00
|
|
|
progressFunc = func;
|
2020-05-01 14:36:41 +02:00
|
|
|
};
|
2020-03-11 11:55:28 +01:00
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.setStdoutFunc = function (func) {
|
|
|
|
const print = function (text) {
|
2020-11-19 16:54:07 +01:00
|
|
|
let msg = text;
|
2020-03-11 11:55:28 +01:00
|
|
|
if (arguments.length > 1) {
|
2020-11-23 12:13:52 +01:00
|
|
|
msg = Array.prototype.slice.call(arguments).join(' ');
|
2020-03-11 11:55:28 +01:00
|
|
|
}
|
2020-11-19 16:54:07 +01:00
|
|
|
func(msg);
|
2020-03-11 11:55:28 +01:00
|
|
|
};
|
2020-11-23 12:13:52 +01:00
|
|
|
if (this.rtenv) {
|
2020-05-01 14:36:41 +02:00
|
|
|
this.rtenv.print = print;
|
2020-11-23 12:13:52 +01:00
|
|
|
}
|
2020-03-11 11:55:28 +01:00
|
|
|
stdout = print;
|
|
|
|
};
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.setStderrFunc = function (func) {
|
|
|
|
const printErr = function (text) {
|
|
|
|
let msg = text;
|
2020-11-19 16:54:07 +01:00
|
|
|
if (arguments.length > 1) {
|
2020-11-23 12:13:52 +01:00
|
|
|
msg = Array.prototype.slice.call(arguments).join(' ');
|
2020-11-19 16:54:07 +01:00
|
|
|
}
|
|
|
|
func(msg);
|
2020-03-11 11:55:28 +01:00
|
|
|
};
|
2020-11-23 12:13:52 +01:00
|
|
|
if (this.rtenv) {
|
2020-05-01 14:36:41 +02:00
|
|
|
this.rtenv.printErr = printErr;
|
2020-11-23 12:13:52 +01:00
|
|
|
}
|
2020-03-11 11:55:28 +01:00
|
|
|
stderr = printErr;
|
|
|
|
};
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.setOnExecute = function (onExecute) {
|
2020-05-01 14:45:45 +02:00
|
|
|
this.onExecute = onExecute;
|
2020-09-09 16:13:55 +02:00
|
|
|
};
|
2020-05-01 14:45:45 +02:00
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.setOnExit = function (onExit) {
|
2020-05-01 14:36:41 +02:00
|
|
|
this.onExit = onExit;
|
2020-09-09 16:13:55 +02:00
|
|
|
};
|
2020-05-01 14:36:41 +02:00
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.copyToFS = function (path, buffer) {
|
2020-05-01 14:36:41 +02:00
|
|
|
if (this.rtenv == null) {
|
2020-11-23 12:13:52 +01:00
|
|
|
throw new Error('Engine must be inited before copying files');
|
2020-05-01 14:36:41 +02:00
|
|
|
}
|
2020-05-08 16:55:01 +02:00
|
|
|
this.rtenv['copyToFS'](path, buffer);
|
2020-09-09 16:13:55 +02:00
|
|
|
};
|
2020-05-01 14:36:41 +02:00
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.setPersistentPaths = function (persistentPaths) {
|
2020-09-18 17:25:05 +02:00
|
|
|
this.persistentPaths = persistentPaths;
|
|
|
|
};
|
|
|
|
|
2020-12-05 00:37:41 +01:00
|
|
|
Engine.prototype.setGDNativeLibraries = function (gdnativeLibs) {
|
|
|
|
this.gdnativeLibs = gdnativeLibs;
|
|
|
|
};
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
Engine.prototype.requestQuit = function () {
|
2020-09-21 13:55:30 +02:00
|
|
|
if (this.rtenv) {
|
|
|
|
this.rtenv['request_quit']();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-11 11:55:28 +01:00
|
|
|
// Closure compiler exported engine methods.
|
|
|
|
/** @export */
|
|
|
|
Engine['isWebGLAvailable'] = Utils.isWebGLAvailable;
|
|
|
|
Engine['load'] = load;
|
|
|
|
Engine['unload'] = unload;
|
2020-05-01 14:36:41 +02:00
|
|
|
Engine.prototype['init'] = Engine.prototype.init;
|
|
|
|
Engine.prototype['preloadFile'] = Engine.prototype.preloadFile;
|
|
|
|
Engine.prototype['start'] = Engine.prototype.start;
|
|
|
|
Engine.prototype['startGame'] = Engine.prototype.startGame;
|
|
|
|
Engine.prototype['setWebAssemblyFilenameExtension'] = Engine.prototype.setWebAssemblyFilenameExtension;
|
|
|
|
Engine.prototype['setUnloadAfterInit'] = Engine.prototype.setUnloadAfterInit;
|
|
|
|
Engine.prototype['setCanvas'] = Engine.prototype.setCanvas;
|
|
|
|
Engine.prototype['setCanvasResizedOnStart'] = Engine.prototype.setCanvasResizedOnStart;
|
|
|
|
Engine.prototype['setLocale'] = Engine.prototype.setLocale;
|
|
|
|
Engine.prototype['setExecutableName'] = Engine.prototype.setExecutableName;
|
|
|
|
Engine.prototype['setProgressFunc'] = Engine.prototype.setProgressFunc;
|
|
|
|
Engine.prototype['setStdoutFunc'] = Engine.prototype.setStdoutFunc;
|
|
|
|
Engine.prototype['setStderrFunc'] = Engine.prototype.setStderrFunc;
|
2020-05-01 14:45:45 +02:00
|
|
|
Engine.prototype['setOnExecute'] = Engine.prototype.setOnExecute;
|
2020-05-01 14:36:41 +02:00
|
|
|
Engine.prototype['setOnExit'] = Engine.prototype.setOnExit;
|
|
|
|
Engine.prototype['copyToFS'] = Engine.prototype.copyToFS;
|
2020-09-18 17:25:05 +02:00
|
|
|
Engine.prototype['setPersistentPaths'] = Engine.prototype.setPersistentPaths;
|
2020-12-05 00:37:41 +01:00
|
|
|
Engine.prototype['setGDNativeLibraries'] = Engine.prototype.setGDNativeLibraries;
|
2020-09-21 13:55:30 +02:00
|
|
|
Engine.prototype['requestQuit'] = Engine.prototype.requestQuit;
|
2020-03-11 11:55:28 +01:00
|
|
|
return Engine;
|
2020-11-23 12:13:52 +01:00
|
|
|
}());
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
window['Engine'] = Engine;
|
|
|
|
}
|