Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TypeNameBuilder.cs
Go to the documentation of this file.
2using System.Text;
3
5
6internal sealed class TypeNameBuilder
7{
8 internal enum Format
9 {
13 }
14
16
17 private int _instNesting;
18
19 private bool _firstInstArg;
20
21 private bool _nestedName;
22
23 private bool _hasAssemblySpec;
24
26
27 private List<int> _stack = new List<int>();
28
29 private int _stackIdx;
30
32 {
33 }
34
35 private void OpenGenericArguments()
36 {
38 _firstInstArg = true;
40 {
41 Append('<');
42 }
43 else
44 {
45 Append('[');
46 }
47 }
48
49 private void CloseGenericArguments()
50 {
52 if (_firstInstArg)
53 {
54 _str.Remove(_str.Length - 1, 1);
55 }
57 {
58 Append('>');
59 }
60 else
61 {
62 Append(']');
63 }
64 }
65
66 private void OpenGenericArgument()
67 {
68 _nestedName = false;
69 if (!_firstInstArg)
70 {
71 Append(',');
72 }
73 _firstInstArg = false;
75 {
76 Append('<');
77 }
78 else
79 {
80 Append('[');
81 }
83 }
84
85 private void CloseGenericArgument()
86 {
88 {
90 {
91 Append('>');
92 }
93 else
94 {
95 Append(']');
96 }
97 }
99 }
100
101 private void AddName(string name)
102 {
103 if (_nestedName)
104 {
105 Append('+');
106 }
107 _nestedName = true;
108 EscapeName(name);
109 }
110
111 private void AddArray(int rank)
112 {
113 if (rank == 1)
114 {
115 Append("[*]");
116 return;
117 }
118 if (rank > 64)
119 {
120 _str.Append('[').Append(rank).Append(']');
121 return;
122 }
123 Append('[');
124 for (int i = 1; i < rank; i++)
125 {
126 Append(',');
127 }
128 Append(']');
129 }
130
131 private void AddAssemblySpec(string assemblySpec)
132 {
133 if (assemblySpec != null && !assemblySpec.Equals(""))
134 {
135 Append(", ");
136 if (_instNesting > 0)
137 {
139 }
140 else
141 {
143 }
144 _hasAssemblySpec = true;
145 }
146 }
147
148 public override string ToString()
149 {
150 return _str.ToString();
151 }
152
153 private static bool ContainsReservedChar(string name)
154 {
155 foreach (char c in name)
156 {
157 if (c == '\0')
158 {
159 break;
160 }
162 {
163 return true;
164 }
165 }
166 return false;
167 }
168
169 private static bool IsTypeNameReservedChar(char ch)
170 {
171 switch (ch)
172 {
173 case '&':
174 case '*':
175 case '+':
176 case ',':
177 case '[':
178 case '\\':
179 case ']':
180 return true;
181 default:
182 return false;
183 }
184 }
185
186 private void EscapeName(string name)
187 {
188 if (ContainsReservedChar(name))
189 {
190 foreach (char c in name)
191 {
192 if (c != 0)
193 {
195 {
196 _str.Append('\\');
197 }
198 _str.Append(c);
199 continue;
200 }
201 break;
202 }
203 }
204 else
205 {
206 Append(name);
207 }
208 }
209
210 private void EscapeAssemblyName(string name)
211 {
212 Append(name);
213 }
214
215 private void EscapeEmbeddedAssemblyName(string name)
216 {
217 if (name.Contains(']'))
218 {
219 foreach (char c in name)
220 {
221 if (c == ']')
222 {
223 Append('\\');
224 }
225 Append(c);
226 }
227 }
228 else
229 {
230 Append(name);
231 }
232 }
233
235 {
237 _stackIdx++;
238 }
239
241 {
242 int num = _stack[--_stackIdx];
244 if (!_hasAssemblySpec)
245 {
246 _str.Remove(num - 1, 1);
247 }
248 _hasAssemblySpec = false;
249 }
250
251 private void Append(string pStr)
252 {
253 foreach (char c in pStr)
254 {
255 if (c != 0)
256 {
257 _str.Append(c);
258 continue;
259 }
260 break;
261 }
262 }
263
264 private void Append(char c)
265 {
266 _str.Append(c);
267 }
268
269 internal static string ToString(Type type, Format format)
270 {
271 if ((format == Format.FullName || format == Format.AssemblyQualifiedName) && !type.IsGenericTypeDefinition && type.ContainsGenericParameters)
272 {
273 return null;
274 }
276 typeNameBuilder.AddAssemblyQualifiedName(type, format);
277 return typeNameBuilder.ToString();
278 }
279
281 {
282 if (type.HasElementType)
283 {
284 AddElementType(type.GetElementType());
285 if (type.IsPointer)
286 {
287 Append('*');
288 }
289 else if (type.IsByRef)
290 {
291 Append('&');
292 }
293 else if (type.IsSZArray)
294 {
295 Append("[]");
296 }
297 else if (type.IsArray)
298 {
299 AddArray(type.GetArrayRank());
300 }
301 }
302 }
303
305 {
306 Type type2 = type;
307 while (type2.HasElementType)
308 {
309 type2 = type2.GetElementType();
310 }
311 List<Type> list = new List<Type>();
312 Type type3 = type2;
313 while (type3 != null)
314 {
315 list.Add(type3);
316 type3 = (type3.IsGenericParameter ? null : type3.DeclaringType);
317 }
318 for (int num = list.Count - 1; num >= 0; num--)
319 {
320 Type type4 = list[num];
321 string text = type4.Name;
322 if (num == list.Count - 1 && type4.Namespace != null && type4.Namespace.Length != 0)
323 {
324 text = type4.Namespace + "." + text;
325 }
326 AddName(text);
327 }
328 if (type2.IsGenericType && (!type2.IsGenericTypeDefinition || format == Format.ToString))
329 {
330 Type[] genericArguments = type2.GetGenericArguments();
332 for (int i = 0; i < genericArguments.Length; i++)
333 {
334 Format format2 = ((format == Format.FullName) ? Format.AssemblyQualifiedName : format);
338 }
340 }
342 if (format == Format.AssemblyQualifiedName)
343 {
344 AddAssemblySpec(type.Module.Assembly.FullName);
345 }
346 }
347}
void Add(TKey key, TValue value)
void RemoveAt(int index)
Definition List.cs:824
void AddAssemblyQualifiedName(Type type, Format format)
static bool ContainsReservedChar(string name)
void AddAssemblySpec(string assemblySpec)
static string ToString(Type type, Format format)
StringBuilder Remove(int startIndex, int length)
override string ToString()
StringBuilder Append(char value, int repeatCount)