engine.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. if (me.onExit)
  113. me.onExit(code);
  114. me.rtenv = null;
  115. }
  116. return new Promise(function(resolve, reject) {
  117. preloader.preloadedFiles.forEach(function(file) {
  118. me.rtenv['copyToFS'](file.path, file.buffer);
  119. });
  120. preloader.preloadedFiles.length = 0; // Clear memory
  121. me.rtenv['callMain'](args);
  122. initPromise = null;
  123. resolve();
  124. });
  125. });
  126. };
  127. Engine.prototype.startGame = function(execName, mainPack, extraArgs) {
  128. // Start and init with execName as loadPath if not inited.
  129. this.executableName = execName;
  130. var me = this;
  131. return Promise.all([
  132. this.init(execName),
  133. this.preloadFile(mainPack, mainPack)
  134. ]).then(function() {
  135. var args = ['--main-pack', mainPack];
  136. if (extraArgs)
  137. args = args.concat(extraArgs);
  138. return me.start.apply(me, args);
  139. });
  140. };
  141. Engine.prototype.setWebAssemblyFilenameExtension = function(override) {
  142. if (String(override).length === 0) {
  143. throw new Error('Invalid WebAssembly filename extension override');
  144. }
  145. wasmExt = String(override);
  146. };
  147. Engine.prototype.setUnloadAfterInit = function(enabled) {
  148. unloadAfterInit = enabled;
  149. };
  150. Engine.prototype.setCanvas = function(canvasElem) {
  151. this.canvas = canvasElem;
  152. };
  153. Engine.prototype.setCanvasResizedOnStart = function(enabled) {
  154. this.resizeCanvasOnStart = enabled;
  155. };
  156. Engine.prototype.setLocale = function(locale) {
  157. this.customLocale = locale;
  158. };
  159. Engine.prototype.setExecutableName = function(newName) {
  160. this.executableName = newName;
  161. };
  162. Engine.prototype.setProgressFunc = function(func) {
  163. progressFunc = func;
  164. };
  165. Engine.prototype.setStdoutFunc = function(func) {
  166. var print = function(text) {
  167. if (arguments.length > 1) {
  168. text = Array.prototype.slice.call(arguments).join(" ");
  169. }
  170. func(text);
  171. };
  172. if (this.rtenv)
  173. this.rtenv.print = print;
  174. stdout = print;
  175. };
  176. Engine.prototype.setStderrFunc = function(func) {
  177. var printErr = function(text) {
  178. if (arguments.length > 1)
  179. text = Array.prototype.slice.call(arguments).join(" ");
  180. func(text);
  181. };
  182. if (this.rtenv)
  183. this.rtenv.printErr = printErr;
  184. stderr = printErr;
  185. };
  186. Engine.prototype.setOnExecute = function(onExecute) {
  187. if (this.rtenv)
  188. this.rtenv.onExecute = onExecute;
  189. this.onExecute = onExecute;
  190. }
  191. Engine.prototype.setOnExit = function(onExit) {
  192. this.onExit = onExit;
  193. }
  194. Engine.prototype.copyToFS = function(path, buffer) {
  195. if (this.rtenv == null) {
  196. throw new Error("Engine must be inited before copying files");
  197. }
  198. this.rtenv['copyToFS'](path, buffer);
  199. }
  200. Engine.prototype.setPersistentPaths = function(persistentPaths) {
  201. this.persistentPaths = persistentPaths;
  202. };
  203. // Closure compiler exported engine methods.
  204. /** @export */
  205. Engine['isWebGLAvailable'] = Utils.isWebGLAvailable;
  206. Engine['load'] = load;
  207. Engine['unload'] = unload;
  208. Engine.prototype['init'] = Engine.prototype.init;
  209. Engine.prototype['preloadFile'] = Engine.prototype.preloadFile;
  210. Engine.prototype['start'] = Engine.prototype.start;
  211. Engine.prototype['startGame'] = Engine.prototype.startGame;
  212. Engine.prototype['setWebAssemblyFilenameExtension'] = Engine.prototype.setWebAssemblyFilenameExtension;
  213. Engine.prototype['setUnloadAfterInit'] = Engine.prototype.setUnloadAfterInit;
  214. Engine.prototype['setCanvas'] = Engine.prototype.setCanvas;
  215. Engine.prototype['setCanvasResizedOnStart'] = Engine.prototype.setCanvasResizedOnStart;
  216. Engine.prototype['setLocale'] = Engine.prototype.setLocale;
  217. Engine.prototype['setExecutableName'] = Engine.prototype.setExecutableName;
  218. Engine.prototype['setProgressFunc'] = Engine.prototype.setProgressFunc;
  219. Engine.prototype['setStdoutFunc'] = Engine.prototype.setStdoutFunc;
  220. Engine.prototype['setStderrFunc'] = Engine.prototype.setStderrFunc;
  221. Engine.prototype['setOnExecute'] = Engine.prototype.setOnExecute;
  222. Engine.prototype['setOnExit'] = Engine.prototype.setOnExit;
  223. Engine.prototype['copyToFS'] = Engine.prototype.copyToFS;
  224. Engine.prototype['setPersistentPaths'] = Engine.prototype.setPersistentPaths;
  225. return Engine;
  226. })();