load bootstrap addons

This commit is contained in:
dfsek 2021-11-17 08:52:23 -07:00
parent 92341751fc
commit c0bcc40f6a
2 changed files with 14 additions and 3 deletions

View File

@ -192,7 +192,7 @@ public abstract class AbstractPlatform implements Platform {
addonRegistry.register(internalAddon.getID(), internalAddon); addonRegistry.register(internalAddon.getID(), internalAddon);
BootstrapAddonLoader bootstrapAddonLoader = new BootstrapAddonLoader(); BootstrapAddonLoader bootstrapAddonLoader = new BootstrapAddonLoader(this);
Path addonsFolder = getDataFolder().toPath().resolve("addons"); Path addonsFolder = getDataFolder().toPath().resolve("addons");

View File

@ -1,6 +1,7 @@
package com.dfsek.terra.addon; package com.dfsek.terra.addon;
import com.dfsek.terra.addon.exception.AddonLoadException; import com.dfsek.terra.addon.exception.AddonLoadException;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.addon.bootstrap.BootstrapBaseAddon; import com.dfsek.terra.api.addon.bootstrap.BootstrapBaseAddon;
import java.io.IOException; import java.io.IOException;
@ -14,17 +15,27 @@ import java.util.stream.Collectors;
public class BootstrapAddonLoader implements BootstrapBaseAddon<BootstrapBaseAddon<?>> { public class BootstrapAddonLoader implements BootstrapBaseAddon<BootstrapBaseAddon<?>> {
private final Platform platform;
public BootstrapAddonLoader(Platform platform) { this.platform = platform; }
@Override @Override
public Iterable<BootstrapBaseAddon<?>> loadAddons(Path addonsFolder, ClassLoader parent) { public Iterable<BootstrapBaseAddon<?>> loadAddons(Path addonsFolder, ClassLoader parent) {
Path bootstrapAddons = addonsFolder.resolve("bootstrap"); Path bootstrapAddons = addonsFolder.resolve("bootstrap");
platform.logger().info("Loading bootstrap addons from " + bootstrapAddons);
try { try {
return Files.walk(bootstrapAddons, 1) return Files.walk(bootstrapAddons, 1)
.filter(path -> path.toFile().isFile() && path.getFileName().endsWith(".jar")) .filter(path -> path.toFile().isFile() && path.toString().endsWith(".jar"))
.map(path -> { .map(path -> {
try { try {
platform.logger().info("Loading bootstrap addon from JAR " + path);
JarFile jar = new JarFile(path.toFile()); JarFile jar = new JarFile(path.toFile());
String entry = jar.getManifest().getMainAttributes().getValue("Bootstrap-Addon-Entry-Point"); String entry = jar.getManifest().getMainAttributes().getValue("Bootstrap-Addon-Entry-Point");
if(entry == null) {
throw new AddonLoadException("No Bootstrap-Addon-Entry-Point attribute defined in addon manifest.");
}
AddonClassLoader loader = new AddonClassLoader(new URL[] {path.toUri().toURL()}, parent); AddonClassLoader loader = new AddonClassLoader(new URL[] {path.toUri().toURL()}, parent);
try { try {