summaryrefslogtreecommitdiff
path: root/modules/mono/glue/Managed/Files/MarshalUtils.cs
blob: 7e72b0edb526ccd2e73c9f43f1bbac22e6c2541d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections;

namespace Godot
{
    using Array = Godot.Collections.Array;
    using Dictionary = Godot.Collections.Dictionary;

    static class MarshalUtils
    {
        static bool TypeIsGenericArray(Type type)
        {
            return type.GetGenericTypeDefinition() == typeof(Godot.Collections.Array<>);
        }

        static bool TypeIsGenericDictionary(Type type)
        {
            return type.GetGenericTypeDefinition() == typeof(Godot.Collections.Dictionary<,>);
        }

        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>
        // TODO: EnumerableToArray and IDictionaryToDictionary can be optimized

        internal static void EnumerableToArray(IEnumerable enumerable, IntPtr godotArrayPtr)
        {
            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);
                }
            }
        }

        internal static void IDictionaryToDictionary(IDictionary dictionary, IntPtr godotDictionaryPtr)
        {
            foreach (DictionaryEntry entry in dictionary)
            {
                Dictionary.godot_icall_Dictionary_Add(godotDictionaryPtr, entry.Key, entry.Value);
            }
        }
    }
}