mirror of
https://github.com/SaphireLattice/quest-reader.git
synced 2026-11-13 02:40:36 -07:00
66 lines
2.6 KiB
C#
66 lines
2.6 KiB
C#
namespace QuestReader.Services;
|
|
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using System.Text.RegularExpressions;
|
|
using QuestReader.Models;
|
|
|
|
public class PostsSource
|
|
{
|
|
public List<ThreadPost> Posts { get; set; }
|
|
|
|
public HashSet<ThreadPost> Accepted { get; set; }
|
|
|
|
public Metadata Metadata { get; set; }
|
|
|
|
public string BasePath { get; set; }
|
|
|
|
public PostsSource(string questName)
|
|
{
|
|
BasePath = $"quests/{questName}";
|
|
|
|
var options = new JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
|
WriteIndented = true
|
|
};
|
|
|
|
using var fileStream = File.OpenRead(Path.Combine(BasePath, "metadata.json"));
|
|
Metadata = JsonSerializer.Deserialize<Metadata>(fileStream, options)
|
|
?? throw new InvalidDataException("Empty deserialisation result for quest metadata");
|
|
fileStream.Dispose();
|
|
|
|
Console.Out.WriteLine($"Loaded metadata: {Metadata}");
|
|
Posts = Metadata.Threads
|
|
.SelectMany(tId => QuestdenParse.GetThread(tId, BasePath).Result)
|
|
.ToList();
|
|
|
|
using var postsListStream = File.OpenRead(Path.Combine(BasePath, "accepted.json"));
|
|
var ids = JsonSerializer.Deserialize<List<int>>(postsListStream, options)
|
|
?? throw new InvalidDataException("Empty deserialisation result for quest metadata");
|
|
Accepted = Posts.Where(p => ids.Contains(p.Id)).ToHashSet();
|
|
|
|
foreach (var chapter in Metadata.Chapters)
|
|
{
|
|
var post = Accepted.Single(p => p.Id == (chapter.Announce ?? chapter.Start));
|
|
post.IsChapterAnnounce = true;
|
|
post.Chapter = chapter;
|
|
}
|
|
|
|
Console.Out.WriteLine($"Loaded a list of {Accepted.Count} posts, referencing {Accepted.Where(a => a.File is not null).Count()} files");
|
|
|
|
foreach (var post in Accepted)
|
|
{
|
|
post.AuthorPost = true;
|
|
if (post.ParsedContent is null || post.ParsedContent.Version < QuestdenParse.LatestCompatibleVersion)
|
|
throw new NotImplementedException("Repairing missing post content or updating it is not implemented yet");
|
|
}
|
|
|
|
var referenced = Accepted.SelectMany(p => p.ParsedContent!.GetReferences());
|
|
Accepted.UnionWith(Posts.Where(p => referenced.Contains(p.Id)));
|
|
Accepted = Accepted.OrderBy(p => p.Id).ToHashSet();
|
|
|
|
Console.Out.WriteLine($"Done loading with {Accepted.Count} posts, referencing {Accepted.Where(a => a.File is not null).Count()} files");
|
|
}
|
|
} |