2019-11-21 05:17:40 +00:00
|
|
|
|
using DynamicData;
|
|
|
|
|
using DynamicData.Binding;
|
2019-11-14 05:28:27 +00:00
|
|
|
|
using ReactiveUI;
|
|
|
|
|
using ReactiveUI.Fody.Helpers;
|
|
|
|
|
using System;
|
2019-12-15 19:20:27 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-12-14 03:47:09 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2019-11-14 05:28:27 +00:00
|
|
|
|
using System.Linq;
|
2019-12-14 03:47:09 +00:00
|
|
|
|
using System.Reactive;
|
2019-11-14 05:28:27 +00:00
|
|
|
|
using System.Reactive.Disposables;
|
|
|
|
|
using System.Reactive.Linq;
|
2019-12-19 01:14:21 +00:00
|
|
|
|
using System.Windows.Input;
|
2019-11-14 05:28:27 +00:00
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
using Wabbajack.Common;
|
2019-12-04 04:12:08 +00:00
|
|
|
|
using Wabbajack.Common.StatusFeed;
|
2019-11-14 05:28:27 +00:00
|
|
|
|
using Wabbajack.Lib;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack
|
|
|
|
|
{
|
2020-01-11 06:43:40 +00:00
|
|
|
|
public class CompilerVM : ViewModel, IBackNavigatingVM, ICpuStatusVM
|
2019-11-14 05:28:27 +00:00
|
|
|
|
{
|
|
|
|
|
public MainWindowVM MWVM { get; }
|
|
|
|
|
|
2019-11-21 15:45:00 +00:00
|
|
|
|
private readonly ObservableAsPropertyHelper<BitmapImage> _image;
|
|
|
|
|
public BitmapImage Image => _image.Value;
|
2019-11-14 05:28:27 +00:00
|
|
|
|
|
2020-01-05 03:06:34 +00:00
|
|
|
|
[Reactive]
|
|
|
|
|
public ViewModel NavigateBackTarget { get; set; }
|
|
|
|
|
|
2019-11-14 05:28:27 +00:00
|
|
|
|
[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-14 05:28:27 +00:00
|
|
|
|
|
2019-11-21 15:45:00 +00:00
|
|
|
|
private readonly ObservableAsPropertyHelper<ModlistSettingsEditorVM> _currentModlistSettings;
|
|
|
|
|
public ModlistSettingsEditorVM CurrentModlistSettings => _currentModlistSettings.Value;
|
2019-11-14 05:28:27 +00:00
|
|
|
|
|
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
|
|
|
|
|
2019-11-24 03:11:25 +00:00
|
|
|
|
private readonly ObservableAsPropertyHelper<float> _percentCompleted;
|
|
|
|
|
public float PercentCompleted => _percentCompleted.Value;
|
|
|
|
|
|
2019-12-15 19:20:27 +00:00
|
|
|
|
public ObservableCollectionExtended<CPUDisplayVM> StatusList { get; } = new ObservableCollectionExtended<CPUDisplayVM>();
|
2019-11-28 17:30:19 +00:00
|
|
|
|
|
2019-12-04 04:12:08 +00:00
|
|
|
|
public ObservableCollectionExtended<IStatusMessage> Log => MWVM.Log;
|
2019-11-24 03:17:22 +00:00
|
|
|
|
|
2020-01-07 05:44:05 +00:00
|
|
|
|
public ReactiveCommand<Unit, Unit> BackCommand { get; }
|
2019-12-14 03:47:09 +00:00
|
|
|
|
public IReactiveCommand GoToModlistCommand { get; }
|
|
|
|
|
public IReactiveCommand CloseWhenCompleteCommand { get; }
|
2019-12-19 01:14:21 +00:00
|
|
|
|
public IReactiveCommand BeginCommand { get; }
|
2019-11-24 23:42:28 +00:00
|
|
|
|
|
2019-12-14 03:44:27 +00:00
|
|
|
|
public FilePickerVM OutputLocation { get; }
|
|
|
|
|
|
2019-12-13 01:58:40 +00:00
|
|
|
|
private readonly ObservableAsPropertyHelper<IUserIntervention> _ActiveGlobalUserIntervention;
|
|
|
|
|
public IUserIntervention ActiveGlobalUserIntervention => _ActiveGlobalUserIntervention.Value;
|
|
|
|
|
|
2019-12-18 03:10:38 +00:00
|
|
|
|
[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
|
|
|
|
|
2019-12-27 21:50:27 +00:00
|
|
|
|
private readonly ObservableAsPropertyHelper<string> _progressTitle;
|
|
|
|
|
public string ProgressTitle => _progressTitle.Value;
|
|
|
|
|
|
2020-01-11 20:20:14 +00:00
|
|
|
|
private readonly ObservableAsPropertyHelper<(int CurrentCPUs, int DesiredCPUs)> _CurrentCpuCount;
|
|
|
|
|
public (int CurrentCPUs, int DesiredCPUs) CurrentCpuCount => _CurrentCpuCount.Value;
|
|
|
|
|
|
2019-11-14 05:28:27 +00:00
|
|
|
|
public CompilerVM(MainWindowVM mainWindowVM)
|
|
|
|
|
{
|
2019-11-21 15:04:33 +00:00
|
|
|
|
MWVM = mainWindowVM;
|
2019-11-14 05:28:27 +00:00
|
|
|
|
|
2019-12-14 03:44:27 +00:00
|
|
|
|
OutputLocation = new FilePickerVM()
|
|
|
|
|
{
|
2019-12-14 20:11:39 +00:00
|
|
|
|
ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty,
|
2019-12-14 03:44:27 +00:00
|
|
|
|
PathType = FilePickerVM.PathTypeOptions.Folder,
|
2020-01-13 21:11:07 +00:00
|
|
|
|
PromptTitle = "Select the folder to export the compiled Wabbajack ModList to",
|
2019-12-14 03:44:27 +00:00
|
|
|
|
};
|
|
|
|
|
|
2019-11-14 05:28:27 +00:00
|
|
|
|
// Load settings
|
2019-11-21 15:04:33 +00:00
|
|
|
|
CompilerSettings settings = MWVM.Settings.Compiler;
|
|
|
|
|
SelectedCompilerType = settings.LastCompiledModManager;
|
2019-12-14 03:44:27 +00:00
|
|
|
|
OutputLocation.TargetPath = settings.OutputLocation;
|
2019-11-21 15:04:33 +00:00
|
|
|
|
MWVM.Settings.SaveSignal
|
2019-11-14 05:28:27 +00:00
|
|
|
|
.Subscribe(_ =>
|
|
|
|
|
{
|
2019-11-21 15:04:33 +00:00
|
|
|
|
settings.LastCompiledModManager = SelectedCompilerType;
|
2019-12-14 03:44:27 +00:00
|
|
|
|
settings.OutputLocation = OutputLocation.TargetPath;
|
2019-11-14 05:28:27 +00:00
|
|
|
|
})
|
2019-11-21 15:04:33 +00:00
|
|
|
|
.DisposeWith(CompositeDisposable);
|
2019-11-14 05:28:27 +00:00
|
|
|
|
|
|
|
|
|
// 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)
|
2019-11-14 05:28:27 +00:00
|
|
|
|
.Select<ModManager, ISubCompilerVM>(type =>
|
|
|
|
|
{
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case ModManager.MO2:
|
|
|
|
|
return new MO2CompilerVM(this);
|
|
|
|
|
case ModManager.Vortex:
|
2019-11-16 23:10:17 +00:00
|
|
|
|
return new VortexCompilerVM(this);
|
2019-11-14 05:28:27 +00:00
|
|
|
|
default:
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
})
|
2019-11-16 23:09:13 +00:00
|
|
|
|
// Unload old VM
|
|
|
|
|
.Pairwise()
|
|
|
|
|
.Do(pair =>
|
|
|
|
|
{
|
|
|
|
|
pair.Previous?.Unload();
|
|
|
|
|
})
|
|
|
|
|
.Select(p => p.Current)
|
2019-11-21 15:04:33 +00:00
|
|
|
|
.ToProperty(this, nameof(Compiler));
|
2019-11-14 05:28:27 +00:00
|
|
|
|
|
2019-11-17 07:59:00 +00:00
|
|
|
|
// 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)
|
2019-11-21 15:04:33 +00:00
|
|
|
|
.ToProperty(this, nameof(CurrentModlistSettings));
|
2019-11-14 05:28:27 +00:00
|
|
|
|
|
2019-11-21 15:45:00 +00:00
|
|
|
|
_image = this.WhenAny(x => x.CurrentModlistSettings.ImagePath.TargetPath)
|
2019-11-15 05:31:46 +00:00
|
|
|
|
// Throttle so that it only loads image after any sets of swaps have completed
|
2019-12-25 01:31:43 +00:00
|
|
|
|
.Throttle(TimeSpan.FromMilliseconds(50), RxApp.TaskpoolScheduler)
|
2019-11-15 05:31:46 +00:00
|
|
|
|
.DistinctUntilChanged()
|
2020-01-09 03:22:49 +00:00
|
|
|
|
.ObserveOnGuiThread()
|
2019-11-14 05:28:27 +00:00
|
|
|
|
.Select(path =>
|
|
|
|
|
{
|
2019-11-29 23:56:56 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(path)) return UIUtils.BitmapImageFromResource("Resources/Wabba_Mouth_No_Text.png");
|
2019-11-14 05:28:27 +00:00
|
|
|
|
if (UIUtils.TryGetBitmapImageFromFile(path, out var image))
|
|
|
|
|
{
|
|
|
|
|
return image;
|
|
|
|
|
}
|
2019-11-15 04:59:59 +00:00
|
|
|
|
return null;
|
2019-11-14 05:28:27 +00:00
|
|
|
|
})
|
2019-11-21 15:04:33 +00:00
|
|
|
|
.ToProperty(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)
|
|
|
|
|
.ObserveOnGuiThread()
|
2019-11-21 15:04:33 +00:00
|
|
|
|
.ToProperty(this, nameof(Compiling));
|
2019-11-21 05:17:40 +00:00
|
|
|
|
|
2019-11-24 23:42:28 +00:00
|
|
|
|
BackCommand = ReactiveCommand.Create(
|
2019-12-14 03:47:09 +00:00
|
|
|
|
execute: () =>
|
|
|
|
|
{
|
2020-01-05 02:50:05 +00:00
|
|
|
|
mainWindowVM.NavigateTo(mainWindowVM.ModeSelectionVM);
|
2019-12-18 03:10:38 +00:00
|
|
|
|
StartedCompilation = false;
|
2019-12-19 05:22:39 +00:00
|
|
|
|
Completed = null;
|
2019-12-14 03:47:09 +00:00
|
|
|
|
},
|
2020-01-05 03:06:34 +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);
|
2019-11-24 03:11:25 +00:00
|
|
|
|
|
|
|
|
|
_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-11-24 03:11:25 +00:00
|
|
|
|
{
|
2019-12-14 03:47:09 +00:00
|
|
|
|
if (compiler == null)
|
|
|
|
|
{
|
2019-12-19 05:22:39 +00:00
|
|
|
|
return Observable.Return<float>(completed != null ? 1f : 0f);
|
2019-12-14 03:47:09 +00:00
|
|
|
|
}
|
2019-12-18 03:10:38 +00:00
|
|
|
|
return compiler.PercentCompleted.StartWith(0);
|
2019-12-14 03:47:09 +00:00
|
|
|
|
})
|
2019-11-24 03:11:25 +00:00
|
|
|
|
.Switch()
|
|
|
|
|
.Debounce(TimeSpan.FromMilliseconds(25))
|
|
|
|
|
.ToProperty(this, nameof(PercentCompleted));
|
2019-12-13 01:58:40 +00:00
|
|
|
|
|
2019-12-19 01:14:21 +00:00
|
|
|
|
BeginCommand = ReactiveCommand.CreateFromTask(
|
|
|
|
|
canExecute: this.WhenAny(x => x.Compiler.CanCompile)
|
|
|
|
|
.Switch(),
|
|
|
|
|
execute: async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await this.Compiler.Compile();
|
2019-12-19 05:22:39 +00:00
|
|
|
|
Completed = ErrorResponse.Success;
|
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");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-12-18 03:10:38 +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(_ =>
|
|
|
|
|
{
|
2019-12-18 03:10:38 +00:00
|
|
|
|
StartedCompilation = true;
|
|
|
|
|
})
|
|
|
|
|
.DisposeWith(CompositeDisposable);
|
|
|
|
|
|
2019-12-13 01:58:40 +00:00
|
|
|
|
// 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())
|
|
|
|
|
.ObserveOnGuiThread()
|
|
|
|
|
.ToProperty(this, nameof(ActiveGlobalUserIntervention));
|
2019-12-14 03:47:09 +00:00
|
|
|
|
|
|
|
|
|
CloseWhenCompleteCommand = 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: () =>
|
|
|
|
|
{
|
|
|
|
|
MWVM.ShutdownApplication();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
GoToModlistCommand = 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 (string.IsNullOrWhiteSpace(OutputLocation.TargetPath))
|
|
|
|
|
{
|
|
|
|
|
Process.Start("explorer.exe", Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Process.Start("explorer.exe", OutputLocation.TargetPath);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-12-27 21:50:27 +00:00
|
|
|
|
|
2020-01-15 05:24:59 +00:00
|
|
|
|
_progressTitle = this.WhenAnyValue(
|
|
|
|
|
x => x.Compiling,
|
|
|
|
|
x => x.StartedCompilation,
|
|
|
|
|
x => x.Completed,
|
|
|
|
|
selector: (compiling, started, completed) =>
|
2019-12-27 21:50:27 +00:00
|
|
|
|
{
|
|
|
|
|
if (compiling)
|
|
|
|
|
{
|
|
|
|
|
return "Compiling";
|
|
|
|
|
}
|
2020-01-15 05:24:59 +00:00
|
|
|
|
else if (started)
|
|
|
|
|
{
|
|
|
|
|
if (completed == null) return "Compiling";
|
|
|
|
|
return completed.Value.Succeeded ? "Compiled" : "Failed";
|
|
|
|
|
}
|
2019-12-27 21:50:27 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2020-01-15 05:24:59 +00:00
|
|
|
|
return "Configuring";
|
2019-12-27 21:50:27 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.ToProperty(this, nameof(ProgressTitle));
|
2020-01-11 20:20:14 +00:00
|
|
|
|
|
|
|
|
|
_CurrentCpuCount = this.WhenAny(x => x.Compiler.ActiveCompilation.Queue.CurrentCpuCount)
|
|
|
|
|
.Switch()
|
|
|
|
|
.ObserveOnGuiThread()
|
|
|
|
|
.ToProperty(this, nameof(CurrentCpuCount));
|
2019-11-14 05:28:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|