This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
mfgames-nitride-cil/src/Nitride/Entities/LinkSerialEntities.cs
2022-06-05 13:44:51 -05:00

53 lines
1.5 KiB
C#

using System.Collections.Generic;
using System.Linq;
using FluentValidation;
using Gallium;
namespace Nitride.Entities;
/// <summary>
/// Links a series of entities together in some manner. This assumes that
/// the entities coming into the operation are already ordered.
/// </summary>
[WithProperties]
public partial class LinkSerialEntities : OperationBase
{
private readonly IValidator<LinkSerialEntities> validator;
public LinkSerialEntities(IValidator<LinkSerialEntities> validator)
{
this.validator = validator;
}
/// <summary>
/// The callback function that sets the previous and next
/// </summary>
public LinkPreviousNextHandler? UpdatePreviousNext { get; set; }
/// <inheritdoc />
public override IEnumerable<Entity> Run(IEnumerable<Entity> input)
{
// Make sure everything is good.
this.validator.ValidateAndThrow(this);
// We pull all the entities into a list so we can walk through them
// more than once along with index access.
var list = input.ToList();
// Go through the list and assign each entity in order.
for (int i = 0; i < list.Count; i++)
{
Entity entity = list[i];
Entity? previous = i > 0 ? list[i - 1] : null;
Entity? next = i < list.Count - 1 ? list[i + 1] : null;
list[i] = this.UpdatePreviousNext!(entity, previous, next);
}
// Return the resulting list.
return list;
}
}