diff --git a/src/c#/GeneralUpdate.Common/Shared/Object/BaseConfigInfo.cs b/src/c#/GeneralUpdate.Common/Shared/Object/BaseConfigInfo.cs index 02bff532..3bce46f2 100644 --- a/src/c#/GeneralUpdate.Common/Shared/Object/BaseConfigInfo.cs +++ b/src/c#/GeneralUpdate.Common/Shared/Object/BaseConfigInfo.cs @@ -92,5 +92,11 @@ public abstract class BaseConfigInfo /// This script is executed after update to ensure proper file permissions. /// public string Script { get; set; } + + /// + /// The directory path containing driver files for driver update functionality. + /// Used when DriveEnabled is true to locate and install driver files during updates. + /// + public string DriverDirectory { get; set; } } } diff --git a/src/c#/GeneralUpdate.Common/Shared/Object/ConfigurationMapper.cs b/src/c#/GeneralUpdate.Common/Shared/Object/ConfigurationMapper.cs index 05b352d6..45e98d66 100644 --- a/src/c#/GeneralUpdate.Common/Shared/Object/ConfigurationMapper.cs +++ b/src/c#/GeneralUpdate.Common/Shared/Object/ConfigurationMapper.cs @@ -43,6 +43,7 @@ public static GlobalConfigInfo MapToGlobalConfigInfo(Configinfo source, GlobalCo target.Scheme = source.Scheme; target.Token = source.Token; target.Script = source.Script; + target.DriverDirectory = source.DriverDirectory; // Map GlobalConfigInfo-specific fields target.UpdateUrl = source.UpdateUrl; @@ -92,6 +93,7 @@ public static ProcessInfo MapToProcessInfo( scheme: source.Scheme, token: source.Token, script: source.Script, + driverDirectory: source.DriverDirectory, // Driver directory for driver updates blackFileFormats: blackFileFormats, // From BlackListManager blackFiles: blackFiles, // From BlackListManager skipDirectories: skipDirectories // From BlackListManager @@ -127,6 +129,7 @@ public static void CopyBaseFields(TSource source, TTarget targ target.Scheme = source.Scheme; target.Token = source.Token; target.Script = source.Script; + target.DriverDirectory = source.DriverDirectory; } } } diff --git a/src/c#/GeneralUpdate.Common/Shared/Object/GlobalConfigInfo.cs b/src/c#/GeneralUpdate.Common/Shared/Object/GlobalConfigInfo.cs index 62c6d0f3..5d0ce974 100644 --- a/src/c#/GeneralUpdate.Common/Shared/Object/GlobalConfigInfo.cs +++ b/src/c#/GeneralUpdate.Common/Shared/Object/GlobalConfigInfo.cs @@ -100,6 +100,12 @@ public class GlobalConfigInfo : BaseConfigInfo /// public bool? DriveEnabled { get; set; } + /// + /// Directory path containing driver files for update. + /// Used when DriveEnabled is true to locate driver files for installation. + /// + public string DriverDirectory { get; set; } + /// /// Indicates whether differential patch update is enabled. /// Computed from UpdateOption.Patch or defaults to true. diff --git a/src/c#/GeneralUpdate.Common/Shared/Object/ProcessInfo.cs b/src/c#/GeneralUpdate.Common/Shared/Object/ProcessInfo.cs index 047beef8..8b8f6932 100644 --- a/src/c#/GeneralUpdate.Common/Shared/Object/ProcessInfo.cs +++ b/src/c#/GeneralUpdate.Common/Shared/Object/ProcessInfo.cs @@ -43,6 +43,7 @@ public ProcessInfo() { } /// The URL scheme for update requests /// The authentication token /// The Linux permission script + /// The directory path containing driver files /// List of file format extensions to skip /// List of specific files to skip /// List of directories to skip @@ -64,6 +65,7 @@ public ProcessInfo(string appName , string scheme , string token , string script + , string driverDirectory , List blackFileFormats , List blackFiles , List skipDirectories) @@ -98,6 +100,7 @@ public ProcessInfo(string appName Scheme = scheme; Token = token; Script = script; + DriverDirectory = driverDirectory; // Set blacklist parameters BlackFileFormats = blackFileFormats; @@ -229,5 +232,12 @@ public ProcessInfo(string appName /// [JsonPropertyName("Script")] public string Script { get; set; } + + /// + /// The directory path containing driver files for driver update functionality. + /// Used when DriveEnabled is true to locate and install driver files. + /// + [JsonPropertyName("DriverDirectory")] + public string DriverDirectory { get; set; } } } \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Core/GeneralUpdate.Core.csproj b/src/c#/GeneralUpdate.Core/GeneralUpdate.Core.csproj index 1bc304aa..0a61943a 100644 --- a/src/c#/GeneralUpdate.Core/GeneralUpdate.Core.csproj +++ b/src/c#/GeneralUpdate.Core/GeneralUpdate.Core.csproj @@ -22,7 +22,7 @@ public upgrade,update true - netstandard2.0; + netstandard2.0;net8.0 @@ -90,6 +90,9 @@ + + + diff --git a/src/c#/GeneralUpdate.Core/GeneralUpdateBootstrap.cs b/src/c#/GeneralUpdate.Core/GeneralUpdateBootstrap.cs index 7382da5c..36baf93c 100644 --- a/src/c#/GeneralUpdate.Core/GeneralUpdateBootstrap.cs +++ b/src/c#/GeneralUpdate.Core/GeneralUpdateBootstrap.cs @@ -221,7 +221,8 @@ private void InitializeFromEnvironment() Token = processInfo.Token, DriveEnabled = GetOption(UpdateOption.Drive) ?? false, PatchEnabled = GetOption(UpdateOption.Patch) ?? true, - Script = processInfo.Script + Script = processInfo.Script, + DriverDirectory = processInfo.DriverDirectory }; } diff --git a/src/c#/GeneralUpdate.Core/Pipeline/DrivelutionMiddleware.cs b/src/c#/GeneralUpdate.Core/Pipeline/DrivelutionMiddleware.cs new file mode 100644 index 00000000..cfcb4fac --- /dev/null +++ b/src/c#/GeneralUpdate.Core/Pipeline/DrivelutionMiddleware.cs @@ -0,0 +1,100 @@ +#if NET8_0_OR_GREATER +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using GeneralUpdate.Common.Internal.Pipeline; +using GeneralUpdate.Common.Shared; +using GeneralUpdate.Drivelution; +using GeneralUpdate.Drivelution.Abstractions.Models; + +namespace GeneralUpdate.Core.Pipeline; + +/// +/// Middleware for handling driver updates using GeneralUpdate.Drivelution functionality. +/// +public class DrivelutionMiddleware : IMiddleware +{ + [RequiresUnreferencedCode("Driver update process includes signature validation that may require runtime reflection on some platforms")] + [RequiresDynamicCode("Driver update process includes signature validation that may require runtime code generation on some platforms")] + public async Task InvokeAsync(PipelineContext context) + { + try + { + var driverDirectory = context.Get("DriverDirectory"); + + if (string.IsNullOrWhiteSpace(driverDirectory)) + { + GeneralTracer.Info("Driver directory not specified in context, skipping driver update."); + return; + } + + if (!Directory.Exists(driverDirectory)) + { + GeneralTracer.Info($"Driver directory does not exist: {driverDirectory}, skipping driver update."); + return; + } + + GeneralTracer.Info($"Starting driver update from directory: {driverDirectory}"); + + // Get drivers from the specified directory + var drivers = await GeneralDrivelution.GetDriversFromDirectoryAsync(driverDirectory); + + if (drivers == null || !drivers.Any()) + { + GeneralTracer.Info($"No drivers found in directory: {driverDirectory}"); + return; + } + + GeneralTracer.Info($"Found {drivers.Count} driver(s) in directory."); + + // Update each driver + var successCount = 0; + var failureCount = 0; + var results = new List(); + + foreach (var driver in drivers) + { + try + { + GeneralTracer.Info($"Updating driver: {driver.Name} (Version: {driver.Version})"); + + var result = await GeneralDrivelution.QuickUpdateAsync(driver); + results.Add(result); + + if (result.Success) + { + successCount++; + GeneralTracer.Info($"Driver {driver.Name} updated successfully. Status: {result.Status}"); + } + else + { + failureCount++; + var errorMessage = result.Error != null + ? $"{result.Error.Code}: {result.Error.Message}" + : "Unknown error"; + GeneralTracer.Warn($"Driver {driver.Name} update failed. Error: {errorMessage}"); + } + } + catch (Exception ex) + { + failureCount++; + GeneralTracer.Error($"Exception while updating driver {driver.Name}", ex); + } + } + + GeneralTracer.Info($"Driver update completed. Success: {successCount}, Failed: {failureCount}"); + + // Store results in context for potential later use + context.Add("DriverUpdateResults", results); + } + catch (Exception ex) + { + GeneralTracer.Error($"Error in DrivelutionMiddleware while processing directory '{context.Get("DriverDirectory")}'", ex); + throw; + } + } +} +#endif diff --git a/src/c#/GeneralUpdate.Core/Strategys/LinuxStrategy.cs b/src/c#/GeneralUpdate.Core/Strategys/LinuxStrategy.cs index 9de91fb7..751453e1 100644 --- a/src/c#/GeneralUpdate.Core/Strategys/LinuxStrategy.cs +++ b/src/c#/GeneralUpdate.Core/Strategys/LinuxStrategy.cs @@ -21,8 +21,7 @@ protected override PipelineContext CreatePipelineContext(VersionInfo version, st // Driver middleware (Linux-specific) if (_configinfo.DriveEnabled == true) { - context.Add("DriverOutPut", StorageManager.GetTempDirectory("DriverOutPut")); - context.Add("FieldMappings", _configinfo.FieldMappings); + context.Add("DriverDirectory", _configinfo.DriverDirectory); } return context; @@ -30,10 +29,14 @@ protected override PipelineContext CreatePipelineContext(VersionInfo version, st protected override PipelineBuilder BuildPipeline(PipelineContext context) { - return new PipelineBuilder(context) + var builder = new PipelineBuilder(context) .UseMiddlewareIf(_configinfo.PatchEnabled) .UseMiddleware() .UseMiddleware(); +#if NET8_0_OR_GREATER + builder.UseMiddlewareIf(_configinfo.DriveEnabled == true); +#endif + return builder; } public override void Execute() diff --git a/src/c#/GeneralUpdate.Core/Strategys/WindowsStrategy.cs b/src/c#/GeneralUpdate.Core/Strategys/WindowsStrategy.cs index 78540f05..0169a5bf 100644 --- a/src/c#/GeneralUpdate.Core/Strategys/WindowsStrategy.cs +++ b/src/c#/GeneralUpdate.Core/Strategys/WindowsStrategy.cs @@ -23,8 +23,7 @@ protected override PipelineContext CreatePipelineContext(VersionInfo version, st // Driver middleware (Windows-specific) if (_configinfo.DriveEnabled == true) { - context.Add("DriverOutPut", StorageManager.GetTempDirectory("DriverOutPut")); - context.Add("FieldMappings", _configinfo.FieldMappings); + context.Add("DriverDirectory", _configinfo.DriverDirectory); } return context; @@ -32,10 +31,14 @@ protected override PipelineContext CreatePipelineContext(VersionInfo version, st protected override PipelineBuilder BuildPipeline(PipelineContext context) { - return new PipelineBuilder(context) + var builder = new PipelineBuilder(context) .UseMiddlewareIf(_configinfo.PatchEnabled) .UseMiddleware() .UseMiddleware(); +#if NET8_0_OR_GREATER + builder.UseMiddlewareIf(_configinfo.DriveEnabled == true); +#endif + return builder; } protected override void OnExecuteComplete()