Skip to content

Commit

Permalink
Add a couple more development options (#173)
Browse files Browse the repository at this point in the history
* Add another dev option

Show mapping source plugin

* Add dev option to debug token highlights

* Allow saving the printed mapping tree to a file

* Show dev options correctly when reopening enigma

* Add translations for the dev menu

* Remove non-breaking space from EntryTreePrinter

* Add translation for "source plugin"
  • Loading branch information
IotaBread authored Dec 30, 2023
1 parent 305e6b3 commit 03bea65
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public final class Config extends ReflectiveConfig {

public final StatsSection stats = new StatsSection();

@Comment("You shouldn't enable options in this section unless you know what you're doing")
public final DevSection development = new DevSection();

/**
* The look and feel stored in the config: do not use this unless setting! Use {@link #activeLookAndFeel} instead,
* since look and feel is final once loaded.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.quiltmc.enigma.gui.config;

import com.google.gson.annotations.SerializedName;
import org.quiltmc.config.api.ReflectiveConfig;
import org.quiltmc.config.api.annotations.Processor;
import org.quiltmc.config.api.values.TrackedValue;
import org.quiltmc.enigma.api.source.DecompiledClassSource;

public class DevSection extends ReflectiveConfig.Section {
@SerializedName("show_mapping_source_plugin")
public final TrackedValue<Boolean> showMappingSourcePlugin = this.value(false);

@SerializedName("debug_token_highlights")
@Processor("processDebugTokenHighlights")
public final TrackedValue<Boolean> debugTokenHighlights = this.value(false);

@SuppressWarnings("unused")
public void processDebugTokenHighlights(TrackedValue.Builder<Boolean> builder) {
builder.callback(trackedValue -> DecompiledClassSource.DEBUG_TOKEN_HIGHLIGHTS = trackedValue.value());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@
import org.quiltmc.enigma.util.Pair;
import org.quiltmc.enigma.util.validation.Message;
import org.quiltmc.enigma.util.validation.ParameterizedMessage;
import org.tinylog.Logger;

import javax.annotation.Nullable;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
Expand Down Expand Up @@ -103,6 +106,8 @@ public class MenuBar {

// Enabled with system property "enigma.development" or "--development" flag
private final JMenu devMenu = new JMenu();
private final JCheckBoxMenuItem showMappingSourcePluginItem = new JCheckBoxMenuItem();
private final JCheckBoxMenuItem debugTokenHighlightsItem = new JCheckBoxMenuItem();
private final JMenuItem printMappingTreeItem = new JMenuItem();

private final Gui gui;
Expand Down Expand Up @@ -174,6 +179,8 @@ public MenuBar(Gui gui) {
this.helpMenu.add(this.githubItem);
ui.add(this.helpMenu);

this.devMenu.add(this.showMappingSourcePluginItem);
this.devMenu.add(this.debugTokenHighlightsItem);
this.devMenu.add(this.printMappingTreeItem);
if (System.getProperty("enigma.development", "false").equalsIgnoreCase("true")) {
ui.add(this.devMenu);
Expand Down Expand Up @@ -207,6 +214,8 @@ public MenuBar(Gui gui) {
this.startServerItem.addActionListener(e -> this.onStartServerClicked());
this.aboutItem.addActionListener(e -> AboutDialog.show(this.gui.getFrame()));
this.githubItem.addActionListener(e -> this.onGithubClicked());
this.showMappingSourcePluginItem.addActionListener(e -> this.onShowMappingSourcePluginClicked());
this.debugTokenHighlightsItem.addActionListener(e -> this.onDebugTokenHighlightsClicked());
this.printMappingTreeItem.addActionListener(e -> this.onPrintMappingTreeClicked());
}

Expand Down Expand Up @@ -243,6 +252,9 @@ public void updateUiState() {
this.exportJarItem.setEnabled(jarOpen);
this.statsItem.setEnabled(jarOpen);
this.printMappingTreeItem.setEnabled(jarOpen);

this.showMappingSourcePluginItem.setState(Config.main().development.showMappingSourcePlugin.value());
this.debugTokenHighlightsItem.setState(Config.main().development.debugTokenHighlights.value());
}

public void retranslateUi() {
Expand Down Expand Up @@ -292,7 +304,9 @@ public void retranslateUi() {
this.githubItem.setText(I18n.translate("menu.help.github"));

this.devMenu.setText("Dev");
this.printMappingTreeItem.setText("Print mapping tree");
this.showMappingSourcePluginItem.setText(I18n.translate("dev.menu.show_mapping_source_plugin"));
this.debugTokenHighlightsItem.setText(I18n.translate("dev.menu.debug_token_highlights"));
this.printMappingTreeItem.setText(I18n.translate("dev.menu.print_mapping_tree"));
}

private void onOpenJarClicked() {
Expand Down Expand Up @@ -479,22 +493,51 @@ private void onGithubClicked() {
GuiUtil.openUrl("https://github.com/QuiltMC/Enigma");
}

private void onShowMappingSourcePluginClicked() {
var value = this.showMappingSourcePluginItem.getState();
Config.main().development.showMappingSourcePlugin.setValue(value, true);
}

private void onDebugTokenHighlightsClicked() {
var value = this.debugTokenHighlightsItem.getState();
Config.main().development.debugTokenHighlights.setValue(value, true);
}

private void onPrintMappingTreeClicked() {
var mappings = this.gui.getController().getProject().getRemapper().getMappings();

StringWriter text = new StringWriter();
var text = new StringWriter();
EntryTreePrinter.print(new PrintWriter(text), mappings);

var frame = new JFrame("Mapping Tree");
var frame = new JFrame(I18n.translate("dev.mapping_tree"));
var pane = frame.getContentPane();
pane.setLayout(new BorderLayout());

var textArea = new JTextArea(text.toString());
textArea.setFont(ScaleUtil.getFont(Font.MONOSPACED, Font.PLAIN, 12));
pane.add(new JScrollPane(textArea), BorderLayout.CENTER);
var button = new JButton("Close");
button.addActionListener(e -> frame.dispose());
pane.add(button, BorderLayout.SOUTH);

var buttonPane = new JPanel();

var saveButton = new JButton(I18n.translate("prompt.save"));
saveButton.addActionListener(e -> {
var chooser = new JFileChooser();
chooser.setSelectedFile(new File("mapping_tree.txt"));
if (chooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
try {
Files.writeString(chooser.getSelectedFile().toPath(), text.toString());
} catch (IOException ex) {
Logger.error(ex, "Failed to save the mapping tree");
}
}
});
buttonPane.add(saveButton);

var closeButton = new JButton(I18n.translate("prompt.ok"));
closeButton.addActionListener(e -> frame.dispose());
buttonPane.add(closeButton);

pane.add(buttonPane, BorderLayout.SOUTH);

frame.setSize(ScaleUtil.getDimension(1200, 400));
frame.setLocationRelativeTo(this.gui.getFrame());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.quiltmc.enigma.gui.EditableType;
import org.quiltmc.enigma.gui.Gui;
import org.quiltmc.enigma.gui.config.Config;
import org.quiltmc.enigma.gui.element.ConvertingTextField;
import org.quiltmc.enigma.gui.event.ConvertingTextFieldListener;
import org.quiltmc.enigma.gui.util.GridBagConstraintsBuilder;
Expand Down Expand Up @@ -150,6 +151,11 @@ public void refreshReference() {
} else {
throw new IllegalStateException("unreachable");
}

if (Config.main().development.showMappingSourcePlugin.value()) {
var mapping = this.gui.getController().getProject().getRemapper().getMapping(this.entry);
th.addStringRow(I18n.translate("dev.source_plugin"), mapping.sourcePluginId());
}
}

th.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.Map;

public class DecompiledClassSource {
protected static final boolean DEBUG_TOKEN_HIGHLIGHTS = false;
public static boolean DEBUG_TOKEN_HIGHLIGHTS = false;

private final ClassEntry classEntry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private <T> void printNode(EntryTreeNode<T> node, boolean hasNext, String indent
var iterator = node.getChildNodes().iterator();
while (iterator.hasNext()) {
var child = iterator.next();
var childIndent = indent + (hasNext ? "│\u00A0\u00A0 " : " "); // \u00A0 is non-breaking space
var childIndent = indent + (hasNext ? "│ " : " ");

this.printNode(child, iterator.hasNext(), childIndent);
}
Expand Down
8 changes: 7 additions & 1 deletion enigma/src/main/resources/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -343,5 +343,11 @@
"notification.type.info": "Info",
"notification.level.none": "No server notifications",
"notification.level.no_chat": "No chat messages",
"notification.level.full": "All server notifications"
"notification.level.full": "All server notifications",

"dev.menu.show_mapping_source_plugin": "Show mapping source plugin",
"dev.menu.debug_token_highlights": "Debug token highlights",
"dev.menu.print_mapping_tree": "Print mapping tree",
"dev.mapping_tree": "Mapping tree",
"dev.source_plugin": "Source plugin"
}

0 comments on commit 03bea65

Please sign in to comment.