From 6621a468f12f30e124a9187553c7f097fa472281 Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Sat, 14 Aug 2021 03:30:38 +0200 Subject: [PATCH] remove PacketInfoSupport -> methods now built-in --- .../java/gearth/extensions/ExtensionBase.java | 73 ++++++++++-- .../java/gearth/extensions/IExtension.java | 1 + .../extensions/extra/tools/ChatConsole.java | 27 ++--- .../extra/tools/PacketInfoSupport.java | 107 ------------------ .../main/java/gearth/protocol/HPacket.java | 2 +- .../gearth/ui/injection/InjectedPackets.java | 4 +- 6 files changed, 80 insertions(+), 134 deletions(-) delete mode 100644 G-Earth/src/main/java/gearth/extensions/extra/tools/PacketInfoSupport.java diff --git a/G-Earth/src/main/java/gearth/extensions/ExtensionBase.java b/G-Earth/src/main/java/gearth/extensions/ExtensionBase.java index 0614606..83b9cc7 100644 --- a/G-Earth/src/main/java/gearth/extensions/ExtensionBase.java +++ b/G-Earth/src/main/java/gearth/extensions/ExtensionBase.java @@ -3,12 +3,18 @@ package gearth.extensions; import gearth.misc.listenerpattern.Observable; import gearth.protocol.HMessage; import gearth.protocol.HPacket; +import gearth.services.packet_info.PacketInfo; import gearth.services.packet_info.PacketInfoManager; +import org.reactfx.util.Lists; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.BiFunction; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.stream.Stream; public abstract class ExtensionBase extends IExtension { @@ -22,6 +28,10 @@ public abstract class ExtensionBase extends IExtension { protected final Map> incomingMessageListeners = new HashMap<>(); protected final Map> outgoingMessageListeners = new HashMap<>(); + protected final Map> hashOrNameIncomingListeners = new HashMap<>(); + protected final Map> hashOrNameOutgoingListeners = new HashMap<>(); + + volatile PacketInfoManager packetInfoManager = PacketInfoManager.EMPTY; /** @@ -46,6 +56,28 @@ public abstract class ExtensionBase extends IExtension { listeners.get(headerId).add(messageListener); } + /** + * Register a listener on a specific packet Type + * @param direction ToClient or ToServer + * @param hashOrName the packet hash or name + * @param messageListener the callback + */ + public void intercept(HMessage.Direction direction, String hashOrName, MessageListener messageListener) { + Map> listeners = + direction == HMessage.Direction.TOCLIENT ? + hashOrNameIncomingListeners : + hashOrNameOutgoingListeners; + + synchronized (listeners) { + if (!listeners.containsKey(hashOrName)) { + listeners.put(hashOrName, new ArrayList<>()); + } + } + + + listeners.get(hashOrName).add(messageListener); + } + /** * Register a listener on all packets * @param direction ToClient or ToServer @@ -84,20 +116,41 @@ public abstract class ExtensionBase extends IExtension { incomingMessageListeners : outgoingMessageListeners; + Map> hashOrNameListeners = + habboMessage.getDestination() == HMessage.Direction.TOCLIENT ? + hashOrNameIncomingListeners : + hashOrNameOutgoingListeners ; + List correctListeners = new ArrayList<>(); - synchronized (incomingMessageListeners) { - synchronized (outgoingMessageListeners) { - if (listeners.containsKey(-1)) { // registered on all packets - for (int i = listeners.get(-1).size() - 1; i >= 0; i--) { - correctListeners.add(listeners.get(-1).get(i)); - } + synchronized (listeners) { + if (listeners.containsKey(-1)) { // registered on all packets + for (int i = listeners.get(-1).size() - 1; i >= 0; i--) { + correctListeners.add(listeners.get(-1).get(i)); } + } - if (listeners.containsKey(habboPacket.headerId())) { - for (int i = listeners.get(habboPacket.headerId()).size() - 1; i >= 0; i--) { - correctListeners.add(listeners.get(habboPacket.headerId()).get(i)); - } + if (listeners.containsKey(habboPacket.headerId())) { + for (int i = listeners.get(habboPacket.headerId()).size() - 1; i >= 0; i--) { + correctListeners.add(listeners.get(habboPacket.headerId()).get(i)); + } + } + } + + synchronized (hashOrNameListeners) { + List packetInfos = packetInfoManager.getAllPacketInfoFromHeaderId(habboMessage.getDestination(), habboPacket.headerId()); + + List identifiers = new ArrayList<>(); + packetInfos.forEach(packetInfo -> { + String name = packetInfo.getName(); + String hash = packetInfo.getHash(); + if (name != null && hashOrNameListeners.containsKey(name)) identifiers.add(name); + if (hash != null && hashOrNameListeners.containsKey(hash)) identifiers.add(hash); + }); + + for (String identifier : identifiers) { + for (int i = hashOrNameListeners.get(identifier).size() - 1; i >= 0; i--) { + correctListeners.add(hashOrNameListeners.get(identifier).get(i)); } } } diff --git a/G-Earth/src/main/java/gearth/extensions/IExtension.java b/G-Earth/src/main/java/gearth/extensions/IExtension.java index e5f47c0..9f99396 100644 --- a/G-Earth/src/main/java/gearth/extensions/IExtension.java +++ b/G-Earth/src/main/java/gearth/extensions/IExtension.java @@ -9,6 +9,7 @@ public abstract class IExtension { public abstract boolean sendToClient(HPacket packet); public abstract boolean sendToServer(HPacket packet); public abstract void intercept(HMessage.Direction direction, int headerId, Extension.MessageListener messageListener); + public abstract void intercept(HMessage.Direction direction, String hashOrName, Extension.MessageListener messageListener); public abstract void intercept(HMessage.Direction direction, Extension.MessageListener messageListener); public abstract boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback); public abstract void writeToConsole(String colorClass, String s); diff --git a/G-Earth/src/main/java/gearth/extensions/extra/tools/ChatConsole.java b/G-Earth/src/main/java/gearth/extensions/extra/tools/ChatConsole.java index d40ed20..129c3fa 100644 --- a/G-Earth/src/main/java/gearth/extensions/extra/tools/ChatConsole.java +++ b/G-Earth/src/main/java/gearth/extensions/extra/tools/ChatConsole.java @@ -17,25 +17,25 @@ public class ChatConsole { private volatile int chatid; private volatile String name; - private volatile PacketInfoSupport packetInfoSupport; private volatile String infoMessage; private volatile boolean firstTime = true; private volatile Observable chatInputObservable = new Observable<>(); + private final IExtension extension; - public ChatConsole(final PacketInfoSupport packetInfoSupport, IExtension extension) { - this(packetInfoSupport, extension, null); + + public ChatConsole(IExtension extension) { + this(extension, null); } /** * infomessage will be used as response for :info and for initialize - * @param packetInfoSupport * @param extension * @param infoMessage */ - public ChatConsole(final PacketInfoSupport packetInfoSupport, IExtension extension, String infoMessage) { - this.packetInfoSupport = packetInfoSupport; + public ChatConsole(IExtension extension, String infoMessage) { + this.extension = extension; this.name = extension.getClass().getAnnotation(ExtensionInfo.class).Title(); chatid = (this.name.hashCode() % 300000000) + 300000000; this.infoMessage = infoMessage; @@ -56,7 +56,7 @@ public class ChatConsole { } }); - packetInfoSupport.intercept(HMessage.Direction.TOCLIENT, "FriendListFragment", hMessage -> { + extension.intercept(HMessage.Direction.TOCLIENT, "FriendListFragment", hMessage -> { if (doOncePerConnection[0]) { doOncePerConnection[0] = false; @@ -72,7 +72,7 @@ public class ChatConsole { } }); - packetInfoSupport.intercept(HMessage.Direction.TOSERVER, "SendMsg", hMessage -> { + extension.intercept(HMessage.Direction.TOSERVER, "SendMsg", hMessage -> { HPacket packet = hMessage.getPacket(); if (packet.readInteger() == chatid) { hMessage.setBlocked(true); @@ -88,11 +88,12 @@ public class ChatConsole { } private void createChat() { - packetInfoSupport.sendToClient("FriendListUpdate", + HPacket packet = new HPacket("FriendListUpdate", HMessage.Direction.TOCLIENT, 0, 1, 0, chatid, " [G-Earth] - " + name, 1, true, false, "ha-1015-64.hd-209-30.cc-260-64.ch-235-64.sh-305-64.lg-285-64", - 0, "", 0, true, false, true, "" - ); + 0, "", 0, true, false, true, ""); + + extension.sendToClient(packet); if (infoMessage != null) { writeOutput(infoMessage, false); @@ -101,10 +102,10 @@ public class ChatConsole { public void writeOutput(String string, boolean asInvite) { if (asInvite) { - packetInfoSupport.sendToClient("RoomInvite", chatid, string); + extension.sendToClient(new HPacket("RoomInvite", HMessage.Direction.TOCLIENT, chatid, string)); } else { - packetInfoSupport.sendToClient("NewConsole", chatid, string, 0, ""); + extension.sendToClient(new HPacket("NewConsole", HMessage.Direction.TOCLIENT, chatid, string, 0, "")); } } diff --git a/G-Earth/src/main/java/gearth/extensions/extra/tools/PacketInfoSupport.java b/G-Earth/src/main/java/gearth/extensions/extra/tools/PacketInfoSupport.java deleted file mode 100644 index 9b58cd3..0000000 --- a/G-Earth/src/main/java/gearth/extensions/extra/tools/PacketInfoSupport.java +++ /dev/null @@ -1,107 +0,0 @@ -package gearth.extensions.extra.tools; - -import gearth.extensions.Extension; -import gearth.extensions.IExtension; -import gearth.services.packet_info.PacketInfo; -import gearth.services.packet_info.PacketInfoManager; -import gearth.protocol.HMessage; -import gearth.protocol.HPacket; - -import java.security.InvalidParameterException; -import java.util.*; - -/** - * Created by Jonas on 10/11/2018. - */ -public class PacketInfoSupport { - - private final Object lock = new Object(); - - private Map> incomingMessageListeners = new HashMap<>(); - private Map> outgoingMessageListeners = new HashMap<>(); - - private IExtension extension; - - public PacketInfoSupport(IExtension extension) { - this.extension = extension; - - extension.intercept(HMessage.Direction.TOSERVER, message -> onReceivePacket(HMessage.Direction.TOSERVER, message)); - extension.intercept(HMessage.Direction.TOCLIENT, message -> onReceivePacket(HMessage.Direction.TOCLIENT, message)); - } - - private void onReceivePacket(HMessage.Direction direction, HMessage message) { - Set callbacks = new HashSet<>(); - Map> messageListeners = - (direction == HMessage.Direction.TOSERVER - ? outgoingMessageListeners - : incomingMessageListeners); - - List packetInfos = extension.getPacketInfoManager().getAllPacketInfoFromHeaderId(direction, message.getPacket().headerId()); - - for (PacketInfo packetInfo : packetInfos) { - List listeners_hash = messageListeners.get(packetInfo.getHash()); - List listeners_name = messageListeners.get(packetInfo.getName()); - if (listeners_hash != null) { - callbacks.addAll(listeners_hash); - } - if (listeners_name != null) { - callbacks.addAll(listeners_name); - } - } - - for (Extension.MessageListener listener : callbacks) { - listener.act(message); - message.getPacket().resetReadIndex(); - } - } - - public void intercept(HMessage.Direction direction, String hashOrName, Extension.MessageListener messageListener) { - Map> messageListeners = - (direction == HMessage.Direction.TOSERVER - ? outgoingMessageListeners - : incomingMessageListeners); - - messageListeners.computeIfAbsent(hashOrName, k -> new ArrayList<>()); - messageListeners.get(hashOrName).add(messageListener); - } - - private boolean send(HMessage.Direction direction, String hashOrName, Object... objects) { - int headerId; - PacketInfo fromname = extension.getPacketInfoManager().getPacketInfoFromName(direction, hashOrName); - if (fromname != null) { - headerId = fromname.getHeaderId(); - } - else { - PacketInfo fromHash = extension.getPacketInfoManager().getPacketInfoFromHash(direction, hashOrName); - if (fromHash == null) return false; - headerId = fromHash.getHeaderId(); - } - - try { - HPacket packetToSend = new HPacket(headerId, objects); - - return (direction == HMessage.Direction.TOCLIENT - ? extension.sendToClient(packetToSend) - : extension.sendToServer(packetToSend)); - } - catch (InvalidParameterException e) { - return false; - } - } - - /** - * - * @return if no errors occurred (invalid hash/invalid parameters/connection error) - */ - public boolean sendToClient(String hashOrName, Object... objects) { - return send(HMessage.Direction.TOCLIENT, hashOrName, objects); - } - - /** - * - * @return if no errors occurred (invalid hash/invalid parameters/connection error) - */ - public boolean sendToServer(String hashOrName, Object... objects) { - return send(HMessage.Direction.TOSERVER, hashOrName, objects); - } -} diff --git a/G-Earth/src/main/java/gearth/protocol/HPacket.java b/G-Earth/src/main/java/gearth/protocol/HPacket.java index c2fe924..55a4411 100644 --- a/G-Earth/src/main/java/gearth/protocol/HPacket.java +++ b/G-Earth/src/main/java/gearth/protocol/HPacket.java @@ -100,7 +100,7 @@ public class HPacket implements StringifyAble { this.identifierDirection = identifierDirection; } - public String getIdentifier() { + public String packetIncompleteIdentifier() { return identifier; } diff --git a/G-Earth/src/main/java/gearth/ui/injection/InjectedPackets.java b/G-Earth/src/main/java/gearth/ui/injection/InjectedPackets.java index df30cbc..44208ef 100644 --- a/G-Earth/src/main/java/gearth/ui/injection/InjectedPackets.java +++ b/G-Earth/src/main/java/gearth/ui/injection/InjectedPackets.java @@ -8,10 +8,8 @@ import gearth.services.packet_info.PacketInfoManager; import org.json.JSONObject; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.function.Predicate; public class InjectedPackets implements StringifyAble { @@ -27,7 +25,7 @@ public class InjectedPackets implements StringifyAble { HPacket packet = new HPacket(packetsAsString); String identifier = null; if (!packet.isPacketComplete()) { - identifier = packet.getIdentifier(); + identifier = packet.packetIncompleteIdentifier(); } else { Optional maybeInfo = packetInfoManager.getAllPacketInfoFromHeaderId(direction, packet.headerId())