2019-10-23 04:15:42 +00:00
|
|
|
using Syroot.Windows.IO;
|
2019-10-11 22:18:51 +00:00
|
|
|
using System;
|
2019-10-12 21:10:58 +00:00
|
|
|
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;
|
2019-10-11 11:15:54 +00:00
|
|
|
using System.IO.Compression;
|
2019-09-26 03:18:36 +00:00
|
|
|
using System.Linq;
|
2019-10-04 20:50:34 +00:00
|
|
|
using System.Net.Http;
|
2019-10-12 18:18:21 +00:00
|
|
|
using System.Reactive.Subjects;
|
2019-10-12 03:07:57 +00:00
|
|
|
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;
|
2019-10-16 03:10:34 +00:00
|
|
|
using Wabbajack.Lib.Downloaders;
|
|
|
|
using Wabbajack.Lib.NexusApi;
|
2019-10-12 18:18:21 +00:00
|
|
|
using DynamicData;
|
|
|
|
using DynamicData.Binding;
|
2019-10-12 21:04:14 +00:00
|
|
|
using System.Reactive;
|
2019-10-13 20:21:09 +00:00
|
|
|
using System.Text;
|
2019-10-16 03:10:34 +00:00
|
|
|
using Wabbajack.Lib;
|
2019-07-22 22:17:46 +00:00
|
|
|
|
|
|
|
namespace Wabbajack
|
|
|
|
{
|
2019-10-25 04:26:29 +00:00
|
|
|
public class InstallerVM : ViewModel, IDataErrorInfo
|
2019-07-22 22:17:46 +00:00
|
|
|
{
|
2019-10-13 19:12:33 +00:00
|
|
|
public SlideShow Slideshow { get; }
|
2019-10-22 03:03:01 +00:00
|
|
|
public MainWindowVM MWVM { get; }
|
2019-10-11 10:14:01 +00:00
|
|
|
|
2019-10-28 04:59:58 +00:00
|
|
|
private readonly ObservableAsPropertyHelper<ModList> _ModList;
|
|
|
|
public ModList ModList => _ModList.Value;
|
2019-10-12 03:07:57 +00:00
|
|
|
|
2019-10-12 03:12:43 +00:00
|
|
|
private string _ModListPath;
|
|
|
|
public string ModListPath { get => _ModListPath; private set => this.RaiseAndSetIfChanged(ref _ModListPath, value); }
|
|
|
|
|
2019-10-25 04:26:29 +00:00
|
|
|
public RunMode Mode => RunMode.Install;
|
2019-10-12 03:31:33 +00:00
|
|
|
|
2019-10-28 04:59:58 +00:00
|
|
|
private readonly ObservableAsPropertyHelper<string> _ModListName;
|
|
|
|
public string ModListName => _ModListName.Value;
|
2019-10-12 03:31:33 +00:00
|
|
|
|
2019-10-12 18:42:47 +00:00
|
|
|
private bool _UIReady;
|
|
|
|
public bool UIReady { get => _UIReady; set => this.RaiseAndSetIfChanged(ref _UIReady, value); }
|
|
|
|
|
2019-10-28 04:59:58 +00:00
|
|
|
private readonly ObservableAsPropertyHelper<string> _HTMLReport;
|
|
|
|
public string HTMLReport => _HTMLReport.Value;
|
2019-10-12 19:11:52 +00:00
|
|
|
|
2019-10-12 21:04:14 +00:00
|
|
|
private bool _Installing;
|
|
|
|
public bool Installing { get => _Installing; set => this.RaiseAndSetIfChanged(ref _Installing, value); }
|
|
|
|
|
2019-10-28 04:59:58 +00:00
|
|
|
private string _Location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
2019-10-25 04:26:29 +00:00
|
|
|
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); }
|
|
|
|
|
2019-10-11 22:30:12 +00:00
|
|
|
// Command properties
|
2019-10-13 08:03:41 +00:00
|
|
|
public IReactiveCommand ChangePathCommand { get; }
|
|
|
|
public IReactiveCommand ChangeDownloadPathCommand { get; }
|
2019-10-12 18:42:47 +00:00
|
|
|
public IReactiveCommand BeginCommand { get; }
|
2019-10-13 08:03:41 +00:00
|
|
|
public IReactiveCommand ShowReportCommand { get; }
|
2019-10-12 03:07:57 +00:00
|
|
|
public IReactiveCommand OpenReadmeCommand { get; }
|
2019-10-11 22:30:12 +00:00
|
|
|
|
2019-10-25 04:26:29 +00:00
|
|
|
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(
|
2019-10-12 09:12:53 +00:00
|
|
|
"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);
|
|
|
|
}
|
|
|
|
|
2019-10-22 03:03:01 +00:00
|
|
|
this.MWVM = mainWindowVM;
|
2019-09-14 04:35:42 +00:00
|
|
|
|
2019-10-28 04:59:58 +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));
|
|
|
|
|
2019-10-13 08:03:41 +00:00
|
|
|
// Define commands
|
|
|
|
this.ChangePathCommand = ReactiveCommand.Create(ExecuteChangePath);
|
|
|
|
this.ChangeDownloadPathCommand = ReactiveCommand.Create(ExecuteChangeDownloadPath);
|
|
|
|
this.ShowReportCommand = ReactiveCommand.Create(ShowReport);
|
2019-10-12 03:07:57 +00:00
|
|
|
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());
|
2019-10-12 03:07:57 +00:00
|
|
|
|
2019-10-13 19:12:33 +00:00
|
|
|
this.Slideshow = new SlideShow(this);
|
2019-07-30 21:45:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 09:22:03 +00:00
|
|
|
private void ExecuteChangePath()
|
|
|
|
{
|
2019-10-25 04:26:29 +00:00
|
|
|
var folder = UIUtils.ShowFolderSelectionDialog("Select Installation directory");
|
|
|
|
if (folder == null) return;
|
|
|
|
Location = folder;
|
|
|
|
if (DownloadLocation == null)
|
2019-10-09 09:22:03 +00:00
|
|
|
{
|
2019-10-25 04:26:29 +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
|
|
|
{
|
2019-10-12 03:07:57 +00:00
|
|
|
var entry = ar.GetEntry(this.ModList.Readme);
|
2019-10-11 13:06:56 +00:00
|
|
|
using (var e = entry.Open())
|
2019-10-13 20:21:09 +00:00
|
|
|
{
|
2019-10-11 13:06:56 +00:00
|
|
|
e.CopyTo(ms);
|
2019-10-13 20:21:09 +00:00
|
|
|
}
|
2019-10-11 13:06:56 +00:00
|
|
|
ms.Seek(0, SeekOrigin.Begin);
|
2019-10-13 20:21:09 +00:00
|
|
|
using (var reader = new StreamReader(ms))
|
2019-10-11 12:57:42 +00:00
|
|
|
{
|
2019-10-13 20:21:09 +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
|
|
|
|
2019-10-09 22:59:58 +00:00
|
|
|
public string Error => "Error";
|
2019-09-14 04:35:42 +00:00
|
|
|
|
2019-10-09 22:59:58 +00:00
|
|
|
public string this[string columnName] => Validate(columnName);
|
2019-09-26 14:35:34 +00:00
|
|
|
|
|
|
|
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)
|
2019-09-26 14:35:34 +00:00
|
|
|
{
|
2019-10-13 18:59:29 +00:00
|
|
|
case RunMode.Install when Location != null && Directory.Exists(Location) && !Directory.EnumerateFileSystemEntries(Location).Any():
|
2019-10-11 08:53:12 +00:00
|
|
|
validationMessage = null;
|
|
|
|
break;
|
2019-10-13 18:59:29 +00:00
|
|
|
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;
|
2019-09-26 14:35:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return validationMessage;
|
2019-07-22 22:17:46 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 03:59:19 +00:00
|
|
|
private void ExecuteBegin()
|
|
|
|
{
|
2019-09-18 03:12:25 +00:00
|
|
|
UIReady = false;
|
2019-10-13 18:59:29 +00:00
|
|
|
if (this.Mode == RunMode.Install)
|
2019-07-31 03:59:19 +00:00
|
|
|
{
|
2019-10-12 21:04:14 +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(() =>
|
|
|
|
{
|
2019-09-18 03:12:25 +00:00
|
|
|
UIReady = false;
|
2019-07-31 03:59:19 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
installer.Install();
|
|
|
|
}
|
2019-08-02 22:31:13 +00:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2019-08-22 22:05:16 +00:00
|
|
|
while (ex.InnerException != null) ex = ex.InnerException;
|
2019-10-23 04:15:42 +00:00
|
|
|
Utils.Log(ex.StackTrace);
|
|
|
|
Utils.Log(ex.ToString());
|
|
|
|
Utils.Log($"{ex.Message} - Can't continue");
|
2019-07-31 03:59:19 +00:00
|
|
|
}
|
2019-09-18 03:12:25 +00:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
UIReady = true;
|
2019-10-12 21:04:14 +00:00
|
|
|
this.Installing = false;
|
2019-09-18 03:12:25 +00:00
|
|
|
}
|
2019-10-07 11:48:39 +00:00
|
|
|
})
|
|
|
|
{
|
|
|
|
Priority = ThreadPriority.BelowNormal
|
|
|
|
};
|
2019-07-31 03:59:19 +00:00
|
|
|
th.Start();
|
|
|
|
}
|
2019-10-25 04:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Init(string source)
|
|
|
|
{
|
2019-10-28 04:59:58 +00:00
|
|
|
this.ModListPath = source;
|
2019-10-25 04:26:29 +00:00
|
|
|
this.UIReady = true;
|
2019-07-31 03:59:19 +00:00
|
|
|
}
|
2019-10-09 22:59:58 +00:00
|
|
|
}
|
2019-07-22 22:17:46 +00:00
|
|
|
}
|