public IActionResult Save([FromBody] PlugInConfigurationDto pluginDto) { try { GameConfiguration gameConfiguration; using (var context = this.persistenceContextProvider.CreateNewContext()) { gameConfiguration = context.GetById <GameConfiguration>(pluginDto.GameConfigurationId); } using (var context = this.persistenceContextProvider.CreateNewContext(gameConfiguration)) { PlugInConfiguration plugin = pluginDto.Id == Guid.Empty ? context.CreateNew <PlugInConfiguration>() : context.GetById <PlugInConfiguration>(pluginDto.Id); plugin.IsActive = pluginDto.IsActive; plugin.CustomPlugInSource = pluginDto.CustomPlugInSource; plugin.ExternalAssemblyName = pluginDto.ExternalAssemblyName; if (pluginDto.Id == Guid.Empty) { plugin.TypeId = pluginDto.TypeId; } context.SaveChanges(); return(this.Ok(plugin.GetId())); } } catch (Exception ex) { Log.Error(ex.Message, ex); throw; } }
private static PlugInConfigurationDto BuildConfigurationDto(Type plugInType, GameConfiguration gameConfiguration, PlugInConfiguration plugInConfiguration) { var plugInAttribute = plugInType.GetCustomAttribute <PlugInAttribute>(); var plugInPoint = plugInType.GetInterfaces().FirstOrDefault(i => i.GetCustomAttribute <PlugInPointAttribute>() != null)?.GetCustomAttribute <PlugInPointAttribute>(); var customPlugInContainer = plugInType.GetInterfaces().FirstOrDefault(i => i.GetCustomAttribute <CustomPlugInContainerAttribute>() != null)?.GetCustomAttribute <CustomPlugInContainerAttribute>(); var dto = new PlugInConfigurationDto { Id = plugInConfiguration.GetId(), GameConfigurationId = gameConfiguration.GetId(), CustomPlugInSource = plugInConfiguration.CustomPlugInSource, ExternalAssemblyName = plugInConfiguration.ExternalAssemblyName, IsActive = plugInConfiguration.IsActive, TypeId = plugInConfiguration.TypeId, TypeName = plugInType.FullName, PlugInName = plugInAttribute?.Name, PlugInDescription = plugInAttribute?.Description, }; if (plugInPoint != null) { dto.PlugInPointName = plugInPoint.Name; dto.PlugInPointDescription = plugInPoint.Description; } else if (customPlugInContainer != null) { var customPlugInInterface = plugInType.GetInterfaces().FirstOrDefault(intf => intf.GetInterfaces().Any(i => i.GetCustomAttribute <CustomPlugInContainerAttribute>() != null)); dto.PlugInPointName = customPlugInInterface == null ? customPlugInContainer.Name : $"{customPlugInContainer.Name} - {customPlugInInterface.Name}"; dto.PlugInPointDescription = customPlugInContainer.Description; } else { dto.PlugInPointName = "N/A"; dto.PlugInPointDescription = "N/A"; } return(dto); }
public ActionResult <List <PlugInConfigurationDto> > List(int offset, int count) { var allPlugIns = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.DefinedTypes.Where(type => type.GetCustomAttribute <PlugInAttribute>() != null)) .ToDictionary(t => t.GUID, t => t); var skipped = 0; using (var context = this.persistenceContextProvider.CreateNewContext()) { var result = new List <PlugInConfigurationDto>(); foreach (var gameConfig in context.Get <GameConfiguration>()) { var rest = count - result.Count; if (rest == 0) { break; } foreach (var plugInConfiguration in gameConfig.PlugInConfigurations.Skip(offset - skipped).Take(rest)) { if (!allPlugIns.TryGetValue(plugInConfiguration.TypeId, out var plugInType)) { continue; } var plugInAttribute = plugInType.GetCustomAttribute <PlugInAttribute>(); var plugInPoint = plugInType.GetInterfaces().FirstOrDefault(intf => intf.GetCustomAttribute <PlugInPointAttribute>() != null)?.GetCustomAttribute <PlugInPointAttribute>(); var customPlugInContainer = plugInType.GetInterfaces().FirstOrDefault(intf => intf.GetCustomAttribute <CustomPlugInContainerAttribute>() != null)?.GetCustomAttribute <CustomPlugInContainerAttribute>(); var customPlugInInterface = plugInType.GetInterfaces().FirstOrDefault(intf => intf.GetInterfaces().Any(i => i.GetCustomAttribute <CustomPlugInContainerAttribute>() != null)); var dto = new PlugInConfigurationDto { Id = plugInConfiguration.GetId(), GameConfigurationId = gameConfig.GetId(), CustomPlugInSource = plugInConfiguration.CustomPlugInSource, ExternalAssemblyName = plugInConfiguration.ExternalAssemblyName, IsActive = plugInConfiguration.IsActive, TypeId = plugInConfiguration.TypeId, TypeName = plugInType.FullName, PlugInName = plugInAttribute?.Name, PlugInDescription = plugInAttribute?.Description, }; if (plugInPoint != null) { dto.PlugInPointName = plugInPoint.Name; dto.PlugInPointDescription = plugInPoint.Description; } else if (customPlugInContainer != null) { dto.PlugInPointName = $"{customPlugInContainer.Name} - {customPlugInInterface?.Name}"; dto.PlugInPointDescription = customPlugInContainer.Description; } else { dto.PlugInPointName = "N/A"; dto.PlugInPointDescription = "N/A"; } result.Add(dto); } skipped += Math.Min(gameConfig.PlugInConfigurations.Count, offset); } return(result); } }