Skip to content

Commit

Permalink
Fix for immediately asking to shutdown servers that autostart.
Browse files Browse the repository at this point in the history
  • Loading branch information
Razzmatazzz committed Mar 23, 2021
1 parent c14750a commit 3c038f5
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 82 deletions.
32 changes: 27 additions & 5 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down Expand Up @@ -103,6 +103,7 @@
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
Expand Down Expand Up @@ -134,10 +135,24 @@
<RadioButton x:Name="radThemeDark" Content="Dark" Margin="135,5,0,5" GroupName="AppTheme" VerticalAlignment="Top" Checked="radThemeDark_Checked" Grid.Row="1"/>
</Grid>
</Border>
<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"/>
<Button x:Name="btnUpdateCheck" Content="Check Now" Margin="251,5,0,5" VerticalAlignment="Center" Click="btnUpdateCheck_Click" Grid.Row="2"/>
<CheckBox x:Name="chkLog" Content="Create log file" Margin="10,5" Grid.Row="3" Checked="chkLog_Checked" Unchecked="chkLog_Checked"/>
<Label x:Name="lblReportBug" Content="Found a bug? Report it on GitHub." Margin="10" Cursor="Hand" Grid.Row="5" MouseLeftButtonUp="lblReportBug_MouseLeftButtonUp"/>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<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"/>
<Button x:Name="btnUpdateCheck" Content="Check Now" Margin="5,5" VerticalAlignment="Center" Click="btnUpdateCheck_Click" Grid.Column="1"/>
</Grid>
<Grid Grid.Row="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<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"/>
<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"/>
</Grid>
<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"/>
</Grid>
</TabItem>
<TabItem Header="Log">
Expand Down Expand Up @@ -165,6 +180,13 @@
</TabItem>
</TabControl>
<StatusBar x:Name="statusbarMain" VerticalAlignment="Bottom" Grid.Row="1">
<StatusBarItem Margin="0,0,10,0">
<Button x:Name="btnReportBug" Height="26" ToolTip="Report a bug" Click="btnReportBug_Click">
<Button.Content>
<Image Source="Resources\Bug_32x.png"/>
</Button.Content>
</Button>
</StatusBarItem>
<StatusBarItem>
<Label x:Name="lblLastMessage" Margin="5"></Label>
</StatusBarItem>
Expand Down
91 changes: 78 additions & 13 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ private string ServerJsonPath
return "valheim_servers.json";
}
}
private string LogPath
{
get
{
return "vswlog.txt";
}
}
public MainWindow()
{
InitializeComponent();
Expand All @@ -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<LogEntry>();
Expand All @@ -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}");
Expand All @@ -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();
Expand All @@ -113,7 +122,11 @@ public MainWindow()
notifyIcon.ContextMenuStrip = cm;
storedWindowState = WindowState.Normal;

//servers = new List<ValheimServer>();
if (Properties.Settings.Default.RunningServerCheck)
{
checkForRunningServers();
}

if (File.Exists(this.ServerJsonPath))
{
try
Expand All @@ -136,7 +149,6 @@ public MainWindow()
}
dgServers.ItemsSource = ValheimServer.Servers;//servers;
RefreshDataGrid();
checkForRunningServers();

if (File.Exists("shutdown.now")) File.Delete("shutdown.now");
shutdownWatcher = new();
Expand All @@ -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;
}
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
}
}
24 changes: 24 additions & 0 deletions Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,11 @@
<Setting Name="WriteAppLog" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="RunningServerCheck" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="StopOnClose" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>
Binary file added Resources/Bug_32x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 28 additions & 13 deletions ServerDetailsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@
<Image Source="Resources\Log_32x.png"/>
</Button.Content>
</Button>
<Button x:Name="btnConnect" Margin="5" ToolTip="Connect to server" Grid.Column="4" Click="btnConnect_Click">
<Button x:Name="btnConnect" Margin="5" ToolTip="Connection..." Grid.Column="4" Click="btnConnect_Click">
<Button.Content>
<DynamicResource ResourceKey="Connect"/>
<Image Source="Resources/Connect_32x.png"/>
</Button.Content>
<Button.ContextMenu>
<ContextMenu>
<ContextMenu x:Name="menuConnect">
<MenuItem x:Name="menuConnectPlay" Header="Connect to server" Click="menuConnectPlay_Click">
<MenuItem.Icon>
<Image Source="Resources/Connect_32x.png"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem x:Name="menuConnectLink" Header="Copy external connection link" Click="menuConnectLink_Click">
<MenuItem.Icon>
<Image Source="Resources/Connected_32x.png"/>
Expand Down Expand Up @@ -129,19 +134,29 @@
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="Status: " HorizontalAlignment="Left" Margin="10,10,0,5" VerticalAlignment="Center" Grid.Row="0"/>
<Label x:Name="lblStatus" Content="" HorizontalAlignment="Left" Margin="0,10,0,5" VerticalAlignment="Center" Grid.Row="0" Grid.Column="1"/>
<Label Content="Start time:" HorizontalAlignment="Left" Margin="10,5,10,10" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0"/>
<Label x:Name="lblStartTime" Content="" HorizontalAlignment="Left" Margin="0,5,0,10" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1"/>
<RichTextBox x:Name="txtServerLog" Grid.Row="2" Grid.ColumnSpan="2" MinHeight="300" VerticalAlignment="Stretch" IsReadOnly="True" IsTabStop="False">
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="Status: " HorizontalAlignment="Left" Margin="10,10,0,5" VerticalAlignment="Center" Grid.Row="0" Grid.Column="0"/>
<Label x:Name="lblStatus" Content="" HorizontalAlignment="Left" Margin="0,10,10,5" VerticalAlignment="Center" Grid.Row="0" Grid.Column="1"/>
<Label Content="Memory Used:" Margin="10,10,10,5" VerticalAlignment="Center" Grid.Row="0" Grid.Column="2"/>
<Label x:Name="lblMemory" Content="0 MB" Margin="0,10,0,5" VerticalAlignment="Center" Grid.Row="0" Grid.Column="3"/>
<Label Content="Start time:" HorizontalAlignment="Left" Margin="10,5,10,10" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0"/>
<Label x:Name="lblStartTime" Content="" HorizontalAlignment="Left" Margin="0,5,0,10" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3"/>
</Grid>
<RichTextBox x:Name="txtServerLog" Grid.Row="1" MinHeight="300" VerticalAlignment="Stretch" IsReadOnly="True" IsTabStop="False">
<RichTextBox.ContextMenu>
<ContextMenu x:Name="menuLog" Opened="menuLog_Opened">
<MenuItem x:Name="menuLogSelectAll" Header="Select All" Click="menuLogSelectAll_Click">
Expand Down
Loading

0 comments on commit 3c038f5

Please sign in to comment.