Skip to content

Commit

Permalink
Data skipping in rewriteMetaVarInt and readSkipSlot, shortcut if oldI…
Browse files Browse the repository at this point in the history
…d equals newId.
  • Loading branch information
Janmm14 committed Nov 9, 2023
1 parent 7d24b0b commit 6dac2a5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
16 changes: 16 additions & 0 deletions protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ public static Either<String, BaseComponent> readEitherBaseComponent(ByteBuf buf,
return ( string ) ? Either.left( readString( buf ) ) : Either.right( readBaseComponent( buf, protocolVersion ) );
}

public static void skipString(ByteBuf buf)
{
skipString( buf, Short.MAX_VALUE );
}

public static void skipString(ByteBuf buf, int maxLen)
{
int len = readVarInt( buf );
if ( len > maxLen * 4 )
{
throw new OverflowPacketException( "Cannot receive string longer than " + maxLen * 4 + " (got " + len + " bytes)" );
}

buf.skipBytes( len );
}

public static BaseComponent readBaseComponent(ByteBuf buf, int protocolVersion)
{
if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 )
Expand Down
48 changes: 32 additions & 16 deletions proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ public void rewriteClientbound(PacketWrapper packet, int oldId, int newId, int p

protected static void rewriteInt(PacketWrapper wrapper, int oldId, int newId, int offset)
{
if ( oldId == newId )
{
return;
}
ByteBuf packet = wrapper.buf;
int readId = packet.getInt( offset );
if ( readId == oldId )
Expand All @@ -149,6 +153,10 @@ protected static void rewriteInt(PacketWrapper wrapper, int oldId, int newId, in
@SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
protected static void rewriteVarInt(PacketWrapper wrapper, int oldId, int newId, int offset)
{
if ( oldId == newId )
{
return;
}
ByteBuf packet = wrapper.buf;
// Need to rewrite the packet because VarInts are variable length
int readId = DefinedPacket.readVarInt( packet );
Expand All @@ -173,6 +181,10 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne

protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int newId, int metaIndex, int protocolVersion)
{
if ( oldId == newId )
{
return;
}
ByteBuf packet = wrapper.buf;
int readerIndex = packet.readerIndex();

Expand All @@ -187,7 +199,7 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne
case 5: // optional chat
if ( packet.readBoolean() )
{
DefinedPacket.readString( packet );
DefinedPacket.skipString( packet );
}
continue;
case 15: // particle
Expand All @@ -199,7 +211,7 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne
{
case 3: // minecraft:block
case 23: // minecraft:falling_dust
DefinedPacket.readVarInt( packet ); // block state
DefinedPacket.skipVarInt( packet ); // block state
break;
case 14: // minecraft:dust
packet.skipBytes( 16 ); // float, float, float, flat
Expand All @@ -214,7 +226,7 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne
{
case 3: // minecraft:block
case 20: // minecraft:falling_dust
DefinedPacket.readVarInt( packet ); // block state
DefinedPacket.skipVarInt( packet ); // block state
break;
case 11: // minecraft:dust
packet.skipBytes( 16 ); // float, float, float, flat
Expand Down Expand Up @@ -246,14 +258,14 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne
rewriteVarInt( wrapper, oldId, newId, position );
packet.readerIndex( position );
}
DefinedPacket.readVarInt( packet );
DefinedPacket.skipVarInt( packet );
break;
case 2:
packet.skipBytes( 4 ); // float
break;
case 3:
case 4:
DefinedPacket.readString( packet );
DefinedPacket.skipString( packet );
break;
case 5:
readSkipSlot( packet, protocolVersion );
Expand All @@ -274,7 +286,7 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne
}
break;
case 10:
DefinedPacket.readVarInt( packet );
DefinedPacket.skipVarInt( packet );
break;
case 11:
if ( packet.readBoolean() )
Expand All @@ -283,7 +295,7 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne
}
break;
case 12:
DefinedPacket.readVarInt( packet );
DefinedPacket.skipVarInt( packet );
break;
case 13:
Tag tag = NamedTag.read( new DataInputStream( new ByteBufInputStream( packet ) ) );
Expand All @@ -293,9 +305,9 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne
}
break;
case 15:
DefinedPacket.readVarInt( packet );
DefinedPacket.readVarInt( packet );
DefinedPacket.readVarInt( packet );
DefinedPacket.skipVarInt( packet );
DefinedPacket.skipVarInt( packet );
DefinedPacket.skipVarInt( packet );
break;
case 16:
if ( index == metaIndex )
Expand All @@ -304,10 +316,10 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne
rewriteVarInt( wrapper, oldId + 1, newId + 1, position );
packet.readerIndex( position );
}
DefinedPacket.readVarInt( packet );
DefinedPacket.skipVarInt( packet );
break;
case 17:
DefinedPacket.readVarInt( packet );
DefinedPacket.skipVarInt( packet );
break;
default:
throw new IllegalArgumentException( "Unknown meta type " + type );
Expand All @@ -323,7 +335,7 @@ private static void readSkipSlot(ByteBuf packet, int protocolVersion)
{
if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_13_2 )
{
DefinedPacket.readVarInt( packet );
DefinedPacket.skipVarInt( packet );
}
packet.skipBytes( ( protocolVersion >= ProtocolConstants.MINECRAFT_1_13 ) ? 1 : 3 ); // byte vs byte, short

Expand All @@ -344,17 +356,21 @@ private static void readSkipSlot(ByteBuf packet, int protocolVersion)
// Handles simple packets
private static void rewrite(PacketWrapper wrapper, int oldId, int newId, boolean[] ints, boolean[] varints)
{
if ( oldId == newId )
{
return;
}
ByteBuf packet = wrapper.buf;
int readerIndex = packet.readerIndex();
int packetId = DefinedPacket.readVarInt( packet );
int packetIdLength = packet.readerIndex() - readerIndex;
int readerIndexAfterId = packet.readerIndex();

if ( ints[packetId] )
{
rewriteInt( wrapper, oldId, newId, readerIndex + packetIdLength );
rewriteInt( wrapper, oldId, newId, readerIndexAfterId );
} else if ( varints[packetId] )
{
rewriteVarInt( wrapper, oldId, newId, readerIndex + packetIdLength );
rewriteVarInt( wrapper, oldId, newId, readerIndexAfterId );
}
packet.readerIndex( readerIndex );
}
Expand Down

0 comments on commit 6dac2a5

Please sign in to comment.