2020-10-17 21:31:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* library_godot_os.js */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2020-10-17 21:31:30 +00: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:27:13 +00:00
|
|
|
get: function (p_id) {
|
2020-10-17 21:31:30 +00:00
|
|
|
return IDHandler._references[p_id];
|
|
|
|
},
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
add: function (p_data) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const id = ++IDHandler._last_id;
|
|
|
|
IDHandler._references[id] = p_data;
|
|
|
|
return id;
|
|
|
|
},
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
remove: function (p_id) {
|
2020-10-17 21:31:30 +00:00
|
|
|
delete IDHandler._references[p_id];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
autoAddDeps(IDHandler, '$IDHandler');
|
2020-10-17 21:31:30 +00:00
|
|
|
mergeInto(LibraryManager.library, IDHandler);
|
|
|
|
|
|
|
|
const GodotConfig = {
|
|
|
|
$GodotConfig__postset: 'Module["initConfig"] = GodotConfig.init_config;',
|
2020-11-19 15:54:07 +00:00
|
|
|
$GodotConfig__deps: ['$GodotRuntime'],
|
2020-10-17 21:31:30 +00:00
|
|
|
$GodotConfig: {
|
|
|
|
canvas: null,
|
2020-11-23 12:27:13 +00:00
|
|
|
locale: 'en',
|
2020-10-17 21:31:30 +00:00
|
|
|
resize_on_start: false,
|
|
|
|
on_execute: null,
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
init_config: function (p_opts) {
|
|
|
|
GodotConfig.resize_on_start = !!p_opts['resizeCanvasOnStart'];
|
2020-10-17 21:31:30 +00:00
|
|
|
GodotConfig.canvas = p_opts['canvas'];
|
|
|
|
GodotConfig.locale = p_opts['locale'] || GodotConfig.locale;
|
|
|
|
GodotConfig.on_execute = p_opts['onExecute'];
|
|
|
|
// This is called by emscripten, even if undocumented.
|
2020-11-19 15:54:07 +00:00
|
|
|
Module['onExit'] = p_opts['onExit']; // eslint-disable-line no-undef
|
|
|
|
},
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
locate_file: function (file) {
|
|
|
|
return Module['locateFile'](file); // eslint-disable-line no-undef
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_config_canvas_id_get__sig: 'vii',
|
2020-11-23 12:27:13 +00: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-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_config_locale_get__sig: 'vii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_config_locale_get: function (p_ptr, p_ptr_max) {
|
2020-11-19 15:54:07 +00:00
|
|
|
GodotRuntime.stringToHeap(GodotConfig.locale, p_ptr, p_ptr_max);
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_config_is_resize_on_start__sig: 'i',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_config_is_resize_on_start: function () {
|
2020-10-17 21:31:30 +00:00
|
|
|
return GodotConfig.resize_on_start ? 1 : 0;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
autoAddDeps(GodotConfig, '$GodotConfig');
|
|
|
|
mergeInto(LibraryManager.library, GodotConfig);
|
|
|
|
|
|
|
|
const GodotFS = {
|
2020-11-19 15:54:07 +00:00
|
|
|
$GodotFS__deps: ['$FS', '$IDBFS', '$GodotRuntime'],
|
2020-10-17 21:31:30 +00:00
|
|
|
$GodotFS__postset: [
|
|
|
|
'Module["initFS"] = GodotFS.init;',
|
|
|
|
'Module["deinitFS"] = GodotFS.deinit;',
|
|
|
|
'Module["copyToFS"] = GodotFS.copy_to_fs;',
|
|
|
|
].join(''),
|
|
|
|
$GodotFS: {
|
|
|
|
_idbfs: false,
|
|
|
|
_syncing: false,
|
|
|
|
_mount_points: [],
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
is_persistent: function () {
|
2020-10-17 21:31:30 +00: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:27:13 +00:00
|
|
|
init: function (persistentPaths) {
|
2020-10-17 21:31:30 +00: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:27:13 +00:00
|
|
|
GodotFS._mount_points.forEach(function (path) {
|
2020-10-17 21:31:30 +00:00
|
|
|
createRecursive(path);
|
|
|
|
FS.mount(IDBFS, {}, path);
|
|
|
|
});
|
2020-11-23 12:27:13 +00:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
FS.syncfs(true, function (err) {
|
2020-10-17 21:31:30 +00:00
|
|
|
if (err) {
|
|
|
|
GodotFS._mount_points = [];
|
|
|
|
GodotFS._idbfs = false;
|
2020-11-23 12:27:13 +00:00
|
|
|
GodotRuntime.print(`IndexedDB not available: ${err.message}`);
|
2020-10-17 21:31:30 +00: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:27:13 +00:00
|
|
|
deinit: function () {
|
|
|
|
GodotFS._mount_points.forEach(function (path) {
|
2020-10-17 21:31:30 +00:00
|
|
|
try {
|
|
|
|
FS.unmount(path);
|
|
|
|
} catch (e) {
|
2020-11-23 12:27:13 +00:00
|
|
|
GodotRuntime.print('Already unmounted', e);
|
2020-10-17 21:31:30 +00: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:27:13 +00:00
|
|
|
sync: function () {
|
2020-10-17 21:31:30 +00:00
|
|
|
if (GodotFS._syncing) {
|
2020-11-19 15:54:07 +00:00
|
|
|
GodotRuntime.error('Already syncing!');
|
2020-10-17 21:31:30 +00:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
GodotFS._syncing = true;
|
|
|
|
return new Promise(function (resolve, reject) {
|
2020-11-23 12:27:13 +00:00
|
|
|
FS.syncfs(false, function (error) {
|
2020-10-17 21:31:30 +00:00
|
|
|
if (error) {
|
2020-11-23 12:27:13 +00:00
|
|
|
GodotRuntime.error(`Failed to save IDB file system: ${error.message}`);
|
2020-10-17 21:31:30 +00:00
|
|
|
}
|
|
|
|
GodotFS._syncing = false;
|
|
|
|
resolve(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Copies a buffer to the internal file system. Creating directories recursively.
|
2020-11-23 12:27:13 +00:00
|
|
|
copy_to_fs: function (path, buffer) {
|
|
|
|
const idx = path.lastIndexOf('/');
|
|
|
|
let dir = '/';
|
2020-10-17 21:31:30 +00: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 09:32:54 +00:00
|
|
|
FS.writeFile(path, new Uint8Array(buffer));
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
mergeInto(LibraryManager.library, GodotFS);
|
|
|
|
|
|
|
|
const GodotOS = {
|
2020-11-19 15:54:07 +00:00
|
|
|
$GodotOS__deps: ['$GodotFS', '$GodotRuntime'],
|
2020-10-17 21:31:30 +00:00
|
|
|
$GodotOS__postset: [
|
|
|
|
'Module["request_quit"] = function() { GodotOS.request_quit() };',
|
|
|
|
'GodotOS._fs_sync_promise = Promise.resolve();',
|
|
|
|
].join(''),
|
|
|
|
$GodotOS: {
|
2020-11-23 12:27:13 +00:00
|
|
|
request_quit: function () {},
|
2020-10-17 21:31:30 +00:00
|
|
|
_async_cbs: [],
|
|
|
|
_fs_sync_promise: null,
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
atexit: function (p_promise_cb) {
|
2020-10-17 21:31:30 +00:00
|
|
|
GodotOS._async_cbs.push(p_promise_cb);
|
|
|
|
},
|
|
|
|
|
2020-11-23 12:27:13 +00:00
|
|
|
finish_async: function (callback) {
|
|
|
|
GodotOS._fs_sync_promise.then(function (err) {
|
2020-10-17 21:31:30 +00:00
|
|
|
const promises = [];
|
2020-11-23 12:27:13 +00:00
|
|
|
GodotOS._async_cbs.forEach(function (cb) {
|
2020-10-17 21:31:30 +00:00
|
|
|
promises.push(new Promise(cb));
|
|
|
|
});
|
|
|
|
return Promise.all(promises);
|
2020-11-23 12:27:13 +00:00
|
|
|
}).then(function () {
|
2020-10-17 21:31:30 +00:00
|
|
|
return GodotFS.sync(); // Final FS sync.
|
2020-11-23 12:27:13 +00:00
|
|
|
}).then(function (err) {
|
2020-10-17 21:31:30 +00:00
|
|
|
// Always deferred.
|
2020-11-23 12:27:13 +00:00
|
|
|
setTimeout(function () {
|
2020-10-17 21:31:30 +00:00
|
|
|
callback();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_os_finish_async__sig: 'vi',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_os_finish_async: function (p_callback) {
|
2020-11-19 15:54:07 +00:00
|
|
|
const func = GodotRuntime.get_func(p_callback);
|
2020-10-17 21:31:30 +00:00
|
|
|
GodotOS.finish_async(func);
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_os_request_quit_cb__sig: 'vi',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_os_request_quit_cb: function (p_callback) {
|
2020-11-19 15:54:07 +00:00
|
|
|
GodotOS.request_quit = GodotRuntime.get_func(p_callback);
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_os_fs_is_persistent__sig: 'i',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_os_fs_is_persistent: function () {
|
2020-10-17 21:31:30 +00:00
|
|
|
return GodotFS.is_persistent();
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_os_fs_sync__sig: 'vi',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_os_fs_sync: function (callback) {
|
2020-11-19 15:54:07 +00:00
|
|
|
const func = GodotRuntime.get_func(callback);
|
2020-10-17 21:31:30 +00:00
|
|
|
GodotOS._fs_sync_promise = GodotFS.sync();
|
2020-11-23 12:27:13 +00:00
|
|
|
GodotOS._fs_sync_promise.then(function (err) {
|
2020-10-17 21:31:30 +00:00
|
|
|
func();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_os_execute__sig: 'ii',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_os_execute: function (p_json) {
|
2020-11-19 15:54:07 +00:00
|
|
|
const json_args = GodotRuntime.parseString(p_json);
|
2020-10-17 21:31:30 +00:00
|
|
|
const args = JSON.parse(json_args);
|
|
|
|
if (GodotConfig.on_execute) {
|
|
|
|
GodotConfig.on_execute(args);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
},
|
|
|
|
|
2020-12-03 11:51:17 +00:00
|
|
|
godot_js_os_shell_open__sig: 'vi',
|
2020-11-23 12:27:13 +00:00
|
|
|
godot_js_os_shell_open: function (p_uri) {
|
2020-11-19 15:54:07 +00:00
|
|
|
window.open(GodotRuntime.parseString(p_uri), '_blank');
|
2020-10-17 21:31:30 +00:00
|
|
|
},
|
2021-02-15 14:48:06 +00:00
|
|
|
|
|
|
|
godot_js_os_hw_concurrency_get__sig: 'i',
|
|
|
|
godot_js_os_hw_concurrency_get: function () {
|
|
|
|
return navigator.hardwareConcurrency || 1;
|
|
|
|
},
|
2020-10-17 21:31:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
autoAddDeps(GodotOS, '$GodotOS');
|
|
|
|
mergeInto(LibraryManager.library, GodotOS);
|