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

187 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.Lib;
namespace Wabbajack
{
public class MO2InstallerVM : ViewModel, ISubInstallerVM
{
public InstallerVM Parent { get; }
2019-12-03 02:38:33 +00:00
2019-12-02 05:36:47 +00:00
public IReactiveCommand BeginCommand { get; }
[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; }
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.ExistCheckOptions.Off,
PathType = FilePickerVM.PathTypeOptions.Folder,
PromptTitle = "Select Installation Directory",
};
Location.AdditionalError = this.WhenAny(x => x.Location.TargetPath)
.Select(x => Utils.IsDirectoryPathValid(x));
DownloadLocation = new FilePickerVM()
{
ExistCheckOption = FilePickerVM.ExistCheckOptions.Off,
PathType = FilePickerVM.PathTypeOptions.Folder,
PromptTitle = "Select a location for MO2 downloads",
};
DownloadLocation.AdditionalError = this.WhenAny(x => x.DownloadLocation.TargetPath)
.Select(x => Utils.IsDirectoryPathValid(x));
2019-12-02 05:36:47 +00:00
BeginCommand = ReactiveCommand.CreateFromTask(
canExecute: Observable.CombineLatest(
2019-12-03 02:38:33 +00:00
this.WhenAny(x => x.Location.InError),
this.WhenAny(x => x.DownloadLocation.InError),
2019-12-03 05:40:59 +00:00
installerVM.WhenAny(x => x.ModListLocation.InError),
resultSelector: (loc, modlist, download) =>
2019-12-02 05:36:47 +00:00
{
return !loc && !download && !modlist;
2019-12-02 05:36:47 +00:00
})
.ObserveOnGuiThread(),
execute: async () =>
{
AInstaller installer;
try
{
installer = new MO2Installer(
2019-12-03 05:40:59 +00:00
archive: installerVM.ModListLocation.TargetPath,
2019-12-02 05:36:47 +00:00
modList: installerVM.ModList.SourceModList,
2019-12-03 02:38:33 +00:00
outputFolder: Location.TargetPath,
downloadFolder: DownloadLocation.TargetPath);
2019-12-02 05:36:47 +00:00
}
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;
}
});
});
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))
.ToProperty(this, nameof(CurrentSettings));
this.WhenAny(x => x.CurrentSettings)
.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-02 05:36:47 +00:00
}
}