wabbajack/Wabbajack.App.Wpf/Views/MainWindow.xaml.cs

168 lines
5.6 KiB
C#
Raw Normal View History

2021-12-26 21:56:44 +00:00
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using MahApps.Metro.Controls;
2021-12-28 19:18:37 +00:00
using Microsoft.Extensions.Logging;
2021-12-26 21:56:44 +00:00
using Wabbajack.Common;
using Wabbajack.Lib;
using Wabbajack.Lib.LibCefHelpers;
2021-12-28 19:18:37 +00:00
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
2021-12-26 21:56:44 +00:00
using Wabbajack.Util;
namespace Wabbajack
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : MetroWindow
{
private MainWindowVM _mwvm;
private MainSettings _settings;
2021-12-28 19:18:37 +00:00
private readonly ILogger<MainWindow> _logger;
private readonly SystemParametersConstructor _systemParams;
2021-12-26 21:56:44 +00:00
2021-12-28 19:18:37 +00:00
public MainWindow(ILogger<MainWindow> logger, SystemParametersConstructor systemParams, LauncherUpdater updater)
2021-12-26 21:56:44 +00:00
{
2021-12-28 19:18:37 +00:00
_logger = logger;
_systemParams = systemParams;
2021-12-26 21:56:44 +00:00
try
{
// Wire any unhandled crashing exceptions to log before exiting
AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
// Don't do any special logging side effects
2021-12-28 19:18:37 +00:00
_logger.LogError((Exception)e.ExceptionObject, "Uncaught error");
2021-12-26 21:56:44 +00:00
Environment.Exit(-1);
};
2021-12-28 19:18:37 +00:00
_logger.LogInformation("Wabbajack Build - {Sha}",ThisAssembly.Git.Sha);
_logger.LogInformation("Running in {EntryPoint}", KnownFolders.EntryPoint);
2021-12-26 21:56:44 +00:00
2021-12-28 19:18:37 +00:00
var p = _systemParams.Create();
2021-12-26 21:56:44 +00:00
2021-12-28 19:18:37 +00:00
_logger.LogInformation("Detected Windows Version: {Version}", Environment.OSVersion.VersionString);
2021-12-26 21:56:44 +00:00
2021-12-28 19:18:37 +00:00
_logger.LogInformation(
"System settings - ({MemorySize} RAM) ({PageSize} Page), Display: {ScreenWidth} x {ScreenHeight} ({Vram} VRAM - VideoMemorySizeMb={ENBVRam})",
p.SystemMemorySize.ToFileSizeString(), p.SystemPageSize.ToFileSizeString(), p.ScreenWidth, p.ScreenHeight, p.VideoMemorySize.ToFileSizeString(), p.EnbLEVRAMSize);
2021-12-26 21:56:44 +00:00
if (p.SystemPageSize == 0)
2021-12-28 19:18:37 +00:00
_logger.LogInformation("Pagefile is disabled! Consider increasing to 20000MB. A disabled pagefile can cause crashes and poor in-game performance");
2021-12-26 21:56:44 +00:00
else if (p.SystemPageSize < 2e+10)
2021-12-28 19:18:37 +00:00
_logger.LogInformation("Pagefile below recommended! Consider increasing to 20000MB. A suboptimal pagefile can cause crashes and poor in-game performance");
2021-12-26 21:56:44 +00:00
Warmup();
2021-12-28 19:18:37 +00:00
var _ = updater.Run();
2021-12-26 21:56:44 +00:00
var (settings, loadedSettings) = MainSettings.TryLoadTypicalSettings().AsTask().Result;
// Load settings
2021-12-28 19:18:37 +00:00
/*
2021-12-26 21:56:44 +00:00
if (CLIArguments.NoSettings || !loadedSettings)
{
_settings = new MainSettings {Version = Consts.SettingsVersion};
RunWhenLoaded(DefaultSettings);
}
else
{
_settings = settings;
RunWhenLoaded(LoadSettings);
2021-12-28 19:18:37 +00:00
}*/
2021-12-26 21:56:44 +00:00
// Set datacontext
_mwvm = new MainWindowVM(this, _settings);
DataContext = _mwvm;
// Bring window to the front if it isn't already
this.Initialized += (s, e) =>
{
this.Activate();
this.Topmost = true;
this.Focus();
};
this.ContentRendered += (s, e) =>
{
this.Topmost = false;
};
}
catch (Exception ex)
{
2021-12-28 19:18:37 +00:00
_logger.LogError(ex, "During Main Window Startup");
2021-12-26 21:56:44 +00:00
Environment.Exit(-1);
}
}
public void Init(MainWindowVM vm, MainSettings settings)
{
DataContext = vm;
_mwvm = vm;
_settings = settings;
}
/// <summary>
/// Starts some background initialization tasks spinning so they're already prepped when actually needed
/// </summary>
private void Warmup()
{
Task.Run(AssociateListsWithWabbajack).FireAndForget();
}
/// <summary>
/// Run logic to associate wabbajack lists with this app in the background
/// </summary>
private void AssociateListsWithWabbajack()
{
2021-12-28 19:18:37 +00:00
/* TODO
2021-12-26 21:56:44 +00:00
var appPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
try
{
if (!ModListAssociationManager.IsAssociated() || ModListAssociationManager.NeedsUpdating(appPath))
{
ModListAssociationManager.Associate(appPath);
}
}
catch (Exception e)
{
Utils.Log($"ExtensionManager had an exception:\n{e}");
2021-12-28 19:18:37 +00:00
}*/
2021-12-26 21:56:44 +00:00
}
private void RunWhenLoaded(Action a)
{
if (IsLoaded)
{
a();
}
else
{
this.Loaded += (sender, e) =>
{
a();
};
}
}
private void LoadSettings()
{
Width = _settings.Width;
Height = _settings.Height;
Left = _settings.PosX;
Top = _settings.PosY;
}
private void DefaultSettings()
{
Width = 1300;
Height = 960;
Left = 15;
Top = 15;
}
private void Window_Closing(object sender, CancelEventArgs e)
{
_mwvm.ShutdownApplication().Wait();
}
}
}