91287c9860
* Fix startup binding and prep v1.2.0 * Polish v1.2.0 validation UX * Fix final v1.2.0 validation polish * Make Activity Logs a ThreadPilot audit trail * Fix high scheduling registry value * Polish process list and automation settings UX * Fix process context menu separator styling * Theme process context menu visuals * Stabilize process context menu styling * Remove process menu icon gutter * Polish startup and idle process refresh * Finalize PR22 release polish
263 lines
12 KiB
C#
263 lines
12 KiB
C#
/*
|
|
* ThreadPilot - Advanced Windows Process and Power Plan Manager
|
|
* Copyright (C) 2025 Prime Build
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, version 3 only.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
namespace ThreadPilot.Services
|
|
{
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using ThreadPilot.Platforms.Windows;
|
|
using ThreadPilot.Services.Abstractions;
|
|
using ThreadPilot.ViewModels;
|
|
|
|
/// <summary>
|
|
/// Centralized service configuration for dependency injection.
|
|
/// </summary>
|
|
public static class ServiceConfiguration
|
|
{
|
|
/// <summary>
|
|
/// Configure all application services.
|
|
/// </summary>
|
|
public static IServiceCollection ConfigureApplicationServices(this IServiceCollection services)
|
|
{
|
|
// Configure service infrastructure
|
|
services.ConfigureServiceInfrastructure();
|
|
|
|
// Configure core system services
|
|
services.ConfigureCoreSystemServices();
|
|
|
|
// Configure process management services
|
|
services.ConfigureProcessManagementServices();
|
|
|
|
// Configure application services
|
|
services.ConfigureApplicationLevelServices();
|
|
|
|
// Configure presentation layer
|
|
services.ConfigurePresentationLayer();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configure service infrastructure (logging, factories, etc.)
|
|
/// </summary>
|
|
private static IServiceCollection ConfigureServiceInfrastructure(this IServiceCollection services)
|
|
{
|
|
// Logging infrastructure
|
|
services.AddLogging(builder =>
|
|
{
|
|
builder.AddConsole();
|
|
builder.SetMinimumLevel(LogLevel.Information);
|
|
});
|
|
|
|
// Enhanced logging service
|
|
services.AddSingleton<IEnhancedLoggingService, EnhancedLoggingService>();
|
|
services.AddSingleton<IActivityAuditService, ActivityAuditService>();
|
|
services.AddSingleton<IProcessRunner, SystemProcessRunner>();
|
|
services.AddSingleton<ISettingsStorage, FileSettingsStorage>();
|
|
services.AddSingleton(sp =>
|
|
{
|
|
var httpClient = new HttpClient();
|
|
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("ThreadPilot", "1.0"));
|
|
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github+json"));
|
|
return httpClient;
|
|
});
|
|
services.AddSingleton<IGitHubReleaseClient, GitHubReleaseClient>();
|
|
services.AddSingleton<GitHubUpdateChecker>();
|
|
|
|
// Memory caching for performance - PERFORMANCE IMPROVEMENT
|
|
services.AddMemoryCache();
|
|
|
|
// Service lifecycle management - PERFORMANCE IMPROVEMENT
|
|
services.AddSingleton<IServiceHealthMonitor, ServiceHealthMonitor>();
|
|
services.AddSingleton<IServiceDisposalCoordinator, ServiceDisposalCoordinator>();
|
|
|
|
// Error recovery and retry policies - RELIABILITY IMPROVEMENT
|
|
services.AddSingleton<IRetryPolicyService, RetryPolicyService>();
|
|
|
|
// Service factory for advanced service management
|
|
services.AddSingleton<IServiceFactory, ServiceFactory>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configure core system services that interact directly with the OS.
|
|
/// </summary>
|
|
private static IServiceCollection ConfigureCoreSystemServices(this IServiceCollection services)
|
|
{
|
|
// Core system interaction services
|
|
services.AddSingleton<IForegroundWindowProvider, WindowsForegroundWindowProvider>();
|
|
services.AddSingleton<IForegroundProcessService, ForegroundProcessService>();
|
|
services.AddSingleton<IPassiveProcessErrorThrottle, PassiveProcessErrorThrottle>();
|
|
services.AddSingleton<IProcessClassifier, ProcessClassifier>();
|
|
services.AddSingleton<IProcessService, ProcessService>();
|
|
services.AddSingleton<IAffinityApplyService, AffinityApplyService>();
|
|
services.AddSingleton<IProcessAffinityApplyCoordinator, ProcessAffinityApplyCoordinator>();
|
|
services.AddSingleton<IProcessMemoryPriorityNativeApi>(ProcessMemoryPriorityNativeApi.Instance);
|
|
services.AddSingleton<IProcessMemoryPriorityService, ProcessMemoryPriorityService>();
|
|
services.AddSingleton<IPersistentProcessRuleStore, PersistentProcessRuleJsonStore>();
|
|
services.AddSingleton<IPersistentProcessRuleMatcher, PersistentProcessRuleMatcher>();
|
|
services.AddSingleton<IPersistentRulesEngine, PersistentRulesEngine>();
|
|
services.AddSingleton<IPersistentRuleAutoApplyService, PersistentRuleAutoApplyService>();
|
|
services.AddSingleton<IProcessRuleCreationService, ProcessRuleCreationService>();
|
|
services.AddSingleton<ProcessFilterService>();
|
|
services.AddSingleton<IVirtualizedProcessService, VirtualizedProcessService>();
|
|
services.AddSingleton<IConditionalProfileService, ConditionalProfileService>();
|
|
services.AddSingleton<IPowerPlanService, PowerPlanService>();
|
|
services.AddSingleton<PowerPlanTransitionGate>();
|
|
services.AddSingleton<ICpuTopologyService, CpuTopologyService>();
|
|
services.AddSingleton<ICpuTopologyProvider, WindowsCpuTopologyProvider>();
|
|
services.AddSingleton<CpuSelectionMigrationService>();
|
|
services.AddSingleton<ICpuPresetGenerator, CpuPresetGenerator>();
|
|
|
|
// CoreMaskService needs IServiceProvider for checking profile references
|
|
services.AddSingleton<ICoreMaskService>(sp =>
|
|
{
|
|
var logger = sp.GetRequiredService<ILogger<CoreMaskService>>();
|
|
var cpuTopologyService = sp.GetRequiredService<ICpuTopologyService>();
|
|
var cpuTopologyProvider = sp.GetRequiredService<ICpuTopologyProvider>();
|
|
var migrationService = sp.GetRequiredService<CpuSelectionMigrationService>();
|
|
return new CoreMaskService(logger, cpuTopologyService, sp, cpuTopologyProvider, migrationService);
|
|
});
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configure process monitoring and management services.
|
|
/// </summary>
|
|
private static IServiceCollection ConfigureProcessManagementServices(this IServiceCollection services)
|
|
{
|
|
// Process monitoring services
|
|
services.AddSingleton<IProcessMonitorService, ProcessMonitorService>();
|
|
services.AddSingleton<IProcessPowerPlanAssociationService, ProcessPowerPlanAssociationService>();
|
|
services.AddSingleton<IProcessMonitorManagerService, ProcessMonitorManagerService>();
|
|
|
|
// Performance monitoring services
|
|
services.AddSingleton<IPerformanceMonitoringService, PerformanceMonitoringService>();
|
|
services.AddSingleton(sp => new Lazy<IPerformanceMonitoringService>(
|
|
() => sp.GetRequiredService<IPerformanceMonitoringService>()));
|
|
services.AddSingleton<ISystemTrayStatusUpdater, SystemTrayStatusUpdater>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configure application-level services (settings, notifications, etc.)
|
|
/// </summary>
|
|
private static IServiceCollection ConfigureApplicationLevelServices(this IServiceCollection services)
|
|
{
|
|
// Application configuration and settings
|
|
services.AddSingleton<IApplicationSettingsService, ApplicationSettingsService>();
|
|
services.AddSingleton<IThemeService, ThemeService>();
|
|
services.AddSingleton<ISelfResourceManagementService, SelfResourceManagementService>();
|
|
|
|
// User interface services
|
|
services.AddSingleton<INotificationService, NotificationService>();
|
|
services.AddSingleton<ISmartNotificationService, SmartNotificationService>();
|
|
services.AddSingleton<ISystemTrayService, SystemTrayService>();
|
|
|
|
// System integration services
|
|
services.AddSingleton<IAutostartService, AutostartService>();
|
|
|
|
// System optimization services
|
|
services.AddSingleton<IGameModeService, GameModeService>();
|
|
|
|
// Security and elevation services
|
|
services.AddSingleton<ISecurityService, SecurityService>();
|
|
services.AddSingleton<IElevationService, ElevationService>();
|
|
services.AddSingleton<IElevatedTaskService, ElevatedTaskService>();
|
|
|
|
// System tweaks service
|
|
services.AddSingleton<ISystemTweaksService, SystemTweaksService>();
|
|
|
|
// Keyboard shortcut service
|
|
services.AddSingleton<IKeyboardShortcutService, KeyboardShortcutService>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configure presentation layer (ViewModels and Views).
|
|
/// </summary>
|
|
private static IServiceCollection ConfigurePresentationLayer(this IServiceCollection services)
|
|
{
|
|
// ViewModel factory for centralized ViewModel management
|
|
services.AddViewModelFactory();
|
|
|
|
// ViewModels - ProcessViewModel as Singleton to share state across views, others as Transient
|
|
services.AddSingleton<ProcessViewModel>();
|
|
services.AddSingleton<MasksViewModel>();
|
|
services.AddTransient<PowerPlanViewModel>();
|
|
services.AddTransient<ProcessPowerPlanAssociationViewModel>();
|
|
services.AddTransient<SettingsViewModel>();
|
|
services.AddTransient<MainWindowViewModel>();
|
|
services.AddTransient<PerformanceViewModel>();
|
|
services.AddTransient(sp => new Lazy<PerformanceViewModel>(
|
|
() => sp.GetRequiredService<PerformanceViewModel>()));
|
|
services.AddTransient<IDiagnosticsViewModelProvider, DiagnosticsViewModelProvider>();
|
|
services.AddTransient<LogViewerViewModel>();
|
|
services.AddTransient<SystemTweaksViewModel>();
|
|
|
|
// Views - Transient for proper lifecycle management
|
|
services.AddTransient<MainWindow>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate service configuration.
|
|
/// </summary>
|
|
public static void ValidateServiceConfiguration(IServiceProvider serviceProvider)
|
|
{
|
|
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
|
|
var logger = loggerFactory.CreateLogger("ServiceConfiguration");
|
|
|
|
try
|
|
{
|
|
// Validate core services can be resolved
|
|
var coreServices = new[]
|
|
{
|
|
typeof(IProcessService),
|
|
typeof(IPowerPlanService),
|
|
typeof(ICpuTopologyService),
|
|
typeof(IEnhancedLoggingService),
|
|
typeof(IActivityAuditService),
|
|
typeof(IApplicationSettingsService),
|
|
};
|
|
|
|
foreach (var serviceType in coreServices)
|
|
{
|
|
var service = serviceProvider.GetRequiredService(serviceType);
|
|
if (service == null)
|
|
{
|
|
throw new InvalidOperationException($"Failed to resolve required service: {serviceType.Name}");
|
|
}
|
|
}
|
|
|
|
logger.LogInformation("Service configuration validation completed successfully");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "Service configuration validation failed");
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|