2017-11-05 10:37:59 +00:00
|
|
|
using System;
|
2019-03-05 17:52:19 +00:00
|
|
|
using System.Collections;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
namespace Godot
|
|
|
|
{
|
2019-03-05 17:52:19 +00:00
|
|
|
using Array = Godot.Collections.Array;
|
|
|
|
using Dictionary = Godot.Collections.Dictionary;
|
|
|
|
|
2018-07-18 21:07:57 +00:00
|
|
|
static class MarshalUtils
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2019-03-05 17:52:19 +00:00
|
|
|
static bool TypeIsGenericArray(Type type)
|
|
|
|
{
|
|
|
|
return type.GetGenericTypeDefinition() == typeof(Godot.Collections.Array<>);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool TypeIsGenericDictionary(Type type)
|
|
|
|
{
|
|
|
|
return type.GetGenericTypeDefinition() == typeof(Godot.Collections.Dictionary<,>);
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:39:50 +00:00
|
|
|
static void ArrayGetElementType(Type type, out Type elementType)
|
|
|
|
{
|
|
|
|
elementType = type.GetGenericArguments()[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DictionaryGetKeyValueTypes(Type type, out Type keyType, out Type valueType)
|
|
|
|
{
|
|
|
|
var genericArgs = type.GetGenericArguments();
|
|
|
|
|
|
|
|
keyType = genericArgs[0];
|
|
|
|
valueType = genericArgs[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Add support for IEnumerable<T> and IDictionary<TKey, TValue>
|
2019-03-05 17:52:19 +00:00
|
|
|
// TODO: EnumerableToArray and IDictionaryToDictionary can be optimized
|
|
|
|
|
|
|
|
internal static void EnumerableToArray(IEnumerable enumerable, IntPtr godotArrayPtr)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2019-03-05 17:52:19 +00:00
|
|
|
if (enumerable is ICollection collection)
|
|
|
|
{
|
|
|
|
int count = collection.Count;
|
|
|
|
|
|
|
|
object[] tempArray = new object[count];
|
|
|
|
collection.CopyTo(tempArray, 0);
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
Array.godot_icall_Array_Add(godotArrayPtr, tempArray[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach (object element in enumerable)
|
|
|
|
{
|
|
|
|
Array.godot_icall_Array_Add(godotArrayPtr, element);
|
|
|
|
}
|
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 17:52:19 +00:00
|
|
|
internal static void IDictionaryToDictionary(IDictionary dictionary, IntPtr godotDictionaryPtr)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2019-03-05 17:52:19 +00:00
|
|
|
foreach (DictionaryEntry entry in dictionary)
|
|
|
|
{
|
|
|
|
Dictionary.godot_icall_Dictionary_Add(godotDictionaryPtr, entry.Key, entry.Value);
|
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|