Refactor pack loading

- Combine initial load and reload logic together between each platform implementation
- Should prevent pack load errors from blocking other packs from loading.
This commit is contained in:
Astrash
2023-11-25 15:10:43 +11:00
parent 4ba71e9c27
commit 59ea5a69d8
5 changed files with 63 additions and 39 deletions

View File

@@ -18,6 +18,9 @@
package com.dfsek.terra;
import com.dfsek.tectonic.api.TypeRegistry;
import com.dfsek.terra.registry.master.ConfigRegistry.PackLoadFailuresException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull;
@@ -147,22 +150,29 @@ public abstract class AbstractPlatform implements Platform {
eventManager.getHandler(FunctionalEventHandler.class)
.register(internalAddon, PlatformInitializationEvent.class)
.then(event -> {
logger.info("Loading config packs...");
try {
configRegistry.loadAll(this);
} catch(IOException e) {
logger.error("Error loading config packs", e);
}
logger.info("Loaded packs.");
})
.then(event -> loadConfigPacks())
.global();
logger.info("Terra addons successfully loaded.");
logger.info("Finished initialization.");
}
protected boolean loadConfigPacks() {
logger.info("Loading config packs...");
ConfigRegistry configRegistry = getRawConfigRegistry();
configRegistry.clear();
try {
configRegistry.loadAll(this);
} catch(IOException e) {
logger.error("Failed to load config packs", e);
return false;
} catch(PackLoadFailuresException e) {
e.getExceptions().forEach(ex -> logger.error("Failed to load config pack", ex));
return false;
}
return true;
}
protected InternalAddon loadAddons() {
List<BaseAddon> addonList = new ArrayList<>();

View File

@@ -17,12 +17,12 @@
package com.dfsek.terra.registry.master;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.Serial;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import com.dfsek.terra.api.Platform;
@@ -41,14 +41,37 @@ public class ConfigRegistry extends OpenRegistryImpl<ConfigPack> {
super(TypeKey.of(ConfigPack.class));
}
public void loadAll(Platform platform) throws IOException {
public void loadAll(Platform platform) throws IOException, PackLoadFailuresException {
Path packsDirectory = platform.getDataFolder().toPath().resolve("packs");
Files.createDirectories(packsDirectory);
List<IOException> failedLoads = new ArrayList<>();
try (Stream<Path> packs = Files.list(packsDirectory)) {
for (Path path : packs.toList()) {
ConfigPack pack = new ConfigPackImpl(path, platform);
registerChecked(pack.getRegistryKey(), pack);
}
packs.forEach(path -> {
try {
ConfigPack pack = new ConfigPackImpl(path, platform);
registerChecked(pack.getRegistryKey(), pack);
} catch (IOException e) {
failedLoads.add(e);
}
});
}
if (!failedLoads.isEmpty()) {
throw new PackLoadFailuresException(failedLoads);
}
}
public static class PackLoadFailuresException extends Exception {
@Serial
private static final long serialVersionUID = 538998844645186306L;
private final List<Throwable> exceptions;
public PackLoadFailuresException(List<? extends Throwable> exceptions) {
this.exceptions = (List<Throwable>) exceptions;
}
public List<Throwable> getExceptions() {
return exceptions;
}
}
}