diff options
| author | 2015-02-08 21:29:25 -0500 | |
|---|---|---|
| committer | 2015-02-08 21:29:25 -0500 | |
| commit | ed9b5cdfc648e86fd463bfa8d86b94c41671e14c (patch) | |
| tree | 2619bbc7e04dfa3b82f8dfd3b1d31f529766cd4b /src/cuchaz/enigma/mapping/MappingsRenamer.java | |
| download | enigma-fork-ed9b5cdfc648e86fd463bfa8d86b94c41671e14c.tar.gz enigma-fork-ed9b5cdfc648e86fd463bfa8d86b94c41671e14c.tar.xz enigma-fork-ed9b5cdfc648e86fd463bfa8d86b94c41671e14c.zip | |
switch all classes to new signature/type system
Diffstat (limited to 'src/cuchaz/enigma/mapping/MappingsRenamer.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/MappingsRenamer.java | 237 |
1 files changed, 237 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/mapping/MappingsRenamer.java b/src/cuchaz/enigma/mapping/MappingsRenamer.java new file mode 100644 index 0000000..0a41c2b --- /dev/null +++ b/src/cuchaz/enigma/mapping/MappingsRenamer.java | |||
| @@ -0,0 +1,237 @@ | |||
| 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.mapping; | ||
| 12 | |||
| 13 | import java.io.IOException; | ||
| 14 | import java.io.ObjectOutputStream; | ||
| 15 | import java.io.OutputStream; | ||
| 16 | import java.util.Set; | ||
| 17 | import java.util.zip.GZIPOutputStream; | ||
| 18 | |||
| 19 | import cuchaz.enigma.Constants; | ||
| 20 | import cuchaz.enigma.analysis.JarIndex; | ||
| 21 | |||
| 22 | public class MappingsRenamer { | ||
| 23 | |||
| 24 | private JarIndex m_index; | ||
| 25 | private Mappings m_mappings; | ||
| 26 | |||
| 27 | public MappingsRenamer(JarIndex index, Mappings mappings) { | ||
| 28 | m_index = index; | ||
| 29 | m_mappings = mappings; | ||
| 30 | } | ||
| 31 | |||
| 32 | public void setClassName(ClassEntry obf, String deobfName) { | ||
| 33 | deobfName = NameValidator.validateClassName(deobfName, !obf.isInnerClass()); | ||
| 34 | ClassEntry targetEntry = new ClassEntry(deobfName); | ||
| 35 | if (m_mappings.containsDeobfClass(deobfName) || m_index.containsObfClass(targetEntry)) { | ||
| 36 | throw new IllegalNameException(deobfName, "There is already a class with that name"); | ||
| 37 | } | ||
| 38 | |||
| 39 | ClassMapping classMapping = getOrCreateClassMapping(obf); | ||
| 40 | |||
| 41 | if (obf.isInnerClass()) { | ||
| 42 | classMapping.setInnerClassName(obf.getInnerClassName(), deobfName); | ||
| 43 | } else { | ||
| 44 | if (classMapping.getDeobfName() != null) { | ||
| 45 | boolean wasRemoved = m_mappings.m_classesByDeobf.remove(classMapping.getDeobfName()) != null; | ||
| 46 | assert (wasRemoved); | ||
| 47 | } | ||
| 48 | classMapping.setDeobfName(deobfName); | ||
| 49 | boolean wasAdded = m_mappings.m_classesByDeobf.put(deobfName, classMapping) == null; | ||
| 50 | assert (wasAdded); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | public void removeClassMapping(ClassEntry obf) { | ||
| 55 | ClassMapping classMapping = getClassMapping(obf); | ||
| 56 | if (obf.isInnerClass()) { | ||
| 57 | classMapping.setInnerClassName(obf.getName(), null); | ||
| 58 | } else { | ||
| 59 | boolean wasRemoved = m_mappings.m_classesByDeobf.remove(classMapping.getDeobfName()) != null; | ||
| 60 | assert (wasRemoved); | ||
| 61 | classMapping.setDeobfName(null); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | public void markClassAsDeobfuscated(ClassEntry obf) { | ||
| 66 | ClassMapping classMapping = getOrCreateClassMapping(obf); | ||
| 67 | if (obf.isInnerClass()) { | ||
| 68 | String innerClassName = Constants.NonePackage + "/" + obf.getInnerClassName(); | ||
| 69 | classMapping.setInnerClassName(innerClassName, innerClassName); | ||
| 70 | } else { | ||
| 71 | classMapping.setDeobfName(obf.getName()); | ||
| 72 | boolean wasAdded = m_mappings.m_classesByDeobf.put(obf.getName(), classMapping) == null; | ||
| 73 | assert (wasAdded); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | public void setFieldName(FieldEntry obf, String deobfName) { | ||
| 78 | deobfName = NameValidator.validateFieldName(deobfName); | ||
| 79 | FieldEntry targetEntry = new FieldEntry(obf.getClassEntry(), deobfName); | ||
| 80 | if (m_mappings.containsDeobfField(obf.getClassEntry(), deobfName) || m_index.containsObfField(targetEntry)) { | ||
| 81 | throw new IllegalNameException(deobfName, "There is already a field with that name"); | ||
| 82 | } | ||
| 83 | |||
| 84 | ClassMapping classMapping = getOrCreateClassMappingOrInnerClassMapping(obf.getClassEntry()); | ||
| 85 | classMapping.setFieldName(obf.getName(), deobfName); | ||
| 86 | } | ||
| 87 | |||
| 88 | public void removeFieldMapping(FieldEntry obf) { | ||
| 89 | ClassMapping classMapping = getClassMappingOrInnerClassMapping(obf.getClassEntry()); | ||
| 90 | classMapping.setFieldName(obf.getName(), null); | ||
| 91 | } | ||
| 92 | |||
| 93 | public void markFieldAsDeobfuscated(FieldEntry obf) { | ||
| 94 | ClassMapping classMapping = getOrCreateClassMappingOrInnerClassMapping(obf.getClassEntry()); | ||
| 95 | classMapping.setFieldName(obf.getName(), obf.getName()); | ||
| 96 | } | ||
| 97 | |||
| 98 | public void setMethodTreeName(MethodEntry obf, String deobfName) { | ||
| 99 | Set<MethodEntry> implementations = m_index.getRelatedMethodImplementations(obf); | ||
| 100 | |||
| 101 | deobfName = NameValidator.validateMethodName(deobfName); | ||
| 102 | for (MethodEntry entry : implementations) { | ||
| 103 | Signature deobfSignature = m_mappings.getTranslator(TranslationDirection.Deobfuscating, m_index.getTranslationIndex()).translateSignature(obf.getSignature()); | ||
| 104 | MethodEntry targetEntry = new MethodEntry(entry.getClassEntry(), deobfName, deobfSignature); | ||
| 105 | if (m_mappings.containsDeobfMethod(entry.getClassEntry(), deobfName, entry.getSignature()) || m_index.containsObfBehavior(targetEntry)) { | ||
| 106 | String deobfClassName = m_mappings.getTranslator(TranslationDirection.Deobfuscating, m_index.getTranslationIndex()).translateClass(entry.getClassName()); | ||
| 107 | throw new IllegalNameException(deobfName, "There is already a method with that name and signature in class " + deobfClassName); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | for (MethodEntry entry : implementations) { | ||
| 112 | setMethodName(entry, deobfName); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | public void setMethodName(MethodEntry obf, String deobfName) { | ||
| 117 | deobfName = NameValidator.validateMethodName(deobfName); | ||
| 118 | MethodEntry targetEntry = new MethodEntry(obf.getClassEntry(), deobfName, obf.getSignature()); | ||
| 119 | if (m_mappings.containsDeobfMethod(obf.getClassEntry(), deobfName, obf.getSignature()) || m_index.containsObfBehavior(targetEntry)) { | ||
| 120 | String deobfClassName = m_mappings.getTranslator(TranslationDirection.Deobfuscating, m_index.getTranslationIndex()).translateClass(obf.getClassName()); | ||
| 121 | throw new IllegalNameException(deobfName, "There is already a method with that name and signature in class " + deobfClassName); | ||
| 122 | } | ||
| 123 | |||
| 124 | ClassMapping classMapping = getOrCreateClassMappingOrInnerClassMapping(obf.getClassEntry()); | ||
| 125 | classMapping.setMethodName(obf.getName(), obf.getSignature(), deobfName); | ||
| 126 | } | ||
| 127 | |||
| 128 | public void removeMethodTreeMapping(MethodEntry obf) { | ||
| 129 | for (MethodEntry implementation : m_index.getRelatedMethodImplementations(obf)) { | ||
| 130 | removeMethodMapping(implementation); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | public void removeMethodMapping(MethodEntry obf) { | ||
| 135 | ClassMapping classMapping = getOrCreateClassMappingOrInnerClassMapping(obf.getClassEntry()); | ||
| 136 | classMapping.setMethodName(obf.getName(), obf.getSignature(), null); | ||
| 137 | } | ||
| 138 | |||
| 139 | public void markMethodTreeAsDeobfuscated(MethodEntry obf) { | ||
| 140 | for (MethodEntry implementation : m_index.getRelatedMethodImplementations(obf)) { | ||
| 141 | markMethodAsDeobfuscated(implementation); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | public void markMethodAsDeobfuscated(MethodEntry obf) { | ||
| 146 | ClassMapping classMapping = getOrCreateClassMappingOrInnerClassMapping(obf.getClassEntry()); | ||
| 147 | classMapping.setMethodName(obf.getName(), obf.getSignature(), obf.getName()); | ||
| 148 | } | ||
| 149 | |||
| 150 | public void setArgumentName(ArgumentEntry obf, String deobfName) { | ||
| 151 | deobfName = NameValidator.validateArgumentName(deobfName); | ||
| 152 | // NOTE: don't need to check arguments for name collisions with names determined by Procyon | ||
| 153 | if (m_mappings.containsArgument(obf.getBehaviorEntry(), deobfName)) { | ||
| 154 | throw new IllegalNameException(deobfName, "There is already an argument with that name"); | ||
| 155 | } | ||
| 156 | |||
| 157 | ClassMapping classMapping = getOrCreateClassMappingOrInnerClassMapping(obf.getClassEntry()); | ||
| 158 | classMapping.setArgumentName(obf.getMethodName(), obf.getMethodSignature(), obf.getIndex(), deobfName); | ||
| 159 | } | ||
| 160 | |||
| 161 | public void removeArgumentMapping(ArgumentEntry obf) { | ||
| 162 | ClassMapping classMapping = getClassMappingOrInnerClassMapping(obf.getClassEntry()); | ||
| 163 | classMapping.removeArgumentName(obf.getMethodName(), obf.getMethodSignature(), obf.getIndex()); | ||
| 164 | } | ||
| 165 | |||
| 166 | public void markArgumentAsDeobfuscated(ArgumentEntry obf) { | ||
| 167 | ClassMapping classMapping = getOrCreateClassMappingOrInnerClassMapping(obf.getClassEntry()); | ||
| 168 | classMapping.setArgumentName(obf.getMethodName(), obf.getMethodSignature(), obf.getIndex(), obf.getName()); | ||
| 169 | } | ||
| 170 | |||
| 171 | public boolean moveFieldToObfClass(ClassMapping classMapping, FieldMapping fieldMapping, ClassEntry obfClass) { | ||
| 172 | classMapping.removeFieldMapping(fieldMapping); | ||
| 173 | ClassMapping targetClassMapping = getOrCreateClassMapping(obfClass); | ||
| 174 | if (!targetClassMapping.containsObfField(fieldMapping.getObfName())) { | ||
| 175 | if (!targetClassMapping.containsDeobfField(fieldMapping.getDeobfName())) { | ||
| 176 | targetClassMapping.addFieldMapping(fieldMapping); | ||
| 177 | return true; | ||
| 178 | } else { | ||
| 179 | System.err.println("WARNING: deobf field was already there: " + obfClass + "." + fieldMapping.getDeobfName()); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | return false; | ||
| 183 | } | ||
| 184 | |||
| 185 | public boolean moveMethodToObfClass(ClassMapping classMapping, MethodMapping methodMapping, ClassEntry obfClass) { | ||
| 186 | classMapping.removeMethodMapping(methodMapping); | ||
| 187 | ClassMapping targetClassMapping = getOrCreateClassMapping(obfClass); | ||
| 188 | if (!targetClassMapping.containsObfMethod(methodMapping.getObfName(), methodMapping.getObfSignature())) { | ||
| 189 | if (!targetClassMapping.containsDeobfMethod(methodMapping.getDeobfName(), methodMapping.getObfSignature())) { | ||
| 190 | targetClassMapping.addMethodMapping(methodMapping); | ||
| 191 | return true; | ||
| 192 | } else { | ||
| 193 | System.err.println("WARNING: deobf method was already there: " + obfClass + "." + methodMapping.getDeobfName() + methodMapping.getObfSignature()); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | return false; | ||
| 197 | } | ||
| 198 | |||
| 199 | public void write(OutputStream out) throws IOException { | ||
| 200 | // TEMP: just use the object output for now. We can find a more efficient storage format later | ||
| 201 | GZIPOutputStream gzipout = new GZIPOutputStream(out); | ||
| 202 | ObjectOutputStream oout = new ObjectOutputStream(gzipout); | ||
| 203 | oout.writeObject(this); | ||
| 204 | gzipout.finish(); | ||
| 205 | } | ||
| 206 | |||
| 207 | private ClassMapping getClassMapping(ClassEntry obfClassEntry) { | ||
| 208 | return m_mappings.m_classesByObf.get(obfClassEntry.getOuterClassName()); | ||
| 209 | } | ||
| 210 | |||
| 211 | private ClassMapping getOrCreateClassMapping(ClassEntry obfClassEntry) { | ||
| 212 | String obfClassName = obfClassEntry.getOuterClassName(); | ||
| 213 | ClassMapping classMapping = m_mappings.m_classesByObf.get(obfClassName); | ||
| 214 | if (classMapping == null) { | ||
| 215 | classMapping = new ClassMapping(obfClassName); | ||
| 216 | boolean obfWasAdded = m_mappings.m_classesByObf.put(classMapping.getObfName(), classMapping) == null; | ||
| 217 | assert (obfWasAdded); | ||
| 218 | } | ||
| 219 | return classMapping; | ||
| 220 | } | ||
| 221 | |||
| 222 | private ClassMapping getClassMappingOrInnerClassMapping(ClassEntry obfClassEntry) { | ||
| 223 | ClassMapping classMapping = getClassMapping(obfClassEntry); | ||
| 224 | if (obfClassEntry.isInDefaultPackage()) { | ||
| 225 | classMapping = classMapping.getInnerClassByObf(obfClassEntry.getInnerClassName()); | ||
| 226 | } | ||
| 227 | return classMapping; | ||
| 228 | } | ||
| 229 | |||
| 230 | private ClassMapping getOrCreateClassMappingOrInnerClassMapping(ClassEntry obfClassEntry) { | ||
| 231 | ClassMapping classMapping = getOrCreateClassMapping(obfClassEntry); | ||
| 232 | if (obfClassEntry.isInnerClass()) { | ||
| 233 | classMapping = classMapping.getOrCreateInnerClass(obfClassEntry.getInnerClassName()); | ||
| 234 | } | ||
| 235 | return classMapping; | ||
| 236 | } | ||
| 237 | } | ||