summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI/Framework')
-rw-r--r--src/SMAPI/Framework/Events/EventManager.cs10
-rw-r--r--src/SMAPI/Framework/Events/ModMultiplayerEvents.cs15
-rw-r--r--src/SMAPI/Framework/SMultiplayer.cs26
3 files changed, 37 insertions, 14 deletions
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>