wabbajack/Wabbajack/View Models/Installers/VortexInstallerVM.cs
2020-01-07 06:50:11 -07:00

89 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Wabbajack.Common;
using Wabbajack.Lib;
using Wabbajack.Util;
namespace Wabbajack
{
public class VortexInstallerVM : ViewModel, ISubInstallerVM
{
public InstallerVM Parent { get; }
[Reactive]
public AInstaller ActiveInstallation { get; private set; }
private readonly ObservableAsPropertyHelper<string> _DownloadLocation;
public string DownloadLocation => _DownloadLocation.Value;
private readonly ObservableAsPropertyHelper<string> _StagingLocation;
public string StagingLocation => _StagingLocation.Value;
private readonly ObservableAsPropertyHelper<Game> _TargetGame;
public Game TargetGame => _TargetGame.Value;
public bool SupportsAfterInstallNavigation => false;
public int ConfigVisualVerticalOffset => 0;
public IObservable<bool> CanInstall { get; }
public VortexInstallerVM(InstallerVM installerVM)
{
Parent = installerVM;
_TargetGame = installerVM.WhenAny(x => x.ModList.SourceModList.GameType)
.ToProperty(this, nameof(TargetGame));
CanInstall = Observable.CombineLatest(
this.WhenAny(x => x.TargetGame)
.Select(game => VortexCompiler.IsActiveVortexGame(game)),
installerVM.WhenAny(x => x.ModListLocation.InError),
resultSelector: (isVortexGame, modListErr) => isVortexGame && !modListErr);
}
public void Unload()
{
}
public void AfterInstallNavigation()
{
throw new NotImplementedException();
}
public async Task Install()
{
AInstaller installer;
var download = VortexCompiler.RetrieveDownloadLocation(TargetGame);
var staging = VortexCompiler.RetrieveStagingLocation(TargetGame);
installer = new VortexInstaller(
archive: Parent.ModListLocation.TargetPath,
modList: Parent.ModList.SourceModList,
outputFolder: staging,
downloadFolder: download,
parameters: SystemParametersConstructor.Create());
await Task.Run(async () =>
{
try
{
var workTask = installer.Begin();
ActiveInstallation = installer;
await workTask;
}
finally
{
ActiveInstallation = null;
}
});
}
}
}