#nullable disable using System; using System.Diagnostics.CodeAnalysis; using System.IO; using Lidgren.Network; using StardewValley.Network; namespace StardewModdingAPI.Framework.Networking { /// A multiplayer server used to connect to an incoming player. This is an implementation of that adds support for SMAPI's metadata context exchange. internal class SLidgrenServer : LidgrenServer { /********* ** Fields *********/ /// SMAPI's implementation of the game's core multiplayer logic. private readonly SMultiplayer Multiplayer; /// 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. private readonly Action, Action> OnProcessingMessage; /********* ** Public methods *********/ /// Construct an instance. /// SMAPI's implementation of the game's core multiplayer logic. /// The underlying game server. /// 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. public SLidgrenServer(IGameServer gameServer, SMultiplayer multiplayer, Action, Action> onProcessingMessage) : base(gameServer) { this.Multiplayer = multiplayer; this.OnProcessingMessage = onProcessingMessage; } /********* ** Protected methods *********/ /// Parse a data message from a client. /// The raw network message to parse. [SuppressMessage("ReSharper", "AccessToDisposedClosure", Justification = "The callback is invoked synchronously.")] protected override void parseDataMessageFromClient(NetIncomingMessage rawMessage) { // add hook to call multiplayer core NetConnection peer = rawMessage.SenderConnection; using IncomingMessage message = new(); using Stream readStream = new NetBufferReadStream(rawMessage); using BinaryReader reader = new(readStream); while (rawMessage.LengthBits - rawMessage.Position >= 8) { message.Read(reader); NetConnection connection = rawMessage.SenderConnection; // don't pass rawMessage into context because it gets reused this.OnProcessingMessage(message, outgoing => this.sendMessage(connection, outgoing), () => { if (this.peers.ContainsLeft(message.FarmerID) && this.peers[message.FarmerID] == peer) this.gameServer.processIncomingMessage(message); else if (message.MessageType == StardewValley.Multiplayer.playerIntroduction) { NetFarmerRoot farmer = this.Multiplayer.readFarmer(message.Reader); this.gameServer.checkFarmhandRequest("", this.getConnectionId(rawMessage.SenderConnection), farmer, msg => this.sendMessage(peer, msg), () => this.peers[farmer.Value.UniqueMultiplayerID] = peer); } }); } } } }