diff options
Diffstat (limited to 'platform/javascript/js/engine/engine.js')
-rw-r--r-- | platform/javascript/js/engine/engine.js | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/platform/javascript/js/engine/engine.js b/platform/javascript/js/engine/engine.js index c51955ed3d..17a8df9e29 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`); + loadPromise = preloader.loadPromise(`${loadPath}.wasm`, size, true); requestAnimationFrame(preloader.animateProgress); } return loadPromise; @@ -96,23 +97,31 @@ 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`]); } - preloader.setProgressFunc(this.config.onProgress); - let config = this.config.getModuleConfig(loadPath, loadPromise); const me = this; - initPromise = new Promise(function (resolve, reject) { - Godot(config).then(function (module) { - module['initFS'](me.config.persistentPaths).then(function (fs_err) { - me.rtenv = module; - if (me.config.unloadAfterInit) { - Engine.unload(); - } - resolve(); - config = null; + function doInit(promise) { + // Care! Promise chaining is bogus with old emscripten versions. + // This caused a regression with the Mono build (which uses an older emscripten version). + // Make sure to test that when refactoring. + return new Promise(function (resolve, reject) { + promise.then(function (response) { + const cloned = new Response(response.clone().body, { 'headers': [['content-type', 'application/wasm']] }); + Godot(me.config.getModuleConfig(loadPath, cloned)).then(function (module) { + const paths = me.config.persistentPaths; + module['initFS'](paths).then(function (err) { + me.rtenv = module; + if (me.config.unloadAfterInit) { + Engine.unload(); + } + resolve(); + }); + }); }); }); - }); + } + preloader.setProgressFunc(this.config.onProgress); + initPromise = doInit(loadPromise); return initPromise; }, @@ -133,7 +142,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]); }, /** |