Skip to content

Commit

Permalink
Raw server log now auto-refreshes by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
Razzmatazzz committed Mar 7, 2021
1 parent 5009717 commit c1c3012
Show file tree
Hide file tree
Showing 9 changed files with 616 additions and 132 deletions.
2 changes: 1 addition & 1 deletion MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<Image Source="Resources/ListView_32x.png"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem x:Name="serversMenuLog" Header="Server Log" Click="serversMenuLog_Click">
<MenuItem x:Name="serversMenuLog" Header="Raw Server Log" Click="serversMenuLog_Click">
<MenuItem.Icon>
<Image Source="Resources/Log_32x.png"/>
</MenuItem.Icon>
Expand Down
6 changes: 3 additions & 3 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,12 +1048,12 @@ private void ShowServerLog(ValheimServer server)
if (win.Server == server)
{
win.WindowState = WindowState.Normal;
win.LoadLogText();
//win.LoadLogText();
return;
}
}
ServerLogWindow window = new ServerLogWindow(server);
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
this.serverLogWindows.Add(window);
window.Closed += ((object sender, EventArgs e) =>
{
Expand Down Expand Up @@ -1188,7 +1188,7 @@ private void menuLogSelectAll_Click(object sender, RoutedEventArgs e)

private void menuLogCopy_Click(object sender, RoutedEventArgs e)
{
txtLog.SelectAll();
Clipboard.SetText(txtLog.Selection.Text);
}

private void menuLogClear_Click(object sender, RoutedEventArgs e)
Expand Down
17 changes: 17 additions & 0 deletions ServerDetailsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,23 @@
<Button x:Name="btnSavePermittedList" Content="Save" Margin="0,10" HorizontalAlignment="Center" Grid.Row="2" Click="btnSavePermittedList_Click"/>
</Grid>
</TabItem>
<TabItem x:Name="tabuMod" Header="uMod">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button x:Name="btnuModUpdate" Content="Update" ToolTip="Update server, uMod, apps, and extensions via uMod" Margin="5" Grid.Row="0" Grid.Column="0" Click="btnuModUpdate_Click"/>
<TextBlock TextWrapping="Wrap" Margin="5" Grid.Row="1" Grid.Column="0">
This tab is visible because VSW detected you have uMod installed and you selected uMod for the server install method. uMod support is extremely experimental and may not work at all.<LineBreak/><LineBreak/>
Because uMod does not have a command to check for updates, it is not currently possible to check for updates while a uMod server is running. But if you have the "Update on restart" option selected,
VSW will attempt to use uMod to update the server on scheduled restarts.
</TextBlock>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>
47 changes: 44 additions & 3 deletions ServerDetailsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ public ServerDetailsWindow(ValheimServer server)
InitializeComponent();
this._server = server;
txtServerLog.Document.Blocks.Clear();
foreach (var i in Enum.GetValues(typeof(ValheimServer.ServerInstallMethod)))
foreach (var installmethod in Enum.GetValues(typeof(ValheimServer.ServerInstallMethod)))
{
cmbServerType.Items.Add(Enum.GetName(typeof(ValheimServer.ServerInstallMethod), i));
cmbServerType.Items.Add(installmethod);
}
if (!uMod.Installed)
{
cmbServerType.Items.Remove(ValheimServer.ServerInstallMethod.uMod);
}
RefreshControls();
attachServerEventHandlers();
Expand Down Expand Up @@ -123,6 +127,7 @@ public void RefreshControls()
//btnStop.Content = FindResource("StopGrey");
//btnStart.Content = FindResource("StartGrey");
btnConnect.Content = FindResource("ConnectGrey");
btnuModUpdate.IsEnabled = false;
if (status == ValheimServer.ServerStatus.Running)
{
//btnStop.IsEnabled = true;
Expand All @@ -141,6 +146,7 @@ public void RefreshControls()
//btnStart.Content = FindResource("Start");
btnStart.Visibility = Visibility.Visible;
menuSteamCmdUpdate.Visibility = Visibility.Visible;
btnuModUpdate.IsEnabled = true;
}
else
{
Expand All @@ -149,10 +155,14 @@ public void RefreshControls()
{
btnWorking.ToolTip = "Starting...";
}
else
else if (status == ValheimServer.ServerStatus.Stopping)
{
btnWorking.ToolTip = "Stopping...";
}
else if (status == ValheimServer.ServerStatus.Updating)
{
btnWorking.ToolTip = "Updating...";
}
menuSteamCmdUpdate.Visibility = Visibility.Collapsed;
}
//btnLog.IsEnabled = (File.Exists(Server.LogRawName));
Expand All @@ -164,12 +174,26 @@ public void RefreshControls()
chkUpdateOnRestart.Visibility = Visibility.Visible;
gridAutoUpdate.Visibility = Visibility.Visible;
}
else if (uMod.Installed && Server.InstallMethod == ValheimServer.ServerInstallMethod.uMod)
{
btnSteamCmd.Visibility = Visibility.Collapsed;
chkUpdateOnRestart.Visibility = Visibility.Visible;
gridAutoUpdate.Visibility = Visibility.Collapsed;
}
else
{
btnSteamCmd.Visibility = Visibility.Collapsed;
chkUpdateOnRestart.Visibility = Visibility.Collapsed;
gridAutoUpdate.Visibility = Visibility.Collapsed;
}
if (uMod.Installed && Server.InstallMethod == ValheimServer.ServerInstallMethod.uMod)
{
tabuMod.Visibility = Visibility.Visible;
}
else
{
tabuMod.Visibility = Visibility.Collapsed;
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -410,6 +434,10 @@ private void btnSave_Click(object sender, RoutedEventArgs e)
Server.Public = chkPublic.IsChecked.GetValueOrDefault();
Server.InstallPath = txtServerDir.Text;
Server.InstallMethod = (ValheimServer.ServerInstallMethod)cmbServerType.SelectedIndex;
if (Server.InstallMethod == ValheimServer.ServerInstallMethod.uMod)
{
chkAutoUpdate.IsChecked = false;
}
Server.Autostart = chkAutostart.IsChecked.GetValueOrDefault();
Server.RawLog = chkRawLog.IsChecked.GetValueOrDefault();
Server.ProcessPriority = (ProcessPriorityClass)cmbPriority.SelectedItem;
Expand Down Expand Up @@ -793,6 +821,19 @@ private void chkAutoUpdate_Checked(object sender, RoutedEventArgs e)
txtUpdateCheckInterval.Text = "20";
}
}

