2020-03-11 11:55:28 +01:00
|
|
|
Function('return this')()['Engine'] = (function() {
|
|
|
|
var preloader = new Preloader();
|
|
|
|
|
2020-05-01 14:36:41 +02:00
|
|
|
var wasmExt = '.wasm';
|
|
|
|
var unloadAfterInit = true;
|
2020-03-11 11:55:28 +01:00
|
|
|
var loadPath = '';
|
|
|
|
var loadPromise = null;
|
|
|
|
var initPromise = null;
|
|
|
|
var stderr = null;
|
|
|
|
var stdout = null;
|
|
|
|
var progressFunc = null;
|
|
|
|
|
|
|
|
function load(basePath) {
|
|
|
|
if (loadPromise == null) {
|
|
|
|
loadPath = basePath;
|
|
|
|
loadPromise = preloader.loadPromise(basePath + wasmExt);
|
|
|
|
preloader.setProgressFunc(progressFunc);
|
|
|
|
requestAnimationFrame(preloader.animateProgress);
|
|
|
|
}
|
|
|
|
return loadPromise;
|
|
|
|
};
|
|
|
|
|
|
|
|
function unload() {
|
|
|
|
loadPromise = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @constructor */
|
2020-05-01 14:36:41 +02:00
|
|
|
function Engine() {
|
|
|
|
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-03-11 11:55:28 +01:00
|
|
|
|
|
|
|
Engine.prototype.init = /** @param {string=} basePath */ function(basePath) {
|
|
|
|
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) {
|
|
|
|
initPromise = Promise.reject(new Error("A base path must be provided when calling `init` and the engine is not loaded."));
|
|
|
|
return initPromise;
|
|
|
|
}
|
|
|
|
load(basePath);
|
|
|
|
}
|
2020-05-01 14:36:41 +02:00
|
|
|
var config = {};
|
2020-03-11 11:55:28 +01:00
|
|
|
if (typeof stdout === 'function')
|
|
|
|
config.print = stdout;
|
|
|
|
if (typeof stderr === 'function')
|
|
|
|
config.printErr = stderr;
|
2020-05-01 14:36:41 +02:00
|
|
|
var me = this;
|
|
|
|
initPromise = new Promise(function(resolve, reject) {
|
|
|
|
config['locateFile'] = Utils.createLocateRewrite(loadPath);
|
|
|
|
config['instantiateWasm'] = Utils.createInstantiatePromise(loadPromise);
|
|
|
|
Godot(config).then(function(module) {
|
|
|
|
me.rtenv = module;
|
2020-03-11 11:55:28 +01:00
|
|
|
if (unloadAfterInit) {
|
2020-05-01 14:36:41 +02:00
|
|
|
unload();
|
2020-03-11 11:55:28 +01:00
|
|
|
}
|
|
|
|
resolve();
|
2020-05-01 14:36:41 +02:00
|
|
|
config = null;
|
2020-03-11 11:55:28 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return initPromise;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @type {function(string, string):Object} */
|
|
|
|
Engine.prototype.preloadFile = function(file, path) {
|
|
|
|
return preloader.preload(file, path);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @type {function(...string):Object} */
|
|
|
|
Engine.prototype.start = function() {
|
|
|
|
// Start from arguments.
|
|
|
|
var args = [];
|
|
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
|
|
args.push(arguments[i]);
|
|
|
|
}
|
|
|
|
var me = this;
|
2020-05-01 14:36:41 +02:00
|
|
|
return me.init().then(function() {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Canvas can grab focus on click, or key events won't work.
|
|
|
|
if (me.canvas.tabIndex < 0) {
|
|
|
|
me.canvas.tabIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable right-click context menu.
|
|
|
|
me.canvas.addEventListener('contextmenu', function(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
// Until context restoration is implemented warn the user of context loss.
|
|
|
|
me.canvas.addEventListener('webglcontextlost', function(ev) {
|
|
|
|
alert("WebGL context lost, please reload the page");
|
|
|
|
ev.preventDefault();
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
// Browser locale, or custom one if defined.
|
|
|
|
var locale = me.customLocale;
|
|
|
|
if (!locale) {
|
|
|
|
locale = navigator.languages ? navigator.languages[0] : navigator.language;
|
|
|
|
locale = locale.split('.')[0];
|
|
|
|
}
|
|
|
|
me.rtenv['locale'] = locale;
|
|
|
|
me.rtenv['canvas'] = me.canvas;
|
|
|
|
me.rtenv['thisProgram'] = me.executableName;
|
|
|
|
me.rtenv['resizeCanvasOnStart'] = me.resizeCanvasOnStart;
|
|
|
|
me.rtenv['noExitRuntime'] = true;
|
2020-05-01 14:45:45 +02:00
|
|
|
me.rtenv['onExecute'] = me.onExecute;
|
2020-05-01 14:36:41 +02:00
|
|
|
me.rtenv['onExit'] = function(code) {
|
|
|
|
if (me.onExit)
|
|
|
|
me.onExit(code);
|
|
|
|
me.rtenv = null;
|
|
|
|
}
|
|
|
|
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-05-01 14:36:41 +02: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-03-11 11:55:28 +01:00
|
|
|
var me = this;
|
|
|
|
return Promise.all([
|
|
|
|
this.init(execName),
|
|
|
|
this.preloadFile(mainPack, mainPack)
|
|
|
|
]).then(function() {
|
2020-05-01 14:36:41 +02:00
|
|
|
var args = ['--main-pack', mainPack];
|
|
|
|
if (extraArgs)
|
|
|
|
args = args.concat(extraArgs);
|
|
|
|
return me.start.apply(me, args);
|
2020-03-11 11:55:28 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Engine.prototype.setWebAssemblyFilenameExtension = function(override) {
|
|
|
|
if (String(override).length === 0) {
|
|
|
|
throw new Error('Invalid WebAssembly filename extension override');
|
|
|
|
}
|
|
|
|
wasmExt = String(override);
|
|
|
|
};
|
|
|
|
|
|
|
|
Engine.prototype.setUnloadAfterInit = function(enabled) {
|
|
|
|
unloadAfterInit = enabled;
|
|
|
|
};
|
|
|
|
|
|
|
|
Engine.prototype.setCanvas = function(canvasElem) {
|
2020-05-01 14:36:41 +02:00
|
|
|
this.canvas = canvasElem;
|
2020-03-11 11:55:28 +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
|
|
|
};
|
|
|
|
|
|
|
|
Engine.prototype.setLocale = function(locale) {
|
2020-05-01 14:36:41 +02:00
|
|
|
this.customLocale = locale;
|
2020-03-11 11:55:28 +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
|
|
|
};
|
|
|
|
|
|
|
|
Engine.prototype.setProgressFunc = function(func) {
|
|
|
|
progressFunc = func;
|
2020-05-01 14:36:41 +02:00
|
|
|
};
|
2020-03-11 11:55:28 +01:00
|
|
|
|
|
|
|
Engine.prototype.setStdoutFunc = function(func) {
|
|
|
|
var print = function(text) {
|
|
|
|
if (arguments.length > 1) {
|
|
|
|
text = Array.prototype.slice.call(arguments).join(" ");
|
|
|
|
}
|
|
|
|
func(text);
|
|
|
|
};
|
2020-05-01 14:36:41 +02:00
|
|
|
if (this.rtenv)
|
|
|
|
this.rtenv.print = print;
|
2020-03-11 11:55:28 +01:00
|
|
|
stdout = print;
|
|
|
|
};
|
|
|
|
|
|
|
|
Engine.prototype.setStderrFunc = function(func) {
|
|
|
|
var printErr = function(text) {
|
|
|
|
if (arguments.length > 1)
|
|
|
|
text = Array.prototype.slice.call(arguments).join(" ");
|
|
|
|
func(text);
|
|
|
|
};
|
2020-05-01 14:36:41 +02:00
|
|
|
if (this.rtenv)
|
|
|
|
this.rtenv.printErr = printErr;
|
2020-03-11 11:55:28 +01:00
|
|
|
stderr = printErr;
|
|
|
|
};
|
|
|
|
|
2020-05-01 14:45:45 +02:00
|
|
|
Engine.prototype.setOnExecute = function(onExecute) {
|
|
|
|
if (this.rtenv)
|
|
|
|
this.rtenv.onExecute = onExecute;
|
|
|
|
this.onExecute = onExecute;
|
|
|
|
}
|
|
|
|
|
2020-05-01 14:36:41 +02:00
|
|
|
Engine.prototype.setOnExit = function(onExit) {
|
|
|
|
this.onExit = onExit;
|
|
|
|
}
|
|
|
|
|
|
|
|
Engine.prototype.copyToFS = function(path, buffer) {
|
|
|
|
if (this.rtenv == null) {
|
|
|
|
throw new Error("Engine must be inited before copying files");
|
|
|
|
}
|
2020-05-08 16:55:01 +02:00
|
|
|
this.rtenv['copyToFS'](path, buffer);
|
2020-05-01 14:36:41 +02:00
|
|
|
}
|
|
|
|
|
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-03-11 11:55:28 +01:00
|
|
|
return Engine;
|
|
|
|
})();
|