Updated CommonTransformer, cleaned up client-side UserConnection, issue template

This commit is contained in:
creeper123123321 2019-11-12 16:15:21 -03:00
parent 322fee616b
commit d72d30f0a0
8 changed files with 130 additions and 60 deletions

26
.github/ISSUE_TEMPLATE/bug_report.md vendored Normal file
View File

@ -0,0 +1,26 @@
---
name: Bug report
about: Create a bug report so we can fix it
---
**Describe the bug, provide any errors**
A clear and concise description of what the bug is. Can you https://hastebin.com the error?
**How can we reproduce it?**
Steps to reproduce the behavior:
1. Login on 1.12'
2. Click on '....'
3. The '....' is displayed wrong
**Expected behaviour**
A clear and concise description of what you expected to happen.
**Screenshots**
If applicable, add screenshots to help explain your problem.
**ViaVersion Dump:**
- Type /viaversion dump, and put the link here.
**Additional server info**
Do you use a proxy (eg. BungeeCord)? What software do you use and what plugins?

View File

@ -0,0 +1,17 @@
---
name: Feature request
about: Suggest an idea for ViaVersion
---
**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
**Describe the solution you'd like**
A clear and concise description of what you want to happen.
**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.
**Which versions is this for?**
Does the feature apply to any specific versions? If so put them here.

View File

