SamsonGame/Assets/Editor/IOSPostprocess/IOSCredentialsPostProcess.cs

94 lines
3.2 KiB
C#
Raw Normal View History

2021-12-29 20:50:11 +03:00
#if UNITY_IOS
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using UnityEngine;
public class IOSCredentialsPostProcess : ScriptableObject
{
public IOSCredentialsList ConfigurationsList;
[PostProcessBuild(0)]
public static void OnPostProcess(BuildTarget buildTarget, string buildPath)
{
if(buildTarget != BuildTarget.iOS)
return;
// var config = PickEntitlementsConfig();
// if(config == null)
// return;
//
// var entitlements_path = AssetDatabase.GetAssetPath(config.Entitlements);
// var proj_path = PBXProject.GetPBXProjectPath(buildPath);
// var proj = new PBXProject();
// proj.ReadFromFile(proj_path);
//
// var target_name = "Unity-iPhone";
// // var target_name = PBXProject.GetUnityTargetName();
// var target_guid = proj.TargetGuidByName(target_name);
// var file_name = Path.GetFileName(entitlements_path);
// var dst = $"{buildPath}/{target_name}/{file_name}";
// FileUtil.CopyFileOrDirectory(entitlements_path, dst);
// proj.AddFile($"{target_name}/{file_name}", file_name);
// proj.AddBuildProperty(target_guid, "CODE_SIGN_ENTITLEMENTS", $"{target_name}/{file_name}");
// AddLocalFile(buildPath, proj, target_guid, "Assets", "GoogleService-Info.plist");
// proj.WriteToFile(proj_path);
}
public static void ReplaceFirebasePlistAccordingToBundleID()
{
ReplaceFirebasePlist(PickEntitlementsConfig());
}
static void ReplaceFirebasePlist(IOSCredentialsList.Configuration config)
{
var firebase_plist_path = AssetDatabase.GetAssetPath(config.FirebasePlist);
var firebase_expected_path = $"{Application.dataPath}/GoogleService-Info.plist";
File.Copy(firebase_plist_path, firebase_expected_path, overwrite: true);
}
static void AddLocalFile(string buildPath, PBXProject proj, string targetName, string fileDir, string fileName)
{
var filePath = Path.Combine(fileDir, fileName);
File.Copy(filePath, Path.Combine(buildPath, fileName));
proj.AddFileToBuild(targetName, proj.AddFile(fileName, fileName));
}
static IOSCredentialsList.Configuration PickEntitlementsConfig()
{
var dummy = ScriptableObject.CreateInstance<IOSCredentialsPostProcess>();
try
{
string app_id = $"{Settings.IOS_TEAM_ID}.{Settings.PACKAGE_ID_FULL}";
string search_term = $"<string>{app_id}</string>";
var options = dummy.ConfigurationsList.Configurations;
for(int i = 0; i < options.Count; i++)
{
var option = options[i];
if(option == null)
continue;
var path = AssetDatabase.GetAssetPath(option.Entitlements);
string contents = File.ReadAllText(path);
if(contents.Contains(search_term))
{
Debug.Log($"Using iOS entitlements file {path} and Firebase plist {option.FirebasePlist.name}");
return option;
}
}
throw new Exception($"None of listed entitlements match current App Id '{app_id}'!");
}
finally
{
ScriptableObject.DestroyImmediate(dummy);
}
}
}
#endif