wabbajack/Wabbajack.Lib/ModListRegistry/ModListMetadata.cs

128 lines
4.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2019-12-06 05:59:57 +00:00
using System.Threading.Tasks;
using Newtonsoft.Json;
2019-10-15 23:23:14 +00:00
using Wabbajack.Common;
using Wabbajack.Common.Serialization.Json;
2019-10-15 23:23:14 +00:00
using Game = Wabbajack.Common.Game;
namespace Wabbajack.Lib.ModListRegistry
2019-10-15 23:23:14 +00:00
{
2020-04-08 04:19:36 +00:00
[JsonName("ModListMetadata")]
2019-10-15 23:23:14 +00:00
public class ModlistMetadata
{
[JsonProperty("title")]
2020-04-10 01:29:53 +00:00
public string Title { get; set; } = string.Empty;
2019-10-15 23:23:14 +00:00
[JsonProperty("description")]
2020-04-10 01:29:53 +00:00
public string Description { get; set; } = string.Empty;
[JsonProperty("author")]
2020-04-10 01:29:53 +00:00
public string Author { get; set; } = string.Empty;
2019-10-15 23:23:14 +00:00
[JsonProperty("game")]
2019-10-15 23:23:14 +00:00
public Game Game { get; set; }
[JsonIgnore] public string GameName => Game.ToDescriptionString();
2019-10-19 11:22:23 +00:00
[JsonProperty("official")]
public bool Official { get; set; }
[JsonProperty("links")]
public LinksObject Links { get; set; } = new LinksObject();
[JsonProperty("download_metadata")]
2020-04-10 01:29:53 +00:00
public DownloadMetadata? DownloadMetadata { get; set; }
[JsonIgnore]
2020-04-08 04:19:36 +00:00
public ModListSummary ValidationSummary { get; set; } = new ModListSummary();
2020-04-08 04:19:36 +00:00
[JsonName("Links")]
public class LinksObject
{
[JsonProperty("image")]
2020-04-10 01:29:53 +00:00
public string ImageUri { get; set; } = string.Empty;
2019-10-15 23:23:14 +00:00
[JsonProperty("readme")]
2020-04-10 01:29:53 +00:00
public string Readme { get; set; } = string.Empty;
[JsonProperty("download")]
2020-04-10 01:29:53 +00:00
public string Download { get; set; } = string.Empty;
[JsonProperty("machineURL")]
2020-04-10 01:29:53 +00:00
public string MachineURL { get; set; } = string.Empty;
}
2019-12-06 05:59:57 +00:00
public static async Task<List<ModlistMetadata>> LoadFromGithub()
2019-10-15 23:23:14 +00:00
{
2020-02-26 04:00:28 +00:00
var client = new Common.Http.Client();
2020-01-13 21:11:07 +00:00
Utils.Log("Loading ModLists from GitHub");
var metadataResult = client.GetStringAsync(Consts.ModlistMetadataURL);
var summaryResult = client.GetStringAsync(Consts.ModlistSummaryURL);
var metadata = (await metadataResult).FromJsonString<List<ModlistMetadata>>();
try
{
2020-04-08 04:19:36 +00:00
var summaries = (await summaryResult).FromJsonString<List<ModListSummary>>().ToDictionary(d => d.MachineURL);
foreach (var data in metadata)
2020-04-08 04:19:36 +00:00
if (summaries.TryGetValue(data.Links.MachineURL, out var summary))
data.ValidationSummary = summary;
}
catch (Exception)
{
// ignored
}
return metadata.OrderBy(m => (m.ValidationSummary?.HasFailures ?? false ? 1 : 0, m.Title)).ToList();
}
2020-03-28 20:04:22 +00:00
public bool NeedsDownload(AbsolutePath modlistPath)
{
if (!modlistPath.Exists) return true;
if (DownloadMetadata?.Hash == null)
{
return true;
}
return DownloadMetadata.Hash != modlistPath.FileHashCached(true);
}
2019-10-15 23:23:14 +00:00
}
[JsonName("DownloadMetadata")]
public class DownloadMetadata
{
2020-03-22 15:50:53 +00:00
public Hash Hash { get; set; }
public long Size { get; set; }
public long NumberOfArchives { get; set; }
public long SizeOfArchives { get; set; }
public long NumberOfInstalledFiles { get; set; }
public long SizeOfInstalledFiles { get; set; }
}
2020-04-08 04:19:36 +00:00
[JsonName("ModListSummary")]
public class ModListSummary
{
2020-01-08 04:41:50 +00:00
[JsonProperty("name")]
2020-04-10 01:29:53 +00:00
public string Name { get; set; } = string.Empty;
[JsonProperty("machineURL")]
2020-04-10 01:29:53 +00:00
public string MachineURL { get; set; } = string.Empty;
2020-01-08 04:41:50 +00:00
[JsonProperty("checked")]
public DateTime Checked { get; set; }
[JsonProperty("failed")]
public int Failed { get; set; }
[JsonProperty("passed")]
public int Passed { get; set; }
[JsonProperty("link")]
public string Link => $"/lists/status/{MachineURL}.json";
2020-01-08 04:41:50 +00:00
[JsonProperty("report")]
public string Report => $"/lists/status/{MachineURL}.html";
2020-01-08 04:41:50 +00:00
[JsonProperty("has_failures")]
public bool HasFailures => Failed > 0;
}
2019-10-15 23:23:14 +00:00
}