using System; using StardewValley; using StardewValley.Network; namespace StardewModdingAPI.Framework.Networking { /// A multiplayer client used to connect to a hosted server. This is an implementation of that adds support for SMAPI's metadata context exchange. internal class SLidgrenClient : LidgrenClient { /********* ** Properties *********/ /// Get the metadata to include in a metadata message sent to other players. private readonly Func GetMetadataMessageFields; /// The method to call when receiving a custom SMAPI message from the server, which returns whether the message was processed. private readonly Func TryProcessMessage; /********* ** Public methods *********/ /// Construct an instance. /// The remote address being connected. /// Get the metadata to include in a metadata message sent to other players. /// The method to call when receiving a custom SMAPI message from the server, which returns whether the message was processed.. public SLidgrenClient(string address, Func getMetadataMessageFields, Func tryProcessMessage) : base(address) { this.GetMetadataMessageFields = getMetadataMessageFields; this.TryProcessMessage = tryProcessMessage; } /// Send the metadata needed to connect with a remote server. public override void sendPlayerIntroduction() { // send custom intro if (this.getUserID() != "") Game1.player.userID.Value = this.getUserID(); this.sendMessage(SMultiplayer.ContextSyncMessageID, this.GetMetadataMessageFields()); base.sendPlayerIntroduction(); } /********* ** Protected methods *********/ /// Process an incoming network message. /// The message to process. protected override void processIncomingMessage(IncomingMessage message) { if (this.TryProcessMessage(this, message)) return; base.processIncomingMessage(message); } } }