mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Plugins - Added ExecuteFileAction
This commit is contained in:
parent
2ebb45dd56
commit
cec50297e7
@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a plugin prerequisite action that executes a file
|
||||
/// </summary>
|
||||
public class ExecuteFileAction : PluginPrerequisiteAction
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ExecuteFileAction" />
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the action</param>
|
||||
/// <param name="fileName">The target file to execute</param>
|
||||
/// <param name="arguments">A set of command-line arguments to use when starting the application</param>
|
||||
/// <param name="waitForExit">A boolean indicating whether the action should wait for the process to exit</param>
|
||||
public ExecuteFileAction(string name, string fileName, string? arguments = null, bool waitForExit = true) : base(name)
|
||||
{
|
||||
FileName = fileName ?? throw new ArgumentNullException(nameof(fileName));
|
||||
Arguments = arguments;
|
||||
WaitForExit = waitForExit;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the target file to execute
|
||||
/// </summary>
|
||||
public string FileName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a set of command-line arguments to use when starting the application
|
||||
/// </summary>
|
||||
public string? Arguments { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a boolean indicating whether the action should wait for the process to exit
|
||||
/// </summary>
|
||||
public bool WaitForExit { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override async Task Execute(CancellationToken cancellationToken)
|
||||
{
|
||||
if (WaitForExit)
|
||||
{
|
||||
Status = $"Running {FileName} and waiting for exit..";
|
||||
ShowProgressBar = true;
|
||||
ProgressIndeterminate = true;
|
||||
|
||||
int result = await RunProcessAsync(FileName, Arguments);
|
||||
|
||||
Status = $"{FileName} exited with code {result}";
|
||||
}
|
||||
else
|
||||
{
|
||||
Status = $"Running {FileName}";
|
||||
Process process = new()
|
||||
{
|
||||
StartInfo = {FileName = FileName, Arguments = Arguments!},
|
||||
EnableRaisingEvents = true
|
||||
};
|
||||
process.Start();
|
||||
}
|
||||
}
|
||||
|
||||
private static Task<int> RunProcessAsync(string fileName, string? arguments)
|
||||
{
|
||||
TaskCompletionSource<int> tcs = new();
|
||||
|
||||
Process process = new()
|
||||
{
|
||||
StartInfo = {FileName = fileName, Arguments = arguments!},
|
||||
EnableRaisingEvents = true
|
||||
};
|
||||
|
||||
process.Exited += (_, _) =>
|
||||
{
|
||||
tcs.SetResult(process.ExitCode);
|
||||
process.Dispose();
|
||||
};
|
||||
|
||||
process.Start();
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2,33 +2,11 @@
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Artemis.UI.Utilities
|
||||
{
|
||||
public static class ProcessUtilities
|
||||
{
|
||||
public static Task<int> RunProcessAsync(string fileName, string arguments)
|
||||
{
|
||||
TaskCompletionSource<int> tcs = new();
|
||||
|
||||
Process process = new()
|
||||
{
|
||||
StartInfo = {FileName = fileName, Arguments = arguments},
|
||||
EnableRaisingEvents = true
|
||||
};
|
||||
|
||||
process.Exited += (sender, args) =>
|
||||
{
|
||||
tcs.SetResult(process.ExitCode);
|
||||
process.Dispose();
|
||||
};
|
||||
|
||||
process.Start();
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
public static Process RunAsDesktopUser(string fileName, string arguments, bool hideWindow)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(fileName))
|
||||
@ -198,24 +176,24 @@ namespace Artemis.UI.Utilities
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
||||
private struct STARTUPINFO
|
||||
{
|
||||
public int cb;
|
||||
public string lpReserved;
|
||||
public string lpDesktop;
|
||||
public string lpTitle;
|
||||
public int dwX;
|
||||
public int dwY;
|
||||
public int dwXSize;
|
||||
public int dwYSize;
|
||||
public int dwXCountChars;
|
||||
public int dwYCountChars;
|
||||
public int dwFillAttribute;
|
||||
public readonly int cb;
|
||||
public readonly string lpReserved;
|
||||
public readonly string lpDesktop;
|
||||
public readonly string lpTitle;
|
||||
public readonly int dwX;
|
||||
public readonly int dwY;
|
||||
public readonly int dwXSize;
|
||||
public readonly int dwYSize;
|
||||
public readonly int dwXCountChars;
|
||||
public readonly int dwYCountChars;
|
||||
public readonly int dwFillAttribute;
|
||||
public int dwFlags;
|
||||
public short wShowWindow;
|
||||
public short cbReserved2;
|
||||
public IntPtr lpReserved2;
|
||||
public IntPtr hStdInput;
|
||||
public IntPtr hStdOutput;
|
||||
public IntPtr hStdError;
|
||||
public readonly short cbReserved2;
|
||||
public readonly IntPtr lpReserved2;
|
||||
public readonly IntPtr hStdInput;
|
||||
public readonly IntPtr hStdOutput;
|
||||
public readonly IntPtr hStdError;
|
||||
}
|
||||
|
||||
[DllImport("kernel32.dll", ExactSpelling = true)]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user