mirror of
https://github.com/sirjonasxx/G-Earth.git
synced 2024-11-23 08:50:52 +01:00
harbleAPI structure support
This commit is contained in:
parent
30def90d77
commit
6550e2f896
@ -4,7 +4,7 @@
|
|||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>SpeechColorizer</artifactId>
|
<artifactId>HappySpeech</artifactId>
|
||||||
|
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@ public class Cacher {
|
|||||||
// e.printStackTrace();
|
// e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.println(GEarthDir);
|
||||||
return GEarthDir
|
return GEarthDir
|
||||||
+ File.separator
|
+ File.separator
|
||||||
+ "Cache";
|
+ "Cache";
|
||||||
|
@ -2,6 +2,7 @@ package gearth.misc.harble_api;
|
|||||||
|
|
||||||
import gearth.misc.Cacher;
|
import gearth.misc.Cacher;
|
||||||
import gearth.protocol.HMessage;
|
import gearth.protocol.HMessage;
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import sun.misc.Cache;
|
import sun.misc.Cache;
|
||||||
|
|
||||||
@ -10,6 +11,7 @@ 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.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Jonas on 10/11/2018.
|
* Created by Jonas on 10/11/2018.
|
||||||
@ -21,13 +23,15 @@ public class HarbleAPI {
|
|||||||
private int headerId;
|
private int headerId;
|
||||||
private String hash;
|
private String hash;
|
||||||
private String name;
|
private String name;
|
||||||
|
private List<String> structure;
|
||||||
|
|
||||||
//name can be NULL
|
//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.destination = destination;
|
||||||
this.headerId = headerId;
|
this.headerId = headerId;
|
||||||
this.hash = hash;
|
this.hash = hash;
|
||||||
this.name = (name == null || name.equals("null") ? null : name);
|
this.name = (name == null || name.equals("null") ? null : name);
|
||||||
|
this.structure = structure;
|
||||||
}
|
}
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
@ -41,9 +45,12 @@ public class HarbleAPI {
|
|||||||
public String getHash() {
|
public String getHash() {
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
public List<String> getStructure() {
|
||||||
|
return structure;
|
||||||
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String s = (headerId+": " + "["+hash+"]" + "["+name+"]");
|
String s = (headerId+": " + "["+hash+"]["+name+"]["+ structure+"]");
|
||||||
return s;
|
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 =
|
Map<Integer, HarbleMessage> headerIdToMessage =
|
||||||
message.getDestination() == HMessage.Side.TOCLIENT
|
message.getDestination() == HMessage.Side.TOCLIENT
|
||||||
? headerIdToMessage_incoming :
|
? headerIdToMessage_incoming :
|
||||||
@ -105,42 +138,23 @@ public class HarbleAPI {
|
|||||||
}
|
}
|
||||||
private void parse(JSONObject object) {
|
private void parse(JSONObject object) {
|
||||||
try {
|
try {
|
||||||
JSONObject incoming = object.getJSONObject("IncomingMessages");
|
JSONObject incoming = object.getJSONObject("Incoming");
|
||||||
JSONObject outgoing = object.getJSONObject("OutgoingMessages");
|
JSONObject outgoing = object.getJSONObject("Outgoing");
|
||||||
|
|
||||||
if (incoming != null && outgoing != null) {
|
if (incoming != null && outgoing != null) {
|
||||||
for (String key : incoming.keySet()) {
|
for (String key : incoming.keySet()) {
|
||||||
try {
|
try {
|
||||||
JSONObject inMsg = incoming.getJSONObject(key);
|
JSONObject inMsg = incoming.getJSONObject(key);
|
||||||
String name;
|
addMessage(HMessage.Side.TOCLIENT, inMsg, key);
|
||||||
try {
|
}
|
||||||
name = inMsg.getString("Name");
|
catch( Exception e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
catch( Exception e) {}
|
|
||||||
}
|
}
|
||||||
for (String key : outgoing.keySet()) {
|
for (String key : outgoing.keySet()) {
|
||||||
try {
|
try {
|
||||||
JSONObject outMsg = outgoing.getJSONObject(key);
|
JSONObject outMsg = outgoing.getJSONObject(key);
|
||||||
String name;
|
addMessage(HMessage.Side.TOSERVER, outMsg, key);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
catch( Exception e) {}
|
catch( Exception e) {}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,23 @@ import java.io.IOException;
|
|||||||
/**
|
/**
|
||||||
* Created by Jonas on 10/11/2018.
|
* 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 class HarbleAPIFetcher {
|
||||||
|
|
||||||
public static final String CACHE_PREFIX = "HARBLE_API-";
|
public static final String CACHE_PREFIX = "HARBLE_API-";
|
||||||
@ -20,7 +37,7 @@ public class HarbleAPIFetcher {
|
|||||||
//latest fetched
|
//latest fetched
|
||||||
public static HarbleAPI HARBLEAPI = null;
|
public static HarbleAPI HARBLEAPI = null;
|
||||||
|
|
||||||
public static void fetch(String hotelversion) {
|
public synchronized static void fetch(String hotelversion) {
|
||||||
String cacheName = CACHE_PREFIX + hotelversion;
|
String cacheName = CACHE_PREFIX + hotelversion;
|
||||||
|
|
||||||
if (Cacher.cacheFileExists(cacheName)) {
|
if (Cacher.cacheFileExists(cacheName)) {
|
||||||
|
@ -2,6 +2,8 @@ package gearth.protocol;
|
|||||||
|
|
||||||
import com.sun.org.apache.xpath.internal.operations.Bool;
|
import com.sun.org.apache.xpath.internal.operations.Bool;
|
||||||
import gearth.misc.StringifyAble;
|
import gearth.misc.StringifyAble;
|
||||||
|
import gearth.misc.harble_api.HarbleAPI;
|
||||||
|
import gearth.misc.harble_api.HarbleAPIFetcher;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
@ -9,6 +11,7 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.security.InvalidParameterException;
|
import java.security.InvalidParameterException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class HPacket implements StringifyAble {
|
public class HPacket implements StringifyAble {
|
||||||
// te komen: toExpressions (+impl. expressies)
|
// te komen: toExpressions (+impl. expressies)
|
||||||
@ -651,6 +654,54 @@ public class HPacket implements StringifyAble {
|
|||||||
isEdited = edited;
|
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
|
* returns "" if not found or not sure enough
|
||||||
* dont hate on the coding quality in this function, its pretty effective.
|
* dont hate on the coding quality in this function, its pretty effective.
|
||||||
|
@ -91,7 +91,7 @@ public class UiLoggerController implements Initializable {
|
|||||||
|
|
||||||
ArrayList<Element> elements = new ArrayList<>();
|
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"));
|
lblHarbleAPI.setText("HarbleAPI: " + (HarbleAPIFetcher.HARBLEAPI == null ? "False" : "True"));
|
||||||
if ((viewMessageName || viewMessageHash) && HarbleAPIFetcher.HARBLEAPI != null) {
|
if ((viewMessageName || viewMessageHash) && HarbleAPIFetcher.HARBLEAPI != null) {
|
||||||
|
Loading…
Reference in New Issue
Block a user