HotPatchSourcesLinker.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Xml.Linq;
  6. using HybridCLR.CodeGen.Link;
  7. using HybridCLR.Mod;
  8. using UnityEditor;
  9. using UnityEditor.Build;
  10. using UnityEditor.Build.Reporting;
  11. using UnityEditor.UnityLinker;
  12. namespace HotPatch.Editor
  13. {
  14. public sealed class HotPatchSourcesLinker : IUnityLinkerProcessor
  15. {
  16. public int callbackOrder => 0;
  17. public string GenerateAdditionalLinkXmlFile(BuildReport report, UnityLinkerBuildPipelineData data) => Generate(report.summary.platform);
  18. public static string Generate() => Generate(EditorUserBuildSettings.activeBuildTarget);
  19. public static string Generate(BuildTarget target)
  20. {
  21. const string sourcesAssembly = "AppRuntimes.Sources.Editor";
  22. var analyzer = new LinkAnalyzer(new PathAssemblyResolver("Library/ScriptAssemblies"));
  23. var types = analyzer.CollectRefs(new List<string> { sourcesAssembly }, LinkPreserveLevel.PreserveAllOfDeclaringType)
  24. .Where(t => !t.AssemblyName.StartsWith("UnityEditor", StringComparison.Ordinal) &&
  25. t.AssemblyName != sourcesAssembly).ToList();
  26. const string directory = "Library/HotPatch/Linker";
  27. Directory.CreateDirectory(directory);
  28. var path = Path.GetFullPath(Path.Combine(directory, "Sources.xml"));
  29. new LinkXmlWriter().Write(path, types);
  30. var document = XDocument.Load(path);
  31. foreach (var node in document.Root.Elements("assembly").Where(n => (string)n.Attribute("fullname") == "AppRuntimes.Base").ToArray()) node.Remove();
  32. document.Root.Add(new XElement("assembly", new XAttribute("fullname", "AppRuntimes.Base"), new XAttribute("preserve", "all")));
  33. if (target == BuildTarget.iOS)
  34. {
  35. const string iosSupport = "Unity.Advertisement.IosSupport";
  36. foreach (var node in document.Root.Elements("assembly").Where(n => (string)n.Attribute("fullname") == iosSupport).ToArray()) node.Remove();
  37. document.Root.Add(new XElement("assembly", new XAttribute("fullname", iosSupport), new XAttribute("preserve", "all")));
  38. }
  39. document.Save(path);
  40. return path;
  41. }
  42. }
  43. }