diff options
| author | 2015-01-13 23:25:04 -0500 | |
|---|---|---|
| committer | 2015-01-13 23:25:04 -0500 | |
| commit | 959cb5fd4f9586ec3bd265b452fe25fe1db82e3f (patch) | |
| tree | bdd8a2c52c2fe053ba3460614bde8542e5378dbe /src/cuchaz/enigma/mapping/Translator.java | |
| parent | got rid of gradle in favor of ivy+ssjb (diff) | |
| download | enigma-fork-959cb5fd4f9586ec3bd265b452fe25fe1db82e3f.tar.gz enigma-fork-959cb5fd4f9586ec3bd265b452fe25fe1db82e3f.tar.xz enigma-fork-959cb5fd4f9586ec3bd265b452fe25fe1db82e3f.zip | |
source format change
don't hate me too much if you were planning a big merge. =P
Diffstat (limited to 'src/cuchaz/enigma/mapping/Translator.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/Translator.java | 280 |
1 files changed, 103 insertions, 177 deletions
diff --git a/src/cuchaz/enigma/mapping/Translator.java b/src/cuchaz/enigma/mapping/Translator.java index 6cb5240..d8d9f48 100644 --- a/src/cuchaz/enigma/mapping/Translator.java +++ b/src/cuchaz/enigma/mapping/Translator.java | |||
| @@ -16,277 +16,203 @@ import com.google.common.collect.Maps; | |||
| 16 | 16 | ||
| 17 | import cuchaz.enigma.mapping.SignatureUpdater.ClassNameUpdater; | 17 | import cuchaz.enigma.mapping.SignatureUpdater.ClassNameUpdater; |
| 18 | 18 | ||
| 19 | public class Translator | 19 | public class Translator { |
| 20 | { | 20 | |
| 21 | private TranslationDirection m_direction; | 21 | private TranslationDirection m_direction; |
| 22 | private Map<String,ClassMapping> m_classes; | 22 | private Map<String,ClassMapping> m_classes; |
| 23 | 23 | ||
| 24 | public Translator( ) | 24 | public Translator() { |
| 25 | { | ||
| 26 | m_direction = null; | 25 | m_direction = null; |
| 27 | m_classes = Maps.newHashMap(); | 26 | m_classes = Maps.newHashMap(); |
| 28 | } | 27 | } |
| 29 | 28 | ||
| 30 | public Translator( TranslationDirection direction, Map<String,ClassMapping> classes ) | 29 | public Translator(TranslationDirection direction, Map<String,ClassMapping> classes) { |
| 31 | { | ||
| 32 | m_direction = direction; | 30 | m_direction = direction; |
| 33 | m_classes = classes; | 31 | m_classes = classes; |
| 34 | } | 32 | } |
| 35 | 33 | ||
| 36 | @SuppressWarnings( "unchecked" ) | 34 | @SuppressWarnings("unchecked") |
| 37 | public <T extends Entry> T translateEntry( T entry ) | 35 | public <T extends Entry> T translateEntry(T entry) { |
| 38 | { | 36 | if (entry instanceof ClassEntry) { |
| 39 | if( entry instanceof ClassEntry ) | 37 | return (T)translateEntry((ClassEntry)entry); |
| 40 | { | 38 | } else if (entry instanceof FieldEntry) { |
| 41 | return (T)translateEntry( (ClassEntry)entry ); | 39 | return (T)translateEntry((FieldEntry)entry); |
| 42 | } | 40 | } else if (entry instanceof MethodEntry) { |
| 43 | else if( entry instanceof FieldEntry ) | 41 | return (T)translateEntry((MethodEntry)entry); |
| 44 | { | 42 | } else if (entry instanceof ConstructorEntry) { |
| 45 | return (T)translateEntry( (FieldEntry)entry ); | 43 | return (T)translateEntry((ConstructorEntry)entry); |
| 46 | } | 44 | } else if (entry instanceof ArgumentEntry) { |
| 47 | else if( entry instanceof MethodEntry ) | 45 | return (T)translateEntry((ArgumentEntry)entry); |
| 48 | { | 46 | } else { |
| 49 | return (T)translateEntry( (MethodEntry)entry ); | 47 | throw new Error("Unknown entry type: " + entry.getClass().getName()); |
| 50 | } | ||
| 51 | else if( entry instanceof ConstructorEntry ) | ||
| 52 | { | ||
| 53 | return (T)translateEntry( (ConstructorEntry)entry ); | ||
| 54 | } | ||
| 55 | else if( entry instanceof ArgumentEntry ) | ||
| 56 | { | ||
| 57 | return (T)translateEntry( (ArgumentEntry)entry ); | ||
| 58 | } | ||
| 59 | else | ||
| 60 | { | ||
| 61 | throw new Error( "Unknown entry type: " + entry.getClass().getName() ); | ||
| 62 | } | 48 | } |
| 63 | } | 49 | } |
| 64 | 50 | ||
| 65 | public String translateClass( String className ) | 51 | public String translateClass(String className) { |
| 66 | { | 52 | return translate(new ClassEntry(className)); |
| 67 | return translate( new ClassEntry( className ) ); | ||
| 68 | } | 53 | } |
| 69 | 54 | ||
| 70 | public String translate( ClassEntry in ) | 55 | public String translate(ClassEntry in) { |
| 71 | { | 56 | ClassMapping classMapping = m_classes.get(in.getOuterClassName()); |
| 72 | ClassMapping classMapping = m_classes.get( in.getOuterClassName() ); | 57 | if (classMapping != null) { |
| 73 | if( classMapping != null ) | 58 | if (in.isInnerClass()) { |
| 74 | { | ||
| 75 | if( in.isInnerClass() ) | ||
| 76 | { | ||
| 77 | // translate the inner class | 59 | // translate the inner class |
| 78 | String translatedInnerClassName = m_direction.choose( | 60 | String translatedInnerClassName = m_direction.choose( |
| 79 | classMapping.getDeobfInnerClassName( in.getInnerClassName() ), | 61 | classMapping.getDeobfInnerClassName(in.getInnerClassName()), |
| 80 | classMapping.getObfInnerClassName( in.getInnerClassName() ) | 62 | classMapping.getObfInnerClassName(in.getInnerClassName()) |
| 81 | ); | 63 | ); |
| 82 | if( translatedInnerClassName != null ) | 64 | if (translatedInnerClassName != null) { |
| 83 | { | ||
| 84 | // try to translate the outer name | 65 | // try to translate the outer name |
| 85 | String translatedOuterClassName = m_direction.choose( | 66 | String translatedOuterClassName = m_direction.choose(classMapping.getDeobfName(), classMapping.getObfName()); |
| 86 | classMapping.getDeobfName(), | 67 | if (translatedOuterClassName != null) { |
| 87 | classMapping.getObfName() | ||
| 88 | ); | ||
| 89 | if( translatedOuterClassName != null ) | ||
| 90 | { | ||
| 91 | return translatedOuterClassName + "$" + translatedInnerClassName; | 68 | return translatedOuterClassName + "$" + translatedInnerClassName; |
| 92 | } | 69 | } else { |
| 93 | else | ||
| 94 | { | ||
| 95 | return in.getOuterClassName() + "$" + translatedInnerClassName; | 70 | return in.getOuterClassName() + "$" + translatedInnerClassName; |
| 96 | } | 71 | } |
| 97 | } | 72 | } |
| 98 | } | 73 | } else { |
| 99 | else | ||
| 100 | { | ||
| 101 | // just return outer | 74 | // just return outer |
| 102 | return m_direction.choose( | 75 | return m_direction.choose(classMapping.getDeobfName(), classMapping.getObfName()); |
| 103 | classMapping.getDeobfName(), | ||
| 104 | classMapping.getObfName() | ||
| 105 | ); | ||
| 106 | } | 76 | } |
| 107 | } | 77 | } |
| 108 | return null; | 78 | return null; |
| 109 | } | 79 | } |
| 110 | 80 | ||
| 111 | public ClassEntry translateEntry( ClassEntry in ) | 81 | public ClassEntry translateEntry(ClassEntry in) { |
| 112 | { | 82 | |
| 113 | // can we translate the inner class? | 83 | // can we translate the inner class? |
| 114 | String name = translate( in ); | 84 | String name = translate(in); |
| 115 | if( name != null ) | 85 | if (name != null) { |
| 116 | { | 86 | return new ClassEntry(name); |
| 117 | return new ClassEntry( name ); | ||
| 118 | } | 87 | } |
| 119 | 88 | ||
| 120 | if( in.isInnerClass() ) | 89 | if (in.isInnerClass()) { |
| 121 | { | 90 | |
| 122 | // guess not. just translate the outer class name then | 91 | // guess not. just translate the outer class name then |
| 123 | String outerClassName = translate( in.getOuterClassEntry() ); | 92 | String outerClassName = translate(in.getOuterClassEntry()); |
| 124 | if( outerClassName != null ) | 93 | if (outerClassName != null) { |
| 125 | { | 94 | return new ClassEntry(outerClassName + "$" + in.getInnerClassName()); |
| 126 | return new ClassEntry( outerClassName + "$" + in.getInnerClassName() ); | ||
| 127 | } | 95 | } |
| 128 | } | 96 | } |
| 129 | 97 | ||
| 130 | return in; | 98 | return in; |
| 131 | } | 99 | } |
| 132 | 100 | ||
| 133 | public String translate( FieldEntry in ) | 101 | public String translate(FieldEntry in) { |
| 134 | { | 102 | |
| 135 | // look for the class | 103 | // look for the class |
| 136 | ClassMapping classMapping = findClassMapping( in.getClassEntry() ); | 104 | ClassMapping classMapping = findClassMapping(in.getClassEntry()); |
| 137 | if( classMapping != null ) | 105 | if (classMapping != null) { |
| 138 | { | 106 | |
| 139 | // look for the field | 107 | // look for the field |
| 140 | String translatedName = m_direction.choose( | 108 | String translatedName = m_direction.choose( |
| 141 | classMapping.getDeobfFieldName( in.getName() ), | 109 | classMapping.getDeobfFieldName(in.getName()), |
| 142 | classMapping.getObfFieldName( in.getName() ) | 110 | classMapping.getObfFieldName(in.getName()) |
| 143 | ); | 111 | ); |
| 144 | if( translatedName != null ) | 112 | if (translatedName != null) { |
| 145 | { | ||
| 146 | return translatedName; | 113 | return translatedName; |
| 147 | } | 114 | } |
| 148 | } | 115 | } |
| 149 | return null; | 116 | return null; |
| 150 | } | 117 | } |
| 151 | 118 | ||
| 152 | public FieldEntry translateEntry( FieldEntry in ) | 119 | public FieldEntry translateEntry(FieldEntry in) { |
| 153 | { | 120 | String name = translate(in); |
| 154 | String name = translate( in ); | 121 | if (name == null) { |
| 155 | if( name == null ) | ||
| 156 | { | ||
| 157 | name = in.getName(); | 122 | name = in.getName(); |
| 158 | } | 123 | } |
| 159 | return new FieldEntry( | 124 | return new FieldEntry(translateEntry(in.getClassEntry()), name); |
| 160 | translateEntry( in.getClassEntry() ), | ||
| 161 | name | ||
| 162 | ); | ||
| 163 | } | 125 | } |
| 164 | 126 | ||
| 165 | public String translate( MethodEntry in ) | 127 | public String translate(MethodEntry in) { |
| 166 | { | 128 | |
| 167 | // look for class | 129 | // look for class |
| 168 | ClassMapping classMapping = findClassMapping( in.getClassEntry() ); | 130 | ClassMapping classMapping = findClassMapping(in.getClassEntry()); |
| 169 | if( classMapping != null ) | 131 | if (classMapping != null) { |
| 170 | { | 132 | |
| 171 | // look for the method | 133 | // look for the method |
| 172 | MethodMapping methodMapping = m_direction.choose( | 134 | MethodMapping methodMapping = m_direction.choose(classMapping.getMethodByObf(in.getName(), in.getSignature()), |
| 173 | classMapping.getMethodByObf( in.getName(), in.getSignature() ), | 135 | classMapping.getMethodByDeobf(in.getName(), translateSignature(in.getSignature()))); |
| 174 | classMapping.getMethodByDeobf( in.getName(), translateSignature( in.getSignature() ) ) | 136 | if (methodMapping != null) { |
| 175 | ); | 137 | return m_direction.choose(methodMapping.getDeobfName(), methodMapping.getObfName()); |
| 176 | if( methodMapping != null ) | ||
| 177 | { | ||
| 178 | return m_direction.choose( | ||
| 179 | methodMapping.getDeobfName(), | ||
| 180 | methodMapping.getObfName() | ||
| 181 | ); | ||
| 182 | } | 138 | } |
| 183 | } | 139 | } |
| 184 | return null; | 140 | return null; |
| 185 | } | 141 | } |
| 186 | 142 | ||
| 187 | public MethodEntry translateEntry( MethodEntry in ) | 143 | public MethodEntry translateEntry(MethodEntry in) { |
| 188 | { | 144 | String name = translate(in); |
| 189 | String name = translate( in ); | 145 | if (name == null) { |
| 190 | if( name == null ) | ||
| 191 | { | ||
| 192 | name = in.getName(); | 146 | name = in.getName(); |
| 193 | } | 147 | } |
| 194 | return new MethodEntry( | 148 | return new MethodEntry(translateEntry(in.getClassEntry()), name, translateSignature(in.getSignature())); |
| 195 | translateEntry( in.getClassEntry() ), | ||
| 196 | name, | ||
| 197 | translateSignature( in.getSignature() ) | ||
| 198 | ); | ||
| 199 | } | 149 | } |
| 200 | 150 | ||
| 201 | public ConstructorEntry translateEntry( ConstructorEntry in ) | 151 | public ConstructorEntry translateEntry(ConstructorEntry in) { |
| 202 | { | 152 | if (in.isStatic()) { |
| 203 | if( in.isStatic() ) | 153 | return new ConstructorEntry(translateEntry(in.getClassEntry())); |
| 204 | { | 154 | } else { |
| 205 | return new ConstructorEntry( translateEntry( in.getClassEntry() ) ); | 155 | return new ConstructorEntry(translateEntry(in.getClassEntry()), translateSignature(in.getSignature())); |
| 206 | } | ||
| 207 | else | ||
| 208 | { | ||
| 209 | return new ConstructorEntry( | ||
| 210 | translateEntry( in.getClassEntry() ), | ||
| 211 | translateSignature( in.getSignature() ) | ||
| 212 | ); | ||
| 213 | } | 156 | } |
| 214 | } | 157 | } |
| 215 | 158 | ||
| 216 | public BehaviorEntry translateEntry( BehaviorEntry in ) | 159 | public BehaviorEntry translateEntry(BehaviorEntry in) { |
| 217 | { | 160 | if (in instanceof MethodEntry) { |
| 218 | if( in instanceof MethodEntry ) | 161 | return translateEntry((MethodEntry)in); |
| 219 | { | 162 | } else if (in instanceof ConstructorEntry) { |
| 220 | return translateEntry( (MethodEntry)in ); | 163 | return translateEntry((ConstructorEntry)in); |
| 221 | } | 164 | } |
| 222 | else if( in instanceof ConstructorEntry ) | 165 | throw new Error("Wrong entry type!"); |
| 223 | { | ||
| 224 | return translateEntry( (ConstructorEntry)in ); | ||
| 225 | } | ||
| 226 | throw new Error( "Wrong entry type!" ); | ||
| 227 | } | 166 | } |
| 228 | 167 | ||
| 229 | public String translate( ArgumentEntry in ) | 168 | public String translate(ArgumentEntry in) { |
| 230 | { | 169 | |
| 231 | // look for the class | 170 | // look for the class |
| 232 | ClassMapping classMapping = findClassMapping( in.getClassEntry() ); | 171 | ClassMapping classMapping = findClassMapping(in.getClassEntry()); |
| 233 | if( classMapping != null ) | 172 | if (classMapping != null) { |
| 234 | { | 173 | |
| 235 | // look for the method | 174 | // look for the method |
| 236 | MethodMapping methodMapping = m_direction.choose( | 175 | MethodMapping methodMapping = m_direction.choose( |
| 237 | classMapping.getMethodByObf( in.getMethodName(), in.getMethodSignature() ), | 176 | classMapping.getMethodByObf(in.getMethodName(), in.getMethodSignature()), |
| 238 | classMapping.getMethodByDeobf( in.getMethodName(), translateSignature( in.getMethodSignature() ) ) | 177 | classMapping.getMethodByDeobf(in.getMethodName(), translateSignature(in.getMethodSignature())) |
| 239 | ); | 178 | ); |
| 240 | if( methodMapping != null ) | 179 | if (methodMapping != null) { |
| 241 | { | ||
| 242 | return m_direction.choose( | 180 | return m_direction.choose( |
| 243 | methodMapping.getDeobfArgumentName( in.getIndex() ), | 181 | methodMapping.getDeobfArgumentName(in.getIndex()), |
| 244 | methodMapping.getObfArgumentName( in.getIndex() ) | 182 | methodMapping.getObfArgumentName(in.getIndex()) |
| 245 | ); | 183 | ); |
| 246 | } | 184 | } |
| 247 | } | 185 | } |
| 248 | return null; | 186 | return null; |
| 249 | } | 187 | } |
| 250 | 188 | ||
| 251 | public ArgumentEntry translateEntry( ArgumentEntry in ) | 189 | public ArgumentEntry translateEntry(ArgumentEntry in) { |
| 252 | { | 190 | String name = translate(in); |
| 253 | String name = translate( in ); | 191 | if (name == null) { |
| 254 | if( name == null ) | ||
| 255 | { | ||
| 256 | name = in.getName(); | 192 | name = in.getName(); |
| 257 | } | 193 | } |
| 258 | return new ArgumentEntry( | 194 | return new ArgumentEntry(translateEntry(in.getBehaviorEntry()), in.getIndex(), name); |
| 259 | translateEntry( in.getBehaviorEntry() ), | ||
| 260 | in.getIndex(), | ||
| 261 | name | ||
| 262 | ); | ||
| 263 | } | 195 | } |
| 264 | 196 | ||
| 265 | public String translateSignature( String signature ) | 197 | public String translateSignature(String signature) { |
| 266 | { | 198 | return SignatureUpdater.update(signature, new ClassNameUpdater() { |
| 267 | return SignatureUpdater.update( signature, new ClassNameUpdater( ) | ||
| 268 | { | ||
| 269 | @Override | 199 | @Override |
| 270 | public String update( String className ) | 200 | public String update(String className) { |
| 271 | { | 201 | String translatedName = translateClass(className); |
| 272 | String translatedName = translateClass( className ); | 202 | if (translatedName != null) { |
| 273 | if( translatedName != null ) | ||
| 274 | { | ||
| 275 | return translatedName; | 203 | return translatedName; |
| 276 | } | 204 | } |
| 277 | return className; | 205 | return className; |
| 278 | } | 206 | } |
| 279 | } ); | 207 | }); |
| 280 | } | 208 | } |
| 281 | 209 | ||
| 282 | private ClassMapping findClassMapping( ClassEntry classEntry ) | 210 | private ClassMapping findClassMapping(ClassEntry classEntry) { |
| 283 | { | 211 | ClassMapping classMapping = m_classes.get(classEntry.getOuterClassName()); |
| 284 | ClassMapping classMapping = m_classes.get( classEntry.getOuterClassName() ); | 212 | if (classMapping != null && classEntry.isInnerClass()) { |
| 285 | if( classMapping != null && classEntry.isInnerClass() ) | ||
| 286 | { | ||
| 287 | classMapping = m_direction.choose( | 213 | classMapping = m_direction.choose( |
| 288 | classMapping.getInnerClassByObf( classEntry.getInnerClassName() ), | 214 | classMapping.getInnerClassByObf(classEntry.getInnerClassName()), |
| 289 | classMapping.getInnerClassByDeobfThenObf( classEntry.getInnerClassName() ) | 215 | classMapping.getInnerClassByDeobfThenObf(classEntry.getInnerClassName()) |
| 290 | ); | 216 | ); |
| 291 | } | 217 | } |
| 292 | return classMapping; | 218 | return classMapping; |