diff --git a/Server/Client.cs b/Server/Client.cs index 79211a9..397fd42 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -38,7 +38,6 @@ public class Client : IDisposable { public async Task Send(T packet, Client? sender = null) where T : struct, IPacket { IMemoryOwner memory = MemoryPool.Shared.RentZero(Constants.HeaderSize + packet.Size); - packet = (T) (PacketTransformer?.Invoke(sender, packet) ?? packet); PacketHeader header = new PacketHeader { Id = sender?.Id ?? Id, Type = Constants.PacketMap[typeof(T)].Type, @@ -49,13 +48,20 @@ public class Client : IDisposable { memory.Dispose(); } - public async Task Send(ReadOnlyMemory data, Client? sender) { + public async Task Send(Memory data, Client? sender) { if (!Connected) { // Server.Logger.Info($"Didn't send {MemoryMarshal.Read(data.Span[16..])} to {Id} because they weren't connected yet"); return; } int packetSize = MemoryMarshal.Read(data.Span[18..]); + if (PacketTransformer != null) { + PacketType type = MemoryMarshal.Read(data.Span[16..]); + IPacket packet = (IPacket) Activator.CreateInstance(Constants.PacketIdMap[type])!; + packet.Deserialize(data.Span); + packet = PacketTransformer?.Invoke(sender, packet) ?? packet; + packet.Serialize(data.Span); + } await Socket!.SendAsync(data[..(Constants.HeaderSize + packetSize)], SocketFlags.None); } diff --git a/Server/Program.cs b/Server/Program.cs index 5e6f057..a3eb314 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -20,7 +20,7 @@ server.ClientJoined += (c, _) => { c.Metadata["speedrun"] = false; foreach (Client client in server.Clients.Where(client => client.Metadata.ContainsKey("lastGamePacket")).ToArray()) { try { - Task.WaitAll(c.Send((GamePacket) client.Metadata["lastGamePacket"]!, client)); + c.Send((GamePacket) client.Metadata["lastGamePacket"]!, client).Wait(); } catch { // lol who gives a fuck @@ -84,12 +84,14 @@ server.PacketHandler = (c, p) => { c.Metadata["speedrun"] = true; ((ConcurrentBag) (c.Metadata["shineSync"] ??= new ConcurrentBag())).Clear(); shineBag.Clear(); + c.Logger.Info("Entered Cap on scenario 0, enabling speedrun flag"); break; case "WaterfallWorldHomeStage": bool wasSpeedrun = (bool) c.Metadata["speedrun"]!; c.Metadata["speedrun"] = false; if (wasSpeedrun) Task.Run(async () => { + c.Logger.Info("Entered Cascade with speedrun mode on"); await Task.Delay(15000); await ClientSyncShineBag(c); }); @@ -215,7 +217,7 @@ CommandHandler.RegisterCommand("scenario", args => { return optionUsage; } case "merge" when args.Length == 1: { - return $"Scenario merging is {(Settings.Instance.Scenario.MergeEnabled)}"; + return $"Scenario merging is {Settings.Instance.Scenario.MergeEnabled}"; } default: return optionUsage; diff --git a/Server/Server.cs b/Server/Server.cs index cd70e8b..7857409 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -180,7 +180,10 @@ public class Server { case ConnectPacket.ConnectionTypes.FirstConnection: { firstConn = true; if (FindExistingClient(header.Id) is { } newClient) { - if (newClient.Connected) throw new Exception($"Tried to join as already connected user {header.Id}"); + if (newClient.Connected) { + newClient.Logger.Info($"Disconnecting already connected client {newClient.Socket?.RemoteEndPoint} for {client.Socket?.RemoteEndPoint}"); + newClient.Dispose(); + } newClient.Socket = client.Socket; client = newClient; } @@ -190,7 +193,10 @@ public class Server { 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}"); + if (newClient.Connected) { + newClient.Logger.Info($"Disconnecting already connected client {newClient.Socket?.RemoteEndPoint} for {client.Socket?.RemoteEndPoint}"); + newClient.Dispose(); + } newClient.Socket = client.Socket; client = newClient; } else { diff --git a/Shared/Packet/PacketHeader.cs b/Shared/Packet/PacketHeader.cs index 706eb84..a1f12c1 100644 --- a/Shared/Packet/PacketHeader.cs +++ b/Shared/Packet/PacketHeader.cs @@ -18,7 +18,7 @@ public struct PacketHeader : IPacket { MemoryMarshal.Write(data[18..], ref PacketSize); } - public void Deserialize(Span data) { + public void Deserialize(ReadOnlySpan data) { Id = MemoryMarshal.Read(data); Type = MemoryMarshal.Read(data[16..]); PacketSize = MemoryMarshal.Read(data[16..]); diff --git a/Shared/Packet/Packets/CapPacket.cs b/Shared/Packet/Packets/CapPacket.cs index ea8b10b..b974c52 100644 --- a/Shared/Packet/Packets/CapPacket.cs +++ b/Shared/Packet/Packets/CapPacket.cs @@ -21,7 +21,7 @@ public struct CapPacket : IPacket { Encoding.UTF8.GetBytes(CapAnim).CopyTo(data[32..(32 + NameSize)]); } - public void Deserialize(Span data) { + public void Deserialize(ReadOnlySpan data) { Position = MemoryMarshal.Read(data); Rotation = MemoryMarshal.Read(data[12..]); CapOut = MemoryMarshal.Read(data[28..]); diff --git a/Shared/Packet/Packets/CapturePacket.cs b/Shared/Packet/Packets/CapturePacket.cs index 93752f1..265474a 100644 --- a/Shared/Packet/Packets/CapturePacket.cs +++ b/Shared/Packet/Packets/CapturePacket.cs @@ -12,7 +12,7 @@ public struct CapturePacket : IPacket { Encoding.UTF8.GetBytes(ModelName).CopyTo(data[..Constants.CostumeNameSize]); } - public void Deserialize(Span data) { + public void Deserialize(ReadOnlySpan data) { ModelName = Encoding.UTF8.GetString(data[..Constants.CostumeNameSize]).TrimNullTerm(); } diff --git a/Shared/Packet/Packets/ChangeStagePacket.cs b/Shared/Packet/Packets/ChangeStagePacket.cs index 1f302d2..8e2838f 100644 --- a/Shared/Packet/Packets/ChangeStagePacket.cs +++ b/Shared/Packet/Packets/ChangeStagePacket.cs @@ -19,7 +19,7 @@ public struct ChangeStagePacket : IPacket { MemoryMarshal.Write(data[(IdSize + StageSize)..(IdSize + StageSize + 1)], ref Scenario); MemoryMarshal.Write(data[(IdSize + StageSize + 1)..(IdSize + StageSize + 2)], ref SubScenarioType); } - public void Deserialize(Span data) { + public void Deserialize(ReadOnlySpan data) { throw new NotImplementedException("This packet should not be sent by the client."); } } \ No newline at end of file diff --git a/Shared/Packet/Packets/ConnectPacket.cs b/Shared/Packet/Packets/ConnectPacket.cs index d89352a..6cb50b9 100644 --- a/Shared/Packet/Packets/ConnectPacket.cs +++ b/Shared/Packet/Packets/ConnectPacket.cs @@ -17,7 +17,7 @@ public struct ConnectPacket : IPacket { Encoding.UTF8.GetBytes(ClientName).CopyTo(data[4..(4 + Constants.CostumeNameSize)]); } - public void Deserialize(Span data) { + public void Deserialize(ReadOnlySpan data) { ConnectionType = MemoryMarshal.Read(data); ClientName = Encoding.UTF8.GetString(data[4..(4 + Constants.CostumeNameSize)]).TrimNullTerm(); } diff --git a/Shared/Packet/Packets/CostumePacket.cs b/Shared/Packet/Packets/CostumePacket.cs index 22f9f84..8679695 100644 --- a/Shared/Packet/Packets/CostumePacket.cs +++ b/Shared/Packet/Packets/CostumePacket.cs @@ -15,7 +15,7 @@ public struct CostumePacket : IPacket { Encoding.UTF8.GetBytes(CapName).CopyTo(data[Constants.CostumeNameSize..]); } - public void Deserialize(Span data) { + public void Deserialize(ReadOnlySpan data) { BodyName = Encoding.UTF8.GetString(data[..Constants.CostumeNameSize]).TrimNullTerm(); CapName = Encoding.UTF8.GetString(data[Constants.CostumeNameSize..]).TrimNullTerm(); } diff --git a/Shared/Packet/Packets/DisconnectPacket.cs b/Shared/Packet/Packets/DisconnectPacket.cs index 0ec4fae..4503f58 100644 --- a/Shared/Packet/Packets/DisconnectPacket.cs +++ b/Shared/Packet/Packets/DisconnectPacket.cs @@ -6,5 +6,5 @@ public struct DisconnectPacket : IPacket { public short Size => 0; public void Serialize(Span data) { } - public void Deserialize(Span data) { } + public void Deserialize(ReadOnlySpan data) { } } \ No newline at end of file diff --git a/Shared/Packet/Packets/GamePacket.cs b/Shared/Packet/Packets/GamePacket.cs index 1bb8f8e..62b68c6 100644 --- a/Shared/Packet/Packets/GamePacket.cs +++ b/Shared/Packet/Packets/GamePacket.cs @@ -22,7 +22,7 @@ public struct GamePacket : IPacket { Encoding.UTF8.GetBytes(Stage).CopyTo(data[2..(2 + StageSize)]); } - public void Deserialize(Span data) { + public void Deserialize(ReadOnlySpan data) { Is2d = MemoryMarshal.Read(data); ScenarioNum = MemoryMarshal.Read(data[1..]); Stage = Encoding.UTF8.GetString(data[2..(2 + StageSize)]).TrimEnd('\0'); diff --git a/Shared/Packet/Packets/IPacket.cs b/Shared/Packet/Packets/IPacket.cs index 6a18212..432bb79 100644 --- a/Shared/Packet/Packets/IPacket.cs +++ b/Shared/Packet/Packets/IPacket.cs @@ -4,5 +4,5 @@ public interface IPacket { short Size { get; } void Serialize(Span data); - void Deserialize(Span data); + void Deserialize(ReadOnlySpan data); } \ No newline at end of file diff --git a/Shared/Packet/Packets/PlayerPacket.cs b/Shared/Packet/Packets/PlayerPacket.cs index 3ca1c68..aa45a69 100644 --- a/Shared/Packet/Packets/PlayerPacket.cs +++ b/Shared/Packet/Packets/PlayerPacket.cs @@ -35,7 +35,7 @@ public struct PlayerPacket : IPacket { MemoryMarshal.Write(data[offset++..++offset], ref SubAct); } - public void Deserialize(Span data) { + public void Deserialize(ReadOnlySpan data) { int offset = 0; Position = MemoryMarshal.Read(data[..(offset += Marshal.SizeOf())]); Rotation = MemoryMarshal.Read(data[offset..(offset += Marshal.SizeOf())]); diff --git a/Shared/Packet/Packets/ShinePacket.cs b/Shared/Packet/Packets/ShinePacket.cs index 123ae1a..197ba9d 100644 --- a/Shared/Packet/Packets/ShinePacket.cs +++ b/Shared/Packet/Packets/ShinePacket.cs @@ -12,7 +12,7 @@ public struct ShinePacket : IPacket { MemoryMarshal.Write(data, ref ShineId); } - public void Deserialize(Span data) { + public void Deserialize(ReadOnlySpan data) { ShineId = MemoryMarshal.Read(data); } } \ No newline at end of file diff --git a/Shared/Packet/Packets/TagPacket.cs b/Shared/Packet/Packets/TagPacket.cs index a1160a1..23f3cfc 100644 --- a/Shared/Packet/Packets/TagPacket.cs +++ b/Shared/Packet/Packets/TagPacket.cs @@ -18,7 +18,7 @@ public struct TagPacket : IPacket { MemoryMarshal.Write(data[4..], ref Minutes); } - public void Deserialize(Span data) { + public void Deserialize(ReadOnlySpan data) { UpdateType = MemoryMarshal.Read(data); IsIt = MemoryMarshal.Read(data[1..]); Seconds = MemoryMarshal.Read(data[2..]); diff --git a/Shared/Packet/Packets/UnhandledPacket.cs b/Shared/Packet/Packets/UnhandledPacket.cs index 3fcfa71..5c24ab5 100644 --- a/Shared/Packet/Packets/UnhandledPacket.cs +++ b/Shared/Packet/Packets/UnhandledPacket.cs @@ -14,7 +14,7 @@ public struct UnhandledPacket : IPacket { Data.CopyTo(data); } - public void Deserialize(Span data) { + public void Deserialize(ReadOnlySpan data) { Data = data.ToArray(); }