diff options
Diffstat (limited to 'src/cuchaz/enigma/mapping/Mappings.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/Mappings.java | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/mapping/Mappings.java b/src/cuchaz/enigma/mapping/Mappings.java new file mode 100644 index 0000000..2a39057 --- /dev/null +++ b/src/cuchaz/enigma/mapping/Mappings.java | |||
| @@ -0,0 +1,128 @@ | |||
| 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.IOException; | ||
| 14 | import java.io.InputStream; | ||
| 15 | import java.io.ObjectInputStream; | ||
| 16 | import java.io.Serializable; | ||
| 17 | import java.util.Map; | ||
| 18 | import java.util.zip.GZIPInputStream; | ||
| 19 | |||
| 20 | import com.beust.jcommander.internal.Maps; | ||
| 21 | |||
| 22 | import cuchaz.enigma.Util; | ||
| 23 | |||
| 24 | public class Mappings implements Serializable | ||
| 25 | { | ||
| 26 | private static final long serialVersionUID = 4649790259460259026L; | ||
| 27 | |||
| 28 | protected Map<String,ClassMapping> m_classesByObf; | ||
| 29 | protected Map<String,ClassMapping> m_classesByDeobf; | ||
| 30 | |||
| 31 | public Mappings( ) | ||
| 32 | { | ||
| 33 | m_classesByObf = Maps.newHashMap(); | ||
| 34 | m_classesByDeobf = Maps.newHashMap(); | ||
| 35 | } | ||
| 36 | |||
| 37 | public Mappings( Iterable<ClassMapping> classes ) | ||
| 38 | { | ||
| 39 | this(); | ||
| 40 | |||
| 41 | for( ClassMapping classMapping : classes ) | ||
| 42 | { | ||
| 43 | m_classesByObf.put( classMapping.getObfName(), classMapping ); | ||
| 44 | m_classesByDeobf.put( classMapping.getDeobfName(), classMapping ); | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | public static Mappings newFromResource( String resource ) | ||
| 49 | throws IOException | ||
| 50 | { | ||
| 51 | InputStream in = null; | ||
| 52 | try | ||
| 53 | { | ||
| 54 | in = Mappings.class.getResourceAsStream( resource ); | ||
| 55 | return newFromStream( in ); | ||
| 56 | } | ||
| 57 | finally | ||
| 58 | { | ||
| 59 | Util.closeQuietly( in ); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | public Iterable<ClassMapping> classes( ) | ||
| 64 | { | ||
| 65 | assert( m_classesByObf.size() == m_classesByDeobf.size() ); | ||
| 66 | return m_classesByObf.values(); | ||
| 67 | } | ||
| 68 | |||
| 69 | protected void addClassMapping( ClassMapping classMapping ) | ||
| 70 | { | ||
| 71 | m_classesByObf.put( classMapping.getObfName(), classMapping ); | ||
| 72 | m_classesByDeobf.put( classMapping.getDeobfName(), classMapping ); | ||
| 73 | } | ||
| 74 | |||
| 75 | public ClassMapping getClassByObf( ClassEntry entry ) | ||
| 76 | { | ||
| 77 | return getClassByObf( entry.getName() ); | ||
| 78 | } | ||
| 79 | |||
| 80 | public ClassMapping getClassByObf( String obfName ) | ||
| 81 | { | ||
| 82 | return m_classesByObf.get( obfName ); | ||
| 83 | } | ||
| 84 | |||
| 85 | public ClassMapping getClassByDeobf( ClassEntry entry ) | ||
| 86 | { | ||
| 87 | return getClassByObf( entry.getName() ); | ||
| 88 | } | ||
| 89 | |||
| 90 | public ClassMapping getClassByDeobf( String deobfName ) | ||
| 91 | { | ||
| 92 | return m_classesByDeobf.get( deobfName ); | ||
| 93 | } | ||
| 94 | |||
| 95 | public Translator getTranslator( Ancestries ancestries, TranslationDirection direction ) | ||
| 96 | { | ||
| 97 | return new Translator( | ||
| 98 | direction, | ||
| 99 | direction.choose( m_classesByObf, m_classesByDeobf ), | ||
| 100 | direction.choose( ancestries, new DeobfuscatedAncestries( ancestries, m_classesByObf, m_classesByDeobf ) ) | ||
| 101 | ); | ||
| 102 | } | ||
| 103 | |||
| 104 | public static Mappings newFromStream( InputStream in ) | ||
| 105 | throws IOException | ||
| 106 | { | ||
| 107 | try | ||
| 108 | { | ||
| 109 | return (Mappings)new ObjectInputStream( new GZIPInputStream( in ) ).readObject(); | ||
| 110 | } | ||
| 111 | catch( ClassNotFoundException ex ) | ||
| 112 | { | ||
| 113 | throw new Error( ex ); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | @Override | ||
| 118 | public String toString( ) | ||
| 119 | { | ||
| 120 | StringBuilder buf = new StringBuilder(); | ||
| 121 | for( ClassMapping classMapping : m_classesByObf.values() ) | ||
| 122 | { | ||
| 123 | buf.append( classMapping.toString() ); | ||
| 124 | buf.append( "\n" ); | ||
| 125 | } | ||
| 126 | return buf.toString(); | ||
| 127 | } | ||
| 128 | } | ||