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
+64 -25
+64 -25
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,18 +257,36 @@ 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
synchronized (treeLock) {
if (isChangingFile) {
return;
}
try { try {
isChangingFile = true; isChangingFile = true;
Main.this.setTitle("ByteEdit - Reloading...");
new Thread(new Runnable() {
public void run() {
try { try {
tree.setModel(new ArchiveTreeModel(new JarFile(jarFile))); ArchiveTreeModel model = new ArchiveTreeModel(new JarFile(jarFile));
} catch (IOException e1) { EventQueue.invokeLater(new Runnable() {
e1.printStackTrace(); public void run() {
} synchronized (treeLock) {
tree.setModel(model);
isChangingFile = false; isChangingFile = false;
Main.this.setTitle("ByteEdit");
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
showError(t); showError(t);
} }
}
} else if (e.getKeyCode() == 127) { // del } else if (e.getKeyCode() == 127) { // del
if (tree.getSelectionPath() == null) if (tree.getSelectionPath() == null)
return; return;
@@ -310,15 +330,22 @@ public class Main extends JFrame {
tree.addTreeSelectionListener(new TreeSelectionListener() { tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) { public void valueChanged(TreeSelectionEvent e) {
synchronized (treeLock) {
if (!isChangingFile) { if (!isChangingFile) {
selectFile(((ByteEditTreeNode) e.getPath().getLastPathComponent()).path); selectFile(((ByteEditTreeNode) e.getPath().getLastPathComponent()).path);
} }
} }
}
}); });
new DropTarget(tree, new DropTargetListener() { new DropTarget(tree, new DropTargetListener() {
@Override @Override
public void drop(DropTargetDropEvent dtde) { public void drop(DropTargetDropEvent dtde) {
synchronized (treeLock) {
if (isChangingFile) {
dtde.rejectDrop();
return;
}
try { try {
Transferable tr = dtde.getTransferable(); Transferable tr = dtde.getTransferable();
DataFlavor[] flavors = tr.getTransferDataFlavors(); DataFlavor[] flavors = tr.getTransferDataFlavors();
@@ -332,10 +359,18 @@ public class Main extends JFrame {
saveFolder = file.getParentFile(); saveFolder = file.getParentFile();
isChangingFile = true; isChangingFile = true;
try { try {
tree.setModel(new ArchiveTreeModel(new JarFile(jarFile))); Main.this.setTitle("ByteEdit - Loading '" + jarFile.getCanonicalPath() + "'");
} catch (IOException e1) { } catch (IOException ex) {
e1.printStackTrace(); 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(""); txtByteEditView.setText("");
currentNodeName = null; currentNodeName = null;
globalSearchBox.setVisible(false); globalSearchBox.setVisible(false);
@@ -346,8 +381,18 @@ public class Main extends JFrame {
renameBox.setVisible(false); renameBox.setVisible(false);
compilationBox.setVisible(false); compilationBox.setVisible(false);
isChangingFile = false; isChangingFile = false;
Main.this.setTitle("ByteEdit");
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} }
dtde.dropComplete(true); dtde.dropComplete(true);
break;
} }
} }
return; return;
@@ -355,6 +400,7 @@ public class Main extends JFrame {
t.printStackTrace(); t.printStackTrace();
showError(t); 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);