AppRuntimesXcodeLayoutValidation.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #if UNITY_IOS
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Security.Cryptography;
  8. using System.Text.RegularExpressions;
  9. using UnityEditor;
  10. using UnityEditor.Callbacks;
  11. using UnityEditor.iOS.Xcode;
  12. public static class AppRuntimesXcodeLayoutValidation
  13. {
  14. private static string originalProject;
  15. private static Dictionary<string, string> hashes;
  16. private static string PackageName => UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(AppSettings).Assembly).name;
  17. [PostProcessBuild(999)]
  18. public static void Capture(BuildTarget target, string buildPath)
  19. {
  20. if (target != BuildTarget.iOS) return;
  21. var project = new PBXProject();
  22. project.ReadFromFile(PBXProject.GetPBXProjectPath(buildPath));
  23. originalProject = project.WriteToString();
  24. hashes = new Dictionary<string, string>();
  25. foreach (string root in new[] { "Frameworks", "Libraries" })
  26. {
  27. string directory = Path.Combine(buildPath, root, PackageName);
  28. if (!Directory.Exists(directory)) continue;
  29. foreach (string file in Directory.GetFiles(directory, "*", SearchOption.AllDirectories))
  30. hashes.Add(Map(file.Substring(buildPath.Length + 1).Replace('\\', '/')), Hash(file));
  31. }
  32. }
  33. public static void Verify(string buildPath)
  34. {
  35. Require(hashes != null && hashes.Count > 0, "Captured package native files before normalization");
  36. string projectPath = PBXProject.GetPBXProjectPath(buildPath);
  37. var before = new PBXProject();
  38. before.ReadFromString(originalProject);
  39. var after = new PBXProject();
  40. after.ReadFromFile(projectPath);
  41. string text = after.WriteToString();
  42. foreach (string root in new[] { "Frameworks", "Libraries" })
  43. {
  44. Require(!Directory.Exists(Path.Combine(buildPath, root, PackageName)), "Removed package directory: " + root);
  45. Require(!text.Contains(root + "/" + PackageName + "/"), "No stale package paths: " + root);
  46. }
  47. Require(!text.Contains("/* " + PackageName + " */"), "Removed package groups from Xcode navigator");
  48. foreach (var file in hashes)
  49. Require(File.Exists(Path.Combine(buildPath, file.Key)) && Hash(Path.Combine(buildPath, file.Key)) == file.Value,
  50. "Native file preserved: " + file.Key);
  51. foreach (string path in before.GetRealPathsOfAllFiles(PBXSourceTree.Source))
  52. {
  53. string guid = before.FindFileGuidByRealPath(path, PBXSourceTree.Source);
  54. Require(after.FindFileGuidByRealPath(Map(path), PBXSourceTree.Source) == guid, "File GUID preserved: " + path);
  55. if (path.StartsWith("Frameworks/" + PackageName + "/") || path.StartsWith("Libraries/" + PackageName + "/"))
  56. Require(after.FindFileGuidByProjectPath(Map(path)) == guid, "Navigator group path matches native file: " + path);
  57. foreach (string target in new[] { before.GetUnityMainTargetGuid(), before.GetUnityFrameworkTargetGuid() })
  58. {
  59. var flagsBefore = before.GetCompileFlagsForFile(target, guid);
  60. var flagsAfter = after.GetCompileFlagsForFile(target, guid);
  61. if (Path.GetFileName(path) == "FacebookDynamicCredentials.m") continue;
  62. Require((flagsBefore ?? new List<string>()).SequenceEqual(flagsAfter ?? new List<string>()),
  63. "Compiler flags preserved: " + path);
  64. }
  65. }
  66. foreach (string section in new[] { "PBXSourcesBuildPhase", "PBXFrameworksBuildPhase", "PBXCopyFilesBuildPhase" })
  67. {
  68. string oldSection = Section(originalProject, section);
  69. string newSection = Section(text, section);
  70. foreach (Match guid in Regex.Matches(oldSection, @"\b[A-Fa-f0-9]{24}\b"))
  71. Require(newSection.Contains(guid.Value), "Build phase membership preserved: " + section);
  72. }
  73. Require(Directory.Exists(Path.Combine(buildPath, "Frameworks/FacebookSDK/Plugins/iOS/XCFrameworks")), "Standard Facebook framework layout");
  74. Require(Directory.Exists(Path.Combine(buildPath, "Frameworks/UniWebView/Plugins/iOS/UniWebView.xcframework")), "Standard UniWebView framework layout");
  75. Require(File.Exists(Path.Combine(buildPath, "Libraries/Plugins/iOS/AppRuntimesNative.mm")), "Standard native plugin layout");
  76. Normalize(buildPath);
  77. Require(File.ReadAllText(projectPath) == text, "Normalization is idempotent");
  78. string fixture = Path.GetFullPath("Library/XcodeLayoutFixture-" + Guid.NewGuid().ToString("N"));
  79. string fixtureProject = PBXProject.GetPBXProjectPath(fixture);
  80. Directory.CreateDirectory(Path.GetDirectoryName(fixtureProject));
  81. string sourcePath = "Libraries/" + PackageName + "/Runtime/Plugins/iOS/DeviceTimeZone.mm";
  82. string source = Path.Combine(fixture, sourcePath);
  83. string destination = Path.Combine(fixture, Map(sourcePath));
  84. Directory.CreateDirectory(Path.GetDirectoryName(source));
  85. Directory.CreateDirectory(Path.GetDirectoryName(destination));
  86. File.WriteAllText(fixtureProject, originalProject);
  87. File.WriteAllText(source, "new plugin");
  88. File.WriteAllText(destination, "host plugin");
  89. bool rejected = false;
  90. try { Normalize(fixture); }
  91. catch (TargetInvocationException exception) when (exception.InnerException is UnityEditor.Build.BuildFailedException) { rejected = true; }
  92. Require(rejected && File.ReadAllText(source) == "new plugin" && File.ReadAllText(destination) == "host plugin"
  93. && File.ReadAllText(fixtureProject) == originalProject, "Conflicts fail before changing export");
  94. File.WriteAllText(destination, "new plugin");
  95. Normalize(fixture);
  96. Require(!File.Exists(source) && File.ReadAllText(destination) == "new plugin", "Identical append-export files supported");
  97. UnityEngine.Debug.Log("APP_RUNTIMES_XCODE_LAYOUT_VALIDATION_PASS");
  98. }
  99. private static void Normalize(string path) => typeof(XcodePostprocessor).Assembly
  100. .GetType("XcodePluginLayout").GetMethod("Normalize", BindingFlags.Static | BindingFlags.NonPublic)
  101. .Invoke(null, new object[] { path });
  102. private static string Map(string path) => Regex.Replace(path,
  103. @"^(Frameworks|Libraries)/" + Regex.Escape(PackageName) + @"/(?:(?:Runtime|ThirdParty)/)?", "$1/");
  104. private static string Hash(string path)
  105. {
  106. if (Path.DirectorySeparatorChar == '\\') path = @"\\?\" + Path.GetFullPath(path);
  107. using (var sha = SHA256.Create())
  108. using (var stream = File.OpenRead(path)) return Convert.ToBase64String(sha.ComputeHash(stream));
  109. }
  110. private static string Section(string text, string name) => Regex.Match(text,
  111. @"(?s)/\* Begin " + name + @" section \*/.*?/\* End " + name + @" section \*/").Value;
  112. private static void Require(bool condition, string message)
  113. {
  114. if (!condition) throw new Exception("Xcode layout validation failed: " + message);
  115. }
  116. }
  117. #endif