public string GetAvailablePort(string defaultport, int increment) { MainWindow WindowsGSM = (MainWindow)System.Windows.Application.Current.MainWindow; int[] portlist = new int[WindowsGSM.ServerGrid.Items.Count]; for (int i = 0; i < WindowsGSM.ServerGrid.Items.Count; i++) { ServerTable row = WindowsGSM.ServerGrid.Items[i] as ServerTable; portlist[i] = int.Parse(string.IsNullOrWhiteSpace(row.Port) ? "0" : row.Port); } Array.Sort(portlist); int port = int.Parse(defaultport); for (int i = 0; i < WindowsGSM.ServerGrid.Items.Count; i++) { if (port == portlist[i] || port == 27020) //SourceTV port 27020 { port += increment; } } return(port.ToString()); }
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; if (!gameServerAction.CanImport(servergame, textbox_ServerDir.Text)) { label_ServerDirWarn.Content = gameServerAction.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) { gameServerAction.CreateServerConfigs(servergame, servername); MainWindow WindowsGSM = (MainWindow)System.Windows.Application.Current.MainWindow; Functions.ServerTable row = new Functions.ServerTable { ID = serverConfig.ServerID, Game = serverConfig.ServerGame, Icon = "/WindowsGSM;component/" + 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, "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"; string servername = textbox_name.Text; string servergame = selectedgame.Name; serverConfig.CreateServerDirectory(); pInstaller = await gameServerAction.Run(servergame); string error = gameServerAction.Error; bool IsSuccess = await gameServerAction.IsSuccess(pInstaller, servergame, servername); if (IsSuccess) { MainWindow WindowsGSM = (MainWindow)System.Windows.Application.Current.MainWindow; Functions.ServerTable row = new Functions.ServerTable { ID = serverConfig.ServerID, Game = serverConfig.ServerGame, Icon = "/WindowsGSM;component/" + 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 {error}"; } button_install.Content = "Install"; } }