Skip to content

Commit bbe53e1

Browse files
Micky5991markatk
authored andcommitted
Extract Native capabilities from EventHandler into separate handler
1 parent 0e1d2ce commit bbe53e1

File tree

3 files changed

+125
-99
lines changed

3 files changed

+125
-99
lines changed

src/AlternateLife.RageMP.Net/Helpers/EventHandler.cs

+10-39
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,33 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Runtime.InteropServices;
54
using System.Threading.Tasks;
6-
using AlternateLife.RageMP.Net.Enums;
7-
using AlternateLife.RageMP.Net.Native;
85

96
namespace AlternateLife.RageMP.Net.Helpers
107
{
11-
internal class EventHandler<TNative, TEvent>
8+
internal class EventHandler<TEvent>
129
{
1310
private readonly Plugin _plugin;
14-
private readonly EventType _type;
15-
private readonly TNative _nativeCallback;
16-
private readonly bool _forceRegistration;
1711

18-
private readonly HashSet<TEvent> _subscriptions = new HashSet<TEvent>();
12+
protected readonly HashSet<TEvent> _subscriptions = new HashSet<TEvent>();
1913

20-
public EventHandler(Plugin plugin, EventType type, TNative nativeCallback, bool forceRegistration = false)
14+
public EventHandler(Plugin plugin)
2115
{
2216
_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-
}
3117
}
3218

33-
public void Subscribe(TEvent callback)
19+
public virtual bool Subscribe(TEvent callback)
3420
{
3521
Contract.NotNull(callback, nameof(callback));
3622

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);
4624
}
4725

48-
public void Unsubscribe(TEvent callback)
26+
public virtual bool Unsubscribe(TEvent callback)
4927
{
5028
Contract.NotNull(callback, nameof(callback));
5129

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);
6031
}
6132

6233
public void Call(Action<TEvent> callback)
@@ -119,7 +90,7 @@ private async void ExecuteSubscriptionAsync(TEvent subscription, Func<TEvent, Ta
11990
}
12091
catch (Exception e)
12192
{
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);
12394
}
12495
}
12596

@@ -131,7 +102,7 @@ private async Task ExecuteSubscriptionAsyncAwaitable(TEvent subscription, Func<T
131102
}
132103
catch (Exception e)
133104
{
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);
135106
}
136107
}
137108

@@ -143,7 +114,7 @@ private void ExecuteSubscription(TEvent subscription, Action<TEvent> callback)
143114
}
144115
catch (Exception e)
145116
{
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);
147118
}
148119
}
149120
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Linq;
2+
using System.Runtime.InteropServices;
3+
using AlternateLife.RageMP.Net.Enums;
4+
using AlternateLife.RageMP.Net.Native;
5+
6+
namespace AlternateLife.RageMP.Net.Helpers
7+
{
8+
internal class NativeEventHandler<TNative, TEvent> : EventHandler<TEvent>
9+
{
10+
private readonly EventType _type;
11+
private readonly TNative _nativeCallback;
12+
private readonly bool _forceRegistration;
13+
14+
public NativeEventHandler(Plugin plugin, EventType type, TNative nativeCallback, bool forceRegistration = false) : base(plugin)
15+
{
16+
_type = type;
17+
_nativeCallback = nativeCallback;
18+
_forceRegistration = forceRegistration;
19+
20+
if (_forceRegistration)
21+
{
22+
Rage.Events.RegisterEventHandler((int) _type, Marshal.GetFunctionPointerForDelegate(_nativeCallback));
23+
}
24+
}
25+
26+
public override bool Subscribe(TEvent callback)
27+
{
28+
var wasEmpty = _subscriptions.Any() == false;
29+
var wasAdded = base.Subscribe(callback);
30+
31+
if (_forceRegistration || wasAdded == false || wasEmpty == false)
32+
{
33+
return true;
34+
}
35+
36+
Rage.Events.RegisterEventHandler((int) _type, Marshal.GetFunctionPointerForDelegate(_nativeCallback));
37+
38+
return true;
39+
}
40+
41+
public override bool Unsubscribe(TEvent callback)
42+
{
43+
var wasRemoved = base.Unsubscribe(callback);
44+
45+
if (_forceRegistration || wasRemoved == false || _subscriptions.Any())
46+
{
47+
return true;
48+
}
49+
50+
Rage.Events.UnregisterEventHandler((int) _type);
51+
52+
return true;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)