library_godot_os.js 15 KB

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