mirror of
https://github.com/sirjonasxx/G-Earth.git
synced 2024-11-27 02:40:51 +01:00
remove PacketInfoSupport -> methods now built-in
This commit is contained in:
parent
bdf8f81d73
commit
6621a468f1
@ -3,12 +3,18 @@ package gearth.extensions;
|
|||||||
import gearth.misc.listenerpattern.Observable;
|
import gearth.misc.listenerpattern.Observable;
|
||||||
import gearth.protocol.HMessage;
|
import gearth.protocol.HMessage;
|
||||||
import gearth.protocol.HPacket;
|
import gearth.protocol.HPacket;
|
||||||
|
import gearth.services.packet_info.PacketInfo;
|
||||||
import gearth.services.packet_info.PacketInfoManager;
|
import gearth.services.packet_info.PacketInfoManager;
|
||||||
|
import org.reactfx.util.Lists;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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 {
|
public abstract class ExtensionBase extends IExtension {
|
||||||
|
|
||||||
@ -22,6 +28,10 @@ public abstract class ExtensionBase extends IExtension {
|
|||||||
protected final Map<Integer, List<MessageListener>> incomingMessageListeners = new HashMap<>();
|
protected final Map<Integer, List<MessageListener>> incomingMessageListeners = new HashMap<>();
|
||||||
protected final Map<Integer, List<MessageListener>> outgoingMessageListeners = new HashMap<>();
|
protected final Map<Integer, List<MessageListener>> outgoingMessageListeners = new HashMap<>();
|
||||||
|
|
||||||
|
protected final Map<String, List<MessageListener>> hashOrNameIncomingListeners = new HashMap<>();
|
||||||
|
protected final Map<String, List<MessageListener>> hashOrNameOutgoingListeners = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
volatile PacketInfoManager packetInfoManager = PacketInfoManager.EMPTY;
|
volatile PacketInfoManager packetInfoManager = PacketInfoManager.EMPTY;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,6 +56,28 @@ public abstract class ExtensionBase extends IExtension {
|
|||||||
listeners.get(headerId).add(messageListener);
|
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<String, List<MessageListener>> 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
|
* Register a listener on all packets
|
||||||
* @param direction ToClient or ToServer
|
* @param direction ToClient or ToServer
|
||||||
@ -84,20 +116,41 @@ public abstract class ExtensionBase extends IExtension {
|
|||||||
incomingMessageListeners :
|
incomingMessageListeners :
|
||||||
outgoingMessageListeners;
|
outgoingMessageListeners;
|
||||||
|
|
||||||
|
Map<String, List<MessageListener>> hashOrNameListeners =
|
||||||
|
habboMessage.getDestination() == HMessage.Direction.TOCLIENT ?
|
||||||
|
hashOrNameIncomingListeners :
|
||||||
|
hashOrNameOutgoingListeners ;
|
||||||
|
|
||||||
List<MessageListener> correctListeners = new ArrayList<>();
|
List<MessageListener> correctListeners = new ArrayList<>();
|
||||||
|
|
||||||
synchronized (incomingMessageListeners) {
|
synchronized (listeners) {
|
||||||
synchronized (outgoingMessageListeners) {
|
if (listeners.containsKey(-1)) { // registered on all packets
|
||||||
if (listeners.containsKey(-1)) { // registered on all packets
|
for (int i = listeners.get(-1).size() - 1; i >= 0; i--) {
|
||||||
for (int i = listeners.get(-1).size() - 1; i >= 0; i--) {
|
correctListeners.add(listeners.get(-1).get(i));
|
||||||
correctListeners.add(listeners.get(-1).get(i));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (listeners.containsKey(habboPacket.headerId())) {
|
if (listeners.containsKey(habboPacket.headerId())) {
|
||||||
for (int i = listeners.get(habboPacket.headerId()).size() - 1; i >= 0; i--) {
|
for (int i = listeners.get(habboPacket.headerId()).size() - 1; i >= 0; i--) {
|
||||||
correctListeners.add(listeners.get(habboPacket.headerId()).get(i));
|
correctListeners.add(listeners.get(habboPacket.headerId()).get(i));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized (hashOrNameListeners) {
|
||||||
|
List<PacketInfo> packetInfos = packetInfoManager.getAllPacketInfoFromHeaderId(habboMessage.getDestination(), habboPacket.headerId());
|
||||||
|
|
||||||
|
List<String> 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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ public abstract class IExtension {
|
|||||||
public abstract boolean sendToClient(HPacket packet);
|
public abstract boolean sendToClient(HPacket packet);
|
||||||
public abstract boolean sendToServer(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, 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 void intercept(HMessage.Direction direction, Extension.MessageListener messageListener);
|
||||||
public abstract boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback);
|
public abstract boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback);
|
||||||
public abstract void writeToConsole(String colorClass, String s);
|
public abstract void writeToConsole(String colorClass, String s);
|
||||||
|
@ -17,25 +17,25 @@ public class ChatConsole {
|
|||||||
|
|
||||||
private volatile int chatid;
|
private volatile int chatid;
|
||||||
private volatile String name;
|
private volatile String name;
|
||||||
private volatile PacketInfoSupport packetInfoSupport;
|
|
||||||
private volatile String infoMessage;
|
private volatile String infoMessage;
|
||||||
|
|
||||||
private volatile boolean firstTime = true;
|
private volatile boolean firstTime = true;
|
||||||
private volatile Observable<ChatInputListener> chatInputObservable = new Observable<>();
|
private volatile Observable<ChatInputListener> 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
|
* infomessage will be used as response for :info and for initialize
|
||||||
* @param packetInfoSupport
|
|
||||||
* @param extension
|
* @param extension
|
||||||
* @param infoMessage
|
* @param infoMessage
|
||||||
*/
|
*/
|
||||||
public ChatConsole(final PacketInfoSupport packetInfoSupport, IExtension extension, String infoMessage) {
|
public ChatConsole(IExtension extension, String infoMessage) {
|
||||||
this.packetInfoSupport = packetInfoSupport;
|
this.extension = extension;
|
||||||
this.name = extension.getClass().getAnnotation(ExtensionInfo.class).Title();
|
this.name = extension.getClass().getAnnotation(ExtensionInfo.class).Title();
|
||||||
chatid = (this.name.hashCode() % 300000000) + 300000000;
|
chatid = (this.name.hashCode() % 300000000) + 300000000;
|
||||||
this.infoMessage = infoMessage;
|
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]) {
|
if (doOncePerConnection[0]) {
|
||||||
doOncePerConnection[0] = false;
|
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();
|
HPacket packet = hMessage.getPacket();
|
||||||
if (packet.readInteger() == chatid) {
|
if (packet.readInteger() == chatid) {
|
||||||
hMessage.setBlocked(true);
|
hMessage.setBlocked(true);
|
||||||
@ -88,11 +88,12 @@ public class ChatConsole {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void createChat() {
|
private void createChat() {
|
||||||
packetInfoSupport.sendToClient("FriendListUpdate",
|
HPacket packet = new HPacket("FriendListUpdate", HMessage.Direction.TOCLIENT,
|
||||||
0, 1, 0, chatid, " [G-Earth] - " + name,
|
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",
|
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) {
|
if (infoMessage != null) {
|
||||||
writeOutput(infoMessage, false);
|
writeOutput(infoMessage, false);
|
||||||
@ -101,10 +102,10 @@ public class ChatConsole {
|
|||||||
|
|
||||||
public void writeOutput(String string, boolean asInvite) {
|
public void writeOutput(String string, boolean asInvite) {
|
||||||
if (asInvite) {
|
if (asInvite) {
|
||||||
packetInfoSupport.sendToClient("RoomInvite", chatid, string);
|
extension.sendToClient(new HPacket("RoomInvite", HMessage.Direction.TOCLIENT, chatid, string));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
packetInfoSupport.sendToClient("NewConsole", chatid, string, 0, "");
|
extension.sendToClient(new HPacket("NewConsole", HMessage.Direction.TOCLIENT, chatid, string, 0, ""));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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<String, List<Extension.MessageListener>> incomingMessageListeners = new HashMap<>();
|
|
||||||
private Map<String, List<Extension.MessageListener>> 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<Extension.MessageListener> callbacks = new HashSet<>();
|
|
||||||
Map<String, List<Extension.MessageListener>> messageListeners =
|
|
||||||
(direction == HMessage.Direction.TOSERVER
|
|
||||||
? outgoingMessageListeners
|
|
||||||
: incomingMessageListeners);
|
|
||||||
|
|
||||||
List<PacketInfo> packetInfos = extension.getPacketInfoManager().getAllPacketInfoFromHeaderId(direction, message.getPacket().headerId());
|
|
||||||
|
|
||||||
for (PacketInfo packetInfo : packetInfos) {
|
|
||||||
List<Extension.MessageListener> listeners_hash = messageListeners.get(packetInfo.getHash());
|
|
||||||
List<Extension.MessageListener> 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<String, List<Extension.MessageListener>> 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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -100,7 +100,7 @@ public class HPacket implements StringifyAble {
|
|||||||
this.identifierDirection = identifierDirection;
|
this.identifierDirection = identifierDirection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getIdentifier() {
|
public String packetIncompleteIdentifier() {
|
||||||
return identifier;
|
return identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,10 +8,8 @@ import gearth.services.packet_info.PacketInfoManager;
|
|||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.function.Predicate;
|
|
||||||
|
|
||||||
public class InjectedPackets implements StringifyAble {
|
public class InjectedPackets implements StringifyAble {
|
||||||
|
|
||||||
@ -27,7 +25,7 @@ public class InjectedPackets implements StringifyAble {
|
|||||||
HPacket packet = new HPacket(packetsAsString);
|
HPacket packet = new HPacket(packetsAsString);
|
||||||
String identifier = null;
|
String identifier = null;
|
||||||
if (!packet.isPacketComplete()) {
|
if (!packet.isPacketComplete()) {
|
||||||
identifier = packet.getIdentifier();
|
identifier = packet.packetIncompleteIdentifier();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Optional<PacketInfo> maybeInfo = packetInfoManager.getAllPacketInfoFromHeaderId(direction, packet.headerId())
|
Optional<PacketInfo> maybeInfo = packetInfoManager.getAllPacketInfoFromHeaderId(direction, packet.headerId())
|
||||||
|
Loading…
Reference in New Issue
Block a user