From 3b085e2665ac724f424038455db305617db44d51 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ignacio=20Rold=C3=A1n=20Etcheverry?=
 <ignalfonsore@gmail.com>
Date: Sun, 16 Jan 2022 12:33:50 +0100
Subject: [PATCH] C#: Allow configuring Mono debugger agent with cmdline args

The command line option is the same you would pass to Mono, but it
begins with `--mono-debugger-agent=` instead of `--debugger-agent=`.

This is useful for platforms where it's difficult for the user to
configure it via environment variables, like Android.
---
 modules/mono/mono_gd/gd_mono.cpp | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/modules/mono/mono_gd/gd_mono.cpp b/modules/mono/mono_gd/gd_mono.cpp
index 894e87c8231..f3d446fbdb9 100644
--- a/modules/mono/mono_gd/gd_mono.cpp
+++ b/modules/mono/mono_gd/gd_mono.cpp
@@ -132,14 +132,31 @@ void gd_mono_debug_init() {
 	CharString da_args = OS::get_singleton()->get_environment("GODOT_MONO_DEBUGGER_AGENT").utf8();
 
 	if (da_args.length()) {
+		// Clear to avoid passing it to child processes
 		OS::get_singleton()->set_environment("GODOT_MONO_DEBUGGER_AGENT", String());
+	} else {
+		// Try with command line arguments. This is useful on platforms where it's difficult to pass
+		// environment variables. The command line arguments can be specified in the export options.
+
+		String da_cmdline_arg;
+		List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
+
+		for (List<String>::Element *E = cmdline_args.front(); E; E = E->next()) {
+			const String &arg = E->get();
+
+			if (arg.begins_with("--mono-debugger-agent=")) {
+				da_cmdline_arg = arg;
+				break;
+			}
+		}
+
+		if (da_cmdline_arg.length()) {
+			da_cmdline_arg.replace_first("--mono-debugger-agent=", "--debugger-agent=");
+			da_args = da_cmdline_arg.utf8();
+		}
 	}
 
 #ifdef TOOLS_ENABLED
-	int da_port = GLOBAL_DEF("mono/debugger_agent/port", 23685);
-	bool da_suspend = GLOBAL_DEF("mono/debugger_agent/wait_for_debugger", false);
-	int da_timeout = GLOBAL_DEF("mono/debugger_agent/wait_timeout", 3000);
-
 	if (Engine::get_singleton()->is_editor_hint() ||
 			ProjectSettings::get_singleton()->get_resource_path().empty() ||
 			Main::is_project_manager()) {
@@ -148,6 +165,12 @@ void gd_mono_debug_init() {
 	}
 
 	if (da_args.length() == 0) {
+		// Use project settings defaults for the editor player
+
+		int da_port = GLOBAL_DEF("mono/debugger_agent/port", 23685);
+		bool da_suspend = GLOBAL_DEF("mono/debugger_agent/wait_for_debugger", false);
+		int da_timeout = GLOBAL_DEF("mono/debugger_agent/wait_timeout", 3000);
+
 		da_args = String("--debugger-agent=transport=dt_socket,address=127.0.0.1:" + itos(da_port) +
 				",embedding=1,server=y,suspend=" + (da_suspend ? "y,timeout=" + itos(da_timeout) : "n"))
 						  .utf8();