示例#1
0
 public static void StopRoutine()
 {
     MinecraftServer.StopRoutine();
     FactorioServer.StopRoutine();
     Wrapper.Mode = Wrapper.Modes.Menu;
     Wrapper.WriteLine("FMCBridge stopped successfully!");
 }
示例#2
0
 public static void ProcessInput(string InputText)
 {
     //Send it to the wrapper if it's a wrapper command
     if (InputText.StartsWith("wrapper"))
     {
         Wrapper.Command(InputText.Remove(0, 8));
     }
     else if (InputText == "/quit")
     {
         FactorioServer.StopRoutine();
     }
     //Send it to the Factorio server
     else
     {
         FactorioServer.Input.WriteLine(InputText);
     }
 }
        /*=======================================
        *  This subroutine starts up the server
        *  and then monitors the output.
        *  =======================================*/
        public static void Run()
        {
            FactorioServer.Loaded   = false;
            FactorioServer.Stopping = false;
            Console.ForegroundColor = ConsoleColor.Cyan;
            Wrapper.InputTarget     = Wrapper.Modes.Menu;

            Wrapper.WriteLine("Preparing Factorio server...");
            string SettingsFileName = "Settings.ini";
            string SettingsFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SettingsFileName);

            /*=======================================
            *  Create a settings file with some default values if it doesn't exist.
            *  These default values probably won't actually work, but it'll at least create the file.
            *  =======================================*/
            if (!File.Exists(SettingsFilePath))
            {
                //ds = DefaultSettings
                IniFile ds = new IniFile(SettingsFileName);
                ds.Write("Version", "1.14", "Minecraft");
            }

            /*=======================================
            *  Read the settings file to find the server path
            *  =======================================*/
            Wrapper.WriteLine("Reading wrapper settings file...");
            IniFile s = new IniFile(SettingsFileName);

            Console.Title = "Wrapper." + Wrapper.Version + " FactorioServer." + s.Read("Version", "Factorio");
            RootPath      = s.Read("ServerFolder", "Windows") + '\\';

            /*=======================================
            *  Setup some files related to logs and crap
            *  =======================================*/
            Wrapper.WriteLine("Configuring custom logging...");
            FactorioServer.CommandLog = new StreamWriter(RootPath + s.Read("CommandLogFile", "Wrapper"), true);
            Wrapper.WriteLine("Checking for previous unstopped servers...");
            PIDFile = RootPath + s.Read("PIDFile", "Factorio");
            if (File.Exists(PIDFile))
            {
                Int32 PreviousPID = Int32.Parse(File.ReadAllText(PIDFile));
                try
                {
                    using (Process PreviousServer = Process.GetProcessById(PreviousPID))
                        using (StreamWriter PreviousInput = PreviousServer.StandardInput)
                        {
                            PreviousInput.WriteLine("/quit");
                            //Make sure the server process has stopped
                            while (PreviousServer.HasExited == false)
                            {
                                ;
                            }
                        }
                    Wrapper.WriteLine("Previous server stopped successfully");
                }
                catch (Exception)
                {
                    Wrapper.WriteLine("No previous server process running");
                }
                File.Delete(PIDFile);
            }

            /*=======================================
            *  Configure the server process before starting it
            *  =======================================*/
            Wrapper.WriteLine("Configuring server process...");
            FactorioServer.Process = new Process();
            FactorioServer.Process.StartInfo.FileName               = s.Read("ExePath", "Factorio");
            FactorioServer.Process.StartInfo.Arguments              = s.Read("ServerArgs", "Factorio");
            FactorioServer.Process.StartInfo.CreateNoWindow         = true;
            FactorioServer.Process.StartInfo.WorkingDirectory       = RootPath;
            FactorioServer.Process.StartInfo.ErrorDialog            = false;
            FactorioServer.Process.StartInfo.UseShellExecute        = false;
            FactorioServer.Process.StartInfo.RedirectStandardError  = true;
            FactorioServer.Process.StartInfo.RedirectStandardOutput = true;
            FactorioServer.Process.StartInfo.RedirectStandardInput  = true;


            /*=======================================
            *  These are what read/print/process the server log.
            *  They'll be run whenever the server process outputs text,
            *  even while the code continues running below.
            *  =======================================*/
            //FileStream TempLog = File.Create("TempLog.txt");
            //StreamWriter TempWriter = new StreamWriter(TempLog);

            Wrapper.WriteLine("Configuring console output...");
            FactorioServer.Process.OutputDataReceived += new DataReceivedEventHandler
                                                         (
                (sender, OutputText) =>
            {
                if (string.IsNullOrWhiteSpace(OutputText.Data) == false)
                {
                    //if (FactorioServer.NoConsole) {
                    //    TempWriter.WriteLine("OLog " + OutputText.Data);
                    //}
                    //else {
                    //    Util.WriteToLog(OutputText.Data, FactorioServer.OutputFormat);
                    //}
                    if (!FactorioServer.NoConsole)
                    {
                        Util.WriteToLog(OutputText.Data, FactorioServer.OutputFormat);
                    }
                    if (FactorioServer.Stopping == true)
                    {
                        FactorioServer.Loaded = !OutputText.Data.Contains("changing state from(Disconnected) to(Closed)");
                    }
                    else if (!FactorioServer.Loaded)
                    {
                        FactorioServer.Loaded = OutputText.Data.Contains("changing state from(CreatingGame) to(InGame)");
                    }
                }
            }
                                                         );
            FactorioServer.Process.ErrorDataReceived += new DataReceivedEventHandler
                                                        (
                (sender, ErrorText) =>
            {
                if (string.IsNullOrWhiteSpace(ErrorText.Data) == false)
                {
                    //if (FactorioServer.NoConsole) {
                    //    TempWriter.WriteLine("ELog " + ErrorText.Data);
                    //}
                    //else {
                    //    Util.WriteToLog(ErrorText.Data, FactorioServer.ErrorFormat);
                    //}
                    if (!FactorioServer.NoConsole)
                    {
                        Util.WriteToLog(ErrorText.Data, FactorioServer.ErrorFormat);
                    }
                }
            }
                                                        );

            /*=======================================
            *  Finally start the dang server process
            *  =======================================*/
            Wrapper.WriteLine("Starting Factorio server...");
            FactorioServer.NoConsole = true;
            FreeConsole();
            FactorioServer.Process.Start();

            //Redirect the input so that the code can send text
            FactorioServer.Input = FactorioServer.Process.StandardInput;

            //Start checking for output
            FactorioServer.Process.BeginOutputReadLine();
            FactorioServer.Process.BeginErrorReadLine();

            //Don't try to do anything else until the server finishes loading
            while (FactorioServer.Loaded == false)
            {
                ;
            }
            AllocConsole();
            FactorioServer.NoConsole = false;
            //using (StreamReader TempReader = new StreamReader(TempLog)) {
            //    string TempLine;
            //    while (TempReader.Peek() >= 0) {
            //        TempLine = TempReader.ReadLine();
            //        if (TempLine.StartsWith("OLog ")) {
            //            Util.WriteToLog(TempLine.Remove(0, 5), FactorioServer.OutputFormat);
            //        }
            //        else if (TempLine.StartsWith("ELog ")) {
            //            Util.WriteToLog(TempLine.Remove(0, 5), FactorioServer.ErrorFormat);
            //        }
            //    }
            //}
            //TempWriter.Close();
            //TempLog.Close();
            Wrapper.WriteLine("Factorio server loaded!");

            Wrapper.Command("InputMode 2");
            Wrapper.WriteLine("Use \"wrapper InputMode 0\" to access Wrapper mode.");
            FactorioServer.Process.WaitForExit();

            /*=======================================
            *  This loop monitors for user input in the console
            *  and sends it to the appropriate process
            *  =======================================*/
            string ConsoleInput = "";

            //string ConsoleInput = Console.In.ReadLineAsync().;
            FactorioServer.Running = true;
            //FactorioServer.Process.
            do
            {
                try
                {
                    //Get user input
                    ConsoleInput = Console.ReadLine();
                    //FactorioServer.Input.WriteLine("test");

                    //If the user wasn't a squit
                    if (string.IsNullOrWhiteSpace(ConsoleInput) == false)
                    {
                        switch (Wrapper.InputTarget)
                        {
                        //Default to wrapper
                        case Wrapper.Modes.Menu:
                            Wrapper.Command(ConsoleInput);
                            break;

                        //Default to Minecraft server
                        case Wrapper.Modes.FactorioServer:
                            //Send it to the wrapper if it's a wrapper command
                            if (ConsoleInput.StartsWith("wrapper"))
                            {
                                Wrapper.Command(ConsoleInput.Remove(0, 7));
                            }
                            //If it's a stop command, switch to the stop routine
                            else if (ConsoleInput == "/stop")
                            {
                                FactorioServer.StopRoutine();
                            }
                            //else if (ConsoleInput == "stop-no-save")
                            //{
                            //    FactorioServer.StopRoutine(false);
                            //}
                            //Send it to the Minecraft server
                            else
                            {
                                //FactorioServer.Process.StandardInput.WriteLine(ConsoleInput);
                                FactorioServer.Input.WriteLine(ConsoleInput);
                            }
                            break;

                        default:
                            Wrapper.InputTarget = Wrapper.Modes.Menu;
                            throw new TrashMonkeyException("Invalid input mode! Defaulting to wrapper mode.");
                        }
                    }
                }
                catch (TrashMonkeyException e)  //Handled errors
                {
                    Wrapper.ErrorWriteLine(e.Message);
                }
                catch (Exception e)             //Something actually broke errors
                {
                    Util.PrintErrorInfo(e);
                }
                ConsoleInput = "";
            } while (FactorioServer.Running == true);
            //Exiting this loop should return to the menu
        }