2019-11-02 20:55:14 +00:00
using System ;
2020-02-08 04:35:08 +00:00
using System.Collections.Generic ;
2019-08-04 22:08:03 +00:00
using System.ComponentModel ;
2019-12-18 03:18:33 +00:00
using System.Threading.Tasks ;
2019-07-22 22:17:46 +00:00
using System.Windows ;
2019-11-25 05:43:56 +00:00
using MahApps.Metro.Controls ;
2020-02-08 04:35:08 +00:00
using Newtonsoft.Json ;
2019-07-22 22:17:46 +00:00
using Wabbajack.Common ;
2020-04-28 22:52:20 +00:00
using Wabbajack.Common.StoreHandlers ;
2021-03-11 15:00:26 +00:00
using Wabbajack.Lib ;
2019-12-18 03:18:33 +00:00
using Wabbajack.Lib.LibCefHelpers ;
2020-03-10 04:11:11 +00:00
using Wabbajack.Util ;
2019-09-27 04:07:54 +00:00
using Application = System . Windows . Application ;
2019-12-14 21:40:10 +00:00
using Utils = Wabbajack . Common . Utils ;
2019-07-22 22:17:46 +00:00
namespace Wabbajack
{
/// <summary>
2019-09-14 04:35:42 +00:00
/// Interaction logic for MainWindow.xaml
2019-07-22 22:17:46 +00:00
/// </summary>
2019-11-25 05:43:56 +00:00
public partial class MainWindow : MetroWindow
2019-07-22 22:17:46 +00:00
{
2019-10-23 04:15:42 +00:00
private MainWindowVM _mwvm ;
2019-11-06 03:22:38 +00:00
private MainSettings _settings ;
2019-09-14 04:35:42 +00:00
2019-11-02 20:55:14 +00:00
public MainWindow ( )
{
2020-06-08 21:59:16 +00:00
try
2019-12-01 22:20:26 +00:00
{
2020-06-08 21:59:16 +00:00
// Wire any unhandled crashing exceptions to log before exiting
AppDomain . CurrentDomain . UnhandledException + = ( sender , e ) = >
{
// Don't do any special logging side effects
Utils . LogStraightToFile ( "Error." ) ;
Utils . LogStraightToFile ( ( ( Exception ) e . ExceptionObject ) . ToString ( ) ) ;
Environment . Exit ( - 1 ) ;
} ;
Utils . Log ( $"Wabbajack Build - {ThisAssembly.Git.Sha}" ) ;
Utils . Log ( $"Running in {AbsolutePath.EntryPoint}" ) ;
var p = SystemParametersConstructor . Create ( ) ;
2019-12-01 22:20:26 +00:00
2020-06-08 21:59:16 +00:00
Utils . Log ( $"Detected Windows Version: {p.WindowsVersion}" ) ;
2021-07-04 22:33:26 +00:00
2020-03-19 02:10:55 +00:00
2021-07-04 22:33:26 +00:00
if ( ! ( p . WindowsVersion > Consts . MaximumUnsupportedWindowsVersion ) )
2020-06-08 21:59:16 +00:00
Utils . Log (
2021-07-04 22:33:26 +00:00
$"You are not running a recent version of Windows ({p.WindowsVersion}), Wabbajack is only supported on Windows versions greater than {Consts.MaximumUnsupportedWindowsVersion}." ) ;
2020-03-19 02:10:55 +00:00
Utils . Log (
2021-01-11 06:59:32 +00:00
$"System settings - ({p.SystemMemorySize.ToFileSizeString()} RAM) ({p.SystemPageSize.ToFileSizeString()} Page), Display: {p.ScreenWidth} x {p.ScreenHeight} ({p.VideoMemorySize.ToFileSizeString()} VRAM - VideoMemorySizeMb={p.EnbLEVRAMSize})" ) ;
if ( p . SystemPageSize = = 0 )
Utils . Log ( "Pagefile is disabled! Consider increasing to 20000MB. A disabled pagefile can cause crashes and poor in-game performance." ) ;
else if ( p . SystemPageSize < 2e+10 )
Utils . Log ( "Pagefile below recommended! Consider increasing to 20000MB. A suboptimal pagefile can cause crashes and poor in-game performance." ) ;
2019-12-18 03:18:33 +00:00
2020-06-08 21:59:16 +00:00
Warmup ( ) ;
2021-03-11 15:00:26 +00:00
var _ = LauncherUpdater . Run ( ) ;
2019-12-01 22:20:26 +00:00
2020-06-08 21:59:16 +00:00
var ( settings , loadedSettings ) = MainSettings . TryLoadTypicalSettings ( ) . AsTask ( ) . Result ;
// Load settings
if ( CLIArguments . NoSettings | | ! loadedSettings )
{
_settings = new MainSettings { Version = Consts . SettingsVersion } ;
RunWhenLoaded ( DefaultSettings ) ;
}
else
2020-03-04 11:21:44 +00:00
{
2020-06-08 21:59:16 +00:00
_settings = settings ;
RunWhenLoaded ( LoadSettings ) ;
}
// 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 ;
2020-03-04 11:21:44 +00:00
} ;
2019-12-01 22:20:26 +00:00
}
2020-06-08 21:59:16 +00:00
catch ( Exception ex )
2019-12-01 22:20:26 +00:00
{
2020-06-08 21:59:16 +00:00
Utils . LogStraightToFile ( "Error" ) ;
2021-01-10 06:04:05 +00:00
Utils . LogStraightToFile ( ex . ToString ( ) ) ;
2020-06-08 21:59:16 +00:00
Environment . Exit ( - 1 ) ;
2019-12-01 22:20:26 +00:00
}
}
2019-11-30 00:25:39 +00:00
2019-12-01 22:20:26 +00:00
public void Init ( MainWindowVM vm , MainSettings settings )
{
DataContext = vm ;
_mwvm = vm ;
_settings = settings ;
}
2020-04-28 22:52:20 +00:00
/// <summary>
/// Starts some background initialization tasks spinning so they're already prepped when actually needed
/// </summary>
private void Warmup ( )
{
TempFolder . Warmup ( ) ;
// ToDo
// Currently this is a blocking call. Perhaps upgrade to be run in a background task.
// Would first need to ensure users of CEF properly await the background initialization before use
Helpers . Init ( ) ;
StoreHandler . Warmup ( ) ;
Task . Run ( AssociateListsWithWabbajack ) . FireAndForget ( ) ;
}
/// <summary>
/// Run logic to associate wabbajack lists with this app in the background
/// </summary>
private void AssociateListsWithWabbajack ( )
{
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}" ) ;
}
}
2019-12-01 22:20:26 +00:00
private void RunWhenLoaded ( Action a )
{
if ( IsLoaded )
2019-11-30 00:25:39 +00:00
{
2019-12-01 22:20:26 +00:00
a ( ) ;
}
else
{
this . Loaded + = ( sender , e ) = >
{
a ( ) ;
} ;
}
2019-07-22 22:17:46 +00:00
}
2019-08-04 22:08:03 +00:00
2019-12-01 22:20:26 +00:00
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 ;
}
2019-09-27 04:07:54 +00:00
2019-08-04 22:08:03 +00:00
private void Window_Closing ( object sender , CancelEventArgs e )
{
2020-08-08 12:33:36 +00:00
_mwvm . ShutdownApplication ( ) . Wait ( ) ;
2019-08-04 22:08:03 +00:00
}
2019-07-22 22:17:46 +00:00
}
2019-11-21 15:04:33 +00:00
}