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