Skip to content

Commit

Permalink
Merge branch 'Konloch:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Bl3nd authored Sep 27, 2024
2 parents bfdab8a + 9353c96 commit 2b70b56
Show file tree
Hide file tree
Showing 100 changed files with 1,257 additions and 430 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import the.bytecode.club.bytecodeviewer.bootloader.BootState;
import the.bytecode.club.bytecodeviewer.bootloader.InstallFatJar;
import the.bytecode.club.bytecodeviewer.bootloader.UpdateCheck;
import the.bytecode.club.bytecodeviewer.cli.CLIAction;
import the.bytecode.club.bytecodeviewer.cli.CommandLineInput;
import the.bytecode.club.bytecodeviewer.gui.MainViewerGUI;
import the.bytecode.club.bytecodeviewer.gui.components.ExtendedJOptionPane;
import the.bytecode.club.bytecodeviewer.gui.components.MultipleChoiceDialog;
Expand Down Expand Up @@ -202,8 +204,8 @@ public static void main(String[] args)
MiscUtils.setLanguage(MiscUtils.guessLanguage());

//handle CLI
int isCLI = CommandLineInput.parseCommandLine(args);
if (isCLI == CommandLineInput.STOP)
CLIAction isCLI = CommandLineInput.parseCommandLine(args);
if (isCLI == CLIAction.STOP)
return;

//load with shaded libraries
Expand All @@ -214,11 +216,11 @@ public static void main(String[] args)
else //load through bootloader
{
BOOT_CHECK.start();
Boot.boot(args, isCLI != CommandLineInput.GUI);
Boot.boot(args, isCLI != CLIAction.GUI);
}

