engine.js 7.5 KB

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