generify BootstrapBaseAddon

This commit is contained in:
dfsek
2021-11-17 08:22:06 -07:00
parent e211b27a80
commit 4f4dc45a48
10 changed files with 56 additions and 45 deletions

View File

@@ -5,12 +5,12 @@ import com.dfsek.terra.api.addon.BaseAddon;
import java.nio.file.Path;
public interface BootstrapBaseAddon extends BaseAddon {
public interface BootstrapBaseAddon<T extends BaseAddon> extends BaseAddon {
/**
* Load all the relevant addons in the specified path.
* @param addonsFolder Path containing addons.
* @param parent
* @return Loaded addons
*/
Iterable<BaseAddon> loadAddons(Path addonsFolder, ClassLoader parent);
Iterable<T> loadAddons(Path addonsFolder, ClassLoader parent);
}

View File

@@ -2,6 +2,7 @@ dependencies {
"shadedApi"(project(":common:api:util"))
"shadedApi"(project(":common:api:noise"))
"shadedApi"(project(":common:api:registry"))
"shadedApi"(project(":common:api:addons"))
"shadedApi"("com.dfsek:Paralithic:0.5.0")

View File

@@ -2,6 +2,7 @@ package com.dfsek.terra.api;
import java.io.File;
import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.addon.TerraAddon;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.PluginConfig;
@@ -47,7 +48,7 @@ public interface Platform extends LoaderRegistrar {
CheckedRegistry<ConfigPack> getConfigRegistry();
Registry<TerraAddon> getAddons();
Registry<BaseAddon> getAddons();
ItemHandle getItemHandle();

View File

@@ -11,7 +11,7 @@ import com.dfsek.terra.api.addon.annotations.Version;
/**
* Represents an entry point for an com.dfsek.terra.addon. Implementations must be annotated with {@link Addon}.
*/
public abstract class TerraAddon {
public abstract class TerraAddon implements BaseAddon {
/**
* Invoked immediately after an com.dfsek.terra.addon is loaded.
*/
@@ -50,4 +50,9 @@ public abstract class TerraAddon {
// .dfsek.terra.addon loader.
return addon.value();
}
@Override
public String getID() {
return getName();
}
}

View File

@@ -1,5 +1,6 @@
package com.dfsek.terra.api.event.functional;
import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.addon.TerraAddon;
import com.dfsek.terra.api.event.EventHandler;
import com.dfsek.terra.api.event.events.Event;
@@ -7,7 +8,7 @@ import com.dfsek.terra.api.util.reflection.TypeKey;
public interface FunctionalEventHandler extends EventHandler {
<T extends Event> EventContext<T> register(TerraAddon addon, Class<T> clazz);
<T extends Event> EventContext<T> register(BaseAddon addon, Class<T> clazz);
<T extends Event> EventContext<T> register(TerraAddon addon, TypeKey<T> clazz);
<T extends Event> EventContext<T> register(BaseAddon addon, TypeKey<T> clazz);
}

View File

@@ -2,8 +2,16 @@ package com.dfsek.terra;
import com.dfsek.tectonic.loading.TypeRegistry;
import com.dfsek.terra.addon.BootstrapAddonLoader;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.event.events.platform.PlatformInitializationEvent;
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
import com.dfsek.terra.registry.LockedRegistryImpl;
import com.dfsek.terra.registry.OpenRegistryImpl;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.yaml.snakeyaml.Yaml;
@@ -40,7 +48,6 @@ import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.event.EventManagerImpl;
import com.dfsek.terra.profiler.ProfilerImpl;
import com.dfsek.terra.registry.CheckedRegistryImpl;
import com.dfsek.terra.registry.master.AddonRegistry;
import com.dfsek.terra.registry.master.ConfigRegistry;
import com.dfsek.terra.util.logging.DebugLogger;
@@ -66,7 +73,9 @@ public abstract class AbstractPlatform implements Platform {
private final CommandManager manager = new TerraCommandManager(this);
private final AddonRegistry addonRegistry = new AddonRegistry(this);
private final CheckedRegistry<BaseAddon> addonRegistry = new CheckedRegistryImpl<>(new OpenRegistryImpl<>());
private final Registry<BaseAddon> lockedAddonRegistry = new LockedRegistryImpl<>(addonRegistry);
private final Lazy<Logger> logger = Lazy.lazy(this::createLogger);
private final Lazy<DebugLogger> debugLogger = Lazy.lazy(() -> new DebugLogger(logger()));
@@ -97,8 +106,8 @@ public abstract class AbstractPlatform implements Platform {
}
@Override
public Registry<TerraAddon> getAddons() {
return addonRegistry;
public Registry<BaseAddon> getAddons() {
return lockedAddonRegistry;
}
@Override
@@ -126,7 +135,7 @@ public abstract class AbstractPlatform implements Platform {
logger().info("Initializing Terra...");
getPlatformAddon().ifPresent(addonRegistry::register);
getPlatformAddon().ifPresent(addon -> addonRegistry.register(addon.getID(), addon));
try(InputStream stream = getClass().getResourceAsStream("/config.yml")) {
File configFile = new File(getDataFolder(), "config.yml");
@@ -181,11 +190,25 @@ public abstract class AbstractPlatform implements Platform {
profiler.start();
}
addonRegistry.register(new InternalAddon(this));
InternalAddon internalAddon = new InternalAddon();
addonRegistry.register(internalAddon.getID(), internalAddon);
if(!addonRegistry.loadAll(getClass().getClassLoader())) { // load all addons
throw new IllegalStateException("Failed to load addons. Please correct addon installations to continue.");
}
BootstrapAddonLoader bootstrapAddonLoader = new BootstrapAddonLoader();
eventManager
.getHandler(FunctionalEventHandler.class)
.register(internalAddon, PlatformInitializationEvent.class)
.then(event -> {
logger().info("Loading config packs...");
getRawConfigRegistry().loadAll(this);
logger().info("Loaded packs.");
})
.global();
logger().info("Loaded addons.");
try {
@@ -200,7 +223,7 @@ public abstract class AbstractPlatform implements Platform {
protected abstract Logger createLogger();
protected Optional<TerraAddon> getPlatformAddon() {
protected Optional<BaseAddon> getPlatformAddon() {
return Optional.empty();
}

View File

@@ -1,5 +1,6 @@
package com.dfsek.terra;
import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.addon.TerraAddon;
import com.dfsek.terra.api.addon.annotations.Addon;
import com.dfsek.terra.api.addon.annotations.Author;
@@ -8,26 +9,9 @@ import com.dfsek.terra.api.event.events.platform.PlatformInitializationEvent;
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
@Addon("terra")
@Author("Terra")
@Version("1.0.0")
public class InternalAddon extends TerraAddon {
private final AbstractPlatform main;
public InternalAddon(AbstractPlatform main) {
this.main = main;
}
public class InternalAddon implements BaseAddon {
@Override
public void initialize() {
main.getEventManager()
.getHandler(FunctionalEventHandler.class)
.register(this, PlatformInitializationEvent.class)
.then(event -> {
main.logger().info("Loading config packs...");
main.getRawConfigRegistry().loadAll(main);
main.logger().info("Loaded packs.");
})
.global();
public String getID() {
return "terra";
}
}

View File

@@ -5,6 +5,7 @@ import com.dfsek.tectonic.loading.TypeRegistry;
import java.util.LinkedHashMap;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.addon.TerraAddon;
import com.dfsek.terra.api.block.BlockType;
import com.dfsek.terra.api.block.state.BlockState;
@@ -33,7 +34,7 @@ public class GenericLoaders implements LoaderRegistrar {
.registerLoader(LinkedHashMap.class, new LinkedHashMapLoader());
if(platform != null) {
registry.registerLoader(TerraAddon.class, platform.getAddons())
registry.registerLoader(BaseAddon.class, platform.getAddons())
.registerLoader(BlockType.class,
(t, object, cf) -> platform.getWorldHandle().createBlockData((String) object).getBlockType())
.registerLoader(BlockState.class, (t, object, cf) -> platform.getWorldHandle().createBlockData((String) object));

View File

@@ -48,10 +48,6 @@ public class AddonRegistry extends OpenRegistryImpl<TerraAddon> {
return register(addon.getName(), addon);
}
public boolean loadAll() {
return loadAll(Platform.class.getClassLoader());
}
public boolean loadAll(ClassLoader parent) {
InjectorImpl<Platform> pluginInjector = new InjectorImpl<>(platform);
pluginInjector.addExplicitTarget(Platform.class);

View File

@@ -1,7 +1,6 @@
package com.dfsek.terra.addon;
import com.dfsek.terra.addon.exception.AddonLoadException;
import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.addon.bootstrap.BootstrapBaseAddon;
import java.io.IOException;
@@ -14,9 +13,9 @@ import java.util.jar.JarFile;
import java.util.stream.Collectors;
public class BootstrapAddonLoader implements BootstrapBaseAddon {
public class BootstrapAddonLoader implements BootstrapBaseAddon<BootstrapBaseAddon<?>> {
@Override
public Iterable<BaseAddon> loadAddons(Path addonsFolder, ClassLoader parent) {
public Iterable<BootstrapBaseAddon<?>> loadAddons(Path addonsFolder, ClassLoader parent) {
Path bootstrapAddons = addonsFolder.resolve("bootstrap");
try {
@@ -33,7 +32,7 @@ public class BootstrapAddonLoader implements BootstrapBaseAddon {
if(!(in instanceof BootstrapBaseAddon)) {
throw new AddonLoadException(in.getClass() + " does not extend " + BootstrapBaseAddon.class);
}
return (BaseAddon) in;
return (BootstrapBaseAddon<?>) in;
} catch(InvocationTargetException e) {
throw new AddonLoadException("Exception occurred while instantiating addon: ", e);
} catch(NoSuchMethodException | IllegalAccessException | InstantiationException e) {