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
|
using System;
using System.Linq;
namespace StardewModdingAPI.Framework.Events
{
/// <summary>An event wrapper which intercepts and logs errors in handler code.</summary>
/// <typeparam name="TEventArgs">The event arguments type.</typeparam>
internal class ManagedEvent<TEventArgs> : ManagedEventBase<EventHandler<TEventArgs>>
{
/*********
** Properties
*********/
/// <summary>The underlying event.</summary>
private event EventHandler<TEventArgs> Event;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="eventName">A human-readable name for the event.</param>
/// <param name="monitor">Writes messages to the log.</param>
/// <param name="modRegistry">The mod registry with which to identify mods.</param>
public ManagedEvent(string eventName, IMonitor monitor, ModRegistry modRegistry)
: base(eventName, monitor, modRegistry) { }
/// <summary>Add an event handler.</summary>
/// <param name="handler">The event handler.</param>
public void Add(EventHandler<TEventArgs> handler)
{
this.Event += handler;
this.AddTracking(handler, this.Event?.GetInvocationList().Cast<EventHandler<TEventArgs>>());
}
/// <summary>Remove an event handler.</summary>
/// <param name="handler">The event handler.</param>
public void Remove(EventHandler<TEventArgs> handler)
{
this.Event -= handler;
this.RemoveTracking(handler, this.Event?.GetInvocationList().Cast<EventHandler<TEventArgs>>());
}
/// <summary>Raise the event and notify all handlers.</summary>
/// <param name="args">The event arguments to pass.</param>
public void Raise(TEventArgs args)
{
if (this.Event == null)
return;
foreach (EventHandler<TEventArgs> handler in this.CachedInvocationList)
{
try
{
handler.Invoke(null, args);
}
catch (Exception ex)
{
this.LogError(handler, ex);
}
}
}
}
/// <summary>An event wrapper which intercepts and logs errors in handler code.</summary>
internal class ManagedEvent : ManagedEventBase<EventHandler>
{
/*********
** Properties
*********/
/// <summary>The underlying event.</summary>
private event EventHandler Event;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="eventName">A human-readable name for the event.</param>
/// <param name="monitor">Writes messages to the log.</param>
/// <param name="modRegistry">The mod registry with which to identify mods.</param>
public ManagedEvent(string eventName, IMonitor monitor, ModRegistry modRegistry)
: base(eventName, monitor, modRegistry) { }
/// <summary>Add an event handler.</summary>
/// <param name="handler">The event handler.</param>
public void Add(EventHandler handler)
{
this.Event += handler;
this.AddTracking(handler, this.Event?.GetInvocationList().Cast<EventHandler>());
}
/// <summary>Remove an event handler.</summary>
/// <param name="handler">The event handler.</param>
public void Remove(EventHandler handler)
{
this.Event -= handler;
this.RemoveTracking(handler, this.Event?.GetInvocationList().Cast<EventHandler>());
}
/// <summary>Raise the event and notify all handlers.</summary>
public void Raise()
{
if (this.Event == null)
return;
foreach (EventHandler handler in this.CachedInvocationList)
{
try
{
handler.Invoke(null, EventArgs.Empty);
}
catch (Exception ex)
{
this.LogError(handler, ex);
}
}
}
}
}
|