/// <inheritdoc/>
        public IEnumerable <IComposable> LoadModule(IModule module)
        {
            var logger = new LogProvider().GetLogger("AssemblyComposer"); // Unknown if logging service is available.

            logger.Info($"Loading module {module.Entry}");
            try
            {
                var deps = module.ModuleDirectory.EnumerateDirectories().First(d => d.Name == "contents");
            }
            catch (InvalidOperationException ex)
            {
                throw new DirectoryNotFoundException($"Unable to find module contents for {module.Entry}", ex);
            }

            var loadContext = new AssemblyModuleLoadContext(module);

            // todo: check for semver!!
            var entryPath = Path.Combine(module.ModuleDirectory.FullName, "contents", module.Entry);

            if (!File.Exists(entryPath))
            {
                throw new FileNotFoundException($"Unable to find specified entry point {module.Entry}");
            }

            var assembly = loadContext.LoadFromAssemblyPath(entryPath);
            IEnumerable <Type> types;

            try
            {
                types = assembly.ExportedTypes
                        .Where(t => t.GetInterfaces().Contains(typeof(IComposable)))
                        .Where(t => t.GetConstructor(Type.EmptyTypes) != null);
            }
            catch (TypeLoadException ex)
            {
                throw new TypeLoadException(
                          $"Unable to load {module.Entry}, are you sure it is compatible with this version of Snowflake?",
                          ex);
            }

            foreach (var type in types)
            {
                var container       = Instantiate.CreateInstance(type);
                var castedContainer = (IComposable)container;
                Console.WriteLine($"Found Container {container.GetType().Name}");
                yield return(castedContainer);
            }
        }
        internal ConfigurationCollection(IConfigurationValueCollection defaults)
        {
            this.Descriptor =
                ConfigurationDescriptorCache.GetCollectionDescriptor <T>();

            var genInstance = typeof(T).GetCustomAttribute <ConfigurationGenerationInstanceAttribute>();

            if (genInstance == null)
            {
                throw new InvalidOperationException("Not generated!"); // todo: mark with interface to fail at compile time.
            }
            this.ValueCollection = defaults;

            this.Configuration = (T)Instantiate.CreateInstance(genInstance.InstanceType,
                                                               new[] { typeof(IConfigurationValueCollection) }, Expression.Constant(this.ValueCollection));
        }
Beispiel #3
0
        internal CollectionInterceptor(IConfigurationValueCollection defaults)
        {
            this.Values = new Dictionary <string, dynamic>();

            foreach (var(type, name) in from props in typeof(T).GetPublicProperties()
                     where props.GetIndexParameters().Length == 0 &&
                     props.PropertyType.GetInterfaces().Contains(typeof(IConfigurationSection))
                     select(type: props.PropertyType, name: props.Name))
            {
                var sectionType = typeof(ConfigurationSection <>).MakeGenericType(type);

                if (name != null)
                {
                    this.Values.Add(name,
                                    Instantiate.CreateInstance(sectionType,
                                                               new Type[] { typeof(IConfigurationValueCollection), typeof(string) },
                                                               Expression.Constant(defaults),
                                                               Expression.Constant(name)));
                }
            }
        }
Beispiel #4
0
        public InputConfiguration(IControllerElementMappingProfile mappedElements, int playerIndex = 0)
        {
            this.PlayerIndex = playerIndex;
            this.Descriptor  = ConfigurationDescriptorCache.GetSectionDescriptor <T>(typeof(T).Name);

            this.Options = (from prop in typeof(T).GetProperties()
                            let inputOptionAttribute = prop.GetCustomAttribute <InputOptionAttribute>()
                                                       where inputOptionAttribute != null
                                                       let name = prop.Name
                                                                  select(name, option: (IInputOption) new InputOption(inputOptionAttribute, name)))
                           .ToDictionary(o => o.name,
                                         o => o.option);
            var overrides = (from element in mappedElements
                             from key in this.Options.Keys
                             let option = this.Options[key]
                                          let target = option.TargetElement
                                                       where element.LayoutElement == target
                                                       where FlagEnums.HasAnyFlags(option.OptionType, element.DeviceCapability.GetClass())
                                                       select(key, element.DeviceCapability)).ToDictionary(d => d.key, d => d.DeviceCapability);
            var map = from key in this.Options.Keys
                      let value = overrides.ContainsKey(key) ? overrides[key] : DeviceCapability.None
                                  select new KeyValuePair <string, DeviceCapability>(key, value);

            this.ValueCollection = new ConfigurationValueCollection();
            var genInstance = typeof(T).GetCustomAttribute <ConfigurationGenerationInstanceAttribute>();

            if (genInstance == null)
            {
                throw new InvalidOperationException("Not generated!"); // todo: mark with interface to fail at compile time.
            }
            this.Configuration =
                (T)Instantiate.CreateInstance(genInstance.InstanceType,
                                              new[] { typeof(IConfigurationSectionDescriptor), typeof(IConfigurationValueCollection), typeof(Dictionary <string, DeviceCapability>) },
                                              Expression.Constant(this.Descriptor), Expression.Constant(this.ValueCollection),
                                              Expression.Constant(map.ToDictionary(m => m.Key, m => m.Value)));
            this.ConfigurationSection = new ConfigurationSection <T>(this.ValueCollection, this.Descriptor, this.Configuration);
        }