72 lines
1.9 KiB
C#
72 lines
1.9 KiB
C#
using Newtonsoft.Json.Linq;
|
|
using Pf2eDataScrapper.Importers.Implementations.Aonprd.Compendium;
|
|
|
|
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd;
|
|
|
|
internal class AonprdLoader(AonprdImporter importer)
|
|
{
|
|
public AonprdImporter Importer = importer;
|
|
|
|
public AonprdCompendium Compendium = new();
|
|
|
|
public int ItemCount => Compendium.Count();
|
|
|
|
public void Prepare()
|
|
{
|
|
using StreamReader indexFile = new(Path.Combine(Importer.Dir, "index.json"));
|
|
JObject indexJson = JObject.Parse(indexFile.ReadToEnd());
|
|
|
|
foreach (KeyValuePair<string, JToken?> kvp in indexJson)
|
|
{
|
|
string file = kvp.Key;
|
|
JArray items = (JArray)kvp.Value!;
|
|
|
|
foreach (JToken item in items)
|
|
{
|
|
Compendium.AddReference(item.ToString(), file);
|
|
}
|
|
}
|
|
}
|
|
|
|
public IEnumerable<LoaderEntry> Entries()
|
|
{
|
|
return Entries([.. Compendium.Types.Keys]);
|
|
}
|
|
|
|
public IEnumerable<LoaderEntry> Entries(string types)
|
|
{
|
|
return Entries([types]);
|
|
}
|
|
|
|
public IEnumerable<LoaderEntry> Entries(string[] types)
|
|
{
|
|
foreach (string file in Compendium.Files(types))
|
|
{
|
|
using StreamReader fileReader = new(Path.Combine(Importer.Dir, $"{file}.json"));
|
|
JArray array = JArray.Parse(fileReader.ReadToEnd());
|
|
|
|
foreach (JToken item in array)
|
|
{
|
|
string? id = item.SelectToken("id")?.ToString();
|
|
|
|
if (id is null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
LoaderEntry entry = new(id, file, (JObject)item);
|
|
|
|
if (types.Contains(entry.Type))
|
|
{
|
|
yield return entry;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public class LoaderEntry(string id, string file, JObject itemJson) : AonprdReference(id, file)
|
|
{
|
|
public JObject Json { get; } = itemJson;
|
|
}
|
|
}
|