From 651cd8a26b99fda535791695ed8f4c1b3693c4ec Mon Sep 17 00:00:00 2001 From: Date: Thu, 10 May 2018 00:16:05 -0700 Subject: [PATCH] Updated ModNet to be compatable with SDV 1.3.10, which adds bug fixes for sprite errors. --- .../ModdedUtilitiesNetworking/Class1.cs | 6 +- .../Framework/Clients/CustomLidgrenClient.cs | 2 +- .../Framework/CustomMultiplayer.cs | 131 +++++++++++++++++- .../Framework/Servers/CustomGameServer.cs | 65 ++++++--- .../Framework/Servers/CustomLidgrenServer.cs | 74 +++++----- 5 files changed, 214 insertions(+), 64 deletions(-) diff --git a/GeneralMods/ModdedUtilitiesNetworking/Class1.cs b/GeneralMods/ModdedUtilitiesNetworking/Class1.cs index f23a7b91..0b337175 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Class1.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Class1.cs @@ -117,11 +117,11 @@ namespace ModdedUtilitiesNetworking ModCore.monitor.Log("Custom multiplayer binding success!"); } - if(Game1.client !=null && multiplayerSet == false) + if (Game1.client != null && multiplayerSet ==false ) { if(Game1.client is LidgrenClient) { - + BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic; // get the field info @@ -131,7 +131,7 @@ namespace ModdedUtilitiesNetworking var address = (string)finfo.GetValue(Game1.client); Game1.client.disconnect(true); //Disconnect old client - CustomLidgrenClient client = new CustomLidgrenClient(address); + LidgrenClient client = new CustomLidgrenClient(address); Game1.client = client; client.connect(); //Connect new client. multiplayerSet = true; diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/Clients/CustomLidgrenClient.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/Clients/CustomLidgrenClient.cs index 0181ccc2..b6928430 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/Clients/CustomLidgrenClient.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/Clients/CustomLidgrenClient.cs @@ -236,7 +236,7 @@ namespace ModdedUtilitiesNetworking.Framework.Clients this.connectionMessage = message.Reader.ReadString(); return; } - ModCore.multiplayer.baseProcessMessage(message); + ModCore.multiplayer.processIncomingMessage(message); //Packet signiture for functions that return nothing. diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs index 7c5d4299..aba2e3e7 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/CustomMultiplayer.cs @@ -7,6 +7,7 @@ using StardewValley.Menus; using StardewValley.Network; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Text; @@ -22,8 +23,49 @@ namespace ModdedUtilitiesNetworking.Framework public CustomMultiplayer() { this.hasConnectedOnce = new List(); + + } + public override void writeObjectFull(BinaryWriter writer, NetRoot root, long? peer) + { + try + { + root.CreateConnectionPacket(writer, peer); + } + catch(Exception err) + { + + } + } + + public override void readObjectDelta(BinaryReader reader, NetRoot root) + { + try + { + root.Read(reader); + } + catch(Exception err) + { + + } + } + + public override void receiveWorldState(BinaryReader msg) + { + this.readObjectDelta(msg, Game1.netWorldState); + if (Game1.IsServer) + return; + int num1 = Game1.timeOfDay; + Game1.netWorldState.Value.WriteToGame1(); + int num2 = Game1.timeOfDay; + if (num1 == num2 || Game1.currentLocation == null || Game1.newDaySync != null) + return; + Game1.performTenMinuteClockUpdate(); + } + + + public override bool isClientBroadcastType(byte messageType) { return true; @@ -31,7 +73,94 @@ namespace ModdedUtilitiesNetworking.Framework public override void processIncomingMessage(IncomingMessage msg) { - base.processIncomingMessage(msg); + if (msg.MessageType <= 19) + { + switch (msg.MessageType) + { + case 0: + NetFarmerRoot netFarmerRoot = this.farmerRoot(msg.Reader.ReadInt64()); + this.readObjectDelta(msg.Reader, (NetRoot)netFarmerRoot); + break; + case 2: + this.receivePlayerIntroduction(msg.Reader); + break; + case 3: + this.readActiveLocation(msg, false); + break; + case 4: + int eventId = msg.Reader.ReadInt32(); + int tileX = msg.Reader.ReadInt32(); + int tileY = msg.Reader.ReadInt32(); + if (Game1.CurrentEvent != null) + break; + this.readWarp(msg.Reader, tileX, tileY, (Action)(() => + { + Farmer farmerActor = (msg.SourceFarmer.NetFields.Root as NetRoot).Clone().Value; + farmerActor.currentLocation = Game1.currentLocation; + farmerActor.completelyStopAnimatingOrDoingAction(); + farmerActor.hidden.Value = false; + Event eventById = Game1.currentLocation.findEventById(eventId, farmerActor); + Game1.currentLocation.startEvent(eventById); + farmerActor.Position = Game1.player.Position; + })); + break; + case 6: + GameLocation gameLocation = this.readLocation(msg.Reader); + if (gameLocation == null) + break; + this.readObjectDelta(msg.Reader, gameLocation.Root); + break; + case 7: + GameLocation location = this.readLocation(msg.Reader); + if (location == null) + break; + location.temporarySprites.AddRange((IEnumerable)this.readSprites(msg.Reader, location)); + break; + case 8: + NPC character = this.readNPC(msg.Reader); + GameLocation targetLocation = this.readLocation(msg.Reader); + if (character == null || targetLocation == null) + break; + Game1.warpCharacter(character, targetLocation, BinaryReaderWriterExtensions.ReadVector2(msg.Reader)); + break; + case 10: + this.receiveChatMessage(msg.SourceFarmer, BinaryReaderWriterExtensions.ReadEnum(msg.Reader), msg.Reader.ReadString()); + break; + case 12: + try + { + this.receiveWorldState(msg.Reader); + } + catch(Exception err) + { + + } + break; + case 13: + this.receiveTeamDelta(msg.Reader); + break; + case 14: + this.receiveNewDaySync(msg); + break; + case 15: + string messageKey = msg.Reader.ReadString(); + string[] args = new string[(int)msg.Reader.ReadByte()]; + for (int index = 0; index < args.Length; ++index) + args[index] = msg.Reader.ReadString(); + this.receiveChatInfoMessage(msg.SourceFarmer, messageKey, args); + break; + case 17: + this.receiveFarmerGainExperience(msg); + break; + case 18: + this.parseServerToClientsMessage(msg.Reader.ReadString()); + break; + case 19: + this.playerDisconnected(msg.SourceFarmer.UniqueMultiplayerID); + break; + } + } + if (msg.MessageType == 20) { ModCore.monitor.Log("CUSTOM FUNCTION???"); diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomGameServer.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomGameServer.cs index 450632f1..adb9ff9b 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomGameServer.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomGameServer.cs @@ -47,13 +47,13 @@ namespace ModdedUtilitiesNetworking.Framework.Servers } } - public int connectionsCount - { - get - { - return Enumerable.Sum((IEnumerable)this.servers, (Func)(s => s.connectionsCount)); - } - } + public int connectionsCount + { + get + { + return Enumerable.Sum((IEnumerable) this.servers, (Func) (s => s.connectionsCount)); + } + } @@ -68,15 +68,15 @@ namespace ModdedUtilitiesNetworking.Framework.Servers return (string)null; } - public string getUserName(long farmerId) - { - foreach (Server server in this.servers) - { - if (server.getUserName(farmerId) != null) - return server.getUserName(farmerId); - } - return (string)null; - } + public string getUserName(long farmerId) + { + foreach (Server server in this.servers) + { + if (server.getUserName(farmerId) != null) + return server.getUserName(farmerId); + } + return (string) null; + } protected void initialize() { @@ -104,6 +104,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers foreach (Server server in this.servers) { + if (server == null) continue; server.receiveMessages(); } if (!this.isGameAvailable()) @@ -116,7 +117,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers public void sendMessage(long peerId, OutgoingMessage message) { foreach (Server server in this.servers) - if (server is CustomLidgrenServer) + if (server is CustomLidgrenServer || server is LidgrenServer) { server.sendMessage(peerId, message); } @@ -136,7 +137,18 @@ namespace ModdedUtilitiesNetworking.Framework.Servers public bool canOfferInvite() { - return Enumerable.Aggregate(Enumerable.Select((IEnumerable)this.servers, (Func)(s => s.canOfferInvite())), false, (Func)((a, b) => a | b)); + if (this.servers == null) + { + ModCore.monitor.Log("WAIT WHY IS THIS HAPPENING????"); + } + foreach(var v in this.servers) + { + if (v.canOfferInvite() == true) continue; + else return false; + } + return true; + + //return Enumerable.Aggregate(Enumerable.Select((IEnumerable)this.servers, (Func)(s => s.canOfferInvite())), false, (Func)((a, b) => a | b)); } public void offerInvite() @@ -177,12 +189,12 @@ namespace ModdedUtilitiesNetworking.Framework.Servers { Console.WriteLine("Starting server. Protocol version: " + ModCore.multiplayer.protocolVersion); this.initialize(); - if ((NetFieldBase>)Game1.serverHost == (NetRef)null) + if (Game1.serverHost.Value == null) Game1.serverHost = new NetFarmerRoot(); Game1.serverHost.Value = Game1.player; Game1.serverHost.MarkClean(); Game1.serverHost.Clock.InterpolationTicks = ModCore.multiplayer.defaultInterpolationTicks; - if ((NetFieldBase>)Game1.netWorldState == (NetRef)null) + if (Game1.netWorldState.Value == (NetRef)null) Game1.netWorldState = new NetRoot((IWorldState)new NetWorldState()); Game1.netWorldState.Clock.InterpolationTicks = 0; Game1.netWorldState.Value.UpdateFromGame1(); @@ -209,6 +221,8 @@ namespace ModdedUtilitiesNetworking.Framework.Servers } } + + public void playerDisconnected(long disconnectee) { Farmer sourceFarmer = (Farmer)null; @@ -217,6 +231,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers if (sourceFarmer == null) return; + if (this.hasPlayerDisconnectedOnce.Contains(disconnectee)) { OutgoingMessage message = new OutgoingMessage((byte)19, sourceFarmer, new object[0]); @@ -230,6 +245,15 @@ namespace ModdedUtilitiesNetworking.Framework.Servers { this.hasPlayerDisconnectedOnce.Add(disconnectee); } + /* + 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); + } + */ + } @@ -455,7 +479,6 @@ namespace ModdedUtilitiesNetworking.Framework.Servers OutgoingMessage message1 = new OutgoingMessage(messageType, f, data); foreach (long peerId in (IEnumerable)Game1.otherFarmers.Keys) { - ModCore.monitor.Log("RESEND MESSAGE TO CLIENT!!!", StardewModdingAPI.LogLevel.Alert); this.sendMessage(peerId, message1); } } diff --git a/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs b/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs index afbac22f..fff6fe97 100644 --- a/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs +++ b/GeneralMods/ModdedUtilitiesNetworking/Framework/Servers/CustomLidgrenServer.cs @@ -14,6 +14,38 @@ namespace ModdedUtilitiesNetworking.Framework.Servers { class CustomLidgrenServer : Server { + ///Save this for later + + protected override void playerDisconnected(long disconnectee) + { + this.gameServer.playerDisconnected(disconnectee); + + this.introductionsSent.Remove(this.peers[disconnectee]); + + this.peers.RemoveLeft(disconnectee); + } + + protected void sendMessage(NetConnection connection, OutgoingMessage message) + { + NetOutgoingMessage message1 = this.server.CreateMessage(); + using (NetBufferWriteStream bufferWriteStream = new NetBufferWriteStream((NetBuffer)message1)) + { + using (BinaryWriter writer = new BinaryWriter((Stream)bufferWriteStream)) { + if (message.MessageType < 20) + { + message.Write(writer); + } + else + { + OutgoingMessageBase.WriteFromMessage(message, writer); + } + } + } + int num = (int)this.server.SendMessage(message1, connection, NetDeliveryMethod.ReliableOrdered); + } + + + private HashSet introductionsSent = new HashSet(); private Bimap peers = new Bimap(); public const int defaultPort = 24642; @@ -35,7 +67,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers } public override string getUserName(long farmerId) - { + { if (!this.peers.ContainsLeft(farmerId)) return (string)null; return this.peers[farmerId].RemoteEndPoint.Address.ToString(); @@ -84,8 +116,6 @@ namespace ModdedUtilitiesNetworking.Framework.Servers NetIncomingMessage netIncomingMessage; while ((netIncomingMessage = this.server.ReadMessage()) != null) { - int i = (int)netIncomingMessage.MessageType; - //ModCore.monitor.Log("Message Type: " + i.ToString()); switch (netIncomingMessage.MessageType) { case NetIncomingMessageType.WarningMessage: @@ -114,11 +144,10 @@ namespace ModdedUtilitiesNetworking.Framework.Servers netIncomingMessage.SenderConnection.Deny(); break; case NetIncomingMessageType.Data: - //ModCore.monitor.Log("GOT DATA~~"); this.parseDataMessageFromClient(netIncomingMessage); break; default: - //ModCore.monitor.Log(netIncomingMessage.ToString()); + Game1.debugOutput = netIncomingMessage.ToString(); break; } this.server.Recycle(netIncomingMessage); @@ -150,28 +179,15 @@ namespace ModdedUtilitiesNetworking.Framework.Servers case NetConnectionStatus.Disconnecting: if (!this.peers.ContainsRight(message.SenderConnection)) break; - //Forces a de-sync with money. - - - this.playerDisconnected(this.peers[message.SenderConnection]); - + 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) { - //ModCore.monitor.Log("DATA MSG: "+dataMsg.ToString()); NetConnection peer = dataMsg.SenderConnection; using (IncomingMessage message = new IncomingMessage()) { @@ -182,7 +198,6 @@ namespace ModdedUtilitiesNetworking.Framework.Servers while ((long)dataMsg.LengthBits - dataMsg.Position >= 8L) { message.Read(reader); - int type = message.MessageType; if (this.peers.ContainsLeft(message.FarmerID) && this.peers[message.FarmerID] == peer) this.gameServer.processIncomingMessage(message); else if ((int)message.MessageType == 2) @@ -203,24 +218,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers this.sendMessage(this.peers[peerId], message); } - protected void sendMessage(NetConnection connection, OutgoingMessage message) - { - NetOutgoingMessage message1 = this.server.CreateMessage(); - using (NetBufferWriteStream bufferWriteStream = new NetBufferWriteStream((NetBuffer)message1)) - { - using (BinaryWriter writer = new BinaryWriter((Stream)bufferWriteStream)) { - if (message.MessageType < 20) - { - message.Write(writer); - } - else - { - OutgoingMessageBase.WriteFromMessage(message, writer); - } - } - } - int num = (int)this.server.SendMessage(message1, connection, NetDeliveryMethod.ReliableOrdered); - } + public override void setLobbyData(string key, string value) {