2019-09-29 04:41:47 +00:00
using System ;
using System.Collections.Concurrent ;
using System.Collections.Generic ;
using System.Linq ;
2019-09-29 22:21:18 +00:00
using System.Net.Http ;
2019-12-04 01:26:26 +00:00
using System.Threading.Tasks ;
2019-09-29 04:41:47 +00:00
using Wabbajack.Common ;
2019-10-16 03:10:34 +00:00
using Wabbajack.Lib.Downloaders ;
2019-09-29 22:21:18 +00:00
using Path = Alphaleonis . Win32 . Filesystem . Path ;
2019-09-29 04:41:47 +00:00
2019-10-16 03:10:34 +00:00
namespace Wabbajack.Lib.Validation
2019-09-29 04:41:47 +00:00
{
/// <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
2019-09-29 04:41:47 +00:00
/// </summary>
public class ValidateModlist
{
2020-03-22 15:50:53 +00:00
public ServerWhitelist ServerWhitelist { get ; private set ; } = new ServerWhitelist ( ) ;
2019-09-29 20:53:25 +00:00
public void LoadServerWhitelist ( string s )
{
2019-10-16 21:36:14 +00:00
ServerWhitelist = s . FromYaml < ServerWhitelist > ( ) ;
2019-09-29 20:53:25 +00:00
}
2019-12-06 05:59:57 +00:00
public async Task LoadListsFromGithub ( )
2019-09-29 22:21:18 +00:00
{
2020-06-26 17:08:30 +00:00
var client = new Wabbajack . Lib . Http . Client ( ) ;
2019-09-29 22:21:18 +00:00
2020-01-13 21:11:07 +00:00
Utils . Log ( "Loading server whitelist" ) ;
2020-02-26 05:05:33 +00:00
using ( var response = await client . GetAsync ( Consts . ServerWhitelistURL ) )
using ( var result = await response . Content . ReadAsStreamAsync ( ) )
2019-09-29 22:21:18 +00:00
{
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" ) ;
2019-09-29 22:21:18 +00:00
}
}
2020-03-22 15:50:53 +00:00
public static async Task RunValidation ( ModList modlist )
2019-09-29 22:21:18 +00:00
{
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
2019-10-01 22:39:25 +00:00
Utils . Log ( "Running validation checks" ) ;
2019-12-04 01:26:26 +00:00
var errors = await validator . Validate ( modlist ) ;
2019-09-29 22:21:18 +00:00
errors . Do ( e = > Utils . Log ( e ) ) ;
2019-09-30 23:39:41 +00:00
if ( errors . Count ( ) > 0 )
{
2019-10-01 22:39:25 +00:00
throw new Exception ( $"{errors.Count()} validation errors found, cannot continue." ) ;
}
else
{
2020-01-13 21:11:07 +00:00
Utils . Log ( "No validation failures" ) ;
2019-09-30 23:39:41 +00:00
}
2019-09-29 22:21:18 +00:00
}
2019-12-04 01:26:26 +00:00
public async Task < IEnumerable < string > > Validate ( ModList modlist )
2019-09-29 04:41:47 +00:00
{
ConcurrentStack < string > ValidationErrors = new ConcurrentStack < string > ( ) ;
2019-09-29 20:53:25 +00:00
modlist . Archives
2019-10-12 22:15:20 +00:00
. Where ( m = > ! m . State . IsWhitelisted ( ServerWhitelist ) )
. Do ( m = >
2019-09-29 20:53:25 +00:00
{
2019-10-12 22:15:20 +00:00
ValidationErrors . Push ( $"{m.Name} is not a whitelisted download" ) ;
2019-09-29 20:53:25 +00:00
} ) ;
2019-10-12 22:15:20 +00:00
2019-09-29 04:41:47 +00:00
return ValidationErrors . ToList ( ) ;
}
}
}