2020-02-11 23:02:12 +00:00
|
|
|
|
using DynamicData;
|
2019-10-22 03:03:01 +00:00
|
|
|
|
using DynamicData.Binding;
|
|
|
|
|
using ReactiveUI;
|
2019-11-02 23:23:11 +00:00
|
|
|
|
using ReactiveUI.Fody.Helpers;
|
2019-10-22 02:12:55 +00:00
|
|
|
|
using System;
|
2019-12-20 20:51:10 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-12-14 23:31:53 +00:00
|
|
|
|
using System.Diagnostics;
|
2019-10-22 02:12:55 +00:00
|
|
|
|
using System.Linq;
|
2020-02-02 07:33:12 +00:00
|
|
|
|
using System.Reactive;
|
2019-10-22 03:03:01 +00:00
|
|
|
|
using System.Reactive.Disposables;
|
|
|
|
|
using System.Reactive.Linq;
|
2020-02-11 05:04:56 +00:00
|
|
|
|
using System.Reflection;
|
2019-12-07 05:40:57 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2019-12-04 04:12:08 +00:00
|
|
|
|
using System.Windows;
|
2019-12-14 23:31:53 +00:00
|
|
|
|
using System.Windows.Input;
|
2019-12-07 05:40:57 +00:00
|
|
|
|
using System.Windows.Threading;
|
2020-02-11 05:04:56 +00:00
|
|
|
|
using Alphaleonis.Win32.Filesystem;
|
2019-10-22 03:03:01 +00:00
|
|
|
|
using Wabbajack.Common;
|
2019-12-04 04:12:08 +00:00
|
|
|
|
using Wabbajack.Common.StatusFeed;
|
2019-10-22 02:12:55 +00:00
|
|
|
|
using Wabbajack.Lib;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack
|
|
|
|
|
{
|
2019-10-23 04:15:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Main View Model for the application.
|
|
|
|
|
/// Keeps track of which sub view is being shown in the window, and has some singleton wiring like WorkQueue and Logging.
|
|
|
|
|
/// </summary>
|
2019-10-22 02:12:55 +00:00
|
|
|
|
public class MainWindowVM : ViewModel
|
|
|
|
|
{
|
2019-10-25 04:26:29 +00:00
|
|
|
|
public MainWindow MainWindow { get; }
|
2019-10-22 02:12:55 +00:00
|
|
|
|
|
2019-11-06 03:22:38 +00:00
|
|
|
|
public MainSettings Settings { get; }
|
|
|
|
|
|
2019-11-24 08:12:28 +00:00
|
|
|
|
[Reactive]
|
2020-01-05 02:50:05 +00:00
|
|
|
|
public ViewModel ActivePane { get; private set; }
|
2019-10-22 02:12:55 +00:00
|
|
|
|
|
2019-12-04 04:12:08 +00:00
|
|
|
|
public ObservableCollectionExtended<IStatusMessage> Log { get; } = new ObservableCollectionExtended<IStatusMessage>();
|
2019-10-23 04:15:42 +00:00
|
|
|
|
|
2019-11-24 08:12:28 +00:00
|
|
|
|
public readonly Lazy<CompilerVM> Compiler;
|
|
|
|
|
public readonly Lazy<InstallerVM> Installer;
|
2020-01-05 03:09:02 +00:00
|
|
|
|
public readonly Lazy<SettingsVM> SettingsPane;
|
2019-11-30 09:08:04 +00:00
|
|
|
|
public readonly Lazy<ModListGalleryVM> Gallery;
|
2019-11-24 08:12:28 +00:00
|
|
|
|
public readonly ModeSelectionVM ModeSelectionVM;
|
2019-12-08 17:00:22 +00:00
|
|
|
|
public readonly UserInterventionHandlers UserInterventionHandlers;
|
2019-12-20 20:51:10 +00:00
|
|
|
|
|
2019-12-14 23:31:53 +00:00
|
|
|
|
public ICommand CopyVersionCommand { get; }
|
2019-12-20 20:51:10 +00:00
|
|
|
|
public ICommand ShowLoginManagerVM { get; }
|
2020-01-05 03:09:02 +00:00
|
|
|
|
public ICommand OpenSettingsCommand { get; }
|
2020-02-11 05:04:56 +00:00
|
|
|
|
public ICommand OpenTerminalCommand { get; }
|
2020-01-05 03:09:02 +00:00
|
|
|
|
|
2019-12-14 23:31:53 +00:00
|
|
|
|
public string VersionDisplay { get; }
|
|
|
|
|
|
2020-02-02 07:33:12 +00:00
|
|
|
|
[Reactive]
|
|
|
|
|
public bool UpdateAvailable { get; private set; }
|
|
|
|
|
|
2019-11-24 08:12:28 +00:00
|
|
|
|
public MainWindowVM(MainWindow mainWindow, MainSettings settings)
|
2019-10-22 02:12:55 +00:00
|
|
|
|
{
|
2020-01-06 04:49:11 +00:00
|
|
|
|
ConverterRegistration.Register();
|
2019-11-21 15:04:33 +00:00
|
|
|
|
MainWindow = mainWindow;
|
|
|
|
|
Settings = settings;
|
2019-11-24 08:12:28 +00:00
|
|
|
|
Installer = new Lazy<InstallerVM>(() => new InstallerVM(this));
|
|
|
|
|
Compiler = new Lazy<CompilerVM>(() => new CompilerVM(this));
|
2020-01-05 03:09:02 +00:00
|
|
|
|
SettingsPane = new Lazy<SettingsVM>(() => new SettingsVM(this));
|
2019-11-30 09:08:04 +00:00
|
|
|
|
Gallery = new Lazy<ModListGalleryVM>(() => new ModListGalleryVM(this));
|
2019-11-24 08:12:28 +00:00
|
|
|
|
ModeSelectionVM = new ModeSelectionVM(this);
|
2019-12-08 19:46:30 +00:00
|
|
|
|
UserInterventionHandlers = new UserInterventionHandlers(this);
|
2019-10-25 04:26:29 +00:00
|
|
|
|
|
2019-10-23 04:15:42 +00:00
|
|
|
|
// Set up logging
|
2019-11-05 04:14:31 +00:00
|
|
|
|
Utils.LogMessages
|
2019-11-10 23:43:27 +00:00
|
|
|
|
.ObserveOn(RxApp.TaskpoolScheduler)
|
2019-10-23 04:15:42 +00:00
|
|
|
|
.ToObservableChangeSet()
|
2019-11-10 23:43:27 +00:00
|
|
|
|
.Buffer(TimeSpan.FromMilliseconds(250), RxApp.TaskpoolScheduler)
|
2019-10-23 04:15:42 +00:00
|
|
|
|
.Where(l => l.Count > 0)
|
|
|
|
|
.FlattenBufferResult()
|
2020-01-09 03:22:49 +00:00
|
|
|
|
.ObserveOnGuiThread()
|
2019-11-21 15:04:33 +00:00
|
|
|
|
.Bind(Log)
|
2019-10-23 04:15:42 +00:00
|
|
|
|
.Subscribe()
|
2019-11-21 15:04:33 +00:00
|
|
|
|
.DisposeWith(CompositeDisposable);
|
2020-02-06 05:30:31 +00:00
|
|
|
|
|
|
|
|
|
var singleton_lock = new AsyncLock();
|
2019-11-17 04:16:42 +00:00
|
|
|
|
|
2019-12-04 04:12:08 +00:00
|
|
|
|
Utils.LogMessages
|
2019-12-08 17:00:22 +00:00
|
|
|
|
.OfType<IUserIntervention>()
|
2019-12-08 19:46:30 +00:00
|
|
|
|
.ObserveOnGuiThread()
|
2020-01-05 01:31:54 +00:00
|
|
|
|
.SelectTask(async msg =>
|
|
|
|
|
{
|
2020-04-17 03:52:19 +00:00
|
|
|
|
using var _ = await singleton_lock.WaitAsync();
|
2020-01-05 01:31:54 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await UserInterventionHandlers.Handle(msg);
|
|
|
|
|
}
|
2020-02-02 07:33:12 +00:00
|
|
|
|
catch (Exception ex)
|
2020-02-06 05:30:31 +00:00
|
|
|
|
when (ex.GetType() != typeof(TaskCanceledException))
|
2020-01-05 01:31:54 +00:00
|
|
|
|
{
|
|
|
|
|
Utils.Error(ex, $"Error while handling user intervention of type {msg?.GetType()}");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!msg.Handled)
|
|
|
|
|
{
|
|
|
|
|
msg.Cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception cancelEx)
|
|
|
|
|
{
|
|
|
|
|
Utils.Error(cancelEx, $"Error while cancelling user intervention of type {msg?.GetType()}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
2019-12-08 19:46:30 +00:00
|
|
|
|
.Subscribe()
|
|
|
|
|
.DisposeWith(CompositeDisposable);
|
2019-12-07 05:40:57 +00:00
|
|
|
|
|
2019-11-24 23:48:39 +00:00
|
|
|
|
if (IsStartingFromModlist(out var path))
|
|
|
|
|
{
|
2019-12-03 05:40:59 +00:00
|
|
|
|
Installer.Value.ModListLocation.TargetPath = path;
|
2020-01-05 02:50:05 +00:00
|
|
|
|
NavigateTo(Installer.Value);
|
2019-11-24 23:48:39 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Start on mode selection
|
2020-01-05 02:50:05 +00:00
|
|
|
|
NavigateTo(ModeSelectionVM);
|
2019-11-24 23:48:39 +00:00
|
|
|
|
}
|
2019-12-14 23:31:53 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-02-24 15:29:33 +00:00
|
|
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
|
|
|
var fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
|
2019-12-14 23:31:53 +00:00
|
|
|
|
VersionDisplay = $"v{fvi.FileVersion}";
|
2020-02-24 15:29:33 +00:00
|
|
|
|
Utils.Log($"Wabbajack Version: {fvi.FileVersion}");
|
2019-12-14 23:31:53 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Utils.Error(ex);
|
|
|
|
|
VersionDisplay = "ERROR";
|
|
|
|
|
}
|
|
|
|
|
CopyVersionCommand = ReactiveCommand.Create(() =>
|
|
|
|
|
{
|
|
|
|
|
Clipboard.SetText($"Wabbajack {VersionDisplay}\n{ThisAssembly.Git.Sha}");
|
|
|
|
|
});
|
2020-01-05 03:09:02 +00:00
|
|
|
|
OpenSettingsCommand = ReactiveCommand.Create(
|
|
|
|
|
canExecute: this.WhenAny(x => x.ActivePane)
|
|
|
|
|
.Select(active => !SettingsPane.IsValueCreated || !object.ReferenceEquals(active, SettingsPane.Value)),
|
|
|
|
|
execute: () => NavigateTo(SettingsPane.Value));
|
2020-02-02 07:33:12 +00:00
|
|
|
|
|
2020-02-11 05:04:56 +00:00
|
|
|
|
OpenTerminalCommand = ReactiveCommand.Create(() => OpenTerminal());
|
2019-11-24 23:48:39 +00:00
|
|
|
|
}
|
2020-01-05 13:09:34 +00:00
|
|
|
|
|
2020-02-11 05:04:56 +00:00
|
|
|
|
private void OpenTerminal()
|
|
|
|
|
{
|
|
|
|
|
var process = new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = "cmd.exe",
|
|
|
|
|
WorkingDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
|
|
|
|
|
};
|
|
|
|
|
Process.Start(process);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 20:04:22 +00:00
|
|
|
|
private static bool IsStartingFromModlist(out AbsolutePath modlistPath)
|
2019-11-24 23:48:39 +00:00
|
|
|
|
{
|
2020-01-05 13:09:34 +00:00
|
|
|
|
if (CLIArguments.InstallPath == null)
|
2019-11-24 23:48:39 +00:00
|
|
|
|
{
|
|
|
|
|
modlistPath = default;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 20:04:22 +00:00
|
|
|
|
modlistPath = (AbsolutePath)CLIArguments.InstallPath;
|
2019-11-24 23:48:39 +00:00
|
|
|
|
return true;
|
2019-10-23 04:15:42 +00:00
|
|
|
|
}
|
2019-11-30 09:08:04 +00:00
|
|
|
|
|
2019-12-20 20:51:10 +00:00
|
|
|
|
|
2020-03-28 20:04:22 +00:00
|
|
|
|
public void OpenInstaller(AbsolutePath path)
|
2019-11-30 09:08:04 +00:00
|
|
|
|
{
|
2020-03-28 20:04:22 +00:00
|
|
|
|
if (path == default) return;
|
2019-11-30 09:08:04 +00:00
|
|
|
|
var installer = Installer.Value;
|
|
|
|
|
Settings.Installer.LastInstalledListLocation = path;
|
2020-01-05 02:50:05 +00:00
|
|
|
|
NavigateTo(installer);
|
2019-12-03 05:40:59 +00:00
|
|
|
|
installer.ModListLocation.TargetPath = path;
|
2019-11-30 09:08:04 +00:00
|
|
|
|
}
|
2019-12-11 04:59:15 +00:00
|
|
|
|
|
2020-01-05 02:33:38 +00:00
|
|
|
|
public void NavigateTo(ViewModel vm)
|
2019-12-20 20:51:10 +00:00
|
|
|
|
{
|
2020-01-05 02:33:38 +00:00
|
|
|
|
ActivePane = vm;
|
2019-12-20 20:51:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 03:06:34 +00:00
|
|
|
|
public void NavigateTo<T>(T vm)
|
|
|
|
|
where T : ViewModel, IBackNavigatingVM
|
2019-12-20 20:51:10 +00:00
|
|
|
|
{
|
2020-01-05 02:33:38 +00:00
|
|
|
|
vm.NavigateBackTarget = ActivePane;
|
2019-12-20 20:51:10 +00:00
|
|
|
|
ActivePane = vm;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 04:59:15 +00:00
|
|
|
|
public void ShutdownApplication()
|
|
|
|
|
{
|
|
|
|
|
Dispose();
|
|
|
|
|
Settings.PosX = MainWindow.Left;
|
|
|
|
|
Settings.PosY = MainWindow.Top;
|
|
|
|
|
Settings.Width = MainWindow.Width;
|
|
|
|
|
Settings.Height = MainWindow.Height;
|
|
|
|
|
MainSettings.SaveSettings(Settings);
|
|
|
|
|
Application.Current.Shutdown();
|
|
|
|
|
}
|
2019-10-22 02:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|