@@ -29,17 +29,18 @@ public class Main extends Thread {
29
29
public static final HashMap <String , Transformer > transformers = new HashMap <>();
30
30
31
31
public File inF , outF ;
32
- public ArrayList <ClassNode > classes = new ArrayList <ClassNode >();
33
- public ArrayList <ClassFile > files = new ArrayList <ClassFile >();
34
- public HashMap <String , ClassNode > nameToNode = new HashMap <String , ClassNode >();
35
- public HashMap <ClassNode , String > nodeToName = new HashMap <ClassNode , String >();
32
+ public ArrayList <ClassNode > classes = new ArrayList <>();
33
+ public ArrayList <ClassFile > files = new ArrayList <>();
34
+ public HashMap <String , ClassNode > nameToNode = new HashMap <>();
35
+ public HashMap <ClassNode , String > nodeToName = new HashMap <>();
36
36
public String [] libraries ;
37
- public ArrayList <Library > loadedAPI = new ArrayList <Library >();
37
+ public ArrayList <Library > loadedAPI = new ArrayList <>();
38
38
public String [] usedTransformers ;
39
- public HashMap <String , Integer > pkgLens = new HashMap <String , Integer >();
39
+ public HashMap <String , Integer > pkgLens = new HashMap <>();
40
40
public SmartNameGen nameGen ;
41
41
public String [] args ;
42
42
public CommandLine cmd ;
43
+ public final long startTime = System .currentTimeMillis ();
43
44
44
45
public static Main getInstance () {
45
46
return instance ;
@@ -78,6 +79,11 @@ public void parseArgs() throws Throwable {
78
79
dictionaryArg .setArgName ("1/2/3" );
79
80
options .addOption (dictionaryArg );
80
81
82
+ Option testArg = new Option ( null , "test" , false , "Means that no output are written to file" );
83
+
84
+ testArg .setRequired (false );
85
+ options .addOption (testArg );
86
+
81
87
CommandLineParser parser = new DefaultParser ();
82
88
HelpFormatter formatter = new HelpFormatter ();
83
89
@@ -95,15 +101,17 @@ public void parseArgs() throws Throwable {
95
101
if (!(inF = new File (cmd .getOptionValue ("input" ))).exists ())
96
102
throw new Throwable ("Error: Input file does not exist." );
97
103
98
- if ((outF = new File (cmd .getOptionValue ("output" ))).exists ())
104
+ if (! cmd . hasOption ( "test" ) && (outF = new File (cmd .getOptionValue ("output" ))).exists ())
99
105
throw new Throwable ("Error: Output file already exists." );
100
106
101
- ArrayList < String > librariesList = new ArrayList <>();
102
- if ( cmd . hasOption ( "libraries" ))
107
+ if ( cmd . hasOption ( "libraries" )) {
108
+ ArrayList < String > librariesList = new ArrayList <>();
103
109
for (String libraryName1 : cmd .getOptionValues ("libraries" ))
104
110
for (String libraryName2 : libraryName1 .split (";" ))
105
111
librariesList .add (libraryName2 );
106
- libraries = librariesList .toArray (new String [librariesList .size ()]);
112
+
113
+ libraries = librariesList .toArray (new String [librariesList .size ()]);
114
+ }
107
115
108
116
ArrayList <String > usedTransformersList = new ArrayList <>();
109
117
for (String transformerName1 : cmd .getOptionValues ("transformers" ))
@@ -142,10 +150,6 @@ public void parseArgs() throws Throwable {
142
150
transformers .put ("TryCatch" , new TryCatch ());
143
151
}
144
152
145
- public static boolean isEmpty (MethodNode mn ) {
146
- return mn .instructions .getFirst () != null ;
147
- }
148
-
149
153
public static void printLogo () {
150
154
System .out .println ("|---------------------------------------------------------------------------|" );
151
155
System .out .println ("| __ __ ______ ______ __ __ ______ ______ ______ |" );
@@ -171,21 +175,23 @@ public void run() {
171
175
return ;
172
176
}
173
177
174
- System .out .println ("Loading java APIs..." );
178
+ System .out .println ("Loading java APIs... " + getDateTag () );
175
179
new DirWalker (new File (System .getProperty ("java.home" ) + File .separatorChar + "lib" ), true );
176
- System .out .println ("Loading user APIs..." );
177
- for (String lib : libraries )
178
- new DirWalker (new File (lib ), true );
179
- System .out .println ("All APIs loaded!" );
180
+ if (libraries != null ) {
181
+ System .out .println ("Loading user APIs... " + getDateTag ());
182
+ for (String lib : libraries )
183
+ new DirWalker (new File (lib ), true );
184
+ }
185
+ System .out .println ("All APIs loaded! " + getDateTag ());
180
186
181
187
System .out .println ("--------------------------------------------------" );
182
188
183
- System .out .println ("Loading input file..." );
189
+ System .out .println ("Loading input file... " + getDateTag () );
184
190
new DirWalker (inF , false );
185
191
186
192
System .out .println ("--------------------------------------------------" );
187
193
188
- System .out .println ("Making class tree..." );
194
+ System .out .println ("Making class tree... " + getDateTag () );
189
195
CustomClassWriter .loadHierachy ();
190
196
191
197
ArrayList <ClassNode > modClasses = new ArrayList <>(classes );
@@ -195,14 +201,14 @@ public void run() {
195
201
System .out .println ("--------------------------------------------------" );
196
202
197
203
for (String transformerName : usedTransformers ) {
198
- System .out .println ("Started transformation with " + transformerName + " transformer" );
204
+ System .out .println ("Started transformation with " + transformerName + " transformer " + getDateTag () );
199
205
200
206
try {
201
207
modClasses = transformers .get (transformerName ).obfuscate (modClasses );
202
208
} catch (NullPointerException npe ) {
203
209
throw new Throwable ("Transformer name \" " + transformerName + "\" aren't defined in Main#transformers." , npe );
204
210
}
205
- System .out .println ("Transformation completed with " + transformerName + " transformer" );
211
+ System .out .println ("Transformation completed with " + transformerName + " transformer " + getDateTag () );
206
212
}
207
213
208
214
System .out .println ("--------------------------------------------------" );
@@ -212,11 +218,16 @@ public void run() {
212
218
dump (cn , true );
213
219
System .out .println ("--------------------------------------------------" );
214
220
System .out .println ("All classes dumped! " + getDateTag ());
215
- System .out .println ("Saving all classes..." );
216
- saveAll ();
217
- System .out .println ("All classes saved!" );
221
+ if (cmd .hasOption ("test" ))
222
+ System .out .println ("All transformations and dumps went successfully!" );
223
+ else {
224
+ System .out .println ("Saving all classes... " + getDateTag ());
225
+ saveAll ();
226
+ System .out .println ("All classes saved! " + getDateTag ());
227
+ }
218
228
} catch (Throwable t ) {
219
229
System .out .println (t .getMessage ());
230
+ t .printStackTrace ();
220
231
}
221
232
}
222
233
@@ -257,10 +268,8 @@ public static String getPackageByClass(String className) {
257
268
return className .substring (0 , className .lastIndexOf ('.' ));
258
269
}
259
270
260
- public static SimpleDateFormat sdf = new SimpleDateFormat ("hh:mm:ss" );
261
-
262
- public static String getDateTag () {
263
- return "[" + sdf .format (new Date ()) + "]" ;
271
+ public String getDateTag () {
272
+ return "[" + (System .currentTimeMillis () - startTime ) + "ms]" ;
264
273
}
265
274
266
275
public byte [] dump (ClassNode node , boolean autoAdd ) {
@@ -299,46 +308,16 @@ public byte[] dump(ClassNode node, boolean autoAdd) {
299
308
300
309
return classBytes ;
301
310
} catch (Throwable t ) {
302
- System .out .println ("Error occurred while writing " + node .name + ". This class will be original . Exception: " + t .getMessage ());
311
+ System .out .println ("Error occurred while writing " + node .name + ". This class will not be written . Exception: " + t .getMessage ());
303
312
}
304
313
return null ;
305
314
}
306
315
307
316
public void saveAll () throws Throwable {
308
317
outF .createNewFile ();
309
318
ZipOutputStream out = new ZipOutputStream (new FileOutputStream (outF ));
310
- /* Start of combination of jar's */
311
- ZipFile zf = new ZipFile (inF );
312
- Enumeration <? extends ZipEntry > in = zf .entries ();
313
-
314
319
byte [] data ;
315
320
316
- while (in .hasMoreElements ()) {
317
- ZipEntry ze = in .nextElement ();
318
- boolean finded = false ;
319
-
320
- for (ClassFile mc : files )
321
- if (mc != null && ze != null && mc .name != null && ze .getName () != null
322
- && mc .name .equals (ze .getName ())) {
323
- finded = true ;
324
- break ;
325
- }
326
-
327
- if (zf != null && ze != null && !finded ) {
328
- DataInputStream dis = new DataInputStream (zf .getInputStream (ze ));
329
- data = new byte [(int ) ze .getSize ()];
330
- dis .readFully (data );
331
- dis .close ();
332
-
333
- ze = modifyEntry (new ZipEntry (ze .getName ()));
334
-
335
- out .putNextEntry (ze );
336
- out .write (data , 0 , data .length );
337
- out .closeEntry ();
338
- }
339
- }
340
- zf .close ();
341
- /* End of combination of jar's */
342
321
for (ClassFile mc : files )
343
322
try {
344
323
data = mc .bytecode ;
0 commit comments