-
Notifications
You must be signed in to change notification settings - Fork 419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a Simple Plugin System #972
Open
MagicLu550
wants to merge
17
commits into
CloudburstMC:master
Choose a base branch
from
Server-Founder:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e6f18e6
add the simple plugin class loader
9aaa39d
Add the simple plugin class loader [#2 finished]
dfb4480
Add the simple plugin class loader [#3 bug fixed]
445feb6
Add some comments
cae8e06
Fix a bug that couldn't register the command
b1cd631
Fix a bug that could cause NullPointerException
0fc0461
Added a new feature to simplify some development
9936cf1
Added a new feature to simplify some development: You can use SimpleC…
6e37bf6
Add a Annotation: EnableRegister,can register the listener automatica…
28cf557
Permission: add the children tags,Command: add the `permissionMessage`
5221242
You can use Plugin.yml while using the content of SimplePluginSystem
8a08b18
Update AutoPlugin.class,You can directly get the main class object th…
679555b
modify TYPE to METHOD
4bbb256
modify METHOD to FIELD
20c4dea
Fix a bug: modify getField to getDeclaredFiled
7e4fed0
add some comments
3d6ecfb
Code duplication
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
package cn.nukkit.plugin; | ||
|
||
import cn.nukkit.Server; | ||
import cn.nukkit.event.Listener; | ||
import cn.nukkit.event.plugin.PluginDisableEvent; | ||
import cn.nukkit.event.plugin.PluginEnableEvent; | ||
import cn.nukkit.plugin.simple.Command; | ||
import cn.nukkit.plugin.simple.EnableRegister; | ||
import cn.nukkit.plugin.simple.Main; | ||
import cn.nukkit.plugin.simple.Permission; | ||
import cn.nukkit.utils.PluginException; | ||
import cn.nukkit.utils.Utils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.*; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
import java.util.regex.Pattern; | ||
|
@@ -25,6 +29,10 @@ public class JavaPluginLoader implements PluginLoader { | |
private final Map<String, Class> classes = new HashMap<>(); | ||
private final Map<String, PluginClassLoader> classLoaders = new HashMap<>(); | ||
|
||
private final Map<File,Class> loadedSimplePlugin = new HashMap<>(); | ||
|
||
private final Map<File,List<Class>> simplePluginEnableClasses = new HashMap<>(); | ||
|
||
public JavaPluginLoader(Server server) { | ||
this.server = server; | ||
} | ||
|
@@ -46,9 +54,7 @@ public Plugin loadPlugin(File file) throws Exception { | |
try { | ||
Class javaClass = classLoader.loadClass(className); | ||
|
||
if (!PluginBase.class.isAssignableFrom(javaClass)) { | ||
throw new PluginException("Main class `" + description.getMain() + "' does not extend PluginBase"); | ||
} | ||
PluginAssert.isPluginBaseChild(javaClass,description.getMain()); | ||
|
||
try { | ||
Class<PluginBase> pluginClass = (Class<PluginBase>) javaClass.asSubclass(PluginBase.class); | ||
|
@@ -64,13 +70,148 @@ public Plugin loadPlugin(File file) throws Exception { | |
} | ||
|
||
} catch (ClassNotFoundException e) { | ||
throw new PluginException("Couldn't load plugin " + description.getName() + ": main class not found"); | ||
PluginAssert.findMainClass(description.getName()); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
//simple load Plugin | ||
@Override | ||
public Plugin simpleLoadPlugin(File file) { | ||
try { | ||
Class<?> pluginClass = getSimplePlugin(file); | ||
PluginDescription pluginDescription = getSimpleDescription(pluginClass); | ||
this.server.getLogger().info(this.server.getLanguage().translateString("nukkit.plugin.load", pluginDescription.getFullName())); | ||
File dataFolder = new File(file.getParentFile(), pluginDescription.getName()); | ||
PluginBase plugin = (PluginBase) pluginClass.newInstance(); | ||
this.initPlugin(plugin,pluginDescription,dataFolder,file); | ||
return plugin; | ||
}catch (InstantiationException | IllegalAccessException e){ | ||
throw new PluginException(e.getMessage()); | ||
} | ||
//do it | ||
} | ||
|
||
@Override | ||
public Plugin simpleLoadPlugin(String fileName) { | ||
return simpleLoadPlugin(new File(fileName)); | ||
} | ||
|
||
private Class getSimplePlugin(File file){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Get and complete the auto-enrollment section |
||
if(loadedSimplePlugin.containsKey(file)){ | ||
return loadedSimplePlugin.get(file); | ||
} | ||
try(JarFile jarFile = new JarFile(file)){ | ||
PluginClassLoader classLoader = new PluginClassLoader(this, this.getClass().getClassLoader(),file); | ||
Enumeration<JarEntry> entries = jarFile.entries(); | ||
boolean isEnableRegister = false; | ||
boolean hasGeted = false; | ||
List<Class> haveLoaded = new ArrayList<>(); | ||
Class<?> mainClass = null; | ||
while (entries.hasMoreElements()){ | ||
String name = entries.nextElement().getName(); | ||
if(name.endsWith(".class")) { | ||
String mainName = name.substring(0, name.lastIndexOf(".")).replace("/", "."); | ||
Class<?> clz = classLoader.loadClass(mainName); | ||
Main main = clz.getAnnotation(Main.class); | ||
haveLoaded.add(clz); | ||
if(main != null){ | ||
loadedSimplePlugin.put(file,clz); | ||
PluginAssert.isPluginBaseChild(clz,mainName); | ||
mainClass = clz; | ||
} | ||
} | ||
} | ||
EnableRegister register; | ||
List<Class<? extends Listener>> noRegister = null; | ||
if(mainClass!=null) { | ||
for (Class<?> clz1 : haveLoaded) { | ||
if (!hasGeted) { | ||
register = mainClass.getAnnotation(EnableRegister.class); | ||
isEnableRegister = register != null; | ||
hasGeted = true; | ||
List<Class> classes = new ArrayList<>(); | ||
simplePluginEnableClasses.put(file, classes); | ||
if (register != null) { | ||
noRegister = Arrays.asList(register.noRegister()); | ||
} | ||
} | ||
if (isEnableRegister) { | ||
|
||
if (Arrays.asList(clz1.getInterfaces()).contains(Listener.class)) { | ||
if (!noRegister.contains(clz1)) { | ||
simplePluginEnableClasses.get(file).add(clz1); | ||
} | ||
} | ||
} | ||
} | ||
return mainClass; | ||
} | ||
PluginAssert.findMainClass(""); | ||
}catch (IOException|ClassNotFoundException e){ | ||
throw new PluginException(e.getMessage()); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* the simple description for simple plugin system | ||
* @param plugin the main class | ||
* @return the description for simple plugin system | ||
*/ | ||
private PluginDescription getSimpleDescription(Class<?> plugin){ | ||
Main main = plugin.getAnnotation(Main.class); | ||
if(main == null){ | ||
return null; | ||
} | ||
String name = main.name(); | ||
String version = main.version(); | ||
String author = main.author(); | ||
String description = main.description(); | ||
String pluginLoadOrder = main.load(); | ||
String website = main.website(); | ||
String prefix = main.prefix(); | ||
List<String> api = Arrays.asList(main.api()); | ||
List<String> depend = Arrays.asList(main.depend()); | ||
List<String> loadBefore = Arrays.asList(main.loadBefore()); | ||
List<String> softDepend = Arrays.asList(main.softDepend()); | ||
Map<String,Object> hashMap = new HashMap<>(); | ||
hashMap.put("name",name); | ||
hashMap.put("version",version); | ||
hashMap.put("author",author); | ||
hashMap.put("api",api); | ||
hashMap.put("depend",depend); | ||
hashMap.put("loadBefore",loadBefore); | ||
hashMap.put("softDepend",softDepend); | ||
hashMap.put("description",description); | ||
hashMap.put("load",pluginLoadOrder); | ||
hashMap.put("website",website); | ||
hashMap.put("prefix",prefix.equals("")?name:prefix); | ||
hashMap.put("main",plugin.getName()); | ||
hashMap.put("isSimple",true); | ||
PluginDescription descript = new PluginDescription(hashMap); | ||
Permission[] permissions = main.permissions(); | ||
hashMap.put("permissions",parsePermission(permissions)); | ||
Command[] commands = main.commands(); | ||
for(Command command:commands){ | ||
descript.getCommands().put(command.name(),command); | ||
} | ||
return descript; | ||
} | ||
|
||
public static HashMap<String,Object> parsePermission(Permission[] permissions){ | ||
HashMap<String,Object> pers = new HashMap<>(); | ||
for(Permission p:permissions){ | ||
HashMap<String,Object> pers_child = new HashMap<>(); | ||
pers_child.put("description",p.description()); | ||
pers_child.put("default",p.theDefault()); | ||
pers.put(p.permission(),pers_child); | ||
} | ||
return pers; | ||
} | ||
|
||
@Override | ||
public Plugin loadPlugin(String filename) throws Exception { | ||
return this.loadPlugin(new File(filename)); | ||
|
@@ -83,6 +224,10 @@ public PluginDescription getPluginDescription(File file) { | |
if (entry == null) { | ||
entry = jar.getJarEntry("plugin.yml"); | ||
if (entry == null) { | ||
Class clz = getSimplePlugin(file); | ||
if(clz != null){ | ||
return getSimpleDescription(clz); | ||
} | ||
return null; | ||
} | ||
} | ||
|
@@ -116,6 +261,17 @@ public void enablePlugin(Plugin plugin) { | |
|
||
((PluginBase) plugin).setEnabled(true); | ||
|
||
try { | ||
if (plugin.getDescription().isSimple()) { | ||
List<Class> listeners = simplePluginEnableClasses.get(((PluginBase) plugin).getFile()); | ||
for (Class clz : listeners) { | ||
server.getPluginManager().registerEvents((Listener) clz.newInstance(), plugin); | ||
} | ||
} | ||
}catch (InstantiationException|IllegalAccessException e){ | ||
throw new PluginException(e.getMessage()); | ||
} | ||
|
||
this.server.getPluginManager().callEvent(new PluginEnableEvent(plugin)); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cn.nukkit.plugin; | ||
|
||
import cn.nukkit.utils.PluginException; | ||
|
||
public class PluginAssert { | ||
|
||
static void isPluginBaseChild(Class<?> javaClass,String main){ | ||
if (!PluginBase.class.isAssignableFrom(javaClass)) { | ||
throw new PluginException("Main class `" + main + "' does not extend PluginBase"); | ||
} | ||
} | ||
|
||
static void findMainClass(String name) throws PluginException{ | ||
throw new PluginException("Couldn't load plugin "+name+": main class not found"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the core part of loading the simple plugin, initializing the plugin information.