2020-11-23 11:13:52 +00:00
|
|
|
const Engine = (function () {
|
|
|
|
const preloader = new Preloader();
|
2020-03-11 10:55:28 +00:00
|
|
|
|
2020-11-23 11:13:52 +00:00
|
|
|
let loadPromise = null;
|
2021-02-07 12:45:04 +00:00
|
|
|
let loadPath = '';
|
2020-11-23 11:13:52 +00:00
|
|
|
let initPromise = null;
|
2020-03-11 10:55:28 +00:00
|
|
|
|
|
|
|
function load(basePath) {
|
|
|
|
if (loadPromise == null) {
|
|
|
|
loadPath = basePath;
|
2021-02-07 12:45:04 +00:00
|
|
|
loadPromise = preloader.loadPromise(`${loadPath}.wasm`);
|
2020-03-11 10:55:28 +00:00
|
|
|
requestAnimationFrame(preloader.animateProgress);
|
|
|
|
}
|
|
|
|
return loadPromise;
|
2020-11-23 11:13:52 +00:00
|
|
|
}
|
2020-03-11 10:55:28 +00:00
|
|
|
|
|
|
|
function unload() {
|
|
|
|
loadPromise = null;
|
2020-11-23 11:13:52 +00:00
|
|
|
}
|
2020-03-11 10:55:28 +00:00
|
|
|
|
|
|
|
/** @constructor */
|
2021-02-07 12:45:04 +00:00
|
|
|
function Engine(opts) { // eslint-disable-line no-shadow
|
|
|
|
this.config = new EngineConfig(opts);
|
2020-05-01 12:36:41 +00:00
|
|
|
this.rtenv = null;
|
2020-11-23 11:13:52 +00:00
|
|
|
}
|
2020-03-11 10:55:28 +00:00
|
|
|
|
2020-11-23 11:13:52 +00:00
|
|
|
Engine.prototype.init = /** @param {string=} basePath */ function (basePath) {
|
2020-03-11 10:55:28 +00:00
|
|
|
if (initPromise) {
|
|
|
|
return initPromise;
|
|
|
|
}
|
2020-05-01 12:36:41 +00:00
|
|
|
if (loadPromise == null) {
|
2020-03-11 10:55:28 +00:00
|
|
|
if (!basePath) {
|
2020-11-23 11:13:52 +00:00
|
|
|
initPromise = Promise.reject(new Error('A base path must be provided when calling `init` and the engine is not loaded.'));
|
2020-03-11 10:55:28 +00:00
|
|
|
return initPromise;
|
|
|
|
}
|
|
|
|
load(basePath);
|
|
|
|
}
|
2021-02-07 12:45:04 +00:00
|
|
|
preloader.setProgressFunc(this.config.onProgress);
|
|
|
|
let config = this.config.getModuleConfig(loadPath, loadPromise);
|
2020-11-23 11:13:52 +00:00
|
|
|
const me = this;
|
|
|
|
initPromise = new Promise(function (resolve, reject) {
|
|
|
|
Godot(config).then(function (module) {
|
2021-02-07 12:45:04 +00:00
|
|
|
module['initFS'](me.config.persistentPaths).then(function (fs_err) {
|
2020-09-18 15:25:05 +00:00
|
|
|
me.rtenv = module;
|
2021-02-07 12:45:04 +00:00
|
|
|
if (me.config.unloadAfterInit) {
|
2020-09-18 15:25:05 +00:00
|
|
|
unload();
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
config = null;
|
|
|
|
});
|
2020-03-11 10:55:28 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return initPromise;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @type {function(string, string):Object} */
|
2020-11-23 11:13:52 +00:00
|
|
|
Engine.prototype.preloadFile = function (file, path) {
|
2020-03-11 10:55:28 +00:00
|
|
|
return preloader.preload(file, path);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @type {function(...string):Object} */
|
2021-02-07 12:45:04 +00:00
|
|
|
Engine.prototype.start = function (override) {
|
|
|
|
this.config.update(override);
|
2020-11-23 11:13:52 +00:00
|
|
|
const me = this;
|
|
|
|
return me.init().then(function () {
|
2020-05-01 12:36:41 +00:00
|
|
|
if (!me.rtenv) {
|
2020-05-08 14:55:01 +00:00
|
|
|
return Promise.reject(new Error('The engine must be initialized before it can be started'));
|
2020-05-01 12:36:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 12:45:04 +00:00
|
|
|
let config = {};
|
|
|
|
try {
|
|
|
|
config = me.config.getGodotConfig(function () {
|
|
|
|
me.rtenv = null;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
return Promise.reject(e);
|
2020-05-01 12:36:41 +00:00
|
|
|
}
|
2020-10-23 16:33:20 +00:00
|
|
|
// Godot configuration.
|
2021-02-07 12:45:04 +00:00
|
|
|
me.rtenv['initConfig'](config);
|
2020-10-23 16:33:20 +00:00
|
|
|
|
2021-02-07 12:45:04 +00:00
|
|
|
// Preload GDNative libraries.
|
|
|
|
const libs = [];
|
|
|
|
me.config.gdnativeLibs.forEach(function (lib) {
|
|
|
|
libs.push(me.rtenv['loadDynamicLibrary'](lib, { 'loadAsync': true }));
|
|
|
|
});
|
|
|
|
return Promise.all(libs).then(function () {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
preloader.preloadedFiles.forEach(function (file) {
|
|
|
|
me.rtenv['copyToFS'](file.path, file.buffer);
|
|
|
|
});
|
|
|
|
preloader.preloadedFiles.length = 0; // Clear memory
|
|
|
|
me.rtenv['callMain'](me.config.args);
|
|
|
|
initPromise = null;
|
|
|
|
resolve();
|
2020-03-11 10:55:28 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-02-07 12:45:04 +00:00
|
|
|
Engine.prototype.startGame = function (override) {
|
|
|
|
this.config.update(override);
|
|
|
|
// Add main-pack argument.
|
|
|
|
const exe = this.config.executable;
|
|
|
|
const pack = this.config.mainPack || `${exe}.pck`;
|
|
|
|
this.config.args = ['--main-pack', pack].concat(this.config.args);
|
2020-03-11 10:55:28 +00:00
|
|
|
// Start and init with execName as loadPath if not inited.
|
2020-11-23 11:13:52 +00:00
|
|
|
const me = this;
|
2020-03-11 10:55:28 +00:00
|
|
|
return Promise.all([
|
2021-02-07 12:45:04 +00:00
|
|
|
this.init(exe),
|
|
|
|
this.preloadFile(pack, pack),
|
2020-11-23 11:13:52 +00:00
|
|
|
]).then(function () {
|
2021-02-07 12:45:04 +00:00
|
|
|
return me.start.apply(me);
|
2020-03-11 10:55:28 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-11-23 11:13:52 +00:00
|
|
|
Engine.prototype.copyToFS = function (path, buffer) {
|
2020-05-01 12:36:41 +00:00
|
|
|
if (this.rtenv == null) {
|
2020-11-23 11:13:52 +00:00
|
|
|
throw new Error('Engine must be inited before copying files');
|
2020-05-01 12:36:41 +00:00
|
|
|
}
|
2020-05-08 14:55:01 +00:00
|
|
|
this.rtenv['copyToFS'](path, buffer);
|
2020-09-09 14:13:55 +00:00
|
|
|
};
|
2020-05-01 12:36:41 +00:00
|
|
|
|
2020-11-23 11:13:52 +00:00
|
|
|
Engine.prototype.requestQuit = function () {
|
2020-09-21 11:55:30 +00:00
|
|
|
if (this.rtenv) {
|
|
|
|
this.rtenv['request_quit']();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-11 10:55:28 +00:00
|
|
|
// Closure compiler exported engine methods.
|
|
|
|
/** @export */
|
|
|
|
Engine['isWebGLAvailable'] = Utils.isWebGLAvailable;
|
|
|
|
Engine['load'] = load;
|
|
|
|
Engine['unload'] = unload;
|
2020-05-01 12:36:41 +00: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['copyToFS'] = Engine.prototype.copyToFS;
|
2020-09-21 11:55:30 +00:00
|
|
|
Engine.prototype['requestQuit'] = Engine.prototype.requestQuit;
|
2020-03-11 10:55:28 +00:00
|
|
|
return Engine;
|
2020-11-23 11:13:52 +00:00
|
|
|
}());
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
window['Engine'] = Engine;
|
|
|
|
}
|