diff --git a/Server/Program.cs b/Server/Program.cs index be6cf5a..b6bab11 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -12,14 +12,15 @@ CancellationTokenSource cts = new CancellationTokenSource(); Task listenTask = server.Listen(cts.Token); Logger consoleLogger = new Logger("Console"); -server.ClientJoined += async (c, _) => { +server.ClientJoined += (c, _) => { c.Metadata["shineSync"] = new ConcurrentBag(); c.Metadata["loadedSave"] = false; c.Metadata["scenario"] = 0; + c.Metadata["2d"] = false; c.PacketTransformer += (sender, packet) => { - if (Settings.Instance.Scenario.MergeEnabled && packet is PlayerPacket playerPacket) { - playerPacket.ScenarioNum = (int) c.Metadata["scenario"]; - return playerPacket; + if (Settings.Instance.Scenario.MergeEnabled && packet is GamePacket gamePacket) { + gamePacket.ScenarioNum = (byte) c.Metadata["scenario"]; + return gamePacket; } return packet; @@ -58,8 +59,9 @@ float MarioSize(bool is2d) { server.PacketHandler = (c, p) => { { - if (p is PlayerPacket playerPacket) { - c.Metadata["scenario"] = playerPacket.ScenarioNum; + if (p is GamePacket gamePacket) { + c.Metadata["scenario"] = gamePacket.ScenarioNum; + c.Metadata["2d"] = gamePacket.Is2d; } } switch (p) { @@ -80,7 +82,7 @@ server.PacketHandler = (c, p) => { case PlayerPacket playerPacket when Settings.Instance.Flip.Enabled && Settings.Instance.Flip.Pov is FlipOptions.Both or FlipOptions.Others && Settings.Instance.Flip.Players.Contains(c.Id): { - playerPacket.Position += Vector3.UnitY * MarioSize(playerPacket.Is2d); + playerPacket.Position += Vector3.UnitY * MarioSize((bool) c.Metadata["2d"]); playerPacket.Rotation *= Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationX(MathF.PI)) * Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationY(MathF.PI)); server.Broadcast(playerPacket, c); return false; @@ -90,7 +92,7 @@ server.PacketHandler = (c, p) => { && !Settings.Instance.Flip.Players.Contains(c.Id): { server.BroadcastReplace(playerPacket, c, (from, to, sp) => { if (Settings.Instance.Flip.Players.Contains(to.Id)) { - sp.Position += Vector3.UnitY * MarioSize(playerPacket.Is2d); + sp.Position += Vector3.UnitY * MarioSize((bool) c.Metadata["2d"]); sp.Rotation *= Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationX(MathF.PI)) * Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationY(MathF.PI)); } diff --git a/Server/Server.cs b/Server/Server.cs index 7979cb1..8a21174 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -144,12 +144,11 @@ public class Server { memory = memoryPool.Rent(Constants.HeaderSize + header.PacketSize); memTemp.Memory.Span[..Constants.HeaderSize].CopyTo(memory.Memory.Span[..Constants.HeaderSize]); memTemp.Dispose(); - Logger.Info("smth"); if (!await Read(memory.Memory, header.PacketSize, Constants.HeaderSize)) throw new Exception("Not enough bytes for packet data sent to server"); } - Logger.Info($"Got your mom {header.Id} {header.Type} 0x{header.PacketSize:X} 0x{memory.Memory.Length:X} 0x{header.Size:X}"); + // if (header.Type is not PacketType.Player and not PacketType.Cap and not PacketType.Capture)Logger.Info($"Got your mom {header.Id} {header.Type} 0x{header.PacketSize:X} 0x{memory.Memory.Length:X} 0x{header.Size:X}"); // connection initialization if (first) { @@ -162,11 +161,11 @@ public class Server { client.Name = connect.ClientName; bool firstConn = false; switch (connect.ConnectionType) { - case ConnectionTypes.FirstConnection: { + case ConnectPacket.ConnectionTypes.FirstConnection: { firstConn = true; break; } - case ConnectionTypes.Reconnecting: { + case ConnectPacket.ConnectionTypes.Reconnecting: { client.Id = header.Id; if (FindExistingClient(header.Id) is { } newClient) { if (newClient.Connected) throw new Exception($"Tried to join as already connected user {header.Id}"); @@ -174,7 +173,7 @@ public class Server { client = newClient; } else { firstConn = true; - connect.ConnectionType = ConnectionTypes.FirstConnection; + connect.ConnectionType = ConnectPacket.ConnectionTypes.FirstConnection; } break; @@ -209,7 +208,7 @@ public class Server { }; MemoryMarshal.Write(tempBuffer.Memory.Span, ref connectHeader); ConnectPacket connectPacket = new ConnectPacket { - ConnectionType = ConnectionTypes.FirstConnection, // doesn't matter what it is + ConnectionType = ConnectPacket.ConnectionTypes.FirstConnection, // doesn't matter what it is ClientName = other.Name }; connectPacket.Serialize(tempBuffer.Memory.Span[Constants.HeaderSize..]); diff --git a/Shared/Packet/Packets/CapPacket.cs b/Shared/Packet/Packets/CapPacket.cs index c9c8d32..ea8b10b 100644 --- a/Shared/Packet/Packets/CapPacket.cs +++ b/Shared/Packet/Packets/CapPacket.cs @@ -9,19 +9,22 @@ public struct CapPacket : IPacket { public const int NameSize = 0x30; public Vector3 Position; public Quaternion Rotation; + public bool CapOut; public string CapAnim; - public short Size => 0x4C; + public short Size => 0x50; public void Serialize(Span data) { MemoryMarshal.Write(data, ref Position); - MemoryMarshal.Write(data[12..], ref Position); - Encoding.UTF8.GetBytes(CapAnim).CopyTo(data[28..]); + MemoryMarshal.Write(data[12..], ref Rotation); + MemoryMarshal.Write(data[28..], ref CapOut); + Encoding.UTF8.GetBytes(CapAnim).CopyTo(data[32..(32 + NameSize)]); } public void Deserialize(Span data) { Position = MemoryMarshal.Read(data); Rotation = MemoryMarshal.Read(data[12..]); - CapAnim = Encoding.UTF8.GetString(data[28..]).TrimEnd('\0'); + CapOut = MemoryMarshal.Read(data[28..]); + CapAnim = Encoding.UTF8.GetString(data[32..(32 + NameSize)]).TrimEnd('\0'); } } \ No newline at end of file diff --git a/Shared/Packet/Packets/CapturePacket.cs b/Shared/Packet/Packets/CapturePacket.cs index 3b2bbee..b035478 100644 --- a/Shared/Packet/Packets/CapturePacket.cs +++ b/Shared/Packet/Packets/CapturePacket.cs @@ -8,17 +8,13 @@ public struct CapturePacket : IPacket { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = Constants.CostumeNameSize)] public string ModelName; - public bool IsCaptured; - - public short Size => Constants.CostumeNameSize + 1; + public short Size => Constants.CostumeNameSize; public void Serialize(Span data) { Encoding.UTF8.GetBytes(ModelName).CopyTo(data[..Constants.CostumeNameSize]); - MemoryMarshal.Write(data[Constants.CostumeNameSize..], ref IsCaptured); } public void Deserialize(Span data) { ModelName = Encoding.UTF8.GetString(data[..Constants.CostumeNameSize]).TrimNullTerm(); - IsCaptured = MemoryMarshal.Read(data[Constants.CostumeNameSize..]); } } \ No newline at end of file diff --git a/Shared/Packet/Packets/ConnectPacket.cs b/Shared/Packet/Packets/ConnectPacket.cs index fe31e62..d89352a 100644 --- a/Shared/Packet/Packets/ConnectPacket.cs +++ b/Shared/Packet/Packets/ConnectPacket.cs @@ -21,4 +21,9 @@ public struct ConnectPacket : IPacket { ConnectionType = MemoryMarshal.Read(data); ClientName = Encoding.UTF8.GetString(data[4..(4 + Constants.CostumeNameSize)]).TrimNullTerm(); } + + public enum ConnectionTypes { + FirstConnection, + Reconnecting + } } \ No newline at end of file diff --git a/Shared/Packet/Packets/ConnectionTypes.cs b/Shared/Packet/Packets/ConnectionTypes.cs deleted file mode 100644 index 1666258..0000000 --- a/Shared/Packet/Packets/ConnectionTypes.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Shared.Packet.Packets; - -public enum ConnectionTypes { - FirstConnection, - Reconnecting -} \ No newline at end of file diff --git a/Shared/Packet/Packets/GamePacket.cs b/Shared/Packet/Packets/GamePacket.cs new file mode 100644 index 0000000..b2aac1f --- /dev/null +++ b/Shared/Packet/Packets/GamePacket.cs @@ -0,0 +1,25 @@ +using System.Runtime.InteropServices; +using System.Text; + +namespace Shared.Packet.Packets; + +[Packet(PacketType.Game)] +public struct GamePacket : IPacket { + private const int StageSize = 0x30; + public bool Is2d; + public byte ScenarioNum; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = StageSize)] + public string Stage = ""; + public short Size => 0x32; + public void Serialize(Span data) { + MemoryMarshal.Write(data[..0], ref Is2d); + MemoryMarshal.Write(data[1..1], ref ScenarioNum); + Encoding.UTF8.GetBytes(Stage).CopyTo(data[2..(2 + StageSize)]); + } + + public void Deserialize(Span data) { + Is2d = MemoryMarshal.Read(data); + ScenarioNum = MemoryMarshal.Read(data[1..]); + Stage = Encoding.UTF8.GetString(data[2..(2 + StageSize)]).TrimEnd('\0'); + } +} \ No newline at end of file diff --git a/Shared/Packet/Packets/PlayerPacket.cs b/Shared/Packet/Packets/PlayerPacket.cs index 6652f12..8e15aa8 100644 --- a/Shared/Packet/Packets/PlayerPacket.cs +++ b/Shared/Packet/Packets/PlayerPacket.cs @@ -6,81 +6,43 @@ namespace Shared.Packet.Packets; [Packet(PacketType.Player)] public struct PlayerPacket : IPacket { - public const int NameSize = 0x20; + public const int ActSize = 0x20; + public const int SubActSize = 0x10; - public Vector3 Position = default; - public Quaternion Rotation = default; + public Vector3 Position; + public Quaternion Rotation; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] public float[] AnimationBlendWeights = Array.Empty(); - public float AnimationRate = 0; - public bool Is2d = false; - public bool ThrowingCap = false; - public bool IsIt = false; - public bool IsCaptured = false; - public int ScenarioNum = 0; - - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x40)] - public string Stage = ""; - - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NameSize)] + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = ActSize)] public string Act = ""; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NameSize)] + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = SubActSize)] public string SubAct = ""; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NameSize)] - public string Hack = ""; + public PlayerPacket() { + Position = default; + Rotation = default; + } - public PlayerPacket() { } - - public short Size => 0xE0; + public short Size => 0x64; public void Serialize(Span data) { int offset = 0; - MemoryMarshal.Write(data, ref Position); - offset += Marshal.SizeOf(); - MemoryMarshal.Write(data[offset..], ref Rotation); - offset += Marshal.SizeOf(); + MemoryMarshal.Write(data[..(offset += Marshal.SizeOf())], ref Position); + MemoryMarshal.Write(data[offset..(offset += Marshal.SizeOf())], ref Rotation); AnimationBlendWeights.CopyTo(MemoryMarshal.Cast(data[offset..(offset += 4 * 6)])); - MemoryMarshal.Write(data[offset..], ref AnimationRate); - offset += 4; - MemoryMarshal.Write(data[offset++..], ref Is2d); - MemoryMarshal.Write(data[offset++..], ref ThrowingCap); - MemoryMarshal.Write(data[offset++..], ref IsIt); - MemoryMarshal.Write(data[offset++..], ref IsCaptured); - MemoryMarshal.Write(data[offset..], ref ScenarioNum); - offset += 4; - Encoding.UTF8.GetBytes(Stage).CopyTo(data[offset..(offset + 0x40)]); - offset += 0x40; - Encoding.UTF8.GetBytes(Act).CopyTo(data[offset..(offset + NameSize)]); - offset += NameSize; - Encoding.UTF8.GetBytes(SubAct).CopyTo(data[offset..(offset + NameSize)]); - offset += NameSize; - Encoding.UTF8.GetBytes(Hack).CopyTo(data[offset..]); + Encoding.UTF8.GetBytes(Act).CopyTo(data[offset..(offset += ActSize)]); + Encoding.UTF8.GetBytes(SubAct).CopyTo(data[offset..(offset + SubActSize)]); } public void Deserialize(Span data) { int offset = 0; - Position = MemoryMarshal.Read(data); - offset += Marshal.SizeOf(); - Rotation = MemoryMarshal.Read(data[offset..]); - offset += Marshal.SizeOf(); + Position = MemoryMarshal.Read(data[..(offset += Marshal.SizeOf())]); + Rotation = MemoryMarshal.Read(data[offset..(offset += Marshal.SizeOf())]); AnimationBlendWeights = MemoryMarshal.Cast(data[offset..(offset += 4 * 6)]).ToArray(); - AnimationRate = MemoryMarshal.Read(data[offset..(offset += 4)]); - Is2d = MemoryMarshal.Read(data[offset++..]); - ThrowingCap = MemoryMarshal.Read(data[offset++..]); - IsIt = MemoryMarshal.Read(data[offset++..]); - IsCaptured = MemoryMarshal.Read(data[offset++..]); - ScenarioNum = MemoryMarshal.Read(data[offset..]); - offset += 4; - Stage = Encoding.UTF8.GetString(data[offset..(offset + 0x40)]).TrimEnd('\0'); - offset += 0x40; - Act = Encoding.UTF8.GetString(data[offset..(offset + NameSize)]).TrimEnd('\0'); - offset += NameSize; - SubAct = Encoding.UTF8.GetString(data[offset..(offset + NameSize)]).TrimEnd('\0'); - offset += NameSize; - Hack = Encoding.UTF8.GetString(data[offset..(offset + NameSize)]).TrimEnd('\0'); + Act = Encoding.UTF8.GetString(data[offset..(offset += ActSize)]).TrimEnd('\0'); + SubAct = Encoding.UTF8.GetString(data[offset..(offset + SubActSize)]).TrimEnd('\0'); } } \ No newline at end of file diff --git a/Shared/Packet/Packets/TagPacket.cs b/Shared/Packet/Packets/TagPacket.cs index 0463992..813c92c 100644 --- a/Shared/Packet/Packets/TagPacket.cs +++ b/Shared/Packet/Packets/TagPacket.cs @@ -4,9 +4,12 @@ namespace Shared.Packet.Packets; [Packet(PacketType.Tag)] public struct TagPacket : IPacket { + public TagUpdate UpdateType; public bool IsIt; + public byte Seconds; + public ushort Minutes; - public short Size => 1; + public short Size => 4; public void Serialize(Span data) { MemoryMarshal.Write(data, ref IsIt); @@ -15,4 +18,10 @@ public struct TagPacket : IPacket { public void Deserialize(Span data) { IsIt = MemoryMarshal.Read(data); } + + [Flags] + public enum TagUpdate : byte { + Time = 1, + State = 2 + } } \ No newline at end of file diff --git a/Shared/Shared.csproj b/Shared/Shared.csproj index 6f7f07a..727c233 100644 --- a/Shared/Shared.csproj +++ b/Shared/Shared.csproj @@ -8,8 +8,8 @@ - - + +