wabbajack/Wabbajack/View Models/InstallerVM.cs

253 lines
9.7 KiB
C#
Raw Normal View History

using Syroot.Windows.IO;
using System;
using ReactiveUI;
2019-07-22 22:17:46 +00:00
using System.Collections.Generic;
using System.Collections.ObjectModel;
2019-07-30 21:45:04 +00:00
using System.ComponentModel;
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.Net.Http;
using System.Reactive.Subjects;
using System.Reactive.Disposables;
using System.Reactive.Linq;
2019-07-22 22:17:46 +00:00
using System.Reflection;
using System.Threading;
2019-07-31 03:59:19 +00:00
using System.Windows;
using System.Windows.Input;
2019-09-26 03:18:36 +00:00
using System.Windows.Media.Imaging;
2019-07-22 22:17:46 +00:00
using System.Windows.Threading;
using Wabbajack.Common;
using Wabbajack.Lib.Downloaders;
using Wabbajack.Lib.NexusApi;
using DynamicData;
using DynamicData.Binding;
using System.Reactive;
using System.Text;
using Wabbajack.Lib;
2019-07-22 22:17:46 +00:00
namespace Wabbajack
{
public class InstallerVM : ViewModel, IDataErrorInfo
2019-07-22 22:17:46 +00:00
{
public SlideShow Slideshow { get; }
public MainWindowVM MWVM { get; }
private readonly ObservableAsPropertyHelper<ModList> _ModList;
public ModList ModList => _ModList.Value;
2019-10-12 03:12:43 +00:00
private string _ModListPath;
public string ModListPath { get => _ModListPath; private set => this.RaiseAndSetIfChanged(ref _ModListPath, value); }
public RunMode Mode => RunMode.Install;
private readonly ObservableAsPropertyHelper<string> _ModListName;
public string ModListName => _ModListName.Value;
2019-10-12 18:42:47 +00:00
private bool _UIReady;
public bool UIReady { get => _UIReady; set => this.RaiseAndSetIfChanged(ref _UIReady, value); }
private readonly ObservableAsPropertyHelper<string> _HTMLReport;
public string HTMLReport => _HTMLReport.Value;
private bool _Installing;
public bool Installing { get => _Installing; set => this.RaiseAndSetIfChanged(ref _Installing, value); }
private string _Location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
public string Location { get => _Location; set => this.RaiseAndSetIfChanged(ref _Location, value); }
private string _DownloadLocation;
public string DownloadLocation { get => _DownloadLocation; set => this.RaiseAndSetIfChanged(ref _DownloadLocation, value); }
// Command properties
public IReactiveCommand ChangePathCommand { get; }
public IReactiveCommand ChangeDownloadPathCommand { get; }
2019-10-12 18:42:47 +00:00
public IReactiveCommand BeginCommand { get; }
public IReactiveCommand ShowReportCommand { get; }
public IReactiveCommand OpenReadmeCommand { get; }
public InstallerVM(MainWindowVM mainWindowVM)
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;
2019-09-14 04:35:42 +00:00
this._ModList = this.WhenAny(x => x.ModListPath)
.Select(source =>
{
if (source == null) return default;
var modlist = Installer.LoadFromFile(source);
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();
});
return default;
}
return modlist;
})
.ObserveOnGuiThread()
.ToProperty(this, nameof(this.ModList));
this._HTMLReport = this.WhenAny(x => x.ModList)
.Select(modList => modList?.ReportHTML)
.ToProperty(this, nameof(this.HTMLReport));
this._ModListName = this.WhenAny(x => x.ModList)
.Select(modList => modList?.Name)
.ToProperty(this, nameof(this.ModListName));
// Define commands
this.ChangePathCommand = ReactiveCommand.Create(ExecuteChangePath);
this.ChangeDownloadPathCommand = ReactiveCommand.Create(ExecuteChangeDownloadPath);
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: this.WhenAny(x => x.UIReady)
.ObserveOnGuiThread());
this.Slideshow = new SlideShow(this);
2019-07-30 21:45:04 +00:00
}
2019-10-09 09:22:03 +00:00
private void ExecuteChangePath()
{
var folder = UIUtils.ShowFolderSelectionDialog("Select Installation directory");
if (folder == null) return;
Location = folder;
if (DownloadLocation == null)
2019-10-09 09:22:03 +00:00
{
DownloadLocation = Path.Combine(Location, "downloads");
2019-10-09 09:22:03 +00:00
}
}
private void ExecuteChangeDownloadPath()
{
var folder = UIUtils.ShowFolderSelectionDialog("Select a location for MO2 downloads");
if (folder != null) DownloadLocation = folder;
}
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);
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
{
var viewer = new TextViewer(reader.ReadToEnd(), this.ModListName);
viewer.Show();
2019-10-11 12:57:42 +00:00
}
}
}
2019-09-26 03:18:36 +00:00
public string Error => "Error";
2019-09-14 04:35:42 +00:00
public string this[string columnName] => Validate(columnName);
private string Validate(string columnName)
{
string validationMessage = null;
switch (columnName)
{
case "Location":
if (Location == null)
{
validationMessage = null;
}
2019-10-11 08:53:12 +00:00
else switch (Mode)
{
case RunMode.Install when Location != null && Directory.Exists(Location) && !Directory.EnumerateFileSystemEntries(Location).Any():
2019-10-11 08:53:12 +00:00
validationMessage = null;
break;
case RunMode.Install when Location != null && Directory.Exists(Location) && Directory.EnumerateFileSystemEntries(Location).Any():
2019-10-11 08:53:12 +00:00
validationMessage = "You have selected a non-empty directory. Installing the modlist here might result in a broken install!";
break;
default:
validationMessage = "Invalid Mod Organizer profile directory";
break;
}
break;
}
return validationMessage;
2019-07-22 22:17:46 +00:00
}
2019-07-31 03:59:19 +00:00
private void ExecuteBegin()
{
UIReady = false;
if (this.Mode == RunMode.Install)
2019-07-31 03:59:19 +00:00
{
this.Installing = true;
2019-10-12 03:12:43 +00:00
var installer = new Installer(this.ModListPath, this.ModList, Location)
2019-10-07 11:48:39 +00:00
{
DownloadFolder = DownloadLocation
};
2019-07-31 03:59:19 +00:00
var th = new Thread(() =>
{
UIReady = false;
2019-07-31 03:59:19 +00:00
try
{
installer.Install();
}
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");
2019-07-31 03:59:19 +00:00
}
finally
{
UIReady = true;
this.Installing = false;
}
2019-10-07 11:48:39 +00:00
})
{
Priority = ThreadPriority.BelowNormal
};
2019-07-31 03:59:19 +00:00
th.Start();
}
}
public void Init(string source)
{
this.ModListPath = source;
this.UIReady = true;
2019-07-31 03:59:19 +00:00
}
}
2019-07-22 22:17:46 +00:00
}