-
Notifications
You must be signed in to change notification settings - Fork 4
/
ServerLogWindow.xaml.cs
184 lines (167 loc) · 5.96 KB
/
ServerLogWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ValheimServerWarden
{
/// <summary>
/// Interaction logic for ServerLogWindow.xaml
/// </summary>
public partial class ServerLogWindow : Window
{
private ValheimServer _server;
private FileSystemWatcher logWatcher;
private DateTime lastRefresh;
private Timer refreshTimer;
private int refreshInterval;
private Timer retryTimer;
private int retryInterval;
private double prevExtentHeight;
public ValheimServer Server
{
get
{
return this._server;
}
}
public ServerLogWindow(ValheimServer server)
{
InitializeComponent();
_server = server;
this.Title = $"{server.LogRawName}";
prevExtentHeight = 0;
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;
}
private void RefreshTimer_Elapsed(object sender, ElapsedEventArgs e)
{
RefreshLogText();
}
private void RetryTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Debug.WriteLine("timer elapsed");
if (lastRefresh.AddMilliseconds(retryInterval) >= DateTime.Now)
{
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;
}
if (File.Exists(_server.LogRawName))
{
this.Dispatcher.Invoke(() =>
{
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;
//Debug.WriteLine(txtLog.VerticalOffset+" "+txtLog.ExtentHeight+" "+txtLog.ViewportHeight);
if (prevExtentHeight < txtLog.ViewportHeight && txtLog.ExtentHeight > txtLog.ViewportHeight)
{
txtLog.ScrollToEnd();
}
else if (txtLog.VerticalOffset+txtLog.ViewportHeight == prevExtentHeight)
{
txtLog.ScrollToEnd();
}
else if (txtLog.VerticalOffset+txtLog.ViewportHeight == txtLog.ExtentHeight)
{
txtLog.ScrollToEnd();
}
prevExtentHeight = txtLog.ExtentHeight;
}
catch (IOException)
{
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;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
txtLog.ScrollToEnd();
prevExtentHeight = txtLog.ExtentHeight;
}
}
}