69 lines
1.5 KiB
C#
69 lines
1.5 KiB
C#
using System.Collections;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using PkmnLib.Static.Utils;
|
|
|
|
namespace PkmnLib.Dynamic.ScriptHandling;
|
|
|
|
/// <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>
|
|
public class ScriptContainer : IEnumerable<ScriptContainer>
|
|
{
|
|
/// <inheritdoc cref="ScriptContainer"/>
|
|
public ScriptContainer()
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc cref="ScriptContainer"/>
|
|
public ScriptContainer(Script script)
|
|
{
|
|
Script = script;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Whether this container is empty.
|
|
/// </summary>
|
|
[MemberNotNullWhen(false, nameof(ScriptHandling.Script))]
|
|
public bool IsEmpty => Script is null;
|
|
|
|
/// <summary>
|
|
/// The script in this container.
|
|
/// </summary>
|
|
public Script? Script { get; private set; }
|
|
|
|
/// <inheritdoc />
|
|
public IEnumerator<ScriptContainer> GetEnumerator()
|
|
{
|
|
yield return this;
|
|
}
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return GetEnumerator();
|
|
}
|
|
|
|
public void Set(Script script)
|
|
{
|
|
if (Script is not null)
|
|
{
|
|
Script.OnRemove();
|
|
Script.MarkForDeletion();
|
|
}
|
|
Script = script;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the script from this container.
|
|
/// </summary>
|
|
public void Clear()
|
|
{
|
|
if (Script is not null)
|
|
{
|
|
Script.OnRemove();
|
|
Script.MarkForDeletion();
|
|
}
|
|
Script = null;
|
|
}
|
|
} |