using System.Collections.Generic;
using JetBrains.Annotations;
using Robust.Shared.IoC;
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 EntityUid entity, out T component) where T : Component, new()
{
var entMan = IoCManager.Resolve();
if (entMan.TryGetComponent(entity, out var comp))
{
component = comp;
return true;
}
component = entMan.AddComponent(entity);
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 EntityUid entity) where T : Component, new()
{
var entMan = IoCManager.Resolve();
if (entMan.TryGetComponent(entity, out T? component))
{
return component;
}
return entMan.AddComponent(entity);
}
///
/// 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 EntityUid entity, out T component, string? warning = null) where T : Component, new()
{
var entMan = IoCManager.Resolve();
if (entMan.TryGetComponent(entity, out var comp))
{
component = comp;
return true;
}
warning ??= $"Entity {entity} at {entMan.GetComponent(entity).MapPosition} did not have a {typeof(T)}";
Logger.Warning(warning);
component = entMan.AddComponent(entity);
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 EntityUid entity, string? warning = null) where T : Component, new()
{
var entMan = IoCManager.Resolve();
if (entMan.TryGetComponent(entity, out T? component))
{
return component;
}
warning ??= $"Entity {entity} at {entMan.GetComponent(entity).MapPosition} did not have a {typeof(T)}";
Logger.Warning(warning);
return entMan.AddComponent(entity);
}
public static IComponent SetAndDirtyIfChanged(
this IComponent comp,
ref TValue backingField,
TValue value)
{
if (EqualityComparer.Default.Equals(backingField, value))
{
return comp;
}
backingField = value;
comp.Dirty();
return comp;
}
}
}