Skip to content

Commit dc61de8

Browse files
authored
Merge pull request #9674 from keveleigh/xrsdk-loaders
Ensure XR SDK providers only run on their specified loaders
2 parents 92d5837 + 25f0e2e commit dc61de8

10 files changed

+143
-42
lines changed

Assets/MRTK/Providers/Oculus/XRSDK/OculusXRSDKDeviceManager.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,13 @@ protected override SupportedControllerType GetCurrentControllerType(InputDevice
154154

155155
#endregion Controller Utilities
156156

157-
private bool? isActiveLoader = null;
158-
private bool IsActiveLoader
159-
{
160-
get
161-
{
157+
private bool IsActiveLoader =>
162158
#if OCULUS_ENABLED
163-
if (!isActiveLoader.HasValue)
164-
{
165-
isActiveLoader = IsLoaderActive<OculusLoader>();
166-
}
159+
LoaderHelpers.IsLoaderActive<OculusLoader>();
160+
#else
161+
false;
167162
#endif // OCULUS_ENABLED
168163

169-
return isActiveLoader ?? false;
170-
}
171-
}
172-
173164
/// <inheritdoc/>
174165
public override void Enable()
175166
{

Assets/MRTK/Providers/OpenXR/Scripts/OpenXRDeviceManager.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,13 @@ public OpenXRDeviceManager(
3434
uint priority = DefaultPriority,
3535
BaseMixedRealityProfile profile = null) : base(inputSystem, name, priority, profile) { }
3636

37-
private bool? isActiveLoader = null;
38-
private bool IsActiveLoader
39-
{
40-
get
41-
{
37+
private bool IsActiveLoader =>
4238
#if UNITY_OPENXR
43-
if (!isActiveLoader.HasValue)
44-
{
45-
isActiveLoader = IsLoaderActive<OpenXRLoaderBase>();
46-
}
39+
LoaderHelpers.IsLoaderActive<OpenXRLoaderBase>();
40+
#else
41+
false;
4742
#endif // UNITY_OPENXR
4843

49-
return isActiveLoader ?? false;
50-
}
51-
}
52-
5344
/// <inheritdoc />
5445
public override void Enable()
5546
{

Assets/MRTK/Providers/OpenXR/Scripts/OpenXREyeGazeDataProvider.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using UnityEngine.XR;
1111

1212
#if UNITY_OPENXR
13+
using UnityEngine.XR.OpenXR;
1314
using UnityEngine.XR.OpenXR.Features.Interactions;
1415
#endif // UNITY_OPENXR
1516

@@ -45,6 +46,13 @@ public OpenXREyeGazeDataProvider(
4546
gazeSmoother.OnSaccadeY += GazeSmoother_OnSaccadeY;
4647
}
4748

49+
private bool IsActiveLoader =>
50+
#if UNITY_OPENXR
51+
LoaderHelpers.IsLoaderActive<OpenXRLoaderBase>();
52+
#else
53+
false;
54+
#endif // UNITY_OPENXR
55+
4856
/// <inheritdoc />
4957
public bool SmoothEyeTracking { get; set; } = false;
5058

@@ -88,6 +96,18 @@ public override void Initialize()
8896
base.Initialize();
8997
}
9098

99+
/// <inheritdoc />
100+
public override void Enable()
101+
{
102+
if (!IsActiveLoader)
103+
{
104+
IsEnabled = false;
105+
return;
106+
}
107+
108+
base.Enable();
109+
}
110+
91111
private void ReadProfile()
92112
{
93113
if (ConfigurationProfile == null)
@@ -117,6 +137,11 @@ public override void Update()
117137
{
118138
using (UpdatePerfMarker.Auto())
119139
{
140+
if (!IsEnabled)
141+
{
142+
return;
143+
}
144+
120145
if (!eyeTrackingDevice.isValid)
121146
{
122147
InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.EyeTracking, InputDeviceList);

Assets/MRTK/Providers/WindowsMixedReality/XRSDK/WindowsMixedRealityDeviceManager.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,13 @@ public WindowsMixedRealityDeviceManager(
5151
uint priority = DefaultPriority,
5252
BaseMixedRealityProfile profile = null) : base(inputSystem, name, priority, profile) { }
5353

54-
private bool? isActiveLoader = null;
55-
private bool IsActiveLoader
56-
{
57-
get
58-
{
54+
private bool IsActiveLoader =>
5955
#if WMR_ENABLED
60-
if (!isActiveLoader.HasValue)
61-
{
62-
isActiveLoader = IsLoaderActive("Windows MR Loader");
63-
}
56+
LoaderHelpers.IsLoaderActive("Windows MR Loader");
57+
#else
58+
false;
6459
#endif // WMR_ENABLED
6560

66-
return isActiveLoader ?? false;
67-
}
68-
}
69-
7061
#region IMixedRealityDeviceManager Interface
7162

7263
/// <inheritdoc />

Assets/MRTK/Providers/WindowsMixedReality/XRSDK/WindowsMixedRealityEyeGazeDataProvider.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ public WindowsMixedRealityEyeGazeDataProvider(
4646
gazeSmoother.OnSaccadeY += GazeSmoother_OnSaccadeY;
4747
}
4848

49+
private bool IsActiveLoader =>
50+
#if WMR_ENABLED
51+
LoaderHelpers.IsLoaderActive("Windows MR Loader");
52+
#else
53+
false;
54+
#endif // WMR_ENABLED
55+
4956
/// <inheritdoc />
5057
public bool SmoothEyeTracking { get; set; } = false;
5158

@@ -68,6 +75,18 @@ public WindowsMixedRealityEyeGazeDataProvider(
6875
public event Action OnSaccadeY;
6976
private void GazeSmoother_OnSaccadeY() => OnSaccadeY?.Invoke();
7077

78+
/// <inheritdoc />
79+
public override void Enable()
80+
{
81+
if (!IsActiveLoader)
82+
{
83+
IsEnabled = false;
84+
return;
85+
}
86+
87+
base.Enable();
88+
}
89+
7190
#region IMixedRealityCapabilityCheck Implementation
7291

7392
/// <inheritdoc />
@@ -127,6 +146,11 @@ public override void Update()
127146
{
128147
using (UpdatePerfMarker.Auto())
129148
{
149+
if (!IsEnabled)
150+
{
151+
return;
152+
}
153+
130154
if (!centerEye.isValid)
131155
{
132156
centerEye = InputDevices.GetDeviceAtXRNode(XRNode.CenterEye);

Assets/MRTK/Providers/WindowsMixedReality/XRSDK/WindowsMixedRealitySpatialMeshObserver.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public WindowsMixedRealitySpatialMeshObserver(
3939
BaseMixedRealityProfile profile = null) : base(spatialAwarenessSystem, name, priority, profile)
4040
{ }
4141

42+
protected override bool IsActiveLoader =>
43+
#if WMR_ENABLED
44+
LoaderHelpers.IsLoaderActive("Windows MR Loader");
45+
#else
46+
false;
47+
#endif // WMR_ENABLED
48+
4249
private static readonly ProfilerMarker ConfigureObserverVolumePerfMarker = new ProfilerMarker("[MRTK] WindowsMixedRealitySpatialMeshObserver.ConfigureObserverVolume");
4350

4451
private Vector3 oldObserverOrigin = Vector3.zero;

Assets/MRTK/Providers/XRSDK/GenericXRSDKSpatialMeshObserver.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ public GenericXRSDKSpatialMeshObserver(
4141
BaseMixedRealityProfile profile = null) : base(spatialAwarenessSystem, name, priority, profile)
4242
{ }
4343

44+
// Don't run this one on Windows MR, since that has its own observer
45+
// There's probably a better way to manage these two...
46+
protected virtual bool IsActiveLoader => !LoaderHelpers.IsLoaderActive("Windows MR Loader");
47+
48+
/// <inheritdoc />
49+
public override void Enable()
50+
{
51+
if (!IsActiveLoader)
52+
{
53+
IsEnabled = false;
54+
return;
55+
}
56+
57+
base.Enable();
58+
}
59+
4460
#region BaseSpatialObserver Implementation
4561

4662
private XRMeshSubsystem meshSubsystem;
@@ -133,6 +149,11 @@ public override void Update()
133149
{
134150
using (UpdatePerfMarker.Auto())
135151
{
152+
if (!IsEnabled)
153+
{
154+
return;
155+
}
156+
136157
base.Update();
137158
UpdateObserver();
138159
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#if XR_MANAGEMENT_ENABLED
5+
using UnityEngine.XR.Management;
6+
#endif
7+
8+
namespace Microsoft.MixedReality.Toolkit.XRSDK
9+
{
10+
public static class LoaderHelpers
11+
{
12+
/// <summary>
13+
/// Checks if the active loader has a specific name. Used in cases where the loader class is internal, like WindowsMRLoader.
14+
/// </summary>
15+
/// <param name="loaderName">The string name to compare against the active loader.</param>
16+
/// <returns>True if the active loader has the same name as the parameter.</returns>
17+
public static bool IsLoaderActive(string loaderName) =>
18+
#if XR_MANAGEMENT_ENABLED
19+
XRGeneralSettings.Instance != null
20+
&& XRGeneralSettings.Instance.Manager != null
21+
&& XRGeneralSettings.Instance.Manager.activeLoader != null
22+
&& XRGeneralSettings.Instance.Manager.activeLoader.name == loaderName;
23+
#else
24+
false;
25+
#endif
26+
27+
#if XR_MANAGEMENT_ENABLED
28+
/// <summary>
29+
/// Checks if the active loader is of a specific type. Used in cases where the loader class is accessible, like OculusLoader.
30+
/// </summary>
31+
/// <typeparam name="T">The loader class type to check against the active loader.</typeparam>
32+
/// <returns>True if the active loader is of the specified type.</returns>
33+
public static bool IsLoaderActive<T>() where T : XRLoader =>
34+
XRGeneralSettings.Instance != null
35+
&& XRGeneralSettings.Instance.Manager != null
36+
&& XRGeneralSettings.Instance.Manager.activeLoader is T;
37+
#endif
38+
}
39+
}

Assets/MRTK/Providers/XRSDK/LoaderHelpers.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/MRTK/Providers/XRSDK/XRSDKDeviceManager.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,16 @@ public virtual bool CheckCapability(MixedRealityCapability capability)
6363
/// </summary>
6464
/// <param name="loaderName">The string name to compare against the active loader.</param>
6565
/// <returns>True if the active loader has the same name as the parameter.</returns>
66-
protected virtual bool IsLoaderActive(string loaderName) => XRGeneralSettings.Instance != null && XRGeneralSettings.Instance.Manager != null &&
67-
XRGeneralSettings.Instance.Manager.activeLoader != null && XRGeneralSettings.Instance.Manager.activeLoader.name == loaderName;
66+
[Obsolete("Use XRSDKLoaderHelpers instead.")]
67+
protected virtual bool IsLoaderActive(string loaderName) => LoaderHelpers.IsLoaderActive(loaderName);
6868

6969
/// <summary>
7070
/// Checks if the active loader is of a specific type. Used in cases where the loader class is accessible, like OculusLoader.
7171
/// </summary>
7272
/// <typeparam name="T">The loader class type to check against the active loader.</typeparam>
7373
/// <returns>True if the active loader is of the specified type.</returns>
74-
protected virtual bool IsLoaderActive<T>() where T : XRLoader => XRGeneralSettings.Instance != null && XRGeneralSettings.Instance.Manager != null && XRGeneralSettings.Instance.Manager.activeLoader is T;
74+
[Obsolete("Use XRSDKLoaderHelpers instead.")]
75+
protected virtual bool IsLoaderActive<T>() where T : XRLoader => LoaderHelpers.IsLoaderActive<T>();
7576
#endif // XR_MANAGEMENT_ENABLED
7677

7778
private static readonly ProfilerMarker UpdatePerfMarker = new ProfilerMarker("[MRTK] XRSDKDeviceManager.Update");

0 commit comments

Comments
 (0)