Skip to content

Commit

Permalink
Merge pull request #5 from apo-tek/master
Browse files Browse the repository at this point in the history
Multiple changes
  • Loading branch information
sotasan authored Sep 30, 2023
2 parents 14089e1 + 2fab5f0 commit b226049
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 44 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Java Decompiler Gui

## Languages

- [Dutch](https://github.com/sotasan/decompiler/blob/master/src/main/resources/langs/language_nl.properties)
- [English](https://github.com/sotasan/decompiler/blob/master/src/main/resources/langs/language.properties)
- [French](https://github.com/sotasan/decompiler/blob/master/src/main/resources/langs/language_fr.properties)
- [German](https://github.com/sotasan/decompiler/blob/master/src/main/resources/langs/language_de.properties)
- [Japanese](https://github.com/sotasan/decompiler/blob/master/src/main/resources/langs/language_ja.properties)
- [Russian](https://github.com/sotasan/decompiler/blob/master/src/main/resources/langs/language_ru.properties)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.sotasan"
version = "0.8.10"
version = "0.9.0"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,36 @@
import com.sotasan.decompiler.models.FileModel;
import com.sotasan.decompiler.views.TabView;
import lombok.Getter;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.jetbrains.annotations.NotNull;

@Getter
public class TabController extends BaseController<TabView> {

private final FileModel fileModel;

public TabController(FileModel fileModel) {
public TabController(@NotNull FileModel fileModel) {
super(new TabView());
this.fileModel = fileModel;
if (fileModel.isClass())
getView().getTextArea().setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
getView().setController(this);
update();
}

public void update() {
try {
getView().getTextArea().setText(TabsController.getINSTANCE().getTransformer().getInstance().transform(fileModel));
getView().getTextArea().setText(getText(fileModel));
getView().getTextArea().setCaretPosition(0);
} catch (Exception e) {
getView().getTextArea().setText(e.getMessage());
}
}

private String getText(@NotNull FileModel fileModel) throws Exception {
return fileModel.isClass()
? TabsController.getINSTANCE().getTransformer().getInstance().transform(fileModel)
: new String(fileModel.getBytes());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ private TabsController() {

@Override
public void actionPerformed(ActionEvent event) {
for (int i = 0; i < getView().getTabCount(); i++)
((TabView) getView().getComponentAt(i)).getController().update();
for (int i = 0; i < getView().getTabCount(); i++) {
TabController controller = ((TabView) getView().getComponentAt(i)).getController();
if (controller.getFileModel().isClass())
controller.update();
}
}

public void addTab(@NotNull FileModel fileModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.sotasan.decompiler.models.ArchiveModel;
import com.sotasan.decompiler.models.BaseModel;
import com.sotasan.decompiler.models.FileModel;
import com.sotasan.decompiler.models.PackageModel;
import com.sotasan.decompiler.views.TreeView;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.util.Collections;
import java.util.List;

public class TreeController extends BaseController<TreeView> {

Expand All @@ -29,15 +29,12 @@ public void setArchive(ArchiveModel archiveModel) {
getView().getTree().expandPath(new TreePath(treeNode.getPath()));
}

// TODO: Update sorting (+ Alphabetic, Inside Loader)
private @NotNull DefaultMutableTreeNode createTreeNode(BaseModel baseModel) {
DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(baseModel);
for (BaseModel child : baseModel.getChildren())
if (child instanceof PackageModel)
treeNode.add(createTreeNode(child));
for (BaseModel child : baseModel.getChildren())
if (child instanceof FileModel)
treeNode.add(createTreeNode(child));
List<BaseModel> children = baseModel.getChildren();
Collections.sort(children);
for (BaseModel child : children)
treeNode.add(createTreeNode(child));
return treeNode;
}

Expand Down
31 changes: 18 additions & 13 deletions src/main/java/com/sotasan/decompiler/models/BaseModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,39 @@

import lombok.Getter;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public abstract class BaseModel {
@Getter
public abstract class BaseModel implements Comparable<BaseModel> {

@Getter private final List<BaseModel> children = new ArrayList<>();
@Getter private final String path;
@Getter private String name;
@Getter private Image icon;
private final List<BaseModel> children = new ArrayList<>();
private final String path;
private String name;
private Image icon;

public BaseModel(String path, boolean directory) {
this.path = path;
name = path;

if (directory)
name = path.substring(0, path.length() - 1);
name = directory ? path.substring(0, path.length() - 1) : path;
name = name.substring(name.lastIndexOf('/') + 1);
}

public boolean isClass() {
return name.toLowerCase().endsWith(".class");
}

@SneakyThrows
public void setIcon(String path) {
icon = Toolkit.getDefaultToolkit().createImage(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream(path)).readAllBytes());
}

@Override
public int compareTo(@NotNull BaseModel baseModel) {
Class<? extends BaseModel> class1 = getClass();
Class<? extends BaseModel> class2 = baseModel.getClass();
return class1.equals(class2) ? getName().compareToIgnoreCase(baseModel.getName())
: class1.equals(FileModel.class) ? 1
: class2.equals(FileModel.class) ? -1
: 0;
}

}
9 changes: 7 additions & 2 deletions src/main/java/com/sotasan/decompiler/models/FileModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

@Getter
public class FileModel extends BaseModel {

@Getter private final JarFile jarFile;
@Getter private final JarEntry jarEntry;
private final JarFile jarFile;
private final JarEntry jarEntry;

public FileModel(JarFile jarFile, @NotNull JarEntry jarEntry) {
super(jarEntry.getName(), false);
Expand All @@ -18,6 +19,10 @@ public FileModel(JarFile jarFile, @NotNull JarEntry jarEntry) {
setIcon(isClass() ? "icons/class.png" : "icons/file.png");
}

public boolean isClass() {
return getName().toLowerCase().endsWith(".class");
}

@SneakyThrows
public byte[] getBytes() {
return jarFile.getInputStream(jarEntry).readAllBytes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@
import org.benf.cfr.reader.api.ClassFileSource;
import org.benf.cfr.reader.api.OutputSinkFactory;
import org.benf.cfr.reader.bytecode.analysis.parse.utils.Pair;
import org.benf.cfr.reader.util.getopt.OptionsImpl;
import org.jetbrains.annotations.NotNull;
import java.util.*;

public class CFRTransformer implements ITransformer, ClassFileSource, OutputSinkFactory, OutputSinkFactory.Sink<String> {

private static final Map<String, String> OPTIONS = Map.of(
OptionsImpl.DECOMPILE_INNER_CLASSES.getName(), String.valueOf(false),
OptionsImpl.RELINK_CONSTANT_STRINGS.getName(), String.valueOf(false),
OptionsImpl.REMOVE_INNER_CLASS_SYNTHETICS.getName(), String.valueOf(false)
);

private FileModel fileModel;
private String output;

@Override
public String transform(@NotNull FileModel fileModel) {
this.fileModel = fileModel;
CfrDriver driver = new CfrDriver.Builder().withClassFileSource(this).withOutputSink(this).build();
CfrDriver driver = new CfrDriver.Builder().withClassFileSource(this).withOptions(OPTIONS).withOutputSink(this).build();
driver.analyse(Collections.singletonList(fileModel.getPath()));
if (output.startsWith("/"))
output = output.substring(37);
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/sotasan/decompiler/views/TabView.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lombok.Setter;
import lombok.SneakyThrows;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rsyntaxtextarea.Theme;
import org.fife.ui.rtextarea.RTextScrollPane;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -35,7 +34,6 @@ public TabView() {
textArea.setDropTarget(null);
textArea.setEditable(false);
textArea.setHighlightCurrentLine(false);
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);

scrollPane = new RTextScrollPane(textArea);
theme.apply(textArea);
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/com/sotasan/decompiler/views/TreeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,20 @@ private static class TreeMouseAdapter extends MouseAdapter {

@Override
public void mouseClicked(@NotNull MouseEvent event) {
if (event.getClickCount() == 2) {
TreePath path = ((JTree) event.getSource()).getSelectionPath();
if (path != null) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
if (node.getUserObject() != null) {
BaseModel model = (BaseModel) node.getUserObject();
if (model instanceof FileModel && model.isClass()) {
TabsController.getINSTANCE().addTab((FileModel) model);
}
}
}
}
if (event.getClickCount() != 2)
return;

TreePath path = ((JTree) event.getSource()).getSelectionPath();
if (path == null)
return;

DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
if (node.getUserObject() == null)
return;

BaseModel model = (BaseModel) node.getUserObject();
if (model instanceof FileModel)
TabsController.getINSTANCE().addTab((FileModel) model);
}

}
Expand Down
14 changes: 14 additions & 0 deletions src/main/resources/langs/language_fr.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
file=Fichier
file.openFile=Ouvrir un fichier
file.newInstance=Nouvelle instance
file.exit=Quitter

help=Aide

empty=Aucun fichier ouvert
empty.drag=Glisser-déposer pour ouvrir

about=À propos
about.version=Version %s
about.vm=Machine Virtuelle
about.ok=OK
14 changes: 14 additions & 0 deletions src/main/resources/langs/language_nl.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
file=Bestand
file.openFile=Bestand openen
file.newInstance=Nieuwe instantie
file.exit=Verlaten

help=Hulp

empty=Geen opende bestand
empty.drag=Slepen en neerzetten om te openen

about=Over
about.version=Versie %s
about.vm=Virtuele Machine
about.ok=OK

0 comments on commit b226049

Please sign in to comment.