3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-12-26 00:00:55 +01:00

Clean up and clarify plugin loader logic.

Dieser Commit ist enthalten in:
Andrew Steinborn 2021-10-31 23:24:39 -04:00
Ursprung 0b0c36dcfc
Commit 6b2b28796b
4 geänderte Dateien mit 31 neuen und 19 gelöschten Zeilen

Datei anzeigen

@ -22,7 +22,6 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.velocitypowered.api.event.EventManager;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.proxy.ProxyReloadEvent; import com.velocitypowered.api.event.proxy.ProxyReloadEvent;
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;

Datei anzeigen

@ -43,7 +43,6 @@ import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -58,7 +57,7 @@ public class VelocityPluginManager implements PluginManager {
private static final Logger logger = LogManager.getLogger(VelocityPluginManager.class); private static final Logger logger = LogManager.getLogger(VelocityPluginManager.class);
private final Map<String, PluginContainer> plugins = new LinkedHashMap<>(); private final Map<String, PluginContainer> pluginsById = new LinkedHashMap<>();
private final Map<Object, PluginContainer> pluginInstances = new IdentityHashMap<>(); private final Map<Object, PluginContainer> pluginInstances = new IdentityHashMap<>();
private final VelocityServer server; private final VelocityServer server;
@ -67,7 +66,7 @@ public class VelocityPluginManager implements PluginManager {
} }
private void registerPlugin(PluginContainer plugin) { private void registerPlugin(PluginContainer plugin) {
plugins.put(plugin.getDescription().getId(), plugin); pluginsById.put(plugin.getDescription().getId(), plugin);
Optional<?> instance = plugin.getInstance(); Optional<?> instance = plugin.getInstance();
instance.ifPresent(o -> pluginInstances.put(o, plugin)); instance.ifPresent(o -> pluginInstances.put(o, plugin));
} }
@ -90,7 +89,7 @@ public class VelocityPluginManager implements PluginManager {
p -> p.toFile().isFile() && p.toString().endsWith(".jar"))) { p -> p.toFile().isFile() && p.toString().endsWith(".jar"))) {
for (Path path : stream) { for (Path path : stream) {
try { try {
found.add(loader.loadPluginDescription(path)); found.add(loader.loadCandidate(path));
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to load plugin {}", path, e); logger.error("Unable to load plugin {}", path, e);
} }
@ -119,7 +118,7 @@ public class VelocityPluginManager implements PluginManager {
} }
try { try {
PluginDescription realPlugin = loader.loadPlugin(candidate); PluginDescription realPlugin = loader.createPluginFromCandidate(candidate);
VelocityPluginContainer container = new VelocityPluginContainer(realPlugin); VelocityPluginContainer container = new VelocityPluginContainer(realPlugin);
pluginContainers.put(container, loader.createModule(container)); pluginContainers.put(container, loader.createModule(container));
loadedPluginsById.add(realPlugin.getId()); loadedPluginsById.add(realPlugin.getId());
@ -175,17 +174,17 @@ public class VelocityPluginManager implements PluginManager {
@Override @Override
public Optional<PluginContainer> getPlugin(String id) { public Optional<PluginContainer> getPlugin(String id) {
checkNotNull(id, "id"); checkNotNull(id, "id");
return Optional.ofNullable(plugins.get(id)); return Optional.ofNullable(pluginsById.get(id));
} }
@Override @Override
public Collection<PluginContainer> getPlugins() { public Collection<PluginContainer> getPlugins() {
return Collections.unmodifiableCollection(plugins.values()); return Collections.unmodifiableCollection(pluginsById.values());
} }
@Override @Override
public boolean isLoaded(String id) { public boolean isLoaded(String id) {
return plugins.containsKey(id); return pluginsById.containsKey(id);
} }
@Override @Override

Datei anzeigen

@ -27,9 +27,23 @@ import java.nio.file.Path;
*/ */
public interface PluginLoader { public interface PluginLoader {
PluginDescription loadPluginDescription(Path source) throws Exception; /**
* Loads a candidate description from the given {@code source}.
*
* @param source the source to load the candidate from
* @return a plugin candidate description
* @throws Exception if anything goes wrong
*/
PluginDescription loadCandidate(Path source) throws Exception;
PluginDescription loadPlugin(PluginDescription source) throws Exception; /**
* Materializes a "real" plugin description from the given {@code candidate}.
*
* @param candidate the candidate to materialize
* @return a plugin description
* @throws Exception if anything goes wrong
*/
PluginDescription createPluginFromCandidate(PluginDescription candidate) throws Exception;
/** /**
* Creates a {@link Module} for the provided {@link PluginContainer} * Creates a {@link Module} for the provided {@link PluginContainer}

Datei anzeigen

@ -57,7 +57,7 @@ public class JavaPluginLoader implements PluginLoader {
} }
@Override @Override
public PluginDescription loadPluginDescription(Path source) throws Exception { public PluginDescription loadCandidate(Path source) throws Exception {
Optional<SerializedPluginDescription> serialized = getSerializedPluginInfo(source); Optional<SerializedPluginDescription> serialized = getSerializedPluginInfo(source);
if (!serialized.isPresent()) { if (!serialized.isPresent()) {
@ -73,20 +73,20 @@ public class JavaPluginLoader implements PluginLoader {
} }
@Override @Override
public PluginDescription loadPlugin(PluginDescription source) throws Exception { public PluginDescription createPluginFromCandidate(PluginDescription candidate) throws Exception {
if (!(source instanceof JavaVelocityPluginDescriptionCandidate)) { if (!(candidate instanceof JavaVelocityPluginDescriptionCandidate)) {
throw new IllegalArgumentException("Description provided isn't of the Java plugin loader"); throw new IllegalArgumentException("Description provided isn't of the Java plugin loader");
} }
URL pluginJarUrl = source.getSource().get().toUri().toURL(); URL pluginJarUrl = candidate.getSource().get().toUri().toURL();
PluginClassLoader loader = AccessController.doPrivileged( PluginClassLoader loader = AccessController.doPrivileged(
(PrivilegedAction<PluginClassLoader>) () -> new PluginClassLoader(new URL[]{pluginJarUrl})); (PrivilegedAction<PluginClassLoader>) () -> new PluginClassLoader(new URL[]{pluginJarUrl}));
loader.addToClassloaders(); loader.addToClassloaders();
JavaVelocityPluginDescriptionCandidate candidate = JavaVelocityPluginDescriptionCandidate candidateInst =
(JavaVelocityPluginDescriptionCandidate) source; (JavaVelocityPluginDescriptionCandidate) candidate;
Class mainClass = loader.loadClass(candidate.getMainClass()); Class mainClass = loader.loadClass(candidateInst.getMainClass());
return createDescription(candidate, mainClass); return createDescription(candidateInst, mainClass);
} }
@Override @Override