Skip to content

Commit

Permalink
Merge pull request godotengine#65905 from raulsntos/dotnet/marshaling…
Browse files Browse the repository at this point in the history
…-collections

C#: Fix marshaling generic Godot collections
  • Loading branch information
neikeq authored Nov 30, 2022
2 parents d75a6b5 + 7535803 commit 5826e96
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
10 changes: 9 additions & 1 deletion modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,11 @@ internal unsafe void GetVariantBorrowElementAtUnchecked(int index, out godot_var
}
}

internal interface IGenericGodotArray
{
public Array UnderlyingArray { get; }
}

/// <summary>
/// Typed wrapper around Godot's Array class, an array of Variant
/// typed elements allocated in the engine in C++. Useful when
Expand All @@ -487,7 +492,8 @@ public sealed class Array<[MustBeVariant] T> :
IList<T>,
IReadOnlyList<T>,
ICollection<T>,
IEnumerable<T>
IEnumerable<T>,
IGenericGodotArray
{
private static godot_variant ToVariantFunc(in Array<T> godotArray) =>
VariantUtils.CreateFromArray(godotArray);
Expand All @@ -503,6 +509,8 @@ static unsafe Array()

private readonly Array _underlyingArray;

Array IGenericGodotArray.UnderlyingArray => _underlyingArray;

internal ref godot_array.movable NativeValue
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
10 changes: 9 additions & 1 deletion modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ public override string ToString()
}
}

internal interface IGenericGodotDictionary
{
public Dictionary UnderlyingDictionary { get; }
}

/// <summary>
/// Typed wrapper around Godot's Dictionary class, a dictionary of Variant
/// typed elements allocated in the engine in C++. Useful when
Expand All @@ -354,7 +359,8 @@ public override string ToString()
/// <typeparam name="TValue">The type of the dictionary's values.</typeparam>
public class Dictionary<[MustBeVariant] TKey, [MustBeVariant] TValue> :
IDictionary<TKey, TValue>,
IReadOnlyDictionary<TKey, TValue>
IReadOnlyDictionary<TKey, TValue>,
IGenericGodotDictionary
{
private static godot_variant ToVariantFunc(in Dictionary<TKey, TValue> godotDictionary) =>
VariantUtils.CreateFromDictionary(godotDictionary);
Expand All @@ -370,6 +376,8 @@ static unsafe Dictionary()

private readonly Dictionary _underlyingDict;

Dictionary IGenericGodotDictionary.UnderlyingDictionary => _underlyingDict;

internal ref godot_dictionary.movable NativeValue
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ public static godot_variant ConvertManagedObjectToVariant(object? p_obj)
return VariantUtils.CreateFromDictionary(godotDictionary);
case Collections.Array godotArray:
return VariantUtils.CreateFromArray(godotArray);
case Collections.IGenericGodotDictionary godotDictionary:
return VariantUtils.CreateFromDictionary(godotDictionary.UnderlyingDictionary);
case Collections.IGenericGodotArray godotArray:
return VariantUtils.CreateFromArray(godotArray.UnderlyingArray);
case Variant variant:
return NativeFuncs.godotsharp_variant_new_copy((godot_variant)variant.NativeVar);
}
Expand Down

0 comments on commit 5826e96

Please sign in to comment.