using System.Collections;
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
namespace Content.Shared.EntityTable;
///
/// Context used by selectors and conditions to evaluate in generic gamestate information.
///
public sealed class EntityTableContext : IEnumerable
{
private readonly Dictionary _data = new();
///
/// Retrieves an arbitrary piece of data from the context based on a provided key.
///
/// A string key that corresponds to the value we are searching for.
/// The value we are trying to extract from the context object
/// The type of that we are trying to retrieve
/// If has a corresponding value of type
[PublicAPI]
public bool TryGetData(EntityTableContextKey key, [NotNullWhen(true)] out T? value)
{
value = default;
if (!_data.TryGetValue(key.Key, out var valueData) || valueData is not T castValueData)
return false;
value = castValueData;
return true;
}
///
/// Sets data into context using provided key.
///
[PublicAPI]
public void SetData(EntityTableContextKey key, T data) where T : notnull
{
_data[key.Key] = data;
}
///
/// Add method duplicates , is used for object initializer.
///
/// Thrown if there is already a value stored under same key.
[PublicAPI]
public void Add(EntityTableContextKey key, T data) where T : notnull
{
_data.Add(key.Key, data);
}
///
/// Removes data from the context, if the key exists.
///
[PublicAPI]
public void RemoveData(EntityTableContextKey key)
{
_data.Remove(key.Key);
}
///
public IEnumerator GetEnumerator()
{
return _data.GetEnumerator();
}
}
///
/// Key for , used to strongly type calls for setting and getting entities,
/// should usually be static readonly fields as anchors.
///
/// Type of value to store/extract to/from context.
///
/// String value under which data will be stored inside. They should not have collisions, otherwise data will be rewritten.
///
public readonly record struct EntityTableContextKey(string Key);