Godot Engine – Multi-platform 2D and 3D game engine
web: Fix onExecute, add pid, onTerminatePID config options
Fabio Alessandrelliec322ab6b294
diff --git a/platform/web/godot_js.h b/platform/web/godot_js.hindex 29b291d566a..30cc1df979f 100644--- a/platform/web/godot_js.h+++ b/platform/web/godot_js.h@@ -42,6 +42,7 @@ extern "C" {extern char *godot_js_emscripten_get_version();// Config+extern int godot_js_config_pid_get();extern void godot_js_config_locale_get(char *p_ptr, int p_ptr_max);extern void godot_js_config_canvas_id_get(char *p_ptr, int p_ptr_max);@@ -50,7 +51,8 @@ extern void godot_js_os_finish_async(void (*p_callback)());extern void godot_js_os_request_quit_cb(void (*p_callback)());extern int godot_js_os_fs_is_persistent();extern void godot_js_os_fs_sync(void (*p_callback)());-extern int godot_js_os_execute(const char *p_json);+extern int godot_js_os_execute(const char *p_path, const char *p_json_args);+extern int godot_js_os_kill(int p_pid);extern void godot_js_os_shell_open(const char *p_uri);extern int godot_js_os_hw_concurrency_get();extern int godot_js_os_thread_pool_size_get();diff --git a/platform/web/js/engine/config.js b/platform/web/js/engine/config.jsindex b800a948fde..ad52a2ae973 100644--- a/platform/web/js/engine/config.js+++ b/platform/web/js/engine/config.js@@ -125,9 +125,9 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-gdextensionLibs: [],/*** @ignore- * @type {Array.<string>}+ * @type {Object<string, number>}*/- fileSizes: [],+ fileSizes: {},/*** @ignore* @type {number}@@ -145,6 +145,13 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-* @type {?MessagePort}*/debugPort: null,+ /**+ * The Process ID assigned to this instance (useful for debugging).+ * @memberof EngineConfig+ * @default+ * @type {number}+ */+ pid: 0,/*** A callback function for handling Godot's ``OS.execute`` calls.*@@ -156,9 +163,22 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-*//*** @ignore- * @type {?function(string, Array.<string>)}+ * @type {?function(string, Array.<string>):?number}*/onExecute: null,+ /**+ * A callback function for handling Godot's ``OS.kill`` calls.+ *+ * This is for example used in the Web Editor template to forcefully terminate a running game instance.+ *+ * @callback EngineConfig.onTerminatePID+ * @param {number} pid The Process ID to terminate.+ */+ /**+ * @ignore+ * @type {?function(number)}+ */+ onTerminatePID: null,/*** A callback function for being notified when the Godot instance quits.*@@ -272,9 +292,11 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-this.fileSizes = parse('fileSizes', this.fileSizes);this.emscriptenPoolSize = parse('emscriptenPoolSize', this.emscriptenPoolSize);this.godotPoolSize = parse('godotPoolSize', this.godotPoolSize);+ this.pid = parse('pid', this.pid);this.args = parse('args', this.args);this.debugPort = parse('debugPort', this.debugPort);this.onExecute = parse('onExecute', this.onExecute);+ this.onTerminatePID = parse('onTerminatePID', this.onTerminatePID);this.onExit = parse('onExit', this.onExit);};@@ -360,6 +382,7 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-// Godot configuration.return {+ 'pid': this.pid,'canvas': this.canvas,'canvasResizePolicy': this.canvasResizePolicy,'locale': locale,@@ -368,6 +391,7 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-'focusCanvas': this.focusCanvas,'debugPort': this.debugPort,'onExecute': this.onExecute,+ 'onTerminatePID': this.onTerminatePID,'onExit': function (p_code) {cleanup(); // We always need to call the cleanup callback to free memory.if (typeof (onExit) === 'function') {diff --git a/platform/web/js/libs/library_godot_os.js b/platform/web/js/libs/library_godot_os.jsindex 9898ea9fa44..6773306bc1f 100644--- a/platform/web/js/libs/library_godot_os.js+++ b/platform/web/js/libs/library_godot_os.js@@ -63,7 +63,9 @@ const GodotConfig = {godot_pool_size: 4,debug_port: null,on_execute: null,+ on_terminate_pid: null,on_exit: null,+ pid: 0,init_config: function (p_opts) {GodotConfig.canvas_resize_policy = p_opts['canvasResizePolicy'];@@ -72,7 +74,9 @@ const GodotConfig = {GodotConfig.virtual_keyboard = p_opts['virtualKeyboard'];GodotConfig.godot_pool_size = p_opts['godotPoolSize'];GodotConfig.debug_port = p_opts['debugPort'];+ GodotConfig.pid = p_opts['pid'];GodotConfig.on_execute = p_opts['onExecute'];+ GodotConfig.on_terminate_pid = p_opts['onTerminatePID'];GodotConfig.on_exit = p_opts['onExit'];if (p_opts['focusCanvas']) {GodotConfig.canvas.focus();@@ -94,6 +98,12 @@ const GodotConfig = {},},+ godot_js_config_pid_get__proxy: 'sync',+ godot_js_config_pid_get__sig: 'i',+ godot_js_config_pid_get: function () {+ return GodotConfig.pid;+ },+godot_js_config_canvas_id_get__proxy: 'sync',godot_js_config_canvas_id_get__sig: 'vii',godot_js_config_canvas_id_get: function (p_ptr, p_ptr_max) {@@ -324,15 +334,25 @@ const GodotOS = {},godot_js_os_execute__proxy: 'sync',- godot_js_os_execute__sig: 'ii',- godot_js_os_execute: function (p_json) {- const json_args = GodotRuntime.parseString(p_json);+ godot_js_os_execute__sig: 'ipp',+ godot_js_os_execute: function (p_path, p_args) {+ const path = GodotRuntime.parseString(p_path);+ const json_args = GodotRuntime.parseString(p_args);const args = JSON.parse(json_args);if (GodotConfig.on_execute) {- GodotConfig.on_execute(args);- return 0;+ return GodotConfig.on_execute(path, args) ?? 0;}- return 1;+ return -1;+ },++ godot_js_os_kill__proxy: 'sync',+ godot_js_os_kill__sig: 'ii',+ godot_js_os_kill: function (p_pid) {+ if (!GodotConfig.on_terminate_pid) {+ return -1;+ }+ GodotConfig.on_terminate_pid(p_pid);+ return 0;},godot_js_os_shell_open__proxy: 'sync',diff --git a/platform/web/os_web.cpp b/platform/web/os_web.cppindex 04f1fd1a485..41533e8d6da 100644--- a/platform/web/os_web.cpp+++ b/platform/web/os_web.cpp@@ -121,17 +121,22 @@ Error OS_Web::create_process(const String &p_path, const List<String> &p_argumenargs.push_back(E);}String json_args = Variant(args).to_json_string();- int failed = godot_js_os_execute(json_args.utf8().get_data());- ERR_FAIL_COND_V_MSG(failed, ERR_UNAVAILABLE, "OS::execute() or create_process() must be implemented in Web via 'engine.setOnExecute' if required.");+ int64_t id = godot_js_os_execute(p_path.utf8().get_data(), json_args.utf8().get_data());+ ERR_FAIL_COND_V_MSG(id < 0, ERR_UNAVAILABLE, "OS::execute() or create_process() must be implemented in JavaScript via 'EngineConfig.onExecute' if required.");+ if (r_child_id) {+ *r_child_id = id;+ }return OK;}Error OS_Web::kill(const ProcessID &p_pid) {- ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "OS::kill() is not available on the Web platform.");+ int ret = godot_js_os_kill(p_pid);+ ERR_FAIL_COND_V_MSG(ret, ERR_UNAVAILABLE, "OS::kill() must be implemented in JavaScript via 'EngineConfig.onTerminatePID' if required.");+ return OK;}int OS_Web::get_process_id() const {- return 0;+ return web_pid;}bool OS_Web::is_process_running(const ProcessID &p_pid) const {@@ -319,6 +324,8 @@ void OS_Web::initialize_joypads() {}OS_Web::OS_Web() {+ web_pid = godot_js_config_pid_get();+char locale_ptr[16];godot_js_config_locale_get(locale_ptr, 16);setenv("LANG", locale_ptr, true);diff --git a/platform/web/os_web.h b/platform/web/os_web.hindex 1c8544d7147..6e8282aabeb 100644--- a/platform/web/os_web.h+++ b/platform/web/os_web.h@@ -45,6 +45,7 @@ class OS_Web : public OS_Unix {MIDIDriverWebMidi midi_driver;+ ProcessID web_pid = 0;bool idb_is_syncing = false;bool idb_available = false;bool idb_needs_sync = false;