summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Thiakil2018-07-10 17:17:55 +0800
committerGravatar Xander2018-07-10 17:29:04 +0800
commitd980311d214c3e1e0234eb011d5ced6701ce3a5a (patch)
tree31d28744416472094384efcccb58b871b9e31d3c /src
parentMerge pull request #6 from thiakil/speedy-getsources (diff)
downloadenigma-d980311d214c3e1e0234eb011d5ced6701ce3a5a.tar.gz
enigma-d980311d214c3e1e0234eb011d5ced6701ce3a5a.tar.xz
enigma-d980311d214c3e1e0234eb011d5ced6701ce3a5a.zip
Allow reader/writer subclasses to provide the underlying file operations
Diffstat (limited to 'src')
-rw-r--r--src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java26
-rw-r--r--src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java4
-rw-r--r--src/main/java/cuchaz/enigma/throwables/MappingParseException.java7
3 files changed, 27 insertions, 10 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java
index a53793d3..ddbee76f 100644
--- a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java
+++ b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java
@@ -5,8 +5,14 @@ import com.google.common.collect.Queues;
5import cuchaz.enigma.throwables.MappingConflict; 5import cuchaz.enigma.throwables.MappingConflict;
6import cuchaz.enigma.throwables.MappingParseException; 6import cuchaz.enigma.throwables.MappingParseException;
7 7
8import java.io.*; 8import java.io.BufferedReader;
9import java.io.File;
10import java.io.FileInputStream;
11import java.io.IOException;
12import java.io.InputStream;
13import java.io.InputStreamReader;
9import java.util.Deque; 14import java.util.Deque;
15import java.util.function.Supplier;
10 16
11public class MappingsEnigmaReader { 17public class MappingsEnigmaReader {
12 18
@@ -39,7 +45,11 @@ public class MappingsEnigmaReader {
39 } 45 }
40 46
41 public Mappings readFile(Mappings mappings, File file) throws IOException, MappingParseException { 47 public Mappings readFile(Mappings mappings, File file) throws IOException, MappingParseException {
42 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8))) { 48 return readFileStream(mappings, new FileInputStream(file), file::getAbsolutePath);
49 }
50
51 public Mappings readFileStream(Mappings mappings, InputStream stream, Supplier<String> filenameSupplier) throws IOException, MappingParseException {
52 try (BufferedReader in = new BufferedReader(new InputStreamReader(stream, Charsets.UTF_8))) {
43 Deque<Object> mappingStack = Queues.newArrayDeque(); 53 Deque<Object> mappingStack = Queues.newArrayDeque();
44 54
45 int lineNumber = 0; 55 int lineNumber = 0;
@@ -87,7 +97,7 @@ public class MappingsEnigmaReader {
87 97
88 // inner class 98 // inner class
89 if (!(mappingStack.peek() instanceof ClassMapping)) { 99 if (!(mappingStack.peek() instanceof ClassMapping)) {
90 throw new MappingParseException(file, lineNumber, "Unexpected CLASS entry here!"); 100 throw new MappingParseException(filenameSupplier, lineNumber, "Unexpected CLASS entry here!");
91 } 101 }
92 102
93 classMapping = readClass(parts, true); 103 classMapping = readClass(parts, true);
@@ -96,26 +106,26 @@ public class MappingsEnigmaReader {
96 mappingStack.push(classMapping); 106 mappingStack.push(classMapping);
97 } else if (token.equalsIgnoreCase("FIELD")) { 107 } else if (token.equalsIgnoreCase("FIELD")) {
98 if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { 108 if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) {
99 throw new MappingParseException(file, lineNumber, "Unexpected FIELD entry here!"); 109 throw new MappingParseException(filenameSupplier, lineNumber, "Unexpected FIELD entry here!");
100 } 110 }
101 ((ClassMapping) mappingStack.peek()).addFieldMapping(readField(parts)); 111 ((ClassMapping) mappingStack.peek()).addFieldMapping(readField(parts));
102 } else if (token.equalsIgnoreCase("METHOD")) { 112 } else if (token.equalsIgnoreCase("METHOD")) {
103 if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { 113 if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) {
104 throw new MappingParseException(file, lineNumber, "Unexpected METHOD entry here!"); 114 throw new MappingParseException(filenameSupplier, lineNumber, "Unexpected METHOD entry here!");
105 } 115 }
106 MethodMapping methodMapping = readMethod(parts); 116 MethodMapping methodMapping = readMethod(parts);
107 ((ClassMapping) mappingStack.peek()).addMethodMapping(methodMapping); 117 ((ClassMapping) mappingStack.peek()).addMethodMapping(methodMapping);
108 mappingStack.push(methodMapping); 118 mappingStack.push(methodMapping);
109 } else if (token.equalsIgnoreCase("ARG")) { 119 } else if (token.equalsIgnoreCase("ARG")) {
110 if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof MethodMapping)) { 120 if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof MethodMapping)) {
111 throw new MappingParseException(file, lineNumber, "Unexpected ARG entry here!"); 121 throw new MappingParseException(filenameSupplier, lineNumber, "Unexpected ARG entry here!");
112 } 122 }
113 ((MethodMapping) mappingStack.peek()).addArgumentMapping(readArgument(parts)); 123 ((MethodMapping) mappingStack.peek()).addArgumentMapping(readArgument(parts));
114 } 124 }
115 } catch (ArrayIndexOutOfBoundsException | IllegalArgumentException ex) { 125 } catch (ArrayIndexOutOfBoundsException | IllegalArgumentException ex) {
116 throw new MappingParseException(file, lineNumber, "Malformed line:\n" + line); 126 throw new MappingParseException(filenameSupplier, lineNumber, "Malformed line:\n" + line);
117 } catch (MappingConflict e) { 127 } catch (MappingConflict e) {
118 throw new MappingParseException(file, lineNumber, e.getMessage()); 128 throw new MappingParseException(filenameSupplier, lineNumber, e.getMessage());
119 } 129 }
120 } 130 }
121 return mappings; 131 return mappings;
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java
index 1e1c4ddc..b29990f5 100644
--- a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java
+++ b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java
@@ -85,7 +85,7 @@ public class MappingsEnigmaWriter {
85 } 85 }
86 } 86 }
87 87
88 private void write(PrintWriter out, ClassMapping classMapping, int depth) throws IOException { 88 protected void write(PrintWriter out, ClassMapping classMapping, int depth) throws IOException {
89 if (classMapping.getDeobfName() == null) { 89 if (classMapping.getDeobfName() == null) {
90 out.format("%sCLASS %s%s\n", getIndent(depth), classMapping.getObfFullName(), 90 out.format("%sCLASS %s%s\n", getIndent(depth), classMapping.getObfFullName(),
91 classMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : classMapping.getModifier().getFormattedName()); 91 classMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : classMapping.getModifier().getFormattedName());
@@ -134,7 +134,7 @@ public class MappingsEnigmaWriter {
134 out.format("%sARG %d %s\n", getIndent(depth), localVariableMapping.getIndex(), localVariableMapping.getName()); 134 out.format("%sARG %d %s\n", getIndent(depth), localVariableMapping.getIndex(), localVariableMapping.getName());
135 } 135 }
136 136
137 private <T extends Comparable<T>> List<T> sorted(Iterable<T> classes) { 137 protected <T extends Comparable<T>> List<T> sorted(Iterable<T> classes) {
138 List<T> out = new ArrayList<>(); 138 List<T> out = new ArrayList<>();
139 for (T t : classes) { 139 for (T t : classes) {
140 out.add(t); 140 out.add(t);
diff --git a/src/main/java/cuchaz/enigma/throwables/MappingParseException.java b/src/main/java/cuchaz/enigma/throwables/MappingParseException.java
index cc5f650a..b7e6d426 100644
--- a/src/main/java/cuchaz/enigma/throwables/MappingParseException.java
+++ b/src/main/java/cuchaz/enigma/throwables/MappingParseException.java
@@ -12,6 +12,7 @@
12package cuchaz.enigma.throwables; 12package cuchaz.enigma.throwables;
13 13
14import java.io.File; 14import java.io.File;
15import java.util.function.Supplier;
15 16
16public class MappingParseException extends Exception { 17public class MappingParseException extends Exception {
17 18
@@ -25,6 +26,12 @@ public class MappingParseException extends Exception {
25 filePath = file.getAbsolutePath(); 26 filePath = file.getAbsolutePath();
26 } 27 }
27 28
29 public MappingParseException(Supplier<String> filenameProvider, int line, String message) {
30 this.line = line;
31 this.message = message;
32 filePath = filenameProvider.get();
33 }
34
28 @Override 35 @Override
29 public String getMessage() { 36 public String getMessage() {
30 return "Line " + line + ": " + message + " in file " + filePath; 37 return "Line " + line + ": " + message + " in file " + filePath;