Code Cleanup

This commit is contained in:
Konloch 2021-06-26 08:56:39 -07:00
parent 0cc98012cd
commit 18473a1167
7 changed files with 72 additions and 42 deletions

View file

@ -74,7 +74,6 @@ import static the.bytecode.club.bytecodeviewer.Constants.*;
* http://the.bytecode.club * http://the.bytecode.club
* *
* TODO BUGS: * TODO BUGS:
* + Last selected directory isn't set on most file chooser dialogues
* + Synchronized scrolling is broken * + Synchronized scrolling is broken
* + Spam-clicking the refresh button will cause the swing thread to deadlock (Quickly opening resources used to also do this) * + Spam-clicking the refresh button will cause the swing thread to deadlock (Quickly opening resources used to also do this)
* This is caused by the ctrlMouseWheelZoom code, a temporary patch is just removing it worst case * This is caused by the ctrlMouseWheelZoom code, a temporary patch is just removing it worst case
@ -627,9 +626,10 @@ public class BytecodeViewer
Constants.SUPPORTED_FILE_EXTENSIONS); Constants.SUPPORTED_FILE_EXTENSIONS);
int returnVal = fc.showOpenDialog(BytecodeViewer.viewer); int returnVal = fc.showOpenDialog(BytecodeViewer.viewer);
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION)
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath(); {
try { try {
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath();
BytecodeViewer.viewer.updateBusyStatus(true); BytecodeViewer.viewer.updateBusyStatus(true);
BytecodeViewer.openFiles(new File[]{fc.getSelectedFile()}, true); BytecodeViewer.openFiles(new File[]{fc.getSelectedFile()}, true);
BytecodeViewer.viewer.updateBusyStatus(false); BytecodeViewer.viewer.updateBusyStatus(false);
@ -669,7 +669,9 @@ public class BytecodeViewer
"zip"); "zip");
int returnVal = fc.showSaveDialog(viewer); int returnVal = fc.showSaveDialog(viewer);
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION)
{
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath();
File file = fc.getSelectedFile(); File file = fc.getSelectedFile();
if (!file.getAbsolutePath().endsWith(".zip")) if (!file.getAbsolutePath().endsWith(".zip"))
file = new File(file.getAbsolutePath() + ".zip"); file = new File(file.getAbsolutePath() + ".zip");

View file

@ -811,8 +811,8 @@ public class MainViewerGUI extends JFrame
int returnVal = fc.showOpenDialog(BytecodeViewer.viewer); int returnVal = fc.showOpenDialog(BytecodeViewer.viewer);
if (returnVal == JFileChooser.APPROVE_OPTION) if (returnVal == JFileChooser.APPROVE_OPTION)
{ {
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath();
try { try {
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath();
BytecodeViewer.viewer.updateBusyStatus(true); BytecodeViewer.viewer.updateBusyStatus(true);
BytecodeViewer.openFiles(new File[]{fc.getSelectedFile()}, true); BytecodeViewer.openFiles(new File[]{fc.getSelectedFile()}, true);
BytecodeViewer.viewer.updateBusyStatus(false); BytecodeViewer.viewer.updateBusyStatus(false);

View file

@ -46,6 +46,7 @@ public class ResourceDecompiling
int returnVal = fc.showSaveDialog(BytecodeViewer.viewer); int returnVal = fc.showSaveDialog(BytecodeViewer.viewer);
if (returnVal == JFileChooser.APPROVE_OPTION) if (returnVal == JFileChooser.APPROVE_OPTION)
{ {
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath();
File file = fc.getSelectedFile(); File file = fc.getSelectedFile();
//auto appened zip //auto appened zip
@ -222,6 +223,7 @@ public class ResourceDecompiling
int returnVal = fc.showSaveDialog(BytecodeViewer.viewer); int returnVal = fc.showSaveDialog(BytecodeViewer.viewer);
if (returnVal == JFileChooser.APPROVE_OPTION) if (returnVal == JFileChooser.APPROVE_OPTION)
{ {
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath();
File file = fc.getSelectedFile(); File file = fc.getSelectedFile();
BytecodeViewer.viewer.updateBusyStatus(true); BytecodeViewer.viewer.updateBusyStatus(true);

View file

@ -42,6 +42,7 @@ public class ResourceExporting
int returnVal = fc.showSaveDialog(BytecodeViewer.viewer); int returnVal = fc.showSaveDialog(BytecodeViewer.viewer);
if (returnVal == JFileChooser.APPROVE_OPTION) if (returnVal == JFileChooser.APPROVE_OPTION)
{ {
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath();
File file = fc.getSelectedFile(); File file = fc.getSelectedFile();
String path = file.getAbsolutePath(); String path = file.getAbsolutePath();
@ -97,6 +98,7 @@ public class ResourceExporting
int returnVal = fc.showSaveDialog(BytecodeViewer.viewer); int returnVal = fc.showSaveDialog(BytecodeViewer.viewer);
if (returnVal == JFileChooser.APPROVE_OPTION) if (returnVal == JFileChooser.APPROVE_OPTION)
{ {
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath();
File file = fc.getSelectedFile(); File file = fc.getSelectedFile();
//auto append .zip //auto append .zip
@ -159,6 +161,7 @@ public class ResourceExporting
int returnVal = fc.showSaveDialog(BytecodeViewer.viewer); int returnVal = fc.showSaveDialog(BytecodeViewer.viewer);
if (returnVal == JFileChooser.APPROVE_OPTION) if (returnVal == JFileChooser.APPROVE_OPTION)
{ {
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath();
final File file = fc.getSelectedFile(); final File file = fc.getSelectedFile();
String output = file.getAbsolutePath(); String output = file.getAbsolutePath();
@ -267,6 +270,7 @@ public class ResourceExporting
int returnVal = fc.showSaveDialog(BytecodeViewer.viewer); int returnVal = fc.showSaveDialog(BytecodeViewer.viewer);
if (returnVal == JFileChooser.APPROVE_OPTION) if (returnVal == JFileChooser.APPROVE_OPTION)
{ {
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath();
final File file = fc.getSelectedFile(); final File file = fc.getSelectedFile();
String output = file.getAbsolutePath(); String output = file.getAbsolutePath();

View file

@ -16,16 +16,26 @@ public class ZipResourceImporter implements Importer
@Override @Override
public boolean open(File file) throws Exception public boolean open(File file) throws Exception
{ {
try { //attempt to load archives using the first method
JarUtils.put(file); try
} catch (IOException z) { {
try { JarUtils.importArchiveA(file);
JarUtils.put2(file); }
} catch (final Exception e) { catch (IOException z)
{
//attempt to load archives using the fallback method on fail
try
{
JarUtils.importArchiveB(file);
}
catch (final Exception e)
{
new ExceptionUI(e); new ExceptionUI(e);
return false; return false;
} }
} catch (final Exception e) { }
catch (final Exception e)
{
new ExceptionUI(e); new ExceptionUI(e);
return false; return false;
} }

View file

@ -57,7 +57,8 @@ public class JarUtils {
* @param jarFile the input jar file * @param jarFile the input jar file
* @throws IOException * @throws IOException
*/ */
public static void put(final File jarFile) throws IOException { public static void importArchiveA(final File jarFile) throws IOException
{
FileContainer container = new FileContainer(jarFile); FileContainer container = new FileContainer(jarFile);
HashMap<String, byte[]> files = new HashMap<>(); HashMap<String, byte[]> files = new HashMap<>();
@ -101,8 +102,16 @@ public class JarUtils {
container.files = files; container.files = files;
BytecodeViewer.files.add(container); BytecodeViewer.files.add(container);
} }
public static void put2(final File jarFile) throws IOException {
/**
* A fallback solution to zip/jar archive importing if the first fails
*
* @param jarFile the input jar file
* @throws IOException
*/
public static void importArchiveB(final File jarFile) throws IOException
{
//if this ever fails, worst case import Sun's jarsigner code from JDK 7 re-sign the jar to rebuild the CRC, //if this ever fails, worst case import Sun's jarsigner code from JDK 7 re-sign the jar to rebuild the CRC,
// should also rebuild the archive byte offsets // should also rebuild the archive byte offsets
@ -143,9 +152,9 @@ public class JarUtils {
container.files = files; container.files = files;
BytecodeViewer.files.add(container); BytecodeViewer.files.add(container);
} }
public static ArrayList<ClassNode> loadClasses(final File jarFile) throws IOException
public static ArrayList<ClassNode> loadClasses(final File jarFile) throws IOException { {
ArrayList<ClassNode> classes = new ArrayList<>(); ArrayList<ClassNode> classes = new ArrayList<>();
ZipInputStream jis = new ZipInputStream(new FileInputStream(jarFile)); ZipInputStream jis = new ZipInputStream(new FileInputStream(jarFile));
ZipEntry entry; ZipEntry entry;

View file

@ -2,7 +2,9 @@ package the.bytecode.club.bytecodeviewer.util;
import me.konloch.kontainer.io.HTTPRequest; import me.konloch.kontainer.io.HTTPRequest;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Configuration;
import the.bytecode.club.bytecodeviewer.api.ExceptionUI; import the.bytecode.club.bytecodeviewer.api.ExceptionUI;
import the.bytecode.club.bytecodeviewer.gui.components.FileChooser;
import javax.swing.*; import javax.swing.*;
import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileFilter;
@ -56,7 +58,8 @@ public class VersionChecker implements Runnable
} }
if (!VERSION.equals(version)) { if (!VERSION.equals(version))
{
JOptionPane pane = new JOptionPane( JOptionPane pane = new JOptionPane(
"Your version: " + VERSION + ", latest version: " "Your version: " + VERSION + ", latest version: "
+ version + nl + "What would you like to do?"); + version + nl + "What would you like to do?");
@ -70,40 +73,37 @@ public class VersionChecker implements Runnable
if (options[k].equals(obj)) if (options[k].equals(obj))
result = k; result = k;
if (result == 0) { if (result == 0)
if (Desktop.isDesktopSupported()) { {
if (Desktop.isDesktopSupported())
Desktop.getDesktop().browse(new URI("https://github.com/Konloch/bytecode-viewer/releases")); Desktop.getDesktop().browse(new URI("https://github.com/Konloch/bytecode-viewer/releases"));
} else { 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");
}
} }
if (result == 1) {
JFileChooser fc = new JFileChooser(); if (result == 1)
{
JFileChooser fc = new FileChooser(new File(Configuration.lastDirectory),
"Select Save File",
"Zip Archives",
"zip");
try { try {
fc.setCurrentDirectory(new File(".").getAbsoluteFile()); //set the current working directory fc.setCurrentDirectory(new File(".").getAbsoluteFile()); //set the current working directory
} catch (Exception e) { } catch (Exception e) {
new ExceptionUI(e); new ExceptionUI(e);
} }
fc.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory() || MiscUtils.extension(f.getAbsolutePath()).equals("zip");
}
@Override
public String getDescription() {
return "Zip Archives";
}
});
fc.setFileHidingEnabled(false);
fc.setAcceptAllFileFilterUsed(false);
int returnVal = fc.showSaveDialog(BytecodeViewer.viewer); int returnVal = fc.showSaveDialog(BytecodeViewer.viewer);
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION)
{
Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath();
File file = fc.getSelectedFile(); File file = fc.getSelectedFile();
if (!file.getAbsolutePath().endsWith(".zip")) if (!file.getAbsolutePath().endsWith(".zip"))
file = new File(file.getAbsolutePath() + ".zip"); file = new File(file.getAbsolutePath() + ".zip");
if (file.exists()) { if (file.exists())
{
pane = new JOptionPane("The file " + file + " exists, would you like to overwrite it?"); pane = new JOptionPane("The file " + file + " exists, would you like to overwrite it?");
options = new String[]{"Yes", "No"}; options = new String[]{"Yes", "No"};
pane.setOptions(options); pane.setOptions(options);
@ -136,12 +136,15 @@ public class VersionChecker implements Runnable
BytecodeViewer.showMessage("Downloading the jar in the background, when it's finished " BytecodeViewer.showMessage("Downloading the jar in the background, when it's finished "
+ "you will be alerted with another message box." + nl + nl + + "you will be alerted with another message box." + nl + nl +
"Expect this to take several minutes."); "Expect this to take several minutes.");
while ((len = is.read(buffer)) > 0) {
while ((len = is.read(buffer)) > 0)
{
fos.write(buffer, 0, len); fos.write(buffer, 0, len);
fos.flush(); fos.flush();
downloaded += 8192; downloaded += 8192;
int mbs = downloaded / 1048576; int mbs = downloaded / 1048576;
if (mbs % 5 == 0 && mbs != 0) { if (mbs % 5 == 0 && mbs != 0)
{
if (!flag) if (!flag)
System.out.println("Downloaded " + mbs + "MBs so far"); System.out.println("Downloaded " + mbs + "MBs so far");
flag = true; flag = true;