2022-03-13 22:47:30 +00:00
using System ;
2022-05-19 21:47:15 +00:00
using System.Collections.ObjectModel ;
2022-03-13 22:47:30 +00:00
using System.ComponentModel ;
2022-05-15 22:38:22 +00:00
using System.Linq ;
2022-03-13 22:47:30 +00:00
using System.Reactive.Linq ;
2023-05-07 20:32:18 +00:00
using System.Threading ;
2022-03-13 22:47:30 +00:00
using System.Windows ;
2022-03-13 23:08:27 +00:00
using System.Windows.Input ;
2022-05-19 21:47:15 +00:00
using DynamicData ;
using DynamicData.Binding ;
2022-03-13 22:47:30 +00:00
using MahApps.Metro.Controls ;
using Microsoft.Extensions.Logging ;
using ReactiveUI ;
2022-05-19 21:47:15 +00:00
using ReactiveUI.Fody.Helpers ;
2022-03-13 22:47:30 +00:00
using Wabbajack.Common ;
2022-05-19 21:47:15 +00:00
using Wabbajack.DTOs ;
using Wabbajack.DTOs.DownloadStates ;
using Wabbajack.DTOs.Interventions ;
2022-03-13 22:47:30 +00:00
using Wabbajack.Messages ;
using Wabbajack.Paths.IO ;
2022-05-19 21:47:15 +00:00
using Wabbajack.UserIntervention ;
2022-03-13 22:47:30 +00:00
using Wabbajack.Util ;
2022-03-14 12:40:12 +00:00
using Wabbajack.Views ;
2022-03-13 22:47:30 +00:00
namespace Wabbajack
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : MetroWindow
{
private MainWindowVM _mwvm ;
private MainSettings _settings ;
private readonly ILogger < MainWindow > _logger ;
private readonly SystemParametersConstructor _systemParams ;
2022-05-19 21:47:15 +00:00
private ObservableCollection < ViewModel > TabVMs = new ObservableCollectionExtended < ViewModel > ( ) ;
2022-03-13 22:47:30 +00:00
public MainWindow ( ILogger < MainWindow > logger , SystemParametersConstructor systemParams , LauncherUpdater updater , MainWindowVM vm )
{
InitializeComponent ( ) ;
_mwvm = vm ;
2022-05-20 03:23:16 +00:00
DataContext = vm ;
2022-03-13 22:47:30 +00:00
_logger = logger ;
_systemParams = systemParams ;
try
{
// Wire any unhandled crashing exceptions to log before exiting
AppDomain . CurrentDomain . UnhandledException + = ( sender , e ) = >
{
// Don't do any special logging side effects
_logger . LogError ( ( Exception ) e . ExceptionObject , "Uncaught error" ) ;
Environment . Exit ( - 1 ) ;
} ;
2022-08-18 23:02:19 +00:00
Closed + = ( s , e ) = >
{
2023-05-07 20:32:18 +00:00
_mwvm . CancelRunningTasks ( TimeSpan . FromSeconds ( 10 ) ) ;
Application . Current . Shutdown ( ) ;
2022-08-18 23:02:19 +00:00
} ;
2022-03-13 22:47:30 +00:00
MessageBus . Current . Listen < TaskBarUpdate > ( )
2022-05-19 03:21:38 +00:00
. ObserveOnGuiThread ( )
2022-03-13 22:47:30 +00:00
. Subscribe ( u = >
{
2022-10-08 04:06:17 +00:00
TaskbarItemInfoControl . Description = u . Description ;
TaskbarItemInfoControl . ProgressValue = u . ProgressValue ;
TaskbarItemInfoControl . ProgressState = u . State ;
2022-03-13 22:47:30 +00:00
} ) ;
2022-05-15 22:38:22 +00:00
2022-03-13 22:47:30 +00:00
_logger . LogInformation ( "Wabbajack Build - {Sha}" , ThisAssembly . Git . Sha ) ;
_logger . LogInformation ( "Running in {EntryPoint}" , KnownFolders . EntryPoint ) ;
var p = _systemParams . Create ( ) ;
_logger . LogInformation ( "Detected Windows Version: {Version}" , Environment . OSVersion . VersionString ) ;
_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 ) ;
if ( p . SystemPageSize = = 0 )
2022-10-08 04:06:17 +00:00
_logger . LogInformation ( "Pagefile is disabled! Consider increasing to 20000MB. A disabled pagefile can cause crashes and poor in-game performance" ) ;
2022-03-13 22:47:30 +00:00
else if ( p . SystemPageSize < 2e+10 )
_logger . LogInformation ( "Pagefile below recommended! Consider increasing to 20000MB. A suboptimal pagefile can cause crashes and poor in-game performance" ) ;
//Warmup();
var _ = updater . Run ( ) ;
var ( settings , loadedSettings ) = MainSettings . TryLoadTypicalSettings ( ) . AsTask ( ) . Result ;
// Load settings
/ *
if ( CLIArguments . NoSettings | | ! loadedSettings )
{
_settings = new MainSettings { Version = Consts . SettingsVersion } ;
RunWhenLoaded ( DefaultSettings ) ;
}
else
{
_settings = settings ;
RunWhenLoaded ( LoadSettings ) ;
} * /
// 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 ;
} ;
2022-05-20 03:23:16 +00:00
2022-05-15 22:38:22 +00:00
( ( MainWindowVM ) DataContext ) . WhenAnyValue ( vm = > vm . OpenSettingsCommand )
. BindTo ( this , view = > view . SettingsButton . Command ) ;
2023-07-16 21:24:02 +00:00
( ( MainWindowVM ) DataContext ) . WhenAnyValue ( vm = > vm . Installer . InstallState )
. ObserveOn ( RxApp . MainThreadScheduler )
. Select ( v = > v = = InstallState . Installing ? Visibility . Collapsed : Visibility . Visible )
. BindTo ( this , view = > view . SettingsButton . Visibility ) ;
2022-03-13 22:47:30 +00:00
}
catch ( Exception ex )
{
_logger . LogError ( ex , "During Main Window Startup" ) ;
Environment . Exit ( - 1 ) ;
}
vm . WhenAnyValue ( vm = > vm . ResourceStatus )
2022-05-20 22:33:09 +00:00
. BindToStrict ( this , view = > view . ResourceUsage . Text ) ;
vm . WhenAnyValue ( vm = > vm . AppName )
. BindToStrict ( this , view = > view . AppName . Text ) ;
2022-03-13 22:47:30 +00:00
}
public void Init ( MainWindowVM vm , MainSettings settings )
{
DataContext = vm ;
_mwvm = vm ;
_settings = settings ;
}
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 ( ) ;
}
2022-03-13 23:08:27 +00:00
private void UIElement_OnMouseDown ( object sender , MouseButtonEventArgs e )
{
this . DragMove ( ) ;
}
2022-05-20 03:24:57 +00:00
2022-03-13 22:47:30 +00:00
}
}