| 1 | /************************************************************************************ |
| 2 | Filename : OVRLipSyncBuildPostProcessor.cs |
| 3 | Content : Editor extension to generate LipSync-powered iOS apps |
| 4 | Created : Feb 11th, 2019 |
| 5 | Copyright : Copyright Facebook Technologies, LLC and its affiliates. |
| 6 | All rights reserved. |
| 7 | |
| 8 | Licensed under the Oculus Audio SDK License Version 3.3 (the "License"); |
| 9 | you may not use the Oculus Audio SDK except in compliance with the License, |
| 10 | which is provided at the time of installation or download, or which |
| 11 | otherwise accompanies this software in either electronic or hard copy form. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | |
| 15 | https://developer.oculus.com/licenses/audio-3.3/ |
| 16 | |
| 17 | Unless required by applicable law or agreed to in writing, the Oculus Audio SDK |
| 18 | distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | See the License for the specific language governing permissions and |
| 21 | limitations under the License. |
| 22 | ************************************************************************************/ |
| 23 | #if UNITY_IOS |
| 24 | using UnityEngine; |
| 25 | using UnityEditor; |
| 26 | using UnityEditor.Callbacks; |
| 27 | using UnityEditor.iOS.Xcode; |
| 28 | using System.IO; |
| 29 | using System.Text.RegularExpressions; |
| 30 | |
| 31 | class OVRLipSyncBuildPostProcessor : MonoBehaviour |
| 32 | { |
| 33 | [PostProcessBuildAttribute(1)] |
| 34 | public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) |
| 35 | { |
| 36 | if (target == BuildTarget.iOS) |
| 37 | { |
| 38 | AddMicrophoneAccess(Path.Combine(pathToBuiltProject, "Info.plist")); |
| 39 | AddEmbeddedBinary(PBXProject.GetPBXProjectPath(pathToBuiltProject)); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | private static void AddMicrophoneAccess(string infoPlistPath) |
| 44 | { |
| 45 | const string micUsageProperty = "NSMicrophoneUsageDescription"; |
| 46 | |
| 47 | var plist = new PlistDocument(); |
| 48 | plist.ReadFromFile(infoPlistPath); |
| 49 | var rootDict = plist.root.AsDict(); |
| 50 | // Don't override the description other might have edited already |
| 51 | if (rootDict.values.ContainsKey(micUsageProperty)) |
| 52 | { |
| 53 | return; |
| 54 | } |
| 55 | rootDict.SetString(micUsageProperty, "To lipsync you"); |
| 56 | plist.WriteToFile(infoPlistPath); |
| 57 | } |
| 58 | |
| 59 | private static void AddEmbeddedBinary(string projectPath) |
| 60 | { |
| 61 | const string buildPhaseName = "Embed Libraries"; |
| 62 | const string dylibName = "libOVRLipSync.dylib"; |
| 63 | |
| 64 | var project = new PBXProject(); |
| 65 | project.ReadFromFile(projectPath); |
| 66 | |
| 67 | // Don't add the same library twice |
| 68 | if (project.FindFileGuidByProjectPath(dylibName) != null) |
| 69 | { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | var targetGUID = project.TargetGuidByName(PBXProject.GetUnityTargetName()); |
| 74 | // Limit the target to ARM64 |
| 75 | project.SetBuildProperty(targetGUID, "ARCHS", "arm64"); |
| 76 | |
| 77 | // Add dylib to the project |
| 78 | var dylibGUID = project.AddFile( |
| 79 | Path.Combine(Application.dataPath, "Oculus/LipSync/Plugins/iOS/" + dylibName), |
| 80 | dylibName); |
| 81 | // Copy it to the same folder as executable |
| 82 | var embedPhaseGuid = project.AddCopyFilesBuildPhase(targetGUID, buildPhaseName, "", "6"); |
| 83 | project.AddFileToBuildSection(targetGUID, embedPhaseGuid, dylibGUID); |
| 84 | var content = project.WriteToString(); |
| 85 | |
| 86 | // Add CodeSignOnCopy attribute ot the library using an ugly regex |
| 87 | content = Regex.Replace(content, |
| 88 | "(?<="+ buildPhaseName + ")(?:.*)(\\/\\* " + Regex.Escape(dylibName) + " \\*\\/)(?=; };)", |
| 89 | m => m.Value.Replace( |
| 90 | "/* " + dylibName + " */", |
| 91 | "/* " + dylibName + " */; settings = {ATTRIBUTES = (CodeSignOnCopy, );}" |
| 92 | ) |
| 93 | ); |
| 94 | File.WriteAllText(projectPath, content); |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | |
| 99 | #endif |