#if UNITY_IOS using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Security.Cryptography; using System.Text.RegularExpressions; using UnityEditor; using UnityEditor.Callbacks; using UnityEditor.iOS.Xcode; public static class AppRuntimesXcodeLayoutValidation { private static string originalProject; private static Dictionary hashes; private static string PackageName => UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(AppSettings).Assembly).name; [PostProcessBuild(999)] public static void Capture(BuildTarget target, string buildPath) { if (target != BuildTarget.iOS) return; var project = new PBXProject(); project.ReadFromFile(PBXProject.GetPBXProjectPath(buildPath)); originalProject = project.WriteToString(); hashes = new Dictionary(); foreach (string root in new[] { "Frameworks", "Libraries" }) { string directory = Path.Combine(buildPath, root, PackageName); if (!Directory.Exists(directory)) continue; foreach (string file in Directory.GetFiles(directory, "*", SearchOption.AllDirectories)) hashes.Add(Map(file.Substring(buildPath.Length + 1).Replace('\\', '/')), Hash(file)); } } public static void Verify(string buildPath) { Require(hashes != null && hashes.Count > 0, "Captured package native files before normalization"); string projectPath = PBXProject.GetPBXProjectPath(buildPath); var before = new PBXProject(); before.ReadFromString(originalProject); var after = new PBXProject(); after.ReadFromFile(projectPath); string text = after.WriteToString(); foreach (string root in new[] { "Frameworks", "Libraries" }) { Require(!Directory.Exists(Path.Combine(buildPath, root, PackageName)), "Removed package directory: " + root); Require(!text.Contains(root + "/" + PackageName + "/"), "No stale package paths: " + root); } Require(!text.Contains("/* " + PackageName + " */"), "Removed package groups from Xcode navigator"); foreach (var file in hashes) Require(File.Exists(Path.Combine(buildPath, file.Key)) && Hash(Path.Combine(buildPath, file.Key)) == file.Value, "Native file preserved: " + file.Key); foreach (string path in before.GetRealPathsOfAllFiles(PBXSourceTree.Source)) { string guid = before.FindFileGuidByRealPath(path, PBXSourceTree.Source); Require(after.FindFileGuidByRealPath(Map(path), PBXSourceTree.Source) == guid, "File GUID preserved: " + path); if (path.StartsWith("Frameworks/" + PackageName + "/") || path.StartsWith("Libraries/" + PackageName + "/")) Require(after.FindFileGuidByProjectPath(Map(path)) == guid, "Navigator group path matches native file: " + path); foreach (string target in new[] { before.GetUnityMainTargetGuid(), before.GetUnityFrameworkTargetGuid() }) { var flagsBefore = before.GetCompileFlagsForFile(target, guid); var flagsAfter = after.GetCompileFlagsForFile(target, guid); if (Path.GetFileName(path) == "FacebookDynamicCredentials.m") continue; Require((flagsBefore ?? new List()).SequenceEqual(flagsAfter ?? new List()), "Compiler flags preserved: " + path); } } foreach (string section in new[] { "PBXSourcesBuildPhase", "PBXFrameworksBuildPhase", "PBXCopyFilesBuildPhase" }) { string oldSection = Section(originalProject, section); string newSection = Section(text, section); foreach (Match guid in Regex.Matches(oldSection, @"\b[A-Fa-f0-9]{24}\b")) Require(newSection.Contains(guid.Value), "Build phase membership preserved: " + section); } Require(Directory.Exists(Path.Combine(buildPath, "Frameworks/FacebookSDK/Plugins/iOS/XCFrameworks")), "Standard Facebook framework layout"); Require(Directory.Exists(Path.Combine(buildPath, "Frameworks/UniWebView/Plugins/iOS/UniWebView.xcframework")), "Standard UniWebView framework layout"); Require(File.Exists(Path.Combine(buildPath, "Libraries/Plugins/iOS/AppRuntimesNative.mm")), "Standard native plugin layout"); Normalize(buildPath); Require(File.ReadAllText(projectPath) == text, "Normalization is idempotent"); string fixture = Path.GetFullPath("Library/XcodeLayoutFixture-" + Guid.NewGuid().ToString("N")); string fixtureProject = PBXProject.GetPBXProjectPath(fixture); Directory.CreateDirectory(Path.GetDirectoryName(fixtureProject)); string sourcePath = "Libraries/" + PackageName + "/Runtime/Plugins/iOS/DeviceTimeZone.mm"; string source = Path.Combine(fixture, sourcePath); string destination = Path.Combine(fixture, Map(sourcePath)); Directory.CreateDirectory(Path.GetDirectoryName(source)); Directory.CreateDirectory(Path.GetDirectoryName(destination)); File.WriteAllText(fixtureProject, originalProject); File.WriteAllText(source, "new plugin"); File.WriteAllText(destination, "host plugin"); bool rejected = false; try { Normalize(fixture); } catch (TargetInvocationException exception) when (exception.InnerException is UnityEditor.Build.BuildFailedException) { rejected = true; } Require(rejected && File.ReadAllText(source) == "new plugin" && File.ReadAllText(destination) == "host plugin" && File.ReadAllText(fixtureProject) == originalProject, "Conflicts fail before changing export"); File.WriteAllText(destination, "new plugin"); Normalize(fixture); Require(!File.Exists(source) && File.ReadAllText(destination) == "new plugin", "Identical append-export files supported"); UnityEngine.Debug.Log("APP_RUNTIMES_XCODE_LAYOUT_VALIDATION_PASS"); } private static void Normalize(string path) => typeof(XcodePostprocessor).Assembly .GetType("XcodePluginLayout").GetMethod("Normalize", BindingFlags.Static | BindingFlags.NonPublic) .Invoke(null, new object[] { path }); private static string Map(string path) => Regex.Replace(path, @"^(Frameworks|Libraries)/" + Regex.Escape(PackageName) + @"/(?:(?:Runtime|ThirdParty)/)?", "$1/"); private static string Hash(string path) { if (Path.DirectorySeparatorChar == '\\') path = @"\\?\" + Path.GetFullPath(path); using (var sha = SHA256.Create()) using (var stream = File.OpenRead(path)) return Convert.ToBase64String(sha.ComputeHash(stream)); } private static string Section(string text, string name) => Regex.Match(text, @"(?s)/\* Begin " + name + @" section \*/.*?/\* End " + name + @" section \*/").Value; private static void Require(bool condition, string message) { if (!condition) throw new Exception("Xcode layout validation failed: " + message); } } #endif