2022-02-15 18:38:47 +00:00
|
|
|
namespace QuestReader.Services;
|
|
|
|
|
|
|
|
using System.Text.Json;
|
|
|
|
using System.Text.Json.Serialization;
|
2022-02-16 12:38:08 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2022-02-19 16:42:47 +00:00
|
|
|
using QuestReader.Models;
|
2022-02-21 09:33:17 +00:00
|
|
|
using SixLabors.ImageSharp;
|
2022-02-15 18:38:47 +00:00
|
|
|
|
|
|
|
public class PostsSource
|
|
|
|
{
|
|
|
|
public List<ThreadPost> Posts { get; set; }
|
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
public HashSet<ThreadPost> Accepted { get; set; }
|
2022-02-15 18:38:47 +00:00
|
|
|
|
|
|
|
public Metadata Metadata { get; set; }
|
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
public string BasePath { get; set; }
|
|
|
|
|
|
|
|
public PostsSource(string questName)
|
2022-02-15 18:38:47 +00:00
|
|
|
{
|
2022-02-20 19:27:27 +00:00
|
|
|
BasePath = $"quests/{questName}";
|
|
|
|
|
2022-02-15 18:38:47 +00:00
|
|
|
var options = new JsonSerializerOptions
|
|
|
|
{
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
2022-02-16 12:38:08 +00:00
|
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
|
|
|
WriteIndented = true
|
2022-02-15 18:38:47 +00:00
|
|
|
};
|
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
using var fileStream = File.OpenRead(Path.Combine(BasePath, "metadata.json"));
|
2022-02-15 18:38:47 +00:00
|
|
|
Metadata = JsonSerializer.Deserialize<Metadata>(fileStream, options)
|
|
|
|
?? throw new InvalidDataException("Empty deserialisation result for quest metadata");
|
|
|
|
fileStream.Dispose();
|
|
|
|
|
|
|
|
Console.Out.WriteLine($"Loaded metadata: {Metadata}");
|
2022-02-20 19:27:27 +00:00
|
|
|
Posts = Metadata.Threads
|
|
|
|
.SelectMany(tId => QuestdenParse.GetThread(tId, BasePath).Result)
|
|
|
|
.ToList();
|
2022-02-15 18:38:47 +00:00
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
using var postsListStream = File.OpenRead(Path.Combine(BasePath, "accepted.json"));
|
2022-02-15 18:38:47 +00:00
|
|
|
var ids = JsonSerializer.Deserialize<List<int>>(postsListStream, options)
|
|
|
|
?? throw new InvalidDataException("Empty deserialisation result for quest metadata");
|
2022-02-20 19:27:27 +00:00
|
|
|
Accepted = Posts.Where(p => ids.Contains(p.Id)).ToHashSet();
|
2022-02-16 12:38:08 +00:00
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
foreach (var chapter in Metadata.Chapters)
|
2022-02-16 12:38:08 +00:00
|
|
|
{
|
2022-02-20 19:27:27 +00:00
|
|
|
var post = Accepted.Single(p => p.Id == (chapter.Announce ?? chapter.Start));
|
|
|
|
post.IsChapterAnnounce = true;
|
|
|
|
post.Chapter = chapter;
|
|
|
|
}
|
2022-02-16 12:38:08 +00:00
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
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");
|
2022-02-16 12:38:08 +00:00
|
|
|
}
|
2022-02-20 19:27:27 +00:00
|
|
|
|
|
|
|
var referenced = Accepted.SelectMany(p => p.ParsedContent!.GetReferences());
|
|
|
|
Accepted.UnionWith(Posts.Where(p => referenced.Contains(p.Id)));
|
|
|
|
Accepted = Accepted.OrderBy(p => p.Id).ToHashSet();
|
|
|
|
|
2022-02-21 09:33:17 +00:00
|
|
|
FileDownloader.DownloadList(BasePath, Metadata.AssetsBaseUrl, Accepted.Where(p => p.File is not null).Select(p => p.File!)).Wait();
|
|
|
|
|
|
|
|
foreach (var post in Accepted.Where(f => f.File is not null))
|
|
|
|
{
|
|
|
|
using var imageStream = File.OpenRead(Path.Combine(BasePath, "assets", post.File!));
|
|
|
|
IImageInfo imageInfo = Image.Identify(imageStream);
|
|
|
|
if (imageInfo is null) {
|
|
|
|
Console.Out.WriteLine($"Not a valid image: {post.File!}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
post.FileHeight = imageInfo.Height;
|
|
|
|
post.FileWidth = imageInfo.Width;
|
|
|
|
}
|
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
Console.Out.WriteLine($"Done loading with {Accepted.Count} posts, referencing {Accepted.Where(a => a.File is not null).Count()} files");
|
2022-02-15 18:38:47 +00:00
|
|
|
}
|
|
|
|
}
|