Manual download improvements

This commit is contained in:
Timothy Baldridge 2020-02-10 17:30:38 -07:00
parent 7164296cb1
commit 608b8c77a7
11 changed files with 136 additions and 50 deletions

View File

@ -123,6 +123,26 @@ namespace Wabbajack.Common
.FirstOrDefault(g => g.SteamIDs != null && g.SteamIDs.Count > 0 && g.SteamIDs.Any(i => i == id));
}
/// <summary>
/// Tries to parse game data from an arbitrary string. Tries first via parsing as a game Enum, then by Nexus name,
/// <param nambe="someName"></param>
/// <returns></returns>
public static GameMetaData GetByFuzzyName(string someName)
{
if (Enum.TryParse(typeof(Game), someName, true, out var metadata)) return ((Game)metadata).MetaData();
GameMetaData result = null;
result = GetByNexusName(someName);
if (result != null) return result;
result = GetByMO2ArchiveName(someName);
if (result != null) return result;
return int.TryParse(someName, out int id) ? GetBySteamID(id) : null;
}
public static IReadOnlyDictionary<Game, GameMetaData> Games = new Dictionary<Game, GameMetaData>
{
{
@ -438,5 +458,6 @@ namespace Wabbajack.Common
}
}
};
}
}

View File

@ -82,34 +82,9 @@ namespace Wabbajack.Lib.Downloaders
public override async Task<bool> Download(Archive a, string destination)
{
var downloader = (ManualDownloader)GetDownloader();
var absPath = Path.Combine(downloader._downloadfolder.Path, a.Name);
using (await downloader.Lock.Wait())
{
try
{
Utils.Log($"You must manually visit {Url} and download {a.Name} file by hand");
Utils.Log($"Waiting for {a.Name}");
downloader._watcher.EnableRaisingEvents = true;
var watcher = downloader._fileEvents
.Where(f => f.Size == a.Size)
.Where(f => f.FullPath.FileHash(true) == a.Hash)
.Buffer(new TimeSpan(0, 5, 0), 1)
.Select(x => x.FirstOrDefault())
.FirstOrDefaultAsync();
Process.Start(Url);
absPath = (await watcher)?.FullPath;
if (!File.Exists(absPath))
throw new InvalidDataException($"File not found after manual download operation");
File.Move(absPath, destination);
}
finally
{
downloader._watcher.EnableRaisingEvents = false;
}
}
return true;
var (uri, client) = await Utils.Log(await ManuallyDownloadFile.Create(this)).Task;
var state = new HTTPDownloader.State {Url = uri.ToString(), Client = client};
return await state.Download(a, destination);
}
public override async Task<bool> Verify(Archive a)

View File

@ -252,17 +252,9 @@ namespace Wabbajack.Lib
Info("Getting Nexus api_key, please click authorize if a browser window appears");
if (IndexedArchives.Any(a => a.IniData?.General?.gameName != null))
{
var nexusClient = await NexusApiClient.Get();
if (!(await nexusClient.IsPremium())) Error($"User {(await nexusClient.Username())} is not a premium Nexus user, so we cannot access the necessary API calls, cannot continue");
}
UpdateTracker.NextStep("Verifying Files");
zEditIntegration.VerifyMerges(this);
UpdateTracker.NextStep("Gathering Archives");
await GatherArchives();
UpdateTracker.NextStep("Including Archive Metadata");

View File

@ -349,7 +349,7 @@ namespace Wabbajack.Lib.NexusApi
public static Uri ManualDownloadUrl(NexusDownloader.State state)
{
return new Uri($"https://www.nexusmods.com/{GameRegistry.GetByMO2ArchiveName(state.GameName).NexusName}/mods/{state.ModID}?tab=files");
return new Uri($"https://www.nexusmods.com/{GameRegistry.GetByFuzzyName(state.GameName).NexusName}/mods/{state.ModID}?tab=files");
}
}
}

View File

@ -1,7 +1,38 @@
namespace Wabbajack.Lib
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Wabbajack.Common;
using Wabbajack.Lib.Downloaders;
namespace Wabbajack.Lib
{
public class ManuallyDownloadFile
public class ManuallyDownloadFile : AUserIntervention
{
public ManualDownloader.State State { get; }
public override string ShortDescription { get; }
public override string ExtendedDescription { get; }
private TaskCompletionSource<(Uri, HttpClient)> _tcs = new TaskCompletionSource<(Uri, HttpClient)>();
public Task<(Uri, HttpClient)> Task => _tcs.Task;
private ManuallyDownloadFile(ManualDownloader.State state)
{
State = state;
}
public static async Task<ManuallyDownloadFile> Create(ManualDownloader.State state)
{
var result = new ManuallyDownloadFile(state);
return result;
}
public override void Cancel()
{
_tcs.SetCanceled();
}
public void Resume(Uri s, HttpClient client)
{
_tcs.SetResult((s, client));
}
}
}

View File

