Skip to content

Commit 25f0e2e

Browse files
committed
Ensure XR SDK providers only run on their specified loaders
1 parent b3e046e commit 25f0e2e

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
@@ -153,22 +153,13 @@ protected override SupportedControllerType GetCurrentControllerType(InputDevice
153153

154154
#endregion Controller Utilities
155155

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

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

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

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

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

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

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

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

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

@@ -87,6 +95,18 @@ public override void Initialize()
8795
base.Initialize();
8896
}
8997

98+
/// <inheritdoc />
99+
public override void Enable()
100+
{
101+
if (!IsActiveLoader)
102+
{
103+
IsEnabled = false;
104+
return;
105+
}
106+
107+
base.Enable();
108+
}
109+
90110
private void ReadProfile()
91111
{
92112
if (ConfigurationProfile == null)
@@ -116,6 +136,11 @@ public override void Update()
116136
{
117137
using (UpdatePerfMarker.Auto())
118138
{
139+
if (!IsEnabled)
140+
{
141+
return;
142+
}
143+
119144
if (!eyeTrackingDevice.isValid)
120145
{
121146
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
@@ -50,22 +50,13 @@ public WindowsMixedRealityDeviceManager(
5050
uint priority = DefaultPriority,
5151
BaseMixedRealityProfile profile = null) : base(inputSystem, name, priority, profile) { }
5252

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

65-
return isActiveLoader ?? false;
66-
}
67-
}
68-
6960
#region IMixedRealityDeviceManager Interface
7061

7162
/// <inheritdoc />

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

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

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

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

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

7291
/// <inheritdoc />
@@ -126,6 +145,11 @@ public override void Update()
126145
{
127146
using (UpdatePerfMarker.Auto())
128147
{
148+
if (!IsEnabled)
149+
{
150+
return;
151+
}
152+
129153
if (!centerEye.isValid)
130154
{
131155
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
@@ -37,6 +37,13 @@ public WindowsMixedRealitySpatialMeshObserver(
3737
BaseMixedRealityProfile profile = null) : base(spatialAwarenessSystem, name, priority, profile)
3838
{ }
3939

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

4249
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
@@ -39,6 +39,22 @@ public GenericXRSDKSpatialMeshObserver(
3939
BaseMixedRealityProfile profile = null) : base(spatialAwarenessSystem, name, priority, profile)
4040
{ }
4141

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

4460
private XRMeshSubsystem meshSubsystem;
@@ -131,6 +147,11 @@ public override void Update()
131147
{
132148
using (UpdatePerfMarker.Auto())
133149
{
150+
if (!IsEnabled)
151+
{
152+
return;
153+
}
154+
134155
base.Update();
135156
UpdateObserver();
136157
}
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
@@ -62,15 +62,16 @@ public virtual bool CheckCapability(MixedRealityCapability capability)
6262
/// </summary>
6363
/// <param name="loaderName">The string name to compare against the active loader.</param>
6464
/// <returns>True if the active loader has the same name as the parameter.</returns>
65-
protected virtual bool IsLoaderActive(string loaderName) => XRGeneralSettings.Instance != null && XRGeneralSettings.Instance.Manager != null &&
66-
XRGeneralSettings.Instance.Manager.activeLoader != null && XRGeneralSettings.Instance.Manager.activeLoader.name == loaderName;
65+
[Obsolete("Use XRSDKLoaderHelpers instead.")]
66+
protected virtual bool IsLoaderActive(string loaderName) => LoaderHelpers.IsLoaderActive(loaderName);
6767

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

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

0 commit comments

Comments
 (0)