1111import java .awt .event .MouseEvent ;
1212import java .awt .event .WindowAdapter ;
1313import java .awt .event .WindowEvent ;
14- import java .io .BufferedReader ;
1514import java .io .File ;
1615import 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 ;
1821import java .util .ArrayList ;
1922import java .util .List ;
2023import java .util .regex .Matcher ;
2831
2932public class DeobfuscatorFrame
3033{
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 ;
3241 private JFrame frame ;
3342 private JTextField deobfuscatorField ;
3443 private File deobfuscatorPath ;
@@ -41,7 +50,23 @@ public class DeobfuscatorFrame
4150 private JList <String > selectedTransformersJList ;
4251 private DefaultListModel <String > librariesList ;
4352 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 ;
4570
4671 /**
4772 * Launch the application.
@@ -509,27 +534,7 @@ public void actionPerformed(ActionEvent e)
509534 public void actionPerformed (ActionEvent e )
510535 {
511536 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- }
531537 // Start
532- ProcessBuilder builder = new ProcessBuilder (command );
533538 JFrame newFrame = new JFrame ();
534539 newFrame .setTitle ("Console" );
535540 JTextArea area = new JTextArea ();
@@ -538,44 +543,75 @@ public void actionPerformed(ActionEvent e)
538543 newFrame .pack ();
539544 newFrame .setSize (800 , 600 );
540545 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 ()
542551 {
543552 @ 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 ()
559554 {
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
561599 {
562- area .append (line );
563- area .append ("\n " );
600+
564601 }
565602 }
566- };
567- worker . execute ();
603+ }) ;
604+ thread . start ();
568605 newFrame .addWindowListener (new WindowAdapter ()
569606 {
570607 @ Override
571608 public void windowClosing (WindowEvent e )
572609 {
573610 btnRun .setEnabled (true );
574- worker .cancel (true );
575- if (process != null )
611+ if (thread != null )
576612 {
577- process . destroyForcibly ();
578- process = null ;
613+ thread . destroy ();
614+ thread = null ;
579615 }
580616 e .getWindow ().dispose ();
581617 }
@@ -588,6 +624,13 @@ private void loadTransformers(String path, JLabel displayLabel)
588624 {
589625 try
590626 {
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" );
591634 ZipInputStream zip = new ZipInputStream (new FileInputStream (path ));
592635 for (ZipEntry entry = zip .getNextEntry (); entry != null ; entry =
593636 zip .getNextEntry ())
@@ -602,13 +645,33 @@ private void loadTransformers(String path, JLabel displayLabel)
602645 {
603646 String name = className .substring (0 ,
604647 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 ;
610666 }
611667 }
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+ }
612675 zip .close ();
613676 displayLabel .setText ("Successfully loaded transformers!" );
614677 displayLabel .setForeground (Color .GREEN );
@@ -620,4 +683,28 @@ private void loadTransformers(String path, JLabel displayLabel)
620683 displayLabel .setForeground (Color .red );
621684 }
622685 }
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+ }
623710}
0 commit comments