public void AddMappings(IControllerElementMappingProfile mappings, string profileName) { using var context = new DatabaseContext(this.Options.Options); // todo: check already exists context.ControllerElementMappings.Add(mappings.AsModel(profileName)); context.SaveChanges(); }
public async Task AddMappingsAsync(IControllerElementMappingProfile mappings, string profileName) { await using var context = new DatabaseContext(this.Options.Options); // todo: check already exists await context.ControllerElementMappings.AddAsync(mappings.AsModel(profileName)); await context.SaveChangesAsync(); }
public EmulatedController(int portIndex, IInputDevice physicalDevice, IInputDeviceInstance driverInstance, IControllerLayout targetLayout, IControllerElementMappingProfile layoutMapping) { this.PortIndex = portIndex; this.PhysicalDevice = physicalDevice; this.PhysicalDeviceInstance = driverInstance; this.TargetLayout = targetLayout; this.LayoutMapping = layoutMapping; }
public static ControllerElementMappingCollectionModel AsModel(this IControllerElementMappingProfile @this, string profileName) { return(new ControllerElementMappingCollectionModel { ProfileID = @this.ProfileGuid, ProfileName = profileName, DriverType = @this.DriverType, ControllerID = @this.ControllerID, DeviceName = @this.DeviceName, VendorID = @this.VendorID, MappedElements = @this.Select(e => e.AsModel(@this.ProfileGuid)).ToList() }); }
public void UpdateMappings(IControllerElementMappingProfile mappings) { using var context = new DatabaseContext(this.Options.Options); var retrievedMappings = context.ControllerElementMappings .Include(p => p.MappedElements) .SingleOrDefault(p => p.ProfileID == mappings.ProfileGuid); if (retrievedMappings == null) { return; } var mappingSet = mappings.Select(k => k.LayoutElement).ToHashSet(); foreach (var mapping in retrievedMappings.MappedElements) { try { if (mappings[mapping.LayoutElement] == mapping.DeviceCapability) { continue; } mapping.DeviceCapability = mappings[mapping.LayoutElement]; context.Entry(mapping).State = EntityState.Modified; } finally { mappingSet.Remove(mapping.LayoutElement); } } foreach (var toChange in mappingSet) { var model = new ControllerElementMappingModel { LayoutElement = toChange, DeviceCapability = mappings[toChange], Collection = retrievedMappings, ProfileID = retrievedMappings.ProfileID }; retrievedMappings.MappedElements.Add(model); context.Entry(model).State = EntityState.Added; } context.SaveChanges(); }
public InputTemplate(IControllerElementMappingProfile mappedElements, int playerIndex = 0) { this.PlayerIndex = playerIndex; ProxyGenerator generator = new ProxyGenerator(); 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.configurationOptions = (from prop in typeof(T).GetProperties() // let configAttribute = prop.GetCustomAttribute<ConfigurationOptionAttribute>() // where configAttribute != null // let name = prop.Name // let metadata = prop.GetCustomAttributes<CustomMetadataAttribute>() // select new ConfigurationOptionDescriptor(configAttribute, metadata, name) as IConfigurationOptionDescriptor).ToList(); this.ValueCollection = new ConfigurationValueCollection(); var configDescriptor = new ConfigurationSectionDescriptor <T>(typeof(T).Name); ((ConfigurationValueCollection)this.ValueCollection).EnsureSectionDefaults(configDescriptor); this.inputTemplateInterceptor = new InputTemplateInterceptor <T>(map.ToDictionary(m => m.Key, m => m.Value), this.ValueCollection, configDescriptor); var circular = new InputTemplateCircularInterceptor <T>(this); this.Configuration = new InputConfigurationSection <T>(circular, this.inputTemplateInterceptor); this.Template = generator.CreateInterfaceProxyWithoutTarget <T>(circular, this.inputTemplateInterceptor); }
protected override void Configure(IObjectTypeDescriptor <IEmulatedPortDeviceEntry> descriptor) { descriptor.Name("ConnectedEmulatorController") .Description("Describe the connection state of the physical device plugged into an emulated port."); descriptor.Field("isConnected") .Description("Whether or not the physical device of the emulated controller is connected.") .Resolver(ctx => { var devices = ctx.SnowflakeService <IDeviceEnumerator>(); var entry = ctx.Parent <IEmulatedPortDeviceEntry>(); return(devices.IsPortDeviceConnected(entry)); }) .Type <NonNullType <BooleanType> >(); descriptor.Field("controller") .Description("The emulated controller plugged into the port.") .Resolver(async ctx => { var deviceEntry = ctx.Parent <IEmulatedPortDeviceEntry>(); var inputConfig = ctx.SnowflakeService <IControllerElementMappingProfileStore>(); var devices = ctx.SnowflakeService <IDeviceEnumerator>(); var stone = ctx.SnowflakeService <IStoneProvider>(); stone.Controllers.TryGetValue(deviceEntry.ControllerID, out var controllerLayout); IInputDevice physicalDevice = devices.GetPortDevice(deviceEntry); IInputDeviceInstance deviceInstance = devices.GetPortDeviceInstance(deviceEntry); IControllerElementMappingProfile mappingProfile = await inputConfig.GetMappingsAsync(deviceEntry.ProfileGuid); return(new EmulatedController(deviceEntry.PortIndex, physicalDevice, deviceInstance, controllerLayout, mappingProfile)); }) .Type <NonNullType <EmulatedControllerType> >(); descriptor.Field(c => c.PortIndex) .Description("The port index.") .Type <NonNullType <IntType> >(); descriptor.Field("portDeviceEntry") .Description("The emulated port device entry.") .Resolver(ctx => ctx.Parent <IEmulatedPortDeviceEntry>()) .Type <NonNullType <EmulatedPortDeviceEntryType> >(); }
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); }