Skip to content

Commit 3c038f5

Browse files
committed
Fix for immediately asking to shutdown servers that autostart.
1 parent c14750a commit 3c038f5

11 files changed

+256
-82
lines changed

MainWindow.xaml

+27-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
ui:WindowHelper.UseModernWindowStyle="True"
88
xmlns:local="clr-namespace:ValheimServerWarden"
99
mc:Ignorable="d"
10-
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">
10+
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">
1111
<Window.Resources>
1212
<ResourceDictionary>
1313
<ResourceDictionary.MergedDictionaries>
@@ -103,6 +103,7 @@
103103
<RowDefinition Height="Auto"/>
104104
<RowDefinition Height="Auto"/>
105105
<RowDefinition Height="Auto"/>
106+
<RowDefinition Height="Auto"/>
106107
<RowDefinition Height="*"/>
107108
<RowDefinition Height="Auto"/>
108109
</Grid.RowDefinitions>
@@ -134,10 +135,24 @@
134135
<RadioButton x:Name="radThemeDark" Content="Dark" Margin="135,5,0,5" GroupName="AppTheme" VerticalAlignment="Top" Checked="radThemeDark_Checked" Grid.Row="1"/>
135136
</Grid>
136137
</Border>
137-
<CheckBox x:Name="chkAutoCheckUpdate" Content="Automatically check for update" Margin="10,5,0,5" VerticalAlignment="Center" Grid.Row="2" Checked="chkAutoCheckUpdate_Click" Unchecked="chkAutoCheckUpdate_Click"/>
138-
<Button x:Name="btnUpdateCheck" Content="Check Now" Margin="251,5,0,5" VerticalAlignment="Center" Click="btnUpdateCheck_Click" Grid.Row="2"/>
139-
<CheckBox x:Name="chkLog" Content="Create log file" Margin="10,5" Grid.Row="3" Checked="chkLog_Checked" Unchecked="chkLog_Checked"/>
140-
<Label x:Name="lblReportBug" Content="Found a bug? Report it on GitHub." Margin="10" Cursor="Hand" Grid.Row="5" MouseLeftButtonUp="lblReportBug_MouseLeftButtonUp"/>
138+
<Grid Grid.Row="2">
139+
<Grid.ColumnDefinitions>
140+
<ColumnDefinition Width="Auto"/>
141+
<ColumnDefinition Width="Auto"/>
142+
<ColumnDefinition Width="Auto"/>
143+
</Grid.ColumnDefinitions>
144+
<CheckBox x:Name="chkAutoCheckUpdate" Content="Automatically check for update" ToolTip="Check for new version of app start" Margin="10,5" VerticalAlignment="Center" Grid.Column="0" Checked="chkAutoCheckUpdate_Click" Unchecked="chkAutoCheckUpdate_Click"/>
145+
<Button x:Name="btnUpdateCheck" Content="Check Now" Margin="5,5" VerticalAlignment="Center" Click="btnUpdateCheck_Click" Grid.Column="1"/>
146+
</Grid>
147+
<Grid Grid.Row="3">
148+
<Grid.ColumnDefinitions>
149+
<ColumnDefinition Width="Auto"/>
150+
<ColumnDefinition Width="Auto"/>
151+
</Grid.ColumnDefinitions>
152+
<CheckBox x:Name="chkRunningServerCheck" Content="Check for running servers on start" ToolTip="Check for any running servers on app start and provide the option to shut them down" Margin="10,5" Grid.Column="0" Checked="chkRunningServerCheck_Checked" Unchecked="chkRunningServerCheck_Checked"/>
153+
<CheckBox x:Name="chkStopOnClose" Content="Stop running servers on app close" ToolTip="Instead of preventing app closing when there are running servers, stop all running servers" Margin="10,5" Grid.Column="1" Checked="chkStopOnClose_Checked" Unchecked="chkStopOnClose_Checked"/>
154+
</Grid>
155+
<CheckBox x:Name="chkLog" Content="Create log file" ToolTip="Write app log outpute to a file (useful for troubleshooting)" Margin="10,5" Grid.Row="4" Checked="chkLog_Checked" Unchecked="chkLog_Checked"/>
141156
</Grid>
142157
</TabItem>
143158
<TabItem Header="Log">
@@ -165,6 +180,13 @@
165180
</TabItem>
166181
</TabControl>
167182
<StatusBar x:Name="statusbarMain" VerticalAlignment="Bottom" Grid.Row="1">
183+
<StatusBarItem Margin="0,0,10,0">
184+
<Button x:Name="btnReportBug" Height="26" ToolTip="Report a bug" Click="btnReportBug_Click">
185+
<Button.Content>
186+
<Image Source="Resources\Bug_32x.png"/>
187+
</Button.Content>
188+
</Button>
189+
</StatusBarItem>
168190
<StatusBarItem>
169191
<Label x:Name="lblLastMessage" Margin="5"></Label>
170192
</StatusBarItem>

