Advertisement
Skylinerw

Untitled

Apr 20th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.70 KB | None | 0 0
  1. /*
  2.  * Decompiled with CFR 0_101.
  3.  *
  4.  * Could not load the following classes:
  5.  *  com.google.common.annotations.VisibleForTesting
  6.  *  com.google.common.collect.Lists
  7.  */
  8. import com.google.common.annotations.VisibleForTesting;
  9. import com.google.common.collect.Lists;
  10. import NBTByteArray;
  11. import NBTByte;
  12. import NBTCompound;
  13. import NBTDouble;
  14. import NBTFloat;
  15. import NBTIntArray;
  16. import NBTFloat;
  17. import NBTList;
  18. import NBTLong;
  19. import NBTPrimitive;
  20. import NBTShort;
  21. import NBTString;
  22. import NBTBase;
  23. import NBTParseException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.regex.Matcher;
  27. import java.util.regex.Pattern;
  28.  
  29. public class stringToNBT {
  30.     private static final Pattern regexDouble1 = Pattern.compile("[-+]?(?:[0-9]+[.]|[0-9]*[.][0-9]+)(?:e[-+]?[0-9]+)?", 2);
  31.     private static final Pattern regexDouble2 = Pattern.compile("[-+]?(?:[0-9]+[.]?|[0-9]*[.][0-9]+)(?:e[-+]?[0-9]+)?d", 2);
  32.     private static final Pattern regexFloat = Pattern.compile("[-+]?(?:[0-9]+[.]?|[0-9]*[.][0-9]+)(?:e[-+]?[0-9]+)?f", 2);
  33.     private static final Pattern regexByte = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)b", 2);
  34.     private static final Pattern regexLong = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)l", 2);
  35.     private static final Pattern regexShort = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)s", 2);
  36.     private static final Pattern regexInt = Pattern.compile("[-+]?(?:0|[1-9][0-9]*)");
  37.     private final String inputString;
  38.     private int cursor;
  39.  
  40.     public static NBTCompound getNewInstance(String string) throws NBTParseException {
  41.         return new stringToNBT(string).parse();
  42.     }
  43.  
  44.     @VisibleForTesting
  45.     NBTCompound parse() throws NBTParseException {
  46.         NBTCompound nbtcompound = this.createCompoundNBT();
  47.         this.movePastWhitespace();
  48.         if (this.withinBounds()) {
  49.             ++this.cursor;
  50.             throw this.newParseException("Trailing data found");
  51.         }
  52.         return nbtcompound;
  53.     }
  54.  
  55.     @VisibleForTesting
  56.     stringToNBT(String raw) {
  57.         this.inputString = raw;
  58.     }
  59.  
  60.     protected String getKey() throws NBTParseException {
  61.         this.movePastWhitespace();
  62.         if (!this.withinBounds()) { // If at end of string, throw.
  63.             throw this.newParseException("Expected key");
  64.         }
  65.         return this.getCurrentChar() == '\"' ? this.getQuotedString() : this.getUnquotedString();
  66.     }
  67.  
  68.     private NBTParseException newParseException(String message) {
  69.         return new NBTParseException(message, this.inputString, this.cursor);
  70.     }
  71.  
  72.     protected NBTBase getSimpleValue() throws NBTParseException {
  73.         this.movePastWhitespace();
  74.         if (this.getCurrentChar() == '\"') {
  75.             return new NBTString(this.getQuotedString());
  76.         }
  77.         String string = this.getUnquotedString();
  78.         if (string.isEmpty()) {
  79.             throw this.newParseException("Expected value");
  80.         }
  81.         return this.createSimpleNBT(string);
  82.     }
  83.  
  84.     private NBTBase createSimpleNBT(String value) {
  85.         try {
  86.             if (regexFloat.matcher((CharSequence)value).matches()) {
  87.                 return new NBTFloat(Float.parseFloat(value.substring(0, value.length() - 1)));
  88.             }
  89.             if (regexByte.matcher((CharSequence)value).matches()) {
  90.                 return new NBTByte(Byte.parseByte(value.substring(0, value.length() - 1)));
  91.             }
  92.             if (regexLong.matcher((CharSequence)value).matches()) {
  93.                 return new NBTLong(Long.parseLong(value.substring(0, value.length() - 1)));
  94.             }
  95.             if (regexShort.matcher((CharSequence)value).matches()) {
  96.                 return new NBTShort(Short.parseShort(value.substring(0, value.length() - 1)));
  97.             }
  98.             if (regexInt.matcher((CharSequence)value).matches()) {
  99.                 return new NBTInteger(Integer.parseInt(value));
  100.             }
  101.             if (regexDouble2.matcher((CharSequence)value).matches()) {
  102.                 return new NBTDouble(Double.parseDouble(value.substring(0, value.length() - 1)));
  103.             }
  104.             if (regexDouble1.matcher((CharSequence)value).matches()) {
  105.                 return new NBTDouble(Double.parseDouble(value));
  106.             }
  107.             if ("true".equalsIgnoreCase(value)) {
  108.                 return new NBTByte(1);
  109.             }
  110.             if ("false".equalsIgnoreCase(value)) {
  111.                 return new NBTByte(0);
  112.             }
  113.         }
  114.         catch (NumberFormatException exception) {
  115.             // empty catch block
  116.         }
  117.         return new NBTString(value);
  118.     }
  119.  
  120.     private String getQuotedString() throws NBTParseException {
  121.         int cursorPos = ++this.cursor;
  122.         StringBuilder stringBuilder = null;
  123.         boolean escaping = false;
  124.         while (this.withinBounds()) {
  125.             char currentChar = this.getToNextChar();
  126.             if (escaping) {
  127.                 if (currentChar != '\\' && currentChar != '\"') {
  128.                     throw this.newParseException("Invalid escape of '" + currentChar + "'");
  129.                 }
  130.                 escaping = false;
  131.             } else {
  132.                 if (currentChar == '\\') {
  133.                     escaping = true;
  134.                     if (stringBuilder != null) continue;
  135.                     stringBuilder = new StringBuilder(this.inputString.substring(cursorPos, this.cursor - 1));
  136.                     continue;
  137.                 }
  138.                 if (currentChar == '\"') {
  139.                     return stringBuilder == null ? this.inputString.substring(cursorPos, this.cursor - 1) : stringBuilder.toString();
  140.                 }
  141.             }
  142.             if (stringBuilder == null) continue;
  143.             stringBuilder.append(currentChar);
  144.         }
  145.         throw this.newParseException("Missing termination quote");
  146.     }
  147.  
  148.     private String getUnquotedString() {
  149.         int n = this.cursor;
  150.         while (this.withinBounds() && this.isValidChar(this.getCurrentChar())) {
  151.             ++this.cursor;
  152.         }
  153.         return this.inputString.substring(n, this.cursor);
  154.     }
  155.  
  156.     protected NBTBase getValue() throws NBTParseException {
  157.         this.movePastWhitespace();
  158.         if (!this.withinBounds()) {
  159.             throw this.newParseException("Expected value");
  160.         }
  161.         char currentChar = this.getCurrentChar();
  162.         if (currentChar == '{') {
  163.             return this.createCompoundNBT();
  164.         }
  165.         if (currentChar == '[') {
  166.             return this.getArrayOrList();
  167.         }
  168.         return this.getSimpleValue();
  169.     }
  170.  
  171.     protected NBTBase getArrayOrList() throws NBTParseException {
  172.         char c;
  173.         if (this.cursorPlusNWithinBounds(1) && ((c = this.getCharAtCursorPlusN(1)) == 'B' || c == 'I') && this.cursorPlusNWithinBounds(2) && this.getCharAtCursorPlusN(2) == ';') {
  174.             return this.getArrayValue();
  175.         }
  176.         return this.createListNBT();
  177.     }
  178.  
  179.     protected NBTCompound createCompoundNBT() throws NBTParseException {
  180.         this.moveToNextChar('{');
  181.         NBTCompound nbtcompound = new NBTCompound();
  182.         this.movePastWhitespace();
  183.         while (this.withinBounds() && this.getCurrentChar() != '}') {
  184.             String key = this.getKey();
  185.             if (key.isEmpty()) {
  186.                 throw this.newParseException("Expected non-empty key");
  187.             }
  188.             this.moveToNextChar(':');
  189.             nbtcompound.setTag(key, this.getValue());
  190.             if (!this.hasNextComma()) break;
  191.             if (this.withinBounds()) continue;
  192.             throw this.newParseException("Expected key");
  193.         }
  194.         this.moveToNextChar('}');
  195.         return nbtcompound;
  196.     }
  197.  
  198.     private NBTBase createListNBT() throws NBTParseException {
  199.         this.moveToNextChar('[');
  200.         this.movePastWhitespace();
  201.         if (!this.withinBounds()) {
  202.             throw this.newParseException("Expected value");
  203.         }
  204.         NBTList nbtlist = new NBTList();
  205.         byte rootType = -1;
  206.         while (this.getCurrentChar() != ']') {
  207.             NBTBase nbtbase = this.getValue();
  208.             byte inputType = nbtbase.getId();
  209.             if (rootType < 0) {
  210.                 rootType = inputType;
  211.             } else if (inputType != rootType) {
  212.                 throw this.newParseException("Unable to insert " + nbtbase.getTypeName(inputType) + " into ListTag of type " + nbtbase.getTypeName(rootType));
  213.             }
  214.             nbtlist.appendTag(nbtbase);
  215.             if (!this.hasNextComma()) break;
  216.             if (this.withinBounds()) continue;
  217.             throw this.newParseException("Expected value");
  218.         }
  219.         this.moveToNextChar(']');
  220.         return nbtlist;
  221.     }
  222.  
  223.     private NBTBase getArrayValue() throws NBTParseException {
  224.         this.moveToNextChar('[');
  225.         char identifier = this.getToNextChar();
  226.         this.getToNextChar();
  227.         this.movePastWhitespace();
  228.         if (!this.withinBounds()) {
  229.             throw this.newParseException("Expected value");
  230.         }
  231.         if (identifier == 'B') {
  232.             return new NBTByteArray(this.createArrayNBT(7, 1));
  233.         }
  234.         return new NBTIntArray(this.createArrayNBT(11, 3));
  235.     }
  236.  
  237.     private <T extends Number> List<T> createArrayNBT(byte arrayType, byte primitiveType) throws NBTParseException {
  238.         ArrayList arrayList = Lists.newArrayList();
  239.         while (this.getCurrentChar() != ']') {
  240.             NBTBase nbtbase = this.getValue();
  241.             byte inputValue = nbtbase.getId();
  242.             if (inputValue != primitiveType) {
  243.                 throw this.newParseException("Unable to insert " + nbtbase.getTypeName(inputValue) + " into " + nbtbase.getTypeName(arrayType));
  244.             }
  245.             if (primitiveType == 1) {
  246.                 arrayList.add(Byte.valueOf(((NBTPrimitive)nbtbase).getByte()));
  247.             } else {
  248.                 arrayList.add(((NBTPrimitive)nbtbase).getInt());
  249.             }
  250.             if (!this.hasNextComma()) break;
  251.             if (this.withinBounds()) continue;
  252.             throw this.newParseException("Expected value");
  253.         }
  254.         this.moveToNextChar(']');
  255.         return arrayList;
  256.     }
  257.  
  258.     private void movePastWhitespace() {
  259.         while (this.withinBounds() && Character.isWhitespace(this.getCurrentChar())) {
  260.             ++this.cursor;
  261.         }
  262.     }
  263.  
  264.     private boolean hasNextComma() {
  265.         this.movePastWhitespace();
  266.         if (this.withinBounds() && this.getCurrentChar() == ',') {
  267.             ++this.cursor;
  268.             this.movePastWhitespace();
  269.             return true;
  270.         }
  271.         return false;
  272.     }
  273.  
  274.     private void moveToNextChar(char requiredNextChar) throws NBTParseException {
  275.         this.movePastWhitespace();
  276.         boolean withinBounds = this.withinBounds();
  277.         if (withinBounds && this.getCurrentChar() == requiredNextChar) {
  278.             ++this.cursor;
  279.             return;
  280.         }
  281.         throw new NBTParseException("Expected '" + requiredNextChar + "' but got '" + (withinBounds ? Character.valueOf(this.getCurrentChar()) : "<EOF>") + "'", this.inputString, this.cursor + 1);
  282.     }
  283.  
  284.     protected boolean isValidChar(char c) {
  285.         return c >= '0' && c <= '9' || c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c == '_' || c == '-' || c == '.' || c == '+';
  286.     }
  287.  
  288.     private boolean cursorPlusNWithinBounds(int n) {
  289.         return this.cursor + n < this.inputString.length();
  290.     }
  291.  
  292.     boolean withinBounds() {
  293.         return this.cursorPlusNWithinBounds(0);
  294.     }
  295.  
  296.     private char getCharAtCursorPlusN(int n) {
  297.         return this.inputString.charAt(this.cursor + n);
  298.     }
  299.  
  300.     private char getCurrentChar() {
  301.         return this.getCharAtCursorPlusN(0);
  302.     }
  303.  
  304.     private char getToNextChar() {
  305.         return this.inputString.charAt(this.cursor++);
  306.     }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement