library_godot_os.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /**************************************************************************/
  2. /* library_godot_os.js */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. const IDHandler = {
  31. $IDHandler: {
  32. _last_id: 0,
  33. _references: {},
  34. get: function (p_id) {
  35. return IDHandler._references[p_id];
  36. },
  37. add: function (p_data) {
  38. const id = ++IDHandler._last_id;
  39. IDHandler._references[id] = p_data;
  40. return id;
  41. },
  42. remove: function (p_id) {
  43. delete IDHandler._references[p_id];
  44. },
  45. },
  46. };
  47. autoAddDeps(IDHandler, '$IDHandler');
  48. mergeInto(LibraryManager.library, IDHandler);
  49. const GodotConfig = {
  50. $GodotConfig__postset: 'Module["initConfig"] = GodotConfig.init_config;',
  51. $GodotConfig__deps: ['$GodotRuntime'],
  52. $GodotConfig: {
  53. canvas: null,
  54. locale: 'en',
  55. canvas_resize_policy: 2, // Adaptive
  56. virtual_keyboard: false,
  57. persistent_drops: false,
  58. on_execute: null,
  59. on_exit: null,
  60. init_config: function (p_opts) {
  61. GodotConfig.canvas_resize_policy = p_opts['canvasResizePolicy'];
  62. GodotConfig.canvas = p_opts['canvas'];
  63. GodotConfig.locale = p_opts['locale'] || GodotConfig.locale;
  64. GodotConfig.virtual_keyboard = p_opts['virtualKeyboard'];
  65. GodotConfig.persistent_drops = !!p_opts['persistentDrops'];
  66. GodotConfig.on_execute = p_opts['onExecute'];
  67. GodotConfig.on_exit = p_opts['onExit'];
  68. if (p_opts['focusCanvas']) {
  69. GodotConfig.canvas.focus();
  70. }
  71. },
  72. locate_file: function (file) {
  73. return Module['locateFile'](file); // eslint-disable-line no-undef
  74. },
  75. clear: function () {
  76. GodotConfig.canvas = null;
  77. GodotConfig.locale = 'en';
  78. GodotConfig.canvas_resize_policy = 2;
  79. GodotConfig.virtual_keyboard = false;
  80. GodotConfig.persistent_drops = false;
  81. GodotConfig.on_execute = null;
  82. GodotConfig.on_exit = null;
  83. },
  84. },
  85. godot_js_config_canvas_id_get__sig: 'vii',
  86. godot_js_config_canvas_id_get: function (p_ptr, p_ptr_max) {
  87. GodotRuntime.stringToHeap(`#${GodotConfig.canvas.id}`, p_ptr, p_ptr_max);
  88. },
  89. godot_js_config_locale_get__sig: 'vii',
  90. godot_js_config_locale_get: function (p_ptr, p_ptr_max) {
  91. GodotRuntime.stringToHeap(GodotConfig.locale, p_ptr, p_ptr_max);
  92. },
  93. };
  94. autoAddDeps(GodotConfig, '$GodotConfig');
  95. mergeInto(LibraryManager.library, GodotConfig);
  96. const GodotFS = {
  97. $GodotFS__deps: ['$FS', '$IDBFS', '$GodotRuntime'],
  98. $GodotFS__postset: [
  99. 'Module["initFS"] = GodotFS.init;',
  100. 'Module["copyToFS"] = GodotFS.copy_to_fs;',
  101. ].join(''),
  102. $GodotFS: {
  103. // ERRNO_CODES works every odd version of emscripten, but this will break too eventually.
  104. ENOENT: 44,
  105. _idbfs: false,
  106. _syncing: false,
  107. _mount_points: [],
  108. is_persistent: function () {
  109. return GodotFS._idbfs ? 1 : 0;
  110. },
  111. // Initialize godot file system, setting up persistent paths.
  112. // Returns a promise that resolves when the FS is ready.
  113. // We keep track of mount_points, so that we can properly close the IDBFS
  114. // since emscripten is not doing it by itself. (emscripten GH#12516).
  115. init: function (persistentPaths) {
  116. GodotFS._idbfs = false;
  117. if (!Array.isArray(persistentPaths)) {
  118. return Promise.reject(new Error('Persistent paths must be an array'));
  119. }
  120. if (!persistentPaths.length) {
  121. return Promise.resolve();
  122. }
  123. GodotFS._mount_points = persistentPaths.slice();
  124. function createRecursive(dir) {
  125. try {
  126. FS.stat(dir);
  127. } catch (e) {
  128. if (e.errno !== GodotFS.ENOENT) {
  129. // Let mkdirTree throw in case, we cannot trust the above check.
  130. GodotRuntime.error(e);
  131. }
  132. FS.mkdirTree(dir);
  133. }
  134. }
  135. GodotFS._mount_points.forEach(function (path) {
  136. createRecursive(path);
  137. FS.mount(IDBFS, {}, path);
  138. });
  139. return new Promise(function (resolve, reject) {
  140. FS.syncfs(true, function (err) {
  141. if (err) {
  142. GodotFS._mount_points = [];
  143. GodotFS._idbfs = false;
  144. GodotRuntime.print(`IndexedDB not available: ${err.message}`);
  145. } else {
  146. GodotFS._idbfs = true;
  147. }
  148. resolve(err);
  149. });
  150. });
  151. },
  152. // Deinit godot file system, making sure to unmount file systems, and close IDBFS(s).
  153. deinit: function () {
  154. GodotFS._mount_points.forEach(function (path) {
  155. try {
  156. FS.unmount(path);
  157. } catch (e) {
  158. GodotRuntime.print('Already unmounted', e);
  159. }
  160. if (GodotFS._idbfs && IDBFS.dbs[path]) {
  161. IDBFS.dbs[path].close();
  162. delete IDBFS.dbs[path];
  163. }
  164. });
  165. GodotFS._mount_points = [];
  166. GodotFS._idbfs = false;
  167. GodotFS._syncing = false;
  168. },
  169. sync: function () {
  170. if (GodotFS._syncing) {
  171. GodotRuntime.error('Already syncing!');
  172. return Promise.resolve();
  173. }
  174. GodotFS._syncing = true;
  175. return new Promise(function (resolve, reject) {
  176. FS.syncfs(false, function (error) {
  177. if (error) {
  178. GodotRuntime.error(`Failed to save IDB file system: ${error.message}`);
  179. }
  180. GodotFS._syncing = false;
  181. resolve(error);
  182. });
  183. });
  184. },
  185. // Copies a buffer to the internal file system. Creating directories recursively.
  186. copy_to_fs: function (path, buffer) {
  187. const idx = path.lastIndexOf('/');
  188. let dir = '/';
  189. if (idx > 0) {
  190. dir = path.slice(0, idx);
  191. }
  192. try {
  193. FS.stat(dir);
  194. } catch (e) {
  195. if (e.errno !== GodotFS.ENOENT) {
  196. // Let mkdirTree throw in case, we cannot trust the above check.
  197. GodotRuntime.error(e);
  198. }
  199. FS.mkdirTree(dir);
  200. }
  201. FS.writeFile(path, new Uint8Array(buffer));
  202. },
  203. },
  204. };
  205. mergeInto(LibraryManager.library, GodotFS);
  206. const GodotOS = {
  207. $GodotOS__deps: ['$GodotRuntime', '$GodotConfig', '$GodotFS'],
  208. $GodotOS__postset: [
  209. 'Module["request_quit"] = function() { GodotOS.request_quit() };',
  210. 'Module["onExit"] = GodotOS.cleanup;',
  211. 'GodotOS._fs_sync_promise = Promise.resolve();',
  212. ].join(''),
  213. $GodotOS: {
  214. request_quit: function () {},
  215. _async_cbs: [],
  216. _fs_sync_promise: null,
  217. atexit: function (p_promise_cb) {
  218. GodotOS._async_cbs.push(p_promise_cb);
  219. },
  220. cleanup: function (exit_code) {
  221. const cb = GodotConfig.on_exit;
  222. GodotFS.deinit();
  223. GodotConfig.clear();
  224. if (cb) {
  225. cb(exit_code);
  226. }
  227. },
  228. finish_async: function (callback) {
  229. GodotOS._fs_sync_promise.then(function (err) {
  230. const promises = [];
  231. GodotOS._async_cbs.forEach(function (cb) {
  232. promises.push(new Promise(cb));
  233. });
  234. return Promise.all(promises);
  235. }).then(function () {
  236. return GodotFS.sync(); // Final FS sync.
  237. }).then(function (err) {
  238. // Always deferred.
  239. setTimeout(function () {
  240. callback();
  241. }, 0);
  242. });
  243. },
  244. },
  245. godot_js_os_finish_async__sig: 'vi',
  246. godot_js_os_finish_async: function (p_callback) {
  247. const func = GodotRuntime.get_func(p_callback);
  248. GodotOS.finish_async(func);
  249. },
  250. godot_js_os_request_quit_cb__sig: 'vi',
  251. godot_js_os_request_quit_cb: function (p_callback) {
  252. GodotOS.request_quit = GodotRuntime.get_func(p_callback);
  253. },
  254. godot_js_os_fs_is_persistent__sig: 'i',
  255. godot_js_os_fs_is_persistent: function () {
  256. return GodotFS.is_persistent();
  257. },
  258. godot_js_os_fs_sync__sig: 'vi',
  259. godot_js_os_fs_sync: function (callback) {
  260. const func = GodotRuntime.get_func(callback);
  261. GodotOS._fs_sync_promise = GodotFS.sync();
  262. GodotOS._fs_sync_promise.then(function (err) {
  263. func();
  264. });
  265. },
  266. godot_js_os_has_feature__sig: 'ii',
  267. godot_js_os_has_feature: function (p_ftr) {
  268. const ftr = GodotRuntime.parseString(p_ftr);
  269. const ua = navigator.userAgent;
  270. if (ftr === 'web_macos') {
  271. return (ua.indexOf('Mac') !== -1) ? 1 : 0;
  272. }
  273. if (ftr === 'web_windows') {
  274. return (ua.indexOf('Windows') !== -1) ? 1 : 0;
  275. }
  276. if (ftr === 'web_android') {
  277. return (ua.indexOf('Android') !== -1) ? 1 : 0;
  278. }
  279. if (ftr === 'web_ios') {
  280. return ((ua.indexOf('iPhone') !== -1) || (ua.indexOf('iPad') !== -1) || (ua.indexOf('iPod') !== -1)) ? 1 : 0;
  281. }
  282. if (ftr === 'web_linuxbsd') {
  283. return ((ua.indexOf('CrOS') !== -1) || (ua.indexOf('BSD') !== -1) || (ua.indexOf('Linux') !== -1) || (ua.indexOf('X11') !== -1)) ? 1 : 0;
  284. }
  285. return 0;
  286. },
  287. godot_js_os_execute__sig: 'ii',
  288. godot_js_os_execute: function (p_json) {
  289. const json_args = GodotRuntime.parseString(p_json);
  290. const args = JSON.parse(json_args);
  291. if (GodotConfig.on_execute) {
  292. GodotConfig.on_execute(args);
  293. return 0;
  294. }
  295. return 1;
  296. },
  297. godot_js_os_shell_open__sig: 'vi',
  298. godot_js_os_shell_open: function (p_uri) {
  299. window.open(GodotRuntime.parseString(p_uri), '_blank');
  300. },
  301. godot_js_os_hw_concurrency_get__sig: 'i',
  302. godot_js_os_hw_concurrency_get: function () {
  303. // TODO Godot core needs fixing to avoid spawning too many threads (> 24).
  304. const concurrency = navigator.hardwareConcurrency || 1;
  305. return concurrency < 2 ? concurrency : 2;
  306. },
  307. godot_js_os_download_buffer__sig: 'viiii',
  308. godot_js_os_download_buffer: function (p_ptr, p_size, p_name, p_mime) {
  309. const buf = GodotRuntime.heapSlice(HEAP8, p_ptr, p_size);
  310. const name = GodotRuntime.parseString(p_name);
  311. const mime = GodotRuntime.parseString(p_mime);
  312. const blob = new Blob([buf], { type: mime });
  313. const url = window.URL.createObjectURL(blob);
  314. const a = document.createElement('a');
  315. a.href = url;
  316. a.download = name;
  317. a.style.display = 'none';
  318. document.body.appendChild(a);
  319. a.click();
  320. a.remove();
  321. window.URL.revokeObjectURL(url);
  322. },
  323. };
  324. autoAddDeps(GodotOS, '$GodotOS');
  325. mergeInto(LibraryManager.library, GodotOS);
  326. /*
  327. * Godot event listeners.
  328. * Keeps track of registered event listeners so it can remove them on shutdown.
  329. */
  330. const GodotEventListeners = {
  331. $GodotEventListeners__deps: ['$GodotOS'],
  332. $GodotEventListeners__postset: 'GodotOS.atexit(function(resolve, reject) { GodotEventListeners.clear(); resolve(); });',
  333. $GodotEventListeners: {
  334. handlers: [],
  335. has: function (target, event, method, capture) {
  336. return GodotEventListeners.handlers.findIndex(function (e) {
  337. return e.target === target && e.event === event && e.method === method && e.capture === capture;
  338. }) !== -1;
  339. },
  340. add: function (target, event, method, capture) {
  341. if (GodotEventListeners.has(target, event, method, capture)) {
  342. return;
  343. }
  344. function Handler(p_target, p_event, p_method, p_capture) {
  345. this.target = p_target;
  346. this.event = p_event;
  347. this.method = p_method;
  348. this.capture = p_capture;
  349. }
  350. GodotEventListeners.handlers.push(new Handler(target, event, method, capture));
  351. target.addEventListener(event, method, capture);
  352. },
  353. clear: function () {
  354. GodotEventListeners.handlers.forEach(function (h) {
  355. h.target.removeEventListener(h.event, h.method, h.capture);
  356. });
  357. GodotEventListeners.handlers.length = 0;
  358. },
  359. },
  360. };
  361. mergeInto(LibraryManager.library, GodotEventListeners);
  362. const GodotPWA = {
  363. $GodotPWA__deps: ['$GodotRuntime', '$GodotEventListeners'],
  364. $GodotPWA: {
  365. hasUpdate: false,
  366. updateState: function (cb, reg) {
  367. if (!reg) {
  368. return;
  369. }
  370. if (!reg.active) {
  371. return;
  372. }
  373. if (reg.waiting) {
  374. GodotPWA.hasUpdate = true;
  375. cb();
  376. }
  377. GodotEventListeners.add(reg, 'updatefound', function () {
  378. const installing = reg.installing;
  379. GodotEventListeners.add(installing, 'statechange', function () {
  380. if (installing.state === 'installed') {
  381. GodotPWA.hasUpdate = true;
  382. cb();
  383. }
  384. });
  385. });
  386. },
  387. },
  388. godot_js_pwa_cb__sig: 'vi',
  389. godot_js_pwa_cb: function (p_update_cb) {
  390. if ('serviceWorker' in navigator) {
  391. const cb = GodotRuntime.get_func(p_update_cb);
  392. navigator.serviceWorker.getRegistration().then(GodotPWA.updateState.bind(null, cb));
  393. }
  394. },
  395. godot_js_pwa_update__sig: 'i',
  396. godot_js_pwa_update: function () {
  397. if ('serviceWorker' in navigator && GodotPWA.hasUpdate) {
  398. navigator.serviceWorker.getRegistration().then(function (reg) {
  399. if (!reg || !reg.waiting) {
  400. return;
  401. }
  402. reg.waiting.postMessage('update');
  403. });
  404. return 0;
  405. }
  406. return 1;
  407. },
  408. };
  409. autoAddDeps(GodotPWA, '$GodotPWA');
  410. mergeInto(LibraryManager.library, GodotPWA);