Resource Decompiling Refactor/Code Cleanup

This commit is contained in:
Konloch 2022-02-13 16:02:27 -06:00
parent 7df1312741
commit a39a13e9be

View file

@ -1,15 +1,14 @@
package the.bytecode.club.bytecodeviewer.resources; package the.bytecode.club.bytecodeviewer.resources;
import java.io.File; import java.io.File;
import java.util.Objects;
import javax.swing.JDialog; import javax.swing.JDialog;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import me.konloch.kontainer.io.DiskWriter; import me.konloch.kontainer.io.DiskWriter;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Configuration; import the.bytecode.club.bytecodeviewer.Configuration;
import the.bytecode.club.bytecodeviewer.api.BCV;
import the.bytecode.club.bytecodeviewer.decompilers.Decompiler; import the.bytecode.club.bytecodeviewer.decompilers.Decompiler;
import the.bytecode.club.bytecodeviewer.gui.components.FileChooser; import the.bytecode.club.bytecodeviewer.gui.components.FileChooser;
import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings; import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings;
@ -44,366 +43,242 @@ import static the.bytecode.club.bytecodeviewer.Constants.tempDirectory;
*/ */
public class ResourceDecompiling public class ResourceDecompiling
{ {
private static final int DECOMPILE_SAVE_ALL = 10;
private static final int DECOMPILE_SAVE_ALL_PROCYON = 11;
private static final int DECOMPILE_SAVE_ALL_CFR = 12;
private static final int DECOMPILE_SAVE_ALL_FERNFLOWER = 13;
private static final int DECOMPILE_SAVE_ALL_KRAKATAU = 14;
//TODO JDGUI,JADX
private static final int DECOMPILE_OPENED_ONLY_ALL = 20;
private static final int DECOMPILE_OPENED_ONLY_PROCYON = 21;
private static final int DECOMPILE_OPENED_ONLY_CFR = 22;
private static final int DECOMPILE_OPENED_ONLY_FERNFLOWER = 23;
private static final int DECOMPILE_OPENED_ONLY_KRAKATAU = 24;
//TODO JDGUI,JADX
public static void decompileSaveAll() public static void decompileSaveAll()
{ {
//alert the user if no classes have been imported into BCV
if (BytecodeViewer.promptIfNoLoadedClasses()) if (BytecodeViewer.promptIfNoLoadedClasses())
return; return;
Thread decompileThread = new Thread(() -> MiscUtils.createNewThread("Decompile Save-All Thread", () ->
{ {
//signal to the user that BCV is performing an action in the background
BytecodeViewer.updateBusyStatus(true);
//auto compile before decompilation
if (!BytecodeViewer.autoCompileSuccessful()) if (!BytecodeViewer.autoCompileSuccessful())
return; return;
JFileChooser fc = new FileChooser(Configuration.getLastSaveDirectory(), final JFileChooser fc = new FileChooser(Configuration.getLastSaveDirectory(), "Select Zip Export",
"Select Zip Export", "Zip Archives", "zip");
"Zip Archives",
"zip");
int returnVal = fc.showSaveDialog(BytecodeViewer.viewer); //if the user doesn't select a file then we should stop while we're ahead
if (returnVal == JFileChooser.APPROVE_OPTION) if (fc.showSaveDialog(BytecodeViewer.viewer) != JFileChooser.APPROVE_OPTION)
return;
//set the last touched save directory for BCV
Configuration.setLastSaveDirectory(fc.getSelectedFile());
//get the save file and auto append zip extension
final File outputZip = MiscUtils.autoAppendFileExtension(".zip", fc.getSelectedFile());
//prompt the user for a dialogue override-this-file option if the file already exists
if (!DialogUtils.canOverwriteFile(outputZip))
return;
//this temporary jar file will be used to store the classes while BCV performs decompilation
File temporaryTargetJar = MiscUtils.deleteExistingFile(new File(tempDirectory + fs + "temp_" + MiscUtils.getRandomizedName() + ".jar"));
//extract all the loaded classes imported into BCV to the temporary target jar
JarUtils.saveAsJarClassesOnly(BytecodeViewer.getLoadedClasses(), temporaryTargetJar.getAbsolutePath());
//signal to the user that BCV is finished performing that action
BytecodeViewer.updateBusyStatus(false);
try
{ {
Configuration.setLastSaveDirectory(fc.getSelectedFile()); //handle the result of the user selection
switch (promptDecompilerUserSelect() + DECOMPILE_SAVE_ALL)
File file = fc.getSelectedFile(); {
case DECOMPILE_SAVE_ALL:
//auto appened zip //decompile using procyon
if (!file.getAbsolutePath().endsWith(".zip")) decompileSaveAll(Decompiler.PROCYON_DECOMPILER, temporaryTargetJar, outputZip, true);
file = new File(file.getAbsolutePath() + ".zip");
//decompile using CFR
if (!DialogUtils.canOverwriteFile(file)) decompileSaveAll(Decompiler.CFR_DECOMPILER, temporaryTargetJar, outputZip, true);
return;
//decompile using fern
final File javaSucks = file; decompileSaveAll(Decompiler.FERNFLOWER_DECOMPILER, temporaryTargetJar, outputZip, true);
final String path = MiscUtils.append(file, ".zip"); // cheap hax cause string is final
//decompile using krakatau
JOptionPane pane = new JOptionPane("What decompiler will you use?"); decompileSaveAll(Decompiler.KRAKATAU_DECOMPILER, temporaryTargetJar, outputZip, true);
Object[] options = new String[]{"All", "Procyon", "CFR", break;
"Fernflower", "Krakatau", "Cancel"};
pane.setOptions(options); case DECOMPILE_SAVE_ALL_PROCYON:
JDialog dialog = pane.createDialog(BytecodeViewer.viewer, //decompile using procyon
"Bytecode Viewer - Select Decompiler"); decompileSaveAll(Decompiler.PROCYON_DECOMPILER, temporaryTargetJar, outputZip, false);
dialog.setVisible(true); break;
Object obj = pane.getValue();
int result = -1; case DECOMPILE_SAVE_ALL_CFR:
for (int k = 0; k < options.length; k++) //decompile using CFR
if (options[k].equals(obj)) decompileSaveAll(Decompiler.CFR_DECOMPILER, temporaryTargetJar, outputZip, false);
result = k; break;
BytecodeViewer.updateBusyStatus(true); case DECOMPILE_SAVE_ALL_FERNFLOWER:
//decompile using fern
File tempZip = new File(tempDirectory + fs + "temp_" + MiscUtils.getRandomizedName() + ".jar"); decompileSaveAll(Decompiler.FERNFLOWER_DECOMPILER, temporaryTargetJar, outputZip, false);
if (tempZip.exists()) break;
tempZip.delete();
case DECOMPILE_SAVE_ALL_KRAKATAU:
JarUtils.saveAsJarClassesOnly(BytecodeViewer.getLoadedClasses(), tempZip.getAbsolutePath()); //decompile using krakatau
decompileSaveAll(Decompiler.KRAKATAU_DECOMPILER, temporaryTargetJar, outputZip, false);
if (result == 0) { break;
Thread t12 = new Thread(() -> {
try {
Decompiler.PROCYON_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(),
MiscUtils.append(javaSucks, "-procyon.zip"));
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
BytecodeViewer.handleException(e);
}
});
t12.start();
Thread t2 = new Thread(() -> {
try {
BytecodeViewer.updateBusyStatus(true);
Decompiler.CFR_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(),
MiscUtils.append(javaSucks, "-CFR.zip"));
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
BytecodeViewer.handleException(e);
}
});
t2.start();
Thread t3 = new Thread(() -> {
try {
BytecodeViewer.updateBusyStatus(true);
Decompiler.FERNFLOWER_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(),
MiscUtils.append(javaSucks, "-fernflower.zip"));
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
BytecodeViewer.handleException(e);
}
});
t3.start();
Thread t4 = new Thread(() -> {
try {
BytecodeViewer.updateBusyStatus(true);
Decompiler.KRAKATAU_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(),
MiscUtils.append(javaSucks, "-kraktau.zip"));
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
BytecodeViewer.handleException(e);
}
});
t4.start();
}
if (result == 1) {
Thread t12 = new Thread(() -> {
try {
Decompiler.PROCYON_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(), path);
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
BytecodeViewer.handleException(e);
}
});
t12.start();
}
if (result == 2) {
Thread t12 = new Thread(() -> {
try {
Decompiler.CFR_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(), path);
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
BytecodeViewer.handleException(e);
}
});
t12.start();
}
if (result == 3) {
Thread t12 = new Thread(() -> {
try {
Decompiler.FERNFLOWER_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(), path);
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
BytecodeViewer.handleException(e);
}
});
t12.start();
}
if (result == 4) {
Thread t12 = new Thread(() -> {
try {
Decompiler.KRAKATAU_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(), path);
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
BytecodeViewer.handleException(e);
}
});
t12.start();
}
if (result == 5) {
BytecodeViewer.updateBusyStatus(false);
} }
} }
}, "Decompile Thread"); catch (Exception e)
decompileThread.start(); {
BytecodeViewer.handleException(e);
}
});
} }
public static void decompileSaveOpenedOnly() public static void decompileSaveOpenedResource()
{ {
//alert the user if no classes have been imported into BCV
if (BytecodeViewer.promptIfNoLoadedClasses()) if (BytecodeViewer.promptIfNoLoadedClasses())
return; return;
//verify the active resource is a valid class file
if (!BytecodeViewer.isActiveResourceClass()) if (!BytecodeViewer.isActiveResourceClass())
{ {
BytecodeViewer.showMessage(TranslatedStrings.FIRST_VIEW_A_CLASS.toString()); BytecodeViewer.showMessage(TranslatedStrings.FIRST_VIEW_A_CLASS.toString());
return; return;
} }
Thread decompileThread = new Thread(() -> MiscUtils.createNewThread("Decompile Save Opened Resource", () ->
{ {
//signal to the user that BCV is performing an action in the background
BytecodeViewer.updateBusyStatus(true);
//auto compile before decompilation
if (!BytecodeViewer.autoCompileSuccessful()) if (!BytecodeViewer.autoCompileSuccessful())
return; return;
final ClassNode cn = BytecodeViewer.getCurrentlyOpenedClassNode(); JFileChooser fc = new FileChooser(Configuration.getLastSaveDirectory(), "Select Java Files",
"Java Source Files", "java");
JFileChooser fc = new FileChooser(Configuration.getLastSaveDirectory(), //if the user doesn't select a file then we should stop while we're ahead
"Select Java Files", if(fc.showSaveDialog(BytecodeViewer.viewer) != JFileChooser.APPROVE_OPTION)
"Java Source Files", return;
"java");
int returnVal = fc.showSaveDialog(BytecodeViewer.viewer); //set the last touched save directory for BCV
if (returnVal == JFileChooser.APPROVE_OPTION) Configuration.setLastSaveDirectory(fc.getSelectedFile());
//get the save file and auto append java extension
File file = MiscUtils.autoAppendFileExtension(".java", fc.getSelectedFile());
//prompt the user for a dialogue override-this-file option if the file already exists
if (!DialogUtils.canOverwriteFile(file))
return;
//signal to the user that BCV is finished performing that action
BytecodeViewer.updateBusyStatus(false);
try
{ {
Configuration.setLastSaveDirectory(fc.getSelectedFile()); //handle the result of the user selection
switch(promptDecompilerUserSelect() + DECOMPILE_OPENED_ONLY_ALL)
File file = fc.getSelectedFile(); {
case DECOMPILE_OPENED_ONLY_ALL:
BytecodeViewer.updateBusyStatus(true); //decompile using procyon
final String path = MiscUtils.append(file, ".java"); decompileCurrentlyOpenedResource(Decompiler.PROCYON_DECOMPILER, file, true);
if (!DialogUtils.canOverwriteFile(path)) //decompile using cfr
return; decompileCurrentlyOpenedResource(Decompiler.CFR_DECOMPILER, file, true);
JOptionPane pane = new JOptionPane( //decompile using fernflower
"What decompiler will you use?"); decompileCurrentlyOpenedResource(Decompiler.FERNFLOWER_DECOMPILER, file, true);
Object[] options = new String[]{"All", "Procyon", "CFR",
"Fernflower", "Krakatau", "Cancel"}; //decompile using krakatau
pane.setOptions(options); decompileCurrentlyOpenedResource(Decompiler.KRAKATAU_DECOMPILER, file, true);
JDialog dialog = pane.createDialog(BytecodeViewer.viewer, break;
"Bytecode Viewer - Select Decompiler");
dialog.setVisible(true); case DECOMPILE_OPENED_ONLY_PROCYON:
Object obj = pane.getValue(); //decompile using procyon
int result = -1; decompileCurrentlyOpenedResource(Decompiler.PROCYON_DECOMPILER, file, false);
for (int k = 0; k < options.length; k++) break;
if (options[k].equals(obj))
result = k; case DECOMPILE_OPENED_ONLY_CFR:
//decompile using cfr
if (result == 0) { decompileCurrentlyOpenedResource(Decompiler.CFR_DECOMPILER, file, false);
Thread t1 = new Thread(() -> { break;
try {
final ClassWriter cw = new ClassWriter(0); case DECOMPILE_OPENED_ONLY_FERNFLOWER:
try { //decompile using fernflower
Objects.requireNonNull(cn).accept(cw); decompileCurrentlyOpenedResource(Decompiler.FERNFLOWER_DECOMPILER, file, false);
} catch (Exception e) { break;
e.printStackTrace();
try { case DECOMPILE_OPENED_ONLY_KRAKATAU:
Thread.sleep(200); //decompile using krakatau
Objects.requireNonNull(cn).accept(cw); decompileCurrentlyOpenedResource(Decompiler.KRAKATAU_DECOMPILER, file, false);
} catch (InterruptedException ignored) { break;
}
}
try {
DiskWriter.replaceFile(MiscUtils.append(file, "-procyon.java"),
Decompiler.PROCYON_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray()), false);
} catch (Exception e) {
e.printStackTrace();
}
try {
DiskWriter.replaceFile(MiscUtils.append(file, "-CFR.java"),
Decompiler.CFR_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray()), false);
} catch (Exception e) {
e.printStackTrace();
}
try {
DiskWriter.replaceFile(MiscUtils.append(file, "-fernflower.java"),
Decompiler.FERNFLOWER_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray()), false);
} catch (Exception e) {
e.printStackTrace();
}
try {
DiskWriter.replaceFile(MiscUtils.append(file, "-kraktau.java"),
Decompiler.KRAKATAU_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray()), false);
} catch (Exception e) {
e.printStackTrace();
}
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
BytecodeViewer.updateBusyStatus(false);
BytecodeViewer.handleException(e);
}
});
t1.start();
}
if (result == 1) {
Thread t1 = new Thread(() -> {
try {
final ClassWriter cw = new ClassWriter(0);
try {
Objects.requireNonNull(cn).accept(cw);
} catch (Exception e) {
e.printStackTrace();
try {
Thread.sleep(200);
Objects.requireNonNull(cn).accept(cw);
} catch (InterruptedException ignored) {
}
}
String contents = Decompiler.PROCYON_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray());
DiskWriter.replaceFile(path, contents, false);
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
BytecodeViewer.updateBusyStatus(false);
BytecodeViewer.handleException(
e);
}
});
t1.start();
}
if (result == 2) {
Thread t1 = new Thread(() -> {
try {
final ClassWriter cw = new ClassWriter(0);
try {
Objects.requireNonNull(cn).accept(cw);
} catch (Exception e) {
e.printStackTrace();
try {
Thread.sleep(200);
Objects.requireNonNull(cn).accept(cw);
} catch (InterruptedException ignored) {
}
}
String contents = Decompiler.CFR_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray());
DiskWriter.replaceFile(path, contents, false);
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
BytecodeViewer.updateBusyStatus(false);
BytecodeViewer.handleException(
e);
}
});
t1.start();
}
if (result == 3) {
Thread t1 = new Thread(() -> {
try {
final ClassWriter cw = new ClassWriter(0);
try {
Objects.requireNonNull(cn).accept(cw);
} catch (Exception e) {
e.printStackTrace();
try {
Thread.sleep(200);
if (cn != null)
cn.accept(cw);
} catch (InterruptedException ignored) {
}
}
String contents = Decompiler.FERNFLOWER_DECOMPILER.getDecompiler().decompileClassNode(cn,
cw.toByteArray());
DiskWriter.replaceFile(path, contents, false);
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
BytecodeViewer.updateBusyStatus(false);
BytecodeViewer.handleException(
e);
}
});
t1.start();
}
if (result == 4) {
Thread t1 = new Thread(() -> {
try {
final ClassWriter cw = new ClassWriter(0);
try {
Objects.requireNonNull(cn).accept(cw);
} catch (Exception e) {
e.printStackTrace();
try {
Thread.sleep(200);
Objects.requireNonNull(cn).accept(cw);
} catch (InterruptedException ignored) { }
}
String contents = Decompiler.KRAKATAU_DECOMPILER.getDecompiler().
decompileClassNode(cn, cw.toByteArray());
DiskWriter.replaceFile(path, contents, false);
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
BytecodeViewer.updateBusyStatus(false);
BytecodeViewer.handleException(e);
}
});
t1.start();
}
if (result == 5) {
BytecodeViewer.updateBusyStatus(false);
} }
} }
}, "Decompile Thread"); catch (Exception e)
decompileThread.start(); {
BytecodeViewer.handleException(e);
}
});
} }
}
public static int promptDecompilerUserSelect()
{
final JOptionPane pane = new JOptionPane("Which decompiler would you like to use?");
final Object[] options = new String[]{ "All", "Procyon", "CFR",
"Fernflower", "Krakatau", "Cancel"}; //TODO JDGUI,JADX
pane.setOptions(options);
final JDialog dialog = pane.createDialog(BytecodeViewer.viewer, "Bytecode Viewer - Select Decompiler");
dialog.setVisible(true);
final Object obj = pane.getValue();
int result = -1;
for (int k = 0; k < options.length; k++)
if (options[k].equals(obj))
result = k;
return result;
}
public static void decompileSaveAll(Decompiler decompiler, File targetJar, File outputZip, boolean saveAll)
{
//signal to the user that BCV is performing an action in the background
BytecodeViewer.updateBusyStatus(true);
//decompile all opened classes to zip
decompiler.getDecompiler().decompileToZip(targetJar.getAbsolutePath(), saveAll
? MiscUtils.append(outputZip, "-" + decompiler.getDecompilerNameProgrammic() + ".zip")
: outputZip.getAbsolutePath());
//signal to the user that BCV is finished performing that action
BytecodeViewer.updateBusyStatus(false);
}
public static void decompileCurrentlyOpenedResource(Decompiler decompiler, File outputFile, boolean saveAll)
{
//signal to the user that BCV is performing an action in the background
BytecodeViewer.updateBusyStatus(true);
//decompile the currently opened resource and save it to the specified file
DiskWriter.replaceFile(saveAll
? MiscUtils.append(outputFile, "-" + decompiler.getDecompilerNameProgrammic() + ".java")
: outputFile.getAbsolutePath(),
BCV.decompileCurrentlyOpenedClassNode(decompiler), false);
//signal to the user that BCV is finished performing that action
BytecodeViewer.updateBusyStatus(false);
}
}