Adds the ability to register a single prototype class with the PrototypeManager.

This commit is contained in:
Acruid
2020-07-10 03:21:10 -07:00
parent 6245b2223b
commit b1a19e8942

View File

@@ -72,6 +72,13 @@ namespace Robust.Shared.Prototypes
/// </summary>
void RegisterIgnore(string name);
/// <summary>
/// Loads a single prototype class type into the manager.
/// </summary>
/// <param name="protoClass">A prototype class type that implements IPrototype. This type also
/// requires a <see cref="PrototypeAttribute"/> with a non-empty class string.</param>
void RegisterType(Type protoClass);
event Action<YamlStream, string>? LoadedData;
}
@@ -281,24 +288,7 @@ namespace Robust.Shared.Prototypes
Clear();
foreach (var type in ReflectionManager.GetAllChildren<IPrototype>())
{
var attribute = (PrototypeAttribute?)Attribute.GetCustomAttribute(type, typeof(PrototypeAttribute));
if (attribute == null)
{
throw new InvalidImplementationException(type, typeof(IPrototype), "No " + nameof(PrototypeAttribute) + " to give it a type string.");
}
if (prototypeTypes.ContainsKey(attribute.Type))
{
throw new InvalidImplementationException(type, typeof(IPrototype),
$"Duplicate prototype type ID: {attribute.Type}. Current: {prototypeTypes[attribute.Type]}");
}
prototypeTypes[attribute.Type] = type;
prototypes[type] = new List<IPrototype>();
if (typeof(IIndexedPrototype).IsAssignableFrom(type))
{
indexedPrototypes[type] = new Dictionary<string, IIndexedPrototype>();
}
RegisterType(type);
}
}
@@ -359,6 +349,37 @@ namespace Robust.Shared.Prototypes
IgnoredPrototypeTypes.Add(name);
}
/// <inheritdoc />
public void RegisterType(Type type)
{
if(!(typeof(IPrototype).IsAssignableFrom(type)))
throw new InvalidOperationException("Type must implement IPrototype.");
var attribute = (PrototypeAttribute?)Attribute.GetCustomAttribute(type, typeof(PrototypeAttribute));
if (attribute == null)
{
throw new InvalidImplementationException(type,
typeof(IPrototype),
"No " + nameof(PrototypeAttribute) + " to give it a type string.");
}
if (prototypeTypes.ContainsKey(attribute.Type))
{
throw new InvalidImplementationException(type,
typeof(IPrototype),
$"Duplicate prototype type ID: {attribute.Type}. Current: {prototypeTypes[attribute.Type]}");
}
prototypeTypes[attribute.Type] = type;
prototypes[type] = new List<IPrototype>();
if (typeof(IIndexedPrototype).IsAssignableFrom(type))
{
indexedPrototypes[type] = new Dictionary<string, IIndexedPrototype>();
}
}
public event Action<YamlStream, string>? LoadedData;
}