Ejemplo n.º 1
0
        private static void RunUpdate(bool noDelay)
        {
            var endpoint = new IPEndPoint(IPAddress.Parse(ServerIP), RCONPort);

            server = ServerQuery.GetServerInstance(QueryMaster.EngineType.Source, endpoint);

            //
            // Find the process
            //
            var     runningProcesses = Process.GetProcessesByName("ShooterGameServer.exe");
            var     expectedExePath  = ServerUpdater.NormalizePath(Path.Combine(InstallDirectory, "ShooterGame", "Binaries", "Win64", "ShooterGameServer.exe"));
            Process process          = null;

            foreach (var runningProcess in runningProcesses)
            {
                var runningPath = ServerUpdater.NormalizePath(runningProcess.Modules[0].FileName);
                if (String.Equals(expectedExePath, runningPath))
                {
                    process = runningProcess;
                    break;
                }
            }

            if (process == null)
            {
                Console.WriteLine("ERROR: Unable to find process for " + InstallDirectory);
                Environment.Exit(-1);
            }

            var serverCommandLine = ServerUpdater.GetCommandLineForProcess(process.Id);

            if (String.IsNullOrEmpty(serverCommandLine))
            {
                Console.WriteLine("ERROR: Unable to retrieve command-line for process " + process.Id);
                Environment.Exit(-1);
            }

            //
            // Stop the server
            //
            Console.WriteLine("Stopping the server...");
            process.EnableRaisingEvents = true;
            if (process.ProcessName == "ShooterGameServer.exe")
            {
                TaskCompletionSource <bool> ts = new TaskCompletionSource <bool>();
                EventHandler handler           = (s, e) => ts.TrySetResult(true);
                process.Exited += handler;
                process.CloseMainWindow();
                if (!process.HasExited)
                {
                    ts.Task.Wait();
                }
            }

            if (!UpdateCache())
            {
                Environment.Exit(-1);
            }

            Console.WriteLine("Re-launching the server");
            process = Process.Start(serverCommandLine);
            if (process == null)
            {
                Console.WriteLine("Failed to restart server.");
                Environment.Exit(-1);
            }

            //
            // Re-schedule updater
            //
            Console.WriteLine("TODO: Reschedule update");

            Environment.Exit(0);
        }
        private static ServerProcessStatus GetServerProcessStatus(ServerStatusUpdateRegistration updateContext, out Process serverProcess)
        {
            serverProcess = null;
            if (String.IsNullOrWhiteSpace(updateContext.InstallDirectory))
            {
                return(ServerProcessStatus.NotInstalled);
            }

            var serverExePath = Path.Combine(updateContext.InstallDirectory, Config.Default.ServerBinaryRelativePath, Config.Default.ServerExe);

            if (!File.Exists(serverExePath))
            {
                return(ServerProcessStatus.NotInstalled);
            }

            //
            // The server appears to be installed, now determine if it is running or stopped.
            //
            try
            {
                foreach (var process in Process.GetProcessesByName(Config.Default.ServerProcessName))
                {
                    var commandLine = ServerUpdater.GetCommandLineForProcess(process.Id);

                    if (commandLine.Contains(updateContext.InstallDirectory) && commandLine.Contains(Config.Default.ServerExe))
                    {
                        // Does this match our server exe and port?
                        var serverArgMatch = String.Format(Config.Default.ServerCommandLineArgsMatchFormat, updateContext.LocalEndpoint.Port);
                        if (commandLine.Contains(serverArgMatch))
                        {
                            // Was an IP set on it?
                            var anyIpArgMatch = String.Format(Config.Default.ServerCommandLineArgsIPMatchFormat, String.Empty);
                            if (commandLine.Contains(anyIpArgMatch))
                            {
                                // If we have a specific IP, check for it.
                                var ipArgMatch = String.Format(Config.Default.ServerCommandLineArgsIPMatchFormat, updateContext.LocalEndpoint.Address.ToString());
                                if (!commandLine.Contains(ipArgMatch))
                                {
                                    // Specific IP set didn't match
                                    continue;
                                }

                                // Specific IP matched
                            }

                            // Either specific IP matched or no specific IP was set and we will claim this is ours.

                            process.EnableRaisingEvents = true;
                            if (process.HasExited)
                            {
                                return(ServerProcessStatus.Stopped);
                            }

                            serverProcess = process;
                            return(ServerProcessStatus.Running);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Debug("Exception while checking process status: {0}\n{1}", ex.Message, ex.StackTrace);
            }

            return(ServerProcessStatus.Stopped);
        }