using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Reflection;
using Robust.Shared.IoC;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
{
public interface INodeFactory
{
///
/// Performs reflection to associate implementations with the
/// string specified in their .
///
void Initialize();
///
/// Returns a new instance.
///
Node MakeNode(string nodeName, NodeGroupID groupID, IEntity owner);
}
public class NodeFactory : INodeFactory
{
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 nodeTypes = _reflectionManager.GetAllChildren();
foreach (var nodeType in nodeTypes)
{
var att = nodeType.GetCustomAttribute();
if (att != null)
{
_groupTypes.Add(att.Name, nodeType);
}
}
}
public Node MakeNode(string nodeName, NodeGroupID groupID, IEntity owner)
{
if (_groupTypes.TryGetValue(nodeName, out var type))
{
var newNode = _typeFactory.CreateInstance(type);
newNode.Initialize(groupID, owner);
return newNode;
}
throw new ArgumentException($"{nodeName} did not have an associated {nameof(Node)}.");
}
}
}