NLDClient-yudde/ProjectNLD/Assets/Editor/XUPorter/XCodePostProcess.cs

212 lines
7.3 KiB
C#
Raw Normal View History

2024-11-15 12:11:44 +08:00
using System;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Callbacks;
#endif
using System.IO;
2024-08-20 12:48:51 +08:00
#if UNITY_EDITOR && UNITY_IOS
2024-08-16 11:57:03 +08:00
using UnityEditor.iOS.Xcode;
2024-08-20 12:48:51 +08:00
#endif
public static class XCodePostProcess
{
2024-08-20 12:48:51 +08:00
#if UNITY_EDITOR && UNITY_IOS
2024-11-15 12:11:44 +08:00
[PostProcessBuild(999)]
public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iOS)
{
Debug.LogWarning("Target is not iPhone. XCodePostProcess will not run");
return;
}
2024-11-15 12:11:44 +08:00
Debug.Log("start modify xcode project");
2024-11-15 13:20:58 +08:00
var projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
2024-11-15 16:45:53 +08:00
2024-11-15 12:11:44 +08:00
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
2026-04-08 12:49:05 +08:00
2024-11-15 12:11:44 +08:00
var frameworkTarget = proj.GetUnityFrameworkTargetGuid();
2026-04-08 12:49:05 +08:00
var mainTargetGuid = proj.GetUnityMainTargetGuid();
2026-04-01 14:45:10 +08:00
2026-04-08 12:49:05 +08:00
AddFrameworkSafe(proj, frameworkTarget, "libz.tbd");
AddFrameworkSafe(proj, frameworkTarget, "Security.framework");
AddFrameworkSafe(proj, frameworkTarget, "SystemConfiguration.framework");
AddFrameworkSafe(proj, frameworkTarget, "MetricKit.framework");
AddFrameworkSafe(proj, frameworkTarget, "AuthenticationServices.framework", true);
2026-04-02 15:23:53 +08:00
2026-04-08 12:49:05 +08:00
AddFrameworkSafe(proj, frameworkTarget, "AdSupport.framework");
AddFrameworkSafe(proj, frameworkTarget, "AppTrackingTransparency.framework", true);
// AIHelper
#if SDK_AIHELPER
AddBuildPropertySafe(proj, frameworkTarget, "OTHER_LDFLAGS", "-ObjC");
AddBuildPropertySafe(proj, mainTargetGuid, "OTHER_LDFLAGS", "-ObjC");
Debug.Log("AIHelper: Added -ObjC");
2026-04-02 15:23:53 +08:00
#endif
2026-04-08 12:49:05 +08:00
// CrashSight
2026-04-03 19:57:05 +08:00
#if SDK_CRASHSIGHT
2026-04-08 12:49:05 +08:00
SetBuildProperty(proj, mainTargetGuid, "GENERATE_DEBUG_SYMBOLS", "YES");
SetBuildProperty(proj, mainTargetGuid, "DEBUG_INFORMATION_FORMAT", "dwarf-with-dsym");
2026-04-03 19:57:05 +08:00
2026-04-08 12:49:05 +08:00
SetBuildProperty(proj, frameworkTarget, "GENERATE_DEBUG_SYMBOLS", "YES");
SetBuildProperty(proj, frameworkTarget, "DEBUG_INFORMATION_FORMAT", "dwarf-with-dsym");
Debug.Log("CrashSight: dSYM enabled");
2026-04-03 19:57:05 +08:00
#endif
2024-11-15 16:45:53 +08:00
2026-04-08 12:49:05 +08:00
// ThingkingData
#if SDK_THINKINGDATA
// 开启异常
SetBuildProperty(proj, frameworkTarget, "GCC_ENABLE_OBJC_EXCEPTIONS", "YES");
SetBuildProperty(proj, mainTargetGuid, "GCC_ENABLE_OBJC_EXCEPTIONS", "YES");
2026-04-01 14:45:10 +08:00
2026-04-08 12:49:05 +08:00
SetBuildProperty(proj, frameworkTarget, "CLANG_ENABLE_OBJC_EXCEPTIONS", "YES");
SetBuildProperty(proj, mainTargetGuid, "CLANG_ENABLE_OBJC_EXCEPTIONS", "YES");
2024-11-15 12:11:44 +08:00
2026-04-08 12:49:05 +08:00
// sqlite
AddFrameworkSafe(proj, frameworkTarget, "libsqlite3.tbd");
AddFrameworkSafe(proj, mainTargetGuid, "libsqlite3.tbd");
2024-11-15 16:45:53 +08:00
2026-04-08 12:49:05 +08:00
// -ObjC
AddBuildPropertySafe(proj, frameworkTarget, "OTHER_LDFLAGS", "-ObjC");
AddBuildPropertySafe(proj, mainTargetGuid, "OTHER_LDFLAGS", "-ObjC");
2024-11-15 16:45:53 +08:00
2026-04-08 12:49:05 +08:00
// libc++
AddFrameworkSafe(proj, frameworkTarget, "libc++.tbd");
AddFrameworkSafe(proj, mainTargetGuid, "libc++.tbd");
2024-11-15 15:45:11 +08:00
2026-04-08 12:49:05 +08:00
// 常见依赖
AddFrameworkSafe(proj, frameworkTarget, "CoreTelephony.framework");
AddFrameworkSafe(proj, frameworkTarget, "StoreKit.framework");
2024-11-15 16:45:53 +08:00
2026-04-08 12:49:05 +08:00
Debug.Log("ThinkingData: Config applied");
#endif
2026-04-30 17:05:20 +08:00
#if USE_OBFUZ
// Obfuscation build settings
SetBuildProperty(proj, frameworkTarget, "ENABLE_TESTABILITY", "YES");
SetBuildProperty(proj, mainTargetGuid, "ENABLE_TESTABILITY", "YES");
SetBuildProperty(proj, frameworkTarget, "COPY_PHASE_STRIP", "NO");
SetBuildProperty(proj, mainTargetGuid, "COPY_PHASE_STRIP", "NO");
SetBuildProperty(proj, frameworkTarget, "STRIP_STYLE", "all");
SetBuildProperty(proj, mainTargetGuid, "STRIP_STYLE", "all");
Debug.Log("USE_OBFUZ: Build settings applied");
#endif
2026-04-08 12:49:05 +08:00
AddCapability(proj, pathToBuiltProject);
proj.WriteToFile(projPath);
ModifyInfoPlist(pathToBuiltProject);
Debug.Log("end modify xcode project");
}
2024-11-15 12:11:44 +08:00
2026-04-08 12:49:05 +08:00
/// <summary>
/// 安全添加 Framework(防重复)
/// </summary>
/// <param name="proj"></param>
/// <param name="target"></param>
/// <param name="framework"></param>
/// <param name="weak"></param>
private static void AddFrameworkSafe(PBXProject proj, string target, string framework, bool weak = false)
{
proj.AddFrameworkToProject(target, framework, weak);
2024-11-15 12:11:44 +08:00
}
2026-04-08 12:49:05 +08:00
/// <summary>
/// 安全添加 BuildProperty(防重复)
/// </summary>
/// <param name="proj"></param>
/// <param name="target"></param>
/// <param name="key"></param>
/// <param name="value"></param>
private static void AddBuildPropertySafe(PBXProject proj, string target, string key, string value)
2024-11-15 12:11:44 +08:00
{
2026-04-08 12:49:05 +08:00
var values = proj.GetBuildPropertyForAnyConfig(target, key);
if (values == null || !values.Contains(value))
2024-11-15 12:11:44 +08:00
{
2026-04-08 12:49:05 +08:00
proj.AddBuildProperty(target, key, value);
2024-11-15 12:11:44 +08:00
}
2026-04-08 12:49:05 +08:00
}
/// <summary>
/// 设置BuildSetting参数
/// </summary>
/// <param name="proj"></param>
/// <param name="target"></param>
/// <param name="key"></param>
/// <param name="value"></param>
private static void SetBuildProperty(PBXProject proj, string target, string key, string value)
{
proj.SetBuildProperty(target, key, value);
}
private static void AddCapability(PBXProject project, string pathToBuiltProject)
{
var target = project.GetUnityMainTargetGuid();
string relativePath = "Unity-iPhone/Unity-iPhone.entitlements";
string fullPath = Path.Combine(pathToBuiltProject, relativePath);
project.AddCapability(target, PBXCapabilityType.SignInWithApple, relativePath, true);
PlistDocument entitlementDoc = new PlistDocument();
if (File.Exists(fullPath)) {
entitlementDoc.ReadFromFile(fullPath);
} else {
entitlementDoc.Create();
}
const string signKey = "com.apple.developer.applesignin";
if (entitlementDoc.root[signKey] == null) {
PlistElementArray array = entitlementDoc.root.CreateArray(signKey);
array.AddString("Default");
2024-11-15 12:11:44 +08:00
}
2026-04-08 12:49:05 +08:00
entitlementDoc.WriteToFile(fullPath);
2024-11-15 12:11:44 +08:00
}
2026-04-01 14:45:10 +08:00
2026-04-08 12:49:05 +08:00
2026-04-01 14:45:10 +08:00
/// <summary>
2026-04-08 12:49:05 +08:00
/// 修改 Info.plist
2026-04-01 14:45:10 +08:00
/// </summary>
/// <param name="pathToBuiltProject"></param>
private static void ModifyInfoPlist(string pathToBuiltProject)
{
string plistPath = pathToBuiltProject + "/Info.plist";
2026-04-08 12:49:05 +08:00
2026-04-01 14:45:10 +08:00
if (!File.Exists(plistPath))
{
2026-04-08 12:49:05 +08:00
Debug.LogError("找不到 Info.plist 文件");
2026-04-01 14:45:10 +08:00
return;
}
2026-04-08 12:49:05 +08:00
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
2026-04-10 19:28:18 +08:00
var root = plist.root;
// IDFA
root.SetString(
2026-04-08 12:49:05 +08:00
"NSUserTrackingUsageDescription",
"同意我们获取设备标识,将有助于我们数据分析,以更好地优化游戏体验"
);
2026-04-10 19:28:18 +08:00
// 相册权限 否则 ITMS-90683
root.SetString(
"NSPhotoLibraryUsageDescription",
"用于选择图片、上传头像或分享内容"
);
// 写入相册权限 和上面搭配食用
root.SetString(
"NSPhotoLibraryAddUsageDescription",
"用于将图片保存到相册"
);
2026-04-08 12:49:05 +08:00
File.WriteAllText(plistPath, plist.WriteToString());
2026-04-01 14:45:10 +08:00
}
2024-11-15 12:11:44 +08:00
#endif
2026-04-08 12:49:05 +08:00
}