diff --git a/core/src/main/java/art/arcane/iris/core/pack/ModuleResourceClosure.java b/core/src/main/java/art/arcane/iris/core/pack/ModuleResourceClosure.java index 6a6beb887..322914944 100644 --- a/core/src/main/java/art/arcane/iris/core/pack/ModuleResourceClosure.java +++ b/core/src/main/java/art/arcane/iris/core/pack/ModuleResourceClosure.java @@ -29,6 +29,7 @@ import art.arcane.volmlib.util.json.JSONObject; import com.google.gson.Gson; import com.volmit.config.ResourceRef; import com.volmit.host.ResourceKindRegistration; +import com.volmit.object.GeneratedObject; import com.volmit.resource.ResourceKind; import java.io.File; @@ -50,8 +51,9 @@ import java.util.TreeSet; /** * The resources of the pack's module resource kinds that a pack export has to carry: what the settings of the * exported dimension, regions and biomes (and anything else handed to {@link #follow}) name by {@link ResourceRef}, - * {@link IrisStructureRef} or {@link RegistryListResource}, and what those resources name in turn. Core does not know the kinds: it follows the - * references the modules declare on their classes. + * {@link IrisStructureRef} or {@link RegistryListResource}, and what those resources name in turn, plus the keys of + * the objects all of them name. Core does not know the kinds: it follows the references the modules declare on their + * classes. */ public final class ModuleResourceClosure { private static final String JAVA_PACKAGE_PREFIX = "java."; @@ -62,7 +64,7 @@ public final class ModuleResourceClosure { private final Map, ResourceKind> kindsByType = new HashMap<>(); private final Map, Map> found = new LinkedHashMap<>(); private final Set visited = java.util.Collections.newSetFromMap(new IdentityHashMap<>()); - private final Map, Set> exportedElsewhere = new HashMap<>(); + private final Set objectKeys = new TreeSet<>(); public ModuleResourceClosure(IrisData data) { this.data = data; @@ -71,16 +73,6 @@ public final class ModuleResourceClosure { } } - /** - * Leaves the resources that read into {@code type} to another exporter, such as Iris's structure closure that - * copies structures with their pools, pieces and loot: {@link #keysOf} names the ones referenced, but they are - * not loaded, followed or written. Call before following anything. - */ - public ModuleResourceClosure exportingElsewhere(Class type) { - exportedElsewhere.putIfAbsent(type, new TreeSet<>()); - return this; - } - /** Adds the resources that the module settings of {@code owner} reference, and those they reference. */ public void followSettingsOf(IrisRegistrant owner) { owner.getModuleSettings().values().forEach(this::follow); @@ -122,13 +114,18 @@ public final class ModuleResourceClosure { /** The keys of the resources found of the kind that reads into {@code type}, in key order. */ public List keysOf(Class type) { - if (exportedElsewhere.containsKey(type)) { - return new ArrayList<>(exportedElsewhere.get(type)); - } ResourceKind kind = kindsByType.get(type); return kind == null || !found.containsKey(kind) ? List.of() : new ArrayList<>(new TreeMap<>(found.get(kind)).keySet()); } + /** + * The keys of the pack objects that what was found references, in key order. Objects are Iris's own files, not a + * module kind, so the export copies them itself. + */ + public List objectKeys() { + return List.copyOf(objectKeys); + } + /** * Writes every resource found as {@code /.json} below {@code targetRoot}, kinds in folder * order and keys in key order. @@ -191,13 +188,15 @@ public final class ModuleResourceClosure { } private void add(Class type, String key) { - ResourceKind kind = kindsByType.get(type); - if (kind == null || key == null || key.isBlank()) { + if (key == null || key.isBlank()) { return; } - Set elsewhere = exportedElsewhere.get(type); - if (elsewhere != null) { - elsewhere.add(key); + if (type == GeneratedObject.class) { + objectKeys.add(key); + return; + } + ResourceKind kind = kindsByType.get(type); + if (kind == null) { return; } Map ofKind = found.computeIfAbsent(kind, ignored -> new LinkedHashMap<>()); diff --git a/core/src/main/java/art/arcane/iris/core/pack/PackExportClosure.java b/core/src/main/java/art/arcane/iris/core/pack/PackExportClosure.java deleted file mode 100644 index 224ac8711..000000000 --- a/core/src/main/java/art/arcane/iris/core/pack/PackExportClosure.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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.pack; - -import art.arcane.iris.builtin.objects.ObjectPlacement; -import art.arcane.volmlib.util.collection.KSet; - -/** - * Shared key collection for the pack packagers. Both the Bukkit re-serializing compiler and the - * modded verbatim-copy compiler must export the complete ambient-spawning graph: object placements - * carry markers, markers carry spawners, spawners carry entities, entities carry loot. These - * helpers keep the two packagers walking placements identically. - */ -public final class PackExportClosure { - private PackExportClosure() { - } - - public static KSet collectObjectKeys(Iterable placements) { - KSet objectKeys = new KSet<>(); - if (placements == null) { - return objectKeys; - } - for (ObjectPlacement placement : placements) { - if (placement == null) { - continue; - } - for (String key : placement.place()) { - if (key == null || key.isBlank()) { - continue; - } - objectKeys.add(key); - } - } - return objectKeys; - } -} diff --git a/core/src/main/java/art/arcane/iris/core/project/IrisPackageCompiler.java b/core/src/main/java/art/arcane/iris/core/project/IrisPackageCompiler.java index 8d5713aa5..c22e27205 100644 --- a/core/src/main/java/art/arcane/iris/core/project/IrisPackageCompiler.java +++ b/core/src/main/java/art/arcane/iris/core/project/IrisPackageCompiler.java @@ -29,13 +29,11 @@ import art.arcane.iris.core.pack.ImageMapPackageClosure; import art.arcane.iris.core.pack.ModuleResourceClosure; import art.arcane.iris.core.pack.PackValidationResult; import art.arcane.iris.core.pack.PackValidator; -import art.arcane.iris.core.pack.StructurePackageClosure; import art.arcane.iris.engine.object.world.biome.IrisBiome; import art.arcane.iris.engine.object.material.IrisBlockData; import art.arcane.iris.engine.object.world.dimension.IrisDimension; import art.arcane.iris.engine.object.terrain.IrisGenerator; import art.arcane.iris.engine.object.world.region.IrisRegion; -import art.arcane.iris.engine.object.structure.IrisStructure; import art.arcane.iris.spi.IrisLogging; import art.arcane.iris.spi.IrisPlatforms; import art.arcane.iris.util.common.plugin.VolmitSender; @@ -101,7 +99,7 @@ public class IrisPackageCompiler { KSet regions = new KSet<>(); KSet biomes = new KSet<>(); KSet generators = new KSet<>(); - ModuleResourceClosure moduleResources = new ModuleResourceClosure(dm).exportingElsewhere(IrisStructure.class); + ModuleResourceClosure moduleResources = new ModuleResourceClosure(dm); KSet blocks = new KSet<>(); // KSet is ConcurrentHashMap-backed: both add(null) and remove(null) throw NPE, so a @@ -121,8 +119,7 @@ public class IrisPackageCompiler { regions.forEach((r) -> allPlacements.addAll(ObjectPlacements.all(r))); biomes.forEach((i) -> allPlacements.addAll(ObjectPlacements.all(i))); moduleResources.follow(allPlacements); - List structureKeys = moduleResources.keysOf(IrisStructure.class); - KList exportedObjectNames = new KList<>(); + List exportedObjectNames = moduleResources.objectKeys(); String a; StringBuilder b = new StringBuilder(); StringBuilder c = new StringBuilder(); @@ -130,16 +127,8 @@ public class IrisPackageCompiler { for (ObjectPlacement j : allPlacements) { b.append(Objects.hashCode(j.json())); - - for (String k : j.place()) { - if (exportedObjectNames.contains(k)) { - continue; - } - - b.append(k); - exportedObjectNames.add(k); - } } + exportedObjectNames.forEach(b::append); StringBuilder gb = new StringBuilder(); ChronoLatch cl = new ChronoLatch(1000); @@ -183,11 +172,6 @@ public class IrisPackageCompiler { IrisLogging.info("Writing Dimensional Scaffold"); try { - StructurePackageClosure structureClosure = StructurePackageClosure.collect(project.getPath(), structureKeys); - if (!structureClosure.isValid()) { - throw new IOException("Structure package closure is invalid: " + String.join("; ", structureClosure.errors())); - } - b.append(structureClosure.writeTo(folder, minify)); b.append(ImageMapPackageClosure.writeAll(dm, folder, minify)); a = withModuleSettings(dimension, Level.DIMENSION, dm).toString(minify ? 0 : 4); IO.writeAll(new File(folder, "dimensions/" + dimension.getLoadKey() + ".json"), a); diff --git a/core/src/test/java/art/arcane/iris/core/pack/ExportClosureComparisonTest.java b/core/src/test/java/art/arcane/iris/core/pack/ExportClosureComparisonTest.java new file mode 100644 index 000000000..5d8664650 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/pack/ExportClosureComparisonTest.java @@ -0,0 +1,222 @@ +package art.arcane.iris.core.pack; + +import art.arcane.iris.builtin.loot.LootTable; +import art.arcane.iris.builtin.objects.ObjectPlacement; +import art.arcane.iris.core.loader.IrisData; +import art.arcane.iris.core.modules.BuiltInModulesJar; +import art.arcane.iris.core.modules.IrisPackResources; +import art.arcane.iris.engine.framework.structure.StructureResources; +import com.volmit.resource.ResourceId; +import art.arcane.iris.engine.object.placement.ObjectPlacements; +import art.arcane.iris.engine.object.structure.IrisStructure; +import art.arcane.iris.engine.object.structure.jigsaw.IrisJigsawPiece; +import art.arcane.iris.engine.object.structure.jigsaw.IrisJigsawPool; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.engine.object.world.dimension.IrisDimension; +import art.arcane.iris.engine.object.world.region.IrisRegion; +import org.junit.Assume; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * The gate for exporting structures through the generic {@link ModuleResourceClosure}: it must carry every structure, + * pool, piece, loot table and object that {@link StructurePackageClosure} and the placements' object list carried. + * Set {@code IRIS_EXPORT_COMPARISON_PACK} to a pack folder to compare on a real pack; it is copied first. + */ +public class ExportClosureComparisonTest { + private static final String COMPARISON_PACK_VARIABLE = "IRIS_EXPORT_COMPARISON_PACK"; + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Test + public void theGenericClosureCarriesWhatTheStructureClosureAndThePlacementsCarried() throws Exception { + Comparison comparison = compare(fixturePack(), "main"); + + assertEquals(Set.of("castle"), comparison.generic.structures); + assertEquals(comparison.previous.structures, comparison.generic.structures); + assertEquals(comparison.previous.pools, comparison.generic.pools); + assertEquals(comparison.previous.pieces, comparison.generic.pieces); + assertEquals(comparison.previous.loot, comparison.generic.loot); + assertEquals(Set.of("castle/keep", "tree"), comparison.generic.objects); + assertEquals(comparison.previous.objects, comparison.generic.objects); + } + + @Test + public void theGenericClosureCarriesWhatTheStructureClosureCarriedOnARealPack() throws Exception { + String packFolder = comparisonPackFolder(); + Comparison comparison = compare(copyOfComparisonPack(packFolder), Path.of(packFolder).getFileName().toString()); + report("export of " + packFolder, comparison); + + assertTrue(String.join("; ", comparison.previous.errors), comparison.previous.isValid); + assertContains("structures", comparison.generic.structures, comparison.previous.structures); + assertContains("pools", comparison.generic.pools, comparison.previous.pools); + assertContains("pieces", comparison.generic.pieces, comparison.previous.pieces); + assertContains("loot", comparison.generic.loot, comparison.previous.loot); + assertContains("objects", comparison.generic.objects, comparison.previous.objects); + } + + @Test + public void theGenericClosureFollowsEveryStructureOfARealPackLikeTheStructureClosure() throws Exception { + String packFolder = comparisonPackFolder(); + Path copy = copyOfComparisonPack(packFolder); + IrisData data = IrisData.openValidation(copy.toFile()); + try { + IrisPackResources resources = data.getPackResources(); + List structureKeys = resources.ids(StructureResources.STRUCTURES).stream().map(ResourceId::path).toList(); + ModuleResourceClosure closure = new ModuleResourceClosure(data); + for (ResourceId id : resources.ids(StructureResources.STRUCTURES)) { + closure.follow(resources.find(StructureResources.STRUCTURES, id).orElseThrow()); + } + Exported generic = new Exported(new TreeSet<>(structureKeys), new TreeSet<>(closure.keysOf(IrisJigsawPool.class)), + new TreeSet<>(closure.keysOf(IrisJigsawPiece.class)), new TreeSet<>(closure.keysOf(LootTable.class)), + new TreeSet<>(closure.objectKeys()), true, List.of()); + StructurePackageClosure structures = StructurePackageClosure.collect(copy.toFile(), structureKeys); + Exported previous = new Exported(new TreeSet<>(structures.structures()), new TreeSet<>(structures.pools()), + new TreeSet<>(structures.pieces()), new TreeSet<>(structures.loot()), new TreeSet<>(structures.objects()), + structures.isValid(), structures.errors()); + + report("every structure of " + packFolder, new Comparison(generic, previous)); + + assertTrue(String.join("; ", previous.errors), previous.isValid); + assertEquals(previous.pools, generic.pools); + assertEquals(previous.pieces, generic.pieces); + assertEquals(previous.loot, generic.loot); + assertEquals(previous.objects, generic.objects); + } finally { + data.close(); + } + } + + private static String comparisonPackFolder() { + String packFolder = System.getenv(COMPARISON_PACK_VARIABLE); + Assume.assumeTrue(COMPARISON_PACK_VARIABLE + " names no pack", packFolder != null && !packFolder.isBlank()); + return packFolder; + } + + private Path copyOfComparisonPack(String packFolder) throws Exception { + Path copy = temporaryFolder.newFolder("real-pack").toPath(); + copyTree(Path.of(packFolder), copy); + BuiltInModulesJar.writeInto(copy); + return copy; + } + + /** Prints the sizes of both sides and every key only the generic closure carries, for the reviewer of the gate. */ + private static void report(String what, Comparison comparison) { + Set onlyGenericObjects = new TreeSet<>(comparison.generic.objects); + onlyGenericObjects.removeAll(comparison.previous.objects); + Set onlyGenericLoot = new TreeSet<>(comparison.generic.loot); + onlyGenericLoot.removeAll(comparison.previous.loot); + System.out.println("Export comparison, " + what + ": " + comparison.generic.summary() + "; previously " + + comparison.previous.summary() + "; only the generic closure carries objects " + onlyGenericObjects + + " and loot " + onlyGenericLoot); + } + + private static void assertContains(String what, Set generic, Set previous) { + Set missing = new TreeSet<>(previous); + missing.removeAll(generic); + assertTrue("The generic closure misses " + what + " " + missing, missing.isEmpty()); + } + + /** Follows the dimension {@code dimensionKey} of {@code pack} the way the packager does, both ways. */ + private static Comparison compare(Path pack, String dimensionKey) { + IrisData data = IrisData.openValidation(pack.toFile()); + try { + IrisDimension dimension = data.getDimensionLoader().load(dimensionKey); + Set regions = new LinkedHashSet<>(dimension.getAllRegions(() -> data)); + regions.remove(null); + Set biomes = new LinkedHashSet<>(); + regions.forEach(region -> biomes.addAll(region.getAllBiomes(() -> data))); + biomes.addAll(dimension.getReachableBiomes(() -> data)); + biomes.remove(null); + List placements = new ArrayList<>(); + regions.forEach(region -> placements.addAll(ObjectPlacements.all(region))); + biomes.forEach(biome -> placements.addAll(ObjectPlacements.all(biome))); + + ModuleResourceClosure closure = new ModuleResourceClosure(data); + closure.followSettingsOf(dimension); + regions.forEach(closure::followSettingsOf); + biomes.forEach(closure::followSettingsOf); + closure.follow(placements); + Exported generic = new Exported( + new TreeSet<>(closure.keysOf(IrisStructure.class)), + new TreeSet<>(closure.keysOf(IrisJigsawPool.class)), + new TreeSet<>(closure.keysOf(IrisJigsawPiece.class)), + new TreeSet<>(closure.keysOf(LootTable.class)), + new TreeSet<>(closure.objectKeys()), true, List.of()); + + StructurePackageClosure structures = StructurePackageClosure.collect(pack.toFile(), generic.structures); + Set previousObjects = new TreeSet<>(structures.objects()); + placements.forEach(placement -> placement.place().stream().filter(key -> key != null && !key.isBlank()).forEach(previousObjects::add)); + Exported previous = new Exported(new TreeSet<>(structures.structures()), new TreeSet<>(structures.pools()), + new TreeSet<>(structures.pieces()), new TreeSet<>(structures.loot()), previousObjects, + structures.isValid(), structures.errors()); + return new Comparison(generic, previous); + } finally { + data.close(); + } + } + + private Path fixturePack() throws Exception { + File pack = temporaryFolder.newFolder("pack"); + write(pack, "dimensions/main.json", "{\"regions\":[\"region\"],\"structures\":[{\"structures\":[\"castle\"]}]}"); + write(pack, "regions/region.json", "{\"landBiomes\":[\"biome\"]}"); + write(pack, "biomes/biome.json", "{\"name\":\"Biome\",\"objects\":[{\"place\":[\"tree\"]}]}"); + write(pack, "structures/castle.json", "{\"startPool\":\"castle/start\",\"loot\":[\"castle/chest\"]}"); + write(pack, "structures/unused.json", "{\"startPool\":\"castle/start\"}"); + write(pack, "jigsaw-pools/castle/start.json", "{\"pieces\":[{\"piece\":\"castle/keep\"}],\"fallback\":\"castle/end\"}"); + write(pack, "jigsaw-pools/castle/end.json", "{\"pieces\":[]}"); + write(pack, "jigsaw-pieces/castle/keep.json", "{\"object\":\"castle/keep\",\"connectors\":[{\"pool\":\"castle/end\"}]}"); + write(pack, "loot/castle/chest.json", "{\"name\":\"Chest\",\"loot\":[{\"type\":\"stick\"}]}"); + write(pack, "objects/castle/keep.iob", "object"); + write(pack, "objects/tree.iob", "object"); + BuiltInModulesJar.writeInto(pack.toPath()); + return pack.toPath(); + } + + private static void copyTree(Path source, Path target) throws Exception { + try (Stream files = Files.walk(source)) { + for (Path file : files.toList()) { + Path copied = target.resolve(source.relativize(file).toString()); + if (Files.isDirectory(file)) { + Files.createDirectories(copied); + } else { + Files.copy(file, copied, StandardCopyOption.REPLACE_EXISTING); + } + } + } + } + + private static void write(File root, String relative, String content) throws Exception { + Path path = root.toPath().resolve(relative); + Files.createDirectories(path.getParent()); + Files.writeString(path, content, StandardCharsets.UTF_8); + } + + private record Exported(Set structures, Set pools, Set pieces, Set loot, + Set objects, boolean isValid, List errors) { + String summary() { + return structures.size() + " structures, " + pools.size() + " pools, " + pieces.size() + " pieces, " + + loot.size() + " loot tables, " + objects.size() + " objects"; + } + } + + private record Comparison(Exported generic, Exported previous) { + } +} diff --git a/core/src/test/java/art/arcane/iris/core/pack/PackExportClosureTest.java b/core/src/test/java/art/arcane/iris/core/pack/IrisPackageCompilerSourceTest.java similarity index 59% rename from core/src/test/java/art/arcane/iris/core/pack/PackExportClosureTest.java rename to core/src/test/java/art/arcane/iris/core/pack/IrisPackageCompilerSourceTest.java index 1ad61241a..677297ac7 100644 --- a/core/src/test/java/art/arcane/iris/core/pack/PackExportClosureTest.java +++ b/core/src/test/java/art/arcane/iris/core/pack/IrisPackageCompilerSourceTest.java @@ -1,41 +1,13 @@ package art.arcane.iris.core.pack; -import art.arcane.iris.builtin.objects.ObjectPlacement; -import art.arcane.volmlib.util.collection.KList; import org.junit.Test; import java.nio.file.Files; import java.nio.file.Path; -import java.util.List; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -public class PackExportClosureTest { - private static ObjectPlacement placement(String objectKey) { - return ObjectPlacement.builder().place(List.of(objectKey)).build(); - } - - @Test - public void collectsObjectKeysFromPlacements() { - List placements = List.of( - placement("houses/hut"), - placement("trees/oak"), - placement("houses/hut")); - - assertEquals(List.of("houses/hut", "trees/oak"), - PackExportClosure.collectObjectKeys(placements).stream().sorted().toList()); - } - - @Test - public void toleratesNullPlacementsAndBlankKeys() { - KList placements = new KList<>(); - placements.add(placement("")); - placements.add((ObjectPlacement) null); - - assertTrue(PackExportClosure.collectObjectKeys(placements).isEmpty()); - } - +public class IrisPackageCompilerSourceTest { /** * Source guard: the packager must export the ambient-spawning graph. Spawner and marker * folders were silently omitted from exports, leaving dangling entitySpawners references.