Page 1 of 1

[17.79] Automating server commands - hooking up to external process

Posted: Mon Apr 27, 2020 10:47 am
by Tybs
Hi all,

My apologies if this is the wrong section, but it seemed to be the most appropriate one for the question.

Many server commands take a long time to write, so I'm trying to create some "shortcuts" for the ones I use the most. I wrote a small program for the command conversion in C#. Now I need to send the converted input to the server process. The problem is: although I can successfully read the output that the server process creates (in game commands / chat etc.), the generated input (commands such as "/c game.player.teleport({X, Y})") is not being received by the server.

I'm using a default C# solution for the hookup, see the code below:

Code: Select all

    class ConsoleManager
    {
        Process ServerProcess = new Process();

        public ConsoleManager()
        {
            InitializeInterpreter();
        }

        private void InitializeInterpreter()
        {
            var workingDir = @"E:\Program Files (x86)\Steam\steamapps\common\Factorio\bin\x64\";

            var psi = new ProcessStartInfo
            {
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                StandardErrorEncoding = Encoding.UTF8,
                StandardOutputEncoding = Encoding.UTF8,
                WindowStyle = ProcessWindowStyle.Hidden,
                CreateNoWindow = true,
                WorkingDirectory = workingDir,
                FileName = $"{workingDir}factorio",
                Arguments = @"--server-settings server-settings.json --start-server tybsania.zip""",
            };

            ServerProcess = new Process
            {
                StartInfo = psi,
                EnableRaisingEvents = true
            };

            ServerProcess.Start();

            ServerProcess.OutputDataReceived += (o, e) =>
            {
                var err = "";
                if (e.Data == null) err = e.Data;
                else Console.WriteLine(e.Data);
            };
            ServerProcess.BeginOutputReadLine();

            ServerProcess.ErrorDataReceived += (o, e) =>
            {
                var err = "";
                if (e.Data == null) err = e.Data;
                else Console.WriteLine(e.Data);
            };

            ServerProcess.BeginErrorReadLine();
        }

        public void SendInput()
        {
            ServerProcess.StandardInput.WriteLine($"Test");
            ServerProcess.StandardInput.Flush();
        }
    }
It seems to be an issue at the side of the server process, otherwise I wouldn't ask it here: hooking up with other processes works without problems. Of course, I could be misunderstanding some crucial element about how the server receives its input. Does anyone have a clue on why the output is successfully retrieved, yet the input does not arrive at all?

Thanks in advance for any help! :)