From c130058694e706ae855a3f02820cd5ca9f11c80a Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Sat, 17 Nov 2018 19:08:54 +0100 Subject: [PATCH] hashing in Logger & example extension --- .../adminonconnect/AdminOnConnect.java | 2 +- .../{SpeechColorizer => HappySpeech}/pom.xml | 4 +- .../extensions/happyspeech/HappySpeech.java} | 40 ++++------ G-Earth/pom.xml | 3 +- .../java/gearth/extensions/Extension.java | 4 +- .../extensions/ExtensionFormLauncher.java | 2 +- .../gearth/misc/harble_api/HarbleAPI.java | 78 ++++++++++++++----- .../misc/harble_api/HarbleAPIFetcher.java | 21 +++-- .../java/gearth/ui/extensions/Extensions.java | 2 + .../main/java/gearth/ui/logger/Logger.java | 1 - .../loggerdisplays/PacketLoggerFactory.java | 1 + .../loggerdisplays/uilogger/Element.java | 14 ++++ .../{ => uilogger}/UiLogger.java | 4 +- .../uilogger}/UiLoggerController.java | 46 ++++++++--- .../gearth/ui/logger/uilogger/UiLogger.fxml | 19 ++++- .../gearth/ui/logger/uilogger/logger.css | 4 + 16 files changed, 173 insertions(+), 72 deletions(-) rename Extensions/{SpeechColorizer => HappySpeech}/pom.xml (94%) rename Extensions/{SpeechColorizer/src/main/java/extensions/speechcolorizer/SpeechColorizer.java => HappySpeech/src/main/java/extensions/happyspeech/HappySpeech.java} (51%) create mode 100644 G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/uilogger/Element.java rename G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/{ => uilogger}/UiLogger.java (96%) rename G-Earth/src/main/java/gearth/ui/{ => logger/loggerdisplays/uilogger}/UiLoggerController.java (80%) diff --git a/Extensions/AdminOnConnect/src/main/java/extensions/adminonconnect/AdminOnConnect.java b/Extensions/AdminOnConnect/src/main/java/extensions/adminonconnect/AdminOnConnect.java index caab840..6f53f3e 100644 --- a/Extensions/AdminOnConnect/src/main/java/extensions/adminonconnect/AdminOnConnect.java +++ b/Extensions/AdminOnConnect/src/main/java/extensions/adminonconnect/AdminOnConnect.java @@ -26,7 +26,7 @@ public class AdminOnConnect extends Extension { private boolean done = true; - protected void init() { + protected void initExtension() { intercept(HMessage.Side.TOCLIENT, message -> { if (!done) { HPacket packet = message.getPacket(); diff --git a/Extensions/SpeechColorizer/pom.xml b/Extensions/HappySpeech/pom.xml similarity index 94% rename from Extensions/SpeechColorizer/pom.xml rename to Extensions/HappySpeech/pom.xml index 405e87c..822ef75 100644 --- a/Extensions/SpeechColorizer/pom.xml +++ b/Extensions/HappySpeech/pom.xml @@ -40,7 +40,7 @@ true true - extensions.speechcolorizer.SpeechColorizer + extensions.happyspeech.HappySpeech false lib/ true @@ -65,7 +65,7 @@ ${project.build.directory}/bin - extensions.speechcolorizer.SpeechColorizer + extensions.happyspeech.HappySpeech diff --git a/Extensions/SpeechColorizer/src/main/java/extensions/speechcolorizer/SpeechColorizer.java b/Extensions/HappySpeech/src/main/java/extensions/happyspeech/HappySpeech.java similarity index 51% rename from Extensions/SpeechColorizer/src/main/java/extensions/speechcolorizer/SpeechColorizer.java rename to Extensions/HappySpeech/src/main/java/extensions/happyspeech/HappySpeech.java index ce9f769..a5cd236 100644 --- a/Extensions/SpeechColorizer/src/main/java/extensions/speechcolorizer/SpeechColorizer.java +++ b/Extensions/HappySpeech/src/main/java/extensions/happyspeech/HappySpeech.java @@ -1,7 +1,8 @@ -package extensions.speechcolorizer; +package extensions.happyspeech; import gearth.extensions.Extension; import gearth.extensions.ExtensionInfo; +import gearth.extensions.extra.hashing.HashSupport; import gearth.protocol.HMessage; import gearth.protocol.HPacket; @@ -16,50 +17,43 @@ import java.util.Random; */ @ExtensionInfo( - Title = "Colorize me!", - Description = "Because we like to be weird", + Title = "HappySpeech", + Description = "Example extension for hashSupport", Version = "1.0", Author = "sirjonasxx" ) -public class SpeechColorizer extends Extension { +public class HappySpeech extends Extension { public static void main(String[] args) { - new SpeechColorizer(args).run(); + new HappySpeech(args).run(); } - private SpeechColorizer(String[] args) { + private HappySpeech(String[] args) { super(args); } - private static final int SPEECH_ID = 3373; private static final String[] COLORS = {"red", "blue", "cyan", "green", "purple"}; private static final Random r = new Random(); + private HashSupport hashSupport; + @Override - protected void init() { - intercept(HMessage.Side.TOSERVER, SPEECH_ID, this::onSendMessage); - System.out.println("test"); + protected void initExtension() { + hashSupport = new HashSupport(this); + hashSupport.intercept(HMessage.Side.TOSERVER, "RoomUserShout", this::onSendMessage); + hashSupport.intercept(HMessage.Side.TOSERVER, "RoomUserTalk", this::onSendMessage); } private void onSendMessage(HMessage message) { HPacket packet = message.getPacket(); String speechtext = packet.readString(); - System.out.println("you said: " + speechtext); message.setBlocked(speechtext.equals("blocked")); - packet.replaceString(6, "@" + COLORS[r.nextInt(COLORS.length)] + "@" + speechtext); + packet.replaceString(6, "@" + COLORS[r.nextInt(COLORS.length)] + "@" + speechtext + " :)"); } - protected String getTitle() { - return "Colorize me!"; - } - protected String getDescription() { - return "Because we like to be weird"; - } - protected String getVersion() { - return "1.0"; - } - protected String getAuthor() { - return "sirjonasxx"; + protected void onClick() { + //{l}{u:1047}{i:0}{s:text}{i:0}{i:0}{i:0}{i:0} + hashSupport.sendToClient("RoomUserTalk", 0, "You clicked on this extension!", 0, 0, 0, 0); } } diff --git a/G-Earth/pom.xml b/G-Earth/pom.xml index 23961fe..5ed3e46 100644 --- a/G-Earth/pom.xml +++ b/G-Earth/pom.xml @@ -60,7 +60,8 @@ jar-with-dependencies - G-Earth-${project.version} + + G-Earth false diff --git a/G-Earth/src/main/java/gearth/extensions/Extension.java b/G-Earth/src/main/java/gearth/extensions/Extension.java index f395dda..a74b280 100644 --- a/G-Earth/src/main/java/gearth/extensions/Extension.java +++ b/G-Earth/src/main/java/gearth/extensions/Extension.java @@ -156,7 +156,7 @@ public abstract class Extension implements IExtension{ flagRequestCallback = null; } else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.INIT) { - init(); + initExtension(); } else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.FREEFLOW) { // nothing to be done yet @@ -338,7 +338,7 @@ public abstract class Extension implements IExtension{ * Gets called when a connection has been established with G-Earth. * This does not imply a connection with Habbo is setup. */ - protected void init(){} + protected void initExtension(){} /** * The application got doubleclicked from the G-Earth interface. Doing something here is optional diff --git a/G-Earth/src/main/java/gearth/extensions/ExtensionFormLauncher.java b/G-Earth/src/main/java/gearth/extensions/ExtensionFormLauncher.java index b0c7253..570de6a 100644 --- a/G-Earth/src/main/java/gearth/extensions/ExtensionFormLauncher.java +++ b/G-Earth/src/main/java/gearth/extensions/ExtensionFormLauncher.java @@ -21,7 +21,7 @@ public class ExtensionFormLauncher extends Application{ extensionForm.extension = new Extension(args) { @Override - protected void init() { + protected void initExtension() { extensionForm.initExtension(); } diff --git a/G-Earth/src/main/java/gearth/misc/harble_api/HarbleAPI.java b/G-Earth/src/main/java/gearth/misc/harble_api/HarbleAPI.java index 4580721..45d26d5 100644 --- a/G-Earth/src/main/java/gearth/misc/harble_api/HarbleAPI.java +++ b/G-Earth/src/main/java/gearth/misc/harble_api/HarbleAPI.java @@ -27,7 +27,7 @@ public class HarbleAPI { this.destination = destination; this.headerId = headerId; this.hash = hash; - this.name = name; + this.name = (name == null || name.equals("null") ? null : name); } public String getName() { return name; @@ -41,6 +41,11 @@ public class HarbleAPI { public String getHash() { return hash; } + + public String toString() { + String s = (headerId+": " + "["+hash+"]" + "["+name+"]"); + return s; + } } private Map headerIdToMessage_incoming = new HashMap<>(); @@ -52,13 +57,25 @@ public class HarbleAPI { private Map nameToMessage_incoming = new HashMap<>(); private Map nameToMessage_outgoing = new HashMap<>(); + private boolean success = false; + /** * cache file must be generated first within G-Earth, inb4 20 extensions requesting it at the same time * @param hotelversion */ + + public static HarbleAPI get(String hotelversion) { + HarbleAPI wannabe = new HarbleAPI(hotelversion); + if (!wannabe.success) { + return null; + } + return wannabe; + } + public HarbleAPI (String hotelversion) { if (Cacher.cacheFileExists(HarbleAPIFetcher.CACHE_PREFIX + hotelversion)) { JSONObject object = Cacher.getCacheContents(HarbleAPIFetcher.CACHE_PREFIX + hotelversion); + success = true; parse(object); } } @@ -87,29 +104,52 @@ public class HarbleAPI { } } private void parse(JSONObject object) { - JSONObject incoming = object.getJSONObject("IncomingMessages"); - JSONObject outgoing = object.getJSONObject("OutgoingMessages"); + try { + JSONObject incoming = object.getJSONObject("IncomingMessages"); + JSONObject outgoing = object.getJSONObject("OutgoingMessages"); - if (incoming != null && outgoing != null) { - for (String key : incoming.keySet()) { - JSONObject inMsg = incoming.getJSONObject(key); - String name = inMsg.getString("Name"); - String hash = inMsg.getString("Hash"); - Integer headerId = Integer.parseInt(key); - HarbleMessage message = new HarbleMessage(HMessage.Side.TOCLIENT, headerId, hash, name); + if (incoming != null && outgoing != null) { + for (String key : incoming.keySet()) { + try { + JSONObject inMsg = incoming.getJSONObject(key); + String name; + try { + name = inMsg.getString("Name"); + } + catch (Exception e) { + name = null; + } + String hash = inMsg.getString("Hash"); + Integer headerId = Integer.parseInt(key); + HarbleMessage message = new HarbleMessage(HMessage.Side.TOCLIENT, headerId, hash, name); - addMessage(message); - } - for (String key : outgoing.keySet()) { - JSONObject outMsg = incoming.getJSONObject(key); - String name = outMsg.getString("Name"); - String hash = outMsg.getString("Hash"); - Integer headerId = Integer.parseInt(key); - HarbleMessage message = new HarbleMessage(HMessage.Side.TOSERVER, headerId, hash, name); + addMessage(message); + } + catch( Exception e) {} + } + for (String key : outgoing.keySet()) { + try { + JSONObject outMsg = outgoing.getJSONObject(key); + String name; + try { + name = outMsg.getString("Name"); + } catch (Exception e) { + name = null; + } + String hash = outMsg.getString("Hash"); + Integer headerId = Integer.parseInt(key); + HarbleMessage message = new HarbleMessage(HMessage.Side.TOSERVER, headerId, hash, name); - addMessage(message); + addMessage(message); + } + catch( Exception e) {} + } } } + catch (Exception e) { + success = false; + } + } public HarbleMessage getHarbleMessageFromHeaderId(HMessage.Side side, int headerId) { diff --git a/G-Earth/src/main/java/gearth/misc/harble_api/HarbleAPIFetcher.java b/G-Earth/src/main/java/gearth/misc/harble_api/HarbleAPIFetcher.java index c51a48f..8d235d1 100644 --- a/G-Earth/src/main/java/gearth/misc/harble_api/HarbleAPIFetcher.java +++ b/G-Earth/src/main/java/gearth/misc/harble_api/HarbleAPIFetcher.java @@ -1,6 +1,7 @@ package gearth.misc.harble_api; import gearth.misc.Cacher; +import gearth.protocol.HMessage; import org.json.JSONObject; import org.jsoup.Connection; import org.jsoup.Jsoup; @@ -16,11 +17,14 @@ public class HarbleAPIFetcher { public static final String CACHE_PREFIX = "HARBLE_API-"; public static final String HARBLE_API_URL = "https://api.harble.net/revisions/$hotelversion$.json"; - public static HarbleAPI fetch(String hotelversion) { + //latest fetched + public static HarbleAPI HARBLEAPI = null; + + public static void fetch(String hotelversion) { String cacheName = CACHE_PREFIX + hotelversion; if (Cacher.cacheFileExists(cacheName)) { - return new HarbleAPI(hotelversion); + HARBLEAPI = new HarbleAPI(hotelversion); } else { Connection connection = Jsoup.connect(HARBLE_API_URL.replace("$hotelversion$", hotelversion)).ignoreContentType(true); @@ -32,20 +36,23 @@ public class HarbleAPIFetcher { s = s.substring(6, s.length() - 7); JSONObject object = new JSONObject(s); Cacher.updateCache(object, cacheName); - - return new HarbleAPI(hotelversion); + HARBLEAPI = new HarbleAPI(hotelversion); } else { - return null; + HARBLEAPI = null; } } catch (IOException e) { - return null; + HARBLEAPI = null; } } } public static void main(String[] args) { - HarbleAPI api = fetch("PRODUCTION-201810171204-70166177"); + fetch("PRODUCTION-201810171204-70166177"); + + HarbleAPI api = HARBLEAPI; + HarbleAPI.HarbleMessage haMessage = api.getHarbleMessageFromHeaderId(HMessage.Side.TOSERVER, 525); + System.out.println(haMessage); } } \ No newline at end of file diff --git a/G-Earth/src/main/java/gearth/ui/extensions/Extensions.java b/G-Earth/src/main/java/gearth/ui/extensions/Extensions.java index 5593b04..a4e6001 100644 --- a/G-Earth/src/main/java/gearth/ui/extensions/Extensions.java +++ b/G-Earth/src/main/java/gearth/ui/extensions/Extensions.java @@ -1,5 +1,6 @@ package gearth.ui.extensions; +import gearth.misc.harble_api.HarbleAPIFetcher; import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.scene.control.Button; @@ -149,6 +150,7 @@ public class Extensions extends SubForm { getHConnection().addStateChangeListener((oldState, newState) -> { if (newState == HConnection.State.CONNECTED) { + HarbleAPIFetcher.fetch(getHConnection().getHotelVersion()); synchronized (gEarthExtensions) { for (GEarthExtension extension : gEarthExtensions) { extension.sendMessage( diff --git a/G-Earth/src/main/java/gearth/ui/logger/Logger.java b/G-Earth/src/main/java/gearth/ui/logger/Logger.java index 9d9ec12..30f4909 100644 --- a/G-Earth/src/main/java/gearth/ui/logger/Logger.java +++ b/G-Earth/src/main/java/gearth/ui/logger/Logger.java @@ -1,6 +1,5 @@ package gearth.ui.logger; -import gearth.ui.logger.loggerdisplays.UiLogger; import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.scene.control.Button; diff --git a/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/PacketLoggerFactory.java b/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/PacketLoggerFactory.java index 2cd44d1..86c5994 100644 --- a/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/PacketLoggerFactory.java +++ b/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/PacketLoggerFactory.java @@ -2,6 +2,7 @@ package gearth.ui.logger.loggerdisplays; import gearth.Main; import gearth.misc.OSValidator; +import gearth.ui.logger.loggerdisplays.uilogger.UiLogger; /** * Created by Jonas on 04/04/18. diff --git a/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/uilogger/Element.java b/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/uilogger/Element.java new file mode 100644 index 0000000..7c6d0d1 --- /dev/null +++ b/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/uilogger/Element.java @@ -0,0 +1,14 @@ +package gearth.ui.logger.loggerdisplays.uilogger; + +/** + * Created by Jonas on 17/11/2018. + */ +class Element { + final String text; + final String className; + + Element(String text, String className) { + this.text = text; + this.className = className; + } +} \ No newline at end of file diff --git a/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/UiLogger.java b/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/uilogger/UiLogger.java similarity index 96% rename from G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/UiLogger.java rename to G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/uilogger/UiLogger.java index aea5c64..0c03e3d 100644 --- a/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/UiLogger.java +++ b/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/uilogger/UiLogger.java @@ -1,7 +1,7 @@ -package gearth.ui.logger.loggerdisplays; +package gearth.ui.logger.loggerdisplays.uilogger; import gearth.protocol.HPacket; -import gearth.ui.UiLoggerController; +import gearth.ui.logger.loggerdisplays.PacketLogger; import javafx.event.Event; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; diff --git a/G-Earth/src/main/java/gearth/ui/UiLoggerController.java b/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/uilogger/UiLoggerController.java similarity index 80% rename from G-Earth/src/main/java/gearth/ui/UiLoggerController.java rename to G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/uilogger/UiLoggerController.java index db9411e..6feffae 100644 --- a/G-Earth/src/main/java/gearth/ui/UiLoggerController.java +++ b/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/uilogger/UiLoggerController.java @@ -1,5 +1,8 @@ -package gearth.ui; +package gearth.ui.logger.loggerdisplays.uilogger; +import gearth.misc.harble_api.HarbleAPI; +import gearth.misc.harble_api.HarbleAPIFetcher; +import gearth.protocol.HMessage; import gearth.protocol.HPacket; import gearth.ui.logger.loggerdisplays.PacketLogger; import javafx.application.Platform; @@ -27,6 +30,9 @@ public class UiLoggerController implements Initializable { public Label lblAutoScrolll; public CheckMenuItem chkAutoscroll; public CheckMenuItem chkSkipBigPackets; + public CheckMenuItem chkMessageName; + public CheckMenuItem chkMessageHash; + public Label lblHarbleAPI; private StyleClassedTextArea area; @@ -35,7 +41,8 @@ public class UiLoggerController implements Initializable { private boolean displayStructure = true; private boolean autoScroll = true; private boolean skiphugepackets = true; - + private boolean viewMessageName = true; + private boolean viewMessageHash = false; private volatile boolean initialized = false; private final List appendLater = new ArrayList<>(); @@ -86,6 +93,25 @@ public class UiLoggerController implements Initializable { String expr = packet.toExpression(); + lblHarbleAPI.setText("HarbleAPI: " + (HarbleAPIFetcher.HARBLEAPI == null ? "False" : "True")); + if ((viewMessageName || viewMessageHash) && HarbleAPIFetcher.HARBLEAPI != null) { + HarbleAPI api = HarbleAPIFetcher.HARBLEAPI; + HarbleAPI.HarbleMessage message = api.getHarbleMessageFromHeaderId( + (isIncoming ? HMessage.Side.TOCLIENT : HMessage.Side.TOSERVER), + packet.headerId() + ); + + if (!(viewMessageName && !viewMessageHash && message.getName() == null)) { + if (viewMessageName && message.getName() != null) { + elements.add(new Element("["+message.getName()+"]", "messageinfo")); + } + if (viewMessageHash) { + elements.add(new Element("["+message.getHash()+"]", "messageinfo")); + } + elements.add(new Element("\n", "")); + } + } + if (isBlocked) elements.add(new Element("[Blocked]\n", "blocked")); else if (isReplaced) elements.add(new Element("[Replaced]\n", "replaced")); @@ -180,14 +206,12 @@ public class UiLoggerController implements Initializable { public void toggleSkipPackets(ActionEvent actionEvent) { skiphugepackets = !skiphugepackets; } -} -class Element { - final String text; - final String className; - - Element(String text, String className) { - this.text = text; - this.className = className; + public void toggleMessageName(ActionEvent actionEvent) { + viewMessageName = !viewMessageName; } -} \ No newline at end of file + + public void toggleMessageHash(ActionEvent actionEvent) { + viewMessageHash = !viewMessageHash; + } +} diff --git a/G-Earth/src/main/resources/gearth/ui/logger/uilogger/UiLogger.fxml b/G-Earth/src/main/resources/gearth/ui/logger/uilogger/UiLogger.fxml index e5419fa..4580284 100644 --- a/G-Earth/src/main/resources/gearth/ui/logger/uilogger/UiLogger.fxml +++ b/G-Earth/src/main/resources/gearth/ui/logger/uilogger/UiLogger.fxml @@ -9,13 +9,15 @@ - + + + @@ -59,7 +61,20 @@ -