119 lines
3.9 KiB
C#
119 lines
3.9 KiB
C#
using UnityEditor;
|
||
using UnityEditor.Callbacks;
|
||
#if UNITY_IOS
|
||
using UnityEditor.iOS.Xcode;
|
||
#endif
|
||
using System.IO;
|
||
using System.Text.RegularExpressions;
|
||
using UnityEngine;
|
||
|
||
public class iOSFixDuplicateShellScript
|
||
{
|
||
#if UNITY_IOS
|
||
[PostProcessBuild(999)]
|
||
public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
|
||
{
|
||
if (target != BuildTarget.iOS) return;
|
||
|
||
FixDuplicateShellScript(pathToBuiltProject);
|
||
FixPodfile(pathToBuiltProject);
|
||
AddNetworkExtensionFramework(pathToBuiltProject);
|
||
}
|
||
|
||
private static void FixDuplicateShellScript(string pathToBuiltProject)
|
||
{
|
||
string projPath = Path.Combine(pathToBuiltProject, "Unity-iPhone.xcodeproj/project.pbxproj");
|
||
if (!File.Exists(projPath))
|
||
{
|
||
Debug.LogWarning("[PostProcess] pbxproj not found: " + projPath);
|
||
return;
|
||
}
|
||
|
||
string content = File.ReadAllText(projPath);
|
||
|
||
// 1. 移除连续重复的 ShellScript 行
|
||
string pattern1 = @"\t+[A-F0-9]+ /\* ShellScript \*/,(\r?\n\t+[A-F0-9]+ /\* ShellScript \*,)+";
|
||
content = Regex.Replace(content, pattern1, m =>
|
||
{
|
||
Debug.Log($"[PostProcess] 移除连续重复 ShellScript 行: {m.Value.TrimEnd().Length} chars");
|
||
return m.Groups[0].Value.Split('\n')[0]; // 只保留第一行
|
||
});
|
||
|
||
// 2. 移除整个文件中非连续的重复 ShellScript(按 ID 去重)
|
||
var lines = content.Split('\n');
|
||
var seenShellScriptIds = new System.Collections.Generic.HashSet<string>();
|
||
var outputLines = new System.Collections.Generic.List<string>();
|
||
int removedCount = 0;
|
||
|
||
foreach (string line in lines)
|
||
{
|
||
var match = Regex.Match(line, @"^\t+([A-F0-9]+) /\* ShellScript \*/,?$");
|
||
if (match.Success)
|
||
{
|
||
string id = match.Groups[1].Value;
|
||
if (!seenShellScriptIds.Add(id))
|
||
{
|
||
removedCount++;
|
||
continue; // 跳过重复
|
||
}
|
||
}
|
||
outputLines.Add(line);
|
||
}
|
||
|
||
if (removedCount > 0)
|
||
{
|
||
content = string.Join("\n", outputLines);
|
||
Debug.Log($"[PostProcess] 按 ID 去重移除了 {removedCount} 个 ShellScript 条目");
|
||
}
|
||
|
||
// 3. 修复光子矩阵 app 大小写
|
||
content = content.Replace("photonmatrix.app", "PhotonMatrix.app");
|
||
|
||
File.WriteAllText(projPath, content);
|
||
}
|
||
|
||
private static void FixPodfile(string pathToBuiltProject)
|
||
{
|
||
string podfilePath = Path.Combine(pathToBuiltProject, "Podfile");
|
||
if (!File.Exists(podfilePath))
|
||
{
|
||
Debug.LogWarning("[PostProcess] Podfile not found");
|
||
return;
|
||
}
|
||
|
||
string content = File.ReadAllText(podfilePath);
|
||
bool modified = false;
|
||
|
||
if (content.Contains("Firebase/Analytics"))
|
||
{
|
||
content = Regex.Replace(content, @"\s*pod\s+'Firebase/Analytics'[^\n]*\n?", "\n");
|
||
modified = true;
|
||
}
|
||
|
||
if (content.Contains("12.4.0"))
|
||
{
|
||
content = content.Replace("12.4.0", "12.14.0");
|
||
modified = true;
|
||
}
|
||
|
||
if (modified)
|
||
{
|
||
File.WriteAllText(podfilePath, content);
|
||
Debug.Log("[PostProcess] Cleaned up Podfile");
|
||
}
|
||
}
|
||
|
||
private static void AddNetworkExtensionFramework(string pathToBuiltProject)
|
||
{
|
||
string projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
|
||
PBXProject proj = new PBXProject();
|
||
proj.ReadFromString(File.ReadAllText(projPath));
|
||
|
||
string unityFrameworkGuid = proj.GetUnityFrameworkTargetGuid();
|
||
proj.AddFrameworkToProject(unityFrameworkGuid, "NetworkExtension.framework", false);
|
||
File.WriteAllText(projPath, proj.WriteToString());
|
||
Debug.Log("[PostProcess] Added NetworkExtension.framework");
|
||
}
|
||
#endif
|
||
}
|