wabbajack/Wabbajack/AppState.cs

553 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;
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 : INotifyPropertyChanged, IDataErrorInfo
2019-07-22 22:17:46 +00:00
{
2019-09-14 04:35:42 +00:00
private ICommand _begin;
2019-07-22 22:17:46 +00:00
2019-09-14 04:35:42 +00:00
private ICommand _changeDownloadPath;
2019-07-22 22:17:46 +00:00
2019-09-14 04:35:42 +00:00
private ICommand _changePath;
private string _downloadLocation;
2019-07-22 22:17:46 +00:00
2019-09-14 04:35:42 +00:00
private string _htmlReport;
2019-07-30 21:45:04 +00:00
2019-09-14 04:35:42 +00:00
private bool _ignoreMissingFiles;
private string _location;
private string _mo2Folder;
private string _mode;
private ModList _modList;
private string _modListName;
private int _queueProgress;
private ICommand _showReportCommand;
2019-09-26 03:18:36 +00:00
private ICommand _visitNexusSiteCommand;
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(
2019-09-24 15:26:44 +00:00
"This app seems to be running inside a folder called `Downloads`, such folders are often highly monitored by antivirus software and they can often " +
2019-09-14 04:35:42 +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`",
MessageBoxButton.OK,
MessageBoxImage.Error);
Environment.Exit(1);
}
_startTime = DateTime.Now;
LogFile = Assembly.GetExecutingAssembly().Location + ".log";
if (LogFile.FileExists())
File.Delete(LogFile);
Mode = mode;
Dirty = false;
dispatcher = d;
Log = new ObservableCollection<string>();
Status = new ObservableCollection<CPUStatus>();
InternalStatus = new List<CPUStatus>();
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();
2019-07-22 22:17:46 +00:00
public ObservableCollection<string> Log { get; }
public ObservableCollection<CPUStatus> Status { get; }
2019-07-30 21:45:04 +00:00
public string Mode
{
2019-09-14 04:35:42 +00:00
get => _mode;
2019-07-30 21:45:04 +00:00
set
{
_mode = value;
OnPropertyChanged("Mode");
}
}
public string ModListName
{
2019-09-14 04:35:42 +00:00
get => _modListName;
2019-07-30 21:45:04 +00:00
set
{
_modListName = value;
OnPropertyChanged("ModListName");
}
}
2019-07-31 03:59:19 +00:00
public string Location
{
2019-09-14 04:35:42 +00:00
get => _location;
2019-07-31 03:59:19 +00:00
set
{
_location = value;
OnPropertyChanged("Location");
}
}
2019-07-31 03:59:19 +00:00
public string DownloadLocation
{
2019-09-14 04:35:42 +00:00
get => _downloadLocation;
set
{
_downloadLocation = value;
OnPropertyChanged("DownloadLocation");
}
}
2019-08-30 23:57:56 +00:00
public Visibility ShowReportButton => _htmlReport == null ? Visibility.Collapsed : Visibility.Visible;
public string HTMLReport
{
2019-09-14 04:35:42 +00:00
get => _htmlReport;
2019-08-30 23:57:56 +00:00
set
{
_htmlReport = value;
OnPropertyChanged("HTMLReport");
OnPropertyChanged("ShowReportButton");
}
}
2019-08-02 23:04:04 +00:00
public int QueueProgress
{
2019-09-14 04:35:42 +00:00
get => _queueProgress;
2019-08-02 23:04:04 +00:00
set
{
if (value != _queueProgress)
{
_queueProgress = value;
OnPropertyChanged("QueueProgress");
}
}
}
2019-07-22 22:17:46 +00:00
private List<CPUStatus> InternalStatus { get; }
2019-09-14 04:35:42 +00:00
public string LogFile { get; }
2019-07-22 22:17:46 +00:00
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
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
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
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
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
{
_uiReady = value;
OnPropertyChanged("UIReady");
}
}
2019-09-26 03:18:36 +00:00
private BitmapImage _wabbajackLogo = null;
private BitmapImage _splashScreenImage = null;
public BitmapImage SplashScreenImage
{
get => _splashScreenImage;
set
{
_splashScreenImage = value;
OnPropertyChanged("SplashScreenImage");
}
}
public string _splashScreenModName = "Wabbajack";
public string SplashScreenModName
{
get => _splashScreenModName;
set
{
_splashScreenModName = value;
OnPropertyChanged("SplashScreenModName");
}
}
public string _splashScreenAuthorName = "Halgari & the Wabbajack Team";
public string SplashScreenAuthorName
{
get => _splashScreenAuthorName;
set
{
_splashScreenAuthorName = value;
OnPropertyChanged("SplashScreenAuthorName");
}
}
public string _splashScreenSummary = "";
public string SplashScreenSummary
{
get => _splashScreenSummary;
set
{
_splashScreenSummary = value;
OnPropertyChanged("SplashScreenSummary");
}
}
2019-09-14 04:35:42 +00:00
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
public string Error
{
get { return "Error"; }
}
public string this[string columnName]
{
get
{
return Validate(columnName);
}
}
private string Validate(string columnName)
{
string validationMessage = null;
switch (columnName)
{
case "Location":
if (Location == null)
{
validationMessage = null;
}
else if (Location != null && Directory.Exists(Location) && File.Exists(Path.Combine(Location, "modlist.txt")))
{
Location = Path.Combine(Location, "modlist.txt");
validationMessage = null;
ConfigureForBuild();
}
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))
{
dispatcher.Invoke(() =>
{
var element = SlideShowElements[_random.Next(0, SlideShowElements.Count)];
SplashScreenImage = new BitmapImage(new Uri(element.ImageURL));
SplashScreenModName = element.ModName;
SplashScreenAuthorName = element.AuthorName;
SplashScreenSummary = element.ModSummary;
_nexusSiteURL = element.ModURL;
_lastSlideShowUpdate = DateTime.Now;
});
}
}
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(ModList modlist)
2019-07-31 03:59:19 +00:00
{
_modList = modlist;
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)
{
msg = $"{(DateTime.Now - _startTime).TotalSeconds:0.##} - {msg}";
2019-07-22 22:17:46 +00:00
dispatcher.Invoke(() => Log.Add(msg));
2019-09-14 04:35:42 +00:00
lock (dispatcher)
{
2019-07-22 22:17:46 +00:00
File.AppendAllText(LogFile, msg + "\r\n");
}
}
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
2019-09-14 04:35:42 +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;
2019-09-04 21:19:37 +00:00
if (_downloadLocation == null)
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")
{
2019-09-26 22:32:15 +00:00
var installer = new Installer(_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
2019-09-26 03:18:36 +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
}