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

Commit

Permalink
Fixes to correctly filter for nested services/data providers in the n…
Browse files Browse the repository at this point in the history
…ew configuraitons inspector (#470)

* Recursively look for generic type arguments on a profile to filter for ServiceConstraint

* Fix BaseExtensionService profile type expected in constructor

* Update XRTK-Core/Packages/com.xrtk.core/Inspectors/Profiles/MixedRealityServiceProviderProfileInspector.cs

Co-Authored-By: Stephen Hodgson <StephenHodgson@users.noreply.github.com>

* Update XRTK-Core/Packages/com.xrtk.core/Extensions/TypeExtensions.cs

Co-Authored-By: Stephen Hodgson <StephenHodgson@users.noreply.github.com>

* Update XRTK-Core/Packages/com.xrtk.core/Definitions/BaseMixedRealityExtensionServiceProfile.cs

Co-Authored-By: Stephen Hodgson <StephenHodgson@users.noreply.github.com>

* Fix class doc

* Fix missing using

* Fix base extension service profile generic type

* Make ServiceConstraint protected to allow for custom override

* Update Tests

* Fix interface base type

Co-authored-by: Stephen Hodgson <StephenHodgson@users.noreply.github.com>
  • Loading branch information
FejZa and StephenHodgson authored Mar 28, 2020
1 parent 6d73c4c commit 233def2
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 4 deletions.
12 changes: 12 additions & 0 deletions Definitions/BaseMixedRealityExtensionServiceProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) XRTK. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using XRTK.Interfaces;

namespace XRTK.Definitions
{
public abstract class BaseMixedRealityExtensionServiceProfile : BaseMixedRealityServiceProfile<IMixedRealityService>
{

}
}
11 changes: 11 additions & 0 deletions Definitions/BaseMixedRealityExtensionServiceProfile.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) XRTK. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using UnityEngine;

namespace XRTK.Extensions
{
/// <summary>
/// Extension methods for <see cref="Type"/> instances.
/// </summary>
public static class TypeExtensions
{
/// <summary>
/// Recursively looks for generic type arguments in type hierarchy, starting with the
/// root type provided. If no generic type arguments are found on a type, it's base
/// type is checked.
/// </summary>
/// <param name="root">Root type to start looking for generic type arguments at.</param>
/// <param name="maxRecursionDepth">The maximum recursion depth until execution gets canceled even if no results found.</param>
/// <returns>Found gneneric type arguments array or null, if none found.</returns>
public static Type[] FindTopmostGenericTypeArguments(this Type root, int maxRecursionDepth = 5)
{
Type[] genericTypeArgs = root?.GenericTypeArguments;
if (genericTypeArgs != null && genericTypeArgs.Length > 0)
{
return genericTypeArgs;
}

if (maxRecursionDepth > 0 && root != null)
{
return FindTopmostGenericTypeArguments(root.BaseType, --maxRecursionDepth);
}

Debug.LogError($"{nameof(FindTopmostGenericTypeArguments)} - Maximum recursion depth reached without finding generic type arguments.");
return null;
}
}
}
11 changes: 11 additions & 0 deletions Extensions/TypeExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using XRTK.Definitions;
using XRTK.Inspectors.PropertyDrawers;
using XRTK.Services;
using XRTK.Extensions;

namespace XRTK.Inspectors.Profiles
{
Expand All @@ -20,7 +21,12 @@ public class MixedRealityServiceProfileInspector : BaseMixedRealityProfileInspec

private SerializedProperty configurations;

protected Type ServiceConstraint { get; private set; } = null;
/// <summary>
/// Gets the service constraint used to filter options listed in the
/// <see cref="configurations"/> instance type dropdown. Set after
/// <see cref="OnEnable"/> was called to override.
/// </summary>
protected Type ServiceConstraint { get; set; } = null;

protected override void OnEnable()
{
Expand All @@ -30,7 +36,7 @@ protected override void OnEnable()

Debug.Assert(configurations != null);
var baseType = ThisProfile.GetType().BaseType;
var genericTypeArgs = baseType?.GenericTypeArguments;
var genericTypeArgs = baseType?.FindTopmostGenericTypeArguments();

Debug.Assert(genericTypeArgs != null);

Expand Down Expand Up @@ -152,4 +158,4 @@ private void OnConfigurationOptionRemoved(ReorderableList list)
}
}
}
}
}
2 changes: 1 addition & 1 deletion Services/BaseExtensionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public abstract class BaseExtensionService : BaseServiceWithConstructor, IMixedR
/// <param name="name"></param>
/// <param name="priority"></param>
/// <param name="profile"></param>
public BaseExtensionService(string name, uint priority, MixedRealityRegisteredServiceProvidersProfile profile) : base(name, priority) { }
public BaseExtensionService(string name, uint priority, BaseMixedRealityExtensionServiceProfile profile) : base(name, priority) { }
}
}

0 comments on commit 233def2

Please sign in to comment.