diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2018-11-14 00:11:09 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2018-11-14 00:11:09 -0500 |
commit | 90ecd377c88ba5a4c08a3c7e67618435358e5685 (patch) | |
tree | 4b792e5e7633286973d07c3ef857455cd4a76661 /src/SMAPI | |
parent | 1f578ed890da9f3266046cc253ac7a631ab7575f (diff) | |
download | SMAPI-90ecd377c88ba5a4c08a3c7e67618435358e5685.tar.gz SMAPI-90ecd377c88ba5a4c08a3c7e67618435358e5685.tar.bz2 SMAPI-90ecd377c88ba5a4c08a3c7e67618435358e5685.zip |
rework multiplayer code to allow for upcoming Galaxy server overrides (#480)
Diffstat (limited to 'src/SMAPI')
-rw-r--r-- | src/SMAPI/Framework/Networking/MultiplayerPeer.cs | 60 | ||||
-rw-r--r-- | src/SMAPI/Framework/Networking/SLidgrenServer.cs | 59 | ||||
-rw-r--r-- | src/SMAPI/Framework/SMultiplayer.cs | 42 | ||||
-rw-r--r-- | src/SMAPI/Patches/LidgrenServerPatch.cs | 30 |
4 files changed, 28 insertions, 163 deletions
diff --git a/src/SMAPI/Framework/Networking/MultiplayerPeer.cs b/src/SMAPI/Framework/Networking/MultiplayerPeer.cs index 7f0fa4f7..44a71978 100644 --- a/src/SMAPI/Framework/Networking/MultiplayerPeer.cs +++ b/src/SMAPI/Framework/Networking/MultiplayerPeer.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using Lidgren.Network; using StardewValley.Network; namespace StardewModdingAPI.Framework.Networking @@ -12,14 +11,8 @@ namespace StardewModdingAPI.Framework.Networking /********* ** Properties *********/ - /// <summary>The server through which to send messages, if this is an incoming farmhand.</summary> - private readonly SLidgrenServer Server; - - /// <summary>The client through which to send messages, if this is the host player.</summary> - private readonly SLidgrenClient Client; - - /// <summary>The network connection to the player.</summary> - private readonly NetConnection ServerConnection; + /// <summary>A method which sends a message to the peer.</summary> + private readonly Action<OutgoingMessage> SendMessageImpl; /********* @@ -53,11 +46,9 @@ namespace StardewModdingAPI.Framework.Networking /// <summary>Construct an instance.</summary> /// <param name="playerID">The player's unique ID.</param> /// <param name="model">The metadata to copy.</param> - /// <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> + /// <param name="sendMessage">A method which sends a message to the peer.</param> /// <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) + public MultiplayerPeer(long playerID, RemoteContextModel model, Action<OutgoingMessage> sendMessage, bool isHost) { this.PlayerID = playerID; this.IsHost = isHost; @@ -68,43 +59,7 @@ namespace StardewModdingAPI.Framework.Networking this.ApiVersion = model.ApiVersion; this.Mods = model.Mods.Select(mod => new MultiplayerPeerMod(mod)).ToArray(); } - this.Server = server; - this.ServerConnection = serverConnection; - this.Client = client; - } - - /// <summary>Construct an instance for a connection to an incoming farmhand.</summary> - /// <param name="playerID">The player's unique ID.</param> - /// <param name="model">The metadata to copy, if available.</param> - /// <param name="server">The server through which to send messages.</param> - /// <param name="serverConnection">The server connection through which to send messages.</param> - public static MultiplayerPeer ForConnectionToFarmhand(long playerID, RemoteContextModel model, SLidgrenServer server, NetConnection serverConnection) - { - return new MultiplayerPeer( - playerID: playerID, - model: model, - server: server, - serverConnection: serverConnection, - client: null, - isHost: false - ); - } - - /// <summary>Construct an instance for a connection to the host player.</summary> - /// <param name="playerID">The player's unique ID.</param> - /// <param name="model">The metadata to copy.</param> - /// <param name="client">The client through which to send messages.</param> - /// <param name="isHost">Whether this connection is for the host player.</param> - public static MultiplayerPeer ForConnectionToHost(long playerID, RemoteContextModel model, SLidgrenClient client, bool isHost) - { - return new MultiplayerPeer( - playerID: playerID, - model: model, - server: null, - serverConnection: null, - client: client, - isHost: isHost - ); + this.SendMessageImpl = sendMessage; } /// <summary>Get metadata for a mod installed by the player.</summary> @@ -123,10 +78,7 @@ namespace StardewModdingAPI.Framework.Networking /// <param name="message">The message to send.</param> public void SendMessage(OutgoingMessage message) { - if (this.IsHost) - this.Client.sendMessage(message); - else - this.Server.SendMessage(this.ServerConnection, message); + this.SendMessageImpl(message); } } } diff --git a/src/SMAPI/Framework/Networking/SLidgrenServer.cs b/src/SMAPI/Framework/Networking/SLidgrenServer.cs index 060b433b..36f96bc3 100644 --- a/src/SMAPI/Framework/Networking/SLidgrenServer.cs +++ b/src/SMAPI/Framework/Networking/SLidgrenServer.cs @@ -20,24 +20,15 @@ namespace StardewModdingAPI.Framework.Networking /// <summary>The constructor for the internal <c>NetBufferReadStream</c> type.</summary> private readonly ConstructorInfo NetBufferReadStreamConstructor = SLidgrenServer.GetNetBufferReadStreamConstructor(); - /// <summary>The constructor for the internal <c>NetBufferWriteStream</c> type.</summary> - private readonly ConstructorInfo NetBufferWriteStreamConstructor = SLidgrenServer.GetNetBufferWriteStreamConstructor(); - /// <summary>A method which reads farmer data from the given binary reader.</summary> private readonly Func<BinaryReader, NetFarmerRoot> ReadFarmer; - /// <summary>A callback to raise when receiving a message. This receives the server instance, raw/parsed incoming message, and a callback to run the default logic.</summary> - private readonly Action<SLidgrenServer, NetIncomingMessage, IncomingMessage, Action> OnProcessingMessage; - - /// <summary>A callback to raise when sending a message. This receives the server instance, outgoing connection, outgoing message, target player ID, and a callback to run the default logic.</summary> - private readonly Action<SLidgrenServer, NetConnection, OutgoingMessage, Action> OnSendingMessage; + /// <summary>A callback to raise when receiving a message. This receives the incoming message, a method to send a message, and a callback to run the default logic.</summary> + private readonly Action<IncomingMessage, Action<OutgoingMessage>, Action> OnProcessingMessage; /// <summary>The peer connections.</summary> private readonly Bimap<long, NetConnection> Peers; - /// <summary>The underlying net server.</summary> - private readonly IReflectedField<NetServer> Server; - /********* ** Public methods @@ -46,34 +37,13 @@ namespace StardewModdingAPI.Framework.Networking /// <param name="gameServer">The underlying game server.</param> /// <param name="reflection">Simplifies access to private code.</param> /// <param name="readFarmer">A method which reads farmer data from the given binary reader.</param> - /// <param name="onProcessingMessage">A callback to raise when receiving a message. This receives the server instance, raw/parsed incoming message, and a callback to run the default logic.</param> - /// <param name="onSendingMessage">A callback to raise when sending a message. This receives the server instance, outgoing connection, outgoing message, and a callback to run the default logic.</param> - public SLidgrenServer(IGameServer gameServer, Reflector reflection, Func<BinaryReader, NetFarmerRoot> readFarmer, Action<SLidgrenServer, NetIncomingMessage, IncomingMessage, Action> onProcessingMessage, Action<SLidgrenServer, NetConnection, OutgoingMessage, Action> onSendingMessage) + /// <param name="onProcessingMessage">A callback to raise when receiving a message. This receives the incoming message, a method to send a message, and a callback to run the default logic.</param> + public SLidgrenServer(IGameServer gameServer, Reflector reflection, Func<BinaryReader, NetFarmerRoot> readFarmer, Action<IncomingMessage, Action<OutgoingMessage>, Action> onProcessingMessage) : base(gameServer) { this.ReadFarmer = readFarmer; this.OnProcessingMessage = onProcessingMessage; - this.OnSendingMessage = onSendingMessage; this.Peers = reflection.GetField<Bimap<long, NetConnection>>(this, "peers").GetValue(); - this.Server = reflection.GetField<NetServer>(this, "server"); - } - - /// <summary>Send a message to a remote server.</summary> - /// <param name="connection">The network connection.</param> - /// <param name="message">The message to send.</param> - /// <remarks>This is an implementation of <see cref="LidgrenServer.sendMessage(NetConnection, OutgoingMessage)"/> which calls <see cref="OnSendingMessage"/>. This method is invoked via <see cref="LidgrenServerPatch.Prefix_LidgrenServer_SendMessage"/>.</remarks> - public void SendMessage(NetConnection connection, OutgoingMessage message) - { - this.OnSendingMessage(this, connection, message, () => - { - NetServer server = this.Server.GetValue(); - NetOutgoingMessage netMessage = server.CreateMessage(); - using (Stream bufferWriteStream = (Stream)this.NetBufferWriteStreamConstructor.Invoke(new object[] { netMessage })) - using (BinaryWriter writer = new BinaryWriter(bufferWriteStream)) - message.Write(writer); - - server.SendMessage(netMessage, connection, NetDeliveryMethod.ReliableOrdered); - }); } /// <summary>Parse a data message from a client.</summary> @@ -91,14 +61,14 @@ namespace StardewModdingAPI.Framework.Networking while (rawMessage.LengthBits - rawMessage.Position >= 8) { message.Read(reader); - this.OnProcessingMessage(this, rawMessage, message, () => + this.OnProcessingMessage(message, outgoing => this.sendMessage(rawMessage.SenderConnection, outgoing), () => { if (this.Peers.ContainsLeft(message.FarmerID) && this.Peers[message.FarmerID] == peer) this.gameServer.processIncomingMessage(message); else if (message.MessageType == Multiplayer.playerIntroduction) { NetFarmerRoot farmer = this.ReadFarmer(message.Reader); - this.gameServer.checkFarmhandRequest("", farmer, msg => this.SendMessage(peer, msg), () => this.Peers[farmer.Value.UniqueMultiplayerID] = peer); + this.gameServer.checkFarmhandRequest("", farmer, msg => this.sendMessage(peer, msg), () => this.Peers[farmer.Value.UniqueMultiplayerID] = peer); } }); } @@ -127,22 +97,5 @@ namespace StardewModdingAPI.Framework.Networking return constructor; } - - /// <summary>Get the constructor for the internal <c>NetBufferWriteStream</c> type.</summary> - private static ConstructorInfo GetNetBufferWriteStreamConstructor() - { - // get type - string typeName = $"StardewValley.Network.NetBufferWriteStream, {Constants.GameAssemblyName}"; - Type type = Type.GetType(typeName); - if (type == null) - throw new InvalidOperationException($"Can't find type: {typeName}"); - - // get constructor - ConstructorInfo constructor = type.GetConstructor(new[] { typeof(NetBuffer) }); - if (constructor == null) - throw new InvalidOperationException($"Can't find constructor for type: {typeName}"); - - return constructor; - } } } diff --git a/src/SMAPI/Framework/SMultiplayer.cs b/src/SMAPI/Framework/SMultiplayer.cs index dc9b8b68..843cd1fb 100644 --- a/src/SMAPI/Framework/SMultiplayer.cs +++ b/src/SMAPI/Framework/SMultiplayer.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; -using Lidgren.Network; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using StardewModdingAPI.Events; @@ -119,26 +118,18 @@ namespace StardewModdingAPI.Framework /// <param name="server">The server to initialise.</param> public override Server InitServer(Server server) { - if (server is LidgrenServer) + switch (server) { - IGameServer gameServer = this.Reflection.GetField<IGameServer>(server, "gameServer").GetValue(); - return new SLidgrenServer(gameServer, this.Reflection, this.readFarmer, this.OnServerProcessingMessage, this.OnServerSendingMessage); - } + case LidgrenServer _: + { + IGameServer gameServer = this.Reflection.GetField<IGameServer>(server, "gameServer").GetValue(); + return new SLidgrenServer(gameServer, this.Reflection, this.readFarmer, this.OnServerProcessingMessage); + } - return server; - } - /// <summary>A callback raised when sending a network message as the host player.</summary> - /// <param name="server">The server sending the message.</param> - /// <param name="connection">The connection to which a message is being sent.</param> - /// <param name="message">The message being sent.</param> - /// <param name="resume">Send the underlying message.</param> - protected void OnServerSendingMessage(SLidgrenServer server, NetConnection connection, OutgoingMessage message, Action resume) - { - if (this.Monitor.IsVerbose) - this.Monitor.Log($"SERVER SEND {(MessageType)message.MessageType} {message.FarmerID}", LogLevel.Trace); - - resume(); + default: + return server; + } } /// <summary>A callback raised when sending a message as a farmhand.</summary> @@ -166,11 +157,10 @@ namespace StardewModdingAPI.Framework } /// <summary>Process an incoming network message as the host player.</summary> - /// <param name="server">The server instance that received the connection.</param> - /// <param name="rawMessage">The raw network message that was received.</param> /// <param name="message">The message to process.</param> + /// <param name="sendMessage">A method which sends the given message to the client.</param> /// <param name="resume">Process the message using the game's default logic.</param> - public void OnServerProcessingMessage(SLidgrenServer server, NetIncomingMessage rawMessage, IncomingMessage message, Action resume) + public void OnServerProcessingMessage(IncomingMessage message, Action<OutgoingMessage> sendMessage, Action resume) { if (this.Monitor.IsVerbose) this.Monitor.Log($"SERVER RECV {(MessageType)message.MessageType} {message.FarmerID}", LogLevel.Trace); @@ -185,7 +175,7 @@ namespace StardewModdingAPI.Framework this.Monitor.Log($"Received context for farmhand {message.FarmerID} running {(model != null ? $"SMAPI {model.ApiVersion} with {model.Mods.Length} mods" : "vanilla")}.", LogLevel.Trace); // store peer - MultiplayerPeer newPeer = MultiplayerPeer.ForConnectionToFarmhand(message.FarmerID, model, server, rawMessage.SenderConnection); + MultiplayerPeer newPeer = new MultiplayerPeer(message.FarmerID, model, sendMessage, isHost: false); if (this.Peers.ContainsKey(message.FarmerID)) { this.Monitor.Log($"Rejected mod context from farmhand {message.FarmerID}: already received context for that player.", LogLevel.Error); @@ -226,7 +216,7 @@ namespace StardewModdingAPI.Framework if (!this.Peers.ContainsKey(message.FarmerID)) { this.Monitor.Log($"Received connection for vanilla player {message.FarmerID}.", LogLevel.Trace); - MultiplayerPeer peer = MultiplayerPeer.ForConnectionToFarmhand(message.FarmerID, null, server, rawMessage.SenderConnection); + MultiplayerPeer peer = new MultiplayerPeer(message.FarmerID, null, sendMessage, isHost: false); this.AddPeer(peer, canBeHost: false); } @@ -264,7 +254,7 @@ namespace StardewModdingAPI.Framework this.Monitor.Log($"Received context for {(model?.IsHost == true ? "host" : "farmhand")} {message.FarmerID} running {(model != null ? $"SMAPI {model.ApiVersion} with {model.Mods.Length} mods" : "vanilla")}.", LogLevel.Trace); // store peer - MultiplayerPeer peer = MultiplayerPeer.ForConnectionToHost(message.FarmerID, model, client, model?.IsHost ?? this.HostPeer == null); + MultiplayerPeer peer = new MultiplayerPeer(message.FarmerID, model, client.sendMessage, isHost: model?.IsHost ?? this.HostPeer == null); if (peer.IsHost && this.HostPeer != null) { this.Monitor.Log($"Rejected mod context from host player {peer.PlayerID}: already received host data from {(peer.PlayerID == this.HostPeer.PlayerID ? "that player" : $"player {peer.PlayerID}")}.", LogLevel.Error); @@ -281,7 +271,7 @@ namespace StardewModdingAPI.Framework if (!this.Peers.ContainsKey(message.FarmerID) && this.HostPeer == null) { this.Monitor.Log($"Received connection for vanilla host {message.FarmerID}.", LogLevel.Trace); - this.AddPeer(MultiplayerPeer.ForConnectionToHost(message.FarmerID, null, client, isHost: true), canBeHost: false); + this.AddPeer(new MultiplayerPeer(message.FarmerID, null, client.sendMessage, isHost: true), canBeHost: false); } resume(); break; @@ -293,7 +283,7 @@ namespace StardewModdingAPI.Framework // store peer if (!this.Peers.TryGetValue(message.FarmerID, out MultiplayerPeer peer)) { - peer = MultiplayerPeer.ForConnectionToHost(message.FarmerID, null, client, isHost: this.HostPeer == null); + peer = new MultiplayerPeer(message.FarmerID, null, client.sendMessage, isHost: this.HostPeer == null); this.Monitor.Log($"Received connection for vanilla {(peer.IsHost ? "host" : "farmhand")} {message.FarmerID}.", LogLevel.Trace); this.AddPeer(peer, canBeHost: true); } diff --git a/src/SMAPI/Patches/LidgrenServerPatch.cs b/src/SMAPI/Patches/LidgrenServerPatch.cs index 6f937665..47acd4c4 100644 --- a/src/SMAPI/Patches/LidgrenServerPatch.cs +++ b/src/SMAPI/Patches/LidgrenServerPatch.cs @@ -1,13 +1,9 @@ -using System; using System.Diagnostics.CodeAnalysis; -using System.IO; using System.Reflection; using Harmony; using Lidgren.Network; -using StardewModdingAPI.Framework; using StardewModdingAPI.Framework.Networking; using StardewModdingAPI.Framework.Patching; -using StardewValley; using StardewValley.Network; namespace StardewModdingAPI.Patches @@ -35,13 +31,6 @@ namespace StardewModdingAPI.Patches MethodInfo prefix = AccessTools.Method(this.GetType(), nameof(LidgrenServerPatch.Prefix_LidgrenServer_ParseDataMessageFromClient)); harmony.Patch(method, new HarmonyMethod(prefix), null); } - - // override sendMessage - { - MethodInfo method = typeof(LidgrenServer).GetMethod("sendMessage", BindingFlags.NonPublic | BindingFlags.Instance, null, new [] { typeof(NetConnection), typeof(OutgoingMessage) }, null); - MethodInfo prefix = AccessTools.Method(this.GetType(), nameof(LidgrenServerPatch.Prefix_LidgrenServer_SendMessage)); - harmony.Patch(method, new HarmonyMethod(prefix), null); - } } @@ -66,24 +55,5 @@ namespace StardewModdingAPI.Patches return true; } - - /// <summary>The method to call instead of the <see cref="LidgrenServer.sendMessage"/> method.</summary> - /// <param name="__instance">The instance being patched.</param> - /// <param name="connection">The connection to which to send the message.</param> - /// <param name="___peers">The private <c>peers</c> field on the <paramref name="__instance"/> instance.</param> - /// <param name="___gameServer">The private <c>gameServer</c> field on the <paramref name="__instance"/> instance.</param> - /// <returns>Returns whether to execute the original method.</returns> - /// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks> - [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony.")] - private static bool Prefix_LidgrenServer_SendMessage(LidgrenServer __instance, NetConnection connection, OutgoingMessage message, Bimap<long, NetConnection> ___peers, IGameServer ___gameServer) - { - if (__instance is SLidgrenServer smapiServer) - { - smapiServer.SendMessage(connection, message); - return false; - } - - return true; - } } } |