@@ -30,20 +30,8 @@ namespace Exiled.Events.Features
3030 /// </summary>
3131 public class Event : IExiledEvent
3232 {
33- private record Registration ( CustomEventHandler handler , int priority ) ;
34-
35- private record AsyncRegistration ( CustomAsyncEventHandler handler , int priority ) ;
36-
3733 private static readonly List < Event > EventsValue = new ( ) ;
3834
39- private static readonly IComparer < Registration > RegisterComparable = Comparer < Registration > . Create ( ( x , y ) => y . priority - x . priority ) ;
40-
41- private static readonly IComparer < AsyncRegistration > AsyncRegisterComparable = Comparer < AsyncRegistration > . Create ( ( x , y ) => y . priority - x . priority ) ;
42-
43- private readonly List < Registration > innerEvent = new ( ) ;
44-
45- private readonly List < AsyncRegistration > innerAsyncEvent = new ( ) ;
46-
4735 private bool patched ;
4836
4937 /// <summary>
@@ -54,6 +42,10 @@ public Event()
5442 EventsValue . Add ( this ) ;
5543 }
5644
45+ private event CustomEventHandler InnerEvent ;
46+
47+ private event CustomAsyncEventHandler InnerAsyncEvent ;
48+
5749 /// <summary>
5850 /// Gets a <see cref="IReadOnlyList{T}"/> of <see cref="Event{T}"/> which contains all the <see cref="Event{T}"/> instances.
5951 /// </summary>
@@ -112,14 +104,6 @@ public Event()
112104 /// </summary>
113105 /// <param name="handler">The handler to add.</param>
114106 public void Subscribe ( CustomEventHandler handler )
115- => Subscribe ( handler , 0 ) ;
116-
117- /// <summary>
118- /// Subscribes a target <see cref="CustomEventHandler"/> to the inner event if the conditional is true.
119- /// </summary>
120- /// <param name="handler">The handler to add.</param>
121- /// <param name="priority">The highest priority is the first called, the lowest the last.</param>
122- public void Subscribe ( CustomEventHandler handler , int priority )
123107 {
124108 Log . Assert ( Events . Instance is not null , $ "{ nameof ( Events . Instance ) } is null, please ensure you have exiled_events enabled!") ;
125109
@@ -129,36 +113,14 @@ public void Subscribe(CustomEventHandler handler, int priority)
129113 patched = true ;
130114 }
131115
132- if ( handler == null )
133- return ;
134-
135- Registration registration = new Registration ( handler , priority ) ;
136- int index = innerEvent . BinarySearch ( registration , RegisterComparable ) ;
137- if ( index < 0 )
138- {
139- innerEvent . Insert ( ~ index , registration ) ;
140- }
141- else
142- {
143- while ( index < innerEvent . Count && innerEvent [ index ] . priority == priority )
144- index ++ ;
145- innerEvent . Insert ( index , registration ) ;
146- }
116+ InnerEvent += handler ;
147117 }
148118
149119 /// <summary>
150120 /// Subscribes a target <see cref="CustomAsyncEventHandler"/> to the inner event if the conditional is true.
151121 /// </summary>
152122 /// <param name="handler">The handler to add.</param>
153123 public void Subscribe ( CustomAsyncEventHandler handler )
154- => Subscribe ( handler , 0 ) ;
155-
156- /// <summary>
157- /// Subscribes a target <see cref="CustomAsyncEventHandler"/> to the inner event if the conditional is true.
158- /// </summary>
159- /// <param name="handler">The handler to add.</param>
160- /// <param name="priority">The highest priority is the first called, the lowest the last.</param>
161- public void Subscribe ( CustomAsyncEventHandler handler , int priority )
162124 {
163125 Log . Assert ( Events . Instance is not null , $ "{ nameof ( Events . Instance ) } is null, please ensure you have exiled_events enabled!") ;
164126
@@ -168,21 +130,7 @@ public void Subscribe(CustomAsyncEventHandler handler, int priority)
168130 patched = true ;
169131 }
170132
171- if ( handler == null )
172- return ;
173-
174- AsyncRegistration registration = new AsyncRegistration ( handler , 0 ) ;
175- int index = innerAsyncEvent . BinarySearch ( registration , AsyncRegisterComparable ) ;
176- if ( index < 0 )
177- {
178- innerAsyncEvent . Insert ( ~ index , registration ) ;
179- }
180- else
181- {
182- while ( index < innerAsyncEvent . Count && innerAsyncEvent [ index ] . priority == priority )
183- index ++ ;
184- innerAsyncEvent . Insert ( index , registration ) ;
185- }
133+ InnerAsyncEvent += handler ;
186134 }
187135
188136 /// <summary>
@@ -191,9 +139,7 @@ public void Subscribe(CustomAsyncEventHandler handler, int priority)
191139 /// <param name="handler">The handler to add.</param>
192140 public void Unsubscribe ( CustomEventHandler handler )
193141 {
194- int index = innerEvent . FindIndex ( p => p . handler == handler ) ;
195- if ( index != - 1 )
196- innerEvent . RemoveAt ( index ) ;
142+ InnerEvent -= handler ;
197143 }
198144
199145 /// <summary>
@@ -202,88 +148,52 @@ public void Unsubscribe(CustomEventHandler handler)
202148 /// <param name="handler">The handler to add.</param>
203149 public void Unsubscribe ( CustomAsyncEventHandler handler )
204150 {
205- int index = innerAsyncEvent . FindIndex ( p => p . handler == handler ) ;
206- if ( index != - 1 )
207- innerAsyncEvent . RemoveAt ( index ) ;
151+ InnerAsyncEvent -= handler ;
208152 }
209153
210154 /// <summary>
211155 /// Executes all <see cref="CustomEventHandler"/> listeners safely.
212156 /// </summary>
213157 public void InvokeSafely ( )
214158 {
215- BlendedInvoke ( ) ;
216- }
217-
218- /// <inheritdoc cref="InvokeSafely"/>
219- internal void BlendedInvoke ( )
220- {
221- Registration [ ] innerEvent = this . innerEvent . ToArray ( ) ;
222- AsyncRegistration [ ] innerAsyncEvent = this . innerAsyncEvent . ToArray ( ) ;
223- int count = innerEvent . Length + innerAsyncEvent . Length ;
224- int eventIndex = 0 , asyncEventIndex = 0 ;
225-
226- for ( int i = 0 ; i < count ; i ++ )
227- {
228- if ( eventIndex < innerEvent . Length && ( asyncEventIndex >= innerAsyncEvent . Length || innerEvent [ eventIndex ] . priority >= innerAsyncEvent [ asyncEventIndex ] . priority ) )
229- {
230- try
231- {
232- innerEvent [ eventIndex ] . handler ( ) ;
233- }
234- catch ( Exception ex )
235- {
236- Log . Error ( $ "Method \" { innerEvent [ eventIndex ] . handler . Method . Name } \" of the class \" { innerEvent [ eventIndex ] . handler . Method . ReflectedType . FullName } \" caused an exception when handling the event \" { GetType ( ) . FullName } \" \n { ex } ") ;
237- }
238-
239- eventIndex ++ ;
240- }
241- else
242- {
243- try
244- {
245- Timing . RunCoroutine ( innerAsyncEvent [ asyncEventIndex ] . handler ( ) ) ;
246- }
247- catch ( Exception ex )
248- {
249- Log . Error ( $ "Method \" { innerAsyncEvent [ asyncEventIndex ] . handler . Method . Name } \" of the class \" { innerAsyncEvent [ asyncEventIndex ] . handler . Method . ReflectedType . FullName } \" caused an exception when handling the event \" { GetType ( ) . FullName } \" \n { ex } ") ;
250- }
251-
252- asyncEventIndex ++ ;
253- }
254- }
159+ InvokeNormal ( ) ;
160+ InvokeAsync ( ) ;
255161 }
256162
257163 /// <inheritdoc cref="InvokeSafely"/>
258164 internal void InvokeNormal ( )
259165 {
260- Registration [ ] innerEvent = this . innerEvent . ToArray ( ) ;
261- foreach ( Registration registration in innerEvent )
166+ if ( InnerEvent is null )
167+ return ;
168+
169+ foreach ( CustomEventHandler handler in InnerEvent . GetInvocationList ( ) . Cast < CustomEventHandler > ( ) )
262170 {
263171 try
264172 {
265- registration . handler ( ) ;
173+ handler ( ) ;
266174 }
267175 catch ( Exception ex )
268176 {
269- Log . Error ( $ "Method \" { registration . handler . Method . Name } \" of the class \" { registration . handler . Method . ReflectedType . FullName } \" caused an exception when handling the event \" { GetType ( ) . FullName } \" \n { ex } ") ;
177+ Log . Error ( $ "Method \" { handler . Method . Name } \" of the class \" { handler . Method . ReflectedType . FullName } \" caused an exception when handling the event \" { GetType ( ) . FullName } \" \n { ex } ") ;
270178 }
271179 }
272180 }
273181
274182 /// <inheritdoc cref="InvokeSafely"/>
275183 internal void InvokeAsync ( )
276184 {
277- AsyncRegistration [ ] innerAsyncEvent = this . innerAsyncEvent . ToArray ( ) ;
278- foreach ( AsyncRegistration registration in innerAsyncEvent )
185+ if ( InnerAsyncEvent is null )
186+ return ;
187+
188+ foreach ( CustomAsyncEventHandler handler in InnerAsyncEvent . GetInvocationList ( ) . Cast < CustomAsyncEventHandler > ( ) )
279189 {
280190 try
281191 {
282- Timing . RunCoroutine ( registration . handler ( ) ) ;
192+ Timing . RunCoroutine ( handler ( ) ) ;
283193 }
284194 catch ( Exception ex )
285195 {
286- Log . Error ( $ "Method \" { registration . handler . Method . Name } \" of the class \" { registration . handler . Method . ReflectedType . FullName } \" caused an exception when handling the event \" { GetType ( ) . FullName } \" \n { ex } ") ;
196+ Log . Error ( $ "Method \" { handler . Method . Name } \" of the class \" { handler . Method . ReflectedType . FullName } \" caused an exception when handling the event \" { GetType ( ) . FullName } \" \n { ex } ") ;
287197 }
288198 }
289199 }
0 commit comments