72 lines
2.5 KiB
C#
72 lines
2.5 KiB
C#
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.Enum;
|
|
using Pf2e.Scrapper.Processors.Collection;
|
|
using Pf2e.Scrapper.Processors.Definitions;
|
|
|
|
namespace Pf2e.Scrapper.Processors.Factory;
|
|
|
|
public 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)
|
|
);
|
|
}
|
|
}
|
|
}
|