diff options
| author | 2016-06-30 00:49:21 +1000 | |
|---|---|---|
| committer | 2016-06-30 00:49:21 +1000 | |
| commit | 4be005617b3b8c3578cca07c5d085d12916f0d1d (patch) | |
| tree | db163431f38703e26da417ef05eaea2b27a498b9 /src/cuchaz/enigma/mapping/MappingsRenamer.java | |
| parent | Some small changes to fix idea importing (diff) | |
| download | enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.tar.gz enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.tar.xz enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.zip | |
Json format (#2)
* Added new format
* Fixed bug
* Updated Version
Diffstat (limited to 'src/cuchaz/enigma/mapping/MappingsRenamer.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/MappingsRenamer.java | 237 |
1 files changed, 0 insertions, 237 deletions
diff --git a/src/cuchaz/enigma/mapping/MappingsRenamer.java b/src/cuchaz/enigma/mapping/MappingsRenamer.java deleted file mode 100644 index 47e5738..0000000 --- a/src/cuchaz/enigma/mapping/MappingsRenamer.java +++ /dev/null | |||
| @@ -1,237 +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 | * | ||
| 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.List; | ||
| 17 | import java.util.Set; | ||
| 18 | import java.util.zip.GZIPOutputStream; | ||
| 19 | |||
| 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 | |||
| 34 | deobfName = NameValidator.validateClassName(deobfName, !obf.isInnerClass()); | ||
| 35 | |||
| 36 | List<ClassMapping> mappingChain = getOrCreateClassMappingChain(obf); | ||
| 37 | if (mappingChain.size() == 1) { | ||
| 38 | |||
| 39 | if (deobfName != null) { | ||
| 40 | // make sure we don't rename to an existing obf or deobf class | ||
| 41 | if (m_mappings.containsDeobfClass(deobfName) || m_index.containsObfClass(new ClassEntry(deobfName))) { | ||
| 42 | throw new IllegalNameException(deobfName, "There is already a class with that name"); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | ClassMapping classMapping = mappingChain.get(0); | ||
| 47 | m_mappings.setClassDeobfName(classMapping, deobfName); | ||
| 48 | |||
| 49 | } else { | ||
| 50 | |||
| 51 | ClassMapping outerClassMapping = mappingChain.get(mappingChain.size() - 2); | ||
| 52 | |||
| 53 | if (deobfName != null) { | ||
| 54 | // make sure we don't rename to an existing obf or deobf inner class | ||
| 55 | if (outerClassMapping.hasInnerClassByDeobf(deobfName) || outerClassMapping.hasInnerClassByObfSimple(deobfName)) { | ||
| 56 | throw new IllegalNameException(deobfName, "There is already a class with that name"); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | outerClassMapping.setInnerClassName(obf, deobfName); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | public void removeClassMapping(ClassEntry obf) { | ||
| 65 | setClassName(obf, null); | ||
| 66 | } | ||
| 67 | |||
| 68 | public void markClassAsDeobfuscated(ClassEntry obf) { | ||
| 69 | String deobfName = obf.isInnerClass() ? obf.getInnermostClassName() : obf.getName(); | ||
| 70 | List<ClassMapping> mappingChain = getOrCreateClassMappingChain(obf); | ||
| 71 | if (mappingChain.size() == 1) { | ||
| 72 | ClassMapping classMapping = mappingChain.get(0); | ||
| 73 | m_mappings.setClassDeobfName(classMapping, deobfName); | ||
| 74 | } else { | ||
| 75 | ClassMapping outerClassMapping = mappingChain.get(mappingChain.size() - 2); | ||
| 76 | outerClassMapping.setInnerClassName(obf, deobfName); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | public void setFieldName(FieldEntry obf, String deobfName) { | ||
| 81 | deobfName = NameValidator.validateFieldName(deobfName); | ||
| 82 | FieldEntry targetEntry = new FieldEntry(obf.getClassEntry(), deobfName, obf.getType()); | ||
| 83 | if (m_mappings.containsDeobfField(obf.getClassEntry(), deobfName, obf.getType()) || m_index.containsObfField(targetEntry)) { | ||
| 84 | throw new IllegalNameException(deobfName, "There is already a field with that name"); | ||
| 85 | } | ||
| 86 | |||
| 87 | ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry()); | ||
| 88 | classMapping.setFieldName(obf.getName(), obf.getType(), deobfName); | ||
| 89 | } | ||
| 90 | |||
| 91 | public void removeFieldMapping(FieldEntry obf) { | ||
| 92 | ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry()); | ||
| 93 | classMapping.removeFieldMapping(classMapping.getFieldByObf(obf.getName(), obf.getType())); | ||
| 94 | } | ||
| 95 | |||
| 96 | public void markFieldAsDeobfuscated(FieldEntry obf) { | ||
| 97 | ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry()); | ||
| 98 | classMapping.setFieldName(obf.getName(), obf.getType(), obf.getName()); | ||
| 99 | } | ||
| 100 | |||
| 101 | public void setMethodTreeName(MethodEntry obf, String deobfName) { | ||
| 102 | Set<MethodEntry> implementations = m_index.getRelatedMethodImplementations(obf); | ||
| 103 | |||
| 104 | deobfName = NameValidator.validateMethodName(deobfName); | ||
| 105 | for (MethodEntry entry : implementations) { | ||
| 106 | Signature deobfSignature = m_mappings.getTranslator(TranslationDirection.Deobfuscating, m_index.getTranslationIndex()).translateSignature(obf.getSignature()); | ||
| 107 | MethodEntry targetEntry = new MethodEntry(entry.getClassEntry(), deobfName, deobfSignature); | ||
| 108 | if (m_mappings.containsDeobfMethod(entry.getClassEntry(), deobfName, entry.getSignature()) || m_index.containsObfBehavior(targetEntry)) { | ||
| 109 | String deobfClassName = m_mappings.getTranslator(TranslationDirection.Deobfuscating, m_index.getTranslationIndex()).translateClass(entry.getClassName()); | ||
| 110 | throw new IllegalNameException(deobfName, "There is already a method with that name and signature in class " + deobfClassName); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | for (MethodEntry entry : implementations) { | ||
| 115 | setMethodName(entry, deobfName); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | public void setMethodName(MethodEntry obf, String deobfName) { | ||
| 120 | deobfName = NameValidator.validateMethodName(deobfName); | ||
| 121 | MethodEntry targetEntry = new MethodEntry(obf.getClassEntry(), deobfName, obf.getSignature()); | ||
| 122 | if (m_mappings.containsDeobfMethod(obf.getClassEntry(), deobfName, obf.getSignature()) || m_index.containsObfBehavior(targetEntry)) { | ||
| 123 | String deobfClassName = m_mappings.getTranslator(TranslationDirection.Deobfuscating, m_index.getTranslationIndex()).translateClass(obf.getClassName()); | ||
| 124 | throw new IllegalNameException(deobfName, "There is already a method with that name and signature in class " + deobfClassName); | ||
| 125 | } | ||
| 126 | |||
| 127 | ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry()); | ||
| 128 | classMapping.setMethodName(obf.getName(), obf.getSignature(), deobfName); | ||
| 129 | } | ||
| 130 | |||
| 131 | public void removeMethodTreeMapping(MethodEntry obf) { | ||
| 132 | for (MethodEntry implementation : m_index.getRelatedMethodImplementations(obf)) { | ||
| 133 | removeMethodMapping(implementation); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | public void removeMethodMapping(MethodEntry obf) { | ||
| 138 | ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry()); | ||
| 139 | classMapping.setMethodName(obf.getName(), obf.getSignature(), null); | ||
| 140 | } | ||
| 141 | |||
| 142 | public void markMethodTreeAsDeobfuscated(MethodEntry obf) { | ||
| 143 | for (MethodEntry implementation : m_index.getRelatedMethodImplementations(obf)) { | ||
| 144 | markMethodAsDeobfuscated(implementation); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | public void markMethodAsDeobfuscated(MethodEntry obf) { | ||
| 149 | ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry()); | ||
| 150 | classMapping.setMethodName(obf.getName(), obf.getSignature(), obf.getName()); | ||
| 151 | } | ||
| 152 | |||
| 153 | public void setArgumentName(ArgumentEntry obf, String deobfName) { | ||
| 154 | deobfName = NameValidator.validateArgumentName(deobfName); | ||
| 155 | // NOTE: don't need to check arguments for name collisions with names determined by Procyon | ||
| 156 | if (m_mappings.containsArgument(obf.getBehaviorEntry(), deobfName)) { | ||
| 157 | throw new IllegalNameException(deobfName, "There is already an argument with that name"); | ||
| 158 | } | ||
| 159 | |||
| 160 | ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry()); | ||
| 161 | classMapping.setArgumentName(obf.getMethodName(), obf.getMethodSignature(), obf.getIndex(), deobfName); | ||
| 162 | } | ||
| 163 | |||
| 164 | public void removeArgumentMapping(ArgumentEntry obf) { | ||
| 165 | ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry()); | ||
| 166 | classMapping.removeArgumentName(obf.getMethodName(), obf.getMethodSignature(), obf.getIndex()); | ||
| 167 | } | ||
| 168 | |||
| 169 | public void markArgumentAsDeobfuscated(ArgumentEntry obf) { | ||
| 170 | ClassMapping classMapping = getOrCreateClassMapping(obf.getClassEntry()); | ||
| 171 | classMapping.setArgumentName(obf.getMethodName(), obf.getMethodSignature(), obf.getIndex(), obf.getName()); | ||
| 172 | } | ||
| 173 | |||
| 174 | public boolean moveFieldToObfClass(ClassMapping classMapping, FieldMapping fieldMapping, ClassEntry obfClass) { | ||
| 175 | classMapping.removeFieldMapping(fieldMapping); | ||
| 176 | ClassMapping targetClassMapping = getOrCreateClassMapping(obfClass); | ||
| 177 | if (!targetClassMapping.containsObfField(fieldMapping.getObfName(), fieldMapping.getObfType())) { | ||
| 178 | if (!targetClassMapping.containsDeobfField(fieldMapping.getDeobfName(), fieldMapping.getObfType())) { | ||
| 179 | targetClassMapping.addFieldMapping(fieldMapping); | ||
| 180 | return true; | ||
| 181 | } else { | ||
| 182 | System.err.println("WARNING: deobf field was already there: " + obfClass + "." + fieldMapping.getDeobfName()); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | return false; | ||
| 186 | } | ||
| 187 | |||
| 188 | public boolean moveMethodToObfClass(ClassMapping classMapping, MethodMapping methodMapping, ClassEntry obfClass) { | ||
| 189 | classMapping.removeMethodMapping(methodMapping); | ||
| 190 | ClassMapping targetClassMapping = getOrCreateClassMapping(obfClass); | ||
| 191 | if (!targetClassMapping.containsObfMethod(methodMapping.getObfName(), methodMapping.getObfSignature())) { | ||
| 192 | if (!targetClassMapping.containsDeobfMethod(methodMapping.getDeobfName(), methodMapping.getObfSignature())) { | ||
| 193 | targetClassMapping.addMethodMapping(methodMapping); | ||
| 194 | return true; | ||
| 195 | } else { | ||
| 196 | System.err.println("WARNING: deobf method was already there: " + obfClass + "." + methodMapping.getDeobfName() + methodMapping.getObfSignature()); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | return false; | ||
| 200 | } | ||
| 201 | |||
| 202 | public void write(OutputStream out) throws IOException { | ||
| 203 | // TEMP: just use the object output for now. We can find a more efficient storage format later | ||
| 204 | GZIPOutputStream gzipout = new GZIPOutputStream(out); | ||
| 205 | ObjectOutputStream oout = new ObjectOutputStream(gzipout); | ||
| 206 | oout.writeObject(this); | ||
| 207 | gzipout.finish(); | ||
| 208 | } | ||
| 209 | |||
| 210 | private ClassMapping getOrCreateClassMapping(ClassEntry obfClassEntry) { | ||
| 211 | List<ClassMapping> mappingChain = getOrCreateClassMappingChain(obfClassEntry); | ||
| 212 | return mappingChain.get(mappingChain.size() - 1); | ||
| 213 | } | ||
| 214 | |||
| 215 | private List<ClassMapping> getOrCreateClassMappingChain(ClassEntry obfClassEntry) { | ||
| 216 | List<ClassEntry> classChain = obfClassEntry.getClassChain(); | ||
| 217 | List<ClassMapping> mappingChain = m_mappings.getClassMappingChain(obfClassEntry); | ||
| 218 | for (int i=0; i<classChain.size(); i++) { | ||
| 219 | ClassEntry classEntry = classChain.get(i); | ||
| 220 | ClassMapping classMapping = mappingChain.get(i); | ||
| 221 | if (classMapping == null) { | ||
| 222 | |||
| 223 | // create it | ||
| 224 | classMapping = new ClassMapping(classEntry.getName()); | ||
| 225 | mappingChain.set(i, classMapping); | ||
| 226 | |||
| 227 | // add it to the right parent | ||
| 228 | if (i == 0) { | ||
| 229 | m_mappings.addClassMapping(classMapping); | ||
| 230 | } else { | ||
| 231 | mappingChain.get(i-1).addInnerClassMapping(classMapping); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | } | ||
| 235 | return mappingChain; | ||
| 236 | } | ||
| 237 | } | ||