From 31d09a807efc973222016a47f832f590c51949ef Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Mon, 15 Aug 2022 18:51:50 +0200 Subject: [PATCH] C#: Use custom project setting for C# project files name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The setting is initially assigned the name of the Godot project, but it's kept freezed to prevent issues when renaming the Godot project. The user can always rename the C# project and solution manually and change the setting to the new name. Co-authored-by: Ignacio Roldán Etcheverry --- modules/mono/csharp_script.cpp | 17 +++++++---- .../GodotTools/Export/AotBuilder.cs | 2 +- .../GodotTools/Export/ExportPlugin.cs | 2 +- .../GodotTools/GodotTools/GodotSharpEditor.cs | 19 +++--------- .../GodotTools/Internals/GodotSharpDirs.cs | 4 +++ modules/mono/editor/editor_internal_calls.cpp | 9 ++++++ modules/mono/godotsharp_dirs.cpp | 30 +++++++++++++++++-- modules/mono/godotsharp_dirs.h | 1 + modules/mono/mono_gd/gd_mono.cpp | 12 ++++---- 9 files changed, 65 insertions(+), 31 deletions(-) diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index ee640976233..7cb686ba050 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -722,19 +722,24 @@ bool CSharpLanguage::is_assembly_reloading_needed() { GDMonoAssembly *proj_assembly = gdmono->get_project_assembly(); 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"; + 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; } - appname_safe += ".dll"; + assembly_name += ".dll"; if (proj_assembly) { String proj_asm_path = proj_assembly->get_path(); if (!FileAccess::exists(proj_asm_path)) { // Maybe it wasn't loaded from the default path, so check this as well - proj_asm_path = GodotSharpDirs::get_res_temp_assemblies_dir().plus_file(appname_safe); + proj_asm_path = GodotSharpDirs::get_res_temp_assemblies_dir().plus_file(assembly_name); if (!FileAccess::exists(proj_asm_path)) return false; // No assembly to load } @@ -742,7 +747,7 @@ bool CSharpLanguage::is_assembly_reloading_needed() { if (FileAccess::get_modified_time(proj_asm_path) <= proj_assembly->get_modified_time()) return false; // Already up to date } else { - if (!FileAccess::exists(GodotSharpDirs::get_res_temp_assemblies_dir().plus_file(appname_safe))) + if (!FileAccess::exists(GodotSharpDirs::get_res_temp_assemblies_dir().plus_file(assembly_name))) return false; // No assembly to load } diff --git a/modules/mono/editor/GodotTools/GodotTools/Export/AotBuilder.cs b/modules/mono/editor/GodotTools/GodotTools/Export/AotBuilder.cs index e7af27fa3d4..20927893fc2 100644 --- a/modules/mono/editor/GodotTools/GodotTools/Export/AotBuilder.cs +++ b/modules/mono/editor/GodotTools/GodotTools/Export/AotBuilder.cs @@ -231,7 +231,7 @@ namespace GodotTools.Export RunLipo(new[] {CompileForArch("arm64"), CompileForArch("x86_64")}, libFilePath); } - string projectAssemblyName = GodotSharpEditor.ProjectAssemblyName; + string projectAssemblyName = GodotSharpDirs.ProjectAssemblyName; string libAotName = $"lib-aot-{projectAssemblyName}"; string libAotXcFrameworkPath = Path.Combine(aotTempDir, $"{libAotName}.xcframework"); diff --git a/modules/mono/editor/GodotTools/GodotTools/Export/ExportPlugin.cs b/modules/mono/editor/GodotTools/GodotTools/Export/ExportPlugin.cs index a8ec8a75760..ba56c7312a4 100644 --- a/modules/mono/editor/GodotTools/GodotTools/Export/ExportPlugin.cs +++ b/modules/mono/editor/GodotTools/GodotTools/Export/ExportPlugin.cs @@ -167,7 +167,7 @@ namespace GodotTools.Export var assemblies = new Godot.Collections.Dictionary(); - string projectDllName = GodotSharpEditor.ProjectAssemblyName; + string projectDllName = GodotSharpDirs.ProjectAssemblyName; string projectDllSrcDir = Path.Combine(GodotSharpDirs.ResTempAssembliesBaseDir, buildConfig); string projectDllSrcPath = Path.Combine(projectDllSrcDir, $"{projectDllName}.dll"); diff --git a/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs b/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs index 852855c3bb0..0df2785ce2a 100644 --- a/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs +++ b/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs @@ -39,18 +39,6 @@ namespace GodotTools public bool SkipBuildBeforePlaying { get; set; } = false; - public static string ProjectAssemblyName - { - get - { - string projectAssemblyName = (string)ProjectSettings.GetSetting("application/config/name"); - projectAssemblyName = projectAssemblyName.ToSafeDirName(); - if (string.IsNullOrEmpty(projectAssemblyName)) - projectAssemblyName = "UnnamedProject"; - return projectAssemblyName; - } - } - private bool CreateProjectSolution() { using (var pr = new EditorProgress("create_csharp_solution", "Generating solution...".TTR(), 3)) @@ -60,7 +48,7 @@ namespace GodotTools string resourceDir = ProjectSettings.GlobalizePath("res://"); string path = resourceDir; - string name = ProjectAssemblyName; + string name = GodotSharpDirs.ProjectAssemblyName; string guid = CsProjOperations.GenerateGameProject(path, name); @@ -375,7 +363,8 @@ namespace GodotTools [UsedImplicitly] public bool OverridesExternalEditor() { - return (ExternalEditorId)_editorSettings.GetSetting("mono/editor/external_editor") != ExternalEditorId.None; + return (ExternalEditorId)_editorSettings.GetSetting("mono/editor/external_editor") != + ExternalEditorId.None; } public override bool Build() @@ -396,7 +385,7 @@ namespace GodotTools // NOTE: The order in which changes are made to the project is important // Migrate to MSBuild project Sdks style if using the old style - ProjectUtils.MigrateToProjectSdksStyle(msbuildProject, ProjectAssemblyName); + ProjectUtils.MigrateToProjectSdksStyle(msbuildProject, GodotSharpDirs.ProjectAssemblyName); ProjectUtils.EnsureGodotSdkIsUpToDate(msbuildProject); diff --git a/modules/mono/editor/GodotTools/GodotTools/Internals/GodotSharpDirs.cs b/modules/mono/editor/GodotTools/GodotTools/Internals/GodotSharpDirs.cs index 5e70c399b28..0356bb12a77 100644 --- a/modules/mono/editor/GodotTools/GodotTools/Internals/GodotSharpDirs.cs +++ b/modules/mono/editor/GodotTools/GodotTools/Internals/GodotSharpDirs.cs @@ -20,6 +20,7 @@ namespace GodotTools.Internals public static string MonoSolutionsDir => internal_MonoSolutionsDir(); public static string BuildLogsDirs => internal_BuildLogsDirs(); + public static string ProjectAssemblyName => internal_ProjectAssemblyName(); public static string ProjectSlnPath => internal_ProjectSlnPath(); public static string ProjectCsProjPath => internal_ProjectCsProjPath(); @@ -74,6 +75,9 @@ namespace GodotTools.Internals [MethodImpl(MethodImplOptions.InternalCall)] private static extern string internal_BuildLogsDirs(); + [MethodImpl(MethodImplOptions.InternalCall)] + private static extern string internal_ProjectAssemblyName(); + [MethodImpl(MethodImplOptions.InternalCall)] private static extern string internal_ProjectSlnPath(); diff --git a/modules/mono/editor/editor_internal_calls.cpp b/modules/mono/editor/editor_internal_calls.cpp index fac1a0aa163..d10c6098269 100644 --- a/modules/mono/editor/editor_internal_calls.cpp +++ b/modules/mono/editor/editor_internal_calls.cpp @@ -107,6 +107,14 @@ MonoString *godot_icall_GodotSharpDirs_BuildLogsDirs() { #endif } +MonoString *godot_icall_GodotSharpDirs_ProjectAssemblyName() { +#ifdef TOOLS_ENABLED + return GDMonoMarshal::mono_string_from_godot(GodotSharpDirs::get_project_assembly_name()); +#else + return NULL; +#endif +} + MonoString *godot_icall_GodotSharpDirs_ProjectSlnPath() { #ifdef TOOLS_ENABLED return GDMonoMarshal::mono_string_from_godot(GodotSharpDirs::get_project_sln_path()); @@ -388,6 +396,7 @@ void register_editor_internal_calls() { GDMonoUtils::add_internal_call("GodotTools.Internals.GodotSharpDirs::internal_MonoLogsDir", godot_icall_GodotSharpDirs_MonoLogsDir); GDMonoUtils::add_internal_call("GodotTools.Internals.GodotSharpDirs::internal_MonoSolutionsDir", godot_icall_GodotSharpDirs_MonoSolutionsDir); GDMonoUtils::add_internal_call("GodotTools.Internals.GodotSharpDirs::internal_BuildLogsDirs", godot_icall_GodotSharpDirs_BuildLogsDirs); + GDMonoUtils::add_internal_call("GodotTools.Internals.GodotSharpDirs::internal_ProjectAssemblyName", godot_icall_GodotSharpDirs_ProjectAssemblyName); GDMonoUtils::add_internal_call("GodotTools.Internals.GodotSharpDirs::internal_ProjectSlnPath", godot_icall_GodotSharpDirs_ProjectSlnPath); GDMonoUtils::add_internal_call("GodotTools.Internals.GodotSharpDirs::internal_ProjectCsProjPath", godot_icall_GodotSharpDirs_ProjectCsProjPath); GDMonoUtils::add_internal_call("GodotTools.Internals.GodotSharpDirs::internal_DataEditorToolsDir", godot_icall_GodotSharpDirs_DataEditorToolsDir); diff --git a/modules/mono/godotsharp_dirs.cpp b/modules/mono/godotsharp_dirs.cpp index d8363a3368d..a6edbdc516e 100644 --- a/modules/mono/godotsharp_dirs.cpp +++ b/modules/mono/godotsharp_dirs.cpp @@ -102,6 +102,7 @@ public: String mono_solutions_dir; String build_logs_dir; + String project_assembly_name; String sln_filepath; String csproj_filepath; @@ -144,16 +145,35 @@ private: mono_solutions_dir = mono_user_dir.plus_file("solutions"); build_logs_dir = mono_user_dir.plus_file("build_logs"); + GLOBAL_DEF("mono/project/assembly_name", ""); + GLOBAL_DEF("mono/project/solution_directory", ""); + GLOBAL_DEF("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"; } - String base_path = ProjectSettings::get_singleton()->globalize_path("res://"); + project_assembly_name = ProjectSettings::get_singleton()->get("mono/project/assembly_name"); + if (project_assembly_name.empty()) { + project_assembly_name = appname_safe; + ProjectSettings::get_singleton()->set("mono/project/assembly_name", project_assembly_name); + } - sln_filepath = base_path.plus_file(appname_safe + ".sln"); - csproj_filepath = base_path.plus_file(appname_safe + ".csproj"); + String sln_parent_dir = ProjectSettings::get_singleton()->get("mono/project/solution_directory"); + if (sln_parent_dir.empty()) { + sln_parent_dir = "res://"; + } + + String csproj_parent_dir = ProjectSettings::get_singleton()->get("mono/project/c#_project_directory"); + if (csproj_parent_dir.empty()) { + csproj_parent_dir = "res://"; + } + + sln_filepath = ProjectSettings::get_singleton()->globalize_path(sln_parent_dir).plus_file(project_assembly_name + ".sln"); + + csproj_filepath = ProjectSettings::get_singleton()->globalize_path(csproj_parent_dir).plus_file(project_assembly_name + ".csproj"); #endif String exe_dir = OS::get_singleton()->get_executable_path().get_base_dir(); @@ -288,6 +308,10 @@ String get_build_logs_dir() { return _GodotSharpDirs::get_singleton().build_logs_dir; } +String get_project_assembly_name() { + return _GodotSharpDirs::get_singleton().project_assembly_name; +} + String get_project_sln_path() { return _GodotSharpDirs::get_singleton().sln_filepath; } diff --git a/modules/mono/godotsharp_dirs.h b/modules/mono/godotsharp_dirs.h index b48176cc6d5..6ca0aff5b40 100644 --- a/modules/mono/godotsharp_dirs.h +++ b/modules/mono/godotsharp_dirs.h @@ -51,6 +51,7 @@ String get_mono_logs_dir(); String get_mono_solutions_dir(); String get_build_logs_dir(); +String get_project_assembly_name(); String get_project_sln_path(); String get_project_csproj_path(); diff --git a/modules/mono/mono_gd/gd_mono.cpp b/modules/mono/mono_gd/gd_mono.cpp index 3860c53b249..40051110c43 100644 --- a/modules/mono/mono_gd/gd_mono.cpp +++ b/modules/mono/mono_gd/gd_mono.cpp @@ -987,13 +987,15 @@ bool GDMono::_load_project_assembly() { if (project_assembly) return true; - 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"; + 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; } - bool success = load_assembly(appname_safe, &project_assembly); + bool success = load_assembly(assembly_name, &project_assembly); if (success) { mono_assembly_set_main(project_assembly->get_assembly());