From 1d86b05a6201f3461b2445f1f630b5718f720459 Mon Sep 17 00:00:00 2001 From: Twirlbug Date: Tue, 4 Jul 2023 19:52:32 -0500 Subject: [PATCH] Moved around some files and packets to start preping for modularizing the code --- .../{ => Communication}/NetworkClient.cs | 0 .../NetworkHandlerClient.cs | 49 ++++++------- .../NetworkHandlerServer.cs | 57 ++++++--------- .../{ => Communication}/NetworkServer.cs | 1 + SRMP/Networking/NetworkPlayer.Animation.cs | 69 +++++++++++++------ SRMP/Networking/NetworkPlayer.cs | 5 ++ SRMP/Packets/Actors/PacketPlayerAnimation.cs | 35 ++++++++++ SRMP/Packets/_Classes/IPacket.cs | 11 ++- SRMP/Packets/_Classes/Packet.cs | 36 ++++++++-- SRMP/Packets/_Classes/PacketType.cs | 4 +- SRMP/Properties/AssemblyInfo.cs | 4 +- SRMP/SRMP.csproj | 9 +-- 12 files changed, 179 insertions(+), 101 deletions(-) rename SRMP/Networking/{ => Communication}/NetworkClient.cs (100%) rename SRMP/Networking/{ => Communication}/NetworkHandlerClient.cs (98%) rename SRMP/Networking/{ => Communication}/NetworkHandlerServer.cs (98%) rename SRMP/Networking/{ => Communication}/NetworkServer.cs (99%) create mode 100644 SRMP/Packets/Actors/PacketPlayerAnimation.cs diff --git a/SRMP/Networking/NetworkClient.cs b/SRMP/Networking/Communication/NetworkClient.cs similarity index 100% rename from SRMP/Networking/NetworkClient.cs rename to SRMP/Networking/Communication/NetworkClient.cs diff --git a/SRMP/Networking/NetworkHandlerClient.cs b/SRMP/Networking/Communication/NetworkHandlerClient.cs similarity index 98% rename from SRMP/Networking/NetworkHandlerClient.cs rename to SRMP/Networking/Communication/NetworkHandlerClient.cs index dd0c9a2..2514e65 100644 --- a/SRMP/Networking/NetworkHandlerClient.cs +++ b/SRMP/Networking/Communication/NetworkHandlerClient.cs @@ -23,22 +23,24 @@ namespace SRMultiplayer.Networking Globals.PacketSize[type] += im.LengthBytes; switch (type) { + //Player amimations + case PacketType.PlayerAnimation: OnPlayerAnimation(new PacketPlayerAnimation(im)); break; + //Players case PacketType.PlayerJoined: OnPlayerJoined(new PacketPlayerJoined(im)); break; case PacketType.PlayerLeft: OnPlayerLeft(new PacketPlayerLeft(im)); break; case PacketType.PlayerLoaded: OnPlayerLoaded(new PacketPlayerLoaded(im)); break; - case PacketType.PlayerPosition: OnPlayerPosition(new PacketPlayerPosition(im)); break; - case PacketType.PlayerAnimationLayer: OnPlayerAnimationLayer(im); break; - case PacketType.PlayerAnimationParameters: OnPlayerAnimationParameters(im); break; - case PacketType.PlayerAnimationSpeed: OnPlayerAnimationSpeed(im); break; + case PacketType.PlayerPosition: OnPlayerPosition(new PacketPlayerPosition(im)); break; case PacketType.PlayerFX: OnPlayerFX(new PacketPlayerFX(im)); break; case PacketType.PlayerCurrency: OnPlayerCurrency(new PacketPlayerCurrency(im)); break; case PacketType.PlayerCurrencyDisplay: OnPlayerCurrencyDisplay(new PacketPlayerCurrencyDisplay(im)); break; case PacketType.PlayerUpgrade: OnPlayerUpgrade(new PacketPlayerUpgrade(im)); break; case PacketType.PlayerUpgradeUnlock: OnPlayerUpgradeUnlock(new PacketPlayerUpgradeUnlock(im)); break; case PacketType.PlayerChat: OnPlayerChat(new PacketPlayerChat(im)); break; + // Region case PacketType.RegionOwner: OnRegionOwner(new PacketRegionOwner(im)); break; + //Actors case PacketType.Actors: OnActors(new PacketActors(im)); break; case PacketType.ActorSpawn: OnActorSpawn(new PacketActorSpawn(im)); break; @@ -149,10 +151,10 @@ namespace SRMultiplayer.Networking // Race case PacketType.RaceActivate: OnRaceActivate(new PacketRaceActivate(im)); break; case PacketType.RaceEnd: OnRaceEnd(new PacketRaceEnd(im)); break; - case PacketType.RaceTime: OnRaceTime(new PacketRaceTime(im)); break; + case PacketType.RaceTime: OnRaceTime(new PacketRaceTime(im)); break; case PacketType.RaceTrigger: OnRaceTrigger(new PacketRaceTrigger(im)); break; default: - SRMP.Log($"Got unhandled packet: {type}"); + SRMP.Log($"Got unhandled packet: {type} " + Enum.GetName(typeof(PacketType), type)); break; } } @@ -1926,30 +1928,21 @@ namespace SRMultiplayer.Networking } } - private static void OnPlayerAnimationSpeed(NetIncomingMessage im) + private static void OnPlayerAnimation(PacketPlayerAnimation packet) { - byte id = im.ReadByte(); - if (Globals.Players.TryGetValue(id, out NetworkPlayer player) && player.HasLoaded) - { - player.ReadAnimatorSpeed(im); - } - } + //handle character animation triggers + PacketPlayerAnimation.AnimationType type = (PacketPlayerAnimation.AnimationType)packet.Type; - private static void OnPlayerAnimationParameters(NetIncomingMessage im) - { - byte id = im.ReadByte(); - if (Globals.Players.TryGetValue(id, out NetworkPlayer player) && player.HasLoaded) - { - player.ReadParameters(im); - } - } - private static void OnPlayerAnimationLayer(NetIncomingMessage im) - { - byte id = im.ReadByte(); - if (Globals.Players.TryGetValue(id, out NetworkPlayer player) && player.HasLoaded) + + if (Globals.Players.TryGetValue(packet.ID, out NetworkPlayer player) && player.HasLoaded) { - player.ReadAnimatorLayer(im); + if(type == PacketPlayerAnimation.AnimationType.Speed) + player.ReadAnimatorSpeed(packet.internalData); + else if (type == PacketPlayerAnimation.AnimationType.Parameters) + player.ReadParameters(packet.internalData); + else + player.ReadAnimatorLayer(packet.internalData); } } @@ -1968,7 +1961,9 @@ namespace SRMultiplayer.Networking SRSingleton.Instance.player.transform.eulerAngles = new Vector3(0, packet.Rotation, 0); SRSingleton.Instance.PlayerState.model.SetCurrRegionSet((RegionRegistry.RegionSetId)packet.RegionSet); - if (!Globals.IsServer) + + //only reload inventory if this is a load up packet and NOT a tp packet + if (!Globals.IsServer && packet.OnLoad) { try { diff --git a/SRMP/Networking/NetworkHandlerServer.cs b/SRMP/Networking/Communication/NetworkHandlerServer.cs similarity index 98% rename from SRMP/Networking/NetworkHandlerServer.cs rename to SRMP/Networking/Communication/NetworkHandlerServer.cs index 3fb97ae..fcaf43e 100644 --- a/SRMP/Networking/NetworkHandlerServer.cs +++ b/SRMP/Networking/Communication/NetworkHandlerServer.cs @@ -2,6 +2,7 @@ using Lidgren.Network; using MonomiPark.SlimeRancher.DataModel; using MonomiPark.SlimeRancher.Regions; +using Newtonsoft.Json.Linq; using SRMultiplayer.Packets; using System; using System.Collections.Generic; @@ -21,12 +22,11 @@ namespace SRMultiplayer.Networking Globals.PacketSize[type] += im.LengthBytes; switch (type) { + //Player animation + case PacketType.PlayerAnimation: OnPlayerAnimation(new PacketPlayerAnimation(im), player); break; //Player case PacketType.PlayerLoaded: OnPlayerLoaded(new PacketPlayerLoaded(im), player); break; case PacketType.PlayerPosition: OnPlayerPosition(new PacketPlayerPosition(im), player); break; - case PacketType.PlayerAnimationLayer: OnPlayerAnimationLayer(im, player); break; - case PacketType.PlayerAnimationParameters: OnPlayerAnimationParameters(im, player); break; - case PacketType.PlayerAnimationSpeed: OnPlayerAnimationSpeed(im, player); break; case PacketType.PlayerCurrency: OnPlayerCurrency(new PacketPlayerCurrency(im), player); break; case PacketType.PlayerCurrencyDisplay: OnPlayerCurrencyDisplay(new PacketPlayerCurrencyDisplay(im), player); break; case PacketType.PlayerUpgrade: OnPlayerUpgrade(new PacketPlayerUpgrade(im), player); break; @@ -132,7 +132,7 @@ namespace SRMultiplayer.Networking case PacketType.RaceTime: OnRaceTime(new PacketRaceTime(im), player); break; case PacketType.RaceTrigger: OnRaceTrigger(new PacketRaceTrigger(im), player); break; default: - SRMP.Log($"Got unhandled packet from {player}: {type}"); + SRMP.Log($"Got unhandled packet from {player}: {type}" + Enum.GetName(typeof(PacketType), type)); break; } } @@ -1639,42 +1639,25 @@ namespace SRMultiplayer.Networking packet.SendToAllExcept(player); } - private static void OnPlayerAnimationSpeed(NetIncomingMessage im, NetworkPlayer player) + private static void OnPlayerAnimation(PacketPlayerAnimation packet, NetworkPlayer player) { if (player.HasLoaded) { - byte id = im.ReadByte(); - player.ReadAnimatorSpeed(im); - - NetOutgoingMessage om = NetworkServer.Instance.CreateMessage(); - om.Write(im); - NetworkServer.Instance.SendToAll(om, player); - } - } - - private static void OnPlayerAnimationParameters(NetIncomingMessage im, NetworkPlayer player) - { - if (player.HasLoaded) - { - byte id = im.ReadByte(); - player.ReadParameters(im); - - NetOutgoingMessage om = NetworkServer.Instance.CreateMessage(); - om.Write(im); - NetworkServer.Instance.SendToAll(om, player); - } - } - - private static void OnPlayerAnimationLayer(NetIncomingMessage im, NetworkPlayer player) - { - if (player.HasLoaded) - { - byte id = im.ReadByte(); - player.ReadAnimatorLayer(im); - - NetOutgoingMessage om = NetworkServer.Instance.CreateMessage(); - om.Write(im); - NetworkServer.Instance.SendToAll(om, player); + switch (packet.Type) + { + case (byte)PacketPlayerAnimation.AnimationType.Speed: + player.ReadAnimatorSpeed(packet.internalData); + break; + case (byte)PacketPlayerAnimation.AnimationType.Layer: + player.ReadAnimatorLayer(packet.internalData); + break; + case (byte)PacketPlayerAnimation.AnimationType.Parameters: + player.ReadParameters(packet.internalData); + break; + } + + //make the incoming message an out going message + packet.SendToAllExcept(player, NetDeliveryMethod.Unreliable); } } diff --git a/SRMP/Networking/NetworkServer.cs b/SRMP/Networking/Communication/NetworkServer.cs similarity index 99% rename from SRMP/Networking/NetworkServer.cs rename to SRMP/Networking/Communication/NetworkServer.cs index b0ffa74..fac69fa 100644 --- a/SRMP/Networking/NetworkServer.cs +++ b/SRMP/Networking/Communication/NetworkServer.cs @@ -340,6 +340,7 @@ namespace SRMultiplayer.Networking { Globals.HandlePacket = true; PacketType type = (PacketType)im.ReadUInt16(); + var player = Globals.Players.Values.FirstOrDefault(p => p.Connection != null && p.Connection.RemoteUniqueIdentifier == im.SenderConnection.RemoteUniqueIdentifier); if (player != null) { diff --git a/SRMP/Networking/NetworkPlayer.Animation.cs b/SRMP/Networking/NetworkPlayer.Animation.cs index 469f01a..921fb20 100644 --- a/SRMP/Networking/NetworkPlayer.Animation.cs +++ b/SRMP/Networking/NetworkPlayer.Animation.cs @@ -62,17 +62,27 @@ namespace SRMultiplayer.Networking continue; } - NetOutgoingMessage writer = CreateMessage(); - writer.Write((ushort)PacketType.PlayerAnimationLayer); - writer.Write(Globals.LocalID); - WriteAnimatorLayer(writer, stateHash, normalizedTime, i, layerWeight[i]); - Send(writer); + //NetOutgoingMessage writer = CreateMessage(); + + //add the object to the writer but dont send it yet + var packet = new PacketPlayerAnimation() + { + Type = (byte)PacketPlayerAnimation.AnimationType.Layer, + ID = Globals.LocalID, + internalData = new NetBuffer() + }; + + //add extra parameters + WriteAnimatorLayer(packet.internalData, stateHash, normalizedTime, i, layerWeight[i]); + + //send the changes + //packet.Send(); } CheckSpeed(); } - void WriteAnimatorLayer(NetOutgoingMessage writer, int stateHash, float normalizedTime, int layerNumber, float layerWeight) + void WriteAnimatorLayer(NetBuffer writer, int stateHash, float normalizedTime, int layerNumber, float layerWeight) { writer.Write(stateHash); writer.Write(normalizedTime); @@ -81,7 +91,7 @@ namespace SRMultiplayer.Networking WriteParameters(writer); } - public void ReadAnimatorLayer(NetIncomingMessage im) + public void ReadAnimatorLayer(NetBuffer im) { if (m_Animator == null) return; @@ -106,20 +116,30 @@ namespace SRMultiplayer.Networking if (Mathf.Abs(previousSpeed - newSpeed) > 0.001f) { previousSpeed = newSpeed; - NetOutgoingMessage writer = CreateMessage(); - writer.Write((ushort)PacketType.PlayerAnimationSpeed); - writer.Write(Globals.LocalID); - WriteAnimatorSpeed(writer, newSpeed); - Send(writer); + //NetOutgoingMessage writer = CreateMessage(); + + //add the object to the writer but dont send it yet + var packet = new PacketPlayerAnimation() + { + Type = (byte)PacketPlayerAnimation.AnimationType.Speed, + ID = Globals.LocalID, + internalData = new NetBuffer() + }; + + //add extra parameters + WriteAnimatorSpeed(packet.internalData, newSpeed); + + //send the speed change + //packet.Send(); } } - void WriteAnimatorSpeed(NetOutgoingMessage writer, float newSpeed) + void WriteAnimatorSpeed(NetBuffer writer, float newSpeed) { writer.Write(newSpeed); } - public void ReadAnimatorSpeed(NetIncomingMessage im) + public void ReadAnimatorSpeed(NetBuffer im) { if (m_Animator == null) return; @@ -179,13 +199,18 @@ namespace SRMultiplayer.Networking { nextSendTime = now + syncInterval; - NetOutgoingMessage writer = CreateMessage(); - writer.Write((ushort)PacketType.PlayerAnimationParameters); - writer.Write(Globals.LocalID); - if (WriteParameters(writer)) + //add the object to the writer but dont send it yet + var packet = new PacketPlayerAnimation() { - Send(writer); - } + Type = (byte)PacketPlayerAnimation.AnimationType.Parameters, + ID = Globals.LocalID, + internalData = new NetBuffer() + }; + + //add extra parameters + WriteParameters(packet.internalData); + + //packet.Send(); } } @@ -226,7 +251,7 @@ namespace SRMultiplayer.Networking return dirtyBits; } - bool WriteParameters(NetOutgoingMessage writer, bool forceAll = false) + bool WriteParameters(NetBuffer writer, bool forceAll = false) { ulong dirtyBits = forceAll ? (~0ul) : NextDirtyBits(); writer.Write(dirtyBits); @@ -255,7 +280,7 @@ namespace SRMultiplayer.Networking return dirtyBits != 0; } - public void ReadParameters(NetIncomingMessage reader) + public void ReadParameters(NetBuffer reader) { if (m_Animator == null) return; diff --git a/SRMP/Networking/NetworkPlayer.cs b/SRMP/Networking/NetworkPlayer.cs index aa4ff56..fb71921 100644 --- a/SRMP/Networking/NetworkPlayer.cs +++ b/SRMP/Networking/NetworkPlayer.cs @@ -150,6 +150,11 @@ namespace SRMultiplayer.Networking } } + public float GetWeaponLocation() + { + return m_ActualWeaponY; + } + private void OnAnimatorIK() { if(m_Animator != null && m_LeftHandTarget != null) diff --git a/SRMP/Packets/Actors/PacketPlayerAnimation.cs b/SRMP/Packets/Actors/PacketPlayerAnimation.cs new file mode 100644 index 0000000..f48894e --- /dev/null +++ b/SRMP/Packets/Actors/PacketPlayerAnimation.cs @@ -0,0 +1,35 @@ +using Lidgren.Network; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SRMultiplayer.Packets +{ + [Packet(PacketType.PlayerAnimation)] + public class PacketPlayerAnimation : Packet + { + public enum AnimationType : int + { + Layer, + Speed, + Parameters + + } + + public byte ID; + public byte Type; + public NetBuffer internalData; + /// + /// mark construction inheritance incase we need it + /// + public PacketPlayerAnimation():base() { } + /// + /// mark construction inheritance so the deserialization automatically happens for is + /// since the base decalres this + /// + public PacketPlayerAnimation(NetIncomingMessage im):base(im) { } + } + +} diff --git a/SRMP/Packets/_Classes/IPacket.cs b/SRMP/Packets/_Classes/IPacket.cs index 6b1c3ed..3236699 100644 --- a/SRMP/Packets/_Classes/IPacket.cs +++ b/SRMP/Packets/_Classes/IPacket.cs @@ -1,4 +1,5 @@ using Lidgren.Network; +using SRMultiplayer.Networking; using System; using System.Collections.Generic; using System.Linq; @@ -9,7 +10,15 @@ namespace SRMultiplayer.Packets public interface IPacket { PacketType GetPacketType(); + /// + /// Searilizes the given packet item + /// + /// Outgoing Message that the packet should be added to void Serialize(NetOutgoingMessage om); - void Deserialize(NetIncomingMessage im); + /// + /// Deserializes the given packet item + /// + /// Incoming Message that the packet should be deserialized from + void Deserialize(NetIncomingMessage im); } } diff --git a/SRMP/Packets/_Classes/Packet.cs b/SRMP/Packets/_Classes/Packet.cs index 8b10008..9f77617 100644 --- a/SRMP/Packets/_Classes/Packet.cs +++ b/SRMP/Packets/_Classes/Packet.cs @@ -9,16 +9,42 @@ namespace SRMultiplayer.Packets { public abstract class Packet : IPacket { + /// + /// Parameterless constructor to be used through inheritance + /// + public Packet() { } + + /// + /// Incoming message based construtor that deserielizes the item for the message + /// + public Packet(NetIncomingMessage im) { Deserialize(im); } + + + /// + /// Searilizes the given packet item + /// + /// Outgoing Message that the packet should be added to + public virtual void Serialize(NetOutgoingMessage om) + { + om.Write((ushort)GetPacketType()); + //writes the object type using the gettype name + //this allows the object to assume its object packet type + //om.Write(this.GetType().AssemblyQualifiedName); + // + + om.WriteAllFields(this, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); + } + + /// + /// Deserializes the given packet item + /// + /// Incoming Message that the packet should be deserialized from public virtual void Deserialize(NetIncomingMessage im) { im.ReadAllFields(this, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); } - public virtual void Serialize(NetOutgoingMessage om) - { - om.Write((ushort)GetPacketType()); - om.WriteAllFields(this, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); - } + public PacketType GetPacketType() { diff --git a/SRMP/Packets/_Classes/PacketType.cs b/SRMP/Packets/_Classes/PacketType.cs index c4e4f07..1f22782 100644 --- a/SRMP/Packets/_Classes/PacketType.cs +++ b/SRMP/Packets/_Classes/PacketType.cs @@ -7,9 +7,7 @@ PlayerLeft, PlayerPosition, PlayerLoaded, - PlayerAnimationLayer, - PlayerAnimationSpeed, - PlayerAnimationParameters, + PlayerAnimation, PlayerFX, PlayAudio, ActorSpawn, diff --git a/SRMP/Properties/AssemblyInfo.cs b/SRMP/Properties/AssemblyInfo.cs index b1bee8b..00d781b 100644 --- a/SRMP/Properties/AssemblyInfo.cs +++ b/SRMP/Properties/AssemblyInfo.cs @@ -15,7 +15,7 @@ using System.Resources; [assembly: AssemblyCulture("")] // Version informationr( -[assembly: AssemblyVersion("0.0.0.1500")] -[assembly: AssemblyFileVersion("0.0.0.1500")] +[assembly: AssemblyVersion("0.0.0.1510")] +[assembly: AssemblyFileVersion("0.0.0.1510")] [assembly: NeutralResourcesLanguageAttribute( "en-US" )] diff --git a/SRMP/SRMP.csproj b/SRMP/SRMP.csproj index 3e340f8..16520f7 100644 --- a/SRMP/SRMP.csproj +++ b/SRMP/SRMP.csproj @@ -242,10 +242,13 @@ + + + + - @@ -254,8 +257,6 @@ - - @@ -269,11 +270,11 @@ - +