Moved around some files and packets to start preping for modularizing the code

This commit is contained in:
Twirlbug 2023-07-04 19:52:32 -05:00
parent 528395d102
commit 1d86b05a62
12 changed files with 179 additions and 101 deletions

View file

@ -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<SceneContext>.Instance.player.transform.eulerAngles = new Vector3(0, packet.Rotation, 0);
SRSingleton<SceneContext>.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
{

View file

@ -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);
}
}

View file

@ -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)
{

View file

@ -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;

View file

@ -150,6 +150,11 @@ namespace SRMultiplayer.Networking
}
}
public float GetWeaponLocation()
{
return m_ActualWeaponY;
}
private void OnAnimatorIK()
{
if(m_Animator != null && m_LeftHandTarget != null)

View file

@ -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;
/// <summary>
/// mark construction inheritance incase we need it
/// </summary>
public PacketPlayerAnimation():base() { }
/// <summary>
/// mark construction inheritance so the deserialization automatically happens for is
/// since the base decalres this
/// </summary>
public PacketPlayerAnimation(NetIncomingMessage im):base(im) { }
}
}

View file

@ -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();
/// <summary>
/// Searilizes the given packet item
/// </summary>
/// <param name="om">Outgoing Message that the packet should be added to</param>
void Serialize(NetOutgoingMessage om);
void Deserialize(NetIncomingMessage im);
/// <summary>
/// Deserializes the given packet item
/// </summary>
/// <param name="om">Incoming Message that the packet should be deserialized from</param>
void Deserialize(NetIncomingMessage im);
}
}

View file

@ -9,16 +9,42 @@ namespace SRMultiplayer.Packets
{
public abstract class Packet : IPacket
{
/// <summary>
/// Parameterless constructor to be used through inheritance
/// </summary>
public Packet() { }
/// <summary>
/// Incoming message based construtor that deserielizes the item for the message
/// </summary>
public Packet(NetIncomingMessage im) { Deserialize(im); }
/// <summary>
/// Searilizes the given packet item
/// </summary>
/// <param name="om">Outgoing Message that the packet should be added to</param>
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);
}
/// <summary>
/// Deserializes the given packet item
/// </summary>
/// <param name="om">Incoming Message that the packet should be deserialized from</param>
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()
{

View file

@ -7,9 +7,7 @@
PlayerLeft,
PlayerPosition,
PlayerLoaded,
PlayerAnimationLayer,
PlayerAnimationSpeed,
PlayerAnimationParameters,
PlayerAnimation,
PlayerFX,
PlayAudio,
ActorSpawn,

View file

@ -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" )]

View file

@ -242,10 +242,13 @@
<Compile Include="Lidgren.Network\Platform\PlatformWinRT.cs" />
<Compile Include="MainSRML.cs" />
<Compile Include="MainStandalone.cs" />
<Compile Include="Networking\Communication\NetworkClient.cs" />
<Compile Include="Networking\Communication\NetworkHandlerClient.cs" />
<Compile Include="Networking\Communication\NetworkHandlerServer.cs" />
<Compile Include="Networking\Communication\NetworkServer.cs" />
<Compile Include="Networking\NetworkAccessDoor.cs" />
<Compile Include="Networking\NetworkActor.cs" />
<Compile Include="Networking\NetworkAmmo.cs" />
<Compile Include="Networking\NetworkClient.cs" />
<Compile Include="Networking\NetworkClientUI.cs" />
<Compile Include="Networking\NetworkDirectedActorSpawner.cs" />
<Compile Include="Networking\NetworkDrone.cs" />
@ -254,8 +257,6 @@
<Compile Include="Networking\NetworkFireColumn.cs" />
<Compile Include="Networking\NetworkGadgetSite.cs" />
<Compile Include="Networking\NetworkGordo.cs" />
<Compile Include="Networking\NetworkHandlerClient.cs" />
<Compile Include="Networking\NetworkHandlerServer.cs" />
<Compile Include="Networking\NetworkHostUI.cs" />
<Compile Include="Networking\NetworkKookadobaPatchNode.cs" />
<Compile Include="Networking\NetworkLandplot.cs" />
@ -269,11 +270,11 @@
<Compile Include="Networking\NetworkPuzzleSlot.cs" />
<Compile Include="Networking\NetworkRaceTrigger.cs" />
<Compile Include="Networking\NetworkRegion.cs" />
<Compile Include="Networking\NetworkServer.cs" />
<Compile Include="Networking\NetworkSpawnResource.cs" />
<Compile Include="Networking\NetworkTreasurePod.cs" />
<Compile Include="Networking\NetworkWorldStateMasterSwitch.cs" />
<Compile Include="Packets\AccessDoors\PacketAccessDoors.cs" />
<Compile Include="Packets\Actors\PacketPlayerAnimation.cs" />
<Compile Include="Packets\Exchanges\PacketExchangeClear.cs" />
<Compile Include="Packets\Exchanges\PacketExchangeOffer.cs" />
<Compile Include="Packets\Exchanges\PacketExchangeOffers.cs" />