From 6550e2f896e53b5a9183bf6670da24f50c4d0213 Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Mon, 26 Nov 2018 00:10:20 +0100 Subject: [PATCH] harbleAPI structure support --- Extensions/HappySpeech/pom.xml | 2 +- G-Earth/src/main/java/gearth/misc/Cacher.java | 1 + .../gearth/misc/harble_api/HarbleAPI.java | 72 +++++++++++-------- .../misc/harble_api/HarbleAPIFetcher.java | 19 ++++- .../main/java/gearth/protocol/HPacket.java | 51 +++++++++++++ .../uilogger/UiLoggerController.java | 2 +- 6 files changed, 115 insertions(+), 32 deletions(-) diff --git a/Extensions/HappySpeech/pom.xml b/Extensions/HappySpeech/pom.xml index 822ef75..713daf7 100644 --- a/Extensions/HappySpeech/pom.xml +++ b/Extensions/HappySpeech/pom.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - SpeechColorizer + HappySpeech jar diff --git a/G-Earth/src/main/java/gearth/misc/Cacher.java b/G-Earth/src/main/java/gearth/misc/Cacher.java index 9933bec..62f0cdc 100644 --- a/G-Earth/src/main/java/gearth/misc/Cacher.java +++ b/G-Earth/src/main/java/gearth/misc/Cacher.java @@ -30,6 +30,7 @@ public class Cacher { // e.printStackTrace(); } + System.out.println(GEarthDir); return GEarthDir + File.separator + "Cache"; 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 45d26d5..0de09bd 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 @@ -2,6 +2,7 @@ package gearth.misc.harble_api; import gearth.misc.Cacher; import gearth.protocol.HMessage; +import org.json.JSONArray; import org.json.JSONObject; import sun.misc.Cache; @@ -10,6 +11,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Consumer; /** * Created by Jonas on 10/11/2018. @@ -21,13 +23,15 @@ public class HarbleAPI { private int headerId; private String hash; private String name; + private List structure; //name can be NULL - public HarbleMessage(HMessage.Side destination, int headerId, String hash, String name) { + public HarbleMessage(HMessage.Side destination, int headerId, String hash, String name, List structure) { this.destination = destination; this.headerId = headerId; this.hash = hash; this.name = (name == null || name.equals("null") ? null : name); + this.structure = structure; } public String getName() { return name; @@ -41,9 +45,12 @@ public class HarbleAPI { public String getHash() { return hash; } + public List getStructure() { + return structure; + } public String toString() { - String s = (headerId+": " + "["+hash+"]" + "["+name+"]"); + String s = (headerId+": " + "["+hash+"]["+name+"]["+ structure+"]"); return s; } } @@ -80,7 +87,33 @@ public class HarbleAPI { } } - private void addMessage(HarbleMessage message) { + private void addMessage(HMessage.Side side, JSONObject object, String id) { + String name; + try { + name = object.getString("Name"); + } + catch (Exception e) { + name = null; + } + String hash = object.getString("Hash"); + Integer headerId = Integer.parseInt(id); + List structure; + + try { + structure = new ArrayList<>(); + JSONArray array = object.getJSONArray("Structure"); + for (Object o : array) { + structure.add((String)o); + } + } + catch (Exception e){ + structure = null; + } + + + HarbleMessage message = new HarbleMessage(side, headerId, hash, name, structure); + + Map headerIdToMessage = message.getDestination() == HMessage.Side.TOCLIENT ? headerIdToMessage_incoming : @@ -105,42 +138,23 @@ public class HarbleAPI { } private void parse(JSONObject object) { try { - JSONObject incoming = object.getJSONObject("IncomingMessages"); - JSONObject outgoing = object.getJSONObject("OutgoingMessages"); + JSONObject incoming = object.getJSONObject("Incoming"); + JSONObject outgoing = object.getJSONObject("Outgoing"); 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); + addMessage(HMessage.Side.TOCLIENT, inMsg, key); + } + catch( Exception e) { + e.printStackTrace(); } - 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(HMessage.Side.TOSERVER, outMsg, key); } catch( Exception e) {} } 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 8d235d1..c41e5f6 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 @@ -12,6 +12,23 @@ import java.io.IOException; /** * Created by Jonas on 10/11/2018. */ + +/** + * Ok the usage of this class is pretty shitty so I'm just gonna add some documentation here + * + * What this class does is fetching the revision (if needed) from the API, this is the only class with communication with the + * actual API. Then the result (if any) gets cached. + * + * The method "fetch(xxx);" needs to be called exactly once at the moment a new connection has been made. + * + * However, at that same moment the Extension class needs to send the "startConnection" signal to the extensions, and we want to make sure + * that the cached revision is already available at the moment the extensions get initialized with a new connection. That's why the + * fetch() method here only gets called by the extension class as that's the only way to ensure this method gets called BEFORE the extensions + * start. (bc im lazy and dont wanna rewrite code too) + * + * + * the "HARBLEAPI" object contains the latest fetched object and is ensured to be up-to-date with the current connection + */ public class HarbleAPIFetcher { public static final String CACHE_PREFIX = "HARBLE_API-"; @@ -20,7 +37,7 @@ public class HarbleAPIFetcher { //latest fetched public static HarbleAPI HARBLEAPI = null; - public static void fetch(String hotelversion) { + public synchronized static void fetch(String hotelversion) { String cacheName = CACHE_PREFIX + hotelversion; if (Cacher.cacheFileExists(cacheName)) { diff --git a/G-Earth/src/main/java/gearth/protocol/HPacket.java b/G-Earth/src/main/java/gearth/protocol/HPacket.java index b5de9c1..5e3c757 100644 --- a/G-Earth/src/main/java/gearth/protocol/HPacket.java +++ b/G-Earth/src/main/java/gearth/protocol/HPacket.java @@ -2,6 +2,8 @@ package gearth.protocol; import com.sun.org.apache.xpath.internal.operations.Bool; import gearth.misc.StringifyAble; +import gearth.misc.harble_api.HarbleAPI; +import gearth.misc.harble_api.HarbleAPIFetcher; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; @@ -9,6 +11,7 @@ import java.nio.charset.StandardCharsets; import java.security.InvalidParameterException; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; public class HPacket implements StringifyAble { // te komen: toExpressions (+impl. expressies) @@ -651,6 +654,54 @@ public class HPacket implements StringifyAble { isEdited = edited; } + private String toExpressionFromGivenStructure(List structure) { + int oldReadIndex = readIndex; + resetReadIndex(); + + try { + StringBuilder builder = new StringBuilder(); + builder.append("{l}{u:").append(headerId()).append("}"); + for(String str : structure) { + builder.append("{"); + builder.append(str.toLowerCase().charAt(0)).append(':'); + switch (str) { + case "int": + builder.append(readInteger()); + break; + case "String": + builder.append(readString()); + break; + case "Byte": + builder.append(readByte()); + break; + case "Boolean": + builder.append(readBoolean()); + break; + } + builder.append("}"); + } + readIndex = oldReadIndex; + return builder.toString(); + } + catch (Exception e) { + readIndex = oldReadIndex; + return toExpression(); + } + } + + public String toExpression(HMessage.Side side) { + if (isCorrupted()) return ""; + + HarbleAPI.HarbleMessage msg; + if (HarbleAPIFetcher.HARBLEAPI != null && + ((msg = HarbleAPIFetcher.HARBLEAPI.getHarbleMessageFromHeaderId(side, headerId())) != null)) { + if (msg.getStructure() != null) { + return toExpressionFromGivenStructure(msg.getStructure()); + } + } + return toExpression(); + } + /** * returns "" if not found or not sure enough * dont hate on the coding quality in this function, its pretty effective. diff --git a/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/uilogger/UiLoggerController.java b/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/uilogger/UiLoggerController.java index 6feffae..7cf489f 100644 --- a/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/uilogger/UiLoggerController.java +++ b/G-Earth/src/main/java/gearth/ui/logger/loggerdisplays/uilogger/UiLoggerController.java @@ -91,7 +91,7 @@ public class UiLoggerController implements Initializable { ArrayList elements = new ArrayList<>(); - String expr = packet.toExpression(); + String expr = packet.toExpression(isIncoming ? HMessage.Side.TOCLIENT : HMessage.Side.TOSERVER); lblHarbleAPI.setText("HarbleAPI: " + (HarbleAPIFetcher.HARBLEAPI == null ? "False" : "True")); if ((viewMessageName || viewMessageHash) && HarbleAPIFetcher.HARBLEAPI != null) {