diff options
Diffstat (limited to 'src/cuchaz/enigma/mapping/Translator.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/Translator.java | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/mapping/Translator.java b/src/cuchaz/enigma/mapping/Translator.java new file mode 100644 index 0000000..bae0dce --- /dev/null +++ b/src/cuchaz/enigma/mapping/Translator.java | |||
| @@ -0,0 +1,201 @@ | |||
| 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.util.ArrayList; | ||
| 14 | import java.util.List; | ||
| 15 | import java.util.Map; | ||
| 16 | |||
| 17 | import cuchaz.enigma.mapping.SignatureUpdater.ClassNameUpdater; | ||
| 18 | |||
| 19 | public class Translator | ||
| 20 | { | ||
| 21 | private TranslationDirection m_direction; | ||
| 22 | private Map<String,ClassIndex> m_classes; | ||
| 23 | private Ancestries m_ancestries; | ||
| 24 | |||
| 25 | protected Translator( TranslationDirection direction, Map<String,ClassIndex> classes, Ancestries ancestries ) | ||
| 26 | { | ||
| 27 | m_direction = direction; | ||
| 28 | m_classes = classes; | ||
| 29 | m_ancestries = ancestries; | ||
| 30 | } | ||
| 31 | |||
| 32 | public String translate( ClassEntry in ) | ||
| 33 | { | ||
| 34 | return translateClass( in.getName() ); | ||
| 35 | } | ||
| 36 | |||
| 37 | public String translateClass( String in ) | ||
| 38 | { | ||
| 39 | ClassIndex classIndex = m_classes.get( in ); | ||
| 40 | if( classIndex != null ) | ||
| 41 | { | ||
| 42 | return m_direction.choose( | ||
| 43 | classIndex.getDeobfName(), | ||
| 44 | classIndex.getObfName() | ||
| 45 | ); | ||
| 46 | } | ||
| 47 | |||
| 48 | return null; | ||
| 49 | } | ||
| 50 | |||
| 51 | public ClassEntry translateEntry( ClassEntry in ) | ||
| 52 | { | ||
| 53 | String name = translate( in ); | ||
| 54 | if( name == null ) | ||
| 55 | { | ||
| 56 | return in; | ||
| 57 | } | ||
| 58 | return new ClassEntry( name ); | ||
| 59 | } | ||
| 60 | |||
| 61 | public String translate( FieldEntry in ) | ||
| 62 | { | ||
| 63 | for( String className : getSelfAndAncestors( in.getClassName() ) ) | ||
| 64 | { | ||
| 65 | // look for the class | ||
| 66 | ClassIndex classIndex = m_classes.get( className ); | ||
| 67 | if( classIndex != null ) | ||
| 68 | { | ||
| 69 | // look for the field | ||
| 70 | String deobfName = m_direction.choose( | ||
| 71 | classIndex.getDeobfFieldName( in.getName() ), | ||
| 72 | classIndex.getObfFieldName( in.getName() ) | ||
| 73 | ); | ||
| 74 | if( deobfName != null ) | ||
| 75 | { | ||
| 76 | return deobfName; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | return null; | ||
| 82 | } | ||
| 83 | |||
| 84 | public FieldEntry translateEntry( FieldEntry in ) | ||
| 85 | { | ||
| 86 | String name = translate( in ); | ||
| 87 | if( name == null ) | ||
| 88 | { | ||
| 89 | name = in.getName(); | ||
| 90 | } | ||
| 91 | return new FieldEntry( | ||
| 92 | translateEntry( in.getClassEntry() ), | ||
| 93 | name | ||
| 94 | ); | ||
| 95 | } | ||
| 96 | |||
| 97 | public String translate( MethodEntry in ) | ||
| 98 | { | ||
| 99 | for( String className : getSelfAndAncestors( in.getClassName() ) ) | ||
| 100 | { | ||
| 101 | // look for the class | ||
| 102 | ClassIndex classIndex = m_classes.get( className ); | ||
| 103 | if( classIndex != null ) | ||
| 104 | { | ||
| 105 | // look for the method | ||
| 106 | MethodIndex methodIndex = m_direction.choose( | ||
| 107 | classIndex.getMethodByObf( in.getName(), in.getSignature() ), | ||
| 108 | classIndex.getMethodByDeobf( in.getName(), in.getSignature() ) | ||
| 109 | ); | ||
| 110 | if( methodIndex != null ) | ||
| 111 | { | ||
| 112 | return m_direction.choose( | ||
| 113 | methodIndex.getDeobfName(), | ||
| 114 | methodIndex.getObfName() | ||
| 115 | ); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | return null; | ||
| 121 | } | ||
| 122 | |||
| 123 | public MethodEntry translateEntry( MethodEntry in ) | ||
| 124 | { | ||
| 125 | String name = translate( in ); | ||
| 126 | if( name == null ) | ||
| 127 | { | ||
| 128 | name = in.getName(); | ||
| 129 | } | ||
| 130 | return new MethodEntry( | ||
| 131 | translateEntry( in.getClassEntry() ), | ||
| 132 | name, | ||
| 133 | translateSignature( in.getSignature() ) | ||
| 134 | ); | ||
| 135 | } | ||
| 136 | |||
| 137 | public String translate( ArgumentEntry in ) | ||
| 138 | { | ||
| 139 | for( String className : getSelfAndAncestors( in.getClassName() ) ) | ||
| 140 | { | ||
| 141 | // look for the class | ||
| 142 | ClassIndex classIndex = m_classes.get( className ); | ||
| 143 | if( classIndex != null ) | ||
| 144 | { | ||
| 145 | // look for the method | ||
| 146 | MethodIndex methodIndex = m_direction.choose( | ||
| 147 | classIndex.getMethodByObf( in.getMethodName(), in.getMethodSignature() ), | ||
| 148 | classIndex.getMethodByDeobf( in.getMethodName(), in.getMethodSignature() ) | ||
| 149 | ); | ||
| 150 | if( methodIndex != null ) | ||
| 151 | { | ||
| 152 | return m_direction.choose( | ||
| 153 | methodIndex.getDeobfArgumentName( in.getIndex() ), | ||
| 154 | methodIndex.getObfArgumentName( in.getIndex() ) | ||
| 155 | ); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | return null; | ||
| 161 | } | ||
| 162 | |||
| 163 | public ArgumentEntry translateEntry( ArgumentEntry in ) | ||
| 164 | { | ||
| 165 | String name = translate( in ); | ||
| 166 | if( name == null ) | ||
| 167 | { | ||
| 168 | name = in.getName(); | ||
| 169 | } | ||
| 170 | return new ArgumentEntry( | ||
| 171 | translateEntry( in.getMethodEntry() ), | ||
| 172 | in.getIndex(), | ||
| 173 | name | ||
| 174 | ); | ||
| 175 | } | ||
| 176 | |||
| 177 | public String translateSignature( String signature ) | ||
| 178 | { | ||
| 179 | return SignatureUpdater.update( signature, new ClassNameUpdater( ) | ||
| 180 | { | ||
| 181 | @Override | ||
| 182 | public String update( String className ) | ||
| 183 | { | ||
| 184 | String translatedName = translateClass( className ); | ||
| 185 | if( translatedName != null ) | ||
| 186 | { | ||
| 187 | return translatedName; | ||
| 188 | } | ||
| 189 | return className; | ||
| 190 | } | ||
| 191 | } ); | ||
| 192 | } | ||
| 193 | |||
| 194 | private List<String> getSelfAndAncestors( String className ) | ||
| 195 | { | ||
| 196 | List<String> ancestry = new ArrayList<String>(); | ||
| 197 | ancestry.add( className ); | ||
| 198 | ancestry.addAll( m_ancestries.getAncestry( className ) ); | ||
| 199 | return ancestry; | ||
| 200 | } | ||
| 201 | } | ||