MainWindow.xaml.cs

+78-13
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ private string ServerJsonPath
4949
return "valheim_servers.json";
5050
}
5151
}
52+
private string LogPath
53+
{
54+
get
55+
{
56+
return "vswlog.txt";
57+
}
58+
}
5259
public MainWindow()
5360
{
5461
InitializeComponent();
@@ -58,10 +65,10 @@ public MainWindow()
5865
Properties.Settings.Default.UpgradeRequired = false;
5966
Properties.Settings.Default.Save();
6067
}
61-
Width = Properties.Settings.Default.MainWindowWidth;
68+
//Width = Properties.Settings.Default.MainWindowWidth;
6269
if (Properties.Settings.Default.MainWindowHeight > 0)
6370
{
64-
Height = Properties.Settings.Default.MainWindowHeight;
71+
//Height = Properties.Settings.Default.MainWindowHeight;
6572
}
6673
LogEntry.NormalColor = ((SolidColorBrush)this.Foreground).Color;
6774
logEntries = new List<LogEntry>();
@@ -79,7 +86,7 @@ public MainWindow()
7986

8087
if (Properties.Settings.Default.WriteAppLog)
8188
{
82-
System.IO.File.WriteAllText("vswlog.txt", "");
89+
System.IO.File.WriteAllText(LogPath, "");
8390
}
8491
txtLog.Document.Blocks.Clear();
8592
logMessage($"Version {typeof(MainWindow).Assembly.GetName().Version}");
@@ -93,6 +100,8 @@ public MainWindow()
93100
cmbServerType.SelectedIndex = Properties.Settings.Default.ServerInstallType;
94101
chkAutoCheckUpdate.IsChecked = Properties.Settings.Default.AutoCheckUpdate;
95102
chkLog.IsChecked = Properties.Settings.Default.WriteAppLog;
103+
chkRunningServerCheck.IsChecked = Properties.Settings.Default.RunningServerCheck;
104+
chkStopOnClose.IsChecked = Properties.Settings.Default.StopOnClose;
96105
if (Properties.Settings.Default.AutoCheckUpdate)
97106
{
98107
checkForUpdate();
@@ -113,7 +122,11 @@ public MainWindow()
113122
notifyIcon.ContextMenuStrip = cm;
114123
storedWindowState = WindowState.Normal;
115124

116-
//servers = new List<ValheimServer>();
125+
if (Properties.Settings.Default.RunningServerCheck)
126+
{
127+
checkForRunningServers();
128+
}
129+
117130
if (File.Exists(this.ServerJsonPath))
118131
{
119132
try
@@ -136,7 +149,6 @@ public MainWindow()
136149
}
137150
dgServers.ItemsSource = ValheimServer.Servers;//servers;
138151
RefreshDataGrid();
139-
checkForRunningServers();
140152

141153
if (File.Exists("shutdown.now")) File.Delete("shutdown.now");
142154
shutdownWatcher = new();
@@ -153,11 +165,27 @@ private void ShutdownWatcher_Created(object sender, FileSystemEventArgs e)
153165
try
154166
{
155167
logMessage("Shutdown file detected, initiating shutdown of server(s).");
168+
ShutdownAndQuit();
169+
}
170+
catch (Exception ex)
171+
{
172+
logMessage($"Error while stopping servers for shutdown: {ex.Message}");
173+
}
174+
}
175+
176+
private void ShutdownAndQuit()
177+
{
178+
try
179+
{
156180
foreach (var server in ValheimServer.Servers)
157181
{
158182
if (server.Status != ValheimServer.ServerStatus.Stopped && server.Status != ValheimServer.ServerStatus.Stopping)
159183
{
184+
server.Stopped += Server_StoppedShutdownCheck;
160185
server.Stop();
186+
} else if (server.Status == ValheimServer.ServerStatus.Stopping)
187+
{
188+
server.Stopped += Server_StoppedShutdownCheck;
161189
}
162190
}
163191
}
@@ -167,6 +195,23 @@ private void ShutdownWatcher_Created(object sender, FileSystemEventArgs e)
167195
}
168196
}
169197