private void btnuModUpdate_Click(object sender, RoutedEventArgs e)
{
if (Server.Status == ValheimServer.ServerStatus.Stopped)
{
Server.Update();
}
else
{
var mmb = new ModernMessageBox(this);
mmb.Show("Stop server before updating.", "Server Running", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}

public class ServerEventArgs : EventArgs
Expand Down
32 changes: 31 additions & 1 deletion ServerLogWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,37 @@
ui:WindowHelper.UseModernWindowStyle="True"
Title="ServerLogWindow" Height="450" Width="800">
<Grid>
<RichTextBox x:Name="txtLog" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<RichTextBox x:Name="txtLog" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ContextMenuOpening="txtLog_ContextMenuOpening">
<RichTextBox.ContextMenu>
<ContextMenu>
<MenuItem x:Name="menuLogSelectAll" Header="Select All" Click="menuLogSelectAll_Click">
<MenuItem.Icon>
<Image Source="Resources/SelectAll_32x.png"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem x:Name="menuLogCopy" Header="Copy" Click="menuLogCopy_Click">
<MenuItem.Icon>
<Image Source="Resources/Copy_32x.png"/>
</MenuItem.Icon>
</MenuItem>
<Separator/>
<MenuItem x:Name="menuRefresh" Header="Refresh" Visibility="Collapsed" Click="menuRefresh_Click">
<MenuItem.Icon>
<Image Source="Resources/Refresh_32x.png"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem x:Name="menuStop" Header="Stop automatic refresh" Click="menuStop_Click">
<MenuItem.Icon>
<Image Source="Resources/Stop_32x.png"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem x:Name="menuStart" Header="Start automatic refresh" Visibility="Collapsed" Click="menuStart_Click">
<MenuItem.Icon>
<Image Source="Resources/StartWithoutDebug_32x.png"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</RichTextBox.ContextMenu>
<FlowDocument>
</FlowDocument>
</RichTextBox>
Expand Down
136 changes: 118 additions & 18 deletions ServerLogWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
Expand All @@ -20,7 +22,13 @@ namespace ValheimServerWarden
/// </summary>
public partial class ServerLogWindow : Window
{
ValheimServer _server;
private ValheimServer _server;
private FileSystemWatcher logWatcher;
private DateTime lastRefresh;
private Timer refreshTimer;
private int refreshInterval;
private Timer retryTimer;
private int retryInterval;
public ValheimServer Server
{
get
Expand All @@ -32,30 +40,122 @@ public ServerLogWindow(ValheimServer server)
{
InitializeComponent();
_server = server;
this.Title = $"{server.LogRawName} (does not update live)";
LoadLogText();
this.Title = $"{server.LogRawName}";
RefreshLogText();

refreshInterval = 1000;
refreshTimer = new();
refreshTimer.AutoReset = false;
refreshTimer.Elapsed += RefreshTimer_Elapsed;

retryInterval = 4000;
retryTimer = new();
retryTimer.AutoReset = false;
retryTimer.Elapsed += RetryTimer_Elapsed;

logWatcher = new FileSystemWatcher();
// Watch for changes in LastWrite times.
logWatcher.NotifyFilter = NotifyFilters.LastWrite;
logWatcher.Path = Environment.CurrentDirectory;

// Only watch .db files.
logWatcher.Filter = $"{server.LogRawName}";

logWatcher.Changed += LogWatcher_Changed;
logWatcher.EnableRaisingEvents = true;
}

public void LoadLogText()
private void RefreshTimer_Elapsed(object sender, ElapsedEventArgs e)
{
try
RefreshLogText();
}

private void RetryTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Debug.WriteLine("timer elapsed");
if (lastRefresh.AddMilliseconds(retryInterval) >= DateTime.Now)
{
if (File.Exists(_server.LogRawName))
{
txtLog.Document.Blocks.Clear();
Run run = new Run(File.ReadAllText(_server.LogRawName));
Paragraph paragraph = new Paragraph(run);
paragraph.Margin = new Thickness(0);
txtLog.Document.Blocks.Add(paragraph);
}
Debug.WriteLine($"timer elapsed >= {retryInterval}ms ago");
RefreshLogText();
}
}

private void LogWatcher_Changed(object sender, FileSystemEventArgs e)
{
refreshTimer.Interval = refreshInterval;
refreshTimer.Enabled = true;
}

public void RefreshLogText()
{
if (_server == null)
{
this.Close();
return;
}
catch (IOException ex)
if (File.Exists(_server.LogRawName))
{
if (ex.Message.Contains("being used by another process"))
this.Dispatcher.Invoke(() =>
{
System.Threading.Thread.Sleep(500);
LoadLogText();
}
try
{
txtLog.Document.Blocks.Clear();
Run run = new Run(File.ReadAllText(_server.LogRawName));
Paragraph paragraph = new Paragraph(run);
paragraph.Margin = new Thickness(0);
txtLog.Document.Blocks.Add(paragraph);
lastRefresh = DateTime.Now;
}
catch (IOException ex)
{
retryTimer.Interval = retryInterval;
retryTimer.Enabled = true;
retryTimer.Start();
}
});
}
}

private void menuRefresh_Click(object sender, RoutedEventArgs e)
{
RefreshLogText();
}

private void menuStop_Click(object sender, RoutedEventArgs e)
{
logWatcher.EnableRaisingEvents = false;
menuStop.Visibility = Visibility.Collapsed;
menuRefresh.Visibility = Visibility.Visible;
menuStart.Visibility = Visibility.Visible;
}

private void menuStart_Click(object sender, RoutedEventArgs e)
{
logWatcher.EnableRaisingEvents = true;
menuStop.Visibility = Visibility.Visible;
menuRefresh.Visibility = Visibility.Collapsed;
menuStart.Visibility = Visibility.Collapsed;
}

private void menuLogSelectAll_Click(object sender, RoutedEventArgs e)
{
txtLog.SelectAll();
}

private void menuLogCopy_Click(object sender, RoutedEventArgs e)
{
Clipboard.SetText(txtLog.Selection.Text);
}

private void txtLog_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
if (txtLog.Selection.IsEmpty)
{
menuLogCopy.Visibility = Visibility.Collapsed;
}
else
{
menuLogCopy.Visibility = Visibility.Visible;
}
}
}
Expand Down
Loading

0 comments on commit c1c3012

Please sign in to comment.