Ejemplo n.º 1
0
        private void EnsureLoaded()
        {
            if (_lookup != null)
            {
                return;
            }

            Dictionary <Guid, Func <Type> > builder = new Dictionary <Guid, Func <Type> >();

            foreach (Assembly asm in _assemblies)
            {
                foreach (Type type in asm.GetTypes())
                {
                    if (!typeof(IIdentifiedComponent).GetTypeInfo().IsAssignableFrom(type) || type.GetTypeInfo().GetConstructor(Type.EmptyTypes) == null || !type.GetTypeInfo().IsClass)
                    {
                        continue;
                    }

                    IReadOnlyList <Type> registerFor = type.GetTypeInfo().ImplementedInterfaces.Where(x => x != typeof(IIdentifiedComponent) && typeof(IIdentifiedComponent).GetTypeInfo().IsAssignableFrom(x)).ToList();
                    if (registerFor.Count == 0)
                    {
                        continue;
                    }

                    IIdentifiedComponent instance = (IIdentifiedComponent)Activator.CreateInstance(type);
                    builder[instance.Id] = () => type;
                }
            }

            _lookup = builder.ToList();
        }
Ejemplo n.º 2
0
        public void Register(Type type)
        {
            if (!typeof(IIdentifiedComponent).IsAssignableFrom(type) || type.GetConstructor(Type.EmptyTypes) == null || !type.GetTypeInfo().IsClass)
            {
                return;
            }

            IReadOnlyList <Type> registerFor = type.GetTypeInfo().ImplementedInterfaces.Where(x => x != typeof(IIdentifiedComponent) && typeof(IIdentifiedComponent).IsAssignableFrom(x)).ToList();

            if (registerFor.Count == 0)
            {
                return;
            }

            IIdentifiedComponent instance = (IIdentifiedComponent)Activator.CreateInstance(type);

            foreach (Type t in registerFor)
            {
                FieldInfo instanceField = typeof(Cache <>).MakeGenericType(t).GetField("Instance", BindingFlags.Public | BindingFlags.Static);
                ICache    cache         = (ICache)instanceField.GetValue(null);
                cache.AddPart(instance);
                _componentIdToAssemblyQualifiedTypeName[instance.Id] = type.AssemblyQualifiedName;
                _settings.ComponentGuidToAssemblyQualifiedName[instance.Id.ToString()] = type.AssemblyQualifiedName;

                if (!_componentIdsByType.TryGetValue(t, out HashSet <Guid> ids))
                {
                    _componentIdsByType[t] = ids = new HashSet <Guid>();
                }

                ids.Add(instance.Id);

                if (!_settings.ComponentTypeToGuidList.TryGetValue(t.FullName, out ids))
                {
                    _settings.ComponentTypeToGuidList[t.FullName] = ids = new HashSet <Guid>();
                }

                ids.Add(instance.Id);
            }

            bool      successfulWrite = false;
            const int maxAttempts     = 10;
            int       attemptCount    = 0;

            while (!successfulWrite && attemptCount++ < maxAttempts)
            {
                try
                {
                    _loader.Save();
                    successfulWrite = true;
                }
                catch (IOException)
                {
                    Thread.Sleep(10);
                }
            }
        }
Ejemplo n.º 3
0
        // This method does not save the settings, it just registers into the memory cache.
        private bool RegisterType(Type type)
        {
            if (!typeof(IIdentifiedComponent).GetTypeInfo().IsAssignableFrom(type) || type.GetTypeInfo().GetConstructor(Type.EmptyTypes) == null || !type.GetTypeInfo().IsClass)
            {
                return(false);
            }

            IReadOnlyList <Type> interfaceTypesToRegisterFor = type.GetTypeInfo().ImplementedInterfaces.Where(x => x != typeof(IIdentifiedComponent) && typeof(IIdentifiedComponent).GetTypeInfo().IsAssignableFrom(x)).ToList();

            if (interfaceTypesToRegisterFor.Count == 0)
            {
                return(false);
            }

            lock (_componentIdToAssemblyQualifiedTypeName)
            {
                IIdentifiedComponent instance = (IIdentifiedComponent)Activator.CreateInstance(type);

                foreach (Type interfaceType in interfaceTypesToRegisterFor)
                {
                    AddComponent(interfaceType, instance);
                    AddProbingPath(Path.GetDirectoryName(type.Assembly.Location));

                    _componentIdToAssemblyQualifiedTypeName[instance.Id] = type.AssemblyQualifiedName;
                    _settings.ComponentGuidToAssemblyQualifiedName[instance.Id.ToString()] = type.AssemblyQualifiedName;

                    if (!_componentIdsByType.TryGetValue(interfaceType, out HashSet <Guid> idsForInterfaceType))
                    {
                        _componentIdsByType[interfaceType] = idsForInterfaceType = new HashSet <Guid>();
                    }
                    idsForInterfaceType.Add(instance.Id);

                    // for backwards compat & cleanup from when the keys were interfaceType.FullName
                    if (_settings.ComponentTypeToGuidList.TryGetValue(interfaceType.FullName, out HashSet <Guid> idsFromOldStyleKey))
                    {
                        _settings.ComponentTypeToGuidList.Remove(interfaceType.FullName);
                    }

                    if (!_settings.ComponentTypeToGuidList.TryGetValue(interfaceType.AssemblyQualifiedName, out HashSet <Guid> idsForInterfaceTypeForSettings))
                    {
                        _settings.ComponentTypeToGuidList[interfaceType.AssemblyQualifiedName] = idsForInterfaceTypeForSettings = new HashSet <Guid>();
                    }
                    idsForInterfaceTypeForSettings.Add(instance.Id);

                    // for backwards compat & cleanup from when the keys were interfaceType.FullName
                    if (idsFromOldStyleKey != null)
                    {
                        idsForInterfaceTypeForSettings.UnionWith(idsFromOldStyleKey);
                    }
                }

                return(true);
            }
        }
