-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathTorchUI.xaml.cs
More file actions
217 lines (185 loc) · 6.82 KB
/
TorchUI.xaml.cs
File metadata and controls
217 lines (185 loc) · 6.82 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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading;
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.Navigation;
using System.Windows.Shapes;
using NLog;
using NLog.Targets.Wrappers;
using Sandbox;
using Torch.API;
using Torch.API.Managers;
using Torch.Server.Managers;
using MessageBoxResult = System.Windows.MessageBoxResult;
namespace Torch.Server
{
/// <summary>
/// Interaction logic for TorchUI.xaml
/// </summary>
public partial class TorchUI : Window
{
private TorchServer _server;
private TorchConfig _config;
private bool _autoscrollLog = true;
private bool _needScroll;
private System.Windows.Forms.Timer _scrollTimer;
public TorchUI(TorchServer server)
{
WindowStartupLocation = WindowStartupLocation.Manual;
_config = (TorchConfig)server.Config;
Width = _config.WindowWidth;
Height = _config.WindowHeight;
_server = server;
//TODO: data binding for whole server
DataContext = server;
InitializeComponent();
AttachConsole();
//Left = _config.WindowPosition.X;
//Top = _config.WindowPosition.Y;
//Width = _config.WindowSize.X;
//Height = _config.WindowSize.Y;
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}";
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();
}
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)
{
return;
}
Task.Run(() =>
{
_server.Stop();
_server.Destroy();
});
}
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
if (_server?.State == ServerState.Running)
{
_server.Stop();
_server.Destroy();
}
_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);
}
}
}