| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
const Engine = (function () { |
| 12 |
const preloader = new Preloader(); |
| 13 |
|
| 14 |
let loadPromise = null; |
| 15 |
let loadPath = ''; |
| 16 |
let initPromise = null; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
function Engine(initConfig) { |
| 30 |
this.config = new InternalConfig(initConfig); |
| 31 |
this.rtenv = null; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
Engine.load = function (basePath, size) { |
| 44 |
if (loadPromise == null) { |
| 45 |
loadPath = basePath; |
| 46 |
loadPromise = preloader.loadPromise(`${loadPath}.wasm`, size, true); |
| 47 |
requestAnimationFrame(preloader.animateProgress); |
| 48 |
} |
| 49 |
return loadPromise; |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
Engine.unload = function () { |
| 60 |
loadPromise = null; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
function SafeEngine(initConfig) { |
| 69 |
const proto = { |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
init: function (basePath) { |
| 78 |
if (initPromise) { |
| 79 |
return initPromise; |
| 80 |
} |
| 81 |
if (loadPromise == null) { |
| 82 |
if (!basePath) { |
| 83 |
initPromise = Promise.reject(new Error('A base path must be provided when calling `init` and the engine is not loaded.')); |
| 84 |
return initPromise; |
| 85 |
} |
| 86 |
Engine.load(basePath, this.config.fileSizes[`${basePath}.wasm`]); |
| 87 |
} |
| 88 |
const me = this; |
| 89 |
function doInit(promise) { |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
return new Promise(function (resolve, reject) { |
| 94 |
promise.then(function (response) { |
| 95 |
const cloned = new Response(response.clone().body, { 'headers': [['content-type', 'application/wasm']] }); |
| 96 |
Godot(me.config.getModuleConfig(loadPath, cloned)).then(function (module) { |
| 97 |
const paths = me.config.persistentPaths; |
| 98 |
module['initFS'](paths).then(function (err) { |
| 99 |
me.rtenv = module; |
| 100 |
if (me.config.unloadAfterInit) { |
| 101 |
Engine.unload(); |
| 102 |
} |
| 103 |
resolve(); |
| 104 |
}); |
| 105 |
}); |
| 106 |
}); |
| 107 |
}); |
| 108 |
} |
| 109 |
preloader.setProgressFunc(this.config.onProgress); |
| 110 |
initPromise = doInit(loadPromise); |
| 111 |
return initPromise; |
| 112 |
}, |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
preloadFile: function (file, path) { |
| 131 |
return preloader.preload(file, path, this.config.fileSizes[file]); |
| 132 |
}, |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
start: function (override) { |
| 147 |
this.config.update(override); |
| 148 |
const me = this; |
| 149 |
return me.init().then(function () { |
| 150 |
if (!me.rtenv) { |
| 151 |
return Promise.reject(new Error('The engine must be initialized before it can be started')); |
| 152 |
} |
| 153 |
|
| 154 |
let config = {}; |
| 155 |
try { |
| 156 |
config = me.config.getGodotConfig(function () { |
| 157 |
me.rtenv = null; |
| 158 |
}); |
| 159 |
} catch (e) { |
| 160 |
return Promise.reject(e); |
| 161 |
} |
| 162 |
|
| 163 |
me.rtenv['initConfig'](config); |
| 164 |
|
| 165 |
|
| 166 |
if (me.config.gdextensionLibs.length > 0 && !me.rtenv['loadDynamicLibrary']) { |
| 167 |
return Promise.reject(new Error('GDExtension libraries are not supported by this engine version. ' |
| 168 |
+ 'Enable "Extensions Support" for your export preset and/or build your custom template with "dlink_enabled=yes".')); |
| 169 |
} |
| 170 |
return new Promise(function (resolve, reject) { |
| 171 |
for (const file of preloader.preloadedFiles) { |
| 172 |
me.rtenv['copyToFS'](file.path, file.buffer); |
| 173 |
} |
| 174 |
preloader.preloadedFiles.length = 0; |
| 175 |
me.rtenv['callMain'](me.config.args); |
| 176 |
initPromise = null; |
| 177 |
me.installServiceWorker(); |
| 178 |
resolve(); |
| 179 |
}); |
| 180 |
}); |
| 181 |
}, |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
startGame: function (override) { |
| 197 |
this.config.update(override); |
| 198 |
|
| 199 |
const exe = this.config.executable; |
| 200 |
const pack = this.config.mainPack || `${exe}.pck`; |
| 201 |
this.config.args = ['--main-pack', pack].concat(this.config.args); |
| 202 |
|
| 203 |
const me = this; |
| 204 |
return Promise.all([ |
| 205 |
this.init(exe), |
| 206 |
this.preloadFile(pack, pack), |
| 207 |
]).then(function () { |
| 208 |
return me.start.apply(me); |
| 209 |
}); |
| 210 |
}, |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
copyToFS: function (path, buffer) { |
| 219 |
if (this.rtenv == null) { |
| 220 |
throw new Error('Engine must be inited before copying files'); |
| 221 |
} |
| 222 |
this.rtenv['copyToFS'](path, buffer); |
| 223 |
}, |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
requestQuit: function () { |
| 233 |
if (this.rtenv) { |
| 234 |
this.rtenv['request_quit'](); |
| 235 |
} |
| 236 |
}, |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
installServiceWorker: function () { |
| 243 |
if (this.config.serviceWorker && 'serviceWorker' in navigator) { |
| 244 |
try { |
| 245 |
return navigator.serviceWorker.register(this.config.serviceWorker); |
| 246 |
} catch (e) { |
| 247 |
return Promise.reject(e); |
| 248 |
} |
| 249 |
} |
| 250 |
return Promise.resolve(); |
| 251 |
}, |
| 252 |
}; |
| 253 |
|
| 254 |
Engine.prototype = proto; |
| 255 |
|
| 256 |
Engine.prototype['init'] = Engine.prototype.init; |
| 257 |
Engine.prototype['preloadFile'] = Engine.prototype.preloadFile; |
| 258 |
Engine.prototype['start'] = Engine.prototype.start; |
| 259 |
Engine.prototype['startGame'] = Engine.prototype.startGame; |
| 260 |
Engine.prototype['copyToFS'] = Engine.prototype.copyToFS; |
| 261 |
Engine.prototype['requestQuit'] = Engine.prototype.requestQuit; |
| 262 |
Engine.prototype['installServiceWorker'] = Engine.prototype.installServiceWorker; |
| 263 |
|
| 264 |
Engine.prototype['load'] = Engine.load; |
| 265 |
Engine.prototype['unload'] = Engine.unload; |
| 266 |
return new Engine(initConfig); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
SafeEngine['load'] = Engine.load; |
| 271 |
SafeEngine['unload'] = Engine.unload; |
| 272 |
|
| 273 |
|
| 274 |
SafeEngine['isWebGLAvailable'] = Features.isWebGLAvailable; |
| 275 |
SafeEngine['isFetchAvailable'] = Features.isFetchAvailable; |
| 276 |
SafeEngine['isSecureContext'] = Features.isSecureContext; |
| 277 |
SafeEngine['isCrossOriginIsolated'] = Features.isCrossOriginIsolated; |
| 278 |
SafeEngine['isSharedArrayBufferAvailable'] = Features.isSharedArrayBufferAvailable; |
| 279 |
SafeEngine['isAudioWorkletAvailable'] = Features.isAudioWorkletAvailable; |
| 280 |
SafeEngine['getMissingFeatures'] = Features.getMissingFeatures; |
| 281 |
|
| 282 |
return SafeEngine; |
| 283 |
}()); |
| 284 |
if (typeof window !== 'undefined') { |
| 285 |
window['Engine'] = Engine; |
| 286 |
} |