wabbajack/Wabbajack.Lib/ModListRegistry/ModListMetadata.cs

176 lines
5.7 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; }
2020-05-09 04:19:40 +00:00
[JsonProperty("tags")]
public List<string> tags { get; set; } = new List<string>();
2020-04-27 10:18:08 +00:00
[JsonProperty("nsfw")]
public bool NSFW { get; set; }
[JsonProperty("utility_list")]
public bool UtilityList { get; set; }
2020-12-16 01:26:41 +00:00
[JsonProperty("image_contains_title")]
public bool ImageContainsTitle { get; set; }
2020-04-27 10:18:08 +00:00
[JsonProperty("force_down")]
public bool ForceDown { 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")]
2021-02-27 00:08:05 +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
{
var client = new Http.Client();
2020-01-13 21:11:07 +00:00
Utils.Log("Loading ModLists from GitHub");
var metadataResult = client.GetStringAsync(Consts.ModlistMetadataURL);
var utilityResult = client.GetStringAsync(Consts.UtilityModlistMetadataURL);
var summaryResult = client.GetStringAsync(Consts.ModlistSummaryURL);
var metadata = (await metadataResult).FromJsonString<List<ModlistMetadata>>();
metadata = metadata.Concat((await utilityResult).FromJsonString<List<ModlistMetadata>>()).ToList();
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
}
var random = new Random();
return metadata
// Sort randomly initially, just to give each list a fair shake
.Shuffle(random)
// Put broken lists at bottom
.OrderBy(m => (m.ValidationSummary?.HasFailures ?? false ? 1 : 0))
.ToList();
}
public static async Task<List<ModlistMetadata>> LoadUnlistedFromGithub()
{
try
{
var client = new Http.Client();
return (await client.GetStringAsync(Consts.UnlistedModlistMetadataURL)).FromJsonString<List<ModlistMetadata>>();
}
2020-10-01 03:50:09 +00:00
catch (Exception)
{
Utils.LogStatus("Error loading unlisted modlists");
return new List<ModlistMetadata>();
}
}
public async ValueTask<bool> NeedsDownload(AbsolutePath modlistPath)
2020-03-28 20:04:22 +00:00
{
if (!modlistPath.Exists) return true;
if (DownloadMetadata?.Hash == null)
{
return true;
}
2021-01-09 19:04:11 +00:00
return DownloadMetadata.Hash != await modlistPath.FileHashCachedAsync();
2020-03-28 20:04:22 +00:00
}
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("updating")]
public int Updating { get; set; }
[JsonProperty("mirrored")]
public int Mirrored { get; set; }
2020-01-08 04:41:50 +00:00
[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";
[JsonProperty("modlist_missing")]
public bool ModListIsMissing { get; set; }
2020-01-08 04:41:50 +00:00
[JsonProperty("has_failures")]
public bool HasFailures => Failed > 0 || ModListIsMissing;
}
2019-10-15 23:23:14 +00:00
}