wabbajack/Wabbajack/View Models/Compilers/CompilerVM.cs

273 lines
11 KiB
C#
Raw Normal View History

using DynamicData;
using DynamicData.Binding;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
2019-12-14 03:47:09 +00:00
using System.Diagnostics;
using System.IO;
using System.Linq;
2019-12-14 03:47:09 +00:00
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
2020-12-07 04:26:02 +00:00
using System.Reactive.Subjects;
2019-12-19 01:14:21 +00:00
using System.Windows.Input;
using System.Windows.Media.Imaging;
using Wabbajack.Common;
2019-12-04 04:12:08 +00:00
using Wabbajack.Common.StatusFeed;
using Wabbajack.Lib;
2021-07-20 05:08:12 +00:00
using Wabbajack.Lib.AuthorApi;
using Wabbajack.Lib.FileUploader;
namespace Wabbajack
{
2020-12-07 04:26:02 +00:00
public class CompilerVM : BackNavigatingVM, IBackNavigatingVM, ICpuStatusVM
{
public MainWindowVM MWVM { get; }
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<BitmapImage> _image;
public BitmapImage Image => _image.Value;
[Reactive]
public ModManager SelectedCompilerType { get; set; }
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<ISubCompilerVM> _compiler;
public ISubCompilerVM Compiler => _compiler.Value;
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<ModlistSettingsEditorVM> _currentModlistSettings;
public ModlistSettingsEditorVM CurrentModlistSettings => _currentModlistSettings.Value;
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<bool> _compiling;
public bool Compiling => _compiling.Value;
2019-11-21 05:15:47 +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;
public ReactiveCommand<Unit, Unit> GoToCommand { get; }
2020-01-20 21:59:26 +00:00
public ReactiveCommand<Unit, Unit> CloseWhenCompleteCommand { get; }
public ReactiveCommand<Unit, Unit> BeginCommand { get; }
2019-11-24 23:42:28 +00:00
public FilePickerVM OutputLocation { get; }
private readonly ObservableAsPropertyHelper<IUserIntervention> _ActiveGlobalUserIntervention;
public IUserIntervention ActiveGlobalUserIntervention => _ActiveGlobalUserIntervention.Value;
[Reactive]
public bool StartedCompilation { get; set; }
2019-12-14 03:47:09 +00:00
[Reactive]
2019-12-19 05:22:39 +00:00
public ErrorResponse? Completed { get; set; }
2019-12-14 03:47:09 +00:00
private readonly ObservableAsPropertyHelper<string> _progressTitle;
public string ProgressTitle => _progressTitle.Value;
private readonly ObservableAsPropertyHelper<(int CurrentCPUs, int DesiredCPUs)> _CurrentCpuCount;
public (int CurrentCPUs, int DesiredCPUs) CurrentCpuCount => _CurrentCpuCount.Value;
2021-07-20 05:08:12 +00:00
2020-12-07 04:26:02 +00:00
public CompilerVM(MainWindowVM mainWindowVM) : base(mainWindowVM)
{
2019-11-21 15:04:33 +00:00
MWVM = mainWindowVM;
2021-07-20 05:08:12 +00:00
OutputLocation = new FilePickerVM()
{
ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty,
PathType = FilePickerVM.PathTypeOptions.Folder,
2020-01-13 21:11:07 +00:00
PromptTitle = "Select the folder to export the compiled Wabbajack ModList to",
};
// Load settings
2019-11-21 15:04:33 +00:00
CompilerSettings settings = MWVM.Settings.Compiler;
SelectedCompilerType = settings.LastCompiledModManager;
OutputLocation.TargetPath = settings.OutputLocation;
2019-11-21 15:04:33 +00:00
MWVM.Settings.SaveSignal
.Subscribe(_ =>
{
2019-11-21 15:04:33 +00:00
settings.LastCompiledModManager = SelectedCompilerType;
settings.OutputLocation = OutputLocation.TargetPath;
})
2019-11-21 15:04:33 +00:00
.DisposeWith(CompositeDisposable);
// Swap to proper sub VM based on selected type
2019-11-21 15:45:00 +00:00
_compiler = this.WhenAny(x => x.SelectedCompilerType)
2019-11-15 04:54:34 +00:00
// Delay so the initial VM swap comes in immediately, subVM comes right after
.DelayInitial(TimeSpan.FromMilliseconds(50), RxApp.MainThreadScheduler)
.Select<ModManager, ISubCompilerVM>(type =>
{
switch (type)
{
case ModManager.MO2:
return new MO2CompilerVM(this);
default:
return null;
}
})
// Unload old VM
.Pairwise()
.Do(pair =>
{
pair.Previous?.Unload();
})
.Select(p => p.Current)
.ToGuiProperty(this, nameof(Compiler));
// Let sub VM determine what settings we're displaying and when
2019-11-21 15:45:00 +00:00
_currentModlistSettings = this.WhenAny(x => x.Compiler.ModlistSettings)
.ToGuiProperty(this, nameof(CurrentModlistSettings));
2019-11-21 15:45:00 +00:00
_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)
.DistinctUntilChanged()
2020-01-09 03:22:49 +00:00
.ObserveOnGuiThread()
.Select(path =>
{
2020-03-28 20:04:22 +00:00
if (path == default) return UIUtils.BitmapImageFromResource("Resources/Wabba_Mouth_No_Text.png");
return UIUtils.TryGetBitmapImageFromFile(path, out var image) ? image : null;
})
.ToGuiProperty(this, nameof(Image));
2019-11-21 05:15:47 +00:00
2019-11-21 15:45:00 +00:00
_compiling = this.WhenAny(x => x.Compiler.ActiveCompilation)
2019-11-21 05:15:47 +00:00
.Select(compilation => compilation != null)
.ToGuiProperty(this, nameof(Compiling));
2019-11-24 23:42:28 +00:00
BackCommand = ReactiveCommand.Create(
2019-12-14 03:47:09 +00:00
execute: () =>
{
mainWindowVM.NavigateTo(mainWindowVM.ModeSelectionVM);
StartedCompilation = false;
2019-12-19 05:22:39 +00:00
Completed = null;
2019-12-14 03:47:09 +00:00
},
canExecute: Observable.CombineLatest(
this.WhenAny(x => x.Compiling)
.Select(x => !x),
this.ConstructCanNavigateBack(),
resultSelector: (i, b) => i && b)
.ObserveOnGuiThread());
2019-11-24 23:42:28 +00:00
2020-01-10 04:27:59 +00:00
UIUtils.BindCpuStatus(
this.WhenAny(x => x.Compiler.ActiveCompilation)
.SelectMany(c => c?.QueueStatus ?? Observable.Empty<CPUStatus>()),
StatusList)
2019-11-21 15:04:33 +00:00
.DisposeWith(CompositeDisposable);
_percentCompleted = this.WhenAny(x => x.Compiler.ActiveCompilation)
.StartWith(default(ACompiler))
2019-12-14 03:47:09 +00:00
.CombineLatest(
this.WhenAny(x => x.Completed),
(compiler, completed) =>
{
2019-12-14 03:47:09 +00:00
if (compiler == 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 compiler.PercentCompleted.StartWith(Percent.Zero);
2019-12-14 03:47:09 +00:00
})
.Switch()
.Debounce(TimeSpan.FromMilliseconds(25), RxApp.MainThreadScheduler)
.ToGuiProperty(this, nameof(PercentCompleted));
2019-12-19 01:14:21 +00:00
BeginCommand = ReactiveCommand.CreateFromTask(
canExecute: this.WhenAny(x => x.Compiler.CanCompile)
.Switch(),
execute: async () =>
{
try
{
2020-12-07 04:26:02 +00:00
IsBackEnabledSubject.OnNext(false);
var modList = await this.Compiler.Compile();
Completed = ErrorResponse.Create(modList.Succeeded);
2019-12-19 01:14:21 +00:00
}
catch (Exception ex)
{
2019-12-19 05:22:39 +00:00
Completed = ErrorResponse.Fail(ex);
2019-12-19 01:14:21 +00:00
while (ex.InnerException != null) ex = ex.InnerException;
Utils.Error(ex, $"Compiler error");
}
2020-12-07 04:26:02 +00:00
finally
{
IsBackEnabledSubject.OnNext(true);
}
2019-12-19 01:14:21 +00:00
});
// When sub compiler begins a compile, mark state variable
2019-12-19 01:14:21 +00:00
BeginCommand.StartingExecution()
2019-12-14 03:47:09 +00:00
.Subscribe(_ =>
{
StartedCompilation = true;
})
.DisposeWith(CompositeDisposable);
// Listen for user interventions, and compile a dynamic list of all unhandled ones
var activeInterventions = this.WhenAny(x => x.Compiler.ActiveCompilation)
.SelectMany(c => c?.LogMessages ?? Observable.Empty<IStatusMessage>())
.WhereCastable<IStatusMessage, IUserIntervention>()
.ToObservableChangeSet()
.AutoRefresh(i => i.Handled)
.Filter(i => !i.Handled)
.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-14 03:47:09 +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-14 03:47:09 +00:00
{
await MWVM.ShutdownApplication();
2019-12-14 03:47:09 +00:00
});
GoToCommand = ReactiveCommand.Create(
2019-12-19 05:22:39 +00:00
canExecute: this.WhenAny(x => x.Completed)
.Select(x => x != null),
2019-12-14 03:47:09 +00:00
execute: () =>
{
if (Completed?.Failed ?? false)
2019-12-14 03:47:09 +00:00
{
Process.Start("explorer.exe", $"/select,\"{Utils.LogFolder}\"");
2019-12-14 03:47:09 +00:00
}
else
{
Process.Start("explorer.exe",
2020-04-04 22:06:14 +00:00
OutputLocation.TargetPath == default
? $"/select,\"{Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)}\""
: $"/select,\"{OutputLocation.TargetPath}\"");
2019-12-14 03:47:09 +00:00
}
});
_progressTitle = this.WhenAnyValue(
x => x.Compiling,
x => x.StartedCompilation,
x => x.Completed,
selector: (compiling, started, completed) =>
{
if (compiling)
{
return "Compiling";
}
else if (started)
{
if (completed == null) return "Compiling";
return completed.Value.Succeeded ? "Compiled" : "Failed";
}
else
{
2020-07-19 20:50:11 +00:00
return "Awaiting Input";
}
})
.ToGuiProperty(this, nameof(ProgressTitle));
_CurrentCpuCount = this.WhenAny(x => x.Compiler.ActiveCompilation.Queue.CurrentCpuCount)
.Switch()
.ToGuiProperty(this, nameof(CurrentCpuCount));
}
}
}