2021-12-28 23:25:16 +01:00
|
|
|
using System;
|
2022-02-27 21:57:30 +01:00
|
|
|
using System.Linq;
|
2022-02-27 21:57:46 +01:00
|
|
|
using System.Text;
|
2021-12-28 23:25:16 +01:00
|
|
|
using Microsoft.CodeAnalysis;
|
|
|
|
|
|
|
|
namespace Godot.SourceGenerators
|
|
|
|
{
|
2022-02-27 21:57:30 +01:00
|
|
|
internal static class MarshalUtils
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
|
|
|
public class TypeCache
|
|
|
|
{
|
|
|
|
public INamedTypeSymbol GodotObjectType { get; }
|
|
|
|
|
2022-08-15 05:57:52 +02:00
|
|
|
public TypeCache(Compilation compilation)
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
|
|
|
INamedTypeSymbol GetTypeByMetadataNameOrThrow(string fullyQualifiedMetadataName)
|
|
|
|
{
|
2022-08-15 05:57:52 +02:00
|
|
|
return compilation.GetTypeByMetadataName(fullyQualifiedMetadataName) ??
|
2022-08-24 13:54:47 +02:00
|
|
|
throw new InvalidOperationException($"Type not found: '{fullyQualifiedMetadataName}'.");
|
2021-12-28 23:25:16 +01:00
|
|
|
}
|
|
|
|
|
2022-12-07 16:16:51 +01:00
|
|
|
GodotObjectType = GetTypeByMetadataNameOrThrow(GodotClasses.GodotObject);
|
2021-12-28 23:25:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 21:57:30 +01:00
|
|
|
public static VariantType? ConvertMarshalTypeToVariantType(MarshalType marshalType)
|
|
|
|
=> marshalType switch
|
|
|
|
{
|
|
|
|
MarshalType.Boolean => VariantType.Bool,
|
|
|
|
MarshalType.Char => VariantType.Int,
|
|
|
|
MarshalType.SByte => VariantType.Int,
|
|
|
|
MarshalType.Int16 => VariantType.Int,
|
|
|
|
MarshalType.Int32 => VariantType.Int,
|
|
|
|
MarshalType.Int64 => VariantType.Int,
|
|
|
|
MarshalType.Byte => VariantType.Int,
|
|
|
|
MarshalType.UInt16 => VariantType.Int,
|
|
|
|
MarshalType.UInt32 => VariantType.Int,
|
|
|
|
MarshalType.UInt64 => VariantType.Int,
|
|
|
|
MarshalType.Single => VariantType.Float,
|
|
|
|
MarshalType.Double => VariantType.Float,
|
|
|
|
MarshalType.String => VariantType.String,
|
|
|
|
MarshalType.Vector2 => VariantType.Vector2,
|
2022-12-07 16:11:39 +01:00
|
|
|
MarshalType.Vector2I => VariantType.Vector2I,
|
2022-02-27 21:57:30 +01:00
|
|
|
MarshalType.Rect2 => VariantType.Rect2,
|
2022-12-07 16:11:39 +01:00
|
|
|
MarshalType.Rect2I => VariantType.Rect2I,
|
|
|
|
MarshalType.Transform2D => VariantType.Transform2D,
|
2022-02-27 21:57:30 +01:00
|
|
|
MarshalType.Vector3 => VariantType.Vector3,
|
2022-12-07 16:11:39 +01:00
|
|
|
MarshalType.Vector3I => VariantType.Vector3I,
|
2022-02-27 21:57:30 +01:00
|
|
|
MarshalType.Basis => VariantType.Basis,
|
|
|
|
MarshalType.Quaternion => VariantType.Quaternion,
|
2022-12-07 16:11:39 +01:00
|
|
|
MarshalType.Transform3D => VariantType.Transform3D,
|
2022-02-27 21:57:30 +01:00
|
|
|
MarshalType.Vector4 => VariantType.Vector4,
|
2022-12-07 16:11:39 +01:00
|
|
|
MarshalType.Vector4I => VariantType.Vector4I,
|
2022-02-27 21:57:30 +01:00
|
|
|
MarshalType.Projection => VariantType.Projection,
|
2022-12-07 16:11:39 +01:00
|
|
|
MarshalType.Aabb => VariantType.Aabb,
|
2022-02-27 21:57:30 +01:00
|
|
|
MarshalType.Color => VariantType.Color,
|
|
|
|
MarshalType.Plane => VariantType.Plane,
|
|
|
|
MarshalType.Callable => VariantType.Callable,
|
2022-12-12 17:14:21 +01:00
|
|
|
MarshalType.Signal => VariantType.Signal,
|
2022-02-27 21:57:30 +01:00
|
|
|
MarshalType.Enum => VariantType.Int,
|
|
|
|
MarshalType.ByteArray => VariantType.PackedByteArray,
|
|
|
|
MarshalType.Int32Array => VariantType.PackedInt32Array,
|
|
|
|
MarshalType.Int64Array => VariantType.PackedInt64Array,
|
2022-02-27 21:57:46 +01:00
|
|
|
MarshalType.Float32Array => VariantType.PackedFloat32Array,
|
|
|
|
MarshalType.Float64Array => VariantType.PackedFloat64Array,
|
2022-02-27 21:57:30 +01:00
|
|
|
MarshalType.StringArray => VariantType.PackedStringArray,
|
|
|
|
MarshalType.Vector2Array => VariantType.PackedVector2Array,
|
|
|
|
MarshalType.Vector3Array => VariantType.PackedVector3Array,
|
|
|
|
MarshalType.ColorArray => VariantType.PackedColorArray,
|
|
|
|
MarshalType.GodotObjectOrDerivedArray => VariantType.Array,
|
2022-07-28 17:41:50 +02:00
|
|
|
MarshalType.SystemArrayOfStringName => VariantType.Array,
|
|
|
|
MarshalType.SystemArrayOfNodePath => VariantType.Array,
|
2022-12-07 16:11:39 +01:00
|
|
|
MarshalType.SystemArrayOfRid => VariantType.Array,
|
2022-07-28 17:41:49 +02:00
|
|
|
MarshalType.Variant => VariantType.Nil,
|
2022-02-27 21:57:30 +01:00
|
|
|
MarshalType.GodotObjectOrDerived => VariantType.Object,
|
|
|
|
MarshalType.StringName => VariantType.StringName,
|
|
|
|
MarshalType.NodePath => VariantType.NodePath,
|
2022-12-07 16:11:39 +01:00
|
|
|
MarshalType.Rid => VariantType.Rid,
|
2022-02-27 21:57:30 +01:00
|
|
|
MarshalType.GodotDictionary => VariantType.Dictionary,
|
|
|
|
MarshalType.GodotArray => VariantType.Array,
|
2022-08-08 01:53:54 +02:00
|
|
|
MarshalType.GodotGenericDictionary => VariantType.Dictionary,
|
|
|
|
MarshalType.GodotGenericArray => VariantType.Array,
|
2022-02-27 21:57:30 +01:00
|
|
|
_ => null
|
|
|
|
};
|
|
|
|
|
|
|
|
public static MarshalType? ConvertManagedTypeToMarshalType(ITypeSymbol type, TypeCache typeCache)
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
|
|
|
var specialType = type.SpecialType;
|
|
|
|
|
|
|
|
switch (specialType)
|
|
|
|
{
|
|
|
|
case SpecialType.System_Boolean:
|
|
|
|
return MarshalType.Boolean;
|
|
|
|
case SpecialType.System_Char:
|
|
|
|
return MarshalType.Char;
|
|
|
|
case SpecialType.System_SByte:
|
|
|
|
return MarshalType.SByte;
|
|
|
|
case SpecialType.System_Int16:
|
|
|
|
return MarshalType.Int16;
|
|
|
|
case SpecialType.System_Int32:
|
|
|
|
return MarshalType.Int32;
|
|
|
|
case SpecialType.System_Int64:
|
|
|
|
return MarshalType.Int64;
|
|
|
|
case SpecialType.System_Byte:
|
|
|
|
return MarshalType.Byte;
|
|
|
|
case SpecialType.System_UInt16:
|
|
|
|
return MarshalType.UInt16;
|
|
|
|
case SpecialType.System_UInt32:
|
|
|
|
return MarshalType.UInt32;
|
|
|
|
case SpecialType.System_UInt64:
|
|
|
|
return MarshalType.UInt64;
|
|
|
|
case SpecialType.System_Single:
|
|
|
|
return MarshalType.Single;
|
|
|
|
case SpecialType.System_Double:
|
|
|
|
return MarshalType.Double;
|
|
|
|
case SpecialType.System_String:
|
|
|
|
return MarshalType.String;
|
2022-02-27 21:57:30 +01:00
|
|
|
default:
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
2022-02-27 21:57:30 +01:00
|
|
|
var typeKind = type.TypeKind;
|
|
|
|
|
|
|
|
if (typeKind == TypeKind.Enum)
|
|
|
|
return MarshalType.Enum;
|
|
|
|
|
|
|
|
if (typeKind == TypeKind.Struct)
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
2022-09-22 12:41:38 +02:00
|
|
|
if (type.ContainingAssembly?.Name == "GodotSharp" &&
|
|
|
|
type.ContainingNamespace?.Name == "Godot")
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
2022-02-27 21:57:30 +01:00
|
|
|
return type switch
|
|
|
|
{
|
|
|
|
{ Name: "Vector2" } => MarshalType.Vector2,
|
2022-12-07 16:11:39 +01:00
|
|
|
{ Name: "Vector2I" } => MarshalType.Vector2I,
|
2022-02-27 21:57:30 +01:00
|
|
|
{ Name: "Rect2" } => MarshalType.Rect2,
|
2022-12-07 16:11:39 +01:00
|
|
|
{ Name: "Rect2I" } => MarshalType.Rect2I,
|
2022-02-27 21:57:30 +01:00
|
|
|
{ Name: "Transform2D" } => MarshalType.Transform2D,
|
|
|
|
{ Name: "Vector3" } => MarshalType.Vector3,
|
2022-12-07 16:11:39 +01:00
|
|
|
{ Name: "Vector3I" } => MarshalType.Vector3I,
|
2022-02-27 21:57:30 +01:00
|
|
|
{ Name: "Basis" } => MarshalType.Basis,
|
|
|
|
{ Name: "Quaternion" } => MarshalType.Quaternion,
|
|
|
|
{ Name: "Transform3D" } => MarshalType.Transform3D,
|
|
|
|
{ Name: "Vector4" } => MarshalType.Vector4,
|
2022-12-07 16:11:39 +01:00
|
|
|
{ Name: "Vector4I" } => MarshalType.Vector4I,
|
2022-02-27 21:57:30 +01:00
|
|
|
{ Name: "Projection" } => MarshalType.Projection,
|
2022-12-07 16:11:39 +01:00
|
|
|
{ Name: "Aabb" } => MarshalType.Aabb,
|
2022-02-27 21:57:30 +01:00
|
|
|
{ Name: "Color" } => MarshalType.Color,
|
|
|
|
{ Name: "Plane" } => MarshalType.Plane,
|
2022-12-07 16:11:39 +01:00
|
|
|
{ Name: "Rid" } => MarshalType.Rid,
|
2022-02-27 21:57:30 +01:00
|
|
|
{ Name: "Callable" } => MarshalType.Callable,
|
2022-12-12 17:14:21 +01:00
|
|
|
{ Name: "Signal" } => MarshalType.Signal,
|
2022-07-28 17:41:49 +02:00
|
|
|
{ Name: "Variant" } => MarshalType.Variant,
|
2022-02-27 21:57:30 +01:00
|
|
|
_ => null
|
|
|
|
};
|
|
|
|
}
|
2021-12-28 23:25:16 +01:00
|
|
|
}
|
2022-02-27 21:57:30 +01:00
|
|
|
else if (typeKind == TypeKind.Array)
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
|
|
|
var arrayType = (IArrayTypeSymbol)type;
|
2022-09-16 23:51:17 +02:00
|
|
|
|
|
|
|
if (arrayType.Rank != 1)
|
|
|
|
return null;
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
var elementType = arrayType.ElementType;
|
|
|
|
|
|
|
|
switch (elementType.SpecialType)
|
|
|
|
{
|
|
|
|
case SpecialType.System_Byte:
|
|
|
|
return MarshalType.ByteArray;
|
|
|
|
case SpecialType.System_Int32:
|
|
|
|
return MarshalType.Int32Array;
|
|
|
|
case SpecialType.System_Int64:
|
|
|
|
return MarshalType.Int64Array;
|
|
|
|
case SpecialType.System_Single:
|
2022-02-27 21:57:46 +01:00
|
|
|
return MarshalType.Float32Array;
|
2021-12-28 23:25:16 +01:00
|
|
|
case SpecialType.System_Double:
|
2022-02-27 21:57:46 +01:00
|
|
|
return MarshalType.Float64Array;
|
2021-12-28 23:25:16 +01:00
|
|
|
case SpecialType.System_String:
|
|
|
|
return MarshalType.StringArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elementType.SimpleDerivesFrom(typeCache.GodotObjectType))
|
|
|
|
return MarshalType.GodotObjectOrDerivedArray;
|
|
|
|
|
2022-09-16 23:51:17 +02:00
|
|
|
if (elementType.ContainingAssembly?.Name == "GodotSharp" &&
|
|
|
|
elementType.ContainingNamespace?.Name == "Godot")
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
2022-02-27 21:57:30 +01:00
|
|
|
switch (elementType)
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
2022-02-27 21:57:30 +01:00
|
|
|
case { Name: "Vector2" }:
|
|
|
|
return MarshalType.Vector2Array;
|
|
|
|
case { Name: "Vector3" }:
|
|
|
|
return MarshalType.Vector3Array;
|
|
|
|
case { Name: "Color" }:
|
2022-02-27 21:57:46 +01:00
|
|
|
return MarshalType.ColorArray;
|
2022-07-28 17:41:50 +02:00
|
|
|
case { Name: "StringName" }:
|
|
|
|
return MarshalType.SystemArrayOfStringName;
|
|
|
|
case { Name: "NodePath" }:
|
|
|
|
return MarshalType.SystemArrayOfNodePath;
|
2022-12-07 16:11:39 +01:00
|
|
|
case { Name: "Rid" }:
|
|
|
|
return MarshalType.SystemArrayOfRid;
|
2022-02-27 21:57:30 +01:00
|
|
|
}
|
2021-12-28 23:25:16 +01:00
|
|
|
}
|
2022-02-27 21:57:30 +01:00
|
|
|
|
|
|
|
return null;
|
2021-12-28 23:25:16 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (type.SimpleDerivesFrom(typeCache.GodotObjectType))
|
|
|
|
return MarshalType.GodotObjectOrDerived;
|
|
|
|
|
2022-09-22 12:41:38 +02:00
|
|
|
if (type.ContainingAssembly?.Name == "GodotSharp")
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
2022-09-22 12:41:38 +02:00
|
|
|
switch (type.ContainingNamespace?.Name)
|
2021-12-28 23:25:16 +01:00
|
|
|
{
|
|
|
|
case "Godot":
|
|
|
|
return type switch
|
|
|
|
{
|
|
|
|
{ Name: "StringName" } => MarshalType.StringName,
|
|
|
|
{ Name: "NodePath" } => MarshalType.NodePath,
|
|
|
|
_ => null
|
|
|
|
};
|
2022-02-27 21:57:30 +01:00
|
|
|
case "Collections"
|
2022-11-24 01:04:15 +01:00
|
|
|
when type.ContainingNamespace?.FullQualifiedNameOmitGlobal() == "Godot.Collections":
|
2021-12-28 23:25:16 +01:00
|
|
|
return type switch
|
|
|
|
{
|
2022-08-08 01:53:54 +02:00
|
|
|
{ Name: "Dictionary" } =>
|
|
|
|
type is INamedTypeSymbol { IsGenericType: false } ?
|
|
|
|
MarshalType.GodotDictionary :
|
|
|
|
MarshalType.GodotGenericDictionary,
|
|
|
|
{ Name: "Array" } =>
|
|
|
|
type is INamedTypeSymbol { IsGenericType: false } ?
|
|
|
|
MarshalType.GodotArray :
|
|
|
|
MarshalType.GodotGenericArray,
|
2021-12-28 23:25:16 +01:00
|
|
|
_ => null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool SimpleDerivesFrom(this ITypeSymbol? type, ITypeSymbol candidateBaseType)
|
|
|
|
{
|
|
|
|
while (type != null)
|
|
|
|
{
|
|
|
|
if (SymbolEqualityComparer.Default.Equals(type, candidateBaseType))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
type = type.BaseType;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-27 21:57:30 +01:00
|
|
|
|
|
|
|
public static ITypeSymbol? GetArrayElementType(ITypeSymbol typeSymbol)
|
|
|
|
{
|
|
|
|
if (typeSymbol.TypeKind == TypeKind.Array)
|
|
|
|
{
|
|
|
|
var arrayType = (IArrayTypeSymbol)typeSymbol;
|
|
|
|
return arrayType.ElementType;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeSymbol is INamedTypeSymbol { IsGenericType: true } genericType)
|
|
|
|
return genericType.TypeArguments.FirstOrDefault();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2022-02-27 21:57:46 +01:00
|
|
|
|
2022-07-28 17:41:49 +02:00
|
|
|
private static StringBuilder Append(this StringBuilder source, string a, string b)
|
|
|
|
=> source.Append(a).Append(b);
|
|
|
|
|
2022-02-27 21:57:46 +01:00
|
|
|
private static StringBuilder Append(this StringBuilder source, string a, string b, string c)
|
|
|
|
=> source.Append(a).Append(b).Append(c);
|
|
|
|
|
|
|
|
private static StringBuilder Append(this StringBuilder source, string a, string b,
|
|
|
|
string c, string d)
|
|
|
|
=> source.Append(a).Append(b).Append(c).Append(d);
|
|
|
|
|
|
|
|
private static StringBuilder Append(this StringBuilder source, string a, string b,
|
|
|
|
string c, string d, string e)
|
|
|
|
=> source.Append(a).Append(b).Append(c).Append(d).Append(e);
|
|
|
|
|
|
|
|
private static StringBuilder Append(this StringBuilder source, string a, string b,
|
|
|
|
string c, string d, string e, string f)
|
|
|
|
=> source.Append(a).Append(b).Append(c).Append(d).Append(e).Append(f);
|
|
|
|
|
|
|
|
private static StringBuilder Append(this StringBuilder source, string a, string b,
|
|
|
|
string c, string d, string e, string f, string g)
|
|
|
|
=> source.Append(a).Append(b).Append(c).Append(d).Append(e).Append(f).Append(g);
|
|
|
|
|
2022-08-08 01:53:54 +02:00
|
|
|
private static StringBuilder Append(this StringBuilder source, string a, string b,
|
|
|
|
string c, string d, string e, string f, string g, string h)
|
|
|
|
=> source.Append(a).Append(b).Append(c).Append(d).Append(e).Append(f).Append(g).Append(h);
|
|
|
|
|
2022-02-27 21:57:46 +01:00
|
|
|
private const string VariantUtils = "global::Godot.NativeInterop.VariantUtils";
|
|
|
|
|
2022-07-28 17:41:50 +02:00
|
|
|
public static StringBuilder AppendNativeVariantToManagedExpr(this StringBuilder source,
|
2022-02-27 21:57:46 +01:00
|
|
|
string inputExpr, ITypeSymbol typeSymbol, MarshalType marshalType)
|
|
|
|
{
|
|
|
|
return marshalType switch
|
|
|
|
{
|
2022-12-11 22:14:25 +01:00
|
|
|
// We need a special case for GodotObjectOrDerived[], because it's not supported by VariantUtils.ConvertTo<T>
|
|
|
|
MarshalType.GodotObjectOrDerivedArray =>
|
|
|
|
source.Append(VariantUtils, ".ConvertToSystemArrayOfGodotObject<",
|
|
|
|
((IArrayTypeSymbol)typeSymbol).ElementType.FullQualifiedNameIncludeGlobal(), ">(",
|
|
|
|
inputExpr, ")"),
|
|
|
|
// We need a special case for generic Godot collections and GodotObjectOrDerived[], because VariantUtils.ConvertTo<T> is slower
|
2022-08-08 01:53:54 +02:00
|
|
|
MarshalType.GodotGenericDictionary =>
|
2022-12-23 19:28:34 +01:00
|
|
|
source.Append(VariantUtils, ".ConvertToDictionary<",
|
2022-11-24 01:04:15 +01:00
|
|
|
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ", ",
|
2022-12-01 01:45:11 +01:00
|
|
|
((INamedTypeSymbol)typeSymbol).TypeArguments[1].FullQualifiedNameIncludeGlobal(), ">(",
|
|
|
|
inputExpr, ")"),
|
2022-08-08 01:53:54 +02:00
|
|
|
MarshalType.GodotGenericArray =>
|
2022-12-23 19:28:34 +01:00
|
|
|
source.Append(VariantUtils, ".ConvertToArray<",
|
2022-12-01 01:45:11 +01:00
|
|
|
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ">(",
|
|
|
|
inputExpr, ")"),
|
|
|
|
_ => source.Append(VariantUtils, ".ConvertTo<",
|
|
|
|
typeSymbol.FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"),
|
2022-02-27 21:57:46 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-01 01:45:11 +01:00
|
|
|
public static StringBuilder AppendManagedToNativeVariantExpr(this StringBuilder source,
|
|
|
|
string inputExpr, ITypeSymbol typeSymbol, MarshalType marshalType)
|
2022-02-27 21:57:46 +01:00
|
|
|
{
|
|
|
|
return marshalType switch
|
|
|
|
{
|
2022-12-11 22:14:25 +01:00
|
|
|
// We need a special case for GodotObjectOrDerived[], because it's not supported by VariantUtils.CreateFrom<T>
|
|
|
|
MarshalType.GodotObjectOrDerivedArray =>
|
|
|
|
source.Append(VariantUtils, ".CreateFromSystemArrayOfGodotObject(", inputExpr, ")"),
|
|
|
|
// We need a special case for generic Godot collections and GodotObjectOrDerived[], because VariantUtils.CreateFrom<T> is slower
|
2022-08-08 01:53:54 +02:00
|
|
|
MarshalType.GodotGenericDictionary =>
|
|
|
|
source.Append(VariantUtils, ".CreateFromDictionary(", inputExpr, ")"),
|
|
|
|
MarshalType.GodotGenericArray =>
|
|
|
|
source.Append(VariantUtils, ".CreateFromArray(", inputExpr, ")"),
|
2022-12-01 01:45:11 +01:00
|
|
|
_ => source.Append(VariantUtils, ".CreateFrom<",
|
|
|
|
typeSymbol.FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"),
|
2022-02-27 21:57:46 +01:00
|
|
|
};
|
|
|
|
}
|
2022-07-28 17:41:50 +02:00
|
|
|
|
|
|
|
public static StringBuilder AppendVariantToManagedExpr(this StringBuilder source,
|
|
|
|
string inputExpr, ITypeSymbol typeSymbol, MarshalType marshalType)
|
|
|
|
{
|
|
|
|
return marshalType switch
|
|
|
|
{
|
2022-12-11 22:14:25 +01:00
|
|
|
// We need a special case for GodotObjectOrDerived[], because it's not supported by Variant.As<T>
|
|
|
|
MarshalType.GodotObjectOrDerivedArray =>
|
|
|
|
source.Append(inputExpr, ".AsGodotObjectArray<",
|
|
|
|
((IArrayTypeSymbol)typeSymbol).ElementType.FullQualifiedNameIncludeGlobal(), ">()"),
|
|
|
|
// We need a special case for generic Godot collections and GodotObjectOrDerived[], because Variant.As<T> is slower
|
2022-12-01 01:45:11 +01:00
|
|
|
MarshalType.GodotGenericDictionary =>
|
|
|
|
source.Append(inputExpr, ".AsGodotDictionary<",
|
|
|
|
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ", ",
|
|
|
|
((INamedTypeSymbol)typeSymbol).TypeArguments[1].FullQualifiedNameIncludeGlobal(), ">()"),
|
|
|
|
MarshalType.GodotGenericArray =>
|
|
|
|
source.Append(inputExpr, ".AsGodotArray<",
|
|
|
|
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ">()"),
|
|
|
|
_ => source.Append(inputExpr, ".As<",
|
|
|
|
typeSymbol.FullQualifiedNameIncludeGlobal(), ">()")
|
2022-07-28 17:41:50 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public static StringBuilder AppendManagedToVariantExpr(this StringBuilder source,
|
2022-12-01 01:45:11 +01:00
|
|
|
string inputExpr, ITypeSymbol typeSymbol, MarshalType marshalType)
|
2022-07-28 17:41:50 +02:00
|
|
|
{
|
2022-12-01 01:45:11 +01:00
|
|
|
return marshalType switch
|
2022-07-28 17:41:50 +02:00
|
|
|
{
|
2022-12-11 22:14:25 +01:00
|
|
|
// We need a special case for GodotObjectOrDerived[], because it's not supported by Variant.From<T>
|
|
|
|
MarshalType.GodotObjectOrDerivedArray =>
|
|
|
|
source.Append("global::Godot.Variant.CreateFrom(", inputExpr, ")"),
|
|
|
|
// We need a special case for generic Godot collections, because Variant.From<T> is slower
|
2022-12-01 01:45:11 +01:00
|
|
|
MarshalType.GodotGenericDictionary or MarshalType.GodotGenericArray =>
|
|
|
|
source.Append("global::Godot.Variant.CreateFrom(", inputExpr, ")"),
|
|
|
|
_ => source.Append("global::Godot.Variant.From<",
|
|
|
|
typeSymbol.FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")")
|
|
|
|
};
|
2022-07-28 17:41:50 +02:00
|
|
|
}
|
2021-12-28 23:25:16 +01:00
|
|
|
}
|
|
|
|
}
|