wabbajack/Wabbajack/AppState.cs

513 lines
17 KiB
C#
Raw Normal View History

2019-08-25 23:52:03 +00:00
using System;
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-09-26 03:18:36 +00:00
using System.Linq;
using System.Net.Http;
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.NexusApi;
2019-07-22 22:17:46 +00:00
namespace Wabbajack
{
internal class AppState : ViewModel, IDataErrorInfo
2019-07-22 22:17:46 +00:00
{
2019-09-14 04:35:42 +00:00
private string _mo2Folder;
private ModList _modList;
2019-09-26 03:18:36 +00:00
2019-09-14 04:35:42 +00:00
private readonly DateTime _startTime;
public volatile bool Dirty;
private readonly Dispatcher dispatcher;
public AppState(Dispatcher d, string mode)
2019-07-30 21:45:04 +00:00
{
2019-09-26 03:18:36 +00:00
var image = new BitmapImage();
image.BeginInit();
image.StreamSource = Assembly.GetExecutingAssembly().GetManifestResourceStream("Wabbajack.banner.png");
image.EndInit();
_wabbajackLogo = image;
_splashScreenImage = image;
SetupSlideshow();
2019-09-14 04:35:42 +00:00
if (Assembly.GetEntryAssembly().Location.ToLower().Contains("\\downloads\\"))
{
MessageBox.Show(
"This app seems to be running inside a folder called 'Downloads', such folders are often highly monitored by antivirus software and they can often " +
"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);
}
_startTime = DateTime.Now;
Mode = mode;
Dirty = false;
dispatcher = d;
var th = new Thread(() => UpdateLoop());
th.Priority = ThreadPriority.BelowNormal;
th.IsBackground = true;
th.Start();
2019-07-30 21:45:04 +00:00
}
2019-09-26 03:18:36 +00:00
private void SetupSlideshow()
{
var files = NexusApiClient.CachedSlideShow;
2019-09-26 03:18:36 +00:00
if (files.Any())
{
SlideShowElements = files.ToList();
}
}
public Random _random = new Random();
public List<SlideShowItem> SlideShowElements = new List<SlideShowItem>();
2019-09-26 03:18:36 +00:00
private DateTime _lastSlideShowUpdate = new DateTime();
public ObservableCollection<string> Log { get; } = new ObservableCollection<string>();
public ObservableCollection<CPUStatus> Status { get; } = new ObservableCollection<CPUStatus>();
2019-07-22 22:17:46 +00:00
private string _mode;
2019-07-30 21:45:04 +00:00
public string Mode
{
2019-09-14 04:35:42 +00:00
get => _mode;
set => this.RaiseAndSetIfChanged(ref _mode, value);
2019-07-30 21:45:04 +00:00
}
private string _modListName;
2019-07-30 21:45:04 +00:00
public string ModListName
{
2019-09-14 04:35:42 +00:00
get => _modListName;
set => this.RaiseAndSetIfChanged(ref _modListName, value);
2019-07-30 21:45:04 +00:00
}
private string _location;
2019-07-31 03:59:19 +00:00
public string Location
{
2019-09-14 04:35:42 +00:00
get => _location;
set => this.RaiseAndSetIfChanged(ref _location, value);
2019-07-31 03:59:19 +00:00
}
private string _locationLabel;
public string LocationLabel
{
get => _locationLabel;
set => this.RaiseAndSetIfChanged(ref _locationLabel, value);
}
2019-07-31 03:59:19 +00:00
private string _downloadLocation;
public string DownloadLocation
{
2019-09-14 04:35:42 +00:00
get => _downloadLocation;
set => this.RaiseAndSetIfChanged(ref _downloadLocation, value);
}
2019-08-30 23:57:56 +00:00
public Visibility ShowReportButton => _htmlReport == null ? Visibility.Collapsed : Visibility.Visible;
private string _htmlReport;
2019-08-30 23:57:56 +00:00
public string HTMLReport
{
2019-09-14 04:35:42 +00:00
get => _htmlReport;
2019-08-30 23:57:56 +00:00
set
{
_htmlReport = value;
RaisePropertyChanged();
RaisePropertyChanged(nameof(ShowReportButton));
2019-08-30 23:57:56 +00:00
}
}
private int _queueProgress;
2019-08-02 23:04:04 +00:00
public int QueueProgress
{
2019-09-14 04:35:42 +00:00
get => _queueProgress;
set => this.RaiseAndSetIfChanged(ref _queueProgress, value);
2019-08-02 23:04:04 +00:00
}
2019-07-22 22:17:46 +00:00
private List<CPUStatus> InternalStatus { get; } = new List<CPUStatus>();
2019-09-14 04:35:42 +00:00
public string LogFile { get; }
2019-07-22 22:17:46 +00:00
private ICommand _changePath;
2019-09-14 04:35:42 +00:00
public ICommand ChangePath
2019-07-22 22:17:46 +00:00
{
2019-09-14 04:35:42 +00:00
get
2019-09-04 01:43:16 +00:00
{
2019-09-14 04:35:42 +00:00
if (_changePath == null) _changePath = new LambdaCommand(() => true, () => ExecuteChangePath());
return _changePath;
2019-09-04 01:43:16 +00:00
}
2019-09-14 04:35:42 +00:00
}
2019-09-04 01:43:16 +00:00
private ICommand _changeDownloadPath;
2019-09-14 04:35:42 +00:00
public ICommand ChangeDownloadPath
{
get
{
if (_changeDownloadPath == null)
_changeDownloadPath = new LambdaCommand(() => true, () => ExecuteChangeDownloadPath());
return _changeDownloadPath;
}
}
2019-07-22 22:17:46 +00:00
private ICommand _begin;
2019-09-14 04:35:42 +00:00
public ICommand Begin
{
get
{
if (_begin == null) _begin = new LambdaCommand(() => true, () => ExecuteBegin());
return _begin;
}
}
2019-07-22 22:17:46 +00:00
private ICommand _showReportCommand;
2019-09-14 04:35:42 +00:00
public ICommand ShowReportCommand
{
get
{
if (_showReportCommand == null) _showReportCommand = new LambdaCommand(() => true, () => ShowReport());
return _showReportCommand;
}
}
2019-07-22 22:17:46 +00:00
private ICommand _visitNexusSiteCommand;
2019-09-26 03:18:36 +00:00
public ICommand VisitNexusSiteCommand
{
get
{
if (_visitNexusSiteCommand == null) _visitNexusSiteCommand = new LambdaCommand(() => true, () => VisitNexusSite());
return _visitNexusSiteCommand;
}
}
public string _nexusSiteURL = null;
2019-09-26 03:18:36 +00:00
private void VisitNexusSite()
{
if (_nexusSiteURL != null && _nexusSiteURL.StartsWith("https://"))
{
Process.Start(_nexusSiteURL);
}
}
private bool _uiReady = false;
public bool UIReady
{
get => _uiReady;
set => this.RaiseAndSetIfChanged(ref _uiReady, value);
}
2019-09-26 03:18:36 +00:00
private BitmapImage _wabbajackLogo = null;
private BitmapImage _splashScreenImage = null;
public BitmapImage SplashScreenImage
{
get => _splashScreenImage;
set
{
_splashScreenImage = value;
RaisePropertyChanged();
2019-09-26 03:18:36 +00:00
}
}
public string _splashScreenModName = "Wabbajack";
public string SplashScreenModName
{
get => _splashScreenModName;
set => this.RaiseAndSetIfChanged(ref _splashScreenModName, value);
2019-09-26 03:18:36 +00:00
}
public string _splashScreenAuthorName = "Halgari & the Wabbajack Team";
public string SplashScreenAuthorName
{
get => _splashScreenAuthorName;
set => this.RaiseAndSetIfChanged(ref _splashScreenAuthorName, value);
2019-09-26 03:18:36 +00:00
}
private string _modListPath;
public string _splashScreenSummary = "";
2019-09-26 03:18:36 +00:00
public string SplashScreenSummary
{
get => _splashScreenSummary;
set => this.RaiseAndSetIfChanged(ref _splashScreenSummary, value);
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;
}
else if (Mode == "Building" && Location != null && Directory.Exists(Location) && File.Exists(Path.Combine(Location, "modlist.txt")))
{
Location = Path.Combine(Location, "modlist.txt");
validationMessage = null;
ConfigureForBuild();
}
else if (Mode == "Installing" && Location != null && Directory.Exists(Location) && !Directory.EnumerateFileSystemEntries(Location).Any())
{
validationMessage = null;
}
else if (Mode == "Installing" && Location != null && Directory.Exists(Location) && Directory.EnumerateFileSystemEntries(Location).Any())
{
validationMessage = "You have selected a non-empty directory. Installing the modlist here might result in a broken install!";
}
else
{
validationMessage = "Invalid Mod Organizer profile directory";
}
break;
}
return validationMessage;
2019-07-22 22:17:46 +00:00
}
private void UpdateLoop()
{
2019-09-27 04:07:54 +00:00
while (Running)
2019-07-22 22:17:46 +00:00
{
if (Dirty)
lock (InternalStatus)
{
var data = InternalStatus.ToArray();
dispatcher.Invoke(() =>
{
2019-09-14 04:35:42 +00:00
for (var idx = 0; idx < data.Length; idx += 1)
2019-07-22 22:17:46 +00:00
if (idx >= Status.Count)
Status.Add(data[idx]);
else if (Status[idx] != data[idx])
Status[idx] = data[idx];
});
Dirty = false;
}
2019-09-14 04:35:42 +00:00
2019-09-26 03:18:36 +00:00
if (SlideShowElements.Any())
{
if (DateTime.Now - _lastSlideShowUpdate > TimeSpan.FromSeconds(10))
{
var idx = _random.Next(0, SlideShowElements.Count);
try
2019-09-26 03:18:36 +00:00
{
var element = SlideShowElements[idx];
var data = new MemoryStream();
using (var stream = new HttpClient().GetStreamSync("asdas" + element.ImageURL))
stream.CopyTo(data);
data.Seek(0, SeekOrigin.Begin);
dispatcher.Invoke(() =>
2019-10-01 23:57:42 +00:00
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = data;
bitmap.EndInit();
2019-10-01 23:57:42 +00:00
SplashScreenImage = bitmap;
2019-10-01 23:57:42 +00:00
SplashScreenModName = element.ModName;
SplashScreenAuthorName = element.AuthorName;
SplashScreenSummary = element.ModSummary;
_nexusSiteURL = element.ModURL;
_lastSlideShowUpdate = DateTime.Now;
});
}
catch (Exception ex)
{
}
2019-09-26 03:18:36 +00:00
}
}
2019-07-23 04:27:26 +00:00
Thread.Sleep(1000);
2019-07-22 22:17:46 +00:00
}
}
2019-09-27 04:07:54 +00:00
public bool Running { get; set; } = true;
internal void ConfigureForInstall(string source, ModList modlist)
2019-07-31 03:59:19 +00:00
{
_modList = modlist;
_modListPath = source;
2019-07-31 03:59:19 +00:00
Mode = "Installing";
ModListName = _modList.Name;
2019-08-30 23:57:56 +00:00
HTMLReport = _modList.ReportHTML;
2019-07-31 03:59:19 +00:00
Location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
2019-09-26 03:18:36 +00:00
SlideShowElements = modlist.Archives.OfType<NexusMod>().Select(m => new SlideShowItem
2019-09-26 03:18:36 +00:00
{
ModName = NexusApiUtils.FixupSummary(m.ModName),
AuthorName = NexusApiUtils.FixupSummary(m.Author),
ModSummary = NexusApiUtils.FixupSummary(m.Summary),
2019-09-26 03:18:36 +00:00
ImageURL = m.SlideShowPic,
ModURL = m.NexusURL,
}).ToList();
2019-07-31 03:59:19 +00:00
}
2019-07-22 22:17:46 +00:00
public void LogMsg(string msg)
{
dispatcher.Invoke(() => Log.Add(msg));
}
public void SetProgress(int id, string msg, int progress)
{
lock (InternalStatus)
{
Dirty = true;
2019-09-14 04:35:42 +00:00
while (id >= InternalStatus.Count) InternalStatus.Add(new CPUStatus());
2019-07-22 22:17:46 +00:00
InternalStatus[id] = new CPUStatus { ID = id, Msg = msg, Progress = progress };
2019-07-22 22:17:46 +00:00
}
}
2019-07-31 03:59:19 +00:00
2019-08-02 23:04:04 +00:00
public void SetQueueSize(int max, int current)
{
if (max == 0)
max = 1;
2019-08-02 23:04:04 +00:00
var total = current * 100 / max;
QueueProgress = total;
}
2019-07-31 03:59:19 +00:00
private void ExecuteChangePath()
{
if (Mode == "Installing")
{
var folder = UIUtils.ShowFolderSelectionDialog("Select Installation directory");
2019-09-18 02:17:12 +00:00
if (folder != null)
2019-07-31 03:59:19 +00:00
{
2019-09-18 02:17:12 +00:00
Location = folder;
if (DownloadLocation == null)
2019-09-04 21:19:37 +00:00
DownloadLocation = Path.Combine(Location, "downloads");
2019-07-31 03:59:19 +00:00
}
}
else
{
var folder = UIUtils.ShowFolderSelectionDialog("Select Your MO2 profile directory");
Location = folder;
2019-07-31 03:59:19 +00:00
}
}
private void ExecuteChangeDownloadPath()
{
var folder = UIUtils.ShowFolderSelectionDialog("Select a location for MO2 downloads");
2019-09-18 02:17:12 +00:00
if (folder != null) DownloadLocation = folder;
}
2019-07-31 03:59:19 +00:00
private void ConfigureForBuild()
{
var profile_folder = Path.GetDirectoryName(Location);
var mo2folder = Path.GetDirectoryName(Path.GetDirectoryName(profile_folder));
if (!File.Exists(Path.Combine(mo2folder, "ModOrganizer.exe")))
LogMsg($"Error! No ModOrganizer2.exe found in {mo2folder}");
var profile_name = Path.GetFileName(profile_folder);
ModListName = profile_name;
Mode = "Building";
2019-09-26 22:32:15 +00:00
var tmp_compiler = new Compiler(mo2folder);
DownloadLocation = tmp_compiler.MO2DownloadsFolder;
2019-07-31 03:59:19 +00:00
_mo2Folder = mo2folder;
}
2019-08-30 23:57:56 +00:00
private void ShowReport()
{
var file = Path.GetTempFileName() + ".html";
File.WriteAllText(file, HTMLReport);
Process.Start(file);
}
2019-07-31 03:59:19 +00:00
private void ExecuteBegin()
{
UIReady = false;
2019-07-31 03:59:19 +00:00
if (Mode == "Installing")
{
var installer = new Installer(_modListPath, _modList, Location);
2019-09-26 03:18:36 +00:00
installer.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;
LogMsg(ex.StackTrace);
2019-08-22 23:29:44 +00:00
LogMsg(ex.ToString());
LogMsg($"{ex.Message} - Can't continue");
2019-07-31 03:59:19 +00:00
}
finally
{
UIReady = true;
}
2019-07-31 03:59:19 +00:00
});
th.Priority = ThreadPriority.BelowNormal;
th.Start();
}
else if (_mo2Folder != null)
2019-07-31 03:59:19 +00:00
{
2019-09-26 22:32:15 +00:00
var compiler = new Compiler(_mo2Folder);
2019-07-31 03:59:19 +00:00
compiler.MO2Profile = ModListName;
var th = new Thread(() =>
{
UIReady = false;
try
{
compiler.Compile();
2019-08-30 23:57:56 +00:00
if (compiler.ModList != null && compiler.ModList.ReportHTML != null)
HTMLReport = compiler.ModList.ReportHTML;
}
catch (Exception ex)
{
while (ex.InnerException != null) ex = ex.InnerException;
LogMsg(ex.StackTrace);
2019-08-22 23:29:44 +00:00
LogMsg(ex.ToString());
LogMsg($"{ex.Message} - Can't continue");
}
finally
{
UIReady = true;
}
2019-07-31 03:59:19 +00:00
});
th.Priority = ThreadPriority.BelowNormal;
th.Start();
}
else
{
Utils.Log("Cannot compile modlist: no valid Mod Organizer profile directory selected.");
UIReady = true;
}
2019-07-31 03:59:19 +00:00
}
}
2019-09-14 04:35:42 +00:00
public class CPUStatus
{
public int Progress { get; internal set; }
public string Msg { get; internal set; }
public int ID { get; internal set; }
}
2019-07-22 22:17:46 +00:00
}