-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeFactory.cs
More file actions
189 lines (157 loc) · 5.3 KB
/
Copy pathTypeFactory.cs
File metadata and controls
189 lines (157 loc) · 5.3 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System.Text;
namespace NodeDev.Core.Types;
public class TypeFactory
{
public List<string> IncludedNamespaces =
[
"System",
"System.Collections.Generic",
"System.Linq",
"System.Text",
"System.Threading",
"System.Threading.Tasks",
"System.Diagnostics",
];
private readonly Dictionary<string, List<string>> TypeCorrespondances = new()
{
["System.Int32"] = ["int"],
["System.Int64"] = ["long"],
["System.Single"] = ["float"],
["System.Double"] = ["double"],
["System.Boolean"] = ["bool"],
["System.String"] = ["string"],
["System.Void"] = ["void"],
};
private readonly ExecType ExecType_;
private readonly Dictionary<string, RealType> RealTypesCache = new(5_000);
public readonly Project Project;
public TypeFactory(Project project)
{
ExecType_ = new();
Project = project;
}
public ExecType ExecType => ExecType_;
public RealType Void => Get(typeof(void), null);
public RealType Get<T>() => Get(typeof(T), null);
public RealType Get(Type type, TypeBase[]? generics)
{
if (generics == null)
{
if (type.IsGenericType)
{
if (!type.IsConstructedGenericType) // this is something list List<T> instead of List<int>
throw new Exception("Unable to create real type with undefined generics. To do so you must manually specify the generics through the 'generics' parameter in the RealType constructor");
generics = type.GetGenericArguments().Select(x => Get(x, null)).ToArray();
type = type.GetGenericTypeDefinition();
}
}
else if (type.IsGenericType)
type = type.GetGenericTypeDefinition(); // make sure we always store the generic type, so List<> instead of List<int>
var sb = new StringBuilder();
sb.Append(type.GetHashCode()); // start with the type hash code
if (generics?.Length > 0)
{
foreach (var generic in generics)
sb.Append('-').Append(generic.GetHashCode()); // add the hash code of each generic
}
var key = sb.ToString();
if (!RealTypesCache.TryGetValue(key, out var realTypeWithGenerics))
{
realTypeWithGenerics = new RealType(this, type, generics);
RealTypesCache[key] = realTypeWithGenerics;
}
return realTypeWithGenerics;
}
public static Type? GetTypeByFullName(string name)
{
return AppDomain.CurrentDomain.GetAssemblies().Select(x => x.GetType(name)).FirstOrDefault(x => x != null);
}
private static Type? GetTypeFromAllAssemblies(string name)
{
if (string.IsNullOrWhiteSpace(name))
return null;
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
return assemblies.Select(ass => ass.GetType(name, false, true)).FirstOrDefault(x => x != null);
}
#region CreateBaseFromUserInput
public string? CreateBaseFromUserInput(string typeName, out TypeBase? type)
{
int nbArray = 0;
while (typeName.EndsWith("[]"))
{
++nbArray;
typeName = typeName[..^2];
}
typeName = typeName.Replace(" ", "");
if (typeName.Count(c => c == '<') != typeName.Count(c => c == '>'))
{
type = null;
return "Bracket opened but never closed";
}
if (!typeName.Any(c => c == '<'))
{
// easy, just find the type, it's either a full name or a name we can find in the included namespaces
var correspondance = TypeCorrespondances.FirstOrDefault(x => x.Value.Contains(typeName));
if (correspondance.Key != null)
typeName = correspondance.Key;
var currentRealType = GetTypeFromAllAssemblies(typeName) ?? IncludedNamespaces.Select(ns => GetTypeFromAllAssemblies($"{ns}.{typeName}")).FirstOrDefault(t => t != null);
if (currentRealType?.IsGenericType == true)
{
type = null;
return "Not all generics are provided for type:" + typeName;
}
else if (currentRealType != null)
{
type = Get(currentRealType, null);
for (int i = 0; i < nbArray; ++i)
type = type.ArrayType;
return null;
}
else if (currentRealType == null)
{
var nodeClass = Project.Classes.FirstOrDefault(x => x.Name.Equals(typeName, StringComparison.InvariantCultureIgnoreCase));
if (nodeClass != null)
{
type = nodeClass.ClassTypeBase;
for (int i = 0; i < nbArray; ++i)
type = type.ArrayType;
return null;
}
}
type = null;
return $"Type {typeName} not found";
}
// we have a generic type, we need to find the base type and the generic arguments
var name = typeName[..typeName.IndexOf('<')];
var genericArgs = typeName[(typeName.IndexOf('<') + 1)..^1].Split(',').Select(s => s.Trim()).ToArray();
// find the base type
var correspondance2 = TypeCorrespondances.FirstOrDefault(x => x.Value.Contains(name));
if (correspondance2.Key != null)
name = correspondance2.Key;
var baseType = GetTypeFromAllAssemblies(name + "`" + genericArgs.Length) ?? IncludedNamespaces.Select(ns => GetTypeFromAllAssemblies($"{ns}.{name}`{genericArgs.Length}")).FirstOrDefault(t => t != null);
if (baseType == null)
{
type = null;
return $"Type {name} not found";
}
// find the generic arguments
var genericArgsTypes = new TypeBase[genericArgs.Length];
for (int i = 0; i < genericArgs.Length; i++)
{
var error = CreateBaseFromUserInput(genericArgs[i], out var genericArgType);
if (error != null)
{
type = null;
return error;
}
genericArgsTypes[i] = genericArgType!;
}
// create the generic type
type = Get(baseType, genericArgsTypes);
for (int i = 0; i < nbArray; ++i)
type = type.ArrayType;
return null;
}
#endregion
}