engine.js 7.0 KB

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