Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SuspiciousActivity committed Mar 9, 2021
1 parent ea282d2 commit c2e22e5
Show file tree
Hide file tree
Showing 7 changed files with 400 additions and 346 deletions.
2 changes: 1 addition & 1 deletion src/me/ByteEdit/boxes/CompilationBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void actionPerformed(ActionEvent e) {
}, ctrlS, JComponent.WHEN_FOCUSED);
}

class JavaSourceFromString extends SimpleJavaFileObject {
private static class JavaSourceFromString extends SimpleJavaFileObject {
final String code;

public JavaSourceFromString(String name, String code) {
Expand Down
5 changes: 3 additions & 2 deletions src/me/ByteEdit/decompiler/FernflowerDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.jar.Manifest;

import org.jetbrains.java.decompiler.main.Fernflower;
Expand Down Expand Up @@ -60,8 +61,8 @@ public String doDecompilation(ClassNode cn, byte[] b, MethodNode mn) {
// TODO decompile method only
this.bytes = b;
HashMap<String, Object> map = new HashMap<>();
for (String key : options.keySet()) {
map.put(key, options.get(key) ? "1" : "0");
for (Entry<String, Boolean> entry : options.entrySet()) {
map.put(entry.getKey(), entry.getValue() ? "1" : "0");
}
Fernflower f = new Fernflower(this, this, map, new IFernflowerLogger() {
@Override
Expand Down
36 changes: 17 additions & 19 deletions src/me/ByteEdit/edit/Assembler.java
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,13 @@ public static ClassNode assemble(String input) {
}
return clazz;
} catch (Throwable e) {
System.err.println("Error at line: " + read.currentLine);
try {
Main.txtByteEditView.setCaretPosition(Main.txtByteEditView.getLineStartOffset(read.currentLine));
} catch (BadLocationException e1) {
System.err.println("Can't show line!");
if (read != null) {
System.err.println("Error at line: " + read.currentLine);
try {
Main.txtByteEditView.setCaretPosition(Main.txtByteEditView.getLineStartOffset(read.currentLine));
} catch (BadLocationException e1) {
System.err.println("Can't show line!");
}
}
Main.showError(e.getMessage());
e.printStackTrace();
Expand Down Expand Up @@ -599,26 +601,28 @@ private static String consolidateStrings(String[] args, int start) {
if (args.length == 0) {
return null;
}
String ret = args[start];
StringBuilder sb = new StringBuilder(args[start]);
if (args.length > start + 1) {
for (int i = start + 1; i < args.length; i++) {
ret = ret + " " + args[i];
sb.append(' ');
sb.append(args[i]);
}
}
return ret;
return sb.toString();
}

private static String consolidateStrings(String[] args, int start, int end) {
if (end == 0) {
return null;
}
String ret = args[start];
StringBuilder sb = new StringBuilder(args[start]);
if (end > start + 1) {
for (int i = start + 1; i < end; i++) {
ret = ret + " " + args[i];
sb.append(' ');
sb.append(args[i]);
}
}
return ret;
return sb.toString();
}

private static Object getValue(HugeStringsRev hsr, String s, String to) {
Expand Down Expand Up @@ -1567,7 +1571,7 @@ private static Object[] parseFrameList(HugeStringsRev hsr, String str, HashMap<L
if (!asd.startsWith("(")) {
int frameType = ClassUtil.getFrameTypeByName(asd);
if (frameType != -1)
list.add(new Integer(frameType));
list.add(Integer.valueOf(frameType));
else
list.add(UnicodeUtils.unescape(hsr, asd.substring(1, asd.length() - 1)));
} else {
Expand All @@ -1590,13 +1594,7 @@ private static Object[] parseFrameList(HugeStringsRev hsr, String str, HashMap<L
}
}
}
Object[] arr = new Object[list.size()];
int c = 0;
for (Object o : list) {
arr[c] = o;
c++;
}
return arr;
return list.toArray(new Object[list.size()]);
}
}

Expand Down
198 changes: 198 additions & 0 deletions src/me/ByteEdit/main/ArchiveTreeModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package me.ByteEdit.main;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.swing.tree.DefaultTreeModel;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;

public class ArchiveTreeModel extends DefaultTreeModel {

public final ArrayList<String> paths = new ArrayList<>();
private final HashMap<String, ClassNode> classNodes;
private final HashMap<String, byte[]> otherFiles;
private final ZipFile jar;

public boolean newCreated = false;

public ArchiveTreeModel(HashMap<String, ClassNode> classNodes, HashMap<String, byte[]> otherFiles) {
super(new ByteEditTreeNode("New"));
this.newCreated = true;
this.jar = null;
this.classNodes = classNodes;
this.otherFiles = otherFiles;
}

public ArchiveTreeModel(ZipFile jar, HashMap<String, ClassNode> classNodes, HashMap<String, byte[]> otherFiles) {
super(new ByteEditTreeNode(jar.getName().split(File.separator.equals("\\") ? "\\\\"
: File.separator)[jar.getName().split(File.separator.equals("\\") ? "\\\\" : File.separator).length
- 1]));
this.newCreated = true;
this.jar = jar;
this.classNodes = classNodes;
this.otherFiles = otherFiles;
initialize();
}

private void initialize() {
try {
classNodes.clear();
otherFiles.clear();
Enumeration<? extends ZipEntry> enumeration = jar.entries();
while (enumeration.hasMoreElements()) {
ZipEntry next = enumeration.nextElement();
byte[] data = toByteArray(jar.getInputStream(next));
if (next.getSize() != 0 && !next.getName().startsWith("META-INF")
&& (next.getName().endsWith(".class") || next.getName().endsWith(".class/"))) {
try {
ClassReader reader = new ClassReader(data);
ClassNode node = new ClassNode();
reader.accept(node, 0);
classNodes.put(Main.getFullName(node.name), node);

if ((next.getName().contains("/")
? (!Main.patternSlash
.split(next.getName())[Main.patternSlash.split(next.getName()).length - 1]
.contains("$"))
: (!next.getName().contains("$")))
|| (next.getName().startsWith("$") || next.getName().contains("$$")
|| next.getName().endsWith("$"))) {
paths.add(Main.getFullName(node.name));
}
} catch (Exception e) {
otherFiles.put(next.getName(), data);
}
} else {
otherFiles.put(next.getName(), data);
}
}
jar.close();
refresh();
} catch (Throwable e) {
classNodes.clear();
otherFiles.clear();
e.printStackTrace();
Main.showError(e);
}
}

private int getSlashCount(String s) {
int count = 0;
for (char c : s.toCharArray()) {
if (c == '/')
count++;
}
return count;
}

public void refresh() {
setRoot(new ByteEditTreeNode(((ByteEditTreeNode) getRoot()).toString()));
Collections.sort(paths, String.CASE_INSENSITIVE_ORDER);
Collections.sort(paths, new Comparator<String>() {
public int compare(String s1, String s2) {
return getSlashCount(s2) - getSlashCount(s1);
}
});

// Windows-like sorting (like file explorer)
List<String> tmp = new ArrayList<>(paths);

List<String> folders = tmp.stream().filter(s -> {
int c = countSlashes(s, 50);
return c > 0 && c < 50;
}).map(s -> {
int idx = s.lastIndexOf('/');
if (idx == s.length() - 1)
idx = s.lastIndexOf('/', idx - 1);
if (idx == -1)
return s;
return s.substring(0, idx);
}).distinct().sorted(new Comparator<String>() {
@Override
public int compare(String s, String s2) {
// From String#compareToIgnoreCase(String)
int n = s.length();
int n2 = s2.length();
int n3 = Math.min(n, n2);
for (int i = 0; i < n3; ++i) {
char c;
char c2 = s.charAt(i);
if (c2 == (c = s2.charAt(i)) || (c2 = Character.toUpperCase(c2)) == (c = Character.toUpperCase(c))
|| (c2 = Character.toLowerCase(c2)) == (c = Character.toLowerCase(c)))
continue;
return c2 - c;
}
// Other way around
return n2 - n;
}
}).collect(Collectors.toList());

tmp.removeAll(folders);
tmp.addAll(0, folders);

for (String s : tmp) {
String[] elements = Main.patternSlash.split(s, 50);
if (elements.length >= 50) {
((ByteEditTreeNode) getRoot()).add(new ByteEditTreeNode(s, s));
} else {
ByteEditTreeNode currentNode = (ByteEditTreeNode) getRoot();
for (int i = 0; i < elements.length; i++) {
String token = elements[i];
ByteEditTreeNode nextNode = findNode(currentNode, token);
if (nextNode == null) {
nextNode = new ByteEditTreeNode(token, s);
currentNode.add(nextNode);
}
currentNode = nextNode;
}
}
}
}

private static ByteEditTreeNode findNode(ByteEditTreeNode parent, String name) {
Enumeration<?> e = parent.children();
while (e.hasMoreElements()) {
ByteEditTreeNode element = (ByteEditTreeNode) e.nextElement();
if (element.getUserObject().equals(name)) {
return element;
}
}
return null;
}

private static int countSlashes(String s, int max) {
int count = 0;
for (char c : s.toCharArray()) {
if (c == '/') {
count++;
if (count == max)
break;
}
}
return count;
}

private static byte[] toByteArray(final InputStream input) throws IOException {
try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
byte[] buffer = new byte[4096];
int read;
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
return output.toByteArray();
}
}
}
34 changes: 34 additions & 0 deletions src/me/ByteEdit/main/FurtherExpandingTreeExpansionListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.ByteEdit.main;

import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class FurtherExpandingTreeExpansionListener implements TreeExpansionListener {

private final JTree tree;

public FurtherExpandingTreeExpansionListener(JTree tree) {
this.tree = tree;
}

public void treeExpanded(TreeExpansionEvent event) {
TreePath treePath = event.getPath();
Object expandedTreePathObject = treePath.getLastPathComponent();
if (!(expandedTreePathObject instanceof TreeNode))
return;
TreeNode expandedTreeNode = (TreeNode) expandedTreePathObject;
if (expandedTreeNode.getChildCount() == 1) {
TreeNode descendantTreeNode = expandedTreeNode.getChildAt(0);
if (descendantTreeNode.isLeaf())
return;
TreePath nextTreePath = treePath.pathByAddingChild(descendantTreeNode);
tree.expandPath(nextTreePath);
}
}

public void treeCollapsed(TreeExpansionEvent event) {
}
}
Loading

0 comments on commit c2e22e5

Please sign in to comment.