-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathTorchUI.xaml.cs
More file actions
312 lines (268 loc) · 10.7 KB
/
TorchUI.xaml.cs
File metadata and controls
312 lines (268 loc) · 10.7 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Navigation;
using NLog;
using NLog.Config;
using NLog.Targets.Wrappers;
using Torch.API;
using Torch.API.Managers;
using Torch.Patches;
using Torch.Server.Managers;
using Torch.Server.Views;
using MessageBox = System.Windows.MessageBox;
using MessageBoxResult = System.Windows.MessageBoxResult;
using Rectangle = System.Drawing.Rectangle;
using TextBox = System.Windows.Controls.TextBox;
namespace Torch.Server
{
/// <summary>
/// Interaction logic for TorchUI.xaml
/// </summary>
public partial class TorchUI : Window
{
private TorchServer _server;
private TorchConfig _config;
private static Logger _log = LogManager.GetCurrentClassLogger();
public static TorchUI Instance;
private bool _autoscrollLog = true;
private bool _needScroll;
private System.Windows.Forms.Timer _scrollTimer;
public TorchUI(TorchServer server)
{
_config = (TorchConfig)server.Config;
WindowStartupLocation = WindowStartupLocation.Manual;
SetWindow();
_server = server;
// TODO: data binding for whole server
DataContext = server;
InitializeComponent();
ConfigTab.Content = new ConfigControl(_server);
var config = LogManager.Configuration;
var customTarget = new NlogCustomTarget()
{
Name = "NlogCustomTarget"
};
config.AddTarget(customTarget);
var rule = new LoggingRule("*", LogLevel.Warn, customTarget); // Adjust the log level as needed
config.LoggingRules.Add(rule);
// Apply changes
LogManager.Configuration = config;
AttachConsole();
Chat.BindServer(server);
PlayerList.BindServer(server);
Plugins.BindServer(server);
LoadConfig((TorchConfig)server.Config);
Themes.uiSource = this;
Themes.SetConfig(_config);
Title = $"{_config.InstanceName} - Torch {server.TorchVersion}, SE {server.GameVersion}";
Instance = this;
EntityManagerTab.Content = new EntitiesControl(_server);
Loaded += TorchUI_Loaded;
}
private void TorchUI_Loaded(object sender, RoutedEventArgs e)
{
var scrollViewer = FindDescendant<ScrollViewer>(ConsoleText);
scrollViewer.ScrollChanged += ConsoleText_OnScrollChanged;
_scrollTimer = new System.Windows.Forms.Timer();
_scrollTimer.Tick += ScrollIfNeed;
_scrollTimer.Interval = 120;
_scrollTimer.Start();
// Show analytics banner if enabled and not hidden
if (_config.EnableAnalytics && !_config.AnalyticsBannerHidden)
AnalyticsBanner.Visibility = Visibility.Visible;
// Keep banner in sync if config values change at runtime
_config.PropertyChanged += (s, args) =>
{
if (args.PropertyName == nameof(TorchConfig.EnableAnalytics) ||
args.PropertyName == nameof(TorchConfig.AnalyticsBannerHidden))
Dispatcher.Invoke(() =>
AnalyticsBanner.Visibility = _config.EnableAnalytics && !_config.AnalyticsBannerHidden
? Visibility.Visible
: Visibility.Collapsed);
};
_server.GameStateChanged += (game, state) =>
{
if (state == TorchGameState.Loaded && _config.MinimizeOnServerStart)
BtnStart.Dispatcher.Invoke(() => // Cheap way to get to UI thread
{
WindowState = WindowState.Minimized;
});
};
}
private void SetWindow()
{
// Set the default window size if no position is saved
if ( _config.WindowWidth == 0 && _config.WindowHeight == 0)
{
Width = 980;
Height = 588;
}
else
{
Width = _config.WindowWidth;
Height = _config.WindowHeight;
}
// Only restore if visible on a screen, otherwise let windows position it.
const int tolerance = 10;
var rect = new Rectangle(_config.WindowX, _config.WindowY, _config.WindowWidth, _config.WindowHeight);
if (Screen.AllScreens.Any(s =>
{
Rectangle area = s.WorkingArea;
area.Inflate(tolerance, tolerance);
return area.Contains(rect);
}))
{
Left = _config.WindowX;
Top = _config.WindowY;
}
LocationChanged += (_, args) =>
{
if (!_config.SaveWindowChanges) return;
_config.WindowX = (int)Left;
_config.WindowY = (int)Top;
_config.Save();
};
SizeChanged += (_, args) =>
{
if (!_config.SaveWindowChanges) return;
_config.WindowHeight = (int)args.NewSize.Height;
_config.WindowWidth = (int)args.NewSize.Width;
};
if (_config.StartMinimized)
WindowState = WindowState.Minimized;
}
private void ScrollIfNeed(object sender, EventArgs e)
{
if (_autoscrollLog && _needScroll)
{
ConsoleText.ScrollToEnd();
_needScroll = false;
}
}
private void AttachConsole()
{
const string target = "wpf";
var doc = LogManager.Configuration.FindTargetByName<FlowDocumentTarget>(target)?.Document;
if (doc == null)
{
var wrapped = LogManager.Configuration.FindTargetByName<WrapperTargetBase>(target);
doc = (wrapped?.WrappedTarget as FlowDocumentTarget)?.Document;
}
ConsoleText.FontSize = _config.FontSize;
ConsoleText.Document = doc ?? new FlowDocument(new Paragraph(new Run("No target!")));
ConsoleText.TextChanged += ConsoleText_OnTextChanged;
}
public static T FindDescendant<T>(DependencyObject obj) where T : DependencyObject
{
if (obj == null) return default(T);
int numberChildren = VisualTreeHelper.GetChildrenCount(obj);
if (numberChildren == 0) return default(T);
for (int i = 0; i < numberChildren; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child is T)
{
return (T)child;
}
}
for (int i = 0; i < numberChildren; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
var potentialMatch = FindDescendant<T>(child);
if (potentialMatch != default(T))
{
return potentialMatch;
}
}
return default(T);
}
private void ConsoleText_OnTextChanged(object sender, TextChangedEventArgs args)
{
_needScroll = true;
}
private void ConsoleText_OnScrollChanged(object sender, ScrollChangedEventArgs e)
{
var scrollViewer = (ScrollViewer) sender;
if (e.ExtentHeightChange == 0)
{
// User change.
_autoscrollLog = scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight;
}
}
public void LoadConfig(TorchConfig config)
{
if (!Directory.Exists(config.InstancePath))
return;
_config = config;
Dispatcher.Invoke(() =>
{
//InstancePathBox.Text = config.InstancePath;
});
}
private void BtnStart_Click(object sender, RoutedEventArgs e)
{
_server.DedicatedInstance.SaveConfig();
Task.Run(() => _server.Start());
}
private void BtnStop_Click(object sender, RoutedEventArgs e)
{
var result = MessageBox.Show("Are you sure you want to stop the server?", "Stop Server", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
_server.Invoke(() => _server.Stop());
}
private void AnalyticsPrivacyLink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri) { UseShellExecute = true });
e.Handled = true;
}
private void AnalyticsHideLink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
_config.AnalyticsBannerHidden = true;
_config.Save();
e.Handled = true;
}
private void AnalyticsDisableLink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
_config.EnableAnalytics = false;
_log.Info("Analytics disabled from UI banner. Set EnableAnalytics=true in Torch.cfg to re-enable.");
e.Handled = true;
}
protected override void OnClosing(CancelEventArgs e)
{
// Can't save here or you'll persist all the command line arguments
//
//var newSize = new Point((int)Width, (int)Height);
//_config.WindowSize = newSize;
//var newPos = new Point((int)Left, (int)Top);
//_config.WindowPosition = newPos;
//_config.Save(); //you idiot
_log.Info("Closing Torch...");
if (_server?.State == ServerState.Running)
_server.Stop();
_scrollTimer.Stop();
Process.GetCurrentProcess().Kill();
}
private void BtnRestart_Click(object sender, RoutedEventArgs e)
{
//MySandboxGame.Static.Invoke(MySandboxGame.ReloadDedicatedServerSession); use i
}
private void InstancePathBox_OnLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
var name = ((TextBox)sender).Text;
if (!Directory.Exists(name))
return;
_config.InstancePath = name;
_server.Managers.GetManager<InstanceManager>().LoadInstance(_config.InstancePath);
}
}
}