2021-02-20 13:20:06 +01:00
|
|
|
/**
|
|
|
|
* An object used to configure the Engine instance based on godot export options, and to override those in custom HTML
|
|
|
|
* templates if needed.
|
|
|
|
*
|
|
|
|
* @header Engine configuration
|
|
|
|
* @summary The Engine configuration object. This is just a typedef, create it like a regular object, e.g.:
|
|
|
|
*
|
|
|
|
* ``const MyConfig = { executable: 'godot', unloadAfterInit: false }``
|
|
|
|
*
|
|
|
|
* @typedef {Object} EngineConfig
|
|
|
|
*/
|
|
|
|
const EngineConfig = {}; // eslint-disable-line no-unused-vars
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @struct
|
|
|
|
* @constructor
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-vars
|
|
|
|
const cfg = /** @lends {InternalConfig.prototype} */ {
|
|
|
|
/**
|
|
|
|
* Whether the unload the engine automatically after the instance is initialized.
|
|
|
|
*
|
|
|
|
* @memberof EngineConfig
|
|
|
|
* @default
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
unloadAfterInit: true,
|
|
|
|
/**
|
|
|
|
* The HTML DOM Canvas object to use.
|
|
|
|
*
|
|
|
|
* By default, the first canvas element in the document will be used is none is specified.
|
|
|
|
*
|
|
|
|
* @memberof EngineConfig
|
|
|
|
* @default
|
|
|
|
* @type {?HTMLCanvasElement}
|
|
|
|
*/
|
|
|
|
canvas: null,
|
|
|
|
/**
|
|
|
|
* The name of the WASM file without the extension. (Set by Godot Editor export process).
|
|
|
|
*
|
|
|
|
* @memberof EngineConfig
|
|
|
|
* @default
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
executable: '',
|
|
|
|
/**
|
|
|
|
* An alternative name for the game pck to load. The executable name is used otherwise.
|
|
|
|
*
|
|
|
|
* @memberof EngineConfig
|
|
|
|
* @default
|
|
|
|
* @type {?string}
|
|
|
|
*/
|
|
|
|
mainPack: null,
|
|
|
|
/**
|
|
|
|
* Specify a language code to select the proper localization for the game.
|
|
|
|
*
|
|
|
|
* The browser locale will be used if none is specified. See complete list of
|
|
|
|
* :ref:`supported locales <doc_locales>`.
|
|
|
|
*
|
|
|
|
* @memberof EngineConfig
|
|
|
|
* @type {?string}
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
locale: null,
|
|
|
|
/**
|
|
|
|
* The canvas resize policy determines how the canvas should be resized by Godot.
|
|
|
|
*
|
|
|
|
* ``0`` means Godot won't do any resizing. This is useful if you want to control the canvas size from
|
|
|
|
* javascript code in your template.
|
|
|
|
*
|
|
|
|
* ``1`` means Godot will resize the canvas on start, and when changing window size via engine functions.
|
|
|
|
*
|
|
|
|
* ``2`` means Godot will adapt the canvas size to match the whole browser window.
|
|
|
|
*
|
|
|
|
* @memberof EngineConfig
|
|
|
|
* @type {number}
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
canvasResizePolicy: 2,
|
|
|
|
/**
|
|
|
|
* The arguments to be passed as command line arguments on startup.
|
|
|
|
*
|
|
|
|
* See :ref:`command line tutorial <doc_command_line_tutorial>`.
|
|
|
|
*
|
|
|
|
* **Note**: :js:meth:`startGame <Engine.prototype.startGame>` will always add the ``--main-pack`` argument.
|
|
|
|
*
|
|
|
|
* @memberof EngineConfig
|
|
|
|
* @type {Array<string>}
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
args: [],
|
2021-06-25 19:07:17 +02:00
|
|
|
/**
|
|
|
|
* When enabled, the game canvas will automatically grab the focus when the engine starts.
|
|
|
|
*
|
|
|
|
* @memberof EngineConfig
|
|
|
|
* @type {boolean}
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
focusCanvas: true,
|
2021-03-08 23:16:51 +01:00
|
|
|
/**
|
|
|
|
* When enabled, this will turn on experimental virtual keyboard support on mobile.
|
|
|
|
*
|
|
|
|
* @memberof EngineConfig
|
|
|
|
* @type {boolean}
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
experimentalVK: false,
|
2021-02-20 13:20:06 +01:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @type {Array.<string>}
|
|
|
|
*/
|
|
|
|
persistentPaths: ['/userfs'],
|
2021-03-12 01:59:16 +01:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
persistentDrops: false,
|
2021-02-20 13:20:06 +01:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @type {Array.<string>}
|
|
|
|
*/
|
|
|
|
gdnativeLibs: [],
|
2021-03-01 17:53:56 +01:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @type {Array.<string>}
|
|
|
|
*/
|
|
|
|
fileSizes: [],
|
2021-02-20 13:20:06 +01:00
|
|
|
/**
|
|
|
|
* A callback function for handling Godot's ``OS.execute`` calls.
|
|
|
|
*
|
|
|
|
* This is for example used in the Web Editor template to switch between project manager and editor, and for running the game.
|
|
|
|
*
|
|
|
|
* @callback EngineConfig.onExecute
|
|
|
|
* @param {string} path The path that Godot's wants executed.
|
|
|
|
* @param {Array.<string>} args The arguments of the "command" to execute.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @type {?function(string, Array.<string>)}
|
|
|
|
*/
|
|
|
|
onExecute: null,
|
|
|
|
/**
|
|
|
|
* A callback function for being notified when the Godot instance quits.
|
|
|
|
*
|
|
|
|
* **Note**: This function will not be called if the engine crashes or become unresponsive.
|
|
|
|
*
|
|
|
|
* @callback EngineConfig.onExit
|
|
|
|
* @param {number} status_code The status code returned by Godot on exit.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @type {?function(number)}
|
|
|
|
*/
|
|
|
|
onExit: null,
|
|
|
|
/**
|
|
|
|
* A callback function for displaying download progress.
|
|
|
|
*
|
|
|
|
* The function is called once per frame while downloading files, so the usage of ``requestAnimationFrame()``
|
|
|
|
* is not necessary.
|
|
|
|
*
|
|
|
|
* If the callback function receives a total amount of bytes as 0, this means that it is impossible to calculate.
|
|
|
|
* Possible reasons include:
|
|
|
|
*
|
|
|
|
* - Files are delivered with server-side chunked compression
|
|
|
|
* - Files are delivered with server-side compression on Chromium
|
|
|
|
* - Not all file downloads have started yet (usually on servers without multi-threading)
|
|
|
|
*
|
|
|
|
* @callback EngineConfig.onProgress
|
|
|
|
* @param {number} current The current amount of downloaded bytes so far.
|
|
|
|
* @param {number} total The total amount of bytes to be downloaded.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @type {?function(number, number)}
|
|
|
|
*/
|
|
|
|
onProgress: null,
|
|
|
|
/**
|
|
|
|
* A callback function for handling the standard output stream. This method should usually only be used in debug pages.
|
|
|
|
*
|
|
|
|
* By default, ``console.log()`` is used.
|
|
|
|
*
|
|
|
|
* @callback EngineConfig.onPrint
|
|
|
|
* @param {...*} [var_args] A variadic number of arguments to be printed.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @type {?function(...*)}
|
|
|
|
*/
|
|
|
|
onPrint: function () {
|
|
|
|
console.log.apply(console, Array.from(arguments)); // eslint-disable-line no-console
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* A callback function for handling the standard error stream. This method should usually only be used in debug pages.
|
|
|
|
*
|
|
|
|
* By default, ``console.error()`` is used.
|
|
|
|
*
|
|
|
|
* @callback EngineConfig.onPrintError
|
|
|
|
* @param {...*} [var_args] A variadic number of arguments to be printed as errors.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @type {?function(...*)}
|
|
|
|
*/
|
|
|
|
onPrintError: function (var_args) {
|
|
|
|
console.error.apply(console, Array.from(arguments)); // eslint-disable-line no-console
|
|
|
|
},
|
2021-02-07 13:45:04 +01:00
|
|
|
};
|
|
|
|
|
2021-02-20 13:20:06 +01:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @struct
|
|
|
|
* @constructor
|
|
|
|
* @param {EngineConfig} opts
|
|
|
|
*/
|
|
|
|
function Config(opts) {
|
|
|
|
this.update(opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
Config.prototype = cfg;
|
2021-02-07 13:45:04 +01:00
|
|
|
|
2021-02-20 13:20:06 +01:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @param {EngineConfig} opts
|
|
|
|
*/
|
|
|
|
Config.prototype.update = function (opts) {
|
|
|
|
const config = opts || {};
|
2022-01-30 17:41:40 +01:00
|
|
|
const me = this;
|
|
|
|
function parse(key) {
|
2021-02-20 13:20:06 +01:00
|
|
|
if (typeof (config[key]) === 'undefined') {
|
2022-01-30 17:41:40 +01:00
|
|
|
return me[key];
|
2021-02-20 13:20:06 +01:00
|
|
|
}
|
|
|
|
return config[key];
|
2021-02-07 13:45:04 +01:00
|
|
|
}
|
2021-02-20 13:20:06 +01:00
|
|
|
// Module config
|
2022-01-30 17:41:40 +01:00
|
|
|
this.unloadAfterInit = parse('unloadAfterInit');
|
|
|
|
this.onPrintError = parse('onPrintError');
|
|
|
|
this.onPrint = parse('onPrint');
|
|
|
|
this.onProgress = parse('onProgress');
|
2021-02-07 13:45:04 +01:00
|
|
|
|
2021-02-20 13:20:06 +01:00
|
|
|
// Godot config
|
2022-01-30 17:41:40 +01:00
|
|
|
this.canvas = parse('canvas');
|
|
|
|
this.executable = parse('executable');
|
|
|
|
this.mainPack = parse('mainPack');
|
|
|
|
this.locale = parse('locale');
|
|
|
|
this.canvasResizePolicy = parse('canvasResizePolicy');
|
|
|
|
this.persistentPaths = parse('persistentPaths');
|
|
|
|
this.persistentDrops = parse('persistentDrops');
|
|
|
|
this.experimentalVK = parse('experimentalVK');
|
|
|
|
this.focusCanvas = parse('focusCanvas');
|
|
|
|
this.gdnativeLibs = parse('gdnativeLibs');
|
|
|
|
this.fileSizes = parse('fileSizes');
|
|
|
|
this.args = parse('args');
|
|
|
|
this.onExecute = parse('onExecute');
|
|
|
|
this.onExit = parse('onExit');
|
2021-02-20 13:20:06 +01:00
|
|
|
};
|
2021-02-07 13:45:04 +01:00
|
|
|
|
2021-02-20 13:20:06 +01:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @param {string} loadPath
|
2021-03-01 11:26:22 +01:00
|
|
|
* @param {Response} response
|
2021-02-20 13:20:06 +01:00
|
|
|
*/
|
2021-03-01 11:26:22 +01:00
|
|
|
Config.prototype.getModuleConfig = function (loadPath, response) {
|
|
|
|
let r = response;
|
2021-02-20 13:20:06 +01:00
|
|
|
return {
|
|
|
|
'print': this.onPrint,
|
|
|
|
'printErr': this.onPrintError,
|
|
|
|
'thisProgram': this.executable,
|
|
|
|
'noExitRuntime': true,
|
|
|
|
'dynamicLibraries': [`${loadPath}.side.wasm`],
|
|
|
|
'instantiateWasm': function (imports, onSuccess) {
|
2021-03-01 11:26:22 +01:00
|
|
|
function done(result) {
|
|
|
|
onSuccess(result['instance'], result['module']);
|
|
|
|
}
|
|
|
|
if (typeof (WebAssembly.instantiateStreaming) !== 'undefined') {
|
|
|
|
WebAssembly.instantiateStreaming(Promise.resolve(r), imports).then(done);
|
|
|
|
} else {
|
|
|
|
r.arrayBuffer().then(function (buffer) {
|
|
|
|
WebAssembly.instantiate(buffer, imports).then(done);
|
2021-02-20 13:20:06 +01:00
|
|
|
});
|
2021-03-01 11:26:22 +01:00
|
|
|
}
|
|
|
|
r = null;
|
2021-02-20 13:20:06 +01:00
|
|
|
return {};
|
|
|
|
},
|
|
|
|
'locateFile': function (path) {
|
|
|
|
if (path.endsWith('.worker.js')) {
|
|
|
|
return `${loadPath}.worker.js`;
|
|
|
|
} else if (path.endsWith('.audio.worklet.js')) {
|
|
|
|
return `${loadPath}.audio.worklet.js`;
|
|
|
|
} else if (path.endsWith('.js')) {
|
|
|
|
return `${loadPath}.js`;
|
|
|
|
} else if (path.endsWith('.side.wasm')) {
|
|
|
|
return `${loadPath}.side.wasm`;
|
|
|
|
} else if (path.endsWith('.wasm')) {
|
|
|
|
return `${loadPath}.wasm`;
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
},
|
|
|
|
};
|
2021-02-07 13:45:04 +01:00
|
|
|
};
|
|
|
|
|
2021-02-20 13:20:06 +01:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @param {function()} cleanup
|
|
|
|
*/
|
|
|
|
Config.prototype.getGodotConfig = function (cleanup) {
|
|
|
|
// Try to find a canvas
|
|
|
|
if (!(this.canvas instanceof HTMLCanvasElement)) {
|
|
|
|
const nodes = document.getElementsByTagName('canvas');
|
|
|
|
if (nodes.length && nodes[0] instanceof HTMLCanvasElement) {
|
|
|
|
this.canvas = nodes[0];
|
|
|
|
}
|
|
|
|
if (!this.canvas) {
|
|
|
|
throw new Error('No canvas found in page');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Canvas can grab focus on click, or key events won't work.
|
|
|
|
if (this.canvas.tabIndex < 0) {
|
|
|
|
this.canvas.tabIndex = 0;
|
2021-02-07 13:45:04 +01:00
|
|
|
}
|
|
|
|
|
2021-02-20 13:20:06 +01:00
|
|
|
// Browser locale, or custom one if defined.
|
|
|
|
let locale = this.locale;
|
|
|
|
if (!locale) {
|
|
|
|
locale = navigator.languages ? navigator.languages[0] : navigator.language;
|
|
|
|
locale = locale.split('.')[0];
|
|
|
|
}
|
|
|
|
const onExit = this.onExit;
|
2021-02-07 13:45:04 +01:00
|
|
|
|
2021-02-20 13:20:06 +01:00
|
|
|
// Godot configuration.
|
|
|
|
return {
|
|
|
|
'canvas': this.canvas,
|
|
|
|
'canvasResizePolicy': this.canvasResizePolicy,
|
|
|
|
'locale': locale,
|
2021-03-12 01:59:16 +01:00
|
|
|
'persistentDrops': this.persistentDrops,
|
2021-03-08 23:16:51 +01:00
|
|
|
'virtualKeyboard': this.experimentalVK,
|
2021-06-25 19:07:17 +02:00
|
|
|
'focusCanvas': this.focusCanvas,
|
2021-02-20 13:20:06 +01:00
|
|
|
'onExecute': this.onExecute,
|
|
|
|
'onExit': function (p_code) {
|
|
|
|
cleanup(); // We always need to call the cleanup callback to free memory.
|
|
|
|
if (typeof (onExit) === 'function') {
|
|
|
|
onExit(p_code);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2021-02-07 13:45:04 +01:00
|
|
|
};
|
2021-02-20 13:20:06 +01:00
|
|
|
return new Config(initConfig);
|
2021-02-07 13:45:04 +01:00
|
|
|
};
|