diff options
Diffstat (limited to 'src/cuchaz/enigma/mapping/ClassIndex.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/ClassIndex.java | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/mapping/ClassIndex.java b/src/cuchaz/enigma/mapping/ClassIndex.java new file mode 100644 index 0000000..699807b --- /dev/null +++ b/src/cuchaz/enigma/mapping/ClassIndex.java | |||
| @@ -0,0 +1,159 @@ | |||
| 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 | import com.google.common.collect.BiMap; | ||
| 18 | import com.google.common.collect.HashBiMap; | ||
| 19 | |||
| 20 | public class ClassIndex implements Serializable | ||
| 21 | { | ||
| 22 | private static final long serialVersionUID = -5148491146902340107L; | ||
| 23 | |||
| 24 | private String m_obfName; | ||
| 25 | private String m_deobfName; | ||
| 26 | private BiMap<String,String> m_fieldsObfToDeobf; | ||
| 27 | private Map<String,MethodIndex> m_methodsByObf; | ||
| 28 | private Map<String,MethodIndex> m_methodsByDeobf; | ||
| 29 | |||
| 30 | public ClassIndex( String obfName, String deobfName ) | ||
| 31 | { | ||
| 32 | m_obfName = obfName; | ||
| 33 | m_deobfName = deobfName; | ||
| 34 | m_fieldsObfToDeobf = HashBiMap.create(); | ||
| 35 | m_methodsByObf = Maps.newHashMap(); | ||
| 36 | m_methodsByDeobf = Maps.newHashMap(); | ||
| 37 | } | ||
| 38 | |||
| 39 | public String getObfName( ) | ||
| 40 | { | ||
| 41 | return m_obfName; | ||
| 42 | } | ||
| 43 | |||
| 44 | public String getDeobfName( ) | ||
| 45 | { | ||
| 46 | return m_deobfName; | ||
| 47 | } | ||
| 48 | public void setDeobfName( String val ) | ||
| 49 | { | ||
| 50 | m_deobfName = val; | ||
| 51 | } | ||
| 52 | |||
| 53 | public String getObfFieldName( String deobfName ) | ||
| 54 | { | ||
| 55 | return m_fieldsObfToDeobf.inverse().get( deobfName ); | ||
| 56 | } | ||
| 57 | |||
| 58 | public String getDeobfFieldName( String obfName ) | ||
| 59 | { | ||
| 60 | return m_fieldsObfToDeobf.get( obfName ); | ||
| 61 | } | ||
| 62 | |||
| 63 | public void setFieldName( String obfName, String deobfName ) | ||
| 64 | { | ||
| 65 | m_fieldsObfToDeobf.put( obfName, deobfName ); | ||
| 66 | } | ||
| 67 | |||
| 68 | public MethodIndex getMethodByObf( String obfName, String signature ) | ||
| 69 | { | ||
| 70 | return m_methodsByObf.get( getMethodKey( obfName, signature ) ); | ||
| 71 | } | ||
| 72 | |||
| 73 | public MethodIndex getMethodByDeobf( String deobfName, String signature ) | ||
| 74 | { | ||
| 75 | return m_methodsByDeobf.get( getMethodKey( deobfName, signature ) ); | ||
| 76 | } | ||
| 77 | |||
| 78 | private String getMethodKey( String name, String signature ) | ||
| 79 | { | ||
| 80 | return name + signature; | ||
| 81 | } | ||
| 82 | |||
| 83 | public void setMethodNameAndSignature( String obfName, String obfSignature, String deobfName, String deobfSignature ) | ||
| 84 | { | ||
| 85 | if( deobfName == null ) | ||
| 86 | { | ||
| 87 | throw new IllegalArgumentException( "deobf name cannot be null!" ); | ||
| 88 | } | ||
| 89 | |||
| 90 | MethodIndex methodIndex = m_methodsByObf.get( getMethodKey( obfName, obfSignature ) ); | ||
| 91 | if( methodIndex == null ) | ||
| 92 | { | ||
| 93 | methodIndex = createMethodIndex( obfName, obfSignature ); | ||
| 94 | } | ||
| 95 | |||
| 96 | m_methodsByDeobf.remove( getMethodKey( methodIndex.getDeobfName(), methodIndex.getDeobfSignature() ) ); | ||
| 97 | methodIndex.setDeobfName( deobfName ); | ||
| 98 | methodIndex.setDeobfSignature( deobfSignature ); | ||
| 99 | m_methodsByDeobf.put( getMethodKey( deobfName, deobfSignature ), methodIndex ); | ||
| 100 | } | ||
| 101 | |||
| 102 | public void updateDeobfMethodSignatures( Translator translator ) | ||
| 103 | { | ||
| 104 | for( MethodIndex methodIndex : m_methodsByObf.values() ) | ||
| 105 | { | ||
| 106 | methodIndex.setDeobfSignature( translator.translateSignature( methodIndex.getObfSignature() ) ); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | public void setArgumentName( String obfMethodName, String obfMethodSignature, int index, String obfName, String deobfName ) | ||
| 111 | { | ||
| 112 | if( deobfName == null ) | ||
| 113 | { | ||
| 114 | throw new IllegalArgumentException( "deobf name cannot be null!" ); | ||
| 115 | } | ||
| 116 | |||
| 117 | MethodIndex methodIndex = m_methodsByObf.get( getMethodKey( obfMethodName, obfMethodSignature ) ); | ||
| 118 | if( methodIndex == null ) | ||
| 119 | { | ||
| 120 | methodIndex = createMethodIndex( obfMethodName, obfMethodSignature ); | ||
| 121 | } | ||
| 122 | methodIndex.setArgumentName( index, obfName, deobfName ); | ||
| 123 | } | ||
| 124 | |||
| 125 | private MethodIndex createMethodIndex( String obfName, String obfSignature ) | ||
| 126 | { | ||
| 127 | MethodIndex methodIndex = new MethodIndex( obfName, obfSignature, obfName, obfSignature ); | ||
| 128 | String key = getMethodKey( obfName, obfSignature ); | ||
| 129 | m_methodsByObf.put( key, methodIndex ); | ||
| 130 | m_methodsByDeobf.put( key, methodIndex ); | ||
| 131 | return methodIndex; | ||
| 132 | } | ||
| 133 | |||
| 134 | @Override | ||
| 135 | public String toString( ) | ||
| 136 | { | ||
| 137 | StringBuilder buf = new StringBuilder(); | ||
| 138 | buf.append( m_obfName ); | ||
| 139 | buf.append( " <-> " ); | ||
| 140 | buf.append( m_deobfName ); | ||
| 141 | buf.append( "\n" ); | ||
| 142 | buf.append( "Fields:\n" ); | ||
| 143 | for( Map.Entry<String,String> entry : m_fieldsObfToDeobf.entrySet() ) | ||
| 144 | { | ||
| 145 | buf.append( "\t" ); | ||
| 146 | buf.append( entry.getKey() ); | ||
| 147 | buf.append( " <-> " ); | ||
| 148 | buf.append( entry.getValue() ); | ||
| 149 | buf.append( "\n" ); | ||
| 150 | } | ||
| 151 | buf.append( "Methods:\n" ); | ||
| 152 | for( MethodIndex methodIndex : m_methodsByObf.values() ) | ||
| 153 | { | ||
| 154 | buf.append( methodIndex.toString() ); | ||
| 155 | buf.append( "\n" ); | ||
| 156 | } | ||
| 157 | return buf.toString(); | ||
| 158 | } | ||
| 159 | } | ||