Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Show Imports decompiler setting #344

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ public void onCloseJar() {

public EditorPanel openClass(ClassEntry entry) {
EditorPanel editorPanel = editors.computeIfAbsent(entry, e -> {
this.controller.getClassHandleProvider().setDecompilerRemoveImports(!UiConfig.shouldShowImports());
ClassHandle ch = controller.getClassHandleProvider().openClass(entry);
if (ch == null) return null;
EditorPanel ed = new EditorPanel(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ public void openStats(Set<StatsMember> includedMembers, String topLevelPackage,
public void setDecompiler(DecompilerService service) {
if (chp != null) {
chp.setDecompilerService(service);
chp.setDecompilerRemoveImports(!UiConfig.shouldShowImports());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public static void setDecompiler(Decompiler d) {
ui.data().section("Decompiler").setEnum("Current", d);
}

public static boolean shouldShowImports() {
return ui.data().section("Decompiler").setIfAbsentBool("Show Imports", false);
}

public static void setShowImports(boolean showImports) {
ui.data().section("Decompiler").setBool("Show Imports", showImports);
}

private static Color fromComponents(int rgb, double alpha) {
int rgba = rgb & 0xFFFFFF | (int) (alpha * 255) << 24;
return new Color(rgba, true);
Expand Down
10 changes: 10 additions & 0 deletions enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class MenuBar {
private final JMenuItem exitItem = new JMenuItem();

private final JMenu decompilerMenu = new JMenu();
private final JCheckBoxMenuItem decompilerShowImportsItem = new JCheckBoxMenuItem();

private final JMenu viewMenu = new JMenu();
private final JMenu themesMenu = new JMenu();
Expand Down Expand Up @@ -101,6 +102,9 @@ public MenuBar(Gui gui) {
this.fileMenu.add(this.exitItem);
this.ui.add(this.fileMenu);

this.decompilerMenu.addSeparator();
this.decompilerMenu.add(this.decompilerShowImportsItem);
this.decompilerShowImportsItem.setSelected(UiConfig.shouldShowImports());
this.ui.add(this.decompilerMenu);

this.viewMenu.add(this.themesMenu);
Expand Down Expand Up @@ -134,6 +138,7 @@ public MenuBar(Gui gui) {
this.exportJarItem.addActionListener(_e -> this.onExportJarClicked());
this.statsItem.addActionListener(_e -> StatsDialog.show(this.gui));
this.exitItem.addActionListener(_e -> this.gui.close());
this.decompilerShowImportsItem.addActionListener(_e -> this.onSourceSettingsClicked());
this.customScaleItem.addActionListener(_e -> this.onCustomScaleClicked());
this.fontItem.addActionListener(_e -> this.onFontClicked(this.gui));
this.searchItem.addActionListener(_e -> this.onSearchClicked());
Expand Down Expand Up @@ -181,6 +186,7 @@ public void retranslateUi() {
this.exitItem.setText(I18n.translate("menu.file.exit"));

this.decompilerMenu.setText(I18n.translate("menu.decompiler"));
this.decompilerShowImportsItem.setText(I18n.translate("menu.decompiler.show_imports"));

this.viewMenu.setText(I18n.translate("menu.view"));
this.themesMenu.setText(I18n.translate("menu.view.themes"));
Expand Down Expand Up @@ -268,6 +274,10 @@ private void onExportJarClicked() {
}
}

private void onSourceSettingsClicked() {
UiConfig.setShowImports(this.decompilerShowImportsItem.isSelected());
}

private void onCustomScaleClicked() {
String answer = (String) JOptionPane.showInputDialog(this.gui.getFrame(), I18n.translate("menu.view.scale.custom.title"), I18n.translate("menu.view.scale.custom.title"),
JOptionPane.QUESTION_MESSAGE, null, null, Float.toString(ScaleUtil.getScaleFactor() * 100));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ public DecompilerService getDecompilerService() {
return ds;
}

/**
* Sets the source settings of the decompiler to new source settings,
* with the provided {@code removeImports} setting.
*
* @param removeImports whether imports should be removed when decompiling
*/
public void setDecompilerRemoveImports(boolean removeImports) {
this.decompiler.setSourceSettings(new SourceSettings(removeImports, true));
}

private Decompiler createDecompiler() {
return ds.create(new CachingClassProvider(new ObfuscationFixClassProvider(project.getClassProvider(), project.getJarIndex())), new SourceSettings(true, true));
}
Expand Down
2 changes: 2 additions & 0 deletions enigma/src/main/java/cuchaz/enigma/source/Decompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ default Source getSource(String className) {
}

Source getSource(String className, @Nullable EntryRemapper remapper);

void setSourceSettings(SourceSettings settings);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public class CfrDecompiler implements Decompiler {
private final DCCommonState state;
// cfr doesn't add final on params so final setting is ignored
private final SourceSettings settings;
private SourceSettings settings;

public CfrDecompiler(ClassProvider classProvider, SourceSettings sourceSettings) {
Map<String, String> options = new HashMap<>();
Expand Down Expand Up @@ -100,4 +100,9 @@ public Source getSource(String className, @Nullable EntryRemapper mapper) {
tree.analyseTop(state, typeUsageCollector);
return new CfrSource(settings, tree, state, typeUsageCollector.getRealTypeUsageInformation(), options, mapper);
}

@Override
public void setSourceSettings(SourceSettings settings) {
this.settings = settings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.objectweb.asm.tree.ClassNode;

public class ProcyonDecompiler implements Decompiler {
private final SourceSettings settings;
private SourceSettings settings;
private final DecompilerSettings decompilerSettings;
private final MetadataSystem metadataSystem;

Expand Down Expand Up @@ -94,6 +94,11 @@ public Source getSource(String className, @Nullable EntryRemapper remapper) {
return new ProcyonSource(source, decompilerSettings);
}

@Override
public void setSourceSettings(SourceSettings settings) {
this.settings = settings;
}

private static boolean getSystemPropertyAsBoolean(String property, boolean defValue) {
String value = System.getProperty(property);
return value == null ? defValue : Boolean.parseBoolean(value);
Expand Down
1 change: 1 addition & 0 deletions enigma/src/main/resources/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"menu.file.stats.generate": "Generate Diagram",
"menu.file.exit": "Exit",
"menu.decompiler": "Decompiler",
"menu.decompiler.show_imports": "Show imports",
"menu.view": "View",
"menu.view.themes": "Themes",
"menu.view.themes.default": "Default",
Expand Down
1 change: 1 addition & 0 deletions enigma/src/main/resources/lang/fr_fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"menu.file.stats.generate": "Générer le diagramme",
"menu.file.exit": "Quitter",
"menu.decompiler": "Décompilateur",
"menu.decompiler.show_imports": "Afficher les importations",
"menu.view": "Affichage",
"menu.view.themes": "Thèmes",
"menu.view.themes.default": "Par défaut",
Expand Down