From b5deb920bdcf68dd7352c9c8d64cba75a1353c04 Mon Sep 17 00:00:00 2001 From: UnfamiliarLegacy <74633542+UnfamiliarLegacy@users.noreply.github.com> Date: Tue, 25 Jun 2024 06:08:17 +0200 Subject: [PATCH] Add extra ShockPacket constructors and fix more charset issues --- .../main/java/gearth/protocol/HPacket.java | 12 ++++--- .../shockwave/packets/ShockPacket.java | 35 +++++++++++++++++-- .../packets/ShockPacketIncoming.java | 19 +++++++++- .../packets/ShockPacketOutgoing.java | 24 +++++++++---- .../PacketStringUtils.java | 2 +- .../test/java/TestPacketStringEncoding.java | 31 ++++++++++++++-- 6 files changed, 105 insertions(+), 18 deletions(-) diff --git a/G-Earth/src/main/java/gearth/protocol/HPacket.java b/G-Earth/src/main/java/gearth/protocol/HPacket.java index a10f7db..cf1e989 100644 --- a/G-Earth/src/main/java/gearth/protocol/HPacket.java +++ b/G-Earth/src/main/java/gearth/protocol/HPacket.java @@ -49,8 +49,7 @@ public class HPacket implements StringifyAble { } public HPacket(int header) { - packetInBytes = new byte[]{0,0,0,2,0,0}; - replacePacketId((short)header); + initPacket(header); isEdited = false; } @@ -72,7 +71,7 @@ public class HPacket implements StringifyAble { } public HPacket(String identifier, HMessage.Direction direction) throws InvalidParameterException { - packetInBytes = new byte[]{0,0,0,2,-1,-1}; + initPacket(0); this.identifier = identifier; this.identifierDirection = direction; } @@ -83,6 +82,11 @@ public class HPacket implements StringifyAble { isEdited = false; } + protected void initPacket(int header) { + packetInBytes = new byte[]{0,0,0,2,0,0}; + replacePacketId((short)header); + } + public HPacketFormat getFormat() { return packetFormat; } @@ -650,7 +654,7 @@ public class HPacket implements StringifyAble { appendShort((Short)o); } else if (o instanceof String) { - appendString((String)o, StandardCharsets.UTF_8); + appendString((String)o, StandardCharsets.ISO_8859_1); } else if (o instanceof Boolean) { appendBoolean((Boolean) o); diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacket.java b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacket.java index 4b8e963..8b78596 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacket.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacket.java @@ -30,11 +30,40 @@ public abstract class ShockPacket extends HPacket { } public ShockPacket(HPacketFormat format, int header) { - super(Base64Encoding.encode(header, 2)); + super(header); packetFormat = format; readIndex = 2; } + public ShockPacket(HPacketFormat format, int header, byte[] bytes) { + super(header, bytes); + packetFormat = format; + readIndex = 2; + } + + public ShockPacket(HPacketFormat format, int header, Object... objects) throws InvalidParameterException { + super(header, objects); + packetFormat = format; + readIndex = 2; + } + + public ShockPacket(HPacketFormat format, String identifier, HMessage.Direction direction) throws InvalidParameterException { + super(identifier, direction); + packetFormat = format; + readIndex = 2; + } + + public ShockPacket(HPacketFormat format, String identifier, HMessage.Direction direction, Object... objects) throws InvalidParameterException { + super(identifier, direction, objects); + packetFormat = format; + readIndex = 2; + } + + @Override + protected void initPacket(int header) { + packetInBytes = Base64Encoding.encode(header, 2); + } + @Override public HPacket appendBoolean(boolean b) { throw new ShockPacketUnsupported(); @@ -102,12 +131,12 @@ public abstract class ShockPacket extends HPacket { @Override public HPacket appendObjects(Object... objects) { - throw new ShockPacketUnsupported(); + return super.appendObjects(objects); } @Override public HPacket appendObject(Object o) throws InvalidParameterException { - throw new ShockPacketUnsupported(); + return super.appendObject(o); } @Override diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketIncoming.java b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketIncoming.java index c06640d..3f25a6b 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketIncoming.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketIncoming.java @@ -2,11 +2,12 @@ package gearth.protocol.packethandler.shockwave.packets; import gearth.encoding.VL64Encoding; import gearth.misc.ArrayUtils; +import gearth.protocol.HMessage; import gearth.protocol.HPacket; import gearth.protocol.HPacketFormat; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; +import java.security.InvalidParameterException; import java.util.Arrays; // Server to Client @@ -27,6 +28,22 @@ public class ShockPacketIncoming extends ShockPacket { super(HPacketFormat.WEDGIE_INCOMING, header); } + public ShockPacketIncoming(int header, byte[] bytes) { + super(HPacketFormat.WEDGIE_INCOMING, header, bytes); + } + + public ShockPacketIncoming(int header, Object... objects) throws InvalidParameterException { + super(HPacketFormat.WEDGIE_INCOMING, header, objects); + } + + public ShockPacketIncoming(String identifier, HMessage.Direction direction) throws InvalidParameterException { + super(HPacketFormat.WEDGIE_INCOMING, identifier, direction); + } + + public ShockPacketIncoming(String identifier, HMessage.Direction direction, Object... objects) throws InvalidParameterException { + super(HPacketFormat.WEDGIE_INCOMING, identifier, direction, objects); + } + @Override public HPacket appendBoolean(boolean value) { isEdited = true; diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketOutgoing.java b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketOutgoing.java index 37b96aa..a588d5e 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketOutgoing.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketOutgoing.java @@ -2,11 +2,12 @@ package gearth.protocol.packethandler.shockwave.packets; import gearth.encoding.Base64Encoding; import gearth.encoding.VL64Encoding; +import gearth.protocol.HMessage; import gearth.protocol.HPacket; import gearth.protocol.HPacketFormat; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; +import java.security.InvalidParameterException; // Client to Server public class ShockPacketOutgoing extends ShockPacket { @@ -26,6 +27,22 @@ public class ShockPacketOutgoing extends ShockPacket { super(HPacketFormat.WEDGIE_OUTGOING, header); } + public ShockPacketOutgoing(int header, byte[] bytes) { + super(HPacketFormat.WEDGIE_INCOMING, header, bytes); + } + + public ShockPacketOutgoing(int header, Object... objects) throws InvalidParameterException { + super(HPacketFormat.WEDGIE_INCOMING, header, objects); + } + + public ShockPacketOutgoing(String identifier, HMessage.Direction direction) throws InvalidParameterException { + super(HPacketFormat.WEDGIE_INCOMING, identifier, direction); + } + + public ShockPacketOutgoing(String identifier, HMessage.Direction direction, Object... objects) throws InvalidParameterException { + super(HPacketFormat.WEDGIE_INCOMING, identifier, direction, objects); + } + @Override public HPacket appendBoolean(boolean value) { return appendInt(value ? 1 : 0); @@ -85,11 +102,6 @@ public class ShockPacketOutgoing extends ShockPacket { return value; } - @Override - public String readString() { - return this.readString(StandardCharsets.UTF_8); - } - @Override public String readString(Charset charset) { int length = readUshort(); diff --git a/G-Earth/src/main/java/gearth/services/packet_representation/PacketStringUtils.java b/G-Earth/src/main/java/gearth/services/packet_representation/PacketStringUtils.java index 90e5bb7..08bd4ff 100644 --- a/G-Earth/src/main/java/gearth/services/packet_representation/PacketStringUtils.java +++ b/G-Earth/src/main/java/gearth/services/packet_representation/PacketStringUtils.java @@ -266,7 +266,7 @@ public class PacketStringUtils { } else if (c == 'i') builder.append("{i:").append(prevInt = p.readInteger()).append('}'); else if (c == 's') builder.append("{s:\"").append( - p.readString(StandardCharsets.UTF_8) + p.readString() .replace("\\", "\\\\") // \ -> \\ .replace("\"", "\\\"") // " -> \" .replace("\r", "\\r") // CR -> \r diff --git a/G-Earth/src/test/java/TestPacketStringEncoding.java b/G-Earth/src/test/java/TestPacketStringEncoding.java index d041183..a6384ec 100644 --- a/G-Earth/src/test/java/TestPacketStringEncoding.java +++ b/G-Earth/src/test/java/TestPacketStringEncoding.java @@ -1,5 +1,6 @@ import gearth.protocol.HPacket; import gearth.protocol.HPacketFormat; +import gearth.protocol.packethandler.shockwave.packets.ShockPacketOutgoing; import gearth.services.packet_representation.PacketStringUtils; import org.bouncycastle.util.encoders.Hex; import org.junit.jupiter.api.Test; @@ -13,19 +14,22 @@ public class TestPacketStringEncoding { @Test public void testWrite() throws Exception { // {h:OUT_CHAT}{s:"ç"} 40 40 45 40 74 40 41 e7 + checkPacket(new ShockPacketOutgoing(OUT_CHAT, "ç"), "40744041e7"); checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@Aç", "40744041e7"); checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@A[231]", "40744041e7"); checkExpression(HPacketFormat.WEDGIE_OUTGOING, String.format("{h:%d}{s:\"ç\"}", OUT_CHAT), "40744041e7"); // {h:OUT_CHAT}{s:"ççç"} 40 40 47 40 74 40 43 e7 e7 e7 - checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@Aççç", "40744041e7e7e7"); - checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@A[231][231][231]", "40744041e7e7e7"); + checkPacket(new ShockPacketOutgoing(OUT_CHAT, "ççç"), "40744043e7e7e7"); + checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@Cççç", "40744043e7e7e7"); + checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@C[231][231][231]", "40744043e7e7e7"); checkExpression(HPacketFormat.WEDGIE_OUTGOING, String.format("{h:%d}{s:\"ççç\"}", OUT_CHAT), "40744043e7e7e7"); // {h:OUT_CHAT}{s:"çãâéèä"} 40 40 47 40 74 40 46 e7 e3 e2 e9 e8 e4 + checkPacket(new ShockPacketOutgoing(OUT_CHAT, "çãâéèä"), "40744046e7e3e2e9e8e4"); checkExpression(HPacketFormat.WEDGIE_OUTGOING, String.format("{h:%d}{s:\"çãâéèä\"}", OUT_CHAT), "40744046e7e3e2e9e8e4"); } @Test - public void testRead() throws Exception { + public void testReadIncoming() throws Exception { final String expectedString = "çãâéèä"; final String packetData = "40585045e7e3e2e9e8e40201"; @@ -36,6 +40,17 @@ public class TestPacketStringEncoding { assertEquals(expectedString, packet.readString()); } + @Test + public void testReadOutgoing() throws Exception { + final String expectedString = "çãâéèä"; + + final String packetData = "40744046e7e3e2e9e8e4"; + final HPacket packet = HPacketFormat.WEDGIE_OUTGOING.createPacket(Hex.decode(packetData)); + + assertEquals(OUT_CHAT, packet.headerId()); + assertEquals(expectedString, packet.readString()); + } + /** * @param format Packet format. * @param expression Handwritten expression. @@ -48,4 +63,14 @@ public class TestPacketStringEncoding { assertEquals(expectedHex, packetData); } + /** + * @param packet The packet. + * @param expectedHex Captured packets from the real client, or server. + */ + private void checkPacket(HPacket packet, String expectedHex) throws Exception { + final String packetData = Hex.toHexString(packet.toBytes()); + + assertEquals(expectedHex, packetData); + } + }