From aba787393fd25091e7945f1aba27acfd11449b12 Mon Sep 17 00:00:00 2001 From: Paul Joannon Date: Tue, 30 Apr 2024 22:21:21 +0200 Subject: [PATCH] C#: Fix generator crash w/ generic arrays --- .../ScriptPropertiesGeneratorTests.cs | 9 ++++ ...icNode(Of T)_ScriptProperties.generated.cs | 49 +++++++++++++++++++ .../TestData/Sources/AbstractGenericNode.cs | 7 +++ .../ScriptPropertiesGenerator.cs | 5 +- 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/AbstractGenericNode(Of T)_ScriptProperties.generated.cs create mode 100644 modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/AbstractGenericNode.cs diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertiesGeneratorTests.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertiesGeneratorTests.cs index 3cc58410975..724fb164e02 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertiesGeneratorTests.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ScriptPropertiesGeneratorTests.cs @@ -57,4 +57,13 @@ public class ScriptPropertiesGeneratorTests "ScriptBoilerplate_ScriptProperties.generated.cs", "OuterClass.NestedClass_ScriptProperties.generated.cs" ); } + + [Fact] + public async void AbstractGenericNode() + { + await CSharpSourceGeneratorVerifier.Verify( + "AbstractGenericNode.cs", + "AbstractGenericNode(Of T)_ScriptProperties.generated.cs" + ); + } } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/AbstractGenericNode(Of T)_ScriptProperties.generated.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/AbstractGenericNode(Of T)_ScriptProperties.generated.cs new file mode 100644 index 00000000000..a561c5fc0db --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/GeneratedSources/AbstractGenericNode(Of T)_ScriptProperties.generated.cs @@ -0,0 +1,49 @@ +using Godot; +using Godot.NativeInterop; + +partial class AbstractGenericNode +{ +#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword + /// + /// Cached StringNames for the properties and fields contained in this class, for fast lookup. + /// + public new class PropertyName : global::Godot.Node.PropertyName { + /// + /// Cached name for the 'MyArray' property. + /// + public new static readonly global::Godot.StringName MyArray = "MyArray"; + } + /// + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + protected override bool SetGodotClassPropertyValue(in godot_string_name name, in godot_variant value) + { + if (name == PropertyName.MyArray) { + this.MyArray = global::Godot.NativeInterop.VariantUtils.ConvertToArray(value); + return true; + } + return base.SetGodotClassPropertyValue(name, value); + } + /// + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + protected override bool GetGodotClassPropertyValue(in godot_string_name name, out godot_variant value) + { + if (name == PropertyName.MyArray) { + value = global::Godot.NativeInterop.VariantUtils.CreateFromArray(this.MyArray); + return true; + } + return base.GetGodotClassPropertyValue(name, out value); + } + /// + /// Get the property information for all the properties declared in this class. + /// This method is used by Godot to register the available properties in the editor. + /// Do not call this method. + /// + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal new static global::System.Collections.Generic.List GetGodotPropertyList() + { + var properties = new global::System.Collections.Generic.List(); + properties.Add(new(type: (global::Godot.Variant.Type)28, name: PropertyName.MyArray, hint: (global::Godot.PropertyHint)0, hintString: "", usage: (global::Godot.PropertyUsageFlags)4102, exported: true)); + return properties; + } +#pragma warning restore CS0109 +} diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/AbstractGenericNode.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/AbstractGenericNode.cs new file mode 100644 index 00000000000..cee4f679214 --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/AbstractGenericNode.cs @@ -0,0 +1,7 @@ +using Godot; + +public abstract partial class AbstractGenericNode<[MustBeVariant] T> : Node +{ + [Export] // This should be included, but without type hints. + public Godot.Collections.Array MyArray { get; set; } = new(); +} diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs index a0e410e31ab..21223654f36 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs @@ -658,7 +658,10 @@ namespace Godot.SourceGenerators var elementType = MarshalUtils.GetArrayElementType(type); if (elementType == null) - return false; // Non-generic Array, so there's no hint to add + return false; // Non-generic Array, so there's no hint to add. + + if (elementType.TypeKind == TypeKind.TypeParameter) + return false; // The generic is not constructed, we can't really hint anything. var elementMarshalType = MarshalUtils.ConvertManagedTypeToMarshalType(elementType, typeCache)!.Value; var elementVariantType = MarshalUtils.ConvertMarshalTypeToVariantType(elementMarshalType)!.Value;