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
fe055af5
Commit
fe055af5
authored
Apr 26, 2022
by
Magix
Committed by
GitHub
Apr 26, 2022
Browse files
Merge pull request #262 from exzork/plugin-system
load all classes from plugin
parents
57c7c704
9fb5b69d
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/plugin/PluginManager.java
View file @
fe055af5
...
@@ -13,6 +13,8 @@ import java.lang.reflect.Method;
...
@@ -13,6 +13,8 @@ import java.lang.reflect.Method;
import
java.net.URL
;
import
java.net.URL
;
import
java.net.URLClassLoader
;
import
java.net.URLClassLoader
;
import
java.util.*
;
import
java.util.*
;
import
java.util.jar.JarEntry
;
import
java.util.jar.JarFile
;
/**
/**
* Manages the server's plugins & the event system.
* Manages the server's plugins & the event system.
...
@@ -52,13 +54,22 @@ public final class PluginManager {
...
@@ -52,13 +54,22 @@ public final class PluginManager {
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"
);
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
);
if
(!
pluginConfig
.
validate
())
{
if
(!
pluginConfig
.
validate
())
{
Utils
.
logObject
(
pluginConfig
);
Utils
.
logObject
(
pluginConfig
);
Grasscutter
.
getLogger
().
warn
(
"Plugin "
+
plugin
.
getName
()
+
" has an invalid config file."
);
Grasscutter
.
getLogger
().
warn
(
"Plugin "
+
plugin
.
getName
()
+
" has an invalid config file."
);
return
;
return
;
}
}
JarFile
jarFile
=
new
JarFile
(
plugin
);
Enumeration
<
JarEntry
>
entries
=
jarFile
.
entries
();
while
(
entries
.
hasMoreElements
())
{
JarEntry
entry
=
entries
.
nextElement
();
if
(
entry
.
isDirectory
()
||
!
entry
.
getName
().
endsWith
(
".class"
))
continue
;
String
className
=
entry
.
getName
().
replace
(
".class"
,
""
).
replace
(
"/"
,
"."
);
Class
<?>
clazz
=
loader
.
loadClass
(
className
);
}
Class
<?>
pluginClass
=
loader
.
loadClass
(
pluginConfig
.
mainClass
);
Class
<?>
pluginClass
=
loader
.
loadClass
(
pluginConfig
.
mainClass
);
Plugin
pluginInstance
=
(
Plugin
)
pluginClass
.
getDeclaredConstructor
().
newInstance
();
Plugin
pluginInstance
=
(
Plugin
)
pluginClass
.
getDeclaredConstructor
().
newInstance
();
...
...
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