@@ -172,8 +172,7 @@ private void InitializeServiceLocator()
172172 Utilities . Editor . InputMappingAxisUtility . CheckUnityInputManagerMappings ( Definitions . Devices . ControllerMappingLibrary . UnityInputManagerAxes ) ;
173173#endif
174174
175- if ( ! RegisterService ( typeof ( IMixedRealityInputSystem ) , Activator . CreateInstance ( ActiveProfile . InputSystemType ) as IMixedRealityInputSystem ) ||
176- InputSystem == null )
175+ if ( ! RegisterService < IMixedRealityInputSystem > ( ActiveProfile . InputSystemType ) || InputSystem == null )
177176 {
178177 Debug . LogError ( "Failed to start the Input System!" ) ;
179178 }
@@ -182,8 +181,7 @@ private void InitializeServiceLocator()
182181 // If the Boundary system has been selected for initialization in the Active profile, enable it in the project
183182 if ( ActiveProfile . IsBoundarySystemEnabled )
184183 {
185- if ( ! RegisterService ( typeof ( IMixedRealityBoundarySystem ) , Activator . CreateInstance ( ActiveProfile . BoundarySystemSystemType ) as IMixedRealityBoundarySystem ) ||
186- BoundarySystem == null )
184+ if ( ! RegisterService < IMixedRealityBoundarySystem > ( ActiveProfile . BoundarySystemSystemType ) || BoundarySystem == null )
187185 {
188186 Debug . LogError ( "Failed to start the Boundary System!" ) ;
189187 }
@@ -192,8 +190,7 @@ private void InitializeServiceLocator()
192190 // If the Spatial Awareness system has been selected for initialization in the Active profile, enable it in the project
193191 if ( ActiveProfile . IsSpatialAwarenessSystemEnabled )
194192 {
195- if ( ! RegisterService ( typeof ( IMixedRealitySpatialAwarenessSystem ) , Activator . CreateInstance ( ActiveProfile . SpatialAwarenessSystemSystemType ) as IMixedRealitySpatialAwarenessSystem ) ||
196- SpatialAwarenessSystem == null )
193+ if ( ! RegisterService < IMixedRealitySpatialAwarenessSystem > ( ActiveProfile . SpatialAwarenessSystemSystemType ) || SpatialAwarenessSystem == null )
197194 {
198195 Debug . LogError ( "Failed to start the Spatial Awareness System!" ) ;
199196 }
@@ -202,17 +199,15 @@ private void InitializeServiceLocator()
202199 // If the Teleport system has been selected for initialization in the Active profile, enable it in the project
203200 if ( ActiveProfile . IsTeleportSystemEnabled )
204201 {
205- if ( ! RegisterService ( typeof ( IMixedRealityTeleportSystem ) , Activator . CreateInstance ( ActiveProfile . TeleportSystemSystemType ) as IMixedRealityTeleportSystem ) ||
206- TeleportSystem == null )
202+ if ( ! RegisterService < IMixedRealityTeleportSystem > ( ActiveProfile . TeleportSystemSystemType ) || TeleportSystem == null )
207203 {
208204 Debug . LogError ( "Failed to start the Teleport System!" ) ;
209205 }
210206 }
211207
212208 if ( ActiveProfile . IsDiagnosticsSystemEnabled )
213209 {
214- if ( ! RegisterService ( typeof ( IMixedRealityDiagnosticsSystem ) , Activator . CreateInstance ( ActiveProfile . DiagnosticsSystemSystemType ) as IMixedRealityDiagnosticsSystem ) ||
215- DiagnosticsSystem == null )
210+ if ( ! RegisterService < IMixedRealityDiagnosticsSystem > ( ActiveProfile . DiagnosticsSystemSystemType ) || DiagnosticsSystem == null )
216211 {
217212 Debug . LogError ( "Failed to start the Diagnostics System!" ) ;
218213 }
@@ -229,12 +224,9 @@ private void InitializeServiceLocator()
229224 if ( Application . platform . IsPlatformSupported ( configuration . RuntimePlatform ) )
230225#endif
231226 {
232- if ( configuration . ComponentType . Type != null )
227+ if ( ! RegisterService < IMixedRealityExtensionService > ( configuration . ComponentType , configuration . ComponentName , configuration . Priority ) )
233228 {
234- if ( ! RegisterService ( typeof ( IMixedRealityExtensionService ) , Activator . CreateInstance ( configuration . ComponentType , configuration . ComponentName , configuration . Priority ) as IMixedRealityExtensionService ) )
235- {
236- Debug . LogError ( $ "Failed to register the { configuration . ComponentType . Type } Extension Service!") ;
237- }
229+ Debug . LogError ( $ "Failed to register the { configuration . ComponentType . Type } Extension Service!") ;
238230 }
239231 }
240232 }
@@ -535,7 +527,7 @@ private void OnDestroy()
535527 /// <summary>
536528 /// Add a new service to the Mixed Reality Toolkit active service registry.
537529 /// </summary>
538- /// <param name="type">The interface type for the system to be managed . E.G. InputSystem, BoundarySystem</param>
530+ /// <param name="type">The interface type for the system to be registered . E.G. InputSystem, BoundarySystem</param>
539531 /// <param name="service">The Instance of the service class to register</param>
540532 public bool RegisterService ( Type type , IMixedRealityService service )
541533 {
@@ -547,40 +539,89 @@ public bool RegisterService(Type type, IMixedRealityService service)
547539
548540 if ( type == null )
549541 {
550- Debug . LogWarning ( "Unable to add a manager of type null." ) ;
542+ Debug . LogWarning ( "Unable to add a service of type null." ) ;
551543 return false ;
552544 }
553545
554546 if ( service == null )
555547 {
556- Debug . LogWarning ( "Unable to add a manager with a null instance." ) ;
548+ Debug . LogWarning ( "Unable to add a service with a null instance." ) ;
557549 return false ;
558550 }
559551
560- if ( IsCoreSystem ( type ) )
552+ return RegisterServiceInternal ( type , service ) ;
553+ }
554+
555+ /// <summary>
556+ /// Add a new service to the Mixed Reality Toolkit active service registry.
557+ /// </summary>
558+ /// <typeparam name="T">The interface type for the system to be registered.</typeparam>
559+ /// <param name="concreteType">The concrete type to instantiate.</param>
560+ /// <param name="args">Optional arguments used when instantiating the concrete type.</param>
561+ /// <returns>True, if the service was successfully registered.</returns>
562+ public bool RegisterService < T > ( Type concreteType , params object [ ] args )
563+ {
564+ if ( ActiveProfile == null ) { return false ; }
565+
566+ if ( concreteType == null )
567+ {
568+ Debug . LogError ( "Unable to register a service with a null concrete type." ) ;
569+ return false ;
570+ }
571+
572+ if ( ! typeof ( IMixedRealityService ) . IsAssignableFrom ( concreteType ) )
573+ {
574+ Debug . LogError ( $ "Unable to register the { concreteType } service. It does not implement the IMixedRealityService interface.") ;
575+ return false ;
576+ }
577+
578+ T serviceInstance ;
579+
580+ try
581+ {
582+ serviceInstance = ( T ) Activator . CreateInstance ( concreteType , args ) ;
583+ }
584+ catch ( Exception e )
585+ {
586+ Debug . LogError ( $ "Unable to register the { concreteType } service: { e . GetType ( ) } - { e . Message } ") ;
587+ return false ;
588+ }
589+
590+ return RegisterServiceInternal ( typeof ( T ) , serviceInstance as IMixedRealityService ) ;
591+ }
592+
593+ /// <summary>
594+ /// Internal service registration.
595+ /// </summary>
596+ /// <param name="interfaceType">The interface type for the system to be registered.</param>
597+ /// <param name="serviceInstance">Instance of the service.</param>
598+ /// <returns>True if registration is successful, false otherwise.</returns>
599+ private bool RegisterServiceInternal ( Type interfaceType , IMixedRealityService serviceInstance )
600+ {
601+ if ( IsCoreSystem ( interfaceType ) )
561602 {
562603 IMixedRealityService preExistingService ;
563604
564- ActiveProfile . ActiveServices . TryGetValue ( type , out preExistingService ) ;
605+ ActiveProfile . ActiveServices . TryGetValue ( interfaceType , out preExistingService ) ;
565606
566607 if ( preExistingService == null )
567608 {
568- ActiveProfile . ActiveServices . Add ( type , service ) ;
609+ ActiveProfile . ActiveServices . Add ( interfaceType , serviceInstance ) ;
569610 return true ;
570611 }
571612
572- Debug . LogError ( $ "There's already a { type . Name } registered.") ;
613+ Debug . LogError ( $ "There's already a { interfaceType . Name } registered.") ;
573614 return false ;
574615 }
575616
576- if ( ! typeof ( IMixedRealityExtensionService ) . IsAssignableFrom ( type ) )
617+ if ( ! typeof ( IMixedRealityExtensionService ) . IsAssignableFrom ( interfaceType ) )
577618 {
578- Debug . LogError ( $ "Unable to register { type } . Concrete type does not implement the IMixedRealityExtensionService implementation.") ;
619+ Debug . LogError ( $ "Unable to register { interfaceType } . Concrete type does not implement the IMixedRealityExtensionService implementation.") ;
579620 return false ;
580621 }
581622
582- MixedRealityComponents . Add ( new Tuple < Type , IMixedRealityExtensionService > ( type , ( IMixedRealityExtensionService ) service ) ) ;
583- if ( ! isInitializing ) { service . Initialize ( ) ; }
623+ MixedRealityComponents . Add ( new Tuple < Type , IMixedRealityExtensionService > ( interfaceType , serviceInstance as IMixedRealityExtensionService ) ) ;
624+ if ( ! isInitializing ) { serviceInstance . Initialize ( ) ; }
584625 mixedRealityComponentsCount = MixedRealityComponents . Count ;
585626 return true ;
586627 }
@@ -624,7 +665,7 @@ public IMixedRealityService GetService(Type type, bool showLogs = true)
624665
625666 if ( type == null )
626667 {
627- Debug . LogError ( "Unable to get null manager type." ) ;
668+ Debug . LogError ( "Unable to get null service type." ) ;
628669 return null ;
629670 }
630671
@@ -669,13 +710,13 @@ public IMixedRealityService GetService(Type type, string serviceName, bool showL
669710
670711 if ( type == null )
671712 {
672- Debug . LogError ( "Unable to get null manager type." ) ;
713+ Debug . LogError ( "Unable to get null service type." ) ;
673714 return null ;
674715 }
675716
676717 if ( string . IsNullOrEmpty ( serviceName ) )
677718 {
678- Debug . LogError ( "Unable to get manager by name without the name being specified." ) ;
719+ Debug . LogError ( "Unable to get service by name without the name being specified." ) ;
679720 return null ;
680721 }
681722
@@ -712,7 +753,7 @@ public void UnregisterService(Type type)
712753
713754 if ( type == null )
714755 {
715- Debug . LogError ( "Unable to remove null manager type." ) ;
756+ Debug . LogError ( "Unable to remove null service type." ) ;
716757 return ;
717758 }
718759
@@ -748,13 +789,13 @@ public void UnregisterService(Type type, string serviceName)
748789
749790 if ( type == null )
750791 {
751- Debug . LogError ( "Unable to remove null manager type." ) ;
792+ Debug . LogError ( "Unable to remove null service type." ) ;
752793 return ;
753794 }
754795
755796 if ( string . IsNullOrEmpty ( serviceName ) )
756797 {
757- Debug . LogError ( "Unable to remove manager by name without the name being specified." ) ;
798+ Debug . LogError ( "Unable to remove service by name without the name being specified." ) ;
758799 return ;
759800 }
760801
@@ -781,7 +822,7 @@ public void DisableService(Type type)
781822 {
782823 if ( type == null )
783824 {
784- Debug . LogError ( "Unable to disable null manager type." ) ;
825+ Debug . LogError ( "Unable to disable null service type." ) ;
785826 return ;
786827 }
787828
@@ -809,13 +850,13 @@ public void DisableService(Type type, string serviceName)
809850 {
810851 if ( type == null )
811852 {
812- Debug . LogError ( "Unable to disable null manager type." ) ;
853+ Debug . LogError ( "Unable to disable null service type." ) ;
813854 return ;
814855 }
815856
816857 if ( string . IsNullOrEmpty ( serviceName ) )
817858 {
818- Debug . LogError ( "Unable to disable manager by name without the name being specified." ) ;
859+ Debug . LogError ( "Unable to disable service by name without the name being specified." ) ;
819860 return ;
820861 }
821862
@@ -842,7 +883,7 @@ public void EnableService(Type type)
842883 {
843884 if ( type == null )
844885 {
845- Debug . LogError ( "Unable to enable null manager type." ) ;
886+ Debug . LogError ( "Unable to enable null service type." ) ;
846887 return ;
847888 }
848889
@@ -870,13 +911,13 @@ public void EnableService(Type type, string serviceName)
870911 {
871912 if ( type == null )
872913 {
873- Debug . LogError ( "Unable to enable null manager type." ) ;
914+ Debug . LogError ( "Unable to enable null service type." ) ;
874915 return ;
875916 }
876917
877918 if ( string . IsNullOrEmpty ( serviceName ) )
878919 {
879- Debug . LogError ( "Unable to enable manager by name without the name being specified." ) ;
920+ Debug . LogError ( "Unable to enable service by name without the name being specified." ) ;
880921 return ;
881922 }
882923
@@ -908,7 +949,7 @@ public List<IMixedRealityService> GetActiveServices(Type type)
908949 {
909950 if ( type == null )
910951 {
911- Debug . LogWarning ( "Unable to get managers with a type of null." ) ;
952+ Debug . LogWarning ( "Unable to get services with a type of null." ) ;
912953 return new List < IMixedRealityService > ( ) ;
913954 }
914955
@@ -931,7 +972,7 @@ public List<IMixedRealityService> GetActiveServices(Type type, string serviceNam
931972
932973 if ( type == null )
933974 {
934- Debug . LogWarning ( "Unable to get managers with a type of null." ) ;
975+ Debug . LogWarning ( "Unable to get services with a type of null." ) ;
935976 return new List < IMixedRealityService > ( ) ;
936977 }
937978
@@ -1096,7 +1137,7 @@ private static bool IsCoreSystem(Type type)
10961137 {
10971138 if ( type == null )
10981139 {
1099- Debug . LogWarning ( "Null cannot be a core manager ." ) ;
1140+ Debug . LogWarning ( "Null cannot be a core system ." ) ;
11001141 return false ;
11011142 }
11021143
0 commit comments