diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-06-15 19:08:02 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-06-15 19:08:02 -0400 |
commit | 02e7318d2b99d311a328746b23a359364575f0c5 (patch) | |
tree | ad1de7e9991235b3262e1ab68c3b6e4159742b28 /src/SMAPI | |
parent | da95a906bf8e812ddcd99a90a4d49942f02f5623 (diff) | |
download | SMAPI-02e7318d2b99d311a328746b23a359364575f0c5.tar.gz SMAPI-02e7318d2b99d311a328746b23a359364575f0c5.tar.bz2 SMAPI-02e7318d2b99d311a328746b23a359364575f0c5.zip |
merge inconsistent event raise methods
Diffstat (limited to 'src/SMAPI')
-rw-r--r-- | src/SMAPI/Framework/Events/ManagedEvent.cs | 38 | ||||
-rw-r--r-- | src/SMAPI/Framework/SGame.cs | 2 |
2 files changed, 11 insertions, 29 deletions
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 /// <summary>Raise the event and notify all handlers.</summary> /// <param name="args">The event arguments to pass.</param> - public void Raise(TEventArgs args) + /// <param name="match">A lambda which returns true if the event should be raised for the given mod.</param> + public void Raise(TEventArgs args, Func<IModMetadata, bool> 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<TEventArgs> 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 }); } - /// <summary>Raise the event and notify all handlers.</summary> - /// <param name="args">The event arguments to pass.</param> - /// <param name="match">A lambda which returns true if the event should be raised for the given mod.</param> - public void RaiseForMods(TEventArgs args, Func<IModMetadata, bool> match) - { - if (this.EventHandlers.Count == 0) - return; - - foreach (ManagedEventHandler<TEventArgs> 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)); } /// <summary>A callback invoked when custom content is removed from the save data to avoid a crash.</summary> |