Skip to content

Commit 4705c18

Browse files
committed
Add shutdown and startup hooks for plugins
1 parent e0134fd commit 4705c18

File tree

8 files changed

+49
-165
lines changed

8 files changed

+49
-165
lines changed

src/main/java/the/bytecode/club/jda/JDA.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.objectweb.asm.ClassWriter;
66
import org.objectweb.asm.tree.ClassNode;
77
import the.bytecode.club.jda.api.ExceptionUI;
8+
import the.bytecode.club.jda.api.Plugin;
9+
import the.bytecode.club.jda.api.PluginLoader;
810
import the.bytecode.club.jda.gui.FileNavigationPane;
911
import the.bytecode.club.jda.gui.MainViewerGUI;
1012
import the.bytecode.club.jda.settings.Settings;
@@ -37,11 +39,12 @@ public class JDA {
3739
private static final long start = System.currentTimeMillis();
3840
/*the rest*/
3941
public static MainViewerGUI viewer = null;
40-
public static ArrayList<FileContainer> files = new ArrayList<>(); //all of BCV's loaded files/classes/etc
42+
public static List<FileContainer> files = new ArrayList<>(); //all of BCV's loaded files/classes/etc
4143
private static int maxRecentFiles = 25;
4244
private static List<String> recentFiles = new ArrayList<>();
4345
public static String lastDirectory = "";
44-
public static ArrayList<Process> createdProcesses = new ArrayList<>();
46+
public static List<Process> createdProcesses = new ArrayList<>();
47+
public static List<Plugin> plugins = new ArrayList<>();
4548

4649
/**
4750
* Main startup
@@ -88,39 +91,27 @@ else if (!pluginsDir.isDirectory())
8891
System.out.println("Skipping non-jar " + pluginFile.getName());
8992
continue;
9093
}
91-
PluginLoader.tryLoadPlugin(pluginFile);
94+
Plugin pluginInstance = PluginLoader.tryLoadPlugin(pluginFile);
95+
if (pluginInstance != null)
96+
plugins.add(pluginInstance);
9297
}
9398
}
9499

100+
public static void onGUILoad() {
101+
plugins.forEach(Plugin::onGUILoad);
102+
}
103+
104+
// todo: rewrite
95105
/**
96106
* The version checker thread
97107
*/
98-
private static final Thread versionChecker = new Thread() {
99-
// todo: rewrite
100-
@Override
101-
public void run() {
102-
}
103-
};
108+
private static final Thread versionChecker = new Thread(() -> {});
104109

105110
/**
106111
* Boot after all of the libraries have been loaded
107112
*/
108113
public static void boot(String[] args) {
109-
Runtime.getRuntime().addShutdownHook(new Thread() {
110-
@Override
111-
public void run() {
112-
for (Process proc : createdProcesses)
113-
proc.destroy();
114-
try {
115-
FileUtils.writeLines(recentsFile, recentFiles);
116-
} catch (IOException e) {
117-
new ExceptionUI(e);
118-
}
119-
if (!viewer.isMaximized)
120-
viewer.unmaximizedPos = viewer.getLocation();
121-
Settings.saveGUI();
122-
}
123-
});
114+
Runtime.getRuntime().addShutdownHook(new Thread(JDA::onExit));
124115

125116
resetRecentFilesMenu();
126117

@@ -139,6 +130,21 @@ public void run() {
139130
}
140131
}
141132

133+
private static void onExit() {
134+
plugins.forEach(Plugin::onExit);
135+
136+
for (Process proc : createdProcesses)
137+
proc.destroy();
138+
try {
139+
FileUtils.writeLines(recentsFile, recentFiles);
140+
} catch (IOException e) {
141+
new ExceptionUI(e);
142+
}
143+
if (!viewer.isMaximized)
144+
viewer.unmaximizedPos = viewer.getLocation();
145+
Settings.saveGUI();
146+
}
147+
142148
/**
143149
* Returns the currently opened ClassNode
144150
*

src/main/java/the/bytecode/club/jda/api/ClassNodeLoader.java

Lines changed: 0 additions & 130 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package the.bytecode.club.jda.api;
2+
3+
public interface Plugin {
4+
int onGUILoad();
5+
6+
int onExit();
7+
}

src/main/java/the/bytecode/club/jda/PluginLoader.java renamed to src/main/java/the/bytecode/club/jda/api/PluginLoader.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
package the.bytecode.club.jda;
1+
package the.bytecode.club.jda.api;
22

33
import java.io.*;
4-
import java.lang.reflect.Method;
54
import java.net.MalformedURLException;
65
import java.net.URL;
76
import java.net.URLClassLoader;
87

98
public class PluginLoader {
10-
public static void tryLoadPlugin(File pluginFile) throws MalformedURLException {
9+
public static Plugin tryLoadPlugin(File pluginFile) throws MalformedURLException {
1110
String pluginFileName = pluginFile.getName();
1211
try {
1312
ClassLoader loader = new URLClassLoader(new URL[] { pluginFile.toURI().toURL() }) {
@@ -22,21 +21,21 @@ public URL getResource(String name) {
2221
InputStream metaInfStream = loader.getResourceAsStream("\0JDA-hack:META-INF/MANIFEST.MF");
2322
if (metaInfStream == null) {
2423
System.out.println("Invalid plugin " + pluginFileName + ": no manifest");
25-
return;
24+
return null;
2625
}
2726

2827
String mainClass = parseManifest(metaInfStream);
2928
if (mainClass == null) {
3029
System.out.println("Invalid plugin " + pluginFileName + ": unable to parse manifest");
31-
return;
30+
return null;
3231
}
3332

34-
Class<?> clazz = Class.forName(mainClass, true, loader);
35-
Method mainMethod = clazz.getMethod("main", String[].class);
36-
mainMethod.invoke(null, (Object) new String[0]);
33+
Class<? extends Plugin> clazz = Class.forName(mainClass, true, loader).asSubclass(Plugin.class);
34+
return clazz.getConstructor().newInstance();
3735
} catch (ReflectiveOperationException e) {
3836
System.err.println("Failed to load plugin " + pluginFileName);
3937
e.printStackTrace();
38+
return null;
4039
}
4140
}
4241

src/main/java/the/bytecode/club/jda/decompilers/CFRDecompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* @author Konloch
3636
*/
37-
public class CFRDecompiler extends Decompiler {
37+
public final class CFRDecompiler extends Decompiler {
3838

3939
public CFRDecompiler() {
4040
for (Settings setting : Settings.values()) {

src/main/java/the/bytecode/club/jda/decompilers/FernflowerDecompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @author WaterWolf
2727
*/
2828

29-
public class FernflowerDecompiler extends Decompiler {
29+
public final class FernflowerDecompiler extends Decompiler {
3030

3131
public FernflowerDecompiler() {
3232
for (Settings setting : Settings.values()) {

src/main/java/the/bytecode/club/jda/decompilers/ProcyonDecompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @author DeathMarine
2727
*/
2828

29-
public class ProcyonDecompiler extends Decompiler {
29+
public final class ProcyonDecompiler extends Decompiler {
3030

3131
public ProcyonDecompiler() {
3232
for (Settings setting : Settings.values()) {

src/main/java/the/bytecode/club/jda/gui/MainViewerGUI.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ public void componentMoved(ComponentEvent e) {
139139
unmaximizedPos = getLocation();
140140

141141
this.setLocationRelativeTo(null);
142+
143+
JDA.onGUILoad();
142144
}
143145

144146
private void initializeMenubar() {

0 commit comments

Comments
 (0)