Ejemplo n.º 4
0
        // This method does not save the settings, it just registers into the memory cache.
        private bool RegisterType(Type type)
        {
            if (!typeof(IIdentifiedComponent).GetTypeInfo().IsAssignableFrom(type) || type.GetTypeInfo().GetConstructor(Type.EmptyTypes) == null || !type.GetTypeInfo().IsClass)
            {
                return(false);
            }

            IReadOnlyList <Type> interfaceTypesToRegisterFor = type.GetTypeInfo().ImplementedInterfaces.Where(x => x != typeof(IIdentifiedComponent) && typeof(IIdentifiedComponent).GetTypeInfo().IsAssignableFrom(x)).ToList();

            if (interfaceTypesToRegisterFor.Count == 0)
            {
                return(false);
            }

            IIdentifiedComponent instance = (IIdentifiedComponent)Activator.CreateInstance(type);

            foreach (Type interfaceType in interfaceTypesToRegisterFor)
            {
                FieldInfo instanceField = typeof(Cache <>).MakeGenericType(interfaceType).GetTypeInfo().GetField("Instance", BindingFlags.Public | BindingFlags.Static);
                ICache    cache         = (ICache)instanceField.GetValue(null);
                cache.AddPart(instance);

                _componentIdToAssemblyQualifiedTypeName[instance.Id] = type.AssemblyQualifiedName;
                _settings.ComponentGuidToAssemblyQualifiedName[instance.Id.ToString()] = type.AssemblyQualifiedName;

                if (!_componentIdsByType.TryGetValue(interfaceType, out HashSet <Guid> idsForInterfaceType))
                {
                    _componentIdsByType[interfaceType] = idsForInterfaceType = new HashSet <Guid>();
                }
                idsForInterfaceType.Add(instance.Id);

                // for backwards compat & cleanup from when the keys were interfaceType.FullName
                if (_settings.ComponentTypeToGuidList.TryGetValue(interfaceType.FullName, out HashSet <Guid> idsFromOldStyleKey))
                {
                    _settings.ComponentTypeToGuidList.Remove(interfaceType.FullName);
                }

                if (!_settings.ComponentTypeToGuidList.TryGetValue(interfaceType.AssemblyQualifiedName, out HashSet <Guid> idsForInterfaceTypeForSettings))
                {
                    _settings.ComponentTypeToGuidList[interfaceType.AssemblyQualifiedName] = idsForInterfaceTypeForSettings = new HashSet <Guid>();
                }
                idsForInterfaceTypeForSettings.Add(instance.Id);

                // for backwards compat & cleanup from when the keys were interfaceType.FullName
                if (idsFromOldStyleKey != null)
                {
                    idsForInterfaceTypeForSettings.UnionWith(idsFromOldStyleKey);
                }
            }

            return(true);
        }
Ejemplo n.º 5
0
        private void AddComponent(Type type, IIdentifiedComponent component)
        {
            if (!type.IsAssignableFrom(component.GetType()))
            {
                throw new ArgumentException($"{component.GetType().Name} should be assignable from {type.Name} type", nameof(type));
            }

            if (!_componentCache.TryGetValue(type, out Dictionary <Guid, object> typeCache))
            {
                typeCache             = new Dictionary <Guid, object>();
                _componentCache[type] = typeCache;
            }
            typeCache[component.Id] = component;
        }
