From d14b21af62b7cd2ffb612d0d53fe3c62e096fbaf Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Sat, 17 Nov 2018 15:13:24 +0100 Subject: [PATCH] change things --- .../java/gearth/extensions/Extension.java | 16 +-- .../java/gearth/extensions/ExtensionForm.java | 16 +-- .../java/gearth/extensions/IExtension.java | 19 ++++ .../extensions/extra/hashing/HashSupport.java | 102 +++++++++--------- 4 files changed, 88 insertions(+), 65 deletions(-) create mode 100644 G-Earth/src/main/java/gearth/extensions/IExtension.java diff --git a/G-Earth/src/main/java/gearth/extensions/Extension.java b/G-Earth/src/main/java/gearth/extensions/Extension.java index 5642b1e..f395dda 100644 --- a/G-Earth/src/main/java/gearth/extensions/Extension.java +++ b/G-Earth/src/main/java/gearth/extensions/Extension.java @@ -14,7 +14,7 @@ import java.util.Map; /** * Created by Jonas on 23/06/18. */ -public abstract class Extension { +public abstract class Extension implements IExtension{ public interface MessageListener { void act(HMessage message); @@ -232,7 +232,7 @@ public abstract class Extension { * @param packet packet to be sent * @return success or failure */ - protected boolean sendToClient(HPacket packet) { + public boolean sendToClient(HPacket packet) { return send(packet, HMessage.Side.TOCLIENT); } @@ -241,7 +241,7 @@ public abstract class Extension { * @param packet packet to be sent * @return success or failure */ - protected boolean sendToServer(HPacket packet) { + public boolean sendToServer(HPacket packet) { return send(packet, HMessage.Side.TOSERVER); } private boolean send(HPacket packet, HMessage.Side side) { @@ -263,7 +263,7 @@ public abstract class Extension { * @param headerId the packet header ID * @param messageListener the callback */ - protected void intercept(HMessage.Side side, int headerId, MessageListener messageListener) { + public void intercept(HMessage.Side side, int headerId, MessageListener messageListener) { Map> listeners = side == HMessage.Side.TOCLIENT ? incomingMessageListeners : @@ -284,7 +284,7 @@ public abstract class Extension { * @param side ToClient or ToServer * @param messageListener the callback */ - protected void intercept(HMessage.Side side, MessageListener messageListener) { + public void intercept(HMessage.Side side, MessageListener messageListener) { intercept(side, -1, messageListener); } @@ -294,7 +294,7 @@ public abstract class Extension { * @param flagRequestCallback callback * @return if the request was successful, will return false if another flagrequest is busy */ - protected boolean requestFlags(FlagsCheckListener flagRequestCallback) { + public boolean requestFlags(FlagsCheckListener flagRequestCallback) { if (this.flagRequestCallback != null) return false; this.flagRequestCallback = flagRequestCallback; return true; @@ -304,7 +304,7 @@ public abstract class Extension { * Write to the console in G-Earth * @param s the text to be written */ - protected void writeToConsole(String s) { + public void writeToConsole(String s) { HPacket packet = new HPacket(Extensions.INCOMING_MESSAGES_IDS.EXTENSIONCONSOLELOG); packet.appendString(s); try { @@ -372,7 +372,7 @@ public abstract class Extension { void act(String host, int port, String hotelversion); } private List onConnectionListeners = new ArrayList<>(); - protected void onConnect(OnConnectionListener listener){ + public void onConnect(OnConnectionListener listener){ onConnectionListeners.add(listener); } private void notifyConnectionListeners(String host, int port, String hotelversion) { diff --git a/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java b/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java index 6364517..4425ff7 100644 --- a/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java +++ b/G-Earth/src/main/java/gearth/extensions/ExtensionForm.java @@ -11,7 +11,7 @@ import java.util.concurrent.Semaphore; /** * Created by Jonas on 22/09/18. */ -public abstract class ExtensionForm { +public abstract class ExtensionForm implements IExtension{ volatile Extension extension; volatile Stage primaryStage; @@ -25,25 +25,25 @@ public abstract class ExtensionForm { public abstract ExtensionForm launchForm(Stage primaryStage) throws Exception; //wrap extension methods - protected boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback){ + public boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback){ return extension.requestFlags(flagRequestCallback); } - protected void writeToConsole(String s) { + public void writeToConsole(String s) { extension.writeToConsole(s); } - protected void intercept(HMessage.Side side, Extension.MessageListener messageListener) { + public void intercept(HMessage.Side side, Extension.MessageListener messageListener) { extension.intercept(side, messageListener); } - protected void intercept(HMessage.Side side, int headerId, Extension.MessageListener messageListener){ + public void intercept(HMessage.Side side, int headerId, Extension.MessageListener messageListener){ extension.intercept(side, headerId, messageListener); } - protected boolean sendToServer(HPacket packet){ + public boolean sendToServer(HPacket packet){ return extension.sendToServer(packet); } - protected boolean sendToClient(HPacket packet){ + public boolean sendToClient(HPacket packet){ return extension.sendToClient(packet); } - protected void onConnect(Extension.OnConnectionListener listener) { + public void onConnect(Extension.OnConnectionListener listener) { extension.onConnect(listener); } diff --git a/G-Earth/src/main/java/gearth/extensions/IExtension.java b/G-Earth/src/main/java/gearth/extensions/IExtension.java new file mode 100644 index 0000000..98ca6e2 --- /dev/null +++ b/G-Earth/src/main/java/gearth/extensions/IExtension.java @@ -0,0 +1,19 @@ +package gearth.extensions; + +import gearth.protocol.HMessage; +import gearth.protocol.HPacket; + +/** + * Created by Jeunez on 17/11/2018. + */ +public interface IExtension { + + boolean sendToClient(HPacket packet); + boolean sendToServer(HPacket packet); + void intercept(HMessage.Side side, int headerId, Extension.MessageListener messageListener); + void intercept(HMessage.Side side, Extension.MessageListener messageListener); + boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback); + void writeToConsole(String s); // not implemented in g-earth yet + void onConnect(Extension.OnConnectionListener listener); + +} diff --git a/G-Earth/src/main/java/gearth/extensions/extra/hashing/HashSupport.java b/G-Earth/src/main/java/gearth/extensions/extra/hashing/HashSupport.java index 905dd30..7e96ff8 100644 --- a/G-Earth/src/main/java/gearth/extensions/extra/hashing/HashSupport.java +++ b/G-Earth/src/main/java/gearth/extensions/extra/hashing/HashSupport.java @@ -1,6 +1,7 @@ package gearth.extensions.extra.hashing; import gearth.extensions.Extension; +import gearth.extensions.IExtension; import gearth.misc.harble_api.HarbleAPI; import gearth.protocol.HMessage; import gearth.protocol.HPacket; @@ -21,37 +22,31 @@ public class HashSupport { private HarbleAPI harbleAPI = new HarbleAPI(""); //empty private Map> incomingMessageListeners = new HashMap<>(); private Map> outgoingMessageListeners = new HashMap<>(); - private PacketSender toClientSender; - private PacketSender toServerSender; + private IExtension extension; - public interface OnConnectRegistration { - void onConnect(Extension.OnConnectionListener listener); - } - public interface InterceptRegistration { - void intercept(HMessage.Side side, Extension.MessageListener messageListener); - } - public interface PacketSender { - boolean send(HPacket packet); - } - - public HashSupport(OnConnectRegistration onConnectRegistration, InterceptRegistration interceptRegistration, PacketSender sendToClient, PacketSender sendToServer) { - toClientSender = sendToClient; - toServerSender = sendToServer; - onConnectRegistration.onConnect((host, port, hotelversion) -> { + public HashSupport(IExtension extension) { + this.extension = extension; + extension.onConnect((host, port, hotelversion) -> { // synchronized (lock) { harbleAPI = new HarbleAPI(hotelversion); // } }); - interceptRegistration.intercept(HMessage.Side.TOSERVER, message -> { + extension.intercept(HMessage.Side.TOSERVER, message -> { // synchronized (lock) { HarbleAPI.HarbleMessage haMessage = harbleAPI.getHarbleMessageFromHeaderId(HMessage.Side.TOSERVER, message.getPacket().headerId()); if (haMessage != null) { - String hash = haMessage.getHash(); - List listeners = outgoingMessageListeners.get(hash); - if (listeners != null) { - for (Extension.MessageListener listener : listeners) { + List listeners_hash = outgoingMessageListeners.get(haMessage.getHash()); + List listeners_name = outgoingMessageListeners.get(haMessage.getName()); + if (listeners_hash != null) { + for (Extension.MessageListener listener : listeners_hash) { + listener.act(message); + message.getPacket().resetReadIndex(); + } + } + if (listeners_name != null) { + for (Extension.MessageListener listener : listeners_name) { listener.act(message); message.getPacket().resetReadIndex(); } @@ -59,14 +54,21 @@ public class HashSupport { } // } }); - interceptRegistration.intercept(HMessage.Side.TOCLIENT, message -> { + extension.intercept(HMessage.Side.TOCLIENT, message -> { // synchronized (lock) { HarbleAPI.HarbleMessage haMessage = harbleAPI.getHarbleMessageFromHeaderId(HMessage.Side.TOCLIENT, message.getPacket().headerId()); if (haMessage != null) { String hash = haMessage.getHash(); - List listeners = incomingMessageListeners.get(hash); - if (listeners != null) { - for (Extension.MessageListener listener : listeners) { + List listeners_hash = incomingMessageListeners.get(haMessage.getHash()); + List listeners_name = incomingMessageListeners.get(haMessage.getName()); + if (listeners_hash != null) { + for (Extension.MessageListener listener : listeners_hash) { + listener.act(message); + message.getPacket().resetReadIndex(); + } + } + if (listeners_name != null) { + for (Extension.MessageListener listener : listeners_name) { listener.act(message); message.getPacket().resetReadIndex(); } @@ -76,29 +78,34 @@ public class HashSupport { }); } - public void intercept(HMessage.Side side, String hash, Extension.MessageListener messageListener) { + public void intercept(HMessage.Side side, String hashOrName, Extension.MessageListener messageListener) { Map> messageListeners = (side == HMessage.Side.TOSERVER ? outgoingMessageListeners : incomingMessageListeners); - messageListeners.computeIfAbsent(hash, k -> new ArrayList<>()); - messageListeners.get(hash).add(messageListener); + messageListeners.computeIfAbsent(hashOrName, k -> new ArrayList<>()); + messageListeners.get(hashOrName).add(messageListener); } - /** - * - * @return if no errors occurred (invalid hash/invalid parameters/connection error) - */ - public boolean sendToClient(String hash, Object... objects) { - List possibilities = harbleAPI.getHarbleMessagesFromHash(HMessage.Side.TOCLIENT, hash); - if (possibilities.size() == 0) return false; - int headerId = possibilities.get(0).getHeaderId(); + private boolean send(HMessage.Side side, String hashOrName, Object... objects) { + int headerId; + HarbleAPI.HarbleMessage fromname = harbleAPI.getHarbleMessageFromName(side, hashOrName); + if (fromname != null) { + headerId = fromname.getHeaderId(); + } + else { + List possibilities = harbleAPI.getHarbleMessagesFromHash(side, hashOrName); + if (possibilities.size() == 0) return false; + headerId = possibilities.get(0).getHeaderId(); + } try { HPacket packetToSend = new HPacket(headerId, objects); - return toClientSender.send(packetToSend); + return (side == HMessage.Side.TOCLIENT + ? extension.sendToClient(packetToSend) + : extension.sendToServer(packetToSend)); } catch (InvalidParameterException e) { return false; @@ -109,19 +116,16 @@ public class HashSupport { * * @return if no errors occurred (invalid hash/invalid parameters/connection error) */ - public boolean sendToServer(String hash, Object... objects) { - List possibilities = harbleAPI.getHarbleMessagesFromHash(HMessage.Side.TOSERVER, hash); - if (possibilities.size() == 0) return false; - int headerId = possibilities.get(0).getHeaderId(); + public boolean sendToClient(String hashOrName, Object... objects) { + return send(HMessage.Side.TOCLIENT, hashOrName, objects); + } - try { - HPacket packetToSend = new HPacket(headerId, objects); - - return toServerSender.send(packetToSend); - } - catch (InvalidParameterException e) { - return false; - } + /** + * + * @return if no errors occurred (invalid hash/invalid parameters/connection error) + */ + public boolean sendToServer(String hashOrName, Object... objects) { + return send(HMessage.Side.TOSERVER, hashOrName, objects); } }