wabbajack/Wabbajack/View Models/InstallerVM.cs

320 lines
14 KiB
C#
Raw Normal View History

using Syroot.Windows.IO;
using System;
using ReactiveUI;
2019-08-30 23:57:56 +00:00
using System.Diagnostics;
2019-07-22 22:17:46 +00:00
using System.IO;
using System.IO.Compression;
2019-09-26 03:18:36 +00:00
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
2019-07-22 22:17:46 +00:00
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
2019-07-31 03:59:19 +00:00
using System.Windows;
2019-09-26 03:18:36 +00:00
using System.Windows.Media.Imaging;
2019-07-22 22:17:46 +00:00
using Wabbajack.Common;
using Wabbajack.Lib;
2019-11-02 23:23:11 +00:00
using ReactiveUI.Fody.Helpers;
2019-11-09 01:53:32 +00:00
using System.Windows.Media;
2019-07-22 22:17:46 +00:00
namespace Wabbajack
{
public class InstallerVM : ViewModel
2019-07-22 22:17:46 +00:00
{
public SlideShow Slideshow { get; }
public MainWindowVM MWVM { get; }
public BitmapImage WabbajackLogo { get; } = UIUtils.BitmapImageFromResource("Wabbajack.Resources.Wabba_Mouth.png");
2019-11-03 06:01:19 +00:00
private readonly ObservableAsPropertyHelper<ModListVM> _ModList;
public ModListVM ModList => _ModList.Value;
2019-11-02 23:23:11 +00:00
[Reactive]
public string ModListPath { get; set; }
2019-11-02 23:23:11 +00:00
[Reactive]
public bool UIReady { get; set; }
2019-10-12 18:42:47 +00:00
private readonly ObservableAsPropertyHelper<string> _HTMLReport;
public string HTMLReport => _HTMLReport.Value;
/// <summary>
/// Tracks whether an install is currently in progress
/// </summary>
2019-11-02 23:23:11 +00:00
[Reactive]
public bool Installing { get; set; }
/// <summary>
/// Tracks whether to show the installing pane
/// </summary>
2019-11-02 23:23:11 +00:00
[Reactive]
public bool InstallingMode { get; set; }
2019-11-11 18:08:08 +00:00
public FilePickerVM Location { get; }
public FilePickerVM DownloadLocation { get; }
private readonly ObservableAsPropertyHelper<float> _ProgressPercent;
public float ProgressPercent => _ProgressPercent.Value;
2019-11-09 01:53:32 +00:00
private readonly ObservableAsPropertyHelper<ImageSource> _Image;
public ImageSource Image => _Image.Value;
private readonly ObservableAsPropertyHelper<string> _TitleText;
public string TitleText => _TitleText.Value;
private readonly ObservableAsPropertyHelper<string> _AuthorText;
public string AuthorText => _AuthorText.Value;
2019-11-01 04:59:10 +00:00
private readonly ObservableAsPropertyHelper<string> _Description;
public string Description => _Description.Value;
2019-11-07 05:33:08 +00:00
private readonly ObservableAsPropertyHelper<string> _ProgressTitle;
public string ProgressTitle => _ProgressTitle.Value;
2019-11-07 05:37:40 +00:00
private readonly ObservableAsPropertyHelper<string> _ModListName;
public string ModListName => _ModListName.Value;
// Command properties
2019-10-12 18:42:47 +00:00
public IReactiveCommand BeginCommand { get; }
public IReactiveCommand ShowReportCommand { get; }
public IReactiveCommand OpenReadmeCommand { get; }
public IReactiveCommand VisitWebsiteCommand { get; }
public InstallerVM(MainWindowVM mainWindowVM, string source)
2019-07-30 21:45:04 +00:00
{
2019-10-12 08:02:58 +00:00
if (Path.GetDirectoryName(Assembly.GetEntryAssembly().Location.ToLower()) == KnownFolders.Downloads.Path.ToLower())
2019-09-14 04:35:42 +00:00
{
MessageBox.Show(
"Wabbajack is running inside your Downloads folder. This folder is often highly monitored by antivirus software and these can often " +
2019-10-12 08:02:58 +00:00
"conflict with the operations Wabbajack needs to perform. Please move this executable outside of your Downloads folder and then restart the app.",
"Cannot run inside Downloads",
2019-09-14 04:35:42 +00:00
MessageBoxButton.OK,
MessageBoxImage.Error);
Environment.Exit(1);
}
this.MWVM = mainWindowVM;
this.ModListPath = source;
this.Location = new FilePickerVM()
{
ExistCheckOption = FilePickerVM.ExistCheckOptions.Off,
PathType = FilePickerVM.PathTypeOptions.Folder,
PromptTitle = "Select Installation Directory",
};
this.Location.AdditionalError = this.WhenAny(x => x.Location.TargetPath)
.Select(x => Utils.IsDirectoryPathValid(x));
this.DownloadLocation = new FilePickerVM()
{
ExistCheckOption = FilePickerVM.ExistCheckOptions.Off,
PathType = FilePickerVM.PathTypeOptions.Folder,
PromptTitle = "Select a location for MO2 downloads",
};
this.DownloadLocation.AdditionalError = this.WhenAny(x => x.DownloadLocation.TargetPath)
.Select(x => Utils.IsDirectoryPathValid(x));
// Load settings
ModlistInstallationSettings settings = this.MWVM.Settings.Installer.ModlistSettings.TryCreate(source);
this.Location.TargetPath = settings.InstallationLocation;
this.DownloadLocation.TargetPath = settings.DownloadLocation;
this.MWVM.Settings.SaveSignal
.Subscribe(_ =>
{
settings.InstallationLocation = this.Location.TargetPath;
settings.DownloadLocation = this.DownloadLocation.TargetPath;
})
.DisposeWith(this.CompositeDisposable);
2019-09-14 04:35:42 +00:00
this._ModList = this.WhenAny(x => x.ModListPath)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(modListPath =>
{
if (modListPath == null) return default(ModListVM);
var modList = MO2Installer.LoadFromFile(modListPath);
2019-11-03 06:01:19 +00:00
if (modList == null)
{
MessageBox.Show("Invalid Modlist, or file not found.", "Invalid Modlist", MessageBoxButton.OK,
MessageBoxImage.Error);
Application.Current.Dispatcher.Invoke(() =>
{
this.MWVM.MainWindow.ExitWhenClosing = false;
var window = new ModeSelectionWindow
{
ShowActivated = true
};
window.Show();
this.MWVM.MainWindow.Close();
});
2019-11-03 06:01:19 +00:00
return default(ModListVM);
}
return new ModListVM(modList, modListPath);
})
.ObserveOnGuiThread()
2019-11-03 06:01:19 +00:00
.StartWith(default(ModListVM))
.ToProperty(this, nameof(this.ModList));
this._HTMLReport = this.WhenAny(x => x.ModList)
.Select(modList => modList?.ReportHTML)
.ToProperty(this, nameof(this.HTMLReport));
this._ProgressPercent = Observable.CombineLatest(
this.WhenAny(x => x.Installing),
this.WhenAny(x => x.InstallingMode),
resultSelector: (installing, mode) => !installing && mode)
.Select(show => show ? 1f : 0f)
// Disable for now, until more reliable
//this.WhenAny(x => x.MWVM.QueueProgress)
// .Select(i => i / 100f)
.ToProperty(this, nameof(this.ProgressPercent));
this.Slideshow = new SlideShow(this);
// Set display items to modlist if configuring or complete,
// or to the current slideshow data if installing
this._Image = Observable.CombineLatest(
2019-11-03 06:01:19 +00:00
this.WhenAny(x => x.ModList)
.SelectMany(x => x?.ImageObservable ?? Observable.Empty<BitmapImage>())
.NotNull()
.StartWith(WabbajackLogo),
this.WhenAny(x => x.Slideshow.Image)
.StartWith(default(BitmapImage)),
this.WhenAny(x => x.Installing),
resultSelector: (modList, slideshow, installing) => installing ? slideshow : modList)
2019-11-09 01:53:32 +00:00
.Select<BitmapImage, ImageSource>(x => x)
.ToProperty(this, nameof(this.Image));
this._TitleText = Observable.CombineLatest(
2019-11-01 04:59:10 +00:00
this.WhenAny(x => x.ModList.Name),
2019-11-03 06:01:19 +00:00
this.WhenAny(x => x.Slideshow.TargetMod.ModName)
.StartWith(default(string)),
this.WhenAny(x => x.Installing),
resultSelector: (modList, mod, installing) => installing ? mod : modList)
.ToProperty(this, nameof(this.TitleText));
this._AuthorText = Observable.CombineLatest(
2019-11-01 04:59:10 +00:00
this.WhenAny(x => x.ModList.Author),
2019-11-03 06:01:19 +00:00
this.WhenAny(x => x.Slideshow.TargetMod.ModAuthor)
.StartWith(default(string)),
this.WhenAny(x => x.Installing),
resultSelector: (modList, mod, installing) => installing ? mod : modList)
.ToProperty(this, nameof(this.AuthorText));
2019-11-01 04:59:10 +00:00
this._Description = Observable.CombineLatest(
this.WhenAny(x => x.ModList.Description),
2019-11-03 06:01:19 +00:00
this.WhenAny(x => x.Slideshow.TargetMod.ModDescription)
.StartWith(default(string)),
this.WhenAny(x => x.Installing),
resultSelector: (modList, mod, installing) => installing ? mod : modList)
2019-11-01 04:59:10 +00:00
.ToProperty(this, nameof(this.Description));
2019-11-07 05:37:40 +00:00
this._ModListName = this.WhenAny(x => x.ModList)
.Select(x => x?.Name)
.ToProperty(this, nameof(this.ModListName));
// Define commands
this.ShowReportCommand = ReactiveCommand.Create(ShowReport);
this.OpenReadmeCommand = ReactiveCommand.Create(
execute: this.OpenReadmeWindow,
2019-10-13 20:14:11 +00:00
canExecute: this.WhenAny(x => x.ModList)
.Select(modList => !string.IsNullOrEmpty(modList?.Readme))
2019-10-12 18:42:47 +00:00
.ObserveOnGuiThread());
this.BeginCommand = ReactiveCommand.Create(
execute: this.ExecuteBegin,
canExecute: Observable.CombineLatest(
this.WhenAny(x => x.Installing),
2019-11-11 18:08:08 +00:00
this.WhenAny(x => x.Location.InError),
this.WhenAny(x => x.DownloadLocation.InError),
resultSelector: (installing, loc, download) =>
{
if (installing) return false;
return !loc && !download;
})
2019-10-12 18:42:47 +00:00
.ObserveOnGuiThread());
this.VisitWebsiteCommand = ReactiveCommand.Create(
execute: () => Process.Start(this.ModList.Website),
canExecute: this.WhenAny(x => x.ModList.Website)
.Select(x => x?.StartsWith("https://") ?? false)
.ObserveOnGuiThread());
// Have Installation location updates modify the downloads location if empty
this.WhenAny(x => x.Location.TargetPath)
.Skip(1) // Don't do it initially
.Subscribe(installPath =>
{
if (string.IsNullOrWhiteSpace(this.DownloadLocation.TargetPath))
{
this.DownloadLocation.TargetPath = Path.Combine(installPath, "downloads");
}
})
.DisposeWith(this.CompositeDisposable);
2019-11-07 05:33:08 +00:00
this._ProgressTitle = Observable.CombineLatest(
this.WhenAny(x => x.Installing),
this.WhenAny(x => x.InstallingMode),
resultSelector: (installing, mode) =>
{
if (!installing) return "Configuring";
return mode ? "Installing" : "Installed";
})
.ToProperty(this, nameof(this.ProgressTitle));
2019-10-09 09:22:03 +00:00
}
2019-09-26 03:18:36 +00:00
2019-10-09 09:22:03 +00:00
private void ShowReport()
{
var file = Path.GetTempFileName() + ".html";
File.WriteAllText(file, HTMLReport);
Process.Start(file);
}
2019-10-11 12:57:42 +00:00
private void OpenReadmeWindow()
{
2019-10-13 20:14:11 +00:00
if (string.IsNullOrEmpty(this.ModList.Readme)) return;
2019-10-12 03:12:43 +00:00
using (var fs = new FileStream(this.ModListPath, FileMode.Open, FileAccess.Read, FileShare.Read))
2019-10-11 13:06:56 +00:00
using (var ar = new ZipArchive(fs, ZipArchiveMode.Read))
using (var ms = new MemoryStream())
2019-10-11 12:57:42 +00:00
{
var entry = ar.GetEntry(this.ModList.Readme);
if (entry == null)
{
Utils.Log($"Tried to open a non-existant readme: {this.ModList.Readme}");
return;
}
2019-10-11 13:06:56 +00:00
using (var e = entry.Open())
{
2019-10-11 13:06:56 +00:00
e.CopyTo(ms);
}
2019-10-11 13:06:56 +00:00
ms.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(ms))
2019-10-11 12:57:42 +00:00
{
2019-11-01 04:59:10 +00:00
var viewer = new TextViewer(reader.ReadToEnd(), this.ModList.Name);
viewer.Show();
2019-10-11 12:57:42 +00:00
}
}
}
2019-09-26 03:18:36 +00:00
2019-07-31 03:59:19 +00:00
private void ExecuteBegin()
{
this.Installing = true;
this.InstallingMode = true;
var installer = new MO2Installer(this.ModListPath, this.ModList.SourceModList, Location.TargetPath)
2019-07-31 03:59:19 +00:00
{
DownloadFolder = DownloadLocation.TargetPath
};
Task.Run(async () =>
{
try
{
await installer.Begin();
}
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
{
this.Installing = false;
}
});
2019-07-31 03:59:19 +00:00
}
}
2019-07-22 22:17:46 +00:00
}