diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/analysis/SourceIndex.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/analysis/SourceIndex.java | 53 |
1 files changed, 26 insertions, 27 deletions
diff --git a/src/main/java/cuchaz/enigma/analysis/SourceIndex.java b/src/main/java/cuchaz/enigma/analysis/SourceIndex.java index 3e0d66b..abdec92 100644 --- a/src/main/java/cuchaz/enigma/analysis/SourceIndex.java +++ b/src/main/java/cuchaz/enigma/analysis/SourceIndex.java | |||
| @@ -11,17 +11,15 @@ | |||
| 11 | 11 | ||
| 12 | package cuchaz.enigma.analysis; | 12 | package cuchaz.enigma.analysis; |
| 13 | 13 | ||
| 14 | import com.google.common.collect.HashMultimap; | 14 | import com.google.common.collect.*; |
| 15 | import com.google.common.collect.Lists; | ||
| 16 | import com.google.common.collect.Maps; | ||
| 17 | import com.google.common.collect.Multimap; | ||
| 18 | import com.strobel.decompiler.languages.Region; | 15 | import com.strobel.decompiler.languages.Region; |
| 19 | import com.strobel.decompiler.languages.java.ast.AstNode; | 16 | import com.strobel.decompiler.languages.java.ast.AstNode; |
| 20 | import com.strobel.decompiler.languages.java.ast.ConstructorDeclaration; | 17 | import com.strobel.decompiler.languages.java.ast.ConstructorDeclaration; |
| 21 | import com.strobel.decompiler.languages.java.ast.Identifier; | 18 | import com.strobel.decompiler.languages.java.ast.Identifier; |
| 22 | import com.strobel.decompiler.languages.java.ast.TypeDeclaration; | 19 | import com.strobel.decompiler.languages.java.ast.TypeDeclaration; |
| 23 | import cuchaz.enigma.mapping.entry.Entry; | 20 | import cuchaz.enigma.translation.representation.entry.Entry; |
| 24 | 21 | ||
| 22 | import javax.annotation.Nullable; | ||
| 25 | import java.util.*; | 23 | import java.util.*; |
| 26 | import java.util.regex.Pattern; | 24 | import java.util.regex.Pattern; |
| 27 | 25 | ||
| @@ -29,9 +27,9 @@ public class SourceIndex { | |||
| 29 | private static Pattern ANONYMOUS_INNER = Pattern.compile("\\$\\d+$"); | 27 | private static Pattern ANONYMOUS_INNER = Pattern.compile("\\$\\d+$"); |
| 30 | 28 | ||
| 31 | private String source; | 29 | private String source; |
| 32 | private TreeMap<Token, EntryReference<Entry, Entry>> tokenToReference; | 30 | private TreeMap<Token, EntryReference<Entry<?>, Entry<?>>> tokenToReference; |
| 33 | private Multimap<EntryReference<Entry, Entry>, Token> referenceToTokens; | 31 | private Multimap<EntryReference<Entry<?>, Entry<?>>, Token> referenceToTokens; |
| 34 | private Map<Entry, Token> declarationToToken; | 32 | private Map<Entry<?>, Token> declarationToToken; |
| 35 | private List<Integer> lineOffsets; | 33 | private List<Integer> lineOffsets; |
| 36 | private boolean ignoreBadTokens; | 34 | private boolean ignoreBadTokens; |
| 37 | 35 | ||
| @@ -42,7 +40,7 @@ public class SourceIndex { | |||
| 42 | public SourceIndex(String source, boolean ignoreBadTokens) { | 40 | public SourceIndex(String source, boolean ignoreBadTokens) { |
| 43 | this.source = source; | 41 | this.source = source; |
| 44 | this.ignoreBadTokens = ignoreBadTokens; | 42 | this.ignoreBadTokens = ignoreBadTokens; |
| 45 | this.tokenToReference = Maps.newTreeMap(); | 43 | this.tokenToReference = new TreeMap<>(); |
| 46 | this.referenceToTokens = HashMultimap.create(); | 44 | this.referenceToTokens = HashMultimap.create(); |
| 47 | this.declarationToToken = Maps.newHashMap(); | 45 | this.declarationToToken = Maps.newHashMap(); |
| 48 | calculateLineOffsets(); | 46 | calculateLineOffsets(); |
| @@ -63,12 +61,12 @@ public class SourceIndex { | |||
| 63 | this.source = source; | 61 | this.source = source; |
| 64 | calculateLineOffsets(); | 62 | calculateLineOffsets(); |
| 65 | 63 | ||
| 66 | for (Entry entry : Lists.newArrayList(declarationToToken.keySet())) { | 64 | for (Entry<?> entry : Lists.newArrayList(declarationToToken.keySet())) { |
| 67 | Token token = declarationToToken.get(entry); | 65 | Token token = declarationToToken.get(entry); |
| 68 | declarationToToken.put(entry, tokenMap.getOrDefault(token, token)); | 66 | declarationToToken.put(entry, tokenMap.getOrDefault(token, token)); |
| 69 | } | 67 | } |
| 70 | 68 | ||
| 71 | for (EntryReference<Entry, Entry> ref : referenceToTokens.keySet()) { | 69 | for (EntryReference<Entry<?>, Entry<?>> ref : referenceToTokens.keySet()) { |
| 72 | Collection<Token> oldTokens = referenceToTokens.get(ref); | 70 | Collection<Token> oldTokens = referenceToTokens.get(ref); |
| 73 | List<Token> newTokens = new ArrayList<>(oldTokens.size()); | 71 | List<Token> newTokens = new ArrayList<>(oldTokens.size()); |
| 74 | 72 | ||
| @@ -79,7 +77,8 @@ public class SourceIndex { | |||
| 79 | referenceToTokens.replaceValues(ref, newTokens); | 77 | referenceToTokens.replaceValues(ref, newTokens); |
| 80 | } | 78 | } |
| 81 | 79 | ||
| 82 | Map<Token, EntryReference<Entry, Entry>> tokenToReferenceCopy = Maps.newHashMap(tokenToReference); | 80 | TreeMap<Token, EntryReference<Entry<?>, Entry<?>>> tokenToReferenceCopy = new TreeMap<>(tokenToReference); |
| 81 | |||
| 83 | tokenToReference.clear(); | 82 | tokenToReference.clear(); |
| 84 | for (Token token : tokenToReferenceCopy.keySet()) { | 83 | for (Token token : tokenToReferenceCopy.keySet()) { |
| 85 | tokenToReference.put(tokenMap.getOrDefault(token, token), tokenToReferenceCopy.get(token)); | 84 | tokenToReference.put(tokenMap.getOrDefault(token, token), tokenToReferenceCopy.get(token)); |
| @@ -112,9 +111,9 @@ public class SourceIndex { | |||
| 112 | return null; | 111 | return null; |
| 113 | } | 112 | } |
| 114 | 113 | ||
| 115 | if (node instanceof Identifier && name.indexOf('$') >=0 && node.getParent() instanceof ConstructorDeclaration && name.lastIndexOf('$') >= 0 && !ANONYMOUS_INNER.matcher(name).matches()){ | 114 | if (node instanceof Identifier && name.indexOf('$') >= 0 && node.getParent() instanceof ConstructorDeclaration && name.lastIndexOf('$') >= 0 && !ANONYMOUS_INNER.matcher(name).matches()) { |
| 116 | TypeDeclaration type = node.getParent().getParent() instanceof TypeDeclaration ? (TypeDeclaration) node.getParent().getParent() : null; | 115 | TypeDeclaration type = node.getParent().getParent() instanceof TypeDeclaration ? (TypeDeclaration) node.getParent().getParent() : null; |
| 117 | if (type != null){ | 116 | if (type != null) { |
| 118 | name = type.getName(); | 117 | name = type.getName(); |
| 119 | token.end = token.start + name.length(); | 118 | token.end = token.start + name.length(); |
| 120 | } | 119 | } |
| @@ -133,19 +132,19 @@ public class SourceIndex { | |||
| 133 | return token; | 132 | return token; |
| 134 | } | 133 | } |
| 135 | 134 | ||
| 136 | public void addReference(AstNode node, Entry deobfEntry, Entry deobfContext) { | 135 | public void addReference(AstNode node, Entry<?> deobfEntry, Entry<?> deobfContext) { |
| 137 | Token token = getToken(node); | 136 | Token token = getToken(node); |
| 138 | if (token != null) { | 137 | if (token != null) { |
| 139 | EntryReference<Entry, Entry> deobfReference = new EntryReference<>(deobfEntry, token.text, deobfContext); | 138 | EntryReference<Entry<?>, Entry<?>> deobfReference = new EntryReference<>(deobfEntry, token.text, deobfContext); |
| 140 | this.tokenToReference.put(token, deobfReference); | 139 | this.tokenToReference.put(token, deobfReference); |
| 141 | this.referenceToTokens.put(deobfReference, token); | 140 | this.referenceToTokens.put(deobfReference, token); |
| 142 | } | 141 | } |
| 143 | } | 142 | } |
| 144 | 143 | ||
| 145 | public void addDeclaration(AstNode node, Entry deobfEntry) { | 144 | public void addDeclaration(AstNode node, Entry<?> deobfEntry) { |
| 146 | Token token = getToken(node); | 145 | Token token = getToken(node); |
| 147 | if (token != null) { | 146 | if (token != null) { |
| 148 | EntryReference<Entry, Entry> reference = new EntryReference<>(deobfEntry, token.text); | 147 | EntryReference<Entry<?>, Entry<?>> reference = new EntryReference<>(deobfEntry, token.text); |
| 149 | this.tokenToReference.put(token, reference); | 148 | this.tokenToReference.put(token, reference); |
| 150 | this.referenceToTokens.put(reference, token); | 149 | this.referenceToTokens.put(reference, token); |
| 151 | this.declarationToToken.put(deobfEntry, token); | 150 | this.declarationToToken.put(deobfEntry, token); |
| @@ -160,22 +159,22 @@ public class SourceIndex { | |||
| 160 | return null; | 159 | return null; |
| 161 | } | 160 | } |
| 162 | 161 | ||
| 163 | public Collection<Token> getReferenceTokens(EntryReference<Entry, Entry> deobfReference) { | 162 | public Collection<Token> getReferenceTokens(EntryReference<Entry<?>, Entry<?>> deobfReference) { |
| 164 | return this.referenceToTokens.get(deobfReference); | 163 | return this.referenceToTokens.get(deobfReference); |
| 165 | } | 164 | } |
| 166 | 165 | ||
| 167 | public EntryReference<Entry, Entry> getDeobfReference(Token token) { | 166 | @Nullable |
| 167 | public EntryReference<Entry<?>, Entry<?>> getDeobfReference(Token token) { | ||
| 168 | if (token == null) { | 168 | if (token == null) { |
| 169 | return null; | 169 | return null; |
| 170 | } | 170 | } |
| 171 | return this.tokenToReference.get(token); | 171 | return this.tokenToReference.get(token); |
| 172 | } | 172 | } |
| 173 | 173 | ||
| 174 | public void replaceDeobfReference(Token token, EntryReference<Entry, Entry> newDeobfReference) { | 174 | public void replaceDeobfReference(Token token, EntryReference<Entry<?>, Entry<?>> newDeobfReference) { |
| 175 | EntryReference<Entry, Entry> oldDeobfReference = this.tokenToReference.get(token); | 175 | EntryReference<Entry<?>, Entry<?>> oldDeobfReferences = this.tokenToReference.replace(token, newDeobfReference); |
| 176 | this.tokenToReference.put(token, newDeobfReference); | 176 | |
| 177 | Collection<Token> tokens = this.referenceToTokens.get(oldDeobfReference); | 177 | Collection<Token> tokens = this.referenceToTokens.removeAll(oldDeobfReferences); |
| 178 | this.referenceToTokens.removeAll(oldDeobfReference); | ||
| 179 | this.referenceToTokens.putAll(newDeobfReference, tokens); | 178 | this.referenceToTokens.putAll(newDeobfReference, tokens); |
| 180 | } | 179 | } |
| 181 | 180 | ||
| @@ -187,11 +186,11 @@ public class SourceIndex { | |||
| 187 | return this.declarationToToken.values(); | 186 | return this.declarationToToken.values(); |
| 188 | } | 187 | } |
| 189 | 188 | ||
| 190 | public Iterable<Entry> declarations() { | 189 | public Iterable<Entry<?>> declarations() { |
| 191 | return this.declarationToToken.keySet(); | 190 | return this.declarationToToken.keySet(); |
| 192 | } | 191 | } |
| 193 | 192 | ||
| 194 | public Token getDeclarationToken(Entry deobfEntry) { | 193 | public Token getDeclarationToken(Entry<?> deobfEntry) { |
| 195 | return this.declarationToToken.get(deobfEntry); | 194 | return this.declarationToToken.get(deobfEntry); |
| 196 | } | 195 | } |
| 197 | 196 | ||