Some ISubInstallerVM work

This commit is contained in:
Justin Swanson 2019-12-01 23:36:47 -06:00
parent 5bb291a310
commit fb2cb28cf4
7 changed files with 153 additions and 79 deletions

View File

@ -28,9 +28,6 @@ namespace Wabbajack
private readonly ObservableAsPropertyHelper<ModlistSettingsEditorVM> _currentModlistSettings;
public ModlistSettingsEditorVM CurrentModlistSettings => _currentModlistSettings.Value;
private readonly ObservableAsPropertyHelper<StatusUpdateTracker> _currentStatusTracker;
public StatusUpdateTracker CurrentStatusTracker => _currentStatusTracker.Value;
private readonly ObservableAsPropertyHelper<bool> _compiling;
public bool Compiling => _compiling.Value;
@ -86,10 +83,6 @@ namespace Wabbajack
_currentModlistSettings = this.WhenAny(x => x.Compiler.ModlistSettings)
.ToProperty(this, nameof(CurrentModlistSettings));
// Let sub VM determine what progress we're seeing
_currentStatusTracker = this.WhenAny(x => x.Compiler.StatusTracker)
.ToProperty(this, nameof(CurrentStatusTracker));
_image = this.WhenAny(x => x.CurrentModlistSettings.ImagePath.TargetPath)
// Throttle so that it only loads image after any sets of swaps have completed
.Throttle(TimeSpan.FromMilliseconds(50), RxApp.MainThreadScheduler)

View File

@ -10,7 +10,6 @@ namespace Wabbajack
ACompiler ActiveCompilation { get; }
ModlistSettingsEditorVM ModlistSettings { get; }
StatusUpdateTracker StatusTracker { get;}
void Unload();
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ReactiveUI;
using Wabbajack.Common;
using Wabbajack.Lib;
namespace Wabbajack
{
public interface ISubInstallerVM
{
IReactiveCommand BeginCommand { get; }
AInstaller ActiveInstallation { get; }
void Unload();
}
}

View File

