11
11
import java .awt .event .MouseEvent ;
12
12
import java .awt .event .WindowAdapter ;
13
13
import java .awt .event .WindowEvent ;
14
- import java .io .BufferedReader ;
15
14
import java .io .File ;
16
15
import java .io .FileInputStream ;
17
- import java .io .InputStreamReader ;
16
+ import java .io .IOException ;
17
+ import java .io .OutputStream ;
18
+ import java .io .PrintStream ;
19
+ import java .net .URL ;
20
+ import java .net .URLClassLoader ;
18
21
import java .util .ArrayList ;
19
22
import java .util .List ;
20
23
import java .util .regex .Matcher ;
28
31
29
32
public class DeobfuscatorFrame
30
33
{
31
- private static final String VERSION = "1.3" ;
34
+ private static final String VERSION = "2.0" ;
35
+
36
+ /**
37
+ * New - Latest API
38
+ * Legacy - Old API
39
+ */
40
+ private static DeobfuscatorVersion DEOBFUSCATOR_VERSION = DeobfuscatorVersion .UNKNOWN ;
32
41
private JFrame frame ;
33
42
private JTextField deobfuscatorField ;
34
43
private File deobfuscatorPath ;
@@ -41,7 +50,23 @@ public class DeobfuscatorFrame
41
50
private JList <String > selectedTransformersJList ;
42
51
private DefaultListModel <String > librariesList ;
43
52
private File libraryPath ;
44
- private Process process ;
53
+ private Thread thread ;
54
+
55
+ /**
56
+ * The singleton instance of URLClassLoader
57
+ */
58
+ private URLClassLoader loader ;
59
+
60
+ /**
61
+ * A list of transformers in this deobfuscator
62
+ */
63
+ private List <Class <?>> transformerClasses = new ArrayList <>();
64
+
65
+ /**
66
+ * If it is legacy mode, has Deobfuscator.class and Transformer.class
67
+ * If its new mode, has Deobfuscator.class, Configuration.class, TransformerConfigDeserializer.class, and Transformer.class
68
+ */
69
+ private Class <?>[] loadClasses ;
45
70
46
71
/**
47
72
* Launch the application.
@@ -509,27 +534,7 @@ public void actionPerformed(ActionEvent e)
509
534
public void actionPerformed (ActionEvent e )
510
535
{
511
536
btnRun .setEnabled (false );
512
- // Converts the above into args
513
- List <String > command = new ArrayList <>();
514
- command .add ("java" );
515
- command .add ("-jar" );
516
- command .add (deobfuscatorField .getText ());
517
- command .add ("-input" );
518
- command .add (inputField .getText ());
519
- command .add ("-output" );
520
- command .add (outputField .getText ());
521
- for (int i = 0 ; i < selectedTransformers .getSize (); i ++)
522
- {
523
- command .add ("-transformer" );
524
- command .add (selectedTransformers .get (i ));
525
- }
526
- for (int i = 0 ; i < librariesList .getSize (); i ++)
527
- {
528
- command .add ("-path" );
529
- command .add (librariesList .get (i ));
530
- }
531
537
// Start
532
- ProcessBuilder builder = new ProcessBuilder (command );
533
538
JFrame newFrame = new JFrame ();
534
539
newFrame .setTitle ("Console" );
535
540
JTextArea area = new JTextArea ();
@@ -538,44 +543,75 @@ public void actionPerformed(ActionEvent e)
538
543
newFrame .pack ();
539
544
newFrame .setSize (800 , 600 );
540
545
newFrame .setVisible (true );
541
- SwingWorker <Void , String > worker = new SwingWorker <Void , String >()
546
+ PrintStream print = new PrintStream (new DeobfuscatorOutputStream (area ));
547
+ System .setErr (print );
548
+ System .setOut (print );
549
+ // Runs it using reflection
550
+ thread = new Thread (new Runnable ()
542
551
{
543
552
@ Override
544
- protected Void doInBackground () throws Exception
545
- {
546
- builder .redirectErrorStream (true );
547
- Process process = builder .start ();
548
- DeobfuscatorFrame .this .process = process ;
549
- BufferedReader reader = new BufferedReader (
550
- new InputStreamReader (process .getInputStream ()));
551
- String line ;
552
- while ((line = reader .readLine ()) != null )
553
- publish (line );
554
- return null ;
555
- }
556
-
557
- @ Override
558
- protected void process (List <String > chunks )
553
+ public void run ()
559
554
{
560
- for (String line : chunks )
555
+ if (DEOBFUSCATOR_VERSION == DeobfuscatorVersion .NEW )
556
+ {
557
+ try
558
+ {
559
+ Object configuration = loadClasses [1 ].newInstance ();
560
+ loadClasses [1 ].getDeclaredMethod ("setInput" , File .class ).
561
+ invoke (configuration , new File (inputField .getText ()));
562
+ loadClasses [1 ].getDeclaredMethod ("setOutput" , File .class ).
563
+ invoke (configuration , new File (outputField .getText ()));
564
+ List <Object > transformers = new ArrayList <>();
565
+ for (Object transformer : selectedTransformers .toArray ())
566
+ try
567
+ {
568
+ Class <?> transformerClass = null ;
569
+ for (Class <?> clazz : transformerClasses )
570
+ if (clazz .getName ().equals ("com.javadeobfuscator.deobfuscator.transformers." + transformer ))
571
+ transformerClass = clazz ;
572
+ if (transformerClass == null )
573
+ throw new ClassNotFoundException ();
574
+ Object transformerConfig =
575
+ loadClasses [2 ].getDeclaredMethod ("configFor" , Class .class ).invoke (
576
+ null , transformerClass .asSubclass (loadClasses [3 ]));
577
+ transformers .add (transformerConfig );
578
+ }catch (ClassNotFoundException e )
579
+ {
580
+ System .out .println ("Could not find transformer " + transformer );
581
+ continue ;
582
+ }
583
+ loadClasses [1 ].getDeclaredMethod ("setTransformers" , List .class ).
584
+ invoke (configuration , transformers );
585
+ List <File > libraries = new ArrayList <>();
586
+ for (Object library : librariesList .toArray ())
587
+ libraries .add (new File ((String )library ));
588
+ loadClasses [1 ].getDeclaredMethod ("setPath" , List .class ).
589
+ invoke (configuration , libraries );
590
+ Object deobfuscator =
591
+ loadClasses [0 ].getDeclaredConstructor (loadClasses [1 ]).newInstance (configuration );
592
+ loadClasses [0 ].getDeclaredMethod ("start" ).invoke (deobfuscator );
593
+ }catch (Exception e )
594
+ {
595
+ e .printStackTrace (print );
596
+ }
597
+
598
+ }else
561
599
{
562
- area .append (line );
563
- area .append ("\n " );
600
+
564
601
}
565
602
}
566
- };
567
- worker . execute ();
603
+ }) ;
604
+ thread . start ();
568
605
newFrame .addWindowListener (new WindowAdapter ()
569
606
{
570
607
@ Override
571
608
public void windowClosing (WindowEvent e )
572
609
{
573
610
btnRun .setEnabled (true );
574
- worker .cancel (true );
575
- if (process != null )
611
+ if (thread != null )
576
612
{
577
- process . destroyForcibly ();
578
- process = null ;
613
+ thread . destroy ();
614
+ thread = null ;
579
615
}
580
616
e .getWindow ().dispose ();
581
617
}
@@ -588,6 +624,13 @@ private void loadTransformers(String path, JLabel displayLabel)
588
624
{
589
625
try
590
626
{
627
+ if (loader != null )
628
+ loader .close ();
629
+ loader = URLClassLoader .newInstance (new URL []{new File (path ).toURI ().toURL ()}, DeobfuscatorFrame .class .getClassLoader ());
630
+ transformerClasses .clear ();
631
+ transformerList .clear ();
632
+ DEOBFUSCATOR_VERSION = DeobfuscatorVersion .LEGACY ;
633
+ Class <?> transformerClass = loader .loadClass ("com.javadeobfuscator.deobfuscator.transformers.Transformer" );
591
634
ZipInputStream zip = new ZipInputStream (new FileInputStream (path ));
592
635
for (ZipEntry entry = zip .getNextEntry (); entry != null ; entry =
593
636
zip .getNextEntry ())
@@ -602,13 +645,33 @@ private void loadTransformers(String path, JLabel displayLabel)
602
645
{
603
646
String name = className .substring (0 ,
604
647
className .length () - ".class" .length ());
605
- String toPut = name .substring (
606
- "com.javadeobfuscator.deobfuscator.transformers."
607
- .length ());
608
- if (!transformerList .contains (toPut ))
609
- transformerList .addElement (toPut );
648
+ Class <?> clazz = loader .loadClass (name );
649
+ if (transformerClass .isAssignableFrom (clazz ))
650
+ {
651
+ transformerClasses .add (loader .loadClass (name ));
652
+ String toPut = name .substring (
653
+ "com.javadeobfuscator.deobfuscator.transformers."
654
+ .length ());
655
+ if (!transformerList .contains (toPut ))
656
+ transformerList .addElement (toPut );
657
+ }
658
+ }else if (className .equals ("com.javadeobfuscator.deobfuscator.config.Configuration.class" ))
659
+ {
660
+ loadClasses = new Class <?>[4 ];
661
+ loadClasses [0 ] = loader .loadClass ("com.javadeobfuscator.deobfuscator.Deobfuscator" );
662
+ loadClasses [1 ] = loader .loadClass ("com.javadeobfuscator.deobfuscator.config.Configuration" );
663
+ loadClasses [2 ] = loader .loadClass ("com.javadeobfuscator.deobfuscator.config.TransformerConfig" );
664
+ loadClasses [3 ] = loader .loadClass ("com.javadeobfuscator.deobfuscator.transformers.Transformer" );
665
+ DEOBFUSCATOR_VERSION = DeobfuscatorVersion .NEW ;
610
666
}
611
667
}
668
+ if (DEOBFUSCATOR_VERSION == DeobfuscatorVersion .LEGACY )
669
+ {
670
+ loadClasses = new Class <?>[2 ];
671
+ loadClasses [0 ] = new URLClassLoader (new URL []{new URL (path )}).
672
+ loadClass ("com.javadeobfuscator.deobfuscator.Deobfuscator" );
673
+ loadClasses [1 ] = loader .loadClass ("com.javadeobfuscator.deobfuscator.transformers.Transformer" );
674
+ }
612
675
zip .close ();
613
676
displayLabel .setText ("Successfully loaded transformers!" );
614
677
displayLabel .setForeground (Color .GREEN );
@@ -620,4 +683,28 @@ private void loadTransformers(String path, JLabel displayLabel)
620
683
displayLabel .setForeground (Color .red );
621
684
}
622
685
}
686
+
687
+ private static enum DeobfuscatorVersion
688
+ {
689
+ NEW ,
690
+ LEGACY ,
691
+ UNKNOWN ;
692
+ }
693
+
694
+ private class DeobfuscatorOutputStream extends OutputStream
695
+ {
696
+ private JTextArea console ;
697
+
698
+ public DeobfuscatorOutputStream (JTextArea console )
699
+ {
700
+ this .console = console ;
701
+ }
702
+
703
+ @ Override
704
+ public void write (int b ) throws IOException
705
+ {
706
+ console .append (String .valueOf ((char )b ));
707
+ console .setCaretPosition (console .getDocument ().getLength ());
708
+ }
709
+ }
623
710
}
0 commit comments