Beispiel #1
0
        public async Task <bool> StartNetworkAsync(NetworkViewModel viewModel, bool startServers)
        {
            ConsoleWriter.Write("\n Starting network " + viewModel.Entity.Name, viewModel);
            Console.WriteLine("Starting network " + viewModel.Entity.Name);
            if (startServers)
            {
                if (viewModel.Servers.Count == 0)
                {
                    ConsoleWriter.Write("WARNING: This network contains no Servers. Players will not be able to join.", viewModel);
                }
                else
                {
                    ConsoleWriter.Write("Starting all " + viewModel.Servers.Count + " servers of this network...", viewModel);
                    foreach (NetworkServer networkServer in viewModel.Servers)
                    {
                        if (networkServer is NetworkForkServer networkForkServer)
                        {
                            ServerViewModel serverViewModel = networkForkServer.ServerViewModel;
                            if (serverViewModel.CurrentStatus == ServerStatus.STOPPED)
                            {
                                ConsoleWriter.Write("\nStarting server " + serverViewModel.Server + " on world: " +
                                                    serverViewModel.Server.ServerSettings.LevelName, viewModel);
                                ServerManager.Instance.StartServerAsync(serverViewModel);
                            }
                            else if (serverViewModel.CurrentStatus == ServerStatus.STARTING)
                            {
                                ConsoleWriter.Write("Server " + serverViewModel.Server + " is already starting.", viewModel);
                            }
                            else if (serverViewModel.CurrentStatus == ServerStatus.RUNNING)
                            {
                                ConsoleWriter.Write("Server " + serverViewModel.Server + " is already running.", viewModel);
                            }
                        }
                        else
                        {
                            ConsoleWriter.Write("Server " + networkServer.Name + " can't be started automatically because it is no Fork server.", viewModel);
                        }
                    }
                }
            }
            else
            {
                ConsoleWriter.Write("Make sure that at least one server configured in the settings is running, else Players won't be able to join this network.", viewModel);
            }

            ConsoleWriter.Write("\n", viewModel);
            if (!viewModel.SettingsSavingTask.IsCompleted)
            {
                ConsoleWriter.Write("Saving settings files before starting proxy server...", viewModel);
                await viewModel.SettingsSavingTask;
            }

            //Start proxy server
            ConsoleWriter.Write("Starting proxy server...", viewModel);
            DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(App.ServerPath, viewModel.Network.Name));

            if (!directoryInfo.Exists)
            {
                ConsoleWriter.Write("ERROR: Can't find network directory: " + directoryInfo.FullName, viewModel);
                return(false);
            }
            JavaVersion javaVersion = JavaVersionUtils.GetInstalledJavaVersion(viewModel.Network.JavaSettings.JavaPath);

            if (javaVersion == null)
            {
                ConsoleWriter.Write("ERROR: Java is not installed! Minecraft networks require Java!", viewModel);
                return(false);
            }
            if (!javaVersion.Is64Bit)
            {
                ConsoleWriter.Write("WARN: The Java installation selected for this network is a 32-bit version, which can cause errors.", viewModel);
            }
            if (javaVersion.VersionComputed < 11)
            {
                ConsoleWriter.Write("WARN: The Java installation selected for this network is outdated. Please update Java to version 11 or higher.", viewModel);
            }

            Process          process   = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                UseShellExecute        = false,
                RedirectStandardError  = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                FileName         = viewModel.Network.JavaSettings.JavaPath,
                WorkingDirectory = directoryInfo.FullName,
                Arguments        = "-Xmx" + viewModel.Network.JavaSettings.MaxRam + "m " + viewModel.Network.JavaSettings.StartupParameters + " -jar server.jar nogui",
                WindowStyle      = ProcessWindowStyle.Hidden,
                CreateNoWindow   = true
            };

            process.StartInfo = startInfo;
            process.Start();
            Task.Run(() =>
            {
                viewModel.TrackPerformance(process);
            });
            viewModel.CurrentStatus = ServerStatus.STARTING;
            ConsoleWriter.RegisterApplication(viewModel, process.StandardOutput, process.StandardError);
            ConsoleReader consoleReader = new ConsoleReader(process.StandardInput);

            viewModel.ConsoleReader = consoleReader;
            Task.Run(async() =>
            {
                await process.WaitForExitAsync();
                ApplicationManager.Instance.ActiveEntities.Remove(viewModel.Entity);
                viewModel.CurrentStatus = ServerStatus.STOPPED;
            });
            ApplicationManager.Instance.ActiveEntities[viewModel.Network] = process;
            Console.WriteLine("Started network " + viewModel.Network);

            return(true);
        }
