From 0d32db6f7b637bbc840a4ede25c88025cdfef772 Mon Sep 17 00:00:00 2001 From: xp Date: Tue, 15 Mar 2022 14:09:57 -0700 Subject: [PATCH 1/4] Beginning --- .../src/main/java/gg/xp/xivsupport/gui/tabs/LibraryTab.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/LibraryTab.java b/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/LibraryTab.java index 277137afc447..f988bd8210e3 100644 --- a/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/LibraryTab.java +++ b/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/LibraryTab.java @@ -14,9 +14,6 @@ public LibraryTab() { } { addTab("Actions/Abilities", ActionTable.table()); - } } - - } From d5c00f3082130475dbf0e7536725f5adb7232a0a Mon Sep 17 00:00:00 2001 From: xp Date: Thu, 17 Mar 2022 19:32:17 -0700 Subject: [PATCH 2/4] Basic groovy console support --- .../java/gg/xp/xivsupport/gui/GuiMain.java | 2 + .../tables/filters/FreeformEventFilter.java | 71 ------ .../gg/xp/xivsupport/gui/tabs/GroovyTab.java | 236 ++++++++++++++++++ 3 files changed, 238 insertions(+), 71 deletions(-) delete mode 100644 xivsupport/src/main/java/gg/xp/xivsupport/gui/tables/filters/FreeformEventFilter.java create mode 100644 xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/GroovyTab.java diff --git a/xivsupport/src/main/java/gg/xp/xivsupport/gui/GuiMain.java b/xivsupport/src/main/java/gg/xp/xivsupport/gui/GuiMain.java index f4c90e3333e3..f88d0e274654 100644 --- a/xivsupport/src/main/java/gg/xp/xivsupport/gui/GuiMain.java +++ b/xivsupport/src/main/java/gg/xp/xivsupport/gui/GuiMain.java @@ -50,6 +50,7 @@ import gg.xp.xivsupport.gui.tables.renderers.ActionAndStatusRenderer; import gg.xp.xivsupport.gui.tables.renderers.NameJobRenderer; import gg.xp.xivsupport.gui.tabs.AdvancedTab; +import gg.xp.xivsupport.gui.tabs.GroovyTab; import gg.xp.xivsupport.gui.tabs.LibraryTab; import gg.xp.xivsupport.gui.tabs.SmartTabbedPane; import gg.xp.xivsupport.gui.util.CatchFatalError; @@ -154,6 +155,7 @@ public GuiMain(EventMaster master, MutablePicoContainer container) { SwingUtilities.invokeLater(() -> tabPane.addTab("Overlays", getOverlayConfigTab())); SwingUtilities.invokeLater(() -> tabPane.addTab("Map", container.getComponent(MapTab.class))); SwingUtilities.invokeLater(() -> tabPane.addTab("Library", new LibraryTab())); + SwingUtilities.invokeLater(() -> tabPane.addTab("Groovy", new GroovyTab(container))); SwingUtilities.invokeLater(() -> tabPane.addTab("Advanced", new AdvancedTab(container))); } diff --git a/xivsupport/src/main/java/gg/xp/xivsupport/gui/tables/filters/FreeformEventFilter.java b/xivsupport/src/main/java/gg/xp/xivsupport/gui/tables/filters/FreeformEventFilter.java deleted file mode 100644 index d3b828888ec9..000000000000 --- a/xivsupport/src/main/java/gg/xp/xivsupport/gui/tables/filters/FreeformEventFilter.java +++ /dev/null @@ -1,71 +0,0 @@ -package gg.xp.xivsupport.gui.tables.filters; - -import gg.xp.reevent.events.Event; -import groovy.lang.Closure; -import groovy.lang.GroovyShell; -import org.jetbrains.annotations.Nullable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.swing.*; -import java.awt.*; - -public class FreeformEventFilter implements VisualFilter { - - private static final Logger log = LoggerFactory.getLogger(FreeformEventFilter.class); - private final GroovyShell shell = new GroovyShell(); - private final TextFieldWithValidation textBox; - private volatile @Nullable Closure filterScript; - private final Runnable filterUpdatedCallback; - - public FreeformEventFilter(Runnable filterUpdatedCallback) { - this.filterUpdatedCallback = filterUpdatedCallback; - this.textBox = new TextFieldWithValidation<>(this::makeFilter, this::setFilter, ""); - } - - private @Nullable Closure makeFilter(@Nullable String filterText) { - if (filterText == null || filterText.isBlank()) { - return null; - } - try { - return (Closure) shell.evaluate(" { event -> " + filterText + "}"); - } - catch (Throwable t) { - textBox.setToolTipText(t.getMessage()); - throw t; - } - } - - private void setFilter(@Nullable Closure filter) { - filterScript = filter; - filterUpdatedCallback.run(); - } - - @Override - public boolean passesFilter(Event item) { - Closure filterScript = this.filterScript; - if (filterScript == null) { - return true; - } - shell.setVariable("event", item); - boolean result; - try { - result = filterScript.call(item); - } - catch (Throwable t) { - return false; - } - return result; - } - - @Override - public Component getComponent() { - JPanel panel = new JPanel(); - panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); - JLabel label = new JLabel("Groovy: "); - label.setLabelFor(textBox); - panel.add(label); - panel.add(textBox); - return panel; - } -} diff --git a/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/GroovyTab.java b/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/GroovyTab.java new file mode 100644 index 000000000000..b72ab5f3f1c9 --- /dev/null +++ b/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/GroovyTab.java @@ -0,0 +1,236 @@ +package gg.xp.xivsupport.gui.tabs; + +import gg.xp.reevent.events.Event; +import gg.xp.xivsupport.events.state.XivState; +import gg.xp.xivsupport.gui.WrapLayout; +import gg.xp.xivsupport.gui.components.ReadOnlyText; +import gg.xp.xivsupport.gui.tables.CustomColumn; +import gg.xp.xivsupport.gui.tables.CustomTableModel; +import groovy.lang.Binding; +import groovy.lang.GroovyShell; +import groovy.transform.CompileStatic; +import groovy.transform.TypeChecked; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.codehaus.groovy.control.CompilerConfiguration; +import org.codehaus.groovy.control.customizers.ImportCustomizer; +import org.picocontainer.PicoContainer; +import org.reflections.Reflections; +import org.reflections.scanners.Scanners; +import org.reflections.util.ClasspathHelper; +import org.reflections.util.ConfigurationBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.swing.*; +import javax.swing.border.TitledBorder; +import java.awt.*; +import java.awt.event.InputEvent; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.function.Predicate; + +import static org.reflections.scanners.Scanners.SubTypes; + +public class GroovyTab extends JPanel { + + private static final Logger log = LoggerFactory.getLogger(GroovyTab.class); + private static final Color invalidBackground = new Color(62, 27, 27); + // TODO: way of cancelling computation + private static final ExecutorService evaluator = Executors.newSingleThreadExecutor(); + + private static final Font mono = new Font(Font.MONOSPACED, Font.PLAIN, 12); + + private final PicoContainer container; + private final JTextArea entryArea; + private final GroovyShell shell; + private final JScrollPane resultScroll; + + // TODO: global groovy binding + public GroovyTab(PicoContainer container) { + setLayout(new BorderLayout()); + setBorder(new TitledBorder("Groovy")); + JSplitPane split; + JPanel top; + JPanel bottom; + + CompilerConfiguration compilerConfiguration = new CompilerConfiguration(); + ImportCustomizer importCustomizer = new ImportCustomizer(); + importCustomizer.addImports( + Predicate.class.getCanonicalName(), + CompileStatic.class.getCanonicalName(), + TypeChecked.class.getCanonicalName()); + importCustomizer.addStarImports( + "gg.xp.xivsupport.events.actlines.events", + "javax.swing", + "gg.xp.xivsupport.gui", + "gg.xp.xivsupport.gui.tables" + ); + Reflections reflections = new Reflections( + new ConfigurationBuilder() + .setUrls(ClasspathHelper.forJavaClassPath()) + .setScanners(Scanners.SubTypes)); + reflections.get(SubTypes.of(Event.class).asClass()) + .stream() + .map(Class::getCanonicalName) + .filter(Objects::nonNull) + .forEach(importCustomizer::addImports); + + compilerConfiguration.addCompilationCustomizers(importCustomizer); + Binding binding = new Binding() { + @Override + public Object getProperty(String property) { + return super.getProperty(property); + } + }; + shell = new GroovyShell(binding, compilerConfiguration); + container.getComponents().forEach(item -> { + String simpleName = item.getClass().getSimpleName(); + simpleName = StringUtils.uncapitalize(simpleName); + binding.setProperty(simpleName, item); + }); + // TODO: find a way to systematically do these + binding.setProperty("xivState", container.getComponent(XivState.class)); + + this.container = container; + { + top = new JPanel(new BorderLayout()); +// top.setPreferredSize(top.getMaximumSize()); + entryArea = new JTextArea(defaultScript); + entryArea.setFont(mono); + JScrollPane entryScroll = new JScrollPane(entryArea); + top.add(entryScroll, BorderLayout.CENTER); + { + JButton runButton = new JButton("Execute (Ctrl-Enter)"); + JPanel buttonHolder = new JPanel(new WrapLayout(WrapLayout.LEFT)); + buttonHolder.add(runButton); + top.add(buttonHolder, BorderLayout.SOUTH); + runButton.addActionListener(l -> submit()); + } + top.add(new ReadOnlyText("DO NOT run random scripts from the internet!"), BorderLayout.NORTH); + entryArea.addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + int code = e.getKeyCode(); + if (code == KeyEvent.VK_ENTER && e.getModifiersEx() == InputEvent.CTRL_DOWN_MASK) { + submit(); + } + super.keyPressed(e); + } + }); + } + { + this.resultScroll = new JScrollPane(); + bottom = new JPanel(new BorderLayout()); +// bottom.setPreferredSize(bottom.getMaximumSize()); + bottom.add(resultScroll, BorderLayout.CENTER); + } + { + split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, top, bottom); + split.setOneTouchExpandable(true); + split.setDividerLocation(0.5); + split.setResizeWeight(0.5); + split.setDividerSize(10); + add(split, BorderLayout.CENTER); + } + } + + private JTextArea textDisplayComponent(String text) { + JTextArea resultsArea = new JTextArea(text); + resultsArea.setFont(mono); + resultsArea.setLineWrap(true); + resultsArea.setWrapStyleWord(true); + resultsArea.setEditable(false); + resultsArea.setCaretPosition(0); + return resultsArea; + } + + private JTextArea errorDisplayComponent(String text) { + JTextArea resultsArea = textDisplayComponent(text); + resultsArea.setBackground(invalidBackground); + return resultsArea; + } + + private JTable simpleListDisplay(Collection values) { + return CustomTableModel.builder(() -> new ArrayList<>(values)) + .addColumn(new CustomColumn<>("Value", GroovyTab::singleValueConversion)) + .build() + .makeTable(); + } + + private JTable simpleMapDisplay(Map map) { + return CustomTableModel.builder(() -> new ArrayList<>(map.entrySet())) + .addColumn(new CustomColumn<>("Key", e -> singleValueConversion(e.getKey()))) + .addColumn(new CustomColumn<>("Value", e -> singleValueConversion(e.getValue()))) + .build() + .makeTable(); + } + + private static String singleValueConversion(Object obj) { + if (obj instanceof Byte || obj instanceof Integer || obj instanceof Long || obj instanceof Short) { + return String.format("%d (0x%x)", obj, obj); + } + return obj.toString(); + + } + + private void submit() { + setResult("Processing..."); + String text = entryArea.getText(); + evaluator.submit(() -> { + try { + Object result = shell.parse(text).run(); + setResult(result); + } + catch (Throwable t) { + setResult(t); + } + }); + } + + private void setResult(Object result) { + if (result == null) { + setResultDisplay(textDisplayComponent("null")); + } + else if (result instanceof Throwable t) { + setResultDisplay(errorDisplayComponent(ExceptionUtils.getStackTrace(t))); + } + else if (result instanceof Map map) { + setResultDisplay(simpleMapDisplay(map)); + } + else if (result instanceof Collection coll) { + setResultDisplay(simpleListDisplay(coll)); + } + else if (result instanceof Component comp) { + setResultDisplay(comp); + } + else { + setResultDisplay(textDisplayComponent(result.toString())); + } + } + + private void setResultDisplay(Component display) { + SwingUtilities.invokeLater(() -> resultScroll.setViewportView(display)); + } + + private static final String defaultScript = """ + \"""Hi There! + + This is the Groovy Console. You can run scripts here, written in Groovy (https://groovy-lang.org/). + For the most part, Java code will also be valid Groovy code, so you can also use this to prototype mainline code. + + By default, everything in the DI container is injected as a variable, with the first letter of the class name lowercased. + + For example, I can see that there are currently ${rawEventStorage.getEvents().size()} events on record. The current player name is ${xivState.getPlayer()?.getName()}. + + Your return type can be a String, a List, Map, or Swing Component. The value will be rendered differently according to its type. In this case, it is a String. + + This does NOT have any sandboxing, so don't run random stuff you found on the internet. It can do anything to your system that compiled Java code would be able to do. \""" + """; +} From 6a8accbfeeedf9a35f9c784a222391184974fe98 Mon Sep 17 00:00:00 2001 From: xp Date: Thu, 17 Mar 2022 19:41:28 -0700 Subject: [PATCH 3/4] Updated example --- .../main/java/gg/xp/xivsupport/gui/tabs/GroovyTab.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/GroovyTab.java b/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/GroovyTab.java index b72ab5f3f1c9..b95d1b707e61 100644 --- a/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/GroovyTab.java +++ b/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/GroovyTab.java @@ -83,12 +83,7 @@ public GroovyTab(PicoContainer container) { .forEach(importCustomizer::addImports); compilerConfiguration.addCompilationCustomizers(importCustomizer); - Binding binding = new Binding() { - @Override - public Object getProperty(String property) { - return super.getProperty(property); - } - }; + Binding binding = new Binding(); shell = new GroovyShell(binding, compilerConfiguration); container.getComponents().forEach(item -> { String simpleName = item.getClass().getSimpleName(); @@ -231,6 +226,8 @@ This is the Groovy Console. You can run scripts here, written in Groovy (https:/ Your return type can be a String, a List, Map, or Swing Component. The value will be rendered differently according to its type. In this case, it is a String. + Variables defined here will be scoped locally. If you want it to be persistent across multiple executions, then use binding.setVariable("name", value). + This does NOT have any sandboxing, so don't run random stuff you found on the internet. It can do anything to your system that compiled Java code would be able to do. \""" """; } From 95e3209718b029cb87a8d9b918329b2728ae457e Mon Sep 17 00:00:00 2001 From: xp Date: Thu, 17 Mar 2022 19:53:16 -0700 Subject: [PATCH 4/4] More examples --- .../src/main/java/gg/xp/xivsupport/gui/tabs/GroovyTab.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/GroovyTab.java b/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/GroovyTab.java index b95d1b707e61..599ee5645009 100644 --- a/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/GroovyTab.java +++ b/xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/GroovyTab.java @@ -223,6 +223,8 @@ This is the Groovy Console. You can run scripts here, written in Groovy (https:/ By default, everything in the DI container is injected as a variable, with the first letter of the class name lowercased. For example, I can see that there are currently ${rawEventStorage.getEvents().size()} events on record. The current player name is ${xivState.getPlayer()?.getName()}. + + You could also run propertiesFilePersistenceProvider.@properties to dump all settings into a key/value display. Your return type can be a String, a List, Map, or Swing Component. The value will be rendered differently according to its type. In this case, it is a String.