private void OnServerStop(bool ServerForceClose) { UpdateCpuRamUsageTimer(false); ServerIsRunning = false; ServerProcess.CancelOutputRead(); BrowseServerFile.IsEnabled = true; ServerFilePath.IsEnabled = true; ForceOnlineMode.IsEnabled = true; ramLimit.IsEnabled = true; KickAll.IsEnabled = false; BanAll.IsEnabled = false; opAll.IsEnabled = false; deopAll.IsEnabled = false; SendCommand.IsEnabled = false; if (ServerForceClose) { ServerOutputWindow.AppendText("\nServer Force Closed"); } if (!ServerForceClose && ServerProcess.HasExited) { ServerOutputWindow.AppendText("\nServer Closed"); } StartStopServer.IsEnabled = true; StartStopServer.Content = "Start Server"; }
//Handle data recieved from server private void ServerOutput_OutputDataRecieved(object sender, DataReceivedEventArgs e) { Dispatcher.InvokeAsync(() => { try { if (e.Data.Contains("WARN")) { ServerOutputWindow.AppendText(e.Data + "\n"); TextRange WarnOutputTextRange = new TextRange(ServerOutputWindow.Document.ContentEnd, ServerOutputWindow.Document.ContentEnd); WarnOutputTextRange.Text = e.Data; WarnOutputTextRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(WarningOutputColor)); } else if (e.Data.Contains("ERROR")) { ServerOutputWindow.AppendText(e.Data + "\n"); TextRange ErrorOutputTextRange = new TextRange(ServerOutputWindow.Document.ContentEnd, ServerOutputWindow.Document.ContentEnd); ErrorOutputTextRange.Text = e.Data; ErrorOutputTextRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(ErrorOutputColor)); } else if (e.Data.Contains("logged in with") || e.Data.Contains("left the game")) { ServerOutputWindow.AppendText(e.Data + "\n"); TextRange PlayEventTextRange = new TextRange(ServerOutputWindow.Document.ContentEnd, ServerOutputWindow.Document.ContentEnd); PlayEventTextRange.Text = e.Data; PlayEventTextRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(PlayerEventOutputColor)); } else if (e.Data.Contains("Done") && e.Data.Contains("For help, type \"help\"")) { ServerOutputWindow.AppendText(e.Data + "\n"); TextRange PlayEventTextRange = new TextRange(ServerOutputWindow.Document.ContentEnd, ServerOutputWindow.Document.ContentEnd); PlayEventTextRange.Text = e.Data; PlayEventTextRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(ServerDoneLoadingColor)); } else { ServerOutputWindow.AppendText(e.Data + "\n"); TextRange DefaultOutputTextRange = new TextRange(ServerOutputWindow.Document.ContentEnd, ServerOutputWindow.Document.ContentEnd); DefaultOutputTextRange.Text = e.Data; DefaultOutputTextRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(DefaultOutputColor)); } } catch (Exception) { } if (ServerOutputWindow_AutoScroll) { ServerOutputWindow.ScrollToEnd(); } }); }
//World Tab Options private async void Worlds_Action(object sender, RoutedEventArgs e) { //Backup Specific World if (sender == Worlds_Backup) { //Check backups directory exists if (!Directory.Exists(WorkingDirectory + @"\Backups")) { Directory.CreateDirectory(WorkingDirectory + @"\Backups"); } //Create Folder to hold backups of current time string DestinationFolder = WorkingDirectory + "\\Backups\\" + DateTime.Now.ToString("dd-mmmm-yyyy-_-HH-mm-ss"); Directory.CreateDirectory(DestinationFolder); //Find all world folders await Task.Run(() => { Dispatcher.Invoke(() => { Directory.CreateDirectory(DestinationFolder + "\\" + WorldsTabListBox.SelectedItem.ToString()); ServerOutputWindow.AppendText("\n[Server Wrapper] Backing up world \"" + WorldsTabListBox.SelectedItem.ToString() + "\""); try { DirectoryCopy(WorkingDirectory + "\\" + WorldsTabListBox.SelectedItem.ToString(), DestinationFolder + "\\" + WorldsTabListBox.SelectedItem.ToString(), true); ServerOutputWindow.AppendText("\n[Server Wrapper] Finished backing up world \"" + WorldsTabListBox.SelectedItem.ToString() + "\""); } catch (Exception f) { ServerOutputWindow.AppendText(f.ToString()); ServerOutputWindow.AppendText("\n[Server Wrapper] Failed to backup world \"" + WorldsTabListBox.SelectedItem.ToString() + "\""); } }); }); } //Open World in Explorer if (sender == Worlds_OpenInExplorer) { Process.Start("explorer.exe", WorkingDirectory + "\\" + WorldsTabListBox.SelectedItem.ToString()); } //Delete World if (sender == Worlds_Delete) { Directory.Delete(WorkingDirectory + "\\" + WorldsTabListBox.SelectedItem.ToString()); } }
//On server start and stop private void OnServerStart() { //Check that server arguements are valid if (ServerArgs == null) { ServerProcess.StartInfo.WorkingDirectory = WorkingDirectory.ToString(); ServerOutputWindow.AppendText("java -Xmx" + ramLimit.Text + "M -jar \"" + ServerFilePath.Text + "\" nogui\n"); ServerArgs = new ProcessStartInfo("java", "-Xmx" + ramLimit.Text + "M -jar \"" + ServerFilePath.Text + "\" nogui"); ServerArgs.RedirectStandardInput = true; ServerArgs.RedirectStandardOutput = true; ServerArgs.UseShellExecute = false; ServerArgs.CreateNoWindow = true; } //Start Server ServerProcess.StartInfo = ServerArgs; ServerProcess.StartInfo.WorkingDirectory = WorkingDirectory.ToString(); ServerProcess.EnableRaisingEvents = true; ServerProcess.OutputDataReceived += new DataReceivedEventHandler(ServerOutput_OutputDataRecieved); ServerProcess.Exited += new EventHandler(ServerClose_Exited); ServerProcess.Start(); ServerProcess.BeginOutputReadLine(); ServerIsRunning = true; BrowseServerFile.IsEnabled = false; ServerFilePath.IsEnabled = false; ForceOnlineMode.IsChecked = false; ramLimit.IsEnabled = false; KickAll.IsEnabled = true; BanAll.IsEnabled = true; opAll.IsEnabled = true; deopAll.IsEnabled = true; SendCommand.IsEnabled = true; StartStopServer.ToolTip = "Shift Click to Force Stop Server"; StartStopServer.Content = "Stop Server"; StatusIndicator.Content = "Server is Running | Server Process ID: " + ServerProcess.Id; StatusLightColor(2); UpdateCpuRamUsageTimer(true); }
private async void BackupWorld_Click(object sender, RoutedEventArgs e) { BackupWorld.IsEnabled = false; BackupWorld.Content = "Backing up Worlds"; ServerOutputWindow.AppendText("\n[Server Wrapper] Starting Backup"); //Check backups directory exists if (!Directory.Exists(WorkingDirectory + @"\Backups")) { Directory.CreateDirectory(WorkingDirectory + @"\Backups"); } //Create Folder to hold backups of current time string DestinationFolder = WorkingDirectory + "\\Backups\\" + DateTime.Now.ToString("dd-mm-yyyy-_-HH-mm-ss"); Directory.CreateDirectory(DestinationFolder); //Find all world folders and copy await Task.Run(() => { foreach (var item in Directory.EnumerateDirectories(WorkingDirectory.ToString())) { if (File.Exists(item + "\\level.dat")) { string[] WorldName = item.Split('\\'); Directory.CreateDirectory(DestinationFolder + "\\" + WorldName[WorldName.Length - 1]); Dispatcher.Invoke(() => { ServerOutputWindow.AppendText("\n[Server Wrapper] Backing up world \"" + WorldName[WorldName.Length - 1] + "\""); }); try { DirectoryCopy(item, DestinationFolder + "\\" + WorldName[WorldName.Length - 1], true); } catch (Exception f) { ServerOutputWindow.AppendText(f.ToString()); } Dispatcher.Invoke(() => { ServerOutputWindow.AppendText("\n[Server Wrapper] Finished backing up world \"" + WorldName[WorldName.Length - 1] + "\""); }); } } }); ServerOutputWindow.AppendText("\n[Server Wrapper] Backuping Worlds Complete"); BackupWorld.Content = "Backup Server World(s)"; BackupWorld.IsEnabled = true; }
private void ServerOutputWindow_AutoScrollButton_Click(object sender, RoutedEventArgs e) { ServerOutputWindow_AutoScroll = true; ServerOutputWindow_AutoScrollButton.Opacity = 0; ServerOutputWindow.ScrollToEnd(); }