Глобальный рефакторинг пространств имён
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Pf2e.Scrapper.Processors.Definitions;
|
||||
using Pf2e.Scrapper.Processors.Collection;
|
||||
|
||||
namespace Pf2e.Scrapper.Processors.Factory;
|
||||
|
||||
/// <summary>
|
||||
/// Абстрактная фабрика создания процессоров.
|
||||
/// </summary>
|
||||
internal abstract class AbstractProcessorFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Подготовить фабрику к работе.
|
||||
/// </summary>
|
||||
public virtual void Prepare() { }
|
||||
|
||||
/// <summary>
|
||||
/// Количество элеметов для итерирования.
|
||||
/// </summary>
|
||||
/// <returns>Количество элеметов</returns>
|
||||
public abstract int Count();
|
||||
|
||||
/// <summary>
|
||||
/// Итератор процессоров.
|
||||
/// </summary>
|
||||
/// <returns>Пара значений: [0] - описание элемента процессор, [1] - процессор.</returns>
|
||||
public abstract IEnumerable<Tuple<string, IProcessor>> GetEnumerator();
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Pf2e.Scrapper.Importers;
|
||||
using Pf2e.Scrapper.Importers.Implementations.Aonprd.Compendium;
|
||||
using Pf2e.Scrapper.Importers.Implementations.FoundryEn.Compendium;
|
||||
using Pf2e.Scrapper.Processors.Attributes;
|
||||
using Pf2e.Scrapper.Processors.Collection;
|
||||
using Pf2e.Scrapper.Processors.Definitions;
|
||||
|
||||
namespace Pf2e.Scrapper.Processors.Factory;
|
||||
|
||||
internal class CompendiumProcessorFactory(ProcessorRecord item, IServiceProvider serviceProvider)
|
||||
: AbstractProcessorFactory
|
||||
{
|
||||
public ProcessorRecord Item { get; } = item;
|
||||
public IServiceProvider ServiceProvider { get; } = serviceProvider;
|
||||
|
||||
public ProcessorCompendiumEnum? CompendiumType { get; set; } = null;
|
||||
public string? InnerType { get; set; } = null;
|
||||
|
||||
public AbstractCompendium Compendium { get; private set; } = null!;
|
||||
public ICollection<string> Types { get; private set; } = [];
|
||||
|
||||
public override void Prepare()
|
||||
{
|
||||
if (CompendiumType is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(CompendiumType));
|
||||
}
|
||||
|
||||
base.Prepare();
|
||||
|
||||
ProcessorCollection collection =
|
||||
ServiceProvider.GetService<ProcessorCollection>() ?? throw new();
|
||||
|
||||
Compendium =
|
||||
CompendiumType switch
|
||||
{
|
||||
ProcessorCompendiumEnum.Aonprd => ServiceProvider.GetService<AonprdCompendium>(),
|
||||
ProcessorCompendiumEnum.FoundryEn =>
|
||||
ServiceProvider.GetService<FoundryEnCompendium>(),
|
||||
_ => (AbstractCompendium?)null,
|
||||
} ?? throw new($"Не могу выбрать импортёр для группы {Item.Group}");
|
||||
|
||||
Types = InnerType is null
|
||||
? collection.GetTypesOfCompendium(CompendiumType ?? throw new())
|
||||
: [InnerType];
|
||||
}
|
||||
|
||||
public override int Count() => Compendium.Count(Types);
|
||||
|
||||
public override IEnumerable<Tuple<string, IProcessor>> GetEnumerator()
|
||||
{
|
||||
foreach ((AbstractCompendiumItem compendiumItem, JObject json) in Compendium.Iterate(Types))
|
||||
{
|
||||
ProcessorInput processorInput = ActivatorUtilities.CreateInstance<ProcessorInput>(
|
||||
ServiceProvider,
|
||||
compendiumItem.Id,
|
||||
json
|
||||
);
|
||||
|
||||
yield return new(
|
||||
compendiumItem.Id,
|
||||
(IProcessor)
|
||||
ActivatorUtilities.CreateInstance(ServiceProvider, Item.Type, processorInput)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Pf2e.Scrapper.Processors.Collection;
|
||||
using Pf2e.Scrapper.Processors.Definitions;
|
||||
|
||||
namespace Pf2e.Scrapper.Processors.Factory;
|
||||
|
||||
internal class SimpleProcessorFactory(ProcessorRecord item, IServiceProvider serviceProvider)
|
||||
: AbstractProcessorFactory
|
||||
{
|
||||
public ProcessorRecord Item { get; } = item;
|
||||
public IServiceProvider ServiceProvider { get; } = serviceProvider;
|
||||
|
||||
public override int Count() => 1;
|
||||
|
||||
public override IEnumerable<Tuple<string, IProcessor>> GetEnumerator()
|
||||
{
|
||||
yield return new(
|
||||
Item.Description,
|
||||
(IProcessor)ActivatorUtilities.CreateInstance(ServiceProvider, Item.Type)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user