harbleAPI structure support

This commit is contained in:
sirjonasxx 2018-11-26 00:10:20 +01:00
parent 30def90d77
commit 6550e2f896
6 changed files with 115 additions and 32 deletions

View File

@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>SpeechColorizer</artifactId>
<artifactId>HappySpeech</artifactId>
<packaging>jar</packaging>

View File

@ -30,6 +30,7 @@ public class Cacher {
// e.printStackTrace();
}
System.out.println(GEarthDir);
return GEarthDir
+ File.separator
+ "Cache";

View File

@ -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<String> 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<String> 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<String> 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<String> 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<Integer, HarbleMessage> 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) {}
}

View File

@ -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)) {

View File

@ -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<String> 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.

View File

@ -91,7 +91,7 @@ public class UiLoggerController implements Initializable {
ArrayList<Element> 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) {