2020-10-23 18:33:20 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* library_godot_os.js */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2020-10-23 18:33:20 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
const IDHandler = {
|
|
|
|
$IDHandler: {
|
|
|
|
_last_id: 0,
|
|
|
|
_references: {},
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
get: function (p_id) {
|
2020-10-23 18:33:20 +02:00
|
|
|
return IDHandler._references[p_id];
|
|
|
|
},
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
add: function (p_data) {
|
2020-10-23 18:33:20 +02:00
|
|
|
const id = ++IDHandler._last_id;
|
|
|
|
IDHandler._references[id] = p_data;
|
|
|
|
return id;
|
|
|
|
},
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
remove: function (p_id) {
|
2020-10-23 18:33:20 +02:00
|
|
|
delete IDHandler._references[p_id];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
autoAddDeps(IDHandler, '$IDHandler');
|
2020-10-23 18:33:20 +02:00
|
|
|
mergeInto(LibraryManager.library, IDHandler);
|
|
|
|
|
|
|
|
const GodotConfig = {
|
|
|
|
$GodotConfig__postset: 'Module["initConfig"] = GodotConfig.init_config;',
|
2020-11-19 16:54:07 +01:00
|
|
|
$GodotConfig__deps: ['$GodotRuntime'],
|
2020-10-23 18:33:20 +02:00
|
|
|
$GodotConfig: {
|
|
|
|
canvas: null,
|
2020-11-23 12:13:52 +01:00
|
|
|
locale: 'en',
|
2021-02-07 13:45:04 +01:00
|
|
|
canvas_resize_policy: 2, // Adaptive
|
2021-03-08 23:16:51 +01:00
|
|
|
virtual_keyboard: false,
|
2021-03-12 01:59:16 +01:00
|
|
|
persistent_drops: false,
|
2020-10-23 18:33:20 +02:00
|
|
|
on_execute: null,
|
2021-02-07 13:45:04 +01:00
|
|
|
on_exit: null,
|
2020-10-23 18:33:20 +02:00
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
init_config: function (p_opts) {
|
2021-02-07 13:45:04 +01:00
|
|
|
GodotConfig.canvas_resize_policy = p_opts['canvasResizePolicy'];
|
2020-10-23 18:33:20 +02:00
|
|
|
GodotConfig.canvas = p_opts['canvas'];
|
|
|
|
GodotConfig.locale = p_opts['locale'] || GodotConfig.locale;
|
2021-03-08 23:16:51 +01:00
|
|
|
GodotConfig.virtual_keyboard = p_opts['virtualKeyboard'];
|
2021-03-12 01:59:16 +01:00
|
|
|
GodotConfig.persistent_drops = !!p_opts['persistentDrops'];
|
2020-10-23 18:33:20 +02:00
|
|
|
GodotConfig.on_execute = p_opts['onExecute'];
|
2021-02-07 13:45:04 +01:00
|
|
|
GodotConfig.on_exit = p_opts['onExit'];
|
2021-06-25 19:07:17 +02:00
|
|
|
if (p_opts['focusCanvas']) {
|
|
|
|
GodotConfig.canvas.focus();
|
|
|
|
}
|
2020-11-19 16:54:07 +01:00
|
|
|
},
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
locate_file: function (file) {
|
|
|
|
return Module['locateFile'](file); // eslint-disable-line no-undef
|
2020-10-23 18:33:20 +02:00
|
|
|
},
|
2021-02-07 13:45:04 +01:00
|
|
|
clear: function () {
|
|
|
|
GodotConfig.canvas = null;
|
|
|
|
GodotConfig.locale = 'en';
|
|
|
|
GodotConfig.canvas_resize_policy = 2;
|
2021-03-08 23:16:51 +01:00
|
|
|
GodotConfig.virtual_keyboard = false;
|
2021-03-12 01:59:16 +01:00
|
|
|
GodotConfig.persistent_drops = false;
|
2021-02-07 13:45:04 +01:00
|
|
|
GodotConfig.on_execute = null;
|
|
|
|
GodotConfig.on_exit = null;
|
|
|
|
},
|
2020-10-23 18:33:20 +02:00
|
|
|
},
|
|
|
|
|
2020-12-03 12:51:17 +01:00
|
|
|
godot_js_config_canvas_id_get__sig: 'vii',
|
2020-11-23 12:13:52 +01:00
|
|
|
godot_js_config_canvas_id_get: function (p_ptr, p_ptr_max) {
|
|
|
|
GodotRuntime.stringToHeap(`#${GodotConfig.canvas.id}`, p_ptr, p_ptr_max);
|
2020-10-23 18:33:20 +02:00
|
|
|
},
|
|
|
|
|
2020-12-03 12:51:17 +01:00
|
|
|
godot_js_config_locale_get__sig: 'vii',
|
2020-11-23 12:13:52 +01:00
|
|
|
godot_js_config_locale_get: function (p_ptr, p_ptr_max) {
|
2020-11-19 16:54:07 +01:00
|
|
|
GodotRuntime.stringToHeap(GodotConfig.locale, p_ptr, p_ptr_max);
|
2020-10-23 18:33:20 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
autoAddDeps(GodotConfig, '$GodotConfig');
|
|
|
|
mergeInto(LibraryManager.library, GodotConfig);
|
|
|
|
|
|
|
|
const GodotFS = {
|
2021-08-18 11:30:50 +02:00
|
|
|
$GodotFS__deps: ['$ERRNO_CODES', '$FS', '$IDBFS', '$GodotRuntime'],
|
2020-10-23 18:33:20 +02:00
|
|
|
$GodotFS__postset: [
|
|
|
|
'Module["initFS"] = GodotFS.init;',
|
|
|
|
'Module["copyToFS"] = GodotFS.copy_to_fs;',
|
|
|
|
].join(''),
|
|
|
|
$GodotFS: {
|
|
|
|
_idbfs: false,
|
|
|
|
_syncing: false,
|
|
|
|
_mount_points: [],
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
is_persistent: function () {
|
2020-10-23 18:33:20 +02:00
|
|
|
return GodotFS._idbfs ? 1 : 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Initialize godot file system, setting up persistent paths.
|
|
|
|
// Returns a promise that resolves when the FS is ready.
|
|
|
|
// We keep track of mount_points, so that we can properly close the IDBFS
|
|
|
|
// since emscripten is not doing it by itself. (emscripten GH#12516).
|
2020-11-23 12:13:52 +01:00
|
|
|
init: function (persistentPaths) {
|
2020-10-23 18:33:20 +02:00
|
|
|
GodotFS._idbfs = false;
|
|
|
|
if (!Array.isArray(persistentPaths)) {
|
|
|
|
return Promise.reject(new Error('Persistent paths must be an array'));
|
|
|
|
}
|
|
|
|
if (!persistentPaths.length) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
GodotFS._mount_points = persistentPaths.slice();
|
|
|
|
|
|
|
|
function createRecursive(dir) {
|
|
|
|
try {
|
|
|
|
FS.stat(dir);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.errno !== ERRNO_CODES.ENOENT) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
FS.mkdirTree(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
GodotFS._mount_points.forEach(function (path) {
|
2020-10-23 18:33:20 +02:00
|
|
|
createRecursive(path);
|
|
|
|
FS.mount(IDBFS, {}, path);
|
|
|
|
});
|
2020-11-23 12:13:52 +01:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
FS.syncfs(true, function (err) {
|
2020-10-23 18:33:20 +02:00
|
|
|
if (err) {
|
|
|
|
GodotFS._mount_points = [];
|
|
|
|
GodotFS._idbfs = false;
|
2020-11-23 12:13:52 +01:00
|
|
|
GodotRuntime.print(`IndexedDB not available: ${err.message}`);
|
2020-10-23 18:33:20 +02:00
|
|
|
} else {
|
|
|
|
GodotFS._idbfs = true;
|
|
|
|
}
|
|
|
|
resolve(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Deinit godot file system, making sure to unmount file systems, and close IDBFS(s).
|
2020-11-23 12:13:52 +01:00
|
|
|
deinit: function () {
|
|
|
|
GodotFS._mount_points.forEach(function (path) {
|
2020-10-23 18:33:20 +02:00
|
|
|
try {
|
|
|
|
FS.unmount(path);
|
|
|
|
} catch (e) {
|
2020-11-23 12:13:52 +01:00
|
|
|
GodotRuntime.print('Already unmounted', e);
|
2020-10-23 18:33:20 +02:00
|
|
|
}
|
|
|
|
if (GodotFS._idbfs && IDBFS.dbs[path]) {
|
|
|
|
IDBFS.dbs[path].close();
|
|
|
|
delete IDBFS.dbs[path];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
GodotFS._mount_points = [];
|
|
|
|
GodotFS._idbfs = false;
|
|
|
|
GodotFS._syncing = false;
|
|
|
|
},
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
sync: function () {
|
2020-10-23 18:33:20 +02:00
|
|
|
if (GodotFS._syncing) {
|
2020-11-19 16:54:07 +01:00
|
|
|
GodotRuntime.error('Already syncing!');
|
2020-10-23 18:33:20 +02:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
GodotFS._syncing = true;
|
|
|
|
return new Promise(function (resolve, reject) {
|
2020-11-23 12:13:52 +01:00
|
|
|
FS.syncfs(false, function (error) {
|
2020-10-23 18:33:20 +02:00
|
|
|
if (error) {
|
2020-11-23 12:13:52 +01:00
|
|
|
GodotRuntime.error(`Failed to save IDB file system: ${error.message}`);
|
2020-10-23 18:33:20 +02:00
|
|
|
}
|
|
|
|
GodotFS._syncing = false;
|
|
|
|
resolve(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Copies a buffer to the internal file system. Creating directories recursively.
|
2020-11-23 12:13:52 +01:00
|
|
|
copy_to_fs: function (path, buffer) {
|
|
|
|
const idx = path.lastIndexOf('/');
|
|
|
|
let dir = '/';
|
2020-10-23 18:33:20 +02:00
|
|
|
if (idx > 0) {
|
|
|
|
dir = path.slice(0, idx);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
FS.stat(dir);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.errno !== ERRNO_CODES.ENOENT) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
FS.mkdirTree(dir);
|
|
|
|
}
|
2020-11-30 11:28:25 +01:00
|
|
|
FS.writeFile(path, new Uint8Array(buffer));
|
2020-10-23 18:33:20 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
mergeInto(LibraryManager.library, GodotFS);
|
|
|
|
|
|
|
|
const GodotOS = {
|
2021-02-07 13:45:04 +01:00
|
|
|
$GodotOS__deps: ['$GodotRuntime', '$GodotConfig', '$GodotFS'],
|
2020-10-23 18:33:20 +02:00
|
|
|
$GodotOS__postset: [
|
|
|
|
'Module["request_quit"] = function() { GodotOS.request_quit() };',
|
2021-02-07 13:45:04 +01:00
|
|
|
'Module["onExit"] = GodotOS.cleanup;',
|
2020-10-23 18:33:20 +02:00
|
|
|
'GodotOS._fs_sync_promise = Promise.resolve();',
|
|
|
|
].join(''),
|
|
|
|
$GodotOS: {
|
2020-11-23 12:13:52 +01:00
|
|
|
request_quit: function () {},
|
2020-10-23 18:33:20 +02:00
|
|
|
_async_cbs: [],
|
|
|
|
_fs_sync_promise: null,
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
atexit: function (p_promise_cb) {
|
2020-10-23 18:33:20 +02:00
|
|
|
GodotOS._async_cbs.push(p_promise_cb);
|
|
|
|
},
|
|
|
|
|
2021-02-07 13:45:04 +01:00
|
|
|
cleanup: function (exit_code) {
|
|
|
|
const cb = GodotConfig.on_exit;
|
|
|
|
GodotFS.deinit();
|
|
|
|
GodotConfig.clear();
|
|
|
|
if (cb) {
|
|
|
|
cb(exit_code);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-11-23 12:13:52 +01:00
|
|
|
finish_async: function (callback) {
|
|
|
|
GodotOS._fs_sync_promise.then(function (err) {
|
2020-10-23 18:33:20 +02:00
|
|
|
const promises = [];
|
2020-11-23 12:13:52 +01:00
|
|
|
GodotOS._async_cbs.forEach(function (cb) {
|
2020-10-23 18:33:20 +02:00
|
|
|
promises.push(new Promise(cb));
|
|
|
|
});
|
|
|
|
return Promise.all(promises);
|
2020-11-23 12:13:52 +01:00
|
|
|
}).then(function () {
|
2020-10-23 18:33:20 +02:00
|
|
|
return GodotFS.sync(); // Final FS sync.
|
2020-11-23 12:13:52 +01:00
|
|
|
}).then(function (err) {
|
2020-10-23 18:33:20 +02:00
|
|
|
// Always deferred.
|
2020-11-23 12:13:52 +01:00
|
|
|
setTimeout(function () {
|
2020-10-23 18:33:20 +02:00
|
|
|
callback();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2020-12-03 12:51:17 +01:00
|
|
|
godot_js_os_finish_async__sig: 'vi',
|
2020-11-23 12:13:52 +01:00
|
|
|
godot_js_os_finish_async: function (p_callback) {
|
2020-11-19 16:54:07 +01:00
|
|
|
const func = GodotRuntime.get_func(p_callback);
|
2020-10-23 18:33:20 +02:00
|
|
|
GodotOS.finish_async(func);
|
|
|
|
},
|
|
|
|
|
2020-12-03 12:51:17 +01:00
|
|
|
godot_js_os_request_quit_cb__sig: 'vi',
|
2020-11-23 12:13:52 +01:00
|
|
|
godot_js_os_request_quit_cb: function (p_callback) {
|
2020-11-19 16:54:07 +01:00
|
|
|
GodotOS.request_quit = GodotRuntime.get_func(p_callback);
|
2020-10-23 18:33:20 +02:00
|
|
|
},
|
|
|
|
|
2020-12-03 12:51:17 +01:00
|
|
|
godot_js_os_fs_is_persistent__sig: 'i',
|
2020-11-23 12:13:52 +01:00
|
|
|
godot_js_os_fs_is_persistent: function () {
|
2020-10-23 18:33:20 +02:00
|
|
|
return GodotFS.is_persistent();
|
|
|
|
},
|
|
|
|
|
2020-12-03 12:51:17 +01:00
|
|
|
godot_js_os_fs_sync__sig: 'vi',
|
2020-11-23 12:13:52 +01:00
|
|
|
godot_js_os_fs_sync: function (callback) {
|
2020-11-19 16:54:07 +01:00
|
|
|
const func = GodotRuntime.get_func(callback);
|
2020-10-23 18:33:20 +02:00
|
|
|
GodotOS._fs_sync_promise = GodotFS.sync();
|
2020-11-23 12:13:52 +01:00
|
|
|
GodotOS._fs_sync_promise.then(function (err) {
|
2020-10-23 18:33:20 +02:00
|
|
|
func();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-12-03 12:51:17 +01:00
|
|
|
godot_js_os_execute__sig: 'ii',
|
2020-11-23 12:13:52 +01:00
|
|
|
godot_js_os_execute: function (p_json) {
|
2020-11-19 16:54:07 +01:00
|
|
|
const json_args = GodotRuntime.parseString(p_json);
|
2020-10-23 18:33:20 +02:00
|
|
|
const args = JSON.parse(json_args);
|
|
|
|
if (GodotConfig.on_execute) {
|
|
|
|
GodotConfig.on_execute(args);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
},
|
|
|
|
|
2020-12-03 12:51:17 +01:00
|
|
|
godot_js_os_shell_open__sig: 'vi',
|
2020-11-23 12:13:52 +01:00
|
|
|
godot_js_os_shell_open: function (p_uri) {
|
2020-11-19 16:54:07 +01:00
|
|
|
window.open(GodotRuntime.parseString(p_uri), '_blank');
|
2020-10-23 18:33:20 +02:00
|
|
|
},
|
2021-02-15 15:48:06 +01:00
|
|
|
|
|
|
|
godot_js_os_hw_concurrency_get__sig: 'i',
|
|
|
|
godot_js_os_hw_concurrency_get: function () {
|
|
|
|
return navigator.hardwareConcurrency || 1;
|
|
|
|
},
|
2021-05-19 15:53:04 +02:00
|
|
|
|
|
|
|
godot_js_os_download_buffer__sig: 'viiii',
|
|
|
|
godot_js_os_download_buffer: function (p_ptr, p_size, p_name, p_mime) {
|
|
|
|
const buf = GodotRuntime.heapSlice(HEAP8, p_ptr, p_size);
|
|
|
|
const name = GodotRuntime.parseString(p_name);
|
|
|
|
const mime = GodotRuntime.parseString(p_mime);
|
|
|
|
const blob = new Blob([buf], { type: mime });
|
|
|
|
const url = window.URL.createObjectURL(blob);
|
|
|
|
const a = document.createElement('a');
|
|
|
|
a.href = url;
|
|
|
|
a.download = name;
|
|
|
|
a.style.display = 'none';
|
|
|
|
document.body.appendChild(a);
|
|
|
|
a.click();
|
|
|
|
a.remove();
|
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
|
},
|
2020-10-23 18:33:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
autoAddDeps(GodotOS, '$GodotOS');
|
|
|
|
mergeInto(LibraryManager.library, GodotOS);
|