Serve saved state only for keys the pack's modules registered

The running world's saved state checks each key against the pack
catalog's registered saved keys and refuses an unregistered key or
another key with a registered id. Mantle and saved keys follow the api's
ResourceId ids and StorageCodec.
This commit is contained in:
piexel committed 2026-11-06 12:54:22 +01:00
1 parent 96532f9a22
commit db4972b42a
13 files changed
+229 -124

No files matched your search

@@ -41,8 +41,9 @@ import com.volmit.host.LoadedPack;
import com.volmit.host.ModuleHost;
import com.volmit.geometry.BlockBox;
import com.volmit.geometry.BlockPosition;
import com.volmit.data.Codec;
import com.volmit.data.StorageCodec;
import com.volmit.mantle.MantleKey;
import com.volmit.resource.ResourceId;
import com.volmit.mantle.MantleLifetime;
import com.volmit.placement.PlacedBlock;
import com.volmit.stage.Stage;
@@ -85,8 +86,8 @@ public class DimensionBottomTest {
/** A chunk stage sees the terrain of its chunk and of the 16 blocks around it. */
private static final BlockBox WHOLE_HEIGHT_AROUND_ONE_CHUNK = new BlockBox(-16, BOTTOM, -16, 32, DIMENSION_HEIGHT, 32);
private static final BlockState STONE = BlockState.parse("minecraft:stone");
private static final MantleKey<String> MARKS = new MantleKey<>("test:bottom/mark", String.class);
private static final Codec<String> TEXT = new Codec<>() {
private static final MantleKey<String> MARKS = new MantleKey<>(ResourceId.parse("test:bottom/mark"), String.class);
private static final StorageCodec<String> TEXT = new StorageCodec<>() {
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(); }
@@ -12,7 +12,7 @@ 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.data.Codec;
import com.volmit.data.StorageCodec;
import com.volmit.geometry.BlockBox;
import com.volmit.mantle.CarvedVoid;
import com.volmit.mantle.MantleKey;
@@ -67,7 +67,7 @@ public final class InMemoryMantle {
}
/** What the layer holds at a block of the mantle's storage coordinates, or null. */
public <T> T value(MantleKey<T> key, Codec<T> codec, int x, int mantleY, int z) {
public <T> T value(MantleKey<T> key, StorageCodec<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;
@@ -48,7 +48,7 @@ import com.volmit.host.LoadedPack;
import com.volmit.host.MantleRegistration;
import com.volmit.host.ModuleHost;
import com.volmit.geometry.BlockBox;
import com.volmit.data.Codec;
import com.volmit.data.StorageCodec;
import com.volmit.mantle.MantleEdit;
import com.volmit.mantle.MantleKey;
import com.volmit.mantle.MantleLayer;
@@ -89,14 +89,14 @@ public class IrisModuleMantleTest {
private static final int SECTIONS = 4;
private static final int HEIGHT = SECTIONS * 16;
private static final PackResourceIds IDS = new PackResourceIds(new java.io.File("pack"));
private static final MantleKey<String> MARKS = new MantleKey<>("iris:marks/mark", String.class);
private static final MantleKey<Integer> DEPTHS = new MantleKey<>("iris:marks/depth", Integer.class);
private static final Codec<String> TEXT = new Codec<>() {
private static final MantleKey<String> MARKS = new MantleKey<>(ResourceId.parse("iris:marks/mark"), String.class);
private static final MantleKey<Integer> DEPTHS = new MantleKey<>(ResourceId.parse("iris:marks/depth"), Integer.class);
private static final StorageCodec<String> TEXT = new StorageCodec<>() {
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 Codec<Integer> NUMBER = new Codec<>() {
private static final StorageCodec<Integer> NUMBER = new StorageCodec<>() {
public int version() { return 0; }
public void write(DataOutput output, Integer value) throws IOException { output.writeInt(value); }
public Integer read(DataInput input, int storedVersion) throws IOException { return input.readInt(); }
@@ -300,9 +300,9 @@ public class IrisModuleMantleTest {
IrisModuleMantle mantle = mantle(new BlockBox(0, 0, 0, 16, HEIGHT, 16));
IllegalArgumentException unregistered = assertThrows(IllegalArgumentException.class,
() -> mantle.layer(new MantleKey<>("iris:marks/other", String.class)));
() -> mantle.layer(new MantleKey<>(ResourceId.parse("iris:marks/other"), String.class)));
assertEquals("Mantle layer iris:marks/other is not registered by any module of the pack", unregistered.getMessage());
assertThrows(IllegalArgumentException.class, () -> mantle.layer(new MantleKey<>("iris:marks/mark", Integer.class)));
assertThrows(IllegalArgumentException.class, () -> mantle.layer(new MantleKey<>(ResourceId.parse("iris:marks/mark"), Integer.class)));
assertThrows(IndexOutOfBoundsException.class, () -> mantle.layer(MARKS).set(16, 4, 0, "out"));
assertThrows(IndexOutOfBoundsException.class, () -> mantle.layer(MARKS).visit(new BlockBox(0, 0, 0, 17, HEIGHT, 16), (x, y, z, v) -> { }));
}
@@ -314,7 +314,7 @@ public class IrisModuleMantleTest {
assertEquals("kept", read.get(MARKS, TEXT));
assertEquals(Integer.valueOf(4), read.get(DEPTHS, NUMBER));
ModuleLayerValues withoutCodecs = ModuleLayerValues.read(input(bytes(written))).without(MARKS.id());
ModuleLayerValues withoutCodecs = ModuleLayerValues.read(input(bytes(written))).without(MARKS.id().toString());
assertArrayEquals(bytes(ModuleLayerValues.EMPTY.with(DEPTHS, NUMBER, 4)), bytes(withoutCodecs));
}
@@ -390,7 +390,7 @@ public class IrisModuleMantleTest {
mantle.layer(MARKS).set(1, 4, 1, "short-lived");
mantle.layer(DEPTHS).set(1, 4, 1, 7);
mantle.layer(MARKS).set(2, 54, 3, "alone");
MantleKey<String> unknown = new MantleKey<>("iris:gone/layer", String.class);
MantleKey<String> unknown = new MantleKey<>(ResourceId.parse("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(34 >> 4).slice(ModuleLayerValues.class).set(4, 34 & 15, 4, fromDisk);
@@ -408,7 +408,7 @@ public class IrisModuleMantleTest {
@Test
public void mantleStagesWriteAroundTheirChunkThroughIrisChunks() throws IOException {
MantleKey<String> planned = new MantleKey<>("iris:veins/planned", String.class);
MantleKey<String> planned = new MantleKey<>(ResourceId.parse("iris:veins/planned"), String.class);
IrisModule veins = new IrisModule() {
public ModuleInfo info() { return ModuleInfo.of("iris:veins", "Veins", "1"); }
public void setup(ModuleSetup setup) {
@@ -40,10 +40,16 @@ import art.arcane.iris.engine.object.world.dimension.IrisDimension;
import art.arcane.iris.engine.object.world.region.IrisRegion;
import art.arcane.iris.spi.PlatformWorld;
import art.arcane.volmlib.util.collection.KList;
import com.volmit.IrisModule;
import com.volmit.ModuleInfo;
import com.volmit.ModuleSetup;
import com.volmit.config.Level;
import com.volmit.config.SettingsStack;
import com.volmit.data.SavedKey;
import com.volmit.data.StorageCodec;
import com.volmit.geometry.BlockPosition;
import com.volmit.placement.PlacementRecord;
import com.volmit.resource.ResourceId;
import com.volmit.world.BiomeKind;
import com.volmit.world.PackWorld;
import com.volmit.world.RunningWorld;
@@ -51,12 +57,15 @@ import com.volmit.world.RunningWorld.Sky;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.util.List;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -69,6 +78,9 @@ public class IrisRunningWorldTest {
private static final int BOTTOM = -64;
private static final int HEIGHT = 384;
@Rule
public TemporaryFolder worldFolder = new TemporaryFolder();
private Engine engine;
private PackWorld world;
private RunningWorld running;
@@ -159,6 +171,46 @@ public class IrisRunningWorldTest {
assertEquals(List.of(new BlockPosition(40, 64, 50), new BlockPosition(41, 94, 51)), running.caveFloors(2, 3));
}
@Test
public void savedStateIsServedForTheKeysTheModulesRegistered() throws java.io.IOException {
SavedKey<String> registered = new SavedKey<>(ResourceId.parse("test:saves/note"), String.class, NOTE_CODEC, () -> "empty");
SavedKey<String> unregistered = new SavedKey<>(ResourceId.parse("test:saves/other"), String.class, NOTE_CODEC, () -> "empty");
ModuleResourceFixture.serveModules(engine.getData(), new IrisModule() {
@Override
public ModuleInfo info() {
return ModuleInfo.of("test:saves", "Saves", "1");
}
@Override
public void setup(ModuleSetup setup) {
setup.registry().registerSaved(registered);
}
});
IrisWorld irisWorld = mock(IrisWorld.class);
when(irisWorld.worldFolder()).thenReturn(worldFolder.newFolder());
when(engine.getWorld()).thenReturn(irisWorld);
assertEquals("empty", running.saved(registered));
assertThrows(IllegalArgumentException.class, () -> running.saved(unregistered));
}
private static final StorageCodec<String> NOTE_CODEC = new StorageCodec<>() {
@Override
public int version() {
return 1;
}
@Override
public void write(java.io.DataOutput output, String value) throws java.io.IOException {
output.writeUTF(value);
}
@Override
public String read(java.io.DataInput input, int storedVersion) throws java.io.IOException {
return input.readUTF();
}
};
@Test
public void aStudioWorldIsADebugWorld() {
when(engine.isStudio()).thenReturn(true);
@@ -8,8 +8,9 @@ import com.volmit.mantle.CarvedVoid;
import com.volmit.placement.PlacementTerrain;
import com.volmit.host.MantleRegistration;
import com.volmit.geometry.BlockBox;
import com.volmit.data.Codec;
import com.volmit.data.StorageCodec;
import com.volmit.mantle.MantleKey;
import com.volmit.resource.ResourceId;
import com.volmit.mantle.MantleLifetime;
import org.junit.After;
import org.junit.Before;
@@ -34,8 +35,8 @@ import static org.mockito.Mockito.when;
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 Codec<String> TEXT = new Codec<>() {
private static final MantleKey<String> MARKS = new MantleKey<>(ResourceId.parse("iris:marks/mark"), String.class);
private static final StorageCodec<String> TEXT = new StorageCodec<>() {
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(); }
@@ -2,8 +2,9 @@ package art.arcane.iris.core.modules;
import art.arcane.iris.core.lifecycle.VanishedWorldStorage;
import com.volmit.ModuleLog;
import com.volmit.data.Codec;
import com.volmit.world.SavedKey;
import com.volmit.data.SavedKey;
import com.volmit.data.StorageCodec;
import com.volmit.resource.ResourceId;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
@@ -15,6 +16,7 @@ import java.io.DataOutput;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertArrayEquals;
@@ -43,7 +45,7 @@ public class SavedWorldStateTest {
public void startWorld() {
VanishedWorldStorage.reset();
log = mock(ModuleLog.class);
state = new SavedWorldState(worldFolder::getRoot, log);
state = stateIn(worldFolder::getRoot);
}
@After
@@ -51,6 +53,10 @@ public class SavedWorldStateTest {
VanishedWorldStorage.reset();
}
private SavedWorldState stateIn(java.util.function.Supplier<File> folder) {
return new SavedWorldState(folder, () -> List.of(COUNT, OTHER, NOTHING, BROKEN), log);
}
/** The count a module keeps, stored as one int and rejecting every other version. */
private static final class Count {
private final AtomicInteger value;
@@ -60,7 +66,7 @@ public class SavedWorldStateTest {
}
}
private static final Codec<Count> COUNT_CODEC = new Codec<>() {
private static final StorageCodec<Count> COUNT_CODEC = new StorageCodec<>() {
@Override
public int version() {
return CODEC_VERSION;
@@ -80,8 +86,64 @@ public class SavedWorldStateTest {
}
};
private static final SavedKey<Count> COUNT = new SavedKey<>("test:counts/waves", Count.class, COUNT_CODEC,
() -> new Count(INITIAL_COUNT));
private static final StorageCodec<Count> NOTHING_CODEC = new StorageCodec<>() {
@Override
public int version() {
return 1;
}
@Override
public void write(DataOutput output, Count count) {
}
@Override
public Count read(DataInput input, int storedVersion) {
return null;
}
};
private static final StorageCodec<Count> FAILING_CODEC = new StorageCodec<>() {
@Override
public int version() {
return 1;
}
@Override
public void write(DataOutput output, Count count) throws IOException {
throw new IOException("cannot write");
}
@Override
public Count read(DataInput input, int storedVersion) {
return new Count(0);
}
};
private static final StorageCodec<String> TEXT_CODEC = new StorageCodec<>() {
@Override
public int version() {
return 1;
}
@Override
public void write(DataOutput output, String value) throws IOException {
output.writeUTF(value);
}
@Override
public String read(DataInput input, int storedVersion) throws IOException {
return input.readUTF();
}
};
private static final SavedKey<Count> COUNT = countKey("test:counts/waves", COUNT_CODEC);
private static final SavedKey<Count> OTHER = countKey("test:counts/other", COUNT_CODEC);
private static final SavedKey<Count> NOTHING = countKey("test:counts/nothing", NOTHING_CODEC);
private static final SavedKey<Count> BROKEN = countKey("test:counts/broken", FAILING_CODEC);
private static SavedKey<Count> countKey(String id, StorageCodec<Count> codec) {
return new SavedKey<>(ResourceId.parse(id), Count.class, codec, () -> new Count(INITIAL_COUNT));
}
@Test
public void aWorldWithNothingStoredStartsWithTheInitialValueAndKeepsOneInstance() {
@@ -96,7 +158,7 @@ public class SavedWorldStateTest {
state.saved(COUNT).value.set(42);
state.saveAll();
SavedWorldState restarted = new SavedWorldState(worldFolder::getRoot, log);
SavedWorldState restarted = stateIn(worldFolder::getRoot);
assertEquals(42, restarted.saved(COUNT).value.get());
}
@@ -126,9 +188,8 @@ public class SavedWorldStateTest {
store(new byte[]{0, 0, 0, CODEC_VERSION, 0, 0});
assertEquals(INITIAL_COUNT, state.saved(COUNT).value.get());
SavedKey<Count> other = new SavedKey<>("test:counts/other", Count.class, COUNT_CODEC, () -> new Count(INITIAL_COUNT));
store("other", new byte[0]);
assertEquals(INITIAL_COUNT, state.saved(other).value.get());
assertEquals(INITIAL_COUNT, state.saved(OTHER).value.get());
}
@Test
@@ -138,55 +199,33 @@ public class SavedWorldStateTest {
state.saveAll();
assertEquals(7, new SavedWorldState(worldFolder::getRoot, log).saved(COUNT).value.get());
assertEquals(7, stateIn(worldFolder::getRoot).saved(COUNT).value.get());
}
@Test
public void aCodecThatReadsNothingIsRejected() throws IOException {
Codec<Count> nothing = new Codec<>() {
@Override
public int version() {
return 1;
}
@Override
public void write(DataOutput output, Count count) {
}
@Override
public Count read(DataInput input, int storedVersion) {
return null;
}
};
SavedKey<Count> key = new SavedKey<>("test:counts/nothing", Count.class, nothing, () -> new Count(INITIAL_COUNT));
store("nothing", new byte[]{0, 0, 0, 1});
assertEquals(INITIAL_COUNT, state.saved(key).value.get());
assertEquals(INITIAL_COUNT, state.saved(NOTHING).value.get());
verify(log).warn(contains("test:counts/nothing"));
}
@Test
public void theSameIdAskedForAnotherTypeIsRefused() {
state.saved(COUNT);
Codec<String> text = new Codec<>() {
@Override
public int version() {
return 1;
}
public void aKeyNobodyRegisteredIsRefused() {
SavedKey<Count> unregistered = countKey("test:counts/unregistered", COUNT_CODEC);
@Override
public void write(DataOutput output, String value) throws IOException {
output.writeUTF(value);
}
IllegalArgumentException refused = assertThrows(IllegalArgumentException.class, () -> state.saved(unregistered));
@Override
public String read(DataInput input, int storedVersion) throws IOException {
return input.readUTF();
}
};
assertEquals("Saved state test:counts/unregistered is not registered by any module of the pack", refused.getMessage());
}
assertThrows(IllegalArgumentException.class,
() -> state.saved(new SavedKey<>("test:counts/waves", String.class, text, () -> "")));
@Test
public void anotherKeyWithARegisteredIdIsRefusedWhateverItsTypeOrCodec() {
SavedKey<String> sameIdOtherType = new SavedKey<>(COUNT.id(), String.class, TEXT_CODEC, () -> "");
SavedKey<Count> sameIdOtherInitial = new SavedKey<>(COUNT.id(), Count.class, COUNT_CODEC, () -> new Count(0));
assertThrows(IllegalArgumentException.class, () -> state.saved(sameIdOtherType));
assertThrows(IllegalArgumentException.class, () -> state.saved(sameIdOtherInitial));
}
@Test
@@ -198,36 +237,20 @@ public class SavedWorldStateTest {
@Test
public void aFailingCodecIsReportedAndDoesNotStopTheOtherValuesFromBeingSaved() {
Codec<Count> failing = new Codec<>() {
@Override
public int version() {
return 1;
}
@Override
public void write(DataOutput output, Count count) throws IOException {
throw new IOException("cannot write");
}
@Override
public Count read(DataInput input, int storedVersion) {
return new Count(0);
}
};
state.saved(new SavedKey<>("test:counts/broken", Count.class, failing, () -> new Count(0)));
state.saved(BROKEN);
state.saved(COUNT).value.set(9);
state.saveAll();
verify(log).error(contains("test:counts/broken"), any(IOException.class));
assertEquals(9, new SavedWorldState(worldFolder::getRoot, log).saved(COUNT).value.get());
assertEquals(9, stateIn(worldFolder::getRoot).saved(COUNT).value.get());
assertFalse(new File(worldFolder.getRoot(), "iris/module-data/test/counts/broken.bin").exists());
}
@Test
public void aWorldWhoseFolderIsGoneIsNotRebuiltBySaving() {
File vanishedFolder = new File(worldFolder.getRoot(), "gone");
SavedWorldState inVanishedWorld = new SavedWorldState(() -> vanishedFolder, log);
SavedWorldState inVanishedWorld = stateIn(() -> vanishedFolder);
inVanishedWorld.saved(COUNT);
inVanishedWorld.saveAll();
@@ -42,8 +42,9 @@ import com.volmit.ModuleSetup;
import com.volmit.block.BlockState;
import com.volmit.geometry.BlockPosition;
import com.volmit.mantle.Mantle;
import com.volmit.data.Codec;
import com.volmit.data.StorageCodec;
import com.volmit.mantle.MantleKey;
import com.volmit.resource.ResourceId;
import com.volmit.mantle.MantleLifetime;
import com.volmit.host.LoadedPack;
import com.volmit.stage.PlacementStage;
@@ -468,8 +469,8 @@ public class ObjectPlacementRegressionTest {
assertEquals(List.of(PackResourceFixture.OAK), found);
}
private static final MantleKey<String> MARKS = new MantleKey<>("test:mark/spot", String.class);
private static final Codec<String> TEXT = new Codec<>() {
private static final MantleKey<String> MARKS = new MantleKey<>(ResourceId.parse("test:mark/spot"), String.class);
private static final StorageCodec<String> TEXT = new StorageCodec<>() {
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(); }
@@ -1,6 +1,6 @@
package art.arcane.iris.engine.spawn;
import com.volmit.data.Codec;
import com.volmit.data.StorageCodec;
import org.junit.Before;
import org.junit.Test;
@@ -134,11 +134,11 @@ public class SpawnCooldownBookTest {
@Test
public void theBookTheWorldKeepsIsSavedWithTheSavedKey() {
assertTrue(SpawnCooldownBook.SAVED.id().startsWith("iris:spawn/"));
assertTrue(SpawnCooldownBook.SAVED.id().toString().startsWith("iris:spawn/"));
assertTrue(SpawnCooldownBook.SAVED.initial().get().canSpawn("wolves", TEN_MINUTES));
}
private Codec<SpawnCooldownBook> codec() {
private StorageCodec<SpawnCooldownBook> codec() {
return SpawnCooldownBook.codec(now::get);
}