library_godot_os.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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);
  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__proxy: 'sync',
  86. godot_js_config_canvas_id_get__sig: 'vii',
  87. godot_js_config_canvas_id_get: function (p_ptr, p_ptr_max) {
  88. GodotRuntime.stringToHeap(`#${GodotConfig.canvas.id}`, p_ptr, p_ptr_max);
  89. },
  90. godot_js_config_locale_get__proxy: 'sync',
  91. godot_js_config_locale_get__sig: 'vii',
  92. godot_js_config_locale_get: function (p_ptr, p_ptr_max) {
  93. GodotRuntime.stringToHeap(GodotConfig.locale, p_ptr, p_ptr_max);
  94. },
  95. };
  96. autoAddDeps(GodotConfig, '$GodotConfig');
  97. mergeInto(LibraryManager.library, GodotConfig);
  98. const GodotFS = {
  99. $GodotFS__deps: ['$FS', '$IDBFS', '$GodotRuntime'],
  100. $GodotFS__postset: [
  101. 'Module["initFS"] = GodotFS.init;',
  102. 'Module["copyToFS"] = GodotFS.copy_to_fs;',
  103. ].join(''),
  104. $GodotFS: {
  105. // ERRNO_CODES works every odd version of emscripten, but this will break too eventually.
  106. ENOENT: 44,
  107. _idbfs: false,
  108. _syncing: false,
  109. _mount_points: [],
  110. is_persistent: function () {
  111. return GodotFS._idbfs ? 1 : 0;
  112. },
  113. // Initialize godot file system, setting up persistent paths.
  114. // Returns a promise that resolves when the FS is ready.
  115. // We keep track of mount_points, so that we can properly close the IDBFS
  116. // since emscripten is not doing it by itself. (emscripten GH#12516).
  117. init: function (persistentPaths) {
  118. GodotFS._idbfs = false;
  119. if (!Array.isArray(persistentPaths)) {
  120. return Promise.reject(new Error('Persistent paths must be an array'));
  121. }
  122. if (!persistentPaths.length) {
  123. return Promise.resolve();
  124. }
  125. GodotFS._mount_points = persistentPaths.slice();
  126. function createRecursive(dir) {
  127. try {
  128. FS.stat(dir);
  129. } catch (e) {
  130. if (e.errno !== GodotFS.ENOENT) {
  131. // Let mkdirTree throw in case, we cannot trust the above check.
  132. GodotRuntime.error(e);
  133. }
  134. FS.mkdirTree(dir);
  135. }
  136. }
  137. GodotFS._mount_points.forEach(function (path) {
  138. createRecursive(path);
  139. FS.mount(IDBFS, {}, path);
  140. });
  141. return new Promise(function (resolve, reject) {
  142. FS.syncfs(true, function (err) {
  143. if (err) {
  144. GodotFS._mount_points = [];
  145. GodotFS._idbfs = false;
  146. GodotRuntime.print(`IndexedDB not available: ${err.message}`);
  147. } else {
  148. GodotFS._idbfs = true;
  149. }
  150. resolve(err);
  151. });
  152. });
  153. },
  154. // Deinit godot file system, making sure to unmount file systems, and close IDBFS(s).
  155. deinit: function () {
  156. GodotFS._mount_points.forEach(function (path) {
  157. try {
  158. FS.unmount(path);
  159. } catch (e) {
  160. GodotRuntime.print('Already unmounted', e);
  161. }
  162. if (GodotFS._idbfs && IDBFS.dbs[path]) {
  163. IDBFS.dbs[path].close();
  164. delete IDBFS.dbs[path];
  165. }
  166. });
  167. GodotFS._mount_points = [];
  168. GodotFS._idbfs = false;
  169. GodotFS._syncing = false;
  170. },
  171. sync: function () {
  172. if (GodotFS._syncing) {
  173. GodotRuntime.error('Already syncing!');
  174. return Promise.resolve();
  175. }
  176. GodotFS._syncing = true;
  177. return new Promise(function (resolve, reject) {
  178. FS.syncfs(false, function (error) {
  179. if (error) {
  180. GodotRuntime.error(`Failed to save IDB file system: ${error.message}`);
  181. }
  182. GodotFS._syncing = false;
  183. resolve(error);
  184. });
  185. });
  186. },
  187. // Copies a buffer to the internal file system. Creating directories recursively.
  188. copy_to_fs: function (path, buffer) {
  189. const idx = path.lastIndexOf('/');
  190. let dir = '/';
  191. if (idx > 0) {
  192. dir = path.slice(0, idx);
  193. }
  194. try {
  195. FS.stat(dir);
  196. } catch (e) {
  197. if (e.errno !== GodotFS.ENOENT) {
  198. // Let mkdirTree throw in case, we cannot trust the above check.
  199. GodotRuntime.error(e);
  200. }
  201. FS.mkdirTree(dir);
  202. }
  203. FS.writeFile(path, new Uint8Array(buffer));
  204. },
  205. },
  206. };
  207. mergeInto(LibraryManager.library, GodotFS);
  208. const GodotOS = {
  209. $GodotOS__deps: ['$GodotRuntime', '$GodotConfig', '$GodotFS'],
  210. $GodotOS__postset: [
  211. 'Module["request_quit"] = function() { GodotOS.request_quit() };',
  212. 'Module["onExit"] = GodotOS.cleanup;',
  213. 'GodotOS._fs_sync_promise = Promise.resolve();',
  214. ].join(''),
  215. $GodotOS: {
  216. request_quit: function () {},
  217. _async_cbs: [],
  218. _fs_sync_promise: null,
  219. atexit: function (p_promise_cb) {
  220. GodotOS._async_cbs.push(p_promise_cb);
  221. },
  222. cleanup: function (exit_code) {
  223. const cb = GodotConfig.on_exit;
  224. GodotFS.deinit();
  225. GodotConfig.clear();
  226. if (cb) {
  227. cb(exit_code);
  228. }
  229. },
  230. finish_async: function (callback) {
  231. GodotOS._fs_sync_promise.then(function (err) {
  232. const promises = [];
  233. GodotOS._async_cbs.forEach(function (cb) {
  234. promises.push(new Promise(cb));
  235. });
  236. return Promise.all(promises);
  237. }).then(function () {
  238. return GodotFS.sync(); // Final FS sync.
  239. }).then(function (err) {
  240. // Always deferred.
  241. setTimeout(function () {
  242. callback();
  243. }, 0);
  244. });
  245. },
  246. },
  247. godot_js_os_finish_async__proxy: 'sync',
  248. godot_js_os_finish_async__sig: 'vi',
  249. godot_js_os_finish_async: function (p_callback) {
  250. const func = GodotRuntime.get_func(p_callback);
  251. GodotOS.finish_async(func);
  252. },
  253. godot_js_os_request_quit_cb__proxy: 'sync',
  254. godot_js_os_request_quit_cb__sig: 'vi',
  255. godot_js_os_request_quit_cb: function (p_callback) {
  256. GodotOS.request_quit = GodotRuntime.get_func(p_callback);
  257. },
  258. godot_js_os_fs_is_persistent__proxy: 'sync',
  259. godot_js_os_fs_is_persistent__sig: 'i',
  260. godot_js_os_fs_is_persistent: function () {
  261. return GodotFS.is_persistent();
  262. },
  263. godot_js_os_fs_sync__proxy: 'sync',
  264. godot_js_os_fs_sync__sig: 'vi',
  265. godot_js_os_fs_sync: function (callback) {
  266. const func = GodotRuntime.get_func(callback);
  267. GodotOS._fs_sync_promise = GodotFS.sync();
  268. GodotOS._fs_sync_promise.then(function (err) {
  269. func();
  270. });
  271. },
  272. godot_js_os_has_feature__proxy: 'sync',
  273. godot_js_os_has_feature__sig: 'ii',
  274. godot_js_os_has_feature: function (p_ftr) {
  275. const ftr = GodotRuntime.parseString(p_ftr);
  276. const ua = navigator.userAgent;
  277. if (ftr === 'web_macos') {
  278. return (ua.indexOf('Mac') !== -1) ? 1 : 0;
  279. }
  280. if (ftr === 'web_windows') {
  281. return (ua.indexOf('Windows') !== -1) ? 1 : 0;
  282. }
  283. if (ftr === 'web_android') {
  284. return (ua.indexOf('Android') !== -1) ? 1 : 0;
  285. }
  286. if (ftr === 'web_ios') {
  287. return ((ua.indexOf('iPhone') !== -1) || (ua.indexOf('iPad') !== -1) || (ua.indexOf('iPod') !== -1)) ? 1 : 0;
  288. }
  289. if (ftr === 'web_linuxbsd') {
  290. return ((ua.indexOf('CrOS') !== -1) || (ua.indexOf('BSD') !== -1) || (ua.indexOf('Linux') !== -1) || (ua.indexOf('X11') !== -1)) ? 1 : 0;
  291. }
  292. return 0;
  293. },
  294. godot_js_os_execute__proxy: 'sync',
  295. godot_js_os_execute__sig: 'ii',
  296. godot_js_os_execute: function (p_json) {
  297. const json_args = GodotRuntime.parseString(p_json);
  298. const args = JSON.parse(json_args);
  299. if (GodotConfig.on_execute) {
  300. GodotConfig.on_execute(args);
  301. return 0;
  302. }
  303. return 1;
  304. },
  305. godot_js_os_shell_open__proxy: 'sync',
  306. godot_js_os_shell_open__sig: 'vi',
  307. godot_js_os_shell_open: function (p_uri) {
  308. window.open(GodotRuntime.parseString(p_uri), '_blank');
  309. },
  310. godot_js_os_hw_concurrency_get__proxy: 'sync',
  311. godot_js_os_hw_concurrency_get__sig: 'i',
  312. godot_js_os_hw_concurrency_get: function () {
  313. // TODO Godot core needs fixing to avoid spawning too many threads (> 24).
  314. const concurrency = navigator.hardwareConcurrency || 1;
  315. return concurrency < 2 ? concurrency : 2;
  316. },
  317. godot_js_os_download_buffer__proxy: 'sync',
  318. godot_js_os_download_buffer__sig: 'viiii',
  319. godot_js_os_download_buffer: function (p_ptr, p_size, p_name, p_mime) {
  320. const buf = GodotRuntime.heapSlice(HEAP8, p_ptr, p_size);
  321. const name = GodotRuntime.parseString(p_name);
  322. const mime = GodotRuntime.parseString(p_mime);
  323. const blob = new Blob([buf], { type: mime });
  324. const url = window.URL.createObjectURL(blob);
  325. const a = document.createElement('a');
  326. a.href = url;
  327. a.download = name;
  328. a.style.display = 'none';
  329. document.body.appendChild(a);
  330. a.click();
  331. a.remove();
  332. window.URL.revokeObjectURL(url);
  333. },
  334. };
  335. autoAddDeps(GodotOS, '$GodotOS');
  336. mergeInto(LibraryManager.library, GodotOS);
  337. /*
  338. * Godot event listeners.
  339. * Keeps track of registered event listeners so it can remove them on shutdown.
  340. */
  341. const GodotEventListeners = {
  342. $GodotEventListeners__deps: ['$GodotOS'],
  343. $GodotEventListeners__postset: 'GodotOS.atexit(function(resolve, reject) { GodotEventListeners.clear(); resolve(); });',
  344. $GodotEventListeners: {
  345. handlers: [],
  346. has: function (target, event, method, capture) {
  347. return GodotEventListeners.handlers.findIndex(function (e) {
  348. return e.target === target && e.event === event && e.method === method && e.capture === capture;
  349. }) !== -1;
  350. },
  351. add: function (target, event, method, capture) {
  352. if (GodotEventListeners.has(target, event, method, capture)) {
  353. return;
  354. }
  355. function Handler(p_target, p_event, p_method, p_capture) {
  356. this.target = p_target;
  357. this.event = p_event;
  358. this.method = p_method;
  359. this.capture = p_capture;
  360. }
  361. GodotEventListeners.handlers.push(new Handler(target, event, method, capture));
  362. target.addEventListener(event, method, capture);
  363. },
  364. clear: function () {
  365. GodotEventListeners.handlers.forEach(function (h) {
  366. h.target.removeEventListener(h.event, h.method, h.capture);
  367. });
  368. GodotEventListeners.handlers.length = 0;
  369. },
  370. },
  371. };
  372. mergeInto(LibraryManager.library, GodotEventListeners);
  373. const GodotPWA = {
  374. $GodotPWA__deps: ['$GodotRuntime', '$GodotEventListeners'],
  375. $GodotPWA: {
  376. hasUpdate: false,
  377. updateState: function (cb, reg) {
  378. if (!reg) {
  379. return;
  380. }
  381. if (!reg.active) {
  382. return;
  383. }
  384. if (reg.waiting) {
  385. GodotPWA.hasUpdate = true;
  386. cb();
  387. }
  388. GodotEventListeners.add(reg, 'updatefound', function () {
  389. const installing = reg.installing;
  390. GodotEventListeners.add(installing, 'statechange', function () {
  391. if (installing.state === 'installed') {
  392. GodotPWA.hasUpdate = true;
  393. cb();
  394. }
  395. });
  396. });
  397. },
  398. },
  399. godot_js_pwa_cb__proxy: 'sync',
  400. godot_js_pwa_cb__sig: 'vi',
  401. godot_js_pwa_cb: function (p_update_cb) {
  402. if ('serviceWorker' in navigator) {
  403. try {
  404. const cb = GodotRuntime.get_func(p_update_cb);
  405. navigator.serviceWorker.getRegistration().then(GodotPWA.updateState.bind(null, cb));
  406. } catch (e) {
  407. GodotRuntime.error('Failed to assign PWA callback', e);
  408. }
  409. }
  410. },
  411. godot_js_pwa_update__proxy: 'sync',
  412. godot_js_pwa_update__sig: 'i',
  413. godot_js_pwa_update: function () {
  414. if ('serviceWorker' in navigator && GodotPWA.hasUpdate) {
  415. try {
  416. navigator.serviceWorker.getRegistration().then(function (reg) {
  417. if (!reg || !reg.waiting) {
  418. return;
  419. }
  420. reg.waiting.postMessage('update');
  421. });
  422. } catch (e) {
  423. GodotRuntime.error(e);
  424. return 1;
  425. }
  426. return 0;
  427. }
  428. return 1;
  429. },
  430. };
  431. autoAddDeps(GodotPWA, '$GodotPWA');
  432. mergeInto(LibraryManager.library, GodotPWA);