add PeerDisconnected event (#480)
This commit is contained in:
parent
222265816d
commit
b4a5b3829f
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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" />
|
||||
|
|
Loading…
Reference in New Issue