From d2a919fc064f074ed90ec904b5a5bd97e492502e Mon Sep 17 00:00:00 2001 From: md_5 Date: Tue, 12 Feb 2013 12:05:06 +1100 Subject: [PATCH] Rework packet reading a little in more preparation for Forge. --- .../net/md_5/bungee/packet/DefinedPacket.java | 97 +++++++++++++++++-- .../net/md_5/bungee/packet/Packet1Login.java | 13 ++- 2 files changed, 101 insertions(+), 9 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/packet/DefinedPacket.java b/proxy/src/main/java/net/md_5/bungee/packet/DefinedPacket.java index 3051b31d..27843c4f 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/DefinedPacket.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/DefinedPacket.java @@ -1,10 +1,11 @@ package net.md_5.bungee.packet; -import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; -import java.io.DataInput; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; import java.io.DataOutput; +import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import lombok.Delegate; @@ -15,7 +16,7 @@ import net.md_5.bungee.Util; * subclasses can read and write to the backing byte array which can be * retrieved via the {@link #getPacket()} method. */ -public abstract class DefinedPacket implements DataInput, DataOutput +public abstract class DefinedPacket implements DataOutput { private interface Overriden @@ -25,8 +26,8 @@ public abstract class DefinedPacket implements DataInput, DataOutput void writeUTF(String s); } - @Delegate(excludes = Overriden.class) - private ByteArrayDataInput in; + private ByteArrayInputStream bin; + private DataInputStream input; @Delegate(excludes = Overriden.class) private ByteArrayDataOutput out; /** @@ -40,7 +41,8 @@ public abstract class DefinedPacket implements DataInput, DataOutput public DefinedPacket(int id, byte[] buf) { - in = ByteStreams.newDataInput( buf ); + bin = new ByteArrayInputStream( buf ); + input = new DataInputStream( bin ); if ( readUnsignedByte() != id ) { throw new IllegalArgumentException( "Wasn't expecting packet id " + Util.hex( id ) ); @@ -74,7 +76,6 @@ public abstract class DefinedPacket implements DataInput, DataOutput writeChars( s ); } - @Override public String readUTF() { short len = readShort(); @@ -100,6 +101,88 @@ public abstract class DefinedPacket implements DataInput, DataOutput return ret; } + public final int available() + { + return bin.available(); + } + + public final void readFully(byte b[]) + { + try + { + input.readFully( b ); + } catch ( IOException e ) + { + throw new IllegalStateException( e ); + } + } + + public final boolean readBoolean() + { + try + { + return input.readBoolean(); + } catch ( IOException e ) + { + throw new IllegalStateException( e ); + } + } + + public final byte readByte() + { + try + { + return input.readByte(); + } catch ( IOException e ) + { + throw new IllegalStateException( e ); + } + } + + public final int readUnsignedByte() + { + try + { + return input.readUnsignedByte(); + } catch ( IOException e ) + { + throw new IllegalStateException( e ); + } + } + + public final short readShort() + { + try + { + return input.readShort(); + } catch ( IOException e ) + { + throw new IllegalStateException( e ); + } + } + + public final char readChar() + { + try + { + return input.readChar(); + } catch ( IOException e ) + { + throw new IllegalStateException( e ); + } + } + + public final int readInt() + { + try + { + return input.readInt(); + } catch ( IOException e ) + { + throw new IllegalStateException( e ); + } + } + @Override public abstract boolean equals(Object obj); diff --git a/proxy/src/main/java/net/md_5/bungee/packet/Packet1Login.java b/proxy/src/main/java/net/md_5/bungee/packet/Packet1Login.java index 7f8264e9..96285c8a 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/Packet1Login.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/Packet1Login.java @@ -11,7 +11,7 @@ public class Packet1Login extends DefinedPacket public int entityId; public String levelType; public byte gameMode; - public byte dimension; + public int dimension; public byte difficulty; public byte unused; public byte maxPlayers; @@ -34,7 +34,16 @@ public class Packet1Login extends DefinedPacket this.entityId = readInt(); this.levelType = readUTF(); this.gameMode = readByte(); - this.dimension = readByte(); + if ( available() == 4 ) + { + this.dimension = readByte(); + } else if ( available() == 7 ) + { + this.dimension = readInt(); + } else + { + throw new IllegalStateException(); + } this.difficulty = readByte(); this.unused = readByte(); this.maxPlayers = readByte();