mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-30 01:34:04 +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 {
|
||||
|
||||
Reference in new issue
Block a user