-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathHandlerCollection.cs
More file actions
150 lines (128 loc) · 4.25 KB
/
HandlerCollection.cs
File metadata and controls
150 lines (128 loc) · 4.25 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;
namespace TerrariaApi.Server
{
public class HandlerCollection<ArgsType>: IEnumerable<HandlerRegistration<ArgsType>> where ArgsType: EventArgs
{
// Always handle this collection like an immuteable object to maintain thread safety!
private List<HandlerRegistration<ArgsType>> registrations;
private readonly object alterRegistrationsLock = new object();
public string hookName { get; private set; }
internal HandlerCollection(string hookName)
{
if (string.IsNullOrWhiteSpace(hookName))
throw new ArgumentException("Invalid hook name.", "hookName");
this.registrations = new List<HandlerRegistration<ArgsType>>();
this.hookName = hookName;
}
public int Count => this.registrations.Count;
public void Register(TerrariaPlugin registrator, HookHandler<ArgsType> handler, int priority)
{
if (registrator == null)
throw new ArgumentNullException("registrator");
if (handler == null)
throw new ArgumentNullException("handler");
var newRegistration = new HandlerRegistration<ArgsType>
{
Registrator = registrator,
Handler = handler,
Priority = priority
};
lock (this.alterRegistrationsLock)
{
var registrationsClone = new List<HandlerRegistration<ArgsType>>(this.registrations.Count);
registrationsClone.AddRange(this.registrations);
// Highest priority goes up in the list, first registered wins if priority equals.
int insertionIndex = registrations.Count;
for (int i = 0; i < registrationsClone.Count; i++)
{
if (registrationsClone[i].Priority < priority) {
insertionIndex = i;
break;
}
}
registrationsClone.Insert(insertionIndex, newRegistration);
Interlocked.Exchange(ref this.registrations, registrationsClone);
}
}
public void Register(TerrariaPlugin registrator, HookHandler<ArgsType> handler)
{
this.Register(registrator, handler, registrator.Order);
}
public bool Deregister(TerrariaPlugin registrator, HookHandler<ArgsType> handler)
{
if (registrator == null)
throw new ArgumentNullException("registrator");
if (handler == null)
throw new ArgumentNullException("handler");
var registration = new HandlerRegistration<ArgsType>
{
Registrator = registrator,
Handler = handler
};
lock (this.alterRegistrationsLock)
{
int registrationIndex = this.registrations.IndexOf(registration);
if (registrationIndex == -1)
return false;
var registrationsClone = new List<HandlerRegistration<ArgsType>>(this.registrations.Count);
for (int i = 0; i < this.registrations.Count; i++)
if (i != registrationIndex)
registrationsClone.Add(this.registrations[i]);
Interlocked.Exchange(ref this.registrations, registrationsClone);
}
return true;
}
public void Invoke(ArgsType args)
{
if (args == null)
throw new ArgumentNullException("args");
if (this.registrations.Count == 0)
return;
// We handle the registrations collection like an immuteable object, looping through it is always thread safe.
List<HandlerRegistration<ArgsType>> registrations = this.registrations;
foreach (var registration in registrations)
{
try
{
if (ServerApi.Profiler.WrappedProfiler == null)
{
registration.Handler(args);
}
else
{
Stopwatch watch = new Stopwatch();
watch.Start();
try
{
registration.Handler(args);
}
finally
{
watch.Stop();
ServerApi.Profiler.InputPluginHandlerTime(registration.Registrator, hookName, watch.Elapsed);
}
}
}
catch (Exception ex)
{
ServerApi.LogWriter.ServerWriteLine(string.Format(
"Plugin \"{0}\" has had an unhandled exception thrown by one of its {1} handlers: \n{2}",
registration.Registrator.Name, hookName, ex), TraceLevel.Warning);
ServerApi.Profiler.InputPluginHandlerExceptionThrown(registration.Registrator, hookName, ex);
}
}
}
IEnumerator<HandlerRegistration<ArgsType>> IEnumerable<HandlerRegistration<ArgsType>>.GetEnumerator()
{
return this.registrations.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.registrations.GetEnumerator();
}
}
}