• 注册
  • 一般交流 一般交流 关注:6157 内容:9750

    一键启动

  • 查看作者
  • 打赏作者
  • 当前位置: ODDBA社区 > 离线版交流区 > 一般交流 > 正文
  • 一般交流
  • 渐入佳境
    2021

    我不知道为什么会有人懒到这样,我也不知道为什么现在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();
        }
    }

    请登录之后再进行评论

    登录
    离线版交流区
  • 今日 3
  • 内容 11324
  • 关注 6157
  • 聊天
    关注 143

    【招募】GRIFFIN TKF项目开工 期待你的加入 || 你是否想加入格里芬书写自己与人形的故事

    捐助我们

    • 微信
    • 支付宝
  • 签到
  • 任务
  • 发布
  • 模式切换
  • 偏好设置
  • 帖子间隔 侧栏位置: