BililiveRecorder/BililiveRecorder.Flv/Pipeline/FlvProcessingContext.cs

74 lines
2.6 KiB
C#
Raw Normal View History

2021-02-08 16:51:19 +08:00
using System;
using System.Collections.Generic;
2021-03-09 00:50:13 +08:00
using System.Linq;
2021-03-09 01:04:31 +08:00
using System.Runtime.CompilerServices;
using BililiveRecorder.Flv.Pipeline.Actions;
using StructLinq;
2021-02-08 16:51:19 +08:00
namespace BililiveRecorder.Flv.Pipeline
{
public class FlvProcessingContext
{
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public FlvProcessingContext()
{
}
public FlvProcessingContext(PipelineAction data, IDictionary<object, object?> sessionItems)
{
this.Reset(data, sessionItems);
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
2021-03-09 00:50:13 +08:00
public List<PipelineAction> Actions { get; set; }
2021-02-08 16:51:19 +08:00
public IDictionary<object, object?> SessionItems { get; private set; }
public IDictionary<object, object?> LocalItems { get; private set; }
2021-02-27 20:44:04 +08:00
public List<ProcessingComment> Comments { get; private set; }
2021-02-08 16:51:19 +08:00
2021-03-09 00:50:13 +08:00
public void Reset(PipelineAction action, IDictionary<object, object?> sessionItems)
{
var actions = new List<PipelineAction> { action ?? throw new ArgumentNullException(nameof(action)) };
this.Reset(actions, sessionItems);
}
public void Reset(List<PipelineAction> actions, IDictionary<object, object?> sessionItems)
2021-02-08 16:51:19 +08:00
{
this.SessionItems = sessionItems ?? throw new ArgumentNullException(nameof(sessionItems));
2021-03-09 00:50:13 +08:00
this.Actions = actions;
2021-02-08 16:51:19 +08:00
this.LocalItems = new Dictionary<object, object?>();
2021-02-27 20:44:04 +08:00
this.Comments = new List<ProcessingComment>();
2021-02-08 16:51:19 +08:00
}
}
public static class FlvProcessingContextExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2021-02-27 20:44:04 +08:00
public static void AddComment(this FlvProcessingContext context, ProcessingComment comment)
2021-02-08 16:51:19 +08:00
=> context.Comments.Add(comment);
2021-03-09 00:50:13 +08:00
public static bool PerActionRun(this FlvProcessingContext context, Func<FlvProcessingContext, PipelineAction, IEnumerable<PipelineAction?>> func)
{
var success = true;
var actions = context.Actions;
var result = new List<PipelineAction>();
foreach (var output in actions.SelectMany(action => func(context, action)))
{
if (output is null)
{
success = false;
goto exit;
}
result.Add(output);
}
exit:
context.Actions = result;
return success;
}
2021-02-08 16:51:19 +08:00
}
}