using JetBrains.Annotations;
using Robust.Shared.Log;
namespace Robust.Shared.GameObjects
{
[PublicAPI]
public static class ComponentExt
{
///
/// Convenience wrapper to implement "create component if it does not already exist".
/// Always gives you back a component, and creates it if it does not exist yet.
///
/// The entity to fetch or create the component on.
/// The existing component, or the new component if none existed yet.
/// The type of the component to fetch or create.
/// True if the component already existed, false if it had to be created.
public static bool EnsureComponent(this IEntity entity, out T component) where T : Component, new()
{
if (entity.TryGetComponent(out component!))
{
return true;
}
component = entity.AddComponent();
return false;
}
///
/// Convenience wrapper to implement "create component if it does not already exist".
/// Always gives you back a component, and creates it if it does not exist yet.
///
/// The entity to fetch or create the component on.
/// The type of the component to fetch or create.
/// The existing component, or the new component if none existed yet.
public static T EnsureComponent(this IEntity entity) where T : Component, new()
{
if (entity.TryGetComponent(out T? component))
{
return component;
}
return entity.AddComponent();
}
///
/// Convenience wrapper to implement "create component if it does not already exist".
/// Always gives you back a component, and creates it if it does not exist yet.
///
/// The entity to fetch or create the component on.
/// The existing component, or the new component if none existed yet.
///
/// The custom warning message to log if the component did not exist already.
/// Defaults to a predetermined warning if null.
///
/// The type of the component to fetch or create.
/// True if the component already existed, false if it had to be created.
public static bool EnsureComponentWarn(this IEntity entity, out T component, string? warning = null) where T : Component, new()
{
if (entity.TryGetComponent(out component!))
{
return true;
}
warning ??= $"Entity {entity} at {entity.Transform.MapPosition} did not have a {typeof(T)}";
Logger.Warning(warning);
component = entity.AddComponent();
return false;
}
///
/// Convenience wrapper to implement "create component if it does not already exist".
/// Always gives you back a component, and creates it if it does not exist yet.
///
/// The entity to fetch or create the component on.
/// The type of the component to fetch or create.
///
/// The custom warning message to log if the component did not exist already.
/// Defaults to a predetermined warning if null.
///
/// The existing component, or the new component if none existed yet.
public static T EnsureComponentWarn(this IEntity entity, string? warning = null) where T : Component, new()
{
if (entity.TryGetComponent(out T? component))
{
return component;
}
warning ??= $"Entity {entity} at {entity.Transform.MapPosition} did not have a {typeof(T)}";
Logger.Warning(warning);
return entity.AddComponent();
}
}
}