Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Added platform overrides #530

Merged
merged 2 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ public abstract class BasePlatform : IMixedRealityPlatform

/// <inheritdoc />
public virtual bool IsBuildTargetAvailable => false;

/// <inheritdoc />
public virtual IMixedRealityPlatform[] PlatformOverrides { get; } = new IMixedRealityPlatform[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,25 @@ public sealed class CurrentBuildTargetPlatform : BasePlatform
/// <returns>True, if any build target is active.</returns>
public static bool IsBuildTargetActive(List<IMixedRealityPlatform> 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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ public interface IMixedRealityPlatform
/// Only returns true in editor.
/// </remarks>
bool IsBuildTargetAvailable { get; }

/// <summary>
/// The list of platforms that this specific platform will override and make the others return not available.
/// </summary>
IMixedRealityPlatform[] PlatformOverrides { get; }
}
}
49 changes: 31 additions & 18 deletions XRTK-Core/Packages/com.xrtk.core/Services/MixedRealityToolkit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,27 +536,46 @@ private static void EnsureMixedRealityRequirements()
.Where(type => typeof(IMixedRealityPlatform).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract)
.OrderBy(type => type.Name);

var platformOverrides = new List<Type>();

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(platformInstance);
availablePlatforms.Add(platform);

if (platformInstance.IsAvailable ||
platformInstance.IsBuildTargetAvailable)
if (platform.IsAvailable ||
platform.IsBuildTargetAvailable)
{
activePlatforms.Add(platformInstance);
foreach (var platformOverride in platform.PlatformOverrides)
{
platformOverrides.Add(platformOverride.GetType());
}
}
}

foreach (var platform in availablePlatforms)
{
if (platformOverrides.Contains(platform.GetType()))
{
continue;
}

if (platform.IsAvailable ||
platform.IsBuildTargetAvailable)
{
activePlatforms.Add(platform);
}
}

Expand Down Expand Up @@ -886,23 +905,17 @@ public static bool TryCreateAndRegisterService<T>(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;
}
}
Expand Down