From 06c0a1abc9bb12d13c69d1269eee2b4a79a94878 Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Wed, 14 Jun 2023 20:18:26 +0200 Subject: [PATCH] C#: Avoid GodotSharp as project assembly name The name GodotSharp conflicts with the name of the Godot assembly, this causes the project assembly to be ignored. --- modules/mono/csharp_script.cpp | 12 ++---------- .../GodotTools.Core/StringExtensions.cs | 6 ++++++ modules/mono/godotsharp_dirs.cpp | 9 ++------- modules/mono/mono_gd/gd_mono.cpp | 8 +------- modules/mono/utils/path_utils.cpp | 19 +++++++++++++++++++ modules/mono/utils/path_utils.h | 2 ++ 6 files changed, 32 insertions(+), 24 deletions(-) diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index 9da68c490f4..b400539d9c6 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -59,6 +59,7 @@ #include "mono_gd/gd_mono_utils.h" #include "signal_awaiter_utils.h" #include "utils/macros.h" +#include "utils/path_utils.h" #include "utils/string_utils.h" #include "utils/thread_local.h" @@ -721,16 +722,7 @@ bool CSharpLanguage::is_assembly_reloading_needed() { GDMonoAssembly *proj_assembly = gdmono->get_project_assembly(); - String appname = ProjectSettings::get_singleton()->get("application/config/name"); - String assembly_name = ProjectSettings::get_singleton()->get_setting("mono/project/assembly_name"); - - if (assembly_name.empty()) { - String appname_safe = OS::get_singleton()->get_safe_dir_name(appname); - if (appname_safe.empty()) { - appname_safe = "UnnamedProject"; - } - assembly_name = appname_safe; - } + String assembly_name = path::get_csharp_project_name(); assembly_name += ".dll"; diff --git a/modules/mono/editor/GodotTools/GodotTools.Core/StringExtensions.cs b/modules/mono/editor/GodotTools/GodotTools.Core/StringExtensions.cs index 60a4f297c9c..2c35b184a6e 100644 --- a/modules/mono/editor/GodotTools/GodotTools.Core/StringExtensions.cs +++ b/modules/mono/editor/GodotTools/GodotTools.Core/StringExtensions.cs @@ -77,6 +77,12 @@ namespace GodotTools.Core foreach (string invalidChar in invalidChars) safeDirName = safeDirName.Replace(invalidChar, "-"); + // Avoid reserved names that conflict with Godot assemblies + if (safeDirName == "GodotSharp" || safeDirName == "GodotSharpEditor") + { + safeDirName += "_"; + } + return safeDirName; } } diff --git a/modules/mono/godotsharp_dirs.cpp b/modules/mono/godotsharp_dirs.cpp index 2fa42a1007c..e57a0c72573 100644 --- a/modules/mono/godotsharp_dirs.cpp +++ b/modules/mono/godotsharp_dirs.cpp @@ -44,6 +44,7 @@ #endif #include "mono_gd/gd_mono.h" +#include "utils/path_utils.h" namespace GodotSharpDirs { @@ -149,15 +150,9 @@ private: GLOBAL_DEF_RST("mono/project/solution_directory", ""); GLOBAL_DEF_RST("mono/project/c#_project_directory", ""); - String appname = ProjectSettings::get_singleton()->get("application/config/name"); - String appname_safe = OS::get_singleton()->get_safe_dir_name(appname); - if (appname_safe.empty()) { - appname_safe = "UnnamedProject"; - } - project_assembly_name = ProjectSettings::get_singleton()->get("mono/project/assembly_name"); if (project_assembly_name.empty()) { - project_assembly_name = appname_safe; + project_assembly_name = path::get_csharp_project_name(); ProjectSettings::get_singleton()->set("mono/project/assembly_name", project_assembly_name); } diff --git a/modules/mono/mono_gd/gd_mono.cpp b/modules/mono/mono_gd/gd_mono.cpp index 0b6ace7deff..4f85c3b94d9 100644 --- a/modules/mono/mono_gd/gd_mono.cpp +++ b/modules/mono/mono_gd/gd_mono.cpp @@ -987,13 +987,7 @@ bool GDMono::_load_project_assembly() { if (project_assembly) return true; - String assembly_name = ProjectSettings::get_singleton()->get("mono/project/assembly_name"); - - if (assembly_name.empty()) { - String appname = ProjectSettings::get_singleton()->get("application/config/name"); - String appname_safe = OS::get_singleton()->get_safe_dir_name(appname); - assembly_name = appname_safe; - } + String assembly_name = path::get_csharp_project_name(); bool success = load_assembly(assembly_name, &project_assembly); diff --git a/modules/mono/utils/path_utils.cpp b/modules/mono/utils/path_utils.cpp index 69ace65d925..77820a53f25 100644 --- a/modules/mono/utils/path_utils.cpp +++ b/modules/mono/utils/path_utils.cpp @@ -208,4 +208,23 @@ String relative_to(const String &p_path, const String &p_relative_to) { return relative_to_impl(path_abs_norm, relative_to_abs_norm); } +String get_csharp_project_name() { + String name = GLOBAL_GET("mono/project/assembly_name"); + if (name.empty()) { + name = GLOBAL_GET("application/config/name"); + name = OS::get_singleton()->get_safe_dir_name(name); + } + + if (name.empty()) { + name = "UnnamedProject"; + } + + // Avoid reserved names that conflict with Godot assemblies. + if (name == "GodotSharp" || name == "GodotSharpEditor") { + name += "_"; + } + + return name; +} + } // namespace path diff --git a/modules/mono/utils/path_utils.h b/modules/mono/utils/path_utils.h index e318311cf08..69ddfee88fa 100644 --- a/modules/mono/utils/path_utils.h +++ b/modules/mono/utils/path_utils.h @@ -59,6 +59,8 @@ String realpath(const String &p_path); String relative_to(const String &p_path, const String &p_relative_to); +String get_csharp_project_name(); + } // namespace path #endif // MONO_PATH_UTILS_H