68 lines
2.2 KiB
C#
68 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace Pf2e.Scrapper.Importers.Implementations.FoundryEn.Compendium;
|
|
|
|
/// <summary>
|
|
/// Загрузчик данных компендиума.
|
|
/// </summary>
|
|
/// <param name="repository"><inheritdoc cref="FoundryEnRepository"/></param>
|
|
public sealed class FoundryEnCompendium(FoundryEnRepository repository) : AbstractCompendium
|
|
{
|
|
/// <inheritdoc/>
|
|
public override FoundryEnRepository Repository { get; } = repository;
|
|
|
|
/// <summary>
|
|
/// Коллекция всех записей компендиума.
|
|
/// </summary>
|
|
public ConcurrentBag<FoundryEnCompendiumItem> Items { get; } = [];
|
|
|
|
/// <inheritdoc/>
|
|
public override int Count(params ICollection<string> types) =>
|
|
types.Count == 0 ? Items.Count : Items.Count(i => types.Contains(i.Type));
|
|
|
|
/// <inheritdoc/>
|
|
public override IEnumerable<Tuple<AbstractCompendiumItem, JObject>> Iterate(
|
|
params ICollection<string> types
|
|
)
|
|
{
|
|
IEnumerable<string> files =
|
|
types.Count == 0
|
|
? Items.Select(item => item.File).Distinct()
|
|
: Items
|
|
.Where(item => types.Contains(item.Type))
|
|
.Select(item => item.File)
|
|
.Distinct();
|
|
|
|
foreach (string file in files)
|
|
{
|
|
Dictionary<string, FoundryEnCompendiumItem> fileReferences = Items
|
|
.Where(i => i.File == file)
|
|
.ToDictionary(i => i.Id);
|
|
|
|
using StreamReader fileReader = new(file);
|
|
JArray array = JArray.Parse(fileReader.ReadToEnd());
|
|
|
|
foreach (JToken item in array)
|
|
{
|
|
string? id = item.SelectToken("_id")?.ToString();
|
|
|
|
if (
|
|
id is null
|
|
|| !fileReferences.TryGetValue(id, out FoundryEnCompendiumItem? reference)
|
|
|| (types.Count > 0 && !types.Contains(reference.Type))
|
|
)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
yield return new(reference, (JObject)item);
|
|
}
|
|
}
|
|
}
|
|
}
|