C#: Add source generator for properties and exports default values
The editor no longer needs to create temporary instances to get the default values. The initializer values of the exported properties are still evaluated at runtime. For example, in the following example, `GetInitialValue()` will be called when first looks for default values: ``` [Export] int MyValue = GetInitialValue(); ``` Exporting fields with a non-supported type now results in a compiler error rather than a runtime error when the script is used.
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
#pragma warning disable CS0169
|
||||
#pragma warning disable CS0414
|
||||
|
||||
namespace Godot.SourceGenerators.Sample
|
||||
{
|
||||
[SuppressMessage("ReSharper", "BuiltInTypeReferenceStyle")]
|
||||
[SuppressMessage("ReSharper", "RedundantNameQualifier")]
|
||||
[SuppressMessage("ReSharper", "ArrangeObjectCreationWhenTypeEvident")]
|
||||
[SuppressMessage("ReSharper", "InconsistentNaming")]
|
||||
public partial class ExportedFields : Godot.Object
|
||||
{
|
||||
[Export] private Boolean field_Boolean = true;
|
||||
[Export] private Char field_Char = 'f';
|
||||
[Export] private SByte field_SByte = 10;
|
||||
[Export] private Int16 field_Int16 = 10;
|
||||
[Export] private Int32 field_Int32 = 10;
|
||||
[Export] private Int64 field_Int64 = 10;
|
||||
[Export] private Byte field_Byte = 10;
|
||||
[Export] private UInt16 field_UInt16 = 10;
|
||||
[Export] private UInt32 field_UInt32 = 10;
|
||||
[Export] private UInt64 field_UInt64 = 10;
|
||||
[Export] private Single field_Single = 10;
|
||||
[Export] private Double field_Double = 10;
|
||||
[Export] private String field_String = "foo";
|
||||
|
||||
// Godot structs
|
||||
[Export] private Vector2 field_Vector2 = new(10f, 10f);
|
||||
[Export] private Vector2i field_Vector2i = Vector2i.Up;
|
||||
[Export] private Rect2 field_Rect2 = new(new Vector2(10f, 10f), new Vector2(10f, 10f));
|
||||
[Export] private Rect2i field_Rect2i = new(new Vector2i(10, 10), new Vector2i(10, 10));
|
||||
[Export] private Transform2D field_Transform2D = Transform2D.Identity;
|
||||
[Export] private Vector3 field_Vector3 = new(10f, 10f, 10f);
|
||||
[Export] private Vector3i field_Vector3i = Vector3i.Back;
|
||||
[Export] private Basis field_Basis = new Basis(Quaternion.Identity);
|
||||
[Export] private Quaternion field_Quaternion = new Quaternion(Basis.Identity);
|
||||
[Export] private Transform3D field_Transform3D = Transform3D.Identity;
|
||||
[Export] private Vector4 field_Vector4 = new(10f, 10f, 10f, 10f);
|
||||
[Export] private Vector4i field_Vector4i = Vector4i.One;
|
||||
[Export] private Projection field_Projection = Projection.Identity;
|
||||
[Export] private AABB field_AABB = new AABB(10f, 10f, 10f, new Vector3(1f, 1f, 1f));
|
||||
[Export] private Color field_Color = Colors.Aquamarine;
|
||||
[Export] private Plane field_Plane = Plane.PlaneXZ;
|
||||
[Export] private Callable field_Callable = new Callable(Engine.GetMainLoop(), "_process");
|
||||
[Export] private SignalInfo field_SignalInfo = new SignalInfo(Engine.GetMainLoop(), "property_list_changed");
|
||||
|
||||
// Enums
|
||||
[SuppressMessage("ReSharper", "UnusedMember.Local")]
|
||||
enum MyEnum
|
||||
{
|
||||
A,
|
||||
B,
|
||||
C
|
||||
}
|
||||
|
||||
[Export] private MyEnum field_Enum = MyEnum.C;
|
||||
|
||||
[Flags]
|
||||
[SuppressMessage("ReSharper", "UnusedMember.Local")]
|
||||
enum MyFlagsEnum
|
||||
{
|
||||
A,
|
||||
B,
|
||||
C
|
||||
}
|
||||
|
||||
[Export] private MyFlagsEnum field_FlagsEnum = MyFlagsEnum.C;
|
||||
|
||||
// Arrays
|
||||
[Export] private Byte[] field_ByteArray = { 0, 1, 2, 3, 4, 5, 6 };
|
||||
[Export] private Int32[] field_Int32Array = { 0, 1, 2, 3, 4, 5, 6 };
|
||||
[Export] private Int64[] field_Int64Array = { 0, 1, 2, 3, 4, 5, 6 };
|
||||
[Export] private Single[] field_SingleArray = { 0f, 1f, 2f, 3f, 4f, 5f, 6f };
|
||||
[Export] private Double[] field_DoubleArray = { 0d, 1d, 2d, 3d, 4d, 5d, 6d };
|
||||
[Export] private String[] field_StringArray = { "foo", "bar" };
|
||||
[Export(PropertyHint.Enum, "A,B,C")] private String[] field_StringArrayEnum = { "foo", "bar" };
|
||||
[Export] private Vector2[] field_Vector2Array = { Vector2.Up, Vector2.Down, Vector2.Left, Vector2.Right };
|
||||
[Export] private Vector3[] field_Vector3Array = { Vector3.Up, Vector3.Down, Vector3.Left, Vector3.Right };
|
||||
[Export] private Color[] field_ColorArray = { Colors.Aqua, Colors.Aquamarine, Colors.Azure, Colors.Beige };
|
||||
[Export] private Godot.Object[] field_GodotObjectOrDerivedArray = { null };
|
||||
[Export] private object[] field_SystemObjectArray = { 0, 1f, 2d, "foo", Vector3i.Up };
|
||||
|
||||
// Generics
|
||||
[Export] private Godot.Collections.Dictionary<string, string> field_GodotGenericDictionary =
|
||||
new Godot.Collections.Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } };
|
||||
|
||||
[Export] private Godot.Collections.Array<string> field_GodotGenericArray =
|
||||
new Godot.Collections.Array<string> { "elem1", "elem2", "elem3" };
|
||||
|
||||
[Export] private System.Collections.Generic.Dictionary<string, string> field_SystemGenericDictionary =
|
||||
new System.Collections.Generic.Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } };
|
||||
|
||||
[Export] private System.Collections.Generic.List<string> field_SystemGenericList =
|
||||
new System.Collections.Generic.List<string> { "elem1", "elem2", "elem3" };
|
||||
|
||||
[Export] private System.Collections.Generic.IDictionary<string, string> field_GenericIDictionary =
|
||||
new System.Collections.Generic.Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } };
|
||||
|
||||
[Export] private System.Collections.Generic.ICollection<string> field_GenericICollection =
|
||||
new System.Collections.Generic.List<string> { "elem1", "elem2", "elem3" };
|
||||
|
||||
[Export] private System.Collections.Generic.IEnumerable<string> field_GenericIEnumerable =
|
||||
new System.Collections.Generic.List<string> { "elem1", "elem2", "elem3" };
|
||||
|
||||
// Variant
|
||||
[Export] private object field_SystemObject = "foo";
|
||||
|
||||
// Classes
|
||||
[Export] private Godot.Object field_GodotObjectOrDerived;
|
||||
[Export] private Godot.Texture field_GodotResourceTexture;
|
||||
[Export] private StringName field_StringName = new StringName("foo");
|
||||
[Export] private NodePath field_NodePath = new NodePath("foo");
|
||||
[Export] private RID field_RID;
|
||||
|
||||
[Export] private Godot.Collections.Dictionary field_GodotDictionary =
|
||||
new() { { "foo", 10 }, { Vector2.Up, Colors.Chocolate } };
|
||||
|
||||
[Export] private Godot.Collections.Array field_GodotArray =
|
||||
new() { "foo", 10, Vector2.Up, Colors.Chocolate };
|
||||
|
||||
[Export] private System.Collections.IDictionary field_IDictionary =
|
||||
new System.Collections.Generic.Dictionary<object, object>
|
||||
{ { "foo", 10 }, { Vector2.Up, Colors.Chocolate } };
|
||||
|
||||
[Export] private System.Collections.ICollection field_ICollection =
|
||||
new System.Collections.Generic.List<object> { "foo", 10, Vector2.Up, Colors.Chocolate };
|
||||
|
||||
[Export] private System.Collections.IEnumerable field_IEnumerable =
|
||||
new System.Collections.Generic.List<object> { "foo", 10, Vector2.Up, Colors.Chocolate };
|
||||
}
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
#pragma warning disable CS0169
|
||||
#pragma warning disable CS0414
|
||||
|
||||
namespace Godot.SourceGenerators.Sample
|
||||
{
|
||||
[SuppressMessage("ReSharper", "BuiltInTypeReferenceStyle")]
|
||||
[SuppressMessage("ReSharper", "RedundantNameQualifier")]
|
||||
[SuppressMessage("ReSharper", "ArrangeObjectCreationWhenTypeEvident")]
|
||||
[SuppressMessage("ReSharper", "InconsistentNaming")]
|
||||
public partial class ExportedProperties : Godot.Object
|
||||
{
|
||||
[Export] private Boolean property_Boolean { get; set; } = true;
|
||||
[Export] private Char property_Char { get; set; } = 'f';
|
||||
[Export] private SByte property_SByte { get; set; } = 10;
|
||||
[Export] private Int16 property_Int16 { get; set; } = 10;
|
||||
[Export] private Int32 property_Int32 { get; set; } = 10;
|
||||
[Export] private Int64 property_Int64 { get; set; } = 10;
|
||||
[Export] private Byte property_Byte { get; set; } = 10;
|
||||
[Export] private UInt16 property_UInt16 { get; set; } = 10;
|
||||
[Export] private UInt32 property_UInt32 { get; set; } = 10;
|
||||
[Export] private UInt64 property_UInt64 { get; set; } = 10;
|
||||
[Export] private Single property_Single { get; set; } = 10;
|
||||
[Export] private Double property_Double { get; set; } = 10;
|
||||
[Export] private String property_String { get; set; } = "foo";
|
||||
|
||||
// Godot structs
|
||||
[Export] private Vector2 property_Vector2 { get; set; } = new(10f, 10f);
|
||||
[Export] private Vector2i property_Vector2i { get; set; } = Vector2i.Up;
|
||||
[Export] private Rect2 property_Rect2 { get; set; } = new(new Vector2(10f, 10f), new Vector2(10f, 10f));
|
||||
[Export] private Rect2i property_Rect2i { get; set; } = new(new Vector2i(10, 10), new Vector2i(10, 10));
|
||||
[Export] private Transform2D property_Transform2D { get; set; } = Transform2D.Identity;
|
||||
[Export] private Vector3 property_Vector3 { get; set; } = new(10f, 10f, 10f);
|
||||
[Export] private Vector3i property_Vector3i { get; set; } = Vector3i.Back;
|
||||
[Export] private Basis property_Basis { get; set; } = new Basis(Quaternion.Identity);
|
||||
[Export] private Quaternion property_Quaternion { get; set; } = new Quaternion(Basis.Identity);
|
||||
[Export] private Transform3D property_Transform3D { get; set; } = Transform3D.Identity;
|
||||
[Export] private Vector4 property_Vector4 { get; set; } = new(10f, 10f, 10f, 10f);
|
||||
[Export] private Vector4i property_Vector4i { get; set; } = Vector4i.One;
|
||||
[Export] private Projection property_Projection { get; set; } = Projection.Identity;
|
||||
[Export] private AABB property_AABB { get; set; } = new AABB(10f, 10f, 10f, new Vector3(1f, 1f, 1f));
|
||||
[Export] private Color property_Color { get; set; } = Colors.Aquamarine;
|
||||
[Export] private Plane property_Plane { get; set; } = Plane.PlaneXZ;
|
||||
[Export] private Callable property_Callable { get; set; } = new Callable(Engine.GetMainLoop(), "_process");
|
||||
[Export] private SignalInfo property_SignalInfo { get; set; } = new SignalInfo(Engine.GetMainLoop(), "property_list_changed");
|
||||
|
||||
// Enums
|
||||
[SuppressMessage("ReSharper", "UnusedMember.Local")]
|
||||
enum MyEnum
|
||||
{
|
||||
A,
|
||||
B,
|
||||
C
|
||||
}
|
||||
|
||||
[Export] private MyEnum property_Enum { get; set; } = MyEnum.C;
|
||||
|
||||
[Flags]
|
||||
[SuppressMessage("ReSharper", "UnusedMember.Local")]
|
||||
enum MyFlagsEnum
|
||||
{
|
||||
A,
|
||||
B,
|
||||
C
|
||||
}
|
||||
|
||||
[Export] private MyFlagsEnum property_FlagsEnum { get; set; } = MyFlagsEnum.C;
|
||||
|
||||
// Arrays
|
||||
[Export] private Byte[] property_ByteArray { get; set; } = { 0, 1, 2, 3, 4, 5, 6 };
|
||||
[Export] private Int32[] property_Int32Array { get; set; } = { 0, 1, 2, 3, 4, 5, 6 };
|
||||
[Export] private Int64[] property_Int64Array { get; set; } = { 0, 1, 2, 3, 4, 5, 6 };
|
||||
[Export] private Single[] property_SingleArray { get; set; } = { 0f, 1f, 2f, 3f, 4f, 5f, 6f };
|
||||
[Export] private Double[] property_DoubleArray { get; set; } = { 0d, 1d, 2d, 3d, 4d, 5d, 6d };
|
||||
[Export] private String[] property_StringArray { get; set; } = { "foo", "bar" };
|
||||
[Export(PropertyHint.Enum, "A,B,C")] private String[] property_StringArrayEnum { get; set; } = { "foo", "bar" };
|
||||
[Export] private Vector2[] property_Vector2Array { get; set; } = { Vector2.Up, Vector2.Down, Vector2.Left, Vector2.Right };
|
||||
[Export] private Vector3[] property_Vector3Array { get; set; } = { Vector3.Up, Vector3.Down, Vector3.Left, Vector3.Right };
|
||||
[Export] private Color[] property_ColorArray { get; set; } = { Colors.Aqua, Colors.Aquamarine, Colors.Azure, Colors.Beige };
|
||||
[Export] private Godot.Object[] property_GodotObjectOrDerivedArray { get; set; } = { null };
|
||||
[Export] private object[] property_SystemObjectArray { get; set; } = { 0, 1f, 2d, "foo", Vector3i.Up };
|
||||
|
||||
// Generics
|
||||
[Export] private Godot.Collections.Dictionary<string, string> property_GodotGenericDictionary { get; set; } =
|
||||
new Godot.Collections.Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } };
|
||||
|
||||
[Export] private Godot.Collections.Array<string> property_GodotGenericArray { get; set; } =
|
||||
new Godot.Collections.Array<string> { "elem1", "elem2", "elem3" };
|
||||
|
||||
[Export] private System.Collections.Generic.Dictionary<string, string> property_SystemGenericDictionary { get; set; } =
|
||||
new System.Collections.Generic.Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } };
|
||||
|
||||
[Export] private System.Collections.Generic.List<string> property_SystemGenericList { get; set; } =
|
||||
new System.Collections.Generic.List<string> { "elem1", "elem2", "elem3" };
|
||||
|
||||
[Export] private System.Collections.Generic.IDictionary<string, string> property_GenericIDictionary { get; set; } =
|
||||
new System.Collections.Generic.Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } };
|
||||
|
||||
[Export] private System.Collections.Generic.ICollection<string> property_GenericICollection { get; set; } =
|
||||
new System.Collections.Generic.List<string> { "elem1", "elem2", "elem3" };
|
||||
|
||||
[Export] private System.Collections.Generic.IEnumerable<string> property_GenericIEnumerable { get; set; } =
|
||||
new System.Collections.Generic.List<string> { "elem1", "elem2", "elem3" };
|
||||
|
||||
// Variant
|
||||
[Export] private object property_SystemObject { get; set; } = "foo";
|
||||
|
||||
// Classes
|
||||
[Export] private Godot.Object property_GodotObjectOrDerived { get; set; }
|
||||
[Export] private Godot.Texture property_GodotResourceTexture { get; set; }
|
||||
[Export] private StringName property_StringName { get; set; } = new StringName("foo");
|
||||
[Export] private NodePath property_NodePath { get; set; } = new NodePath("foo");
|
||||
[Export] private RID property_RID { get; set; }
|
||||
|
||||
[Export] private Godot.Collections.Dictionary property_GodotDictionary { get; set; } =
|
||||
new() { { "foo", 10 }, { Vector2.Up, Colors.Chocolate } };
|
||||
|
||||
[Export] private Godot.Collections.Array property_GodotArray { get; set; } =
|
||||
new() { "foo", 10, Vector2.Up, Colors.Chocolate };
|
||||
|
||||
[Export] private System.Collections.IDictionary property_IDictionary { get; set; } =
|
||||
new System.Collections.Generic.Dictionary<object, object>
|
||||
{ { "foo", 10 }, { Vector2.Up, Colors.Chocolate } };
|
||||
|
||||
[Export] private System.Collections.ICollection property_ICollection { get; set; } =
|
||||
new System.Collections.Generic.List<object> { "foo", 10, Vector2.Up, Colors.Chocolate };
|
||||
|
||||
[Export] private System.Collections.IEnumerable property_IEnumerable { get; set; } =
|
||||
new System.Collections.Generic.List<object> { "foo", 10, Vector2.Up, Colors.Chocolate };
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,21 @@
|
||||
#pragma warning disable CS0169
|
||||
|
||||
namespace Godot.SourceGenerators.Sample
|
||||
{
|
||||
partial class Generic<T> : Godot.Object
|
||||
{
|
||||
private int _field;
|
||||
}
|
||||
|
||||
// Generic again but different generic parameters
|
||||
partial class Generic<T, R> : Godot.Object
|
||||
{
|
||||
private int _field;
|
||||
}
|
||||
|
||||
// Generic again but without generic parameters
|
||||
partial class Generic : Godot.Object
|
||||
{
|
||||
private int _field;
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -7,6 +7,8 @@
|
||||
<PropertyGroup>
|
||||
<!-- $(GodotProjectDir) would normally be defined by the Godot.NET.Sdk -->
|
||||
<GodotProjectDir>$(MSBuildProjectDirectory)</GodotProjectDir>
|
||||
<!-- For compiling GetGodotPropertyDefaultValues. -->
|
||||
<DefineConstants>$(DefineConstants);TOOLS</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
#pragma warning disable CS0169
|
||||
|
||||
namespace Godot.SourceGenerators.Sample
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user