AppRuntimesXcodeLayoutValidation.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. [PostProcessBuild(999)]
  17. public static void Capture(BuildTarget target, string buildPath)
  18. {
  19. if (target != BuildTarget.iOS) return;
  20. var project = new PBXProject();
  21. project.ReadFromFile(PBXProject.GetPBXProjectPath(buildPath));
  22. originalProject = project.WriteToString();
  23. hashes = new Dictionary<string, string>();
  24. foreach (string root in new[] { "Frameworks", "Libraries" })
  25. {
  26. string directory = Path.Combine(buildPath, root, "come.app.runtimes");
  27. foreach (string file in Directory.GetFiles(directory, "*", SearchOption.AllDirectories))
  28. hashes.Add(Map(file.Substring(buildPath.Length + 1).Replace('\\', '/')), Hash(file));
  29. }
  30. }
  31. public static void Verify(string buildPath)
  32. {
  33. Require(hashes != null && hashes.Count > 0, "Captured package native files before normalization");
  34. string projectPath = PBXProject.GetPBXProjectPath(buildPath);
  35. var before = new PBXProject();
  36. before.ReadFromString(originalProject);
  37. var after = new PBXProject();
  38. after.ReadFromFile(projectPath);
  39. string text = after.WriteToString();
  40. foreach (string root in new[] { "Frameworks", "Libraries" })
  41. {
  42. Require(!Directory.Exists(Path.Combine(buildPath, root, "come.app.runtimes")), "Removed package directory: " + root);
  43. Require(!text.Contains(root + "/come.app.runtimes/"), "No stale package paths: " + root);
  44. }
  45. Require(!text.Contains("/* come.app.runtimes */"), "Removed package groups from Xcode navigator");
  46. foreach (var file in hashes)
  47. Require(File.Exists(Path.Combine(buildPath, file.Key)) && Hash(Path.Combine(buildPath, file.Key)) == file.Value,
  48. "Native file preserved: " + file.Key);
  49. foreach (string path in before.GetRealPathsOfAllFiles(PBXSourceTree.Source))
  50. {
  51. string guid = before.FindFileGuidByRealPath(path, PBXSourceTree.Source);
  52. Require(after.FindFileGuidByRealPath(Map(path), PBXSourceTree.Source) == guid, "File GUID preserved: " + path);
  53. if (path.StartsWith("Frameworks/come.app.runtimes/") || path.StartsWith("Libraries/come.app.runtimes/"))
  54. Require(after.FindFileGuidByProjectPath(Map(path)) == guid, "Navigator group path matches native file: " + path);
  55. foreach (string target in new[] { before.GetUnityMainTargetGuid(), before.GetUnityFrameworkTargetGuid() })
  56. {
  57. var flagsBefore = before.GetCompileFlagsForFile(target, guid);
  58. var flagsAfter = after.GetCompileFlagsForFile(target, guid);
  59. // Facebook credentials gains ARC in the existing callback at order 1000.
  60. if (Path.GetFileName(path) == "FacebookDynamicCredentials.m") continue;
  61. Require((flagsBefore ?? new List<string>()).SequenceEqual(flagsAfter ?? new List<string>()),
  62. "Compiler flags preserved: " + path);
  63. }
  64. }
  65. foreach (string section in new[] { "PBXSourcesBuildPhase", "PBXFrameworksBuildPhase", "PBXCopyFilesBuildPhase" })
  66. {
  67. // LocalFacebookSdk adds system frameworks; all original memberships must survive.
  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. // Exercise append exports and collision protection without touching the real export.
  79. string fixture = Path.GetFullPath("Library/XcodeLayoutFixture-" + Guid.NewGuid().ToString("N"));
  80. string fixtureProject = PBXProject.GetPBXProjectPath(fixture);
  81. Directory.CreateDirectory(Path.GetDirectoryName(fixtureProject));
  82. const string sourcePath = "Libraries/come.app.runtimes/Runtime/Plugins/iOS/DeviceTimeZone.mm";
  83. string source = Path.Combine(fixture, sourcePath);
  84. string destination = Path.Combine(fixture, Map(sourcePath));
  85. Directory.CreateDirectory(Path.GetDirectoryName(source));
  86. Directory.CreateDirectory(Path.GetDirectoryName(destination));
  87. File.WriteAllText(fixtureProject, originalProject);
  88. File.WriteAllText(source, "new plugin");
  89. File.WriteAllText(destination, "host plugin");
  90. bool rejected = false;
  91. try { Normalize(fixture); }
  92. catch (TargetInvocationException exception) when (exception.InnerException is UnityEditor.Build.BuildFailedException) { rejected = true; }
  93. Require(rejected && File.ReadAllText(source) == "new plugin" && File.ReadAllText(destination) == "host plugin"
  94. && File.ReadAllText(fixtureProject) == originalProject, "Conflicts fail before changing export");
  95. File.WriteAllText(destination, "new plugin");
  96. Normalize(fixture);
  97. Require(!File.Exists(source) && File.ReadAllText(destination) == "new plugin", "Identical append-export files supported");
  98. UnityEngine.Debug.Log("APP_RUNTIMES_XCODE_LAYOUT_VALIDATION_PASS");
  99. }
  100. private static void Normalize(string path) => typeof(XcodePostprocessor).Assembly
  101. .GetType("XcodePluginLayout").GetMethod("Normalize", BindingFlags.Static | BindingFlags.NonPublic)
  102. .Invoke(null, new object[] { path });
  103. private static string Map(string path) => Regex.Replace(path,
  104. @"^(Frameworks|Libraries)/come\.app\.runtimes/(?:(?:Runtime|ThirdParty)/)?", "$1/");
  105. private static string Hash(string path)
  106. {
  107. if (Path.DirectorySeparatorChar == '\\') path = @"\\?\" + Path.GetFullPath(path);
  108. using (var sha = SHA256.Create())
  109. using (var stream = File.OpenRead(path)) return Convert.ToBase64String(sha.ComputeHash(stream));
  110. }
  111. private static string Section(string text, string name) => Regex.Match(text,
  112. @"(?s)/\* Begin " + name + @" section \*/.*?/\* End " + name + @" section \*/").Value;
  113. private static void Require(bool condition, string message)
  114. {
  115. if (!condition) throw new Exception("Xcode layout validation failed: " + message);
  116. }
  117. }
  118. #endif