Export structures and objects through the generic resource closure

The packager no longer runs StructurePackageClosure: ModuleResourceClosure
already follows the structure, pool, piece and loot references the model
annotates, and now also collects the object keys everything it follows names.
Pack validation runs first and reports broken structure graphs.

ExportClosureComparisonTest is the gate. On the overworld pack, following every
structure gives the same 210 pools, 1169 pieces and 1169 objects as before. The
export now also carries 10 objects that floating child biome object overrides
place, which the old export left out.

PackExportClosure had no callers and is removed.
This commit is contained in:
piexel committed 2026-11-06 10:07:32 +01:00
1 parent 4e7a46fb2d
commit 5979e3d49d
5 files changed
+246 -121

No files matched your search

@@ -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<Class<?>, ResourceKind<?>> kindsByType = new HashMap<>();
private final Map<ResourceKind<?>, Map<String, Object>> found = new LinkedHashMap<>();
private final Set<Object> visited = java.util.Collections.newSetFromMap(new IdentityHashMap<>());
private final Map<Class<?>, Set<String>> exportedElsewhere = new HashMap<>();
private final Set<String> 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<String> 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<String> objectKeys() {
return List.copyOf(objectKeys);
}
/**
* Writes every resource found as {@code <folder of its kind>/<key>.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<String> 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<String, Object> ofKind = found.computeIfAbsent(kind, ignored -> new LinkedHashMap<>());
@@ -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 <https://www.gnu.org/licenses/>.
*/
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<String> collectObjectKeys(Iterable<ObjectPlacement> placements) {
KSet<String> 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;
}
}
@@ -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<IrisRegion> regions = new KSet<>();
KSet<IrisBiome> biomes = new KSet<>();
KSet<IrisGenerator> generators = new KSet<>();
ModuleResourceClosure moduleResources = new ModuleResourceClosure(dm).exportingElsewhere(IrisStructure.class);
ModuleResourceClosure moduleResources = new ModuleResourceClosure(dm);
KSet<IrisBlockData> 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<String> structureKeys = moduleResources.keysOf(IrisStructure.class);
KList<String> exportedObjectNames = new KList<>();
List<String> 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);
@@ -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<String> 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<String> onlyGenericObjects = new TreeSet<>(comparison.generic.objects);
onlyGenericObjects.removeAll(comparison.previous.objects);
Set<String> 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<String> generic, Set<String> previous) {
Set<String> 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<IrisRegion> regions = new LinkedHashSet<>(dimension.getAllRegions(() -> data));
regions.remove(null);
Set<IrisBiome> biomes = new LinkedHashSet<>();
regions.forEach(region -> biomes.addAll(region.getAllBiomes(() -> data)));
biomes.addAll(dimension.getReachableBiomes(() -> data));
biomes.remove(null);
List<ObjectPlacement> 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<String> 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<Path> 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<String> structures, Set<String> pools, Set<String> pieces, Set<String> loot,
Set<String> objects, boolean isValid, List<String> 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) {
}
}
@@ -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<ObjectPlacement> 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<ObjectPlacement> 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.