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

129 lines
4.7 KiB
C#

using System;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Callbacks;
#endif
using System.IO;
#if UNITY_EDITOR && UNITY_IOS
using UnityEditor.iOS.Xcode;
#endif
public static class XCodePostProcess
{
#if UNITY_EDITOR1
[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;
}
// Create a new project object from build target
XCProject project = new XCProject( pathToBuiltProject );
// Find and run through all projmods files to patch the project.
// Please pay attention that ALL projmods files in your project folder will be excuted!
string[] files = Directory.GetFiles( Application.dataPath, "*.projmods", SearchOption.AllDirectories );
foreach( string file in files ) {
UnityEngine.Debug.Log("ProjMod File: "+file);
project.ApplyMod( file );
}
//TODO implement generic settings as a module option
project.overwriteBuildSetting("CODE_SIGN_IDENTITY[sdk=iphoneos*]", "iPhone Distribution", "Release");
// Finally save the xcode project
project.Save();
}
#endif
#if UNITY_EDITOR && UNITY_IOS
[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;
}
Debug.Log("start modify xcode project");
// Get the project file
var projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
Debug.Log("Xcode ProjPath: " + projPath);
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
var frameworkTarget = proj.GetUnityFrameworkTargetGuid();
proj.AddFrameworkToProject(frameworkTarget, "libz.tbd", false);
proj.AddFrameworkToProject(frameworkTarget, "Security.framework", false);
proj.AddFrameworkToProject(frameworkTarget, "SystemConfiguration.framework", false);
proj.AddFrameworkToProject(frameworkTarget, "MetricKit.framework", false);
proj.AddFrameworkToProject(frameworkTarget, "AuthenticationServices.framework", true);
AddCapability(proj, pathToBuiltProject);
proj.WriteToFile(projPath);
Debug.Log("end modify xcode project");
}
private static void AddCapability(PBXProject project, string pathToBuiltProject)
{
var target = project.GetUnityMainTargetGuid();
string relativeEntitlementFilePath = "Unity-iPhone/Unity-iPhone.entitlements";
string absoluteEntitlementFilePath = pathToBuiltProject + "/" + relativeEntitlementFilePath;
PlistDocument tempEntitlements = new PlistDocument();
PlistElementArray signinArr = new PlistElementArray();
signinArr.AddString("Default");
tempEntitlements.root["com.apple.developer.applesignin"] = signinArr;
project.AddCapability(target, PBXCapabilityType.SignInWithApple, relativeEntitlementFilePath, true);
var projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
Debug.Log("Absolute Entitlement File Path: " + absoluteEntitlementFilePath);
File.WriteAllText(projPath, project.WriteToString());
tempEntitlements.WriteToFile(absoluteEntitlementFilePath);
ModifyEntitlementFile(absoluteEntitlementFilePath);
}
private static void ModifyEntitlementFile(string absoluteEntitlementFilePath)
{
try
{
if (!File.Exists(absoluteEntitlementFilePath))
{
Debug.Log("Cant Find EntitlementFile :" + absoluteEntitlementFilePath);
return;
}
StreamReader reader = new StreamReader(absoluteEntitlementFilePath);
var content = reader.ReadToEnd().Trim();
reader.Close();
var needFindString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
var changeString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n" +
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">";
Debug.Log("Xcode Entitlement Before: " + content);
content = content.Replace(needFindString, changeString);
Debug.Log("Xcode Entitlement After: " + content);
StreamWriter writer = new StreamWriter(new FileStream(absoluteEntitlementFilePath, FileMode.Create));
writer.WriteLine(content);
writer.Flush();
writer.Close();
}
catch (Exception e)
{
Debug.Log("ModifyEntitlementFile - Failed: " + e.Message);
}
}
#endif
}