From db4972b42a923988701684ba1e2db15dc18b0d6f Mon Sep 17 00:00:00 2001 From: piexel Date: Fri, 6 Nov 2026 12:54:22 +0100 Subject: [PATCH] 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. --- .../iris/core/modules/GenerationLayers.java | 2 +- .../iris/core/modules/IrisModuleMantle.java | 26 +-- .../iris/core/modules/IrisRunningWorld.java | 5 +- .../iris/core/modules/ModuleLayerValues.java | 18 +-- .../iris/core/modules/SavedWorldState.java | 48 ++++-- .../core/modules/DimensionBottomTest.java | 7 +- .../iris/core/modules/InMemoryMantle.java | 4 +- .../core/modules/IrisModuleMantleTest.java | 20 +-- .../core/modules/IrisRunningWorldTest.java | 52 ++++++ .../core/modules/PlacementMantleTest.java | 7 +- .../core/modules/SavedWorldStateTest.java | 151 ++++++++++-------- .../ObjectPlacementRegressionTest.java | 7 +- .../engine/spawn/SpawnCooldownBookTest.java | 6 +- 13 files changed, 229 insertions(+), 124 deletions(-) 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 index b1788b4f4..07c0f0833 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/GenerationLayers.java +++ b/core/src/main/java/art/arcane/iris/core/modules/GenerationLayers.java @@ -51,7 +51,7 @@ public final class GenerationLayers { public static GenerationLayers of(Collection> registrations) { Set generation = registrations.stream() .filter(registration -> registration.lifetime() == MantleLifetime.GENERATION) - .map(registration -> registration.key().id()) + .map(registration -> registration.key().id().toString()) .collect(Collectors.toUnmodifiableSet()); return generation.isEmpty() ? NONE : new GenerationLayers(generation); } 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 8ab12706c..706aba137 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 @@ -30,7 +30,7 @@ import com.volmit.host.MantleRegistration; import com.volmit.geometry.BlockBox; import com.volmit.mantle.CarvedVoid; import com.volmit.mantle.Mantle; -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; @@ -74,7 +74,7 @@ final class IrisModuleMantle implements Mantle { private final BlockBox bounds; private final Chunks chunks; - private final Map> registrations; + private final Map> registrations; private final PlannedBlocks plannedBlocks; private final PackResourceIds ids; private CarvedVoids carved; @@ -148,7 +148,7 @@ final class IrisModuleMantle implements Mantle { } @SuppressWarnings("unchecked") // The registration's type was compared with the key's. - private Codec codecOf(MantleKey key) { + private StorageCodec codecOf(MantleKey key) { Objects.requireNonNull(key, "key"); MantleRegistration registration = registrations.get(key.id()); if (registration == null) { @@ -158,16 +158,16 @@ final class IrisModuleMantle implements Mantle { throw new IllegalArgumentException("Mantle layer " + key.id() + " is registered as " + registration.key().type().getName() + ", not " + key.type().getName()); } - return (Codec) registration.codec(); + return (StorageCodec) registration.codec(); } private record Stored(int x, int y, int z, T value) {} private final class Layer implements MantleLayer { private final MantleKey key; - private final Codec codec; + private final StorageCodec codec; - Layer(MantleKey key, Codec codec) { + Layer(MantleKey key, StorageCodec codec) { this.key = key; this.codec = codec; } @@ -188,7 +188,7 @@ final class IrisModuleMantle implements Mantle { @Override public void remove(int x, int y, int z) { requireNoOpenEdit(); - change(x, y, z, values -> values.without(key.id())); + change(x, y, z, values -> values.without(key.id().toString())); } @Override @@ -227,7 +227,7 @@ final class IrisModuleMantle implements Mantle { chunk.iterate(ModuleLayerValues.class, (localX, y, localZ, values) -> { int x = originX + localX; int z = originZ + localZ; - if (values.has(key.id()) && area.contains(x, y, z)) { + if (values.has(key.id().toString()) && area.contains(x, y, z)) { stored.add(new Stored<>(x, y, z, values.get(key, codec))); } }); @@ -242,7 +242,7 @@ final class IrisModuleMantle implements Mantle { ModuleLayerValues applyTo(ModuleLayerValues values); } - private record Put(MantleKey key, Codec codec, T value) implements Change { + private record Put(MantleKey key, StorageCodec codec, T value) implements Change { @Override public ModuleLayerValues applyTo(ModuleLayerValues values) { return values.with(key, codec, value); @@ -373,7 +373,7 @@ final class IrisModuleMantle implements Mantle { @Override public T get(int x, int y, int z) { requireOpen(); - Change change = changeAt(x, y, z, stored.key.id()); + Change change = changeAt(x, y, z, stored.key.id().toString()); if (change == null) { return stored.get(x, y, z); } @@ -384,13 +384,13 @@ final class IrisModuleMantle implements Mantle { 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)); + buffer(x, y, z, stored.key.id().toString(), 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())); + buffer(x, y, z, stored.key.id().toString(), new Removal(stored.key.id().toString())); } @Override @@ -414,7 +414,7 @@ final class IrisModuleMantle implements Mantle { visible.put(new Position(value.x(), value.y(), value.z()), value.value()); } changes.forEach((position, blockChanges) -> { - Change change = blockChanges.get(stored.key.id()); + Change change = blockChanges.get(stored.key.id().toString()); if (change == null || !area.contains(position.x(), position.y(), position.z())) { return; } diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisRunningWorld.java b/core/src/main/java/art/arcane/iris/core/modules/IrisRunningWorld.java index 7c19723c6..23a06abee 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/IrisRunningWorld.java +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisRunningWorld.java @@ -27,11 +27,11 @@ import art.arcane.volmlib.util.matter.Matter; import art.arcane.volmlib.util.matter.slices.MarkerMatter; import com.volmit.ModuleLog; import com.volmit.config.FeatureSettings; +import com.volmit.data.SavedKey; import com.volmit.geometry.BlockPosition; import com.volmit.placement.PlacementRecord; import com.volmit.resource.ResourceId; import com.volmit.world.RunningWorld; -import com.volmit.world.SavedKey; import com.volmit.world.TerrainSampler; import java.util.List; @@ -55,7 +55,8 @@ final class IrisRunningWorld implements RunningWorld { IrisRunningWorld(Engine engine, ModuleLog log) { this.engine = engine; - this.savedState = new SavedWorldState(() -> engine.getWorld().worldFolder(), log); + this.savedState = new SavedWorldState(() -> engine.getWorld().worldFolder(), + () -> engine.getData().getModules().pack().catalog().saved(), log); } @Override 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 14a399de1..fb0803e42 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 @@ -18,7 +18,7 @@ package art.arcane.iris.core.modules; -import com.volmit.data.Codec; +import com.volmit.data.StorageCodec; import com.volmit.mantle.MantleKey; import java.io.ByteArrayInputStream; @@ -49,8 +49,8 @@ public final class ModuleLayerValues { } /** The value of {@code key}, decoded with {@code codec} if it was read from disk; null when the layer holds none. */ - public T get(MantleKey key, Codec codec) { - LayerValue value = layers.get(key.id()); + public T get(MantleKey key, StorageCodec codec) { + LayerValue value = layers.get(key.id().toString()); return value == null ? null : key.type().cast(value.decode(key, codec)); } @@ -63,9 +63,9 @@ public final class ModuleLayerValues { } /** These values with {@code key} holding {@code value}. */ - public ModuleLayerValues with(MantleKey key, Codec codec, T value) { + public ModuleLayerValues with(MantleKey key, StorageCodec codec, T value) { TreeMap changed = new TreeMap<>(layers); - changed.put(key.id(), new Decoded<>(value, codec)); + changed.put(key.id().toString(), new Decoded<>(value, codec)); return new ModuleLayerValues(changed); } @@ -118,14 +118,14 @@ public final class ModuleLayerValues { } private sealed interface LayerValue permits Decoded, Encoded { - Object decode(MantleKey key, Codec codec); + Object decode(MantleKey key, StorageCodec codec); Encoded encoded() throws IOException; } - private record Decoded(T value, Codec codec) implements LayerValue { + private record Decoded(T value, StorageCodec codec) implements LayerValue { @Override - public Object decode(MantleKey key, Codec requested) { + public Object decode(MantleKey key, StorageCodec requested) { return value; } @@ -152,7 +152,7 @@ public final class ModuleLayerValues { } @Override - public Object decode(MantleKey key, Codec codec) { + public Object decode(MantleKey key, StorageCodec codec) { try { return codec.read(new DataInputStream(new ByteArrayInputStream(payload)), version); } catch (IOException failure) { diff --git a/core/src/main/java/art/arcane/iris/core/modules/SavedWorldState.java b/core/src/main/java/art/arcane/iris/core/modules/SavedWorldState.java index 707666b5a..7372008ed 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/SavedWorldState.java +++ b/core/src/main/java/art/arcane/iris/core/modules/SavedWorldState.java @@ -20,7 +20,8 @@ package art.arcane.iris.core.modules; import art.arcane.iris.core.lifecycle.VanishedWorldStorage; import com.volmit.ModuleLog; -import com.volmit.world.SavedKey; +import com.volmit.data.SavedKey; +import com.volmit.resource.ResourceId; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -33,10 +34,13 @@ import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; import java.util.function.Supplier; +import java.util.stream.Collectors; /** * What modules keep with one running world ({@link com.volmit.world.RunningWorld#saved}): the world's one instance per @@ -49,27 +53,51 @@ final class SavedWorldState { private static final String TEMPORARY_EXTENSION = ".tmp"; private final Supplier worldFolder; + private final Supplier>> registeredKeys; private final ModuleLog log; - private final Map> values = new ConcurrentHashMap<>(); + private final Map> values = new ConcurrentHashMap<>(); private final Object loading = new Object(); + private volatile Map> registered; - SavedWorldState(Supplier worldFolder, ModuleLog log) { + /** @param registeredKeys the keys the pack's modules registered, asked for when the first value is */ + SavedWorldState(Supplier worldFolder, Supplier>> registeredKeys, ModuleLog log) { this.worldFolder = worldFolder; + this.registeredKeys = registeredKeys; this.log = log; } T saved(SavedKey key) { + requireRegistered(key); Saved known = values.get(key.id()); if (known == null) { known = loadOnce(key); } - if (known.key().type() != key.type()) { - throw new IllegalArgumentException("Saved state " + key.id() + " is kept as " + known.key().type().getName() - + ", not " + key.type().getName()); - } return key.type().cast(known.value()); } + private void requireRegistered(SavedKey key) { + SavedKey registeredKey = registeredById().get(key.id()); + if (registeredKey == null) { + throw new IllegalArgumentException("Saved state " + key.id() + " is not registered by any module of the pack"); + } + if (!registeredKey.equals(key)) { + throw new IllegalArgumentException("Saved state " + key.id() + " is registered with another key; use the key the module registered"); + } + } + + private Map> registeredById() { + Map> known = registered; + if (known != null) { + return known; + } + synchronized (loading) { + if (registered == null) { + registered = registeredKeys.get().stream().collect(Collectors.toUnmodifiableMap(SavedKey::id, Function.identity())); + } + return registered; + } + } + /** Writes every value asked for so far; a value that cannot be written is reported and does not stop the others. */ void saveAll() { List> asked = List.copyOf(values.values()); @@ -126,10 +154,8 @@ final class SavedWorldState { } private Path fileOf(SavedKey key) { - String id = key.id(); - String namespace = id.substring(0, id.indexOf(':')); - String path = id.substring(id.indexOf(':') + 1); - return worldFolder.get().toPath().resolve(STORAGE_DIRECTORY).resolve(namespace).resolve(path + FILE_EXTENSION); + ResourceId id = key.id(); + return worldFolder.get().toPath().resolve(STORAGE_DIRECTORY).resolve(id.namespace()).resolve(id.path() + FILE_EXTENSION); } private static void writeAtomically(Path file, byte[] content) throws IOException { diff --git a/core/src/test/java/art/arcane/iris/core/modules/DimensionBottomTest.java b/core/src/test/java/art/arcane/iris/core/modules/DimensionBottomTest.java index e4433796a..13e4816ae 100644 --- a/core/src/test/java/art/arcane/iris/core/modules/DimensionBottomTest.java +++ b/core/src/test/java/art/arcane/iris/core/modules/DimensionBottomTest.java @@ -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 MARKS = new MantleKey<>("test:bottom/mark", String.class); - private static final Codec TEXT = new Codec<>() { + private static final MantleKey MARKS = new MantleKey<>(ResourceId.parse("test:bottom/mark"), String.class); + private static final StorageCodec 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(); } diff --git a/core/src/test/java/art/arcane/iris/core/modules/InMemoryMantle.java b/core/src/test/java/art/arcane/iris/core/modules/InMemoryMantle.java index 7c20900cd..d36a88004 100644 --- a/core/src/test/java/art/arcane/iris/core/modules/InMemoryMantle.java +++ b/core/src/test/java/art/arcane/iris/core/modules/InMemoryMantle.java @@ -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 value(MantleKey key, Codec codec, int x, int mantleY, int z) { + public T value(MantleKey key, StorageCodec codec, int x, int mantleY, int z) { MantleChunk chunk = chunk(x >> 4, z >> 4); if (!chunk.exists(mantleY >> 4) || !chunk.get(mantleY >> 4).hasSlice(ModuleLayerValues.class)) { return null; 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 ff0ee24aa..212d103ef 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 @@ -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 MARKS = new MantleKey<>("iris:marks/mark", String.class); - private static final MantleKey DEPTHS = new MantleKey<>("iris:marks/depth", Integer.class); - private static final Codec TEXT = new Codec<>() { + private static final MantleKey MARKS = new MantleKey<>(ResourceId.parse("iris:marks/mark"), String.class); + private static final MantleKey DEPTHS = new MantleKey<>(ResourceId.parse("iris:marks/depth"), Integer.class); + private static final StorageCodec 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 NUMBER = new Codec<>() { + private static final StorageCodec 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 unknown = new MantleKey<>("iris:gone/layer", String.class); + MantleKey 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 planned = new MantleKey<>("iris:veins/planned", String.class); + MantleKey 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) { diff --git a/core/src/test/java/art/arcane/iris/core/modules/IrisRunningWorldTest.java b/core/src/test/java/art/arcane/iris/core/modules/IrisRunningWorldTest.java index 92cdcd4e2..8aad6a45d 100644 --- a/core/src/test/java/art/arcane/iris/core/modules/IrisRunningWorldTest.java +++ b/core/src/test/java/art/arcane/iris/core/modules/IrisRunningWorldTest.java @@ -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 registered = new SavedKey<>(ResourceId.parse("test:saves/note"), String.class, NOTE_CODEC, () -> "empty"); + SavedKey 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 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); diff --git a/core/src/test/java/art/arcane/iris/core/modules/PlacementMantleTest.java b/core/src/test/java/art/arcane/iris/core/modules/PlacementMantleTest.java index d534e2739..24309c4f1 100644 --- a/core/src/test/java/art/arcane/iris/core/modules/PlacementMantleTest.java +++ b/core/src/test/java/art/arcane/iris/core/modules/PlacementMantleTest.java @@ -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 MARKS = new MantleKey<>("iris:marks/mark", String.class); - private static final Codec TEXT = new Codec<>() { + private static final MantleKey MARKS = new MantleKey<>(ResourceId.parse("iris:marks/mark"), String.class); + private static final StorageCodec 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(); } diff --git a/core/src/test/java/art/arcane/iris/core/modules/SavedWorldStateTest.java b/core/src/test/java/art/arcane/iris/core/modules/SavedWorldStateTest.java index 07ac661cf..6489ae0d2 100644 --- a/core/src/test/java/art/arcane/iris/core/modules/SavedWorldStateTest.java +++ b/core/src/test/java/art/arcane/iris/core/modules/SavedWorldStateTest.java @@ -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 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_CODEC = new Codec<>() { + private static final StorageCodec COUNT_CODEC = new StorageCodec<>() { @Override public int version() { return CODEC_VERSION; @@ -80,8 +86,64 @@ public class SavedWorldStateTest { } }; - private static final SavedKey COUNT = new SavedKey<>("test:counts/waves", Count.class, COUNT_CODEC, - () -> new Count(INITIAL_COUNT)); + private static final StorageCodec 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 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 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 = countKey("test:counts/waves", COUNT_CODEC); + private static final SavedKey OTHER = countKey("test:counts/other", COUNT_CODEC); + private static final SavedKey NOTHING = countKey("test:counts/nothing", NOTHING_CODEC); + private static final SavedKey BROKEN = countKey("test:counts/broken", FAILING_CODEC); + + private static SavedKey countKey(String id, StorageCodec 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 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 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 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 text = new Codec<>() { - @Override - public int version() { - return 1; - } + public void aKeyNobodyRegisteredIsRefused() { + SavedKey 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 sameIdOtherType = new SavedKey<>(COUNT.id(), String.class, TEXT_CODEC, () -> ""); + SavedKey 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 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(); diff --git a/core/src/test/java/art/arcane/iris/engine/object/placement/ObjectPlacementRegressionTest.java b/core/src/test/java/art/arcane/iris/engine/object/placement/ObjectPlacementRegressionTest.java index 926db9395..239ea95b4 100644 --- a/core/src/test/java/art/arcane/iris/engine/object/placement/ObjectPlacementRegressionTest.java +++ b/core/src/test/java/art/arcane/iris/engine/object/placement/ObjectPlacementRegressionTest.java @@ -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 MARKS = new MantleKey<>("test:mark/spot", String.class); - private static final Codec TEXT = new Codec<>() { + private static final MantleKey MARKS = new MantleKey<>(ResourceId.parse("test:mark/spot"), String.class); + private static final StorageCodec 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(); } diff --git a/core/src/test/java/art/arcane/iris/engine/spawn/SpawnCooldownBookTest.java b/core/src/test/java/art/arcane/iris/engine/spawn/SpawnCooldownBookTest.java index 0959dd79f..87318b281 100644 --- a/core/src/test/java/art/arcane/iris/engine/spawn/SpawnCooldownBookTest.java +++ b/core/src/test/java/art/arcane/iris/engine/spawn/SpawnCooldownBookTest.java @@ -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 codec() { + private StorageCodec codec() { return SpawnCooldownBook.codec(now::get); }