wabbajack/Wabbajack.App.Wpf/App.xaml.cs
Luca a87f8dac7f
New Features from External Contributors [Merged to Internal Test Branch so Tests can run!] (#2325)
* added enderalse GOGID

* Fix readme opening twice when loading last modlist

* Edit Wabbajack CLI button text

* Cancel running downloads when shutting down application

* Add resume support for IHttpDownloader

* Add resume support for manual downloads

* Update CHANGELOG.md

* Improve game selection to only show games with results combined with the amount of lists

* Undo accidental removal of loading settings

* Add more tooltips and improve existing ones

* Update CHANGELOG.md

* Main test external pull readme fix (#2335)

* Fix SelectedGameType crashing Wabbajack when no settings are present yet, fix readme being clickable when not specified resulting in crash

* Add readme fix to CHANGELOG, fix typo

* Add readme button fix to changelog

---------

Co-authored-by: UrbanCMC <UrbanCMC@web.de>
Co-authored-by: Angad <angadmisra28@gmail.com>
Co-authored-by: trawzified <55751269+tr4wzified@users.noreply.github.com>
Co-authored-by: Timothy Baldridge <tbaldridge@gmail.com>
2023-05-07 14:32:18 -06:00

199 lines
7.0 KiB
C#

using System;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using NLog.Targets;
using Orc.FileAssociation;
using ReactiveUI;
using Wabbajack.CLI.Builder;
using Wabbajack.DTOs;
using Wabbajack.DTOs.Interventions;
using Wabbajack.Interventions;
using Wabbajack.LoginManagers;
using Wabbajack.Models;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
using Wabbajack.Services.OSIntegrated;
using Wabbajack.UserIntervention;
using Wabbajack.Util;
using Ext = Wabbajack.Common.Ext;
namespace Wabbajack
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
private IHost _host;
private void OnStartup(object sender, StartupEventArgs e)
{
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);
}
}
RxApp.MainThreadScheduler = new DispatcherScheduler(Dispatcher.CurrentDispatcher);
_host = Host.CreateDefaultBuilder(Array.Empty<string>())
.ConfigureLogging(AddLogging)
.ConfigureServices((host, services) =>
{
ConfigureServices(services);
})
.Build();
var args = e.Args;
RxApp.MainThreadScheduler.Schedule(0, (_, _) =>
{
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)
{
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;
}
return Disposable.Empty;
});
}
private static bool IsAdmin()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return false;
try
{
var identity = WindowsIdentity.GetCurrent();
var owner = identity.Owner;
if (owner is not null) return owner.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid);
var principle = new WindowsPrincipal(identity);
return principle.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (Exception)
{
return false;
}
}
private void AddLogging(ILoggingBuilder loggingBuilder)
{
var config = new NLog.Config.LoggingConfiguration();
var logFolder = KnownFolders.LauncherAwarePath.Combine("logs");
if (!logFolder.DirectoryExists())
logFolder.CreateDirectory();
var fileTarget = new FileTarget("file")
{
FileName = logFolder.Combine("Wabbajack.current.log").ToString(),
ArchiveFileName = logFolder.Combine("Wabbajack.{##}.log").ToString(),
ArchiveOldFileOnStartup = true,
MaxArchiveFiles = 10,
Layout = "${processtime} [${level:uppercase=true}] (${logger}) ${message:withexception=true}",
Header = "############ Wabbajack log file - ${longdate} ############"
};
var consoleTarget = new ConsoleTarget("console");
var uiTarget = new LogStream
{
Name = "ui",
Layout = "${message:withexception=false}",
};
loggingBuilder.Services.AddSingleton(uiTarget);
config.AddRuleForAllLevels(fileTarget);
config.AddRuleForAllLevels(consoleTarget);
config.AddRuleForAllLevels(uiTarget);
loggingBuilder.ClearProviders();
loggingBuilder.SetMinimumLevel(LogLevel.Information);
loggingBuilder.AddNLog(config);
}
private static IServiceCollection ConfigureServices(IServiceCollection services)
{
services.AddOSIntegrated();
// Orc.FileAssociation
services.AddSingleton<IApplicationRegistrationService>(new ApplicationRegistrationService());
services.AddSingleton<CefService>();
services.AddSingleton<IUserInterventionHandler, UserIntreventionHandler>();
services.AddTransient<MainWindow>();
services.AddTransient<MainWindowVM>();
services.AddTransient<BrowserWindow>();
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>();
// Login Handlers
services.AddTransient<VectorPlexusLoginHandler>();
services.AddTransient<NexusLoginHandler>();
services.AddTransient<LoversLabLoginHandler>();
// Login Managers
services.AddAllSingleton<INeedsLogin, LoversLabLoginManager>();
services.AddAllSingleton<INeedsLogin, NexusLoginManager>();
services.AddAllSingleton<INeedsLogin, VectorPlexusLoginManager>();
services.AddSingleton<ManualDownloadHandler>();
services.AddSingleton<ManualBlobDownloadHandler>();
// Verbs
services.AddSingleton<CommandLineBuilder>();
services.AddCLIVerbs();
return services;
}
}
}