| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- /************************************************************************************
- Copyright : Copyright 2017 Oculus VR, LLC. All Rights reserved.
- Licensed under the Oculus VR Rift SDK License Version 3.4.1 (the "License");
- you may not use the Oculus VR Rift SDK except in compliance with the License,
- which is provided at the time of installation or download, or which
- otherwise accompanies this software in either electronic or hard copy form.
- You may obtain a copy of the License at
- https://developer.oculus.com/licenses/sdk-3.4.1
- Unless required by applicable law or agreed to in writing, the Oculus VR SDK
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ************************************************************************************/
- using UnityEngine;
- using UnityEditor;
- using UnityEditor.Callbacks;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.IO;
- using System.Diagnostics;
- [InitializeOnLoad]
- class OVRPluginUpdater
- {
- class PluginPackage
- {
- public string RootPath;
- public System.Version Version;
- public Dictionary<BuildTarget, string> Plugins = new Dictionary<BuildTarget, string>();
- public bool IsBundledPluginPackage()
- {
- return (RootPath == GetBundledPluginRootPath());
- }
- public bool IsEnabled()
- {
- // TODO: Check each individual platform rather than using the Win64 DLL status for the overall package status.
- string win64PluginPath = "";
- if (Plugins.TryGetValue(BuildTarget.StandaloneWindows64, out win64PluginPath))
- {
- return File.Exists(win64PluginPath);
- }
- return false;
- }
- }
- static OVRPluginUpdater()
- {
- EditorApplication.delayCall += OnDelayCall;
- }
- static void OnDelayCall()
- {
- if (ShouldAttemptPluginUpdate())
- {
- AttemptPluginUpdate(true);
- }
- }
- private static PluginPackage GetPluginPackage(string rootPath)
- {
- return new PluginPackage()
- {
- RootPath = rootPath,
- Version = GetPluginVersion(rootPath),
- Plugins = new Dictionary<BuildTarget, string>()
- {
- { BuildTarget.Android, rootPath + GetPluginBuildTargetSubPath(BuildTarget.Android) },
- #if UNITY_2017_3_OR_NEWER
- { BuildTarget.StandaloneOSX, rootPath + GetPluginBuildTargetSubPath(BuildTarget.StandaloneOSX) },
- #else
- { BuildTarget.StandaloneOSXUniversal, rootPath + GetPluginBuildTargetSubPath(BuildTarget.StandaloneOSXUniversal) },
- #endif
- { BuildTarget.StandaloneWindows, rootPath + GetPluginBuildTargetSubPath(BuildTarget.StandaloneWindows) },
- { BuildTarget.StandaloneWindows64, rootPath + GetPluginBuildTargetSubPath(BuildTarget.StandaloneWindows64) },
- }
- };
- }
- private static PluginPackage GetBundledPluginPackage()
- {
- return GetPluginPackage(GetBundledPluginRootPath());
- }
- private static List<PluginPackage> GetAllUtilitiesPluginPackages()
- {
- string pluginRootPath = GetUtilitiesPluginRootPath();
- List<PluginPackage> packages = new List<PluginPackage>();
- if (Directory.Exists(pluginRootPath))
- {
- var dirs = Directory.GetDirectories(pluginRootPath);
- foreach(string dir in dirs)
- {
- packages.Add(GetPluginPackage(dir));
- }
- }
- return packages;
- }
- private static string GetCurrentProjectPath()
- {
- return Directory.GetParent(Application.dataPath).FullName;
- }
- private static string GetUtilitiesPluginRootPath()
- {
- return GetUtilitiesRootPath() + @"/Plugins";
- }
- private static string GetUtilitiesRootPath()
- {
- var so = ScriptableObject.CreateInstance(typeof(OVRPluginUpdaterStub));
- var script = MonoScript.FromScriptableObject(so);
- string assetPath = AssetDatabase.GetAssetPath(script);
- string editorDir = Directory.GetParent(assetPath).FullName;
- string ovrDir = Directory.GetParent(editorDir).FullName;
- return ovrDir;
- }
- private static string GetBundledPluginRootPath()
- {
- string basePath = EditorApplication.applicationContentsPath;
- string pluginPath = @"/UnityExtensions/Unity/VR";
- return basePath + pluginPath;
- }
- private static string GetPluginBuildTargetSubPath(BuildTarget target)
- {
- string path = string.Empty;
- switch (target)
- {
- case BuildTarget.Android:
- path = @"/Android/OVRPlugin.aar";
- break;
- #if UNITY_2017_3_OR_NEWER
- case BuildTarget.StandaloneOSX:
- #else
- case BuildTarget.StandaloneOSXUniversal:
- #endif
- path = @"/OSXUniversal/OVRPlugin.bundle";
- break;
- case BuildTarget.StandaloneWindows:
- path = @"/Win/OVRPlugin.dll";
- break;
- case BuildTarget.StandaloneWindows64:
- path = @"/Win64/OVRPlugin.dll";
- break;
- default:
- throw new ArgumentException("Attempted GetPluginBuildTargetSubPath() for unsupported BuildTarget: " + target);
- }
- return path;
- }
- private static string GetDisabledPluginSuffix()
- {
- return @".disabled";
- }
- private static System.Version GetPluginVersion(string path)
- {
- System.Version invalidVersion = new System.Version("0.0.0");
- System.Version pluginVersion = invalidVersion;
- try
- {
- pluginVersion = new System.Version(Path.GetFileName(path));
- }
- catch
- {
- pluginVersion = invalidVersion;
- }
- if (pluginVersion == invalidVersion)
- {
- //Unable to determine version from path, fallback to Win64 DLL meta data
- path += GetPluginBuildTargetSubPath(BuildTarget.StandaloneWindows64);
- if (!File.Exists(path))
- {
- path += GetDisabledPluginSuffix();
- if (!File.Exists(path))
- {
- return invalidVersion;
- }
- }
- FileVersionInfo pluginVersionInfo = FileVersionInfo.GetVersionInfo(path);
- if (pluginVersionInfo == null || pluginVersionInfo.ProductVersion == null || pluginVersionInfo.ProductVersion == "")
- {
- return invalidVersion;
- }
- pluginVersion = new System.Version(pluginVersionInfo.ProductVersion);
- }
- return pluginVersion;
- }
-
- private static bool ShouldAttemptPluginUpdate()
- {
- return autoUpdateEnabled && !restartPending && !Application.isPlaying;
- }
- private static void DisableAllUtilitiesPluginPackages()
- {
- List<PluginPackage> allUtilsPluginPkgs = GetAllUtilitiesPluginPackages();
- foreach(PluginPackage pluginPkg in allUtilsPluginPkgs)
- {
- foreach(string path in pluginPkg.Plugins.Values)
- {
- if ((Directory.Exists(path)) || (File.Exists(path)))
- {
- string basePath = GetCurrentProjectPath();
- string relPath = path.Substring(basePath.Length + 1);
- AssetDatabase.MoveAsset(relPath, relPath + GetDisabledPluginSuffix());
- AssetDatabase.ImportAsset(relPath + GetDisabledPluginSuffix(), ImportAssetOptions.ForceUpdate);
- }
- }
- }
- AssetDatabase.Refresh();
- AssetDatabase.SaveAssets();
- }
- private static void EnablePluginPackage(PluginPackage pluginPkg)
- {
- foreach(var kvp in pluginPkg.Plugins)
- {
- BuildTarget platform = kvp.Key;
- string path = kvp.Value;
- if ((Directory.Exists(path + GetDisabledPluginSuffix())) || (File.Exists(path + GetDisabledPluginSuffix())))
- {
- string basePath = GetCurrentProjectPath();
- string relPath = path.Substring(basePath.Length + 1);
- AssetDatabase.MoveAsset(relPath + GetDisabledPluginSuffix(), relPath);
- AssetDatabase.ImportAsset(relPath, ImportAssetOptions.ForceUpdate);
- PluginImporter pi = PluginImporter.GetAtPath(relPath) as PluginImporter;
- if (pi == null)
- {
- continue;
- }
- pi.SetCompatibleWithAnyPlatform(false);
- switch (platform)
- {
- case BuildTarget.Android:
- pi.SetCompatibleWithPlatform(BuildTarget.Android, true);
- pi.SetPlatformData(BuildTarget.Android, "CPU", "ARMv7");
- #if UNITY_2017_3_OR_NEWER
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSX, false);
- #else
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXUniversal, false);
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel, false);
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel64, false);
- #endif
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows, false);
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows64, false);
- pi.SetCompatibleWithEditor(false);
- break;
- #if UNITY_2017_3_OR_NEWER
- case BuildTarget.StandaloneOSX:
- #else
- case BuildTarget.StandaloneOSXUniversal:
- #endif
- pi.SetCompatibleWithPlatform(BuildTarget.Android, false);
- pi.SetPlatformData(BuildTarget.Android, "CPU", "ARMv7");
- #if UNITY_2017_3_OR_NEWER
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSX, true);
- #else
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXUniversal, true);
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel, true);
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel64, true);
- #endif
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows, false);
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows64, false);
- pi.SetCompatibleWithEditor(true);
- pi.SetEditorData("CPU", "AnyCPU");
- pi.SetEditorData("OS", "OSX");
- pi.SetPlatformData("Editor", "CPU", "AnyCPU");
- pi.SetPlatformData("Editor", "OS", "OSX");
- break;
- case BuildTarget.StandaloneWindows:
- pi.SetCompatibleWithPlatform(BuildTarget.Android, false);
- pi.SetPlatformData(BuildTarget.Android, "CPU", "ARMv7");
- #if UNITY_2017_3_OR_NEWER
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSX, false);
- #else
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXUniversal, false);
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel, false);
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel64, false);
- #endif
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows, true);
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows64, false);
- pi.SetCompatibleWithEditor(true);
- pi.SetEditorData("CPU", "X86");
- pi.SetEditorData("OS", "Windows");
- pi.SetPlatformData("Editor", "CPU", "X86");
- pi.SetPlatformData("Editor", "OS", "Windows");
- break;
- case BuildTarget.StandaloneWindows64:
- pi.SetCompatibleWithPlatform(BuildTarget.Android, false);
- pi.SetPlatformData(BuildTarget.Android, "CPU", "ARMv7");
- #if UNITY_2017_3_OR_NEWER
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSX, false);
- #else
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXUniversal, false);
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel, false);
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel64, false);
- #endif
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows, false);
- pi.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows64, true);
- pi.SetCompatibleWithEditor(true);
- pi.SetEditorData("CPU", "X86_64");
- pi.SetEditorData("OS", "Windows");
- pi.SetPlatformData("Editor", "CPU", "X86_64");
- pi.SetPlatformData("Editor", "OS", "Windows");
- break;
- default:
- throw new ArgumentException("Attempted EnablePluginPackage() for unsupported BuildTarget: " + platform);
- }
- AssetDatabase.ImportAsset(relPath, ImportAssetOptions.ForceUpdate);
- }
- }
- AssetDatabase.Refresh();
- AssetDatabase.SaveAssets();
- }
- private static bool restartPending = false;
- private static readonly string autoUpdateEnabledKey = "Oculus_Utilities_OVRPluginUpdater_AutoUpdate_" + OVRManager.utilitiesVersion;
- private static bool autoUpdateEnabled
- {
- get {
- return PlayerPrefs.GetInt(autoUpdateEnabledKey, 1) == 1;
- }
- set {
- PlayerPrefs.SetInt(autoUpdateEnabledKey, value ? 1 : 0);
- }
- }
- [MenuItem("Tools/Oculus/Disable OVR Utilities Plugin")]
- private static void AttemptPluginDisable()
- {
- PluginPackage bundledPluginPkg = GetBundledPluginPackage();
- List<PluginPackage> allUtilsPluginPkgs = GetAllUtilitiesPluginPackages();
- PluginPackage enabledUtilsPluginPkg = null;
- foreach(PluginPackage pluginPkg in allUtilsPluginPkgs)
- {
- if (pluginPkg.IsEnabled())
- {
- if ((enabledUtilsPluginPkg == null) || (pluginPkg.Version > enabledUtilsPluginPkg.Version))
- {
- enabledUtilsPluginPkg = pluginPkg;
- }
- }
- }
- if (enabledUtilsPluginPkg == null)
- {
- if (EditorUtility.DisplayDialog("Disable Oculus Utilities Plugin", "The OVRPlugin included with Oculus Utilities is already disabled. The OVRPlugin bundled with the Unity Editor will continue to be used.\n\nBundled version: " + bundledPluginPkg.Version, "Ok", ""))
- {
- return;
- }
- }
- else
- {
- if (EditorUtility.DisplayDialog("Disable Oculus Utilities Plugin", "Do you want to disable the OVRPlugin included with Oculus Utilities and revert to the OVRPlugin bundled with the Unity Editor?\n\nCurrent version: " + enabledUtilsPluginPkg.Version + "\nBundled version: " + bundledPluginPkg.Version, "Yes", "No"))
- {
- DisableAllUtilitiesPluginPackages();
- if (EditorUtility.DisplayDialog("Restart Unity", "OVRPlugin has been updated to " + bundledPluginPkg.Version + ".\n\nPlease restart the Unity Editor to complete the update process. You may need to manually relaunch Unity if you are using Unity 5.6 and higher.", "Restart", "Not Now"))
- {
- RestartUnityEditor();
- }
- }
- }
- }
- [MenuItem("Tools/Oculus/Update OVR Utilities Plugin")]
- private static void RunPluginUpdate()
- {
- AttemptPluginUpdate(false);
- }
- private static void AttemptPluginUpdate(bool triggeredByAutoUpdate)
- {
- autoUpdateEnabled = true;
- PluginPackage bundledPluginPkg = GetBundledPluginPackage();
- List<PluginPackage> allUtilsPluginPkgs = GetAllUtilitiesPluginPackages();
- PluginPackage enabledUtilsPluginPkg = null;
- PluginPackage newestUtilsPluginPkg = null;
- foreach(PluginPackage pluginPkg in allUtilsPluginPkgs)
- {
- if ((newestUtilsPluginPkg == null) || (pluginPkg.Version > newestUtilsPluginPkg.Version))
- {
- newestUtilsPluginPkg = pluginPkg;
- }
- if (pluginPkg.IsEnabled())
- {
- if ((enabledUtilsPluginPkg == null) || (pluginPkg.Version > enabledUtilsPluginPkg.Version))
- {
- enabledUtilsPluginPkg = pluginPkg;
- }
- }
- }
- PluginPackage targetPluginPkg = null;
- if ((newestUtilsPluginPkg != null) && (newestUtilsPluginPkg.Version > bundledPluginPkg.Version))
- {
- if ((enabledUtilsPluginPkg == null) || (enabledUtilsPluginPkg.Version != newestUtilsPluginPkg.Version))
- {
- targetPluginPkg = newestUtilsPluginPkg;
- }
- }
- else if ((enabledUtilsPluginPkg != null) && (enabledUtilsPluginPkg.Version < bundledPluginPkg.Version))
- {
- targetPluginPkg = bundledPluginPkg;
- }
-
- System.Version currentVersion = (enabledUtilsPluginPkg != null) ? enabledUtilsPluginPkg.Version : bundledPluginPkg.Version;
- if (targetPluginPkg == null)
- {
- if (!triggeredByAutoUpdate)
- {
- EditorUtility.DisplayDialog("Update Oculus Utilities Plugin", "OVRPlugin is already up to date.\n\nCurrent version: " + currentVersion + "\nBundled version: " + bundledPluginPkg.Version, "Ok", "");
- }
- return; // No update necessary.
- }
- System.Version targetVersion = targetPluginPkg.Version;
- int dialogResult = EditorUtility.DisplayDialogComplex("Update Oculus Utilities Plugin", "Oculus Utilities has detected that a newer OVRPlugin is available. Using the newest version is recommended. Do you want to enable it?\n\nCurrent version: " + currentVersion + "\nAvailable version: " + targetVersion, "Yes", "No, Don't Ask Again", "No");
- bool userAcceptsUpdate = false;
- switch (dialogResult)
- {
- case 0: // "Yes"
- userAcceptsUpdate = true;
- break;
- case 1: // "No, Don't Ask Again"
- autoUpdateEnabled = false;
- EditorUtility.DisplayDialog("Oculus Utilities OVRPlugin", "To manually update in the future, use the following menu option:\n\n[Tools -> Oculus -> Update OVR Utilities Plugin]", "Ok", "");
- return;
- case 2: // "No"
- return;
- }
- if (userAcceptsUpdate)
- {
- DisableAllUtilitiesPluginPackages();
- if (!targetPluginPkg.IsBundledPluginPackage())
- {
- EnablePluginPackage(targetPluginPkg);
- }
- if (EditorUtility.DisplayDialog("Restart Unity", "OVRPlugin has been updated to " + targetPluginPkg.Version + ".\n\nPlease restart the Unity Editor to complete the update process. You may need to manually relaunch Unity if you are using Unity 5.6 and higher.", "Restart", "Not Now"))
- {
- RestartUnityEditor();
- }
- }
- }
- private static void RestartUnityEditor()
- {
- restartPending = true;
- EditorApplication.OpenProject(GetCurrentProjectPath());
- }
- }
|