Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
ziqian zhang
Grasscutter
Commits
f8bc471d
Commit
f8bc471d
authored
May 07, 2022
by
muhammadeko
Browse files
PluginManager: Use the same class loader and add getPlugin method
parent
dc3c7447
Changes
1
Show whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/plugin/PluginManager.java
View file @
f8bc471d
...
@@ -4,12 +4,12 @@ import emu.grasscutter.Grasscutter;
...
@@ -4,12 +4,12 @@ import emu.grasscutter.Grasscutter;
import
emu.grasscutter.server.event.Event
;
import
emu.grasscutter.server.event.Event
;
import
emu.grasscutter.server.event.EventHandler
;
import
emu.grasscutter.server.event.EventHandler
;
import
emu.grasscutter.server.event.HandlerPriority
;
import
emu.grasscutter.server.event.HandlerPriority
;
import
emu.grasscutter.utils.EventConsumer
;
import
emu.grasscutter.utils.Utils
;
import
emu.grasscutter.utils.Utils
;
import
java.io.File
;
import
java.io.File
;
import
java.io.InputStreamReader
;
import
java.io.InputStreamReader
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Method
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.net.URL
;
import
java.net.URLClassLoader
;
import
java.net.URLClassLoader
;
import
java.util.*
;
import
java.util.*
;
...
@@ -48,11 +48,22 @@ public final class PluginManager {
...
@@ -48,11 +48,22 @@ public final class PluginManager {
.
filter
(
file
->
file
.
getName
().
endsWith
(
".jar"
))
.
filter
(
file
->
file
.
getName
().
endsWith
(
".jar"
))
.
toList
();
.
toList
();
URL
[]
pluginNames
=
new
URL
[
plugins
.
size
()];
plugins
.
forEach
(
plugin
->
{
try
{
pluginNames
[
plugins
.
indexOf
(
plugin
)]
=
plugin
.
toURI
().
toURL
();
}
catch
(
MalformedURLException
e
)
{
e
.
printStackTrace
();
}
});
URLClassLoader
classLoader
=
new
URLClassLoader
(
pluginNames
);
plugins
.
forEach
(
plugin
->
{
plugins
.
forEach
(
plugin
->
{
try
{
try
{
URL
url
=
plugin
.
toURI
().
toURL
();
URL
url
=
plugin
.
toURI
().
toURL
();
try
(
URLClassLoader
loader
=
new
URLClassLoader
(
new
URL
[]{
url
}))
{
try
(
URLClassLoader
loader
=
new
URLClassLoader
(
new
URL
[]{
url
}))
{
URL
configFile
=
loader
.
findResource
(
"plugin.json"
);
URL
configFile
=
loader
.
findResource
(
"plugin.json"
);
// Find the plugin.json file for each plugin.
InputStreamReader
fileReader
=
new
InputStreamReader
(
configFile
.
openStream
());
InputStreamReader
fileReader
=
new
InputStreamReader
(
configFile
.
openStream
());
PluginConfig
pluginConfig
=
Grasscutter
.
getGsonFactory
().
fromJson
(
fileReader
,
PluginConfig
.
class
);
PluginConfig
pluginConfig
=
Grasscutter
.
getGsonFactory
().
fromJson
(
fileReader
,
PluginConfig
.
class
);
...
@@ -68,10 +79,10 @@ public final class PluginManager {
...
@@ -68,10 +79,10 @@ public final class PluginManager {
JarEntry
entry
=
entries
.
nextElement
();
JarEntry
entry
=
entries
.
nextElement
();
if
(
entry
.
isDirectory
()
||
!
entry
.
getName
().
endsWith
(
".class"
)
||
entry
.
getName
().
contains
(
"module-info"
))
continue
;
if
(
entry
.
isDirectory
()
||
!
entry
.
getName
().
endsWith
(
".class"
)
||
entry
.
getName
().
contains
(
"module-info"
))
continue
;
String
className
=
entry
.
getName
().
replace
(
".class"
,
""
).
replace
(
"/"
,
"."
);
String
className
=
entry
.
getName
().
replace
(
".class"
,
""
).
replace
(
"/"
,
"."
);
l
oader
.
loadClass
(
className
);
classL
oader
.
loadClass
(
className
);
//For all plugin we use the same class loader.
}
}
Class
<?>
pluginClass
=
l
oader
.
loadClass
(
pluginConfig
.
mainClass
);
Class
<?>
pluginClass
=
classL
oader
.
loadClass
(
pluginConfig
.
mainClass
);
Plugin
pluginInstance
=
(
Plugin
)
pluginClass
.
getDeclaredConstructor
().
newInstance
();
Plugin
pluginInstance
=
(
Plugin
)
pluginClass
.
getDeclaredConstructor
().
newInstance
();
this
.
loadPlugin
(
pluginInstance
,
PluginIdentifier
.
fromPluginConfig
(
pluginConfig
),
loader
);
this
.
loadPlugin
(
pluginInstance
,
PluginIdentifier
.
fromPluginConfig
(
pluginConfig
),
loader
);
...
@@ -156,6 +167,10 @@ public final class PluginManager {
...
@@ -156,6 +167,10 @@ public final class PluginManager {
.
toList
().
forEach
(
handler
->
this
.
invokeHandler
(
event
,
handler
));
.
toList
().
forEach
(
handler
->
this
.
invokeHandler
(
event
,
handler
));
}
}
public
Plugin
getPlugin
(
String
name
)
{
return
this
.
plugins
.
get
(
name
);
}
/**
/**
* Performs logic checks then invokes the provided event handler.
* Performs logic checks then invokes the provided event handler.
* @param event The event passed through to the handler.
* @param event The event passed through to the handler.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment