|
@@ -22,46 +22,53 @@
|
22
|
22
|
|
23
|
23
|
package com.dmdirc.addons.scriptplugin;
|
24
|
24
|
|
25
|
|
-import com.dmdirc.actions.CoreActionType;
|
26
|
25
|
import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
|
|
26
|
+import com.dmdirc.events.DMDircEvent;
|
|
27
|
+import com.dmdirc.events.PluginLoadedEvent;
|
|
28
|
+import com.dmdirc.events.PluginUnloadedEvent;
|
27
|
29
|
import com.dmdirc.interfaces.ActionController;
|
28
|
|
-import com.dmdirc.interfaces.ActionListener;
|
29
|
|
-import com.dmdirc.interfaces.actions.ActionType;
|
30
|
30
|
import com.dmdirc.logger.ErrorLevel;
|
31
|
31
|
import com.dmdirc.logger.Logger;
|
32
|
32
|
|
|
33
|
+import com.google.common.eventbus.EventBus;
|
|
34
|
+import com.google.common.eventbus.Subscribe;
|
|
35
|
+
|
33
|
36
|
import java.io.File;
|
34
|
37
|
import java.io.FileInputStream;
|
35
|
38
|
import java.io.FileOutputStream;
|
36
|
39
|
import java.io.IOException;
|
|
40
|
+import java.lang.reflect.Method;
|
|
41
|
+import java.util.ArrayList;
|
37
|
42
|
import java.util.List;
|
38
|
|
-import java.util.Map;
|
39
|
43
|
|
40
|
44
|
import javax.inject.Inject;
|
41
|
45
|
import javax.script.ScriptEngineManager;
|
42
|
46
|
|
43
|
|
-public class ScriptPluginManager implements ActionListener {
|
|
47
|
+public class ScriptPluginManager {
|
44
|
48
|
|
|
49
|
+ private final EventBus eventBus;
|
45
|
50
|
private final ActionController actionController;
|
46
|
51
|
private final String scriptDir;
|
47
|
52
|
private final ScriptManager scriptManager;
|
48
|
53
|
private final TypedProperties globalVariables;
|
49
|
54
|
|
50
|
55
|
@Inject
|
51
|
|
- public ScriptPluginManager(final ActionController actionController,
|
|
56
|
+ public ScriptPluginManager(final EventBus eventBus,
|
|
57
|
+ final ActionController actionController,
|
52
|
58
|
@Directory(ScriptModule.SCRIPTS) final String scriptDir,
|
53
|
59
|
final ScriptManager scriptManager,
|
54
|
60
|
final ScriptEngineManager scriptEngineManager) {
|
55
|
61
|
this.actionController = actionController;
|
56
|
62
|
this.scriptDir = scriptDir;
|
57
|
63
|
this.scriptManager = scriptManager;
|
|
64
|
+ this.eventBus = eventBus;
|
58
|
65
|
globalVariables = (TypedProperties) scriptEngineManager.get("globalVariables");
|
59
|
66
|
}
|
60
|
67
|
|
61
|
68
|
public void onLoad() {
|
62
|
69
|
// Register the plugin_loaded action initially, this will be called
|
63
|
70
|
// after this method finishes for us to register the rest.
|
64
|
|
- actionController.registerListener(this, CoreActionType.PLUGIN_LOADED);
|
|
71
|
+ eventBus.register(this);
|
65
|
72
|
|
66
|
73
|
// Make sure our scripts dir exists
|
67
|
74
|
final File newDir = new File(scriptDir);
|
|
@@ -81,7 +88,7 @@ public class ScriptPluginManager implements ActionListener {
|
81
|
88
|
}
|
82
|
89
|
|
83
|
90
|
public void onUnLoad() {
|
84
|
|
- actionController.unregisterListener(this);
|
|
91
|
+ eventBus.unregister(this);
|
85
|
92
|
|
86
|
93
|
final File savedVariables = new File(scriptDir + "storedVariables");
|
87
|
94
|
try (FileOutputStream fos = new FileOutputStream(savedVariables)) {
|
|
@@ -92,27 +99,22 @@ public class ScriptPluginManager implements ActionListener {
|
92
|
99
|
}
|
93
|
100
|
}
|
94
|
101
|
|
95
|
|
- /**
|
96
|
|
- * Register all the action types. This will unregister all the actions first.
|
97
|
|
- */
|
98
|
|
- private void registerAll() {
|
99
|
|
- actionController.unregisterListener(this);
|
100
|
|
- for (Map.Entry<String, List<ActionType>> entry
|
101
|
|
- : actionController.getGroupedTypes().entrySet()) {
|
102
|
|
- final List<ActionType> types = entry.getValue();
|
103
|
|
- actionController.registerListener(this, types.toArray(new ActionType[types.size()]));
|
|
102
|
+ @Subscribe
|
|
103
|
+ public void handlePluginLoadEvent(final DMDircEvent event) throws ReflectiveOperationException {
|
|
104
|
+ if (event instanceof PluginLoadedEvent || event instanceof PluginUnloadedEvent) {
|
|
105
|
+ return;
|
104
|
106
|
}
|
105
|
|
- }
|
106
|
|
-
|
107
|
|
- @Override
|
108
|
|
- public void processEvent(final ActionType type, final StringBuffer format,
|
109
|
|
- final Object... arguments) {
|
110
|
|
- // Plugins may to register/unregister action types, so lets reregister all
|
111
|
|
- // the action types, except Plugin loaded and plugin unloaded.
|
112
|
|
- if (type.equals(CoreActionType.PLUGIN_LOADED) || type.equals(CoreActionType.PLUGIN_UNLOADED)) {
|
113
|
|
- registerAll();
|
|
107
|
+ final String name = event.getClass().getSimpleName()
|
|
108
|
+ .replaceAll("Event$", "")
|
|
109
|
+ .replaceAll("(.)([A-Z])", "$1_$2")
|
|
110
|
+ .toUpperCase();
|
|
111
|
+ final List<Object> arguments = new ArrayList<>();
|
|
112
|
+ for (Method method : event.getClass().getMethods()) {
|
|
113
|
+ if ((method.getName().startsWith("get") && method.getParameterCount() == 0)
|
|
114
|
+ && !method.getName().equals("getDisplayFormat")) {
|
|
115
|
+ arguments.add(method.invoke(event));
|
|
116
|
+ }
|
114
|
117
|
}
|
115
|
|
- scriptManager.callFunctionAll("action_" + type.toString().toLowerCase(), arguments);
|
|
118
|
+ scriptManager.callFunctionAll("action_" + name, arguments.toArray());
|
116
|
119
|
}
|
117
|
|
-
|
118
|
120
|
}
|