Improvements

This commit is contained in:
SuspiciousActivity authored and GitHub committed 2020-11-23 19:52:23 +01:00
1 parent c91d8ccada
commit a70bc89deb
1 file changed
+99 -60
+99 -60
View File
@@ -98,7 +98,7 @@ public class Main extends JFrame {
public static Main INSTANCE; public static Main INSTANCE;
private JPanel contentPane; private JPanel contentPane;
private boolean isChangingFile; private volatile boolean isChangingFile;
public static File jarFile; public static File jarFile;
public static HashMap<String, byte[]> OTHER_FILES = new HashMap<>(); public static HashMap<String, byte[]> OTHER_FILES = new HashMap<>();
public static HashMap<String, ClassNode> classNodes = new HashMap<>(); public static HashMap<String, ClassNode> classNodes = new HashMap<>();
@@ -118,6 +118,8 @@ public class Main extends JFrame {
public static HashSet<String> fakedFolders = new HashSet<>(); public static HashSet<String> fakedFolders = new HashSet<>();
public static final Object treeLock = new Object();
/** /**
* Launch the application. * Launch the application.
*/ */
@@ -255,17 +257,35 @@ public class Main extends JFrame {
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == 116 && jarFile != null) { // F5 if (e.getKeyCode() == 116 && jarFile != null) { // F5
try { synchronized (treeLock) {
isChangingFile = true; if (isChangingFile) {
return;
}
try { try {
tree.setModel(new ArchiveTreeModel(new JarFile(jarFile))); isChangingFile = true;
} catch (IOException e1) { Main.this.setTitle("ByteEdit - Reloading...");
e1.printStackTrace(); new Thread(new Runnable() {
public void run() {
try {
ArchiveTreeModel model = new ArchiveTreeModel(new JarFile(jarFile));
EventQueue.invokeLater(new Runnable() {
public void run() {
synchronized (treeLock) {
tree.setModel(model);
isChangingFile = false;
Main.this.setTitle("ByteEdit");
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} catch (Throwable t) {
t.printStackTrace();
showError(t);
} }
isChangingFile = false;
} catch (Throwable t) {
t.printStackTrace();
showError(t);
} }
} else if (e.getKeyCode() == 127) { // del } else if (e.getKeyCode() == 127) { // del
if (tree.getSelectionPath() == null) if (tree.getSelectionPath() == null)
@@ -310,8 +330,10 @@ public class Main extends JFrame {
tree.addTreeSelectionListener(new TreeSelectionListener() { tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) { public void valueChanged(TreeSelectionEvent e) {
if (!isChangingFile) { synchronized (treeLock) {
selectFile(((ByteEditTreeNode) e.getPath().getLastPathComponent()).path); if (!isChangingFile) {
selectFile(((ByteEditTreeNode) e.getPath().getLastPathComponent()).path);
}
} }
} }
}); });
@@ -319,41 +341,65 @@ public class Main extends JFrame {
@Override @Override
public void drop(DropTargetDropEvent dtde) { public void drop(DropTargetDropEvent dtde) {
try { synchronized (treeLock) {
Transferable tr = dtde.getTransferable(); if (isChangingFile) {
DataFlavor[] flavors = tr.getTransferDataFlavors(); dtde.rejectDrop();
for (int i = 0; i < flavors.length; i++) { return;
if (flavors[i].isFlavorJavaFileListType()) { }
dtde.acceptDrop(dtde.getDropAction()); try {
java.util.List<File> files = (java.util.List<File>) tr.getTransferData(flavors[i]); Transferable tr = dtde.getTransferable();
final File file = files.get(0); DataFlavor[] flavors = tr.getTransferDataFlavors();
if (file.getName().endsWith(".jar")) { for (int i = 0; i < flavors.length; i++) {
jarFile = file; if (flavors[i].isFlavorJavaFileListType()) {
saveFolder = file.getParentFile(); dtde.acceptDrop(dtde.getDropAction());
isChangingFile = true; java.util.List<File> files = (java.util.List<File>) tr.getTransferData(flavors[i]);
try { final File file = files.get(0);
tree.setModel(new ArchiveTreeModel(new JarFile(jarFile))); if (file.getName().endsWith(".jar")) {
} catch (IOException e1) { jarFile = file;
e1.printStackTrace(); saveFolder = file.getParentFile();
isChangingFile = true;
try {
Main.this.setTitle("ByteEdit - Loading '" + jarFile.getCanonicalPath() + "'");
} catch (IOException ex) {
Main.this.setTitle("ByteEdit - Loading '" + jarFile.getAbsolutePath() + "'");
}
new Thread(new Runnable() {
public void run() {
try {
ArchiveTreeModel model = new ArchiveTreeModel(new JarFile(jarFile));
EventQueue.invokeLater(new Runnable() {
public void run() {
synchronized (treeLock) {
tree.setModel(model);
txtByteEditView.setText("");
currentNodeName = null;
globalSearchBox.setVisible(false);
searchBox.setVisible(false);
typeOpenBox.setVisible(false);
optionBox.setVisible(false);
unicodeBox.setVisible(false);
renameBox.setVisible(false);
compilationBox.setVisible(false);
isChangingFile = false;
Main.this.setTitle("ByteEdit");
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} }
txtByteEditView.setText(""); dtde.dropComplete(true);
currentNodeName = null; break;
globalSearchBox.setVisible(false);
searchBox.setVisible(false);
typeOpenBox.setVisible(false);
optionBox.setVisible(false);
unicodeBox.setVisible(false);
renameBox.setVisible(false);
compilationBox.setVisible(false);
isChangingFile = false;
} }
dtde.dropComplete(true);
} }
return;
} catch (Throwable t) {
t.printStackTrace();
showError(t);
} }
return;
} catch (Throwable t) {
t.printStackTrace();
showError(t);
} }
dtde.rejectDrop(); dtde.rejectDrop();
} }
@@ -543,14 +589,10 @@ public class Main extends JFrame {
if (line.startsWith("invoke")) { if (line.startsWith("invoke")) {
String[] split = line.split(" "); String[] split = line.split(" ");
String desc = UnicodeUtils.unescape(split[1]); String desc = UnicodeUtils.unescape(split[1]);
String className = UnicodeUtils.unescape(split[2]); String target = split[2];
String[] nameSplit = className.split("/"); int index = target.lastIndexOf("/");
String methodName = nameSplit[nameSplit.length - 1]; String className = UnicodeUtils.unescape(target.substring(0, index));
className = className.substring(0, className.length() - methodName.length() - 1); String methodName = UnicodeUtils.unescape(target.substring(index + 1));
while (className.endsWith("/")) {
methodName = "/" + methodName;
className = className.substring(0, className.length() - 1);
}
ClassNode classNode = classNodes.get(getFullName(className)); ClassNode classNode = classNodes.get(getFullName(className));
if (classNode == null) { if (classNode == null) {
return; return;
@@ -579,14 +621,10 @@ public class Main extends JFrame {
|| line.startsWith("putfield ")) { || line.startsWith("putfield ")) {
String[] split = line.split(" "); String[] split = line.split(" ");
String desc = UnicodeUtils.unescape(split[1]); String desc = UnicodeUtils.unescape(split[1]);
String className = UnicodeUtils.unescape(split[2]); String target = split[2];
String[] nameSplit = className.split("/"); int index = target.lastIndexOf("/");
String fieldName = nameSplit[nameSplit.length - 1]; String className = UnicodeUtils.unescape(target.substring(0, index));
className = className.substring(0, className.length() - fieldName.length() - 1); String fieldName = UnicodeUtils.unescape(target.substring(index + 1));
while (className.endsWith("/")) {
fieldName = "/" + fieldName;
className = className.substring(0, className.length() - 1);
}
ClassNode classNode = classNodes.get(getFullName(className)); ClassNode classNode = classNodes.get(getFullName(className));
if (classNode == null) { if (classNode == null) {
return; return;
@@ -909,9 +947,10 @@ public class Main extends JFrame {
} }
}); });
// Windows-like sorting (like file explorer)
List<String> tmp = new ArrayList<>(paths); List<String> tmp = new ArrayList<>(paths);
List<String> folders = tmp.stream().map(s -> { List<String> folders = tmp.stream().filter(s -> s.contains("/")).map(s -> {
int idx = s.lastIndexOf('/'); int idx = s.lastIndexOf('/');
if (idx == s.length() - 1) if (idx == s.length() - 1)
idx = s.lastIndexOf('/', idx - 1); idx = s.lastIndexOf('/', idx - 1);