config.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /** @constructor */
  2. function EngineConfig(opts) {
  3. // Module config
  4. this.unloadAfterInit = true;
  5. this.onPrintError = function () {
  6. console.error.apply(console, Array.from(arguments)); // eslint-disable-line no-console
  7. };
  8. this.onPrint = function () {
  9. console.log.apply(console, Array.from(arguments)); // eslint-disable-line no-console
  10. };
  11. this.onProgress = null;
  12. // Godot Config
  13. this.canvas = null;
  14. this.executable = '';
  15. this.mainPack = null;
  16. this.locale = null;
  17. this.canvasResizePolicy = false;
  18. this.persistentPaths = ['/userfs'];
  19. this.gdnativeLibs = [];
  20. this.args = [];
  21. this.onExecute = null;
  22. this.onExit = null;
  23. this.update(opts);
  24. }
  25. EngineConfig.prototype.update = function (opts) {
  26. const config = opts || {};
  27. function parse(key, def) {
  28. if (typeof (config[key]) === 'undefined') {
  29. return def;
  30. }
  31. return config[key];
  32. }
  33. // Module config
  34. this.unloadAfterInit = parse('unloadAfterInit', this.unloadAfterInit);
  35. this.onPrintError = parse('onPrintError', this.onPrintError);
  36. this.onPrint = parse('onPrint', this.onPrint);
  37. this.onProgress = parse('onProgress', this.onProgress);
  38. // Godot config
  39. this.canvas = parse('canvas', this.canvas);
  40. this.executable = parse('executable', this.executable);
  41. this.mainPack = parse('mainPack', this.mainPack);
  42. this.locale = parse('locale', this.locale);
  43. this.canvasResizePolicy = parse('canvasResizePolicy', this.canvasResizePolicy);
  44. this.persistentPaths = parse('persistentPaths', this.persistentPaths);
  45. this.gdnativeLibs = parse('gdnativeLibs', this.gdnativeLibs);
  46. this.args = parse('args', this.args);
  47. this.onExecute = parse('onExecute', this.onExecute);
  48. this.onExit = parse('onExit', this.onExit);
  49. };
  50. EngineConfig.prototype.getModuleConfig = function (loadPath, loadPromise) {
  51. const me = this;
  52. return {
  53. 'print': this.onPrint,
  54. 'printErr': this.onPrintError,
  55. 'locateFile': Utils.createLocateRewrite(loadPath),
  56. 'instantiateWasm': Utils.createInstantiatePromise(loadPromise),
  57. 'thisProgram': me.executable,
  58. 'noExitRuntime': true,
  59. 'dynamicLibraries': [`${me.executable}.side.wasm`],
  60. };
  61. };
  62. EngineConfig.prototype.getGodotConfig = function (cleanup) {
  63. if (!(this.canvas instanceof HTMLCanvasElement)) {
  64. this.canvas = Utils.findCanvas();
  65. if (!this.canvas) {
  66. throw new Error('No canvas found in page');
  67. }
  68. }
  69. // Canvas can grab focus on click, or key events won't work.
  70. if (this.canvas.tabIndex < 0) {
  71. this.canvas.tabIndex = 0;
  72. }
  73. // Browser locale, or custom one if defined.
  74. let locale = this.locale;
  75. if (!locale) {
  76. locale = navigator.languages ? navigator.languages[0] : navigator.language;
  77. locale = locale.split('.')[0];
  78. }
  79. const onExit = this.onExit;
  80. // Godot configuration.
  81. return {
  82. 'canvas': this.canvas,
  83. 'canvasResizePolicy': this.canvasResizePolicy,
  84. 'locale': locale,
  85. 'onExecute': this.onExecute,
  86. 'onExit': function (p_code) {
  87. cleanup(); // We always need to call the cleanup callback to free memory.
  88. if (typeof (onExit) === 'function') {
  89. onExit(p_code);
  90. }
  91. },
  92. };
  93. };