153 lines
5.0 KiB
C#
153 lines
5.0 KiB
C#
using System.ComponentModel;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Newtonsoft.Json.Linq;
|
|
using Pf2eDataScrapper.App;
|
|
using Pf2eDataScrapper.Helpers;
|
|
using Pf2eDataScrapper.Importers;
|
|
using Pf2eDataScrapper.Importers.Implementations.Aonprd.Compendium;
|
|
using Pf2eDataScrapper.Importers.Implementations.FoundryEn;
|
|
using Pf2eModel;
|
|
|
|
namespace Pf2eDataScrapper.Processors;
|
|
|
|
internal sealed class ProcessorExecutor(
|
|
IServiceProvider serviceProvider,
|
|
ProcessorCollection collection,
|
|
ProcessProgress progress,
|
|
Pf2eDbContext context,
|
|
AonprdCompendium aonprdCompendium,
|
|
FoundryEnCompendium foundryEnCompendium
|
|
)
|
|
{
|
|
public IServiceProvider ServiceProvider { get; } = serviceProvider;
|
|
public ProcessorCollection Collection { get; } = collection;
|
|
public ProcessProgress Progress { get; } = progress;
|
|
public Pf2eDbContext Context { get; } = context;
|
|
public AonprdCompendium AonprdCompendium { get; } = aonprdCompendium;
|
|
public FoundryEnCompendium FoundryEnCompendium { get; } = foundryEnCompendium;
|
|
|
|
public void Start()
|
|
{
|
|
Collection.SortItems();
|
|
|
|
Progress.Processors.Reset(Collection.Items.Count);
|
|
|
|
StartItem();
|
|
}
|
|
|
|
private int _currentIndex = 0;
|
|
|
|
private void StartItem()
|
|
{
|
|
ProcessorCollectionItem item = Collection.Items[_currentIndex];
|
|
|
|
Progress.Processors.Start(item.Description);
|
|
Progress.Progress(Progress.Processors.ToString());
|
|
|
|
BackgroundWorker worker = new();
|
|
|
|
worker.DoWork += (_, _) =>
|
|
{
|
|
foreach (IProcessor processor in IterateProcessors(item))
|
|
{
|
|
try
|
|
{
|
|
processor.Process();
|
|
processor.Save();
|
|
}
|
|
catch (SkipItemException exception)
|
|
{
|
|
Progress.Information(exception.Message);
|
|
}
|
|
}
|
|
Context.SaveChanges();
|
|
};
|
|
|
|
worker.RunWorkerCompleted += Continue;
|
|
|
|
worker.RunWorkerAsync();
|
|
}
|
|
|
|
private void Continue(object? sender, RunWorkerCompletedEventArgs e)
|
|
{
|
|
Progress.Processors.End();
|
|
_currentIndex++;
|
|
|
|
if (_currentIndex < Collection.Items.Count)
|
|
{
|
|
StartItem();
|
|
}
|
|
else
|
|
{
|
|
Progress.Processors.Complete();
|
|
}
|
|
}
|
|
|
|
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), что запрещено."
|
|
);
|
|
}
|
|
|
|
AbstractCompendium compendium = item.Group switch
|
|
{
|
|
ProcessorGroupEnum.AonprdPreparation
|
|
or ProcessorGroupEnum.AonprdEntities
|
|
or ProcessorGroupEnum.AonprdSubclasses => AonprdCompendium,
|
|
ProcessorGroupEnum.FoundryEnPreparation or ProcessorGroupEnum.FoundryEnEntities =>
|
|
FoundryEnCompendium,
|
|
_ => throw new($"Не могу выбрать импортёр для группы {item.Group}"),
|
|
};
|
|
|
|
Progress.Local.Reset(compendium.Count(item.InnerType!));
|
|
|
|
foreach (
|
|
(AbstractCompendiumItem compendiumItem, JObject json) in compendium.Iterate(
|
|
item.InnerType!
|
|
)
|
|
)
|
|
{
|
|
Progress.Local.Start(compendiumItem.Id);
|
|
|
|
ProcessorInput processorInput = ActivatorUtilities.CreateInstance<ProcessorInput>(
|
|
ServiceProvider,
|
|
compendiumItem.Id,
|
|
json
|
|
);
|
|
|
|
yield return (IProcessor)
|
|
ActivatorUtilities.CreateInstance(ServiceProvider, item.Type, processorInput);
|
|
|
|
Progress.Local.End();
|
|
}
|
|
|
|
Progress.Local.Complete();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Исключение, показывающее, что обработка сущности пропускается как устаревшая.
|
|
/// </summary>
|
|
/// <param name="name">Название сущности.</param>
|
|
public class LegacyItemException(string name) : SkipItemException(name, "Устаревший элемент") { }
|
|
|
|
/// <summary>
|
|
/// Исключение, показывающее, что обработка сущности пропускается.
|
|
/// </summary>
|
|
/// <param name="name">Название сущности.</param>
|
|
/// <param name="reason">Причина пропуска.</param>
|
|
public class SkipItemException(string name, string reason)
|
|
: Exception($"Пропускаю элемент '{name}', указана причина: {reason}.") { }
|