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.

329 lines
13 KiB

  1. /************************************************************************************
  2. Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. Licensed under the Oculus SDK License Version 3.4.1 (the "License");
  4. you may not use the Oculus SDK except in compliance with the License,
  5. which is provided at the time of installation or download, or which
  6. otherwise accompanies this software in either electronic or hard copy form.
  7. You may obtain a copy of the License at
  8. https://developer.oculus.com/licenses/sdk-3.4.1
  9. Unless required by applicable law or agreed to in writing, the Oculus SDK
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ************************************************************************************/
  15. using UnityEngine;
  16. using UnityEditor;
  17. using UnityEditor.Callbacks;
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Text.RegularExpressions;
  22. using System.IO;
  23. using System.Diagnostics;
  24. [InitializeOnLoad]
  25. class ONSPAudioPluginUpdater
  26. {
  27. private static bool restartPending = false;
  28. private static bool unityRunningInBatchmode = false;
  29. private static System.Version invalidVersion = new System.Version("0.0.0");
  30. static ONSPAudioPluginUpdater()
  31. {
  32. EditorApplication.delayCall += OnDelayCall;
  33. }
  34. static void OnDelayCall()
  35. {
  36. if (System.Environment.CommandLine.Contains("-batchmode"))
  37. {
  38. unityRunningInBatchmode = true;
  39. }
  40. if (ShouldAttemptPluginUpdate())
  41. {
  42. AttemptSpatializerPluginUpdate(true);
  43. }
  44. }
  45. private static string GetCurrentProjectPath()
  46. {
  47. return Directory.GetParent(Application.dataPath).FullName;
  48. }
  49. private static string GetUtilitiesRootPath()
  50. {
  51. var so = ScriptableObject.CreateInstance(typeof(ONSPAudioPluginUpdaterStub));
  52. var script = MonoScript.FromScriptableObject(so);
  53. string assetPath = AssetDatabase.GetAssetPath(script);
  54. string editorDir = Directory.GetParent(assetPath).FullName;
  55. string ovrDir = Directory.GetParent(editorDir).FullName;
  56. return ovrDir;
  57. }
  58. public static string GetVersionDescription(System.Version version)
  59. {
  60. bool isVersionValid = (version != invalidVersion);
  61. return isVersionValid ? version.ToString() : "(Unknown)";
  62. }
  63. private static bool ShouldAttemptPluginUpdate()
  64. {
  65. if (unityRunningInBatchmode)
  66. {
  67. return false;
  68. }
  69. else
  70. {
  71. return (autoUpdateEnabled && !restartPending && !Application.isPlaying);
  72. }
  73. }
  74. private static readonly string autoUpdateEnabledKey = "Oculus_Utilities_ONSPAudioPluginUpdater_AutoUpdate_" + 1.0;//PASOVRManager.utilitiesVersion;
  75. private static bool autoUpdateEnabled
  76. {
  77. get
  78. {
  79. return PlayerPrefs.GetInt(autoUpdateEnabledKey, 1) == 1;
  80. }
  81. set
  82. {
  83. PlayerPrefs.SetInt(autoUpdateEnabledKey, value ? 1 : 0);
  84. }
  85. }
  86. [MenuItem("Oculus/Tools/Update Spatializer Plugin")]
  87. private static void RunSpatializerPluginUpdate()
  88. {
  89. autoUpdateEnabled = true;
  90. AttemptSpatializerPluginUpdate(false);
  91. }
  92. // Separate entry point needed since "-executeMethod" does not support parameters or default parameter values
  93. private static void BatchmodePluginUpdate()
  94. {
  95. OnDelayCall(); // manually invoke when running editor in batchmode
  96. AttemptSpatializerPluginUpdate(false);
  97. }
  98. private static string GetSpatializerPluginsRootPath()
  99. {
  100. string ovrPath = GetUtilitiesRootPath();
  101. string spatializerPluginsPath = Path.GetFullPath(Path.Combine(ovrPath, "../Spatializer/Plugins"));
  102. return spatializerPluginsPath;
  103. }
  104. private static bool RenameSpatializerPluginToOld(string currentPluginPath)
  105. {
  106. if (File.Exists(currentPluginPath))
  107. {
  108. int index = 0;
  109. string targetPluginPath;
  110. string targetPluginMetaPath;
  111. for (; ; )
  112. {
  113. targetPluginPath = currentPluginPath + ".old" + index.ToString();
  114. targetPluginMetaPath = targetPluginPath + ".meta";
  115. if (!File.Exists(targetPluginPath) && !File.Exists(targetPluginPath))
  116. break;
  117. ++index;
  118. }
  119. try
  120. {
  121. File.Move(currentPluginPath, targetPluginPath);
  122. File.Move(currentPluginPath + ".meta", targetPluginMetaPath);
  123. UnityEngine.Debug.LogFormat("Spatializer plugin renamed: {0} to {1}", currentPluginPath, targetPluginPath);
  124. return true;
  125. }
  126. catch (Exception e)
  127. {
  128. UnityEngine.Debug.LogWarningFormat("Unable to rename spatializer plugin: {0}, exception {1}", currentPluginPath, e.Message);
  129. return false;
  130. }
  131. }
  132. return false;
  133. }
  134. private static void AttemptSpatializerPluginUpdate(bool triggeredByAutoUpdate)
  135. {
  136. // We use a simplified path to update spatializer plugins:
  137. // If there is a new AudioPluginOculusSpatializer.dll.new, we'll rename the original one to .old, and the new one to .dll, and restart the editor
  138. string pluginsPath = GetSpatializerPluginsRootPath();
  139. string newX86PluginPath = Path.GetFullPath(Path.Combine(pluginsPath, "x86/AudioPluginOculusSpatializer.dll.new"));
  140. string newX64PluginPath = Path.GetFullPath(Path.Combine(pluginsPath, "x86_64/AudioPluginOculusSpatializer.dll.new"));
  141. if (File.Exists(newX86PluginPath) || File.Exists(newX64PluginPath))
  142. {
  143. bool userAcceptsUpdate = false;
  144. if (unityRunningInBatchmode)
  145. {
  146. userAcceptsUpdate = true;
  147. }
  148. else
  149. {
  150. int dialogResult = EditorUtility.DisplayDialogComplex("Update Spatializer Plugins",
  151. "New spatializer plugin found. Do you want to upgrade? If you choose 'Upgrade', the old plugin will be renamed to AudioPluginOculusSpatializer.old",
  152. "Upgrade", "Don't upgrade", "Delete new plugin");
  153. if (dialogResult == 0)
  154. {
  155. userAcceptsUpdate = true;
  156. }
  157. else if (dialogResult == 1)
  158. {
  159. // do nothing
  160. }
  161. else if (dialogResult == 2)
  162. {
  163. try
  164. {
  165. File.Delete(newX86PluginPath);
  166. File.Delete(newX86PluginPath + ".meta");
  167. File.Delete(newX64PluginPath);
  168. File.Delete(newX64PluginPath + ".meta");
  169. }
  170. catch (Exception e)
  171. {
  172. UnityEngine.Debug.LogWarning("Exception happened when deleting new spatializer plugin: " + e.Message);
  173. }
  174. }
  175. }
  176. if (userAcceptsUpdate)
  177. {
  178. bool upgradeDone = false;
  179. string curX86PluginPath = Path.Combine(pluginsPath, "x86/AudioPluginOculusSpatializer.dll");
  180. if (File.Exists(newX86PluginPath))
  181. {
  182. RenameSpatializerPluginToOld(curX86PluginPath);
  183. try
  184. {
  185. File.Move(newX86PluginPath, curX86PluginPath);
  186. File.Move(newX86PluginPath + ".meta", curX86PluginPath + ".meta");
  187. // fix the platform
  188. string curX86PluginPathRel = "Assets/Oculus/Spatializer/Plugins/x86/AudioPluginOculusSpatializer.dll";
  189. UnityEngine.Debug.Log("path = " + curX86PluginPathRel);
  190. AssetDatabase.ImportAsset(curX86PluginPathRel, ImportAssetOptions.ForceUpdate);
  191. PluginImporter pi = PluginImporter.GetAtPath(curX86PluginPathRel) as PluginImporter;
  192. pi.SetCompatibleWithEditor(false);
  193. pi.SetCompatibleWithAnyPlatform(false);
  194. pi.SetCompatibleWithPlatform(BuildTarget.Android, false);
  195. pi.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows64, false);
  196. #if UNITY_2017_3_OR_NEWER
  197. pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSX, false);
  198. #else
  199. pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXUniversal, false);
  200. pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel, false);
  201. pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel64, false);
  202. #endif
  203. pi.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows, true);
  204. pi.SetCompatibleWithEditor(true);
  205. pi.SetEditorData("CPU", "X86");
  206. pi.SetEditorData("OS", "Windows");
  207. pi.SetPlatformData("Editor", "CPU", "X86");
  208. pi.SetPlatformData("Editor", "OS", "Windows");
  209. AssetDatabase.ImportAsset(curX86PluginPathRel, ImportAssetOptions.ForceUpdate);
  210. AssetDatabase.Refresh();
  211. AssetDatabase.SaveAssets();
  212. upgradeDone = true;
  213. }
  214. catch (Exception e)
  215. {
  216. UnityEngine.Debug.LogWarning("Unable to rename the new spatializer plugin: " + e.Message);
  217. }
  218. }
  219. string curX64PluginPath = Path.Combine(pluginsPath, "x86_64/AudioPluginOculusSpatializer.dll");
  220. if (File.Exists(newX64PluginPath))
  221. {
  222. RenameSpatializerPluginToOld(curX64PluginPath);
  223. try
  224. {
  225. File.Move(newX64PluginPath, curX64PluginPath);
  226. File.Move(newX64PluginPath + ".meta", curX64PluginPath + ".meta");
  227. // fix the platform
  228. string curX64PluginPathRel = "Assets/Oculus/Spatializer/Plugins/x86_64/AudioPluginOculusSpatializer.dll";
  229. UnityEngine.Debug.Log("path = " + curX64PluginPathRel);
  230. AssetDatabase.ImportAsset(curX64PluginPathRel, ImportAssetOptions.ForceUpdate);
  231. PluginImporter pi = PluginImporter.GetAtPath(curX64PluginPathRel) as PluginImporter;
  232. pi.SetCompatibleWithEditor(false);
  233. pi.SetCompatibleWithAnyPlatform(false);
  234. pi.SetCompatibleWithPlatform(BuildTarget.Android, false);
  235. pi.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows, false);
  236. #if UNITY_2017_3_OR_NEWER
  237. pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSX, false);
  238. #else
  239. pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXUniversal, false);
  240. pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel, false);
  241. pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel64, false);
  242. #endif
  243. pi.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows64, true);
  244. pi.SetCompatibleWithEditor(true);
  245. pi.SetEditorData("CPU", "X86_64");
  246. pi.SetEditorData("OS", "Windows");
  247. pi.SetPlatformData("Editor", "CPU", "X86_64");
  248. pi.SetPlatformData("Editor", "OS", "Windows");
  249. AssetDatabase.ImportAsset(curX64PluginPathRel, ImportAssetOptions.ForceUpdate);
  250. AssetDatabase.Refresh();
  251. AssetDatabase.SaveAssets();
  252. upgradeDone = true;
  253. }
  254. catch (Exception e)
  255. {
  256. UnityEngine.Debug.LogWarning("Unable to rename the new spatializer plugin: " + e.Message);
  257. }
  258. }
  259. if (upgradeDone)
  260. {
  261. if (unityRunningInBatchmode
  262. || EditorUtility.DisplayDialog("Restart Unity",
  263. "Spatializer plugins has been upgraded."
  264. + "\n\nPlease restart the Unity Editor to complete the update process."
  265. #if !UNITY_2017_1_OR_NEWER
  266. + " You may need to manually relaunch Unity if you are using Unity 5.6 and higher."
  267. #endif
  268. ,
  269. "Restart",
  270. "Not Now"))
  271. {
  272. RestartUnityEditor();
  273. }
  274. }
  275. }
  276. }
  277. else
  278. {
  279. UnityEngine.Debug.Log("No new spatializer plugin(s) found");
  280. }
  281. }
  282. private static void RestartUnityEditor()
  283. {
  284. if (unityRunningInBatchmode)
  285. {
  286. EditorApplication.Exit(0);
  287. }
  288. else
  289. {
  290. restartPending = true;
  291. EditorApplication.OpenProject(GetCurrentProjectPath());
  292. }
  293. }
  294. }