using System; using System.Collections.Generic; using System.Reflection; using Robust.Shared.Interfaces.Reflection; using Robust.Shared.IoC; namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups { public interface INodeGroupFactory { /// /// Performs reflection to associate implementations with the /// string specified in their . /// void Initialize(); /// /// Returns a new instance. /// INodeGroup MakeNodeGroup(NodeGroupID nodeGroupType); } public class NodeGroupFactory : INodeGroupFactory { private readonly Dictionary _groupTypes = new Dictionary(); #pragma warning disable 649 [Dependency] private readonly IReflectionManager _reflectionManager; [Dependency] private readonly IDynamicTypeFactory _typeFactory; #pragma warning restore 649 public void Initialize() { var nodeGroupTypes = _reflectionManager.GetAllChildren(); foreach (var nodeGroupType in nodeGroupTypes) { var att = nodeGroupType.GetCustomAttribute(); if (att != null) { foreach (var groupID in att.NodeGroupIDs) { _groupTypes.Add(groupID, nodeGroupType); } } } } public INodeGroup MakeNodeGroup(NodeGroupID nodeGroupType) { if (_groupTypes.TryGetValue(nodeGroupType, out var type)) { return _typeFactory.CreateInstance(type); } throw new ArgumentException($"{nodeGroupType} did not have an associated {nameof(INodeGroup)}."); } } public enum NodeGroupID { Default, HVPower, MVPower, Apc, } }