@ -219,12 +219,6 @@ namespace Wabbajack.Lib
InstallDirectives = results.Where(i => !(i is IgnoredDirectly)).ToList();
Info("Getting Nexus api_key, please click authorize if a browser window appears");
if (IndexedArchives.Any(a => a.IniData?.General?.gameName != null))
{
var nexusClient = await NexusApiClient.Get();
if (!await nexusClient.IsPremium()) Error($"User {await nexusClient.Username()} is not a premium Nexus user, so we cannot access the necessary API calls, cannot continue");
}
if (cancel.IsCancellationRequested) return false;
UpdateTracker.NextStep("Gathering Archives");

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using CefSharp;
@ -60,6 +61,8 @@ namespace Wabbajack.Lib.WebAutomation
while (!_browser.IsBrowserInitialized)
await Task.Delay(100);
}
public string Location => _browser.Address;
}
public class DownloadHandler : IDownloadHandler

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Wabbajack.Lib.LibCefHelpers;

View File

@ -1,7 +1,37 @@
namespace Wabbajack.Test
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Wabbajack.Common;
namespace Wabbajack.Test
{
[TestClass]
public class EncryptedDataTests
{
[TestMethod]
public async Task CanDetectNewEncryptedData()
{
var test_string = Guid.NewGuid().ToString();
var data = new ConcurrentBag<string>();
var events = Utils.HaveEncryptedJsonObservable(test_string).Subscribe(e =>
{
if (e)
data.Add(test_string);
else
data.Clear();
});
test_string.ToEcryptedJson(test_string);
await Task.Delay(100);
CollectionAssert.Contains(data, test_string);
}
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@ -11,6 +12,7 @@ using ReactiveUI;
using Wabbajack.Common;
using Wabbajack.Lib;
using Wabbajack.Lib.Downloaders;
using Wabbajack.Lib.LibCefHelpers;
using Wabbajack.Lib.NexusApi;
using Wabbajack.Lib.WebAutomation;
@ -27,7 +29,7 @@ namespace Wabbajack
private async Task WrapBrowserJob(IUserIntervention intervention, Func<WebBrowserVM, CancellationTokenSource, Task> toDo)
{
CancellationTokenSource cancel = new CancellationTokenSource();
var cancel = new CancellationTokenSource();
var oldPane = MainWindow.ActivePane;
var vm = await WebBrowserVM.GetNew();
MainWindow.NavigateTo(vm);
@ -85,6 +87,9 @@ namespace Wabbajack
case ManuallyDownloadNexusFile c:
await WrapBrowserJob(msg, (vm, cancel) => HandleManualNexusDownload(vm, cancel, c));
break;
case ManuallyDownloadFile c:
await WrapBrowserJob(msg, (vm, cancel) => HandleManualDownload(vm, cancel, c));
break;
case RequestBethesdaNetLogin c:
await WrapBethesdaNetLogin(c);
break;
@ -108,10 +113,42 @@ namespace Wabbajack
}
}
private async Task HandleManualDownload(WebBrowserVM vm, CancellationTokenSource cancel, ManuallyDownloadFile manuallyDownloadFile)
{
var browser = new CefSharpWrapper(vm.Browser);
vm.Instructions = $"Please locate and download {manuallyDownloadFile.State.Url}";
var result = new TaskCompletionSource<Uri>();
browser.DownloadHandler = uri =>
{
//var client = Helpers.GetClient(browser.GetCookies("").Result, browser.Location);
result.SetResult(uri);
};
await vm.Driver.WaitForInitialized();
await browser.NavigateTo(new Uri(manuallyDownloadFile.State.Url));
while (!cancel.IsCancellationRequested)
{
if (result.Task.IsCompleted)
{
var cookies = await Helpers.GetCookies();
var referer = browser.Location;
var client = Helpers.GetClient(cookies, referer);
manuallyDownloadFile.Resume(result.Task.Result, client);
break;
}
await Task.Delay(100);
}
}
private async Task HandleManualNexusDownload(WebBrowserVM vm, CancellationTokenSource cancel, ManuallyDownloadNexusFile manuallyDownloadNexusFile)
{
var state = manuallyDownloadNexusFile.State;
var game = GameRegistry.GetByMO2ArchiveName(state.GameName);
var game = GameRegistry.GetByFuzzyName(state.GameName);
var hrefs = new[]
{
$"/Core/Libs/Common/Widgets/DownloadPopUp?id={state.FileID}&game_id={game.NexusGameId}",
@ -124,6 +161,7 @@ namespace Wabbajack
browser.DownloadHandler = uri =>
{
manuallyDownloadNexusFile.Resume(uri);
browser.DownloadHandler = null;
};
await browser.NavigateTo(NexusApiClient.ManualDownloadUrl(manuallyDownloadNexusFile.State));

View File

@ -41,15 +41,16 @@
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="47" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<wabbajack:TopProgressView
Background="#92000000"
Title="{Binding Instructions}"
Grid.Row="0"
Grid.RowSpan="2"
Grid.RowSpan="1"
ShadowMargin="False" />
<Button
x:Name="BackButton"