Godot Engine – Multi-platform 2D and 3D game engine
web: Implement Web Editor debugger via MessagePort
Fabio Alessandrellibafac1cafbef
diff --git a/editor/debugger/editor_debugger_node.cpp b/editor/debugger/editor_debugger_node.cppindex 975e9d9d20b..316fcaeb448 100644--- a/editor/debugger/editor_debugger_node.cpp+++ b/editor/debugger/editor_debugger_node.cpp@@ -279,7 +279,11 @@ ScriptEditorDebugger *EditorDebuggerNode::get_default_debugger() const {}String EditorDebuggerNode::get_server_uri() const {+#ifdef WEB_ENABLED+ return server.is_valid() ? server->get_uri() : "messageport://";+#elsereturn server.is_valid() ? server->get_uri() : "";+#endif}void EditorDebuggerNode::set_keep_open(bool p_keep_open) {@@ -313,8 +317,8 @@ Error EditorDebuggerNode::start(const String &p_uri) {stop(true);current_uri = p_uri;- server = Ref<EditorDebuggerServer>(EditorDebuggerServer::create(p_uri.substr(0, p_uri.find("://") + 3)));- RETURN_IF_ERROR(server->start(p_uri));+ server = Ref<EditorDebuggerServer>(EditorDebuggerServer::create(current_uri.substr(0, current_uri.find("://") + 3)));+ RETURN_IF_ERROR(server->start(current_uri));set_process(true);EditorNode::get_log()->add_message("--- Debugging process started ---", EditorLog::MSG_TYPE_EDITOR);return OK;diff --git a/misc/dist/html/editor.html b/misc/dist/html/editor.htmlindex b03b9d12c3f..65a48fa47ea 100644--- a/misc/dist/html/editor.html+++ b/misc/dist/html/editor.html@@ -666,6 +666,7 @@ function startEditor(zip) {'unloadAfterInit': false,'canvas': gameCanvas,'canvasResizePolicy': 1,+ 'debugPort': null,'onExit': function () {gameCanvas = replaceCanvas(gameCanvas);setGameTabEnabled(false);@@ -693,11 +694,14 @@ function startEditor(zip) {return;}setGameTabEnabled(true);+ const channel = new MessageChannel();+ gameConfig['debugPort'] = channel.port1;game = new Engine(gameConfig);showTab('game');game.init().then(function () {requestAnimationFrame(function () {game.start({ 'args': args, 'canvas': gameCanvas }).then(function () {+ editor.addDebuggerSession(channel.port2);gameCanvas.focus();});});diff --git a/platform/web/SCsub b/platform/web/SCsubindex c5c068093ce..82cf164280e 100644--- a/platform/web/SCsub+++ b/platform/web/SCsub@@ -26,6 +26,7 @@ web_files = ["display_server_web.cpp","http_client_web.cpp","javascript_bridge_singleton.cpp",+ "remote_debugger_peer_messageport.cpp","web_main.cpp","os_web.cpp",]@@ -44,6 +45,7 @@ sys_env.AddJSLibraries(["js/libs/library_godot_runtime.js","js/libs/library_godot_input.js","js/libs/library_godot_webgl2.js",+ "js/libs/library_godot_debugger.js",])sys_env.AddJSExterns(["js/libs/library_godot_webgl2.externs.js",@@ -55,6 +57,8 @@ sys_env.AddJSPost([if env["javascript_eval"]:sys_env.AddJSLibraries(["js/libs/library_godot_javascript_singleton.js"])+if env["target"] == "editor":+ sys_env.AddJSLibraries(["js/libs/library_godot_editor_debugger.js"])for lib in sys_env["JS_LIBS"]:sys_env.Append(LINKFLAGS=["--js-library", lib.abspath])diff --git a/platform/web/editor/editor_debugger_server_messageport.cpp b/platform/web/editor/editor_debugger_server_messageport.cppnew file mode 100644index 00000000000..05ce6b22c76--- /dev/null+++ b/platform/web/editor/editor_debugger_server_messageport.cpp@@ -0,0 +1,97 @@+/**************************************************************************/+/* editor_debugger_server_messageport.cpp */+/**************************************************************************/+/* This file is part of: */+/* GODOT ENGINE */+/* https://godotengine.org */+/**************************************************************************/+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */+/* */+/* 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. */+/**************************************************************************/++#include "editor_debugger_server_messageport.h"++#include "editor/editor_log.h"+#include "editor/editor_node.h"++extern "C" {+bool godot_js_editor_debugger_active();+void godot_js_editor_debugger_cb(void (*p_callback)(int p_id));+}++EditorDebuggerServerMessagePort *EditorDebuggerServerMessagePort::singleton = nullptr;++void EditorDebuggerServerMessagePort::_add_session(int p_session) {+ ERR_FAIL_NULL(singleton);+ singleton->pending.push_back(p_session);+}++void EditorDebuggerServerMessagePort::initialize() {+ EditorDebuggerServer::register_protocol_handler("messageport://", EditorDebuggerServerMessagePort::create);+}++void EditorDebuggerServerMessagePort::poll() {+}++String EditorDebuggerServerMessagePort::get_uri() const {+ return "messageport://";+}++Error EditorDebuggerServerMessagePort::start(const String &p_uri) {+ godot_js_editor_debugger_cb(&_add_session);+ return OK;+}++void EditorDebuggerServerMessagePort::stop() {+ godot_js_editor_debugger_cb(nullptr);+ pending.clear();+}++bool EditorDebuggerServerMessagePort::is_active() const {+ return godot_js_editor_debugger_active();+}++bool EditorDebuggerServerMessagePort::is_connection_available() const {+ return pending.size();+}++Ref<RemoteDebuggerPeer> EditorDebuggerServerMessagePort::take_connection() {+ ERR_FAIL_COND_V(!is_connection_available(), Ref<RemoteDebuggerPeer>());+ Ref<RemoteDebuggerPeerMessagePort> peer = memnew(RemoteDebuggerPeerMessagePort(pending.front()->get()));+ pending.pop_front();+ return peer;+}++EditorDebuggerServerMessagePort::EditorDebuggerServerMessagePort() {+ ERR_FAIL_COND(singleton != nullptr);+ singleton = this;+}++EditorDebuggerServerMessagePort::~EditorDebuggerServerMessagePort() {+ stop();+ singleton = nullptr;+}++Ref<EditorDebuggerServer> EditorDebuggerServerMessagePort::create(const String &p_protocol) {+ ERR_FAIL_COND_V(p_protocol != "messageport://", nullptr);+ return memnew(EditorDebuggerServerMessagePort);+}diff --git a/platform/web/editor/editor_debugger_server_messageport.h b/platform/web/editor/editor_debugger_server_messageport.hnew file mode 100644index 00000000000..86e2d7079b9--- /dev/null+++ b/platform/web/editor/editor_debugger_server_messageport.h@@ -0,0 +1,61 @@+/**************************************************************************/+/* editor_debugger_server_messageport.h */+/**************************************************************************/+/* This file is part of: */+/* GODOT ENGINE */+/* https://godotengine.org */+/**************************************************************************/+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */+/* */+/* 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. */+/**************************************************************************/++#pragma once++#include "../remote_debugger_peer_messageport.h"+#include "godot_js.h"++#include "editor/debugger/editor_debugger_server.h"++class EditorDebuggerServerMessagePort : public EditorDebuggerServer {+ GDCLASS(EditorDebuggerServerMessagePort, EditorDebuggerServer);++private:+ List<int> pending;+ static EditorDebuggerServerMessagePort *singleton;++ WASM_EXPORT static void _add_session(int p_session);++public:+ static void initialize();+ static Ref<EditorDebuggerServer> create(const String &p_protocol);++ virtual void poll() override;+ virtual String get_uri() const override;+ virtual Error start(const String &p_uri = "") override;+ virtual void stop() override;+ virtual bool is_active() const override;+ virtual bool is_connection_available() const override;+ virtual Ref<RemoteDebuggerPeer> take_connection() override;++ EditorDebuggerServerMessagePort();+ virtual ~EditorDebuggerServerMessagePort();+};diff --git a/platform/web/export/export_plugin.cpp b/platform/web/export/export_plugin.cppindex 4e9857fd90f..9432721ff15 100644--- a/platform/web/export/export_plugin.cpp+++ b/platform/web/export/export_plugin.cpp@@ -907,6 +907,14 @@ Ref<Texture2D> EditorExportPlatformWeb::get_run_icon() const {return run_icon;}+String EditorExportPlatformWeb::get_debug_protocol() const {+#ifdef WEB_ENABLED+ return "messageport://";+#else+ return "ws://";+#endif+}+void EditorExportPlatformWeb::initialize() {if (EditorNode::get_singleton()) {server.instantiate();diff --git a/platform/web/export/export_plugin.h b/platform/web/export/export_plugin.hindex b5a1640c8c6..047bd1815c2 100644--- a/platform/web/export/export_plugin.h+++ b/platform/web/export/export_plugin.h@@ -143,7 +143,7 @@ public:virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, HashSet<String> &p_features) override {}- String get_debug_protocol() const override { return "ws://"; }+ virtual String get_debug_protocol() const override;virtual void initialize() override;~EditorExportPlatformWeb();diff --git a/platform/web/js/engine/config.js b/platform/web/js/engine/config.jsindex d867376e415..14b81ed58cf 100644--- a/platform/web/js/engine/config.js+++ b/platform/web/js/engine/config.js@@ -143,6 +143,13 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-* @type {number}*/godotPoolSize: 4,+ /**+ * A MessagePort used to send debug messages when using protocol messageport://+ * @memberof EngineConfig+ * @default+ * @type {?MessagePort}+ */+ debugPort: null,/*** A callback function for handling Godot's ``OS.execute`` calls.*@@ -272,6 +279,7 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-this.emscriptenPoolSize = parse('emscriptenPoolSize', this.emscriptenPoolSize);this.godotPoolSize = parse('godotPoolSize', this.godotPoolSize);this.args = parse('args', this.args);+ this.debugPort = parse('debugPort', this.debugPort);this.onExecute = parse('onExecute', this.onExecute);this.onExit = parse('onExit', this.onExit);};@@ -365,6 +373,7 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-'virtualKeyboard': this.experimentalVK,'godotPoolSize': this.godotPoolSize,'focusCanvas': this.focusCanvas,+ 'debugPort': this.debugPort,'onExecute': this.onExecute,'onExit': function (p_code) {cleanup(); // We always need to call the cleanup callback to free memory.diff --git a/platform/web/js/engine/engine.js b/platform/web/js/engine/engine.jsindex 1aeeb62f18d..0261ca55b7a 100644--- a/platform/web/js/engine/engine.js+++ b/platform/web/js/engine/engine.js@@ -235,6 +235,19 @@ const Engine = (function () {}},+ /**+ * Editor-Only.+ *+ * Adds a new EditorDebuggerSession connected via the given port.+ *+ * @param {MessagePort} port The port used for the debugger session.+ */+ addDebuggerSession: function (port) {+ if (this.rtenv) {+ this.rtenv['add_debugger_session'](port);+ }+ },+/*** Install the progressive-web app service worker.* @returns {Promise} The service worker registration promise.@@ -259,6 +272,7 @@ const Engine = (function () {Engine.prototype['startGame'] = Engine.prototype.startGame;Engine.prototype['copyToFS'] = Engine.prototype.copyToFS;Engine.prototype['requestQuit'] = Engine.prototype.requestQuit;+ Engine.prototype['addDebuggerSession'] = Engine.prototype.addDebuggerSession;Engine.prototype['installServiceWorker'] = Engine.prototype.installServiceWorker;// Also expose static methods as instance methodsEngine.prototype['load'] = Engine.load;diff --git a/platform/web/js/libs/library_godot_debugger.js b/platform/web/js/libs/library_godot_debugger.jsnew file mode 100644index 00000000000..9237eb7b611--- /dev/null+++ b/platform/web/js/libs/library_godot_debugger.js@@ -0,0 +1,139 @@+/**************************************************************************/+/* library_godot_debugger.js */+/**************************************************************************/+/* This file is part of: */+/* GODOT ENGINE */+/* https://godotengine.org */+/**************************************************************************/+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */+/* */+/* 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 GodotDebugger = {+ $GodotDebugger__deps: ['$GodotRuntime', '$IDHandler', '$GodotConfig'],+ $GodotDebugger: {+ debug_id: -1,+ bind: function (id, ref, cb) {+ const o = IDHandler.get(id);+ if (!o) {+ return -1;+ }+ o.port.onmessage = function (event) {+ const obj = IDHandler.get(id);+ if (!obj) {+ return;+ }+ if (typeof (event.data) === 'string') {+ if (event.data === 'stop') {+ obj.active = false;+ } else {+ GodotRuntime.error('Invalid message', event.data);+ }+ return;+ } else if (!(event.data instanceof Uint8Array)) {+ GodotRuntime.error('Invalid message', event.data);+ return;+ }+ const buffer = event.data;+ if (buffer.length == 0) {+ return;+ }+ const len = buffer.length * buffer.BYTES_PER_ELEMENT;+ const out = GodotRuntime.malloc(len);+ HEAPU8.set(buffer, out);+ cb(ref, out, len);+ GodotRuntime.free(out);+ };+ return id;+ },+ post: function (id, msg) {+ const obj = IDHandler.get(id);+ if (!obj) {+ return;+ }+ obj.port.postMessage(msg);+ },+ unbind: function (id) {+ const obj = IDHandler.get(id);+ if (!obj) {+ return;+ }+ obj.port.postMessage('stop');+ obj.port.close();+ obj.port.onmessage = null;+ },+ },++ godot_js_debugger_post__proxy: 'sync',+ godot_js_debugger_post__sig: 'vipi',+ godot_js_debugger_post: function (p_id, p_ptr, p_size) {+ if (!p_size) {+ return;+ }+ const msg = GodotRuntime.heapSlice(HEAPU8, p_ptr, p_size);+ GodotDebugger.post(p_id, msg);+ },++ godot_js_debugger_bind__proxy: 'sync',+ godot_js_debugger_bind__sig: 'iipp',+ godot_js_debugger_bind: function (p_id, p_cb, p_ref) {+ let id = p_id;+ if (id == -1) {+ if (!GodotConfig.debug_port) {+ return -1;+ }+ if (GodotDebugger.debug_id !== -1) {+ GodotRuntime.error('Already bound');+ return -1;+ }+ id = IDHandler.add({+ port: GodotConfig.debug_port,+ active: true,+ });+ GodotDebugger.debug_id = id;+ }+ const cb = GodotRuntime.get_func(p_cb);+ return GodotDebugger.bind(id, p_ref, cb);+ },++ godot_js_debugger_unbind__proxy: 'sync',+ godot_js_debugger_unbind__sig: 'vi',+ godot_js_debugger_unbind: function (p_id) {+ GodotDebugger.unbind(p_id);+ IDHandler.remove(p_id);+ GodotDebugger.debug_id = -1;+ },++ godot_js_debugger_active__proxy: 'sync',+ godot_js_debugger_active__sig: 'ii',+ godot_js_debugger_active: function (p_id) {+ const obj = IDHandler.get(p_id);+ if (!obj) {+ return 0;+ }+ return obj.active ? 1 : 0;+ },++};++autoAddDeps(GodotDebugger, '$GodotDebugger');+mergeInto(LibraryManager.library, GodotDebugger);diff --git a/platform/web/js/libs/library_godot_editor_debugger.js b/platform/web/js/libs/library_godot_editor_debugger.jsnew file mode 100644index 00000000000..b3c4cfc3dcb--- /dev/null+++ b/platform/web/js/libs/library_godot_editor_debugger.js@@ -0,0 +1,65 @@+/**************************************************************************/+/* library_godot_editor_debugger.js */+/**************************************************************************/+/* This file is part of: */+/* GODOT ENGINE */+/* https://godotengine.org */+/**************************************************************************/+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */+/* */+/* 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 GodotEditorDebugger = {+ $GodotEditorDebugger__postset: [+ 'Module["add_debugger_session"] = GodotEditorDebugger.addSession;',+ ].join(''),+ $GodotEditorDebugger__deps: ['$GodotDebugger', '$GodotRuntime', '$IDHandler'],+ $GodotEditorDebugger: {+ session_callback: null,+ addSession: function (port) {+ if (!GodotEditorDebugger.session_callback) {+ GodotRuntime.error('Editor debugger not bound.');+ return;+ }+ const id = IDHandler.add({+ port: port,+ active: true,+ });+ GodotEditorDebugger.session_callback(id);+ },+ },++ godot_js_editor_debugger_cb__proxy: 'sync',+ godot_js_editor_debugger_cb__sig: 'vp',+ godot_js_editor_debugger_cb: function (p_cb) {+ GodotEditorDebugger.session_callback = p_cb ? GodotRuntime.get_func(p_cb) : null;+ },++ godot_js_editor_debugger_active__proxy: 'sync',+ godot_js_editor_debugger_active__sig: 'i',+ godot_js_editor_debugger_active: function () {+ return GodotEditorDebugger.session_callback ? 1 : 0;+ },+};++autoAddDeps(GodotEditorDebugger, '$GodotEditorDebugger');+mergeInto(LibraryManager.library, GodotEditorDebugger);diff --git a/platform/web/js/libs/library_godot_os.js b/platform/web/js/libs/library_godot_os.jsindex 0e931b794e9..a147554cb1a 100644--- a/platform/web/js/libs/library_godot_os.js+++ b/platform/web/js/libs/library_godot_os.js@@ -62,6 +62,7 @@ const GodotConfig = {virtual_keyboard: false,persistent_drops: false,godot_pool_size: 4,+ debug_port: null,on_execute: null,on_exit: null,@@ -72,6 +73,7 @@ const GodotConfig = {GodotConfig.virtual_keyboard = p_opts['virtualKeyboard'];GodotConfig.persistent_drops = !!p_opts['persistentDrops'];GodotConfig.godot_pool_size = p_opts['godotPoolSize'];+ GodotConfig.debug_port = p_opts['debugPort'];GodotConfig.on_execute = p_opts['onExecute'];GodotConfig.on_exit = p_opts['onExit'];if (p_opts['focusCanvas']) {@@ -82,12 +84,14 @@ const GodotConfig = {locate_file: function (file) {return Module['locateFile'](file);},+clear: function () {GodotConfig.canvas = null;GodotConfig.locale = 'en';GodotConfig.canvas_resize_policy = 2;GodotConfig.virtual_keyboard = false;GodotConfig.persistent_drops = false;+ GodotConfig.debug_port = null;GodotConfig.on_execute = null;GodotConfig.on_exit = null;},diff --git a/platform/web/os_web.cpp b/platform/web/os_web.cppindex 0adf0199671..0d0af3b1ab5 100644--- a/platform/web/os_web.cpp+++ b/platform/web/os_web.cpp@@ -35,7 +35,9 @@#include "godot_js.h"#include "ip_web.h"#include "net_socket_web.h"+#include "remote_debugger_peer_messageport.h"+#include "core/debugger/engine_debugger.h"#include "core/io/file_access.h"#include "core/os/main_loop.h"#include "core/os/os.h"@@ -57,6 +59,7 @@ void OS_Web::initialize() {IPWeb::make_default();NetSocketWeb::make_default();DisplayServerWeb::register_web_driver();+ EngineDebugger::register_uri_handler("messageport://", RemoteDebuggerPeerMessagePort::create);}void OS_Web::resume_audio() {diff --git a/platform/web/remote_debugger_peer_messageport.cpp b/platform/web/remote_debugger_peer_messageport.cppnew file mode 100644index 00000000000..29f9b0c21eb--- /dev/null+++ b/platform/web/remote_debugger_peer_messageport.cpp@@ -0,0 +1,130 @@+/**************************************************************************/+/* remote_debugger_peer_messageport.cpp */+/**************************************************************************/+/* This file is part of: */+/* GODOT ENGINE */+/* https://godotengine.org */+/**************************************************************************/+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */+/* */+/* 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. */+/**************************************************************************/++#include "remote_debugger_peer_messageport.h"++#include "core/config/project_settings.h"+#include "core/io/marshalls.h"++extern "C" {+int godot_js_debugger_bind(int p_id, void (*p_callback)(void *p_ref, const uint8_t *p_buffer, size_t p_len), void *p_ref);+void godot_js_debugger_post(int p_id, const uint8_t *p_buffer, size_t p_len);+int godot_js_debugger_active(int p_id);+void godot_js_debugger_unbind(int p_id);+}++void RemoteDebuggerPeerMessagePort::on_debug_message(void *p_ref, const uint8_t *p_buffer, size_t p_len) {+ RemoteDebuggerPeerMessagePort *peer = static_cast<RemoteDebuggerPeerMessagePort *>(p_ref);+ ERR_FAIL_COND(peer->in_queue.size() >= peer->max_queued_messages);+ Variant var;+ Error err = decode_variant(var, p_buffer, p_len);+ ERR_FAIL_COND(err != OK);+ ERR_FAIL_COND(var.get_type() != Variant::ARRAY);+ peer->in_queue.push_back(var);+}++Error RemoteDebuggerPeerMessagePort::connect_to_host(const String &p_uri) {+ _js_id = godot_js_debugger_bind(-1, &on_debug_message, this);+ return _js_id != -1 ? OK : FAILED;+}++bool RemoteDebuggerPeerMessagePort::is_peer_connected() {+ if (_js_id == -1) {+ return false;+ }+ return godot_js_debugger_active(_js_id);+}++void RemoteDebuggerPeerMessagePort::poll() {+}++int RemoteDebuggerPeerMessagePort::get_max_message_size() const {+ return out_buffer.size();+}++bool RemoteDebuggerPeerMessagePort::has_message() {+ return in_queue.size() > 0;+}++Array RemoteDebuggerPeerMessagePort::get_message() {+ ERR_FAIL_COND_V(in_queue.is_empty(), Array());+ const Array msg = in_queue.front()->get();+ in_queue.pop_front();+ return msg;+}++Error RemoteDebuggerPeerMessagePort::put_message(const Array &p_arr) {+ if (_js_id == -1) {+ return ERR_UNCONFIGURED;+ }+ Variant v = p_arr;+ int size = 0;+ Error err = encode_variant(v, nullptr, size);+ ERR_FAIL_COND_V(err != OK || size < 1 || size > out_buffer.size(), FAILED);+ encode_variant(v, out_buffer.ptrw(), size);+ godot_js_debugger_post(_js_id, out_buffer.ptr(), size);+ return OK;+}++void RemoteDebuggerPeerMessagePort::close() {+ if (_js_id == -1) {+ return;+ }+ godot_js_debugger_unbind(_js_id);+ _js_id = -1;+}++bool RemoteDebuggerPeerMessagePort::can_block() const {+ return false;+}++RemoteDebuggerPeerMessagePort::RemoteDebuggerPeerMessagePort(int p_id) {+ max_queued_messages = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");+ out_buffer.resize(1 << 18);+ if (p_id != -1) {+ _js_id = godot_js_debugger_bind(p_id, &on_debug_message, this);+ ERR_FAIL_COND(_js_id == -1);+ }+}++RemoteDebuggerPeerMessagePort::~RemoteDebuggerPeerMessagePort() {+ close();+}++Ref<RemoteDebuggerPeer> RemoteDebuggerPeerMessagePort::create(const String &p_uri) {+ ERR_FAIL_COND_V(!p_uri.begins_with("messageport://"), nullptr);+ Ref<RemoteDebuggerPeerMessagePort> peer;+ peer.instantiate();+ Error err = peer->connect_to_host(p_uri);+ if (err != OK) {+ return nullptr;+ }+ return peer;+}diff --git a/platform/web/remote_debugger_peer_messageport.h b/platform/web/remote_debugger_peer_messageport.hnew file mode 100644index 00000000000..81078b2895f--- /dev/null+++ b/platform/web/remote_debugger_peer_messageport.h@@ -0,0 +1,65 @@+/**************************************************************************/+/* remote_debugger_peer_messageport.h */+/**************************************************************************/+/* This file is part of: */+/* GODOT ENGINE */+/* https://godotengine.org */+/**************************************************************************/+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */+/* */+/* 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. */+/**************************************************************************/++#pragma once++#include "godot_js.h"++#include "core/debugger/remote_debugger_peer.h"++class RemoteDebuggerPeerMessagePort : public RemoteDebuggerPeer {+ GDSOFTCLASS(RemoteDebuggerPeerMessagePort, RemoteDebuggerPeer);++ List<Array> in_queue;+ Vector<uint8_t> out_buffer;++ int max_queued_messages;++ int _js_id = -1;++ WASM_EXPORT static void on_debug_message(void *p_ref, const uint8_t *p_buffer, size_t p_len);++public:+ static Ref<RemoteDebuggerPeer> create(const String &p_uri);++ Error connect_to_host(const String &p_uri);++ virtual bool is_peer_connected() override;+ virtual int get_max_message_size() const override;+ virtual bool has_message() override;+ virtual Error put_message(const Array &p_arr) override;+ virtual Array get_message() override;+ virtual void close() override;+ virtual void poll() override;+ virtual bool can_block() const override;++ RemoteDebuggerPeerMessagePort(int p_id = -1);+ virtual ~RemoteDebuggerPeerMessagePort();+};diff --git a/platform/web/web_main.cpp b/platform/web/web_main.cppindex 6b57b0712e2..7f8ba32f24b 100644--- a/platform/web/web_main.cpp+++ b/platform/web/web_main.cpp@@ -41,6 +41,8 @@#ifdef TOOLS_ENABLED#include "core/io/file_access.h"+#include "editor/editor_debugger_server_messageport.h"+#include "editor/editor_node.h"#include "editor/web_tools_editor_plugin.h"#include "scene/main/scene_tree.h"#include "scene/main/window.h" // SceneTree only forward declares it.@@ -137,6 +139,7 @@ extern EMSCRIPTEN_KEEPALIVE int godot_web_main(int argc, char *argv[]) {#ifdef TOOLS_ENABLEDWebToolsEditorPlugin::initialize();+ EditorNode::add_init_callback(&EditorDebuggerServerMessagePort::initialize);#endif// We must override main when testing is enabled