engine.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. const Engine = (function () {
  2. const preloader = new Preloader();
  3. let wasmExt = '.wasm';
  4. let unloadAfterInit = true;
  5. let loadPath = '';
  6. let loadPromise = null;
  7. let initPromise = null;
  8. let stderr = null;
  9. let stdout = null;
  10. let progressFunc = null;
  11. function load(basePath) {
  12. if (loadPromise == null) {
  13. loadPath = basePath;
  14. loadPromise = preloader.loadPromise(basePath + wasmExt);
  15. preloader.setProgressFunc(progressFunc);
  16. requestAnimationFrame(preloader.animateProgress);
  17. }
  18. return loadPromise;
  19. }
  20. function unload() {
  21. loadPromise = null;
  22. }
  23. /** @constructor */
  24. function Engine() { // eslint-disable-line no-shadow
  25. this.canvas = null;
  26. this.executableName = '';
  27. this.rtenv = null;
  28. this.customLocale = null;
  29. this.resizeCanvasOnStart = false;
  30. this.onExecute = null;
  31. this.onExit = null;
  32. this.persistentPaths = ['/userfs'];
  33. }
  34. Engine.prototype.init = /** @param {string=} basePath */ function (basePath) {
  35. if (initPromise) {
  36. return initPromise;
  37. }
  38. if (loadPromise == null) {
  39. if (!basePath) {
  40. initPromise = Promise.reject(new Error('A base path must be provided when calling `init` and the engine is not loaded.'));
  41. return initPromise;
  42. }
  43. load(basePath);
  44. }
  45. let config = {};
  46. if (typeof stdout === 'function') {
  47. config.print = stdout;
  48. }
  49. if (typeof stderr === 'function') {
  50. config.printErr = stderr;
  51. }
  52. const me = this;
  53. initPromise = new Promise(function (resolve, reject) {
  54. config['locateFile'] = Utils.createLocateRewrite(loadPath);
  55. config['instantiateWasm'] = Utils.createInstantiatePromise(loadPromise);
  56. // Emscripten configuration.
  57. config['thisProgram'] = me.executableName;
  58. config['noExitRuntime'] = true;
  59. Godot(config).then(function (module) {
  60. module['initFS'](me.persistentPaths).then(function (fs_err) {
  61. me.rtenv = module;
  62. if (unloadAfterInit) {
  63. unload();
  64. }
  65. resolve();
  66. config = null;
  67. });
  68. });
  69. });
  70. return initPromise;
  71. };
  72. /** @type {function(string, string):Object} */
  73. Engine.prototype.preloadFile = function (file, path) {
  74. return preloader.preload(file, path);
  75. };
  76. /** @type {function(...string):Object} */
  77. Engine.prototype.start = function () {
  78. // Start from arguments.
  79. const args = [];
  80. for (let i = 0; i < arguments.length; i++) {
  81. args.push(arguments[i]);
  82. }
  83. const me = this;
  84. return me.init().then(function () {
  85. if (!me.rtenv) {
  86. return Promise.reject(new Error('The engine must be initialized before it can be started'));
  87. }
  88. if (!(me.canvas instanceof HTMLCanvasElement)) {
  89. me.canvas = Utils.findCanvas();
  90. if (!me.canvas) {
  91. return Promise.reject(new Error('No canvas found in page'));
  92. }
  93. }
  94. // Canvas can grab focus on click, or key events won't work.
  95. if (me.canvas.tabIndex < 0) {
  96. me.canvas.tabIndex = 0;
  97. }
  98. // Disable right-click context menu.
  99. me.canvas.addEventListener('contextmenu', function (ev) {
  100. ev.preventDefault();
  101. }, false);
  102. // Until context restoration is implemented warn the user of context loss.
  103. me.canvas.addEventListener('webglcontextlost', function (ev) {
  104. alert('WebGL context lost, please reload the page'); // eslint-disable-line no-alert
  105. ev.preventDefault();
  106. }, false);
  107. // Browser locale, or custom one if defined.
  108. let locale = me.customLocale;
  109. if (!locale) {
  110. locale = navigator.languages ? navigator.languages[0] : navigator.language;
  111. locale = locale.split('.')[0];
  112. }
  113. // Godot configuration.
  114. me.rtenv['initConfig']({
  115. 'resizeCanvasOnStart': me.resizeCanvasOnStart,
  116. 'canvas': me.canvas,
  117. 'locale': locale,
  118. 'onExecute': function (p_args) {
  119. if (me.onExecute) {
  120. me.onExecute(p_args);
  121. return 0;
  122. }
  123. return 1;
  124. },
  125. 'onExit': function (p_code) {
  126. me.rtenv['deinitFS']();
  127. if (me.onExit) {
  128. me.onExit(p_code);
  129. }
  130. me.rtenv = null;
  131. },
  132. });
  133. return new Promise(function (resolve, reject) {
  134. preloader.preloadedFiles.forEach(function (file) {
  135. me.rtenv['copyToFS'](file.path, file.buffer);
  136. });
  137. preloader.preloadedFiles.length = 0; // Clear memory
  138. me.rtenv['callMain'](args);
  139. initPromise = null;
  140. resolve();
  141. });
  142. });
  143. };
  144. Engine.prototype.startGame = function (execName, mainPack, extraArgs) {
  145. // Start and init with execName as loadPath if not inited.
  146. this.executableName = execName;
  147. const me = this;
  148. return Promise.all([
  149. this.init(execName),
  150. this.preloadFile(mainPack, mainPack),
  151. ]).then(function () {
  152. let args = ['--main-pack', mainPack];
  153. if (extraArgs) {
  154. args = args.concat(extraArgs);
  155. }
  156. return me.start.apply(me, args);
  157. });
  158. };
  159. Engine.prototype.setWebAssemblyFilenameExtension = function (override) {
  160. if (String(override).length === 0) {
  161. throw new Error('Invalid WebAssembly filename extension override');
  162. }
  163. wasmExt = String(override);
  164. };
  165. Engine.prototype.setUnloadAfterInit = function (enabled) {
  166. unloadAfterInit = enabled;
  167. };
  168. Engine.prototype.setCanvas = function (canvasElem) {
  169. this.canvas = canvasElem;
  170. };
  171. Engine.prototype.setCanvasResizedOnStart = function (enabled) {
  172. this.resizeCanvasOnStart = enabled;
  173. };
  174. Engine.prototype.setLocale = function (locale) {
  175. this.customLocale = locale;
  176. };
  177. Engine.prototype.setExecutableName = function (newName) {
  178. this.executableName = newName;
  179. };
  180. Engine.prototype.setProgressFunc = function (func) {
  181. progressFunc = func;
  182. };
  183. Engine.prototype.setStdoutFunc = function (func) {
  184. const print = function (text) {
  185. let msg = text;
  186. if (arguments.length > 1) {
  187. msg = Array.prototype.slice.call(arguments).join(' ');
  188. }
  189. func(msg);
  190. };
  191. if (this.rtenv) {
  192. this.rtenv.print = print;
  193. }
  194. stdout = print;
  195. };
  196. Engine.prototype.setStderrFunc = function (func) {
  197. const printErr = function (text) {
  198. let msg = text;
  199. if (arguments.length > 1) {
  200. msg = Array.prototype.slice.call(arguments).join(' ');
  201. }
  202. func(msg);
  203. };
  204. if (this.rtenv) {
  205. this.rtenv.printErr = printErr;
  206. }
  207. stderr = printErr;
  208. };
  209. Engine.prototype.setOnExecute = function (onExecute) {
  210. this.onExecute = onExecute;
  211. };
  212. Engine.prototype.setOnExit = function (onExit) {
  213. this.onExit = onExit;
  214. };
  215. Engine.prototype.copyToFS = function (path, buffer) {
  216. if (this.rtenv == null) {
  217. throw new Error('Engine must be inited before copying files');
  218. }
  219. this.rtenv['copyToFS'](path, buffer);
  220. };
  221. Engine.prototype.setPersistentPaths = function (persistentPaths) {
  222. this.persistentPaths = persistentPaths;
  223. };
  224. Engine.prototype.requestQuit = function () {
  225. if (this.rtenv) {
  226. this.rtenv['request_quit']();
  227. }
  228. };
  229. // Closure compiler exported engine methods.
  230. /** @export */
  231. Engine['isWebGLAvailable'] = Utils.isWebGLAvailable;
  232. Engine['load'] = load;
  233. Engine['unload'] = unload;
  234. Engine.prototype['init'] = Engine.prototype.init;
  235. Engine.prototype['preloadFile'] = Engine.prototype.preloadFile;
  236. Engine.prototype['start'] = Engine.prototype.start;
  237. Engine.prototype['startGame'] = Engine.prototype.startGame;
  238. Engine.prototype['setWebAssemblyFilenameExtension'] = Engine.prototype.setWebAssemblyFilenameExtension;
  239. Engine.prototype['setUnloadAfterInit'] = Engine.prototype.setUnloadAfterInit;
  240. Engine.prototype['setCanvas'] = Engine.prototype.setCanvas;
  241. Engine.prototype['setCanvasResizedOnStart'] = Engine.prototype.setCanvasResizedOnStart;
  242. Engine.prototype['setLocale'] = Engine.prototype.setLocale;
  243. Engine.prototype['setExecutableName'] = Engine.prototype.setExecutableName;
  244. Engine.prototype['setProgressFunc'] = Engine.prototype.setProgressFunc;
  245. Engine.prototype['setStdoutFunc'] = Engine.prototype.setStdoutFunc;
  246. Engine.prototype['setStderrFunc'] = Engine.prototype.setStderrFunc;
  247. Engine.prototype['setOnExecute'] = Engine.prototype.setOnExecute;
  248. Engine.prototype['setOnExit'] = Engine.prototype.setOnExit;
  249. Engine.prototype['copyToFS'] = Engine.prototype.copyToFS;
  250. Engine.prototype['setPersistentPaths'] = Engine.prototype.setPersistentPaths;
  251. Engine.prototype['requestQuit'] = Engine.prototype.requestQuit;
  252. return Engine;
  253. }());
  254. if (typeof window !== 'undefined') {
  255. window['Engine'] = Engine;
  256. }