2022-03-13 22:47:30 +00:00
using System ;
using System.Reactive.Concurrency ;
using System.Reactive.Disposables ;
2022-11-05 19:35:48 +00:00
using System.Runtime.InteropServices ;
using System.Security.Principal ;
2022-10-14 22:46:38 +00:00
using System.Threading.Tasks ;
2022-03-13 22:47:30 +00:00
using System.Windows ;
using System.Windows.Threading ;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.Extensions.Hosting ;
using Microsoft.Extensions.Logging ;
2022-05-22 04:27:02 +00:00
using NLog.Extensions.Logging ;
using NLog.Targets ;
2022-10-16 12:00:50 +00:00
using Orc.FileAssociation ;
2022-03-13 22:47:30 +00:00
using ReactiveUI ;
2022-10-14 22:46:38 +00:00
using Wabbajack.CLI.Builder ;
2022-03-13 22:47:30 +00:00
using Wabbajack.DTOs ;
2022-05-16 22:14:52 +00:00
using Wabbajack.DTOs.Interventions ;
using Wabbajack.Interventions ;
2022-03-13 22:47:30 +00:00
using Wabbajack.LoginManagers ;
using Wabbajack.Models ;
2022-10-16 12:17:30 +00:00
using Wabbajack.Paths ;
2022-10-01 13:02:47 +00:00
using Wabbajack.Paths.IO ;
2022-03-13 22:47:30 +00:00
using Wabbajack.Services.OSIntegrated ;
using Wabbajack.UserIntervention ;
using Wabbajack.Util ;
2022-10-16 12:17:30 +00:00
using Ext = Wabbajack . Common . Ext ;
2022-03-13 22:47:30 +00:00
namespace Wabbajack
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
2022-10-14 21:11:09 +00:00
private IHost _host ;
2022-03-13 22:47:30 +00:00
2022-10-14 21:11:09 +00:00
private void OnStartup ( object sender , StartupEventArgs e )
2022-03-13 22:47:30 +00:00
{
2022-11-05 19:35:48 +00:00
if ( IsAdmin ( ) )
{
var messageBox = MessageBox . Show ( "Don't run Wabbajack as Admin!" , "Error" , MessageBoxButton . OK , MessageBoxImage . Error , MessageBoxResult . OK , MessageBoxOptions . DefaultDesktopOnly ) ;
if ( messageBox = = MessageBoxResult . OK )
{
Environment . Exit ( 1 ) ;
}
else
{
Environment . Exit ( 1 ) ;
}
}
2022-05-23 13:07:05 +00:00
RxApp . MainThreadScheduler = new DispatcherScheduler ( Dispatcher . CurrentDispatcher ) ;
2022-10-14 22:46:38 +00:00
_host = Host . CreateDefaultBuilder ( Array . Empty < string > ( ) )
2022-05-22 04:27:02 +00:00
. ConfigureLogging ( AddLogging )
2022-03-13 22:47:30 +00:00
. ConfigureServices ( ( host , services ) = >
{
ConfigureServices ( services ) ;
} )
. Build ( ) ;
2022-10-14 22:46:38 +00:00
var args = e . Args ;
2022-10-14 21:11:09 +00:00
RxApp . MainThreadScheduler . Schedule ( 0 , ( _ , _ ) = >
{
2022-10-16 12:17:30 +00:00
if ( args . Length = = 1 )
{
var arg = args [ 0 ] . ToAbsolutePath ( ) ;
if ( arg . FileExists ( ) & & arg . Extension = = Ext . Wabbajack )
{
var mainWindow = _host . Services . GetRequiredService < MainWindow > ( ) ;
mainWindow ! . Show ( ) ;
return Disposable . Empty ;
}
} else if ( args . Length > 0 )
2022-10-14 22:46:38 +00:00
{
var builder = _host . Services . GetRequiredService < CommandLineBuilder > ( ) ;
builder . Run ( e . Args ) . ContinueWith ( async x = >
{
Environment . Exit ( await x ) ;
} ) ;
return Disposable . Empty ;
}
else
{
var mainWindow = _host . Services . GetRequiredService < MainWindow > ( ) ;
mainWindow ! . Show ( ) ;
return Disposable . Empty ;
}
2022-10-16 12:17:30 +00:00
return Disposable . Empty ;
2022-10-14 21:11:09 +00:00
} ) ;
2022-03-13 22:47:30 +00:00
}
2022-05-22 04:27:02 +00:00
2022-11-05 19:35:48 +00:00
private static bool IsAdmin ( )
{
if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ) return false ;
try
{
var identity = WindowsIdentity . GetCurrent ( ) ;
var principle = new WindowsPrincipal ( identity ) ;
return principle . IsInRole ( WindowsBuiltInRole . Administrator ) ;
}
catch ( Exception )
{
return false ;
}
}
2022-05-22 04:27:02 +00:00
private void AddLogging ( ILoggingBuilder loggingBuilder )
{
var config = new NLog . Config . LoggingConfiguration ( ) ;
2022-10-06 22:54:01 +00:00
var logFolder = KnownFolders . LauncherAwarePath . Combine ( "logs" ) ;
if ( ! logFolder . DirectoryExists ( ) )
logFolder . CreateDirectory ( ) ;
2022-11-05 19:35:48 +00:00
2022-05-22 04:27:02 +00:00
var fileTarget = new FileTarget ( "file" )
{
2022-10-01 13:02:47 +00:00
FileName = logFolder . Combine ( "Wabbajack.current.log" ) . ToString ( ) ,
ArchiveFileName = logFolder . Combine ( "Wabbajack.{##}.log" ) . ToString ( ) ,
2022-05-22 04:27:02 +00:00
ArchiveOldFileOnStartup = true ,
MaxArchiveFiles = 10 ,
Layout = "${processtime} [${level:uppercase=true}] (${logger}) ${message:withexception=true}" ,
Header = "############ Wabbajack log file - ${longdate} ############"
} ;
2022-11-05 19:35:48 +00:00
2022-05-22 04:27:02 +00:00
var consoleTarget = new ConsoleTarget ( "console" ) ;
2022-11-05 19:35:48 +00:00
2022-05-23 13:07:05 +00:00
var uiTarget = new LogStream
2022-05-22 04:27:02 +00:00
{
Name = "ui" ,
2022-05-23 13:07:05 +00:00
Layout = "${message:withexception=false}" ,
2022-05-22 04:27:02 +00:00
} ;
2022-11-05 19:35:48 +00:00
2022-05-22 04:27:02 +00:00
loggingBuilder . Services . AddSingleton ( uiTarget ) ;
config . AddRuleForAllLevels ( fileTarget ) ;
config . AddRuleForAllLevels ( consoleTarget ) ;
config . AddRuleForAllLevels ( uiTarget ) ;
loggingBuilder . ClearProviders ( ) ;
loggingBuilder . SetMinimumLevel ( LogLevel . Trace ) ;
loggingBuilder . AddNLog ( config ) ;
}
2022-03-13 22:47:30 +00:00
private static IServiceCollection ConfigureServices ( IServiceCollection services )
{
services . AddOSIntegrated ( ) ;
2022-10-16 12:00:50 +00:00
// Orc.FileAssociation
services . AddSingleton < IApplicationRegistrationService > ( new ApplicationRegistrationService ( ) ) ;
2022-03-13 22:47:30 +00:00
services . AddSingleton < CefService > ( ) ;
2022-05-21 21:11:43 +00:00
services . AddSingleton < IUserInterventionHandler , UserIntreventionHandler > ( ) ;
2022-11-05 19:35:48 +00:00
2022-03-13 22:47:30 +00:00
services . AddTransient < MainWindow > ( ) ;
services . AddTransient < MainWindowVM > ( ) ;
2022-05-20 04:12:16 +00:00
services . AddTransient < BrowserWindow > ( ) ;
2022-03-13 22:47:30 +00:00
services . AddSingleton < SystemParametersConstructor > ( ) ;
services . AddSingleton < LauncherUpdater > ( ) ;
services . AddSingleton < ResourceMonitor > ( ) ;
services . AddSingleton < MainSettings > ( ) ;
services . AddTransient < CompilerVM > ( ) ;
services . AddTransient < InstallerVM > ( ) ;
services . AddTransient < ModeSelectionVM > ( ) ;
services . AddTransient < ModListGalleryVM > ( ) ;
services . AddTransient < CompilerVM > ( ) ;
services . AddTransient < InstallerVM > ( ) ;
services . AddTransient < SettingsVM > ( ) ;
services . AddTransient < WebBrowserVM > ( ) ;
2022-11-05 19:35:48 +00:00
2022-03-13 22:47:30 +00:00
// Login Handlers
services . AddTransient < VectorPlexusLoginHandler > ( ) ;
services . AddTransient < NexusLoginHandler > ( ) ;
services . AddTransient < LoversLabLoginHandler > ( ) ;
2022-11-05 19:35:48 +00:00
2022-03-13 22:47:30 +00:00
// Login Managers
services . AddAllSingleton < INeedsLogin , LoversLabLoginManager > ( ) ;
services . AddAllSingleton < INeedsLogin , NexusLoginManager > ( ) ;
services . AddAllSingleton < INeedsLogin , VectorPlexusLoginManager > ( ) ;
2022-05-16 22:14:52 +00:00
services . AddSingleton < ManualDownloadHandler > ( ) ;
2022-05-19 03:21:38 +00:00
services . AddSingleton < ManualBlobDownloadHandler > ( ) ;
2022-11-05 19:35:48 +00:00
2022-10-14 22:46:38 +00:00
// Verbs
services . AddSingleton < CommandLineBuilder > ( ) ;
services . AddCLIVerbs ( ) ;
2022-11-05 19:35:48 +00:00
2022-03-13 22:47:30 +00:00
return services ;
}
2022-10-14 21:11:09 +00:00
2022-03-13 22:47:30 +00:00
private void OnExit ( object sender , ExitEventArgs e )
{
using ( _host )
{
_host . StopAsync ( ) ;
}
base . OnExit ( e ) ;
}
}
}