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

201 lines
7.1 KiB
C#
Raw Permalink Normal View History

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;
using System.Windows;
using System.Windows.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
New Wabbajack release (#2479) * added more visible error messages to avoid user confusion added hard drive free space detection, added red error message text, removed overwrite checkbox, added wiki button link extended the error text for starting wabbajack in protected location removed debug code shortened error message to fit in text box * restored warning removed in error, updated changelog, removed debug includes * Update InstallerVM.cs * Update InstallerVM.cs * Update MainWindowViewModel.cs * added json optional flag to only show version number over modlist image in installer view, if the modlist image already contains the title removed debug code change to pascal case and match existing code style update changelog * Fix manual downloads sometimes launching in browser * Fix manual downloads from secure servers * Remove duplicate user agent code * Create configuration project and performance settings * Bind new performance settings to UI * Use performance settings to limit maximum memory per download * Remove unused settings and related classes * Updated CHANGELOG.md * update CHANGELOG.md * moved the existing files popup to an error message , heralding the return of the overwrite install checkbox * added newline * reverted erroneous edit * gogID for fallout4 added * update CHANGELOG.md * Fix deadlock when loading new settings * change folder/directory check logic * update CHANGELOG.md * revert unnecessary change * update CHANGELOG.md * Bump Wabbajack to .NET 7 * Bump ReactiveUI packages & deps * Update GameFinder to 4.0.0 * Update CHANGELOG.md * Update CHANGELOG.md * Don't try to add cookies if array is empty * Start download from scratch if .download_package can't be parsed * Log when application is shutting down * update CHANGELOG.md * exclude the "SP Consent Message" on nexus from the cleared iframes * Actually use the --outputPath compile option if the user provides one It was just not being used, defaulting to the parent of installPath. it still does, if the user does not specify a directory using --outputPath * update Wabbajack.Compression.BSA dependencies * improve log message to include storage space reference * clearing temp folder when the app closes * update logging message * update CHANGELOG.md * update CHANGELOG.md * update logging for possible exceptions thrown when clearing temp folder * fix cloudflare captcha * update Final Fantasy VII: RI metadata * Update CHANGELOG.md * fix error * Update GameRegistry.cs * Updated GameFinder, Added EADesktop module * updated version number in CHANGELOG.md * updated logging code * update CHANGELOG.md * Fix Nexus login (#2476) * Fix Nexus login handler after API keys web page changes * Automatically press request button * Automatically press request API key button * Fix BG3 being named badlursgate3 in Game enum instead of baldursgate3 * Fix WebView2 taking up tons of memory, use single WebView window - still needs refactoring * Remove commented WebView * Add changelog, fix Wabbajack CLI bat pointing to the wrong directory --------- Co-authored-by: JanuarySnow <bobfordiscord12@gmail.com> Co-authored-by: JanuarySnow <85711747+JanuarySnow@users.noreply.github.com> Co-authored-by: UrbanCMC <UrbanCMC@web.de> Co-authored-by: EzioTheDeadPoet <52624146+EzioTheDeadPoet@users.noreply.github.com> Co-authored-by: Marco Antonio Jaguaribe Costa <marco.antonio.costa@gmail.com>
2024-01-14 21:48:34 +00:00
using Microsoft.Web.WebView2.Wpf;
using NLog.Extensions.Logging;
using NLog.Targets;
2022-10-16 12:00:50 +00:00
using Orc.FileAssociation;
using ReactiveUI;
2022-10-14 22:46:38 +00:00
using Wabbajack.CLI.Builder;
using Wabbajack.DTOs;
2022-05-16 22:14:52 +00:00
using Wabbajack.DTOs.Interventions;
using Wabbajack.Interventions;
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;
using Wabbajack.Services.OSIntegrated;
using Wabbajack.UserIntervention;
using Wabbajack.Util;
2022-10-16 12:17:30 +00:00
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)
{
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>())
.ConfigureLogging(AddLogging)
.ConfigureServices((host, services) =>
{
ConfigureServices(services);
})
.Build();
2022-10-14 22:46:38 +00:00
var args = e.Args;
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-11-05 19:35:48 +00:00
private static bool IsAdmin()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return false;
try
{
var identity = WindowsIdentity.GetCurrent();
2022-11-13 11:24:53 +00:00
var owner = identity.Owner;
if (owner is not null) return owner.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid);
2022-11-05 19:35:48 +00:00
var principle = new WindowsPrincipal(identity);
return principle.IsInRole(WindowsBuiltInRole.Administrator);
2022-11-13 11:24:53 +00:00
2022-11-05 19:35:48 +00:00
}
catch (Exception)
{
return false;
}
}
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
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(),
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
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
{
Name = "ui",
2022-05-23 13:07:05 +00:00
Layout = "${message:withexception=false}",
};
2022-11-05 19:35:48 +00:00
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();
2022-10-16 12:00:50 +00:00
// Orc.FileAssociation
services.AddSingleton<IApplicationRegistrationService>(new ApplicationRegistrationService());
services.AddSingleton<CefService>();
2022-05-21 21:11:43 +00:00
services.AddSingleton<IUserInterventionHandler, UserIntreventionHandler>();
2022-11-05 19:35:48 +00:00
services.AddTransient<MainWindow>();
services.AddTransient<MainWindowVM>();
2022-05-20 04:12:16 +00:00
services.AddTransient<BrowserWindow>();
services.AddSingleton<SystemParametersConstructor>();
services.AddSingleton<LauncherUpdater>();
services.AddSingleton<ResourceMonitor>();
New Wabbajack release (#2479) * added more visible error messages to avoid user confusion added hard drive free space detection, added red error message text, removed overwrite checkbox, added wiki button link extended the error text for starting wabbajack in protected location removed debug code shortened error message to fit in text box * restored warning removed in error, updated changelog, removed debug includes * Update InstallerVM.cs * Update InstallerVM.cs * Update MainWindowViewModel.cs * added json optional flag to only show version number over modlist image in installer view, if the modlist image already contains the title removed debug code change to pascal case and match existing code style update changelog * Fix manual downloads sometimes launching in browser * Fix manual downloads from secure servers * Remove duplicate user agent code * Create configuration project and performance settings * Bind new performance settings to UI * Use performance settings to limit maximum memory per download * Remove unused settings and related classes * Updated CHANGELOG.md * update CHANGELOG.md * moved the existing files popup to an error message , heralding the return of the overwrite install checkbox * added newline * reverted erroneous edit * gogID for fallout4 added * update CHANGELOG.md * Fix deadlock when loading new settings * change folder/directory check logic * update CHANGELOG.md * revert unnecessary change * update CHANGELOG.md * Bump Wabbajack to .NET 7 * Bump ReactiveUI packages & deps * Update GameFinder to 4.0.0 * Update CHANGELOG.md * Update CHANGELOG.md * Don't try to add cookies if array is empty * Start download from scratch if .download_package can't be parsed * Log when application is shutting down * update CHANGELOG.md * exclude the "SP Consent Message" on nexus from the cleared iframes * Actually use the --outputPath compile option if the user provides one It was just not being used, defaulting to the parent of installPath. it still does, if the user does not specify a directory using --outputPath * update Wabbajack.Compression.BSA dependencies * improve log message to include storage space reference * clearing temp folder when the app closes * update logging message * update CHANGELOG.md * update CHANGELOG.md * update logging for possible exceptions thrown when clearing temp folder * fix cloudflare captcha * update Final Fantasy VII: RI metadata * Update CHANGELOG.md * fix error * Update GameRegistry.cs * Updated GameFinder, Added EADesktop module * updated version number in CHANGELOG.md * updated logging code * update CHANGELOG.md * Fix Nexus login (#2476) * Fix Nexus login handler after API keys web page changes * Automatically press request button * Automatically press request API key button * Fix BG3 being named badlursgate3 in Game enum instead of baldursgate3 * Fix WebView2 taking up tons of memory, use single WebView window - still needs refactoring * Remove commented WebView * Add changelog, fix Wabbajack CLI bat pointing to the wrong directory --------- Co-authored-by: JanuarySnow <bobfordiscord12@gmail.com> Co-authored-by: JanuarySnow <85711747+JanuarySnow@users.noreply.github.com> Co-authored-by: UrbanCMC <UrbanCMC@web.de> Co-authored-by: EzioTheDeadPoet <52624146+EzioTheDeadPoet@users.noreply.github.com> Co-authored-by: Marco Antonio Jaguaribe Costa <marco.antonio.costa@gmail.com>
2024-01-14 21:48:34 +00:00
services.AddSingleton<WebView2>();
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
// Login Handlers
services.AddTransient<VectorPlexusLoginHandler>();
services.AddTransient<NexusLoginHandler>();
services.AddTransient<LoversLabLoginHandler>();
2022-11-05 19:35:48 +00:00
// Login Managers
Wabbajack 3.3.0.0 Update (#2416) * added more visible error messages to avoid user confusion added hard drive free space detection, added red error message text, removed overwrite checkbox, added wiki button link extended the error text for starting wabbajack in protected location removed debug code shortened error message to fit in text box * restored warning removed in error, updated changelog, removed debug includes * Update InstallerVM.cs * Update InstallerVM.cs * Update MainWindowViewModel.cs * added json optional flag to only show version number over modlist image in installer view, if the modlist image already contains the title removed debug code change to pascal case and match existing code style update changelog * Fix manual downloads sometimes launching in browser * Fix manual downloads from secure servers * Remove duplicate user agent code * Create configuration project and performance settings * Bind new performance settings to UI * Use performance settings to limit maximum memory per download * Remove unused settings and related classes * Updated CHANGELOG.md * update CHANGELOG.md * moved the existing files popup to an error message , heralding the return of the overwrite install checkbox * added newline * reverted erroneous edit * gogID for fallout4 added * update CHANGELOG.md * Fix deadlock when loading new settings * change folder/directory check logic * update CHANGELOG.md * revert unnecessary change * update CHANGELOG.md * Bump Wabbajack to .NET 7 * Bump ReactiveUI packages & deps * Update GameFinder to 4.0.0 * Update CHANGELOG.md * Update CHANGELOG.md --------- Co-authored-by: JanuarySnow <bobfordiscord12@gmail.com> Co-authored-by: JanuarySnow <85711747+JanuarySnow@users.noreply.github.com> Co-authored-by: UrbanCMC <UrbanCMC@web.de> Co-authored-by: trawzified <55751269+tr4wzified@users.noreply.github.com>
2023-10-12 18:33:06 +00:00
//Disabled LL because it is currently not used and broken due to the way LL butchers their API
//services.AddAllSingleton<INeedsLogin, LoversLabLoginManager>();
services.AddAllSingleton<INeedsLogin, NexusLoginManager>();
services.AddAllSingleton<INeedsLogin, VectorPlexusLoginManager>();
2022-05-16 22:14:52 +00:00
services.AddSingleton<ManualDownloadHandler>();
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
return services;
}
}
}