2019-11-29 05:52:33 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-11-30 09:08:04 +00:00
|
|
|
|
using System.Diagnostics;
|
2019-11-29 05:52:33 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
2019-11-30 09:08:04 +00:00
|
|
|
|
using System.Reactive;
|
2019-11-29 05:52:33 +00:00
|
|
|
|
using System.Reactive.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using Alphaleonis.Win32.Filesystem;
|
|
|
|
|
using ReactiveUI;
|
2019-11-30 09:08:04 +00:00
|
|
|
|
using ReactiveUI.Fody.Helpers;
|
2019-11-29 05:52:33 +00:00
|
|
|
|
using Wabbajack.Common;
|
|
|
|
|
using Wabbajack.Lib;
|
|
|
|
|
using Wabbajack.Lib.Downloaders;
|
|
|
|
|
using Wabbajack.Lib.ModListRegistry;
|
|
|
|
|
|
2019-11-30 09:08:04 +00:00
|
|
|
|
namespace Wabbajack
|
2019-11-29 05:52:33 +00:00
|
|
|
|
{
|
|
|
|
|
public class ModListMetadataVM : ViewModel
|
|
|
|
|
{
|
2019-11-30 09:08:04 +00:00
|
|
|
|
public ModlistMetadata Metadata { get; }
|
|
|
|
|
private ModListGalleryVM _parent;
|
2019-11-29 05:52:33 +00:00
|
|
|
|
|
2019-11-30 09:08:04 +00:00
|
|
|
|
public ICommand OpenWebsiteCommand { get; }
|
|
|
|
|
public ICommand ExecuteCommand { get; }
|
2019-11-29 05:52:33 +00:00
|
|
|
|
|
2019-11-30 09:08:04 +00:00
|
|
|
|
private readonly ObservableAsPropertyHelper<bool> _Exists;
|
|
|
|
|
public bool Exists => _Exists.Value;
|
2019-11-29 05:52:33 +00:00
|
|
|
|
|
2019-11-30 09:08:04 +00:00
|
|
|
|
public string Location => Path.Combine(Consts.ModListDownloadFolder, Metadata.Links.MachineURL + ExtensionManager.Extension);
|
2019-11-29 05:52:33 +00:00
|
|
|
|
|
2019-11-30 09:08:04 +00:00
|
|
|
|
[Reactive]
|
|
|
|
|
public double ProgressPercent { get; private set; }
|
|
|
|
|
|
2019-12-17 04:06:30 +00:00
|
|
|
|
[Reactive]
|
|
|
|
|
public bool IsBroken { get; private set; }
|
|
|
|
|
|
2019-11-30 09:08:04 +00:00
|
|
|
|
public ModListMetadataVM(ModListGalleryVM parent, ModlistMetadata metadata)
|
2019-11-29 05:52:33 +00:00
|
|
|
|
{
|
|
|
|
|
_parent = parent;
|
|
|
|
|
Metadata = metadata;
|
2019-12-17 04:06:30 +00:00
|
|
|
|
IsBroken = metadata.ValidationSummary.HasFailures;
|
2019-11-30 09:08:04 +00:00
|
|
|
|
OpenWebsiteCommand = ReactiveCommand.Create(() => Process.Start($"https://www.wabbajack.org/modlist/{Metadata.Links.MachineURL}"));
|
2019-12-17 04:06:30 +00:00
|
|
|
|
ExecuteCommand = ReactiveCommand.CreateFromObservable<Unit, bool>(
|
|
|
|
|
canExecute: this.WhenAny(x => x.IsBroken).Select(x => !x),
|
|
|
|
|
execute: (unit) =>
|
2019-11-30 09:08:04 +00:00
|
|
|
|
Observable.Return(unit)
|
|
|
|
|
.WithLatestFrom(
|
|
|
|
|
this.WhenAny(x => x.Exists),
|
|
|
|
|
(_, e) => e)
|
|
|
|
|
// Do any download work on background thread
|
|
|
|
|
.ObserveOn(RxApp.TaskpoolScheduler)
|
|
|
|
|
.SelectTask(async (exists) =>
|
|
|
|
|
{
|
|
|
|
|
if (!exists)
|
|
|
|
|
{
|
|
|
|
|
await Download();
|
|
|
|
|
// Return an updated check on exists
|
|
|
|
|
return File.Exists(Location);
|
|
|
|
|
}
|
|
|
|
|
return exists;
|
|
|
|
|
})
|
|
|
|
|
// Do any install page swap over on GUI thread
|
|
|
|
|
.ObserveOnGuiThread()
|
|
|
|
|
.Do(exists =>
|
|
|
|
|
{
|
|
|
|
|
if (exists)
|
|
|
|
|
{
|
|
|
|
|
_parent.MWVM.OpenInstaller(Path.GetFullPath(Location));
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
_Exists = Observable.Interval(TimeSpan.FromSeconds(0.5))
|
|
|
|
|
.Unit()
|
|
|
|
|
.StartWith(Unit.Default)
|
2019-12-18 04:30:36 +00:00
|
|
|
|
.Select(_ => !metadata.NeedsDownload(Location))
|
2019-11-30 09:08:04 +00:00
|
|
|
|
.ToProperty(this, nameof(Exists));
|
2019-11-29 05:52:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-30 09:08:04 +00:00
|
|
|
|
private Task Download()
|
2019-11-29 05:52:33 +00:00
|
|
|
|
{
|
2019-11-30 09:08:04 +00:00
|
|
|
|
ProgressPercent = 0d;
|
2019-11-29 05:52:33 +00:00
|
|
|
|
var queue = new WorkQueue(1);
|
2019-11-30 09:08:04 +00:00
|
|
|
|
var sub = queue.Status.Select(i => i.ProgressPercent)
|
|
|
|
|
.Subscribe(percent => ProgressPercent = percent);
|
|
|
|
|
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
|
2019-12-15 04:33:48 +00:00
|
|
|
|
var metric = Metrics.Send("downloading", Metadata.Title);
|
2019-12-04 01:26:26 +00:00
|
|
|
|
queue.QueueTask(async () =>
|
2019-11-29 05:52:33 +00:00
|
|
|
|
{
|
|
|
|
|
var downloader = DownloadDispatcher.ResolveArchive(Metadata.Links.Download);
|
2019-12-06 05:29:17 +00:00
|
|
|
|
await downloader.Download(new Archive{ Name = Metadata.Title, Size = Metadata.DownloadMetadata?.Size ?? 0}, Location);
|
2019-11-29 05:52:33 +00:00
|
|
|
|
Location.FileHashCached();
|
|
|
|
|
sub.Dispose();
|
2019-11-30 09:08:04 +00:00
|
|
|
|
tcs.SetResult(true);
|
2019-11-29 05:52:33 +00:00
|
|
|
|
});
|
|
|
|
|
|
2019-11-30 09:08:04 +00:00
|
|
|
|
return tcs.Task;
|
2019-11-29 05:52:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|