This repository was archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMainWindowServerQuery.cs
More file actions
177 lines (154 loc) · 5.56 KB
/
Copy pathMainWindowServerQuery.cs
File metadata and controls
177 lines (154 loc) · 5.56 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using SPCode.Interop;
using ValveQuery.GameServer;
namespace SPCode.UI;
public partial class MainWindow
{
/// <summary>
/// Queries the server with the specified command in the command box of the config.
/// </summary>
private void Server_Query()
{
var output = new List<string>();
var c = Program.Configs[Program.SelectedConfig];
if (string.IsNullOrWhiteSpace(c.RConIP) || string.IsNullOrWhiteSpace(c.RConCommands))
{
output.Add("The RCON IP or the Commands fields are empty.");
goto Dispatcher;
}
try
{
using var server = ServerQuery.GetServerInstance(c.RConIP, c.RConPort, false, 1000, 1000, 2, true);
var serverInfo = server.GetInfo();
if (serverInfo == null)
{
output.Add("No server found to send commands to.");
goto Dispatcher;
}
server.GetControl(c.RConPassword, false);
output.Add($"Server: {serverInfo.Name}");
output.Add("Sending commands...");
var cmds = ReplaceRconCMDVariables(c.RConCommands).Split('\n');
if (cmds.Any(x => x.Contains("{plugin")))
{
output.Add("No plugins available to replace placeholders commands with. Removing them...");
cmds = cmds.Where(x => !x.Contains("{plugin")).ToArray();
if (cmds.Length == 0)
{
output.Add("No commands sent.");
goto Dispatcher;
}
}
foreach (var cmd in cmds)
{
var t = Task.Run(() =>
{
var command = cmd.Trim('\r').Trim();
if (!string.IsNullOrWhiteSpace(command))
{
output.Add(server.Rcon.SendCommand(command));
}
});
t.Wait();
}
output.Add("Commands sent.");
}
catch (Exception ex)
{
if (ex.Message.Contains("Not authorized"))
{
output.Add("Incorrect RCON password.");
goto Dispatcher;
}
if (ex is SocketException socketEx)
{
switch ((SocketError)socketEx.ErrorCode)
{
case SocketError.ConnectionReset:
case SocketError.NotConnected:
case SocketError.TimedOut:
output.Add("Connection to the server reset or timed out. Make sure you've set it up correctly, and the server you're targeting is available.\n" +
"Your data:\n" +
$" - IP: {c.RConIP}\n" +
$" - Port: {c.RConPort}");
goto Dispatcher;
}
}
output.Add($"SPCode unhandled error: {ex.Message}");
}
Dispatcher:
Dispatcher.Invoke(() =>
{
output.ForEach(x => LoggingControl.LogAction(x));
if (CompileOutputRow.Height.Value < 11.0)
{
CompileOutputRow.Height = new GridLength(200.0);
}
});
}
/// <summary>
/// Replaces the placeholders from the commands box with the corresponding content.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private string ReplaceRconCMDVariables(string input)
{
if (CompiledFileNames.Count < 1)
{
return input;
}
if (input.IndexOf("{plugins_reload}", StringComparison.Ordinal) >= 0)
{
var replacement = new StringBuilder();
replacement.AppendLine();
foreach (var fileName in CompiledFileNames)
{
replacement.Append("sm plugins reload " + StripSMXPostFix(fileName) + ";");
}
replacement.AppendLine();
input = input.Replace("{plugins_reload}", replacement.ToString());
}
if (input.IndexOf("{plugins_load}", StringComparison.Ordinal) >= 0)
{
var replacement = new StringBuilder();
replacement.AppendLine();
foreach (var fileName in CompiledFileNames)
{
replacement.Append("sm plugins load " + StripSMXPostFix(fileName) + ";");
}
replacement.AppendLine();
input = input.Replace("{plugins_load}", replacement.ToString());
}
if (input.IndexOf("{plugins_unload}", StringComparison.Ordinal) >= 0)
{
var replacement = new StringBuilder();
replacement.AppendLine();
foreach (var fileName in CompiledFileNames)
{
replacement.Append("sm plugins unload " + StripSMXPostFix(fileName) + ";");
}
replacement.AppendLine();
input = input.Replace("{plugins_unload}", replacement.ToString());
}
return input;
}
/// <summary>
/// Strips the '.smx' from the specified string
/// </summary>
/// <param name="fileName">File name to strip the extension from.</param>
/// <returns></returns>
private string StripSMXPostFix(string fileName)
{
if (fileName.EndsWith(".smx"))
{
return fileName.Substring(0, fileName.Length - 4);
}
return fileName;
}
}