-
Notifications
You must be signed in to change notification settings - Fork 2
/
TriggerHappyPlugin.cs
executable file
·258 lines (215 loc) · 9.26 KB
/
TriggerHappyPlugin.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
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
using System;
using Terraria;
using TerrariaApi.Server;
using TerrariaApi;
using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Text.RegularExpressions;
namespace TriggerHappy {
[ApiVersion(1, 15)]
public class TriggerHappyPlugin : TerrariaPlugin {
internal ChainLoader chainLoader = null;
internal bool enabled = true;
internal List<Chain> chainList = new List<Chain>();
internal Dictionary<string, Type> actionTypes = null;
internal Dictionary<string, Type> filterTypes = null;
internal Dictionary<string, Type> triggerTypes = null;
internal volatile List<long> processTimerStats = new List<long>(100);
internal readonly object processTimerMutex = new object();
internal long packetCounter = 0;
internal long handledCounter = 0;
internal long triggerPullCounter = 0;
private static readonly Regex commandParamRegex = new Regex("\"[^\"]+\"|[\\w]+");
private static readonly Regex commandNameRegex = new Regex("!([^ ]+)");
#region "TerrariaPlugin overrides"
public override string Author {
get {
return "Wolfje, Ijwu and others.";
}
}
public override string Description {
get {
return "Packet-level protection for Terraria servers";
}
}
public override string Name {
get {
return "Trigger Happy";
}
}
public override Version Version {
get {
return Assembly.GetExecutingAssembly().GetName().Version;
}
}
#endregion
/// <summary>
/// Updates the internal processor statistics with the number of packets
/// processed and the time it took.
/// </summary>
/// <param name="time"></param>
void UpdateProcessorStats(long time) {
lock (processTimerMutex) {
processTimerStats.Insert(0, time);
if (processTimerStats.Count > 100) {
processTimerStats.RemoveAt(processTimerStats.Count - 1);
}
}
}
/// <summary>
/// Occurs when a packet gets sent from a client to the server.
/// </summary>
/// <param name="args">Arguments.</param>
void Net_GetData(GetDataEventArgs args) {
Chain incomingChain = null;
string packetInfo = null;
Stopwatch processTimer = null;
if (enabled == false || (incomingChain = GetIncomingChain()) == null) {
return;
}
processTimer = new Stopwatch();
processTimer.Start();
packetInfo = string.Format("{0} len {1} from client slot {2}", args.MsgID.ToString("X"), args.Length, args.Msg.whoAmI);
THLog.Debug("Packet in: " + packetInfo);
incomingChain.ProcessChain(ref args, false);
if (args.Handled == false) {
THLog.Debug("Packet out: " + packetInfo);
Interlocked.Increment(ref packetCounter);
} else {
Interlocked.Increment(ref handledCounter);
}
processTimer.Stop();
UpdateProcessorStats(processTimer.ElapsedMilliseconds);
THLog.Debug("Chain process time: {0}ms", processTimer.ElapsedMilliseconds);
}
public override void Initialize() {
ServerApi.Hooks.ServerCommand.Register(this, Server_Command);
InitChains();
}
/// <summary>
/// Loads all the chains in the triggerhappy/chains directory and then registers the data handler.
/// </summary>
private void InitChains() {
THLog.Debug("DeregisterGetDataHook");
ServerApi.Hooks.NetGetData.Deregister(this, Net_GetData);
THLog.Debug("DeregisterGetDataHook succeeded");
try {
LoadTypeCache();
chainLoader.LoadChainsInDirectory("triggerhappy" + System.IO.Path.DirectorySeparatorChar + "chains");
if (chainLoader.VerifyChains() == false) {
THLog.Log(LogLevel.Error, "Error: Initializing TriggerHappy failed and will be disabled. Use !chain reload to retry");
return;
}
} catch (Exception) {
THLog.Log(LogLevel.Error, "Error: Initializing TriggerHappy failed and will be disabled. Use !chain reload to retry");
return;
}
THLog.Debug("RegisterGetDataHook");
ServerApi.Hooks.NetGetData.Register(this, Net_GetData);
THLog.Debug("RegisterGetDataHook succeeded");
}
private void Server_Command(CommandEventArgs args) {
string command = null;
string[] arguments = null;
MatchCollection paramMatch = null;
int paramCount = 0;
if (commandNameRegex.IsMatch(args.Command) == false) {
return;
}
command = args.Command;
if (commandParamRegex.IsMatch(command) == true) {
paramMatch = commandParamRegex.Matches(command);
paramCount = paramMatch.Count;
}
if (paramCount > 0) {
arguments = new string[paramCount - 1];
for (int i = 1; i < paramCount; i++) {
arguments[i - 1] = paramMatch[i].Value;
}
}
ProcessCommand(commandParamRegex.Match(command).Groups[0].Value, arguments);
args.Handled = true;
}
private void ProcessCommand(string command, string[] args) {
THLog.Debug("Command: {0} params: {1}", command, args.Length);
if (command == "chain") {
if (args.Length == 0) {
//todo: write !chain help
return;
}
if (args[0] == "list") {
foreach (Chain chain in chainList) {
THLog.Log(LogLevel.Info, "{0}", chain);
}
} else if (args[0] == "reload") {
this.chainList.Clear();
InitChains();
}
} else if (command == "fw") {
if (args.Length == 0) {
//todo: write !fw help
return;
}
if (args[0] == "on") {
enabled = true;
} else if (args[0] == "off") {
enabled = false;
} else if (args[0] == "debug") {
THLog.debugMode = !THLog.debugMode;
} else if (args[0] == "perf") {
THLog.Log(LogLevel.Info, "{0} packets in, {1} filtered, {2} pulled.", packetCounter, handledCounter, triggerPullCounter);
lock (processTimerMutex) {
if (processTimerStats.Count == 0) {
THLog.Log(LogLevel.Info, "Perf: 0ms avg, 0ms min, 0ms max.");
return;
}
THLog.Log(LogLevel.Info, "Perf: {0:0.000}ms avg, {1:0.000}ms min, {2:0.000}ms max.", processTimerStats.Average(), processTimerStats.Min(), processTimerStats.Max());
}
}
}
}
public TriggerHappyPlugin(Main game) : base(game) {
this.chainLoader = new ChainLoader(this);
}
public void LoadTypeCache() {
this.actionTypes = this.chainLoader.LoadAttributeTypes<ActionAttribute>();
this.filterTypes = this.chainLoader.LoadAttributeTypes<FilterAttribute>();
this.triggerTypes = this.chainLoader.LoadAttributeTypes<TriggerAttribute>();
}
#region "Trigger Accessors"
public Chain GetIncomingChain() {
return GetChainByName("__INCOMING__");
}
public Chain GetChainByName(string chainName) {
return chainList.FirstOrDefault(i => i.Name.Equals(chainName));
}
public Type GetActionTypeByName(string name) {
if (actionTypes == null || actionTypes.ContainsKey(name) == false) {
return null;
}
return actionTypes[name];
}
public Type GetFilterTypeByName(string name) {
if (filterTypes == null || filterTypes.ContainsKey(name) == false) {
return null;
}
return filterTypes[name];
}
public Type GetTriggerTypeByName(string name) {
if (triggerTypes == null || triggerTypes.ContainsKey(name) == false) {
return null;
}
return triggerTypes[name];
}
#endregion
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
if (disposing) {
ServerApi.Hooks.NetGetData.Deregister(this, Net_GetData);
}
}
}
}