Add extensions to EntityPrototype

This commit is contained in:
Víctor Aguilera Puerto
2020-10-28 11:12:37 +01:00
parent 04cf4d4a20
commit 1f031ae842

View File

@@ -1,5 +1,6 @@
#nullable enable #nullable enable
using System; using System;
using JetBrains.Annotations;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC; using Robust.Shared.IoC;
@@ -7,8 +8,23 @@ using Robust.Shared.Prototypes;
namespace Content.Shared.Utility namespace Content.Shared.Utility
{ {
[UsedImplicitly]
public static class EntityPrototypeHelpers public static class EntityPrototypeHelpers
{ {
public static bool HasComponent<T>(this EntityPrototype prototype, IComponentFactory? componentFactory = null) where T : IComponent
{
return prototype.HasComponent(typeof(T), componentFactory);
}
public static bool HasComponent(this EntityPrototype prototype, Type component, IComponentFactory? componentFactory = null)
{
componentFactory ??= IoCManager.Resolve<IComponentFactory>();
var registration = componentFactory.GetRegistration(component);
return prototype.Components.ContainsKey(registration.Name);
}
public static bool HasComponent<T>(string prototype, IPrototypeManager? prototypeManager = null, IComponentFactory? componentFactory = null) where T : IComponent public static bool HasComponent<T>(string prototype, IPrototypeManager? prototypeManager = null, IComponentFactory? componentFactory = null) where T : IComponent
{ {
return HasComponent(prototype, typeof(T), prototypeManager, componentFactory); return HasComponent(prototype, typeof(T), prototypeManager, componentFactory);
@@ -17,16 +33,8 @@ namespace Content.Shared.Utility
public static bool HasComponent(string prototype, Type component, IPrototypeManager? prototypeManager = null, IComponentFactory? componentFactory = null) public static bool HasComponent(string prototype, Type component, IPrototypeManager? prototypeManager = null, IComponentFactory? componentFactory = null)
{ {
prototypeManager ??= IoCManager.Resolve<IPrototypeManager>(); prototypeManager ??= IoCManager.Resolve<IPrototypeManager>();
componentFactory ??= IoCManager.Resolve<IComponentFactory>();
var registration = componentFactory.GetRegistration(component); return prototypeManager.TryIndex(prototype, out EntityPrototype proto) && proto.HasComponent(component, componentFactory);
if (!prototypeManager.TryIndex(prototype, out EntityPrototype proto))
{
return proto.Components.ContainsKey(registration.Name);
}
return false;
} }
} }
} }