Ejemplo n.º 6
0
        // This method does not save the settings, it just registers into the memory cache.
        private bool RegisterType(Type type)
        {
            if (!typeof(IIdentifiedComponent).GetTypeInfo().IsAssignableFrom(type) || type.GetTypeInfo().GetConstructor(Type.EmptyTypes) == null || !type.GetTypeInfo().IsClass)
            {
                return(false);
            }

            IReadOnlyList <Type> registerFor = type.GetTypeInfo().ImplementedInterfaces.Where(x => x != typeof(IIdentifiedComponent) && typeof(IIdentifiedComponent).GetTypeInfo().IsAssignableFrom(x)).ToList();

            if (registerFor.Count == 0)
            {
                return(false);
            }

            IIdentifiedComponent instance = (IIdentifiedComponent)Activator.CreateInstance(type);

            foreach (Type t in registerFor)
            {
                FieldInfo instanceField = typeof(Cache <>).MakeGenericType(t).GetTypeInfo().GetField("Instance", BindingFlags.Public | BindingFlags.Static);
                ICache    cache         = (ICache)instanceField.GetValue(null);
                cache.AddPart(instance);

                _componentIdToAssemblyQualifiedTypeName[instance.Id] = type.AssemblyQualifiedName;
                _settings.ComponentGuidToAssemblyQualifiedName[instance.Id.ToString()] = type.AssemblyQualifiedName;

                if (!_componentIdsByType.TryGetValue(t, out HashSet <Guid> ids))
                {
                    _componentIdsByType[t] = ids = new HashSet <Guid>();
                }

                ids.Add(instance.Id);

                if (!_settings.ComponentTypeToGuidList.TryGetValue(t.FullName, out ids))
                {
                    _settings.ComponentTypeToGuidList[t.FullName] = ids = new HashSet <Guid>();
                }

                ids.Add(instance.Id);
            }

            return(true);
        }
Ejemplo n.º 7
0
        public void Register(Type type)
        {
            if (!ComponentType.IsAssignableFrom(type))
            {
                return;
            }

            List <Type> types = new List <Type>();

            foreach (Type t in type.GetTypeInfo().GetInterfaces())
            {
                if (ComponentType.IsAssignableFrom(t))
                {
                    Type                 strongCache = typeof(Cache <>).MakeGenericType(t);
                    MethodInfo           method      = strongCache.GetTypeInfo().GetMethod(nameof(Cache <IIdentifiedComponent> .Get), BindingFlags.Public | BindingFlags.Static);
                    ICache               cache       = (ICache)method.Invoke(null, new object[] { this });
                    IIdentifiedComponent c           = (IIdentifiedComponent)Activator.CreateInstance(type);
                    cache.Register(c.Id, c);
                }
            }
        }
        public void Register(Type type)
        {
            if (!ComponentType.IsAssignableFrom(type))
            {
                return;
            }

            List <Type> types = new List <Type>();

            foreach (Type t in type.GetTypeInfo().GetInterfaces())
            {
                if (ComponentType.IsAssignableFrom(t))
                {
                    Type                 strongCache = typeof(Cache <>).MakeGenericType(t);
                    FieldInfo            field       = strongCache.GetTypeInfo().GetField(nameof(Cache <IIdentifiedComponent> .Instance), BindingFlags.Public | BindingFlags.Static);
                    ICache               cache       = (ICache)field.GetValue(null);
                    IIdentifiedComponent c           = (IIdentifiedComponent)Activator.CreateInstance(type);
                    cache.Register(c.Id, c);
                }
            }
        }
Ejemplo n.º 9
0
#pragma warning disable SA1202 // Elements should be ordered by access
        public void AddComponent(Type type, IIdentifiedComponent component)
#pragma warning restore SA1202 // Elements should be ordered by access
        {
            if (!type.IsAssignableFrom(component.GetType()))
            {
                throw new ArgumentException($"{component.GetType().Name} should be assignable from {type.Name} type", nameof(type));
            }

            if (!ComponentCache.TryGetValue(type, out Dictionary <Guid, object> typeCache))
            {
                typeCache            = new Dictionary <Guid, object>();
                ComponentCache[type] = typeCache;
            }
            typeCache[component.Id] = component;

            if (!_componentIdsByType.TryGetValue(type, out HashSet <Guid> ids))
            {
                ids = new HashSet <Guid>();
                _componentIdsByType[type] = ids;
            }
            ids.Add(component.Id);
        }
Ejemplo n.º 10
0
 public void AddPart(IIdentifiedComponent component)
 {
     Parts[component.Id] = (T)component;
 }
Ejemplo n.º 11
0
 public void Register(Guid id, IIdentifiedComponent o)
 {
     Lookup[id] = (T)o;
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Adds component to manager, which can be looked up later via <see cref="IComponentManager.TryGetComponent{T}(Guid, out T)"/> or <see cref="IComponentManager.OfType{T}"/>.
 /// Added components are not persisted and need to be called every time new instance of <see cref="Bootstrapper"/> is created.
 /// </summary>
 /// <param name="interfaceType">Interface type that added component implements.</param>
 /// <param name="component">Instance of type that implements <paramref name="interfaceType"/>.</param>
 public void AddComponent(Type interfaceType, IIdentifiedComponent component)
 {
     _engineEnvironmentSettings.Components.AddComponent(interfaceType, component);
 }