Most of Wabbajack.Lib is ported

This commit is contained in:
Timothy Baldridge 2021-12-26 15:32:36 -07:00
parent b53f98eebd
commit 8890169eff
13 changed files with 125 additions and 680 deletions

6
Wabbajack.Lib/Consts.cs Normal file
View File

@ -0,0 +1,6 @@
namespace Wabbajack.Lib;
public static class Consts
{
public static string AppName = "Wabbajack";
}

View File

@ -1,318 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Compression.BSA;
using Newtonsoft.Json;
using Wabbajack.Common;
using Wabbajack.Common.Serialization.Json;
using Wabbajack.ImageHashing;
using Wabbajack.Lib.Downloaders;
using Wabbajack.VirtualFileSystem;
namespace Wabbajack.Lib
{
public class RawSourceFile
{
public readonly RelativePath Path;
public RawSourceFile(VirtualFile file, RelativePath path)
{
File = file;
Path = path;
}
public AbsolutePath AbsolutePath
{
get
{
if (!File.IsNative)
throw new InvalidDataException("Can't get the absolute path of a non-native file");
return File.FullPath.Base;
}
}
public VirtualFile File { get; }
public Hash Hash => File.Hash;
public T EvolveTo<T>() where T : Directive, new()
{
var v = new T {To = Path, Hash = File.Hash, Size = File.Size};
return v;
}
}
[JsonName("ModList")]
public class ModList
{
/// <summary>
/// Archives required by this modlist
/// </summary>
public List<Archive> Archives = new List<Archive>();
/// <summary>
/// Author of the ModList
/// </summary>
public string Author = string.Empty;
/// <summary>
/// Description of the ModList
/// </summary>
public string Description = string.Empty;
/// <summary>
/// Install directives
/// </summary>
public List<Directive> Directives = new List<Directive>();
/// <summary>
/// The game variant to which this game applies
/// </summary>
public Game GameType;
/// <summary>
/// Hash of the banner-image
/// </summary>
public RelativePath Image;
/// <summary>
/// The Mod Manager used to create the modlist
/// </summary>
public ModManager ModManager;
/// <summary>
/// Name of the ModList
/// </summary>
public string Name = string.Empty;
/// <summary>
/// URL to the readme
/// </summary>
public string Readme = string.Empty;
/// <summary>
/// The build version of Wabbajack used when compiling the Modlist
/// </summary>
public Version? WabbajackVersion;
/// <summary>
/// Website of the ModList
/// </summary>
public Uri? Website;
/// <summary>
/// Current Version of the Modlist
/// </summary>
public Version Version = new Version(1, 0, 0, 0);
/// <summary>
/// Whether the Modlist is NSFW or not
/// </summary>
public bool IsNSFW;
/// <summary>
/// The size of all the archives once they're downloaded
/// </summary>
[JsonIgnore]
public long DownloadSize => Archives.Sum(a => a.Size);
/// <summary>
/// The size of all the files once they are installed (excluding downloaded archives)
/// </summary>
[JsonIgnore]
public long InstallSize => Directives.Sum(s => s.Size);
public ModList Clone()
{
using var ms = new MemoryStream();
this.ToJson(ms);
ms.Position = 0;
return ms.FromJson<ModList>();
}
}
public abstract class Directive
{
public Hash Hash { get; set; }
public long Size { get; set; }
/// <summary>
/// location the file will be copied to, relative to the install path.
/// </summary>
public RelativePath To { get; set; }
}
public class IgnoredDirectly : Directive
{
public string Reason = string.Empty;
}
public class NoMatch : IgnoredDirectly
{
}
[JsonName("InlineFile")]
public class InlineFile : Directive
{
/// <summary>
/// Data that will be written as-is to the destination location;
/// </summary>
public RelativePath SourceDataID { get; set; }
[JsonIgnore]
public VirtualFile? SourceDataFile { get; set; }
}
[JsonName("ArchiveMeta")]
public class ArchiveMeta : Directive
{
public RelativePath SourceDataID { get; set; }
}
public enum PropertyType { Banner, Readme }
/// <summary>
/// File meant to be extracted before the installation
/// </summary>
[JsonName("PropertyFile")]
public class PropertyFile : InlineFile
{
public PropertyType Type;
}
[JsonName("CleanedESM")]
public class CleanedESM : InlineFile
{
public Hash SourceESMHash;
}
/// <summary>
/// A file that has the game and MO2 folders remapped on installation
/// </summary>
[JsonName("RemappedInlineFile")]
public class RemappedInlineFile : InlineFile
{
}
[JsonName("SteamMeta")]
public class SteamMeta : ArchiveMeta
{
public int ItemID { get; set; }
}
[JsonName("FromArchive")]
public class FromArchive : Directive
{
private string? _fullPath;
public HashRelativePath ArchiveHashPath { get; set; }
[JsonIgnore]
public VirtualFile? FromFile { get; set; }
[JsonIgnore]
public string FullPath => _fullPath ??= string.Join("|", ArchiveHashPath);
}
[JsonName("CreateBSA")]
public class CreateBSA : Directive
{
public RelativePath TempID { get; set; }
public ArchiveStateObject State { get; }
public List<FileStateObject> FileStates { get; set; } = new List<FileStateObject>();
public CreateBSA(ArchiveStateObject state, IEnumerable<FileStateObject>? items = null)
{
State = state;
if (items != null)
{
FileStates.AddRange(items);
}
}
}
[JsonName("PatchedFromArchive")]
public class PatchedFromArchive : FromArchive
{
public Hash FromHash { get; set; }
/// <summary>
/// The file to apply to the source file to patch it
/// </summary>
public RelativePath PatchID { get; set; }
/// <summary>
/// During compilation this holds several possible files that could be used as a patch source. At the end
/// of compilation we'll go through all of these and find the smallest patch file.
/// </summary>
[JsonIgnore]
public VirtualFile[] Choices { get; set; } = { };
}
[JsonName("TransformedTexture")]
public class TransformedTexture : FromArchive
{
/// <summary>
/// The file to apply to the source file to patch it
/// </summary>
public ImageState ImageState { get; set; } = new();
}
[JsonName("SourcePatch")]
public class SourcePatch
{
public Hash Hash { get; set; }
public RelativePath RelativePath { get; set; }
}
[JsonName("MergedPatch")]
public class MergedPatch : Directive
{
public RelativePath PatchID { get; set; }
public List<SourcePatch> Sources { get; set; } = new List<SourcePatch>();
}
[JsonName("Archive")]
public class Archive
{
/// <summary>
/// xxHash64 of the archive
/// </summary>
public Hash Hash { get; set; }
/// <summary>
/// Meta INI for the downloaded archive
/// </summary>
public string? Meta { get; set; }
/// <summary>
/// Human friendly name of this archive
/// </summary>
public string Name { get; set; } = string.Empty;
public long Size { get; set; }
public AbstractDownloadState State { get; }
public Archive(AbstractDownloadState state)
{
State = state;
}
}
public class IndexedArchive
{
public dynamic? IniData;
public string Meta = string.Empty;
public string Name = string.Empty;
public VirtualFile File { get; }
public AbstractDownloadState? State { get; set; }
public IndexedArchive(VirtualFile file)
{
File = file;
}
}
}