//CLI arguments say spawn the GUI
if (isCLI == CommandLineInput.GUI)
if (isCLI == CLIAction.GUI)
{
BytecodeViewer.boot(false);
Configuration.bootState = BootState.GUI_SHOWING;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class Constants

public static final String FS = System.getProperty("file.separator");
public static final String NL = System.getProperty("line.separator");
public static final String[] SUPPORTED_FILE_EXTENSIONS = ResourceType.supportedBCVExtensionMap.keySet().toArray(new String[0]);
public static final String[] SUPPORTED_FILE_EXTENSIONS = ResourceType.SUPPORTED_BCV_EXTENSION_MAP.keySet().toArray(new String[0]);
public static final int ASM_VERSION = Opcodes.ASM9;

public static final File BCVDir = resolveBCVRoot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ else if ((e.getKeyCode() == KeyEvent.VK_S) && ((e.getModifiersEx() & KeyEvent.CT
JFileChooser fc = new FileChooser(Configuration.getLastSaveDirectory(), "Select Zip Export", "Zip Archives", "zip");

int returnVal = fc.showSaveDialog(BytecodeViewer.viewer);

if (returnVal == JFileChooser.APPROVE_OPTION)
{
Configuration.setLastSaveDirectory(fc.getSelectedFile());
Expand All @@ -124,6 +125,7 @@ else if ((e.getKeyCode() == KeyEvent.VK_S) && ((e.getModifiersEx() & KeyEvent.CT
jarExport.start();
}
}, "Resource Export");

resourceExport.start();
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/the/bytecode/club/bytecodeviewer/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,32 +127,38 @@ protected static void resetRecentFilesMenu()
{
//build recent files
BytecodeViewer.viewer.recentFilesSecondaryMenu.removeAll();

for (String s : recentFiles)
{
if (!s.isEmpty())
{
JMenuItem m = new JMenuItem(s);

m.addActionListener(e ->
{
JMenuItem m12 = (JMenuItem) e.getSource();
BytecodeViewer.openFiles(new File[]{new File(m12.getText())}, true);
});

BytecodeViewer.viewer.recentFilesSecondaryMenu.add(m);
}
}

//build recent plugins
BytecodeViewer.viewer.recentPluginsSecondaryMenu.removeAll();

for (String s : recentPlugins)
{
if (!s.isEmpty())
{
JMenuItem m = new JMenuItem(s);

m.addActionListener(e ->
{
JMenuItem m1 = (JMenuItem) e.getSource();
BytecodeViewer.startPlugin(new File(m1.getText()));
});

BytecodeViewer.viewer.recentPluginsSecondaryMenu.add(m);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,13 @@ public static void loadSettings()
BytecodeViewer.viewer.refreshOnChange.setSelected(asBoolean(84));

boolean bool = Boolean.parseBoolean(asString(85));

if (bool)
{
BytecodeViewer.viewer.setExtendedState(JFrame.MAXIMIZED_BOTH);
BytecodeViewer.viewer.isMaximized = true;
}

//86 is deprecated
//87 is deprecated
Configuration.lastOpenDirectory = asString(88);
Expand Down Expand Up @@ -388,6 +390,7 @@ public static void loadSettings()
//line 130 is used for preload
if (Configuration.language != Language.ENGLISH)
Configuration.language.setLanguageTranslations(); //load language translations

Settings.hasSetLanguageAsSystemLanguage = true;

BytecodeViewer.viewer.viewPane1.setPaneEditable(asBoolean(131));
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/the/bytecode/club/bytecodeviewer/cli/CLIAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package the.bytecode.club.bytecodeviewer.cli;

/**
* @author Konloch
* @since 9/27/2024
*/
public enum CLIAction
{
STOP,
GUI,
CLI
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/

package the.bytecode.club.bytecodeviewer;
package the.bytecode.club.bytecodeviewer.cli;

import me.konloch.kontainer.io.DiskWriter;
import org.apache.commons.cli.CommandLine;
Expand All @@ -25,13 +25,17 @@
import org.apache.commons.cli.Options;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Configuration;
import the.bytecode.club.bytecodeviewer.Constants;
import the.bytecode.club.bytecodeviewer.decompilers.Decompiler;
import the.bytecode.club.bytecodeviewer.translation.Language;
import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;

import java.io.File;

import static the.bytecode.club.bytecodeviewer.cli.CLIAction.*;
import static the.bytecode.club.bytecodeviewer.Constants.*;

/**
Expand All @@ -46,11 +50,6 @@ public class CommandLineInput
private static final Options OPTIONS = new Options();
private static final CommandLineParser PARSER = new DefaultParser();

/*BECAUSE WHO DOESN'T LOVE MAGIC NUMBERS*/
public static final int STOP = -1;
public static final int GUI = 0;
public static final int CLI = 1;

static
{
OPTIONS.addOption("help", false, "prints the help menu.");
Expand Down Expand Up @@ -92,7 +91,7 @@ public static boolean containsCommand(String[] args)
return false;
}

public static int parseCommandLine(String[] args)
public static CLIAction parseCommandLine(String[] args)
{
if (!containsCommand(args))
return GUI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,23 @@ public class JavaCompiler extends InternalCompiler
@Override
public byte[] compile(String contents, String fullyQualifiedName)
{
String fileStart = TEMP_DIRECTORY + FS + "temp" + MiscUtils.randomString(12) + FS;
String fileStart2 = TEMP_DIRECTORY + FS + "temp" + MiscUtils.randomString(12) + FS;
File java = new File(fileStart + FS + fullyQualifiedName + ".java");
File clazz = new File(fileStart2 + FS + fullyQualifiedName + ".class");
File cp = new File(TEMP_DIRECTORY + FS + "cpath_" + MiscUtils.randomString(12) + ".jar");
File tempD = new File(fileStart + FS + fullyQualifiedName.substring(0, fullyQualifiedName.length() -
final String fileStart = TEMP_DIRECTORY + FS + "temp" + MiscUtils.randomString(12) + FS;
final String fileStart2 = TEMP_DIRECTORY + FS + "temp" + MiscUtils.randomString(12) + FS;

final File javaFile = new File(fileStart + FS + fullyQualifiedName + ".java");
final File classFile = new File(fileStart2 + FS + fullyQualifiedName + ".class");
final File classPath = new File(TEMP_DIRECTORY + FS + "cpath_" + MiscUtils.randomString(12) + ".jar");
final File tempDirectory = new File(fileStart + FS + fullyQualifiedName.substring(0, fullyQualifiedName.length() -
fullyQualifiedName.split("/")[fullyQualifiedName.split("/").length - 1].length()));

tempD.mkdirs();
//create the temp directories
tempDirectory.mkdirs();
new File(fileStart2).mkdirs();

if (Configuration.javac.isEmpty() || !new File(Configuration.javac).exists())
{
BytecodeViewer.showMessage("You need to set your Javac path, this requires the JDK to be downloaded." + NL + "(C:/Program Files/Java/JDK_xx/bin/javac.exe)");
BytecodeViewer.showMessage("You need to set your Javac path, this requires the JDK to be downloaded."
+ NL + "(C:/Program Files/Java/JDK_xx/bin/javac.exe)");
ExternalResources.getSingleton().selectJavac();
}

Expand All @@ -66,8 +69,11 @@ public byte[] compile(String contents, String fullyQualifiedName)
return null;
}

DiskWriter.replaceFile(java.getAbsolutePath(), contents, false);
JarUtils.saveAsJar(BytecodeViewer.getLoadedClasses(), cp.getAbsolutePath());
//write the file we're assembling to disk
DiskWriter.replaceFile(javaFile.getAbsolutePath(), contents, false);

//write the entire temporary classpath to disk
JarUtils.saveAsJar(BytecodeViewer.getLoadedClasses(), classPath.getAbsolutePath());

boolean cont = true;
try
Expand All @@ -77,10 +83,10 @@ public byte[] compile(String contents, String fullyQualifiedName)

if (Configuration.library.isEmpty())
pb = new ProcessBuilder(Configuration.javac, "-d", fileStart2,
"-classpath", cp.getAbsolutePath(), java.getAbsolutePath());
"-classpath", classPath.getAbsolutePath(), javaFile.getAbsolutePath());
else
pb = new ProcessBuilder(Configuration.javac, "-d", fileStart2,
"-classpath", cp.getAbsolutePath() + System.getProperty("path.separator") + Configuration.library, java.getAbsolutePath());
"-classpath", classPath.getAbsolutePath() + System.getProperty("path.separator") + Configuration.library, javaFile.getAbsolutePath());

Process process = pb.start();
BytecodeViewer.createdProcesses.add(process);
Expand Down Expand Up @@ -111,6 +117,7 @@ public byte[] compile(String contents, String fullyQualifiedName)
}

log.append(NL).append(NL).append(TranslatedStrings.ERROR2).append(NL).append(NL);

try (InputStream is = process.getErrorStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr))
Expand All @@ -123,7 +130,7 @@ public byte[] compile(String contents, String fullyQualifiedName)
log.append(NL).append(NL).append(TranslatedStrings.EXIT_VALUE_IS).append(" ").append(exitValue);
System.out.println(log);

if (!clazz.exists())
if (!classFile.exists())
throw new Exception(log.toString());
}
catch (Exception e)
Expand All @@ -132,12 +139,12 @@ public byte[] compile(String contents, String fullyQualifiedName)
e.printStackTrace();
}

cp.delete();
classPath.delete();

if (cont)
try
{
return org.apache.commons.io.FileUtils.readFileToByteArray(clazz);
return org.apache.commons.io.FileUtils.readFileToByteArray(classFile);
}
catch (IOException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,19 @@ public byte[] compile(String contents, String fullyQualifiedName)
if (!ExternalResources.getSingleton().hasSetPython2Command())
return null;

File tempD = new File(Constants.TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + FS);
tempD.mkdir();
final File tempDirectory1 = new File(Constants.TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + FS);
final File tempDirectory2 = new File(Constants.TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + FS);
final File javaFile = new File(tempDirectory1.getAbsolutePath() + FS + fullyQualifiedName + ".j");
final File tempJar = new File(Constants.TEMP_DIRECTORY + FS + "temp" + MiscUtils.randomString(32) + ".jar");

File tempJ = new File(tempD.getAbsolutePath() + FS + fullyQualifiedName + ".j");
DiskWriter.replaceFile(tempJ.getAbsolutePath(), contents, true);
//create the temp directories
tempDirectory1.mkdir();
tempDirectory2.mkdir();

final File tempDirectory = new File(Constants.TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + FS);
tempDirectory.mkdir();
//write the file we're assembling to disk
DiskWriter.replaceFile(javaFile.getAbsolutePath(), contents, true);

final File tempJar = new File(Constants.TEMP_DIRECTORY + FS + "temp" + MiscUtils.randomString(32) + ".jar");
//write the entire temporary classpath to disk
JarUtils.saveAsJar(BytecodeViewer.getLoadedClasses(), tempJar.getAbsolutePath());

StringBuilder log = new StringBuilder();
Expand All @@ -72,7 +75,7 @@ public byte[] compile(String contents, String fullyQualifiedName)
pythonCommands = ArrayUtils.addAll(pythonCommands, "-2");

ProcessBuilder pb = new ProcessBuilder(ArrayUtils.addAll(pythonCommands, "-O", //love you storyyeller <3
krakatauWorkingDirectory + FS + "assemble.py", "-out", tempDirectory.getAbsolutePath(), tempJ.getAbsolutePath()));
krakatauWorkingDirectory + FS + "assemble.py", "-out", tempDirectory2.getAbsolutePath(), javaFile.getAbsolutePath()));

Process process = pb.start();
BytecodeViewer.createdProcesses.add(process);
Expand Down Expand Up @@ -101,10 +104,15 @@ public byte[] compile(String contents, String fullyQualifiedName)
log.append(NL).append(NL).append(TranslatedStrings.EXIT_VALUE_IS).append(" ").append(exitValue);
System.err.println(log);

byte[] b = FileUtils.readFileToByteArray(Objects.requireNonNull(ExternalResources.getSingleton().findFile(tempDirectory, ".class")));
tempDirectory.delete();
//read the assembled bytes from disk
byte[] assembledBytes = FileUtils.readFileToByteArray(Objects.requireNonNull(ExternalResources.getSingleton().findFile(tempDirectory2, ".class")));

//cleanup
tempDirectory2.delete();
tempJar.delete();
return b;

//return the assembled file
return assembledBytes;
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,21 @@ public class SmaliAssembler extends InternalCompiler
@Override
public byte[] compile(String contents, String fullyQualifiedName)
{
String fileStart = TEMP_DIRECTORY + FS + "temp";
int fileNumber = MiscUtils.getClassNumber(fileStart, ".dex");

final String fileStart = TEMP_DIRECTORY + FS + "temp";
final int fileNumber = MiscUtils.getClassNumber(fileStart, ".dex");
final File tempSmaliFolder = new File(fileStart + fileNumber + "-smalifolder" + FS);
tempSmaliFolder.mkdir();

File tempSmali = new File(tempSmaliFolder.getAbsolutePath() + FS + fileNumber + ".smali");
File tempDex = new File("./out.dex");
File tempJar = new File(fileStart + fileNumber + ".jar");
File tempJarFolder = new File(fileStart + fileNumber + "-jar" + FS);
final File tempSmali = new File(tempSmaliFolder.getAbsolutePath() + FS + fileNumber + ".smali");
final File tempDex = new File("./out.dex");
final File tempJar = new File(fileStart + fileNumber + ".jar");
final File tempJarFolder = new File(fileStart + fileNumber + "-jar" + FS);

//create the temp directory
tempSmaliFolder.mkdir();

try
{
//write the file we're assembling to disk
DiskWriter.replaceFile(tempSmali.getAbsolutePath(), contents, false);
}
catch (Exception e)
Expand Down Expand Up @@ -107,6 +109,7 @@ else if (BytecodeViewer.viewer.apkConversionGroup.isSelected(BytecodeViewer.view

System.out.println("Saved as: " + outputClass.getAbsolutePath());

//return the assembled file
return FileUtils.readFileToByteArray(outputClass);
}
catch (java.lang.NullPointerException ignored)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
public abstract class InternalDecompiler
{
public abstract String decompileClassNode(ClassNode cn, byte[] b);
public abstract String decompileClassNode(ClassNode cn, byte[] bytes);

public abstract void decompileToZip(String sourceJar, String zipName);
}
Loading

0 comments on commit 2b70b56

Please sign in to comment.