Skip to content

Commit

Permalink
Add manifest support
Browse files Browse the repository at this point in the history
  • Loading branch information
sotasan committed Oct 18, 2023
1 parent 54b7273 commit 9df70f7
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sotasan.decompiler.controllers;

import com.sotasan.decompiler.models.FileModel;
import com.sotasan.decompiler.types.ClassType;
import com.sotasan.decompiler.views.TabView;
import lombok.Getter;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
Expand All @@ -21,8 +22,8 @@ public TabController(@NotNull FileModel fileModel) {
public void update() {
try {
getView().getTextArea().setText(getText(fileModel));
if (fileModel.isClass())
getView().getTextArea().setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
if (fileModel.getType() != null)
getView().getTextArea().setSyntaxEditingStyle(fileModel.getType().getSyntax());
} catch (Exception e) {
getView().getTextArea().setText(e.getMessage());
getView().getTextArea().setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);
Expand All @@ -31,7 +32,7 @@ public void update() {
}

private String getText(@NotNull FileModel fileModel) throws Exception {
return fileModel.isClass()
return fileModel.getType() instanceof ClassType
? TabsController.getINSTANCE().getTransformer().getInstance().transform(fileModel)
: new String(fileModel.getBytes());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.sotasan.decompiler.models.FileModel;
import com.sotasan.decompiler.transformers.Transformer;
import com.sotasan.decompiler.types.ClassType;
import com.sotasan.decompiler.views.TabsView;
import com.sotasan.decompiler.views.TabView;
import lombok.Getter;
Expand All @@ -23,7 +24,7 @@ private TabsController() {
public void actionPerformed(ActionEvent event) {
for (int i = 0; i < getView().getTabCount(); i++) {
TabController controller = ((TabView) getView().getComponentAt(i)).getController();
if (controller.getFileModel().isClass())
if (controller.getFileModel().getType() instanceof ClassType)
controller.update();
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/sotasan/decompiler/models/FileModel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sotasan.decompiler.models;

import com.sotasan.decompiler.services.TypeService;
import com.sotasan.decompiler.types.Type;
import lombok.Getter;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
Expand All @@ -11,16 +13,14 @@ public class FileModel extends BaseModel {

private final JarFile jarFile;
private final JarEntry jarEntry;
private final Type type;

public FileModel(JarFile jarFile, @NotNull JarEntry jarEntry) {
super(jarEntry.getName(), false);
this.jarFile = jarFile;
this.jarEntry = jarEntry;
setIcon(isClass() ? "icons/class.png" : "icons/file.png");
}

public boolean isClass() {
return getName().toLowerCase().endsWith(".class");
type = TypeService.getType(this);
setIcon(type != null ? type.getIcon() : "icons/file.png");
}

@SneakyThrows
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/sotasan/decompiler/services/TypeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.sotasan.decompiler.services;

import com.sotasan.decompiler.models.FileModel;
import com.sotasan.decompiler.types.ClassType;
import com.sotasan.decompiler.types.ManifestType;
import com.sotasan.decompiler.types.Type;
import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.Nullable;
import java.util.List;

@UtilityClass
public class TypeService {

private static final List<Type> TYPES = List.of(
new ClassType(),
new ManifestType()
);

public static @Nullable Type getType(FileModel fileModel) {
for (Type format : TYPES)
if (format.isFormat(fileModel))
return format;
return null;
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/sotasan/decompiler/types/ClassType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.sotasan.decompiler.types;

import com.sotasan.decompiler.models.FileModel;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.jetbrains.annotations.NotNull;

public class ClassType extends Type {

public ClassType() {
super("icons/class.png", SyntaxConstants.SYNTAX_STYLE_JAVA);
}

@Override
public boolean isFormat(@NotNull FileModel fileModel) {
return fileModel.getName().toLowerCase().endsWith(".class");
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/sotasan/decompiler/types/ManifestType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.sotasan.decompiler.types;

import com.sotasan.decompiler.models.FileModel;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.jetbrains.annotations.NotNull;

public class ManifestType extends Type {

public ManifestType() {
super("icons/manifest.png", SyntaxConstants.SYNTAX_STYLE_PROPERTIES_FILE);
}

@Override
public boolean isFormat(@NotNull FileModel fileModel) {
return fileModel.getName().toLowerCase().endsWith(".mf");
}

}
19 changes: 19 additions & 0 deletions src/main/java/com/sotasan/decompiler/types/Type.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.sotasan.decompiler.types;

import com.sotasan.decompiler.models.FileModel;
import lombok.Getter;

@Getter
public abstract class Type {

private final String icon;
private final String syntax;

public Type(String icon, String syntax) {
this.icon = icon;
this.syntax = syntax;
}

public abstract boolean isFormat(FileModel fileModel);

}

0 comments on commit 9df70f7

Please sign in to comment.