Fix packet string encoding when using expressions

This commit is contained in:
UnfamiliarLegacy 2024-06-25 05:50:35 +02:00
parent 6cb98be64c
commit c6064b05ad
5 changed files with 54 additions and 18 deletions

View File

@ -87,7 +87,7 @@ public abstract class ShockPacket extends HPacket {
@Override @Override
public HPacket appendString(String s) { public HPacket appendString(String s) {
throw new ShockPacketUnsupported(); return appendString(s, StandardCharsets.ISO_8859_1);
} }
@Override @Override
@ -202,7 +202,7 @@ public abstract class ShockPacket extends HPacket {
@Override @Override
public String readString() { public String readString() {
throw new ShockPacketUnsupported(); return this.readString(StandardCharsets.ISO_8859_1);
} }
@Override @Override

View File

@ -53,11 +53,6 @@ public class ShockPacketIncoming extends ShockPacket {
return this; return this;
} }
@Override
public HPacket appendString(String value) {
return appendString(value, StandardCharsets.ISO_8859_1);
}
@Override @Override
public HPacket appendString(String value, Charset charset) { public HPacket appendString(String value, Charset charset) {
isEdited = true; isEdited = true;
@ -93,11 +88,6 @@ public class ShockPacketIncoming extends ShockPacket {
return value; return value;
} }
@Override
public String readString() {
return this.readString(StandardCharsets.UTF_8);
}
@Override @Override
public String readString(Charset charset) { public String readString(Charset charset) {
String result; String result;

View File

@ -50,11 +50,6 @@ public class ShockPacketOutgoing extends ShockPacket {
return this; return this;
} }
@Override
public HPacket appendString(String value) {
return appendString(value, StandardCharsets.ISO_8859_1);
}
@Override @Override
public HPacket appendString(String value, Charset charset) { public HPacket appendString(String value, Charset charset) {
isEdited = true; isEdited = true;

View File

@ -161,7 +161,7 @@ public class PacketStringUtils {
} }
actualString.append(match); actualString.append(match);
String latin = new String(actualString.toString().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1); String latin = new String(actualString.toString().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.ISO_8859_1);
HPacket temp = format.createPacket(0); HPacket temp = format.createPacket(0);
int sizeBefore = temp.getBytesLength(); int sizeBefore = temp.getBytesLength();

View File

@ -0,0 +1,51 @@
import gearth.protocol.HPacket;
import gearth.protocol.HPacketFormat;
import gearth.services.packet_representation.PacketStringUtils;
import org.bouncycastle.util.encoders.Hex;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestPacketStringEncoding {
private static final int OUT_CHAT = 52;
@Test
public void testWrite() throws Exception {
// {h:OUT_CHAT}{s:"ç"} 40 40 45 40 74 40 41 e7
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");
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
checkExpression(HPacketFormat.WEDGIE_OUTGOING, String.format("{h:%d}{s:\"çãâéèä\"}", OUT_CHAT), "40744046e7e3e2e9e8e4");
}
@Test
public void testRead() throws Exception {
final String expectedString = "çãâéèä";
final String packetData = "40585045e7e3e2e9e8e40201";
final HPacket packet = HPacketFormat.WEDGIE_INCOMING.createPacket(Hex.decode(packetData));
assertEquals(24, packet.headerId());
assertEquals(20, packet.readInteger());
assertEquals(expectedString, packet.readString());
}
/**
* @param format Packet format.
* @param expression Handwritten expression.
* @param expectedHex Captured packets from the real client, or server.
*/
private void checkExpression(HPacketFormat format, String expression, String expectedHex) throws Exception {
final HPacket packet = PacketStringUtils.fromString(expression, format);
final String packetData = Hex.toHexString(packet.toBytes());
assertEquals(expectedHex, packetData);
}
}