XRLegacyUninstaller.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEditor.PackageManager;
  6. using UnityEditor.PackageManager.Requests;
  7. using UnityEditor.XR.Management;
  8. using UnityEngine;
  9. using UnityEngine.XR;
  10. using UnityEngine.XR.Management;
  11. namespace UnityEditor.XR.Management.Legacy
  12. {
  13. [InitializeOnLoad]
  14. class XRLegacyUninstaller
  15. {
  16. private static List<string> k_PackagesToBeRemoved = new List<string>(){
  17. "com.unity.xr.googlevr.android",
  18. "com.unity.xr.googlevr.ios",
  19. "com.unity.xr.oculus.android",
  20. "com.unity.xr.oculus.standalone",
  21. "com.unity.xr.openvr.standalone",
  22. "com.unity.xr.windowsmr.metro",
  23. };
  24. [Serializable]
  25. struct PackageRemovalRequest
  26. {
  27. [SerializeField]
  28. public string packageId;
  29. [SerializeField]
  30. public bool shouldCheckForInstallation;
  31. }
  32. [Serializable]
  33. struct PackageRemovalOperation
  34. {
  35. [SerializeField]
  36. public string packageId;
  37. [SerializeField]
  38. public RemoveRequest packageRemoveRequest;
  39. [SerializeField]
  40. public bool shouldCheckForInstallation;
  41. }
  42. [Serializable]
  43. struct LegacyPackageListRequest
  44. {
  45. [SerializeField]
  46. public ListRequest packageListRequest;
  47. }
  48. private static readonly string k_PackageToRemoveQueue = "Remove Package Queue";
  49. private static EditorWorkQueue<PackageRemovalOperation> s_PackageRemovelQueue => EditorWorkQueue<PackageRemovalOperation>.Instance;
  50. private static bool hasPackageBeingRemoved => s_PackageRemovelQueue.HasWorkItems;
  51. private static readonly string k_WaitingToRemoveQueue = "Waiting Remove Queue";
  52. private static EditorWorkQueue<PackageRemovalRequest> s_PackagesToRemoveQueue => EditorWorkQueue<PackageRemovalRequest>.Instance;
  53. private static bool hasPackagesToRemove => s_PackagesToRemoveQueue.HasWorkItems;
  54. private static readonly string k_LocalPackageListingQueryQueue = "Local Package List";
  55. private static EditorWorkQueue<LegacyPackageListRequest> s_PackageListQueue => EditorWorkQueue<LegacyPackageListRequest>.Instance;
  56. private static bool hasActivePackageListQuery => EditorWorkQueueBase.SessionStateHasStoredData(k_LocalPackageListingQueryQueue);
  57. internal static bool IsPackageInstalled(string package)
  58. {
  59. return File.Exists($"Packages/{package}/package.json");
  60. }
  61. static XRLegacyUninstaller()
  62. {
  63. s_PackageRemovelQueue.QueueName = k_PackageToRemoveQueue;
  64. s_PackageRemovelQueue.ProcessItemCallback += (PackageRemovalOperation prr) =>
  65. {
  66. if (prr.packageRemoveRequest == null || String.IsNullOrEmpty(prr.packageRemoveRequest.PackageIdOrName))
  67. {
  68. return;
  69. }
  70. if (prr.packageRemoveRequest.Status == StatusCode.Failure)
  71. {
  72. return;
  73. }
  74. if (!prr.packageRemoveRequest.IsCompleted || (prr.shouldCheckForInstallation && IsPackageInstalled(prr.packageId)))
  75. {
  76. s_PackageRemovelQueue.QueueWorkItem(prr);
  77. return;
  78. }
  79. };
  80. if (s_PackageRemovelQueue.HasWorkItems) s_PackageRemovelQueue.StartQueue();
  81. s_PackagesToRemoveQueue.QueueName = k_WaitingToRemoveQueue;
  82. s_PackagesToRemoveQueue.ProcessItemCallback += (PackageRemovalRequest prr) =>
  83. {
  84. if (s_PackageRemovelQueue.HasWorkItems)
  85. {
  86. s_PackagesToRemoveQueue.QueueWorkItem(prr);
  87. return;
  88. }
  89. if (!String.IsNullOrEmpty(prr.packageId) && IsPackageInstalled(prr.packageId))
  90. {
  91. PackageRemovalOperation pro = new PackageRemovalOperation { packageId = prr.packageId, packageRemoveRequest = PackageManager.Client.Remove(prr.packageId), shouldCheckForInstallation=prr.shouldCheckForInstallation };
  92. s_PackageRemovelQueue.QueueWorkItem(pro);
  93. }
  94. };
  95. if (s_PackagesToRemoveQueue.HasWorkItems) s_PackagesToRemoveQueue.StartQueue();
  96. var packageSettings = XRPackageInitializationSettings.Instance;
  97. if (packageSettings.HasSettings("ShouldQueryLegacyPackageRemoval"))
  98. {
  99. return;
  100. }
  101. if (!packageSettings.HasSettings("RemoveLegacyInputHelpersForReload"))
  102. {
  103. PackageRemovalRequest lihremreq = new PackageRemovalRequest();
  104. lihremreq.packageId = "com.unity.xr.legacyinputhelpers";
  105. lihremreq.shouldCheckForInstallation = false;
  106. s_PackagesToRemoveQueue.QueueWorkItem(lihremreq);
  107. packageSettings.AddSettings("RemoveLegacyInputHelpersForReload");
  108. packageSettings.SaveSettings();
  109. }
  110. s_PackageListQueue.QueueName = k_LocalPackageListingQueryQueue;
  111. s_PackageListQueue.ProcessItemCallback += (LegacyPackageListRequest plr) => {
  112. if (plr.packageListRequest == null)
  113. return;
  114. if (!plr.packageListRequest.IsCompleted)
  115. {
  116. s_PackageListQueue.QueueWorkItem(plr);
  117. }
  118. else
  119. {
  120. if (plr.packageListRequest.Status == PackageManager.StatusCode.Success)
  121. {
  122. List<string> packageIdsToRemove = new List<string>();
  123. string removeRequestText = $"{XRConstants.k_XRPluginManagement} has detected that this project is using built in VR. Built in VR is incompatible with the new XR Plug-in system and any built in packages should be removed.\nDo you want {XRConstants.k_XRPluginManagement} to remove the following packages for you?\n\n";
  124. foreach (var package in plr.packageListRequest.Result)
  125. {
  126. if (k_PackagesToBeRemoved.Contains(package.name))
  127. {
  128. packageIdsToRemove.Add(package.name);
  129. removeRequestText += $"{package.displayName}\n";
  130. }
  131. }
  132. removeRequestText += "\nChoosing to cancel will require manual removal of built-in integration packages through the Package Manger window.";
  133. if (packageIdsToRemove.Count > 0)
  134. {
  135. if (EditorUtility.DisplayDialog("Built in VR Detected", removeRequestText, "Ok", "Cancel"))
  136. {
  137. foreach (string packageId in packageIdsToRemove)
  138. {
  139. PackageRemovalRequest remreq = new PackageRemovalRequest();
  140. remreq.packageId = packageId;
  141. remreq.shouldCheckForInstallation = true;
  142. s_PackagesToRemoveQueue.QueueWorkItem(remreq);
  143. }
  144. }
  145. var packSettings = XRPackageInitializationSettings.Instance;
  146. packSettings.AddSettings("ShouldQueryLegacyPackageRemoval");
  147. packSettings.SaveSettings();
  148. }
  149. #if !UNITY_2020_2_OR_NEWER
  150. bool virtualRealityEnabled = false;
  151. foreach (var buildTargetGroup in (BuildTargetGroup[])Enum.GetValues(typeof(BuildTargetGroup)))
  152. {
  153. #pragma warning disable CS0618
  154. virtualRealityEnabled |= PlayerSettings.GetVirtualRealitySupported(buildTargetGroup);
  155. #pragma warning restore CS0618
  156. }
  157. if (virtualRealityEnabled)
  158. {
  159. if (EditorUtility.DisplayDialog("Built in VR Enabled", $"{XRConstants.k_XRPluginManagement} has detected that Built-In VR is enabled. We must disable Built-In VR prior to using {XRConstants.k_XRPluginManagement}.", "Ok"))
  160. {
  161. foreach (var buildTargetGroup in (BuildTargetGroup[])Enum.GetValues(typeof(BuildTargetGroup)))
  162. {
  163. #pragma warning disable CS0618
  164. if (PlayerSettings.GetVirtualRealitySupported(buildTargetGroup))
  165. {
  166. PlayerSettings.SetVirtualRealitySupported(buildTargetGroup, false);
  167. UnityEditorInternal.VR.VREditor.SetVREnabledDevicesOnTargetGroup(buildTargetGroup, new string[]{});
  168. }
  169. #pragma warning restore CS0618
  170. }
  171. }
  172. }
  173. #endif //!UNITY_2020_2_OR_NEWER
  174. }
  175. }
  176. };
  177. LegacyPackageListRequest req = new LegacyPackageListRequest();
  178. req.packageListRequest = PackageManager.Client.List(true, true);
  179. s_PackageListQueue.QueueWorkItem(req);
  180. }
  181. }
  182. }