|
|
|
@ -1,3 +1,7 @@
|
|
|
|
|
#if DEBUG |
|
|
|
|
using StabilityMatrix.Avalonia.Diagnostics.LogViewer; |
|
|
|
|
using StabilityMatrix.Avalonia.Diagnostics.LogViewer.Extensions; |
|
|
|
|
#endif |
|
|
|
|
using System; |
|
|
|
|
using System.Diagnostics; |
|
|
|
|
using System.Diagnostics.CodeAnalysis; |
|
|
|
@ -33,8 +37,6 @@ using Refit;
|
|
|
|
|
using Sentry; |
|
|
|
|
using StabilityMatrix.Avalonia.Controls; |
|
|
|
|
using StabilityMatrix.Avalonia.DesignData; |
|
|
|
|
using StabilityMatrix.Avalonia.Diagnostics.LogViewer; |
|
|
|
|
using StabilityMatrix.Avalonia.Diagnostics.LogViewer.Extensions; |
|
|
|
|
using StabilityMatrix.Avalonia.Helpers; |
|
|
|
|
using StabilityMatrix.Avalonia.Languages; |
|
|
|
|
using StabilityMatrix.Avalonia.Models; |
|
|
|
@ -68,12 +70,19 @@ namespace StabilityMatrix.Avalonia;
|
|
|
|
|
|
|
|
|
|
public sealed class App : Application |
|
|
|
|
{ |
|
|
|
|
[NotNull] public static IServiceProvider? Services { get; private set; } |
|
|
|
|
[NotNull] public static Visual? VisualRoot { get; private set; } |
|
|
|
|
[NotNull] public static IStorageProvider? StorageProvider { get; private set; } |
|
|
|
|
[NotNull] |
|
|
|
|
public static IServiceProvider? Services { get; private set; } |
|
|
|
|
|
|
|
|
|
[NotNull] |
|
|
|
|
public static Visual? VisualRoot { get; private set; } |
|
|
|
|
|
|
|
|
|
[NotNull] |
|
|
|
|
public static IStorageProvider? StorageProvider { get; private set; } |
|
|
|
|
|
|
|
|
|
// ReSharper disable once MemberCanBePrivate.Global |
|
|
|
|
[NotNull] public static IConfiguration? Config { get; private set; } |
|
|
|
|
|
|
|
|
|
[NotNull] |
|
|
|
|
public static IConfiguration? Config { get; private set; } |
|
|
|
|
|
|
|
|
|
// ReSharper disable once MemberCanBePrivate.Global |
|
|
|
|
public IClassicDesktopStyleApplicationLifetime? DesktopLifetime => |
|
|
|
|
ApplicationLifetime as IClassicDesktopStyleApplicationLifetime; |
|
|
|
@ -88,11 +97,11 @@ public sealed class App : Application
|
|
|
|
|
RequestedThemeVariant = ThemeVariant.Dark; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnFrameworkInitializationCompleted() |
|
|
|
|
{ |
|
|
|
|
base.OnFrameworkInitializationCompleted(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Design.IsDesignMode) |
|
|
|
|
{ |
|
|
|
|
DesignData.DesignData.Initialize(); |
|
|
|
@ -117,9 +126,10 @@ public sealed class App : Application
|
|
|
|
|
setupWindow.ShowAsDialog = true; |
|
|
|
|
setupWindow.ShowActivated = true; |
|
|
|
|
setupWindow.ShowAsyncCts = new CancellationTokenSource(); |
|
|
|
|
|
|
|
|
|
setupWindow.ExtendClientAreaChromeHints = Program.Args.NoWindowChromeEffects ? |
|
|
|
|
ExtendClientAreaChromeHints.NoChrome : ExtendClientAreaChromeHints.PreferSystemChrome; |
|
|
|
|
|
|
|
|
|
setupWindow.ExtendClientAreaChromeHints = Program.Args.NoWindowChromeEffects |
|
|
|
|
? ExtendClientAreaChromeHints.NoChrome |
|
|
|
|
: ExtendClientAreaChromeHints.PreferSystemChrome; |
|
|
|
|
|
|
|
|
|
DesktopLifetime.MainWindow = setupWindow; |
|
|
|
|
|
|
|
|
@ -146,47 +156,55 @@ public sealed class App : Application
|
|
|
|
|
|
|
|
|
|
private void ShowMainWindow() |
|
|
|
|
{ |
|
|
|
|
if (DesktopLifetime is null) return; |
|
|
|
|
|
|
|
|
|
if (DesktopLifetime is null) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
var mainViewModel = Services.GetRequiredService<MainWindowViewModel>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var mainWindow = Services.GetRequiredService<MainWindow>(); |
|
|
|
|
mainWindow.DataContext = mainViewModel; |
|
|
|
|
|
|
|
|
|
mainWindow.ExtendClientAreaChromeHints = Program.Args.NoWindowChromeEffects ? |
|
|
|
|
ExtendClientAreaChromeHints.NoChrome : ExtendClientAreaChromeHints.PreferSystemChrome; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mainWindow.ExtendClientAreaChromeHints = Program.Args.NoWindowChromeEffects |
|
|
|
|
? ExtendClientAreaChromeHints.NoChrome |
|
|
|
|
: ExtendClientAreaChromeHints.PreferSystemChrome; |
|
|
|
|
|
|
|
|
|
var settingsManager = Services.GetRequiredService<ISettingsManager>(); |
|
|
|
|
var windowSettings = settingsManager.Settings.WindowSettings; |
|
|
|
|
if (windowSettings != null && !Program.Args.ResetWindowPosition) |
|
|
|
|
{ |
|
|
|
|
mainWindow.Position = new PixelPoint(windowSettings.X, windowSettings.Y); |
|
|
|
|
mainWindow.Width = windowSettings.Width; |
|
|
|
|
mainWindow.Height = windowSettings.Height; |
|
|
|
|
mainWindow.Height = windowSettings.Height; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
mainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mainWindow.Closing += (_, _) => |
|
|
|
|
{ |
|
|
|
|
var validWindowPosition = |
|
|
|
|
mainWindow.Screens.All.Any(screen => screen.Bounds.Contains(mainWindow.Position)); |
|
|
|
|
var validWindowPosition = mainWindow.Screens.All.Any( |
|
|
|
|
screen => screen.Bounds.Contains(mainWindow.Position) |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
settingsManager.Transaction(s => |
|
|
|
|
{ |
|
|
|
|
s.WindowSettings = new WindowSettings( |
|
|
|
|
mainWindow.Width, mainWindow.Height, |
|
|
|
|
validWindowPosition ? mainWindow.Position.X : 0, |
|
|
|
|
validWindowPosition ? mainWindow.Position.Y : 0); |
|
|
|
|
}, ignoreMissingLibraryDir: true); |
|
|
|
|
settingsManager.Transaction( |
|
|
|
|
s => |
|
|
|
|
{ |
|
|
|
|
s.WindowSettings = new WindowSettings( |
|
|
|
|
mainWindow.Width, |
|
|
|
|
mainWindow.Height, |
|
|
|
|
validWindowPosition ? mainWindow.Position.X : 0, |
|
|
|
|
validWindowPosition ? mainWindow.Position.Y : 0 |
|
|
|
|
); |
|
|
|
|
}, |
|
|
|
|
ignoreMissingLibraryDir: true |
|
|
|
|
); |
|
|
|
|
}; |
|
|
|
|
mainWindow.Closed += (_, _) => Shutdown(); |
|
|
|
|
|
|
|
|
|
VisualRoot = mainWindow; |
|
|
|
|
StorageProvider = mainWindow.StorageProvider; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DesktopLifetime.MainWindow = mainWindow; |
|
|
|
|
DesktopLifetime.Exit += OnExit; |
|
|
|
|
} |
|
|
|
@ -195,49 +213,50 @@ public sealed class App : Application
|
|
|
|
|
{ |
|
|
|
|
var services = ConfigureServices(); |
|
|
|
|
Services = services.BuildServiceProvider(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var settingsManager = Services.GetRequiredService<ISettingsManager>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (settingsManager.TryFindLibrary()) |
|
|
|
|
{ |
|
|
|
|
Cultures.TrySetSupportedCulture(settingsManager.Settings.Language); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Services.GetRequiredService<ProgressManagerViewModel>().StartEventListener(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
internal static void ConfigurePageViewModels(IServiceCollection services) |
|
|
|
|
{ |
|
|
|
|
services.AddSingleton<PackageManagerViewModel>() |
|
|
|
|
services |
|
|
|
|
.AddSingleton<PackageManagerViewModel>() |
|
|
|
|
.AddSingleton<SettingsViewModel>() |
|
|
|
|
.AddSingleton<CheckpointBrowserViewModel>() |
|
|
|
|
.AddSingleton<CheckpointsPageViewModel>() |
|
|
|
|
.AddSingleton<NewCheckpointsPageViewModel>() |
|
|
|
|
.AddSingleton<LaunchPageViewModel>() |
|
|
|
|
.AddSingleton<ProgressManagerViewModel>(); |
|
|
|
|
|
|
|
|
|
services.AddSingleton<MainWindowViewModel>(provider => |
|
|
|
|
new MainWindowViewModel(provider.GetRequiredService<ISettingsManager>(), |
|
|
|
|
provider.GetRequiredService<IDiscordRichPresenceService>(), |
|
|
|
|
provider.GetRequiredService<ServiceManager<ViewModelBase>>(), |
|
|
|
|
provider.GetRequiredService<ITrackedDownloadService>()) |
|
|
|
|
{ |
|
|
|
|
Pages = |
|
|
|
|
{ |
|
|
|
|
provider.GetRequiredService<LaunchPageViewModel>(), |
|
|
|
|
provider.GetRequiredService<PackageManagerViewModel>(), |
|
|
|
|
provider.GetRequiredService<CheckpointsPageViewModel>(), |
|
|
|
|
provider.GetRequiredService<CheckpointBrowserViewModel>(), |
|
|
|
|
}, |
|
|
|
|
FooterPages = |
|
|
|
|
|
|
|
|
|
services.AddSingleton<MainWindowViewModel>( |
|
|
|
|
provider => |
|
|
|
|
new MainWindowViewModel( |
|
|
|
|
provider.GetRequiredService<ISettingsManager>(), |
|
|
|
|
provider.GetRequiredService<IDiscordRichPresenceService>(), |
|
|
|
|
provider.GetRequiredService<ServiceManager<ViewModelBase>>(), |
|
|
|
|
provider.GetRequiredService<ITrackedDownloadService>() |
|
|
|
|
) |
|
|
|
|
{ |
|
|
|
|
provider.GetRequiredService<SettingsViewModel>() |
|
|
|
|
Pages = |
|
|
|
|
{ |
|
|
|
|
provider.GetRequiredService<LaunchPageViewModel>(), |
|
|
|
|
provider.GetRequiredService<PackageManagerViewModel>(), |
|
|
|
|
provider.GetRequiredService<CheckpointsPageViewModel>(), |
|
|
|
|
provider.GetRequiredService<CheckpointBrowserViewModel>(), |
|
|
|
|
}, |
|
|
|
|
FooterPages = { provider.GetRequiredService<SettingsViewModel>() } |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
// Register disposable view models for shutdown cleanup |
|
|
|
|
services.AddSingleton<IDisposable>(p |
|
|
|
|
=> p.GetRequiredService<LaunchPageViewModel>()); |
|
|
|
|
services.AddSingleton<IDisposable>(p => p.GetRequiredService<LaunchPageViewModel>()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
internal static void ConfigureDialogViewModels(IServiceCollection services) |
|
|
|
@ -251,43 +270,44 @@ public sealed class App : Application
|
|
|
|
|
services.AddTransient<ExceptionViewModel>(); |
|
|
|
|
services.AddTransient<EnvVarsViewModel>(); |
|
|
|
|
services.AddTransient<PackageImportViewModel>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Dialog view models (singleton) |
|
|
|
|
services.AddSingleton<FirstLaunchSetupViewModel>(); |
|
|
|
|
services.AddSingleton<UpdateViewModel>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Other transients (usually sub view models) |
|
|
|
|
services.AddTransient<CheckpointFolder>(); |
|
|
|
|
services.AddTransient<CheckpointFile>(); |
|
|
|
|
services.AddTransient<CheckpointBrowserCardViewModel>(); |
|
|
|
|
services.AddTransient<PackageCardViewModel>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Global progress |
|
|
|
|
services.AddSingleton<ProgressManagerViewModel>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Controls |
|
|
|
|
services.AddTransient<RefreshBadgeViewModel>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Dialog factory |
|
|
|
|
services.AddSingleton<ServiceManager<ViewModelBase>>(provider => |
|
|
|
|
new ServiceManager<ViewModelBase>() |
|
|
|
|
.Register(provider.GetRequiredService<InstallerViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<OneClickInstallViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<SelectModelVersionViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<SelectDataDirectoryViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<LaunchOptionsViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<UpdateViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<CheckpointBrowserCardViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<CheckpointFolder>) |
|
|
|
|
.Register(provider.GetRequiredService<CheckpointFile>) |
|
|
|
|
.Register(provider.GetRequiredService<PackageCardViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<RefreshBadgeViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<ExceptionViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<EnvVarsViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<ProgressManagerViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<FirstLaunchSetupViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<PackageImportViewModel>) |
|
|
|
|
); |
|
|
|
|
services.AddSingleton<ServiceManager<ViewModelBase>>( |
|
|
|
|
provider => |
|
|
|
|
new ServiceManager<ViewModelBase>() |
|
|
|
|
.Register(provider.GetRequiredService<InstallerViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<OneClickInstallViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<SelectModelVersionViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<SelectDataDirectoryViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<LaunchOptionsViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<UpdateViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<CheckpointBrowserCardViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<CheckpointFolder>) |
|
|
|
|
.Register(provider.GetRequiredService<CheckpointFile>) |
|
|
|
|
.Register(provider.GetRequiredService<PackageCardViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<RefreshBadgeViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<ExceptionViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<EnvVarsViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<ProgressManagerViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<FirstLaunchSetupViewModel>) |
|
|
|
|
.Register(provider.GetRequiredService<PackageImportViewModel>) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
internal static void ConfigureViews(IServiceCollection services) |
|
|
|
@ -300,7 +320,7 @@ public sealed class App : Application
|
|
|
|
|
services.AddSingleton<CheckpointBrowserPage>(); |
|
|
|
|
services.AddSingleton<ProgressManagerPage>(); |
|
|
|
|
services.AddSingleton<NewCheckpointsPage>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Dialogs |
|
|
|
|
services.AddTransient<SelectDataDirectoryDialog>(); |
|
|
|
|
services.AddTransient<LaunchOptionsDialog>(); |
|
|
|
@ -308,15 +328,15 @@ public sealed class App : Application
|
|
|
|
|
services.AddTransient<ExceptionDialog>(); |
|
|
|
|
services.AddTransient<EnvVarsDialog>(); |
|
|
|
|
services.AddTransient<PackageImportDialog>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Controls |
|
|
|
|
services.AddTransient<RefreshBadge>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Windows |
|
|
|
|
services.AddSingleton<MainWindow>(); |
|
|
|
|
services.AddSingleton<FirstLaunchSetupWindow>(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal static void ConfigurePackages(IServiceCollection services) |
|
|
|
|
{ |
|
|
|
|
services.AddSingleton<BasePackage, A3WebUI>(); |
|
|
|
@ -336,7 +356,7 @@ public sealed class App : Application
|
|
|
|
|
ConfigurePageViewModels(services); |
|
|
|
|
ConfigureDialogViewModels(services); |
|
|
|
|
ConfigurePackages(services); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Other services |
|
|
|
|
services.AddSingleton<ISettingsManager, SettingsManager>(); |
|
|
|
|
services.AddSingleton<ISharedFolders, SharedFolders>(); |
|
|
|
@ -350,23 +370,25 @@ public sealed class App : Application
|
|
|
|
|
services.AddSingleton<IUpdateHelper, UpdateHelper>(); |
|
|
|
|
services.AddSingleton<INavigationService, NavigationService>(); |
|
|
|
|
services.AddSingleton<IModelIndexService, ModelIndexService>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
services.AddSingleton<ITrackedDownloadService, TrackedDownloadService>(); |
|
|
|
|
services.AddSingleton<IDisposable>(provider => |
|
|
|
|
(IDisposable) provider.GetRequiredService<ITrackedDownloadService>()); |
|
|
|
|
|
|
|
|
|
services.AddSingleton<IDisposable>( |
|
|
|
|
provider => (IDisposable)provider.GetRequiredService<ITrackedDownloadService>() |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
// Rich presence |
|
|
|
|
services.AddSingleton<IDiscordRichPresenceService, DiscordRichPresenceService>(); |
|
|
|
|
services.AddSingleton<IDisposable>(provider => |
|
|
|
|
provider.GetRequiredService<IDiscordRichPresenceService>()); |
|
|
|
|
services.AddSingleton<IDisposable>( |
|
|
|
|
provider => provider.GetRequiredService<IDiscordRichPresenceService>() |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
Config = new ConfigurationBuilder() |
|
|
|
|
.SetBasePath(Directory.GetCurrentDirectory()) |
|
|
|
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) |
|
|
|
|
.Build(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
services.Configure<DebugOptions>(Config.GetSection(nameof(DebugOptions))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Compat.IsWindows) |
|
|
|
|
{ |
|
|
|
|
services.AddSingleton<IPrerequisiteHelper, WindowsPrerequisiteHelper>(); |
|
|
|
@ -408,13 +430,13 @@ public sealed class App : Application
|
|
|
|
|
jsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter()); |
|
|
|
|
jsonSerializerOptions.Converters.Add(new DefaultUnknownEnumConverter<CivitFileType>()); |
|
|
|
|
jsonSerializerOptions.Converters.Add( |
|
|
|
|
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); |
|
|
|
|
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) |
|
|
|
|
); |
|
|
|
|
jsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; |
|
|
|
|
|
|
|
|
|
var defaultRefitSettings = new RefitSettings |
|
|
|
|
{ |
|
|
|
|
ContentSerializer = |
|
|
|
|
new SystemTextJsonContentSerializer(jsonSerializerOptions) |
|
|
|
|
ContentSerializer = new SystemTextJsonContentSerializer(jsonSerializerOptions) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// HTTP Policies |
|
|
|
@ -426,9 +448,10 @@ public sealed class App : Application
|
|
|
|
|
HttpStatusCode.ServiceUnavailable, // 503 |
|
|
|
|
HttpStatusCode.GatewayTimeout // 504 |
|
|
|
|
}; |
|
|
|
|
var delay = Backoff |
|
|
|
|
.DecorrelatedJitterBackoffV2(medianFirstRetryDelay: TimeSpan.FromMilliseconds(80), |
|
|
|
|
retryCount: 5); |
|
|
|
|
var delay = Backoff.DecorrelatedJitterBackoffV2( |
|
|
|
|
medianFirstRetryDelay: TimeSpan.FromMilliseconds(80), |
|
|
|
|
retryCount: 5 |
|
|
|
|
); |
|
|
|
|
var retryPolicy = HttpPolicyExtensions |
|
|
|
|
.HandleTransientHttpError() |
|
|
|
|
.Or<TimeoutRejectedException>() |
|
|
|
@ -437,25 +460,29 @@ public sealed class App : Application
|
|
|
|
|
|
|
|
|
|
// Shorter timeout for local requests |
|
|
|
|
var localTimeout = Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(3)); |
|
|
|
|
var localDelay = Backoff |
|
|
|
|
.DecorrelatedJitterBackoffV2(medianFirstRetryDelay: TimeSpan.FromMilliseconds(50), |
|
|
|
|
retryCount: 3); |
|
|
|
|
var localDelay = Backoff.DecorrelatedJitterBackoffV2( |
|
|
|
|
medianFirstRetryDelay: TimeSpan.FromMilliseconds(50), |
|
|
|
|
retryCount: 3 |
|
|
|
|
); |
|
|
|
|
var localRetryPolicy = HttpPolicyExtensions |
|
|
|
|
.HandleTransientHttpError() |
|
|
|
|
.Or<TimeoutRejectedException>() |
|
|
|
|
.OrResult(r => retryStatusCodes.Contains(r.StatusCode)) |
|
|
|
|
.WaitAndRetryAsync(localDelay, onRetryAsync: (_, _) => |
|
|
|
|
{ |
|
|
|
|
Debug.WriteLine("Retrying local request..."); |
|
|
|
|
return Task.CompletedTask; |
|
|
|
|
}); |
|
|
|
|
.WaitAndRetryAsync( |
|
|
|
|
localDelay, |
|
|
|
|
onRetryAsync: (_, _) => |
|
|
|
|
{ |
|
|
|
|
Debug.WriteLine("Retrying local request..."); |
|
|
|
|
return Task.CompletedTask; |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
// named client for update |
|
|
|
|
services.AddHttpClient("UpdateClient") |
|
|
|
|
.AddPolicyHandler(retryPolicy); |
|
|
|
|
services.AddHttpClient("UpdateClient").AddPolicyHandler(retryPolicy); |
|
|
|
|
|
|
|
|
|
// Add Refit clients |
|
|
|
|
services.AddRefitClient<ICivitApi>(defaultRefitSettings) |
|
|
|
|
services |
|
|
|
|
.AddRefitClient<ICivitApi>(defaultRefitSettings) |
|
|
|
|
.ConfigureHttpClient(c => |
|
|
|
|
{ |
|
|
|
|
c.BaseAddress = new Uri("https://civitai.com"); |
|
|
|
@ -464,27 +491,31 @@ public sealed class App : Application
|
|
|
|
|
.AddPolicyHandler(retryPolicy); |
|
|
|
|
|
|
|
|
|
// Add Refit client managers |
|
|
|
|
services.AddHttpClient("A3Client") |
|
|
|
|
services |
|
|
|
|
.AddHttpClient("A3Client") |
|
|
|
|
.AddPolicyHandler(localTimeout.WrapAsync(localRetryPolicy)); |
|
|
|
|
|
|
|
|
|
ConditionalAddLogViewer(services); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add logging |
|
|
|
|
services.AddLogging(builder => |
|
|
|
|
{ |
|
|
|
|
builder.ClearProviders(); |
|
|
|
|
builder.AddFilter("Microsoft.Extensions.Http", LogLevel.Warning) |
|
|
|
|
builder |
|
|
|
|
.AddFilter("Microsoft.Extensions.Http", LogLevel.Warning) |
|
|
|
|
.AddFilter("Microsoft.Extensions.Http.DefaultHttpClientFactory", LogLevel.Warning) |
|
|
|
|
.AddFilter("Microsoft", LogLevel.Warning) |
|
|
|
|
.AddFilter("System", LogLevel.Warning); |
|
|
|
|
builder.SetMinimumLevel(LogLevel.Debug); |
|
|
|
|
#if DEBUG |
|
|
|
|
builder.AddNLog(ConfigureLogging(), |
|
|
|
|
builder.AddNLog( |
|
|
|
|
ConfigureLogging(), |
|
|
|
|
new NLogProviderOptions |
|
|
|
|
{ |
|
|
|
|
IgnoreEmptyEventId = false, |
|
|
|
|
CaptureEventId = EventIdCaptureType.Legacy |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
#else |
|
|
|
|
builder.AddNLog(ConfigureLogging()); |
|
|
|
|
#endif |
|
|
|
@ -501,8 +532,8 @@ public sealed class App : Application
|
|
|
|
|
/// <exception cref="NullReferenceException">If Application.Current is null</exception> |
|
|
|
|
public static void Shutdown(int exitCode = 0) |
|
|
|
|
{ |
|
|
|
|
if (Current is null) throw new NullReferenceException( |
|
|
|
|
"Current Application was null when Shutdown called"); |
|
|
|
|
if (Current is null) |
|
|
|
|
throw new NullReferenceException("Current Application was null when Shutdown called"); |
|
|
|
|
if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime) |
|
|
|
|
{ |
|
|
|
|
lifetime.Shutdown(exitCode); |
|
|
|
@ -516,11 +547,9 @@ public sealed class App : Application
|
|
|
|
|
var settingsManager = Services.GetRequiredService<ISettingsManager>(); |
|
|
|
|
|
|
|
|
|
// If RemoveFolderLinksOnShutdown is set, delete all package junctions |
|
|
|
|
if (settingsManager is |
|
|
|
|
{ |
|
|
|
|
IsLibraryDirSet: true, |
|
|
|
|
Settings.RemoveFolderLinksOnShutdown: true |
|
|
|
|
}) |
|
|
|
|
if ( |
|
|
|
|
settingsManager is { IsLibraryDirSet: true, Settings.RemoveFolderLinksOnShutdown: true } |
|
|
|
|
) |
|
|
|
|
{ |
|
|
|
|
var sharedFolders = Services.GetRequiredService<ISharedFolders>(); |
|
|
|
|
sharedFolders.RemoveLinksForAllPackages(); |
|
|
|
@ -542,40 +571,48 @@ public sealed class App : Application
|
|
|
|
|
var setupBuilder = LogManager.Setup(); |
|
|
|
|
|
|
|
|
|
ConditionalAddLogViewerNLog(setupBuilder); |
|
|
|
|
|
|
|
|
|
setupBuilder.LoadConfiguration(builder => { |
|
|
|
|
var debugTarget = builder.ForTarget("console").WriteTo(new DebuggerTarget |
|
|
|
|
{ |
|
|
|
|
Layout = "${message}" |
|
|
|
|
}).WithAsync(); |
|
|
|
|
|
|
|
|
|
var fileTarget = builder.ForTarget("logfile").WriteTo(new FileTarget |
|
|
|
|
{ |
|
|
|
|
Layout = "${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}", |
|
|
|
|
ArchiveOldFileOnStartup = true, |
|
|
|
|
FileName = "${specialfolder:folder=ApplicationData}/StabilityMatrix/app.log", |
|
|
|
|
ArchiveFileName = "${specialfolder:folder=ApplicationData}/StabilityMatrix/app.{#}.log", |
|
|
|
|
ArchiveNumbering = ArchiveNumberingMode.Rolling, |
|
|
|
|
MaxArchiveFiles = 2 |
|
|
|
|
}).WithAsync(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setupBuilder.LoadConfiguration(builder => |
|
|
|
|
{ |
|
|
|
|
var debugTarget = builder |
|
|
|
|
.ForTarget("console") |
|
|
|
|
.WriteTo(new DebuggerTarget { Layout = "${message}" }) |
|
|
|
|
.WithAsync(); |
|
|
|
|
|
|
|
|
|
var fileTarget = builder |
|
|
|
|
.ForTarget("logfile") |
|
|
|
|
.WriteTo( |
|
|
|
|
new FileTarget |
|
|
|
|
{ |
|
|
|
|
Layout = |
|
|
|
|
"${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}", |
|
|
|
|
ArchiveOldFileOnStartup = true, |
|
|
|
|
FileName = |
|
|
|
|
"${specialfolder:folder=ApplicationData}/StabilityMatrix/app.log", |
|
|
|
|
ArchiveFileName = |
|
|
|
|
"${specialfolder:folder=ApplicationData}/StabilityMatrix/app.{#}.log", |
|
|
|
|
ArchiveNumbering = ArchiveNumberingMode.Rolling, |
|
|
|
|
MaxArchiveFiles = 2 |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
.WithAsync(); |
|
|
|
|
|
|
|
|
|
// Filter some sources to be warn levels or above only |
|
|
|
|
builder.ForLogger("System.*").WriteToNil(NLog.LogLevel.Warn); |
|
|
|
|
builder.ForLogger("Microsoft.*").WriteToNil(NLog.LogLevel.Warn); |
|
|
|
|
builder.ForLogger("Microsoft.Extensions.Http.*").WriteToNil(NLog.LogLevel.Warn); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
builder.ForLogger().FilterMinLevel(NLog.LogLevel.Trace).WriteTo(debugTarget); |
|
|
|
|
builder.ForLogger().FilterMinLevel(NLog.LogLevel.Debug).WriteTo(fileTarget); |
|
|
|
|
|
|
|
|
|
#if DEBUG |
|
|
|
|
var logViewerTarget = builder.ForTarget("DataStoreLogger").WriteTo(new DataStoreLoggerTarget() |
|
|
|
|
{ |
|
|
|
|
Layout = "${message}" |
|
|
|
|
}); |
|
|
|
|
var logViewerTarget = builder |
|
|
|
|
.ForTarget("DataStoreLogger") |
|
|
|
|
.WriteTo(new DataStoreLoggerTarget() { Layout = "${message}" }); |
|
|
|
|
builder.ForLogger().FilterMinLevel(NLog.LogLevel.Trace).WriteTo(logViewerTarget); |
|
|
|
|
#endif |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Sentry |
|
|
|
|
if (SentrySdk.IsEnabled) |
|
|
|
|
{ |
|
|
|
@ -594,20 +631,26 @@ public sealed class App : Application
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
LogManager.ReconfigExistingLoggers(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return LogManager.Configuration; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Conditional("DEBUG")] |
|
|
|
|
private static void ConditionalAddLogViewer(IServiceCollection services) |
|
|
|
|
{ |
|
|
|
|
#if DEBUG |
|
|
|
|
services.AddLogViewer(); |
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Conditional("DEBUG")] |
|
|
|
|
private static void ConditionalAddLogViewerNLog(ISetupBuilder setupBuilder) |
|
|
|
|
{ |
|
|
|
|
setupBuilder.SetupExtensions(extensionBuilder => |
|
|
|
|
extensionBuilder.RegisterTarget<DataStoreLoggerTarget>("DataStoreLogger")); |
|
|
|
|
#if DEBUG |
|
|
|
|
setupBuilder.SetupExtensions( |
|
|
|
|
extensionBuilder => |
|
|
|
|
extensionBuilder.RegisterTarget<DataStoreLoggerTarget>("DataStoreLogger") |
|
|
|
|
); |
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|