wabbajack/Wabbajack/View Models/Installers/MO2InstallerVM.cs

85 lines
3.0 KiB
C#
Raw Normal View History

2019-12-02 05:36:47 +00:00
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;
namespace Wabbajack
{
public class MO2InstallerVM : ViewModel, ISubInstallerVM
{
public IReactiveCommand BeginCommand { get; }
[Reactive]
public AInstaller ActiveInstallation { get; private set; }
public MO2InstallerVM(InstallerVM installerVM)
{
BeginCommand = ReactiveCommand.CreateFromTask(
canExecute: Observable.CombineLatest(
installerVM.WhenAny(x => x.Location.InError),
installerVM.WhenAny(x => x.DownloadLocation.InError),
resultSelector: (loc, download) =>
{
return !loc && !download;
})
.ObserveOnGuiThread(),
execute: async () =>
{
AInstaller installer;
try
{
installer = new MO2Installer(
archive: installerVM.ModListPath.TargetPath,
modList: installerVM.ModList.SourceModList,
outputFolder: installerVM.Location.TargetPath,
downloadFolder: installerVM.DownloadLocation.TargetPath);
}
catch (Exception ex)
{
while (ex.InnerException != null) ex = ex.InnerException;
Utils.Log(ex.StackTrace);
Utils.Log(ex.ToString());
Utils.Log($"{ex.Message} - Can't continue");
ActiveInstallation = null;
return;
}
await Task.Run(async () =>
{
IDisposable subscription = null;
try
{
var workTask = installer.Begin();
ActiveInstallation = installer;
await workTask;
}
catch (Exception ex)
{
while (ex.InnerException != null) ex = ex.InnerException;
Utils.Log(ex.StackTrace);
Utils.Log(ex.ToString());
Utils.Log($"{ex.Message} - Can't continue");
}
finally
{
// Dispose of CPU tracking systems
subscription?.Dispose();
ActiveInstallation = null;
}
});
});
}
public void Unload()
{
}
}
}