BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Spatializer / editor / ONSPAudioPluginUpdater.cs

ONSPAudioPluginUpdater.cs

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