diff options
Diffstat (limited to 'platform/javascript/js')
-rw-r--r-- | platform/javascript/js/engine/config.js | 6 | ||||
-rw-r--r-- | platform/javascript/js/engine/engine.js | 9 | ||||
-rw-r--r-- | platform/javascript/js/engine/preloader.js | 12 |
3 files changed, 17 insertions, 10 deletions
diff --git a/platform/javascript/js/engine/config.js b/platform/javascript/js/engine/config.js index 8c2636c7ab..82ff273ecf 100644 --- a/platform/javascript/js/engine/config.js +++ b/platform/javascript/js/engine/config.js @@ -101,6 +101,11 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused- */ gdnativeLibs: [], /** + * @ignore + * @type {Array.<string>} + */ + fileSizes: [], + /** * 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. @@ -219,6 +224,7 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused- this.canvasResizePolicy = parse('canvasResizePolicy', this.canvasResizePolicy); this.persistentPaths = parse('persistentPaths', this.persistentPaths); this.gdnativeLibs = parse('gdnativeLibs', this.gdnativeLibs); + this.fileSizes = parse('fileSizes', this.fileSizes); this.args = parse('args', this.args); this.onExecute = parse('onExecute', this.onExecute); this.onExit = parse('onExit', this.onExit); diff --git a/platform/javascript/js/engine/engine.js b/platform/javascript/js/engine/engine.js index e2d27c4c02..19a0552c8c 100644 --- a/platform/javascript/js/engine/engine.js +++ b/platform/javascript/js/engine/engine.js @@ -35,14 +35,15 @@ const Engine = (function () { * Load the engine from the specified base path. * * @param {string} basePath Base path of the engine to load. + * @param {number=} [size=0] The file size if known. * @returns {Promise} A Promise that resolves once the engine is loaded. * * @function Engine.load */ - Engine.load = function (basePath) { + Engine.load = function (basePath, size) { if (loadPromise == null) { loadPath = basePath; - loadPromise = preloader.loadPromise(`${loadPath}.wasm`, true); + loadPromise = preloader.loadPromise(`${loadPath}.wasm`, size, true); requestAnimationFrame(preloader.animateProgress); } return loadPromise; @@ -96,7 +97,7 @@ const Engine = (function () { initPromise = Promise.reject(new Error('A base path must be provided when calling `init` and the engine is not loaded.')); return initPromise; } - Engine.load(basePath); + Engine.load(basePath, this.config.fileSizes[`${basePath}.wasm`]); } const me = this; function doInit(promise) { @@ -137,7 +138,7 @@ const Engine = (function () { * @returns {Promise} A Promise that resolves once the file is loaded. */ preloadFile: function (file, path) { - return preloader.preload(file, path); + return preloader.preload(file, path, this.config.fileSizes[file]); }, /** diff --git a/platform/javascript/js/engine/preloader.js b/platform/javascript/js/engine/preloader.js index 45bdcf26d4..3535fdb361 100644 --- a/platform/javascript/js/engine/preloader.js +++ b/platform/javascript/js/engine/preloader.js @@ -43,9 +43,9 @@ const Preloader = /** @constructor */ function () { // eslint-disable-line no-un }), { headers: response.headers }); } - function loadFetch(file, tracker, raw) { + function loadFetch(file, tracker, fileSize, raw) { tracker[file] = { - total: 0, + total: fileSize || 0, loaded: 0, done: false, }; @@ -117,16 +117,16 @@ const Preloader = /** @constructor */ function () { // eslint-disable-line no-un progressFunc = callback; }; - this.loadPromise = function (file, raw = false) { - return retry(loadFetch.bind(null, file, loadingFiles, raw), DOWNLOAD_ATTEMPTS_MAX); + this.loadPromise = function (file, fileSize, raw = false) { + return retry(loadFetch.bind(null, file, loadingFiles, fileSize, raw), DOWNLOAD_ATTEMPTS_MAX); }; this.preloadedFiles = []; - this.preload = function (pathOrBuffer, destPath) { + this.preload = function (pathOrBuffer, destPath, fileSize) { let buffer = null; if (typeof pathOrBuffer === 'string') { const me = this; - return this.loadPromise(pathOrBuffer).then(function (buf) { + return this.loadPromise(pathOrBuffer, fileSize).then(function (buf) { me.preloadedFiles.push({ path: destPath || pathOrBuffer, buffer: buf, |