Skip to content

Commit

Permalink
Code Style Cleanup / General Code Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Konloch committed Sep 27, 2024
1 parent 3ab9932 commit 9353c96
Show file tree
Hide file tree
Showing 84 changed files with 544 additions and 384 deletions.
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
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ public static PrefixedStringBuilder decompile(PrefixedStringBuilder sb, FieldNod
{
String s = getAccessString(f.access);
sb.append(s);

if (s.length() > 0)
sb.append(" ");

sb.append(Type.getType(f.desc).getClassName());
sb.append(" ");
sb.append(f.name);

if (f.value != null)
{
sb.append(" = ");
Expand All @@ -59,13 +62,16 @@ public static PrefixedStringBuilder decompile(PrefixedStringBuilder sb, FieldNod
sb.append(")");
}
}

sb.append(";");

return sb;
}

private static String getAccessString(int access)
{
List<String> tokens = new ArrayList<>();

if ((access & Opcodes.ACC_PUBLIC) != 0)
tokens.add("public");
if ((access & Opcodes.ACC_PRIVATE) != 0)
Expand All @@ -84,13 +90,15 @@ private static String getAccessString(int access)
tokens.add("volatile");
if (tokens.size() == 0)
return "";

// hackery delimeters
StringBuilder sb = new StringBuilder(tokens.get(0));
for (int i = 1; i < tokens.size(); i++)
{
sb.append(" ");
sb.append(tokens.get(i));
}

return sb.toString();
}
}
Loading

0 comments on commit 9353c96

Please sign in to comment.