LinkXmlWriter.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using dnlib.DotNet;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace HybridCLR.Editor.Link
  10. {
  11. public class LinkXmlWriter
  12. {
  13. public void Write(string outputLinkXmlFile, HashSet<TypeRef> refTypes)
  14. {
  15. string parentDir = Directory.GetParent(outputLinkXmlFile).FullName;
  16. Directory.CreateDirectory(parentDir);
  17. var writer = System.Xml.XmlWriter.Create(outputLinkXmlFile,
  18. new System.Xml.XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true});
  19. writer.WriteStartDocument();
  20. writer.WriteStartElement("linker");
  21. var typesByAssembly = refTypes.GroupBy(t => t.DefinitionAssembly.Name.String).ToList();
  22. typesByAssembly.Sort((a, b) => String.Compare(a.Key, b.Key, StringComparison.Ordinal));
  23. foreach(var assembly in typesByAssembly)
  24. {
  25. writer.WriteStartElement("assembly");
  26. writer.WriteAttributeString("fullname", assembly.Key);
  27. List<string> assTypeNames = assembly.Select(t => t.FullName).ToList();
  28. assTypeNames.Sort(string.CompareOrdinal);
  29. foreach(var typeName in assTypeNames)
  30. {
  31. #if UNITY_2023_1_OR_NEWER
  32. if (typeName == "UnityEngine.Debug")
  33. {
  34. continue;
  35. }
  36. #endif
  37. writer.WriteStartElement("type");
  38. writer.WriteAttributeString("fullname", typeName);
  39. writer.WriteAttributeString("preserve", "all");
  40. writer.WriteEndElement();
  41. }
  42. writer.WriteEndElement();
  43. }
  44. writer.WriteEndElement();
  45. writer.WriteEndDocument();
  46. writer.Close();
  47. }
  48. }
  49. }