From 02e7318d2b99d311a328746b23a359364575f0c5 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 15 Jun 2020 19:08:02 -0400 Subject: merge inconsistent event raise methods --- src/SMAPI/Framework/Events/ManagedEvent.cs | 38 ++++++++---------------------- src/SMAPI/Framework/SGame.cs | 2 +- 2 files changed, 11 insertions(+), 29 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs index b0f0ae71..b37fb376 100644 --- a/src/SMAPI/Framework/Events/ManagedEvent.cs +++ b/src/SMAPI/Framework/Events/ManagedEvent.cs @@ -90,8 +90,13 @@ namespace StardewModdingAPI.Framework.Events /// Raise the event and notify all handlers. /// The event arguments to pass. - public void Raise(TEventArgs args) + /// A lambda which returns true if the event should be raised for the given mod. + public void Raise(TEventArgs args, Func match = null) { + // skip if no handlers + if (this.EventHandlers.Count == 0) + return; + // sort event handlers by priority // (This is done here to avoid repeatedly sorting when handlers are added/removed.) if (this.NeedsSort) @@ -100,13 +105,14 @@ namespace StardewModdingAPI.Framework.Events this.EventHandlers.Sort(); } - // raise - if (this.EventHandlers.Count == 0) - return; + // raise event this.PerformanceMonitor.Track(this.EventName, () => { foreach (ManagedEventHandler handler in this.EventHandlers) { + if (match != null && !match(handler.SourceMod)) + continue; + try { this.PerformanceMonitor.Track(this.EventName, this.GetModNameForPerformanceCounters(handler), () => handler.Handler.Invoke(null, args)); @@ -119,30 +125,6 @@ namespace StardewModdingAPI.Framework.Events }); } - /// 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.EventHandlers.Count == 0) - return; - - foreach (ManagedEventHandler handler in this.EventHandlers) - { - if (match(handler.SourceMod)) - { - try - { - handler.Handler.Invoke(null, args); - } - catch (Exception ex) - { - this.LogError(handler, ex); - } - } - } - } - /********* ** Private methods diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 2a30b595..23358afb 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -240,7 +240,7 @@ namespace StardewModdingAPI.Framework modIDs.Remove(message.FromModID); // don't send a broadcast back to the sender // raise events - this.Events.ModMessageReceived.RaiseForMods(new ModMessageReceivedEventArgs(message), mod => mod != null && modIDs.Contains(mod.Manifest.UniqueID)); + this.Events.ModMessageReceived.Raise(new ModMessageReceivedEventArgs(message), mod => mod != null && modIDs.Contains(mod.Manifest.UniqueID)); } /// A callback invoked when custom content is removed from the save data to avoid a crash. -- cgit