diff --git a/core/src/main/java/art/arcane/iris/core/IrisDatapackCompiler.java b/core/src/main/java/art/arcane/iris/core/IrisDatapackCompiler.java index 1f55c7338..32dbad3ce 100644 --- a/core/src/main/java/art/arcane/iris/core/IrisDatapackCompiler.java +++ b/core/src/main/java/art/arcane/iris/core/IrisDatapackCompiler.java @@ -4,7 +4,7 @@ import art.arcane.iris.engine.object.world.biome.custom.IrisBiomeCustom; import art.arcane.iris.BuildConstants; import art.arcane.iris.core.lifecycle.BukkitWorldConfiguration.IrisGeneratorBinding; import art.arcane.iris.core.loader.IrisData; -import art.arcane.iris.core.loader.ResourceLoader; +import art.arcane.iris.core.modules.PackModulesException; import art.arcane.iris.core.nms.datapack.DataVersion; import art.arcane.iris.core.nms.datapack.IDataFixer; import art.arcane.iris.core.pack.AtomicDirectoryPublisher; @@ -48,6 +48,7 @@ public final class IrisDatapackCompiler { private static final int INPUT_FINGERPRINT_SCHEMA = 2; 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 = """ @@ -186,16 +187,7 @@ public final class IrisDatapackCompiler { } IrisData data = IrisData.openDatapackCompiler(packRoot); try { - 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(packRoot, data).dimensions()) { collectRegistryRequirements( requirements, installedBiomeKeys, @@ -315,6 +307,7 @@ public final class IrisDatapackCompiler { DimensionHeight height = new DimensionHeight(fixer); Map> biomes = new LinkedHashMap<>(); Map> dimensions = new LinkedHashMap<>(); + PackUse packUse = new PackUse(packRoots, normalizedBindings); int packCount = 0; int dimensionCount = 0; for (File packRoot : packRoots) { @@ -324,18 +317,8 @@ public final class IrisDatapackCompiler { } IrisData data = IrisData.openDatapackCompiler(packRoot); try { - 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(packRoot, data), packUse)) { IrisLogging.debug(" Compiling Dimension " + dimension.getLoadFile().getPath()); height.merge(dimension); dimensions.computeIfAbsent(dimension.getLoadKey(), ignored -> new ArrayList<>()) @@ -359,11 +342,86 @@ public final class IrisDatapackCompiler { } IrisDimension.writeShared(datapackRoots, height, adjustVanillaHeight); + createDimensionTypeFolders(datapackRoots); installLevelStemBindings(normalizedBindings, dimensions, datapackRoots); validateOutputs(datapackRoots, dimensionCount, normalizedBindings.size()); return new CompilationResult(packCount, dimensionCount, countBiomes(biomes)); } + /** + * The dimensions of the pack at {@code packRoot}. 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 its modules or any of its dimensions cannot load. A + * pack that cannot load is left out of the datapack as a whole, never in part. + */ + private static LoadedPack loadPack(File packRoot, IrisData data) throws IOException { + String[] dimensionKeys = data.getDimensionLoader().getPossibleKeys(); + if (dimensionKeys == null || dimensionKeys.length == 0) { + throw new IOException("Iris pack has no dimension definitions: " + packRoot); + } + List keys = List.of(dimensionKeys); + String refusal = moduleRefusal(data); + if (refusal != null) { + return LoadedPack.refused(keys, refusal); + } + List loaded = new ArrayList<>(); + for (String key : keys) { + IrisDimension dimension = data.getDimensionLoader().load(key); + if (dimension == null) { + return LoadedPack.refused(keys, "its dimension '" + key + "' failed to load; the error above says why"); + } + loaded.add(dimension); + } + return new LoadedPack(keys, 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; + } + } + + /** Why the pack's modules cannot load, or null when they load. */ + private static String moduleRefusal(IrisData data) { + try { + data.getModules().pack(); + return null; + } catch (PackModulesException failure) { + return failure.getMessage(); + } + } + + /** + * The folder of Iris's dimension types exists in every compiled datapack, also when every pack was left out, so a + * datapack that compiled no type still reads as complete. + */ + private static void createDimensionTypeFolders(Collection datapackRoots) throws IOException { + for (File datapackRoot : datapackRoots) { + Files.createDirectories(datapackRoot.toPath().resolve(DIMENSION_TYPE_FOLDER)); + } + } + private static void installLevelStemBindings( List bindings, Map> dimensions, @@ -637,7 +695,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/core/PackUse.java b/core/src/main/java/art/arcane/iris/core/PackUse.java new file mode 100644 index 000000000..bc96abff9 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/PackUse.java @@ -0,0 +1,103 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core; + +import art.arcane.iris.core.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 /dimensions///iris/pack}) belongs to that world, configured or not. A pack installed + * in Iris's own folder is used by every configured 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 final Set worldsWithOwnPack; + private final List bindings; + + PackUse(Collection packRoots, List bindings) { + this.worldsWithOwnPack = packRoots.stream() + .map(root -> worldOwning(root.toPath())) + .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 owner = worldOwning(packRoot.toPath()); + if (owner.isPresent()) { + return List.of(nameOf(owner.get())); + } + return bindings.stream() + .filter(binding -> dimensions.contains(binding.dimension())) + .filter(binding -> !worldsWithOwnPack.contains(binding.worldKey())) + .map(IrisGeneratorBinding::configuredWorldName) + .toList(); + } + + private String nameOf(WorldSlotKey world) { + return bindings.stream() + .filter(binding -> binding.worldKey().equals(world)) + .map(IrisGeneratorBinding::configuredWorldName) + .findFirst() + .orElse(world.toString()); + } + + /** The world whose folder holds the pack at {@code packRoot}, or empty for a pack installed outside any world. */ + static Optional worldOwning(Path packRoot) { + Path irisFolder = packRoot.toAbsolutePath().normalize().getParent(); + if (!WORLD_PACK_FOLDER.equals(nameOf(packRoot)) || irisFolder == null || !WORLD_IRIS_FOLDER.equals(nameOf(irisFolder))) { + return Optional.empty(); + } + Path worldFolder = irisFolder.getParent(); + 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/core/IrisDatapackCompilerTest.java b/core/src/test/java/art/arcane/iris/core/IrisDatapackCompilerTest.java index 468c621ae..3e72b8a53 100644 --- a/core/src/test/java/art/arcane/iris/core/IrisDatapackCompilerTest.java +++ b/core/src/test/java/art/arcane/iris/core/IrisDatapackCompilerTest.java @@ -16,6 +16,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; +import java.util.Optional; import java.util.Map; import static org.junit.Assert.assertEquals; @@ -313,6 +314,113 @@ public class IrisDatapackCompilerTest { assertTrue(Files.isRegularFile(datapackRoot.resolve("data/iris/dimension/moon.json"))); } + @Test + public void aPackThatCannotLoadAndNoWorldUsesIsLeftOut() throws Exception { + Path dataDirectory = temporaryFolder.newFolder("unused-refused-data").toPath(); + Path serverRoot = temporaryFolder.newFolder("unused-refused-server").toPath(); + createPack(dataDirectory.resolve("packs/healthy"), "healthy", "healthy_custom"); + createRefusedPack(dataDirectory.resolve("packs/refused"), "refused"); + Path datapackRoot = temporaryFolder.newFolder("unused-refused-datapack").toPath(); + + IrisDatapackCompiler.CompilationResult result = IrisDatapackCompiler.compile( + IrisDatapackCompiler.collectPackRoots(dataDirectory, serverRoot), + new KList().qadd(datapackRoot.toFile()), + List.of(binding("main", "healthy")), + new DataFixerV1217(), + false + ); + + assertEquals(1, result.packCount()); + assertTrue(Files.isRegularFile(datapackRoot.resolve("data/iris/dimension_type/healthy.json"))); + assertFalse(Files.exists(datapackRoot.resolve("data/iris/dimension_type/refused.json"))); + } + + @Test + public void aPackThatCannotLoadAddsNoRegistryRequirements() throws Exception { + Path healthyPack = temporaryFolder.newFolder("registry-healthy-pack").toPath(); + Path refusedPack = temporaryFolder.newFolder("registry-refused-pack").toPath(); + createPack(healthyPack, "healthy", "healthy_custom"); + createRefusedPack(refusedPack, "refused"); + DataFixerV1217 fixer = new DataFixerV1217(); + + Map healthyOnly = IrisDatapackCompiler.computeRegistryRequirements( + List.of(healthyPack.toFile()), + fixer); + Map withRefused = IrisDatapackCompiler.computeRegistryRequirements( + List.of(healthyPack.toFile(), refusedPack.toFile()), + fixer); + + assertEquals(healthyOnly, withRefused); + } + + @Test + public void onlyPacksThatCannotLoadStillGiveACompleteDatapack() throws Exception { + Path packRoot = temporaryFolder.newFolder("only-refused-pack").toPath(); + createRefusedPack(packRoot, "refused"); + Path datapackRoot = temporaryFolder.newFolder("only-refused-datapack").toPath(); + + IrisDatapackCompiler.CompilationResult result = IrisDatapackCompiler.compile( + List.of(packRoot.toFile()), + new KList().qadd(datapackRoot.toFile()), + List.of(), + new DataFixerV1217(), + false + ); + + assertEquals(0, result.dimensionCount()); + assertTrue(Files.isDirectory(datapackRoot.resolve("data/iris/dimension_type"))); + } + + @Test + public void aPackThatCannotLoadStopsTheCompileWhenAConfiguredWorldSelectsIt() throws Exception { + Path packRoot = temporaryFolder.newFolder("selected-refused-pack").toPath(); + createRefusedPack(packRoot, "refused"); + Path datapackRoot = temporaryFolder.newFolder("selected-refused-datapack").toPath(); + + IOException failure = assertThrows(IOException.class, () -> IrisDatapackCompiler.compile( + List.of(packRoot.toFile()), + new KList().qadd(datapackRoot.toFile()), + List.of(binding("moon", "refused")), + new DataFixerV1217(), + false + )); + + assertTrue(failure.getMessage(), failure.getMessage().contains("cannot load")); + assertTrue(failure.getMessage(), failure.getMessage().contains("world_iris_moon")); + } + + @Test + public void aWorldsOwnPackThatCannotLoadStopsTheCompileEvenWhenNoWorldIsConfigured() throws Exception { + Path dataDirectory = temporaryFolder.newFolder("world-refused-data").toPath(); + Path serverRoot = temporaryFolder.newFolder("world-refused-server").toPath(); + createRefusedPack(serverRoot.resolve("dimensions/iris/old_world/iris/pack"), "overworld"); + Path datapackRoot = temporaryFolder.newFolder("world-refused-datapack").toPath(); + + IOException failure = assertThrows(IOException.class, () -> IrisDatapackCompiler.compile( + IrisDatapackCompiler.collectPackRoots(dataDirectory, serverRoot), + new KList().qadd(datapackRoot.toFile()), + List.of(), + new DataFixerV1217(), + false + )); + + assertTrue(failure.getMessage(), failure.getMessage().contains("iris:old_world")); + } + + @Test + public void aPackInAWorldsFolderBelongsToThatWorld() { + assertEquals(Optional.of(new WorldSlotKey("iris", "old_world")), + PackUse.worldOwning(Path.of("/server/world/dimensions/iris/old_world/iris/pack"))); + assertEquals(Optional.empty(), PackUse.worldOwning(Path.of("/server/plugins/Iris/packs/overworld"))); + } + + /** A pack whose modules cannot load: its {@code modules} folder holds a file that is no jar. */ + private static void createRefusedPack(Path root, String dimensionKey) throws Exception { + createPack(root, dimensionKey, dimensionKey + "_custom"); + Files.createDirectories(root.resolve("modules")); + Files.writeString(root.resolve("modules/broken.jar"), "not a jar", StandardCharsets.UTF_8); + } + private static void createPack(Path root, String dimensionKey, String biomeId) throws Exception { createPack(root, dimensionKey, biomeId, "NORMAL"); } diff --git a/core/src/test/java/art/arcane/iris/core/pack/DefaultPackBootstrapProvisionerTest.java b/core/src/test/java/art/arcane/iris/core/pack/DefaultPackBootstrapProvisionerTest.java index 477334d61..481038234 100644 --- a/core/src/test/java/art/arcane/iris/core/pack/DefaultPackBootstrapProvisionerTest.java +++ b/core/src/test/java/art/arcane/iris/core/pack/DefaultPackBootstrapProvisionerTest.java @@ -560,7 +560,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(