2023-01-17 04:09:06 +00:00
|
|
|
using System;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
using MfGames.Gallium;
|
|
|
|
using MfGames.Nitride.Generators;
|
|
|
|
|
2023-01-17 04:10:24 +00:00
|
|
|
using Zio;
|
|
|
|
|
2023-01-17 04:09:06 +00:00
|
|
|
namespace MfGames.Nitride.Temporal.Schedules;
|
|
|
|
|
|
|
|
/// <summary>
|
2023-01-21 05:28:41 +00:00
|
|
|
/// A common base for a schedule that is built off the path associated with
|
|
|
|
/// the entity.
|
2023-01-17 04:09:06 +00:00
|
|
|
/// </summary>
|
|
|
|
[WithProperties]
|
2023-01-21 05:28:41 +00:00
|
|
|
public abstract partial class PathRegexScheduleBase : ISchedule
|
2023-01-17 04:09:06 +00:00
|
|
|
{
|
2023-01-21 05:28:41 +00:00
|
|
|
protected PathRegexScheduleBase()
|
2023-01-17 04:09:06 +00:00
|
|
|
{
|
2023-01-19 05:51:37 +00:00
|
|
|
this.PathRegex = @"^.*?(\d+)[^\d]*$";
|
2023-01-18 00:42:31 +00:00
|
|
|
this.GetPath = entity => entity.Get<UPath>().ToString();
|
2023-01-17 04:09:06 +00:00
|
|
|
this.CaptureGroup = 1;
|
|
|
|
}
|
|
|
|
|
2023-01-17 04:10:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the group number of the capture group with 1 being being the
|
|
|
|
/// first group in PathRegex.
|
|
|
|
/// </summary>
|
|
|
|
public int CaptureGroup { get; set; }
|
2023-01-17 04:09:06 +00:00
|
|
|
|
2023-01-17 04:10:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the offset to make the resulting capture group a zero-based
|
|
|
|
/// number.
|
|
|
|
/// </summary>
|
|
|
|
public int CaptureOffset { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the method for retrieving the path for the entity.
|
|
|
|
/// </summary>
|
2023-01-17 04:09:06 +00:00
|
|
|
public Func<Entity, string> GetPath { get; set; }
|
2023-01-17 04:10:24 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the regular expression to identify a matching path.
|
|
|
|
/// </summary>
|
2023-01-19 04:32:54 +00:00
|
|
|
public string? PathRegex { get; set; }
|
2023-01-17 04:10:24 +00:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public virtual Entity Apply(
|
|
|
|
Entity entity,
|
2023-01-21 07:52:52 +00:00
|
|
|
TimeService timeService)
|
2023-01-17 04:10:24 +00:00
|
|
|
{
|
|
|
|
// Get the path and match it.
|
2023-01-21 05:28:41 +00:00
|
|
|
string path = this.GetPath(entity);
|
2023-01-19 04:32:54 +00:00
|
|
|
Regex? regex = this.GetRegex();
|
2023-01-21 05:28:41 +00:00
|
|
|
Match match = regex?.Match(path)
|
2023-01-17 04:10:24 +00:00
|
|
|
?? throw new NullReferenceException(
|
|
|
|
"PathRegex was not configured for the "
|
|
|
|
+ this.GetType().Name
|
|
|
|
+ ".");
|
|
|
|
|
|
|
|
if (!match.Success)
|
|
|
|
{
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
2023-01-19 05:51:37 +00:00
|
|
|
if (match.Groups.Count < 2)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
"There must be at least one capture group in '"
|
|
|
|
+ this.PathRegex
|
|
|
|
+ "'.");
|
|
|
|
}
|
|
|
|
|
2023-01-17 04:10:24 +00:00
|
|
|
// Figure out the index/number of this entry.
|
2023-01-19 05:51:37 +00:00
|
|
|
string numberValue = match.Groups[this.CaptureGroup].Value;
|
|
|
|
|
|
|
|
if (!int.TryParse(numberValue, out int number))
|
|
|
|
{
|
|
|
|
throw new FormatException(
|
|
|
|
path + ": Cannot parse '" + numberValue + "' as integer.");
|
|
|
|
}
|
|
|
|
|
|
|
|
number += this.CaptureOffset;
|
2023-01-17 04:10:24 +00:00
|
|
|
|
2023-01-21 05:28:41 +00:00
|
|
|
// Pass it onto the extending class.
|
2023-01-21 07:52:52 +00:00
|
|
|
return this.Apply(entity, number, timeService);
|
2023-01-17 04:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public virtual bool CanApply(Entity entity)
|
|
|
|
{
|
2023-01-19 04:32:54 +00:00
|
|
|
string path = this.GetPath(entity);
|
|
|
|
Regex? regex = this.GetRegex();
|
2023-01-21 05:28:41 +00:00
|
|
|
Match match = regex?.Match(path)
|
|
|
|
?? throw new NullReferenceException(
|
|
|
|
"PathRegex was not configured for the "
|
|
|
|
+ this.GetType().Name
|
|
|
|
+ ".");
|
2023-01-17 04:10:24 +00:00
|
|
|
|
2023-01-21 05:28:41 +00:00
|
|
|
return match.Success;
|
2023-01-17 04:10:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 05:28:41 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Applies the schedule according to the normalized number (after
|
|
|
|
/// CaptureOffset).
|
|
|
|
/// </summary>
|
|
|
|
protected abstract Entity Apply(
|
|
|
|
Entity entity,
|
|
|
|
int number,
|
2023-01-21 07:52:52 +00:00
|
|
|
TimeService timeService);
|
2023-01-21 05:28:41 +00:00
|
|
|
|
|
|
|
private Regex? GetRegex()
|
2023-01-19 04:32:54 +00:00
|
|
|
{
|
|
|
|
return this.PathRegex == null
|
|
|
|
? null
|
|
|
|
: new Regex(this.PathRegex);
|
|
|
|
}
|
2023-01-17 04:09:06 +00:00
|
|
|
}
|