Skip to content

Commit 4deb038

Browse files
author
David Kline
authored
Merge pull request #2742 from keveleigh/MixedRealityManagerGetCalls
Fix GetManager(s) calls in MixedRealityManager
2 parents ba181f7 + 49afe4d commit 4deb038

File tree

1 file changed

+52
-44
lines changed

1 file changed

+52
-44
lines changed

Assets/MixedRealityToolkit/_Core/Managers/MixedRealityManager.cs

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -721,26 +721,14 @@ public List<IMixedRealityManager> GetManagers(Type type, string managerName)
721721
}
722722
else
723723
{
724-
//If no name provided, return all components of the same type. Else return the type/name combination.
724+
// If no name provided, return all components of the same type. Else return the type/name combination.
725725
if (string.IsNullOrWhiteSpace(managerName))
726726
{
727-
for (int i = 0; i < mixedRealityComponentsCount; i++)
728-
{
729-
if (MixedRealityComponents[i].Item1.Name == type.Name)
730-
{
731-
managers.Add(MixedRealityComponents[i].Item2);
732-
}
733-
}
727+
GetComponentsByType(type, ref managers);
734728
}
735729
else
736730
{
737-
for (int i = 0; i < mixedRealityComponentsCount; i++)
738-
{
739-
if (MixedRealityComponents[i].Item1.Name == type.Name && MixedRealityComponents[i].Item2.Name == managerName)
740-
{
741-
managers.Add(MixedRealityComponents[i].Item2);
742-
}
743-
}
731+
GetComponentsByTypeAndName(type, managerName, ref managers);
744732
}
745733
}
746734

@@ -893,32 +881,7 @@ private void GetComponentByType(Type type, out IMixedRealityManager manager)
893881
{
894882
if (type == null) { throw new ArgumentNullException(nameof(type)); }
895883

896-
manager = null;
897-
898-
for (int i = 0; i < mixedRealityComponentsCount; i++)
899-
{
900-
if (MixedRealityComponents[i].Item1.Name == type.Name ||
901-
MixedRealityComponents[i].Item2.GetType().Name == type.Name)
902-
{
903-
manager = MixedRealityComponents[i].Item2;
904-
break;
905-
}
906-
907-
var interfaces = MixedRealityComponents[i].Item2.GetType().GetInterfaces();
908-
for (int j = 0; j < interfaces.Length; j++)
909-
{
910-
if (interfaces[j].Name == type.Name)
911-
{
912-
manager = MixedRealityComponents[i].Item2;
913-
break;
914-
}
915-
}
916-
}
917-
918-
if (manager == null)
919-
{
920-
throw new NullReferenceException($"Unable to find {type.Name} Manager.");
921-
}
884+
GetComponentByTypeAndName(type, string.Empty, out manager);
922885
}
923886

924887
/// <summary>
@@ -930,22 +893,67 @@ private void GetComponentByType(Type type, out IMixedRealityManager manager)
930893
private void GetComponentByTypeAndName(Type type, string managerName, out IMixedRealityManager manager)
931894
{
932895
if (type == null) { throw new ArgumentNullException(nameof(type)); }
933-
if (string.IsNullOrEmpty(managerName)) { throw new ArgumentNullException(nameof(managerName)); }
934896

935897
manager = null;
936898

937899
for (int i = 0; i < mixedRealityComponentsCount; i++)
938900
{
939-
if (MixedRealityComponents[i].Item1.Name == type.Name &&
940-
MixedRealityComponents[i].Item2.Name == managerName)
901+
if (CheckComponentMatch(type, managerName, MixedRealityComponents[i]))
941902
{
942903
manager = MixedRealityComponents[i].Item2;
943904
break;
944905
}
945906
}
946907
}
947908

909+
private void GetComponentsByType(Type type, ref List<IMixedRealityManager> managers)
910+
{
911+
if (type == null) { throw new ArgumentNullException(nameof(type)); }
912+
913+
GetComponentsByTypeAndName(type, string.Empty, ref managers);
914+
}
915+
916+
private void GetComponentsByTypeAndName(Type type, string managerName, ref List<IMixedRealityManager> managers)
917+
{
918+
if (type == null) { throw new ArgumentNullException(nameof(type)); }
919+
920+
for (int i = 0; i < mixedRealityComponentsCount; i++)
921+
{
922+
if (CheckComponentMatch(type, managerName, MixedRealityComponents[i]))
923+
{
924+
managers.Add(MixedRealityComponents[i].Item2);
925+
}
926+
}
927+
}
928+
929+
private bool CheckComponentMatch(Type type, string managerName, Tuple<Type, IMixedRealityManager> managerTuple)
930+
{
931+
if ((managerTuple.Item1.Name == type.Name ||
932+
managerTuple.Item2.GetType().Name == type.Name) &&
933+
(string.IsNullOrEmpty(managerName) ? true :
934+
managerTuple.Item2.Name == managerName))
935+
{
936+
return true;
937+
}
938+
else
939+
{
940+
var interfaces = managerTuple.Item2.GetType().GetInterfaces();
941+
for (int j = 0; j < interfaces.Length; j++)
942+
{
943+
if (interfaces[j].Name == type.Name &&
944+
(string.IsNullOrEmpty(managerName) ? true :
945+
managerTuple.Item2.Name == managerName))
946+
{
947+
return true;
948+
}
949+
}
950+
}
951+
952+
return false;
953+
}
954+
948955
#endregion Manager Utilities
956+
949957
#endregion Manager Container Management
950958
}
951959
}

0 commit comments

Comments
 (0)