Javascript plugin support

This commit is contained in:
Konloch 2021-06-25 20:52:27 -07:00
parent f5f510a2e0
commit 7214aa1bde
3 changed files with 63 additions and 15 deletions

View file

@ -92,7 +92,9 @@ public class FileViewer extends ResourceViewer
nameLowerCase.endsWith(".gif") || nameLowerCase.endsWith(".tif") || nameLowerCase.endsWith(".bmp")) nameLowerCase.endsWith(".gif") || nameLowerCase.endsWith(".tif") || nameLowerCase.endsWith(".bmp"))
{ {
canRefresh = true; canRefresh = true;
image = MiscUtils.loadImage(image, contents); //gifs fail because of this image = MiscUtils.loadImage(image, contents); //gifs fail because of this
mainPanel.add(new ImageJLabel(image), BorderLayout.CENTER); mainPanel.add(new ImageJLabel(image), BorderLayout.CENTER);
mainPanel.addMouseWheelListener(e -> mainPanel.addMouseWheelListener(e ->
{ {

View file

@ -7,11 +7,7 @@ import java.util.Set;
import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileFilter;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.api.Plugin; import the.bytecode.club.bytecodeviewer.api.Plugin;
import the.bytecode.club.bytecodeviewer.plugin.strategies.CompiledJavaPluginLaunchStrategy; import the.bytecode.club.bytecodeviewer.plugin.strategies.*;
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.util.MiscUtils; import the.bytecode.club.bytecodeviewer.util.MiscUtils;
/*************************************************************************** /***************************************************************************
@ -47,9 +43,11 @@ public final class PluginManager {
private static final PluginFileFilter filter = new PluginFileFilter(); private static final PluginFileFilter filter = new PluginFileFilter();
private static Plugin pluginInstance; private static Plugin pluginInstance;
static { static
{
launchStrategies.put("jar", new CompiledJavaPluginLaunchStrategy()); launchStrategies.put("jar", new CompiledJavaPluginLaunchStrategy());
launchStrategies.put("java", new JavaPluginLaunchStrategy()); launchStrategies.put("java", new JavaPluginLaunchStrategy());
launchStrategies.put("js", new JavascriptPluginLaunchStrategy());
GroovyPluginLaunchStrategy groovy = new GroovyPluginLaunchStrategy(); GroovyPluginLaunchStrategy groovy = new GroovyPluginLaunchStrategy();
launchStrategies.put("gy", groovy); launchStrategies.put("gy", groovy);
@ -85,20 +83,18 @@ public final class PluginManager {
* @param f the file of the plugin * @param f the file of the plugin
* @throws Exception * @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); String ext = f.getName().substring(f.getName().lastIndexOf('.') + 1);
PluginLaunchStrategy strategy = launchStrategies.get(ext); PluginLaunchStrategy strategy = launchStrategies.get(ext);
if (strategy == null) { if (strategy == null)
throw new RuntimeException(String.format("No launch strategy for extension %s (%s)", ext, throw new RuntimeException(String.format("No launch strategy for extension %s (%s)", ext, f.getAbsolutePath()));
f.getAbsolutePath()));
}
Plugin p = strategy.run(f); Plugin p = strategy.run(f);
if (p != null) { if (p != null)
runPlugin(p); runPlugin(p);
}
} }
public static void register(String name, PluginLaunchStrategy strat) { public static void register(String name, PluginLaunchStrategy strat) {
@ -113,8 +109,8 @@ public final class PluginManager {
return filter; return filter;
} }
public static class PluginFileFilter extends FileFilter { public static class PluginFileFilter extends FileFilter
{
@Override @Override
public boolean accept(File f) { public boolean accept(File f) {
if (f.isDirectory()) if (f.isDirectory())

View file

@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
/**
* @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");
}
}