From 42a797a01240893e9a8e645253a269087b2d178d Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 11 May 2022 19:56:45 -0400 Subject: don't raise events that have no listeners This mainly avoids allocating event arg objects unnecessarily. --- src/SMAPI/Framework/SMultiplayer.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/SMAPI/Framework/SMultiplayer.cs') diff --git a/src/SMAPI/Framework/SMultiplayer.cs b/src/SMAPI/Framework/SMultiplayer.cs index 2badcbbf..441a50ef 100644 --- a/src/SMAPI/Framework/SMultiplayer.cs +++ b/src/SMAPI/Framework/SMultiplayer.cs @@ -235,7 +235,8 @@ namespace StardewModdingAPI.Framework } // raise event - this.EventManager.PeerContextReceived.Raise(new PeerContextReceivedEventArgs(newPeer)); + if (this.EventManager.PeerContextReceived.HasListeners) + this.EventManager.PeerContextReceived.Raise(new PeerContextReceivedEventArgs(newPeer)); } break; @@ -259,7 +260,8 @@ namespace StardewModdingAPI.Framework resume(); // raise event - this.EventManager.PeerConnected.Raise(new PeerConnectedEventArgs(this.Peers[message.FarmerID])); + if (this.EventManager.PeerConnected.HasListeners) + this.EventManager.PeerConnected.Raise(new PeerConnectedEventArgs(this.Peers[message.FarmerID])); break; // handle mod message @@ -370,7 +372,9 @@ namespace StardewModdingAPI.Framework { this.Monitor.Log($"Player quit: {playerID}"); this.Peers.Remove(playerID); - this.EventManager.PeerDisconnected.Raise(new PeerDisconnectedEventArgs(peer)); + + if (this.EventManager.PeerDisconnected.HasListeners) + this.EventManager.PeerDisconnected.Raise(new PeerDisconnectedEventArgs(peer)); } } @@ -481,7 +485,7 @@ namespace StardewModdingAPI.Framework this.HostPeer = peer; // raise event - if (raiseEvent) + if (raiseEvent && this.EventManager.PeerContextReceived.HasListeners) this.EventManager.PeerContextReceived.Raise(new PeerContextReceivedEventArgs(peer)); } -- cgit