diff --git a/G-Earth/src/main/java/gearth/encoding/VL64Encoding.java b/G-Earth/src/main/java/gearth/encoding/VL64Encoding.java index 33d6415..bba0593 100644 --- a/G-Earth/src/main/java/gearth/encoding/VL64Encoding.java +++ b/G-Earth/src/main/java/gearth/encoding/VL64Encoding.java @@ -35,7 +35,10 @@ public class VL64Encoding { } public static int decode(byte[] bzData) { - int pos = 0; + return decode(bzData, 0); + } + + public static int decode(byte[] bzData, int pos) { int v = 0; boolean negative = (bzData[pos] & 4) == 4; diff --git a/G-Earth/src/main/java/gearth/extensions/Extension.java b/G-Earth/src/main/java/gearth/extensions/Extension.java index 55c80db..970a20b 100644 --- a/G-Earth/src/main/java/gearth/extensions/Extension.java +++ b/G-Earth/src/main/java/gearth/extensions/Extension.java @@ -1,6 +1,7 @@ package gearth.extensions; import gearth.misc.HostInfo; +import gearth.protocol.HPacketFormat; import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage; import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage.Incoming; import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage.Outgoing; @@ -174,12 +175,14 @@ public abstract class Extension extends ExtensionBase { } else if (packet.headerId() == Outgoing.PacketIntercept.HEADER_ID) { String stringifiedMessage = packet.readLongString(); - HMessage habboMessage = new HMessage(stringifiedMessage); + HPacketFormat packetFormat = HPacketFormat.fromId(packet.readInteger()); + HMessage habboMessage = new HMessage(packetFormat, stringifiedMessage); modifyMessage(habboMessage); HPacket response = new HPacket(Incoming.ManipulatedPacket.MANIPULATED_PACKET); response.appendLongString(habboMessage.stringify()); + response.appendInt(habboMessage.getPacket().getFormat().getId()); writeToStream(response.toBytes()); } @@ -237,6 +240,7 @@ public abstract class Extension extends ExtensionBase { packet1.appendByte(direction == HMessage.Direction.TOCLIENT ? (byte)0 : (byte)1); packet1.appendInt(packet.getBytesLength()); packet1.appendBytes(packet.toBytes()); + packet1.appendInt(packet.getFormat().getId()); try { writeToStream(packet1.toBytes()); return true; diff --git a/G-Earth/src/main/java/gearth/protocol/HMessage.java b/G-Earth/src/main/java/gearth/protocol/HMessage.java index b1e970f..3ccee81 100644 --- a/G-Earth/src/main/java/gearth/protocol/HMessage.java +++ b/G-Earth/src/main/java/gearth/protocol/HMessage.java @@ -15,7 +15,9 @@ public class HMessage implements StringifyAble { private boolean isBlocked; - public HMessage(String fromString) { + public HMessage(HPacketFormat format, String fromString) { + // A little bit hacky to get the correct packet class inside constructFromString. + this.hPacket = format.createPacket(0); constructFromString(fromString); } @@ -65,9 +67,8 @@ public class HMessage implements StringifyAble { this.isBlocked = parts[0].equals("1"); this.index = Integer.parseInt(parts[1]); this.direction = parts[2].equals("TOCLIENT") ? Direction.TOCLIENT : Direction.TOSERVER; - HPacket p = new HPacket(new byte[0]); - p.constructFromString(parts[3]); - this.hPacket = p; + this.hPacket = hPacket.getFormat().createPacket(0); + this.hPacket.constructFromString(parts[3]); } public void constructFromHMessage(HMessage message) { diff --git a/G-Earth/src/main/java/gearth/protocol/HPacket.java b/G-Earth/src/main/java/gearth/protocol/HPacket.java index 7d5a8ca..a10f7db 100644 --- a/G-Earth/src/main/java/gearth/protocol/HPacket.java +++ b/G-Earth/src/main/java/gearth/protocol/HPacket.java @@ -1,6 +1,5 @@ package gearth.protocol; -import gearth.extensions.parsers.HDirection; import gearth.misc.StringifyAble; import gearth.services.packet_info.PacketInfo; import gearth.services.packet_info.PacketInfoManager; @@ -17,6 +16,7 @@ import java.util.Optional; public class HPacket implements StringifyAble { protected boolean isEdited = false; + protected HPacketFormat packetFormat = HPacketFormat.EVA_WIRE; protected byte[] packetInBytes; protected int readIndex = 6; @@ -83,7 +83,9 @@ public class HPacket implements StringifyAble { isEdited = false; } - + public HPacketFormat getFormat() { + return packetFormat; + } public String toString() { return PacketStringUtils.toString(packetInBytes); diff --git a/G-Earth/src/main/java/gearth/protocol/HPacketFormat.java b/G-Earth/src/main/java/gearth/protocol/HPacketFormat.java index fb2201c..47338a0 100644 --- a/G-Earth/src/main/java/gearth/protocol/HPacketFormat.java +++ b/G-Earth/src/main/java/gearth/protocol/HPacketFormat.java @@ -1,15 +1,39 @@ package gearth.protocol; -import gearth.extensions.parsers.HDirection; import gearth.protocol.connection.HClient; import gearth.protocol.packethandler.shockwave.packets.ShockPacketIncoming; import gearth.protocol.packethandler.shockwave.packets.ShockPacketOutgoing; +import java.util.HashMap; +import java.util.Map; + public enum HPacketFormat { - EVA_WIRE, - WEDGIE_INCOMING, - WEDGIE_OUTGOING; + EVA_WIRE(0), + WEDGIE_INCOMING(1), + WEDGIE_OUTGOING(2); + + private static final Map ID_MAP = new HashMap<>(); + + static { + for (HPacketFormat format : values()) { + ID_MAP.put(format.id, format); + } + } + + public static HPacketFormat fromId(int id) { + return ID_MAP.get(id); + } + + private final int id; + + HPacketFormat(int id) { + this.id = id; + } + + public int getId() { + return id; + } public static HPacketFormat getFormat(HClient client, HMessage.Direction direction) { if (client != HClient.SHOCKWAVE) { diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/buffers/ShockwaveInBuffer.java b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/buffers/ShockwaveInBuffer.java index 3e9fbc0..041572d 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/buffers/ShockwaveInBuffer.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/buffers/ShockwaveInBuffer.java @@ -3,6 +3,7 @@ package gearth.protocol.packethandler.shockwave.buffers; import gearth.protocol.HPacket; import gearth.protocol.packethandler.ByteArrayUtils; import gearth.protocol.packethandler.shockwave.packets.ShockPacket; +import gearth.protocol.packethandler.shockwave.packets.ShockPacketIncoming; import java.util.ArrayList; import java.util.Arrays; @@ -31,7 +32,7 @@ public class ShockwaveInBuffer implements ShockwaveBuffer { for (int i = 0; i < buffer.length; i++) { if (buffer[i] == 1) { byte[] packetData = Arrays.copyOfRange(buffer, curPos, i); - packets.add(new ShockPacket(packetData)); + packets.add(new ShockPacketIncoming(packetData)); curPos = i + 1; } } diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/buffers/ShockwaveOutBuffer.java b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/buffers/ShockwaveOutBuffer.java index c4994c9..9bc5fd6 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/buffers/ShockwaveOutBuffer.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/buffers/ShockwaveOutBuffer.java @@ -4,6 +4,7 @@ import gearth.encoding.Base64Encoding; import gearth.protocol.HPacket; import gearth.protocol.packethandler.ByteArrayUtils; import gearth.protocol.packethandler.shockwave.packets.ShockPacket; +import gearth.protocol.packethandler.shockwave.packets.ShockPacketOutgoing; import java.util.ArrayList; import java.util.Arrays; @@ -39,7 +40,7 @@ public class ShockwaveOutBuffer implements ShockwaveBuffer { int endPos = length + PACKET_LENGTH_SIZE; byte[] packet = Arrays.copyOfRange(buffer, PACKET_LENGTH_SIZE, endPos); - out.add(new ShockPacket(packet)); + out.add(new ShockPacketOutgoing(packet)); buffer = Arrays.copyOfRange(buffer, endPos, buffer.length); } 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 eabb424..8fd613d 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 @@ -10,24 +10,28 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.InvalidParameterException; -public class ShockPacket extends HPacket { - public ShockPacket(byte[] packet) { +public abstract class ShockPacket extends HPacket { + public ShockPacket(HPacketFormat format, byte[] packet) { super(packet); + packetFormat = format; readIndex = 2; } - public ShockPacket(HPacket packet) { + public ShockPacket(HPacketFormat format, HPacket packet) { super(packet); + packetFormat = format; readIndex = 2; } - public ShockPacket(String packet, HPacketFormat format) { + public ShockPacket(HPacketFormat format, String packet) { super(packet, format); + packetFormat = format; readIndex = 2; } - public ShockPacket(int header) { + public ShockPacket(HPacketFormat format, int header) { super(Base64Encoding.encode(header, 2)); + packetFormat = format; readIndex = 2; } @@ -106,6 +110,136 @@ public class ShockPacket extends HPacket { throw new ShockPacketUnsupported(); } + @Override + public byte readByte() { + throw new ShockPacketUnsupported(); + } + + @Override + public byte readByte(int index) { + throw new ShockPacketUnsupported(); + } + + @Override + public short readShort() { + throw new ShockPacketUnsupported(); + } + + @Override + public short readShort(int index) { + throw new ShockPacketUnsupported(); + } + + @Override + public int readUshort() { + throw new ShockPacketUnsupported(); + } + + @Override + public int readUshort(int index) { + throw new ShockPacketUnsupported(); + } + + @Override + public int readInteger() { + throw new ShockPacketUnsupported(); + } + + @Override + public int readInteger(int index) { + throw new ShockPacketUnsupported(); + } + + @Override + public double readDouble() { + throw new ShockPacketUnsupported(); + } + + @Override + public double readDouble(int index) { + throw new ShockPacketUnsupported(); + } + + @Override + public float readFloat() { + throw new ShockPacketUnsupported(); + } + + @Override + public float readFloat(int index) { + throw new ShockPacketUnsupported(); + } + + @Override + public byte[] readBytes(int length) { + return super.readBytes(length); + } + + @Override + public byte[] readBytes(int length, int index) { + return super.readBytes(length, index); + } + + @Override + public long readLong() { + throw new ShockPacketUnsupported(); + } + + @Override + public long readLong(int index) { + throw new ShockPacketUnsupported(); + } + + @Override + public String readString(Charset charset) { + throw new ShockPacketUnsupported(); + } + + @Override + public String readString(int index, Charset charset) { + throw new ShockPacketUnsupported(); + } + + @Override + public String readString() { + throw new ShockPacketUnsupported(); + } + + @Override + public String readString(int index) { + throw new ShockPacketUnsupported(); + } + + @Override + public String readLongString(Charset charset) { + throw new ShockPacketUnsupported(); + } + + @Override + public String readLongString(int index, Charset charset) { + throw new ShockPacketUnsupported(); + } + + @Override + public String readLongString() { + throw new ShockPacketUnsupported(); + } + + @Override + public String readLongString(int index) { + throw new ShockPacketUnsupported(); + } + + @Override + public boolean readBoolean() { + throw new ShockPacketUnsupported(); + } + + @Override + public boolean readBoolean(int index) { + throw new ShockPacketUnsupported(); + } + @Override protected void replacePacketId(short headerId) { final byte[] header = Base64Encoding.encode(headerId, 2); @@ -128,8 +262,8 @@ public class ShockPacket extends HPacket { } @Override - public HPacket copy() { - return new ShockPacket(this); + public void resetReadIndex() { + setReadIndex(2); } @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 66c95ec..78e95d6 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 @@ -1,42 +1,48 @@ package gearth.protocol.packethandler.shockwave.packets; -import gearth.encoding.Base64Encoding; import gearth.encoding.VL64Encoding; import gearth.protocol.HPacket; import gearth.protocol.HPacketFormat; +import org.apache.commons.lang3.ArrayUtils; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.util.Arrays; // Server to Client public class ShockPacketIncoming extends ShockPacket { public ShockPacketIncoming(byte[] packet) { - super(packet); + super(HPacketFormat.WEDGIE_INCOMING, packet); } public ShockPacketIncoming(HPacket packet) { - super(packet); + super(HPacketFormat.WEDGIE_INCOMING, packet); } public ShockPacketIncoming(String packet) { - super(packet, HPacketFormat.WEDGIE_INCOMING); + super(HPacketFormat.WEDGIE_INCOMING, packet); } public ShockPacketIncoming(int header) { - super(header); + super(HPacketFormat.WEDGIE_INCOMING, header); } @Override - public HPacket appendBoolean(boolean b) { + public HPacket appendBoolean(boolean value) { isEdited = true; - appendBytes(VL64Encoding.encode(b ? 1 : 0)); + appendBytes(VL64Encoding.encode(value ? 1 : 0)); return this; } @Override - public HPacket appendUShort(int ushort) { + public HPacket appendShort(short value) { + return appendUShort(value); + } + + @Override + public HPacket appendUShort(int value) { isEdited = true; - appendBytes(VL64Encoding.encode(ushort)); + appendBytes(VL64Encoding.encode(value)); return this; } @@ -48,17 +54,69 @@ public class ShockPacketIncoming extends ShockPacket { } @Override - public HPacket appendString(String s) { - return appendString(s, StandardCharsets.ISO_8859_1); + public HPacket appendString(String value) { + return appendString(value, StandardCharsets.ISO_8859_1); } @Override - public HPacket appendString(String s, Charset charset) { + public HPacket appendString(String value, Charset charset) { isEdited = true; - final byte[] data = s.getBytes(charset); + final byte[] data = value.getBytes(charset); appendBytes(data); appendByte((byte) 2); return this; } + + @Override + public boolean readBoolean() { + return readInteger() == 1; + } + + @Override + public short readShort() { + return (short) readInteger(); + } + + @Override + public int readUshort() { + return this.readShort(); + } + + @Override + public int readInteger() { + int length = packetInBytes[readIndex] >> 3 & 7; + int value = VL64Encoding.decode(packetInBytes, readIndex); + + readIndex += length; + + return value; + } + + @Override + public String readString() { + return this.readString(StandardCharsets.UTF_8); + } + + @Override + public String readString(Charset charset) { + String result; + + int startPos = readIndex; + int endPos = ArrayUtils.indexOf(packetInBytes, (byte) 2, startPos); + if (endPos > 0) { + result = new String(Arrays.copyOfRange(packetInBytes, startPos, endPos), charset); + } else { + result = ""; + } + + readIndex = endPos + 1; + + return result; + } + + @Override + public HPacket copy() { + return new ShockPacketIncoming(this); + } } 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 c44e25b..ebf9465 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 @@ -11,30 +11,35 @@ import java.nio.charset.StandardCharsets; // Client to Server public class ShockPacketOutgoing extends ShockPacket { public ShockPacketOutgoing(byte[] packet) { - super(packet); + super(HPacketFormat.WEDGIE_OUTGOING, packet); } public ShockPacketOutgoing(HPacket packet) { - super(packet); + super(HPacketFormat.WEDGIE_OUTGOING, packet); } public ShockPacketOutgoing(String packet) { - super(packet, HPacketFormat.WEDGIE_OUTGOING); + super(HPacketFormat.WEDGIE_OUTGOING, packet); } public ShockPacketOutgoing(int header) { - super(header); + super(HPacketFormat.WEDGIE_OUTGOING, header); } @Override - public HPacket appendBoolean(boolean b) { - return appendInt(b ? 1 : 0); + public HPacket appendBoolean(boolean value) { + return appendInt(value ? 1 : 0); } @Override - public HPacket appendUShort(int ushort) { + public HPacket appendShort(short value) { + return appendUShort(value); + } + + @Override + public HPacket appendUShort(int value) { isEdited = true; - appendBytes(Base64Encoding.encode(ushort, 2)); + appendBytes(Base64Encoding.encode(value, 2)); return this; } @@ -46,17 +51,60 @@ public class ShockPacketOutgoing extends ShockPacket { } @Override - public HPacket appendString(String s) { - return appendString(s, StandardCharsets.ISO_8859_1); + public HPacket appendString(String value) { + return appendString(value, StandardCharsets.ISO_8859_1); } @Override - public HPacket appendString(String s, Charset charset) { + public HPacket appendString(String value, Charset charset) { isEdited = true; - final byte[] data = s.getBytes(charset); + final byte[] data = value.getBytes(charset); appendUShort(data.length); appendBytes(data); return this; } + + @Override + public boolean readBoolean() { + return readInteger() == 1; + } + + @Override + public short readShort() { + return (short) Base64Encoding.decode(readBytes(2)); + } + + @Override + public int readUshort() { + return this.readShort(); + } + + @Override + public int readInteger() { + int length = packetInBytes[readIndex] >> 3 & 7; + int value = VL64Encoding.decode(packetInBytes, readIndex); + + readIndex += length; + + return value; + } + + @Override + public String readString() { + return this.readString(StandardCharsets.UTF_8); + } + + @Override + public String readString(Charset charset) { + int length = readUshort(); + byte[] data = readBytes(length); + + return new String(data, charset); + } + + @Override + public HPacket copy() { + return new ShockPacketOutgoing(this); + } } diff --git a/G-Earth/src/main/java/gearth/services/extension_handler/ExtensionHandler.java b/G-Earth/src/main/java/gearth/services/extension_handler/ExtensionHandler.java index 1c42506..be8e43a 100644 --- a/G-Earth/src/main/java/gearth/services/extension_handler/ExtensionHandler.java +++ b/G-Earth/src/main/java/gearth/services/extension_handler/ExtensionHandler.java @@ -6,18 +6,17 @@ import gearth.misc.listenerpattern.Observable; import gearth.protocol.HConnection; import gearth.protocol.HMessage; import gearth.protocol.HPacket; +import gearth.protocol.HPacketFormat; import gearth.protocol.connection.HState; import gearth.services.extension_handler.extensions.ExtensionListener; import gearth.services.extension_handler.extensions.GEarthExtension; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducer; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerFactory; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerObserver; -import gearth.ui.themes.Theme; import javafx.util.Pair; import java.io.IOException; import java.util.*; -import java.util.function.Consumer; public class ExtensionHandler { @@ -244,7 +243,7 @@ public class ExtensionHandler { } @Override - protected void stringToPacketRequest(String string) { + protected void stringToPacketRequest(String string, HPacketFormat format) { HPacket packet = new HPacket(string); if (!packet.isPacketComplete()) packet.completePacket(hConnection.getPacketInfoManager()); extension.stringToPacketResponse(packet); diff --git a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/ExtensionListener.java b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/ExtensionListener.java index 59d5942..e362c76 100644 --- a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/ExtensionListener.java +++ b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/ExtensionListener.java @@ -2,6 +2,7 @@ package gearth.services.extension_handler.extensions; import gearth.protocol.HMessage; import gearth.protocol.HPacket; +import gearth.protocol.HPacketFormat; public abstract class ExtensionListener { @@ -13,6 +14,11 @@ public abstract class ExtensionListener { protected void hasClosed() {} protected void packetToStringRequest(HPacket packet) {} - protected void stringToPacketRequest(String string) {} + @Deprecated + protected void stringToPacketRequest(String string) { + // Kept for backwards compatibility with old extensions + stringToPacketRequest(string, HPacketFormat.EVA_WIRE); + } + protected void stringToPacketRequest(String string, HPacketFormat format) {} } diff --git a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/GEarthExtension.java b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/GEarthExtension.java index 7a18161..431bc84 100644 --- a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/GEarthExtension.java +++ b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/GEarthExtension.java @@ -3,6 +3,7 @@ package gearth.services.extension_handler.extensions; import gearth.misc.HostInfo; import gearth.misc.listenerpattern.Observable; import gearth.misc.listenerpattern.SynchronizedObservable; +import gearth.protocol.HPacketFormat; import gearth.services.packet_info.PacketInfoManager; import gearth.protocol.HMessage; import gearth.protocol.HPacket; @@ -92,8 +93,8 @@ public abstract class GEarthExtension { }); packet.setReadIndex(orgIndex); } - protected void stringToPacketRequest(String string) { - extensionObservable.fireEvent(l -> l.stringToPacketRequest(string)); + protected void stringToPacketRequest(String string, HPacketFormat format) { + extensionObservable.fireEvent(l -> l.stringToPacketRequest(string, format)); } // -------------------------------------------------------------------- diff --git a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionClient.java b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionClient.java index 7b1fa3d..70bb8df 100644 --- a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionClient.java +++ b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionClient.java @@ -3,6 +3,7 @@ package gearth.services.extension_handler.extensions.implementations.network; import gearth.misc.HostInfo; import gearth.protocol.HMessage; import gearth.protocol.HPacket; +import gearth.protocol.HPacketFormat; import gearth.protocol.connection.HClient; import gearth.services.extension_handler.extensions.ExtensionType; import gearth.services.extension_handler.extensions.GEarthExtension; @@ -76,11 +77,13 @@ public final class NetworkExtensionClient extends GEarthExtension { } else if (incoming instanceof Incoming.ExtensionConsoleLog) { log(((Incoming.ExtensionConsoleLog) incoming).getContents()); } else if (incoming instanceof Incoming.PacketToStringRequest) { - final HPacket hPacket = new HPacket(new byte[0]); + final Incoming.PacketToStringRequest incomingPacket = (Incoming.PacketToStringRequest) incoming; + final HPacket hPacket = incomingPacket.getFormat().createPacket(0); hPacket.constructFromString(((Incoming.PacketToStringRequest) incoming).getString()); packetToStringRequest(hPacket); } else if (incoming instanceof Incoming.StringToPacketRequest) { - stringToPacketRequest(((Incoming.StringToPacketRequest) incoming).getString()); + final Incoming.StringToPacketRequest incomingPacket = (Incoming.StringToPacketRequest) incoming; + stringToPacketRequest(incomingPacket.getString(), incomingPacket.getFormat()); } } catch (Exception e){ LOGGER.error("Failed to handle incoming message {} (channel={})", incoming, channel, e); @@ -138,7 +141,9 @@ public final class NetworkExtensionClient extends GEarthExtension { @Override public void packetIntercept(HMessage hMessage) { final String messageAsString = hMessage.stringify(); - channel.writeAndFlush(new Outgoing.PacketIntercept(messageAsString)); + final HPacketFormat packetFormat = hMessage.getPacket().getFormat(); + + channel.writeAndFlush(new Outgoing.PacketIntercept(messageAsString, packetFormat)); } @Override diff --git a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionCodec.java b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionCodec.java index 6fb1807..3271038 100644 --- a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionCodec.java +++ b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionCodec.java @@ -3,6 +3,7 @@ package gearth.services.extension_handler.extensions.implementations.network; import gearth.misc.HostInfo; import gearth.protocol.HMessage; import gearth.protocol.HPacket; +import gearth.protocol.HPacketFormat; import gearth.protocol.connection.HClient; import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage.Outgoing; import gearth.services.extension_handler.extensions.implementations.network.NetworkExtensionMessage.Incoming; @@ -141,10 +142,14 @@ public final class NetworkExtensionCodec { })); register(Incoming.ManipulatedPacket.MANIPULATED_PACKET, Incoming.ManipulatedPacket.class, - (message, hPacket) -> hPacket.appendLongString(message.gethMessage().stringify()), + (message, hPacket) -> { + hPacket.appendLongString(message.gethMessage().stringify()); + hPacket.appendInt(message.gethMessage().getPacket().getFormat().getId()); + }, (hPacket -> { - final String packetString = hPacket.readLongString(6); - final HMessage hMessage = new HMessage(packetString); + final String packetString = hPacket.readLongString(); + final HPacketFormat packetFormat = hPacket.isEOF() == 0 ? HPacketFormat.fromId(hPacket.readInteger()) : HPacketFormat.EVA_WIRE; + final HMessage hMessage = new HMessage(packetFormat, packetString); return new Incoming.ManipulatedPacket(hMessage); })); register(Incoming.SendMessage.HEADER_ID, @@ -153,12 +158,14 @@ public final class NetworkExtensionCodec { hPacket.appendByte((byte) (message.getDirection() == TOCLIENT ? 0 : 1)); hPacket.appendInt(message.getPacket().getBytesLength()); hPacket.appendBytes(message.getPacket().toBytes()); + hPacket.appendInt(message.getPacket().getFormat().getId()); }), (hPacket -> { final byte side = hPacket.readByte(); final int length = hPacket.readInteger(); final byte[] data = hPacket.readBytes(length); - final HPacket packet = new HPacket(data); + final HPacketFormat format = hPacket.isEOF() == 0 ? HPacketFormat.fromId(hPacket.readInteger()) : HPacketFormat.EVA_WIRE; + final HPacket packet = format.createPacket(data); return new Incoming.SendMessage(packet, side == 0 ? TOCLIENT : TOSERVER); })); register(Incoming.RequestFlags.HEADER_ID, @@ -172,12 +179,22 @@ public final class NetworkExtensionCodec { (hPacket -> new Incoming.ExtensionConsoleLog(hPacket.readString()))); register(Incoming.PacketToStringRequest.HEADER_ID, Incoming.PacketToStringRequest.class, - (message, hPacket) -> hPacket.appendLongString(message.getString()), - (hPacket -> new Incoming.PacketToStringRequest(hPacket.readLongString()))); + (message, hPacket) -> { + hPacket.appendLongString(message.getString()); + hPacket.appendInt(message.getFormat().getId()); + }, + (hPacket -> new Incoming.PacketToStringRequest( + hPacket.readLongString(), + hPacket.isEOF() == 0 ? HPacketFormat.fromId(hPacket.readInteger()) : HPacketFormat.EVA_WIRE))); register(Incoming.StringToPacketRequest.HEADER_ID, Incoming.StringToPacketRequest.class, - (message, hPacket) -> hPacket.appendLongString(message.getString(), StandardCharsets.UTF_8), - (hPacket -> new Incoming.StringToPacketRequest(hPacket.readLongString(StandardCharsets.UTF_8)))); + (message, hPacket) -> { + hPacket.appendLongString(message.getString(), StandardCharsets.UTF_8); + hPacket.appendInt(message.getFormat().getId()); + }, + (hPacket -> new Incoming.StringToPacketRequest( + hPacket.readLongString(StandardCharsets.UTF_8), + hPacket.isEOF() == 0 ? HPacketFormat.fromId(hPacket.readInteger()) : HPacketFormat.EVA_WIRE))); } private static void registerOutgoingMessages() { @@ -236,8 +253,13 @@ public final class NetworkExtensionCodec { (hPacket -> new Outgoing.OnDoubleClick())); register(Outgoing.PacketIntercept.HEADER_ID, Outgoing.PacketIntercept.class, - (message, hPacket) -> hPacket.appendLongString(message.getPacketString()), - (hPacket -> new Outgoing.PacketIntercept(hPacket.readLongString()))); + (message, hPacket) -> { + hPacket.appendLongString(message.getPacketString()); + hPacket.appendInt(message.getPacketFormat().getId()); + }, + (hPacket -> new Outgoing.PacketIntercept( + hPacket.readLongString(), + hPacket.isEOF() == 0 ? HPacketFormat.fromId(hPacket.readInteger()) : HPacketFormat.EVA_WIRE))); register(Outgoing.UpdateHostInfo.HEADER_ID, Outgoing.UpdateHostInfo.class, (message, hPacket) -> message.getHostInfo().appendToPacket(hPacket), diff --git a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionMessage.java b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionMessage.java index 460aca0..1cef058 100644 --- a/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionMessage.java +++ b/G-Earth/src/main/java/gearth/services/extension_handler/extensions/implementations/network/NetworkExtensionMessage.java @@ -3,6 +3,7 @@ package gearth.services.extension_handler.extensions.implementations.network; import gearth.misc.HostInfo; import gearth.protocol.HMessage; import gearth.protocol.HPacket; +import gearth.protocol.HPacketFormat; import gearth.protocol.connection.HClient; import gearth.services.packet_info.PacketInfoManager; @@ -140,14 +141,20 @@ public class NetworkExtensionMessage { public static final int HEADER_ID = 20; private final String string; + private final HPacketFormat format; - public PacketToStringRequest(String string) { + public PacketToStringRequest(String string, HPacketFormat format) { this.string = string; + this.format = format; } public String getString() { return string; } + + public HPacketFormat getFormat() { + return format; + } } /** @@ -160,14 +167,20 @@ public class NetworkExtensionMessage { public static final int HEADER_ID = 21; private final String string; + private final HPacketFormat format; - public StringToPacketRequest(String string) { + public StringToPacketRequest(String string, HPacketFormat format) { this.string = string; + this.format = format; } public String getString() { return string; } + + public HPacketFormat getFormat() { + return format; + } } /** @@ -239,14 +252,20 @@ public class NetworkExtensionMessage { public static final int HEADER_ID = 3; private final String packetString; + private final HPacketFormat packetFormat; - public PacketIntercept(String packetString) { + public PacketIntercept(String packetString, HPacketFormat packetFormat) { this.packetString = packetString; + this.packetFormat = packetFormat; } public String getPacketString() { return packetString; } + + public HPacketFormat getPacketFormat() { + return packetFormat; + } } /**