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

190 lines
7.3 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.Common.StatusFeed;
2019-12-02 05:36:47 +00:00
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
2020-07-25 22:58:45 +00:00
private readonly ObservableAsPropertyHelper<ErrorResponse> _CanInstall;
public ErrorResponse CanInstall => _CanInstall.Value;
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
2020-07-25 22:58:45 +00:00
_CanInstall = Observable.CombineLatest(
this.WhenAny(x => x.Location.ErrorState),
this.WhenAny(x => x.DownloadLocation.ErrorState),
installerVM.WhenAny(x => x.ModListLocation.ErrorState),
resultSelector: (loc, modlist, download) =>
{
return ErrorResponse.FirstFail(loc, modlist, download);
})
.ToProperty(this, nameof(CanInstall));
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 =>
{
2020-03-28 20:04:22 +00:00
if (DownloadLocation.TargetPath == default)
2019-12-03 02:38:33 +00:00
{
2020-03-28 20:04:22 +00:00
DownloadLocation.TargetPath = installPath.Combine("downloads");
2019-12-03 02:38:33 +00:00
}
})
.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)
.Select(x => x?.LogMessages ?? Observable.Empty<IStatusMessage>())
.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()
{
2020-03-28 20:04:22 +00:00
Process.Start("explorer.exe", (string)Location.TargetPath);
2019-12-11 04:59:15 +00:00
}
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()))
{
2020-08-05 22:01:45 +00:00
installer.UseCompression = Parent.MWVM.Settings.Filters.UseCompression;
2020-09-12 20:23:03 +00:00
Parent.MWVM.Settings.Performance.SetProcessorSettings(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
}
}