changes for updated harble api

This commit is contained in:
sirjonasxx 2019-01-04 22:36:02 +01:00
parent 587f725424
commit 031a6bf64d
2 changed files with 50 additions and 42 deletions

View File

@ -23,15 +23,15 @@ public class HarbleAPI {
private int headerId;
private String hash;
private String name;
private List<String> structure;
private String structure;
//name can be NULL
public HarbleMessage(HMessage.Side destination, int headerId, String hash, String name, List<String> structure) {
public HarbleMessage(HMessage.Side destination, int headerId, String hash, String name, String structure) {
this.destination = destination;
this.headerId = headerId;
this.hash = hash;
this.name = (name == null || name.equals("null") ? null : name);
this.structure = structure;
this.structure = (structure == null || structure.equals("null") ? null : structure);
}
public String getName() {
return name;
@ -45,7 +45,7 @@ public class HarbleAPI {
public String getHash() {
return hash;
}
public List<String> getStructure() {
public String getStructure() {
return structure;
}
@ -97,14 +97,10 @@ public class HarbleAPI {
}
String hash = object.getString("Hash");
Integer headerId = Integer.parseInt(id);
List<String> structure;
String structure;
try {
structure = new ArrayList<>();
JSONArray array = object.getJSONArray("Structure");
for (Object o : array) {
structure.add((String)o);
}
structure = object.getString("Structure");
}
catch (Exception e) {
structure = null;

View File

@ -1,6 +1,5 @@
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;
@ -11,7 +10,6 @@ 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 {
@ -132,6 +130,10 @@ public class HPacket implements StringifyAble {
ByteBuffer b = ByteBuffer.allocate(4).putInt(Integer.parseInt(inhoud));
newString.append(new HPacket(b.array()).toString());
}
else if (type.equals("d")) {
ByteBuffer b = ByteBuffer.allocate(8).putDouble(Double.parseDouble(inhoud));
newString.append(new HPacket(b.array()).toString());
}
else if (type.equals("b")) { // could be a byte or a boolean, no one cares
if (inhoud.toLowerCase().equals("true") || inhoud.toLowerCase().equals("false")) {
newString.append(inhoud.toLowerCase().equals("true") ? "[1]" : "[0]");
@ -681,39 +683,34 @@ public class HPacket implements StringifyAble {
isEdited = edited;
}
private String toExpressionFromGivenStructure(List<String> structure) {
int oldReadIndex = readIndex;
resetReadIndex();
try {
StringBuilder builder = new StringBuilder();
private void buildExpressionFromGivenStructure(String struct, int indexInGivenStruct, StringBuilder builder) {
int prevInt = 0;
if (indexInGivenStruct == -1) {
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;
indexInGivenStruct = 0;
}
builder.append("}");
while (indexInGivenStruct < struct.length()) {
char c = struct.charAt(indexInGivenStruct++);
if (c == '(') {
for (int i = 0; i < prevInt; i++) buildExpressionFromGivenStructure(struct, indexInGivenStruct, builder);
int skipping = 1;
while (skipping > 0) {
char c2 = struct.charAt(indexInGivenStruct++);
if (c2 == '(') skipping++;
else if (c2 == ')') skipping--;
}
readIndex = oldReadIndex;
return builder.toString();
}
catch (Exception e) {
readIndex = oldReadIndex;
return toExpression();
else if (c == 'i') builder.append("{i:").append(prevInt = readInteger()).append('}');
else if (c == 's') builder.append("{s:").append(readString()).append('}');
else if (c == 'd') builder.append("{d:").append(readDouble()).append('}');
else if (c == 'b') builder.append("{b:").append(readByte()).append('}');
else if (c == 'B') builder.append("{b:").append(readBoolean()).append('}');
else return; // ')'
}
}
public String toExpression(HMessage.Side side) {
@ -723,7 +720,12 @@ public class HPacket implements StringifyAble {
if (HarbleAPIFetcher.HARBLEAPI != null &&
((msg = HarbleAPIFetcher.HARBLEAPI.getHarbleMessageFromHeaderId(side, headerId())) != null)) {
if (msg.getStructure() != null) {
return toExpressionFromGivenStructure(msg.getStructure());
int oldReadIndex = readIndex;
resetReadIndex();
StringBuilder builder = new StringBuilder();
buildExpressionFromGivenStructure(msg.getStructure(), -1, builder);
readIndex = oldReadIndex;
return builder.toString();
}
}
return toExpression();
@ -1022,6 +1024,16 @@ public class HPacket implements StringifyAble {
}
public static void main(String[] args) {
HPacket packet = new HPacket("{l}{u:4564}{i:3}{i:0}{s:hi}{i:0}{i:1}{s:how}{i:3}{b:1}{b:2}{b:3}{i:2}{s:r u}{i:1}{b:120}{i:2}{b:true}");
StringBuilder builder = new StringBuilder();
packet.buildExpressionFromGivenStructure("i(isi(b))iB", -1, builder);
String str = builder.toString();
HPacket packetverify = new HPacket(str);
System.out.println(str);
System.out.println(packetverify.toString().equals(packet.toString()));
}
}