7
7
import org .jetbrains .annotations .Nullable ;
8
8
import top .focess .qq .FocessQQ ;
9
9
import top .focess .qq .api .command .*;
10
- import top .focess .qq .api .event .EventManager ;
11
- import top .focess .qq .api .event .EventSubmitException ;
12
- import top .focess .qq .api .event .ListenerHandler ;
10
+ import top .focess .qq .api .command .converter .DataConverterType ;
11
+ import top .focess .qq .api .command .converter .IllegalDataConverterClassException ;
12
+ import top .focess .qq .api .command .data .DataBuffer ;
13
+ import top .focess .qq .api .event .*;
13
14
import top .focess .qq .api .event .plugin .PluginLoadEvent ;
14
15
import top .focess .qq .api .event .plugin .PluginUnloadEvent ;
15
16
import top .focess .qq .api .plugin .*;
25
26
import java .io .File ;
26
27
import java .io .IOException ;
27
28
import java .lang .annotation .Annotation ;
28
- import java .lang .reflect .Field ;
29
- import java .lang .reflect .Method ;
30
- import java .lang .reflect .Modifier ;
29
+ import java .lang .reflect .*;
31
30
import java .net .MalformedURLException ;
32
31
import java .net .URL ;
33
32
import java .net .URLClassLoader ;
@@ -55,6 +54,7 @@ public class PluginClassLoader extends URLClassLoader {
55
54
private static final Object LOCK = new Object ();
56
55
private static final Map <String , Set <File >> AFTER_PLUGINS_MAP = Maps .newHashMap ();
57
56
private static final Map <Class <? extends Annotation >, AnnotationHandler > HANDLERS = Maps .newHashMap ();
57
+ private static final Map <Class <? extends Annotation >, FieldAnnotationHandler > FIELD_ANNOTATION_HANDLERS = Maps .newHashMap ();
58
58
59
59
private static final List <ResourceHandler > RESOURCE_HANDLERS = Lists .newArrayList ();
60
60
@@ -137,9 +137,7 @@ public PluginDescription getPluginDescription() {
137
137
CommandType commandType = (CommandType ) annotation ;
138
138
if (Command .class .isAssignableFrom (c ) && !Modifier .isAbstract (c .getModifiers ())) {
139
139
try {
140
- Plugin plugin = Plugin .getPlugin (commandType .plugin ());
141
- if (plugin == null )
142
- throw new IllegalCommandClassException (c );
140
+ Plugin plugin = classLoader .plugin ;
143
141
Command command = (Command ) c .newInstance ();
144
142
if (!commandType .name ().isEmpty ()){
145
143
COMMAND_NAME_FIELD .set (command ,commandType .name ());
@@ -153,7 +151,7 @@ public PluginDescription getPluginDescription() {
153
151
COMMAND_INITIALIZE_FIELD .set (command ,true );
154
152
}
155
153
}
156
- Command . register ( plugin , command );
154
+ plugin . registerCommand ( command );
157
155
return true ;
158
156
} catch (Exception e ) {
159
157
if (e instanceof CommandDuplicateException )
@@ -164,6 +162,39 @@ else if (e instanceof CommandLoadException)
164
162
}
165
163
} else throw new IllegalCommandClassException (c );
166
164
});
165
+
166
+ HANDLERS .put (ListenerType .class , (c , annotation , classLoader ) -> {
167
+ if (Listener .class .isAssignableFrom (c ) && !Modifier .isInterface (c .getModifiers ()) && !Modifier .isAbstract (c .getModifiers ())) {
168
+ try {
169
+ Plugin plugin = classLoader .plugin ;
170
+ Listener listener = (Listener ) c .newInstance ();
171
+ plugin .registerListener (listener );
172
+ return true ;
173
+ } catch (Exception e ) {
174
+ throw new IllegalListenerClassException ((Class <? extends Listener >) c , e );
175
+ }
176
+ } else throw new IllegalListenerClassException (c );
177
+ });
178
+
179
+ FIELD_ANNOTATION_HANDLERS .put (DataConverterType .class ,(field , annotation , classLoader ) -> {
180
+ DataConverterType dataConverterType = (DataConverterType ) annotation ;
181
+ if (DataConverter .class .isAssignableFrom (field .getType ())) {
182
+ try {
183
+ Plugin plugin = classLoader .plugin ;
184
+ DataConverter dataConverter = (DataConverter ) field .get (null );
185
+ Constructor <DataBuffer <?>> constructor = (Constructor <DataBuffer <?>>) dataConverterType .buffer ().getDeclaredConstructor (int .class );
186
+ plugin .registerBuffer (dataConverter , size -> {
187
+ try {
188
+ return constructor .newInstance (size );
189
+ } catch (Exception e ) {
190
+ throw new RuntimeException (e );
191
+ }
192
+ });
193
+ } catch (Exception e ) {
194
+ throw new IllegalDataConverterClassException ((Class <? extends DataConverter >) field .getType (), e );
195
+ }
196
+ } else throw new IllegalDataConverterClassException (field .getType ());
197
+ });
167
198
}
168
199
169
200
private static final Scheduler SCHEDULER = Schedulers .newThreadPoolScheduler (FocessQQ .getMainPlugin (),2 ,false ,"PluginLoader" );
@@ -175,6 +206,8 @@ else if (e instanceof CommandLoadException)
175
206
* @param plugin the plugin need to be enabled
176
207
* @throws PluginLoaderException if the classloader of the plugin is not {@link PluginClassLoader}
177
208
* @throws PluginDuplicateException if the plugin name already exists in the registered plugins
209
+ * @throws PluginLoadException if there is an error while enabling the plugin
210
+ * @throws PluginUnloadException if the plugin should be unloaded
178
211
*/
179
212
public static void enablePlugin (Plugin plugin ) {
180
213
if (plugin .getClass () != FocessQQ .MainPlugin .class ) {
@@ -378,7 +411,7 @@ public boolean load() {
378
411
379
412
for (Class <?> c : loadedClasses )
380
413
analyseClass (c );
381
- FocessQQ .getLogger ().debugLang ("load-command- class" );
414
+ FocessQQ .getLogger ().debugLang ("load-class" );
382
415
383
416
FocessQQ .getLogger ().debugLang ("load-depend-plugin" );
384
417
for (File file : AFTER_PLUGINS_MAP .getOrDefault (plugin .getName (), Sets .newHashSet ())) {
0 commit comments