Clean up ProtocolPipeline

The filter functionality isn't something that's likely to be used again, so move out its usage and only check on <1.9 servers
This commit is contained in:
KennyTV 2021-04-27 15:56:34 +02:00
parent f1c8d271b1
commit 95e20677fd
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
9 changed files with 161 additions and 160 deletions

View File

@ -24,8 +24,6 @@ package com.viaversion.viaversion.api.protocol;
import com.google.common.base.Preconditions;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.platform.providers.ViaProviders;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.Direction;
import com.viaversion.viaversion.api.protocol.packet.PacketType;
@ -39,7 +37,6 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
@ -127,11 +124,6 @@ public abstract class Protocol<C1 extends ClientboundPacketType, C2 extends Clie
}
}
@Override
public void filterPacket(UserConnection info, Object packet, List output) throws Exception {
output.add(packet);
}
/**
* Register the packets for this protocol. To be overriden.
*/
@ -152,24 +144,6 @@ public abstract class Protocol<C1 extends ClientboundPacketType, C2 extends Clie
protected void onMappingDataLoaded() {
}
@Override
public void register(ViaProviders providers) {
}
@Override
public void init(UserConnection userConnection) {
}
@Override
public void registerIncoming(State state, int oldPacketID, int newPacketID) {
registerIncoming(state, oldPacketID, newPacketID, null);
}
@Override
public void registerIncoming(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper) {
registerIncoming(state, oldPacketID, newPacketID, packetRemapper, false);
}
@Override
public void registerIncoming(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper, boolean override) {
ProtocolPacket protocolPacket = new ProtocolPacket(state, oldPacketID, newPacketID, packetRemapper);
@ -191,21 +165,6 @@ public abstract class Protocol<C1 extends ClientboundPacketType, C2 extends Clie
});
}
@Override
public void cancelIncoming(State state, int newPacketID) {
cancelIncoming(state, -1, newPacketID);
}
@Override
public void registerOutgoing(State state, int oldPacketID, int newPacketID) {
registerOutgoing(state, oldPacketID, newPacketID, null);
}
@Override
public void registerOutgoing(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper) {
registerOutgoing(state, oldPacketID, newPacketID, packetRemapper, false);
}
@Override
public void cancelOutgoing(State state, int oldPacketID, int newPacketID) {
registerOutgoing(state, oldPacketID, newPacketID, new PacketRemapper() {
@ -216,11 +175,6 @@ public abstract class Protocol<C1 extends ClientboundPacketType, C2 extends Clie
});
}
@Override
public void cancelOutgoing(State state, int oldPacketID) {
cancelOutgoing(state, oldPacketID, -1);
}
@Override
public void registerOutgoing(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper, boolean override) {
ProtocolPacket protocolPacket = new ProtocolPacket(state, oldPacketID, newPacketID, packetRemapper);
@ -254,11 +208,6 @@ public abstract class Protocol<C1 extends ClientboundPacketType, C2 extends Clie
registerOutgoing(State.PLAY, packetType.ordinal(), mappedPacketType != null ? mappedPacketType.ordinal() : -1, packetRemapper);
}
@Override
public void registerOutgoing(C1 packetType, @Nullable C2 mappedPacketType) {
registerOutgoing(packetType, mappedPacketType, null);
}
@Override
public void cancelOutgoing(C1 packetType) {
cancelOutgoing(State.PLAY, packetType.ordinal(), packetType.ordinal());

View File

@ -48,12 +48,12 @@ public interface ProtocolPipeline extends SimpleProtocol {
void add(List<Protocol> protocols);
/**
* Check if the pipeline contains a protocol
* Returns whether the protocol is in this pipeline.
*
* @param pipeClass The class to check
* @return True if the protocol class is in the pipeline
* @param protocolClass protocol class
* @return whether the protocol class is in this pipeline
*/
boolean contains(Class<? extends Protocol> pipeClass);
boolean contains(Class<? extends Protocol> protocolClass);
/**
* Returns the protocol from the given class if present in the pipeline.
@ -62,20 +62,10 @@ public interface ProtocolPipeline extends SimpleProtocol {
* @param <P> protocol
* @return protocol from class
* @see #contains(Class)
* @see ProtocolManager#getProtocol(Class) for a faster alternative
* @see ProtocolManager#getProtocol(Class) for a faster implementation
*/
@Nullable <P extends Protocol> P getProtocol(Class<P> pipeClass);
/**
* Use the pipeline to filter a NMS packet
*
* @param o The NMS packet object
* @param list The output list to write to
* @return If it should not write the input object to te list.
* @throws Exception If it failed to convert / packet cancelld.
*/
boolean filter(Object o, List list) throws Exception;
/**
* Returns the list of protocols this pipeline contains.
*
@ -83,6 +73,13 @@ public interface ProtocolPipeline extends SimpleProtocol {
*/
List<Protocol> pipes();
/**
* Returns whether this pipe has protocols that are not base protocols, as given by {@link Protocol#isBaseProtocol()}.
*
* @return whether this pipe has protocols that are not base protocols
*/
boolean hasNonBaseProtocols();
/**
* Cleans the pipe and adds the base protocol.
* /!\ WARNING - It doesn't add version-specific base Protocol.

View File

@ -25,7 +25,6 @@ package com.viaversion.viaversion.api.protocol.base;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.MappingData;
import com.viaversion.viaversion.api.platform.providers.ViaProviders;
import com.viaversion.viaversion.api.protocol.AbstractSimpleProtocol;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.Direction;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
@ -34,8 +33,6 @@ import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.List;
/**
* Abstract protocol class handling packet transformation between two protocol versions.
* Clientbound and serverbount packet types can be set to enforce correct usage of them.
@ -44,55 +41,10 @@ import java.util.List;
* @param <C2> new clientbound packet types
* @param <S1> old serverbound packet types
* @param <S2> new serverbound packet types
* @see AbstractSimpleProtocol for a helper class if you do not want to define any of the types above
* @see SimpleProtocol for a helper class if you do not want to define any of the types above
*/
public interface Protocol<C1 extends ClientboundPacketType, C2 extends ClientboundPacketType, S1 extends ServerboundPacketType, S2 extends ServerboundPacketType> {
/**
* Should this protocol filter an object packet from this class.
* Default: false
*
* @param packetClass The class of the current input
* @return True if it should handle the filtering
*/
default boolean isFiltered(Class packetClass) {
return false;
}
/**
* Filter a packet into the output
*
* @param info The current user connection
* @param packet The input packet as an object (NMS)
* @param output The list to put the object into.
* @throws Exception Throws exception if cancelled / error.
*/
void filterPacket(UserConnection info, Object packet, List output) throws Exception;
/**
* Loads the mappingdata.
*/
void loadMappingData();
/**
* Handle protocol registration phase, use this to register providers / tasks.
* <p>
* To be overridden if needed.
*
* @param providers The current providers
*/
void register(ViaProviders providers);
/**
* Initialise a user for this protocol setting up objects.
* /!\ WARNING - May be called more than once in a single {@link UserConnection}
* <p>
* To be overridden if needed.
*
* @param userConnection The user to initialise
*/
void init(UserConnection userConnection);
/**
* Register an incoming packet, with simple id transformation.
*
@ -100,7 +52,9 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param oldPacketID The old packet ID
* @param newPacketID The new packet ID
*/
void registerIncoming(State state, int oldPacketID, int newPacketID);
default void registerIncoming(State state, int oldPacketID, int newPacketID) {
registerIncoming(state, oldPacketID, newPacketID, null);
}
/**
* Register an incoming packet, with id transformation and remapper.
@ -110,13 +64,17 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param newPacketID The new packet ID
* @param packetRemapper The remapper to use for the packet
*/
void registerIncoming(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper);
default void registerIncoming(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper) {
registerIncoming(state, oldPacketID, newPacketID, packetRemapper, false);
}
void registerIncoming(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper, boolean override);
void cancelIncoming(State state, int oldPacketID, int newPacketID);
void cancelIncoming(State state, int newPacketID);
default void cancelIncoming(State state, int newPacketID) {
cancelIncoming(state, -1, newPacketID);
}
/**
* Register an outgoing packet, with simple id transformation.
@ -125,7 +83,9 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param oldPacketID The old packet ID
* @param newPacketID The new packet ID
*/
void registerOutgoing(State state, int oldPacketID, int newPacketID);
default void registerOutgoing(State state, int oldPacketID, int newPacketID) {
registerOutgoing(state, oldPacketID, newPacketID, null);
}
/**
* Register an outgoing packet, with id transformation and remapper.
@ -135,11 +95,15 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param newPacketID The new packet ID
* @param packetRemapper The remapper to use for the packet
*/
void registerOutgoing(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper);
default void registerOutgoing(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper) {
registerOutgoing(state, oldPacketID, newPacketID, packetRemapper, false);
}
void cancelOutgoing(State state, int oldPacketID, int newPacketID);
void cancelOutgoing(State state, int oldPacketID);
default void cancelOutgoing(State state, int oldPacketID) {
cancelOutgoing(state, oldPacketID, -1);
}
void registerOutgoing(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper, boolean override);
@ -167,8 +131,15 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param packetType clientbound packet type the server initially sends
* @param mappedPacketType clientbound packet type after transforming for the client
*/
void registerOutgoing(C1 packetType, C2 mappedPacketType);
default void registerOutgoing(C1 packetType, @Nullable C2 mappedPacketType) {
registerOutgoing(packetType, mappedPacketType, null);
}
/**
* Cancels any clientbound packets from the given type.
*
* @param packetType clientbound packet type to cancel
*/
void cancelOutgoing(C1 packetType);
/**
@ -186,8 +157,13 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param mappedPacketType serverbound packet type after transforming for the server
* @param packetRemapper remapper
*/
void registerIncoming(S2 packetType, S1 mappedPacketType, @Nullable PacketRemapper packetRemapper);
void registerIncoming(S2 packetType, @Nullable S1 mappedPacketType, @Nullable PacketRemapper packetRemapper);
/**
* Cancels any serverbound packets from the given type.
*
* @param packetType serverbound packet type to cancel
*/
void cancelIncoming(S2 packetType);
/**
@ -218,8 +194,20 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
*/
void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception;
/**
* Returns a cached object by the given type if present.
*
* @param objectClass class of the object to get
* @param <T> type
* @return object if present, else null
*/
@Nullable <T> T get(Class<T> objectClass);
/**
* Caches an object, retrievable by using {@link #get(Class)}.
*
* @param object object to cache
*/
void put(Object object);
/**
@ -232,6 +220,39 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
*/
boolean hasMappingDataToLoad();
/**
* Loads the protocol's mapping data.
*
* @throws NullPointerException if this protocol has no mapping data
*/
void loadMappingData();
/**
* Handle protocol registration phase, use this to register providers / tasks.
* <p>
* To be overridden if needed.
*
* @param providers The current providers
*/
default void register(ViaProviders providers) {
}
/**
* Initialise a user for this protocol setting up objects.
* /!\ WARNING - May be called more than once in a single {@link UserConnection}
* <p>
* To be overridden if needed.
*
* @param userConnection The user to initialise
*/
default void init(UserConnection userConnection) {
}
/**
* Returns the protocol's mapping data if present.
*
* @return mapping data if present
*/
default @Nullable MappingData getMappingData() {
return null;
}

View File

@ -17,7 +17,9 @@
*/
package com.viaversion.viaversion.bukkit.handlers;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
@ -39,13 +41,23 @@ public class BukkitPacketHandler extends MessageToMessageEncoder {
if (!(o instanceof ByteBuf)) {
info.getPacketTracker().setLastPacket(o);
/* This transformer is more for fixing issues which we find hard at packet level :) */
if (info.isActive()) {
if (info.getProtocolInfo().getPipeline().filter(o, list)) {
return;
}
if (info.isActive() && filter(o, list)) {
return;
}
}
list.add(o);
}
@Deprecated
public boolean filter(Object o, List list) throws Exception {
if (info.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) {
Protocol1_9To1_8 protocol = Via.getManager().getProtocolManager().getProtocol(Protocol1_9To1_8.class);
if (protocol.isFiltered(o.getClass())) {
protocol.filterPacket(info, o, list);
return true;
}
}
return false;
}
}

View File

@ -22,7 +22,7 @@
*/
package com.viaversion.viaversion.protocol;
import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.platform.ViaPlatform;
@ -36,6 +36,8 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
@ -45,6 +47,7 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot
* Protocol list ordered from client to server transforation with the base protocols at the end.
*/
private List<Protocol> protocolList;
private Set<Class<? extends Protocol>> protocolSet;
public ProtocolPipelineImpl(UserConnection userConnection) {
this.userConnection = userConnection;
@ -54,8 +57,13 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot
@Override
protected void registerPackets() {
protocolList = new CopyOnWriteArrayList<>();
// Create a backing set for faster contains calls with larger pipes
protocolSet = Sets.newSetFromMap(new ConcurrentHashMap<>());
// This is a pipeline so we register basic pipes
protocolList.add(Via.getManager().getProtocolManager().getBaseProtocol());
Protocol baseProtocol = Via.getManager().getProtocolManager().getBaseProtocol();
protocolList.add(baseProtocol);
protocolSet.add(baseProtocol.getClass());
}
@Override
@ -65,9 +73,8 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot
@Override
public void add(Protocol protocol) {
Preconditions.checkNotNull(protocolList, "Tried to add protocol too early");
protocolList.add(protocol);
protocolSet.add(protocol.getClass());
protocol.init(userConnection);
if (!protocol.isBaseProtocol()) {
@ -77,11 +84,10 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot
@Override
public void add(List<Protocol> protocols) {
Preconditions.checkNotNull(protocolList, "Tried to add protocol too early");
protocolList.addAll(protocols);
for (Protocol protocol : protocols) {
protocol.init(userConnection);
this.protocolSet.add(protocol.getClass());
}
moveBaseProtocolsToTail();
@ -142,13 +148,8 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot
}
@Override
public boolean contains(Class<? extends Protocol> pipeClass) {
for (Protocol protocol : protocolList) {
if (protocol.getClass() == pipeClass) {
return true;
}
}
return false;
public boolean contains(Class<? extends Protocol> protocolClass) {
return protocolSet.contains(protocolClass);
}
@Override
@ -162,20 +163,18 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot
}
@Override
public boolean filter(Object o, List list) throws Exception {
for (Protocol protocol : protocolList) {
if (protocol.isFiltered(o.getClass())) {
protocol.filterPacket(userConnection, o, list);
return true;
}
}
return false;
public List<Protocol> pipes() {
return protocolList;
}
@Override
public List<Protocol> pipes() {
return protocolList;
public boolean hasNonBaseProtocols() {
for (Protocol protocol : protocolList) {
if (!protocol.isBaseProtocol()) {
return true;
}
}
return false;
}
@Override

View File

@ -147,7 +147,7 @@ public class BaseProtocol1_7 extends AbstractSimpleProtocol {
// Add to ported clients
Via.getManager().getConnectionManager().onLoginSuccess(wrapper.user());
if (info.getPipeline().pipes().stream().allMatch(Via.getManager().getProtocolManager()::isBaseProtocol)) { // Only base protocol
if (!info.getPipeline().hasNonBaseProtocols()) { // Only base protocol
wrapper.user().setActive(false);
}

View File

@ -147,13 +147,24 @@ public class Protocol1_9To1_8 extends Protocol<ClientboundPackets1_8, Clientboun
providers.require(MovementTransmitterProvider.class);
}
@Override
/**
* Should this protocol filter an object packet from this class.
*
* @param packetClass The class of the current input
* @return True if it should handle the filtering
*/
public boolean isFiltered(Class packetClass) {
return Via.getManager().getProviders().get(BulkChunkTranslatorProvider.class).isFiltered(packetClass);
}
@Override
/**
* Filter a packet into the output
*
* @param info The current user connection
* @param packet The input packet as an object (NMS)
* @param output The list to put the object into.
* @throws Exception Throws exception if cancelled / error.
*/
public void filterPacket(UserConnection info, Object packet, List output) throws Exception {
output.addAll(info.get(ClientChunks.class).transformMapChunkBulk(packet));
}

View File

@ -338,7 +338,7 @@ public class EntityTracker1_9 extends EntityTracker {
PacketWrapper wrapper = PacketWrapper.create(0x39, null, getUser());
wrapper.write(Type.VAR_INT, entityId);
wrapper.write(Types1_9.METADATA_LIST, metadataList);
getUser().getProtocolInfo().getPipeline().getProtocol(Protocol1_9To1_8.class).get(MetadataRewriter1_9To1_8.class)
Via.getManager().getProtocolManager().getProtocol(Protocol1_9To1_8.class).get(MetadataRewriter1_9To1_8.class)
.handleMetadata(entityId, metadataList, getUser());
handleMetadata(entityId, metadataList);
if (!metadataList.isEmpty()) {

View File

@ -17,7 +17,9 @@
*/
package com.viaversion.viaversion.sponge.handlers;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
@ -39,13 +41,23 @@ public class SpongePacketHandler extends MessageToMessageEncoder {
if (!(o instanceof ByteBuf)) {
info.getPacketTracker().setLastPacket(o);
/* This transformer is more for fixing issues which we find hard at packet level :) */
if (info.isActive()) {
if (info.getProtocolInfo().getPipeline().filter(o, list)) {
return;
}
if (info.isActive() && filter(o, list)) {
return;
}
}
list.add(o);
}
@Deprecated
public boolean filter(Object o, List list) throws Exception {
if (info.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) {
Protocol1_9To1_8 protocol = Via.getManager().getProtocolManager().getProtocol(Protocol1_9To1_8.class);
if (protocol.isFiltered(o.getClass())) {
protocol.filterPacket(info, o, list);
return true;
}
}
return false;
}
}