Beispiel #2
0
        public bool StartNetwork(NetworkViewModel viewModel, bool startServers)
        {
            viewModel.ConsoleOutList.Add("\n Starting network " + viewModel.Entity.Name);
            Console.WriteLine("Starting network " + viewModel.Entity.Name);
            if (startServers)
            {
                if (viewModel.Servers.Count == 0)
                {
                    viewModel.ConsoleOutList.Add("WARNING: This network contains no Servers. Players will not be able to join.");
                }
                else
                {
                    viewModel.ConsoleOutList.Add("Starting all " + viewModel.Servers.Count + " servers of this network...");
                    foreach (NetworkServer networkServer in viewModel.Servers)
                    {
                        if (networkServer is NetworkForkServer networkForkServer)
                        {
                            ServerViewModel serverViewModel = networkForkServer.ServerViewModel;
                            if (serverViewModel.CurrentStatus == ServerStatus.STOPPED)
                            {
                                viewModel.ConsoleOutList.Add("\nStarting server " + serverViewModel.Server + " on world: " +
                                                             serverViewModel.Server.ServerSettings.LevelName);
                                ServerManager.Instance.StartServerAsync(serverViewModel);
                            }
                            else if (serverViewModel.CurrentStatus == ServerStatus.STARTING)
                            {
                                viewModel.ConsoleOutList.Add("Server " + serverViewModel.Server + " is already starting.");
                            }
                            else if (serverViewModel.CurrentStatus == ServerStatus.RUNNING)
                            {
                                viewModel.ConsoleOutList.Add("Server " + serverViewModel.Server + " is already running.");
                            }
                        }
                        else
                        {
                            viewModel.ConsoleOutList.Add("Server " + networkServer.Name + " can't be started automatically because it is no Fork server.");
                        }
                    }
                }
            }
            else
            {
                viewModel.ConsoleOutList.Add("Make sure that at least one server configured in the settings is running, else Players won't be able to join this network.");
            }

            //Start proxy server
            viewModel.ConsoleOutList.Add("Starting proxy server...");
            DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(App.ServerPath, viewModel.Network.Name));

            if (!directoryInfo.Exists)
            {
                viewModel.ConsoleOutList.Add("ERROR: Can't find network directory: " + directoryInfo.FullName);
                return(false);
            }

            Process          process   = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                UseShellExecute        = false,
                RedirectStandardError  = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                FileName         = "java.exe",
                WorkingDirectory = directoryInfo.FullName,
                Arguments        = "-Xmx" + viewModel.Network.JavaSettings.MaxRam + "m -Xms" +
                                   viewModel.Network.JavaSettings.MinRam + "m -jar server.jar nogui",
                WindowStyle    = ProcessWindowStyle.Hidden,
                CreateNoWindow = true
            };

            process.StartInfo = startInfo;
            process.Start();
            new Thread(() =>
            {
                viewModel.TrackPerformance(process);
            }).Start();
            viewModel.CurrentStatus = ServerStatus.STARTING;
            ConsoleWriter.RegisterApplication(viewModel, process.StandardOutput, process.StandardError);
            ConsoleReader consoleReader = new ConsoleReader(process.StandardInput);

            viewModel.ConsoleReader = consoleReader;
            new Thread(() =>
            {
                process.WaitForExit();
                ApplicationManager.Instance.ActiveEntities.Remove(viewModel.Entity);
                viewModel.CurrentStatus = ServerStatus.STOPPED;
            }).Start();
            ApplicationManager.Instance.ActiveEntities[viewModel.Network] = process;
            Console.WriteLine("Started network " + viewModel.Network);

            return(true);
        }