198+
private void Server_StoppedShutdownCheck(object sender, ServerStoppedEventArgs e)
199+
{
200+
var allStopped = true;
201+
foreach (var s in ValheimServer.Servers)
202+
{
203+
if (s.Status != ValheimServer.ServerStatus.Stopped)
204+
{
205+
allStopped = false;
206+
break;
207+
}
208+
}
209+
this.Dispatcher.Invoke(() =>
210+
{
211+
if (allStopped) Close();
212+
});
213+
}
214+
170215
private void NotifyMenuQuit_Click(object sender, EventArgs e)
171216
{
172217
foreach (var server in ValheimServer.Servers)
@@ -665,8 +710,16 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs
665710
{
666711
if (server.Running)
667712
{
668-
logMessage($"Server {server.DisplayName} is still running. Please stop all servers before exiting.", LogEntryType.Error);
669713
e.Cancel = true;
714+
if (Properties.Settings.Default.StopOnClose)
715+
{
716+
logMessage($"Stopping all servers for app exit.");
717+
WindowState = WindowState.Minimized;
718+
ShutdownAndQuit();
719+
} else
720+
{
721+
logMessage($"Server {server.DisplayName} is still running. Please stop all servers before exiting.", LogEntryType.Error);
722+
}
670723
}
671724
else
672725
{
@@ -831,7 +884,7 @@ public void logMessage(LogEntry entry)
831884
});
832885
if (Properties.Settings.Default.WriteAppLog)
833886
{
834-
StreamWriter writer = System.IO.File.AppendText("vswlog.txt");
887+
StreamWriter writer = System.IO.File.AppendText(LogPath);
835888
writer.WriteLine(entry.TimeStamp+": " +entry.Message);
836889
writer.Close();
837890
}
@@ -1130,11 +1183,6 @@ private void checkForRunningServers()
11301183
}
11311184
}
11321185

1133-
private void lblReportBug_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
1134-
{
1135-
Process.Start("cmd", "/C start https://github.com/Razzmatazzz/ValheimServerWarden/issues");
1136-
}
1137-
11381186
private void btnSteamCmdPath_Click(object sender, RoutedEventArgs e)
11391187
{
11401188
var openFolderDialog = new System.Windows.Forms.FolderBrowserDialog();
@@ -1233,10 +1281,27 @@ private void chkLog_Checked(object sender, RoutedEventArgs e)
12331281
bool newValue = chkLog.IsChecked.HasValue ? chkLog.IsChecked.Value : false;
12341282
if (newValue & !Properties.Settings.Default.WriteAppLog)
12351283
{
1236-
System.IO.File.WriteAllText("vswlog.txt", DateTime.Now.ToString() + ": Version " + typeof(MainWindow).Assembly.GetName().Version + "\r\n");
1284+
System.IO.File.WriteAllText(LogPath, DateTime.Now.ToString() + ": Version " + typeof(MainWindow).Assembly.GetName().Version + "\r\n");
12371285
}
12381286
Properties.Settings.Default.WriteAppLog = newValue;
12391287
Properties.Settings.Default.Save();
12401288
}
1289+
1290+
private void btnReportBug_Click(object sender, RoutedEventArgs e)
1291+
{
1292+
Process.Start("cmd", "/C start https://github.com/Razzmatazzz/ValheimServerWarden/issues");
1293+
}
1294+
1295+
private void chkRunningServerCheck_Checked(object sender, RoutedEventArgs e)
1296+
{
1297+
Properties.Settings.Default.RunningServerCheck = chkRunningServerCheck.IsChecked.GetValueOrDefault();
1298+
Properties.Settings.Default.Save();
1299+
}
1300+
1301+
private void chkStopOnClose_Checked(object sender, RoutedEventArgs e)
1302+
{
1303+
Properties.Settings.Default.StopOnClose = chkStopOnClose.IsChecked.GetValueOrDefault();
1304+
Properties.Settings.Default.Save();
1305+
}
12411306
}
12421307
}

Properties/Settings.Designer.cs

+24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Properties/Settings.settings

+6
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,11 @@
3232
<Setting Name="WriteAppLog" Type="System.Boolean" Scope="User">
3333
<Value Profile="(Default)">False</Value>
3434
</Setting>
35+
<Setting Name="RunningServerCheck" Type="System.Boolean" Scope="User">
36+
<Value Profile="(Default)">True</Value>
37+
</Setting>
38+
<Setting Name="StopOnClose" Type="System.Boolean" Scope="User">
39+
<Value Profile="(Default)">False</Value>
40+
</Setting>
3541
</Settings>
3642
</SettingsFile>

Resources/Bug_32x.png

379 Bytes
Loading

ServerDetailsWindow.xaml

