e2afe700f6
This source generator adds a newly introduced attribute, `ScriptPath` to all classes that: - Are top-level classes (not inner/nested). - Have the `partial` modifier. - Inherit `Godot.Object`. - The class name matches the file name. A build error is thrown if the generator finds a class that meets these conditions but is not declared `partial`, unless the class is annotated with the `DisableGodotGenerators` attribute. We also generate an `AssemblyHasScripts` assembly attribute which Godot uses to get all the script classes in the assembly, eliminating the need for Godot to search them. We can also avoid searching in assemblies that don't have this attribute. This will be good for performance in the future once we support multiple assemblies with Godot script classes. This is an example of what the generated code looks like: ``` using Godot; namespace Foo { [ScriptPathAttribute("res://Player.cs")] // Multiple partial declarations are allowed [ScriptPathAttribute("res://Foo/Player.cs")] partial class Player {} } [assembly:AssemblyHasScripts(new System.Type[] { typeof(Foo.Player) })] ``` The new attributes replace script metadata which we were generating by determining the namespace of script classes with a very simple parser. This fixes several issues with the old approach related to parser errors and conditional compilation. It also makes the task part of the MSBuild project build, rather than a separate step executed by the Godot editor.
53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using System;
|
|
using Microsoft.Build.Construction;
|
|
|
|
namespace GodotTools.ProjectEditor
|
|
{
|
|
public sealed class MSBuildProject
|
|
{
|
|
internal ProjectRootElement Root { get; set; }
|
|
|
|
public bool HasUnsavedChanges { get; set; }
|
|
|
|
public void Save() => Root.Save();
|
|
|
|
public MSBuildProject(ProjectRootElement root)
|
|
{
|
|
Root = root;
|
|
}
|
|
}
|
|
|
|
public static class ProjectUtils
|
|
{
|
|
public static MSBuildProject Open(string path)
|
|
{
|
|
var root = ProjectRootElement.Open(path);
|
|
return root != null ? new MSBuildProject(root) : null;
|
|
}
|
|
|
|
public static void MigrateToProjectSdksStyle(MSBuildProject project, string projectName)
|
|
{
|
|
var origRoot = project.Root;
|
|
|
|
if (!string.IsNullOrEmpty(origRoot.Sdk))
|
|
return;
|
|
|
|
project.Root = ProjectGenerator.GenGameProject(projectName);
|
|
project.Root.FullPath = origRoot.FullPath;
|
|
project.HasUnsavedChanges = true;
|
|
}
|
|
|
|
public static void EnsureGodotSdkIsUpToDate(MSBuildProject project)
|
|
{
|
|
var root = project.Root;
|
|
string godotSdkAttrValue = ProjectGenerator.GodotSdkAttrValue;
|
|
|
|
if (!string.IsNullOrEmpty(root.Sdk) && root.Sdk.Trim().Equals(godotSdkAttrValue, StringComparison.OrdinalIgnoreCase))
|
|
return;
|
|
|
|
root.Sdk = godotSdkAttrValue;
|
|
project.HasUnsavedChanges = true;
|
|
}
|
|
}
|
|
}
|