Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SourceInfo.cs
Go to the documentation of this file.
6
8
9internal sealed class SourceInfo
10{
11 private static readonly Regex s_regex = new Regex("([(][(](?<t>[^)]+)[)])?(?<a>[^[]+)[[](?<ia>.+)[]][)]?");
12
13 private static readonly Regex s_regex2 = new Regex("[(][(](?<cast>[^)]+)[)](?<arg>[^)]+)[)]");
14
15 private static readonly Lazy<MethodInfo> s_iListGetItemMethod = new Lazy<MethodInfo>(() => typeof(IList).GetMethod("get_Item", new Type[1] { typeof(int) }));
16
17 public string Source;
18
19 public readonly string Arg;
20
21 public readonly MemberInfo MemberInfo;
22
24 public readonly Type Type;
25
26 public readonly CodeGenerator ILG;
27
29 {
30 Source = source;
31 Arg = arg ?? source;
33 Type = type;
34 ILG = ilg;
35 }
36
38 {
39 return new SourceInfo("((" + td.CSharpName + ")" + Source + ")", Arg, MemberInfo, td.Type, ILG);
40 }
41
42 [RequiresUnreferencedCode("calls InternalLoad")]
44 {
46 }
47
48 [RequiresUnreferencedCode("calls InternalLoad")]
49 public void Load(Type elementType)
50 {
52 }
53
54 [RequiresUnreferencedCode("calls LoadMemberAddress")]
55 private void InternalLoad(Type elementType, bool asAddress = false)
56 {
57 Match match = s_regex.Match(Arg);
58 if (match.Success)
59 {
60 object variable = ILG.GetVariable(match.Groups["a"].Value);
62 object variable2 = ILG.GetVariable(match.Groups["ia"].Value);
63 if (variableType.IsArray)
64 {
69 {
72 return;
73 }
74 if (elementType2.IsValueType)
75 {
77 if (!asAddress)
78 {
80 }
81 }
82 else
83 {
85 }
86 if (elementType != null)
87 {
89 }
90 return;
91 }
94 MethodInfo methodInfo = variableType.GetMethod("get_Item", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, new Type[1] { typeof(int) });
95 if (methodInfo == null && typeof(IList).IsAssignableFrom(variableType))
96 {
98 }
100 Type returnType = methodInfo.ReturnType;
102 {
107 }
108 else
109 {
110 if (elementType != null && !returnType.IsAssignableFrom(elementType) && !elementType.IsAssignableFrom(returnType))
111 {
112 throw new CodeGeneratorConversionException(returnType, elementType, asAddress, "IsNotAssignableFrom");
113 }
115 }
116 return;
117 }
118 if (Source == "null")
119 {
120 ILG.Load(null);
121 return;
122 }
123 Type type;
124 if (Arg.StartsWith("o.@", StringComparison.Ordinal) || MemberInfo != null)
125 {
126 object variable3 = ILG.GetVariable(Arg.StartsWith("o.@", StringComparison.Ordinal) ? "o" : Arg);
128 if (type.IsValueType)
129 {
131 }
132 else
133 {
135 }
136 }
137 else
138 {
139 object variable3 = ILG.GetVariable(Arg);
141 if (CodeGenerator.IsNullableGenericType(type) && type.GetGenericArguments()[0] == elementType)
142 {
145 }
146 else if (asAddress)
147 {
149 }
150 else
151 {
153 }
154 }
155 if (MemberInfo != null)
156 {
157 Type type2 = ((MemberInfo is FieldInfo) ? ((FieldInfo)MemberInfo).FieldType : ((PropertyInfo)MemberInfo).PropertyType);
159 {
162 }
163 else
164 {
167 }
168 return;
169 }
170 match = s_regex2.Match(Source);
171 if (match.Success)
172 {
173 if (asAddress)
174 {
176 }
177 else
178 {
180 }
181 type = Type;
182 }
184 }
185
187 {
188 if (targetType != null)
189 {
190 if (asAddress)
191 {
193 }
194 else
195 {
197 }
198 }
199 }
200
202 {
204 {
205 MethodInfo method = nullableType.GetMethod("get_Value", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, Type.EmptyTypes);
206 ILG.Call(method);
207 if (targetType != null)
208 {
209 ILG.ConvertValue(method.ReturnType, targetType);
210 }
211 }
212 }
213
214 public static implicit operator string(SourceInfo source)
215 {
216 return source.Source;
217 }
218
219 public static bool operator !=(SourceInfo a, SourceInfo b)
220 {
221 if ((object)a != null)
222 {
223 return !a.Equals(b);
224 }
225 return (object)b != null;
226 }
227
228 public static bool operator ==(SourceInfo a, SourceInfo b)
229 {
230 return a?.Equals(b) ?? ((object)b == null);
231 }
232
233 public override bool Equals([NotNullWhen(true)] object obj)
234 {
235 if (obj == null)
236 {
237 return Source == null;
238 }
240 if (sourceInfo != null)
241 {
242 return Source == sourceInfo.Source;
243 }
244 return false;
245 }
246
247 public override int GetHashCode()
248 {
249 if (Source != null)
250 {
251 return Source.GetHashCode();
252 }
253 return 0;
254 }
255}
Type? GetElementType()
static readonly Type[] EmptyTypes
Definition Type.cs:19
void Ldelem(Type arrayElementType)
static bool IsNullableGenericType(Type type)
void ConvertAddress(Type source, Type target)
void ConvertValue(Type source, Type target)
Type LoadMemberAddress(MemberInfo memberInfo)
void Ldelema(Type arrayElementType)
Type LoadMember(object obj, MemberInfo memberInfo)
void Call(MethodInfo methodInfo)
void Stloc(Type type, string name)
void Ldloca(LocalBuilder localBuilder)
SourceInfo(string source, string arg, MemberInfo memberInfo, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors|DynamicallyAccessedMemberTypes.PublicMethods|DynamicallyAccessedMemberTypes.NonPublicMethods)] Type type, CodeGenerator ilg)
Definition SourceInfo.cs:28
void ConvertNullableValue([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods|DynamicallyAccessedMemberTypes.NonPublicMethods)] Type nullableType, Type targetType)
static readonly Regex s_regex2
Definition SourceInfo.cs:13
void LoadAddress(Type elementType)
Definition SourceInfo.cs:43
static bool operator==(SourceInfo a, SourceInfo b)
void InternalLoad(Type elementType, bool asAddress=false)
Definition SourceInfo.cs:55
void Convert(Type sourceType, Type targetType, bool asAddress)
void Load(Type elementType)
Definition SourceInfo.cs:49
static readonly Regex s_regex
Definition SourceInfo.cs:11
static bool operator!=(SourceInfo a, SourceInfo b)
override bool Equals([NotNullWhen(true)] object obj)
static readonly Lazy< MethodInfo > s_iListGetItemMethod
Definition SourceInfo.cs:15
readonly CodeGenerator ILG
Definition SourceInfo.cs:26
SourceInfo CastTo(TypeDesc td)
Definition SourceInfo.cs:37