@ -28,37 +28,52 @@ import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.packets.Direction;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import java.util.function.Function;
// TODO delete this when https://github.com/ViaVersion/ViaVersion/pull/1505 is merged
public class CommonTransformer {
public static final String HANDLER_DECODER_NAME = "viafabric_decoder_handler";
public static final String HANDLER_ENCODER_NAME = "viafabric_encoder_handler";
public static final String HANDLER_DECODER_NAME = "via-decoder";
public static final String HANDLER_ENCODER_NAME = "via-encoder";
public static void transformClientbound(ByteBuf draft, UserConnection user) throws Exception {
if (!draft.isReadable()) return;
// Increment sent
user.incrementSent();
transform(draft, user, Direction.OUTGOING);
}
public static void transformServerbound(ByteBuf draft, UserConnection user) throws Exception {
if (!draft.isReadable()) return;
public static boolean preServerboundCheck(UserConnection user) {
// Ignore if pending disconnect
if (user.isPendingDisconnect()) return;
if (user.isPendingDisconnect()) return true;
// Increment received + Check PPS
if (user.incrementReceived() && user.handlePPS()) return;
transform(draft, user, Direction.INCOMING);
return user.incrementReceived() && user.handlePPS();
}
private static void transform(ByteBuf draft, UserConnection user, Direction direction) throws Exception {
if (!user.isActive()) return;
public static void preClientbound(UserConnection user) {
user.incrementSent();
}
public static boolean willTransformPacket(UserConnection user) {
return user.isActive();
}
public static void transformClientbound(ByteBuf draft, UserConnection user, Function<Throwable, Exception> cancelSupplier) throws Exception {
if (!draft.isReadable()) return;
transform(draft, user, Direction.OUTGOING, cancelSupplier);
}
public static void transformServerbound(ByteBuf draft, UserConnection user, Function<Throwable, Exception> cancelSupplier) throws Exception {
if (!draft.isReadable()) return;
transform(draft, user, Direction.INCOMING, cancelSupplier);
}
private static void transform(ByteBuf draft, UserConnection user, Direction direction, Function<Throwable, Exception> cancelSupplier) throws Exception {
int id = Type.VAR_INT.read(draft);
if (id == PacketWrapper.PASSTHROUGH_ID) return;
PacketWrapper wrapper = new PacketWrapper(id, draft, user);
ProtocolInfo protInfo = user.get(ProtocolInfo.class);
protInfo.getPipeline().transform(direction, protInfo.getState(), wrapper);
try {
protInfo.getPipeline().transform(direction, protInfo.getState(), wrapper);
} catch (CancelException ex) {
throw cancelSupplier.apply(ex);
}
ByteBuf transformed = draft.alloc().buffer();
try {
wrapper.writeToBuffer(transformed);

View File

@ -45,9 +45,14 @@ public class VRDecodeHandler extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
CommonTransformer.preClientbound(user);
if (!CommonTransformer.willTransformPacket(user)) {
out.add(msg.readRetainedSlice(msg.readableBytes()));
return;
}
ByteBuf draft = msg.alloc().buffer().writeBytes(msg);
try {
CommonTransformer.transformClientbound(draft, user);
CommonTransformer.transformClientbound(draft, user, ignored -> CancelException.CACHED);
out.add(draft.retain());
} finally {
draft.release();

View File

@ -27,13 +27,14 @@ package com.github.creeper123123321.viafabric.handler.clientside;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.EncoderException;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.util.PipelineUtil;
public class VREncodeHandler extends MessageToByteEncoder {
import java.util.List;
public class VREncodeHandler extends MessageToMessageEncoder<ByteBuf> {
private UserConnection user;
public VREncodeHandler(UserConnection user) {
@ -41,9 +42,21 @@ public class VREncodeHandler extends MessageToByteEncoder {
}
@Override
protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
out.writeBytes((ByteBuf) msg);
CommonTransformer.transformServerbound(out, user);
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
if (CommonTransformer.preServerboundCheck(user)) {
throw CancelException.CACHED; // Theoretically a server with m2m decoder would hold this packet, but we'll just cancel for now and this is used for kicking packet spamming
}
if (!CommonTransformer.willTransformPacket(user)) {
out.add(msg.readRetainedSlice(msg.readableBytes()));
return;
}
ByteBuf draft = ctx.alloc().buffer().writeBytes(msg);
try {
CommonTransformer.transformServerbound(draft, user, ignored -> CancelException.CACHED);
out.add(draft.retain());
} finally {
draft.release();
}
}
@Override

View File

@ -45,9 +45,14 @@ public class FabricDecodeHandler extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (CommonTransformer.preServerboundCheck(user)) return;
if (!CommonTransformer.willTransformPacket(user)) {
out.add(in.readRetainedSlice(in.readableBytes()));
return;
}
ByteBuf draft = ctx.alloc().buffer().writeBytes(in);
try {
CommonTransformer.transformServerbound(draft, user);
CommonTransformer.transformServerbound(draft, user, ignored -> CancelException.CACHED);
out.add(draft.retain());
} finally {
draft.release();

View File

@ -27,13 +27,14 @@ package com.github.creeper123123321.viafabric.handler.serverside;
import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.EncoderException;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.util.PipelineUtil;
public class FabricEncodeHandler extends MessageToByteEncoder {
import java.util.List;
public class FabricEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
private final UserConnection user;
public FabricEncodeHandler(UserConnection user) {
@ -41,9 +42,19 @@ public class FabricEncodeHandler extends MessageToByteEncoder {
}
@Override
protected void encode(final ChannelHandlerContext ctx, Object in, final ByteBuf out) throws Exception {
out.writeBytes((ByteBuf) in);
CommonTransformer.transformClientbound(out, user);
protected void encode(final ChannelHandlerContext ctx, ByteBuf in, final List<Object> out) throws Exception {
CommonTransformer.preClientbound(user);
if (!CommonTransformer.willTransformPacket(user)) {
out.add(in.readRetainedSlice(in.readableBytes()));
return;
}
ByteBuf draft = ctx.alloc().buffer().writeBytes(in);
try {
CommonTransformer.transformClientbound(draft, user, ignored -> CancelException.CACHED);
out.add(draft.retain());
} finally {
draft.release();
}
}
@Override

View File

@ -28,11 +28,7 @@ import com.github.creeper123123321.viafabric.handler.CommonTransformer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.util.PipelineUtil;
public class VRClientSideUserConnection extends UserConnection {
public VRClientSideUserConnection(Channel socketChannel) {
@ -42,37 +38,19 @@ public class VRClientSideUserConnection extends UserConnection {
@Override
public void sendRawPacket(final ByteBuf packet, boolean currentThread) {
ByteBuf copy = packet.alloc().buffer();
try {
Type.VAR_INT.write(copy, PacketWrapper.PASSTHROUGH_ID);
} catch (Exception e) {
e.printStackTrace();
}
copy.writeBytes(packet);
packet.release();
final Channel channel = this.getChannel();
Runnable act = () -> getChannel().pipeline().context(CommonTransformer.HANDLER_DECODER_NAME)
.fireChannelRead(packet);
if (currentThread) {
PipelineUtil.getPreviousContext(CommonTransformer.HANDLER_DECODER_NAME, channel.pipeline()).fireChannelRead(copy);
act.run();
} else {
channel.eventLoop().submit(() -> {
PipelineUtil.getPreviousContext(CommonTransformer.HANDLER_DECODER_NAME, channel.pipeline()).fireChannelRead(copy);
});
getChannel().eventLoop().execute(act);
}
}
@Override
public ChannelFuture sendRawPacketFuture(ByteBuf packet) {
ByteBuf copy = packet.alloc().buffer();
try {
Type.VAR_INT.write(copy, PacketWrapper.PASSTHROUGH_ID);
} catch (Exception e) {
e.printStackTrace();
}
copy.writeBytes(packet);
packet.release();
final Channel channel = this.getChannel();
PipelineUtil.getPreviousContext(CommonTransformer.HANDLER_DECODER_NAME, channel.pipeline()).fireChannelRead(copy);
return channel.newSucceededFuture();
getChannel().pipeline().context(CommonTransformer.HANDLER_DECODER_NAME).fireChannelRead(packet);
return getChannel().newSucceededFuture();
}
@Override