Skip to content

Commit

Permalink
remove protocol states
Browse files Browse the repository at this point in the history
  • Loading branch information
caunt committed Jul 29, 2024
1 parent 354580a commit f3112d9
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 117 deletions.
6 changes: 1 addition & 5 deletions src/Void.Proxy.API/Network/IO/Messages/IMinecraftPacket.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Void.Proxy.API.Network.IO.Buffers;
using Void.Proxy.API.Network.Protocol;
using Void.Proxy.API.Network.Protocol.States;

namespace Void.Proxy.API.Network.IO.Messages;

Expand All @@ -9,10 +8,7 @@ public interface IMinecraftPacket : IMinecraftMessage
public void Encode(ref MinecraftBuffer buffer, ProtocolVersion protocolVersion);
}

public delegate TSelf DecodeDelegate<out TSelf>(ref MinecraftBuffer buffer, ProtocolVersion protocolVersion) where TSelf : IMinecraftPacket;

public interface IMinecraftPacket<in TState, out TSelf> : IMinecraftPacket where TState : IProtocolState where TSelf : IMinecraftPacket
public interface IMinecraftPacket<out TSelf> : IMinecraftPacket where TSelf : IMinecraftPacket
{
public static abstract TSelf Decode(ref MinecraftBuffer buffer, ProtocolVersion protocolVersion);
Task<bool> HandleAsync(TState state);
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private void EncodeNetwork(IMinecraftNetworkStream stream, IMinecraftPacket pack
var packetId = packet switch
{
BinaryPacket binaryPacket => binaryPacket.Id,
_ when GetRegistry(true) is { } registry && registry.TryGetPacketId(packet, out var id) => id,
_ when RegistryHolder?.GetRegistry(Flow, Operation.Write) is { } registry && registry.TryGetPacketId(packet, out var id) => id,
_ => throw new InvalidOperationException($"Cannot find id for packet {packet}")
};

Expand Down Expand Up @@ -141,7 +141,7 @@ private async ValueTask EncodeNetworkAsync(IMinecraftNetworkStream stream, IMine
var packetId = packet switch
{
BinaryPacket binaryPacket => binaryPacket.Id,
_ when GetRegistry(true) is { } registry && registry.TryGetPacketId(packet, out var id) => id,
_ when RegistryHolder?.GetRegistry(Flow, Operation.Write) is { } registry && registry.TryGetPacketId(packet, out var id) => id,
_ => throw new InvalidOperationException($"Cannot find id for packet {packet}")
};

Expand All @@ -162,7 +162,7 @@ _ when GetRegistry(true) is { } registry && registry.TryGetPacketId(packet, out
await stream.WriteAsync(memory, cancellationToken);
return;

void TryEncodePacket() // divided into local function to support ref struct and Span
void TryEncodePacket()
{
var buffer = new MinecraftBuffer(memory.Span);
packet.Encode(ref buffer, ProtocolVersion.Latest);
Expand All @@ -177,15 +177,15 @@ public IMinecraftPacket ToPacket(Memory<byte> memory, IMemoryOwner<byte> memoryO

memory = memory[buffer.Position..];

var result = GetRegistry() switch
var result = RegistryHolder?.GetRegistry(Flow, Operation.Read) switch
{
{ } registry when registry.TryCreateDecoder(id, out var decoder) => TryDecodePacket(decoder),
_ => new BinaryPacket(id, memory, memoryOwner)
};

return result;

IMinecraftPacket TryDecodePacket(DecodeDelegate<IMinecraftPacket> decoder) // divided into local function to support ref struct and Span
IMinecraftPacket TryDecodePacket(PacketDecoder<IMinecraftPacket> decoder)
{
var buffer = new MinecraftBuffer(memory.Span);
var decodedPacket = decoder(ref buffer, ProtocolVersion.Latest);
Expand All @@ -196,16 +196,4 @@ IMinecraftPacket TryDecodePacket(DecodeDelegate<IMinecraftPacket> decoder) // di
return decodedPacket;
}
}

public IPacketRegistry? GetRegistry(bool writing = false)
{
return Flow switch
{
Direction.Clientbound when writing => RegistryHolder?.ServerboundRegistry,
Direction.Serverbound when writing => RegistryHolder?.ClientboundRegistry,
Direction.Clientbound when !writing => RegistryHolder?.ClientboundRegistry,
Direction.Serverbound when !writing => RegistryHolder?.ServerboundRegistry,
_ => null
};
}
}
7 changes: 7 additions & 0 deletions src/Void.Proxy.API/Network/Operation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Void.Proxy.API.Network;

public enum Operation
{
Write,
Read
}
6 changes: 6 additions & 0 deletions src/Void.Proxy.API/Network/Protocol/PacketDecoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Void.Proxy.API.Network.IO.Buffers;
using Void.Proxy.API.Network.IO.Messages;

namespace Void.Proxy.API.Network.Protocol;

public delegate TSelf PacketDecoder<out TSelf>(ref MinecraftBuffer buffer, ProtocolVersion protocolVersion) where TSelf : IMinecraftPacket;
6 changes: 0 additions & 6 deletions src/Void.Proxy.API/Network/Protocol/States/IProtocolState.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Void.Proxy.API/Registries/Packets/IPacketRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface IPacketRegistry
{
public ProtocolVersion ProtocolVersion { get; }

public bool TryCreateDecoder(int id, [MaybeNullWhen(false)] out DecodeDelegate<IMinecraftPacket> packet);
public bool TryCreateDecoder(int id, [MaybeNullWhen(false)] out PacketDecoder<IMinecraftPacket> packet);
public bool TryGetPacketId(IMinecraftPacket packet, [MaybeNullWhen(false)] out int id);
public void RegisterPackets(IReadOnlyDictionary<PacketMapping[], Type> mappings);
}
20 changes: 17 additions & 3 deletions src/Void.Proxy.API/Registries/Packets/IPacketRegistryHolder.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
using Void.Proxy.API.Plugins;
using Void.Proxy.API.Network;
using Void.Proxy.API.Plugins;

namespace Void.Proxy.API.Registries.Packets;

public interface IPacketRegistryHolder
{
public bool IsEmpty { get; }
public IPlugin? ManagedBy { get; set; }
public IPacketRegistry? ClientboundRegistry { get; set; }
public IPacketRegistry? ServerboundRegistry { get; set; }
public IPacketRegistry? ClientRegistry { get; set; }
public IPacketRegistry? ServerRegistry { get; set; }

public IPacketRegistry? GetRegistry(Direction? flow, Operation? operation)
{
return (flow, operation) switch
{
(Direction.Clientbound, Operation.Write) => ServerRegistry,
(Direction.Serverbound, Operation.Write) => ClientRegistry,
(Direction.Clientbound, Operation.Read) => ClientRegistry,
(Direction.Serverbound, Operation.Read) => ServerRegistry,
_ => null
};
}
public void Reset();
}
13 changes: 13 additions & 0 deletions src/Void.Proxy.Plugins.ModsSupport.Forge/ForgeMarker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
namespace Void.Proxy.Plugins.ProtocolSupport.Java.v1_20_2_to_latest.Forge;

// (HandshakePacket packet)
// var addressParts = packet.ServerAddress.Split('\0', StringSplitOptions.RemoveEmptyEntries);
// var isForge = ForgeMarker.Range().Any(marker => addressParts.Contains(marker.Value));
//
// if (isForge)
// link.Player.SetClientType(ClientType.Forge);
// else if (addressParts.Length > 1)
// Console.WriteLine($"Player {link.Player} had extra marker(s) {string.Join(", ", addressParts[1..])} in handshake, ignoring");
//
// link.SetProtocolVersion(ProtocolVersion.Get(packet.ProtocolVersion));
// link.SwitchState(packet.NextState);
// link.SaveHandshake(packet);

// public class ForgeMarker
// {
// private static readonly List<ForgeMarker> _markers = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Void.Proxy.API.Network.IO.Buffers;
using Void.Proxy.API.Network.IO.Messages;
using Void.Proxy.API.Network.Protocol;
using Void.Proxy.Plugins.ProtocolSupport.Java.v1_20_2_to_latest.States.Common;

namespace Void.Proxy.Plugins.ProtocolSupport.Java.v1_20_2_to_latest.Packets.Serverbound;

public class HandshakePacket : IMinecraftPacket<HandshakeState, HandshakePacket>
public class HandshakePacket : IMinecraftPacket<HandshakePacket>
{
public required int ProtocolVersion { get; set; }
public required string ServerAddress { get; set; }
Expand All @@ -31,11 +30,6 @@ public void Encode(ref MinecraftBuffer buffer, ProtocolVersion protocolVersion)
buffer.WriteVarInt(NextState);
}

public async Task<bool> HandleAsync(HandshakeState state)
{
return await state.HandleAsync(this);
}

public void Dispose()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public void OnPluginUnload(PluginUnloadEvent @event, CancellationToken cancellat
if (holder.ManagedBy != this)
continue;

holder.ClientboundRegistry = null;
holder.ServerboundRegistry = null;
holder.Reset();
}
}

Expand All @@ -73,8 +72,8 @@ public void OnPlayerConnected(PlayerConnectedEvent @event, CancellationToken can
return;

holder.ManagedBy = this;
holder.ClientboundRegistry = new PacketRegistry { ProtocolVersion = @event.Player.ProtocolVersion, Mappings = Mappings.ClientboundHandshakeMappings };
holder.ServerboundRegistry = new PacketRegistry { ProtocolVersion = @event.Player.ProtocolVersion, Mappings = Mappings.ServerboundHandshakeMappings };
holder.ClientRegistry = new PacketRegistry { ProtocolVersion = @event.Player.ProtocolVersion, Mappings = Mappings.ClientHandshakeMappings };
holder.ServerRegistry = new PacketRegistry { ProtocolVersion = @event.Player.ProtocolVersion, Mappings = Mappings.ServerHandshakeMappings };
}

[Subscribe]
Expand All @@ -85,8 +84,7 @@ public void OnPlayerDisconnected(PlayerDisconnectedEvent @event, CancellationTok
if (holder.ManagedBy != this)
return;

holder.ClientboundRegistry = null;
holder.ServerboundRegistry = null;
holder.Reset();
}

[Subscribe]
Expand Down Expand Up @@ -122,7 +120,7 @@ public void OnMessageReceived(MessageReceivedEvent @event, CancellationToken can
return;
case HandshakePacket handshake:
var holder = @event.Player.Scope.ServiceProvider.GetRequiredService<IPacketRegistryHolder>();
holder.ClientboundRegistry = null;
holder.ClientRegistry = null;
break;
}

Expand All @@ -142,7 +140,7 @@ public void OnMessageSent(MessageSentEvent @event, CancellationToken cancellatio
return;
case HandshakePacket handshake:
var holder = @event.Player.Scope.ServiceProvider.GetRequiredService<IPacketRegistryHolder>();
holder.ServerboundRegistry = null;
holder.ServerRegistry = null;
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@ namespace Void.Proxy.Plugins.ProtocolSupport.Java.v1_20_2_to_latest.Registries;

public static class Mappings
{
public static readonly IReadOnlyDictionary<PacketMapping[], Type> ClientboundHandshakeMappings = new Dictionary<PacketMapping[], Type>();
public static readonly IReadOnlyDictionary<PacketMapping[], Type> ClientHandshakeMappings = new Dictionary<PacketMapping[], Type>();

public static readonly IReadOnlyDictionary<PacketMapping[], Type> ServerboundHandshakeMappings = new Dictionary<PacketMapping[], Type>
public static readonly IReadOnlyDictionary<PacketMapping[], Type> ServerHandshakeMappings = new Dictionary<PacketMapping[], Type>
{
{ [new PacketMapping(0x00, ProtocolVersion.MINECRAFT_1_7_2)], typeof(HandshakePacket) }
};

public static readonly IReadOnlyDictionary<PacketMapping[], Type> ClientboundLoginMappings = new Dictionary<PacketMapping[], Type>();
public static readonly IReadOnlyDictionary<PacketMapping[], Type> ClientLoginMappings = new Dictionary<PacketMapping[], Type>();

public static readonly IReadOnlyDictionary<PacketMapping[], Type> ServerboundLoginMappings = new Dictionary<PacketMapping[], Type>();
public static readonly IReadOnlyDictionary<PacketMapping[], Type> ServerLoginMappings = new Dictionary<PacketMapping[], Type>();

public static readonly IReadOnlyDictionary<PacketMapping[], Type> ClientboundConfigurationMappings = new Dictionary<PacketMapping[], Type>();
public static readonly IReadOnlyDictionary<PacketMapping[], Type> ClientConfigurationMappings = new Dictionary<PacketMapping[], Type>();

public static readonly IReadOnlyDictionary<PacketMapping[], Type> ServerboundConfigurationMappings = new Dictionary<PacketMapping[], Type>();
public static readonly IReadOnlyDictionary<PacketMapping[], Type> ServerConfigurationMappings = new Dictionary<PacketMapping[], Type>();

public static readonly IReadOnlyDictionary<PacketMapping[], Type> ClientboundPlayMappings = new Dictionary<PacketMapping[], Type>();
public static readonly IReadOnlyDictionary<PacketMapping[], Type> ClientPlayMappings = new Dictionary<PacketMapping[], Type>();

public static readonly IReadOnlyDictionary<PacketMapping[], Type> ServerboundPlayMappings = new Dictionary<PacketMapping[], Type>();
public static readonly IReadOnlyDictionary<PacketMapping[], Type> ServerPlayMappings = new Dictionary<PacketMapping[], Type>();

public static void Fill()
{
} // will initialize static fields
// will initialize static fields
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public required IReadOnlyDictionary<PacketMapping[], Type> Mappings

public required ProtocolVersion ProtocolVersion { get; init; }

public bool TryCreateDecoder(int id, [MaybeNullWhen(false)] out DecodeDelegate<IMinecraftPacket> packet)
public bool TryCreateDecoder(int id, [MaybeNullWhen(false)] out PacketDecoder<IMinecraftPacket> packet)
{
packet = _mappings.TryGetValue(id, out var type) ? type.GetMethod(nameof(HandshakePacket.Decode))!.CreateDelegate<DecodeDelegate<IMinecraftPacket>>() : null;
packet = _mappings.TryGetValue(id, out var type) ? type.GetMethod(nameof(HandshakePacket.Decode))!.CreateDelegate<PacketDecoder<IMinecraftPacket>>() : null;
return packet != null;
}

Expand Down

This file was deleted.

This file was deleted.

3 changes: 1 addition & 2 deletions src/Void.Proxy/Network/Protocol/ChannelBuilderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ namespace Void.Proxy.Network.Protocol;
public class ChannelBuilderService(ILogger<ChannelBuilderService> logger, IEventService events) : IChannelBuilderService
{
public const int MaxHandshakeSize = 4096;
private Memory<byte> _buffer = Memory<byte>.Empty;

private Memory<byte> _buffer = Memory<byte>.Empty;
private ChannelBuilder _builder = (_, networkStream, _) => ValueTask.FromResult<IMinecraftChannel>(new SimpleMinecraftChannel(new SimpleNetworkStream(networkStream)));

private bool _found;

public async ValueTask SearchChannelBuilderAsync(IPlayer player, CancellationToken cancellationToken = default)
Expand Down
13 changes: 10 additions & 3 deletions src/Void.Proxy/Registries/Packets/PacketRegistryHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ namespace Void.Proxy.Registries.Packets;

public class PacketRegistryHolder : IPacketRegistryHolder
{
public bool IsEmpty => this is { ClientboundRegistry: null, ServerboundRegistry: null, ManagedBy: null };
public bool IsEmpty => this is { ClientRegistry: null, ServerRegistry: null, ManagedBy: null };
public IPlugin? ManagedBy { get; set; }
public IPacketRegistry? ClientboundRegistry { get; set; }
public IPacketRegistry? ServerboundRegistry { get; set; }
public IPacketRegistry? ClientRegistry { get; set; }
public IPacketRegistry? ServerRegistry { get; set; }

public void Reset()
{
ManagedBy = null;
ClientRegistry = null;
ServerRegistry = null;
}
}

0 comments on commit f3112d9

Please sign in to comment.