PkmnLib.NET/PkmnLib.Dynamic/ScriptHandling/ScriptIterator.cs

55 lines
1.5 KiB
C#
Raw Normal View History

2024-07-27 14:26:45 +00:00
using System.Collections;
namespace PkmnLib.Dynamic.ScriptHandling;
public class ScriptIterator : IEnumerable<ScriptContainer>
{
private readonly IReadOnlyList<IEnumerable<ScriptContainer>> _scripts;
public ScriptIterator(IReadOnlyList<IEnumerable<ScriptContainer>> scripts)
{
_scripts = scripts;
}
2024-07-28 09:56:34 +00:00
private IEnumerable<ScriptContainer> GetAsEnumerable()
2024-07-27 14:26:45 +00:00
{
2024-07-28 09:56:34 +00:00
foreach (var enumerable in _scripts)
2024-07-27 14:26:45 +00:00
{
2024-07-28 09:56:34 +00:00
if (enumerable is IScriptSet set)
2024-07-27 14:26:45 +00:00
{
2024-07-28 09:56:34 +00:00
// We can't assume that the set remains the same size, so we need to iterate through it
// with a for loop.
for (var j = 0; j < set.Count; j++)
2024-07-27 14:26:45 +00:00
{
2024-07-28 09:56:34 +00:00
var script = set.At(j);
// We can ignore empty scripts.
if (script.IsEmpty)
continue;
yield return script;
2024-07-27 14:26:45 +00:00
}
}
2024-07-28 09:56:34 +00:00
else
2024-07-27 14:26:45 +00:00
{
2024-07-28 09:56:34 +00:00
foreach (var script in enumerable)
{
// We can ignore empty scripts.
if (script.IsEmpty)
continue;
yield return script;
}
2024-07-27 14:26:45 +00:00
}
}
}
/// <inheritdoc />
public IEnumerator<ScriptContainer> GetEnumerator()
{
2024-07-28 09:56:34 +00:00
return GetAsEnumerable().GetEnumerator();
2024-07-27 14:26:45 +00:00
}
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}