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

Commit

Permalink
Added platform overrides (#530)
Browse files Browse the repository at this point in the history
* Added platform overrides

* removed debug
  • Loading branch information
StephenHodgson authored Apr 20, 2020
1 parent b21fda6 commit 81dbe8b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
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

0 comments on commit 81dbe8b

Please sign in to comment.