diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/source/SourceIndex.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/source/SourceIndex.java | 174 |
1 files changed, 0 insertions, 174 deletions
diff --git a/src/main/java/cuchaz/enigma/source/SourceIndex.java b/src/main/java/cuchaz/enigma/source/SourceIndex.java deleted file mode 100644 index 6a335ec..0000000 --- a/src/main/java/cuchaz/enigma/source/SourceIndex.java +++ /dev/null | |||
| @@ -1,174 +0,0 @@ | |||
| 1 | package cuchaz.enigma.source; | ||
| 2 | |||
| 3 | import com.google.common.collect.HashMultimap; | ||
| 4 | import com.google.common.collect.Lists; | ||
| 5 | import com.google.common.collect.Maps; | ||
| 6 | import com.google.common.collect.Multimap; | ||
| 7 | import cuchaz.enigma.analysis.EntryReference; | ||
| 8 | import cuchaz.enigma.analysis.Token; | ||
| 9 | import cuchaz.enigma.gui.SourceRemapper; | ||
| 10 | import cuchaz.enigma.translation.mapping.EntryResolver; | ||
| 11 | import cuchaz.enigma.translation.mapping.ResolutionStrategy; | ||
| 12 | import cuchaz.enigma.translation.representation.entry.Entry; | ||
| 13 | |||
| 14 | import java.util.Collection; | ||
| 15 | import java.util.List; | ||
| 16 | import java.util.Map; | ||
| 17 | import java.util.TreeMap; | ||
| 18 | import java.util.stream.Collectors; | ||
| 19 | |||
| 20 | public class SourceIndex { | ||
| 21 | private String source; | ||
| 22 | private List<Integer> lineOffsets; | ||
| 23 | private final TreeMap<Token, EntryReference<Entry<?>, Entry<?>>> tokenToReference; | ||
| 24 | private final Multimap<EntryReference<Entry<?>, Entry<?>>, Token> referenceToTokens; | ||
| 25 | private final Map<Entry<?>, Token> declarationToToken; | ||
| 26 | |||
| 27 | public SourceIndex() { | ||
| 28 | tokenToReference = new TreeMap<>(); | ||
| 29 | referenceToTokens = HashMultimap.create(); | ||
| 30 | declarationToToken = Maps.newHashMap(); | ||
| 31 | } | ||
| 32 | |||
| 33 | public SourceIndex(String source) { | ||
| 34 | this(); | ||
| 35 | setSource(source); | ||
| 36 | } | ||
| 37 | |||
| 38 | public void setSource(String source) { | ||
| 39 | this.source = source; | ||
| 40 | lineOffsets = Lists.newArrayList(); | ||
| 41 | lineOffsets.add(0); | ||
| 42 | |||
| 43 | for (int i = 0; i < this.source.length(); i++) { | ||
| 44 | if (this.source.charAt(i) == '\n') { | ||
| 45 | lineOffsets.add(i + 1); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | public String getSource() { | ||
| 51 | return source; | ||
| 52 | } | ||
| 53 | |||
| 54 | public int getLineNumber(int position) { | ||
| 55 | int line = 0; | ||
| 56 | |||
| 57 | for (int offset : lineOffsets) { | ||
| 58 | if (offset > position) { | ||
| 59 | break; | ||
| 60 | } | ||
| 61 | |||
| 62 | line++; | ||
| 63 | } | ||
| 64 | |||
| 65 | return line; | ||
| 66 | } | ||
| 67 | |||
| 68 | public int getColumnNumber(int position) { | ||
| 69 | return position - lineOffsets.get(getLineNumber(position) - 1) + 1; | ||
| 70 | } | ||
| 71 | |||
| 72 | public int getPosition(int line, int column) { | ||
| 73 | return lineOffsets.get(line - 1) + column - 1; | ||
| 74 | } | ||
| 75 | |||
| 76 | public Iterable<Entry<?>> declarations() { | ||
| 77 | return declarationToToken.keySet(); | ||
| 78 | } | ||
| 79 | |||
| 80 | public Iterable<Token> declarationTokens() { | ||
| 81 | return declarationToToken.values(); | ||
| 82 | } | ||
| 83 | |||
| 84 | public Token getDeclarationToken(Entry<?> entry) { | ||
| 85 | return declarationToToken.get(entry); | ||
| 86 | } | ||
| 87 | |||
| 88 | public void addDeclaration(Token token, Entry<?> deobfEntry) { | ||
| 89 | if (token != null) { | ||
| 90 | EntryReference<Entry<?>, Entry<?>> reference = new EntryReference<>(deobfEntry, token.text); | ||
| 91 | tokenToReference.put(token, reference); | ||
| 92 | referenceToTokens.put(reference, token); | ||
| 93 | declarationToToken.put(deobfEntry, token); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | public Iterable<EntryReference<Entry<?>, Entry<?>>> references() { | ||
| 98 | return referenceToTokens.keySet(); | ||
| 99 | } | ||
| 100 | |||
| 101 | public EntryReference<Entry<?>, Entry<?>> getReference(Token token) { | ||
| 102 | if (token == null) { | ||
| 103 | return null; | ||
| 104 | } | ||
| 105 | |||
| 106 | return tokenToReference.get(token); | ||
| 107 | } | ||
| 108 | |||
| 109 | public Iterable<Token> referenceTokens() { | ||
| 110 | return tokenToReference.keySet(); | ||
| 111 | } | ||
| 112 | |||
| 113 | public Token getReferenceToken(int pos) { | ||
| 114 | Token token = tokenToReference.floorKey(new Token(pos, pos, null)); | ||
| 115 | |||
| 116 | if (token != null && token.contains(pos)) { | ||
| 117 | return token; | ||
| 118 | } | ||
| 119 | |||
| 120 | return null; | ||
| 121 | } | ||
| 122 | |||
| 123 | public Collection<Token> getReferenceTokens(EntryReference<Entry<?>, Entry<?>> deobfReference) { | ||
| 124 | return referenceToTokens.get(deobfReference); | ||
| 125 | } | ||
| 126 | |||
| 127 | public void addReference(Token token, Entry<?> deobfEntry, Entry<?> deobfContext) { | ||
| 128 | if (token != null) { | ||
| 129 | EntryReference<Entry<?>, Entry<?>> deobfReference = new EntryReference<>(deobfEntry, token.text, deobfContext); | ||
| 130 | tokenToReference.put(token, deobfReference); | ||
| 131 | referenceToTokens.put(deobfReference, token); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | public void resolveReferences(EntryResolver resolver) { | ||
| 136 | // resolve all the classes in the source references | ||
| 137 | for (Token token : Lists.newArrayList(referenceToTokens.values())) { | ||
| 138 | EntryReference<Entry<?>, Entry<?>> reference = tokenToReference.get(token); | ||
| 139 | EntryReference<Entry<?>, Entry<?>> resolvedReference = resolver.resolveFirstReference(reference, ResolutionStrategy.RESOLVE_CLOSEST); | ||
| 140 | |||
| 141 | // replace the reference | ||
| 142 | tokenToReference.replace(token, resolvedReference); | ||
| 143 | |||
| 144 | Collection<Token> tokens = referenceToTokens.removeAll(reference); | ||
| 145 | referenceToTokens.putAll(resolvedReference, tokens); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | public SourceIndex remapTo(SourceRemapper.Result result) { | ||
| 150 | SourceIndex remapped = new SourceIndex(result.getSource()); | ||
| 151 | |||
| 152 | for (Map.Entry<Entry<?>, Token> entry : declarationToToken.entrySet()) { | ||
| 153 | remapped.declarationToToken.put(entry.getKey(), result.getRemappedToken(entry.getValue())); | ||
| 154 | } | ||
| 155 | |||
| 156 | for (Map.Entry<EntryReference<Entry<?>, Entry<?>>, Collection<Token>> entry : referenceToTokens.asMap().entrySet()) { | ||
| 157 | EntryReference<Entry<?>, Entry<?>> reference = entry.getKey(); | ||
| 158 | Collection<Token> oldTokens = entry.getValue(); | ||
| 159 | |||
| 160 | Collection<Token> newTokens = oldTokens | ||
| 161 | .stream() | ||
| 162 | .map(result::getRemappedToken) | ||
| 163 | .collect(Collectors.toList()); | ||
| 164 | |||
| 165 | remapped.referenceToTokens.putAll(reference, newTokens); | ||
| 166 | } | ||
| 167 | |||
| 168 | for (Map.Entry<Token, EntryReference<Entry<?>, Entry<?>>> entry : tokenToReference.entrySet()) { | ||
| 169 | remapped.tokenToReference.put(result.getRemappedToken(entry.getKey()), entry.getValue()); | ||
| 170 | } | ||
| 171 | |||
| 172 | return remapped; | ||
| 173 | } | ||
| 174 | } | ||