Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/c#/GeneralUpdate.Core/Driver/DisableUACCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace GeneralUpdate.Core.Driver
{
/// <summary>
/// Command to disable User Account Control (UAC) via script execution.
/// The script content is not managed by this class; only the script entry point is provided.
/// </summary>
public class DisableUACCommand : ScriptCommand
{
public DisableUACCommand(string scriptPath) : base(scriptPath)
{
}
}
}
22 changes: 22 additions & 0 deletions src/c#/GeneralUpdate.Core/Driver/DriverInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public class DriverInformation
public string OutPutDirectory { get; private set; }

public string DriverDirectory { get; private set; }

/// <summary>
/// Path to the script that disables UAC.
/// </summary>
public string DisableUACScriptPath { get; private set; }

/// <summary>
/// Path to the script that restores UAC.
/// </summary>
public string RestoreUACScriptPath { get; private set; }

/// <summary>
/// A collection of driver files to be backed up.
Expand Down Expand Up @@ -54,6 +64,18 @@ public Builder SetFieldMappings(Dictionary<string, string> fieldMappings)
_information.FieldMappings = fieldMappings;
return this;
}

public Builder SetDisableUACScriptPath(string scriptPath)
{
_information.DisableUACScriptPath = scriptPath;
return this;
}

public Builder SetRestoreUACScriptPath(string scriptPath)
{
_information.RestoreUACScriptPath = scriptPath;
return this;
}

public DriverInformation Build()
{
Expand Down
13 changes: 13 additions & 0 deletions src/c#/GeneralUpdate.Core/Driver/RestoreUACCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace GeneralUpdate.Core.Driver
{
/// <summary>
/// Command to restore User Account Control (UAC) via script execution.
/// The script content is not managed by this class; only the script entry point is provided.
/// </summary>
public class RestoreUACCommand : ScriptCommand
{
public RestoreUACCommand(string scriptPath) : base(scriptPath)
{
}
}
}
62 changes: 62 additions & 0 deletions src/c#/GeneralUpdate.Core/Driver/ScriptCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Diagnostics;
using GeneralUpdate.Common.Shared;

namespace GeneralUpdate.Core.Driver
{
/// <summary>
/// Base class for executing script commands.
/// </summary>
public abstract class ScriptCommand : DriverCommand
{
protected string ScriptPath { get; }

protected ScriptCommand(string scriptPath)
{
if (string.IsNullOrWhiteSpace(scriptPath))
throw new ArgumentNullException(nameof(scriptPath), "Script path cannot be null or empty.");

ScriptPath = scriptPath;
}

public override void Execute()
{
ExecuteScript(ScriptPath);
}

/// <summary>
/// Execute a script file.
/// </summary>
/// <param name="scriptPath">Path to the script file to execute.</param>
protected virtual void ExecuteScript(string scriptPath)
{
if (!System.IO.File.Exists(scriptPath))
throw new ApplicationException($"Script file not found: {scriptPath}");

var processStartInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = scriptPath,
UseShellExecute = true,
Verb = "runas"
};

var process = new Process();
try
{
process.StartInfo = processStartInfo;
process.Start();
process.WaitForExit();

if (process.ExitCode != 0)
throw new ApplicationException($"Script execution failed for '{scriptPath}' with exit code: {process.ExitCode}");

GeneralTracer.Info($"Script executed successfully: {scriptPath}");
}
finally
{
process.Dispose();
}
}
}
}
26 changes: 23 additions & 3 deletions src/c#/GeneralUpdate.Core/Pipeline/DriverMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,37 @@ public Task InvokeAsync(PipelineContext context)
if (fieldMappings == null || fieldMappings.Count == 0)
return;

var information = new DriverInformation.Builder()
var disableUACScriptPath = context.Get<string>("DisableUACScriptPath");
var restoreUACScriptPath = context.Get<string>("RestoreUACScriptPath");

var builder = new DriverInformation.Builder()
.SetDriverFileExtension(FileExtension)
.SetOutPutDirectory(outPutPath)
.SetDriverDirectory(patchPath)
.SetFieldMappings(fieldMappings)
.Build();
.SetFieldMappings(fieldMappings);

if (!string.IsNullOrWhiteSpace(disableUACScriptPath))
builder.SetDisableUACScriptPath(disableUACScriptPath);

if (!string.IsNullOrWhiteSpace(restoreUACScriptPath))
builder.SetRestoreUACScriptPath(restoreUACScriptPath);

var information = builder.Build();

var processor = new DriverProcessor();

// Disable UAC before driver operations if script path is provided
if (!string.IsNullOrWhiteSpace(information.DisableUACScriptPath))
processor.AddCommand(new DisableUACCommand(information.DisableUACScriptPath));

processor.AddCommand(new BackupDriverCommand(information));
processor.AddCommand(new DeleteDriverCommand(information));
processor.AddCommand(new InstallDriverCommand(information));

// Restore UAC after driver operations if script path is provided
if (!string.IsNullOrWhiteSpace(information.RestoreUACScriptPath))
processor.AddCommand(new RestoreUACCommand(information.RestoreUACScriptPath));

processor.ProcessCommands();
});
}
Expand Down