diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-02-23 19:05:23 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-02-23 19:05:23 -0500 |
commit | 68528f7decadccb4c5ed62f3fff10aeff22dcd43 (patch) | |
tree | eb73bbc7d1e808064bc7abc55946f9b650bd8c1a /src/SMAPI/Framework/Events/ManagedEvent.cs | |
parent | c8162c2fb624f1c963615d79b557d7df5a23ff09 (diff) | |
download | SMAPI-68528f7decadccb4c5ed62f3fff10aeff22dcd43.tar.gz SMAPI-68528f7decadccb4c5ed62f3fff10aeff22dcd43.tar.bz2 SMAPI-68528f7decadccb4c5ed62f3fff10aeff22dcd43.zip |
overhaul events to track the mod which added each handler, and log errors under their name (#451)
Diffstat (limited to 'src/SMAPI/Framework/Events/ManagedEvent.cs')
-rw-r--r-- | src/SMAPI/Framework/Events/ManagedEvent.cs | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs new file mode 100644 index 00000000..e54a4fd3 --- /dev/null +++ b/src/SMAPI/Framework/Events/ManagedEvent.cs @@ -0,0 +1,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); + } + } + } + } +} |