107 lines
3.3 KiB
C#
107 lines
3.3 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Diagnostics;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Newtonsoft.Json.Linq;
|
|
using Pf2eDataScrapper.App;
|
|
using Pf2eDataScrapper.Importers;
|
|
using Pf2eDataScrapper.Importers.Implementations.Aonprd;
|
|
using Pf2eDataScrapper.Importers.Implementations.FoundryEn;
|
|
|
|
namespace Pf2eDataScrapper.Processors;
|
|
|
|
internal sealed class ProcessorExecutor(
|
|
IServiceProvider serviceProvider,
|
|
ProcessorCollection collection,
|
|
ProcessProgress progress
|
|
)
|
|
{
|
|
public IServiceProvider ServiceProvider { get; } = serviceProvider;
|
|
public ProcessorCollection Collection { get; } = collection;
|
|
public ProcessProgress Progress { get; } = progress;
|
|
|
|
public void Start()
|
|
{
|
|
Collection.SortItems();
|
|
|
|
Progress.Processors.Reset(Collection.Items.Count);
|
|
|
|
foreach (ProcessorCollectionItem item in Collection.Items)
|
|
{
|
|
Progress.Processors.Start(item.Description);
|
|
|
|
Process(item);
|
|
|
|
Progress.Processors.End();
|
|
}
|
|
|
|
Progress.Processors.Complete();
|
|
}
|
|
|
|
private void Process(ProcessorCollectionItem item)
|
|
{
|
|
Thread thread = new(() =>
|
|
{
|
|
foreach (IProcessor processor in IterateProcessors(item))
|
|
{
|
|
processor.Process();
|
|
processor.Save();
|
|
}
|
|
});
|
|
}
|
|
|
|
private IEnumerable<IProcessor> IterateProcessors(ProcessorCollectionItem item)
|
|
{
|
|
if (!item.IsCollectionProcessor)
|
|
{
|
|
Progress.Local.Reset(1);
|
|
Progress.Local.Start(item.Description);
|
|
yield return (IProcessor)ActivatorUtilities.CreateInstance(ServiceProvider, item.Type);
|
|
Progress.Local.Complete();
|
|
yield break;
|
|
}
|
|
|
|
if (item.Group == ProcessorGroupEnum.General)
|
|
{
|
|
throw new(
|
|
$"Обработчик {item.Description} помечен как общий (поле Group == General) и коллекционный (поле InnerType), что запрещено."
|
|
);
|
|
}
|
|
|
|
Type importerType = item.Group switch
|
|
{
|
|
ProcessorGroupEnum.AonprdPreparation
|
|
or ProcessorGroupEnum.AonprdEntities
|
|
or ProcessorGroupEnum.AonprdSubclasses => typeof(AonprdImporter),
|
|
ProcessorGroupEnum.FoundryEnPreparation or ProcessorGroupEnum.FoundryEnEntities =>
|
|
typeof(FoundryEnImporter),
|
|
_ => throw new($"Не могу выбрать импортёр для группы {item.Group}"),
|
|
};
|
|
|
|
AbstractImporter importer = (AbstractImporter)
|
|
ActivatorUtilities.CreateInstance(ServiceProvider, importerType);
|
|
|
|
Progress.Local.Reset(importer.Compendium.Count(item.InnerType!));
|
|
|
|
foreach (
|
|
(AbstractCompendiumItem compendiumItem, JObject json) in importer.Compendium.Iterate(
|
|
item.InnerType!
|
|
)
|
|
)
|
|
{
|
|
Progress.Local.Start(compendiumItem.Id);
|
|
|
|
yield return (IProcessor)
|
|
ActivatorUtilities.CreateInstance(
|
|
ServiceProvider,
|
|
item.Type,
|
|
new ProcessorInput(compendiumItem.Id, json)
|
|
);
|
|
|
|
Progress.Local.End();
|
|
}
|
|
|
|
Progress.Local.Complete();
|
|
}
|
|
}
|