View File

@ -1,18 +1,51 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.VisualBasic.CompilerServices;
using Newtonsoft.Json; using Newtonsoft.Json;
using Wabbajack.Common; using Wabbajack.Downloaders;
using Wabbajack.Lib.Http; using Wabbajack.DTOs;
using Wabbajack.DTOs.DownloadStates;
using Wabbajack.DTOs.JsonConverters;
using Wabbajack.Networking.Http;
using Wabbajack.Networking.Http.Interfaces;
using Wabbajack.Networking.WabbajackClientApi;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
using Wabbajack.RateLimiter;
namespace Wabbajack.Lib namespace Wabbajack.Lib
{ {
public class LauncherUpdater public class LauncherUpdater
{ {
private readonly ILogger<LauncherUpdater> _logger;
private readonly HttpClient _client;
private readonly Client _wjclient;
private readonly DTOSerializer _dtos;
private readonly DownloadDispatcher _downloader;
private static Uri GITHUB_REPO_RELEASES = new("https://api.github.com/repos/wabbajack-tools/wabbajack/releases");
public LauncherUpdater(ILogger<LauncherUpdater> logger, HttpClient client, Client wjclient, DTOSerializer dtos,
DownloadDispatcher downloader)
{
_logger = logger;
_client = client;
_wjclient = wjclient;
_dtos = dtos;
_downloader = downloader;
}
public static Lazy<AbsolutePath> CommonFolder = new (() => public static Lazy<AbsolutePath> CommonFolder = new (() =>
{ {
var entryPoint = AbsolutePath.EntryPoint; var entryPoint = KnownFolders.EntryPoint;
// If we're not in a folder that looks like a version, abort // If we're not in a folder that looks like a version, abort
if (!Version.TryParse(entryPoint.FileName.ToString(), out var version)) if (!Version.TryParse(entryPoint.FileName.ToString(), out var version))
@ -21,24 +54,26 @@ namespace Wabbajack.Lib
} }
// If we're not in a folder that has Wabbajack.exe in the parent folder, abort // If we're not in a folder that has Wabbajack.exe in the parent folder, abort
if (!entryPoint.Parent.Combine(Consts.AppName).WithExtension(new Extension(".exe")).IsFile) if (!entryPoint.Parent.Combine(Consts.AppName).WithExtension(new Extension(".exe")).FileExists())
{ {
return entryPoint; return entryPoint;
} }
return entryPoint.Parent; return entryPoint.Parent;
}); });
public static async Task Run()
public async Task Run()
{ {
if (CommonFolder.Value == AbsolutePath.EntryPoint) if (CommonFolder.Value == KnownFolders.EntryPoint)
{ {
Utils.Log("Outside of standard install folder, not updating"); _logger.LogInformation("Outside of standard install folder, not updating");
return; return;
} }
var version = Version.Parse(AbsolutePath.EntryPoint.FileName.ToString()); var version = Version.Parse(KnownFolders.EntryPoint.FileName.ToString());
var oldVersions = CommonFolder.Value var oldVersions = CommonFolder.Value
.EnumerateDirectories() .EnumerateDirectories()
@ -52,8 +87,8 @@ namespace Wabbajack.Lib
foreach (var (_, path) in oldVersions) foreach (var (_, path) in oldVersions)
{ {
Utils.Log($"Deleting old Wabbajack version at: {path}"); _logger.LogInformation("Deleting old Wabbajack version at: {Path}", path);
await path.DeleteDirectory(); path.DeleteDirectory();
} }
var release = (await GetReleases()) var release = (await GetReleases())
@ -68,41 +103,51 @@ namespace Wabbajack.Lib
}) })
.FirstOrDefault(); .FirstOrDefault();
var launcherFolder = AbsolutePath.EntryPoint.Parent; var launcherFolder = KnownFolders.EntryPoint.Parent;
var exePath = launcherFolder.Combine("Wabbajack.exe"); var exePath = launcherFolder.Combine("Wabbajack.exe");
var launcherVersion = FileVersionInfo.GetVersionInfo(exePath.ToString()); var launcherVersion = FileVersionInfo.GetVersionInfo(exePath.ToString());
if (release != default && release.version > Version.Parse(launcherVersion.FileVersion!)) if (release != default && release.version > Version.Parse(launcherVersion.FileVersion!))
{ {
Utils.Log($"Updating Launcher from {launcherVersion.FileVersion} to {release.version}"); _logger.LogInformation("Updating Launcher from {OldVersion} to {NewVersion}", launcherVersion.FileVersion, release.version);
var tempPath = launcherFolder.Combine("Wabbajack.exe.temp"); var tempPath = launcherFolder.Combine("Wabbajack.exe.temp");
var client = new Client();
client.UseChromeUserAgent();
await client.DownloadAsync(release.asset.BrowserDownloadUrl!, tempPath);
if (tempPath.Size != release.asset.Size) await _downloader.Download(new Archive
{ {
Utils.Log( State = new Http {Url = release.asset.BrowserDownloadUrl!},
$"Downloaded launcher did not match expected size: {tempPath.Size} expected {release.asset.Size}"); Name = release.asset.Name,
Size = release.asset.Size
}, tempPath, CancellationToken.None);
if (tempPath.Size() != release.asset.Size)
{
_logger.LogInformation(
"Downloaded launcher did not match expected size: {DownloadedSize} expected {ExpectedSize}", tempPath.Size(), release.asset.Size);
return; return;
} }
if (exePath.Exists) if (exePath.FileExists())
await exePath.DeleteAsync(); exePath.Delete();
await tempPath.MoveToAsync(exePath); await tempPath.MoveToAsync(exePath, true, CancellationToken.None);
Utils.Log("Finished updating wabbajack"); _logger.LogInformation("Finished updating wabbajack");
await Metrics.Send("updated_launcher", $"{launcherVersion.FileVersion} -> {release.version}"); await _wjclient.SendMetric("updated_launcher", $"{launcherVersion.FileVersion} -> {release.version}");
} }
} }
private static async Task<Release[]> GetReleases() private async Task<Release[]> GetReleases()
{ {
Utils.Log("Getting new Wabbajack version list"); _logger.LogInformation("Getting new Wabbajack version list");
var client = new Client(); var msg = MakeMessage(GITHUB_REPO_RELEASES);
client.UseChromeUserAgent(); return await _client.GetJsonFromSendAsync<Release[]>(msg, _dtos.Options);
return await client.GetJsonAsync<Release[]>(Consts.GITHUB_REPO_RELEASES.ToString()); }
private HttpRequestMessage MakeMessage(Uri uri)
{
var msg = new HttpRequestMessage(HttpMethod.Get, uri);
msg.UseChromeUserAgent();
return msg;
} }

