From 7214aa1bdeb218ae49060fd6163507e85bd50e1f Mon Sep 17 00:00:00 2001 From: Konloch Date: Fri, 25 Jun 2021 20:52:27 -0700 Subject: [PATCH] Javascript plugin support --- .../gui/resourceviewer/viewer/FileViewer.java | 2 + .../bytecodeviewer/plugin/PluginManager.java | 26 ++++------ .../JavascriptPluginLaunchStrategy.java | 50 +++++++++++++++++++ 3 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 src/main/java/the/bytecode/club/bytecodeviewer/plugin/strategies/JavascriptPluginLaunchStrategy.java diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/viewer/FileViewer.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/viewer/FileViewer.java index 54fba386..7ee06530 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/viewer/FileViewer.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/viewer/FileViewer.java @@ -92,7 +92,9 @@ public class FileViewer extends ResourceViewer nameLowerCase.endsWith(".gif") || nameLowerCase.endsWith(".tif") || nameLowerCase.endsWith(".bmp")) { canRefresh = true; + image = MiscUtils.loadImage(image, contents); //gifs fail because of this + mainPanel.add(new ImageJLabel(image), BorderLayout.CENTER); mainPanel.addMouseWheelListener(e -> { diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginManager.java b/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginManager.java index 1b57844f..ae57d02b 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginManager.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginManager.java @@ -7,11 +7,7 @@ import java.util.Set; import javax.swing.filechooser.FileFilter; import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.api.Plugin; -import the.bytecode.club.bytecodeviewer.plugin.strategies.CompiledJavaPluginLaunchStrategy; -import the.bytecode.club.bytecodeviewer.plugin.strategies.GroovyPluginLaunchStrategy; -import the.bytecode.club.bytecodeviewer.plugin.strategies.JavaPluginLaunchStrategy; -import the.bytecode.club.bytecodeviewer.plugin.strategies.PythonPluginLaunchStrategy; -import the.bytecode.club.bytecodeviewer.plugin.strategies.RubyPluginLaunchStrategy; +import the.bytecode.club.bytecodeviewer.plugin.strategies.*; import the.bytecode.club.bytecodeviewer.util.MiscUtils; /*************************************************************************** @@ -47,9 +43,11 @@ public final class PluginManager { private static final PluginFileFilter filter = new PluginFileFilter(); private static Plugin pluginInstance; - static { + static + { launchStrategies.put("jar", new CompiledJavaPluginLaunchStrategy()); launchStrategies.put("java", new JavaPluginLaunchStrategy()); + launchStrategies.put("js", new JavascriptPluginLaunchStrategy()); GroovyPluginLaunchStrategy groovy = new GroovyPluginLaunchStrategy(); launchStrategies.put("gy", groovy); @@ -85,20 +83,18 @@ public final class PluginManager { * @param f the file of the plugin * @throws Exception */ - public static void runPlugin(File f) throws Throwable { + public static void runPlugin(File f) throws Throwable + { String ext = f.getName().substring(f.getName().lastIndexOf('.') + 1); PluginLaunchStrategy strategy = launchStrategies.get(ext); - if (strategy == null) { - throw new RuntimeException(String.format("No launch strategy for extension %s (%s)", ext, - f.getAbsolutePath())); - } + if (strategy == null) + throw new RuntimeException(String.format("No launch strategy for extension %s (%s)", ext, f.getAbsolutePath())); Plugin p = strategy.run(f); - if (p != null) { + if (p != null) runPlugin(p); - } } public static void register(String name, PluginLaunchStrategy strat) { @@ -113,8 +109,8 @@ public final class PluginManager { return filter; } - public static class PluginFileFilter extends FileFilter { - + public static class PluginFileFilter extends FileFilter + { @Override public boolean accept(File f) { if (f.isDirectory()) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/plugin/strategies/JavascriptPluginLaunchStrategy.java b/src/main/java/the/bytecode/club/bytecodeviewer/plugin/strategies/JavascriptPluginLaunchStrategy.java new file mode 100644 index 00000000..6f6b7ffb --- /dev/null +++ b/src/main/java/the/bytecode/club/bytecodeviewer/plugin/strategies/JavascriptPluginLaunchStrategy.java @@ -0,0 +1,50 @@ +package the.bytecode.club.bytecodeviewer.plugin.strategies; + +import the.bytecode.club.bytecodeviewer.api.Plugin; +import the.bytecode.club.bytecodeviewer.plugin.PluginLaunchStrategy; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import java.io.File; +import java.io.FileReader; +import java.io.Reader; + +/*************************************************************************** + * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite * + * Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + ***************************************************************************/ + +/** + * @author Konloch + * @author Bibl (don't ban me pls) + * @since 06/25/2021 + */ +public class JavascriptPluginLaunchStrategy implements PluginLaunchStrategy { + + @Override + public Plugin run(File file) throws Throwable { + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine engine = manager.getEngineByName("rhino"); + + if (engine == null) + throw new Exception("Cannot find Rhino script engine! Please contact Konloch."); + + Reader reader = new FileReader(file); + engine.eval(reader); + + return (Plugin) engine.eval(file.getName().replace(".js", "") + ".new"); + } +} \ No newline at end of file