mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-17 06:11:24 +00:00
initialize manifest addons
This commit is contained in:
+26
-2
@@ -1,14 +1,26 @@
|
|||||||
package com.dfsek.terra.addons.manifest.impl;
|
package com.dfsek.terra.addons.manifest.impl;
|
||||||
|
|
||||||
|
import com.dfsek.terra.addons.manifest.api.AddonInitializer;
|
||||||
import com.dfsek.terra.addons.manifest.impl.config.AddonManifest;
|
import com.dfsek.terra.addons.manifest.impl.config.AddonManifest;
|
||||||
|
import com.dfsek.terra.api.Platform;
|
||||||
import com.dfsek.terra.api.addon.BaseAddon;
|
import com.dfsek.terra.api.addon.BaseAddon;
|
||||||
|
import com.dfsek.terra.api.inject.Injector;
|
||||||
|
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
public class ManifestAddon implements BaseAddon {
|
public class ManifestAddon implements BaseAddon {
|
||||||
private final AddonManifest manifest;
|
private final AddonManifest manifest;
|
||||||
|
|
||||||
public ManifestAddon(AddonManifest manifest) {
|
private final List<AddonInitializer> initializers;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private Platform platform;
|
||||||
|
|
||||||
|
public ManifestAddon(AddonManifest manifest, List<AddonInitializer> initializers) {
|
||||||
this.manifest = manifest;
|
this.manifest = manifest;
|
||||||
|
this.initializers = initializers;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -17,6 +29,18 @@ public class ManifestAddon implements BaseAddon {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
|
Injector<BaseAddon> addonInjector = Injector.get(this);
|
||||||
|
addonInjector.addExplicitTarget(BaseAddon.class);
|
||||||
|
|
||||||
|
Injector<Platform> platformInjector = Injector.get(platform);
|
||||||
|
platformInjector.addExplicitTarget(Platform.class);
|
||||||
|
|
||||||
|
platform.logger().info("Initializing addon " + getID());
|
||||||
|
|
||||||
|
initializers.forEach(initializer -> {
|
||||||
|
addonInjector.inject(initializer);
|
||||||
|
platformInjector.inject(initializer);
|
||||||
|
initializer.initialize();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -45,7 +45,7 @@ public class ManifestAddonLoader implements BootstrapBaseAddon<ManifestAddon> {
|
|||||||
try {
|
try {
|
||||||
return Files.walk(addonsFolder, 1)
|
return Files.walk(addonsFolder, 1)
|
||||||
.filter(path -> path.toFile().isFile() && path.toString().endsWith(".jar"))
|
.filter(path -> path.toFile().isFile() && path.toString().endsWith(".jar"))
|
||||||
.flatMap(path -> {
|
.map(path -> {
|
||||||
try {
|
try {
|
||||||
platform.getDebugLogger().info("Loading addon from JAR " + path);
|
platform.getDebugLogger().info("Loading addon from JAR " + path);
|
||||||
JarFile jar = new JarFile(path.toFile());
|
JarFile jar = new JarFile(path.toFile());
|
||||||
@@ -66,14 +66,14 @@ public class ManifestAddonLoader implements BootstrapBaseAddon<ManifestAddon> {
|
|||||||
|
|
||||||
ManifestAddonClassLoader loader = new ManifestAddonClassLoader(new URL[]{ path.toUri().toURL() },
|
ManifestAddonClassLoader loader = new ManifestAddonClassLoader(new URL[]{ path.toUri().toURL() },
|
||||||
getClass().getClassLoader());
|
getClass().getClassLoader());
|
||||||
|
|
||||||
return manifest.getEntryPoints().stream().map(entryPoint -> {
|
return new ManifestAddon(manifest, manifest.getEntryPoints().stream().map(entryPoint -> {
|
||||||
try {
|
try {
|
||||||
Object in = loader.loadClass(entryPoint).getConstructor().newInstance();
|
Object in = loader.loadClass(entryPoint).getConstructor().newInstance();
|
||||||
if(!(in instanceof AddonInitializer)) {
|
if(!(in instanceof AddonInitializer)) {
|
||||||
throw new AddonException(in.getClass() + " does not extend " + AddonInitializer.class);
|
throw new AddonException(in.getClass() + " does not extend " + AddonInitializer.class);
|
||||||
}
|
}
|
||||||
return new ManifestAddon(manifest);
|
return (AddonInitializer) in;
|
||||||
} catch(InvocationTargetException e) {
|
} catch(InvocationTargetException e) {
|
||||||
throw new AddonException("Exception occurred while instantiating addon: ", e);
|
throw new AddonException("Exception occurred while instantiating addon: ", e);
|
||||||
} catch(NoSuchMethodException | IllegalAccessException | InstantiationException e) {
|
} catch(NoSuchMethodException | IllegalAccessException | InstantiationException e) {
|
||||||
@@ -81,7 +81,7 @@ public class ManifestAddonLoader implements BootstrapBaseAddon<ManifestAddon> {
|
|||||||
} catch(ClassNotFoundException e) {
|
} catch(ClassNotFoundException e) {
|
||||||
throw new AddonException("Entry point " + entryPoint + " not found in JAR.");
|
throw new AddonException("Entry point " + entryPoint + " not found in JAR.");
|
||||||
}
|
}
|
||||||
});
|
}).collect(Collectors.toList()));
|
||||||
|
|
||||||
} catch(LoadException e) {
|
} catch(LoadException e) {
|
||||||
throw new ManifestException("Failed to load addon manifest", e);
|
throw new ManifestException("Failed to load addon manifest", e);
|
||||||
|
|||||||
Reference in New Issue
Block a user