library_godot_os.js 12 KB

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