Bridge mantle access for placement stages: writes are buffered with the placement and reach the mantle on its commit

This commit is contained in:
piexel committed 2026-10-08 14:09:27 +02:00
1 parent a07788fffe
commit 9b20fc9c91
8 files changed
+551 -28

No files matched your search

@@ -0,0 +1,88 @@
package art.arcane.iris.core.modules;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.mantle.EngineMantle;
import art.arcane.iris.util.project.matter.IrisMatterSupport;
import art.arcane.volmlib.util.function.Consumer4;
import art.arcane.volmlib.util.io.CountingDataInputStream;
import art.arcane.volmlib.util.mantle.runtime.Mantle;
import art.arcane.volmlib.util.mantle.runtime.MantleChunk;
import art.arcane.volmlib.util.mantle.runtime.MantleDataAdapter;
import art.arcane.volmlib.util.mantle.runtime.MantleHooks;
import art.arcane.volmlib.util.matter.IrisMatter;
import art.arcane.volmlib.util.matter.Matter;
import art.arcane.volmlib.util.matter.MatterSlice;
import com.volmit.mantle.MantleCodec;
import com.volmit.mantle.MantleKey;
import java.io.DataOutputStream;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/** A mantle held in memory, for tests of what stages write into it. Bind an engine to it before the engine's mantle is used. */
public final class InMemoryMantle {
private final int sections;
private final Map<Long, MantleChunk<Matter>> chunks = new HashMap<>();
/** @param sections how many 16 block sections high the mantle is */
public InMemoryMantle(int sections) {
IrisMatterSupport.ensureRegistered();
this.sections = sections;
}
public MantleChunk<Matter> chunk(int chunkX, int chunkZ) {
return chunks.computeIfAbsent(((long) chunkX << 32) ^ (chunkZ & 0xffffffffL), ignored ->
new MantleChunk<>(sections, chunkX, chunkZ, new Sections(), MantleHooks.NONE));
}
/** Whether any chunk has been created, which is whether anything asked for a chunk. */
public boolean isTouched() {
return !chunks.isEmpty();
}
/** Makes {@code engine}'s mantle this one. The platform must be bound already: the engine mantle's constants read it. */
@SuppressWarnings("unchecked")
public void bindTo(Engine engine) {
Mantle<Matter> mantle = mock(Mantle.class);
when(mantle.getChunk(anyInt(), anyInt())).thenAnswer(call -> chunk(call.getArgument(0), call.getArgument(1)));
EngineMantle engineMantle = mock(EngineMantle.class);
when(engineMantle.getMantle()).thenReturn(mantle);
when(engine.getMantle()).thenReturn(engineMantle);
}
/** What the layer holds at a block of the mantle's storage coordinates, or null. */
public <T> T value(MantleKey<T> key, MantleCodec<T> codec, int x, int mantleY, int z) {
MantleChunk<Matter> chunk = chunk(x >> 4, z >> 4);
if (!chunk.exists(mantleY >> 4) || !chunk.get(mantleY >> 4).hasSlice(ModuleLayerValues.class)) {
return null;
}
ModuleLayerValues values = chunk.get(mantleY >> 4).<ModuleLayerValues>getSlice(ModuleLayerValues.class).get(x & 15, mantleY & 15, z & 15);
return values == null ? null : values.get(key, codec);
}
/** Iris's in-memory mantle sections, without the engine's static state that would bind a platform for every later test. */
public static final class Sections implements MantleDataAdapter<Matter> {
public Matter createSection() { return new IrisMatter(16, 16, 16); }
public Matter readSection(CountingDataInputStream input) { throw new UnsupportedOperationException(); }
public void writeSection(Matter section, DataOutputStream output) { throw new UnsupportedOperationException(); }
public void trimSection(Matter section) { section.trimSlices(); }
public boolean isSectionEmpty(Matter section) { return section.getSliceMap().isEmpty(); }
public Class<?> classifyValue(Object value) { return value.getClass(); }
public <T> void set(Matter section, int x, int y, int z, Class<?> type, T value) { throw new UnsupportedOperationException(); }
public <T> void remove(Matter section, int x, int y, int z, Class<T> type) { throw new UnsupportedOperationException(); }
public <T> T get(Matter section, int x, int y, int z, Class<T> type) { return section.<T>slice(type).get(x, y, z); }
public boolean hasSlice(Matter section, Class<?> type) { return section.hasSlice(type); }
public void deleteSlice(Matter section, Class<?> type) { section.deleteSlice(type); }
public <T> void iterate(Matter section, Class<T> type, Consumer4<Integer, Integer, Integer, T> iterator) {
MatterSlice<T> slice = section.getSlice(type);
if (slice != null) {
slice.iterateSync(iterator);
}
}
}
}
@@ -105,7 +105,7 @@ public class IrisModuleMantleTest {
private MantleChunk<Matter> chunk(int chunkX, int chunkZ) {
return chunks.computeIfAbsent(((long) chunkX << 32) ^ (chunkZ & 0xffffffffL), ignored ->
new MantleChunk<>(SECTIONS, chunkX, chunkZ, new Sections(), MantleHooks.NONE));
new MantleChunk<>(SECTIONS, chunkX, chunkZ, new InMemoryMantle.Sections(), MantleHooks.NONE));
}
private IrisModuleMantle mantle(BlockBox bounds) {
@@ -322,28 +322,6 @@ public class IrisModuleMantleTest {
assertEquals(List.of(PackResourceFixture.OAK), found);
}
/** Iris's in-memory mantle sections, without the engine's static state that would bind a platform for every later test. */
private static final class Sections implements MantleDataAdapter<Matter> {
public Matter createSection() { return new IrisMatter(16, 16, 16); }
public Matter readSection(CountingDataInputStream input) { throw new UnsupportedOperationException(); }
public void writeSection(Matter section, DataOutputStream output) { throw new UnsupportedOperationException(); }
public void trimSection(Matter section) { section.trimSlices(); }
public boolean isSectionEmpty(Matter section) { return section.getSliceMap().isEmpty(); }
public Class<?> classifyValue(Object value) { return value.getClass(); }
public <T> void set(Matter section, int x, int y, int z, Class<?> type, T value) { throw new UnsupportedOperationException(); }
public <T> void remove(Matter section, int x, int y, int z, Class<T> type) { throw new UnsupportedOperationException(); }
public <T> T get(Matter section, int x, int y, int z, Class<T> type) { return section.<T>slice(type).get(x, y, z); }
public boolean hasSlice(Matter section, Class<?> type) { return section.hasSlice(type); }
public void deleteSlice(Matter section, Class<?> type) { section.deleteSlice(type); }
public <T> void iterate(Matter section, Class<T> type, Consumer4<Integer, Integer, Integer, T> iterator) {
MatterSlice<T> slice = section.getSlice(type);
if (slice != null) {
slice.iterateSync(iterator);
}
}
}
private static byte[] bytes(ModuleLayerValues values) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
values.write(new DataOutputStream(bytes));
@@ -0,0 +1,149 @@
package art.arcane.iris.core.modules;
import art.arcane.iris.engine.UsableBlockPlatform;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.object.placement.IObjectPlacer;
import com.volmit.host.MantleRegistration;
import com.volmit.mantle.BlockBox;
import com.volmit.mantle.MantleCodec;
import com.volmit.mantle.MantleKey;
import com.volmit.mantle.MantleLifetime;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/** A placement's mantle writes, direct or through an edit, reach the engine's mantle only with the placement's commit. */
public class PlacementMantleTest {
private static final int HEIGHT = 64;
private static final int Y = 20;
private static final MantleKey<String> MARKS = new MantleKey<>("iris:marks/mark", String.class);
private static final MantleCodec<String> TEXT = new MantleCodec<>() {
public int version() { return 1; }
public void write(DataOutput output, String value) throws IOException { output.writeUTF(value); }
public String read(DataInput input, int storedVersion) throws IOException { return input.readUTF(); }
};
private static final List<MantleRegistration<?>> LAYERS =
List.of(new MantleRegistration<>(MARKS, MantleLifetime.GENERATION, TEXT, 1));
private final InMemoryMantle storage = new InMemoryMantle(HEIGHT / 16);
/** A placement area has no vertical limit. */
private final BlockBox area = new BlockBox(-8, Integer.MIN_VALUE, -8, 24, Integer.MAX_VALUE, 24);
@Before
public void bindPlatform() {
UsableBlockPlatform.bind();
}
@After
public void unbindPlatform() {
UsableBlockPlatform.unbind();
}
private Engine engine() {
Engine engine = mock(Engine.class);
when(engine.getHeight()).thenReturn(HEIGHT);
storage.bindTo(engine);
return engine;
}
private ObjectPlacerEdit edit(Engine engine) {
return new ObjectPlacerEdit(mock(IObjectPlacer.class), area, new PlatformBlockStates(key -> null), null,
() -> new PlacementMantle(engine, area, LAYERS));
}
private String stored(int x, int z) {
return storage.value(MARKS, TEXT, x, Y, z);
}
@Test
public void writesReachTheMantleTogetherWhenThePlacementCommits() {
ObjectPlacerEdit placement = edit(engine());
placement.mantle().layer(MARKS).set(1, Y, 1, "near");
placement.mantle().layer(MARKS).set(17, Y, 2, "next chunk");
placement.mantle().layer(MARKS).set(17, Y, 3, "removed");
placement.mantle().layer(MARKS).remove(17, Y, 3);
assertEquals("a stage reads what it wrote", "near", placement.mantle().layer(MARKS).get(1, Y, 1));
assertNull(placement.mantle().layer(MARKS).get(17, Y, 3));
assertNull("nothing is in the mantle before the placement commits", stored(1, 1));
assertNull(stored(17, 2));
placement.commit();
assertEquals("near", stored(1, 1));
assertEquals("next chunk", stored(17, 2));
assertNull(stored(17, 3));
}
@Test
public void aDiscardedPlacementLeavesTheMantleAsItWas() {
ObjectPlacerEdit placement = edit(engine());
placement.mantle().layer(MARKS).set(1, Y, 1, "written");
placement.close();
assertNull(stored(1, 1));
}
@Test
public void aStageCannotOpenAnEditOfItsOwn() {
ObjectPlacerEdit placement = edit(engine());
assertThrows(UnsupportedOperationException.class, () -> placement.mantle().edit());
}
@Test
public void visitsAndClearsStayWithinTheHeightsTheMantleStores() {
ObjectPlacerEdit placement = edit(engine());
placement.mantle().layer(MARKS).set(1, Y, 1, "low");
placement.mantle().layer(MARKS).set(2, HEIGHT - 1, 1, "high");
List<String> visited = new ArrayList<>();
placement.mantle().layer(MARKS).visit(area, (x, y, z, value) -> visited.add(value));
placement.mantle().layer(MARKS).clear(area);
assertEquals(List.of("low", "high"), visited);
assertNull(placement.mantle().layer(MARKS).get(1, Y, 1));
}
@Test
public void aPlacementWhoseStagesNeverAskForTheMantleNeverTouchesIt() {
Engine engine = engine();
ObjectPlacerEdit placement = edit(engine);
placement.commit();
assertFalse(storage.isTouched());
}
@Test
public void writesOutsideThePlacementAreaFail() {
ObjectPlacerEdit placement = edit(engine());
assertThrows(IndexOutOfBoundsException.class, () -> placement.mantle().layer(MARKS).set(24, Y, 0, "out"));
assertThrows("above the heights the mantle stores", IndexOutOfBoundsException.class,
() -> placement.mantle().layer(MARKS).set(0, HEIGHT, 0, "above"));
assertNull("but reading there is allowed and finds nothing", placement.mantle().layer(MARKS).get(0, HEIGHT + 5, 0));
}
@Test
public void aPlacementWithoutAnEngineHasNoMantle() {
ObjectPlacerEdit placement = new ObjectPlacerEdit(mock(IObjectPlacer.class), area, new PlatformBlockStates(key -> null), null, () -> {
throw new UnsupportedOperationException("This placement runs without an Iris engine, so it has no mantle");
});
assertThrows(UnsupportedOperationException.class, placement::mantle);
}
}
@@ -17,6 +17,7 @@ import art.arcane.iris.builtin.blocks.BlocksStages;
import art.arcane.iris.builtin.bore.BoreStages;
import art.arcane.iris.core.loader.IrisData;
import art.arcane.iris.core.modules.BuiltInPack;
import art.arcane.iris.core.modules.InMemoryMantle;
import art.arcane.iris.core.modules.ModuleObjectPlacement;
import art.arcane.iris.core.modules.PackResourceFixture;
import art.arcane.iris.core.modules.ObjectPlacementResult;
@@ -32,10 +33,15 @@ import art.arcane.iris.spi.PlatformRegistries;
import art.arcane.iris.util.project.stream.ProceduralStream;
import art.arcane.volmlib.util.collection.KList;
import art.arcane.volmlib.util.math.RNG;
import com.volmit.FeatureRegistry;
import com.volmit.IrisModule;
import com.volmit.ModuleInfo;
import com.volmit.block.BlockState;
import com.volmit.mantle.BlockBox;
import com.volmit.mantle.Mantle;
import com.volmit.mantle.MantleCodec;
import com.volmit.mantle.MantleKey;
import com.volmit.mantle.MantleLifetime;
import com.volmit.object.GeneratedObject;
import com.volmit.object.ObjectBlock;
import com.volmit.object.ObjectTransform;
@@ -60,6 +66,7 @@ import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
@@ -447,6 +454,63 @@ public class IrisObjectPlacementRegressionTest {
assertEquals(List.of(PackResourceFixture.OAK), found);
}
private static final MantleKey<String> MARKS = new MantleKey<>("test:mark/spot", String.class);
private static final MantleCodec<String> TEXT = new MantleCodec<>() {
public int version() { return 1; }
public void write(java.io.DataOutput output, String value) throws IOException { output.writeUTF(value); }
public String read(java.io.DataInput input, int storedVersion) throws IOException { return input.readUTF(); }
};
/** Marks the mantle beside the anchor. */
private static final class MarkModule implements IrisModule {
private static final PlacementStage<Void> MARK = Stage.placement("test:mark/spot");
@Override
public ModuleInfo info() {
return ModuleInfo.of("test:mark", "Mark", "1");
}
@Override
public void install(StageGraph graph) {
graph.define(MARK).requires(BlocksStages.COMPLETE).run(context -> {
Mantle mantle = context.access().mantle();
mantle.layer(MARKS).set(0, ANCHOR_Y, 0, "first");
mantle.layer(MARKS).set(1, ANCHOR_Y, 0, "second");
});
}
@Override
public void register(FeatureRegistry registry) {
registry.registerMantle(MARKS, MantleLifetime.GENERATION, TEXT);
}
}
@Test
public void aPlacementStageMarksTheMantleWithTheCommitOfThePlacement() throws IOException {
InMemoryMantle storage = new InMemoryMantle(16);
storage.bindTo(engine);
usePack(List.of(new MarkModule()));
ObjectPlacementResult result = place(placement(), ANCHOR_Y, new RecordingPlacer(engine), null);
assertTrue(result.isPlaced());
assertEquals("first", storage.value(MARKS, TEXT, 0, ANCHOR_Y, 0));
assertEquals("second", storage.value(MARKS, TEXT, 1, ANCHOR_Y, 0));
}
@Test
public void aRejectedPlacementLeavesTheMantleUnmarked() throws IOException {
InMemoryMantle storage = new InMemoryMantle(16);
storage.bindTo(engine);
usePack(List.of(new MarkModule(), new VetoModule()));
ObjectPlacementResult result = place(placement(), ANCHOR_Y, new RecordingPlacer(engine), null);
assertFalse(result.isPlaced());
assertNull(storage.value(MARKS, TEXT, 0, ANCHOR_Y, 0));
assertNull(storage.value(MARKS, TEXT, 1, ANCHOR_Y, 0));
}
/** Lays the pack's oak on its side, a quarter turn about X, through the placement's object transformer. */
private static final class TurnModule implements IrisModule {
private static final PlacementStage<Void> TURN = Stage.placement("test:turn/oak");