From e407b599b916f3fa3701cec6ea6f68d20ea554bb Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Mon, 20 Apr 2020 13:37:38 -0400 Subject: [PATCH 1/2] Added platform overrides --- .../Definitions/Platforms/BasePlatform.cs | 3 + .../Platforms/CurrentBuildTargetPlatform.cs | 28 ++++----- .../Interfaces/IMixedRealityPlatform.cs | 5 ++ .../Services/MixedRealityToolkit.cs | 59 +++++++++++++------ 4 files changed, 62 insertions(+), 33 deletions(-) diff --git a/XRTK-Core/Packages/com.xrtk.core/Definitions/Platforms/BasePlatform.cs b/XRTK-Core/Packages/com.xrtk.core/Definitions/Platforms/BasePlatform.cs index 717fcbd77..3845119c1 100644 --- a/XRTK-Core/Packages/com.xrtk.core/Definitions/Platforms/BasePlatform.cs +++ b/XRTK-Core/Packages/com.xrtk.core/Definitions/Platforms/BasePlatform.cs @@ -17,5 +17,8 @@ public abstract class BasePlatform : IMixedRealityPlatform /// public virtual bool IsBuildTargetAvailable => false; + + /// + public virtual IMixedRealityPlatform[] PlatformOverrides { get; } = new IMixedRealityPlatform[0]; } } \ No newline at end of file diff --git a/XRTK-Core/Packages/com.xrtk.core/Definitions/Platforms/CurrentBuildTargetPlatform.cs b/XRTK-Core/Packages/com.xrtk.core/Definitions/Platforms/CurrentBuildTargetPlatform.cs index 517fbe2bf..64f18bfda 100644 --- a/XRTK-Core/Packages/com.xrtk.core/Definitions/Platforms/CurrentBuildTargetPlatform.cs +++ b/XRTK-Core/Packages/com.xrtk.core/Definitions/Platforms/CurrentBuildTargetPlatform.cs @@ -22,27 +22,25 @@ public sealed class CurrentBuildTargetPlatform : BasePlatform /// True, if any build target is active. public static bool IsBuildTargetActive(List platforms) { + if (!Application.isEditor) { return false; } + var isEditor = false; var isBuildTargetActive = false; var isEditorPlatformActive = false; - for (var i = 0; i < platforms.Count; i++) + foreach (var platform in platforms) { - var platform = platforms[i]; - - if (platform is AllPlatforms) - { - return true; - } - - if (platform is CurrentBuildTargetPlatform) - { - isEditor = true; - isEditorPlatformActive = platform.IsAvailable; - } - else + switch (platform) { - isBuildTargetActive |= platform.IsBuildTargetAvailable; + case AllPlatforms _: + return true; + case CurrentBuildTargetPlatform _: + isEditor = true; + isEditorPlatformActive = platform.IsAvailable; + break; + default: + isBuildTargetActive |= platform.IsBuildTargetAvailable; + break; } } diff --git a/XRTK-Core/Packages/com.xrtk.core/Interfaces/IMixedRealityPlatform.cs b/XRTK-Core/Packages/com.xrtk.core/Interfaces/IMixedRealityPlatform.cs index a29161264..61ee4a9cd 100644 --- a/XRTK-Core/Packages/com.xrtk.core/Interfaces/IMixedRealityPlatform.cs +++ b/XRTK-Core/Packages/com.xrtk.core/Interfaces/IMixedRealityPlatform.cs @@ -20,5 +20,10 @@ public interface IMixedRealityPlatform /// Only returns true in editor. /// bool IsBuildTargetAvailable { get; } + + /// + /// The list of platforms that this specific platform will override and make the others return not available. + /// + IMixedRealityPlatform[] PlatformOverrides { get; } } } \ No newline at end of file diff --git a/XRTK-Core/Packages/com.xrtk.core/Services/MixedRealityToolkit.cs b/XRTK-Core/Packages/com.xrtk.core/Services/MixedRealityToolkit.cs index 208abf9ef..b0803ce92 100644 --- a/XRTK-Core/Packages/com.xrtk.core/Services/MixedRealityToolkit.cs +++ b/XRTK-Core/Packages/com.xrtk.core/Services/MixedRealityToolkit.cs @@ -536,27 +536,56 @@ private static void EnsureMixedRealityRequirements() .Where(type => typeof(IMixedRealityPlatform).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract) .OrderBy(type => type.Name); + var platformOverrides = new List(); + + Debug.Log("Available Platforms:"); foreach (var platformType in platformTypes) { - IMixedRealityPlatform platformInstance = null; + IMixedRealityPlatform platform = null; try { - platformInstance = Activator.CreateInstance(platformType) as IMixedRealityPlatform; + platform = Activator.CreateInstance(platformType) as IMixedRealityPlatform; } catch (Exception e) { Debug.LogError(e); } - if (platformInstance == null) { continue; } + if (platform == null) { continue; } + + availablePlatforms.Add(platform); + Debug.Log($"{platform.GetType().Name} | {platform.IsAvailable} | {platform.IsBuildTargetAvailable}"); + + if (platform.IsAvailable || + platform.IsBuildTargetAvailable) + { + foreach (var platformOverride in platform.PlatformOverrides) + { + platformOverrides.Add(platformOverride.GetType()); + } + } + } + + Debug.LogWarning("Platform Overrides:"); + foreach (var platform in platformOverrides) + { + Debug.LogWarning(platform.Name); + } - availablePlatforms.Add(platformInstance); + Debug.LogError("Active Platforms:"); + foreach (var platform in availablePlatforms) + { + if (platformOverrides.Contains(platform.GetType())) + { + continue; + } - if (platformInstance.IsAvailable || - platformInstance.IsBuildTargetAvailable) + if (platform.IsAvailable || + platform.IsBuildTargetAvailable) { - activePlatforms.Add(platformInstance); + Debug.LogError(platform.GetType().Name); + activePlatforms.Add(platform); } } @@ -886,23 +915,17 @@ public static bool TryCreateAndRegisterService(Type concreteType, IReadOnlyLi Debug.Assert(ActivePlatforms.Count > 0); - for (var i = 0; i < runtimePlatforms?.Count; i++) + for (var i = 0; i < ActivePlatforms.Count; i++) { - var runtimePlatform = runtimePlatforms[i].GetType(); - - if (runtimePlatform == typeof(AllPlatforms)) - { - platforms.Add(runtimePlatforms[i]); - break; - } + var activePlatform = ActivePlatforms[i].GetType(); - for (var j = 0; j < ActivePlatforms.Count; j++) + for (var j = 0; j < runtimePlatforms?.Count; j++) { - var activePlatform = ActivePlatforms[j].GetType(); + var runtimePlatform = runtimePlatforms[j].GetType(); if (activePlatform == runtimePlatform) { - platforms.Add(runtimePlatforms[i]); + platforms.Add(runtimePlatforms[j]); break; } } From 2912fd42f2e80a43cce32130d3dd5a8a4a7bcd1b Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Mon, 20 Apr 2020 13:42:03 -0400 Subject: [PATCH 2/2] removed debug --- .../com.xrtk.core/Services/MixedRealityToolkit.cs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/XRTK-Core/Packages/com.xrtk.core/Services/MixedRealityToolkit.cs b/XRTK-Core/Packages/com.xrtk.core/Services/MixedRealityToolkit.cs index b0803ce92..957f0ec88 100644 --- a/XRTK-Core/Packages/com.xrtk.core/Services/MixedRealityToolkit.cs +++ b/XRTK-Core/Packages/com.xrtk.core/Services/MixedRealityToolkit.cs @@ -538,7 +538,6 @@ private static void EnsureMixedRealityRequirements() var platformOverrides = new List(); - Debug.Log("Available Platforms:"); foreach (var platformType in platformTypes) { IMixedRealityPlatform platform = null; @@ -555,7 +554,6 @@ private static void EnsureMixedRealityRequirements() if (platform == null) { continue; } availablePlatforms.Add(platform); - Debug.Log($"{platform.GetType().Name} | {platform.IsAvailable} | {platform.IsBuildTargetAvailable}"); if (platform.IsAvailable || platform.IsBuildTargetAvailable) @@ -567,13 +565,6 @@ private static void EnsureMixedRealityRequirements() } } - Debug.LogWarning("Platform Overrides:"); - foreach (var platform in platformOverrides) - { - Debug.LogWarning(platform.Name); - } - - Debug.LogError("Active Platforms:"); foreach (var platform in availablePlatforms) { if (platformOverrides.Contains(platform.GetType())) @@ -584,7 +575,6 @@ private static void EnsureMixedRealityRequirements() if (platform.IsAvailable || platform.IsBuildTargetAvailable) { - Debug.LogError(platform.GetType().Name); activePlatforms.Add(platform); } }