2021-06-09 22:19:39 +02:00
|
|
|
using System.Reflection;
|
2023-08-12 22:41:55 +02:00
|
|
|
using Content.Server.Power.Generation.Teg;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Robust.Shared.Reflection;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.NodeContainer.NodeGroups
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
public interface INodeGroupFactory
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs reflection to associate <see cref="INodeGroup"/> implementations with the
|
|
|
|
|
/// string specified in their <see cref="NodeGroupAttribute"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
void Initialize();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a new <see cref="INodeGroup"/> instance.
|
|
|
|
|
/// </summary>
|
2021-07-04 18:11:52 +02:00
|
|
|
INodeGroup MakeNodeGroup(NodeGroupID id);
|
2020-06-28 09:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class NodeGroupFactory : INodeGroupFactory
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly IReflectionManager _reflectionManager = default!;
|
|
|
|
|
[Dependency] private readonly IDynamicTypeFactory _typeFactory = default!;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
2020-11-27 11:00:49 +01:00
|
|
|
private readonly Dictionary<NodeGroupID, Type> _groupTypes = new();
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
2020-08-27 09:45:27 -06:00
|
|
|
var nodeGroupTypes = _reflectionManager.GetAllChildren<INodeGroup>();
|
2020-06-28 09:23:26 -06:00
|
|
|
foreach (var nodeGroupType in nodeGroupTypes)
|
|
|
|
|
{
|
|
|
|
|
var att = nodeGroupType.GetCustomAttribute<NodeGroupAttribute>();
|
|
|
|
|
if (att != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var groupID in att.NodeGroupIDs)
|
|
|
|
|
{
|
|
|
|
|
_groupTypes.Add(groupID, nodeGroupType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-04 18:11:52 +02:00
|
|
|
public INodeGroup MakeNodeGroup(NodeGroupID id)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
2021-07-04 18:11:52 +02:00
|
|
|
if (!_groupTypes.TryGetValue(id, out var type))
|
|
|
|
|
throw new ArgumentException($"{id} did not have an associated {nameof(INodeGroup)} implementation.");
|
|
|
|
|
|
|
|
|
|
var instance = _typeFactory.CreateInstance<INodeGroup>(type);
|
|
|
|
|
instance.Create(id);
|
|
|
|
|
return instance;
|
2020-06-28 09:23:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 13:54:25 +10:00
|
|
|
public enum NodeGroupID : byte
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
Default,
|
|
|
|
|
HVPower,
|
|
|
|
|
MVPower,
|
|
|
|
|
Apc,
|
2020-08-29 06:05:44 -05:00
|
|
|
AMEngine,
|
2020-08-27 09:45:27 -06:00
|
|
|
Pipe,
|
2023-04-10 15:37:03 +10:00
|
|
|
WireNet,
|
2023-08-12 22:41:55 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Group used by the TEG.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <seealso cref="TegSystem"/>
|
|
|
|
|
/// <seealso cref="TegNodeGroup"/>
|
|
|
|
|
Teg,
|
2020-06-28 09:23:26 -06:00
|
|
|
}
|
|
|
|
|
}
|