BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / LipSync / Editor / OVRLipSyncBuildPostProcessor.cs

OVRLipSyncBuildPostProcessor.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRLipSyncBuildPostProcessor.cs 3.8 KiB Download Raw
1/************************************************************************************
2Filename : OVRLipSyncBuildPostProcessor.cs
3Content : Editor extension to generate LipSync-powered iOS apps
4Created : Feb 11th, 2019
5Copyright : Copyright Facebook Technologies, LLC and its affiliates.
6 All rights reserved.
7
8Licensed under the Oculus Audio SDK License Version 3.3 (the "License");
9you may not use the Oculus Audio SDK except in compliance with the License,
10which is provided at the time of installation or download, or which
11otherwise accompanies this software in either electronic or hard copy form.
12
13You may obtain a copy of the License at
14
15https://developer.oculus.com/licenses/audio-3.3/
16
17Unless required by applicable law or agreed to in writing, the Oculus Audio SDK
18distributed under the License is distributed on an "AS IS" BASIS,
19WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20See the License for the specific language governing permissions and
21limitations under the License.
22************************************************************************************/
23#if UNITY_IOS
24using UnityEngine;
25using UnityEditor;
26using UnityEditor.Callbacks;
27using UnityEditor.iOS.Xcode;
28using System.IO;
29using System.Text.RegularExpressions;
30
31class 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