From 4a3ce335cfed72c2aa958cead3c18d92cd677971 Mon Sep 17 00:00:00 2001 From: piexel Date: Sat, 14 Nov 2026 18:07:42 +0100 Subject: [PATCH] Stop startup only for packs a world uses - Leave out a pack that cannot load when no world uses it, with a warning naming it - Fail the compile naming the pack and its worlds when one does - Leave such packs out of the pinned registry requirements - Always create the dimension type folder --- .../pack/datapack/IrisDatapackCompiler.java | 88 ++++++++--- .../arcane/iris/pack/datapack/PackUse.java | 111 ++++++++++++++ .../DefaultPackBootstrapProvisionerTest.java | 2 +- .../datapack/IrisDatapackCompilerTest.java | 139 ++++++++++++++++++ 4 files changed, 317 insertions(+), 23 deletions(-) create mode 100644 core/src/main/java/art/arcane/iris/pack/datapack/PackUse.java diff --git a/core/src/main/java/art/arcane/iris/pack/datapack/IrisDatapackCompiler.java b/core/src/main/java/art/arcane/iris/pack/datapack/IrisDatapackCompiler.java index 05c58959c..93f75d7ee 100644 --- a/core/src/main/java/art/arcane/iris/pack/datapack/IrisDatapackCompiler.java +++ b/core/src/main/java/art/arcane/iris/pack/datapack/IrisDatapackCompiler.java @@ -63,6 +63,7 @@ public final class IrisDatapackCompiler { private static final int INPUT_FINGERPRINT_SCHEMA = 4; private static final int INPUT_BUFFER_BYTES = 64 * 1024; private static final int WORLD_PACK_SCAN_DEPTH = 8; + private static final String DIMENSION_TYPE_FOLDER = "data/iris/dimension_type"; private static final Pattern REGISTRY_KEY_PATTERN = Pattern.compile("[a-z0-9_.-]+:[a-z0-9/._-]+"); private static final List INPUT_DIRECTORIES = List.of("dimensions", "biomes", "snippet"); private static final String FLAT_VOID_LEVEL_STEM = """ @@ -210,16 +211,7 @@ public final class IrisDatapackCompiler { if (aliasContext.retainedContract() != null) { data.bindGenerationRegistryContract(aliasContext.retainedContract()); } - ResourceLoader loader = data.getDimensionLoader(); - String[] possibleKeys = loader.getPossibleKeys(); - if (possibleKeys == null || possibleKeys.length == 0) { - throw new IOException("Iris pack has no dimension definitions: " + packRoot); - } - for (String possibleKey : possibleKeys) { - IrisDimension dimension = loader.load(possibleKey); - if (dimension == null) { - throw new IOException("Unable to load Iris dimension '" + possibleKey + "' from " + packRoot); - } + for (IrisDimension dimension : loadPack(data).dimensions()) { collectRegistryRequirements( requirements, biomeTags, @@ -455,6 +447,7 @@ public final class IrisDatapackCompiler { Map> biomes = new LinkedHashMap<>(); Map> dimensions = new LinkedHashMap<>(); Map histories = new LinkedHashMap<>(); + PackUse packUse = new PackUse(packRoots, normalizedBindings); int packCount = 0; int dimensionCount = 0; for (File packRoot : packRoots) { @@ -468,18 +461,8 @@ public final class IrisDatapackCompiler { if (aliasContext.retainedContract() != null) { data.bindGenerationRegistryContract(aliasContext.retainedContract()); } - ResourceLoader loader = data.getDimensionLoader(); - String[] possibleKeys = loader.getPossibleKeys(); - if (possibleKeys == null || possibleKeys.length == 0) { - throw new IOException("Iris pack has no dimension definitions: " + packRoot); - } - int installedDimensions = 0; - for (String possibleKey : possibleKeys) { - IrisDimension dimension = loader.load(possibleKey); - if (dimension == null) { - throw new IOException("Unable to load Iris dimension '" + possibleKey + "' from " + packRoot); - } + for (IrisDimension dimension : dimensionsOfUsedPack(packRoot, loadPack(data), packUse)) { IrisLogging.debug(" Compiling Dimension " + dimension.getLoadFile().getPath()); height.merge(dimension); dimensions.computeIfAbsent(dimension.getLoadKey(), ignored -> new ArrayList<>()) @@ -514,12 +497,73 @@ public final class IrisDatapackCompiler { } IrisDimension.writeShared(datapackRoots, height, adjustVanillaHeight); + createDimensionTypeFolders(datapackRoots); installRetainedRegistrySources(packRoots, datapackRoots, fixer, histories); installLevelStemBindings(normalizedBindings, dimensions, datapackRoots); validateOutputs(datapackRoots, dimensionCount, normalizedBindings.size()); return new CompilationResult(packCount, dimensionCount, countBiomes(biomes)); } + /** + * A pack that cannot load stops the compile when a world uses it, so the server never starts that world without + * its dimension; a pack no world uses is left out with a warning. + */ + private static List dimensionsOfUsedPack(File packRoot, LoadedPack pack, PackUse packUse) + throws IOException { + if (pack.loads()) { + return pack.dimensions(); + } + List users = packUse.worldsUsing(packRoot, pack.dimensionKeys()); + if (!users.isEmpty()) { + throw new IOException("Iris pack " + packRoot + " cannot load (" + pack.refusal() + ") and world(s) " + + String.join(", ", users) + " use it. Fix the pack, then start the server again."); + } + IrisLogging.warn("Iris pack %s cannot load (%s). No world uses it, so it is left out and the server starts.", + packRoot, pack.refusal()); + return List.of(); + } + + /** Every dimension of the pack, or none with the reason when any of them cannot load; never part of a pack. */ + private static LoadedPack loadPack(IrisData data) { + ResourceLoader loader = data.getDimensionLoader(); + String[] possibleKeys = loader.getPossibleKeys(); + if (possibleKeys == null || possibleKeys.length == 0) { + return LoadedPack.refused(List.of(), "it has no dimension definitions"); + } + List dimensionKeys = List.of(possibleKeys); + List loaded = new ArrayList<>(); + for (String key : dimensionKeys) { + IrisDimension dimension; + try { + dimension = loader.load(key); + } catch (RuntimeException failure) { + return LoadedPack.refused(dimensionKeys, "its dimension '" + key + "' failed to load: " + failure.getMessage()); + } + if (dimension == null) { + return LoadedPack.refused(dimensionKeys, "its dimension '" + key + "' failed to load; the error above says why"); + } + loaded.add(dimension); + } + return new LoadedPack(dimensionKeys, List.copyOf(loaded), null); + } + + private record LoadedPack(List dimensionKeys, List dimensions, String refusal) { + static LoadedPack refused(List dimensionKeys, String refusal) { + return new LoadedPack(dimensionKeys, List.of(), refusal); + } + + boolean loads() { + return refusal == null; + } + } + + /** The dimension type folder exists in every compiled datapack, so one built only from left-out packs reads as complete. */ + private static void createDimensionTypeFolders(Collection datapackRoots) throws IOException { + for (File datapackRoot : datapackRoots) { + Files.createDirectories(datapackRoot.toPath().resolve(DIMENSION_TYPE_FOLDER)); + } + } + public static void installRetainedRegistrySources(List packRoots, Collection datapackRoots, IDataFixer fixer) throws IOException { installRetainedRegistrySources(packRoots, datapackRoots, fixer, new LinkedHashMap<>()); @@ -988,7 +1032,7 @@ public final class IrisDatapackCompiler { if (!Files.isRegularFile(root.resolve("pack.mcmeta"))) { throw new IOException("Iris datapack metadata was not generated at " + root); } - if (dimensionCount > 0 && !Files.isDirectory(root.resolve("data/iris/dimension_type"))) { + if (dimensionCount > 0 && !Files.isDirectory(root.resolve(DIMENSION_TYPE_FOLDER))) { throw new IOException("Iris dimension types were not generated at " + root); } if (levelStemCount > 0 && !Files.isDirectory(root.resolve("data/iris/dimension"))) { diff --git a/core/src/main/java/art/arcane/iris/pack/datapack/PackUse.java b/core/src/main/java/art/arcane/iris/pack/datapack/PackUse.java new file mode 100644 index 000000000..23a7ffa4b --- /dev/null +++ b/core/src/main/java/art/arcane/iris/pack/datapack/PackUse.java @@ -0,0 +1,111 @@ +package art.arcane.iris.pack.datapack; + +import art.arcane.iris.world.WorldSlotKey; +import art.arcane.iris.world.lifecycle.BukkitWorldConfiguration.IrisGeneratorBinding; + +import java.io.File; +import java.nio.file.Path; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Which worlds use a pack the startup datapack is compiled from. A pack copied into a world's folder + * ({@code /iris/pack} or {@code /iris/generation/epochs//pack}) belongs to that world, configured + * or not. A pack installed in Iris's own folder is used by every bound world that selects one of its dimensions and + * has no copy of its own. + */ +final class PackUse { + private static final String DIMENSIONS_FOLDER = "dimensions"; + private static final String WORLD_IRIS_FOLDER = "iris"; + private static final String WORLD_PACK_FOLDER = "pack"; + private static final String GENERATION_FOLDER = "generation"; + private static final String EPOCHS_FOLDER = "epochs"; + + private final Set worldsWithOwnPack; + private final List bindings; + + PackUse(Collection packRoots, List bindings) { + this.worldsWithOwnPack = packRoots.stream() + .map(root -> worldFolderOwning(root.toPath())) + .flatMap(Optional::stream) + .map(PackUse::slotKeyOf) + .flatMap(Optional::stream) + .collect(Collectors.toUnmodifiableSet()); + this.bindings = List.copyOf(bindings); + } + + /** The names of the worlds that use the pack at {@code packRoot}, which defines {@code dimensions}. */ + List worldsUsing(File packRoot, Collection dimensions) { + Optional ownerFolder = worldFolderOwning(packRoot.toPath()); + if (ownerFolder.isPresent()) { + return List.of(nameOfWorld(ownerFolder.get())); + } + return bindings.stream() + .filter(binding -> dimensions.contains(binding.dimension())) + .filter(binding -> !worldsWithOwnPack.contains(binding.worldKey())) + .map(IrisGeneratorBinding::configuredWorldName) + .toList(); + } + + private String nameOfWorld(Path worldFolder) { + Optional worldKey = slotKeyOf(worldFolder); + if (worldKey.isEmpty()) { + return nameOf(worldFolder); + } + return bindings.stream() + .filter(binding -> binding.worldKey().equals(worldKey.get())) + .map(IrisGeneratorBinding::configuredWorldName) + .findFirst() + .orElse(worldKey.get().toString()); + } + + /** The folder of the world whose copy the pack at {@code packRoot} is, or empty for a pack installed outside any world. */ + static Optional worldFolderOwning(Path packRoot) { + Path pack = packRoot.toAbsolutePath().normalize(); + if (!WORLD_PACK_FOLDER.equals(nameOf(pack))) { + return Optional.empty(); + } + Path parent = pack.getParent(); + if (WORLD_IRIS_FOLDER.equals(nameOf(parent))) { + return Optional.ofNullable(parent.getParent()); + } + Path epochs = parentOf(parent); + Path generation = parentOf(epochs); + Path iris = parentOf(generation); + if (EPOCHS_FOLDER.equals(nameOf(epochs)) + && GENERATION_FOLDER.equals(nameOf(generation)) + && WORLD_IRIS_FOLDER.equals(nameOf(iris))) { + return Optional.ofNullable(iris.getParent()); + } + return Optional.empty(); + } + + /** The key of a world stored at {@code /dimensions//}, or empty for the level's own folder. */ + static Optional slotKeyOf(Path worldFolder) { + for (Path namespace = parentOf(worldFolder); namespace != null; namespace = namespace.getParent()) { + if (DIMENSIONS_FOLDER.equals(nameOf(namespace.getParent()))) { + return slotKey(nameOf(namespace), namespace.relativize(worldFolder)); + } + } + return Optional.empty(); + } + + private static Optional slotKey(String namespace, Path world) { + try { + return Optional.of(new WorldSlotKey(namespace, world.toString().replace(File.separatorChar, '/'))); + } catch (IllegalArgumentException notAWorldKey) { + return Optional.empty(); + } + } + + private static Path parentOf(Path path) { + return path == null ? null : path.getParent(); + } + + private static String nameOf(Path path) { + return path == null || path.getFileName() == null ? null : path.getFileName().toString(); + } +} diff --git a/core/src/test/java/art/arcane/iris/pack/DefaultPackBootstrapProvisionerTest.java b/core/src/test/java/art/arcane/iris/pack/DefaultPackBootstrapProvisionerTest.java index 17c569198..ee3de8406 100644 --- a/core/src/test/java/art/arcane/iris/pack/DefaultPackBootstrapProvisionerTest.java +++ b/core/src/test/java/art/arcane/iris/pack/DefaultPackBootstrapProvisionerTest.java @@ -588,7 +588,7 @@ public class DefaultPackBootstrapProvisionerTest { byte[] metadata = Files.readAllBytes(first.datapackRoot().resolve("pack.mcmeta")); byte[] originalOverworld = Files.readAllBytes(dataDirectory.resolve("packs/overworld/biomes/local.json")); byte[] originalUnderworld = Files.readAllBytes(dataDirectory.resolve("packs/underworld/biomes/local.json")); - Path invalidPack = dataDirectory.resolve("packs/invalid"); + Path invalidPack = root.resolve("dimensions/iris/broken_world/iris/pack"); Files.createDirectories(invalidPack.resolve("dimensions")); Files.writeString(invalidPack.resolve("dimensions/broken.json"), "{", StandardCharsets.UTF_8); updateServer = server( diff --git a/core/src/test/java/art/arcane/iris/pack/datapack/IrisDatapackCompilerTest.java b/core/src/test/java/art/arcane/iris/pack/datapack/IrisDatapackCompilerTest.java index 97e0eb1a6..66eaf1cfa 100644 --- a/core/src/test/java/art/arcane/iris/pack/datapack/IrisDatapackCompilerTest.java +++ b/core/src/test/java/art/arcane/iris/pack/datapack/IrisDatapackCompilerTest.java @@ -35,6 +35,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Stream; import static org.junit.Assert.assertEquals; @@ -388,6 +389,144 @@ public class IrisDatapackCompilerTest { assertTrue(failure.getMessage().contains("selects missing dimension \"missing\"")); } + @Test + public void leavesOutAPackThatCannotLoadWhenNoWorldUsesIt() throws Exception { + Path workingPack = temporaryFolder.newFolder("unused-refused-working").toPath(); + Path refusedPack = temporaryFolder.newFolder("unused-refused-broken").toPath(); + Path datapackRoot = temporaryFolder.newFolder("unused-refused-datapack").toPath(); + createPack(workingPack, "working", "working_custom"); + createRefusedPack(refusedPack, "broken"); + + IrisDatapackCompiler.CompilationResult result = IrisDatapackCompiler.compile( + List.of(workingPack.toFile(), refusedPack.toFile()), + new KList().qadd(datapackRoot.toFile()), + List.of(binding("moon", "working")), + new DataFixerV1217(), + false + ); + + assertEquals(1, result.packCount()); + assertEquals(1, result.dimensionCount()); + assertTrue(Files.isRegularFile(datapackRoot.resolve("data/iris/dimension/moon.json"))); + } + + @Test + public void leftOutPacksStillProduceACompleteDatapack() throws Exception { + Path refusedPack = temporaryFolder.newFolder("only-refused-broken").toPath(); + Path datapackRoot = temporaryFolder.newFolder("only-refused-datapack").toPath(); + createRefusedPack(refusedPack, "broken"); + + IrisDatapackCompiler.CompilationResult result = IrisDatapackCompiler.compile( + List.of(refusedPack.toFile()), + new KList().qadd(datapackRoot.toFile()), + List.of(), + new DataFixerV1217(), + false + ); + + assertEquals(0, result.dimensionCount()); + assertTrue(Files.isRegularFile(datapackRoot.resolve("pack.mcmeta"))); + assertTrue(Files.isDirectory(datapackRoot.resolve("data/iris/dimension_type"))); + } + + @Test + public void registryRequirementsLeaveOutAPackThatCannotLoad() throws Exception { + Path workingPack = temporaryFolder.newFolder("requirements-working").toPath(); + Path refusedPack = temporaryFolder.newFolder("requirements-broken").toPath(); + createPack(workingPack, "working", "working_custom"); + createRefusedPack(refusedPack, "broken"); + DataFixerV1217 fixer = new DataFixerV1217(); + + Map withRefused = IrisDatapackCompiler.computeRegistryRequirements( + List.of(workingPack.toFile(), refusedPack.toFile()), + fixer); + Map withoutRefused = IrisDatapackCompiler.computeRegistryRequirements( + List.of(workingPack.toFile()), + fixer); + + assertEquals(withoutRefused, withRefused); + } + + @Test + public void stopsTheCompileWhenABoundWorldSelectsAPackThatCannotLoad() throws Exception { + Path refusedPack = temporaryFolder.newFolder("bound-refused-broken").toPath(); + Path datapackRoot = temporaryFolder.newFolder("bound-refused-datapack").toPath(); + createRefusedPack(refusedPack, "broken"); + + IOException failure = assertThrows( + IOException.class, + () -> IrisDatapackCompiler.compile( + List.of(refusedPack.toFile()), + new KList().qadd(datapackRoot.toFile()), + List.of(binding("moon", "broken")), + new DataFixerV1217(), + false + ) + ); + + assertTrue(failure.getMessage(), failure.getMessage().contains(refusedPack.toString())); + assertTrue(failure.getMessage(), failure.getMessage().contains("world(s) world_iris_moon use it")); + } + + @Test + public void stopsTheCompileWhenAWorldsOwnPackCannotLoad() throws Exception { + Path serverRoot = temporaryFolder.newFolder("own-refused-server").toPath(); + Path ownPack = serverRoot.resolve("dimensions/iris/moon/iris/pack"); + Path datapackRoot = temporaryFolder.newFolder("own-refused-datapack").toPath(); + createRefusedPack(ownPack, "broken"); + + IOException failure = assertThrows( + IOException.class, + () -> IrisDatapackCompiler.compile( + List.of(ownPack.toFile()), + new KList().qadd(datapackRoot.toFile()), + List.of(), + new DataFixerV1217(), + false + ) + ); + + assertTrue(failure.getMessage(), failure.getMessage().contains("world(s) iris:moon use it")); + } + + @Test + public void aWorldWithItsOwnPackDoesNotUseAnInstalledPackThatCannotLoad() throws Exception { + Path serverRoot = temporaryFolder.newFolder("own-working-server").toPath(); + Path ownPack = serverRoot.resolve("dimensions/iris/moon/iris/pack"); + Path installedPack = temporaryFolder.newFolder("own-working-installed").toPath(); + Path datapackRoot = temporaryFolder.newFolder("own-working-datapack").toPath(); + createPack(ownPack, "shared", "shared_custom"); + createRefusedPack(installedPack, "shared"); + + IrisDatapackCompiler.CompilationResult result = IrisDatapackCompiler.compile( + List.of(installedPack.toFile(), ownPack.toFile()), + new KList().qadd(datapackRoot.toFile()), + List.of(binding("moon", "shared")), + new DataFixerV1217(), + false + ); + + assertEquals(1, result.dimensionCount()); + } + + @Test + public void findsTheWorldOwningEachWorldPackLayout() { + Path level = Path.of("/server/world"); + Path moon = level.resolve("dimensions/iris/moon"); + + assertEquals(Optional.of(moon), PackUse.worldFolderOwning(moon.resolve("iris/pack"))); + assertEquals(Optional.of(moon), PackUse.worldFolderOwning(moon.resolve("iris/generation/epochs/0001/pack"))); + assertEquals(Optional.of(level), PackUse.worldFolderOwning(level.resolve("iris/pack"))); + assertEquals(Optional.empty(), PackUse.worldFolderOwning(Path.of("/server/plugins/Iris/packs/pack"))); + assertEquals(Optional.of(new WorldSlotKey("iris", "moon")), PackUse.slotKeyOf(moon)); + assertEquals(Optional.empty(), PackUse.slotKeyOf(level)); + } + + private static void createRefusedPack(Path root, String dimensionKey) throws IOException { + Files.createDirectories(root.resolve("dimensions")); + Files.writeString(root.resolve("dimensions").resolve(dimensionKey + ".json"), "{", StandardCharsets.UTF_8); + } + @Test public void rejectsBindingToConflictingDuplicateDimension() throws Exception { Path firstPack = temporaryFolder.newFolder("ambiguous-binding-first").toPath();