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

View File

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