我不知道为什么会有人懒到这样,我也不知道为什么现在AI这么发达他们不拿AI帮自己做,但是总之这东西我做出来了
-
它会启动服务端之后检测6969是否运行的是spt,否则自动杀死进程
-
使用前需要先手动启动一次
-
把exe放进spt同一级目录然后双击
下载
SPTStarter.exe(访问密码:9684)
源码公开
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
class Program
{
const int TargetPort = 6969;
const string ServerExe = "SPT.Server.exe";
const string LauncherExe = "SPT.Launcher.exe";
static void Main()
{
string baseDir = AppContext.BaseDirectory;
string serverPath = Path.Combine(baseDir, ServerExe);
string launcherPath = Path.Combine(baseDir, LauncherExe);
if (!File.Exists(serverPath) || !File.Exists(launcherPath))
{
Console.WriteLine("Required files missing.");
Pause();
return;
}
StartServer(serverPath, baseDir);
while (true)
{
Thread.Sleep(1500);
int? pid = GetListeningProcessId(TargetPort);
if (pid == null)
{
Console.WriteLine("Port not listening yet...");
continue;
}
PrintProcessInfo(pid.Value);
var proc = SafeGetProcess(pid.Value);
if (proc == null)
continue;
string exeName = proc.ProcessName + ".exe";
if (!exeName.Equals(ServerExe, StringComparison.OrdinalIgnoreCase))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Unexpected process on port: {exeName}. Killing...");
Console.ResetColor();
TryKill(proc);
StartServer(serverPath, baseDir);
continue;
}
if (TestTcpConnection("127.0.0.1", TargetPort))
{
Console.WriteLine("TCP connection successful. Server ready.");
break;
}
Console.WriteLine("Listening but not ready yet...");
}
StartLauncher(launcherPath, baseDir);
}
static void StartServer(string path, string dir)
{
Console.WriteLine("Starting SPT.Server.exe...");
Process.Start(new ProcessStartInfo
{
FileName = path,
WorkingDirectory = dir,
UseShellExecute = true, // 新窗口
CreateNoWindow = false
});
}
static void StartLauncher(string path, string dir)
{
Console.WriteLine("Starting SPT.Launcher.exe...");
Process.Start(new ProcessStartInfo
{
FileName = path,
WorkingDirectory = dir,
UseShellExecute = true, // 新窗口
CreateNoWindow = false
});
}
static int? GetListeningProcessId(int port)
{
var props = IPGlobalProperties.GetIPGlobalProperties();
var listeners = props.GetActiveTcpListeners();
if (!listeners.Any(ep => ep.Port == port))
return null;
var psi = new ProcessStartInfo("netstat", "-ano -p tcp")
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using var p = Process.Start(psi);
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
foreach (var line in output.Split('\n'))
{
if (!line.Contains($":{port}") || !line.Contains("LISTENING"))
continue;
var parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 5 && int.TryParse(parts[^1], out int pid))
return pid;
}
return null;
}
static bool TestTcpConnection(string host, int port)
{
try
{
using var client = new TcpClient();
var task = client.ConnectAsync(host, port);
bool ok = task.Wait(2000);
return ok && client.Connected;
}
catch
{
return false;
}
}
static Process? SafeGetProcess(int pid)
{
try { return Process.GetProcessById(pid); }
catch { return null; }
}
static void TryKill(Process proc)
{
try
{
proc.Kill(true);
proc.WaitForExit(5000);
Console.WriteLine("Process killed.");
}
catch (Exception ex)
{
Console.WriteLine("Kill failed: " + ex.Message);
}
}
static void PrintProcessInfo(int pid)
{
try
{
var proc = Process.GetProcessById(pid);
Console.WriteLine($"Port {TargetPort} LISTENING by PID {pid}");
Console.WriteLine("---- Process Details ----");
Console.WriteLine($"Name: {proc.ProcessName}.exe");
Console.WriteLine($"PID: {proc.Id}");
Console.WriteLine($"Parent PID: {GetParentPid(pid)}");
Console.WriteLine($"Path: {proc.MainModule?.FileName}");
Console.WriteLine($"CommandLine: {GetCommandLine(pid)}");
Console.WriteLine("--------------------------");
}
catch
{
Console.WriteLine("Unable to read process info.");
}
}
static int GetParentPid(int pid)
{
using var searcher = new ManagementObjectSearcher(
$"SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {pid}");
foreach (ManagementObject obj in searcher.Get())
return Convert.ToInt32(obj["ParentProcessId"]);
return -1;
}
static string GetCommandLine(int pid)
{
using var searcher = new ManagementObjectSearcher(
$"SELECT CommandLine FROM Win32_Process WHERE ProcessId = {pid}");
foreach (ManagementObject obj in searcher.Get())
return obj["CommandLine"]?.ToString() ?? "";
return "";
}
static void Pause()
{
Console.WriteLine("Program will remain open. Press any key to exit.");
Console.ReadKey();
}
}