@ -17,6 +17,7 @@ using ReactiveUI.Fody.Helpers;
using System.Windows.Media;
using DynamicData;
using DynamicData.Binding;
using System.Reactive;
namespace Wabbajack
{
@ -33,15 +34,12 @@ namespace Wabbajack
public FilePickerVM ModListPath { get; }
[Reactive]
public bool UIReady { get; set; }
private readonly ObservableAsPropertyHelper<ISubInstallerVM> _installer;
public ISubInstallerVM Installer => _installer.Value;
private readonly ObservableAsPropertyHelper<string> _htmlReport;
public string HTMLReport => _htmlReport.Value;
[Reactive]
public AInstaller ActiveInstallation { get; private set; }
private readonly ObservableAsPropertyHelper<bool> _installing;
public bool Installing => _installing.Value;
@ -82,8 +80,10 @@ namespace Wabbajack
private readonly ObservableAsPropertyHelper<ModlistInstallationSettings> _CurrentSettings;
public ModlistInstallationSettings CurrentSettings => _CurrentSettings.Value;
private readonly ObservableAsPropertyHelper<ModManager?> _TargetManager;
public ModManager? TargetManager => _TargetManager.Value;
// Command properties
public IReactiveCommand BeginCommand { get; }
public IReactiveCommand ShowReportCommand { get; }
public IReactiveCommand OpenReadmeCommand { get; }
public IReactiveCommand VisitWebsiteCommand { get; }
@ -127,6 +127,31 @@ namespace Wabbajack
PromptTitle = "Select a modlist to install"
};
// 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);
case ModManager.Vortex:
throw new NotImplementedException();
default:
return null;
}
})
// Unload old VM
.Pairwise()
.Do(pair =>
{
pair.Previous?.Unload();
})
.Select(p => p.Current)
.ToProperty(this, nameof(Installer));
// Load settings
_CurrentSettings = this.WhenAny(x => x.ModListPath.TargetPath)
.Select(path => path == null ? null : MWVM.Settings.Installer.ModlistSettings.TryCreate(path))
@ -161,17 +186,20 @@ namespace Wabbajack
_htmlReport = this.WhenAny(x => x.ModList)
.Select(modList => modList?.ReportHTML)
.ToProperty(this, nameof(HTMLReport));
_installing = this.WhenAny(x => x.ActiveInstallation)
_installing = this.WhenAny(x => x.Installer.ActiveInstallation)
.Select(compilation => compilation != null)
.ObserveOnGuiThread()
.ToProperty(this, nameof(Installing));
_TargetManager = this.WhenAny(x => x.ModList)
.Select(modList => modList?.ModManager)
.ToProperty(this, nameof(TargetManager));
BackCommand = ReactiveCommand.Create(
execute: () => mainWindowVM.ActivePane = mainWindowVM.ModeSelectionVM,
canExecute: this.WhenAny(x => x.Installing)
.Select(x => !x));
_percentCompleted = this.WhenAny(x => x.ActiveInstallation)
_percentCompleted = this.WhenAny(x => x.Installer.ActiveInstallation)
.StartWith(default(AInstaller))
.Pairwise()
.Select(c =>
@ -233,18 +261,6 @@ namespace Wabbajack
canExecute: this.WhenAny(x => x.ModList)
.Select(modList => !string.IsNullOrEmpty(modList?.Readme))
.ObserveOnGuiThread());
BeginCommand = ReactiveCommand.CreateFromTask(
execute: ExecuteBegin,
canExecute: Observable.CombineLatest(
this.WhenAny(x => x.Installing),
this.WhenAny(x => x.Location.InError),
this.WhenAny(x => x.DownloadLocation.InError),
resultSelector: (installing, loc, download) =>
{
if (installing) return false;
return !loc && !download;
})
.ObserveOnGuiThread());
VisitWebsiteCommand = ReactiveCommand.Create(
execute: () => Process.Start(ModList.Website),
canExecute: this.WhenAny(x => x.ModList.Website)
@ -274,7 +290,7 @@ namespace Wabbajack
.ToProperty(this, nameof(ProgressTitle));
// Compile progress updates and populate ObservableCollection
this.WhenAny(x => x.ActiveInstallation)
this.WhenAny(x => x.Installer.ActiveInstallation)
.SelectMany(c => c?.QueueStatus ?? Observable.Empty<CPUStatus>())
.ObserveOn(RxApp.TaskpoolScheduler)
.ToObservableChangeSet(x => x.ID)
@ -286,6 +302,16 @@ namespace Wabbajack
.Bind(StatusList)
.Subscribe()
.DisposeWith(CompositeDisposable);
// When sub installer begins an install, mark state variable
this.WhenAny(x => x.Installer.BeginCommand)
.Select(x => x?.StartingExecution() ?? Observable.Empty<Unit>())
.Switch()
.Subscribe(_ =>
{
InstallingMode = true;
})
.DisposeWith(CompositeDisposable);
}
private void ShowReport()
@ -321,54 +347,6 @@ namespace Wabbajack
}
}
private async Task ExecuteBegin()
{
InstallingMode = true;
AInstaller installer;
try
{
installer = new MO2Installer(
archive: ModListPath.TargetPath,
modList: ModList.SourceModList,
outputFolder: Location.TargetPath,
downloadFolder: DownloadLocation.TargetPath);
}
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;
}
});
}
private void SaveSettings(ModlistInstallationSettings settings)
{
MWVM.Settings.Installer.LastInstalledListLocation = ModListPath.TargetPath;

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
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 IReactiveCommand BeginCommand { get; }
[Reactive]
public AInstaller ActiveInstallation { get; private set; }
public MO2InstallerVM(InstallerVM installerVM)
{
BeginCommand = ReactiveCommand.CreateFromTask(
canExecute: Observable.CombineLatest(
installerVM.WhenAny(x => x.Location.InError),
installerVM.WhenAny(x => x.DownloadLocation.InError),
resultSelector: (loc, download) =>
{
return !loc && !download;
})
.ObserveOnGuiThread(),
execute: async () =>
{
AInstaller installer;
try
{
installer = new MO2Installer(
archive: installerVM.ModListPath.TargetPath,
modList: installerVM.ModList.SourceModList,
outputFolder: installerVM.Location.TargetPath,
downloadFolder: installerVM.DownloadLocation.TargetPath);
}
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;
}
});
});
}
public void Unload()
{
}
}
}

View File

@ -347,7 +347,7 @@
Grid.Column="4"
Margin="0,0,25,0"
HorizontalAlignment="Right"
Command="{Binding BeginCommand, Mode=OneWay}" />
Command="{Binding Installer.BeginCommand, Mode=OneWay}" />
</Grid>
</ScrollViewer>
</Grid>

View File

@ -169,6 +169,8 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Converters\EqualsToBoolConverter.cs" />
<Compile Include="View Models\Installers\ISubInstallerVM.cs" />
<Compile Include="View Models\Installers\MO2InstallerVM.cs" />
<Compile Include="View Models\ModListGalleryVM.cs" />
<Compile Include="View Models\ModListMetadataVM.cs" />
<Compile Include="Views\Common\HeatedBackgroundView.xaml.cs">
@ -301,7 +303,7 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="View Models\InstallerVM.cs" />
<Compile Include="View Models\Installers\InstallerVM.cs" />
<Compile Include="Util\AutoScrollBehavior.cs" />
<Compile Include="Views\MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>