From 83126454eae37d39d37c544a0c8eda019de8e426 Mon Sep 17 00:00:00 2001 From: dfsek Date: Thu, 18 Nov 2021 21:13:32 -0700 Subject: [PATCH] addon dependency sorting --- .../addons/manifest/impl/ManifestAddon.java | 18 ++++ .../manifest/impl/ManifestAddonLoader.java | 98 ++++++++++--------- .../manifest/impl/config/AddonManifest.java | 2 +- 3 files changed, 72 insertions(+), 46 deletions(-) diff --git a/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/impl/ManifestAddon.java b/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/impl/ManifestAddon.java index 509e5bc73..7fcb80385 100644 --- a/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/impl/ManifestAddon.java +++ b/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/impl/ManifestAddon.java @@ -1,6 +1,10 @@ package com.dfsek.terra.addons.manifest.impl; import java.util.List; +import java.util.Map; + +import ca.solostudios.strata.version.Version; +import ca.solostudios.strata.version.VersionRange; import com.dfsek.terra.addons.manifest.api.AddonInitializer; import com.dfsek.terra.addons.manifest.impl.config.AddonManifest; @@ -43,4 +47,18 @@ public class ManifestAddon implements BaseAddon { initializer.initialize(); }); } + + public AddonManifest getManifest() { + return manifest; + } + + @Override + public Map getDependencies() { + return manifest.getDependencies(); + } + + @Override + public Version getVersion() { + return manifest.getVersion(); + } } diff --git a/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/impl/ManifestAddonLoader.java b/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/impl/ManifestAddonLoader.java index 60b41076a..b6768b978 100644 --- a/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/impl/ManifestAddonLoader.java +++ b/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/impl/ManifestAddonLoader.java @@ -1,5 +1,6 @@ package com.dfsek.terra.addons.manifest.impl; +import ca.solostudios.strata.Versions; import ca.solostudios.strata.version.Version; import ca.solostudios.strata.version.VersionRange; import com.dfsek.tectonic.exception.LoadException; @@ -33,6 +34,8 @@ public class ManifestAddonLoader implements BootstrapBaseAddon { @Inject private Platform platform; + private static final Version VERSION = Versions.getVersion(1, 0, 0); + @Override public Iterable loadAddons(Path addonsFolder, ClassLoader parent) { platform.logger().info("Loading addons..."); @@ -44,51 +47,51 @@ public class ManifestAddonLoader implements BootstrapBaseAddon { try { return Files.walk(addonsFolder, 1) - .filter(path -> path.toFile().isFile() && path.toString().endsWith(".jar")) - .map(path -> { - try { - platform.getDebugLogger().info("Loading addon from JAR " + path); - JarFile jar = new JarFile(path.toFile()); - - JarEntry manifestEntry = jar.getJarEntry("terra.addon.yml"); - if(manifestEntry == null) { - throw new ManifestNotPresentException("Addon " + path + " does not contain addon manifest."); - } - - - try { - AddonManifest manifest = manifestLoader.load(new AddonManifest(), - new YamlConfiguration(jar.getInputStream(manifestEntry), - "terra.addon.yml")); - - platform.logger().info("Loading addon " + manifest.getID()); - - ManifestAddonClassLoader loader = new ManifestAddonClassLoader(new URL[]{ path.toUri().toURL() }, - getClass().getClassLoader()); - - return new ManifestAddon(manifest, manifest.getEntryPoints().stream().map(entryPoint -> { - try { - Object in = loader.loadClass(entryPoint).getConstructor().newInstance(); - if(!(in instanceof AddonInitializer)) { - throw new AddonException(in.getClass() + " does not extend " + AddonInitializer.class); - } - return (AddonInitializer) in; - } catch(InvocationTargetException e) { - throw new AddonException("Exception occurred while instantiating addon: ", e); - } catch(NoSuchMethodException | IllegalAccessException | InstantiationException e) { - throw new AddonException("No valid default constructor found in entry point " + entryPoint); - } catch(ClassNotFoundException e) { - throw new AddonException("Entry point " + entryPoint + " not found in JAR."); - } - }).collect(Collectors.toList())); - - } catch(LoadException e) { - throw new ManifestException("Failed to load addon manifest", e); - } - } catch(IOException e) { - throw new UncheckedIOException(e); - } - }).collect(Collectors.toList()); + .filter(path -> path.toFile().isFile() && path.toString().endsWith(".jar")) + .map(path -> { + try { + platform.getDebugLogger().info("Loading addon from JAR " + path); + JarFile jar = new JarFile(path.toFile()); + + JarEntry manifestEntry = jar.getJarEntry("terra.addon.yml"); + if(manifestEntry == null) { + throw new ManifestNotPresentException("Addon " + path + " does not contain addon manifest."); + } + + + try { + AddonManifest manifest = manifestLoader.load(new AddonManifest(), + new YamlConfiguration(jar.getInputStream(manifestEntry), + "terra.addon.yml")); + + platform.logger().info("Loading addon " + manifest.getID()); + + ManifestAddonClassLoader loader = new ManifestAddonClassLoader(new URL[]{ path.toUri().toURL() }, + getClass().getClassLoader()); + + return new ManifestAddon(manifest, manifest.getEntryPoints().stream().map(entryPoint -> { + try { + Object in = loader.loadClass(entryPoint).getConstructor().newInstance(); + if(!(in instanceof AddonInitializer)) { + throw new AddonException(in.getClass() + " does not extend " + AddonInitializer.class); + } + return (AddonInitializer) in; + } catch(InvocationTargetException e) { + throw new AddonException("Exception occurred while instantiating addon: ", e); + } catch(NoSuchMethodException | IllegalAccessException | InstantiationException e) { + throw new AddonException("No valid default constructor found in entry point " + entryPoint); + } catch(ClassNotFoundException e) { + throw new AddonException("Entry point " + entryPoint + " not found in JAR."); + } + }).collect(Collectors.toList())); + + } catch(LoadException e) { + throw new ManifestException("Failed to load addon manifest", e); + } + } catch(IOException e) { + throw new UncheckedIOException(e); + } + }).collect(Collectors.toList()); } catch(IOException e) { throw new UncheckedIOException(e); } @@ -98,4 +101,9 @@ public class ManifestAddonLoader implements BootstrapBaseAddon { public String getID() { return "MANIFEST"; } + + @Override + public Version getVersion() { + return VERSION; + } } diff --git a/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/impl/config/AddonManifest.java b/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/impl/config/AddonManifest.java index 2481ae3a2..a6036ed87 100644 --- a/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/impl/config/AddonManifest.java +++ b/common/addons/manifest-addon-loader/src/main/java/com/dfsek/terra/addons/manifest/impl/config/AddonManifest.java @@ -36,7 +36,7 @@ public class AddonManifest implements ConfigTemplate, StringIdentifiable { @Value("depends") @Default - private Map dependencies; + private Map dependencies = Collections.emptyMap(); @Value("website") private WebsiteConfig website;