diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/MethodMapping.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MethodMapping.java | 210 |
1 files changed, 0 insertions, 210 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MethodMapping.java b/src/main/java/cuchaz/enigma/mapping/MethodMapping.java deleted file mode 100644 index 2f10144..0000000 --- a/src/main/java/cuchaz/enigma/mapping/MethodMapping.java +++ /dev/null | |||
| @@ -1,210 +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 | |||
| 12 | package cuchaz.enigma.mapping; | ||
| 13 | |||
| 14 | import com.google.common.base.Preconditions; | ||
| 15 | import com.google.common.collect.Maps; | ||
| 16 | import cuchaz.enigma.mapping.entry.ClassEntry; | ||
| 17 | import cuchaz.enigma.mapping.entry.MethodEntry; | ||
| 18 | import cuchaz.enigma.throwables.IllegalNameException; | ||
| 19 | import cuchaz.enigma.throwables.MappingConflict; | ||
| 20 | |||
| 21 | import java.util.Map; | ||
| 22 | |||
| 23 | public class MethodMapping implements Comparable<MethodMapping>, MemberMapping<MethodEntry> { | ||
| 24 | |||
| 25 | private String obfName; | ||
| 26 | private String deobfName; | ||
| 27 | private MethodDescriptor obfDescriptor; | ||
| 28 | private Map<Integer, LocalVariableMapping> localVariables; | ||
| 29 | private Mappings.EntryModifier modifier; | ||
| 30 | |||
| 31 | public MethodMapping(String obfName, MethodDescriptor obfDescriptor) { | ||
| 32 | this(obfName, obfDescriptor, null, Mappings.EntryModifier.UNCHANGED); | ||
| 33 | } | ||
| 34 | |||
| 35 | public MethodMapping(String obfName, MethodDescriptor obfDescriptor, String deobfName) { | ||
| 36 | this(obfName, obfDescriptor, deobfName, Mappings.EntryModifier.UNCHANGED); | ||
| 37 | } | ||
| 38 | |||
| 39 | public MethodMapping(String obfName, MethodDescriptor obfDescriptor, String deobfName, Mappings.EntryModifier modifier) { | ||
| 40 | Preconditions.checkNotNull(obfName, "Method obf name cannot be null"); | ||
| 41 | Preconditions.checkNotNull(obfDescriptor, "Method obf desc cannot be null"); | ||
| 42 | this.obfName = obfName; | ||
| 43 | this.deobfName = NameValidator.validateMethodName(deobfName); | ||
| 44 | this.obfDescriptor = obfDescriptor; | ||
| 45 | this.localVariables = Maps.newTreeMap(); | ||
| 46 | this.modifier = modifier; | ||
| 47 | } | ||
| 48 | |||
| 49 | public MethodMapping(MethodMapping other, Translator translator) { | ||
| 50 | this.obfName = other.obfName; | ||
| 51 | this.deobfName = other.deobfName; | ||
| 52 | this.modifier = other.modifier; | ||
| 53 | this.obfDescriptor = translator.getTranslatedMethodDesc(other.obfDescriptor); | ||
| 54 | this.localVariables = Maps.newTreeMap(); | ||
| 55 | for (Map.Entry<Integer, LocalVariableMapping> entry : other.localVariables.entrySet()) { | ||
| 56 | this.localVariables.put(entry.getKey(), new LocalVariableMapping(entry.getValue())); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | @Override | ||
| 61 | public String getObfName() { | ||
| 62 | return this.obfName; | ||
| 63 | } | ||
| 64 | |||
| 65 | public void setObfName(String name) { | ||
| 66 | try { | ||
| 67 | NameValidator.validateMethodName(name); | ||
| 68 | } catch (IllegalNameException ex) { | ||
| 69 | // Invalid name, damn obfuscation! Map to a deob name with another name to avoid issues | ||
| 70 | if (this.deobfName == null) { | ||
| 71 | System.err.println("WARNING: " + name + " is conflicting, auto deobfuscate to " + (name + "_auto_deob")); | ||
| 72 | setDeobfName(name + "_auto_deob"); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | this.obfName = name; | ||
| 76 | } | ||
| 77 | |||
| 78 | public String getDeobfName() { | ||
| 79 | if (deobfName == null) { | ||
| 80 | return obfName; | ||
| 81 | } | ||
| 82 | return this.deobfName; | ||
| 83 | } | ||
| 84 | |||
| 85 | public void setDeobfName(String val) { | ||
| 86 | this.deobfName = NameValidator.validateMethodName(val); | ||
| 87 | } | ||
| 88 | |||
| 89 | public MethodDescriptor getObfDesc() { | ||
| 90 | return this.obfDescriptor; | ||
| 91 | } | ||
| 92 | |||
| 93 | public void setObfDescriptor(MethodDescriptor val) { | ||
| 94 | this.obfDescriptor = val; | ||
| 95 | } | ||
| 96 | |||
| 97 | public Iterable<LocalVariableMapping> arguments() { | ||
| 98 | return this.localVariables.values(); | ||
| 99 | } | ||
| 100 | |||
| 101 | public void addArgumentMapping(LocalVariableMapping localVariableMapping) throws MappingConflict { | ||
| 102 | if (this.localVariables.containsKey(localVariableMapping.getIndex())) { | ||
| 103 | throw new MappingConflict("argument", localVariableMapping.getName(), this.localVariables.get(localVariableMapping.getIndex()).getName()); | ||
| 104 | } | ||
| 105 | this.localVariables.put(localVariableMapping.getIndex(), localVariableMapping); | ||
| 106 | } | ||
| 107 | |||
| 108 | public String getObfLocalVariableName(int index) { | ||
| 109 | LocalVariableMapping localVariableMapping = this.localVariables.get(index); | ||
| 110 | if (localVariableMapping != null) { | ||
| 111 | return localVariableMapping.getName(); | ||
| 112 | } | ||
| 113 | |||
| 114 | return null; | ||
| 115 | } | ||
| 116 | |||
| 117 | public String getDeobfLocalVariableName(int index) { | ||
| 118 | LocalVariableMapping localVariableMapping = this.localVariables.get(index); | ||
| 119 | if (localVariableMapping != null) { | ||
| 120 | return localVariableMapping.getName(); | ||
| 121 | } | ||
| 122 | |||
| 123 | return null; | ||
| 124 | } | ||
| 125 | |||
| 126 | public void setLocalVariableName(int index, String name) { | ||
| 127 | LocalVariableMapping localVariableMapping = this.localVariables.get(index); | ||
| 128 | if (localVariableMapping == null) { | ||
| 129 | localVariableMapping = new LocalVariableMapping(index, name); | ||
| 130 | boolean wasAdded = this.localVariables.put(index, localVariableMapping) == null; | ||
| 131 | assert (wasAdded); | ||
| 132 | } else { | ||
| 133 | localVariableMapping.setName(name); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | public void removeLocalVariableName(int index) { | ||
| 138 | boolean wasRemoved = this.localVariables.remove(index) != null; | ||
| 139 | assert (wasRemoved); | ||
| 140 | } | ||
| 141 | |||
| 142 | @Override | ||
| 143 | public String toString() { | ||
| 144 | StringBuilder buf = new StringBuilder(); | ||
| 145 | buf.append("\t"); | ||
| 146 | buf.append(this.obfName); | ||
| 147 | buf.append(" <-> "); | ||
| 148 | buf.append(this.deobfName); | ||
| 149 | buf.append("\n"); | ||
| 150 | buf.append("\t"); | ||
| 151 | buf.append(this.obfDescriptor); | ||
| 152 | buf.append("\n"); | ||
| 153 | buf.append("\tLocal Variables:\n"); | ||
| 154 | for (LocalVariableMapping localVariableMapping : this.localVariables.values()) { | ||
| 155 | buf.append("\t\t"); | ||
| 156 | buf.append(localVariableMapping.getIndex()); | ||
| 157 | buf.append(" -> "); | ||
| 158 | buf.append(localVariableMapping.getName()); | ||
| 159 | buf.append("\n"); | ||
| 160 | } | ||
| 161 | return buf.toString(); | ||
| 162 | } | ||
| 163 | |||
| 164 | @Override | ||
| 165 | public int compareTo(MethodMapping other) { | ||
| 166 | return (this.obfName + this.obfDescriptor).compareTo(other.obfName + other.obfDescriptor); | ||
| 167 | } | ||
| 168 | |||
| 169 | public boolean containsLocalVariable(String name) { | ||
| 170 | for (LocalVariableMapping localVariableMapping : this.localVariables.values()) { | ||
| 171 | if (localVariableMapping.getName().equals(name)) { | ||
| 172 | return true; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | return false; | ||
| 176 | } | ||
| 177 | |||
| 178 | public boolean renameObfClass(final String oldObfClassName, final String newObfClassName) { | ||
| 179 | // rename obf classes in the signature | ||
| 180 | MethodDescriptor newDescriptor = obfDescriptor.remap(className -> { | ||
| 181 | if (className.equals(oldObfClassName)) { | ||
| 182 | return newObfClassName; | ||
| 183 | } | ||
| 184 | return className; | ||
| 185 | }); | ||
| 186 | |||
| 187 | if (!newDescriptor.equals(this.obfDescriptor)) { | ||
| 188 | this.obfDescriptor = newDescriptor; | ||
| 189 | return true; | ||
| 190 | } | ||
| 191 | return false; | ||
| 192 | } | ||
| 193 | |||
| 194 | @Override | ||
| 195 | public MethodEntry getObfEntry(ClassEntry classEntry) { | ||
| 196 | return new MethodEntry(classEntry, this.obfName, this.obfDescriptor); | ||
| 197 | } | ||
| 198 | |||
| 199 | public Mappings.EntryModifier getModifier() { | ||
| 200 | return modifier; | ||
| 201 | } | ||
| 202 | |||
| 203 | public void setModifier(Mappings.EntryModifier modifier) { | ||
| 204 | this.modifier = modifier; | ||
| 205 | } | ||
| 206 | |||
| 207 | public boolean isObfuscated() { | ||
| 208 | return deobfName == null || deobfName.equals(obfName); | ||
| 209 | } | ||
| 210 | } | ||