summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/convert/MatchesReader.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cuchaz/enigma/convert/MatchesReader.java')
-rw-r--r--src/main/java/cuchaz/enigma/convert/MatchesReader.java105
1 files changed, 0 insertions, 105 deletions
diff --git a/src/main/java/cuchaz/enigma/convert/MatchesReader.java b/src/main/java/cuchaz/enigma/convert/MatchesReader.java
deleted file mode 100644
index 1cf50fa..0000000
--- a/src/main/java/cuchaz/enigma/convert/MatchesReader.java
+++ /dev/null
@@ -1,105 +0,0 @@
1/*******************************************************************************
2 * Copyright (c) 2015 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser General Public
5 * License v3.0 which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 * <p>
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11
12package cuchaz.enigma.convert;
13
14import com.google.common.collect.Lists;
15import cuchaz.enigma.mapping.*;
16
17import java.io.*;
18import java.nio.charset.Charset;
19import java.util.Collection;
20import java.util.List;
21
22public class MatchesReader {
23
24 public static ClassMatches readClasses(File file)
25 throws IOException {
26 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")))) {
27 ClassMatches matches = new ClassMatches();
28 String line;
29 while ((line = in.readLine()) != null) {
30 matches.add(readClassMatch(line));
31 }
32 return matches;
33 }
34 }
35
36 private static ClassMatch readClassMatch(String line) {
37 String[] sides = line.split(":", 2);
38 return new ClassMatch(readClasses(sides[0]), readClasses(sides[1]));
39 }
40
41 private static Collection<ClassEntry> readClasses(String in) {
42 List<ClassEntry> entries = Lists.newArrayList();
43 for (String className : in.split(",")) {
44 className = className.trim();
45 if (!className.isEmpty()) {
46 entries.add(new ClassEntry(className));
47 }
48 }
49 return entries;
50 }
51
52 public static <T extends Entry> MemberMatches<T> readMembers(File file)
53 throws IOException {
54 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")))) {
55 MemberMatches<T> matches = new MemberMatches<>();
56 String line;
57 while ((line = in.readLine()) != null) {
58 readMemberMatch(matches, line);
59 }
60 return matches;
61 }
62 }
63
64 private static <T extends Entry> void readMemberMatch(MemberMatches<T> matches, String line) {
65 if (line.startsWith("!")) {
66 T source = readEntry(line.substring(1));
67 matches.addUnmatchableSourceEntry(source);
68 } else {
69 String[] parts = line.split(":", 2);
70 T source = readEntry(parts[0]);
71 T dest = readEntry(parts[1]);
72 if (source != null && dest != null) {
73 matches.addMatch(source, dest);
74 } else if (source != null) {
75 matches.addUnmatchedSourceEntry(source);
76 } else if (dest != null) {
77 matches.addUnmatchedDestEntry(dest);
78 }
79 }
80 }
81
82 @SuppressWarnings("unchecked")
83 private static <T extends Entry> T readEntry(String in) {
84 if (in.length() <= 0) {
85 return null;
86 }
87 String[] parts = in.split(" ");
88 if (parts.length == 3 && parts[2].indexOf('(') < 0) {
89 return (T) new FieldEntry(
90 new ClassEntry(parts[0]),
91 parts[1],
92 new Type(parts[2])
93 );
94 } else {
95 assert (parts.length == 2 || parts.length == 3);
96 if (parts.length == 2) {
97 return (T) EntryFactory.getBehaviorEntry(parts[0], parts[1]);
98 } else if (parts.length == 3) {
99 return (T) EntryFactory.getBehaviorEntry(parts[0], parts[1], parts[2]);
100 } else {
101 throw new Error("Malformed behavior entry: " + in);
102 }
103 }
104 }
105}