From a3c82a4fddac930b976d100f147e79ade5869289 Mon Sep 17 00:00:00 2001 From: Rex Chan Date: Sat, 3 Mar 2018 14:06:55 -0800 Subject: [PATCH] Fixed issue with parser deleting data --- src/StellarisDK/FileClasses/DataPattern.java | 7 ++- src/StellarisDK/FileClasses/GenericData.java | 44 ++++++++++++------- .../FileClasses/Helper/DataMap.java | 6 +-- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/StellarisDK/FileClasses/DataPattern.java b/src/StellarisDK/FileClasses/DataPattern.java index b882e0d..d9eb439 100644 --- a/src/StellarisDK/FileClasses/DataPattern.java +++ b/src/StellarisDK/FileClasses/DataPattern.java @@ -20,16 +20,15 @@ public class DataPattern { // Group 4-6: Recursion Pattern // Group 7-9: Standard key value pair // Group 10-12: String literal pair - // Group 13: Special match for tags - public static final Pattern sLre = Pattern.compile("([\\w:]+)(\\s*[=><]+\\s*)(hsv\\s*\\{.+?\\})|([\\w:]+)(\\s*[=><]+\\s*)\\{(.+?\\})|([\\w:]+)(\\s*[=><]+\\s*)([^\"]+?)\\s|([\\w:]+)(\\s*[=><]+\\s*)(.+?\")\\s*|(\\w+)"); + // Group 13: Special match for tags + public static final Pattern sLre = Pattern.compile("([\\w:]+)(\\s*[=><]+\\s*)(hsv\\s*\\{.+?\\})|([\\w:]+)(\\s*[=><]+\\s*)\\{(.+?\\})|([\\w:]+)(\\s*[=><]+\\s*)([^\"]+?)\\s|([\\w:]+)(\\s*[=><]+\\s*)(.+?\")\\s*|([\\w\"]+)"); public static final Pattern color = Pattern.compile("(hsv|rgb)\\s*\\{\\s*([\\d.]+)\\s*([\\d.]+)\\s*([\\d.]+)\\s*\\}\\s?"); // Pattern matches for main data structure // Group 1-4: Multi line recursion // Group 5-8: Single line - // Group 9-10: Tags - public static final Pattern newCombine = Pattern.compile("(?m)^\\s?([\\w:]+)(\\s*[=<>]+\\s*)\\{\\s*(#.*)?[\\r\\n]([\\W\\D\\S]+?)^\\s?\\}|^\\s?(\\w+)(\\s*[=<>]+\\s*)([^#\\r\\n]+)(#.+)?|^\\s?([\\w:\"]+)\\s?"); + public static final Pattern newCombine = Pattern.compile("(?m)^\\s?([\\w:]+)(\\s*[=<>]+\\s*)\\{\\s*(#.*)?[\\r\\n]([\\W\\D\\S]+?)^\\s?\\}|^\\s?(\\w+)(\\s*[=<>]+\\s*)([^#\\r\\n]+)(#.+)?"); // Mod Descriptor Specific Pattern public static final Pattern mDSpec = Pattern.compile("(?s)(?m)^\\t?(\\w+)=\\{[\\r\\n](.+?)^\\}"); diff --git a/src/StellarisDK/FileClasses/GenericData.java b/src/StellarisDK/FileClasses/GenericData.java index f8c9f23..915f29a 100644 --- a/src/StellarisDK/FileClasses/GenericData.java +++ b/src/StellarisDK/FileClasses/GenericData.java @@ -133,17 +133,25 @@ private ArrayList sLrecursion(String input) { ArrayList data = new ArrayList() { @Override public String toString() { - GenericData.changeTab(true); - String tabs = "\r\n"; - for (int k = 0; k < GenericData.getTab(); k++) { - tabs += "\t"; - } - String out = "{"; - for (Object item : this) { - out += tabs+item; + if (size() != 0 && this.get(0) instanceof VPair) { + GenericData.changeTab(true); + String tabs = "\r\n"; + for (int k = 0; k < GenericData.getTab(); k++) { + tabs += "\t"; + } + String out = "{"; + for (Object item : this) { + out += tabs + item; + } + GenericData.changeTab(false); + return out + tabs.replaceFirst("\t", "") + "}"; + } else { + String out = "{ "; + for (Object item : this) { + out += item + " "; + } + return out + "}"; } - GenericData.changeTab(false); - return out + tabs.replaceFirst("\t", "") + "}"; } }; Matcher matcher = DataPattern.sLre.matcher(input); @@ -182,9 +190,9 @@ public Object load(String input) { if (matcher.group(7).contains("{")) { ValueTriplet dat; Matcher color = DataPattern.color.matcher(matcher.group(7)); - if(color.find()){ - dat = new ValueTriplet<>(matcher.group(6).trim(), new StellarisColor(color.group(1).trim(),color.group(2).trim(),color.group(3).trim(),color.group(4).trim()), size++); - }else{ + if (color.find()) { + dat = new ValueTriplet<>(matcher.group(6).trim(), new StellarisColor(color.group(1).trim(), color.group(2).trim(), color.group(3).trim(), color.group(4).trim()), size++); + } else { dat = new ValueTriplet<>(matcher.group(6).trim(), sLrecursion(matcher.group(7)), size++); } if (data.containsKey(matcher.group(5))) { @@ -207,8 +215,14 @@ public Object load(String input) { } else if (matcher.group(1) != null) { temp = new PairArrayList(); int order = size++; - DataMap secMap = (DataMap) load(matcher.group(4).replaceAll("(?m)^\t", "")); - ValueTriplet dat = new ValueTriplet<>(matcher.group(2).trim(), secMap, order); + DataMap secMap; + ValueTriplet dat; + if(!matcher.group(4).contains("=")){ + dat = new ValueTriplet<>(matcher.group(2).trim(), sLrecursion(matcher.group(4).trim().replaceAll("[\\t\\n\\r]", " ")), order); + }else{ + secMap = (DataMap) load(matcher.group(4).replaceAll("(?m)^\t", "")); + dat = new ValueTriplet<>(matcher.group(2).trim(), secMap, order); + } temp.add(dat); if (data.containsKey(matcher.group(1).trim())) { data.get(matcher.group(1).trim()).add(dat); diff --git a/src/StellarisDK/FileClasses/Helper/DataMap.java b/src/StellarisDK/FileClasses/Helper/DataMap.java index 6eb1ba7..4c48eea 100644 --- a/src/StellarisDK/FileClasses/Helper/DataMap.java +++ b/src/StellarisDK/FileClasses/Helper/DataMap.java @@ -35,10 +35,10 @@ public Object[] compressToPairArray() { try { if (((ValueTriplet) data).getValue() != null) objs[((ValueTriplet) data).getOrder()] = new ValueTriplet<>(key, ((ValueTriplet) data).toPair(), ((ValueTriplet) data).getOrder()); - } catch (StackOverflowError e) { + } catch (ArrayIndexOutOfBoundsException e) { System.out.println("[ERROR] StackOverFlow, FROM: DataMap"); - System.out.println("[ERROR] CAUSE: Tried " + ((ValueTriplet) data).getOrder()); - System.out.println("[ERROR] SIZE: " + getFullSize()); + System.out.println("[ERROR] CAUSE: Tried " + key + " Value: " + ((ValueTriplet) data).getKey()); + System.out.println("[ERROR] SIZE: " + getFullSize() + ", Actual: " + ((ValueTriplet) data).getOrder()); } } }