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

481 lines
20 KiB
C#
Raw Normal View History

2021-12-26 21:56:44 +00:00
using System;
using ReactiveUI;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Windows.Media.Imaging;
using ReactiveUI.Fody.Helpers;
using System.Windows.Media;
using DynamicData;
using DynamicData.Binding;
using System.Reactive;
2021-12-28 00:53:14 +00:00
using Microsoft.Extensions.DependencyInjection;
2021-12-28 00:24:53 +00:00
using Microsoft.Extensions.Logging;
2021-12-26 21:56:44 +00:00
using Microsoft.WindowsAPICodePack.Dialogs;
using Microsoft.WindowsAPICodePack.Shell;
2021-12-28 00:53:14 +00:00
using Wabbajack.DTOs.JsonConverters;
2021-12-30 00:15:37 +00:00
using Wabbajack.Extensions;
using Wabbajack.Interventions;
using Wabbajack.Messages;
using Wabbajack.RateLimiter;
2021-12-27 05:13:28 +00:00
using Wabbajack.View_Models;
2021-12-28 06:11:39 +00:00
using Wabbajack.Paths.IO;
2021-12-30 00:15:37 +00:00
using Consts = Wabbajack.Consts;
2021-12-28 06:11:39 +00:00
using KnownFolders = Wabbajack.Paths.IO.KnownFolders;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
namespace Wabbajack;
2021-12-28 00:54:56 +00:00
public enum ModManager
2021-12-26 21:56:44 +00:00
{
2021-12-28 00:24:53 +00:00
Standard
}
public class InstallerVM : BackNavigatingVM, IBackNavigatingVM, ICpuStatusVM
{
public SlideShow Slideshow { get; }
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
public MainWindowVM MWVM { get; }
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
private readonly ObservableAsPropertyHelper<ModListVM> _modList;
public ModListVM ModList => _modList.Value;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
public FilePickerVM ModListLocation { get; }
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
private readonly ObservableAsPropertyHelper<ISubInstallerVM> _installer;
public ISubInstallerVM Installer => _installer.Value;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
private readonly ObservableAsPropertyHelper<bool> _installing;
public bool Installing => _installing.Value;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
[Reactive]
public bool StartedInstallation { get; set; }
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
[Reactive]
public ErrorResponse? Completed { get; set; }
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
private readonly ObservableAsPropertyHelper<ImageSource> _image;
public ImageSource Image => _image.Value;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
private readonly ObservableAsPropertyHelper<string> _titleText;
public string TitleText => _titleText.Value;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
private readonly ObservableAsPropertyHelper<string> _authorText;
public string AuthorText => _authorText.Value;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
private readonly ObservableAsPropertyHelper<string> _description;
public string Description => _description.Value;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
private readonly ObservableAsPropertyHelper<string> _progressTitle;
public string ProgressTitle => _progressTitle.Value;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
private readonly ObservableAsPropertyHelper<string> _modListName;
public string ModListName => _modListName.Value;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
private readonly ObservableAsPropertyHelper<Percent> _percentCompleted;
public Percent PercentCompleted => _percentCompleted.Value;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
public ObservableCollectionExtended<CPUDisplayVM> StatusList { get; } = new ObservableCollectionExtended<CPUDisplayVM>();
public ObservableCollectionExtended<IStatusMessage> Log => MWVM.Log;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
private readonly ObservableAsPropertyHelper<ModManager?> _TargetManager;
public ModManager? TargetManager => _TargetManager.Value;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
private readonly ObservableAsPropertyHelper<IUserIntervention> _ActiveGlobalUserIntervention;
public IUserIntervention ActiveGlobalUserIntervention => _ActiveGlobalUserIntervention.Value;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
private readonly ObservableAsPropertyHelper<(int CurrentCPUs, int DesiredCPUs)> _CurrentCpuCount;
public (int CurrentCPUs, int DesiredCPUs) CurrentCpuCount => _CurrentCpuCount.Value;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
private readonly ObservableAsPropertyHelper<bool> _LoadingModlist;
private readonly ILogger<InstallerVM> _logger;
public bool LoadingModlist => _LoadingModlist.Value;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
// Command properties
public ReactiveCommand<Unit, Unit> ShowManifestCommand { get; }
public ReactiveCommand<Unit, Unit> OpenReadmeCommand { get; }
public ReactiveCommand<Unit, Unit> VisitModListWebsiteCommand { get; }
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
public ReactiveCommand<Unit, Unit> CloseWhenCompleteCommand { get; }
public ReactiveCommand<Unit, Unit> OpenLogsCommand { get; }
public ReactiveCommand<Unit, Unit> GoToInstallCommand { get; }
public ReactiveCommand<Unit, Unit> BeginCommand { get; }
2021-12-26 21:56:44 +00:00
public InstallerVM(ILogger<InstallerVM> logger, MainWindowVM mainWindowVM, IServiceProvider serviceProvider) : base(logger)
2021-12-28 00:24:53 +00:00
{
_logger = logger;
2021-12-28 06:11:39 +00:00
/* TODO
2021-12-28 00:24:53 +00:00
var downloadsPath = KnownFolders.Downloads.Path;
var skyDrivePath = KnownFolders.SkyDrive.Path;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
if (downloadsPath != null && AbsolutePath.EntryPoint.IsChildOf(new AbsolutePath(downloadsPath)))
{
logger.LogError(Error(new CriticalFailureIntervention(
"Wabbajack is running inside your Downloads folder. This folder is often highly monitored by antivirus software and these can often " +
"conflict with the operations Wabbajack needs to perform. Please move Wabbajack outside of your Downloads folder and then restart the app.",
"Cannot run inside Downloads", true));
}
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
if (skyDrivePath != null && AbsolutePath.EntryPoint.IsChildOf(new AbsolutePath(skyDrivePath)))
{
Utils.Error(new CriticalFailureIntervention(
$"Wabbajack is running inside a OneDrive folder \"{skyDrivePath}\". This folder is known to cause issues with Wabbajack. " +
"Please move Wabbajack outside of your OneDrive folder and then restart the app.",
"Cannot run inside OneDrive", true));
}*/
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
MWVM = mainWindowVM;
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
ModListLocation = new FilePickerVM
{
ExistCheckOption = FilePickerVM.CheckOptions.On,
PathType = FilePickerVM.PathTypeOptions.File,
PromptTitle = "Select a ModList to install"
};
ModListLocation.Filters.Add(new CommonFileDialogFilter("Wabbajack Modlist", "*.wabbajack"));
// 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 =>
2021-12-26 21:56:44 +00:00
{
2021-12-28 00:24:53 +00:00
switch (type)
2021-12-26 21:56:44 +00:00
{
2021-12-28 00:24:53 +00:00
case ModManager.Standard:
return new MO2InstallerVM(this);
default:
return null;
}
})
// Unload old VM
.Pairwise()
.Do(pair =>
{
pair.Previous?.Unload();
})
.Select(p => p.Current)
.ToGuiProperty(this, nameof(Installer));
// Load settings
MWVM.Settings.SaveSignal
.Subscribe(_ =>
{
MWVM.Settings.Installer.LastInstalledListLocation = ModListLocation.TargetPath;
})
.DisposeWith(CompositeDisposable);
2021-12-28 00:24:53 +00:00
// 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 =>
{
if (!x.active) return default;
return x.path;
})
// Throttle slightly so changes happen more atomically
.Throttle(TimeSpan.FromMilliseconds(50), RxApp.MainThreadScheduler)
.Replay(1)
.RefCount();
2021-12-28 00:53:14 +00:00
2021-12-28 00:24:53 +00:00
_modList = activePath
.ObserveOn(RxApp.TaskpoolScheduler)
// Convert from active path to modlist VM
.Select(modListPath =>
{
if (modListPath == default) return default;
if (!modListPath.FileExists()) return default;
2021-12-28 23:18:55 +00:00
return new ModListVM(serviceProvider.GetRequiredService<ILogger<ModListVM>>()!, modListPath, serviceProvider.GetRequiredService<DTOSerializer>());
2021-12-28 00:24:53 +00:00
})
.DisposeOld()
.ObserveOnGuiThread()
.StartWith(default(ModListVM))
.ToGuiProperty(this, nameof(ModList));
// 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)
.Subscribe(x =>
{
GC.Collect();
});
_LoadingModlist = Observable.Merge(
// When active path changes, mark as loading
activePath
.Select(_ => true),
// When the resulting modlist comes in, mark it as done
this.WhenAny(x => x.ModList)
.Select(_ => false))
.ToGuiProperty(this, nameof(LoadingModlist));
_installing = this.WhenAny(x => x.Installer.ActiveInstallation)
.Select(i => i != null)
.ToGuiProperty(this, nameof(Installing));
2021-12-28 00:53:14 +00:00
/*TODO
2021-12-28 00:24:53 +00:00
_TargetManager = this.WhenAny(x => x.ModList)
.Select(modList => ModManager.Standard)
.ToGuiProperty(this, nameof(TargetManager));
2021-12-28 00:53:14 +00:00
*/
2021-12-28 00:24:53 +00:00
// Add additional error check on ModList
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.CurrentMinimumWabbajackVersion)
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;
});
BackCommand = ReactiveCommand.Create(
execute: () =>
{
StartedInstallation = false;
Completed = null;
NavigateToGlobal.Send(NavigateToGlobal.ScreenType.ModeSelectionView);
2021-12-28 00:24:53 +00:00
},
canExecute: Observable.CombineLatest(
this.WhenAny(x => x.Installing)
.Select(x => !x),
this.ConstructCanNavigateBack(),
resultSelector: (i, b) => i && b)
.ObserveOnGuiThread());
2021-12-26 21:56:44 +00:00
2021-12-28 00:53:14 +00:00
/* TODO
2021-12-28 00:24:53 +00:00
_percentCompleted = this.WhenAny(x => x.Installer.ActiveInstallation)
.StartWith(default(IInstaller))
.CombineLatest(
this.WhenAny(x => x.Completed),
(installer, completed) =>
2021-12-26 21:56:44 +00:00
{
2021-12-28 00:24:53 +00:00
if (installer == null)
{
return Observable.Return<Percent>(completed != null ? Percent.One : Percent.Zero);
}
return installer.PercentCompleted.StartWith(Percent.Zero);
2021-12-26 21:56:44 +00:00
})
2021-12-28 00:24:53 +00:00
.Switch()
.Debounce(TimeSpan.FromMilliseconds(25), RxApp.MainThreadScheduler)
.ToGuiProperty(this, nameof(PercentCompleted));
2021-12-28 00:53:14 +00:00
*/
2021-12-28 00:24:53 +00:00
2021-12-28 00:53:14 +00:00
Slideshow = new SlideShow( this, serviceProvider);
2021-12-28 00:24:53 +00:00
// Set display items to ModList if configuring or complete,
// or to the current slideshow data if installing
_image = Observable.CombineLatest(
this.WhenAny(x => x.ModList.Error),
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) =>
2021-12-26 21:56:44 +00:00
{
2021-12-28 00:24:53 +00:00
if (err != null)
{
return ResourceLinks.WabbajackErrLogo.Value;
}
if (loading) return default;
return installing ? slideshow : modList;
2021-12-26 21:56:44 +00:00
})
2021-12-28 00:24:53 +00:00
.Select<BitmapImage, ImageSource>(x => x)
.ToGuiProperty(this, nameof(Image));
_titleText = Observable.CombineLatest(
this.WhenAny(x => x.ModList)
.Select(modList => modList?.Name ?? string.Empty),
this.WhenAny(x => x.Slideshow.TargetMod.State.Name)
.StartWith(default(string)),
this.WhenAny(x => x.Installing),
resultSelector: (modList, mod, installing) => installing ? mod : modList)
.ToGuiProperty(this, nameof(TitleText));
_authorText = Observable.CombineLatest(
this.WhenAny(x => x.ModList)
.Select(modList => modList?.Author ?? string.Empty),
this.WhenAny(x => x.Slideshow.TargetMod.State.Author)
.StartWith(default(string)),
this.WhenAny(x => x.Installing),
resultSelector: (modList, mod, installing) => installing ? mod : modList)
.ToGuiProperty(this, nameof(AuthorText));
_description = Observable.CombineLatest(
this.WhenAny(x => x.ModList)
.Select(modList => modList?.Description ?? string.Empty),
this.WhenAny(x => x.Slideshow.TargetMod.State.Description)
.StartWith(default(string)),
this.WhenAny(x => x.Installing),
resultSelector: (modList, mod, installing) => installing ? mod : modList)
.ToGuiProperty(this, nameof(Description));
2021-12-28 00:53:14 +00:00
/* TODO
2021-12-28 00:24:53 +00:00
_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) =>
2021-12-26 21:56:44 +00:00
{
2021-12-28 00:24:53 +00:00
if (err) return "Corrupted Modlist";
return name;
2021-12-26 21:56:44 +00:00
})
2021-12-28 00:24:53 +00:00
.Merge(this.WhenAny(x => x.Installer.ActiveInstallation)
.Where(c => c != null)
.SelectMany(c => c.TextStatus))
.ToGuiProperty(this, nameof(ModListName));
2021-12-28 00:53:14 +00:00
*/
2021-12-28 00:24:53 +00:00
ShowManifestCommand = ReactiveCommand.Create(() =>
{
UIUtils.OpenWebsite(new Uri("https://www.wabbajack.org/#/modlists/manifest"));
}, this.WhenAny(x => x.ModList)
.Select(x => x?.SourceModList != null)
.ObserveOnGuiThread());
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
OpenReadmeCommand = ReactiveCommand.Create(
execute: () => this.ModList?.OpenReadme(),
canExecute: this.WhenAny(x => x.ModList)
.Select(modList => !string.IsNullOrEmpty(modList?.Readme))
.ObserveOnGuiThread());
2021-12-26 21:56:44 +00:00
2021-12-28 00:53:14 +00:00
/*TODO
2021-12-28 00:24:53 +00:00
OpenLogsCommand = ReactiveCommand.Create(
execute: () => UIUtils.OpenFolder(Consts.LogsFolder));
2021-12-28 00:53:14 +00:00
*/
2021-12-28 00:24:53 +00:00
VisitModListWebsiteCommand = ReactiveCommand.Create(
execute: () =>
{
UIUtils.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) =>
2021-12-26 21:56:44 +00:00
{
2021-12-28 00:24:53 +00:00
if (installing)
{
return "Installing";
}
else if (started)
{
if (completed == null) return "Installing";
return completed.Value.Succeeded ? "Installed" : "Failed";
}
else
2021-12-26 21:56:44 +00:00
{
2021-12-28 00:24:53 +00:00
return "Awaiting Input";
}
})
.ToGuiProperty(this, nameof(ProgressTitle));
2021-12-28 00:53:14 +00:00
/*
2021-12-28 00:24:53 +00:00
UIUtils.BindCpuStatus(
2021-12-26 21:56:44 +00:00
this.WhenAny(x => x.Installer.ActiveInstallation)
.SelectMany(c => c?.QueueStatus ?? Observable.Empty<CPUStatus>()),
StatusList)
2021-12-28 00:24:53 +00:00
.DisposeWith(CompositeDisposable);
2021-12-28 00:53:14 +00:00
*/
2021-12-28 00:24:53 +00:00
BeginCommand = ReactiveCommand.CreateFromTask(
canExecute: this.WhenAny(x => x.Installer.CanInstall)
.Select(err => err.Succeeded),
execute: async () =>
{
try
2021-12-26 21:56:44 +00:00
{
2021-12-28 00:53:14 +00:00
_logger.LogInformation("Starting to install {Name}", ModList.Name);
2021-12-28 00:24:53 +00:00
IsBackEnabledSubject.OnNext(false);
var success = await this.Installer.Install();
Completed = ErrorResponse.Create(success);
2021-12-26 21:56:44 +00:00
try
{
2021-12-28 00:24:53 +00:00
this.ModList?.OpenReadme();
2021-12-26 21:56:44 +00:00
}
catch (Exception ex)
{
2021-12-28 00:53:14 +00:00
_logger.LogError(ex, "During installation");
2021-12-26 21:56:44 +00:00
}
2021-12-28 00:24:53 +00:00
}
catch (Exception ex)
{
2021-12-28 00:53:14 +00:00
_logger.LogError(ex, $"Encountered error, can't continue");
2021-12-28 00:24:53 +00:00
while (ex.InnerException != null) ex = ex.InnerException;
Completed = ErrorResponse.Fail(ex);
}
finally
{
IsBackEnabledSubject.OnNext(true);
}
});
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
// When sub installer begins an install, mark state variable
BeginCommand.StartingExecution()
.Subscribe(_ =>
{
StartedInstallation = true;
})
.DisposeWith(CompositeDisposable);
2021-12-28 00:53:14 +00:00
/*
2021-12-28 00:24:53 +00:00
// 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) =>
2021-12-26 21:56:44 +00:00
{
2021-12-28 00:24:53 +00:00
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));
2021-12-26 21:56:44 +00:00
})
2021-12-28 00:24:53 +00:00
.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));
2021-12-28 00:53:14 +00:00
*/
2021-12-28 00:24:53 +00:00
CloseWhenCompleteCommand = ReactiveCommand.CreateFromTask(
canExecute: this.WhenAny(x => x.Completed)
.Select(x => x != null),
execute: async () =>
{
await MWVM.ShutdownApplication();
});
2021-12-26 21:56:44 +00:00
2021-12-28 00:24:53 +00:00
GoToInstallCommand = ReactiveCommand.Create(
canExecute: Observable.CombineLatest(
this.WhenAny(x => x.Completed)
2021-12-26 21:56:44 +00:00
.Select(x => x != null),
2021-12-28 00:24:53 +00:00
this.WhenAny(x => x.Installer.SupportsAfterInstallNavigation),
resultSelector: (complete, supports) => complete && supports),
execute: () =>
{
Installer.AfterInstallNavigation();
});
2021-12-26 21:56:44 +00:00
2021-12-28 00:53:14 +00:00
/*
2021-12-28 00:24:53 +00:00
_CurrentCpuCount = this.WhenAny(x => x.Installer.ActiveInstallation.Queue.CurrentCpuCount)
.Switch()
.ToGuiProperty(this, nameof(CurrentCpuCount));
2021-12-28 00:53:14 +00:00
*/
2021-12-26 21:56:44 +00:00
}
2021-12-28 00:24:53 +00:00
}