1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
- using System . Runtime . InteropServices ;
5
4
using System . Threading . Tasks ;
6
- using AlternateLife . RageMP . Net . Enums ;
7
- using AlternateLife . RageMP . Net . Native ;
8
5
9
6
namespace AlternateLife . RageMP . Net . Helpers
10
7
{
11
- internal class EventHandler < TNative , TEvent >
8
+ internal class EventHandler < TEvent >
12
9
{
13
10
private readonly Plugin _plugin ;
14
- private readonly EventType _type ;
15
- private readonly TNative _nativeCallback ;
16
- private readonly bool _forceRegistration ;
17
11
18
- private readonly HashSet < TEvent > _subscriptions = new HashSet < TEvent > ( ) ;
12
+ protected readonly HashSet < TEvent > _subscriptions = new HashSet < TEvent > ( ) ;
19
13
20
- public EventHandler ( Plugin plugin , EventType type , TNative nativeCallback , bool forceRegistration = false )
14
+ public EventHandler ( Plugin plugin )
21
15
{
22
16
_plugin = plugin ;
23
- _type = type ;
24
- _nativeCallback = nativeCallback ;
25
- _forceRegistration = forceRegistration ;
26
-
27
- if ( _forceRegistration )
28
- {
29
- Rage . Events . RegisterEventHandler ( ( int ) _type , Marshal . GetFunctionPointerForDelegate ( _nativeCallback ) ) ;
30
- }
31
17
}
32
18
33
- public void Subscribe ( TEvent callback )
19
+ public virtual bool Subscribe ( TEvent callback )
34
20
{
35
21
Contract . NotNull ( callback , nameof ( callback ) ) ;
36
22
37
- var wasEmpty = _subscriptions . Any ( ) == false ;
38
- var wasAdded = _subscriptions . Add ( callback ) ;
39
-
40
- if ( _forceRegistration || wasAdded == false || wasEmpty == false )
41
- {
42
- return ;
43
- }
44
-
45
- Rage . Events . RegisterEventHandler ( ( int ) _type , Marshal . GetFunctionPointerForDelegate ( _nativeCallback ) ) ;
23
+ return _subscriptions . Add ( callback ) ;
46
24
}
47
25
48
- public void Unsubscribe ( TEvent callback )
26
+ public virtual bool Unsubscribe ( TEvent callback )
49
27
{
50
28
Contract . NotNull ( callback , nameof ( callback ) ) ;
51
29
52
- var wasRemoved = _subscriptions . Remove ( callback ) ;
53
-
54
- if ( _forceRegistration || wasRemoved == false || _subscriptions . Any ( ) )
55
- {
56
- return ;
57
- }
58
-
59
- Rage . Events . UnregisterEventHandler ( ( int ) _type ) ;
30
+ return _subscriptions . Remove ( callback ) ;
60
31
}
61
32
62
33
public void Call ( Action < TEvent > callback )
@@ -119,7 +90,7 @@ private async void ExecuteSubscriptionAsync(TEvent subscription, Func<TEvent, Ta
119
90
}
120
91
catch ( Exception e )
121
92
{
122
- _plugin . Logger . Error ( $ "An error occured during execution of event { _type } ", e ) ;
93
+ _plugin . Logger . Error ( $ "An error occured during execution of event { typeof ( TEvent ) } ", e ) ;
123
94
}
124
95
}
125
96
@@ -131,7 +102,7 @@ private async Task ExecuteSubscriptionAsyncAwaitable(TEvent subscription, Func<T
131
102
}
132
103
catch ( Exception e )
133
104
{
134
- _plugin . Logger . Error ( $ "An error occured during execution of event { _type } ", e ) ;
105
+ _plugin . Logger . Error ( $ "An error occured during execution of event { typeof ( TEvent ) } ", e ) ;
135
106
}
136
107
}
137
108
@@ -143,7 +114,7 @@ private void ExecuteSubscription(TEvent subscription, Action<TEvent> callback)
143
114
}
144
115
catch ( Exception e )
145
116
{
146
- _plugin . Logger . Error ( $ "An error occured during execution of event { _type } ", e ) ;
117
+ _plugin . Logger . Error ( $ "An error occured during execution of event { typeof ( TEvent ) } ", e ) ;
147
118
}
148
119
}
149
120
}
0 commit comments