Exemplo n.º 1
0
        private async void Button_install_Click(object sender, RoutedEventArgs e)
        {
            if (pInstaller != null)
            {
                if (!pInstaller.HasExited)
                {
                    pInstaller.Kill();
                }

                pInstaller = null;
            }

            if (button_install.Content.Equals("Cancel Installation"))
            {
                serverConfig.DeleteServerDirectory();

                textbox_name.IsEnabled = true;
                comboBox.IsEnabled     = true;
                progressbar_progress.IsIndeterminate = false;
                textblock_progress.Text = "Fail to install";
                button_install.Content  = "Install";

                return;
            }

            Images.Row selectedgame = comboBox.SelectedItem as Images.Row;
            label_gamewarn.Content = (selectedgame == null) ? "Please select a game server" : "";
            label_namewarn.Content = (string.IsNullOrWhiteSpace(textbox_name.Text)) ? "Server name cannot be null" : "";

            if (string.IsNullOrWhiteSpace(textbox_name.Text) || selectedgame == null)
            {
                return;
            }

            string installPath = MainWindow.WGSM_PATH + @"\servers\" + serverConfig.ServerID + @"\serverfiles";

            if (Directory.Exists(installPath))
            {
                try
                {
                    Directory.Delete(installPath, true);
                }
                catch
                {
                    System.Windows.Forms.MessageBox.Show(installPath + " is not accessible!", "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                Directory.CreateDirectory(installPath);
            }

            //Installation start
            textbox_name.IsEnabled = false;
            comboBox.IsEnabled     = false;
            progressbar_progress.IsIndeterminate = true;

            textblock_progress.Text = "Installing";
            button_install.Content  = "Cancel Installation";

            string servername = textbox_name.Text;
            string servergame = selectedgame.Name;

            serverConfig.CreateServerDirectory();

            pInstaller = await gameServerAction.Run(servergame);

            bool IsSuccess = await gameServerAction.IsSuccess(pInstaller, servergame, servername);

            if (IsSuccess)
            {
                MainWindow WindowsGSM = (MainWindow)System.Windows.Application.Current.MainWindow;

                Function.ServerTable row = new Function.ServerTable
                {
                    ID         = serverConfig.ServerID,
                    Game       = serverConfig.ServerGame,
                    Icon       = GameServer.Data.Icon.ResourceManager.GetString(servergame),
                    Status     = "Stopped",
                    Name       = serverConfig.ServerName,
                    IP         = serverConfig.ServerIP,
                    Port       = serverConfig.ServerPort,
                    Defaultmap = serverConfig.ServerMap,
                    Maxplayers = serverConfig.ServerMaxPlayer
                };
                WindowsGSM.ServerGrid.Items.Add(row);
                WindowsGSM.LoadServerTable();
                WindowsGSM.Log(serverConfig.ServerID, "Install: Success");

                Close();
            }
            else
            {
                textbox_name.IsEnabled = true;
                comboBox.IsEnabled     = true;
                progressbar_progress.IsIndeterminate = false;

                if (pInstaller != null)
                {
                    textblock_progress.Text = "Fail to install [ERROR] Exit code: " + pInstaller.ExitCode.ToString();
                }
                else
                {
                    textblock_progress.Text = "Fail to install";
                }

                button_install.Content = "Install";
            }
        }
Exemplo n.º 2
0
        private async void Button_install_Click(object sender, RoutedEventArgs e)
        {
            if (pInstaller != null)
            {
                if (!pInstaller.HasExited)
                {
                    pInstaller.Kill();
                }

                pInstaller = null;
            }

            if (button_install.Content.Equals("Cancel Installation"))
            {
                serverConfig.DeleteServerDirectory();

                textbox_name.IsEnabled = true;
                comboBox.IsEnabled     = true;
                progressbar_progress.IsIndeterminate = false;
                textblock_progress.Text = "Fail to install";
                button_install.Content  = "Install";

                return;
            }

            Images.Row selectedgame = comboBox.SelectedItem as Images.Row;
            label_gamewarn.Content = (selectedgame == null) ? "Please select a game server" : "";
            label_namewarn.Content = (string.IsNullOrWhiteSpace(textbox_name.Text)) ? "Server name cannot be null" : "";

            if (string.IsNullOrWhiteSpace(textbox_name.Text) || selectedgame == null)
            {
                return;
            }

            string installPath = MainWindow.WGSM_PATH + @"\servers\" + serverConfig.ServerID + @"\serverfiles";

            if (Directory.Exists(installPath))
            {
                try
                {
                    Directory.Delete(installPath, true);
                }
                catch
                {
                    System.Windows.Forms.MessageBox.Show(installPath + " is not accessible!", "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                Directory.CreateDirectory(installPath);
            }

            //Installation start
            textbox_name.IsEnabled = false;
            comboBox.IsEnabled     = false;
            progressbar_progress.IsIndeterminate = true;

            textblock_progress.Text = "Installing";
            button_install.Content  = "Cancel Installation";

            textbox_installlog.Text = "";

            string servername = textbox_name.Text;
            string servergame = selectedgame.Name;

            serverConfig.CreateServerDirectory();

            dynamic gameServer = GameServer.ClassObject.Get(servergame, serverConfig);

            pInstaller = await gameServer.Install();

            if (pInstaller != null)
            {
                //Wait installer exit. Example: steamcmd.exe
                await Task.Run(() =>
                {
                    var reader = pInstaller.StandardOutput;
                    while (!reader.EndOfStream)
                    {
                        var nextLine = reader.ReadLine();
                        if (nextLine.Contains("Logging in user "))
                        {
                            nextLine += System.Environment.NewLine + "Please send the Login Token:";
                        }

                        System.Windows.Application.Current.Dispatcher.Invoke(() =>
                        {
                            textbox_installlog.AppendText(nextLine + System.Environment.NewLine);
                        });
                    }

                    pInstaller?.WaitForExit();
                });
            }

            if (gameServer.IsInstallValid())
            {
                serverConfig.ServerIP   = serverConfig.GetIPAddress();
                serverConfig.ServerPort = serverConfig.GetAvailablePort(gameServer.Port, gameServer.PortIncrements);

                //Create WindowsGSM.cfg
                serverConfig.CreateWindowsGSMConfig(servergame, servername, serverConfig.ServerIP, serverConfig.ServerPort, gameServer.Defaultmap, gameServer.Maxplayers, "", gameServer.Additional, gameServer.ToggleConsole);

                //Create game server config
                try
                {
                    gameServer = GameServer.ClassObject.Get(servergame, serverConfig);
                    gameServer.CreateServerCFG();
                }
                catch
                {
                }

                System.Windows.Application.Current.Dispatcher.Invoke(() =>
                {
                    MainWindow WindowsGSM = (MainWindow)Application.Current.MainWindow;
                    WindowsGSM.LoadServerTable();
                    WindowsGSM.Log(serverConfig.ServerID, "Install: Success");

                    if (WindowsGSM.MahAppSwitch_SendStatistics.IsChecked ?? false)
                    {
                        var analytics = new Functions.GoogleAnalytics();
                        analytics.SendGameServerInstall(serverConfig.ServerID, servergame);
                    }

                    Close();
                });
            }
            else
            {
                textbox_name.IsEnabled = true;
                comboBox.IsEnabled     = true;
                progressbar_progress.IsIndeterminate = false;

                if (pInstaller != null)
                {
                    textblock_progress.Text = "Fail to install [ERROR] Exit code: " + pInstaller.ExitCode.ToString();
                }
                else
                {
                    textblock_progress.Text = $"Fail to install {gameServer.Error}";
                }

                button_install.Content = "Install";
            }
        }