From 3c038f5924595bf13a9c3e9f3fb1509a657b3ade Mon Sep 17 00:00:00 2001 From: Razzmatazz Date: Mon, 22 Mar 2021 20:51:32 -0500 Subject: [PATCH] Fix for immediately asking to shutdown servers that autostart. --- MainWindow.xaml | 32 +++++++++-- MainWindow.xaml.cs | 91 ++++++++++++++++++++++++++----- Properties/Settings.Designer.cs | 24 +++++++++ Properties/Settings.settings | 6 +++ Resources/Bug_32x.png | Bin 0 -> 379 bytes ServerDetailsWindow.xaml | 41 +++++++++----- ServerDetailsWindow.xaml.cs | 93 +++++++++++++++++++++----------- ValheimServer.cs | 25 ++++++++- ValheimServerWarden.csproj | 8 +-- uMod.cs | 6 +-- uModJson.cs | 12 ----- 11 files changed, 256 insertions(+), 82 deletions(-) create mode 100644 Resources/Bug_32x.png delete mode 100644 uModJson.cs diff --git a/MainWindow.xaml b/MainWindow.xaml index 64e189a..2f877fe 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -7,7 +7,7 @@ ui:WindowHelper.UseModernWindowStyle="True" xmlns:local="clr-namespace:ValheimServerWarden" mc:Ignorable="d" - Title="Valheim Server Warden" Height="450" Width="800" Closed="Window_Closed" StateChanged="Window_StateChanged" Closing="Window_Closing" Icon="/Resources/vsw2_256.png" IsVisibleChanged="Window_IsVisibleChanged"> + Title="Valheim Server Warden" Height="475" Width="800" Closed="Window_Closed" StateChanged="Window_StateChanged" Closing="Window_Closing" Icon="/Resources/vsw2_256.png" IsVisibleChanged="Window_IsVisibleChanged"> @@ -103,6 +103,7 @@ + @@ -134,10 +135,24 @@ - - + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 413964a..8222415 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -49,6 +49,13 @@ private string ServerJsonPath return "valheim_servers.json"; } } + private string LogPath + { + get + { + return "vswlog.txt"; + } + } public MainWindow() { InitializeComponent(); @@ -58,10 +65,10 @@ public MainWindow() Properties.Settings.Default.UpgradeRequired = false; Properties.Settings.Default.Save(); } - Width = Properties.Settings.Default.MainWindowWidth; + //Width = Properties.Settings.Default.MainWindowWidth; if (Properties.Settings.Default.MainWindowHeight > 0) { - Height = Properties.Settings.Default.MainWindowHeight; + //Height = Properties.Settings.Default.MainWindowHeight; } LogEntry.NormalColor = ((SolidColorBrush)this.Foreground).Color; logEntries = new List(); @@ -79,7 +86,7 @@ public MainWindow() if (Properties.Settings.Default.WriteAppLog) { - System.IO.File.WriteAllText("vswlog.txt", ""); + System.IO.File.WriteAllText(LogPath, ""); } txtLog.Document.Blocks.Clear(); logMessage($"Version {typeof(MainWindow).Assembly.GetName().Version}"); @@ -93,6 +100,8 @@ public MainWindow() cmbServerType.SelectedIndex = Properties.Settings.Default.ServerInstallType; chkAutoCheckUpdate.IsChecked = Properties.Settings.Default.AutoCheckUpdate; chkLog.IsChecked = Properties.Settings.Default.WriteAppLog; + chkRunningServerCheck.IsChecked = Properties.Settings.Default.RunningServerCheck; + chkStopOnClose.IsChecked = Properties.Settings.Default.StopOnClose; if (Properties.Settings.Default.AutoCheckUpdate) { checkForUpdate(); @@ -113,7 +122,11 @@ public MainWindow() notifyIcon.ContextMenuStrip = cm; storedWindowState = WindowState.Normal; - //servers = new List(); + if (Properties.Settings.Default.RunningServerCheck) + { + checkForRunningServers(); + } + if (File.Exists(this.ServerJsonPath)) { try @@ -136,7 +149,6 @@ public MainWindow() } dgServers.ItemsSource = ValheimServer.Servers;//servers; RefreshDataGrid(); - checkForRunningServers(); if (File.Exists("shutdown.now")) File.Delete("shutdown.now"); shutdownWatcher = new(); @@ -153,11 +165,27 @@ private void ShutdownWatcher_Created(object sender, FileSystemEventArgs e) try { logMessage("Shutdown file detected, initiating shutdown of server(s)."); + ShutdownAndQuit(); + } + catch (Exception ex) + { + logMessage($"Error while stopping servers for shutdown: {ex.Message}"); + } + } + + private void ShutdownAndQuit() + { + try + { foreach (var server in ValheimServer.Servers) { if (server.Status != ValheimServer.ServerStatus.Stopped && server.Status != ValheimServer.ServerStatus.Stopping) { + server.Stopped += Server_StoppedShutdownCheck; server.Stop(); + } else if (server.Status == ValheimServer.ServerStatus.Stopping) + { + server.Stopped += Server_StoppedShutdownCheck; } } } @@ -167,6 +195,23 @@ private void ShutdownWatcher_Created(object sender, FileSystemEventArgs e) } } + private void Server_StoppedShutdownCheck(object sender, ServerStoppedEventArgs e) + { + var allStopped = true; + foreach (var s in ValheimServer.Servers) + { + if (s.Status != ValheimServer.ServerStatus.Stopped) + { + allStopped = false; + break; + } + } + this.Dispatcher.Invoke(() => + { + if (allStopped) Close(); + }); + } + private void NotifyMenuQuit_Click(object sender, EventArgs e) { foreach (var server in ValheimServer.Servers) @@ -665,8 +710,16 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs { if (server.Running) { - logMessage($"Server {server.DisplayName} is still running. Please stop all servers before exiting.", LogEntryType.Error); e.Cancel = true; + if (Properties.Settings.Default.StopOnClose) + { + logMessage($"Stopping all servers for app exit."); + WindowState = WindowState.Minimized; + ShutdownAndQuit(); + } else + { + logMessage($"Server {server.DisplayName} is still running. Please stop all servers before exiting.", LogEntryType.Error); + } } else { @@ -831,7 +884,7 @@ public void logMessage(LogEntry entry) }); if (Properties.Settings.Default.WriteAppLog) { - StreamWriter writer = System.IO.File.AppendText("vswlog.txt"); + StreamWriter writer = System.IO.File.AppendText(LogPath); writer.WriteLine(entry.TimeStamp+": " +entry.Message); writer.Close(); } @@ -1130,11 +1183,6 @@ private void checkForRunningServers() } } - private void lblReportBug_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) - { - Process.Start("cmd", "/C start https://github.com/Razzmatazzz/ValheimServerWarden/issues"); - } - private void btnSteamCmdPath_Click(object sender, RoutedEventArgs e) { var openFolderDialog = new System.Windows.Forms.FolderBrowserDialog(); @@ -1233,10 +1281,27 @@ private void chkLog_Checked(object sender, RoutedEventArgs e) bool newValue = chkLog.IsChecked.HasValue ? chkLog.IsChecked.Value : false; if (newValue & !Properties.Settings.Default.WriteAppLog) { - System.IO.File.WriteAllText("vswlog.txt", DateTime.Now.ToString() + ": Version " + typeof(MainWindow).Assembly.GetName().Version + "\r\n"); + System.IO.File.WriteAllText(LogPath, DateTime.Now.ToString() + ": Version " + typeof(MainWindow).Assembly.GetName().Version + "\r\n"); } Properties.Settings.Default.WriteAppLog = newValue; Properties.Settings.Default.Save(); } + + private void btnReportBug_Click(object sender, RoutedEventArgs e) + { + Process.Start("cmd", "/C start https://github.com/Razzmatazzz/ValheimServerWarden/issues"); + } + + private void chkRunningServerCheck_Checked(object sender, RoutedEventArgs e) + { + Properties.Settings.Default.RunningServerCheck = chkRunningServerCheck.IsChecked.GetValueOrDefault(); + Properties.Settings.Default.Save(); + } + + private void chkStopOnClose_Checked(object sender, RoutedEventArgs e) + { + Properties.Settings.Default.StopOnClose = chkStopOnClose.IsChecked.GetValueOrDefault(); + Properties.Settings.Default.Save(); + } } } diff --git a/Properties/Settings.Designer.cs b/Properties/Settings.Designer.cs index 6f3abba..505518e 100644 --- a/Properties/Settings.Designer.cs +++ b/Properties/Settings.Designer.cs @@ -142,5 +142,29 @@ public bool WriteAppLog { this["WriteAppLog"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("True")] + public bool RunningServerCheck { + get { + return ((bool)(this["RunningServerCheck"])); + } + set { + this["RunningServerCheck"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("False")] + public bool StopOnClose { + get { + return ((bool)(this["StopOnClose"])); + } + set { + this["StopOnClose"] = value; + } + } } } diff --git a/Properties/Settings.settings b/Properties/Settings.settings index 4eab179..c54f4e1 100644 --- a/Properties/Settings.settings +++ b/Properties/Settings.settings @@ -32,5 +32,11 @@ False + + True + + + False + \ No newline at end of file diff --git a/Resources/Bug_32x.png b/Resources/Bug_32x.png new file mode 100644 index 0000000000000000000000000000000000000000..77d84a4b05f4d1ab899893513225db0ae4e4d20a GIT binary patch literal 379 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdzmSQK*5Dp-y;YjHK@;M7UB8wRq zWZOZQvH$7ERG^?_iEBhjaDG}zd16s2LwR|*US?i)adKios$PCk`s{Z$Qb0vNJY5_^ zB3j>0H{@z|5ODq9BGTQPpf9J+qI0QQ_-IhSaL|n>7TtRjl*=ZoaSNHQ+m*%iTl?U_ zN}IV8Rrg#r;XNGC@j|m@hhU&}XtmMREz)WGw&ea!NHYxee&r;|akwC~`2P2asggG5 zFB>J^afqDeF^MHKG;^keX@JPlH_u9AmHU!CG`IRnm_FF0_sPaxfaR82&;F*J0<(P% z%5?ZLm?*rP&CKJtB$JCF;o0g{mio0np6`>GEyXJ6D!63I-ruvOUO4rniZ|qMWDt8$ z&~UUm;pF`yjsMvW<-&eX3z)?Y_%*Uh{AIf~Z$e4&j>dZ@GuB)5IUnGcs9-dIzi7qz TU-M1^!+^ol)z4*}Q$iB}Qh1M~ literal 0 HcmV?d00001 diff --git a/ServerDetailsWindow.xaml b/ServerDetailsWindow.xaml index 15c6d91..8f3f0eb 100644 --- a/ServerDetailsWindow.xaml +++ b/ServerDetailsWindow.xaml @@ -74,12 +74,17 @@ -