Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.8 KiB

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