private static bool TryGetServiceInternal(Type interfaceType, out IMixedRealityService serviceInstance, out IMixedRealityServiceRegistrar registrar, string name = null) { // Assume failed and return null unless proven otherwise serviceInstance = null; registrar = null; // If there is an entry for the interface key provided, search that small list first if (registry.ContainsKey(interfaceType)) { if (FindEntry(registry[interfaceType], interfaceType, name, out serviceInstance, out registrar)) { return(true); } } // Either there is no entry for the interface type, or it was not placed in that list. // Services can have multiple supported interfaces thus they may match the requested query but be placed in a different registry bin // Thus, search all bins until a match is found foreach (var list in registry.Values) { if (FindEntry(list, interfaceType, name, out serviceInstance, out registrar)) { return(true); } } return(false); }
public void SetService(IMixedRealityService service, Transform facadeParent) { this.service = service; this.facadeParent = facadeParent; if (service == null) { serviceType = null; name = "(Destroyed)"; gameObject.SetActive(false); return; } else { this.serviceType = service.GetType(); name = serviceType.Name; gameObject.SetActive(true); if (!FacadeServiceLookup.ContainsKey(serviceType)) { FacadeServiceLookup.Add(serviceType, this); } else { FacadeServiceLookup[serviceType] = this; } if (!ActiveFacadeObjects.Contains(this)) { ActiveFacadeObjects.Add(this); } } }
public TestDataProvider1( IMixedRealityServiceRegistrar registrar, IMixedRealityService service, string name, uint priority) : base(registrar, service, name, priority) { }
/// <summary> /// Helper method to search list of IMixedRealityService/IMixedRealityServiceRegistrar pairs to find first service that matches name and interface type query /// </summary> /// <param name="serviceList">list of IMixedRealityService/IMixedRealityServiceRegistrar pairs to search</param> /// <param name="interfaceType">type of interface to check</param> /// <param name="name">name of service to check. Wildcard if null or empty</param> /// <param name="serviceInstance">reference to IMixedRealityService matching query, null otherwise</param> /// <param name="registrar">reference to IMixedRealityServiceRegistrar matching query, null otherwise</param> /// <returns>true if found first entry to match query, false otherwise</returns> private static bool FindEntry(List <KeyValuePair <IMixedRealityService, IMixedRealityServiceRegistrar> > serviceList, Type interfaceType, string name, out IMixedRealityService serviceInstance, out IMixedRealityServiceRegistrar registrar) { using (FindEntryPerfMarker.Auto()) { // Assume failed and return null unless proven otherwise serviceInstance = null; registrar = null; for (int i = 0; i < serviceList.Count; ++i) { var svc = serviceList[i].Key; if ((string.IsNullOrEmpty(name) || svc.Name == name) && interfaceType.IsAssignableFrom(svc.GetType())) { serviceInstance = svc; registrar = serviceList[i].Value; return(true); } } return(false); } }
/// <summary> /// Gets all services by type and name. /// </summary> /// <param name="serviceName">The name of the service to search for. If the string is empty than any matching <see cref="interfaceType"/> will be added to the <see cref="services"/> list.</param> private IReadOnlyList <T> GetAllServicesByNameInternal <T>(Type interfaceType, string serviceName) where T : IMixedRealityService { List <T> services = new List <T>(); if (!CanGetService(interfaceType)) { return(new List <T>() as IReadOnlyList <T>); } if (IsCoreSystem(interfaceType)) { IMixedRealityService serviceInstance = GetServiceByName <T>(serviceName); if ((serviceInstance != null) && CheckServiceMatch(interfaceType, serviceName, interfaceType, serviceInstance)) { services.Add((T)serviceInstance); } } else { for (int i = 0; i < registeredMixedRealityServices.Count; i++) { if (CheckServiceMatch(interfaceType, serviceName, registeredMixedRealityServices[i].Item1, registeredMixedRealityServices[i].Item2)) { services.Add((T)registeredMixedRealityServices[i].Item2); } } } return(services); }
/// <summary> /// Retrieve the first component from the registry that meets the selected type and name /// </summary> /// <param name="type">Interface type of the component being requested</param> /// <param name="serviceName">Name of the specific service</param> /// <param name="service">return parameter of the function</param> private bool GetService(Type type, string serviceName, out IMixedRealityService service) { if (type == null) { Debug.LogWarning("Unable to get a component with a type of null."); service = null; return(false); } service = null; if (isInitializing) { Debug.LogWarning("Unable to get a service while initializing!"); return(false); } if (mixedRealityComponentsCount != MixedRealityComponents.Count) { Initialize(); } for (int i = 0; i < mixedRealityComponentsCount; i++) { if (CheckComponentMatch(type, serviceName, MixedRealityComponents[i])) { service = MixedRealityComponents[i].Item2; return(true); } } return(false); }
/// <summary> /// Gets the instance of the requested service from the registry. /// </summary> /// <typeparam name="T">The interface type of the service being requested.</typeparam> /// <param name="serviceInstance">Output parameter to receive the requested service instance.</param> /// <param name="registrar">Output parameter to receive the registrar that loaded the service instance.</param> /// <param name="name">Optional name of the service.</param> /// <returns> /// True if the requested service is being returned, false otherwise. /// </returns> public static bool TryGetService <T>( out T serviceInstance, out IMixedRealityServiceRegistrar registrar, string name = null) { Type interfaceType = typeof(T); if (!registry.ContainsKey(interfaceType)) { serviceInstance = default(T); registrar = null; return(false); } List <KeyValuePair <IMixedRealityService, IMixedRealityServiceRegistrar> > services = registry[interfaceType]; int registryIndex = -1; if (!string.IsNullOrWhiteSpace(name)) { // Find the desired service by it's name. for (int i = 0; i < services.Count; i++) { if (services[i].Key.Name != name) { continue; } registryIndex = i; break; } if (registryIndex == -1) { // Failed to find the requested service. serviceInstance = default(T); registrar = null; return(false); } } else { if (services.Count > 1) { Debug.LogWarning("Multiple instances of the requested service were found. Please re-call this method and provide a value for the name parameter."); serviceInstance = default(T); registrar = null; return(false); } registryIndex = 0; } IMixedRealityService tempService = services[registryIndex].Key; Debug.Assert(tempService is T, "The service in the registry does not match the expected type."); serviceInstance = (T)tempService; registrar = services[registryIndex].Value; return(true); }
/// <summary> /// Helper method to search list of IMixedRealityService/IMixedRealityServiceRegistrar pairs to find first service that matches name and interface type query /// </summary> /// <param name="serviceList">list of IMixedRealityService/IMixedRealityServiceRegistrar pairs to search</param> /// <param name="interfaceType">type of interface to check</param> /// <param name="name">name of service to check. Wildcard if null or empty</param> /// <param name="serviceInstance">reference to IMixedRealityService matching query, null otherwise</param> /// <param name="registrar">reference to IMixedRealityServiceRegistrar matching query, null otherwise</param> /// <returns>true if found first entry to match query, false otherwise</returns> private static bool FindEntry(List <KeyValuePair <IMixedRealityService, IMixedRealityServiceRegistrar> > serviceList, Type interfaceType, string name, out IMixedRealityService serviceInstance, out IMixedRealityServiceRegistrar registrar) { Profiler.BeginSample("[MRTK] MixedRealityServiceRegistry.FindEntry"); // Assume failed and return null unless proven otherwise serviceInstance = null; registrar = null; for (int i = 0; i < serviceList.Count; ++i) { var svc = serviceList[i].Key; if ((string.IsNullOrEmpty(name) || svc.Name == name) && interfaceType.IsAssignableFrom(svc.GetType())) { serviceInstance = svc; registrar = serviceList[i].Value; Profiler.EndSample(); // FindEntry - found return(true); } } Profiler.EndSample(); // FindEntry return(false); }
private static void CreateFacade(Transform parent, IMixedRealityService service, int facadeIndex) { ServiceFacade facade = null; if (facadeIndex > parent.transform.childCount - 1) { GameObject facadeObject = new GameObject(); facadeObject.transform.parent = parent; facade = facadeObject.AddComponent <ServiceFacade>(); } else { Transform child = parent.GetChild(facadeIndex); facade = child.GetComponent <ServiceFacade>(); if (facade == null) { facade = child.gameObject.AddComponent <ServiceFacade>(); } } if (facade.transform.hasChanged) { facade.transform.localPosition = Vector3.zero; facade.transform.localRotation = Quaternion.identity; facade.transform.localScale = Vector3.one; facade.transform.hasChanged = false; } facade.SetService(service, parent); }
/// <summary> /// Gets the instance of the requested service from the registry. /// </summary> /// <param name="interfaceType">The interface type of the service being requested.</param> /// <param name="serviceInstance">Output parameter to receive the requested service instance.</param> /// <param name="registrar">Output parameter to receive the registrar that loaded the service instance.</param> /// <param name="name">Optional name of the service.</param> /// <returns> /// True if the requested service is being returned, false otherwise. /// </returns> public static bool TryGetService(Type interfaceType, out IMixedRealityService serviceInstance, out IMixedRealityServiceRegistrar registrar, string name = null) { return(TryGetServiceInternal(interfaceType, out serviceInstance, out registrar, name)); }
public TestBaseDataProvider( IMixedRealityService service, string name = null, uint priority = DefaultPriority, BaseMixedRealityProfile profile = null) : base(service, name, priority, profile) { }
/// <summary> /// Gets first matching data provider of provided type T registered to the provided mixed reality service. /// </summary> /// <typeparam name="T">Type of data provider to return. Must implement and/or extend from <see cref="Microsoft.MixedReality.Toolkit.IMixedRealityDataProvider" /></typeparam> /// <param name="service">This function will attempt to get first available data provider registered to this service.</param> /// <remarks> /// Service parameter is expected to implement <see cref="Microsoft.MixedReality.Toolkit.IMixedRealityDataProviderAccess" />. If not, then will return default(T) /// </remarks> public static T GetDataProvider <T>(IMixedRealityService service) where T : IMixedRealityDataProvider { if (service is IMixedRealityDataProviderAccess dataProviderAccess) { return(dataProviderAccess.GetDataProvider <T>()); } return(default(T)); }
protected BaseDataProvider( IMixedRealityServiceRegistrar registrar, IMixedRealityService service, string name = null, uint priority = DefaultPriority, BaseMixedRealityProfile profile = null) : this(service, name, priority, profile) { Registrar = registrar; }
/// <summary> /// Constructor. /// </summary> /// <param name="registrar">The <see cref="IMixedRealityServiceRegistrar"/> instance that loaded the data provider.</param> /// <param name="service">The <see cref="IMixedRealityService"/> to which the provider is providing data.</param> /// <param name="name">The friendly name of the data provider.</param> /// <param name="priority">The registration priority of the data provider.</param> /// <param name="profile">The configuration profile for the data provider.</param> public BaseDataProvider( IMixedRealityServiceRegistrar registrar, IMixedRealityService service, string name = null, uint priority = DefaultPriority, BaseMixedRealityProfile profile = null) : base(registrar, name, priority, profile) { Service = service; }
/// <summary> /// Gets first matching data provider of provided type T registered to the provided mixed reality service. /// </summary> /// <typeparam name="T">Type of data provider to return. Must implement and/or extend from <see cref="Microsoft.MixedReality.Toolkit.IMixedRealityDataProvider" /></typeparam> /// <param name="service">This function will attempt to get first available data provider registered to this service.</param> /// <remarks> /// Service parameter is expected to implement <see cref="Microsoft.MixedReality.Toolkit.IMixedRealityDataProviderAccess" />. If not, then will return default(T) /// </remarks> public static T GetDataProvider <T>(IMixedRealityService service) where T : IMixedRealityDataProvider { var dataProviderAccess = service as IMixedRealityDataProviderAccess; if (dataProviderAccess != null) { return(dataProviderAccess.GetDataProvider <T>()); } return(default(T)); }
/// <summary> /// Retrieve the first component from the registry that meets the selected type /// </summary> /// <param name="type">Interface type of the component being requested</param> /// <param name="service">return parameter of the function</param> private void GetService(Type type, out IMixedRealityService service) { if (type == null) { Debug.LogWarning("Unable to get a component with a type of null."); service = null; return; } GetService(type, string.Empty, out service); }
private static void CreateFacade(Transform parent, IMixedRealityService service, int facadeIndex) { GameObject facadeObject = new GameObject(); facadeObject.transform.parent = parent; facadeObject.transform.SetSiblingIndex(facadeIndex); ServiceFacade facade = facadeObject.AddComponent <ServiceFacade>(); facade.SetService(service); }
/// <summary> /// Constructor. /// </summary> /// <param name="service">The <see cref="IMixedRealityService"/> to which the provider is providing data.</param> /// <param name="name">The friendly name of the data provider.</param> /// <param name="priority">The registration priority of the data provider.</param> /// <param name="profile">The configuration profile for the data provider.</param> protected BaseDataProvider( IMixedRealityService service, string name = null, uint priority = DefaultPriority, BaseMixedRealityProfile profile = null) : base() { Service = service; Name = name; Priority = priority; ConfigurationProfile = profile; }
/// <summary> /// Removes an <see cref="IMixedRealityService"/> instance from the registry. /// </summary> /// <param name="interfaceType">The interface type of the service being removed.</param> /// <param name="serviceInstance">Instance of the service to remove.</param> /// <param name="registrar">Instance of the registrar manages the service.</param> /// <returns> /// True if the service was successfully removed, false otherwise. /// </returns> private static bool RemoveServiceInternal( Type interfaceType, IMixedRealityService serviceInstance, IMixedRealityServiceRegistrar registrar) { if (!registry.ContainsKey(interfaceType)) { return false; } List<KeyValuePair<IMixedRealityService, IMixedRealityServiceRegistrar>> services = registry[interfaceType]; return services.Remove(new KeyValuePair<IMixedRealityService, IMixedRealityServiceRegistrar>(serviceInstance, registrar)); }
private static bool TryGetServiceInternal(Type interfaceType, out IMixedRealityService serviceInstance, out IMixedRealityServiceRegistrar registrar, string name = null) { // Assume failed to return null unless proven otherwise serviceInstance = null; registrar = null; if (!registry.ContainsKey(interfaceType)) { return(false); } List <KeyValuePair <IMixedRealityService, IMixedRealityServiceRegistrar> > services = registry[interfaceType]; int registryIndex = -1; if (!string.IsNullOrWhiteSpace(name)) { // Find the desired service by it's name. for (int i = 0; i < services.Count; i++) { if (services[i].Key.Name != name) { continue; } registryIndex = i; break; } if (registryIndex == -1) { // Failed to find the requested service. return(false); } } else { if (services.Count > 1) { Debug.LogWarning("Multiple instances of the requested service were found. Please re-call this method and provide a value for the name parameter."); return(false); } registryIndex = 0; } serviceInstance = services[registryIndex].Key; registrar = services[registryIndex].Value; return(true); }
/// <summary> /// Constructor. /// </summary> /// <param name="registrar">The <see cref="IMixedRealityServiceRegistrar"/> instance that loaded the data provider.</param> /// <param name="service">The <see cref="IMixedRealityService"/> to which the provider is providing data.</param> /// <param name="name">The friendly name of the data provider.</param> /// <param name="priority">The registration priority of the data provider.</param> /// <param name="profile">The configuration profile for the data provider.</param> public BaseDataProvider( IMixedRealityServiceRegistrar registrar, IMixedRealityService service, string name = null, uint priority = DefaultPriority, BaseMixedRealityProfile profile = null) : base() { Registrar = registrar; Service = service; Name = name; Priority = priority; ConfigurationProfile = profile; }
/// <summary> /// Add a new service to the Mixed Reality Toolkit active service registry. /// </summary> /// <param name="type">The interface type for the system to be managed. E.G. InputSystem, BoundarySystem</param> /// <param name="service">The Instance of the service class to register</param> public bool RegisterService(Type type, IMixedRealityService service) { if (ActiveProfile == null) { Debug.LogError($"Unable to add a new {type.Name} Service as the Mixed Reality Toolkit has to Active Profile"); return(false); } if (type == null) { Debug.LogWarning("Unable to add a manager of type null."); return(false); } if (service == null) { Debug.LogWarning("Unable to add a manager with a null instance."); return(false); } if (IsCoreSystem(type)) { IMixedRealityService preExistingService; ActiveProfile.ActiveServices.TryGetValue(type, out preExistingService); if (preExistingService == null) { ActiveProfile.ActiveServices.Add(type, service); return(true); } Debug.LogError($"There's already a {type.Name} registered."); return(false); } if (!typeof(IMixedRealityExtensionService).IsAssignableFrom(type)) { Debug.LogError($"Unable to register {type}. Concrete type does not implement the IMixedRealityExtensionService implementation."); return(false); } MixedRealityComponents.Add(new Tuple <Type, IMixedRealityExtensionService>(type, (IMixedRealityExtensionService)service)); if (!isInitializing) { service.Initialize(); } mixedRealityComponentsCount = MixedRealityComponents.Count; return(true); }
/// <summary> /// Gets the first instance of the requested service from the registry that matches the given query. /// </summary> /// <param name="interfaceType">The interface type of the service being requested.</param> /// <param name="serviceInstance">Output parameter to receive the requested service instance.</param> /// <param name="registrar">Output parameter to receive the registrar that loaded the service instance.</param> /// <param name="name">Optional name of the service.</param> /// <returns> /// True if the requested service is being returned, false otherwise. /// </returns> public static bool TryGetService(Type interfaceType, out IMixedRealityService serviceInstance, out IMixedRealityServiceRegistrar registrar, string name = null) { if (!typeof(IMixedRealityService).IsAssignableFrom(interfaceType)) { Debug.LogWarning($"Cannot find type {interfaceType.Name} since it does not extend IMixedRealityService"); serviceInstance = null; registrar = null; return(false); } return(TryGetServiceInternal(interfaceType, out serviceInstance, out registrar, name)); }
/// <summary> /// Removes the given service/registrar combination from the GetAllServices cache /// </summary> private static void RemoveServiceFromCache( IMixedRealityService service, IMixedRealityServiceRegistrar registrar) { // Removing from the sorted list keeps sort order, so re-sorting isn't necessary allServices.Remove(service); if (allServicesByRegistrar.ContainsKey(registrar)) { allServicesByRegistrar[registrar].Remove(service); if (allServicesByRegistrar[registrar].Count == 0) { allServicesByRegistrar.Remove(registrar); } } }
/// <summary> /// Adds the given service/registrar combination to the GetAllServices cache /// </summary> private static void AddServiceToCache( IMixedRealityService service, IMixedRealityServiceRegistrar registrar) { // Services are stored in ascending priority order - adding them to the // list requires that we re-enforce that order. This must happen // in both the allServices and allServicesByRegistrar data structures. allServices.Add(service); allServices.Sort(ascendingOrderComparer); if (!allServicesByRegistrar.ContainsKey(registrar)) { allServicesByRegistrar.Add(registrar, new List <IMixedRealityService>()); } allServicesByRegistrar[registrar].Add(service); allServicesByRegistrar[registrar].Sort(ascendingOrderComparer); }
// NOTE: This method intentionally does not add to the registry. This is actually mostly a helper function for RegisterServiceInternal<T>. private bool RegisterServiceInternal(Type interfaceType, IMixedRealityService serviceInstance) { if (serviceInstance == null) { Debug.LogWarning($"Unable to add a {interfaceType.Name} service with a null instance."); return(false); } if (!CanGetService(interfaceType)) { return(false); } IMixedRealityService preExistingService = GetServiceByNameInternal(interfaceType, serviceInstance.Name); if (preExistingService != null) { Debug.LogError($"There's already a {interfaceType.Name}.{preExistingService.Name} registered!"); return(false); } if (IsCoreSystem(interfaceType)) { activeSystems.Add(interfaceType, serviceInstance); } else if (typeof(IMixedRealityDataProvider).IsAssignableFrom(interfaceType) || typeof(IMixedRealityExtensionService).IsAssignableFrom(interfaceType)) { registeredMixedRealityServices.Add(new Tuple <Type, IMixedRealityService>(interfaceType, serviceInstance)); } else { Debug.LogError($"Unable to register {interfaceType.Name}. Concrete type does not implement {typeof(IMixedRealityExtensionService).Name} or {typeof(IMixedRealityDataProvider).Name}."); return(false); } if (!isInitializing) { serviceInstance.Initialize(); serviceInstance.Enable(); } return(true); }
/// <summary> /// Removes an <see cref="IMixedRealityService"/> instance from the registry. /// </summary> /// <param name="interfaceType">The interface type of the service being removed.</param> /// <param name="serviceInstance">Instance of the service to remove.</param> /// <param name="registrar">Instance of the registrar manages the service.</param> /// <returns> /// True if the service was successfully removed, false otherwise. /// </returns> private static bool RemoveServiceInternal( Type interfaceType, IMixedRealityService serviceInstance, IMixedRealityServiceRegistrar registrar) { if (!registry.ContainsKey(interfaceType)) { return(false); } List <KeyValuePair <IMixedRealityService, IMixedRealityServiceRegistrar> > services = registry[interfaceType]; bool removed = services.Remove(new KeyValuePair <IMixedRealityService, IMixedRealityServiceRegistrar>(serviceInstance, registrar)); if (services.Count == 0) { // If the last service was removed, the key can be removed. registry.Remove(interfaceType); } return(removed); }
private static HashSet <IMixedRealityService> GetAllServices() { HashSet <IMixedRealityService> serviceList = new HashSet <IMixedRealityService>(MixedRealityServiceRegistry.GetAllServices()); // These are core systems that are likely out-of-box services and known to have register DataProviders // Search for any dataproviders that service facades can be created for var dataProviderManagers = new IMixedRealityService[] { CoreServices.InputSystem, CoreServices.SpatialAwarenessSystem }; foreach (var system in dataProviderManagers) { var dataProviderAccess = system as IMixedRealityDataProviderAccess; if (dataProviderAccess != null) { foreach (var dataProvider in dataProviderAccess.GetDataProviders()) { serviceList.Add(dataProvider); } } } return(serviceList); }
/// <summary> /// Check if the interface type and name matches the registered interface type and service instance found. /// </summary> /// <param name="interfaceType">The interface type of the service to check.</param> /// <param name="serviceName">The name of the service to check.</param> /// <param name="registeredInterfaceType">The registered interface type.</param> /// <param name="serviceInstance">The instance of the registered service.</param> /// <returns>True, if the registered service contains the interface type and name.</returns> private static bool CheckServiceMatch(Type interfaceType, string serviceName, Type registeredInterfaceType, IMixedRealityService serviceInstance) { bool isValid = string.IsNullOrEmpty(serviceName) || serviceInstance.Name == serviceName; if ((registeredInterfaceType.Name == interfaceType.Name || serviceInstance.GetType().Name == interfaceType.Name) && isValid) { return(true); } var interfaces = serviceInstance.GetType().GetInterfaces(); for (int i = 0; i < interfaces.Length; i++) { if (interfaces[i].Name == interfaceType.Name && isValid) { return(true); } } return(false); }
/// <summary> /// Constructor. /// </summary> /// <param name="name">The name of the service.</param> /// <param name="priority">The priority of the service.</param> /// <param name="profile">The optional <see cref="BaseMixedRealityProfile"/> for the data provider.</param> /// <param name="parentService">The <see cref="IMixedRealityService"/> that this <see cref="IMixedRealityDataProvider"/> is assigned to.</param> protected BaseDataProvider(string name, uint priority, BaseMixedRealityProfile profile, IMixedRealityService parentService) : base(name, priority) { ParentService = parentService ?? throw new ArgumentNullException($"{nameof(parentService)} cannot be null"); }