private async void Button_Import_Click(object sender, RoutedEventArgs e) { if (pImporter != null) { if (!pImporter.HasExited) { pImporter.Kill(); } pImporter = null; } if (button_Import.Content.Equals("Cancel Import")) { serverConfig.DeleteServerDirectory(); textbox_name.IsEnabled = true; comboBox.IsEnabled = true; textbox_ServerDir.IsEnabled = true; button_Browse.IsEnabled = true; progressbar_progress.IsIndeterminate = false; textblock_progress.Text = "[ERROR] Fail to import"; button_Import.Content = "Import"; 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" : ""; label_ServerDirWarn.Content = (!Directory.Exists(textbox_ServerDir.Text)) ? "Server Dir is invalid" : ""; if (string.IsNullOrWhiteSpace(textbox_name.Text) || selectedgame == null || !Directory.Exists(textbox_ServerDir.Text)) { return; } string servername = textbox_name.Text; string servergame = selectedgame.Name; dynamic gameServer = GameServer.Data.Class.Get(servergame, serverConfig); if (!gameServer.IsImportValid(textbox_ServerDir.Text)) { label_ServerDirWarn.Content = gameServer.Error; return; } string installPath = MainWindow.WGSM_PATH + @"\servers\" + serverConfig.ServerID + @"\serverfiles"; if (Directory.Exists(installPath)) { await Task.Run(() => { try { Directory.Delete(installPath, true); } catch { } }); if (Directory.Exists(installPath)) { System.Windows.Forms.MessageBox.Show(installPath + " is not accessible!", "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return; } } //Import start textbox_name.IsEnabled = false; comboBox.IsEnabled = false; textbox_ServerDir.IsEnabled = false; button_Browse.IsEnabled = false; progressbar_progress.IsIndeterminate = true; textblock_progress.Text = "Importing"; button_Import.Content = "Cancel Import"; bool isImportSuccess = false; string xcopyPath = Environment.GetEnvironmentVariable("WINDIR") + @"\System32\xcopy.exe"; ProcessStartInfo psi = new ProcessStartInfo { FileName = xcopyPath, Arguments = string.Format("\"{0}\" \"{1}\" /E /I /-Y", textbox_ServerDir.Text, installPath), WindowStyle = ProcessWindowStyle.Minimized }; pImporter = Process.Start(psi); await Task.Run(() => pImporter.WaitForExit()); if (pImporter == null) { textbox_name.IsEnabled = true; comboBox.IsEnabled = true; textbox_ServerDir.IsEnabled = true; button_Browse.IsEnabled = true; progressbar_progress.IsIndeterminate = false; textblock_progress.Text = "[ERROR] Fail to import"; button_Import.Content = "Import"; return; } if (pImporter.HasExited) { if (pImporter.ExitCode == 0) { isImportSuccess = true; } } if (isImportSuccess) { // Create WindowsGSM.cfg serverConfig.SetData(servergame, servername, gameServer); serverConfig.CreateWindowsGSMConfig(); System.Windows.Application.Current.Dispatcher.Invoke(() => { MainWindow WindowsGSM = (MainWindow)System.Windows.Application.Current.MainWindow; WindowsGSM.LoadServerTable(); WindowsGSM.Log(serverConfig.ServerID, "Import: Success"); Close(); }); } else { textbox_name.IsEnabled = true; comboBox.IsEnabled = true; textbox_ServerDir.IsEnabled = true; button_Browse.IsEnabled = true; progressbar_progress.IsIndeterminate = false; textblock_progress.Text = "[ERROR] Fail to import"; button_Import.Content = "Import"; } }
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"; } }