mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
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:
1 parent
96532f9a22
commit
db4972b42a
13 files changed
+229
-124
No files matched your search
@@ -51,7 +51,7 @@ public final class GenerationLayers {
|
||||
public static GenerationLayers of(Collection<MantleRegistration<?>> registrations) {
|
||||
Set<String> 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);
|
||||
}
|
||||
|
||||
@@ -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<String, MantleRegistration<?>> registrations;
|
||||
private final Map<ResourceId, MantleRegistration<?>> 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 <T> Codec<T> codecOf(MantleKey<T> key) {
|
||||
private <T> StorageCodec<T> codecOf(MantleKey<T> 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<T>) registration.codec();
|
||||
return (StorageCodec<T>) registration.codec();
|
||||
}
|
||||
|
||||
private record Stored<T>(int x, int y, int z, T value) {}
|
||||
|
||||
private final class Layer<T> implements MantleLayer<T> {
|
||||
private final MantleKey<T> key;
|
||||
private final Codec<T> codec;
|
||||
private final StorageCodec<T> codec;
|
||||
|
||||
Layer(MantleKey<T> key, Codec<T> codec) {
|
||||
Layer(MantleKey<T> key, StorageCodec<T> 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<T>(MantleKey<T> key, Codec<T> codec, T value) implements Change {
|
||||
private record Put<T>(MantleKey<T> key, StorageCodec<T> 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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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> T get(MantleKey<T> key, Codec<T> codec) {
|
||||
LayerValue value = layers.get(key.id());
|
||||
public <T> T get(MantleKey<T> key, StorageCodec<T> 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 <T> ModuleLayerValues with(MantleKey<T> key, Codec<T> codec, T value) {
|
||||
public <T> ModuleLayerValues with(MantleKey<T> key, StorageCodec<T> codec, T value) {
|
||||
TreeMap<String, LayerValue> 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>(T value, Codec<T> codec) implements LayerValue {
|
||||
private record Decoded<T>(T value, StorageCodec<T> 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) {
|
||||
|
||||
@@ -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<File> worldFolder;
|
||||
private final Supplier<? extends Collection<SavedKey<?>>> registeredKeys;
|
||||
private final ModuleLog log;
|
||||
private final Map<String, Saved<?>> values = new ConcurrentHashMap<>();
|
||||
private final Map<ResourceId, Saved<?>> values = new ConcurrentHashMap<>();
|
||||
private final Object loading = new Object();
|
||||
private volatile Map<ResourceId, SavedKey<?>> registered;
|
||||
|
||||
SavedWorldState(Supplier<File> worldFolder, ModuleLog log) {
|
||||
/** @param registeredKeys the keys the pack's modules registered, asked for when the first value is */
|
||||
SavedWorldState(Supplier<File> worldFolder, Supplier<? extends Collection<SavedKey<?>>> registeredKeys, ModuleLog log) {
|
||||
this.worldFolder = worldFolder;
|
||||
this.registeredKeys = registeredKeys;
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
<T> T saved(SavedKey<T> 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<ResourceId, SavedKey<?>> registeredById() {
|
||||
Map<ResourceId, SavedKey<?>> 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<Saved<?>> 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 {
|
||||
|
||||
@@ -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();
|
||||
|
||||
+4
-3
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user