summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/convert/MatchesReader.java
diff options
context:
space:
mode:
authorGravatar Thog2017-03-08 08:17:04 +0100
committerGravatar Thog2017-03-08 08:17:04 +0100
commit6e464ea251cab63c776ece0b2a356f1498ffa294 (patch)
tree5ed30c03f5ac4cd2d6877874f5ede576049954f7 /src/main/java/cuchaz/enigma/convert/MatchesReader.java
parentDrop unix case style and implement hashCode when equals is overrided (diff)
downloadenigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.tar.gz
enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.tar.xz
enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.zip
Follow Fabric guidelines
Diffstat (limited to 'src/main/java/cuchaz/enigma/convert/MatchesReader.java')
-rw-r--r--src/main/java/cuchaz/enigma/convert/MatchesReader.java157
1 files changed, 78 insertions, 79 deletions
diff --git a/src/main/java/cuchaz/enigma/convert/MatchesReader.java b/src/main/java/cuchaz/enigma/convert/MatchesReader.java
index d86d6c2..1cf50fa 100644
--- a/src/main/java/cuchaz/enigma/convert/MatchesReader.java
+++ b/src/main/java/cuchaz/enigma/convert/MatchesReader.java
@@ -8,99 +8,98 @@
8 * Contributors: 8 * Contributors:
9 * Jeff Martin - initial API and implementation 9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/ 10 ******************************************************************************/
11
11package cuchaz.enigma.convert; 12package cuchaz.enigma.convert;
12 13
13import com.google.common.collect.Lists; 14import com.google.common.collect.Lists;
15import cuchaz.enigma.mapping.*;
14 16
15import java.io.*; 17import java.io.*;
16import java.nio.charset.Charset; 18import java.nio.charset.Charset;
17import java.util.Collection; 19import java.util.Collection;
18import java.util.List; 20import java.util.List;
19 21
20import cuchaz.enigma.mapping.*;
21
22
23public class MatchesReader { 22public class MatchesReader {
24 23
25 public static ClassMatches readClasses(File file) 24 public static ClassMatches readClasses(File file)
26 throws IOException { 25 throws IOException {
27 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")))) { 26 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")))) {
28 ClassMatches matches = new ClassMatches(); 27 ClassMatches matches = new ClassMatches();
29 String line; 28 String line;
30 while ((line = in.readLine()) != null) { 29 while ((line = in.readLine()) != null) {
31 matches.add(readClassMatch(line)); 30 matches.add(readClassMatch(line));
32 } 31 }
33 return matches; 32 return matches;
34 } 33 }
35 } 34 }
36 35
37 private static ClassMatch readClassMatch(String line) { 36 private static ClassMatch readClassMatch(String line) {
38 String[] sides = line.split(":", 2); 37 String[] sides = line.split(":", 2);
39 return new ClassMatch(readClasses(sides[0]), readClasses(sides[1])); 38 return new ClassMatch(readClasses(sides[0]), readClasses(sides[1]));
40 } 39 }
41 40
42 private static Collection<ClassEntry> readClasses(String in) { 41 private static Collection<ClassEntry> readClasses(String in) {
43 List<ClassEntry> entries = Lists.newArrayList(); 42 List<ClassEntry> entries = Lists.newArrayList();
44 for (String className : in.split(",")) { 43 for (String className : in.split(",")) {
45 className = className.trim(); 44 className = className.trim();
46 if (className.length() > 0) { 45 if (!className.isEmpty()) {
47 entries.add(new ClassEntry(className)); 46 entries.add(new ClassEntry(className));
48 } 47 }
49 } 48 }
50 return entries; 49 return entries;
51 } 50 }
52 51
53 public static <T extends Entry> MemberMatches<T> readMembers(File file) 52 public static <T extends Entry> MemberMatches<T> readMembers(File file)
54 throws IOException { 53 throws IOException {
55 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")))) { 54 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")))) {
56 MemberMatches<T> matches = new MemberMatches<>(); 55 MemberMatches<T> matches = new MemberMatches<>();
57 String line; 56 String line;
58 while ((line = in.readLine()) != null) { 57 while ((line = in.readLine()) != null) {
59 readMemberMatch(matches, line); 58 readMemberMatch(matches, line);
60 } 59 }
61 return matches; 60 return matches;
62 } 61 }
63 } 62 }
64 63
65 private static <T extends Entry> void readMemberMatch(MemberMatches<T> matches, String line) { 64 private static <T extends Entry> void readMemberMatch(MemberMatches<T> matches, String line) {
66 if (line.startsWith("!")) { 65 if (line.startsWith("!")) {
67 T source = readEntry(line.substring(1)); 66 T source = readEntry(line.substring(1));
68 matches.addUnmatchableSourceEntry(source); 67 matches.addUnmatchableSourceEntry(source);
69 } else { 68 } else {
70 String[] parts = line.split(":", 2); 69 String[] parts = line.split(":", 2);
71 T source = readEntry(parts[0]); 70 T source = readEntry(parts[0]);
72 T dest = readEntry(parts[1]); 71 T dest = readEntry(parts[1]);
73 if (source != null && dest != null) { 72 if (source != null && dest != null) {
74 matches.addMatch(source, dest); 73 matches.addMatch(source, dest);
75 } else if (source != null) { 74 } else if (source != null) {
76 matches.addUnmatchedSourceEntry(source); 75 matches.addUnmatchedSourceEntry(source);
77 } else if (dest != null) { 76 } else if (dest != null) {
78 matches.addUnmatchedDestEntry(dest); 77 matches.addUnmatchedDestEntry(dest);
79 } 78 }
80 } 79 }
81 } 80 }
82 81
83 @SuppressWarnings("unchecked") 82 @SuppressWarnings("unchecked")
84 private static <T extends Entry> T readEntry(String in) { 83 private static <T extends Entry> T readEntry(String in) {
85 if (in.length() <= 0) { 84 if (in.length() <= 0) {
86 return null; 85 return null;
87 } 86 }
88 String[] parts = in.split(" "); 87 String[] parts = in.split(" ");
89 if (parts.length == 3 && parts[2].indexOf('(') < 0) { 88 if (parts.length == 3 && parts[2].indexOf('(') < 0) {
90 return (T) new FieldEntry( 89 return (T) new FieldEntry(
91 new ClassEntry(parts[0]), 90 new ClassEntry(parts[0]),
92 parts[1], 91 parts[1],
93 new Type(parts[2]) 92 new Type(parts[2])
94 ); 93 );
95 } else { 94 } else {
96 assert (parts.length == 2 || parts.length == 3); 95 assert (parts.length == 2 || parts.length == 3);
97 if (parts.length == 2) { 96 if (parts.length == 2) {
98 return (T) EntryFactory.getBehaviorEntry(parts[0], parts[1]); 97 return (T) EntryFactory.getBehaviorEntry(parts[0], parts[1]);
99 } else if (parts.length == 3) { 98 } else if (parts.length == 3) {
100 return (T) EntryFactory.getBehaviorEntry(parts[0], parts[1], parts[2]); 99 return (T) EntryFactory.getBehaviorEntry(parts[0], parts[1], parts[2]);
101 } else { 100 } else {
102 throw new Error("Malformed behavior entry: " + in); 101 throw new Error("Malformed behavior entry: " + in);
103 } 102 }
104 } 103 }
105 } 104 }
106} 105}