Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TempAssembly.cs
Go to the documentation of this file.
5using System.IO;
9using System.Text;
11
13
14internal sealed class TempAssembly
15{
16 internal sealed class TempMethod
17 {
19
21
22 internal string name;
23
24 internal string ns;
25
26 internal bool isSoap;
27
28 internal string methodKey;
29 }
30
31 internal sealed class TempMethodDictionary : Dictionary<string, TempMethod>
32 {
33 }
34
35 private readonly Assembly _assembly;
36
38
40
42
44
45 internal static bool UseLegacySerializerGeneration => false;
46
48 {
49 [RequiresUnreferencedCode("calls GetTypeFromAssembly")]
50 get
51 {
52 if (_contract == null)
53 {
55 }
56 return _contract;
57 }
58 }
59
66
67 [RequiresUnreferencedCode("calls GenerateRefEmitAssembly")]
68 internal TempAssembly(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace, string location)
69 {
70 bool flag = false;
71 for (int i = 0; i < xmlMappings.Length; i++)
72 {
73 xmlMappings[i].CheckShallow();
74 if (xmlMappings[i].IsSoap)
75 {
76 flag = true;
77 }
78 }
79 bool flag2 = false;
80 if (!flag)
81 {
83 try
84 {
86 }
88 {
89 flag2 = true;
90 }
91 }
92 else
93 {
94 flag2 = true;
95 }
96 if (flag2)
97 {
99 }
101 }
102
104 {
106 for (int i = 0; i < xmlMappings.Length; i++)
107 {
109 tempMethod.isSoap = xmlMappings[i].IsSoap;
112 {
113 tempMethod.name = xmlTypeMapping.ElementName;
114 tempMethod.ns = xmlTypeMapping.Namespace;
115 }
117 }
118 }
119
120 [RequiresUnreferencedCode("calls LoadFrom")]
121 [UnconditionalSuppressMessage("SingleFile", "IL3000: Avoid accessing Assembly file path when publishing as a single file", Justification = "Annotating this as dangerous will make the core of the serializer to be marked as not safe, instead this pattern is only dangerous if using sgen only. See https://github.com/dotnet/runtime/issues/50820")]
123 {
124 Assembly assembly = null;
125 contract = null;
126 string text = null;
127 object[] customAttributes = type.GetCustomAttributes(typeof(XmlSerializerAssemblyAttribute), inherit: false);
128 if (customAttributes.Length == 0)
129 {
130 AssemblyName name = type.Assembly.GetName();
131 text = (name.Name = Compiler.GetTempAssemblyName(name, defaultNamespace));
132 name.CodeBase = null;
134 try
135 {
136 assembly = Assembly.Load(name);
137 }
138 catch (Exception ex)
139 {
141 {
142 throw;
143 }
144 }
145 if ((object)assembly == null)
146 {
148 }
149 if (assembly == null)
150 {
151 if (XmlSerializer.Mode == SerializationMode.PreGenOnly)
152 {
154 }
155 return null;
156 }
157 if (!IsSerializerVersionMatch(assembly, type, defaultNamespace))
158 {
159 XmlSerializationEventSource.Log.XmlSerializerExpired(text, type.FullName);
160 return null;
161 }
162 }
163 else
164 {
166 if (xmlSerializerAssemblyAttribute.AssemblyName != null && xmlSerializerAssemblyAttribute.CodeBase != null)
167 {
169 }
170 if (xmlSerializerAssemblyAttribute.AssemblyName != null)
171 {
174 }
175 else if (xmlSerializerAssemblyAttribute.CodeBase != null && xmlSerializerAssemblyAttribute.CodeBase.Length > 0)
176 {
179 }
180 else
181 {
182 text = type.Assembly.FullName;
183 assembly = type.Assembly;
184 }
185 if (assembly == null)
186 {
187 throw new FileNotFoundException(null, text);
188 }
189 }
190 Type typeFromAssembly = GetTypeFromAssembly(assembly, "XmlSerializerContract");
192 if (contract.CanSerialize(type))
193 {
194 return assembly;
195 }
196 return null;
197 }
198
199 [RequiresUnreferencedCode("calls LoadFile")]
200 [UnconditionalSuppressMessage("SingleFile", "IL3000: Avoid accessing Assembly file path when publishing as a single file", Justification = "Annotating this as dangerous will make the core of the serializer to be marked as not safe, instead this pattern is only dangerous if using sgen only. See https://github.com/dotnet/runtime/issues/50820")]
202 {
203 Assembly result = null;
204 string text = null;
205 try
206 {
207 if (!string.IsNullOrEmpty(type.Assembly.Location))
208 {
209 text = Path.Combine(Path.GetDirectoryName(type.Assembly.Location), assemblyName + ".dll");
210 }
211 if ((string.IsNullOrEmpty(text) || !File.Exists(text)) && !string.IsNullOrEmpty(Assembly.GetEntryAssembly()?.Location))
212 {
214 }
215 if ((string.IsNullOrEmpty(text) || !File.Exists(text)) && !string.IsNullOrEmpty(AppContext.BaseDirectory))
216 {
218 }
219 if (!string.IsNullOrEmpty(text))
220 {
221 result = Assembly.LoadFile(text);
222 }
223 }
224 catch (Exception ex)
225 {
227 {
228 throw;
229 }
230 }
231 return result;
232 }
233
234 private static bool IsSerializerVersionMatch(Assembly serializer, Type type, string defaultNamespace)
235 {
236 if (serializer == null)
237 {
238 return false;
239 }
241 if (customAttributes.Length != 1)
242 {
243 return false;
244 }
247 {
248 return true;
249 }
250 return false;
251 }
252
253 private static string GenerateAssemblyId(Type type)
254 {
255 Module[] modules = type.Assembly.GetModules();
257 for (int i = 0; i < modules.Length; i++)
258 {
259 arrayList.Add(modules[i].ModuleVersionId.ToString());
260 }
261 arrayList.Sort();
263 for (int j = 0; j < arrayList.Count; j++)
264 {
266 stringBuilder.Append(',');
267 }
268 return stringBuilder.ToString();
269 }
270
271 [RequiresUnreferencedCode("calls GenerateBegin")]
273 {
274 Compiler compiler = new Compiler();
275 try
276 {
277 Hashtable hashtable = new Hashtable();
279 {
280 hashtable[xmlMapping.Scope] = xmlMapping;
281 }
282 TypeScope[] array = new TypeScope[hashtable.Keys.Count];
283 hashtable.Keys.CopyTo(array, 0);
285 Hashtable types2 = new Hashtable();
287 foreach (TypeScope typeScope in array2)
288 {
289 foreach (Type type3 in typeScope.Types)
290 {
291 compiler.AddImport(type3, types2);
292 Assembly assembly2 = type3.Assembly;
293 string fullName = assembly2.FullName;
294 if (assemblies[fullName] == null)
295 {
297 }
298 }
299 }
300 for (int k = 0; k < types.Length; k++)
301 {
302 compiler.AddImport(types[k], types2);
303 }
304 compiler.AddImport(typeof(object).Assembly);
307 indentedWriter.WriteLine("[assembly:System.Security.AllowPartiallyTrustedCallers()]");
308 indentedWriter.WriteLine("[assembly:System.Security.SecurityTransparent()]");
309 indentedWriter.WriteLine("[assembly:System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]");
310 if (assembly != null && types.Length != 0)
311 {
312 for (int l = 0; l < types.Length; l++)
313 {
314 Type type2 = types[l];
315 if (!(type2 == null) && DynamicAssemblies.IsTypeDynamic(type2))
316 {
318 }
319 }
320 indentedWriter.Write("[assembly:");
322 indentedWriter.Write("(");
323 indentedWriter.Write("ParentAssemblyId=");
325 indentedWriter.Write(", Version=");
327 if (defaultNamespace != null)
328 {
329 indentedWriter.Write(", Namespace=");
331 }
332 indentedWriter.WriteLine(")]");
333 }
335 codeIdentifiers.AddUnique("XmlSerializationWriter", "XmlSerializationWriter");
336 codeIdentifiers.AddUnique("XmlSerializationReader", "XmlSerializationReader");
337 string text = null;
338 if (types != null && types.Length == 1 && types[0] != null)
339 {
341 if (types[0].IsArray)
342 {
343 text += "Array";
344 }
345 }
346 indentedWriter.WriteLine("namespace Microsoft.Xml.Serialization.GeneratedAssembly {");
347 indentedWriter.Indent++;
348 indentedWriter.WriteLine();
349 string text2 = "XmlSerializationWriter" + text;
350 text2 = codeIdentifiers.AddUnique(text2, text2);
352 xmlSerializationWriterCodeGen.GenerateBegin();
353 string[] array3 = new string[xmlMappings.Length];
354 for (int m = 0; m < xmlMappings.Length; m++)
355 {
357 }
358 xmlSerializationWriterCodeGen.GenerateEnd();
359 indentedWriter.WriteLine();
360 string text3 = "XmlSerializationReader" + text;
361 text3 = codeIdentifiers.AddUnique(text3, text3);
363 xmlSerializationReaderCodeGen.GenerateBegin();
364 string[] array4 = new string[xmlMappings.Length];
365 for (int n = 0; n < xmlMappings.Length; n++)
366 {
368 }
370 string baseSerializer = xmlSerializationReaderCodeGen.GenerateBaseSerializer("XmlSerializer1", text3, text2, codeIdentifiers);
372 for (int num = 0; num < xmlMappings.Length; num++)
373 {
374 if (hashtable2[xmlMappings[num].Key] == null)
375 {
377 }
378 }
379 xmlSerializationReaderCodeGen.GenerateSerializerContract("XmlSerializerContract", xmlMappings, types, text3, array4, text2, array3, hashtable2);
380 indentedWriter.Indent--;
381 indentedWriter.WriteLine("}");
382 string s = compiler.Source.ToString();
383 byte[] bytes = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true).GetBytes(s);
384 stream.Write(bytes, 0, bytes.Length);
385 stream.Flush();
386 return true;
387 }
388 finally
389 {
390 compiler.Close();
391 }
392 }
393
394 [RequiresUnreferencedCode("calls GenerateElement")]
395 internal static Assembly GenerateRefEmitAssembly(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace)
396 {
399 {
401 }
402 TypeScope[] array = new TypeScope[dictionary.Keys.Count];
403 dictionary.Keys.CopyTo(array, 0);
404 string name = "Microsoft.GeneratedCode";
406 if (types != null && types.Length != 0 && types[0] != null)
407 {
408 ConstructorInfo constructor = typeof(AssemblyVersionAttribute).GetConstructor(new Type[1] { typeof(string) });
409 string text = types[0].Assembly.GetName().Version.ToString();
410 assemblyBuilder.SetCustomAttribute(new CustomAttributeBuilder(constructor, new object[1] { text }));
411 }
413 codeIdentifiers.AddUnique("XmlSerializationWriter", "XmlSerializationWriter");
414 codeIdentifiers.AddUnique("XmlSerializationReader", "XmlSerializationReader");
415 string text2 = null;
416 if (types != null && types.Length == 1 && types[0] != null)
417 {
419 if (types[0].IsArray)
420 {
421 text2 += "Array";
422 }
423 }
425 string text3 = "XmlSerializationWriter" + text2;
426 text3 = codeIdentifiers.AddUnique(text3, text3);
429 xmlSerializationWriterILGen.GenerateBegin();
430 string[] array2 = new string[xmlMappings.Length];
431 for (int j = 0; j < xmlMappings.Length; j++)
432 {
433 array2[j] = xmlSerializationWriterILGen.GenerateElement(xmlMappings[j]);
434 }
435 Type type = xmlSerializationWriterILGen.GenerateEnd();
436 string text4 = "XmlSerializationReader" + text2;
437 text4 = codeIdentifiers.AddUnique(text4, text4);
440 xmlSerializationReaderILGen.CreatedTypes.Add(type.Name, type);
441 xmlSerializationReaderILGen.GenerateBegin();
442 string[] array3 = new string[xmlMappings.Length];
443 for (int k = 0; k < xmlMappings.Length; k++)
444 {
445 array3[k] = xmlSerializationReaderILGen.GenerateElement(xmlMappings[k]);
446 }
448 string baseSerializer = xmlSerializationReaderILGen.GenerateBaseSerializer("XmlSerializer1", text4, text3, codeIdentifiers);
450 for (int l = 0; l < xmlMappings.Length; l++)
451 {
453 {
455 }
456 }
457 xmlSerializationReaderILGen.GenerateSerializerContract("XmlSerializerContract", xmlMappings, types, text4, array3, text3, array2, dictionary2);
458 return type.Assembly;
459 }
460
462 {
463 MethodInfo method = type.GetMethod(methodName);
464 if (method != null)
465 {
466 return method;
467 }
469 throw ex;
470 }
471
472 [RequiresUnreferencedCode("calls GetType")]
473 internal static Type GetTypeFromAssembly(Assembly assembly, string typeName)
474 {
475 typeName = "Microsoft.Xml.Serialization.GeneratedAssembly." + typeName;
476 Type type = assembly.GetType(typeName);
477 if (type == null)
478 {
480 }
481 return type;
482 }
483
485 {
486 if (mapping == null)
487 {
488 return false;
489 }
490 if (mapping.Accessor.Any)
491 {
492 return true;
493 }
495 return xmlReader.IsStartElement(tempMethod.name, tempMethod.ns);
496 }
497
498 [return: NotNullIfNotNull("encodingStyle")]
499 private string ValidateEncodingStyle(string encodingStyle, string methodKey)
500 {
501 if (encodingStyle != null && encodingStyle.Length > 0)
502 {
503 if (!_methods[methodKey].isSoap)
504 {
506 }
507 if (encodingStyle != "http://schemas.xmlsoap.org/soap/encoding/" && encodingStyle != "http://www.w3.org/2003/05/soap-encoding")
508 {
509 throw new InvalidOperationException(System.SR.Format(System.SR.XmlInvalidEncoding3, encodingStyle, "http://schemas.xmlsoap.org/soap/encoding/", "http://www.w3.org/2003/05/soap-encoding"));
510 }
511 }
512 else if (_methods[methodKey].isSoap)
513 {
514 encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/";
515 }
516 return encodingStyle;
517 }
518
519 [RequiresUnreferencedCode("calls Contract")]
521 {
523 try
524 {
528 if (_methods[mapping.Key].readMethod == null)
529 {
530 if (_readerMethods == null)
531 {
533 }
534 string text = (string)_readerMethods[mapping.Key];
535 if (text == null)
536 {
538 }
539 _methods[mapping.Key].readMethod = GetMethodFromType(xmlSerializationReader.GetType(), text);
540 }
541 return _methods[mapping.Key].readMethod.Invoke(xmlSerializationReader, Array.Empty<object>());
542 }
544 {
546 }
547 finally
548 {
549 xmlSerializationReader?.Dispose();
550 }
551 }
552
553 [RequiresUnreferencedCode("calls Contract")]
555 {
557 try
558 {
562 if (_methods[mapping.Key].writeMethod == null)
563 {
564 if (_writerMethods == null)
565 {
567 }
568 string text = (string)_writerMethods[mapping.Key];
569 if (text == null)
570 {
572 }
573 _methods[mapping.Key].writeMethod = GetMethodFromType(xmlSerializationWriter.GetType(), text);
574 }
575 _methods[mapping.Key].writeMethod.Invoke(xmlSerializationWriter, new object[1] { o });
576 }
578 {
580 }
581 finally
582 {
583 xmlSerializationWriter?.Dispose();
584 }
585 }
586}
static ? object CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture)
Definition Activator.cs:17
static string BaseDirectory
Definition AppContext.cs:21
void Add(TKey key, TValue value)
static CultureInfo InvariantCulture
static bool Exists([NotNullWhen(true)] string? path)
Definition File.cs:97
static string Combine(string path1, string path2)
Definition Path.cs:304
static ? string GetDirectoryName(string? path)
Definition Path.cs:121
static Assembly Load(string assemblyString)
Definition Assembly.cs:157
static ? Assembly GetEntryAssembly()
Definition Assembly.cs:486
static Assembly LoadFile(string path)
Definition Assembly.cs:518
virtual object[] GetCustomAttributes(bool inherit)
Definition Assembly.cs:332
static Assembly LoadFrom(string assemblyFile)
Definition Assembly.cs:588
static string FailLoadAssemblyUnderPregenMode
Definition SR.cs:2146
static string XmlPregenTypeDynamic
Definition SR.cs:2142
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string XmlInvalidEncoding3
Definition SR.cs:1716
static string XmlNotSerializable
Definition SR.cs:1792
static string XmlNoPartialTrust
Definition SR.cs:1712
static string CompilingScriptsNotSupported
Definition SR.cs:2148
static string XmlPregenInvalidXmlSerializerAssemblyAttribute
Definition SR.cs:1794
static string XmlMissingType
Definition SR.cs:1392
static string XmlInvalidEncodingNotEncoded1
Definition SR.cs:1714
Definition SR.cs:7
static ModuleBuilder CreateModuleBuilder(AssemblyBuilder assemblyBuilder, string name)
static AssemblyBuilder CreateAssemblyBuilder(string name)
static string MakeValid(string identifier)
static string GetTempAssemblyName(AssemblyName parent, string ns)
Definition Compiler.cs:82
static void WriteQuotedCSharpString(IndentedWriter writer, string value)
void InvokeWriter(XmlMapping mapping, XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces, string encodingStyle, string id)
TempAssembly(XmlMapping[] xmlMappings, Assembly assembly, XmlSerializerImplementation contract)
static Assembly LoadGeneratedAssembly(Type type, string defaultNamespace, out XmlSerializerImplementation contract)
XmlSerializerImplementation _contract
static bool IsSerializerVersionMatch(Assembly serializer, Type type, string defaultNamespace)
static Type GetTypeFromAssembly(Assembly assembly, string typeName)
static MethodInfo GetMethodFromType([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type type, string methodName)
bool CanRead(XmlMapping mapping, XmlReader xmlReader)
object InvokeReader(XmlMapping mapping, XmlReader xmlReader, XmlDeserializationEvents events, string encodingStyle)
TempAssembly(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace, string location)
static string GenerateAssemblyId(Type type)
string ValidateEncodingStyle(string encodingStyle, string methodKey)
static Assembly LoadAssemblyByPath(Type type, string assemblyName)
void InitAssemblyMethods(XmlMapping[] xmlMappings)
static Assembly GenerateRefEmitAssembly(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace)
XmlSerializerImplementation Contract
static bool GenerateSerializerToStream(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace, Assembly assembly, Hashtable assemblies, Stream stream)