Add PacketWrapper#setCancelled(boolean)

This commit is contained in:
Nassim Jahnke 2023-02-28 21:49:08 +01:00
parent 8f97f43f12
commit 7161377794
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
2 changed files with 73 additions and 33 deletions

View File

@ -300,11 +300,6 @@ public interface PacketWrapper {
*/ */
PacketWrapper apply(Direction direction, State state, int index, List<Protocol> pipeline) throws Exception; PacketWrapper apply(Direction direction, State state, int index, List<Protocol> pipeline) throws Exception;
/**
* Cancel this packet from sending
*/
void cancel();
/** /**
* Check if this packet is cancelled. * Check if this packet is cancelled.
* *
@ -312,6 +307,20 @@ public interface PacketWrapper {
*/ */
boolean isCancelled(); boolean isCancelled();
/**
* Cancel this packet from sending.
*/
default void cancel() {
setCancelled(true);
}
/**
* Sets the cancellation state of the packet.
*
* @param cancel whether the packet should be cancelled
*/
void setCancelled(boolean cancel);
/** /**
* Get the user associated with this Packet * Get the user associated with this Packet
* *

View File

@ -30,7 +30,6 @@ import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.TypeConverter; import com.viaversion.viaversion.api.type.TypeConverter;
import com.viaversion.viaversion.exception.CancelException; import com.viaversion.viaversion.exception.CancelException;
import com.viaversion.viaversion.exception.InformativeException; import com.viaversion.viaversion.exception.InformativeException;
import com.viaversion.viaversion.util.Pair;
import com.viaversion.viaversion.util.PipelineUtil; import com.viaversion.viaversion.util.PipelineUtil;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
@ -45,6 +44,8 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public class PacketWrapperImpl implements PacketWrapper { public class PacketWrapperImpl implements PacketWrapper {
private static final Protocol[] PROTOCOL_ARRAY = new Protocol[0]; private static final Protocol[] PROTOCOL_ARRAY = new Protocol[0];
private final Deque<PacketValue> readableObjects = new ArrayDeque<>();
private final List<PacketValue> packetValues = new ArrayList<>();
private final ByteBuf inputBuffer; private final ByteBuf inputBuffer;
private final UserConnection userConnection; private final UserConnection userConnection;
private boolean send = true; private boolean send = true;
@ -53,8 +54,6 @@ public class PacketWrapperImpl implements PacketWrapper {
*/ */
private PacketType packetType; private PacketType packetType;
private int id; private int id;
private final Deque<Pair<Type, Object>> readableObjects = new ArrayDeque<>();
private final List<Pair<Type, Object>> packetValues = new ArrayList<>();
public PacketWrapperImpl(int packetId, @Nullable ByteBuf inputBuffer, UserConnection userConnection) { public PacketWrapperImpl(int packetId, @Nullable ByteBuf inputBuffer, UserConnection userConnection) {
this.id = packetId; this.id = packetId;
@ -72,8 +71,10 @@ public class PacketWrapperImpl implements PacketWrapper {
@Override @Override
public <T> T get(Type<T> type, int index) throws Exception { public <T> T get(Type<T> type, int index) throws Exception {
int currentIndex = 0; int currentIndex = 0;
for (Pair<Type, Object> packetValue : packetValues) { for (PacketValue packetValue : packetValues) {
if (packetValue.key() != type) continue; if (packetValue.type() != type) {
continue;
}
if (currentIndex == index) { if (currentIndex == index) {
//noinspection unchecked //noinspection unchecked
return (T) packetValue.value(); return (T) packetValue.value();
@ -86,8 +87,10 @@ public class PacketWrapperImpl implements PacketWrapper {
@Override @Override
public boolean is(Type type, int index) { public boolean is(Type type, int index) {
int currentIndex = 0; int currentIndex = 0;
for (Pair<Type, Object> packetValue : packetValues) { for (PacketValue packetValue : packetValues) {
if (packetValue.key() != type) continue; if (packetValue.type() != type) {
continue;
}
if (currentIndex == index) { if (currentIndex == index) {
return true; return true;
} }
@ -99,8 +102,10 @@ public class PacketWrapperImpl implements PacketWrapper {
@Override @Override
public boolean isReadable(Type type, int index) { public boolean isReadable(Type type, int index) {
int currentIndex = 0; int currentIndex = 0;
for (Pair<Type, Object> packetValue : readableObjects) { for (PacketValue packetValue : readableObjects) {
if (packetValue.key().getBaseClass() != type.getBaseClass()) continue; if (packetValue.type().getBaseClass() != type.getBaseClass()) {
continue;
}
if (currentIndex == index) { if (currentIndex == index) {
return true; return true;
} }
@ -113,8 +118,10 @@ public class PacketWrapperImpl implements PacketWrapper {
@Override @Override
public <T> void set(Type<T> type, int index, T value) throws Exception { public <T> void set(Type<T> type, int index, T value) throws Exception {
int currentIndex = 0; int currentIndex = 0;
for (Pair<Type, Object> packetValue : packetValues) { for (PacketValue packetValue : packetValues) {
if (packetValue.key() != type) continue; if (packetValue.type() != type) {
continue;
}
if (currentIndex == index) { if (currentIndex == index) {
packetValue.setValue(attemptTransform(type, value)); packetValue.setValue(attemptTransform(type, value));
return; return;
@ -140,23 +147,23 @@ public class PacketWrapperImpl implements PacketWrapper {
} }
} }
Pair<Type, Object> read = readableObjects.poll(); PacketValue readValue = readableObjects.poll();
Type rtype = read.key(); Type readType = readValue.type();
if (rtype == type if (readType == type
|| (type.getBaseClass() == rtype.getBaseClass() || (type.getBaseClass() == readType.getBaseClass()
&& type.getOutputClass() == rtype.getOutputClass())) { && type.getOutputClass() == readType.getOutputClass())) {
//noinspection unchecked //noinspection unchecked
return (T) read.value(); return (T) readValue.value();
} else if (rtype == Type.NOTHING) { } else if (readType == Type.NOTHING) {
return read(type); // retry return read(type); // retry
} else { } else {
throw createInformativeException(new IOException("Unable to read type " + type.getTypeName() + ", found " + read.key().getTypeName()), type, readableObjects.size()); throw createInformativeException(new IOException("Unable to read type " + type.getTypeName() + ", found " + readValue.type().getTypeName()), type, readableObjects.size());
} }
} }
@Override @Override
public <T> void write(Type<T> type, T value) { public <T> void write(Type<T> type, T value) {
packetValues.add(new Pair<>(type, attemptTransform(type, value))); packetValues.add(new PacketValue(type, attemptTransform(type, value)));
} }
/** /**
@ -207,11 +214,11 @@ public class PacketWrapperImpl implements PacketWrapper {
} }
int index = 0; int index = 0;
for (Pair<Type, Object> packetValue : packetValues) { for (PacketValue packetValue : packetValues) {
try { try {
packetValue.key().write(buffer, packetValue.value()); packetValue.type().write(buffer, packetValue.value());
} catch (Exception e) { } catch (Exception e) {
throw createInformativeException(e, packetValue.key(), index); throw createInformativeException(e, packetValue.type(), index);
} }
index++; index++;
} }
@ -392,13 +399,13 @@ public class PacketWrapperImpl implements PacketWrapper {
} }
@Override @Override
public void cancel() { public boolean isCancelled() {
this.send = false; return !this.send;
} }
@Override @Override
public boolean isCancelled() { public void setCancelled(boolean cancel) {
return !this.send; this.send = !cancel;
} }
@Override @Override
@ -452,7 +459,9 @@ public class PacketWrapperImpl implements PacketWrapper {
} }
private void sendToServer0(Class<? extends Protocol> protocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception { private void sendToServer0(Class<? extends Protocol> protocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception {
if (isCancelled()) return; if (isCancelled()) {
return;
}
try { try {
ByteBuf output = constructPacket(protocol, skipCurrentPipeline, Direction.SERVERBOUND); ByteBuf output = constructPacket(protocol, skipCurrentPipeline, Direction.SERVERBOUND);
@ -505,4 +514,26 @@ public class PacketWrapperImpl implements PacketWrapper {
", readableObjects=" + readableObjects + ", readableObjects=" + readableObjects +
'}'; '}';
} }
private static final class PacketValue {
private final Type type;
private Object value;
private PacketValue(Type type, @Nullable Object value) {
this.type = type;
this.value = value;
}
public Type type() {
return type;
}
public @Nullable Object value() {
return value;
}
public void setValue(@Nullable Object value) {
this.value = value;
}
}
} }