bcv-vf/src/main/java/the/bytecode/club/bytecodeviewer/resources/ResourceDecompiling.java

406 lines
12 KiB
Java
Raw Normal View History

2021-06-26 15:10:02 +00:00
package the.bytecode.club.bytecodeviewer.resources;
import me.konloch.kontainer.io.DiskWriter;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
2021-06-26 15:03:11 +00:00
import the.bytecode.club.bytecodeviewer.Configuration;
import the.bytecode.club.bytecodeviewer.api.ExceptionUI;
import the.bytecode.club.bytecodeviewer.decompilers.Decompiler;
2021-06-26 15:03:11 +00:00
import the.bytecode.club.bytecodeviewer.gui.components.FileChooser;
2021-07-06 22:57:42 +00:00
import the.bytecode.club.bytecodeviewer.gui.resourceviewer.viewer.ClassViewer;
import the.bytecode.club.bytecodeviewer.util.DialogueUtils;
2021-06-26 15:03:11 +00:00
import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
import javax.swing.*;
import java.io.File;
import java.util.Objects;
import static the.bytecode.club.bytecodeviewer.Constants.fs;
import static the.bytecode.club.bytecodeviewer.Constants.tempDirectory;
/**
* @author Konloch
* @since 6/21/2021
*/
public class ResourceDecompiling
{
public static void decompileSaveAll()
{
2021-06-26 15:03:11 +00:00
if (BytecodeViewer.getLoadedClasses().isEmpty())
{
BytecodeViewer.showMessage("First open a class, jar, zip, apk or dex file.");
return;
}
2021-06-27 21:15:39 +00:00
Thread decompileThread = new Thread(() ->
2021-06-26 15:03:11 +00:00
{
2021-07-03 19:29:31 +00:00
if (BytecodeViewer.autoCompileSuccessful())
return;
2021-06-26 15:03:11 +00:00
JFileChooser fc = new FileChooser(Configuration.getLastDirectory(),
2021-06-26 15:03:11 +00:00
"Select Zip Export",
"Zip Archives",
"zip");
int returnVal = fc.showSaveDialog(BytecodeViewer.viewer);
2021-06-26 15:03:11 +00:00
if (returnVal == JFileChooser.APPROVE_OPTION)
{
2021-06-26 15:56:39 +00:00
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath();
File file = fc.getSelectedFile();
2021-06-26 15:03:11 +00:00
//auto appened zip
if (!file.getAbsolutePath().endsWith(".zip"))
file = new File(file.getAbsolutePath() + ".zip");
if (!DialogueUtils.canOverwriteFile(file))
return;
final File javaSucks = file;
final String path = MiscUtils.append(file, ".zip"); // cheap hax cause string is final
2021-06-26 15:03:11 +00:00
JOptionPane pane = new JOptionPane("What decompiler will you use?");
Object[] options = new String[]{"All", "Procyon", "CFR",
"Fernflower", "Krakatau", "Cancel"};
pane.setOptions(options);
JDialog dialog = pane.createDialog(BytecodeViewer.viewer,
"Bytecode Viewer - Select Decompiler");
dialog.setVisible(true);
Object obj = pane.getValue();
int result = -1;
for (int k = 0; k < options.length; k++)
if (options[k].equals(obj))
result = k;
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(true);
File tempZip = new File(tempDirectory + fs + "temp_" + MiscUtils.getRandomizedName() + ".jar");
if (tempZip.exists())
tempZip.delete();
JarUtils.saveAsJarClassesOnly(BytecodeViewer.getLoadedClasses(), tempZip.getAbsolutePath());
if (result == 0) {
Thread t12 = new Thread(() -> {
try {
2021-07-04 05:59:42 +00:00
Decompiler.PROCYON_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(),
MiscUtils.append(javaSucks, "-proycon.zip"));
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
new ExceptionUI(e);
}
});
t12.start();
Thread t2 = new Thread(() -> {
try {
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(true);
2021-07-04 05:59:42 +00:00
Decompiler.CFR_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(),
MiscUtils.append(javaSucks, "-CFR.zip"));
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
new ExceptionUI(e);
}
});
t2.start();
Thread t3 = new Thread(() -> {
try {
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(true);
2021-07-04 05:59:42 +00:00
Decompiler.FERNFLOWER_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(),
MiscUtils.append(javaSucks, "-fernflower.zip"));
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
new ExceptionUI(e);
}
});
t3.start();
Thread t4 = new Thread(() -> {
try {
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(true);
2021-07-04 05:59:42 +00:00
Decompiler.KRAKATAU_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(),
MiscUtils.append(javaSucks, "-kraktau.zip"));
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
new ExceptionUI(e);
}
});
t4.start();
}
if (result == 1) {
Thread t12 = new Thread(() -> {
try {
2021-07-04 05:59:42 +00:00
Decompiler.PROCYON_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(), path);
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
new ExceptionUI(e);
}
});
t12.start();
}
if (result == 2) {
Thread t12 = new Thread(() -> {
try {
2021-07-04 05:59:42 +00:00
Decompiler.CFR_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(), path);
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
new ExceptionUI(e);
}
});
t12.start();
}
if (result == 3) {
Thread t12 = new Thread(() -> {
try {
2021-07-04 05:59:42 +00:00
Decompiler.FERNFLOWER_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(), path);
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
new ExceptionUI(e);
}
});
t12.start();
}
if (result == 4) {
Thread t12 = new Thread(() -> {
try {
2021-07-04 05:59:42 +00:00
Decompiler.KRAKATAU_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(), path);
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
new ExceptionUI(e);
}
});
t12.start();
}
if (result == 5) {
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
}
}
2021-07-01 09:48:10 +00:00
}, "Decompile Thread");
2021-06-27 21:15:39 +00:00
decompileThread.start();
}
public static void decompileSaveOpenedOnly()
{
2021-07-06 22:57:42 +00:00
if (BytecodeViewer.getLoadedClasses().isEmpty())
{
BytecodeViewer.showMessage("First open a class, jar, zip, apk or dex file.");
return;
}
2021-07-06 22:57:42 +00:00
if (BytecodeViewer.viewer.workPane.getCurrentViewer() == null || !(BytecodeViewer.viewer.workPane.getCurrentViewer() instanceof ClassViewer))
{
BytecodeViewer.showMessage("First open a class file.");
return;
}
Thread decompileThread = new Thread(() ->
{
2021-07-03 19:29:31 +00:00
if (BytecodeViewer.autoCompileSuccessful())
return;
final String s = BytecodeViewer.viewer.workPane.getCurrentViewer().cn.name;
if (s == null)
return;
JFileChooser fc = new FileChooser(Configuration.getLastDirectory(),
2021-06-26 15:03:11 +00:00
"Select Java Files",
"Java Source Files",
"java");
int returnVal = fc.showSaveDialog(BytecodeViewer.viewer);
2021-06-26 15:03:11 +00:00
if (returnVal == JFileChooser.APPROVE_OPTION)
{
2021-06-26 15:56:39 +00:00
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath();
File file = fc.getSelectedFile();
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(true);
final String path = MiscUtils.append(file, ".java");
if (!DialogueUtils.canOverwriteFile(path))
return;
JOptionPane pane = new JOptionPane(
"What decompiler will you use?");
Object[] options = new String[]{"All", "Procyon", "CFR",
"Fernflower", "Krakatau", "Cancel"};
pane.setOptions(options);
JDialog dialog = pane.createDialog(BytecodeViewer.viewer,
"Bytecode Viewer - Select Decompiler");
dialog.setVisible(true);
Object obj = pane.getValue();
int result = -1;
for (int k = 0; k < options.length; k++)
if (options[k].equals(obj))
result = k;
if (result == 0) {
Thread t1 = new Thread(() -> {
try {
ClassNode cn = BytecodeViewer.getClassNode(s);
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) {
}
}
try {
DiskWriter.replaceFile(MiscUtils.append(file, "-proycon.java"),
2021-07-04 05:59:42 +00:00
Decompiler.PROCYON_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray()), false);
} catch (Exception e) {
e.printStackTrace();
}
try {
DiskWriter.replaceFile(MiscUtils.append(file, "-CFR.java"),
2021-07-04 05:59:42 +00:00
Decompiler.CFR_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray()), false);
} catch (Exception e) {
e.printStackTrace();
}
try {
DiskWriter.replaceFile(MiscUtils.append(file, "-fernflower.java"),
2021-07-04 05:59:42 +00:00
Decompiler.FERNFLOWER_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray()), false);
} catch (Exception e) {
e.printStackTrace();
}
try {
DiskWriter.replaceFile(MiscUtils.append(file, "-kraktau.java"),
2021-07-04 05:59:42 +00:00
Decompiler.KRAKATAU_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray()), false);
} catch (Exception e) {
e.printStackTrace();
}
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
new ExceptionUI(e);
}
});
t1.start();
}
if (result == 1) {
Thread t1 = new Thread(() -> {
try {
ClassNode cn = BytecodeViewer.getClassNode(s);
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) {
}
}
2021-07-04 05:59:42 +00:00
String contents = Decompiler.PROCYON_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray());
DiskWriter.replaceFile(path, contents, false);
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
new ExceptionUI(
e);
}
});
t1.start();
}
if (result == 2) {
Thread t1 = new Thread(() -> {
try {
ClassNode cn = BytecodeViewer.getClassNode(s);
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) {
}
}
2021-07-04 05:59:42 +00:00
String contents = Decompiler.CFR_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray());
DiskWriter.replaceFile(path, contents, false);
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
new ExceptionUI(
e);
}
});
t1.start();
}
if (result == 3) {
Thread t1 = new Thread(() -> {
try {
ClassNode cn = BytecodeViewer.getClassNode(s);
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) {
}
}
2021-07-04 05:59:42 +00:00
String contents = Decompiler.FERNFLOWER_DECOMPILER.getDecompiler().decompileClassNode(cn,
cw.toByteArray());
DiskWriter.replaceFile(path, contents, false);
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
new ExceptionUI(
e);
}
});
t1.start();
}
if (result == 4) {
Thread t1 = new Thread(() -> {
try {
ClassNode cn = BytecodeViewer.getClassNode(s);
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) {
}
}
2021-07-04 05:59:42 +00:00
String contents = Decompiler.KRAKATAU_DECOMPILER.getDecompiler().decompileClassNode(cn,
cw.toByteArray());
DiskWriter.replaceFile(path, contents, false);
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
} catch (Exception e) {
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
new ExceptionUI(
e);
}
});
t1.start();
}
if (result == 5) {
2021-07-06 22:57:42 +00:00
BytecodeViewer.updateBusyStatus(false);
}
}
2021-07-01 09:48:10 +00:00
}, "Decompile Thread");
2021-06-27 21:15:39 +00:00
decompileThread.start();
}
}