diff options
| author | 2014-07-30 23:43:09 -0400 | |
|---|---|---|
| committer | 2014-07-30 23:43:09 -0400 | |
| commit | 4349d22cc8abf5ec74075dde1b45c5f2f8679bbf (patch) | |
| tree | 6200f39f3539e2def5010078200592141498be33 /src/cuchaz/enigma/mapping/ClassMapping.java | |
| parent | forgot to apply copyright notices (diff) | |
| download | enigma-fork-4349d22cc8abf5ec74075dde1b45c5f2f8679bbf.tar.gz enigma-fork-4349d22cc8abf5ec74075dde1b45c5f2f8679bbf.tar.xz enigma-fork-4349d22cc8abf5ec74075dde1b45c5f2f8679bbf.zip | |
switched to line-by-line mergable, human-readable file format for mappings
Diffstat (limited to 'src/cuchaz/enigma/mapping/ClassMapping.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/ClassMapping.java | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/mapping/ClassMapping.java b/src/cuchaz/enigma/mapping/ClassMapping.java new file mode 100644 index 0000000..3ba3569 --- /dev/null +++ b/src/cuchaz/enigma/mapping/ClassMapping.java | |||
| @@ -0,0 +1,217 @@ | |||
| 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.Serializable; | ||
| 14 | import java.util.Map; | ||
| 15 | |||
| 16 | import com.beust.jcommander.internal.Maps; | ||
| 17 | |||
| 18 | public class ClassMapping implements Serializable | ||
| 19 | { | ||
| 20 | private static final long serialVersionUID = -5148491146902340107L; | ||
| 21 | |||
| 22 | private String m_obfName; | ||
| 23 | private String m_deobfName; | ||
| 24 | private Map<String,FieldMapping> m_fieldsByObf; | ||
| 25 | private Map<String,FieldMapping> m_fieldsByDeobf; | ||
| 26 | private Map<String,MethodMapping> m_methodsByObf; | ||
| 27 | private Map<String,MethodMapping> m_methodsByDeobf; | ||
| 28 | |||
| 29 | // NOTE: this argument order is important for the MethodReader/MethodWriter | ||
| 30 | public ClassMapping( String obfName, String deobfName ) | ||
| 31 | { | ||
| 32 | m_obfName = obfName; | ||
| 33 | m_deobfName = deobfName; | ||
| 34 | m_fieldsByObf = Maps.newHashMap(); | ||
| 35 | m_fieldsByDeobf = Maps.newHashMap(); | ||
| 36 | m_methodsByObf = Maps.newHashMap(); | ||
| 37 | m_methodsByDeobf = Maps.newHashMap(); | ||
| 38 | } | ||
| 39 | |||
| 40 | public String getObfName( ) | ||
| 41 | { | ||
| 42 | return m_obfName; | ||
| 43 | } | ||
| 44 | |||
| 45 | public String getDeobfName( ) | ||
| 46 | { | ||
| 47 | return m_deobfName; | ||
| 48 | } | ||
| 49 | public void setDeobfName( String val ) | ||
| 50 | { | ||
| 51 | m_deobfName = val; | ||
| 52 | } | ||
| 53 | |||
| 54 | public Iterable<FieldMapping> fields( ) | ||
| 55 | { | ||
| 56 | assert( m_fieldsByObf.size() == m_fieldsByDeobf.size() ); | ||
| 57 | return m_fieldsByObf.values(); | ||
| 58 | } | ||
| 59 | |||
| 60 | protected void addFieldMapping( FieldMapping fieldMapping ) | ||
| 61 | { | ||
| 62 | m_fieldsByObf.put( fieldMapping.getObfName(), fieldMapping ); | ||
| 63 | m_fieldsByDeobf.put( fieldMapping.getDeobfName(), fieldMapping ); | ||
| 64 | } | ||
| 65 | |||
| 66 | public Iterable<MethodMapping> methods( ) | ||
| 67 | { | ||
| 68 | assert( m_methodsByObf.size() == m_methodsByDeobf.size() ); | ||
| 69 | return m_methodsByObf.values(); | ||
| 70 | } | ||
| 71 | |||
| 72 | protected void addMethodMapping( MethodMapping methodMapping ) | ||
| 73 | { | ||
| 74 | m_methodsByObf.put( getMethodKey( methodMapping.getObfName(), methodMapping.getObfSignature() ), methodMapping ); | ||
| 75 | m_methodsByDeobf.put( getMethodKey( methodMapping.getDeobfName(), methodMapping.getDeobfSignature() ), methodMapping ); | ||
| 76 | } | ||
| 77 | |||
| 78 | public String getObfFieldName( String deobfName ) | ||
| 79 | { | ||
| 80 | FieldMapping fieldMapping = m_fieldsByDeobf.get( deobfName ); | ||
| 81 | if( fieldMapping != null ) | ||
| 82 | { | ||
| 83 | return fieldMapping.getObfName(); | ||
| 84 | } | ||
| 85 | return null; | ||
| 86 | } | ||
| 87 | |||
| 88 | public String getDeobfFieldName( String obfName ) | ||
| 89 | { | ||
| 90 | FieldMapping fieldMapping = m_fieldsByObf.get( obfName ); | ||
| 91 | if( fieldMapping != null ) | ||
| 92 | { | ||
| 93 | return fieldMapping.getDeobfName(); | ||
| 94 | } | ||
| 95 | return null; | ||
| 96 | } | ||
| 97 | |||
| 98 | public void setFieldName( String obfName, String deobfName ) | ||
| 99 | { | ||
| 100 | if( deobfName == null ) | ||
| 101 | { | ||
| 102 | throw new IllegalArgumentException( "deobf name cannot be null!" ); | ||
| 103 | } | ||
| 104 | |||
| 105 | FieldMapping fieldMapping = m_fieldsByObf.get( obfName ); | ||
| 106 | if( fieldMapping == null ) | ||
| 107 | { | ||
| 108 | fieldMapping = new FieldMapping( obfName, deobfName ); | ||
| 109 | m_fieldsByObf.put( obfName, fieldMapping ); | ||
| 110 | m_fieldsByDeobf.put( deobfName, fieldMapping ); | ||
| 111 | } | ||
| 112 | |||
| 113 | m_fieldsByDeobf.remove( fieldMapping.getDeobfName() ); | ||
| 114 | fieldMapping.setDeobfName( deobfName ); | ||
| 115 | m_fieldsByDeobf.put( deobfName, fieldMapping ); | ||
| 116 | } | ||
| 117 | |||
| 118 | public MethodMapping getMethodByObf( String obfName, String signature ) | ||
| 119 | { | ||
| 120 | return m_methodsByObf.get( getMethodKey( obfName, signature ) ); | ||
| 121 | } | ||
| 122 | |||
| 123 | public MethodMapping getMethodByDeobf( String deobfName, String signature ) | ||
| 124 | { | ||
| 125 | return m_methodsByDeobf.get( getMethodKey( deobfName, signature ) ); | ||
| 126 | } | ||
| 127 | |||
| 128 | private String getMethodKey( String name, String signature ) | ||
| 129 | { | ||
| 130 | if( name == null ) | ||
| 131 | { | ||
| 132 | throw new IllegalArgumentException( "name cannot be null!" ); | ||
| 133 | } | ||
| 134 | if( signature == null ) | ||
| 135 | { | ||
| 136 | throw new IllegalArgumentException( "signature cannot be null!" ); | ||
| 137 | } | ||
| 138 | return name + signature; | ||
| 139 | } | ||
| 140 | |||
| 141 | public void setMethodNameAndSignature( String obfName, String obfSignature, String deobfName, String deobfSignature ) | ||
| 142 | { | ||
| 143 | if( deobfName == null ) | ||
| 144 | { | ||
| 145 | throw new IllegalArgumentException( "deobf name cannot be null!" ); | ||
| 146 | } | ||
| 147 | |||
| 148 | MethodMapping methodIndex = m_methodsByObf.get( getMethodKey( obfName, obfSignature ) ); | ||
| 149 | if( methodIndex == null ) | ||
| 150 | { | ||
| 151 | methodIndex = createMethodIndex( obfName, obfSignature ); | ||
| 152 | } | ||
| 153 | |||
| 154 | m_methodsByDeobf.remove( getMethodKey( methodIndex.getDeobfName(), methodIndex.getDeobfSignature() ) ); | ||
| 155 | methodIndex.setDeobfName( deobfName ); | ||
| 156 | methodIndex.setDeobfSignature( deobfSignature ); | ||
| 157 | m_methodsByDeobf.put( getMethodKey( deobfName, deobfSignature ), methodIndex ); | ||
| 158 | } | ||
| 159 | |||
| 160 | public void updateDeobfMethodSignatures( Translator translator ) | ||
| 161 | { | ||
| 162 | for( MethodMapping methodIndex : m_methodsByObf.values() ) | ||
| 163 | { | ||
| 164 | methodIndex.setDeobfSignature( translator.translateSignature( methodIndex.getObfSignature() ) ); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | public void setArgumentName( String obfMethodName, String obfMethodSignature, int argumentIndex, String argumentName ) | ||
| 169 | { | ||
| 170 | if( argumentName == null ) | ||
| 171 | { | ||
| 172 | throw new IllegalArgumentException( "argument name cannot be null!" ); | ||
| 173 | } | ||
| 174 | |||
| 175 | MethodMapping methodIndex = m_methodsByObf.get( getMethodKey( obfMethodName, obfMethodSignature ) ); | ||
| 176 | if( methodIndex == null ) | ||
| 177 | { | ||
| 178 | methodIndex = createMethodIndex( obfMethodName, obfMethodSignature ); | ||
| 179 | } | ||
| 180 | methodIndex.setArgumentName( argumentIndex, argumentName ); | ||
| 181 | } | ||
| 182 | |||
| 183 | private MethodMapping createMethodIndex( String obfName, String obfSignature ) | ||
| 184 | { | ||
| 185 | MethodMapping methodIndex = new MethodMapping( obfName, obfName, obfSignature, obfSignature ); | ||
| 186 | String key = getMethodKey( obfName, obfSignature ); | ||
| 187 | m_methodsByObf.put( key, methodIndex ); | ||
| 188 | m_methodsByDeobf.put( key, methodIndex ); | ||
| 189 | return methodIndex; | ||
| 190 | } | ||
| 191 | |||
| 192 | @Override | ||
| 193 | public String toString( ) | ||
| 194 | { | ||
| 195 | StringBuilder buf = new StringBuilder(); | ||
| 196 | buf.append( m_obfName ); | ||
| 197 | buf.append( " <-> " ); | ||
| 198 | buf.append( m_deobfName ); | ||
| 199 | buf.append( "\n" ); | ||
| 200 | buf.append( "Fields:\n" ); | ||
| 201 | for( FieldMapping fieldMapping : fields() ) | ||
| 202 | { | ||
| 203 | buf.append( "\t" ); | ||
| 204 | buf.append( fieldMapping.getObfName() ); | ||
| 205 | buf.append( " <-> " ); | ||
| 206 | buf.append( fieldMapping.getDeobfName() ); | ||
| 207 | buf.append( "\n" ); | ||
| 208 | } | ||
| 209 | buf.append( "Methods:\n" ); | ||
| 210 | for( MethodMapping methodIndex : m_methodsByObf.values() ) | ||
| 211 | { | ||
| 212 | buf.append( methodIndex.toString() ); | ||
| 213 | buf.append( "\n" ); | ||
| 214 | } | ||
| 215 | return buf.toString(); | ||
| 216 | } | ||
| 217 | } | ||