diff --git a/G-Earth/src/main/java/gearth/misc/packetrepresentation/PacketStringUtils.java b/G-Earth/src/main/java/gearth/misc/packetrepresentation/PacketStringUtils.java index adbafe8a..a71b9b73 100644 --- a/G-Earth/src/main/java/gearth/misc/packetrepresentation/PacketStringUtils.java +++ b/G-Earth/src/main/java/gearth/misc/packetrepresentation/PacketStringUtils.java @@ -208,6 +208,7 @@ else if (c == 's') builder.append("{s:\"").append( .replace("\r", "\\r") // CR -> \r ).append("\"}"); else if (c == 'd') builder.append("{d:").append(p.readDouble()).append('}'); + else if (c == 'f') builder.append("{f:").append(p.readFloat()).append('}'); else if (c == 'b') builder.append("{b:").append(p.readByte()).append('}'); else if (c == 'B') builder.append("{b:").append(p.readBoolean()).append('}'); else if (c == 'l') builder.append("{l:").append(p.readLong()).append('}'); diff --git a/G-Earth/src/main/java/gearth/protocol/HPacket.java b/G-Earth/src/main/java/gearth/protocol/HPacket.java index 650685c2..16b32058 100644 --- a/G-Earth/src/main/java/gearth/protocol/HPacket.java +++ b/G-Earth/src/main/java/gearth/protocol/HPacket.java @@ -149,6 +149,15 @@ public double readDouble(int index) { return java.nio.ByteBuffer.wrap(packetInBytes).getDouble(index); } + public float readFloat(){ + float result = readFloat(readIndex); + readIndex += 4; + return result; + } + public float readFloat(int index) { + return java.nio.ByteBuffer.wrap(packetInBytes).getFloat(index); + } + public int length() { return readInteger(0); } @@ -263,6 +272,14 @@ public HPacket replaceDouble(int index, double d) { } return this; } + public HPacket replaceFloat(int index, float f) { + isEdited = true; + ByteBuffer b = ByteBuffer.allocate(4).putFloat(f); + for (int j = 0; j < 4; j++) { + packetInBytes[index + j] = b.array()[j]; + } + return this; + } public HPacket replaceByte(int index, byte b) { isEdited = true; packetInBytes[index] = b; @@ -443,6 +460,16 @@ public HPacket appendDouble(double d) { fixLength(); return this; } + public HPacket appendFloat(float f) { + isEdited = true; + packetInBytes = Arrays.copyOf(packetInBytes, packetInBytes.length + 4); + ByteBuffer byteBuffer = ByteBuffer.allocate(4).putFloat(f); + for (int j = 0; j < 4; j++) { + packetInBytes[packetInBytes.length - 4 + j] = byteBuffer.array()[j]; + } + fixLength(); + return this; + } public HPacket appendByte(byte b) { isEdited = true; packetInBytes = Arrays.copyOf(packetInBytes, packetInBytes.length + 1); @@ -526,6 +553,12 @@ else if (o instanceof Boolean) { else if (o instanceof Long) { appendLong((Long) o); } + else if (o instanceof Float) { + appendFloat((Float) o); + } + else if (o instanceof Double) { + appendDouble((Double) o); + } else { throw new InvalidParameterException(); } @@ -608,16 +641,4 @@ public boolean equals(Object object) { HPacket packet2 = (HPacket) object; return Arrays.equals(packetInBytes, packet2.packetInBytes) && (isEdited == packet2.isEdited); } - - public static void main(String[] args) { - HPacket packet = new HPacket("{l}{h:4564}{i:3}{i:0}{s:\"hi\"}{i:0}{i:1}{s:\"how\"}{i:3}{b:1}{b:2}{b:3}{i:2}{s:\"r u\"}{i:1}{b:120}{i:2}{b:true}{d:1.4}"); - - String str = PacketStringUtils.toExpressionFromGivenStructure(packet, "i(isi(b))iBd"); - - HPacket packetverify = new HPacket(str); - - System.out.println(str); - System.out.println(packetverify.toString().equals(packet.toString())); - - } }