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 { /// /// Core class for rights management. Given a Wabbajack ModList this class will return a list of all the /// known rights violations of the ModList /// public class ValidateModlist { public ServerWhitelist ServerWhitelist { get; private set; } = new ServerWhitelist(); public void LoadServerWhitelist(string s) { ServerWhitelist = s.FromYaml(); } public async Task LoadListsFromGithub() { var client = new Common.Http.Client(); Utils.Log("Loading server whitelist"); using (var response = await client.GetAsync(Consts.ServerWhitelistURL)) using (var result = await response.Content.ReadAsStreamAsync()) { ServerWhitelist = result.FromYaml(); Utils.Log($"Loaded permissions for {ServerWhitelist.AllowedPrefixes.Count} servers and {ServerWhitelist.GoogleIDs.Count} Google Drive files"); } } public static async Task RunValidation(ModList modlist) { var validator = new ValidateModlist(); await validator.LoadListsFromGithub(); 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 { Utils.Log("No validation failures"); } } public async Task> Validate(ModList modlist) { ConcurrentStack ValidationErrors = new ConcurrentStack(); modlist.Archives .Where(m => !m.State.IsWhitelisted(ServerWhitelist)) .Do(m => { ValidationErrors.Push($"{m.Name} is not a whitelisted download"); }); return ValidationErrors.ToList(); } } }