BililiveRecorder/BililiveRecorder.Flv/Pipeline/IProcessingPipelineBuilderExtensions.cs

46 lines
2.3 KiB
C#
Raw Normal View History

2021-02-08 16:51:19 +08:00
using System;
using BililiveRecorder.Flv.Pipeline.Rules;
using Microsoft.Extensions.DependencyInjection;
namespace BililiveRecorder.Flv.Pipeline
{
public static class IProcessingPipelineBuilderExtensions
{
public static IProcessingPipelineBuilder ConfigureServices(this IProcessingPipelineBuilder builder, Action<IServiceCollection> configure)
{
configure?.Invoke(builder.ServiceCollection);
return builder;
}
public static IProcessingPipelineBuilder AddRule<T>(this IProcessingPipelineBuilder builder) where T : IProcessingRule =>
builder.AddRule((next, services) => ActivatorUtilities.GetServiceOrCreateInstance<T>(services) switch
2021-02-08 16:51:19 +08:00
{
2021-03-09 00:50:13 +08:00
ISimpleProcessingRule simple => context => simple.Run(context, () => next(context)),
IFullProcessingRule full => context => full.Run(context, next),
2021-02-08 16:51:19 +08:00
_ => throw new ArgumentException($"Type ({typeof(T).FullName}) does not ISimpleProcessingRule or IFullProcessingRule")
});
public static IProcessingPipelineBuilder AddRule<T>(this IProcessingPipelineBuilder builder, T instance) where T : IProcessingRule =>
2021-02-08 16:51:19 +08:00
instance switch
{
ISimpleProcessingRule simple => builder.AddRule((next, services) => context => simple.Run(context, () => next(context))),
IFullProcessingRule full => builder.AddRule((next, services) => context => full.Run(context, next)),
2021-02-08 16:51:19 +08:00
_ => throw new ArgumentException($"Type ({typeof(T).FullName}) does not ISimpleProcessingRule or IFullProcessingRule")
};
public static IProcessingPipelineBuilder AddDefaultRules(this IProcessingPipelineBuilder builder) =>
2021-02-08 16:51:19 +08:00
builder
.AddRule<HandleEndTagRule>()
.AddRule<HandleDelayedAudioHeaderRule>()
// .AddRule<UpdateTimestampOffsetRule>()
.AddRule<UpdateTimestampJumpRule>()
.AddRule<HandleNewScriptRule>()
.AddRule<HandleNewHeaderRule>()
.AddRule<RemoveDuplicatedChunkRule>()
2021-02-08 16:51:19 +08:00
;
public static IProcessingPipelineBuilder AddRemoveFillerDataRule(this IProcessingPipelineBuilder builder) =>
builder.AddRule<RemoveFillerDataRule>();
2021-02-08 16:51:19 +08:00
}
}