MacEditorScript.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Copyright (c) 2021 Vuplex Inc. All rights reserved.
  3. *
  4. * Licensed under the Vuplex Commercial Software Library License, you may
  5. * not use this file except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * https://vuplex.com/commercial-library-license
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #if UNITY_EDITOR_OSX
  17. using System.Diagnostics;
  18. using System.IO;
  19. using UnityEditor;
  20. namespace Vuplex.WebView {
  21. /// <summary>
  22. /// Mac editor script that modifies the VuplexWebViewMac
  23. /// native plugin downloaded from the Asset Store to allow it to run.
  24. /// </summary>
  25. [InitializeOnLoad]
  26. class MacEditorScript {
  27. static MacEditorScript() {
  28. var macPluginPath = EditorUtils.FindDirectory("Assets/Vuplex/WebView/Plugins/Mac/VuplexWebViewMac.bundle");
  29. // This removes the com.apple.quarantine attribute that
  30. // macOS adds to the VuplexWebViewMac.bundle plugin when it's downloaded
  31. // from the internet, which prevents the Unity editor from loading it.
  32. //
  33. // This is currently needed in order for the Unity
  34. // editor to load 3rd party plugins, because the Unity editor app doesn't
  35. // yet include the com.apple.security.cs.disable-library-validation
  36. // entitlement that's required to be able to load 3rd party bundles
  37. // with macOS 10.15 Catalina's hardened runtime. Without the
  38. // com.apple.security.cs.disable-library-validation entitlement,
  39. // the hardened runtime's library validation prevents an app from
  40. // loading libraries unless they’re either signed by Apple or signed with the
  41. // same team ID as the app (i.e. Unity's team ID in this case).
  42. //
  43. // References:
  44. // • https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_cs_disable-library-validation
  45. // • https://bugzilla.mozilla.org/show_bug.cgi?id=1570840
  46. // • https://bugzilla.mozilla.org/show_bug.cgi?id=1470597
  47. _executeBashCommand("xattr -d com.apple.quarantine \"" + macPluginPath + "\"");
  48. // When Unity's Asset Store tools compress and decompress the asset, the executable permission
  49. // gets stripped off of the plugin's executables. So, we add the executable permission back
  50. // on here using chmod.
  51. var binaryRelativePaths = new string[] {
  52. "Contents/Frameworks/Vuplex WebView.app/Contents/MacOS/Vuplex WebView",
  53. "Contents/Frameworks/Vuplex WebView.app/Contents/Frameworks/Vuplex WebView Helper.app/Contents/MacOS/Vuplex WebView Helper",
  54. "Contents/Frameworks/Vuplex WebView.app/Contents/Frameworks/Vuplex WebView Helper (GPU).app/Contents/MacOS/Vuplex WebView Helper (GPU)",
  55. "Contents/Frameworks/Vuplex WebView.app/Contents/Frameworks/Vuplex WebView Helper (Plugin).app/Contents/MacOS/Vuplex WebView Helper (Plugin)",
  56. "Contents/Frameworks/Vuplex WebView.app/Contents/Frameworks/Vuplex WebView Helper (Renderer).app/Contents/MacOS/Vuplex WebView Helper (Renderer)"
  57. };
  58. foreach (var relativePath in binaryRelativePaths) {
  59. var path = Path.Combine(macPluginPath, relativePath);
  60. _chmod("755 \"" + path + "\"");
  61. }
  62. }
  63. static string _executeBashCommand(string command) {
  64. // Escape quotes
  65. command = command.Replace("\"","\"\"");
  66. var proc = new Process {
  67. StartInfo = new ProcessStartInfo {
  68. FileName = "/bin/bash",
  69. Arguments = "-c \""+ command + "\"",
  70. UseShellExecute = false,
  71. RedirectStandardOutput = true,
  72. CreateNoWindow = true
  73. }
  74. };
  75. proc.Start();
  76. proc.WaitForExit();
  77. return proc.StandardOutput.ReadToEnd();
  78. }
  79. static string _chmod(string arguments) {
  80. var proc = new Process {
  81. StartInfo = new ProcessStartInfo {
  82. FileName = "/bin/chmod",
  83. Arguments = arguments,
  84. UseShellExecute = false,
  85. RedirectStandardOutput = true,
  86. CreateNoWindow = true
  87. }
  88. };
  89. proc.Start();
  90. proc.WaitForExit();
  91. return proc.StandardOutput.ReadToEnd();
  92. }
  93. }
  94. }
  95. #endif