summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/mapping/MappingsReader.java
diff options
context:
space:
mode:
authorGravatar jeff2015-01-13 23:25:04 -0500
committerGravatar jeff2015-01-13 23:25:04 -0500
commit959cb5fd4f9586ec3bd265b452fe25fe1db82e3f (patch)
treebdd8a2c52c2fe053ba3460614bde8542e5378dbe /src/cuchaz/enigma/mapping/MappingsReader.java
parentgot rid of gradle in favor of ivy+ssjb (diff)
downloadenigma-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/MappingsReader.java')
-rw-r--r--src/cuchaz/enigma/mapping/MappingsReader.java218
1 files changed, 83 insertions, 135 deletions
diff --git a/src/cuchaz/enigma/mapping/MappingsReader.java b/src/cuchaz/enigma/mapping/MappingsReader.java
index 4bd9f12..72e829d 100644
--- a/src/cuchaz/enigma/mapping/MappingsReader.java
+++ b/src/cuchaz/enigma/mapping/MappingsReader.java
@@ -20,209 +20,157 @@ import com.google.common.collect.Queues;
20import cuchaz.enigma.Constants; 20import cuchaz.enigma.Constants;
21import cuchaz.enigma.mapping.SignatureUpdater.ClassNameUpdater; 21import cuchaz.enigma.mapping.SignatureUpdater.ClassNameUpdater;
22 22
23public class MappingsReader 23public class MappingsReader {
24{ 24
25 public Mappings read( Reader in ) 25 public Mappings read(Reader in) throws IOException, MappingParseException {
26 throws IOException, MappingParseException 26 return read(new BufferedReader(in));
27 {
28 return read( new BufferedReader( in ) );
29 } 27 }
30 28
31 public Mappings read( BufferedReader in ) 29 public Mappings read(BufferedReader in) throws IOException, MappingParseException {
32 throws IOException, MappingParseException
33 {
34 Mappings mappings = new Mappings(); 30 Mappings mappings = new Mappings();
35 Deque<Object> mappingStack = Queues.newArrayDeque(); 31 Deque<Object> mappingStack = Queues.newArrayDeque();
36 32
37 int lineNumber = 0; 33 int lineNumber = 0;
38 String line = null; 34 String line = null;
39 while( ( line = in.readLine() ) != null ) 35 while ( (line = in.readLine()) != null) {
40 {
41 lineNumber++; 36 lineNumber++;
42 37
43 // strip comments 38 // strip comments
44 int commentPos = line.indexOf( '#' ); 39 int commentPos = line.indexOf('#');
45 if( commentPos >= 0 ) 40 if (commentPos >= 0) {
46 { 41 line = line.substring(0, commentPos);
47 line = line.substring( 0, commentPos );
48 } 42 }
49 43
50 // skip blank lines 44 // skip blank lines
51 if( line.trim().length() <= 0 ) 45 if (line.trim().length() <= 0) {
52 {
53 continue; 46 continue;
54 } 47 }
55 48
56 // get the indent of this line 49 // get the indent of this line
57 int indent = 0; 50 int indent = 0;
58 for( int i=0; i<line.length(); i++ ) 51 for (int i = 0; i < line.length(); i++) {
59 { 52 if (line.charAt(i) != '\t') {
60 if( line.charAt( i ) != '\t' )
61 {
62 break; 53 break;
63 } 54 }
64 indent++; 55 indent++;
65 } 56 }
66 57
67 // handle stack pops 58 // handle stack pops
68 while( indent < mappingStack.size() ) 59 while (indent < mappingStack.size()) {
69 {
70 mappingStack.pop(); 60 mappingStack.pop();
71 } 61 }
72 62
73 String[] parts = line.trim().split( "\\s" ); 63 String[] parts = line.trim().split("\\s");
74 try 64 try {
75 {
76 // read the first token 65 // read the first token
77 String token = parts[0]; 66 String token = parts[0];
78 67
79 if( token.equalsIgnoreCase( "CLASS" ) ) 68 if (token.equalsIgnoreCase("CLASS")) {
80 {
81 ClassMapping classMapping; 69 ClassMapping classMapping;
82 if( indent == 0 ) 70 if (indent == 0) {
83 {
84 // outer class 71 // outer class
85 classMapping = readClass( parts, false ); 72 classMapping = readClass(parts, false);
86 mappings.addClassMapping( classMapping ); 73 mappings.addClassMapping(classMapping);
87 } 74 } else if (indent == 1) {
88 else if( indent == 1 )
89 {
90 // inner class 75 // inner class
91 if( !( mappingStack.getFirst() instanceof ClassMapping ) ) 76 if (! (mappingStack.getFirst() instanceof ClassMapping)) {
92 { 77 throw new MappingParseException(lineNumber, "Unexpected CLASS entry here!");
93 throw new MappingParseException( lineNumber, "Unexpected CLASS entry here!" );
94 } 78 }
95 79
96 classMapping = readClass( parts, true ); 80 classMapping = readClass(parts, true);
97 ((ClassMapping)mappingStack.getFirst()).addInnerClassMapping( classMapping ); 81 ((ClassMapping)mappingStack.getFirst()).addInnerClassMapping(classMapping);
82 } else {
83 throw new MappingParseException(lineNumber, "Unexpected CLASS entry nesting!");
98 } 84 }
99 else 85 mappingStack.push(classMapping);
100 { 86 } else if (token.equalsIgnoreCase("FIELD")) {
101 throw new MappingParseException( lineNumber, "Unexpected CLASS entry nesting!" ); 87 if (mappingStack.isEmpty() || ! (mappingStack.getFirst() instanceof ClassMapping)) {
88 throw new MappingParseException(lineNumber, "Unexpected FIELD entry here!");
102 } 89 }
103 mappingStack.push( classMapping ); 90 ((ClassMapping)mappingStack.getFirst()).addFieldMapping(readField(parts));
104 } 91 } else if (token.equalsIgnoreCase("METHOD")) {
105 else if( token.equalsIgnoreCase( "FIELD" ) ) 92 if (mappingStack.isEmpty() || ! (mappingStack.getFirst() instanceof ClassMapping)) {
106 { 93 throw new MappingParseException(lineNumber, "Unexpected METHOD entry here!");
107 if( mappingStack.isEmpty() || !(mappingStack.getFirst() instanceof ClassMapping) )
108 {
109 throw new MappingParseException( lineNumber, "Unexpected FIELD entry here!" );
110 }
111 ((ClassMapping)mappingStack.getFirst()).addFieldMapping( readField( parts ) );
112 }
113 else if( token.equalsIgnoreCase( "METHOD" ) )
114 {
115 if( mappingStack.isEmpty() || !(mappingStack.getFirst() instanceof ClassMapping) )
116 {
117 throw new MappingParseException( lineNumber, "Unexpected METHOD entry here!" );
118 } 94 }
119 MethodMapping methodMapping = readMethod( parts ); 95 MethodMapping methodMapping = readMethod(parts);
120 ((ClassMapping)mappingStack.getFirst()).addMethodMapping( methodMapping ); 96 ((ClassMapping)mappingStack.getFirst()).addMethodMapping(methodMapping);
121 mappingStack.push( methodMapping ); 97 mappingStack.push(methodMapping);
122 } 98 } else if (token.equalsIgnoreCase("ARG")) {
123 else if( token.equalsIgnoreCase( "ARG" ) ) 99 if (mappingStack.isEmpty() || ! (mappingStack.getFirst() instanceof MethodMapping)) {
124 { 100 throw new MappingParseException(lineNumber, "Unexpected ARG entry here!");
125 if( mappingStack.isEmpty() || !(mappingStack.getFirst() instanceof MethodMapping) )
126 {
127 throw new MappingParseException( lineNumber, "Unexpected ARG entry here!" );
128 } 101 }
129 ((MethodMapping)mappingStack.getFirst()).addArgumentMapping( readArgument( parts ) ); 102 ((MethodMapping)mappingStack.getFirst()).addArgumentMapping(readArgument(parts));
130 } 103 }
131 } 104 } catch (ArrayIndexOutOfBoundsException | NumberFormatException ex) {
132 catch( ArrayIndexOutOfBoundsException | NumberFormatException ex ) 105 throw new MappingParseException(lineNumber, "Malformed line!");
133 {
134 throw new MappingParseException( lineNumber, "Malformed line!" );
135 } 106 }
136 } 107 }
137 108
138 return mappings; 109 return mappings;
139 } 110 }
140 111
141 private ArgumentMapping readArgument( String[] parts ) 112 private ArgumentMapping readArgument(String[] parts) {
142 { 113 return new ArgumentMapping(Integer.parseInt(parts[1]), parts[2]);
143 return new ArgumentMapping( Integer.parseInt( parts[1] ), parts[2] );
144 } 114 }
145 115
146 private ClassMapping readClass( String[] parts, boolean makeSimple ) 116 private ClassMapping readClass(String[] parts, boolean makeSimple) {
147 { 117 if (parts.length == 2) {
148 if( parts.length == 2 ) 118 String obfName = processName(parts[1], makeSimple);
149 { 119 return new ClassMapping(obfName);
150 String obfName = processName( parts[1], makeSimple ); 120 } else {
151 return new ClassMapping( obfName ); 121 String obfName = processName(parts[1], makeSimple);
152 } 122 String deobfName = processName(parts[2], makeSimple);
153 else 123 return new ClassMapping(obfName, deobfName);
154 {
155 String obfName = processName( parts[1], makeSimple );
156 String deobfName = processName( parts[2], makeSimple );
157 return new ClassMapping( obfName, deobfName );
158 } 124 }
159 } 125 }
160 126
161 private String processName( String name, boolean makeSimple ) 127 private String processName(String name, boolean makeSimple) {
162 { 128 if (makeSimple) {
163 if( makeSimple ) 129 return new ClassEntry(name).getSimpleName();
164 { 130 } else {
165 return new ClassEntry( name ).getSimpleName(); 131 return moveClassOutOfDefaultPackage(name, Constants.NonePackage);
166 }
167 else
168 {
169 return moveClassOutOfDefaultPackage( name, Constants.NonePackage );
170 } 132 }
171 } 133 }
172 134
173 private String moveClassOutOfDefaultPackage( String className, String newPackageName ) 135 private String moveClassOutOfDefaultPackage(String className, String newPackageName) {
174 { 136 ClassEntry classEntry = new ClassEntry(className);
175 ClassEntry classEntry = new ClassEntry( className ); 137 if (classEntry.isInDefaultPackage()) {
176 if( classEntry.isInDefaultPackage() )
177 {
178 return newPackageName + "/" + classEntry.getName(); 138 return newPackageName + "/" + classEntry.getName();
179 } 139 }
180 return className; 140 return className;
181 } 141 }
182 142
183 private FieldMapping readField( String[] parts ) 143 private FieldMapping readField(String[] parts) {
184 { 144 return new FieldMapping(parts[1], parts[2]);
185 return new FieldMapping( parts[1], parts[2] );
186 } 145 }
187 146
188 private MethodMapping readMethod( String[] parts ) 147 private MethodMapping readMethod(String[] parts) {
189 { 148 if (parts.length == 3) {
190 if( parts.length == 3 )
191 {
192 String obfName = parts[1]; 149 String obfName = parts[1];
193 String obfSignature = moveSignatureOutOfDefaultPackage( parts[2], Constants.NonePackage ); 150 String obfSignature = moveSignatureOutOfDefaultPackage(parts[2], Constants.NonePackage);
194 return new MethodMapping( obfName, obfSignature ); 151 return new MethodMapping(obfName, obfSignature);
195 } 152 } else {
196 else
197 {
198 String obfName = parts[1]; 153 String obfName = parts[1];
199 String deobfName = parts[2]; 154 String deobfName = parts[2];
200 String obfSignature = moveSignatureOutOfDefaultPackage( parts[3], Constants.NonePackage ); 155 String obfSignature = moveSignatureOutOfDefaultPackage(parts[3], Constants.NonePackage);
201 if( obfName.equals( deobfName ) ) 156 if (obfName.equals(deobfName)) {
202 { 157 return new MethodMapping(obfName, obfSignature);
203 return new MethodMapping( obfName, obfSignature ); 158 } else {
204 } 159 return new MethodMapping(obfName, obfSignature, deobfName);
205 else
206 {
207 return new MethodMapping( obfName, obfSignature, deobfName );
208 } 160 }
209 } 161 }
210 } 162 }
211 163
212 private String moveSignatureOutOfDefaultPackage( String signature, final String newPackageName ) 164 private String moveSignatureOutOfDefaultPackage(String signature, final String newPackageName) {
213 { 165 return SignatureUpdater.update(signature, new ClassNameUpdater() {
214 return SignatureUpdater.update( signature, new ClassNameUpdater( )
215 {
216 @Override 166 @Override
217 public String update( String className ) 167 public String update(String className) {
218 { 168 ClassEntry classEntry = new ClassEntry(className);
219 ClassEntry classEntry = new ClassEntry( className ); 169 if (classEntry.isInDefaultPackage()) {
220 if( classEntry.isInDefaultPackage() )
221 {
222 return newPackageName + "/" + className; 170 return newPackageName + "/" + className;
223 } 171 }
224 return className; 172 return className;
225 } 173 }
226 } ); 174 });
227 } 175 }
228} 176}