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

444 lines
20 KiB
C#
Raw Normal View History

using System;
using ReactiveUI;
2019-08-30 23:57:56 +00:00
using System.Diagnostics;
2019-07-22 22:17:46 +00:00
using System.IO;
using System.IO.Compression;
2019-09-26 03:18:36 +00:00
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
2019-07-22 22:17:46 +00:00
using System.Reflection;
using System.Threading.Tasks;
2019-07-31 03:59:19 +00:00
using System.Windows;
2019-09-26 03:18:36 +00:00
using System.Windows.Media.Imaging;
2019-07-22 22:17:46 +00:00
using Wabbajack.Common;
using Wabbajack.Lib;
2019-11-02 23:23:11 +00:00
using ReactiveUI.Fody.Helpers;
2019-11-09 01:53:32 +00:00
using System.Windows.Media;
using DynamicData;
using DynamicData.Binding;
2019-12-04 04:12:08 +00:00
using Wabbajack.Common.StatusFeed;
2019-12-02 05:36:47 +00:00
using System.Reactive;
using System.Collections.Generic;
using System.Windows.Input;
using Microsoft.WindowsAPICodePack.Dialogs;
using Wabbajack.Common.IO;
2019-07-22 22:17:46 +00:00
namespace Wabbajack
{
public class InstallerVM : ViewModel, IBackNavigatingVM, ICpuStatusVM
2019-07-22 22:17:46 +00:00
{
public SlideShow Slideshow { get; }
public MainWindowVM MWVM { get; }
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<ModListVM> _modList;
public ModListVM ModList => _modList.Value;
2019-12-03 05:40:59 +00:00
public FilePickerVM ModListLocation { get; }
[Reactive]
public ViewModel NavigateBackTarget { get; set; }
2019-12-02 05:36:47 +00:00
private readonly ObservableAsPropertyHelper<ISubInstallerVM> _installer;
public ISubInstallerVM Installer => _installer.Value;
2019-10-12 18:42:47 +00:00
private readonly ObservableAsPropertyHelper<bool> _installing;
public bool Installing => _installing.Value;
2019-11-02 23:23:11 +00:00
[Reactive]
public bool StartedInstallation { get; set; }
[Reactive]
2019-12-19 05:22:39 +00:00
public ErrorResponse? Completed { get; set; }
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<ImageSource> _image;
public ImageSource Image => _image.Value;
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<string> _titleText;
public string TitleText => _titleText.Value;
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<string> _authorText;
public string AuthorText => _authorText.Value;
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<string> _description;
public string Description => _description.Value;
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<string> _progressTitle;
public string ProgressTitle => _progressTitle.Value;
2019-11-07 05:33:08 +00:00
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<string> _modListName;
public string ModListName => _modListName.Value;
2019-11-07 05:37:40 +00:00
2020-02-08 04:35:08 +00:00
private readonly ObservableAsPropertyHelper<Percent> _percentCompleted;
public Percent PercentCompleted => _percentCompleted.Value;
public ObservableCollectionExtended<CPUDisplayVM> StatusList { get; } = new ObservableCollectionExtended<CPUDisplayVM>();
2019-12-04 04:12:08 +00:00
public ObservableCollectionExtended<IStatusMessage> Log => MWVM.Log;
2019-12-02 05:36:47 +00:00
private readonly ObservableAsPropertyHelper<ModManager?> _TargetManager;
public ModManager? TargetManager => _TargetManager.Value;
private readonly ObservableAsPropertyHelper<IUserIntervention> _ActiveGlobalUserIntervention;
public IUserIntervention ActiveGlobalUserIntervention => _ActiveGlobalUserIntervention.Value;
private readonly ObservableAsPropertyHelper<(int CurrentCPUs, int DesiredCPUs)> _CurrentCpuCount;
public (int CurrentCPUs, int DesiredCPUs) CurrentCpuCount => _CurrentCpuCount.Value;
2020-01-16 02:29:45 +00:00
private readonly ObservableAsPropertyHelper<bool> _LoadingModlist;
public bool LoadingModlist => _LoadingModlist.Value;
private readonly ObservableAsPropertyHelper<bool> _IsActive;
public bool IsActive => _IsActive.Value;
// Command properties
public ReactiveCommand<Unit, Unit> ShowManifestCommand { get; }
public ReactiveCommand<Unit, Unit> OpenReadmeCommand { get; }
public ReactiveCommand<Unit, Unit> VisitModListWebsiteCommand { get; }
2020-01-07 05:44:05 +00:00
public ReactiveCommand<Unit, Unit> BackCommand { get; }
public ReactiveCommand<Unit, Unit> CloseWhenCompleteCommand { get; }
public ReactiveCommand<Unit, Unit> GoToInstallCommand { get; }
public ReactiveCommand<Unit, Unit> BeginCommand { get; }
2019-11-24 08:12:28 +00:00
public InstallerVM(MainWindowVM mainWindowVM)
2019-07-30 21:45:04 +00:00
{
2019-10-12 08:02:58 +00:00
if (Path.GetDirectoryName(Assembly.GetEntryAssembly().Location.ToLower()) == KnownFolders.Downloads.Path.ToLower())
2019-09-14 04:35:42 +00:00
{
2020-06-01 20:26:03 +00:00
Utils.Error(new CriticalFailureIntervention(
"Wabbajack is running inside your Downloads folder. This folder is often highly monitored by antivirus software and these can often " +
2019-10-12 08:02:58 +00:00
"conflict with the operations Wabbajack needs to perform. Please move this executable outside of your Downloads folder and then restart the app.",
2020-06-01 20:26:03 +00:00
"Cannot run inside Downloads"));
2019-09-14 04:35:42 +00:00
}
2019-11-21 15:04:33 +00:00
MWVM = mainWindowVM;
ModListLocation = new FilePickerVM
{
ExistCheckOption = FilePickerVM.CheckOptions.On,
PathType = FilePickerVM.PathTypeOptions.File,
2020-01-13 21:11:07 +00:00
PromptTitle = "Select a ModList to install"
};
ModListLocation.Filters.Add(new CommonFileDialogFilter("Wabbajack Modlist", ".wabbajack"));
2019-12-02 05:36:47 +00:00
// Swap to proper sub VM based on selected type
_installer = this.WhenAny(x => x.TargetManager)
// Delay so the initial VM swap comes in immediately, subVM comes right after
.DelayInitial(TimeSpan.FromMilliseconds(50), RxApp.MainThreadScheduler)
.Select<ModManager?, ISubInstallerVM>(type =>
{
switch (type)
{
case ModManager.MO2:
return new MO2InstallerVM(this);
default:
return null;
}
})
// Unload old VM
.Pairwise()
.Do(pair =>
{
pair.Previous?.Unload();
})
.Select(p => p.Current)
.ToGuiProperty(this, nameof(Installer));
2019-12-02 05:36:47 +00:00
// Load settings
2019-12-03 02:38:33 +00:00
MWVM.Settings.SaveSignal
.Subscribe(_ =>
{
2019-12-03 05:40:59 +00:00
MWVM.Settings.Installer.LastInstalledListLocation = ModListLocation.TargetPath;
})
2019-11-21 15:04:33 +00:00
.DisposeWith(CompositeDisposable);
2019-09-14 04:35:42 +00:00
_IsActive = this.ConstructIsActive(MWVM)
.ToGuiProperty(this, nameof(IsActive));
// Active path represents the path to currently have loaded
// If we're not actively showing, then "unload" the active path
var activePath = Observable.CombineLatest(
this.WhenAny(x => x.ModListLocation.TargetPath),
this.WhenAny(x => x.IsActive),
resultSelector: (path, active) => (path, active))
.Select(x =>
{
2020-03-28 20:04:22 +00:00
if (!x.active) return default;
return x.path;
})
// Throttle slightly so changes happen more atomically
.Throttle(TimeSpan.FromMilliseconds(50), RxApp.MainThreadScheduler)
.Replay(1)
.RefCount();
_modList = activePath
.ObserveOn(RxApp.TaskpoolScheduler)
// Convert from active path to modlist VM
.Select(modListPath =>
{
2020-03-28 20:04:22 +00:00
if (modListPath == default) return default;
if (!modListPath.Exists) return default;
return new ModListVM(modListPath);
})
2020-01-16 03:54:06 +00:00
.DisposeOld()
.ObserveOnGuiThread()
2019-11-03 06:01:19 +00:00
.StartWith(default(ModListVM))
.ToGuiProperty(this, nameof(ModList));
2020-01-16 03:54:06 +00:00
// Force GC collect when modlist changes, just to make sure we clean up any loose large items immediately
this.WhenAny(x => x.ModList)
.Delay(TimeSpan.FromMilliseconds(50), RxApp.MainThreadScheduler)
2020-01-16 03:54:06 +00:00
.Subscribe(x =>
{
GC.Collect();
});
2020-01-16 02:29:45 +00:00
_LoadingModlist = Observable.Merge(
// When active path changes, mark as loading
activePath
2020-01-16 02:29:45 +00:00
.Select(_ => true),
// When the resulting modlist comes in, mark it as done
this.WhenAny(x => x.ModList)
.Select(_ => false))
.ToGuiProperty(this, nameof(LoadingModlist));
2019-12-02 05:36:47 +00:00
_installing = this.WhenAny(x => x.Installer.ActiveInstallation)
.Select(i => i != null)
.ToGuiProperty(this, nameof(Installing));
2019-12-02 05:36:47 +00:00
_TargetManager = this.WhenAny(x => x.ModList)
.Select(modList => modList?.ModManager)
.ToGuiProperty(this, nameof(TargetManager));
2020-01-13 21:11:07 +00:00
// Add additional error check on ModList
2019-12-03 05:40:59 +00:00
ModListLocation.AdditionalError = this.WhenAny(x => x.ModList)
.Select<ModListVM, IErrorResponse>(modList =>
{
if (modList == null) return ErrorResponse.Fail("Modlist path resulted in a null object.");
if (modList.Error != null) return ErrorResponse.Fail("Modlist is corrupt", modList.Error);
if (modList.WabbajackVersion != null && modList.WabbajackVersion > Consts.CurrentWabbajackVersion)
return ErrorResponse.Fail("The Modlist you are trying to install was made using a newer Version of Wabbajack. Please update Wabbajack before installing!");
return ErrorResponse.Success;
});
2019-11-24 23:42:28 +00:00
BackCommand = ReactiveCommand.Create(
2019-12-14 03:47:09 +00:00
execute: () =>
{
StartedInstallation = false;
2019-12-19 05:22:39 +00:00
Completed = null;
mainWindowVM.NavigateTo(mainWindowVM.ModeSelectionVM);
2019-12-14 03:47:09 +00:00
},
canExecute: Observable.CombineLatest(
this.WhenAny(x => x.Installing)
.Select(x => !x),
this.ConstructCanNavigateBack(),
resultSelector: (i, b) => i && b)
.ObserveOnGuiThread());
2019-11-24 23:42:28 +00:00
2019-12-02 05:36:47 +00:00
_percentCompleted = this.WhenAny(x => x.Installer.ActiveInstallation)
.StartWith(default(AInstaller))
2019-12-14 03:47:09 +00:00
.CombineLatest(
this.WhenAny(x => x.Completed),
(installer, completed) =>
{
2019-12-14 03:47:09 +00:00
if (installer == null)
{
2020-02-08 04:35:08 +00:00
return Observable.Return<Percent>(completed != null ? Percent.One : Percent.Zero);
2019-12-14 03:47:09 +00:00
}
2020-02-08 04:35:08 +00:00
return installer.PercentCompleted.StartWith(Percent.Zero);
2019-12-14 03:47:09 +00:00
})
.Switch()
.Debounce(TimeSpan.FromMilliseconds(25), RxApp.MainThreadScheduler)
.ToGuiProperty(this, nameof(PercentCompleted));
2019-11-21 15:04:33 +00:00
Slideshow = new SlideShow(this);
2020-01-13 21:11:07 +00:00
// Set display items to ModList if configuring or complete,
// or to the current slideshow data if installing
2019-11-21 15:45:00 +00:00
_image = Observable.CombineLatest(
this.WhenAny(x => x.ModList.Error),
2019-11-03 06:01:19 +00:00
this.WhenAny(x => x.ModList)
.Select(x => x?.ImageObservable ?? Observable.Return(default(BitmapImage)))
.Switch()
.StartWith(default(BitmapImage)),
this.WhenAny(x => x.Slideshow.Image)
.StartWith(default(BitmapImage)),
this.WhenAny(x => x.Installing),
this.WhenAny(x => x.LoadingModlist),
resultSelector: (err, modList, slideshow, installing, loading) =>
{
if (err != null)
{
2020-01-25 23:25:51 +00:00
return ResourceLinks.WabbajackErrLogo.Value;
}
if (loading) return default;
return installing ? slideshow : modList;
})
2019-11-09 01:53:32 +00:00
.Select<BitmapImage, ImageSource>(x => x)
.ToGuiProperty(this, nameof(Image));
2019-11-21 15:45:00 +00:00
_titleText = Observable.CombineLatest(
this.WhenAny(x => x.ModList)
.Select(modList => modList?.Name ?? string.Empty),
this.WhenAny(x => x.Slideshow.TargetMod.State.Name)
2019-11-03 06:01:19 +00:00
.StartWith(default(string)),
this.WhenAny(x => x.Installing),
resultSelector: (modList, mod, installing) => installing ? mod : modList)
.ToGuiProperty(this, nameof(TitleText));
2019-11-21 15:45:00 +00:00
_authorText = Observable.CombineLatest(
this.WhenAny(x => x.ModList)
.Select(modList => modList?.Author ?? string.Empty),
this.WhenAny(x => x.Slideshow.TargetMod.State.Author)
2019-11-03 06:01:19 +00:00
.StartWith(default(string)),
this.WhenAny(x => x.Installing),
resultSelector: (modList, mod, installing) => installing ? mod : modList)
.ToGuiProperty(this, nameof(AuthorText));
2019-11-21 15:45:00 +00:00
_description = Observable.CombineLatest(
this.WhenAny(x => x.ModList)
.Select(modList => modList?.Description ?? string.Empty),
this.WhenAny(x => x.Slideshow.TargetMod.State.Description)
2019-11-03 06:01:19 +00:00
.StartWith(default(string)),
this.WhenAny(x => x.Installing),
resultSelector: (modList, mod, installing) => installing ? mod : modList)
.ToGuiProperty(this, nameof(Description));
_modListName = Observable.CombineLatest(
this.WhenAny(x => x.ModList.Error)
.Select(x => x != null),
this.WhenAny(x => x.ModList)
.Select(x => x?.Name),
resultSelector: (err, name) =>
{
if (err) return "Corrupted Modlist";
return name;
})
.ToGuiProperty(this, nameof(ModListName));
ShowManifestCommand = ReactiveCommand.Create(() =>
{
new ManifestWindow(ModList.SourceModList).Show();
}, this.WhenAny(x => x.ModList)
.Select(x => x?.SourceModList != null)
.ObserveOnGuiThread());
2019-11-21 15:04:33 +00:00
OpenReadmeCommand = ReactiveCommand.Create(
2020-04-15 17:40:41 +00:00
execute: () => this.ModList?.OpenReadme(),
2019-10-13 20:14:11 +00:00
canExecute: this.WhenAny(x => x.ModList)
.Select(modList => !string.IsNullOrEmpty(modList?.Readme))
2019-10-12 18:42:47 +00:00
.ObserveOnGuiThread());
VisitModListWebsiteCommand = ReactiveCommand.Create(
execute: () =>
{
Utils.OpenWebsite(ModList.Website);
return Unit.Default;
},
canExecute: this.WhenAny(x => x.ModList.Website)
.Select(x => x != null)
.ObserveOnGuiThread());
_progressTitle = this.WhenAnyValue(
x => x.Installing,
x => x.StartedInstallation,
x => x.Completed,
selector: (installing, started, completed) =>
2019-11-07 05:33:08 +00:00
{
if (installing)
{
return "Installing";
}
else if (started)
{
if (completed == null) return "Installing";
return completed.Value.Succeeded ? "Installed" : "Failed";
}
else
{
2020-07-19 20:50:11 +00:00
return "Awaiting Input";
}
2019-11-07 05:33:08 +00:00
})
.ToGuiProperty(this, nameof(ProgressTitle));
2020-01-10 04:27:59 +00:00
UIUtils.BindCpuStatus(
this.WhenAny(x => x.Installer.ActiveInstallation)
.SelectMany(c => c?.QueueStatus ?? Observable.Empty<CPUStatus>()),
StatusList)
.DisposeWith(CompositeDisposable);
2019-12-02 05:36:47 +00:00
2019-12-19 01:14:21 +00:00
BeginCommand = ReactiveCommand.CreateFromTask(
canExecute: this.WhenAny(x => x.Installer.CanInstall)
2020-07-25 22:58:45 +00:00
.Select(err => err.Succeeded),
2019-12-19 01:14:21 +00:00
execute: async () =>
{
try
{
Utils.Log($"Starting to install {ModList.Name}");
var success = await this.Installer.Install();
Completed = ErrorResponse.Create(success);
try
{
2020-04-15 17:40:41 +00:00
this.ModList?.OpenReadme();
}
catch (Exception ex)
{
Utils.Error(ex);
}
2019-12-19 01:14:21 +00:00
}
catch (Exception ex)
{
Utils.Error(ex, $"Encountered error, can't continue");
2019-12-19 01:14:21 +00:00
while (ex.InnerException != null) ex = ex.InnerException;
2019-12-19 05:22:39 +00:00
Completed = ErrorResponse.Fail(ex);
2019-12-19 01:14:21 +00:00
}
});
2019-12-02 05:36:47 +00:00
// When sub installer begins an install, mark state variable
2019-12-19 01:14:21 +00:00
BeginCommand.StartingExecution()
2019-12-02 05:36:47 +00:00
.Subscribe(_ =>
{
StartedInstallation = true;
})
.DisposeWith(CompositeDisposable);
// Listen for user interventions, and compile a dynamic list of all unhandled ones
var activeInterventions = this.WhenAny(x => x.Installer.ActiveInstallation)
.WithLatestFrom(
this.WhenAny(x => x.Installer),
(activeInstall, installer) =>
{
if (activeInstall == null) return Observable.Empty<IChangeSet<IUserIntervention>>();
return activeInstall.LogMessages
.WhereCastable<IStatusMessage, IUserIntervention>()
.ToObservableChangeSet()
.AutoRefresh(i => i.Handled)
.Filter(i => !i.Handled)
.Transform(x => installer.InterventionConverter(x));
})
.Switch()
.AsObservableList();
// Find the top intervention /w no CPU ID to be marked as "global"
_ActiveGlobalUserIntervention = activeInterventions.Connect()
.Filter(x => x.CpuID == WorkQueue.UnassignedCpuId)
.QueryWhenChanged(query => query.FirstOrDefault())
.ToGuiProperty(this, nameof(ActiveGlobalUserIntervention));
2019-12-11 04:59:15 +00:00
CloseWhenCompleteCommand = ReactiveCommand.CreateFromTask(
2019-12-19 05:22:39 +00:00
canExecute: this.WhenAny(x => x.Completed)
.Select(x => x != null),
execute: async () =>
2019-12-11 04:59:15 +00:00
{
await MWVM.ShutdownApplication();
2019-12-11 04:59:15 +00:00
});
GoToInstallCommand = ReactiveCommand.Create(
canExecute: Observable.CombineLatest(
2019-12-19 05:22:39 +00:00
this.WhenAny(x => x.Completed)
.Select(x => x != null),
2019-12-11 04:59:15 +00:00
this.WhenAny(x => x.Installer.SupportsAfterInstallNavigation),
resultSelector: (complete, supports) => complete && supports),
execute: () =>
{
Installer.AfterInstallNavigation();
});
_CurrentCpuCount = this.WhenAny(x => x.Installer.ActiveInstallation.Queue.CurrentCpuCount)
.Switch()
.ToGuiProperty(this, nameof(CurrentCpuCount));
2019-10-09 09:22:03 +00:00
}
}
}