diff options
| author | 2015-02-03 22:00:53 -0500 | |
|---|---|---|
| committer | 2015-02-03 22:00:53 -0500 | |
| commit | 52ab426d8fad3dbee7e728f523a35af94facebda (patch) | |
| tree | 146fadfd8e639a909d6c1d6a193e7eddeab0be4a /src/cuchaz/enigma/analysis/SourceIndex.java | |
| download | enigma-fork-52ab426d8fad3dbee7e728f523a35af94facebda.tar.gz enigma-fork-52ab426d8fad3dbee7e728f523a35af94facebda.tar.xz enigma-fork-52ab426d8fad3dbee7e728f523a35af94facebda.zip | |
oops, don't depend on local procyon project
Diffstat (limited to 'src/cuchaz/enigma/analysis/SourceIndex.java')
| -rw-r--r-- | src/cuchaz/enigma/analysis/SourceIndex.java | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/analysis/SourceIndex.java b/src/cuchaz/enigma/analysis/SourceIndex.java new file mode 100644 index 0000000..b43ab61 --- /dev/null +++ b/src/cuchaz/enigma/analysis/SourceIndex.java | |||
| @@ -0,0 +1,173 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2014 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Public License v3.0 | ||
| 5 | * which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/gpl.html | ||
| 7 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.analysis; | ||
| 12 | |||
| 13 | import java.util.Collection; | ||
| 14 | import java.util.List; | ||
| 15 | import java.util.Map; | ||
| 16 | import java.util.TreeMap; | ||
| 17 | |||
| 18 | import com.google.common.collect.HashMultimap; | ||
| 19 | import com.google.common.collect.Lists; | ||
| 20 | import com.google.common.collect.Maps; | ||
| 21 | import com.google.common.collect.Multimap; | ||
| 22 | import com.strobel.decompiler.languages.Region; | ||
| 23 | import com.strobel.decompiler.languages.java.ast.AstNode; | ||
| 24 | import com.strobel.decompiler.languages.java.ast.Identifier; | ||
| 25 | |||
| 26 | import cuchaz.enigma.mapping.Entry; | ||
| 27 | |||
| 28 | public class SourceIndex { | ||
| 29 | |||
| 30 | private String m_source; | ||
| 31 | private TreeMap<Token,EntryReference<Entry,Entry>> m_tokenToReference; | ||
| 32 | private Multimap<EntryReference<Entry,Entry>,Token> m_referenceToTokens; | ||
| 33 | private Map<Entry,Token> m_declarationToToken; | ||
| 34 | private List<Integer> m_lineOffsets; | ||
| 35 | |||
| 36 | public SourceIndex(String source) { | ||
| 37 | m_source = source; | ||
| 38 | m_tokenToReference = Maps.newTreeMap(); | ||
| 39 | m_referenceToTokens = HashMultimap.create(); | ||
| 40 | m_declarationToToken = Maps.newHashMap(); | ||
| 41 | m_lineOffsets = Lists.newArrayList(); | ||
| 42 | |||
| 43 | // count the lines | ||
| 44 | m_lineOffsets.add(0); | ||
| 45 | for (int i = 0; i < source.length(); i++) { | ||
| 46 | if (source.charAt(i) == '\n') { | ||
| 47 | m_lineOffsets.add(i + 1); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | public String getSource() { | ||
| 53 | return m_source; | ||
| 54 | } | ||
| 55 | |||
| 56 | public Token getToken(AstNode node) { | ||
| 57 | |||
| 58 | // get the text of the node | ||
| 59 | String name = ""; | ||
| 60 | if (node instanceof Identifier) { | ||
| 61 | name = ((Identifier)node).getName(); | ||
| 62 | } | ||
| 63 | |||
| 64 | // get a token for this node's region | ||
| 65 | Region region = node.getRegion(); | ||
| 66 | if (region.getBeginLine() == 0 || region.getEndLine() == 0) { | ||
| 67 | // DEBUG | ||
| 68 | System.err.println(String.format("WARNING: %s \"%s\" has invalid region: %s", node.getNodeType(), name, region)); | ||
| 69 | return null; | ||
| 70 | } | ||
| 71 | Token token = new Token( | ||
| 72 | toPos(region.getBeginLine(), region.getBeginColumn()), | ||
| 73 | toPos(region.getEndLine(), region.getEndColumn()), | ||
| 74 | m_source | ||
| 75 | ); | ||
| 76 | if (token.start == 0) { | ||
| 77 | // DEBUG | ||
| 78 | System.err.println(String.format("WARNING: %s \"%s\" has invalid start: %s", node.getNodeType(), name, region)); | ||
| 79 | return null; | ||
| 80 | } | ||
| 81 | |||
| 82 | // DEBUG | ||
| 83 | // System.out.println( String.format( "%s \"%s\" region: %s", node.getNodeType(), name, region ) ); | ||
| 84 | |||
| 85 | // for tokens representing inner classes, make sure we only get the simple name | ||
| 86 | int pos = name.lastIndexOf('$'); | ||
| 87 | if (pos >= 0) { | ||
| 88 | token.end -= pos + 1; | ||
| 89 | } | ||
| 90 | |||
| 91 | return token; | ||
| 92 | } | ||
| 93 | |||
| 94 | public void addReference(AstNode node, Entry deobfEntry, Entry deobfContext) { | ||
| 95 | Token token = getToken(node); | ||
| 96 | if (token != null) { | ||
| 97 | EntryReference<Entry,Entry> deobfReference = new EntryReference<Entry,Entry>(deobfEntry, token.text, deobfContext); | ||
| 98 | m_tokenToReference.put(token, deobfReference); | ||
| 99 | m_referenceToTokens.put(deobfReference, token); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | public void addDeclaration(AstNode node, Entry deobfEntry) { | ||
| 104 | Token token = getToken(node); | ||
| 105 | if (token != null) { | ||
| 106 | EntryReference<Entry,Entry> reference = new EntryReference<Entry,Entry>(deobfEntry, token.text); | ||
| 107 | m_tokenToReference.put(token, reference); | ||
| 108 | m_referenceToTokens.put(reference, token); | ||
| 109 | m_declarationToToken.put(deobfEntry, token); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | public Token getReferenceToken(int pos) { | ||
| 114 | Token token = m_tokenToReference.floorKey(new Token(pos, pos, null)); | ||
| 115 | if (token != null && token.contains(pos)) { | ||
| 116 | return token; | ||
| 117 | } | ||
| 118 | return null; | ||
| 119 | } | ||
| 120 | |||
| 121 | public Collection<Token> getReferenceTokens(EntryReference<Entry,Entry> deobfReference) { | ||
| 122 | return m_referenceToTokens.get(deobfReference); | ||
| 123 | } | ||
| 124 | |||
| 125 | public EntryReference<Entry,Entry> getDeobfReference(Token token) { | ||
| 126 | if (token == null) { | ||
| 127 | return null; | ||
| 128 | } | ||
| 129 | return m_tokenToReference.get(token); | ||
| 130 | } | ||
| 131 | |||
| 132 | public void replaceDeobfReference(Token token, EntryReference<Entry,Entry> newDeobfReference) { | ||
| 133 | EntryReference<Entry,Entry> oldDeobfReference = m_tokenToReference.get(token); | ||
| 134 | m_tokenToReference.put(token, newDeobfReference); | ||
| 135 | Collection<Token> tokens = m_referenceToTokens.get(oldDeobfReference); | ||
| 136 | m_referenceToTokens.removeAll(oldDeobfReference); | ||
| 137 | m_referenceToTokens.putAll(newDeobfReference, tokens); | ||
| 138 | } | ||
| 139 | |||
| 140 | public Iterable<Token> referenceTokens() { | ||
| 141 | return m_tokenToReference.keySet(); | ||
| 142 | } | ||
| 143 | |||
| 144 | public Iterable<Token> declarationTokens() { | ||
| 145 | return m_declarationToToken.values(); | ||
| 146 | } | ||
| 147 | |||
| 148 | public Token getDeclarationToken(Entry deobfEntry) { | ||
| 149 | return m_declarationToToken.get(deobfEntry); | ||
| 150 | } | ||
| 151 | |||
| 152 | public int getLineNumber(int pos) { | ||
| 153 | // line number is 1-based | ||
| 154 | int line = 0; | ||
| 155 | for (Integer offset : m_lineOffsets) { | ||
| 156 | if (offset > pos) { | ||
| 157 | break; | ||
| 158 | } | ||
| 159 | line++; | ||
| 160 | } | ||
| 161 | return line; | ||
| 162 | } | ||
| 163 | |||
| 164 | public int getColumnNumber(int pos) { | ||
| 165 | // column number is 1-based | ||
| 166 | return pos - m_lineOffsets.get(getLineNumber(pos) - 1) + 1; | ||
| 167 | } | ||
| 168 | |||
| 169 | private int toPos(int line, int col) { | ||
| 170 | // line and col are 1-based | ||
| 171 | return m_lineOffsets.get(line - 1) + col - 1; | ||
| 172 | } | ||
| 173 | } | ||