Skip to content

Commit

Permalink
Code Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Konloch committed Sep 30, 2024
1 parent fb82558 commit 5dab52b
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public class Configuration
public static boolean python3Extra = false;
public static String rt = "";
public static String library = "";
public static String java = Constants.JAVA_BINARY.exists() ? Constants.JAVA_BINARY.getAbsolutePath() : Constants.JAVA_BINARY_NIX.exists() ? Constants.JAVA_BINARY_NIX.getAbsolutePath() : "";
public static String java = Constants.JAVA_BINARY.exists()
? Constants.JAVA_BINARY.getAbsolutePath()
: Constants.JAVA_BINARY_NIX.exists()
? Constants.JAVA_BINARY_NIX.getAbsolutePath()
: "";
public static String javac = "";
public static String javaTools = "";
public static File krakatauTempDir;
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/the/bytecode/club/bytecodeviewer/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ public class Constants

//version is set via maven
public static final String VERSION = getVersion(BytecodeViewer.class.getPackage().getImplementationVersion());

//CHECKSTYLE:OFF
//dev mode is just a check for running via IDE
public static boolean DEV_MODE;
//CHECKSTYLE:ON

//if true the version checker will prompt and ask how you would like to proceed
public static final boolean FORCE_VERSION_CHECKER_PROMPT = false;
Expand All @@ -71,7 +74,7 @@ public class Constants
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();
public static final File BCV_DIR = resolveBCVRoot();
public static final File RT_JAR = new File(System.getProperty("java.home") + FS + "lib" + FS + "rt.jar");
public static final File JAVA_BINARY = new File(System.getProperty("java.home") + FS + "bin" + FS + "java.exe");
public static final File JAVA_BINARY_NIX = new File(System.getProperty("java.home") + FS + "bin" + FS + "java");
Expand Down Expand Up @@ -119,18 +122,18 @@ public static File resolveBCVRoot()
*/
public static String getBCVDirectory()
{
while (!BCVDir.exists())
BCVDir.mkdirs();
while (!BCV_DIR.exists())
BCV_DIR.mkdirs();

//hides the BCV directory
if (isWindows() && !BCVDir.isHidden())
if (isWindows() && !BCV_DIR.isHidden())
{
new Thread(() ->
{
try
{
// Hide file by running attrib system command (on Windows)
Process p = new ProcessBuilder("attrib", "+H", BCVDir.getAbsolutePath()).start();
Process p = new ProcessBuilder("attrib", "+H", BCV_DIR.getAbsolutePath()).start();
}
catch (Exception e)
{
Expand All @@ -139,7 +142,7 @@ public static String getBCVDirectory()
}, "Hide BCV Dir").start();
}

return BCVDir.getAbsolutePath();
return BCV_DIR.getAbsolutePath();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ public static String buildErrorLogHeader(String author)

return TranslatedStrings.PLEASE_SEND_THIS_ERROR_LOG_TO + " " + author + "\n"
+ TranslatedStrings.PLEASE_SEND_RESOURCES
+ "\nBytecode Viewer Version: " + VERSION + fatJar + ", OS: " + System.getProperty("os.name") + ", Java: " + System.getProperty("java.version");
+ "\nBytecode Viewer Version: " + VERSION + fatJar
+ ", OS: " + System.getProperty("os.name")
+ ", Java: " + System.getProperty("java.version");
}

private static final long serialVersionUID = -5230501978224926296L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ public static void dropKrakatau()
while (temp.exists())
temp.delete();

try (InputStream is = BytecodeViewer.class.getClassLoader().getResourceAsStream("Krakatau-" + krakatauVersion + ".zip"); FileOutputStream baos = new FileOutputStream(temp))
try (InputStream is = BytecodeViewer.class.getClassLoader().getResourceAsStream("Krakatau-" + krakatauVersion + ".zip");
FileOutputStream baos = new FileOutputStream(temp))
{
int r;
byte[] buffer = new byte[8192];
Expand Down Expand Up @@ -368,7 +369,8 @@ public static void dropEnjarify()
while (temp.exists())
temp.delete();

try (InputStream is = BytecodeViewer.class.getClassLoader().getResourceAsStream("enjarify-" + Constants.enjarifyVersion + ".zip"); FileOutputStream baos = new FileOutputStream(temp))
try (InputStream is = BytecodeViewer.class.getClassLoader().getResourceAsStream("enjarify-" + Constants.enjarifyVersion + ".zip");
FileOutputStream baos = new FileOutputStream(temp))
{
int r;
byte[] buffer = new byte[8192];
Expand Down Expand Up @@ -405,7 +407,8 @@ public static void downloadZipsOnly()
setState("Bytecode Viewer Boot Screen - Downloading " + fileName + "...");
System.out.println("Downloading " + fileName);

try (InputStream is = new URL("https://github.com/Konloch/bytecode-viewer/raw/master/libs/" + fileName).openConnection().getInputStream(); FileOutputStream fos = new FileOutputStream(file))
try (InputStream is = new URL("https://github.com/Konloch/bytecode-viewer/raw/master/libs/" + fileName).openConnection().getInputStream();
FileOutputStream fos = new FileOutputStream(file))
{
System.out.println("Downloading from " + s);
byte[] buffer = new byte[8192];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public void run()
if (Desktop.isDesktopSupported())
Desktop.getDesktop().browse(new URI("https://github.com/Konloch/bytecode-viewer/releases"));
else
BytecodeViewer.showMessage("Cannot open the page, please manually type it." + NL + "https://github.com/Konloch/bytecode-viewer/releases");
BytecodeViewer.showMessage("Cannot open the page, please manually type it."
+ NL + "https://github.com/Konloch/bytecode-viewer/releases");
}
else if (result == 1)
{
Expand Down Expand Up @@ -176,7 +177,7 @@ public static File promptFileSave(String description, String extension) throws I
}

//used to download all released versions of BCV
/*public static void main(String[] args)
/*public static void main(String[] args)
{
BytecodeViewer.viewer = new MainViewerGUI();
for(String version : BCV_VERSIONS)
Expand Down Expand Up @@ -234,7 +235,8 @@ private static boolean validURl(String url) throws Exception
private static void download(String url, File saveTo, Runnable onFinish) throws Exception
{
BCV.log("Downloading from: " + url);
BytecodeViewer.showMessage("Downloading the jar in the background, when it's finished you will be alerted with another message box." + NL + NL + "Expect this to take several minutes.");
BytecodeViewer.showMessage("Downloading the jar in the background, when it's finished you will be alerted with another message box."
+ NL + NL + "Expect this to take several minutes.");

try (InputStream is = new URL(url).openConnection().getInputStream(); FileOutputStream fos = new FileOutputStream(saveTo))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,15 @@ else if (cmd.hasOption("help"))
//if its zip/jar/apk/dex attempt unzip as whole zip
//if its just class allow any

if (decompiler != null && !decompiler.equalsIgnoreCase("procyon") && !decompiler.equalsIgnoreCase("cfr") && !decompiler.equalsIgnoreCase("fernflower") && !decompiler.equalsIgnoreCase("krakatau") && !decompiler.equalsIgnoreCase("krakatau-bytecode") && !decompiler.equalsIgnoreCase("jd-gui") && !decompiler.equalsIgnoreCase("smali") && !decompiler.equalsIgnoreCase("asmifier"))
if (decompiler != null
&& !decompiler.equalsIgnoreCase("procyon")
&& !decompiler.equalsIgnoreCase("cfr")
&& !decompiler.equalsIgnoreCase("fernflower")
&& !decompiler.equalsIgnoreCase("krakatau")
&& !decompiler.equalsIgnoreCase("krakatau-bytecode")
&& !decompiler.equalsIgnoreCase("jd-gui")
&& !decompiler.equalsIgnoreCase("smali")
&& !decompiler.equalsIgnoreCase("asmifier"))
{
System.out.println("Error, no decompiler called '" + decompiler + "' found. Type -list" + " for the list");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public InstructionPrinter(MethodNode m, InstructionPattern pattern, TypeAndName[
this(m, args);
InstructionSearcher searcher = new InstructionSearcher(m.instructions, pattern);
match = searcher.search();

if (match)
{
for (AbstractInsnNode[] ains : searcher.getMatches())
Expand Down Expand Up @@ -102,6 +103,7 @@ public List<String> createPrint()
{
firstLabel = false;
info.clear();

for (AbstractInsnNode ain : mNode.instructions)
{
String line = printInstruction(ain);
Expand All @@ -114,8 +116,10 @@ public List<String> createPrint()
info.add(line);
}
}

if (firstLabel && BytecodeViewer.viewer.appendBracketsToLabels.isSelected())
info.add("}");

return info;
}

Expand Down Expand Up @@ -169,16 +173,12 @@ protected String printVarInsnNode(VarInsnNode vin)
if (BytecodeViewer.viewer.debugHelpers.isSelected())
{
if (vin.var == 0 && !Modifier.isStatic(mNode.access))
{
sb.append(" // reference to self");
}
else
{
final int refIndex = vin.var - (Modifier.isStatic(mNode.access) ? 0 : 1);
if (refIndex >= 0 && refIndex < args.length - 1)
{
sb.append(" // reference to ").append(args[refIndex].name);
}
}
}

Expand Down Expand Up @@ -229,9 +229,11 @@ protected String printMethodInsnNode(MethodInsnNode min)
protected String printLdcInsnNode(LdcInsnNode ldc)
{
if (ldc.cst instanceof String)
return nameOpcode(ldc.getOpcode()) + " \"" + StringEscapeUtils.escapeJava(ldc.cst.toString()) + "\" (" + ldc.cst.getClass().getCanonicalName() + ")";
return nameOpcode(ldc.getOpcode()) + " \"" + StringEscapeUtils.escapeJava(ldc.cst.toString())
+ "\" (" + ldc.cst.getClass().getCanonicalName() + ")";

return nameOpcode(ldc.getOpcode()) + " " + StringEscapeUtils.escapeJava(ldc.cst.toString()) + " (" + ldc.cst.getClass().getCanonicalName() + ")";
return nameOpcode(ldc.getOpcode()) + " " + StringEscapeUtils.escapeJava(ldc.cst.toString())
+ " (" + ldc.cst.getClass().getCanonicalName() + ")";
}

protected String printInsnNode(InsnNode in)
Expand Down Expand Up @@ -270,6 +272,7 @@ protected String printLabelNode(LabelNode label)
String starting = tcbs.stream().filter(tcb -> tcb.start == label).map(tcb -> "start TCB" + tcbs.indexOf(tcb)).collect(Collectors.joining(", "));
String ending = tcbs.stream().filter(tcb -> tcb.end == label).map(tcb -> "end TCB" + tcbs.indexOf(tcb)).collect(Collectors.joining(", "));
String handlers = tcbs.stream().filter(tcb -> tcb.handler == label).map(tcb -> "handle TCB" + tcbs.indexOf(tcb)).collect(Collectors.joining(", "));

if (!ending.isEmpty())
info.add("// " + ending);
if (!starting.isEmpty())
Expand Down Expand Up @@ -325,10 +328,12 @@ protected String printTableSwitchInsnNode(TableSwitchInsnNode tin)
StringBuilder line = new StringBuilder(nameOpcode(tin.getOpcode()) + " \n");
List<?> labels = tin.labels;
int count = 0;

for (int i = tin.min; i < tin.max + 1; i++)
{
line.append(" val: ").append(i).append(" -> ").append("L").append(resolveLabel((LabelNode) labels.get(count++))).append("\n");
}

line.append(" default" + " -> L").append(resolveLabel(tin.dflt));
return line.toString();
}
Expand Down Expand Up @@ -408,6 +413,7 @@ private String printFrameObject(Object obj)
{
if (obj instanceof LabelNode)
return "label [L" + resolveLabel((LabelNode) obj) + "]";

if (obj instanceof Integer)
{
switch ((int) obj)
Expand All @@ -430,8 +436,10 @@ private String printFrameObject(Object obj)
return "unknown";
}
}

if (obj instanceof String)
return obj.toString();

return "unknown [" + obj.toString() + "]";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ public String decompileClassNode(ClassNode cn, byte[] bytes)
@Override
public void decompileToZip(String sourceJar, String zipName)
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static HTMLPane fromString(String text)
text = text.replace("{fatJar}", String.valueOf(FAT_JAR));
text = text.replace("{java}", Configuration.java);
text = text.replace("{javac}", Configuration.javac);
text = text.replace("{bcvDir}", BCVDir.getAbsolutePath());
text = text.replace("{bcvDir}", BCV_DIR.getAbsolutePath());
text = text.replace("{python}", Configuration.python2 + " " + (Configuration.python2Extra ? "-2" : ""));
text = text.replace("{python3}", Configuration.python3 + " " + (Configuration.python3Extra ? "-3" : ""));
text = text.replace("{rt}", Configuration.rt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public enum LAFTheme
SOLARIZED_DARK("Solarized Dark Theme", RSTATheme.THEME_MATCH, TranslatedComponents.SOLARIZED_DARK_THEME),
SOLARIZED_LIGHT("Solarized Light Theme", RSTATheme.THEME_MATCH, TranslatedComponents.SOLARIZED_LIGHT_THEME),
HIGH_CONTRAST_DARK("High Contrast Dark Theme", RSTATheme.THEME_MATCH, TranslatedComponents.HIGH_CONTRAST_DARK_THEME),
HIGH_CONTRAST_LIGHT("High Contrast Light Theme", RSTATheme.THEME_MATCH, TranslatedComponents.HIGH_CONTRAST_LIGHT_THEME),
;
HIGH_CONTRAST_LIGHT("High Contrast Light Theme", RSTATheme.THEME_MATCH, TranslatedComponents.HIGH_CONTRAST_LIGHT_THEME);

private final String readableName;
private final RSTATheme rstaTheme;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ public enum RSTATheme
THEME_MATCH("Theme Match (Recommended)", null, TranslatedComponents.THEME_MATCH), //uses the default theme from RSyntaxTextArea
DEFAULT("Default (Recommended Light)", "/org/fife/ui/rsyntaxtextarea/themes/default.xml", TranslatedComponents.DEFAULT_RECOMMENDED_LIGHT), //uses the default dark theme from RSyntaxTextArea
DARK("Dark (Recommended Dark)", "/org/fife/ui/rsyntaxtextarea/themes/dark.xml", TranslatedComponents.DARK),
DEFAULT_ALT("Default-Alt", "/org/fife/ui/rsyntaxtextarea/themes/default-alt.xml", TranslatedComponents.DEFAULT_ALT), ECLIPSE("Eclipse", "/org/fife/ui/rsyntaxtextarea/themes/eclipse.xml", TranslatedComponents.ECLIPSE), IDEA("IntelliJ", "/org/fife/ui/rsyntaxtextarea/themes/idea.xml", TranslatedComponents.INTELLIJ), VS("Visual Studio", "/org/fife/ui/rsyntaxtextarea/themes/vs.xml", TranslatedComponents.VISUAL_STUDIO), DRUID("Druid (Dark)", "/org/fife/ui/rsyntaxtextarea/themes/druid.xml", TranslatedComponents.DRUID_DARK), MONOKAI("Monokai (Dark)", "/org/fife/ui/rsyntaxtextarea/themes/monokai.xml", TranslatedComponents.MONOKAI_DARK);
DEFAULT_ALT("Default-Alt", "/org/fife/ui/rsyntaxtextarea/themes/default-alt.xml", TranslatedComponents.DEFAULT_ALT),
ECLIPSE("Eclipse", "/org/fife/ui/rsyntaxtextarea/themes/eclipse.xml", TranslatedComponents.ECLIPSE),
IDEA("IntelliJ", "/org/fife/ui/rsyntaxtextarea/themes/idea.xml", TranslatedComponents.INTELLIJ),
VS("Visual Studio", "/org/fife/ui/rsyntaxtextarea/themes/vs.xml", TranslatedComponents.VISUAL_STUDIO),
DRUID("Druid (Dark)", "/org/fife/ui/rsyntaxtextarea/themes/druid.xml", TranslatedComponents.DRUID_DARK),
MONOKAI("Monokai (Dark)", "/org/fife/ui/rsyntaxtextarea/themes/monokai.xml", TranslatedComponents.MONOKAI_DARK);

private final String readableName;
private final String file;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public void obfuscate()
{
FieldNode f = (FieldNode) o;
String newName = generateUniqueName(stringLength);
ASMResourceUtil.renameFieldNode(c.name, f.name, f.desc, null, newName, null);
ASMResourceUtil.renameFieldNode(c.name, f.name, f.desc,
null, newName, null);
f.name = newName;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public void obfuscate()
if (!m.name.equals("main") && !m.name.equals("<init>") && !m.name.equals("<clinit>"))
{
String newName = generateUniqueName(stringLength);
ASMResourceUtil.renameMethodNode(c.name, m.name, m.desc, null, newName, null);
ASMResourceUtil.renameMethodNode(c.name, m.name, m.desc,
null, newName, null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ public Object mapValue(Object value)
if (value instanceof Handle)
{
Handle h = (Handle) value;
return new Handle(h.getTag(), mapType(h.getOwner()), mapMethodName(h.getOwner(), h.getName(), h.getDesc()), mapMethodDesc(h.getDesc()), h.getTag() == Opcodes.H_INVOKEINTERFACE);
return new Handle(h.getTag(),
mapType(h.getOwner()),
mapMethodName(h.getOwner(), h.getName(), h.getDesc()),
mapMethodDesc(h.getDesc()),
h.getTag() == Opcodes.H_INVOKEINTERFACE);
}

return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void open()
{
if (Configuration.runningObfuscation)
{
BytecodeViewer.showMessage("You're currently running an obfuscation task, wait for this to finish" + ".");
BytecodeViewer.showMessage("You're currently running an obfuscation task, wait for this to finish.");
return;
}

Expand Down Expand Up @@ -75,9 +75,6 @@ public void obfuscate()
String newName = generateUniqueName(stringLength);

BytecodeViewer.refactorer.getHooks().addClass(new MappingData(c.name, newName));

/*ASMUtil_OLD.renameClassNode(c.name, newName);
c.name = newName;*/
}

System.out.println("Obfuscated class names.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ public void obfuscate()
String newName = generateUniqueName(stringLength);

BytecodeViewer.refactorer.getHooks().addField(new FieldMappingData(c.name, new MappingData(f.name, newName), f.desc));

/*ASMUtil_OLD.renameFieldNode(c.name, f.name, f.desc, null, newName, null);
f.name = newName;*/
}
}

Expand Down
Loading

0 comments on commit 5dab52b

Please sign in to comment.