From 0fb7e729c1f77625c425024721fe138623a9af90 Mon Sep 17 00:00:00 2001 From: Date: Wed, 9 May 2018 15:57:20 -0700 Subject: [PATCH] Updated NetMod to prevent redundant messages and errors. --- .../Framework/Clients/CustomLidgrenClient.cs | 44 +++++++++++++++++-- .../Framework/CustomMultiplayer.cs | 27 ++++++++++++ .../Framework/Servers/CustomGameServer.cs | 31 ++++++------- .../Framework/Servers/CustomLidgrenServer.cs | 22 ++++++---- 4 files changed, 98 insertions(+), 26 deletions(-) diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/Clients/CustomLidgrenClient.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/Clients/CustomLidgrenClient.cs index 89d7971e..0181ccc2 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/Clients/CustomLidgrenClient.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/Clients/CustomLidgrenClient.cs @@ -25,6 +25,8 @@ namespace ModdedUtilitiesNetworking.Framework.Clients this.address = address; } + + public override string getUserID() { @@ -198,8 +200,44 @@ namespace ModdedUtilitiesNetworking.Framework.Clients protected override void processIncomingMessage(IncomingMessage message) { - - base.processIncomingMessage(message); + + byte messageType = message.MessageType; + if ((uint)messageType <= 9U) + { + switch (messageType) + { + case 1: + this.receiveServerIntroduction(message.Reader); + return; + case 2: + this.userNames[message.FarmerID] = message.Reader.ReadString(); + ModCore.multiplayer.processIncomingMessage(message); + return; + case 3: + ModCore.multiplayer.processIncomingMessage(message); + return; + case 9: + this.receiveAvailableFarmhands(message.Reader); + return; + } + } + else if ((int)messageType != 11) + { + if ((int)messageType == 16) + { + if (message.FarmerID != Game1.serverHost.Value.UniqueMultiplayerID) + return; + this.receiveUserNameUpdate(message.Reader); + return; + } + } + else + { + this.connectionMessage = message.Reader.ReadString(); + return; + } + ModCore.multiplayer.baseProcessMessage(message); + //Packet signiture for functions that return nothing. if (message.MessageType == Enums.MessageTypes.SendOneWay || message.MessageType == Enums.MessageTypes.SendToAll) @@ -250,7 +288,7 @@ namespace ModdedUtilitiesNetworking.Framework.Clients this.setUpGame(); if (Game1.chatBox == null) return; - Game1.chatBox.listPlayers(); + //Game1.chatBox.listPlayers(); } } } diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs index f12cec5c..7c5d4299 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs @@ -1,7 +1,9 @@ using ModdedUtilitiesNetworking.Framework.Clients; using ModdedUtilitiesNetworking.Framework.Messages; using ModdedUtilitiesNetworking.Framework.Servers; +using Netcode; using StardewValley; +using StardewValley.Menus; using StardewValley.Network; using System; using System.Collections.Generic; @@ -15,6 +17,12 @@ namespace ModdedUtilitiesNetworking.Framework { public class CustomMultiplayer : StardewValley.Multiplayer { + public List hasConnectedOnce = new List(); + + public CustomMultiplayer() + { + this.hasConnectedOnce = new List(); + } public override bool isClientBroadcastType(byte messageType) { @@ -78,6 +86,11 @@ namespace ModdedUtilitiesNetworking.Framework this.tickLocationRoots(); } + public void baseProcessMessage(IncomingMessage message) + { + base.processIncomingMessage(message); + } + /// /// Creates a net outgoing message that is written specifically to call a void function when sent. USed to specifiy types and specific ways to handle them. /// @@ -255,6 +268,20 @@ namespace ModdedUtilitiesNetworking.Framework } } + public override void addPlayer(NetFarmerRoot f) + { + long uniqueMultiplayerId = f.Value.UniqueMultiplayerID; + //Surpresses the first connected chat info message on non modded clients. + if (this.hasConnectedOnce.Contains(uniqueMultiplayerId)) + { + base.addPlayer(f); + } + else + { + this.hasConnectedOnce.Add(uniqueMultiplayerId); + } + } + /// /// Get's the farmer in the player two slot for the server. /// diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomGameServer.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomGameServer.cs index b407c4c1..450632f1 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomGameServer.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomGameServer.cs @@ -21,6 +21,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers { public List servers = new List(); public List pendingGameAvailableActions = new List(); + public List hasPlayerDisconnectedOnce = new List(); public CustomGameServer() { @@ -28,7 +29,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers this.servers.Add(ModCore.multiplayer.InitServer((Server)new CustomLidgrenServer((IGameServer)this))); ModCore.monitor.Log("Custom Lidgren Server Created"); ModCore.monitor.Log("Custom Game Server Created"); - + hasPlayerDisconnectedOnce = new List(); try { if (Program.sdk.Networking == null) return; @@ -46,16 +47,6 @@ namespace ModdedUtilitiesNetworking.Framework.Servers } } - public CustomGameServer(List servers) - { - this.servers = new List(); - - foreach(var server in servers) - { - this.servers.Add(server); - } - } - public int connectionsCount { get @@ -225,14 +216,24 @@ namespace ModdedUtilitiesNetworking.Framework.Servers ModCore.multiplayer.playerDisconnected(disconnectee); if (sourceFarmer == null) return; - OutgoingMessage message = new OutgoingMessage((byte)19, sourceFarmer, new object[0]); - foreach (long peerId in (IEnumerable)Game1.otherFarmers.Keys) + + if (this.hasPlayerDisconnectedOnce.Contains(disconnectee)) { - if (peerId != disconnectee) - this.sendMessage(peerId, message); + OutgoingMessage message = new OutgoingMessage((byte)19, sourceFarmer, new object[0]); + foreach (long peerId in (IEnumerable)Game1.otherFarmers.Keys) + { + if (peerId != disconnectee) + this.sendMessage(peerId, message); + } + } + else + { + this.hasPlayerDisconnectedOnce.Add(disconnectee); } } + + public bool isGameAvailable() { bool flag1 = Game1.currentMinigame is Intro || Game1.Date.DayOfMonth == 0; diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs index 5c48fa49..afbac22f 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs @@ -12,7 +12,7 @@ using System.Threading.Tasks; namespace ModdedUtilitiesNetworking.Framework.Servers { - class CustomLidgrenServer : LidgrenServer + class CustomLidgrenServer : Server { private HashSet introductionsSent = new HashSet(); private Bimap peers = new Bimap(); @@ -150,18 +150,24 @@ namespace ModdedUtilitiesNetworking.Framework.Servers case NetConnectionStatus.Disconnecting: if (!this.peers.ContainsRight(message.SenderConnection)) break; - try - { - this.playerDisconnected(this.peers[message.SenderConnection]); - } - catch(Exception err) - { + //Forces a de-sync with money. + + + this.playerDisconnected(this.peers[message.SenderConnection]); - } break; } } + protected override void playerDisconnected(long disconnectee) + { + this.gameServer.playerDisconnected(disconnectee); + + this.introductionsSent.Remove(this.peers[disconnectee]); + + this.peers.RemoveLeft(disconnectee); + } + private void parseDataMessageFromClient(NetIncomingMessage dataMsg) {