| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
const EngineConfig = {}; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
const InternalConfig = function (initConfig) { |
| 20 |
const cfg = { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
unloadAfterInit: true, |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
canvas: null, |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
executable: '', |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
mainPack: null, |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
locale: null, |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
canvasResizePolicy: 2, |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
args: [], |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
focusCanvas: true, |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
experimentalVK: false, |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
serviceWorker: '', |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
persistentPaths: ['/userfs'], |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
persistentDrops: false, |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
gdextensionLibs: [], |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
fileSizes: [], |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
emscriptenPoolSize: 8, |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
godotPoolSize: 4, |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
onExecute: null, |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
onExit: null, |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
onProgress: null, |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
onPrint: function () { |
| 208 |
console.log.apply(console, Array.from(arguments)); |
| 209 |
}, |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
onPrintError: function (var_args) { |
| 223 |
console.error.apply(console, Array.from(arguments)); |
| 224 |
}, |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
function Config(opts) { |
| 234 |
this.update(opts); |
| 235 |
} |
| 236 |
|
| 237 |
Config.prototype = cfg; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
Config.prototype.update = function (opts) { |
| 244 |
const config = opts || {}; |
| 245 |
|
| 246 |
|
| 247 |
function parse(key, def) { |
| 248 |
if (typeof (config[key]) === 'undefined') { |
| 249 |
return def; |
| 250 |
} |
| 251 |
return config[key]; |
| 252 |
} |
| 253 |
|
| 254 |
this.unloadAfterInit = parse('unloadAfterInit', this.unloadAfterInit); |
| 255 |
this.onPrintError = parse('onPrintError', this.onPrintError); |
| 256 |
this.onPrint = parse('onPrint', this.onPrint); |
| 257 |
this.onProgress = parse('onProgress', this.onProgress); |
| 258 |
|
| 259 |
|
| 260 |
this.canvas = parse('canvas', this.canvas); |
| 261 |
this.executable = parse('executable', this.executable); |
| 262 |
this.mainPack = parse('mainPack', this.mainPack); |
| 263 |
this.locale = parse('locale', this.locale); |
| 264 |
this.canvasResizePolicy = parse('canvasResizePolicy', this.canvasResizePolicy); |
| 265 |
this.persistentPaths = parse('persistentPaths', this.persistentPaths); |
| 266 |
this.persistentDrops = parse('persistentDrops', this.persistentDrops); |
| 267 |
this.experimentalVK = parse('experimentalVK', this.experimentalVK); |
| 268 |
this.focusCanvas = parse('focusCanvas', this.focusCanvas); |
| 269 |
this.serviceWorker = parse('serviceWorker', this.serviceWorker); |
| 270 |
this.gdextensionLibs = parse('gdextensionLibs', this.gdextensionLibs); |
| 271 |
this.fileSizes = parse('fileSizes', this.fileSizes); |
| 272 |
this.emscriptenPoolSize = parse('emscriptenPoolSize', this.emscriptenPoolSize); |
| 273 |
this.godotPoolSize = parse('godotPoolSize', this.godotPoolSize); |
| 274 |
this.args = parse('args', this.args); |
| 275 |
this.onExecute = parse('onExecute', this.onExecute); |
| 276 |
this.onExit = parse('onExit', this.onExit); |
| 277 |
}; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
Config.prototype.getModuleConfig = function (loadPath, response) { |
| 285 |
let r = response; |
| 286 |
const gdext = this.gdextensionLibs; |
| 287 |
return { |
| 288 |
'print': this.onPrint, |
| 289 |
'printErr': this.onPrintError, |
| 290 |
'thisProgram': this.executable, |
| 291 |
'noExitRuntime': false, |
| 292 |
'dynamicLibraries': [`${loadPath}.side.wasm`].concat(this.gdextensionLibs), |
| 293 |
'emscriptenPoolSize': this.emscriptenPoolSize, |
| 294 |
'instantiateWasm': function (imports, onSuccess) { |
| 295 |
function done(result) { |
| 296 |
onSuccess(result['instance'], result['module']); |
| 297 |
} |
| 298 |
if (typeof (WebAssembly.instantiateStreaming) !== 'undefined') { |
| 299 |
WebAssembly.instantiateStreaming(Promise.resolve(r), imports).then(done); |
| 300 |
} else { |
| 301 |
r.arrayBuffer().then(function (buffer) { |
| 302 |
WebAssembly.instantiate(buffer, imports).then(done); |
| 303 |
}); |
| 304 |
} |
| 305 |
r = null; |
| 306 |
return {}; |
| 307 |
}, |
| 308 |
'locateFile': function (path) { |
| 309 |
if (!path.startsWith('godot.')) { |
| 310 |
return path; |
| 311 |
} else if (path.endsWith('.audio.worklet.js')) { |
| 312 |
return `${loadPath}.audio.worklet.js`; |
| 313 |
} else if (path.endsWith('.audio.position.worklet.js')) { |
| 314 |
return `${loadPath}.audio.position.worklet.js`; |
| 315 |
} else if (path.endsWith('.js')) { |
| 316 |
return `${loadPath}.js`; |
| 317 |
} else if (path in gdext) { |
| 318 |
return path; |
| 319 |
} else if (path.endsWith('.side.wasm')) { |
| 320 |
return `${loadPath}.side.wasm`; |
| 321 |
} else if (path.endsWith('.wasm')) { |
| 322 |
return `${loadPath}.wasm`; |
| 323 |
} |
| 324 |
return path; |
| 325 |
}, |
| 326 |
}; |
| 327 |
}; |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
Config.prototype.getGodotConfig = function (cleanup) { |
| 334 |
|
| 335 |
if (!(this.canvas instanceof HTMLCanvasElement)) { |
| 336 |
const nodes = document.getElementsByTagName('canvas'); |
| 337 |
if (nodes.length && nodes[0] instanceof HTMLCanvasElement) { |
| 338 |
const first = nodes[0]; |
| 339 |
this.canvas = (first); |
| 340 |
} |
| 341 |
if (!this.canvas) { |
| 342 |
throw new Error('No canvas found in page'); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
if (this.canvas.tabIndex < 0) { |
| 347 |
this.canvas.tabIndex = 0; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
let locale = this.locale; |
| 352 |
if (!locale) { |
| 353 |
locale = navigator.languages ? navigator.languages[0] : navigator.language; |
| 354 |
locale = locale.split('.')[0]; |
| 355 |
} |
| 356 |
locale = locale.replace('-', '_'); |
| 357 |
const onExit = this.onExit; |
| 358 |
|
| 359 |
|
| 360 |
return { |
| 361 |
'canvas': this.canvas, |
| 362 |
'canvasResizePolicy': this.canvasResizePolicy, |
| 363 |
'locale': locale, |
| 364 |
'persistentDrops': this.persistentDrops, |
| 365 |
'virtualKeyboard': this.experimentalVK, |
| 366 |
'godotPoolSize': this.godotPoolSize, |
| 367 |
'focusCanvas': this.focusCanvas, |
| 368 |
'onExecute': this.onExecute, |
| 369 |
'onExit': function (p_code) { |
| 370 |
cleanup(); |
| 371 |
if (typeof (onExit) === 'function') { |
| 372 |
onExit(p_code); |
| 373 |
} |
| 374 |
}, |
| 375 |
}; |
| 376 |
}; |
| 377 |
return new Config(initConfig); |
| 378 |
}; |