diff options
| author | 2016-06-30 00:49:21 +1000 | |
|---|---|---|
| committer | 2016-06-30 00:49:21 +1000 | |
| commit | 4be005617b3b8c3578cca07c5d085d12916f0d1d (patch) | |
| tree | db163431f38703e26da417ef05eaea2b27a498b9 /src/cuchaz/enigma/convert/MatchesReader.java | |
| parent | Some small changes to fix idea importing (diff) | |
| download | enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.tar.gz enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.tar.xz enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.zip | |
Json format (#2)
* Added new format
* Fixed bug
* Updated Version
Diffstat (limited to 'src/cuchaz/enigma/convert/MatchesReader.java')
| -rw-r--r-- | src/cuchaz/enigma/convert/MatchesReader.java | 113 |
1 files changed, 0 insertions, 113 deletions
diff --git a/src/cuchaz/enigma/convert/MatchesReader.java b/src/cuchaz/enigma/convert/MatchesReader.java deleted file mode 100644 index 7514e2a..0000000 --- a/src/cuchaz/enigma/convert/MatchesReader.java +++ /dev/null | |||
| @@ -1,113 +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 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.convert; | ||
| 12 | |||
| 13 | import java.io.BufferedReader; | ||
| 14 | import java.io.File; | ||
| 15 | import java.io.FileReader; | ||
| 16 | import java.io.IOException; | ||
| 17 | import java.util.Collection; | ||
| 18 | import java.util.List; | ||
| 19 | |||
| 20 | import com.google.common.collect.Lists; | ||
| 21 | |||
| 22 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 23 | import cuchaz.enigma.mapping.Entry; | ||
| 24 | import cuchaz.enigma.mapping.EntryFactory; | ||
| 25 | import cuchaz.enigma.mapping.FieldEntry; | ||
| 26 | import cuchaz.enigma.mapping.Type; | ||
| 27 | |||
| 28 | |||
| 29 | public class MatchesReader { | ||
| 30 | |||
| 31 | public static ClassMatches readClasses(File file) | ||
| 32 | throws IOException { | ||
| 33 | try (BufferedReader in = new BufferedReader(new FileReader(file))) { | ||
| 34 | ClassMatches matches = new ClassMatches(); | ||
| 35 | String line = null; | ||
| 36 | while ((line = in.readLine()) != null) { | ||
| 37 | matches.add(readClassMatch(line)); | ||
| 38 | } | ||
| 39 | return matches; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | private static ClassMatch readClassMatch(String line) | ||
| 44 | throws IOException { | ||
| 45 | String[] sides = line.split(":", 2); | ||
| 46 | return new ClassMatch(readClasses(sides[0]), readClasses(sides[1])); | ||
| 47 | } | ||
| 48 | |||
| 49 | private static Collection<ClassEntry> readClasses(String in) { | ||
| 50 | List<ClassEntry> entries = Lists.newArrayList(); | ||
| 51 | for (String className : in.split(",")) { | ||
| 52 | className = className.trim(); | ||
| 53 | if (className.length() > 0) { | ||
| 54 | entries.add(new ClassEntry(className)); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | return entries; | ||
| 58 | } | ||
| 59 | |||
| 60 | public static <T extends Entry> MemberMatches<T> readMembers(File file) | ||
| 61 | throws IOException { | ||
| 62 | try (BufferedReader in = new BufferedReader(new FileReader(file))) { | ||
| 63 | MemberMatches<T> matches = new MemberMatches<T>(); | ||
| 64 | String line = null; | ||
| 65 | while ((line = in.readLine()) != null) { | ||
| 66 | readMemberMatch(matches, line); | ||
| 67 | } | ||
| 68 | return matches; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | private static <T extends Entry> void readMemberMatch(MemberMatches<T> matches, String line) { | ||
| 73 | if (line.startsWith("!")) { | ||
| 74 | T source = readEntry(line.substring(1)); | ||
| 75 | matches.addUnmatchableSourceEntry(source); | ||
| 76 | } else { | ||
| 77 | String[] parts = line.split(":", 2); | ||
| 78 | T source = readEntry(parts[0]); | ||
| 79 | T dest = readEntry(parts[1]); | ||
| 80 | if (source != null && dest != null) { | ||
| 81 | matches.addMatch(source, dest); | ||
| 82 | } else if (source != null) { | ||
| 83 | matches.addUnmatchedSourceEntry(source); | ||
| 84 | } else if (dest != null) { | ||
| 85 | matches.addUnmatchedDestEntry(dest); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | @SuppressWarnings("unchecked") | ||
| 91 | private static <T extends Entry> T readEntry(String in) { | ||
| 92 | if (in.length() <= 0) { | ||
| 93 | return null; | ||
| 94 | } | ||
| 95 | String[] parts = in.split(" "); | ||
| 96 | if (parts.length == 3 && parts[2].indexOf('(') < 0) { | ||
| 97 | return (T)new FieldEntry( | ||
| 98 | new ClassEntry(parts[0]), | ||
| 99 | parts[1], | ||
| 100 | new Type(parts[2]) | ||
| 101 | ); | ||
| 102 | } else { | ||
| 103 | assert(parts.length == 2 || parts.length == 3); | ||
| 104 | if (parts.length == 2) { | ||
| 105 | return (T)EntryFactory.getBehaviorEntry(parts[0], parts[1]); | ||
| 106 | } else if (parts.length == 3) { | ||
| 107 | return (T)EntryFactory.getBehaviorEntry(parts[0], parts[1], parts[2]); | ||
| 108 | } else { | ||
| 109 | throw new Error("Malformed behavior entry: " + in); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||