View File

@ -1,62 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Wabbajack.Common;
using Wabbajack.Common.Serialization.Json;
namespace Wabbajack.Lib
{
[JsonName("Manifest")]
public class Manifest
{
public readonly string Name;
public readonly Version Version;
public readonly string Author;
public readonly string Description;
public readonly Game GameType;
// Enum toString for better parsing in other software
public string GameName;
public readonly ModManager ModManager;
// Enum toString for better parsing in other software
public string ModManagerName;
public readonly long DownloadSize;
public readonly long InstallSize;
public bool IsNSFW;
public readonly List<Archive> Archives;
public readonly List<InlineFile> InlinedFiles;
public Manifest(ModList modlist)
{
Name = modlist.Name;
Version = modlist.Version;
Author = modlist.Author;
Description = modlist.Description;
GameType = modlist.GameType;
GameName = GameType.ToString();
ModManager = modlist.ModManager;
ModManagerName = ModManager.ToString();
DownloadSize = modlist.DownloadSize;
InstallSize = modlist.InstallSize;
IsNSFW = modlist.IsNSFW;
// meta is being omitted due to it being useless and not very space friendly
Archives = modlist.Archives.Select(a => new Archive(a.State)
{
Hash = a.Hash,
Name = a.Name,
Size = a.Size,
}).ToList();
InlinedFiles = modlist.Directives.OfType<InlineFile>().ToList();
}
}
}