+28-13
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,17 @@
7474
<Image Source="Resources\Log_32x.png"/>
7575
</Button.Content>
7676
</Button>
77-
<Button x:Name="btnConnect" Margin="5" ToolTip="Connect to server" Grid.Column="4" Click="btnConnect_Click">
77+
<Button x:Name="btnConnect" Margin="5" ToolTip="Connection..." Grid.Column="4" Click="btnConnect_Click">
7878
<Button.Content>
79-
<DynamicResource ResourceKey="Connect"/>
79+
<Image Source="Resources/Connect_32x.png"/>
8080
</Button.Content>
8181
<Button.ContextMenu>
82-
<ContextMenu>
82+
<ContextMenu x:Name="menuConnect">
83+
<MenuItem x:Name="menuConnectPlay" Header="Connect to server" Click="menuConnectPlay_Click">
84+
<MenuItem.Icon>
85+
<Image Source="Resources/Connect_32x.png"/>
86+
</MenuItem.Icon>
87+
</MenuItem>
8388
<MenuItem x:Name="menuConnectLink" Header="Copy external connection link" Click="menuConnectLink_Click">
8489
<MenuItem.Icon>
8590
<Image Source="Resources/Connected_32x.png"/>
@@ -129,19 +134,29 @@
129134
</Grid.ColumnDefinitions>
130135
<Grid Grid.Column="0">
131136
<Grid.RowDefinitions>
132-
<RowDefinition Height="Auto"/>
133137
<RowDefinition Height="Auto"/>
134138
<RowDefinition Height="*"/>
135139
</Grid.RowDefinitions>
136-
<Grid.ColumnDefinitions>
137-
<ColumnDefinition Width="Auto"/>
138-
<ColumnDefinition/>
139-
</Grid.ColumnDefinitions>
140-
<Label Content="Status: " HorizontalAlignment="Left" Margin="10,10,0,5" VerticalAlignment="Center" Grid.Row="0"/>
141-
<Label x:Name="lblStatus" Content="" HorizontalAlignment="Left" Margin="0,10,0,5" VerticalAlignment="Center" Grid.Row="0" Grid.Column="1"/>
142-
<Label Content="Start time:" HorizontalAlignment="Left" Margin="10,5,10,10" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0"/>
143-
<Label x:Name="lblStartTime" Content="" HorizontalAlignment="Left" Margin="0,5,0,10" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1"/>
144-
<RichTextBox x:Name="txtServerLog" Grid.Row="2" Grid.ColumnSpan="2" MinHeight="300" VerticalAlignment="Stretch" IsReadOnly="True" IsTabStop="False">
140+
<Grid Grid.Row="0">
141+
<Grid.RowDefinitions>
142+
<RowDefinition Height="Auto"/>
143+
<RowDefinition Height="Auto"/>
144+
<RowDefinition Height="*"/>
145+
</Grid.RowDefinitions>
146+
<Grid.ColumnDefinitions>
147+
<ColumnDefinition Width="Auto"/>
148+
<ColumnDefinition Width="Auto"/>
149+
<ColumnDefinition Width="Auto"/>
150+
<ColumnDefinition/>
151+
</Grid.ColumnDefinitions>
152+
<Label Content="Status: " HorizontalAlignment="Left" Margin="10,10,0,5" VerticalAlignment="Center" Grid.Row="0" Grid.Column="0"/>
153+
<Label x:Name="lblStatus" Content="" HorizontalAlignment="Left" Margin="0,10,10,5" VerticalAlignment="Center" Grid.Row="0" Grid.Column="1"/>
154+
<Label Content="Memory Used:" Margin="10,10,10,5" VerticalAlignment="Center" Grid.Row="0" Grid.Column="2"/>
155+
<Label x:Name="lblMemory" Content="0 MB" Margin="0,10,0,5" VerticalAlignment="Center" Grid.Row="0" Grid.Column="3"/>
156+
<Label Content="Start time:" HorizontalAlignment="Left" Margin="10,5,10,10" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0"/>
157+
<Label x:Name="lblStartTime" Content="" HorizontalAlignment="Left" Margin="0,5,0,10" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3"/>
158+
</Grid>
159+
<RichTextBox x:Name="txtServerLog" Grid.Row="1" MinHeight="300" VerticalAlignment="Stretch" IsReadOnly="True" IsTabStop="False">
145160
<RichTextBox.ContextMenu>
146161
<ContextMenu x:Name="menuLog" Opened="menuLog_Opened">
147162
<MenuItem x:Name="menuLogSelectAll" Header="Select All" Click="menuLogSelectAll_Click">

0 commit comments

Comments
 (0)