LANCommander/LANCommander.SDK/ProcessExecutionContext.cs

216 lines
7.8 KiB
C#
Raw Permalink Normal View History

using LANCommander.SDK.Extensions;
using LANCommander.SDK.Helpers;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using LANCommander.SDK.Enums;
Implement SDK using dependency injection - API requests are now made through the ApiRequestBuilder with DI supplied via the ApiRequestFactory singleton - Reliance on RestSharp and WebClient has been removed in favor of HttpClient - Auth token is now being tracked by the ITokenProvider - Network information (MAC address, broadcast addresses, IP address) is now supplied with the INetworkInformationProvider singleton - Connection state is now being maintained by the ConnectionService, with a reliance on RPC (SignalR websocket) reporting actual connection state without relying on pings - All download streams are now provided as a TrackableStream - Singleton Client class has been removed. All usage of the SDK should happen via Dependency Injection This is just a base, non-functional refactor of the SDK to use DI. The following changes to the rest of the codebase need to be made: - Usage of the SDK client in the launcher needs to be replaced in favor of injecting SDK services - PowerShell cmdlets need to be able to have services injected. Most likely a separate scope will have to be opened up per PS runtime? - Usage of the SDK client in the server needs to be replaced in favor of injecting SDK services. This should be minimal and should actually provide benefit when it comes to executing client-like features (scripts mostly) without needing a fully configured client that maintains connection state. - Configuration of the client needs to be implemented using ILANCommanderConfiguration
2025-09-22 00:29:51 -05:00
using LANCommander.SDK.Services;
namespace LANCommander.SDK
{
Implement SDK using dependency injection - API requests are now made through the ApiRequestBuilder with DI supplied via the ApiRequestFactory singleton - Reliance on RestSharp and WebClient has been removed in favor of HttpClient - Auth token is now being tracked by the ITokenProvider - Network information (MAC address, broadcast addresses, IP address) is now supplied with the INetworkInformationProvider singleton - Connection state is now being maintained by the ConnectionService, with a reliance on RPC (SignalR websocket) reporting actual connection state without relying on pings - All download streams are now provided as a TrackableStream - Singleton Client class has been removed. All usage of the SDK should happen via Dependency Injection This is just a base, non-functional refactor of the SDK to use DI. The following changes to the rest of the codebase need to be made: - Usage of the SDK client in the launcher needs to be replaced in favor of injecting SDK services - PowerShell cmdlets need to be able to have services injected. Most likely a separate scope will have to be opened up per PS runtime? - Usage of the SDK client in the server needs to be replaced in favor of injecting SDK services. This should be minimal and should actually provide benefit when it comes to executing client-like features (scripts mostly) without needing a fully configured client that maintains connection state. - Configuration of the client needs to be implemented using ILANCommanderConfiguration
2025-09-22 00:29:51 -05:00
public class ProcessExecutionContext(
ILogger<ProcessExecutionContext> logger,
LobbyClient lobbyClient) : IDisposable
{
private Process Process;
private Dictionary<string, string> Variables { get; set; } = new Dictionary<string, string>();
public event DataReceivedEventHandler? OutputDataReceived;
public event DataReceivedEventHandler? ErrorDataReceived;
public void AddVariable(string key, string value)
{
Variables[key] = value;
}
public string ExpandVariables(string input, string workingDirectory, Dictionary<string, string> additionalVariables = null, bool skipSlashes = false)
{
try
{
if (input == null)
return input;
foreach (var variable in Variables)
{
input = input.Replace($"{{{variable.Key}}}", variable.Value);
}
if (additionalVariables != null)
foreach (var variable in additionalVariables)
{
input = input.Replace($"{{{variable.Key}}}", variable.Value);
}
return input.ExpandEnvironmentVariables(workingDirectory, skipSlashes);
}
catch (Exception ex)
{
Implement SDK using dependency injection - API requests are now made through the ApiRequestBuilder with DI supplied via the ApiRequestFactory singleton - Reliance on RestSharp and WebClient has been removed in favor of HttpClient - Auth token is now being tracked by the ITokenProvider - Network information (MAC address, broadcast addresses, IP address) is now supplied with the INetworkInformationProvider singleton - Connection state is now being maintained by the ConnectionService, with a reliance on RPC (SignalR websocket) reporting actual connection state without relying on pings - All download streams are now provided as a TrackableStream - Singleton Client class has been removed. All usage of the SDK should happen via Dependency Injection This is just a base, non-functional refactor of the SDK to use DI. The following changes to the rest of the codebase need to be made: - Usage of the SDK client in the launcher needs to be replaced in favor of injecting SDK services - PowerShell cmdlets need to be able to have services injected. Most likely a separate scope will have to be opened up per PS runtime? - Usage of the SDK client in the server needs to be replaced in favor of injecting SDK services. This should be minimal and should actually provide benefit when it comes to executing client-like features (scripts mostly) without needing a fully configured client that maintains connection state. - Configuration of the client needs to be implemented using ILANCommanderConfiguration
2025-09-22 00:29:51 -05:00
logger?.LogError(ex, "Could not expand runtime variables");
return input;
}
}
public Process GetProcess()
{
return Process;
}
public async Task ExecuteServerAsync(Models.Server server,
CancellationTokenSource cancellationTokenSource = default)
{
Process = new Process();
Process.EnableRaisingEvents = true;
var processStartInfo = new ProcessStartInfo();
processStartInfo.Arguments = ExpandVariables(server.Arguments, server.WorkingDirectory, skipSlashes: true);
processStartInfo.FileName = ExpandVariables(server.Path, server.WorkingDirectory);
processStartInfo.WorkingDirectory = server.WorkingDirectory;
processStartInfo.UseShellExecute = server.UseShellExecute;
2025-02-28 22:58:28 -06:00
if (!server.UseShellExecute)
{
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardOutput = true;
}
Process.StartInfo = processStartInfo;
2025-02-28 22:58:28 -06:00
if (OutputDataReceived != null && !processStartInfo.UseShellExecute)
Process.OutputDataReceived += OutputDataReceived;
2025-02-28 22:58:28 -06:00
if (OutputDataReceived != null && !processStartInfo.UseShellExecute)
Process.ErrorDataReceived += ErrorDataReceived;
Implement SDK using dependency injection - API requests are now made through the ApiRequestBuilder with DI supplied via the ApiRequestFactory singleton - Reliance on RestSharp and WebClient has been removed in favor of HttpClient - Auth token is now being tracked by the ITokenProvider - Network information (MAC address, broadcast addresses, IP address) is now supplied with the INetworkInformationProvider singleton - Connection state is now being maintained by the ConnectionService, with a reliance on RPC (SignalR websocket) reporting actual connection state without relying on pings - All download streams are now provided as a TrackableStream - Singleton Client class has been removed. All usage of the SDK should happen via Dependency Injection This is just a base, non-functional refactor of the SDK to use DI. The following changes to the rest of the codebase need to be made: - Usage of the SDK client in the launcher needs to be replaced in favor of injecting SDK services - PowerShell cmdlets need to be able to have services injected. Most likely a separate scope will have to be opened up per PS runtime? - Usage of the SDK client in the server needs to be replaced in favor of injecting SDK services. This should be minimal and should actually provide benefit when it comes to executing client-like features (scripts mostly) without needing a fully configured client that maintains connection state. - Configuration of the client needs to be implemented using ILANCommanderConfiguration
2025-09-22 00:29:51 -05:00
logger?.LogTrace("Running server executable");
logger?.LogTrace("Arguments: {Arguments}", Process.StartInfo.Arguments);
logger?.LogTrace("File Name: {FileName}", Process.StartInfo.FileName);
logger?.LogTrace("Working Directory: {WorkingDirectory}", Process.StartInfo.WorkingDirectory);
bool exited = false;
Process.Start();
Process.Exited += (sender, args) =>
{
if (cancellationTokenSource?.Token.CanBeCanceled ?? false)
cancellationTokenSource.Cancel();
};
if (processStartInfo.RedirectStandardError)
Process.BeginErrorReadLine();
if (processStartInfo.RedirectStandardOutput)
Process.BeginOutputReadLine();
cancellationTokenSource?.Token.WaitHandle.WaitOne();
if (server.ProcessTerminationMethod == ProcessTerminationMethod.Close)
Process.CloseMainWindow();
else if (server.ProcessTerminationMethod == ProcessTerminationMethod.Kill)
Process.Kill();
else
{
int signal = 1;
int pid = Process.Id;
Process.Close();
switch (server.ProcessTerminationMethod)
{
case ProcessTerminationMethod.SIGHUP:
signal = 1;
break;
case ProcessTerminationMethod.SIGINT:
signal = 2;
break;
case ProcessTerminationMethod.SIGKILL:
signal = 9;
break;
case ProcessTerminationMethod.SIGTERM:
signal = 15;
break;
}
using (var terminator = new Process())
{
terminator.StartInfo.FileName = "kill";
terminator.StartInfo.Arguments = $"-{signal} {pid}";
terminator.Start();
await terminator.WaitForExitAsync();
}
}
}
public async Task ExecuteGameActionAsync(string installDirectory, Guid gameId, Models.Manifest.Action action, string args = "", CancellationToken cancellationToken = default)
{
var manifest = await ManifestHelper.ReadAsync<Models.Manifest.Game>(installDirectory, gameId);
if (action == null)
action = manifest.Actions.FirstOrDefault(a => a.IsPrimaryAction);
if (manifest.CustomFields != null && manifest.CustomFields.Any())
{
foreach (var customField in manifest.CustomFields)
{
AddVariable(customField.Name, customField.Value);
}
}
Process = new Process();
Process.StartInfo.Arguments = ExpandVariables(action.Arguments, installDirectory, skipSlashes: true);
Process.StartInfo.FileName = ExpandVariables(action.Path, installDirectory);
Process.StartInfo.WorkingDirectory = ExpandVariables(action.WorkingDirectory, installDirectory);
Process.StartInfo.UseShellExecute = true;
if (OutputDataReceived != null)
Process.OutputDataReceived += OutputDataReceived;
if (ErrorDataReceived != null)
Process.ErrorDataReceived += ErrorDataReceived;
if (String.IsNullOrWhiteSpace(action.WorkingDirectory))
Process.StartInfo.WorkingDirectory = installDirectory;
if (!String.IsNullOrWhiteSpace(args))
Process.StartInfo.Arguments += " " + args;
Implement SDK using dependency injection - API requests are now made through the ApiRequestBuilder with DI supplied via the ApiRequestFactory singleton - Reliance on RestSharp and WebClient has been removed in favor of HttpClient - Auth token is now being tracked by the ITokenProvider - Network information (MAC address, broadcast addresses, IP address) is now supplied with the INetworkInformationProvider singleton - Connection state is now being maintained by the ConnectionService, with a reliance on RPC (SignalR websocket) reporting actual connection state without relying on pings - All download streams are now provided as a TrackableStream - Singleton Client class has been removed. All usage of the SDK should happen via Dependency Injection This is just a base, non-functional refactor of the SDK to use DI. The following changes to the rest of the codebase need to be made: - Usage of the SDK client in the launcher needs to be replaced in favor of injecting SDK services - PowerShell cmdlets need to be able to have services injected. Most likely a separate scope will have to be opened up per PS runtime? - Usage of the SDK client in the server needs to be replaced in favor of injecting SDK services. This should be minimal and should actually provide benefit when it comes to executing client-like features (scripts mostly) without needing a fully configured client that maintains connection state. - Configuration of the client needs to be implemented using ILANCommanderConfiguration
2025-09-22 00:29:51 -05:00
logger?.LogTrace("Running game executable");
logger?.LogTrace("Arguments: {Arguments}", Process.StartInfo.Arguments);
logger?.LogTrace("File Name: {FileName}", Process.StartInfo.FileName);
logger?.LogTrace("Working Directory: {WorkingDirectory}", Process.StartInfo.WorkingDirectory);
logger?.LogTrace("Manifest Path: {ManifestPath}", ManifestHelper.GetPath(installDirectory, gameId));
bool exited = false;
Process.Start();
await Process.WaitForAllExitAsync(cancellationToken);
}
public void Dispose()
{
try
{
if (Process != null)
{
if (!Process.HasExited)
Process.Close();
Process.Dispose();
}
}
catch { }
lobbyClient.ReleaseSteam();
}
}
}