Add project files.
This commit is contained in:
80
LeagueAPI/Utils/PortTokenWithProcessList.cs
Normal file
80
LeagueAPI/Utils/PortTokenWithProcessList.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace LeagueAPI.Utils;
|
||||
|
||||
internal partial class PortTokenWithProcessList : IPortTokenBehavior
|
||||
{
|
||||
[GeneratedRegex("--remoting-auth-token=([\\w-]*)")]
|
||||
private static partial Regex AuthTokenRegex();
|
||||
[GeneratedRegex("--app-port=([0-9]*)")]
|
||||
private static partial Regex AppPortRegex();
|
||||
|
||||
public bool TryGet(Process process, out string remotingAuthToken, out int appPort, [NotNullWhen(false)] out Exception? exception)
|
||||
{
|
||||
try
|
||||
{
|
||||
ProcessStartInfo startInfo;
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "powershell.exe",
|
||||
Arguments = "Get-CimInstance Win32_Process -Filter \"\"\"name = 'LeagueClientUx.exe'\"\"\" | Select-Object -ExpandProperty CommandLine",
|
||||
WindowStyle = ProcessWindowStyle.Hidden,
|
||||
CreateNoWindow = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!OperatingSystem.IsMacOS())
|
||||
{
|
||||
throw new PlatformNotSupportedException("This behavior is only supported on Windows or MacOS.");
|
||||
}
|
||||
|
||||
startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "/bin/bash",
|
||||
Arguments = "-c \"ps x -o args | grep 'LeagueClientUx'\"",
|
||||
WindowStyle = ProcessWindowStyle.Hidden,
|
||||
CreateNoWindow = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true
|
||||
};
|
||||
}
|
||||
|
||||
using Process process2 = new()
|
||||
{
|
||||
StartInfo = startInfo
|
||||
};
|
||||
process2.Start();
|
||||
process2.WaitForExit();
|
||||
string text = process2.StandardOutput.ReadToEnd();
|
||||
string text2 = process2.StandardError.ReadToEnd();
|
||||
if (string.IsNullOrEmpty(text))
|
||||
{
|
||||
if (!string.IsNullOrEmpty(text2))
|
||||
{
|
||||
throw new Exception(text2);
|
||||
}
|
||||
|
||||
throw new FormatException("The output string is empty.");
|
||||
}
|
||||
|
||||
remotingAuthToken = AuthTokenRegex().Match(text).Groups[1].Value;
|
||||
appPort = int.Parse(AppPortRegex().Match(text).Groups[1].Value);
|
||||
exception = null;
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
remotingAuthToken = string.Empty;
|
||||
appPort = 0;
|
||||
exception = ex;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user