Add project files.
This commit is contained in:
59
LeagueAPI/Utils/ProcessFinder.cs
Normal file
59
LeagueAPI/Utils/ProcessFinder.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System.Diagnostics;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace LeagueAPI.Utils;
|
||||
|
||||
public static class ProcessFinder
|
||||
{
|
||||
public static Process GetProcess()
|
||||
{
|
||||
Process[] processesByName = Process.GetProcessesByName("LeagueClientUx");
|
||||
return processesByName.FirstOrDefault(p => p.ProcessName == "LeagueClientUx")
|
||||
?? throw new InvalidOperationException("Failed to find LCUx process.");
|
||||
}
|
||||
|
||||
public static ProcessInfo GetProcessInfo()
|
||||
{
|
||||
return new ProcessInfo(GetProcess());
|
||||
}
|
||||
|
||||
public static bool IsActive()
|
||||
{
|
||||
try
|
||||
{
|
||||
GetProcessInfo();
|
||||
return true;
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsPortOpen()
|
||||
{
|
||||
try
|
||||
{
|
||||
return IsPortOpen(GetProcessInfo());
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsPortOpen(ProcessInfo processInfo)
|
||||
{
|
||||
try
|
||||
{
|
||||
using TcpClient tcpClient = new();
|
||||
tcpClient.Connect("127.0.0.1", processInfo.AppPort);
|
||||
tcpClient.Close();
|
||||
return true;
|
||||
}
|
||||
catch (SocketException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user