wabbajack/Wabbajack.Lib/Validation/ValidateModlist.cs

72 lines
2.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Wabbajack.Common;
using Wabbajack.Lib.Downloaders;
using Path = Alphaleonis.Win32.Filesystem.Path;
namespace Wabbajack.Lib.Validation
{
/// <summary>
2020-01-13 21:11:07 +00:00
/// Core class for rights management. Given a Wabbajack ModList this class will return a list of all the
/// known rights violations of the ModList
/// </summary>
public class ValidateModlist
{
2020-03-22 15:50:53 +00:00
public ServerWhitelist ServerWhitelist { get; private set; } = new ServerWhitelist();
public void LoadServerWhitelist(string s)
{
2019-10-16 21:36:14 +00:00
ServerWhitelist = s.FromYaml<ServerWhitelist>();
}
2019-12-06 05:59:57 +00:00
public async Task LoadListsFromGithub()
{
2020-06-26 17:08:30 +00:00
var client = new Wabbajack.Lib.Http.Client();
2020-01-13 21:11:07 +00:00
Utils.Log("Loading server whitelist");
using (var response = await client.GetAsync(Consts.ServerWhitelistURL))
using (var result = await response.Content.ReadAsStreamAsync())
{
2019-10-16 21:36:14 +00:00
ServerWhitelist = result.FromYaml<ServerWhitelist>();
2020-04-08 04:19:36 +00:00
Utils.Log($"Loaded permissions for {ServerWhitelist.AllowedPrefixes?.Count ?? 0} servers and {ServerWhitelist.GoogleIDs?.Count ?? 0} Google Drive files");
}
}
2020-03-22 15:50:53 +00:00
public static async Task RunValidation(ModList modlist)
{
2020-03-22 15:50:53 +00:00
var validator = new ValidateModlist();
2019-10-03 02:55:16 +00:00
2019-12-06 05:59:57 +00:00
await validator.LoadListsFromGithub();
2019-10-03 02:55:16 +00:00
Utils.Log("Running validation checks");
var errors = await validator.Validate(modlist);
errors.Do(e => Utils.Log(e));
if (errors.Count() > 0)
{
throw new Exception($"{errors.Count()} validation errors found, cannot continue.");
}
else
{
2020-01-13 21:11:07 +00:00
Utils.Log("No validation failures");
}
}
public async Task<IEnumerable<string>> Validate(ModList modlist)
{
2021-02-06 16:43:11 +00:00
ConcurrentStack<string> ValidationErrors = new();
modlist.Archives
.Where(m => !m.State.IsWhitelisted(ServerWhitelist))
.Do(m =>
{
ValidationErrors.Push($"{m.Name} is not a whitelisted download");
});
return ValidationErrors.ToList();
}
}
}