|
|
@@ -0,0 +1,176 @@
|
|
|
+#if UNITY_EDITOR && UNITY_IOS
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Text.RegularExpressions;
|
|
|
+using UnityEditor.Build;
|
|
|
+using UnityEditor.iOS.Xcode;
|
|
|
+
|
|
|
+internal static class XcodePluginLayout
|
|
|
+{
|
|
|
+ private const string PackageName = "come.app.runtimes";
|
|
|
+ private static readonly string[] ExportRoots = { "Frameworks", "Libraries" };
|
|
|
+ private static readonly string[] PackageFolders = { "Runtime", "ThirdParty" };
|
|
|
+
|
|
|
+ internal static void Normalize(string buildPath)
|
|
|
+ {
|
|
|
+ string projectPath = PBXProject.GetPBXProjectPath(buildPath);
|
|
|
+ var project = new PBXProject();
|
|
|
+ project.ReadFromFile(projectPath);
|
|
|
+
|
|
|
+ // Work on Unity's canonical serialization. Keep all file/build GUIDs, target
|
|
|
+ // memberships, embed settings and per-file compiler flags intact.
|
|
|
+ string projectText = FlattenGroups(project.WriteToString());
|
|
|
+ var moves = new List<KeyValuePair<string, string>>();
|
|
|
+ foreach (string root in ExportRoots)
|
|
|
+ {
|
|
|
+ string packagePrefix = root + "/" + PackageName + "/";
|
|
|
+ foreach (string folder in PackageFolders)
|
|
|
+ projectText = projectText.Replace(packagePrefix + folder + "/", root + "/");
|
|
|
+ projectText = projectText.Replace(packagePrefix, root + "/");
|
|
|
+
|
|
|
+ string source = Path.Combine(buildPath, root, PackageName);
|
|
|
+ if (!Directory.Exists(source)) continue;
|
|
|
+ foreach (string file in Directory.GetFiles(source, "*", SearchOption.AllDirectories))
|
|
|
+ {
|
|
|
+ string relative = file.Substring(source.Length + 1).Replace('\\', '/');
|
|
|
+ foreach (string folder in PackageFolders)
|
|
|
+ if (relative.StartsWith(folder + "/", StringComparison.Ordinal))
|
|
|
+ {
|
|
|
+ relative = relative.Substring(folder.Length + 1);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ moves.Add(new KeyValuePair<string, string>(file, Path.Combine(buildPath, root, relative)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Check every collision before changing the export. Identical files from
|
|
|
+ // an append export are safe; different files require a clean export folder.
|
|
|
+ var destinations = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
|
+ foreach (var move in moves)
|
|
|
+ {
|
|
|
+ if (!destinations.Add(move.Value) || Directory.Exists(move.Value)
|
|
|
+ || (File.Exists(move.Value) && !FilesEqual(move.Key, move.Value)))
|
|
|
+ throw new BuildFailedException("AppRuntimes iOS export path conflict: " + move.Value
|
|
|
+ + ". Export to an empty folder or remove the conflicting plugin.");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Validate the rewritten project before moving any files.
|
|
|
+ project.ReadFromString(projectText);
|
|
|
+ foreach (var move in moves)
|
|
|
+ {
|
|
|
+ Directory.CreateDirectory(Path.GetDirectoryName(move.Value));
|
|
|
+ if (File.Exists(move.Value)) File.Delete(move.Key); // Identical, checked above.
|
|
|
+ else File.Move(move.Key, move.Value);
|
|
|
+ }
|
|
|
+ foreach (string root in ExportRoots)
|
|
|
+ RemoveEmptyDirectories(Path.Combine(buildPath, root, PackageName));
|
|
|
+ project.WriteToFile(projectPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static string FlattenGroups(string text)
|
|
|
+ {
|
|
|
+ const string begin = "/* Begin PBXGroup section */";
|
|
|
+ const string end = "/* End PBXGroup section */";
|
|
|
+ int start = text.IndexOf(begin, StringComparison.Ordinal);
|
|
|
+ int finish = text.IndexOf(end, StringComparison.Ordinal);
|
|
|
+ if (start < 0 || finish < start)
|
|
|
+ throw new BuildFailedException("AppRuntimes could not find Xcode project groups.");
|
|
|
+
|
|
|
+ string section = text.Substring(start, finish - start);
|
|
|
+ var groups = Regex.Matches(section,
|
|
|
+ @"(?ms)^\t\t(?<id>[A-Fa-f0-9]{24}) /\* .*? \*/ = \{\r?\n.*?^\t\t\};\r?\n")
|
|
|
+ .Cast<Match>().ToDictionary(m => m.Groups["id"].Value);
|
|
|
+ var removed = new HashSet<string>();
|
|
|
+ var replacements = new Dictionary<string, string>();
|
|
|
+
|
|
|
+ foreach (var entry in groups)
|
|
|
+ {
|
|
|
+ string root = GroupPath(entry.Value.Value);
|
|
|
+ if (!ExportRoots.Contains(root)) continue;
|
|
|
+ string children = Children(entry.Value.Value).Groups["items"].Value;
|
|
|
+ foreach (Match child in ChildReferences(children))
|
|
|
+ {
|
|
|
+ string id = child.Groups["id"].Value;
|
|
|
+ if (!groups.TryGetValue(id, out Match package) || GroupPath(package.Value) != PackageName)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ string promoted = Children(package.Value).Groups["items"].Value;
|
|
|
+ foreach (Match nested in ChildReferences(promoted))
|
|
|
+ {
|
|
|
+ string nestedId = nested.Groups["id"].Value;
|
|
|
+ if (groups.TryGetValue(nestedId, out Match wrapper)
|
|
|
+ && PackageFolders.Contains(GroupPath(wrapper.Value)))
|
|
|
+ {
|
|
|
+ promoted = promoted.Replace(nested.Value, Children(wrapper.Value).Groups["items"].Value);
|
|
|
+ removed.Add(nestedId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ children = children.Replace(child.Value, promoted);
|
|
|
+ removed.Add(id);
|
|
|
+ }
|
|
|
+ Match original = Children(entry.Value.Value);
|
|
|
+ Group items = original.Groups["items"];
|
|
|
+ replacements[entry.Key] = entry.Value.Value.Remove(items.Index, items.Length).Insert(items.Index, children);
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (var entry in groups)
|
|
|
+ {
|
|
|
+ if (removed.Contains(entry.Key)) section = section.Replace(entry.Value.Value, "");
|
|
|
+ else if (replacements.TryGetValue(entry.Key, out string replacement))
|
|
|
+ section = section.Replace(entry.Value.Value, replacement);
|
|
|
+ }
|
|
|
+ if (Regex.IsMatch(section, @"\bpath = ""?" + Regex.Escape(PackageName) + @"""?;"))
|
|
|
+ throw new BuildFailedException("AppRuntimes could not normalize the package's Xcode groups.");
|
|
|
+ return text.Substring(0, start) + section + text.Substring(finish);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static string GroupPath(string group) =>
|
|
|
+ Regex.Match(group, @"(?m)^\s*path = ""?(?<path>[^"";\r\n]+)""?;").Groups["path"].Value;
|
|
|
+
|
|
|
+ private static Match Children(string group) =>
|
|
|
+ Regex.Match(group, @"(?s)children = \(\r?\n(?<items>.*?)\t\t\t\);");
|
|
|
+
|
|
|
+ private static MatchCollection ChildReferences(string children) =>
|
|
|
+ Regex.Matches(children, @"(?m)^\t\t\t\t(?<id>[A-Fa-f0-9]{24}) /\* .*? \*/,\r?\n");
|
|
|
+
|
|
|
+ private static bool FilesEqual(string first, string second)
|
|
|
+ {
|
|
|
+ // Unity's Mono FileStream needs the extended prefix for deep XCFramework paths on Windows.
|
|
|
+ if (Path.DirectorySeparatorChar == '\\')
|
|
|
+ {
|
|
|
+ first = @"\\?\" + Path.GetFullPath(first);
|
|
|
+ second = @"\\?\" + Path.GetFullPath(second);
|
|
|
+ }
|
|
|
+ if (new FileInfo(first).Length != new FileInfo(second).Length) return false;
|
|
|
+ using (var a = File.OpenRead(first))
|
|
|
+ using (var b = File.OpenRead(second))
|
|
|
+ {
|
|
|
+ var left = new byte[81920];
|
|
|
+ var right = new byte[left.Length];
|
|
|
+ int count;
|
|
|
+ while ((count = a.Read(left, 0, left.Length)) > 0)
|
|
|
+ {
|
|
|
+ int read = 0;
|
|
|
+ while (read < count)
|
|
|
+ {
|
|
|
+ int chunk = b.Read(right, read, count - read);
|
|
|
+ if (chunk == 0) return false;
|
|
|
+ read += chunk;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < count; i++)
|
|
|
+ if (left[i] != right[i]) return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void RemoveEmptyDirectories(string directory)
|
|
|
+ {
|
|
|
+ if (!Directory.Exists(directory)) return;
|
|
|
+ foreach (string child in Directory.GetDirectories(directory)) RemoveEmptyDirectories(child);
|
|
|
+ Directory.Delete(directory, false);
|
|
|
+ }
|
|
|
+}
|
|
|
+#endif
|