From 6cce16de89162363c97dfb36226a13f5fd53ff4a Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 28 Jun 2023 20:46:22 +0200 Subject: [PATCH] Ported from godot: 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. - raulsntos https://github.com/godotengine/godot/commit/06c0a1abc9bb12d13c69d1269eee2b4a79a94878 --- csharp_script.cpp | 12 ++---------- .../GodotTools.Core/StringExtensions.cs | 6 ++++++ godotsharp_dirs.cpp | 9 ++------- mono_gd/gd_mono.cpp | 8 +------- utils/path_utils.cpp | 19 +++++++++++++++++++ utils/path_utils.h | 2 ++ 6 files changed, 32 insertions(+), 24 deletions(-) diff --git a/csharp_script.cpp b/csharp_script.cpp index 7886cf6..85da21d 100644 --- a/csharp_script.cpp +++ b/csharp_script.cpp @@ -60,6 +60,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" @@ -739,16 +740,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/editor/GodotTools/GodotTools.Core/StringExtensions.cs b/editor/GodotTools/GodotTools.Core/StringExtensions.cs index 60a4f29..2c35b18 100644 --- a/editor/GodotTools/GodotTools.Core/StringExtensions.cs +++ b/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/godotsharp_dirs.cpp b/godotsharp_dirs.cpp index 6bf21ce..eca4599 100644 --- a/godotsharp_dirs.cpp +++ b/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/mono_gd/gd_mono.cpp b/mono_gd/gd_mono.cpp index 172015b..0f784a6 100644 --- a/mono_gd/gd_mono.cpp +++ b/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/utils/path_utils.cpp b/utils/path_utils.cpp index 8923fe7..221f9c5 100644 --- a/utils/path_utils.cpp +++ b/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/utils/path_utils.h b/utils/path_utils.h index 5cb0828..fc649dd 100644 --- a/utils/path_utils.h +++ b/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