diff options
author | Ignacio Roldán Etcheverry <ignalfonsore@gmail.com> | 2022-07-28 17:41:49 +0200 |
---|---|---|
committer | Ignacio Roldán Etcheverry <ignalfonsore@gmail.com> | 2022-08-22 03:36:52 +0200 |
commit | 344f5028d48d4a5caf321abdf023c34f52aae0a4 (patch) | |
tree | 929d1feecedfe4e3553b03f3c72023af135049f7 /modules/mono/glue | |
parent | a9892f257153a2d760a5d221dc16e484e1428c71 (diff) |
C#: Add dedicated Variant struct, replacing System.Object
Diffstat (limited to 'modules/mono/glue')
11 files changed, 735 insertions, 69 deletions
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs index afe295126e..8ceb7ea882 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs @@ -714,5 +714,11 @@ namespace Godot.Collections /// </summary> /// <returns>A string representation of this array.</returns> public override string ToString() => _underlyingArray.ToString(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Array<T> from) => Variant.From(from); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Array<T>(Variant from) => from.AsGodotGenericArray<T>(); } } diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/CSharpInstanceBridge.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/CSharpInstanceBridge.cs index 9ede67b285..3636a08377 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/CSharpInstanceBridge.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/CSharpInstanceBridge.cs @@ -63,7 +63,7 @@ namespace Godot.Bridge var nameManaged = StringName.CreateTakingOwnershipOfDisposableValue( NativeFuncs.godotsharp_string_name_new_copy(CustomUnsafe.AsRef(name))); - object valueManaged = Marshaling.ConvertVariantToManagedObject(CustomUnsafe.AsRef(value)); + Variant valueManaged = Variant.CreateCopyingBorrowed(*value); return godotObject._Set(nameManaged, valueManaged).ToGodotBool(); } @@ -94,9 +94,9 @@ namespace Godot.Bridge var nameManaged = StringName.CreateTakingOwnershipOfDisposableValue( NativeFuncs.godotsharp_string_name_new_copy(CustomUnsafe.AsRef(name))); - object ret = godotObject._Get(nameManaged); + Variant ret = godotObject._Get(nameManaged); - if (ret == null) + if (ret.VariantType == Variant.Type.Nil) { *outRet = default; return godot_bool.False; diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs index 106cd7619f..a71ee1190e 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs @@ -5,6 +5,7 @@ using Godot.NativeInterop; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Linq; +using System.Runtime.CompilerServices; namespace Godot.Collections { @@ -805,5 +806,11 @@ namespace Godot.Collections /// </summary> /// <returns>A string representation of this dictionary.</returns> public override string ToString() => _underlyingDict.ToString(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Dictionary<TKey, TValue> from) => Variant.From(from); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Dictionary<TKey, TValue>(Variant from) => from.AsGodotGenericDictionary<TKey, TValue>(); } } diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs index 563af91ae5..8802f229b3 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs @@ -148,9 +148,6 @@ namespace Godot.NativeInterop if (typeof(Godot.Object[]).IsAssignableFrom(type)) return Variant.Type.Array; - - if (type == typeof(object[])) - return Variant.Type.Array; } else if (type.IsGenericType) { @@ -178,7 +175,7 @@ namespace Godot.NativeInterop if (typeof(Godot.Object).IsAssignableFrom(type)) return Variant.Type.Object; } - else if (type == typeof(object)) + else if (type == typeof(Variant)) { r_nil_is_variant = true; return Variant.Type.Nil; @@ -315,16 +312,6 @@ namespace Godot.NativeInterop return VariantUtils.CreateFromSystemArrayOfSupportedType(ridArray); case Godot.Object[] godotObjectArray: return VariantUtils.CreateFromSystemArrayOfGodotObject(godotObjectArray); - case object[] objectArray: // Last one to avoid catching others like string[] and Godot.Object[] - { - // The pattern match for `object[]` catches arrays on any reference type, - // so we need to check the actual type to make sure it's truly `object[]`. - if (objectArray.GetType() == typeof(object[])) - return VariantUtils.CreateFromSystemArrayOfVariant(objectArray); - - GD.PushError("Attempted to convert a managed array of unmarshallable element type to Variant."); - return new godot_variant(); - } case Godot.Object godotObject: return VariantUtils.CreateFromGodotObject(godotObject); case StringName stringName: @@ -337,6 +324,8 @@ namespace Godot.NativeInterop return VariantUtils.CreateFromDictionary(godotDictionary); case Collections.Array godotArray: return VariantUtils.CreateFromArray(godotArray); + case Variant variant: + return NativeFuncs.godotsharp_variant_new_copy((godot_variant)variant.NativeVar); case Collections.IGenericGodotDictionary genericGodotDictionary: { var godotDict = genericGodotDictionary.UnderlyingDictionary; @@ -509,8 +498,9 @@ namespace Godot.NativeInterop return ConvertVariantToSystemArrayOfType(p_var, type); else if (type.IsGenericType) return ConvertVariantToManagedObjectOfGenericType(p_var, type); - else if (type == typeof(object)) - return ConvertVariantToManagedObject(p_var); + else if (type == typeof(Variant)) + return Variant.CreateCopyingBorrowed(p_var); + if (ConvertVariantToManagedObjectOfClass(p_var, type, out object? res)) return res; @@ -564,9 +554,6 @@ namespace Godot.NativeInterop if (typeof(Godot.Object[]).IsAssignableFrom(type)) return VariantUtils.ConvertToSystemArrayOfGodotObject(p_var, type); - if (type == typeof(object[])) - return VariantUtils.ConvertToSystemArrayOfVariant(p_var); - GD.PushError("Attempted to convert Variant to array of unsupported element type. Name: " + type.GetElementType()!.FullName + "."); return null; @@ -962,19 +949,6 @@ namespace Godot.NativeInterop // Array - public static object[] ConvertNativeGodotArrayToSystemArray(in godot_array p_array) - { - var array = Collections.Array.CreateTakingOwnershipOfDisposableValue( - NativeFuncs.godotsharp_array_new_copy(p_array)); - - int length = array.Count; - var ret = new object[length]; - - array.CopyTo(ret, 0); // ConvertVariantToManagedObject handled by Collections.Array - - return ret; - } - internal static T[] ConvertNativeGodotArrayToSystemArrayOfType<T>(in godot_array p_array) { var array = Collections.Array.CreateTakingOwnershipOfDisposableValue( @@ -1019,23 +993,6 @@ namespace Godot.NativeInterop return ret; } - public static godot_array ConvertSystemArrayToNativeGodotArray(object[] p_array) - { - int length = p_array.Length; - - if (length == 0) - return NativeFuncs.godotsharp_array_new(); - - using var array = new Collections.Array(); - array.Resize(length); - - for (int i = 0; i < length; i++) - array[i] = p_array[i]; - - var src = (godot_array)array.NativeValue; - return NativeFuncs.godotsharp_array_new_copy(src); - } - public static godot_array ConvertSystemArrayToNativeGodotArray<T>(T[] p_array) { int length = p_array.Length; diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs index b2df6e16b1..e56e4944b0 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs @@ -200,6 +200,9 @@ namespace Godot.NativeInterop godotsharp_variant_new_string_name(out godot_variant r_dest, in godot_string_name p_s); [DllImport(GodotDllName)] + public static extern void godotsharp_variant_new_copy(out godot_variant r_dest, in godot_variant p_src); + + [DllImport(GodotDllName)] public static extern void godotsharp_variant_new_node_path(out godot_variant r_dest, in godot_node_path p_np); [DllImport(GodotDllName)] diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.extended.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.extended.cs index 0c49660cf0..2ea3c18d26 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.extended.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.extended.cs @@ -4,6 +4,44 @@ namespace Godot.NativeInterop { public static partial class NativeFuncs { + public static godot_variant godotsharp_variant_new_copy(in godot_variant src) + { + switch (src.Type) + { + case Variant.Type.Nil: + return default; + case Variant.Type.Bool: + return new godot_variant() { Bool = src.Bool }; + case Variant.Type.Int: + return new godot_variant() { Int = src.Int }; + case Variant.Type.Float: + return new godot_variant() { Float = src.Float }; + case Variant.Type.Vector2: + return new godot_variant() { Vector2 = src.Vector2 }; + case Variant.Type.Vector2i: + return new godot_variant() { Vector2i = src.Vector2i }; + case Variant.Type.Rect2: + return new godot_variant() { Rect2 = src.Rect2 }; + case Variant.Type.Rect2i: + return new godot_variant() { Rect2i = src.Rect2i }; + case Variant.Type.Vector3: + return new godot_variant() { Vector3 = src.Vector3 }; + case Variant.Type.Vector3i: + return new godot_variant() { Vector3i = src.Vector3i }; + case Variant.Type.Plane: + return new godot_variant() { Plane = src.Plane }; + case Variant.Type.Quaternion: + return new godot_variant() { Quaternion = src.Quaternion }; + case Variant.Type.Color: + return new godot_variant() { Color = src.Color }; + case Variant.Type.Rid: + return new godot_variant() { RID = src.RID }; + } + + godotsharp_variant_new_copy(out godot_variant ret, src); + return ret; + } + public static godot_string_name godotsharp_string_name_new_copy(in godot_string_name src) { if (src.IsEmpty) diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs index 7aa27c2867..fe0d7104ea 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs @@ -227,14 +227,6 @@ namespace Godot.NativeInterop return CreateFromArray(array); } - public static godot_variant CreateFromSystemArrayOfVariant(object[]? from) - { - if (from == null) - return default; // Nil - using godot_array array = Marshaling.ConvertSystemArrayToNativeGodotArray(from); - return CreateFromArray(array); - } - public static godot_variant CreateFromArray(godot_array from) { NativeFuncs.godotsharp_variant_new_array(out godot_variant ret, from); @@ -671,12 +663,6 @@ namespace Godot.NativeInterop return Marshaling.ConvertNativeGodotArrayToSystemArrayOfGodotObjectType(godotArray, type); } - public static object[] ConvertToSystemArrayOfVariant(in godot_variant p_var) - { - using var godotArray = NativeFuncs.godotsharp_variant_as_array(p_var); - return Marshaling.ConvertNativeGodotArrayToSystemArray(godotArray); - } - public static Array<T> ConvertToGenericArrayObject<T>(in godot_variant p_var) => new(ConvertToArrayObject(p_var)); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Object.base.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Object.base.cs index dd3f8477d9..04920ccfab 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Object.base.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Object.base.cs @@ -1,6 +1,4 @@ using System; -using System.Linq; -using System.Reflection; using System.Runtime.InteropServices; using Godot.Bridge; using Godot.NativeInterop; diff --git a/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj b/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj index d95b839767..0a61069a1e 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj +++ b/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj @@ -124,6 +124,7 @@ <Compile Include="Core\Vector4i.cs" /> <Compile Include="GlobalUsings.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="Variant.cs" /> </ItemGroup> <!-- We import a props file with auto-generated includes. This works well with Rider. diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Variant.cs b/modules/mono/glue/GodotSharp/GodotSharp/Variant.cs new file mode 100644 index 0000000000..c1c321829a --- /dev/null +++ b/modules/mono/glue/GodotSharp/GodotSharp/Variant.cs @@ -0,0 +1,665 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using Godot.NativeInterop; + +namespace Godot; + +#nullable enable + +[SuppressMessage("ReSharper", "RedundantNameQualifier")] +public partial struct Variant : IDisposable +{ + internal godot_variant.movable NativeVar; + private object? _obj; + private Disposer? _disposer; + + private class Disposer : IDisposable + { + private godot_variant.movable _native; + + private WeakReference<IDisposable>? _weakReferenceToSelf; + + public Disposer(in godot_variant.movable nativeVar) + { + _native = nativeVar; + _weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this); + } + + ~Disposer() + { + Dispose(false); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + public void Dispose(bool disposing) + { + _native.DangerousSelfRef.Dispose(); + + if (_weakReferenceToSelf != null) + { + DisposablesTracker.UnregisterDisposable(_weakReferenceToSelf); + } + } + } + + private Variant(in godot_variant nativeVar) + { + NativeVar = (godot_variant.movable)nativeVar; + _obj = null; + + switch (nativeVar.Type) + { + case Type.Nil: + case Type.Bool: + case Type.Int: + case Type.Float: + case Type.Vector2: + case Type.Vector2i: + case Type.Rect2: + case Type.Rect2i: + case Type.Vector3: + case Type.Vector3i: + case Type.Plane: + case Type.Quaternion: + case Type.Color: + case Type.Rid: + _disposer = null; + break; + default: + { + _disposer = new Disposer(NativeVar); + break; + } + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // Explicit name to make it very clear + public static Variant CreateTakingOwnershipOfDisposableValue(in godot_variant nativeValueToOwn) => + new(nativeValueToOwn); + + // Explicit name to make it very clear + public static Variant CreateCopyingBorrowed(in godot_variant nativeValueToOwn) => + new(NativeFuncs.godotsharp_variant_new_copy(nativeValueToOwn)); + + /// <summary> + /// Constructs a new <see cref="Godot.NativeInterop.godot_variant"/> from this instance. + /// The caller is responsible of disposing the new instance to avoid memory leaks. + /// </summary> + public godot_variant CopyNativeVariant() => + NativeFuncs.godotsharp_variant_new_copy((godot_variant)NativeVar); + + public void Dispose() + { + _disposer?.Dispose(); + NativeVar = default; + _obj = null; + } + + // TODO: Consider renaming Variant.Type to VariantType and this property to Type. VariantType would also avoid ambiguity with System.Type. + public Type VariantType => NativeVar.DangerousSelfRef.Type; + + public object? Obj + { + get + { + if (_obj == null) + _obj = Marshaling.ConvertVariantToManagedObject((godot_variant)NativeVar); + + return _obj; + } + } + + // TODO: Consider implicit operators + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool AsBool() => + VariantUtils.ConvertToBool((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public char AsChar() => + (char)VariantUtils.ConvertToUInt16((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public sbyte AsSByte() => + VariantUtils.ConvertToInt8((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public short AsInt16() => + VariantUtils.ConvertToInt16((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int AsInt32() => + VariantUtils.ConvertToInt32((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public long AsInt64() => + VariantUtils.ConvertToInt64((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public byte AsByte() => + VariantUtils.ConvertToUInt8((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ushort AsUInt16() => + VariantUtils.ConvertToUInt16((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public uint AsUInt32() => + VariantUtils.ConvertToUInt32((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ulong AsUInt64() => + VariantUtils.ConvertToUInt64((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public float AsSingle() => + VariantUtils.ConvertToFloat32((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public double AsDouble() => + VariantUtils.ConvertToFloat64((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public string AsString() => + VariantUtils.ConvertToStringObject((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector2 AsVector2() => + VariantUtils.ConvertToVector2((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector2i AsVector2i() => + VariantUtils.ConvertToVector2i((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Rect2 AsRect2() => + VariantUtils.ConvertToRect2((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Rect2i AsRect2i() => + VariantUtils.ConvertToRect2i((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Transform2D AsTransform2D() => + VariantUtils.ConvertToTransform2D((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector3 AsVector3() => + VariantUtils.ConvertToVector3((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector3i AsVector3i() => + VariantUtils.ConvertToVector3i((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Basis AsBasis() => + VariantUtils.ConvertToBasis((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Quaternion AsQuaternion() => + VariantUtils.ConvertToQuaternion((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Transform3D AsTransform3D() => + VariantUtils.ConvertToTransform3D((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public AABB AsAABB() => + VariantUtils.ConvertToAABB((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Color AsColor() => + VariantUtils.ConvertToColor((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Plane AsPlane() => + VariantUtils.ConvertToPlane((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Callable AsCallable() => + VariantUtils.ConvertToCallableManaged((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public SignalInfo AsSignalInfo() => + VariantUtils.ConvertToSignalInfo((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public byte[] AsByteArray() => + VariantUtils.ConvertAsPackedByteArrayToSystemArray((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int[] AsInt32Array() => + VariantUtils.ConvertAsPackedInt32ArrayToSystemArray((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public long[] AsInt64Array() => + VariantUtils.ConvertAsPackedInt64ArrayToSystemArray((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public float[] AsFloat32Array() => + VariantUtils.ConvertAsPackedFloat32ArrayToSystemArray((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public double[] AsFloat64Array() => + VariantUtils.ConvertAsPackedFloat64ArrayToSystemArray((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public string[] AsStringArray() => + VariantUtils.ConvertAsPackedStringArrayToSystemArray((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector2[] AsVector2Array() => + VariantUtils.ConvertAsPackedVector2ArrayToSystemArray((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector3[] AsVector3Array() => + VariantUtils.ConvertAsPackedVector3ArrayToSystemArray((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Color[] AsColorArray() => + VariantUtils.ConvertAsPackedColorArrayToSystemArray((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public T[] AsGodotObjectArray<T>() + where T : Godot.Object => + VariantUtils.ConvertToSystemArrayOfGodotObject<T>((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public T[] AsSystemArrayOfSupportedType<T>() => + VariantUtils.ConvertToSystemArrayOfSupportedType<T>((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Collections.Dictionary<TKey, TValue> AsGodotGenericDictionary<TKey, TValue>() => + VariantUtils.ConvertToGenericDictionaryObject<TKey, TValue>((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Collections.Array<T> AsGodotGenericArray<T>() => + VariantUtils.ConvertToGenericArrayObject<T>((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public System.Collections.Generic.Dictionary<TKey, TValue> AsSystemGenericDictionary<TKey, TValue>() + where TKey : notnull => + VariantUtils.ConvertToSystemGenericDictionary<TKey, TValue>((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public System.Collections.Generic.List<T> AsSystemGenericList<T>() => + VariantUtils.ConvertToSystemGenericList<T>((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Godot.Object AsGodotObject() => + VariantUtils.ConvertToGodotObject((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public StringName AsStringName() => + VariantUtils.ConvertToStringNameObject((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public NodePath AsNodePath() => + VariantUtils.ConvertToNodePathObject((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public RID AsRID() => + VariantUtils.ConvertToRID((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Collections.Dictionary AsGodotDictionary() => + VariantUtils.ConvertToDictionaryObject((godot_variant)NativeVar); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Collections.Array AsGodotArray() => + VariantUtils.ConvertToArrayObject((godot_variant)NativeVar); + + // Explicit conversion operators to supported types + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator bool(Variant from) => from.AsBool(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator char(Variant from) => from.AsChar(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator sbyte(Variant from) => from.AsSByte(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator short(Variant from) => from.AsInt16(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator int(Variant from) => from.AsInt32(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator long(Variant from) => from.AsInt64(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator byte(Variant from) => from.AsByte(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator ushort(Variant from) => from.AsUInt16(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator uint(Variant from) => from.AsUInt32(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator ulong(Variant from) => from.AsUInt64(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator float(Variant from) => from.AsSingle(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator double(Variant from) => from.AsDouble(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator string(Variant from) => from.AsString(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Vector2(Variant from) => from.AsVector2(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Vector2i(Variant from) => from.AsVector2i(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Rect2(Variant from) => from.AsRect2(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Rect2i(Variant from) => from.AsRect2i(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Transform2D(Variant from) => from.AsTransform2D(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Vector3(Variant from) => from.AsVector3(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Vector3i(Variant from) => from.AsVector3i(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Basis(Variant from) => from.AsBasis(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Quaternion(Variant from) => from.AsQuaternion(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Transform3D(Variant from) => from.AsTransform3D(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator AABB(Variant from) => from.AsAABB(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Color(Variant from) => from.AsColor(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Plane(Variant from) => from.AsPlane(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Callable(Variant from) => from.AsCallable(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator SignalInfo(Variant from) => from.AsSignalInfo(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator byte[](Variant from) => from.AsByteArray(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator int[](Variant from) => from.AsInt32Array(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator long[](Variant from) => from.AsInt64Array(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator float[](Variant from) => from.AsFloat32Array(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator double[](Variant from) => from.AsFloat64Array(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator string[](Variant from) => from.AsStringArray(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Vector2[](Variant from) => from.AsVector2Array(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Vector3[](Variant from) => from.AsVector3Array(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Color[](Variant from) => from.AsColorArray(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Godot.Object(Variant from) => from.AsGodotObject(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator StringName(Variant from) => from.AsStringName(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator NodePath(Variant from) => from.AsNodePath(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator RID(Variant from) => from.AsRID(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Collections.Dictionary(Variant from) => from.AsGodotDictionary(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static explicit operator Collections.Array(Variant from) => from.AsGodotArray(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(bool from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromBool(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(char from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromInt(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(sbyte from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromInt(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(short from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromInt(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(int from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromInt(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(long from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromInt(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(byte from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromInt(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(ushort from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromInt(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(uint from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromInt(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(ulong from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromInt(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(float from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromFloat(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(double from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromFloat(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(string from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromString(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Vector2 from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromVector2(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Vector2i from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromVector2i(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Rect2 from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromRect2(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Rect2i from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromRect2i(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Transform2D from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromTransform2D(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Vector3 from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromVector3(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Vector3i from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromVector3i(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Basis from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromBasis(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Quaternion from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromQuaternion(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Transform3D from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromTransform3D(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(AABB from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromAABB(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Color from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromColor(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Plane from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromPlane(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Callable from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromCallable(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(SignalInfo from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromSignalInfo(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Span<byte> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromPackedByteArray(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Span<int> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromPackedInt32Array(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Span<long> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromPackedInt64Array(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Span<float> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromPackedFloat32Array(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Span<double> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromPackedFloat64Array(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Span<string> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromPackedStringArray(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Span<Vector2> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromPackedVector2Array(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Span<Vector3> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromPackedVector3Array(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Span<Color> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromPackedColorArray(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Godot.Object[]? from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromSystemArrayOfGodotObject(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Variant From<TKey, TValue>(Collections.Dictionary<TKey, TValue> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromDictionary(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Variant From<T>(Collections.Array<T> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromArray(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Variant From<TKey, TValue>(System.Collections.Generic.Dictionary<TKey, TValue> from) + where TKey : notnull => CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromSystemDictionary(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Variant From<T>(System.Collections.Generic.List<T> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromSystemICollection(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Variant From<TKey, TValue>(System.Collections.Generic.IDictionary<TKey, TValue> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromSystemGenericIDictionary(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Variant From<T>(System.Collections.Generic.ICollection<T> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromSystemGenericICollection(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Variant From<T>(System.Collections.Generic.IEnumerable<T> from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromSystemGenericIEnumerable(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Godot.Object from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromGodotObject(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(StringName from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromStringName(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(NodePath from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromNodePath(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(RID from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromRID(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Collections.Dictionary from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromDictionary(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Variant(Collections.Array from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromArray(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Variant From(System.Collections.IDictionary from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromSystemIDictionary(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Variant From(System.Collections.ICollection from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromSystemICollection(from)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Variant From(System.Collections.IEnumerable from) => + CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromSystemIEnumerable(from)); +} diff --git a/modules/mono/glue/runtime_interop.cpp b/modules/mono/glue/runtime_interop.cpp index fc28e7beb2..751d4b1d23 100644 --- a/modules/mono/glue/runtime_interop.cpp +++ b/modules/mono/glue/runtime_interop.cpp @@ -540,6 +540,10 @@ GD_PINVOKE_EXPORT godot_variant godotsharp_method_bind_call(MethodBind *p_method // variant.h +GD_PINVOKE_EXPORT void godotsharp_variant_new_copy(godot_variant *r_dest, const Variant *p_src) { + memnew_placement(r_dest, Variant(*p_src)); +} + GD_PINVOKE_EXPORT void godotsharp_variant_new_string_name(godot_variant *r_dest, const StringName *p_s) { memnew_placement(r_dest, Variant(*p_s)); } @@ -1315,7 +1319,7 @@ GD_PINVOKE_EXPORT void godotsharp_object_to_string(Object *p_ptr, godot_string * #endif // We need this to prevent the functions from being stripped. -void *godotsharp_pinvoke_funcs[185] = { +void *godotsharp_pinvoke_funcs[186] = { (void *)godotsharp_method_bind_get_method, (void *)godotsharp_get_class_constructor, (void *)godotsharp_engine_get_singleton, @@ -1359,6 +1363,7 @@ void *godotsharp_pinvoke_funcs[185] = { (void *)godotsharp_callable_call_deferred, (void *)godotsharp_method_bind_ptrcall, (void *)godotsharp_method_bind_call, + (void *)godotsharp_variant_new_copy, (void *)godotsharp_variant_new_string_name, (void *)godotsharp_variant_new_node_path, (void *)godotsharp_variant_new_object, |