using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Xml.Linq; using HybridCLR.CodeGen.Link; using HybridCLR.Mod; using UnityEditor; using UnityEditor.Build; using UnityEditor.Build.Reporting; using UnityEditor.UnityLinker; namespace HotPatch.Editor { public sealed class HotPatchSourcesLinker : IUnityLinkerProcessor { public int callbackOrder => 0; public string GenerateAdditionalLinkXmlFile(BuildReport report, UnityLinkerBuildPipelineData data) => Generate(report.summary.platform); public static string Generate() => Generate(EditorUserBuildSettings.activeBuildTarget); public static string Generate(BuildTarget target) { const string sourcesAssembly = "AppRuntimes.Sources.Editor"; var analyzer = new LinkAnalyzer(new PathAssemblyResolver("Library/ScriptAssemblies")); var types = analyzer.CollectRefs(new List { sourcesAssembly }, LinkPreserveLevel.PreserveAllOfDeclaringType) .Where(t => !t.AssemblyName.StartsWith("UnityEditor", StringComparison.Ordinal) && t.AssemblyName != sourcesAssembly).ToList(); const string directory = "Library/HotPatch/Linker"; Directory.CreateDirectory(directory); var path = Path.GetFullPath(Path.Combine(directory, "Sources.xml")); new LinkXmlWriter().Write(path, types); var document = XDocument.Load(path); foreach (var node in document.Root.Elements("assembly").Where(n => (string)n.Attribute("fullname") == "AppRuntimes.Base").ToArray()) node.Remove(); document.Root.Add(new XElement("assembly", new XAttribute("fullname", "AppRuntimes.Base"), new XAttribute("preserve", "all"))); if (target == BuildTarget.iOS) { const string iosSupport = "Unity.Advertisement.IosSupport"; foreach (var node in document.Root.Elements("assembly").Where(n => (string)n.Attribute("fullname") == iosSupport).ToArray()) node.Remove(); document.Root.Add(new XElement("assembly", new XAttribute("fullname", iosSupport), new XAttribute("preserve", "all"))); } document.Save(path); return path; } } }