diff --git a/core/src/main/java/art/arcane/iris/core/modules/GenerationLayers.java b/core/src/main/java/art/arcane/iris/core/modules/GenerationLayers.java new file mode 100644 index 000000000..b1788b4f4 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/GenerationLayers.java @@ -0,0 +1,86 @@ +/* + * 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.modules; + +import art.arcane.volmlib.util.mantle.runtime.MantleChunk; +import art.arcane.volmlib.util.matter.Matter; +import art.arcane.volmlib.util.matter.MatterSlice; +import com.volmit.host.MantleRegistration; +import com.volmit.mantle.MantleLifetime; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * The module mantle layers a pack registers with {@link MantleLifetime#GENERATION}: they only serve the chunks + * generating around them, so Iris removes them when it cleans a generated mantle chunk, as it deletes its own + * transient slices. Persistent layers, and layers no loaded module registers, stay. + */ +public final class GenerationLayers { + /** No generation layers: cleanup leaves module layers alone. */ + public static final GenerationLayers NONE = new GenerationLayers(Set.of()); + + private static final int SECTION_SHIFT = 4; + private static final int LOCAL_MASK = 15; + + private final Set layerIds; + + private GenerationLayers(Set layerIds) { + this.layerIds = layerIds; + } + + public static GenerationLayers of(Collection> registrations) { + Set generation = registrations.stream() + .filter(registration -> registration.lifetime() == MantleLifetime.GENERATION) + .map(registration -> registration.key().id()) + .collect(Collectors.toUnmodifiableSet()); + return generation.isEmpty() ? NONE : new GenerationLayers(generation); + } + + /** Removes the generation layers' values from every block of {@code chunk}. */ + public void removeFrom(MantleChunk chunk) { + if (layerIds.isEmpty()) { + return; + } + synchronized (chunk) { + for (Kept kept : keptValues(chunk)) { + MatterSlice slice = chunk.getOrCreate(kept.mantleY() >> SECTION_SHIFT).slice(ModuleLayerValues.class); + slice.set(kept.x(), kept.mantleY() & LOCAL_MASK, kept.z(), kept.values().isEmpty() ? null : kept.values()); + } + } + } + + /** The blocks whose values change, with what they keep; collected first so the slices are not changed while iterated. */ + private List keptValues(MantleChunk chunk) { + List kept = new ArrayList<>(); + chunk.iterate(ModuleLayerValues.class, (x, mantleY, z, values) -> { + ModuleLayerValues remaining = values.withoutAll(layerIds); + if (remaining != values) { + kept.add(new Kept(x, mantleY, z, remaining)); + } + }); + return kept; + } + + private record Kept(int x, int mantleY, int z, ModuleLayerValues values) { + } +} diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisModuleMantle.java b/core/src/main/java/art/arcane/iris/core/modules/IrisModuleMantle.java index edf85c7ac..950f33ba8 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/IrisModuleMantle.java +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisModuleMantle.java @@ -33,10 +33,14 @@ import com.volmit.mantle.MantleVisitor; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Objects; +import java.util.TreeMap; import java.util.function.Function; +import java.util.function.UnaryOperator; import java.util.stream.Collectors; /** @@ -44,12 +48,15 @@ import java.util.stream.Collectors; * Every module layer a block holds lives in one {@link ModuleLayerValues} slice, so a layer needs no slice type * of its own and a layer no loaded module registers survives unchanged. Only registered layers can be used. * Changing a block's values is atomic per mantle chunk, so stages running on neighboring chunks at once never - * lose each other's layers. + * lose each other's layers. {@link #edit()} buffers changes across layers until they are committed; while an + * edit is open, the view itself only reads. */ final class IrisModuleMantle implements Mantle { private static final int CHUNK_SHIFT = 4; private static final int SECTION_SHIFT = 4; private static final int LOCAL_MASK = 15; + private static final Comparator POSITION_ORDER = Comparator.comparingInt(Position::x) + .thenComparingInt(Position::y).thenComparingInt(Position::z); private static final Comparator> VISIT_ORDER = Comparator.>comparingInt(Stored::x) .thenComparingInt(Stored::y).thenComparingInt(Stored::z); @@ -62,6 +69,7 @@ final class IrisModuleMantle implements Mantle { private final int minY; private final Chunks chunks; private final Map> registrations; + private BufferedEdit openEdit; /** @param minY the world height of the mantle's lowest block */ IrisModuleMantle(BlockBox bounds, int minY, Chunks chunks, Collection> registrations) { @@ -77,8 +85,21 @@ final class IrisModuleMantle implements Mantle { } @Override - @SuppressWarnings("unchecked") // The registration's type was compared with the key's. public MantleLayer layer(MantleKey key) { + return new Layer<>(key, codecOf(key)); + } + + @Override + public MantleEdit edit() { + if (openEdit != null) { + throw new IllegalStateException("This mantle already has an open edit; commit or close it first"); + } + openEdit = new BufferedEdit(); + return openEdit; + } + + @SuppressWarnings("unchecked") // The registration's type was compared with the key's. + private MantleCodec codecOf(MantleKey key) { Objects.requireNonNull(key, "key"); MantleRegistration registration = registrations.get(key.id()); if (registration == null) { @@ -88,12 +109,7 @@ final class IrisModuleMantle implements Mantle { throw new IllegalArgumentException("Mantle layer " + key.id() + " is registered as " + registration.key().type().getName() + ", not " + key.type().getName()); } - return new Layer<>(key, (MantleCodec) registration.codec()); - } - - @Override - public MantleEdit edit() { - throw new UnsupportedOperationException("Buffered mantle edits are not bridged to Iris yet; write through layer(...)"); + return (MantleCodec) registration.codec(); } private record Stored(int x, int y, int z, T value) {} @@ -115,16 +131,14 @@ final class IrisModuleMantle implements Mantle { @Override public void set(int x, int y, int z, T value) { - Objects.requireNonNull(value, "value"); - if (!key.type().isInstance(value)) { - throw new IllegalArgumentException("Mantle layer " + key.id() + " holds " + key.type().getName() + ", not " - + value.getClass().getName()); - } + requireNoOpenEdit(); + requireValue(key, value); change(x, y, z, values -> values.with(key, codec, value)); } @Override public void remove(int x, int y, int z) { + requireNoOpenEdit(); change(x, y, z, values -> values.without(key.id())); } @@ -138,22 +152,12 @@ final class IrisModuleMantle implements Mantle { @Override public void clear(BlockBox area) { + requireNoOpenEdit(); for (Stored stored : storedIn(area)) { remove(stored.x(), stored.y(), stored.z()); } } - private void change(int x, int y, int z, Function change) { - MantleChunk chunk = chunkAt(x, y, z); - int mantleY = y - minY; - synchronized (chunk) { - ModuleLayerValues current = valuesAt(chunk, x, mantleY, z); - ModuleLayerValues changed = change.apply(current == null ? ModuleLayerValues.EMPTY : current); - MatterSlice slice = chunk.getOrCreate(mantleY >> SECTION_SHIFT).slice(ModuleLayerValues.class); - slice.set(x & LOCAL_MASK, mantleY & LOCAL_MASK, z & LOCAL_MASK, changed.isEmpty() ? null : changed); - } - } - /** This layer's values in {@code area}, ordered by x, then y, then z, as {@link MantleLayer#visit} promises. */ private List> storedIn(BlockBox area) { requireInside(area); @@ -182,6 +186,204 @@ final class IrisModuleMantle implements Mantle { } } + /** Where an edit stands: only an open edit buffers and reads; a failed commit leaves it to be closed. */ + private enum EditState { OPEN, COMMITTED, FAILED, CLOSED } + + /** One buffered change of one layer at one block. */ + private sealed interface Change permits Put, Removal { + ModuleLayerValues applyTo(ModuleLayerValues values); + } + + private record Put(MantleKey key, MantleCodec codec, T value) implements Change { + @Override + public ModuleLayerValues applyTo(ModuleLayerValues values) { + return values.with(key, codec, value); + } + } + + private record Removal(String layerId) implements Change { + @Override + public ModuleLayerValues applyTo(ModuleLayerValues values) { + return values.without(layerId); + } + } + + private record Position(int x, int y, int z) {} + + /** + * Changes across layers, buffered per block in x, y, z order, that reach the mantle only on {@link #commit()}. + * Reads and visits see them over the unchanged mantle. + */ + private final class BufferedEdit implements MantleEdit { + private final TreeMap> changes = new TreeMap<>(POSITION_ORDER); + private EditState state = EditState.OPEN; + + @Override + public BlockBox bounds() { + return bounds; + } + + @Override + public MantleLayer layer(MantleKey key) { + requireOpen(); + return new EditLayer<>(new Layer<>(key, codecOf(key))); + } + + @Override + public MantleEdit edit() { + throw new UnsupportedOperationException("Mantle edits cannot be nested; write through this edit"); + } + + /** Publishes every change, or none when a chunk it touches is missing; the edit then only closes. */ + @Override + public void commit() { + requireOpen(); + try { + publish(); + } catch (RuntimeException failure) { + state = EditState.FAILED; + throw failure; + } + end(EditState.COMMITTED); + } + + @Override + public void close() { + if (state == EditState.OPEN || state == EditState.FAILED) { + changes.clear(); + end(EditState.CLOSED); + } + } + + private void publish() { + for (Position position : changes.keySet()) { + chunkAt(position.x(), position.y(), position.z()); + } + changes.forEach((position, blockChanges) -> change(position.x(), position.y(), position.z(), values -> { + ModuleLayerValues changed = values; + for (Change layerChange : blockChanges.values()) { + changed = layerChange.applyTo(changed); + } + return changed; + })); + } + + private void end(EditState ended) { + state = ended; + openEdit = null; + } + + private void requireOpen() { + if (state != EditState.OPEN) { + throw new IllegalStateException("This mantle edit is " + state.name().toLowerCase(Locale.ROOT) + "; open a new one with edit()"); + } + } + + private Change changeAt(int x, int y, int z, String layerId) { + Map blockChanges = changes.get(new Position(x, y, z)); + return blockChanges == null ? null : blockChanges.get(layerId); + } + + private void buffer(int x, int y, int z, String layerId, Change change) { + if (!bounds.contains(x, y, z)) { + throw new IndexOutOfBoundsException("Block " + x + ", " + y + ", " + z + " is outside the mantle this invocation prepared, " + bounds); + } + changes.computeIfAbsent(new Position(x, y, z), ignored -> new LinkedHashMap<>()).put(layerId, change); + } + + /** A layer seen through the edit: buffered changes over the mantle's stored values. */ + private final class EditLayer implements MantleLayer { + private final Layer stored; + + EditLayer(Layer stored) { + this.stored = stored; + } + + @Override + public T get(int x, int y, int z) { + requireOpen(); + Change change = changeAt(x, y, z, stored.key.id()); + if (change == null) { + return stored.get(x, y, z); + } + return change instanceof Put put ? stored.key.type().cast(put.value()) : null; + } + + @Override + public void set(int x, int y, int z, T value) { + requireOpen(); + requireValue(stored.key, value); + buffer(x, y, z, stored.key.id(), new Put<>(stored.key, stored.codec, value)); + } + + @Override + public void remove(int x, int y, int z) { + requireOpen(); + buffer(x, y, z, stored.key.id(), new Removal(stored.key.id())); + } + + @Override + public void visit(BlockBox area, MantleVisitor visitor) { + Objects.requireNonNull(visitor, "visitor"); + visible(area).forEach((position, value) -> visitor.accept(position.x(), position.y(), position.z(), value)); + } + + @Override + public void clear(BlockBox area) { + for (Position position : visible(area).keySet()) { + remove(position.x(), position.y(), position.z()); + } + } + + /** This layer's values in {@code area} as the edit sees them, in x, y, z order. */ + private TreeMap visible(BlockBox area) { + requireOpen(); + TreeMap visible = new TreeMap<>(POSITION_ORDER); + for (Stored value : stored.storedIn(area)) { + visible.put(new Position(value.x(), value.y(), value.z()), value.value()); + } + changes.forEach((position, blockChanges) -> { + Change change = blockChanges.get(stored.key.id()); + if (change == null || !area.contains(position.x(), position.y(), position.z())) { + return; + } + if (change instanceof Put put) { + visible.put(position, stored.key.type().cast(put.value())); + } else { + visible.remove(position); + } + }); + return visible; + } + } + } + + /** Replaces the values of one block under the chunk's lock, so concurrent stages on neighboring chunks keep each other's layers. */ + private void change(int x, int y, int z, UnaryOperator change) { + MantleChunk chunk = chunkAt(x, y, z); + int mantleY = y - minY; + synchronized (chunk) { + ModuleLayerValues current = valuesAt(chunk, x, mantleY, z); + ModuleLayerValues changed = change.apply(current == null ? ModuleLayerValues.EMPTY : current); + MatterSlice slice = chunk.getOrCreate(mantleY >> SECTION_SHIFT).slice(ModuleLayerValues.class); + slice.set(x & LOCAL_MASK, mantleY & LOCAL_MASK, z & LOCAL_MASK, changed.isEmpty() ? null : changed); + } + } + + private void requireNoOpenEdit() { + if (openEdit != null) { + throw new IllegalStateException("This mantle has an open edit; write through the edit until it is committed or closed"); + } + } + + private static void requireValue(MantleKey key, T value) { + Objects.requireNonNull(value, "value"); + if (!key.type().isInstance(value)) { + throw new IllegalArgumentException("Mantle layer " + key.id() + " holds " + key.type().getName() + ", not " + + value.getClass().getName()); + } + } + private MantleChunk chunkAt(int x, int y, int z) { if (!bounds.contains(x, y, z)) { throw new IndexOutOfBoundsException("Block " + x + ", " + y + ", " + z + " is outside the mantle this invocation prepared, " + bounds); diff --git a/core/src/main/java/art/arcane/iris/core/modules/ModuleLayerValues.java b/core/src/main/java/art/arcane/iris/core/modules/ModuleLayerValues.java index 91fd7bf55..224ee1f81 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/ModuleLayerValues.java +++ b/core/src/main/java/art/arcane/iris/core/modules/ModuleLayerValues.java @@ -31,6 +31,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Set; import java.util.TreeMap; /** @@ -78,6 +79,16 @@ public final class ModuleLayerValues { return changed.isEmpty() ? EMPTY : new ModuleLayerValues(changed); } + /** These values without any of {@code layerIds}'. */ + public ModuleLayerValues withoutAll(Set layerIds) { + if (layerIds.stream().noneMatch(layers::containsKey)) { + return this; + } + TreeMap changed = new TreeMap<>(layers); + changed.keySet().removeAll(layerIds); + return changed.isEmpty() ? EMPTY : new ModuleLayerValues(changed); + } + /** Layer count, then per layer its id, codec version, payload length and payload. */ public void write(DataOutputStream output) throws IOException { output.writeInt(layers.size()); diff --git a/core/src/main/java/art/arcane/iris/engine/IrisEngineMantle.java b/core/src/main/java/art/arcane/iris/engine/IrisEngineMantle.java index 2888c4367..ab836581f 100644 --- a/core/src/main/java/art/arcane/iris/engine/IrisEngineMantle.java +++ b/core/src/main/java/art/arcane/iris/engine/IrisEngineMantle.java @@ -20,7 +20,9 @@ package art.arcane.iris.engine; import art.arcane.iris.core.IrisSettings; import art.arcane.iris.core.loader.IrisData; +import art.arcane.iris.core.modules.GenerationLayers; import art.arcane.iris.core.modules.ModuleMantleComponent; +import art.arcane.iris.core.modules.PackModulesException; import art.arcane.iris.core.tools.WorldMaintenance; import art.arcane.iris.engine.data.cache.AtomicCache; import art.arcane.iris.engine.framework.Engine; @@ -70,6 +72,8 @@ public class IrisEngineMantle implements EngineMantle { private final AtomicCache> stageComponents = new AtomicCache<>(); @Getter(AccessLevel.NONE) private final AtomicCache> passes = new AtomicCache<>(); + @Getter(AccessLevel.NONE) + private final AtomicCache generationLayers = new AtomicCache<>(); public IrisEngineMantle(Engine engine) { this.engine = engine; @@ -98,6 +102,19 @@ public class IrisEngineMantle implements EngineMantle { public void hotload() { stageComponents.reset(); passes.reset(); + generationLayers.reset(); + } + + /** A pack whose modules fail to load keeps its module layers: without the modules, nothing says which are short-lived. */ + @Override + public GenerationLayers getGenerationLayers() { + return generationLayers.aquire(() -> { + try { + return GenerationLayers.of(engine.getData().getModules().pack().catalog().mantle()); + } catch (PackModulesException failedLoad) { + return GenerationLayers.NONE; + } + }); } private List stageComponents() { diff --git a/core/src/main/java/art/arcane/iris/engine/mantle/EngineMantle.java b/core/src/main/java/art/arcane/iris/engine/mantle/EngineMantle.java index d7f31a28b..068bc6ae8 100644 --- a/core/src/main/java/art/arcane/iris/engine/mantle/EngineMantle.java +++ b/core/src/main/java/art/arcane/iris/engine/mantle/EngineMantle.java @@ -20,6 +20,8 @@ package art.arcane.iris.engine.mantle; import art.arcane.iris.core.IrisSettings; import art.arcane.iris.core.loader.IrisData; +import art.arcane.iris.core.modules.GenerationLayers; +import art.arcane.iris.core.modules.ModuleLayerValues; import art.arcane.iris.engine.IrisComplex; import art.arcane.iris.engine.UpperDimensionContext; import art.arcane.iris.engine.framework.Engine; @@ -253,6 +255,7 @@ public interface EngineMantle extends MatterGenerator { MantleSliceRetention.deleteUnlessRetained(chunk, MatterFluidBody.class); MantleSliceRetention.deleteUnlessRetained(chunk, MatterMarker.class); MantleSliceRetention.deleteUnlessRetained(chunk, TreeBlockMaterial.class); + removeGenerationLayersUnlessRetained(chunk); chunk.trimSlices(); }); } finally { @@ -268,6 +271,7 @@ public interface EngineMantle extends MatterGenerator { MantleSliceRetention.deleteUnlessRetained(chunk, UpdateMatter.class); MantleSliceRetention.deleteUnlessRetained(chunk, MatterCavern.class); MantleSliceRetention.deleteUnlessRetained(chunk, MatterFluidBody.class); + removeGenerationLayersUnlessRetained(chunk); chunk.trimSlices(); }); } finally { @@ -275,6 +279,18 @@ public interface EngineMantle extends MatterGenerator { } } + /** The pack's module mantle layers that cleanup removes; none unless the engine's pack registers some. */ + default GenerationLayers getGenerationLayers() { + return GenerationLayers.NONE; + } + + private void removeGenerationLayersUnlessRetained(MantleChunk chunk) { + if (MantleSliceRetention.isRetained(ModuleLayerValues.class)) { + return; + } + getGenerationLayers().removeFrom(chunk); + } + default int getUnloadRegionCount() { return getMantle().getUnloadRegionCount(); } diff --git a/core/src/test/java/art/arcane/iris/core/modules/IrisModuleMantleTest.java b/core/src/test/java/art/arcane/iris/core/modules/IrisModuleMantleTest.java index 27e9e077e..e99ac3326 100644 --- a/core/src/test/java/art/arcane/iris/core/modules/IrisModuleMantleTest.java +++ b/core/src/test/java/art/arcane/iris/core/modules/IrisModuleMantleTest.java @@ -41,7 +41,9 @@ import com.volmit.host.MantleRegistration; import com.volmit.host.ModuleHost; import com.volmit.mantle.BlockBox; import com.volmit.mantle.MantleCodec; +import com.volmit.mantle.MantleEdit; import com.volmit.mantle.MantleKey; +import com.volmit.mantle.MantleLayer; import com.volmit.mantle.MantleLifetime; import com.volmit.stage.Stage; import com.volmit.stage.StageGraph; @@ -169,6 +171,93 @@ public class IrisModuleMantleTest { assertArrayEquals(bytes(ModuleLayerValues.EMPTY.with(DEPTHS, NUMBER, 4)), bytes(withoutCodecs)); } + @Test + public void anEditBuffersChangesAcrossLayersUntilItCommits() { + IrisModuleMantle mantle = mantle(new BlockBox(-16, MIN_Y, -16, 32, 0, 32)); + mantle.layer(MARKS).set(1, -60, 1, "stored"); + mantle.layer(MARKS).set(2, -60, 1, "removed"); + List visited = new ArrayList<>(); + + try (MantleEdit edit = mantle.edit()) { + edit.layer(MARKS).set(20, -50, 0, "added"); + edit.layer(MARKS).remove(2, -60, 1); + edit.layer(DEPTHS).set(20, -50, 0, 3); + edit.layer(MARKS).visit(new BlockBox(-16, MIN_Y, -16, 32, 0, 32), (x, y, z, value) -> visited.add(value + "@" + x)); + + assertEquals("added", edit.layer(MARKS).get(20, -50, 0)); + assertNull(edit.layer(MARKS).get(2, -60, 1)); + assertNull("the mantle is unchanged until the commit", mantle.layer(MARKS).get(20, -50, 0)); + assertEquals("removed", mantle.layer(MARKS).get(2, -60, 1)); + edit.commit(); + } + + assertEquals(List.of("stored@1", "added@20"), visited); + assertEquals("added", mantle.layer(MARKS).get(20, -50, 0)); + assertEquals(Integer.valueOf(3), mantle.layer(DEPTHS).get(20, -50, 0)); + assertNull(mantle.layer(MARKS).get(2, -60, 1)); + assertEquals("stored", mantle.layer(MARKS).get(1, -60, 1)); + } + + @Test + public void aClosedEditChangesNothingAndAnOpenEditKeepsTheMantleReadOnly() { + IrisModuleMantle mantle = mantle(new BlockBox(0, MIN_Y, 0, 16, 0, 16)); + MantleEdit edit = mantle.edit(); + MantleLayer marks = edit.layer(MARKS); + marks.set(1, -60, 1, "discarded"); + + assertThrows(IllegalStateException.class, () -> mantle.layer(MARKS).set(0, -60, 0, "direct")); + assertThrows(IllegalStateException.class, mantle::edit); + assertThrows(UnsupportedOperationException.class, edit::edit); + assertThrows(IndexOutOfBoundsException.class, () -> marks.set(16, -60, 0, "out")); + edit.close(); + edit.close(); + + assertNull(mantle.layer(MARKS).get(1, -60, 1)); + IllegalStateException closed = assertThrows(IllegalStateException.class, () -> marks.get(1, -60, 1)); + assertEquals("This mantle edit is closed; open a new one with edit()", closed.getMessage()); + assertThrows(IllegalStateException.class, edit::commit); + mantle.layer(MARKS).set(0, -60, 0, "direct"); + mantle.edit().commit(); + } + + @Test + public void aCommitThatCannotReachEveryChunkPublishesNothing() { + IrisModuleMantle mantle = new IrisModuleMantle(new BlockBox(0, MIN_Y, 0, 32, 0, 16), MIN_Y, + (chunkX, chunkZ) -> chunkX == 0 ? chunk(chunkX, chunkZ) : null, LAYERS); + MantleEdit edit = mantle.edit(); + edit.layer(MARKS).set(1, -60, 1, "first"); + edit.layer(MARKS).set(20, -60, 1, "unprepared"); + + assertThrows(IllegalStateException.class, edit::commit); + assertNull(mantle.layer(MARKS).get(1, -60, 1)); + assertThrows(IllegalStateException.class, edit::commit); + assertThrows(IllegalStateException.class, () -> mantle.layer(MARKS).set(2, -60, 1, "still open")); + edit.close(); + mantle.layer(MARKS).set(2, -60, 1, "after close"); + } + + @Test + public void cleanupRemovesGenerationLayersAndKeepsPersistentAndUnknownOnes() throws IOException { + IrisModuleMantle mantle = mantle(new BlockBox(0, MIN_Y, 0, 16, 0, 16)); + mantle.layer(MARKS).set(1, -60, 1, "short-lived"); + mantle.layer(DEPTHS).set(1, -60, 1, 7); + mantle.layer(MARKS).set(2, -10, 3, "alone"); + MantleKey unknown = new MantleKey<>("iris:gone/layer", String.class); + ModuleLayerValues fromDisk = ModuleLayerValues.read(input(bytes(ModuleLayerValues.EMPTY.with(unknown, TEXT, "kept") + .with(MARKS, TEXT, "old")))); + chunk(0, 0).getOrCreate((-30 - MIN_Y) >> 4).slice(ModuleLayerValues.class).set(4, (-30 - MIN_Y) & 15, 4, fromDisk); + + GenerationLayers.of(LAYERS).removeFrom(chunk(0, 0)); + + assertNull(mantle.layer(MARKS).get(1, -60, 1)); + assertEquals(Integer.valueOf(7), mantle.layer(DEPTHS).get(1, -60, 1)); + assertNull("a block left without values holds nothing", chunk(0, 0).get((-10 - MIN_Y) >> 4) + .slice(ModuleLayerValues.class).get(2, (-10 - MIN_Y) & 15, 3)); + assertNull(mantle.layer(MARKS).get(4, -30, 4)); + ModuleLayerValues left = chunk(0, 0).get((-30 - MIN_Y) >> 4).slice(ModuleLayerValues.class).get(4, (-30 - MIN_Y) & 15, 4); + assertEquals("a layer no module registers stays", "kept", left.get(unknown, TEXT)); + } + @Test public void mantleStagesWriteAroundTheirChunkThroughIrisChunks() throws IOException { MantleKey planned = new MantleKey<>("iris:veins/planned", String.class); diff --git a/core/src/test/java/art/arcane/iris/engine/mantle/EngineMantleCleanupTest.java b/core/src/test/java/art/arcane/iris/engine/mantle/EngineMantleCleanupTest.java index cc1c8090f..abbbc1e2a 100644 --- a/core/src/test/java/art/arcane/iris/engine/mantle/EngineMantleCleanupTest.java +++ b/core/src/test/java/art/arcane/iris/engine/mantle/EngineMantleCleanupTest.java @@ -1,6 +1,8 @@ package art.arcane.iris.engine.mantle; import art.arcane.iris.core.link.Identifier; +import art.arcane.iris.core.modules.GenerationLayers; +import art.arcane.iris.core.modules.ModuleLayerValues; import art.arcane.iris.engine.framework.TreeBlockMaterial; import art.arcane.iris.spi.IrisPlatform; import art.arcane.iris.spi.IrisPlatforms; @@ -26,6 +28,7 @@ import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -116,6 +119,31 @@ public class EngineMantleCleanupTest { verifyCleanup(fixture.chunk()); } + @Test + public void bothCleanupsRemoveThePacksGenerationLayers() { + CleanupFixture fixture = cleanupFixture(); + GenerationLayers layers = mock(GenerationLayers.class); + doReturn(layers).when(fixture.engineMantle()).getGenerationLayers(); + doReturn(true).when(fixture.engineMantle()).isCovered(7, -4); + + fixture.engineMantle().cleanupChunk(7, -4); + fixture.engineMantle().forceCleanupChunk(7, -4); + + verify(layers, times(2)).removeFrom(fixture.chunk()); + } + + @Test + public void retainedModuleLayersSurviveCleanup() { + MantleSliceRetention.retain(ModuleLayerValues.class.getCanonicalName()); + CleanupFixture fixture = cleanupFixture(); + GenerationLayers layers = mock(GenerationLayers.class); + doReturn(layers).when(fixture.engineMantle()).getGenerationLayers(); + + fixture.engineMantle().forceCleanupChunk(7, -4); + + verify(layers, never()).removeFrom(any()); + } + @SuppressWarnings("unchecked") private CleanupFixture cleanupFixture() { EngineMantle engineMantle = mock(EngineMantle.class, CALLS_REAL_METHODS);