chore: refactor packet registration

This commit is contained in:
Riley Park
2024-04-22 21:58:25 -07:00
parent 784806848d
commit 7048cabaf8
4 changed files with 660 additions and 689 deletions

View File

@@ -0,0 +1,35 @@
/*
* Copyright (C) 2018-2023 Velocity Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.velocitypowered.proxy.protocol;
import com.velocitypowered.api.network.ProtocolVersion;
import org.jspecify.annotations.Nullable;
interface PacketMapper {
default void readWrite(final int id, final ProtocolVersion from) {
this.readWrite(id, from, null);
}
void readWrite(final int id, final ProtocolVersion from, final @Nullable ProtocolVersion to);
default void writeOnly(final int id, final ProtocolVersion from) {
this.writeOnly(id, from, null);
}
void writeOnly(final int id, final ProtocolVersion from, final @Nullable ProtocolVersion to);
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright (C) 2018-2023 Velocity Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.velocitypowered.proxy.protocol;
import com.velocitypowered.api.network.ProtocolVersion;
import org.jspecify.annotations.Nullable;
/**
* Packet mapping.
*/
public record PacketMapping(
int id,
ProtocolVersion from,
@Nullable ProtocolVersion to,
boolean writeOnly
) {
}

View File

@@ -45,10 +45,11 @@ class PacketRegistryTest {
private StateRegistry.PacketRegistry setupRegistry() { private StateRegistry.PacketRegistry setupRegistry() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY); ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
registry.register(HandshakePacket.class, HandshakePacket::new, registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_8, null, false), m.readWrite(0x01, MINECRAFT_1_8, null);
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, null, false), m.readWrite(0x00, MINECRAFT_1_12, null);
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_15, MINECRAFT_1_16, false)); m.readWrite(0x00, MINECRAFT_1_15, MINECRAFT_1_16);
});
return registry; return registry;
} }
@@ -86,7 +87,7 @@ class PacketRegistryTest {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY); ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class,
() -> registry.register(HandshakePacket.class, HandshakePacket::new)); () -> registry.register(HandshakePacket.class, HandshakePacket::new, m -> {}));
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class,
() -> registry.getProtocolRegistry(ProtocolVersion.UNKNOWN) () -> registry.getProtocolRegistry(ProtocolVersion.UNKNOWN)
.getPacketId(new HandshakePacket())); .getPacketId(new HandshakePacket()));
@@ -97,54 +98,62 @@ class PacketRegistryTest {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY); ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class,
() -> registry.register(HandshakePacket.class, HandshakePacket::new, () -> registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, null, false), m.readWrite(0x01, MINECRAFT_1_13, null);
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, null, false))); m.readWrite(0x00, MINECRAFT_1_8, null);
}));
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class,
() -> registry.register(HandshakePacket.class, HandshakePacket::new, () -> registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, null, false), m.readWrite(0x01, MINECRAFT_1_13, null);
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, null, false))); m.readWrite(0x01, MINECRAFT_1_13, null);
}));
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class,
() -> registry.register(HandshakePacket.class, HandshakePacket::new, () -> registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, MINECRAFT_1_8, false))); m.readWrite(0x01, MINECRAFT_1_13, MINECRAFT_1_8);
}));
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class,
() -> registry.register(HandshakePacket.class, HandshakePacket::new, () -> registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_8, MINECRAFT_1_14, false), m.readWrite(0x01, MINECRAFT_1_8, MINECRAFT_1_14);
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_16, null, false))); m.readWrite(0x00, MINECRAFT_1_16, null);
}));
} }
@Test @Test
void failOnDuplicate() { void failOnDuplicate() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY); ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
registry.register(HandshakePacket.class, HandshakePacket::new, registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, null, false)); m.readWrite(0x00, MINECRAFT_1_8, null);
});
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class,
() -> registry.register(HandshakePacket.class, HandshakePacket::new, () -> registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12, null, false))); m.readWrite(0x01, MINECRAFT_1_12, null);
}));
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class,
() -> registry.register(StatusPingPacket.class, StatusPingPacket::new, () -> registry.register(StatusPingPacket.class, StatusPingPacket::new, m -> {
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_13, null, false))); m.readWrite(0x00, MINECRAFT_1_13, null);
}));
} }
@Test @Test
void shouldNotFailWhenRegisterLatestProtocolVersion() { void shouldNotFailWhenRegisterLatestProtocolVersion() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY); ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
assertDoesNotThrow(() -> registry.register(HandshakePacket.class, HandshakePacket::new, assertDoesNotThrow(() -> registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, null, false), m.readWrite(0x00, MINECRAFT_1_8, null);
new StateRegistry.PacketMapping(0x01, getLast(ProtocolVersion.SUPPORTED_VERSIONS), m.readWrite(0x01, getLast(ProtocolVersion.SUPPORTED_VERSIONS), null);
null, false))); }));
} }
@Test @Test
void registrySuppliesCorrectPacketsByProtocol() { void registrySuppliesCorrectPacketsByProtocol() {
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(
ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY); ProtocolUtils.Direction.CLIENTBOUND, StateRegistry.PLAY);
registry.register(HandshakePacket.class, HandshakePacket::new, registry.register(HandshakePacket.class, HandshakePacket::new, m -> {
new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, null, false), m.readWrite(0x00, MINECRAFT_1_12, null);
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12_1, null, false), m.readWrite(0x01, MINECRAFT_1_12_1, null);
new StateRegistry.PacketMapping(0x02, MINECRAFT_1_13, null, false)); m.readWrite(0x02, MINECRAFT_1_13, null);
});
assertEquals(HandshakePacket.class, assertEquals(HandshakePacket.class,
registry.getProtocolRegistry(MINECRAFT_1_12).createPacket(0x00).getClass()); registry.getProtocolRegistry(MINECRAFT_1_12).createPacket(0x00).getClass());
assertEquals(HandshakePacket.class, assertEquals(HandshakePacket.class,