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-12-21 14:41:26 +01:00
|
|
|
/**
|
|
|
|
* The progressive web app service worker to install.
|
|
|
|
* @memberof EngineConfig
|
|
|
|
* @default
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
serviceWorker: '',
|
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>}
|
|
|
|
*/
|
2022-12-07 12:11:28 +01:00
|
|
|
gdextensionLibs: [],
|
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-02-03 19:10:08 +01:00
|
|
|
// NOTE: We must explicitly pass the default, accessing it via
|
|
|
|
// the key will fail due to closure compiler renames.
|
|
|
|
function parse(key, def) {
|
2021-02-20 13:20:06 +01:00
|
|
|
if (typeof (config[key]) === 'undefined') {
|
2022-02-03 19:10:08 +01:00
|
|
|
return def;
|
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-02-03 19:10:08 +01:00
|
|
|
this.unloadAfterInit = parse('unloadAfterInit', this.unloadAfterInit);
|
|
|
|
this.onPrintError = parse('onPrintError', this.onPrintError);
|
|
|
|
this.onPrint = parse('onPrint', this.onPrint);
|
|
|
|
this.onProgress = parse('onProgress', this.onProgress);
|
2021-02-07 13:45:04 +01:00
|
|
|
|
2021-02-20 13:20:06 +01:00
|
|
|
// Godot config
|
2022-02-03 19:10:08 +01:00
|
|
|
this.canvas = parse('canvas', this.canvas);
|
|
|
|
this.executable = parse('executable', this.executable);
|
|
|
|
this.mainPack = parse('mainPack', this.mainPack);
|
|
|
|
this.locale = parse('locale', this.locale);
|
|
|
|
this.canvasResizePolicy = parse('canvasResizePolicy', this.canvasResizePolicy);
|
|
|
|
this.persistentPaths = parse('persistentPaths', this.persistentPaths);
|
|
|
|
this.persistentDrops = parse('persistentDrops', this.persistentDrops);
|
|
|
|
this.experimentalVK = parse('experimentalVK', this.experimentalVK);
|
|
|
|
this.focusCanvas = parse('focusCanvas', this.focusCanvas);
|
2021-12-21 14:41:26 +01:00
|
|
|
this.serviceWorker = parse('serviceWorker', this.serviceWorker);
|
2022-12-07 12:11:28 +01:00
|
|
|
this.gdextensionLibs = parse('gdextensionLibs', this.gdextensionLibs);
|
2022-02-03 19:10:08 +01:00
|
|
|
this.fileSizes = parse('fileSizes', this.fileSizes);
|
|
|
|
this.args = parse('args', this.args);
|
|
|
|
this.onExecute = parse('onExecute', this.onExecute);
|
|
|
|
this.onExit = parse('onExit', this.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,
|
2022-11-03 14:00:52 +01:00
|
|
|
'noExitRuntime': false,
|
2021-02-20 13:20:06 +01:00
|
|
|
'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) {
|
2022-09-08 09:44:14 +02:00
|
|
|
const first = nodes[0];
|
|
|
|
this.canvas = /** @type {!HTMLCanvasElement} */ (first);
|
2021-02-20 13:20:06 +01:00
|
|
|
}
|
|
|
|
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];
|
|
|
|
}
|
2022-07-15 18:37:42 +02:00
|
|
|
locale = locale.replace('-', '_');
|
2021-02-20 13:20:06 +01:00
|
|
|
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
|
|
|
};
|