PkmnLib.NET/PkmnLib.Dynamic/ScriptHandling/ScriptContainer.cs

78 lines
1.8 KiB
C#
Raw Normal View History

2024-07-27 14:26:45 +00:00
using System.Collections;
2024-07-27 14:53:06 +00:00
using System.Diagnostics.CodeAnalysis;
using PkmnLib.Static.Utils;
2024-07-27 14:26:45 +00:00
namespace PkmnLib.Dynamic.ScriptHandling;
2025-03-08 09:26:45 +00:00
public interface IReadOnlyScriptContainer : IEnumerable<ScriptContainer>, IDeepCloneable
{
/// <summary>
/// Whether this container is empty.
/// </summary>
public bool IsEmpty { get; }
/// <summary>
/// The script in this container.
/// </summary>
public Script? Script { get; }
}
2024-07-28 10:52:17 +00:00
/// <summary>
/// A holder class for a script. This is used so we can cache a list of these, and iterate over them, even when
/// the underlying script changes.
/// </summary>
2025-03-08 09:26:45 +00:00
public class ScriptContainer : IReadOnlyScriptContainer
2024-07-27 14:26:45 +00:00
{
2024-07-28 12:00:26 +00:00
/// <inheritdoc cref="ScriptContainer"/>
public ScriptContainer()
{
}
/// <inheritdoc cref="ScriptContainer"/>
public ScriptContainer(Script script)
{
Script = script;
}
2025-03-08 09:26:45 +00:00
/// <inheritdoc />
2024-07-27 14:53:06 +00:00
[MemberNotNullWhen(false, nameof(ScriptHandling.Script))]
2024-07-28 10:52:17 +00:00
public bool IsEmpty => Script is null;
2025-03-08 09:26:45 +00:00
/// <inheritdoc />
public Script? Script { get; private set; }
2024-07-27 14:26:45 +00:00
/// <inheritdoc />
public IEnumerator<ScriptContainer> GetEnumerator()
{
yield return this;
}
2024-07-28 10:52:17 +00:00
2025-03-02 16:19:57 +00:00
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
2024-08-10 09:18:10 +00:00
/// <summary>
/// Assigns a new script to this container. If there was a script already, it is removed.
/// </summary>
/// <param name="script"></param>
public void Set(Script script)
{
Script?.OnRemove();
Script = script;
}
/// <summary>
/// Removes the script from this container.
/// </summary>
public void Clear()
{
if (Script is not null)
{
Script.OnRemove();
}
Script = null;
}
2025-03-02 16:19:57 +00:00
public void ClearWithoutRemoving()
{
Script = null;
}
2024-07-27 14:26:45 +00:00
}