diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-11-04 23:07:10 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-11-04 23:07:10 -0500 |
commit | b4a5b3829f0f738e5b7e05048068eaec9d2d01d1 (patch) | |
tree | a78c44e929ff1de70d20b012385c418aea7e78a6 /src/SMAPI | |
parent | 222265816d803e8e145c0a500568412d03dd49da (diff) | |
download | SMAPI-b4a5b3829f0f738e5b7e05048068eaec9d2d01d1.tar.gz SMAPI-b4a5b3829f0f738e5b7e05048068eaec9d2d01d1.tar.bz2 SMAPI-b4a5b3829f0f738e5b7e05048068eaec9d2d01d1.zip |
add PeerDisconnected event (#480)
Diffstat (limited to 'src/SMAPI')
-rw-r--r-- | src/SMAPI/Events/IMultiplayerEvents.cs | 7 | ||||
-rw-r--r-- | src/SMAPI/Events/PeerContextReceivedEventArgs.cs (renamed from src/SMAPI/Events/ContextReceivedEventArgs.cs) | 10 | ||||
-rw-r--r-- | src/SMAPI/Events/PeerDisconnectedEventArgs.cs | 25 | ||||
-rw-r--r-- | src/SMAPI/Framework/Events/EventManager.cs | 10 | ||||
-rw-r--r-- | src/SMAPI/Framework/Events/ModMultiplayerEvents.cs | 15 | ||||
-rw-r--r-- | src/SMAPI/Framework/SMultiplayer.cs | 26 | ||||
-rw-r--r-- | src/SMAPI/StardewModdingAPI.csproj | 3 |
7 files changed, 74 insertions, 22 deletions
diff --git a/src/SMAPI/Events/IMultiplayerEvents.cs b/src/SMAPI/Events/IMultiplayerEvents.cs index 91e0789c..4a31f48e 100644 --- a/src/SMAPI/Events/IMultiplayerEvents.cs +++ b/src/SMAPI/Events/IMultiplayerEvents.cs @@ -5,10 +5,13 @@ namespace StardewModdingAPI.Events /// <summary>Events raised for multiplayer messages and connections.</summary> public interface IMultiplayerEvents { - /// <summary>Raised after the mod context for a player is received. This happens before the game approves the connection, so the player does not yet exist in the game. This is the earliest point where messages can be sent to the player via SMAPI.</summary> - event EventHandler<ContextReceivedEventArgs> ContextReceived; + /// <summary>Raised after the mod context for a peer is received. This happens before the game approves the connection, so the player doesn't yet exist in the game. This is the earliest point where messages can be sent to the peer via SMAPI.</summary> + event EventHandler<PeerContextReceivedEventArgs> PeerContextReceived; /// <summary>Raised after a mod message is received over the network.</summary> event EventHandler<ModMessageReceivedEventArgs> ModMessageReceived; + + /// <summary>Raised after the connection with a peer is severed.</summary> + event EventHandler<PeerDisconnectedEventArgs> PeerDisconnected; } } diff --git a/src/SMAPI/Events/ContextReceivedEventArgs.cs b/src/SMAPI/Events/PeerContextReceivedEventArgs.cs index c715cf1c..151a295c 100644 --- a/src/SMAPI/Events/ContextReceivedEventArgs.cs +++ b/src/SMAPI/Events/PeerContextReceivedEventArgs.cs @@ -2,13 +2,13 @@ using System; namespace StardewModdingAPI.Events { - /// <summary>Event arguments for an <see cref="IMultiplayerEvents.ContextReceived"/> event.</summary> - public class ContextReceivedEventArgs : EventArgs + /// <summary>Event arguments for an <see cref="IMultiplayerEvents.PeerContextReceived"/> event.</summary> + public class PeerContextReceivedEventArgs : EventArgs { /********* ** Accessors *********/ - /// <summary>The player whose metadata was received.</summary> + /// <summary>The peer whose metadata was received.</summary> public IMultiplayerPeer Peer { get; } @@ -16,8 +16,8 @@ namespace StardewModdingAPI.Events ** Public methods *********/ /// <summary>Construct an instance.</summary> - /// <param name="peer">The player to whom a connection is being established.</param> - internal ContextReceivedEventArgs(IMultiplayerPeer peer) + /// <param name="peer">The peer whose metadata was received.</param> + internal PeerContextReceivedEventArgs(IMultiplayerPeer peer) { this.Peer = peer; } diff --git a/src/SMAPI/Events/PeerDisconnectedEventArgs.cs b/src/SMAPI/Events/PeerDisconnectedEventArgs.cs new file mode 100644 index 00000000..8517988a --- /dev/null +++ b/src/SMAPI/Events/PeerDisconnectedEventArgs.cs @@ -0,0 +1,25 @@ +using System; + +namespace StardewModdingAPI.Events +{ + /// <summary>Event arguments for an <see cref="IMultiplayerEvents.PeerDisconnected"/> event.</summary> + public class PeerDisconnectedEventArgs : EventArgs + { + /********* + ** Accessors + *********/ + /// <summary>The peer who disconnected.</summary> + public IMultiplayerPeer Peer { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="peer">The peer who disconnected.</param> + internal PeerDisconnectedEventArgs(IMultiplayerPeer peer) + { + this.Peer = peer; + } + } +} diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs index 63ac17ee..b9d1c453 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -101,12 +101,15 @@ namespace StardewModdingAPI.Framework.Events /**** ** Multiplayer ****/ - /// <summary>Raised after the mod context for a player is received. This happens before the game approves the connection, so the player does not yet exist in the game. This is the earliest point where messages can be sent to the player via SMAPI.</summary> - public readonly ManagedEvent<ContextReceivedEventArgs> ContextReceived; + /// <summary>Raised after the mod context for a peer is received. This happens before the game approves the connection, so the player doesn't yet exist in the game. This is the earliest point where messages can be sent to the peer via SMAPI.</summary> + public readonly ManagedEvent<PeerContextReceivedEventArgs> PeerContextReceived; /// <summary>Raised after a mod message is received over the network.</summary> public readonly ManagedEvent<ModMessageReceivedEventArgs> ModMessageReceived; + /// <summary>Raised after the connection with a peer is severed.</summary> + public readonly ManagedEvent<PeerDisconnectedEventArgs> PeerDisconnected; + /**** ** Player ****/ @@ -383,8 +386,9 @@ namespace StardewModdingAPI.Framework.Events this.CursorMoved = ManageEventOf<CursorMovedEventArgs>(nameof(IModEvents.Input), nameof(IInputEvents.CursorMoved)); this.MouseWheelScrolled = ManageEventOf<MouseWheelScrolledEventArgs>(nameof(IModEvents.Input), nameof(IInputEvents.MouseWheelScrolled)); - this.ContextReceived = ManageEventOf<ContextReceivedEventArgs>(nameof(IModEvents.Multiplayer), nameof(IMultiplayerEvents.ContextReceived)); + this.PeerContextReceived = ManageEventOf<PeerContextReceivedEventArgs>(nameof(IModEvents.Multiplayer), nameof(IMultiplayerEvents.PeerContextReceived)); this.ModMessageReceived = ManageEventOf<ModMessageReceivedEventArgs>(nameof(IModEvents.Multiplayer), nameof(IMultiplayerEvents.ModMessageReceived)); + this.PeerDisconnected = ManageEventOf<PeerDisconnectedEventArgs>(nameof(IModEvents.Multiplayer), nameof(IMultiplayerEvents.PeerDisconnected)); this.InventoryChanged = ManageEventOf<InventoryChangedEventArgs>(nameof(IModEvents.Player), nameof(IPlayerEvents.InventoryChanged)); this.LevelChanged = ManageEventOf<LevelChangedEventArgs>(nameof(IModEvents.Player), nameof(IPlayerEvents.LevelChanged)); diff --git a/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs b/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs index 432a92d3..152c4e0c 100644 --- a/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs +++ b/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs @@ -9,6 +9,13 @@ namespace StardewModdingAPI.Framework.Events /********* ** Accessors *********/ + /// <summary>Raised after the mod context for a peer is received. This happens before the game approves the connection, so the player doesn't yet exist in the game. This is the earliest point where messages can be sent to the peer via SMAPI.</summary> + public event EventHandler<PeerContextReceivedEventArgs> PeerContextReceived + { + add => this.EventManager.PeerContextReceived.Add(value); + remove => this.EventManager.PeerContextReceived.Remove(value); + } + /// <summary>Raised after a mod message is received over the network.</summary> public event EventHandler<ModMessageReceivedEventArgs> ModMessageReceived { @@ -16,11 +23,11 @@ namespace StardewModdingAPI.Framework.Events remove => this.EventManager.ModMessageReceived.Remove(value); } - /// <summary>Raised after the mod context for a player is received. This happens before the game approves the connection, so the player does not yet exist in the game. This is the earliest point where messages can be sent to the player via SMAPI.</summary> - public event EventHandler<ContextReceivedEventArgs> ContextReceived + /// <summary>Raised after the connection with a peer is severed.</summary> + public event EventHandler<PeerDisconnectedEventArgs> PeerDisconnected { - add => this.EventManager.ContextReceived.Add(value); - remove => this.EventManager.ContextReceived.Remove(value); + add => this.EventManager.PeerDisconnected.Add(value); + remove => this.EventManager.PeerDisconnected.Remove(value); } diff --git a/src/SMAPI/Framework/SMultiplayer.cs b/src/SMAPI/Framework/SMultiplayer.cs index 9f649639..2d0f8b9b 100644 --- a/src/SMAPI/Framework/SMultiplayer.cs +++ b/src/SMAPI/Framework/SMultiplayer.cs @@ -140,6 +140,9 @@ namespace StardewModdingAPI.Framework /// <param name="resume">Send the underlying message.</param> protected void OnServerSendingMessage(SLidgrenServer server, NetConnection connection, OutgoingMessage message, Action resume) { + if (this.VerboseLogging) + this.Monitor.Log($"SERVER SEND {(MessageType)message.MessageType} {message.FarmerID}", LogLevel.Trace); + resume(); } @@ -149,6 +152,9 @@ namespace StardewModdingAPI.Framework /// <param name="resume">Send the underlying message.</param> protected void OnClientSendingMessage(SLidgrenClient client, OutgoingMessage message, Action resume) { + if (this.VerboseLogging) + this.Monitor.Log($"CLIENT SEND {(MessageType)message.MessageType} {message.FarmerID}", LogLevel.Trace); + switch (message.MessageType) { // sync mod context (step 1) @@ -171,6 +177,8 @@ namespace StardewModdingAPI.Framework /// <param name="resume">Process the message using the game's default logic.</param> public void OnServerProcessingMessage(SLidgrenServer server, NetIncomingMessage rawMessage, IncomingMessage message, Action resume) { + if (this.VerboseLogging) + this.Monitor.Log($"SERVER RECV {(MessageType)message.MessageType} {message.FarmerID}", LogLevel.Trace); switch (message.MessageType) { @@ -213,7 +221,7 @@ namespace StardewModdingAPI.Framework } // raise event - this.EventManager.ContextReceived.Raise(new ContextReceivedEventArgs(newPeer)); + this.EventManager.PeerContextReceived.Raise(new PeerContextReceivedEventArgs(newPeer)); } break; @@ -248,8 +256,8 @@ namespace StardewModdingAPI.Framework /// <returns>Returns whether the message was handled.</returns> public void OnClientProcessingMessage(SLidgrenClient client, IncomingMessage message, Action resume) { - if (message.MessageType != Multiplayer.farmerDelta && message.MessageType != Multiplayer.locationDelta && message.MessageType != Multiplayer.teamDelta && message.MessageType != Multiplayer.worldDelta) - this.Monitor.Log($"CLIENT RECV {(MessageType)message.MessageType} {message.FarmerID}", LogLevel.Alert); + if (this.VerboseLogging) + this.Monitor.Log($"CLIENT RECV {(MessageType)message.MessageType} {message.FarmerID}", LogLevel.Trace); switch (message.MessageType) { @@ -315,8 +323,12 @@ namespace StardewModdingAPI.Framework { foreach (long playerID in this.DisconnectingFarmers) { - this.Monitor.Log($"Player quit: {playerID}", LogLevel.Trace); - this.Peers.Remove(playerID); + if (this.Peers.TryGetValue(playerID, out MultiplayerPeer peer)) + { + this.Monitor.Log($"Player quit: {playerID}", LogLevel.Trace); + this.Peers.Remove(playerID); + this.EventManager.PeerDisconnected.Raise(new PeerDisconnectedEventArgs(peer)); + } } base.removeDisconnectedFarmers(); @@ -397,7 +409,7 @@ namespace StardewModdingAPI.Framework /// <summary>Save a received peer.</summary> /// <param name="peer">The peer to add.</param> /// <param name="canBeHost">Whether to track the peer as the host if applicable.</param> - /// <param name="raiseEvent">Whether to raise the <see cref="Events.EventManager.ContextReceived"/> event.</param> + /// <param name="raiseEvent">Whether to raise the <see cref="Events.EventManager.PeerContextReceived"/> event.</param> private void AddPeer(MultiplayerPeer peer, bool canBeHost, bool raiseEvent = true) { // store @@ -407,7 +419,7 @@ namespace StardewModdingAPI.Framework // raise event if (raiseEvent) - this.EventManager.ContextReceived.Raise(new ContextReceivedEventArgs(peer)); + this.EventManager.PeerContextReceived.Raise(new PeerContextReceivedEventArgs(peer)); } /// <summary>Read the metadata context for a player.</summary> diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj index 09ecddcd..8c12c1f9 100644 --- a/src/SMAPI/StardewModdingAPI.csproj +++ b/src/SMAPI/StardewModdingAPI.csproj @@ -85,7 +85,6 @@ <Compile Include="Events\ButtonReleasedEventArgs.cs" /> <Compile Include="Events\ChangeType.cs" /> <Compile Include="Events\ContentEvents.cs" /> - <Compile Include="Events\ContextReceivedEventArgs.cs" /> <Compile Include="Events\ControlEvents.cs" /> <Compile Include="Events\CursorMovedEventArgs.cs" /> <Compile Include="Events\DayEndingEventArgs.cs" /> @@ -137,6 +136,8 @@ <Compile Include="Events\MultiplayerEvents.cs" /> <Compile Include="Events\NpcListChangedEventArgs.cs" /> <Compile Include="Events\ObjectListChangedEventArgs.cs" /> + <Compile Include="Events\PeerContextReceivedEventArgs.cs" /> + <Compile Include="Events\PeerDisconnectedEventArgs.cs" /> <Compile Include="Events\PlayerEvents.cs" /> <Compile Include="Events\RenderedActiveMenuEventArgs.cs" /> <Compile Include="Events\RenderedEventArgs.cs" /> |