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

185 lines
7.0 KiB
C#
Raw Normal View History

2019-12-02 05:36:47 +00:00
using System;
using System.Collections.Generic;
2019-12-11 04:59:15 +00:00
using System.Diagnostics;
2019-12-03 02:38:33 +00:00
using System.IO;
2019-12-02 05:36:47 +00:00
using System.Linq;
2019-12-03 02:38:33 +00:00
using System.Reactive.Disposables;
2019-12-02 05:36:47 +00:00
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Wabbajack.Common;
using Wabbajack.Lib;
2020-01-07 13:50:11 +00:00
using Wabbajack.Util;
2019-12-02 05:36:47 +00:00
namespace Wabbajack
{
public class MO2InstallerVM : ViewModel, ISubInstallerVM
{
public InstallerVM Parent { get; }
2019-12-03 02:38:33 +00:00
2019-12-19 01:14:21 +00:00
public IObservable<bool> CanInstall { get; }
2019-12-02 05:36:47 +00:00
[Reactive]
public AInstaller ActiveInstallation { get; private set; }
2019-12-03 02:38:33 +00:00
private readonly ObservableAsPropertyHelper<Mo2ModlistInstallationSettings> _CurrentSettings;
public Mo2ModlistInstallationSettings CurrentSettings => _CurrentSettings.Value;
public FilePickerVM Location { get; }
public FilePickerVM DownloadLocation { get; }
2019-12-11 04:59:15 +00:00
public bool SupportsAfterInstallNavigation => true;
[Reactive]
public bool AutomaticallyOverwrite { get; set; }
public int ConfigVisualVerticalOffset => 25;
2019-12-02 05:36:47 +00:00
public MO2InstallerVM(InstallerVM installerVM)
{
Parent = installerVM;
2019-12-03 02:38:33 +00:00
Location = new FilePickerVM()
{
ExistCheckOption = FilePickerVM.CheckOptions.Off,
2019-12-03 02:38:33 +00:00
PathType = FilePickerVM.PathTypeOptions.Folder,
PromptTitle = "Select Installation Directory",
};
DownloadLocation = new FilePickerVM()
{
ExistCheckOption = FilePickerVM.CheckOptions.Off,
2019-12-03 02:38:33 +00:00
PathType = FilePickerVM.PathTypeOptions.Folder,
PromptTitle = "Select a location for MO2 downloads",
};
DownloadLocation.AdditionalError = this.WhenAny(x => x.DownloadLocation.TargetPath)
.Select(x => Utils.IsDirectoryPathValid(x));
Location.AdditionalError = Observable.CombineLatest(
this.WhenAny(x => x.Location.TargetPath),
this.WhenAny(x => x.DownloadLocation.TargetPath),
resultSelector: (target, download) => (target, download))
2019-12-25 01:31:43 +00:00
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(i => MO2Installer.CheckValidInstallPath(i.target, i.download))
.ObserveOnGuiThread();
2019-12-03 02:38:33 +00:00
2019-12-19 01:14:21 +00:00
CanInstall = Observable.CombineLatest(
this.WhenAny(x => x.Location.InError),
this.WhenAny(x => x.DownloadLocation.InError),
installerVM.WhenAny(x => x.ModListLocation.InError),
resultSelector: (loc, modlist, download) =>
2019-12-02 05:36:47 +00:00
{
2019-12-19 01:14:21 +00:00
return !loc && !download && !modlist;
2019-12-02 05:36:47 +00:00
});
2019-12-03 02:38:33 +00:00
// Have Installation location updates modify the downloads location if empty
this.WhenAny(x => x.Location.TargetPath)
.Skip(1) // Don't do it initially
.Subscribe(installPath =>
{
if (string.IsNullOrWhiteSpace(DownloadLocation.TargetPath))
{
DownloadLocation.TargetPath = Path.Combine(installPath, "downloads");
}
})
.DisposeWith(CompositeDisposable);
// Load settings
2019-12-03 05:40:59 +00:00
_CurrentSettings = installerVM.WhenAny(x => x.ModListLocation.TargetPath)
2019-12-03 02:38:33 +00:00
.Select(path => path == null ? null : installerVM.MWVM.Settings.Installer.Mo2ModlistSettings.TryCreate(path))
.ToGuiProperty(this, nameof(CurrentSettings));
this.WhenAny(x => x.CurrentSettings)
2019-12-03 02:38:33 +00:00
.Pairwise()
.Subscribe(settingsPair =>
{
SaveSettings(settingsPair.Previous);
if (settingsPair.Current == null) return;
Location.TargetPath = settingsPair.Current.InstallationLocation;
DownloadLocation.TargetPath = settingsPair.Current.DownloadLocation;
AutomaticallyOverwrite = settingsPair.Current.AutomaticallyOverrideExistingInstall;
2019-12-03 02:38:33 +00:00
})
.DisposeWith(CompositeDisposable);
installerVM.MWVM.Settings.SaveSignal
.Subscribe(_ => SaveSettings(CurrentSettings))
.DisposeWith(CompositeDisposable);
// Hook onto user interventions, and intercept MO2 specific ones for customization
this.WhenAny(x => x.ActiveInstallation.LogMessages)
.Switch()
.Subscribe(x =>
{
switch (x)
{
case ConfirmUpdateOfExistingInstall c:
if (AutomaticallyOverwrite)
{
c.Confirm();
}
break;
default:
break;
}
})
.DisposeWith(CompositeDisposable);
2019-12-02 05:36:47 +00:00
}
public void Unload()
{
2019-12-03 02:38:33 +00:00
SaveSettings(this.CurrentSettings);
}
private void SaveSettings(Mo2ModlistInstallationSettings settings)
{
Parent.MWVM.Settings.Installer.LastInstalledListLocation = Parent.ModListLocation.TargetPath;
2019-12-03 02:38:33 +00:00
if (settings == null) return;
settings.InstallationLocation = Location.TargetPath;
settings.DownloadLocation = DownloadLocation.TargetPath;
settings.AutomaticallyOverrideExistingInstall = AutomaticallyOverwrite;
2019-12-02 05:36:47 +00:00
}
2019-12-11 04:59:15 +00:00
public void AfterInstallNavigation()
{
Process.Start("explorer.exe", Location.TargetPath);
}
2019-12-19 01:14:21 +00:00
public async Task<bool> Install()
2019-12-19 01:14:21 +00:00
{
using (var installer = new MO2Installer(
2019-12-19 01:14:21 +00:00
archive: Parent.ModListLocation.TargetPath,
modList: Parent.ModList.SourceModList,
outputFolder: Location.TargetPath,
2020-01-07 13:50:11 +00:00
downloadFolder: DownloadLocation.TargetPath,
parameters: SystemParametersConstructor.Create()))
{
Parent.MWVM.Settings.Performance.AttachToBatchProcessor(installer);
2019-12-19 01:14:21 +00:00
return await Task.Run(async () =>
2019-12-19 01:14:21 +00:00
{
try
{
var workTask = installer.Begin();
ActiveInstallation = installer;
return await workTask;
}
finally
{
ActiveInstallation = null;
}
});
}
2019-12-19 01:14:21 +00:00
}
public IUserIntervention InterventionConverter(IUserIntervention intervention)
{
switch (intervention)
{
case ConfirmUpdateOfExistingInstall confirm:
return new ConfirmUpdateOfExistingInstallVM(this, confirm);
default:
return intervention;
}
}
2019-12-02 05:36:47 +00:00
}
}