View File

@ -1,13 +0,0 @@
using System.Threading.Tasks;
using Wabbajack.Common;
namespace Wabbajack.Lib.Tasks
{
public class MakeNewMO2Project
{
public static async Task Execute(AbsolutePath folder, Game game)
{
}
}
}

View File

@ -1,53 +0,0 @@
using System.Threading.Tasks;
using Wabbajack.Common;
namespace Wabbajack.Lib.Tasks
{
public class MigrateGameFolder
{
public static async Task<bool> Execute(AbsolutePath mo2Folder)
{
var iniPath = mo2Folder.Combine(Consts.ModOrganizer2Ini);
if (!iniPath.Exists)
{
Utils.Log($"Game folder conversion failed, {Consts.ModOrganizer2Ini} does not exist in {mo2Folder}");
return false;
}
var newGamePath = mo2Folder.Combine(Consts.GameFolderFilesDir);
newGamePath.CreateDirectory();
var gameIni = iniPath.LoadIniFile();
if (!GameRegistry.TryGetByFuzzyName((string)gameIni.General.gameName, out var gameMeta))
{
Utils.Log($"Could not locate game for {gameIni.General.gameName}");
return false;
}
var orginGamePath = gameMeta.GameLocation();
foreach (var file in gameMeta.GameLocation().EnumerateFiles())
{
var relPath = file.RelativeTo(orginGamePath);
var newFile = relPath.RelativeTo(newGamePath);
if (newFile.Exists)
{
Utils.Log($"Skipping {relPath} it already exists in the target path");
continue;
}
Utils.Log($"Copying/Linking {relPath}");
await file.HardLinkIfOversize(newFile);
}
Utils.Log("Remapping INI");
var iniString = await iniPath.ReadAllTextAsync();
iniString = iniString.Replace((string)orginGamePath, (string)newGamePath);
iniString = iniString.Replace(((string)orginGamePath).Replace(@"\", @"\\"), ((string)newGamePath).Replace(@"\", @"\\"));
iniString = iniString.Replace(((string)orginGamePath).Replace(@"\", @"/"), ((string)newGamePath).Replace(@"\", @"/"));
await iniPath.WriteAllTextAsync(iniString);
return true;
}
}
}

View File

@ -1,41 +0,0 @@
using System.Collections.Generic;
namespace Wabbajack.Lib.Validation
{
public class Permissions
{
public bool? CanExtractBSAs { get; set; }
public bool? CanModifyESPs { get; set; }
public bool? CanModifyAssets { get; set; }
public bool? CanUseInOtherGames { get; set; }
}
//public class Author
//{
// public Permissions Permissions { get; set; }
// public Dictionary<string, Game> Games;
//}
//public class Game
//{
// public Permissions Permissions;
// public Dictionary<string, Mod> Mods;
//}
//public class Mod
//{
// public Permissions Permissions;
// public Dictionary<string, File> Files;
//}
//public class File
//{
// public Permissions Permissions;
//}
public class ServerWhitelist
{
public List<string> GoogleIDs = new List<string>();
public List<string> AllowedPrefixes = new List<string>();
}
}

View File

@ -1,71 +0,0 @@
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>
/// 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
{
public ServerWhitelist ServerWhitelist { get; private set; } = new ServerWhitelist();
public void LoadServerWhitelist(string s)
{
ServerWhitelist = s.FromYaml<ServerWhitelist>();
}
public async Task LoadListsFromGithub()
{
var client = new Wabbajack.Lib.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<ServerWhitelist>();
Utils.Log($"Loaded permissions for {ServerWhitelist.AllowedPrefixes?.Count ?? 0} servers and {ServerWhitelist.GoogleIDs?.Count ?? 0} 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<IEnumerable<string>> Validate(ModList modlist)
{
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();
}
}
}

View File

@ -1,78 +0,0 @@
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Org.BouncyCastle.Bcpg;
using Wabbajack.Common;
using Wabbajack.Common.FileSignatures;
namespace Wabbajack.Lib
{
/// <summary>
/// Wrapper around Windows Defender's commandline tool
/// </summary>
public class VirusScanner
{
public enum Result : int
{
NotMalware = 0,
Malware = 2
}
private static AbsolutePath ScannerPath()
{
return ((AbsolutePath)@"C:\ProgramData\Microsoft\Windows Defender\Platform")
.EnumerateDirectories(recursive:false)
.OrderByDescending(f => f.FileName)
.First()
.EnumerateFiles(recursive:true)
.First(f => f.FileName == (RelativePath)"MpCmdRun.exe");
}
public static async Task<(Hash, Result)> ScanStream(Stream stream)
{
var ms = new MemoryStream();
await stream.CopyToAsync(ms);
ms.Position = 0;
var hash = await ms.xxHashAsync();
ms.Position = 0;
await using var file = new TempFile();
try
{
await file.Path.WriteAllAsync(ms);
}
catch (IOException ex)
{
// Was caught before we could fully scan the file due to real-time virus scans
if (ex.Message.ToLowerInvariant().Contains("malware"))
{
return (hash, Result.Malware);
}
}
var process = new ProcessHelper
{
Path = ScannerPath(),
Arguments = new object[] {"-Scan", "-ScanType", "3", "-DisableRemediation", "-File", file.Path},
};
return (hash, (Result)await process.Start());
}
private static SignatureChecker ExecutableChecker = new SignatureChecker(Definitions.FileType.DLL,
Definitions.FileType.EXE,
Definitions.FileType.PIF,
Definitions.FileType.QXD,
Definitions.FileType.QTX,
Definitions.FileType.DRV,
Definitions.FileType.SYS,
Definitions.FileType.COM);
public static async Task<bool> ShouldScan(AbsolutePath path)
{
return await ExecutableChecker.MatchesAsync(path) != null;
}
}
}

View File

@ -74,6 +74,8 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Compression.BSA\Compression.BSA.csproj" /> <ProjectReference Include="..\Compression.BSA\Compression.BSA.csproj" />
<ProjectReference Include="..\Wabbajack.Common\Wabbajack.Common.csproj" /> <ProjectReference Include="..\Wabbajack.Common\Wabbajack.Common.csproj" />
<ProjectReference Include="..\Wabbajack.Downloaders.Dispatcher\Wabbajack.Downloaders.Dispatcher.csproj" />
<ProjectReference Include="..\Wabbajack.Networking.WabbajackClientApi\Wabbajack.Networking.WabbajackClientApi.csproj" />
<ProjectReference Include="..\Wabbajack.VirtualFileSystem\Wabbajack.VirtualFileSystem.csproj" /> <ProjectReference Include="..\Wabbajack.VirtualFileSystem\Wabbajack.VirtualFileSystem.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,16 +1,12 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using CefSharp; using CefSharp;
using Wabbajack.Common; using Microsoft.Extensions.Logging;
using Wabbajack.Common.Exceptions; using Microsoft.VisualBasic.CompilerServices;
using Wabbajack.Lib.LibCefHelpers; using Wabbajack.Lib.LibCefHelpers;
using Wabbajack.Networking.Http;
using Wabbajack.Paths;
namespace Wabbajack.Lib.WebAutomation namespace Wabbajack.Lib.WebAutomation
{ {
@ -18,8 +14,9 @@ namespace Wabbajack.Lib.WebAutomation
{ {
private readonly IWebBrowser _browser; private readonly IWebBrowser _browser;
public Action<Uri>? DownloadHandler { get; set; } public Action<Uri>? DownloadHandler { get; set; }
public CefSharpWrapper(IWebBrowser browser) public CefSharpWrapper(ILogger logger, IWebBrowser browser)
{ {
_logger = logger;
_browser = browser; _browser = browser;
_browser.DownloadHandler = new DownloadHandler(this); _browser.DownloadHandler = new DownloadHandler(this);
@ -59,6 +56,7 @@ namespace Wabbajack.Lib.WebAutomation
("We could not locate the item you are trying to view.", 404), ("We could not locate the item you are trying to view.", 404),
}; };
private static readonly Random RetryRandom = new Random(); private static readonly Random RetryRandom = new Random();
private readonly ILogger _logger;
public async Task<long> NavigateToAndDownload(Uri uri, AbsolutePath dest, bool quickMode = false, CancellationToken? token = null) public async Task<long> NavigateToAndDownload(Uri uri, AbsolutePath dest, bool quickMode = false, CancellationToken? token = null)
{ {
@ -86,7 +84,7 @@ namespace Wabbajack.Lib.WebAutomation
{ {
retryCount += 1; retryCount += 1;
var retry = RetryRandom.Next(retryCount * 5000, retryCount * 5000 * 2); var retry = RetryRandom.Next(retryCount * 5000, retryCount * 5000 * 2);
Utils.Log($"Got server load error from {uri} retying in {retry}ms [{err}]"); _logger.LogWarning("Got server load error from {Uri} retying in {Retry}ms [{Error}]", uri, retry, err);
await Task.Delay(TimeSpan.FromMilliseconds(retry)); await Task.Delay(TimeSpan.FromMilliseconds(retry));
goto RETRY; goto RETRY;
} }
@ -98,7 +96,7 @@ namespace Wabbajack.Lib.WebAutomation
throw new HttpException(httpCode,$"Web driver failed: {err}"); throw new HttpException(httpCode,$"Web driver failed: {err}");
} }
Utils.Log($"Loaded page {uri} starting download..."); _logger.LogInformation("Loaded page {Uri} starting download", uri);
return await handler.TaskResult; return await handler.TaskResult;
} }
finally { finally {

View File

@ -6,6 +6,7 @@ using System.Windows;
using CefSharp; using CefSharp;
using CefSharp.OffScreen; using CefSharp.OffScreen;
using HtmlAgilityPack; using HtmlAgilityPack;
using Microsoft.Extensions.Logging;
using Wabbajack.Common; using Wabbajack.Common;
using Wabbajack.Lib.LibCefHelpers; using Wabbajack.Lib.LibCefHelpers;
using Wabbajack.Paths; using Wabbajack.Paths;
@ -18,16 +19,16 @@ namespace Wabbajack.Lib.WebAutomation
private readonly IWebBrowser _browser; private readonly IWebBrowser _browser;
private readonly CefSharpWrapper _driver; private readonly CefSharpWrapper _driver;
public Driver() public Driver(ILogger logger)
{ {
_browser = new ChromiumWebBrowser(); _browser = new ChromiumWebBrowser();
_driver = new CefSharpWrapper(_browser); _driver = new CefSharpWrapper(logger, _browser);
} }
public static async Task<Driver> Create() public static async Task<Driver> Create(ILogger logger)
{ {
Helpers.Init(); Helpers.Init();
var driver = new Driver(); var driver = new Driver(logger);
await driver._driver.WaitForInitialized(); await driver._driver.WaitForInitialized();
return driver; return driver;
} }

View File

@ -0,0 +1,29 @@
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace Wabbajack.Networking.Http;
public static class Extensions
{
public static HttpRequestMessage UseChromeUserAgent(this HttpRequestMessage msg)
{
msg.Headers.UserAgent.Clear();
msg.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36");
return msg;
}
public static async Task<T> GetJsonFromSendAsync<T>(this HttpClient client, HttpRequestMessage msg,
JsonSerializerOptions opts, CancellationToken? token = null)
{
token ??= CancellationToken.None;
using var result = await client.SendAsync(msg, token.Value);
if (!result.IsSuccessStatusCode)
{
throw new HttpException(result);
}
return (await JsonSerializer.DeserializeAsync<T>(await result.Content.ReadAsStreamAsync(token.Value)))!;
}
}