2828import the .bytecode .club .bytecodeviewer .decompilers .AbstractDecompiler ;
2929import the .bytecode .club .bytecodeviewer .resources .ExternalResources ;
3030import the .bytecode .club .bytecodeviewer .translation .TranslatedStrings ;
31- import the .bytecode .club .bytecodeviewer .util .JarUtils ;
32- import the .bytecode .club .bytecodeviewer .util .MiscUtils ;
33- import the .bytecode .club .bytecodeviewer .util .ZipUtils ;
31+ import the .bytecode .club .bytecodeviewer .util .*;
3432
3533import java .io .*;
3634import java .util .Arrays ;
3735import java .util .stream .Collectors ;
3836
3937import static the .bytecode .club .bytecodeviewer .Constants .*;
38+ import static the .bytecode .club .bytecodeviewer .translation .TranslatedStrings .*;
4039
4140/**
4241 * Krakatau Java Decompiler Wrapper, requires Python 2.7
@@ -51,24 +50,6 @@ public KrakatauDecompiler()
5150 super ("Krakatau Decompiler" , "krakatau" );
5251 }
5352
54- public String buildCLIArguments ()
55- {
56- if (Configuration .library .isEmpty ())
57- return "" ;
58-
59- File dir = new File (Configuration .library );
60- if (!dir .exists ())
61- return "" ;
62- if (!dir .isDirectory ())
63- return ";" + Configuration .library ;
64-
65- File [] files = dir .listFiles ();
66- if (files == null || files .length == 0 )
67- return "" ;
68-
69- return ";" + Arrays .stream (files ).filter (File ::isFile ).map (File ::getAbsolutePath ).collect (Collectors .joining (";" ));
70- }
71-
7253 @ Override
7354 public String decompileClassNode (ClassNode cn , byte [] bytes )
7455 {
@@ -79,104 +60,104 @@ public String decompileClassNode(ClassNode cn, byte[] bytes)
7960
8061 if (Configuration .rt .isEmpty ())
8162 {
82- BytecodeViewer .showMessage (TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_A + "\r \n " + TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_B );
63+ BytecodeViewer .showMessage (TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_A
64+ + "\r \n " + TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_B );
8365 ExternalResources .getSingleton ().selectJRERTLibrary ();
8466 }
8567
8668 if (Configuration .rt .isEmpty ())
8769 {
88- BytecodeViewer .showMessage (TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_A + "\r \n " + TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_B );
89- return TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_A + " " + TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_B ;
70+ BytecodeViewer .showMessage (TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_A
71+ + "\r \n " + TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_B );
72+
73+ return TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_A
74+ + " " + TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_B ;
9075 }
9176
92- final File tempDirectory = new File (Constants .TEMP_DIRECTORY + FS + MiscUtils .randomString (32 ) + FS );
93- final File tempJar = new File (Constants .TEMP_DIRECTORY + FS + "temp" + MiscUtils .randomString (32 ) + ".jar" );
77+ StringBuilder processOut = new StringBuilder (NL + NL );
78+ StringBuilder processErr = new StringBuilder (NL + NL );
79+ int exitCode = Integer .MAX_VALUE ;
80+ TempFile tempFile = null ;
81+ String exception ;
9482
95- tempDirectory .mkdir ();
83+ try
84+ {
85+ //create the temporary files
86+ tempFile = TempFile .createTemporaryFile (false , ".jar" );
87+ tempFile .newTemporaryParent ();
88+ File tempInputJarFile = tempFile .getFile ();
89+ File tempDir = tempFile .createFileFromExtension (true , false , ".txt" ).getParentFile ();
90+ File tempOutputJavaFile = new File (tempDir .getAbsolutePath () + FS + cn .name + ".java" );
9691
97- JarUtils .saveAsJarClassesOnly (BytecodeViewer .getLoadedClasses (), tempJar .getAbsolutePath ());
92+ //create out dir
93+ tempDir .mkdirs ();
94+ tempOutputJavaFile .getParentFile ().mkdirs ();
9895
99- return decompileClassNode ( tempJar , tempDirectory , cn );
100- }
96+ //final File tempDirectory = new File(Constants.TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + FS );
97+ //javaFile = new File(Constants.TEMP_DIRECTORY + FS + "temp" + MiscUtils.randomString(32) + ".jar");
10198
102- public String decompileClassNode (File tempJar , File tempDir , ClassNode cn )
103- {
104- if (!ExternalResources .getSingleton ().hasSetPython2Command ())
105- return TranslatedStrings .YOU_NEED_TO_SET_YOUR_PYTHON_2_PATH .toString ();
99+ JarUtils .saveAsJarClassesOnly (BytecodeViewer .getLoadedClasses (), tempInputJarFile .getAbsolutePath ());
106100
107- ExternalResources .getSingleton ().rtCheck ();
101+ if (!ExternalResources .getSingleton ().hasSetPython2Command ())
102+ return TranslatedStrings .YOU_NEED_TO_SET_YOUR_PYTHON_2_PATH .toString ();
108103
109- if (Configuration .rt .isEmpty ())
110- {
111- BytecodeViewer .showMessage (TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_A + "\r \n " + TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_B );
112- ExternalResources .getSingleton ().selectJRERTLibrary ();
113- }
104+ ExternalResources .getSingleton ().rtCheck ();
114105
115- if (Configuration .rt .isEmpty ())
116- {
117- BytecodeViewer .showMessage (TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_A + "\r \n " + TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_B );
118- return TranslatedStrings . YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_A + " " + TranslatedStrings . YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_B ;
119- }
106+ if (Configuration .rt .isEmpty ())
107+ {
108+ BytecodeViewer .showMessage (TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_A + "\r \n " + TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_B );
109+ ExternalResources . getSingleton (). selectJRERTLibrary () ;
110+ }
120111
121- String returnString = ExceptionUI .SEND_STACKTRACE_TO_NL ;
112+ if (Configuration .rt .isEmpty ())
113+ {
114+ BytecodeViewer .showMessage (TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_A + "\r \n " + TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_B );
115+ return TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_A + " " + TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_B ;
116+ }
122117
123- try
124- {
125118 String [] pythonCommands = new String []{Configuration .python2 };
126119 if (Configuration .python2Extra )
127120 pythonCommands = ArrayUtils .addAll (pythonCommands , "-2" );
128121
129122 ProcessBuilder pb = new ProcessBuilder (ArrayUtils .addAll (pythonCommands , "-O" , //love you storyyeller <3
130- krakatauWorkingDirectory + FS + "decompile.py" , "-skip" , //love you storyyeller <3
131- "-nauto" , "-path" , Configuration .rt + ";" + tempJar .getAbsolutePath () + buildCLIArguments (),
132- "-out" , tempDir .getAbsolutePath (), cn .name + ".class" ));
123+ krakatauWorkingDirectory + FS + "decompile.py" ,
124+ "-skip" , //love you storyyeller <3
125+ "-nauto" ,
126+ "-path" , Configuration .rt + ";" + tempInputJarFile .getAbsolutePath () + buildCLIArguments (),
127+ "-out" , tempDir .getAbsolutePath (),
128+ cn .name + ".class" ));
133129
134130 Process process = pb .start ();
135131 BytecodeViewer .createdProcesses .add (process );
136132
137- StringBuilder log = new StringBuilder (TranslatedStrings .PROCESS2 + NL + NL );
138-
139133 //Read out dir output
140- try (InputStream is = process .getInputStream ();
141- InputStreamReader isr = new InputStreamReader (is );
142- BufferedReader br = new BufferedReader (isr ))
143- {
144- String line ;
145- while ((line = br .readLine ()) != null )
146- {
147- log .append (NL ).append (line );
148- }
149- }
134+ //ProcessUtils.readProcessToStringBuilderAsync(process, processOut, processErr);
135+ ProcessUtils .readProcessToStringBuilder (process , processOut , processErr );
150136
151- log .append (NL ).append (NL ).append (TranslatedStrings .ERROR2 ).append (NL ).append (NL );
137+ //wait for process to exit
138+ exitCode = process .waitFor ();
152139
153- try (InputStream is = process .getErrorStream ();
154- InputStreamReader isr = new InputStreamReader (is );
155- BufferedReader br = new BufferedReader (isr ))
156- {
157- String line ;
158- while ((line = br .readLine ()) != null )
159- {
160- log .append (NL ).append (line );
161- }
162- }
140+ //handle simulated errors
141+ if (Constants .DEV_FLAG_DECOMPILERS_SIMULATED_ERRORS )
142+ throw new RuntimeException (DEV_MODE_SIMULATED_ERROR .toString ());
163143
164- int exitValue = process .waitFor ();
165- log .append (NL ).append (NL ).append (TranslatedStrings .EXIT_VALUE_IS ).append (" " ).append (exitValue );
166- returnString = log .toString ();
167-
168- // update the string on a successful disassemble
169- returnString = DiskReader .loadAsString (tempDir .getAbsolutePath () + FS + cn .name + ".java" );
144+ // read the java file on a successful disassemble
145+ return DiskReader .loadAsString (tempOutputJavaFile .getAbsolutePath ());
170146 }
171- catch (Exception e )
147+ catch (Throwable e )
172148 {
173- StringWriter sw = new StringWriter ();
174- e .printStackTrace (new PrintWriter (sw ));
175- e .printStackTrace ();
176- returnString += NL + ExceptionUI .SEND_STACKTRACE_TO_NL + sw ;
149+ exception = ProcessUtils .mergeLogs (processOut , processErr , exitCode )
150+ + ExceptionUtils .exceptionToString (e );
151+ }
152+ finally
153+ {
154+ //delete all temporary files
155+ if (tempFile != null )
156+ tempFile .cleanup ();
177157 }
178158
179- return returnString ;
159+ return KRAKATAU + " " + ERROR + "! " + ExceptionUI .SEND_STACKTRACE_TO + NL + NL
160+ + TranslatedStrings .SUGGESTED_FIX_DECOMPILER_ERROR + NL + NL + exception ;
180161 }
181162
182163 @ Override
@@ -189,7 +170,8 @@ public void decompileToZip(String sourceJar, String zipName)
189170
190171 if (Configuration .rt .isEmpty ())
191172 {
192- BytecodeViewer .showMessage (TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_A + "\r \n " + TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_B );
173+ BytecodeViewer .showMessage (TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_A + "\r \n "
174+ + TranslatedStrings .YOU_NEED_TO_SET_YOUR_JAVA_RT_PATH_B );
193175 ExternalResources .getSingleton ().selectJRERTLibrary ();
194176 }
195177
@@ -207,7 +189,8 @@ public void decompileToZip(String sourceJar, String zipName)
207189
208190 ProcessBuilder pb = new ProcessBuilder (ArrayUtils .addAll (pythonCommands , "-O" , //love you storyyeller <3
209191 krakatauWorkingDirectory + FS + "decompile.py" , "-skip" , //love you storyyeller <3
210- "-nauto" , "-path" , Configuration .rt + ";" + tempJar .getAbsolutePath (), "-out" , tempDirectory .getAbsolutePath (), tempJar .getAbsolutePath ()));
192+ "-nauto" , "-path" , Configuration .rt + ";" + tempJar .getAbsolutePath (),
193+ "-out" , tempDirectory .getAbsolutePath (), tempJar .getAbsolutePath ()));
211194
212195 Process process = pb .start ();
213196 BytecodeViewer .createdProcesses .add (process );
@@ -221,4 +204,25 @@ public void decompileToZip(String sourceJar, String zipName)
221204 BytecodeViewer .handleException (e );
222205 }
223206 }
207+
208+ public String buildCLIArguments ()
209+ {
210+ if (Configuration .library .isEmpty ())
211+ return "" ;
212+
213+ File dir = new File (Configuration .library );
214+
215+ if (!dir .exists ())
216+ return "" ;
217+
218+ if (!dir .isDirectory ())
219+ return ";" + Configuration .library ;
220+
221+ File [] files = dir .listFiles ();
222+ if (files == null || files .length == 0 )
223+ return "" ;
224+
225+ return ";" + Arrays .stream (files ).filter (File ::isFile )
226+ .map (File ::getAbsolutePath ).collect (Collectors .joining (";" ));
227+ }
224228}
0 commit comments