forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeAdapter.cs
More file actions
170 lines (151 loc) · 6.1 KB
/
NodeAdapter.cs
File metadata and controls
170 lines (151 loc) · 6.1 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace UnityEditor.Experimental.GraphView
{
// types of port to adapt
public class PortSource<T>
{
}
// attribute to declare and adapter
public class TypeAdapter : Attribute
{
}
// TODO: This is a straight port from Canvas2D. I don't think that having to check for types in the assembly using reflection is the way we want to go.
public class NodeAdapter
{
private static List<MethodInfo> s_TypeAdapters;
private static Dictionary<int, MethodInfo> s_NodeAdapterDictionary;
public bool CanAdapt(object a, object b)
{
if (a == b)
return false; // self connections are not permitted
if (a == null || b == null)
return false;
MethodInfo mi = GetAdapter(a, b);
if (mi == null)
{
Debug.Log("adapter node not found for: " + a.GetType() + " -> " + b.GetType());
}
return mi != null;
}
public bool Connect(object a, object b)
{
MethodInfo mi = GetAdapter(a, b);
if (mi == null)
{
Debug.LogError("Attempt to connect 2 unadaptable types: " + a.GetType() + " -> " + b.GetType());
return false;
}
object retVal = mi.Invoke(this, new[] { this, a, b });
return (bool)retVal;
}
IEnumerable<MethodInfo> GetExtensionMethods(Assembly assembly, Type extendedType)
{
return assembly.GetTypes()
.Where(t => t.IsSealed && !t.IsGenericType && !t.IsNested)
.SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
.Where(m => m.IsDefined(typeof(ExtensionAttribute), false) && m.GetParameters()[0].ParameterType == extendedType);
}
public MethodInfo GetAdapter(object a, object b)
{
if (a == null || b == null)
return null;
if (s_NodeAdapterDictionary == null)
{
s_NodeAdapterDictionary = new Dictionary<int, MethodInfo>();
// add extension methods
AppDomain currentDomain = AppDomain.CurrentDomain;
foreach (Assembly assembly in currentDomain.GetAssemblies())
{
IEnumerable<MethodInfo> methods;
try
{
methods = GetExtensionMethods(assembly, typeof(NodeAdapter));
}
// Invalid DLLs might raise this exception, simply ignore it
catch (ReflectionTypeLoadException)
{
continue;
}
foreach (MethodInfo method in methods)
{
ParameterInfo[] methodParams = method.GetParameters();
if (methodParams.Length == 3)
{
string pa = methodParams[1].ParameterType + methodParams[2].ParameterType.ToString();
int hash = pa.GetHashCode();
if (s_NodeAdapterDictionary.ContainsKey(hash))
{
Debug.Log("NodeAdapter: multiple extensions have the same signature:\n" +
"1: " + method + "\n" +
"2: " + s_NodeAdapterDictionary[hash]);
}
else
{
s_NodeAdapterDictionary.Add(hash, method);
}
}
}
}
}
string s = a.GetType().ToString() + b.GetType();
MethodInfo methodInfo;
return s_NodeAdapterDictionary.TryGetValue(s.GetHashCode(), out methodInfo) ? methodInfo : null;
}
public MethodInfo GetTypeAdapter(Type from, Type to)
{
if (s_TypeAdapters == null)
{
s_TypeAdapters = new List<MethodInfo>();
AppDomain currentDomain = AppDomain.CurrentDomain;
foreach (Assembly assembly in currentDomain.GetAssemblies())
{
try
{
foreach (Type temptype in assembly.GetTypes())
{
MethodInfo[] methodInfos = temptype.GetMethods(BindingFlags.Public | BindingFlags.Static);
foreach (MethodInfo i in methodInfos)
{
object[] allAttrs = i.GetCustomAttributes(typeof(TypeAdapter), false);
if (allAttrs.Any())
{
s_TypeAdapters.Add(i);
}
}
}
}
// Invalid DLLs might raise this exception, simply ignore it
catch (ReflectionTypeLoadException)
{
continue;
}
catch (Exception ex)
{
Debug.Log(ex);
}
}
}
foreach (MethodInfo i in s_TypeAdapters)
{
if (i.ReturnType == to)
{
ParameterInfo[] allParams = i.GetParameters();
if (allParams.Length == 1)
{
if (allParams[0].ParameterType == from)
return i;
}
}
}
return null;
}
}
}