diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-11-03 01:29:01 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-11-03 01:29:01 -0400 |
commit | 02a46bf13f29ce0dd8ac2f422113083c59dae42d (patch) | |
tree | 134a670897ab4fb9b76485b022b19f0cb1167df3 /src/SMAPI/Framework/Networking | |
parent | 6f23aaf2954f7eeb93b7cc5aad2d3f9b237883f3 (diff) | |
download | SMAPI-02a46bf13f29ce0dd8ac2f422113083c59dae42d.tar.gz SMAPI-02a46bf13f29ce0dd8ac2f422113083c59dae42d.tar.bz2 SMAPI-02a46bf13f29ce0dd8ac2f422113083c59dae42d.zip |
add APIs to send/receive messages in multiplayer (#480)
Diffstat (limited to 'src/SMAPI/Framework/Networking')
-rw-r--r-- | src/SMAPI/Framework/Networking/ModMessageModel.cs | 72 | ||||
-rw-r--r-- | src/SMAPI/Framework/Networking/MultiplayerPeer.cs | 14 |
2 files changed, 81 insertions, 5 deletions
diff --git a/src/SMAPI/Framework/Networking/ModMessageModel.cs b/src/SMAPI/Framework/Networking/ModMessageModel.cs new file mode 100644 index 00000000..7ee39863 --- /dev/null +++ b/src/SMAPI/Framework/Networking/ModMessageModel.cs @@ -0,0 +1,72 @@ +using System.Linq; +using Newtonsoft.Json.Linq; + +namespace StardewModdingAPI.Framework.Networking +{ + /// <summary>The metadata for a mod message.</summary> + internal class ModMessageModel + { + /********* + ** Accessors + *********/ + /**** + ** Origin + ****/ + /// <summary>The unique ID of the player who broadcast the message.</summary> + public long FromPlayerID { get; set; } + + /// <summary>The unique ID of the mod which broadcast the message.</summary> + public string FromModID { get; set; } + + /**** + ** Destination + ****/ + /// <summary>The players who should receive the message, or <c>null</c> for all players.</summary> + public long[] ToPlayerIDs { get; set; } + + /// <summary>The mods which should receive the message, or <c>null</c> for all mods.</summary> + public string[] ToModIDs { get; set; } + + /// <summary>A message type which receiving mods can use to decide whether it's the one they want to handle, like <c>SetPlayerLocation</c>. This doesn't need to be globally unique, since mods should check the originating mod ID.</summary> + public string Type { get; set; } + + /// <summary>The custom mod data being broadcast.</summary> + public JToken Data { get; set; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + public ModMessageModel() { } + + /// <summary>Construct an instance.</summary> + /// <param name="fromPlayerID">The unique ID of the player who broadcast the message.</param> + /// <param name="fromModID">The unique ID of the mod which broadcast the message.</param> + /// <param name="toPlayerIDs">The players who should receive the message, or <c>null</c> for all players.</param> + /// <param name="toModIDs">The mods which should receive the message, or <c>null</c> for all mods.</param> + /// <param name="type">A message type which receiving mods can use to decide whether it's the one they want to handle, like <c>SetPlayerLocation</c>. This doesn't need to be globally unique, since mods should check the originating mod ID.</param> + /// <param name="data">The custom mod data being broadcast.</param> + public ModMessageModel(long fromPlayerID, string fromModID, long[] toPlayerIDs, string[] toModIDs, string type, JToken data) + { + this.FromPlayerID = fromPlayerID; + this.FromModID = fromModID; + this.ToPlayerIDs = toPlayerIDs; + this.ToModIDs = toModIDs; + this.Type = type; + this.Data = data; + } + + /// <summary>Construct an instance.</summary> + /// <param name="message">The message to clone.</param> + public ModMessageModel(ModMessageModel message) + { + this.FromPlayerID = message.FromPlayerID; + this.FromModID = message.FromModID; + this.ToPlayerIDs = message.ToPlayerIDs?.ToArray(); + this.ToModIDs = message.ToModIDs?.ToArray(); + this.Type = message.Type; + this.Data = message.Data; + } + } +} diff --git a/src/SMAPI/Framework/Networking/MultiplayerPeer.cs b/src/SMAPI/Framework/Networking/MultiplayerPeer.cs index e97e36bc..c7f8ffad 100644 --- a/src/SMAPI/Framework/Networking/MultiplayerPeer.cs +++ b/src/SMAPI/Framework/Networking/MultiplayerPeer.cs @@ -30,7 +30,7 @@ namespace StardewModdingAPI.Framework.Networking public long PlayerID { get; } /// <summary>Whether this is a connection to the host player.</summary> - public bool IsHostPlayer => this.PlayerID == Game1.MasterPlayer.UniqueMultiplayerID; + public bool IsHost { get; } /// <summary>Whether the player has SMAPI installed.</summary> public bool HasSmapi => this.ApiVersion != null; @@ -57,9 +57,11 @@ namespace StardewModdingAPI.Framework.Networking /// <param name="server">The server through which to send messages.</param> /// <param name="serverConnection">The server connection through which to send messages.</param> /// <param name="client">The client through which to send messages.</param> - public MultiplayerPeer(long playerID, RemoteContextModel model, SLidgrenServer server, NetConnection serverConnection, SLidgrenClient client) + /// <param name="isHost">Whether this is a connection to the host player.</param> + public MultiplayerPeer(long playerID, RemoteContextModel model, SLidgrenServer server, NetConnection serverConnection, SLidgrenClient client, bool isHost) { this.PlayerID = playerID; + this.IsHost = isHost; if (model != null) { this.Platform = model.Platform; @@ -84,7 +86,8 @@ namespace StardewModdingAPI.Framework.Networking model: model, server: server, serverConnection: serverConnection, - client: null + client: null, + isHost: false ); } @@ -99,7 +102,8 @@ namespace StardewModdingAPI.Framework.Networking model: model, server: null, serverConnection: null, - client: client + client: client, + isHost: true ); } @@ -119,7 +123,7 @@ namespace StardewModdingAPI.Framework.Networking /// <param name="message">The message to send.</param> public void SendMessage(OutgoingMessage message) { - if (this.IsHostPlayer) + if (this.IsHost) this.Client.sendMessage(message); else this.Server.SendMessage(this.ServerConnection, message); |