Skip to content

Commit

Permalink
Merge pull request Grasscutters#628 from exzork/development
Browse files Browse the repository at this point in the history
PluginManager: Use the same class loader and add getPlugin method
  • Loading branch information
KingRainbow44 committed May 7, 2022
2 parents 75032b4 + f133a8b commit d70df77
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/main/java/emu/grasscutter/plugin/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import emu.grasscutter.server.event.Event;
import emu.grasscutter.server.event.EventHandler;
import emu.grasscutter.server.event.HandlerPriority;
import emu.grasscutter.utils.EventConsumer;
import emu.grasscutter.utils.Utils;

import java.io.File;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
Expand Down Expand Up @@ -47,12 +47,23 @@ private void loadPlugins() {
List<File> plugins = Arrays.stream(files)
.filter(file -> file.getName().endsWith(".jar"))
.toList();


URL[] pluginNames = new URL[plugins.size()];
plugins.forEach(plugin -> {
try {
pluginNames[plugins.indexOf(plugin)] = plugin.toURI().toURL();
} catch (MalformedURLException exception) {
Grasscutter.getLogger().warn("Unable to load plugin.", exception);
}
});

URLClassLoader classLoader = new URLClassLoader(pluginNames);

plugins.forEach(plugin -> {
try {
URL url = plugin.toURI().toURL();
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());

PluginConfig pluginConfig = Grasscutter.getGsonFactory().fromJson(fileReader, PluginConfig.class);
Expand All @@ -68,10 +79,10 @@ private void loadPlugins() {
JarEntry entry = entries.nextElement();
if(entry.isDirectory() || !entry.getName().endsWith(".class") || entry.getName().contains("module-info")) continue;
String className = entry.getName().replace(".class", "").replace("/", ".");
loader.loadClass(className);
classLoader.loadClass(className); // Use the same class loader for ALL plugins.
}

Class<?> pluginClass = loader.loadClass(pluginConfig.mainClass);
Class<?> pluginClass = classLoader.loadClass(pluginConfig.mainClass);
Plugin pluginInstance = (Plugin) pluginClass.getDeclaredConstructor().newInstance();
this.loadPlugin(pluginInstance, PluginIdentifier.fromPluginConfig(pluginConfig), loader);

Expand Down Expand Up @@ -156,6 +167,10 @@ private void checkAndFilter(Event event, HandlerPriority priority) {
.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.
* @param event The event passed through to the handler.
Expand All @@ -167,4 +182,4 @@ private <T extends Event> void invokeHandler(Event event, EventHandler<T> handle
(event.isCanceled() && handler.ignoresCanceled())
) handler.getCallback().consume((T) event);
}
}
}

0 comments on commit d70df77

Please sign in to comment.