SmoOnlineServer/Shared/Packet/Packets/PlayerPacket.cs

48 lines
1.9 KiB
C#
Raw Normal View History

2022-02-04 09:45:38 +00:00
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
namespace Shared.Packet.Packets;
[Packet(PacketType.Player)]
public struct PlayerPacket : IPacket {
2022-03-13 11:14:01 +00:00
public const int ActSize = 0x20;
public const int SubActSize = 0x10;
2022-02-04 09:45:38 +00:00
2022-03-13 11:14:01 +00:00
public Vector3 Position;
public Quaternion Rotation;
2022-02-10 01:44:50 +00:00
2022-02-04 09:45:38 +00:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public float[] AnimationBlendWeights = Array.Empty<float>();
2022-02-10 01:44:50 +00:00
2022-03-13 11:14:01 +00:00
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = ActSize)]
public string Act = "";
2022-02-10 01:44:50 +00:00
2022-03-13 11:14:01 +00:00
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = SubActSize)]
public string SubAct = "";
2022-02-10 01:44:50 +00:00
2022-03-13 11:14:01 +00:00
public PlayerPacket() {
Position = default;
Rotation = default;
}
2022-03-13 11:14:01 +00:00
public short Size => 0x64;
2022-02-04 09:45:38 +00:00
public void Serialize(Span<byte> data) {
int offset = 0;
2022-03-13 11:14:01 +00:00
MemoryMarshal.Write(data[..(offset += Marshal.SizeOf<Vector3>())], ref Position);
MemoryMarshal.Write(data[offset..(offset += Marshal.SizeOf<Quaternion>())], ref Rotation);
2022-02-04 09:45:38 +00:00
AnimationBlendWeights.CopyTo(MemoryMarshal.Cast<byte, float>(data[offset..(offset += 4 * 6)]));
2022-03-13 11:14:01 +00:00
Encoding.UTF8.GetBytes(Act).CopyTo(data[offset..(offset += ActSize)]);
Encoding.UTF8.GetBytes(SubAct).CopyTo(data[offset..(offset + SubActSize)]);
2022-02-04 09:45:38 +00:00
}
public void Deserialize(Span<byte> data) {
int offset = 0;
2022-03-13 11:14:01 +00:00
Position = MemoryMarshal.Read<Vector3>(data[..(offset += Marshal.SizeOf<Vector3>())]);
Rotation = MemoryMarshal.Read<Quaternion>(data[offset..(offset += Marshal.SizeOf<Quaternion>())]);
2022-02-04 09:45:38 +00:00
AnimationBlendWeights = MemoryMarshal.Cast<byte, float>(data[offset..(offset += 4 * 6)]).ToArray();
2022-03-13 11:14:01 +00:00
Act = Encoding.UTF8.GetString(data[offset..(offset += ActSize)]).TrimEnd('\0');
SubAct = Encoding.UTF8.GetString(data[offset..(offset + SubActSize)]).TrimEnd('\0');
2022-02-04 09:45:38 +00:00
}
2021-11-29 04:04:34 +00:00
}