engine.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const Engine = (function () {
  2. const preloader = new Preloader();
  3. let loadPromise = null;
  4. let loadPath = '';
  5. let initPromise = null;
  6. function load(basePath) {
  7. if (loadPromise == null) {
  8. loadPath = basePath;
  9. loadPromise = preloader.loadPromise(`${loadPath}.wasm`);
  10. requestAnimationFrame(preloader.animateProgress);
  11. }
  12. return loadPromise;
  13. }
  14. function unload() {
  15. loadPromise = null;
  16. }
  17. /** @constructor */
  18. function Engine(opts) { // eslint-disable-line no-shadow
  19. this.config = new EngineConfig(opts);
  20. this.rtenv = null;
  21. }
  22. Engine.prototype.init = /** @param {string=} basePath */ function (basePath) {
  23. if (initPromise) {
  24. return initPromise;
  25. }
  26. if (loadPromise == null) {
  27. if (!basePath) {
  28. initPromise = Promise.reject(new Error('A base path must be provided when calling `init` and the engine is not loaded.'));
  29. return initPromise;
  30. }
  31. load(basePath);
  32. }
  33. preloader.setProgressFunc(this.config.onProgress);
  34. let config = this.config.getModuleConfig(loadPath, loadPromise);
  35. const me = this;
  36. initPromise = new Promise(function (resolve, reject) {
  37. Godot(config).then(function (module) {
  38. module['initFS'](me.config.persistentPaths).then(function (fs_err) {
  39. me.rtenv = module;
  40. if (me.config.unloadAfterInit) {
  41. unload();
  42. }
  43. resolve();
  44. config = null;
  45. });
  46. });
  47. });
  48. return initPromise;
  49. };
  50. /** @type {function(string, string):Object} */
  51. Engine.prototype.preloadFile = function (file, path) {
  52. return preloader.preload(file, path);
  53. };
  54. /** @type {function(...string):Object} */
  55. Engine.prototype.start = function (override) {
  56. this.config.update(override);
  57. const me = this;
  58. return me.init().then(function () {
  59. if (!me.rtenv) {
  60. return Promise.reject(new Error('The engine must be initialized before it can be started'));
  61. }
  62. let config = {};
  63. try {
  64. config = me.config.getGodotConfig(function () {
  65. me.rtenv = null;
  66. });
  67. } catch (e) {
  68. return Promise.reject(e);
  69. }
  70. // Godot configuration.
  71. me.rtenv['initConfig'](config);
  72. // Preload GDNative libraries.
  73. const libs = [];
  74. me.config.gdnativeLibs.forEach(function (lib) {
  75. libs.push(me.rtenv['loadDynamicLibrary'](lib, { 'loadAsync': true }));
  76. });
  77. return Promise.all(libs).then(function () {
  78. return new Promise(function (resolve, reject) {
  79. preloader.preloadedFiles.forEach(function (file) {
  80. me.rtenv['copyToFS'](file.path, file.buffer);
  81. });
  82. preloader.preloadedFiles.length = 0; // Clear memory
  83. me.rtenv['callMain'](me.config.args);
  84. initPromise = null;
  85. resolve();
  86. });
  87. });
  88. });
  89. };
  90. Engine.prototype.startGame = function (override) {
  91. this.config.update(override);
  92. // Add main-pack argument.
  93. const exe = this.config.executable;
  94. const pack = this.config.mainPack || `${exe}.pck`;
  95. this.config.args = ['--main-pack', pack].concat(this.config.args);
  96. // Start and init with execName as loadPath if not inited.
  97. const me = this;
  98. return Promise.all([
  99. this.init(exe),
  100. this.preloadFile(pack, pack),
  101. ]).then(function () {
  102. return me.start.apply(me);
  103. });
  104. };
  105. Engine.prototype.copyToFS = function (path, buffer) {
  106. if (this.rtenv == null) {
  107. throw new Error('Engine must be inited before copying files');
  108. }
  109. this.rtenv['copyToFS'](path, buffer);
  110. };
  111. Engine.prototype.requestQuit = function () {
  112. if (this.rtenv) {
  113. this.rtenv['request_quit']();
  114. }
  115. };
  116. // Closure compiler exported engine methods.
  117. /** @export */
  118. Engine['isWebGLAvailable'] = Utils.isWebGLAvailable;
  119. Engine['load'] = load;
  120. Engine['unload'] = unload;
  121. Engine.prototype['init'] = Engine.prototype.init;
  122. Engine.prototype['preloadFile'] = Engine.prototype.preloadFile;
  123. Engine.prototype['start'] = Engine.prototype.start;
  124. Engine.prototype['startGame'] = Engine.prototype.startGame;
  125. Engine.prototype['copyToFS'] = Engine.prototype.copyToFS;
  126. Engine.prototype['requestQuit'] = Engine.prototype.requestQuit;
  127. return Engine;
  128. }());
  129. if (typeof window !== 'undefined') {
  130. window['Engine'] = Engine;
  131. }