Debian Codegraph @

platform/web/js/libs/library_godot_os.js

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