Register saved keys per module, key mantle and saved ids by ResourceId

SavedKey moves to com.volmit.data and Codec becomes StorageCodec, which
ends the clash with JsonForms.Codec. SavedKey.id and MantleKey.id are
ResourceIds, so the copied id regex is gone. Modules register saved keys
in setup with FeatureRegistry.registerSaved, owned like mantle keys, and
RunningWorld.saved rejects a key that is not the registered one.
This commit is contained in:
piexel committed 2026-11-06 12:50:42 +01:00
1 parent 6c8fa31a7b
commit 307b89c5fe
19 files changed
+156 -88

No files matched your search

@@ -1,6 +1,7 @@
package com.volmit;
import com.volmit.data.Codec;
import com.volmit.data.SavedKey;
import com.volmit.data.StorageCodec;
import com.volmit.mantle.MantleKey;
import com.volmit.mantle.MantleLifetime;
import com.volmit.resource.ResourceKind;
@@ -11,7 +12,7 @@ import java.util.Set;
/** Host implementation collects non-stage registrations before loading pack configuration.
* Feature IDs must be unique. JSON keys must be unique per {@link com.volmit.config.Level}, including the
* settings types stages name in {@code when(...)}. Feature IDs, mantle keys and resource kind ids a module
* settings types stages name in {@code when(...)}. Feature IDs, mantle keys, saved keys and resource kind ids a module
* registers are its module id or start with it and {@code /}, such as {@code iris:snow} or {@code iris:snow/depth}.
*/
public interface FeatureRegistry {
@@ -21,7 +22,13 @@ public interface FeatureRegistry {
* preserve unknown persisted layers or fail explicitly, never silently discard them.
* GENERATION layers also require codecs.
*/
<T> void registerMantle(MantleKey<T> key, MantleLifetime lifetime, Codec<T> codec);
<T> void registerMantle(MantleKey<T> key, MantleLifetime lifetime, StorageCodec<T> codec);
/** Registers a value the module keeps with each running world across restarts, so
* {@link com.volmit.world.RunningWorld#saved} serves it; that method throws IllegalArgumentException for a key
* nobody registered. Null arguments throw NullPointerException; a duplicate id throws IllegalArgumentException.
*/
void registerSaved(SavedKey<?> key);
/** Catalogs a settings type that no stage names in {@code when(...)}, such as one read only
* through {@link com.volmit.config.FeatureSettings#at}, so the host decodes it and studio
@@ -1,32 +1,30 @@
package com.volmit.world;
package com.volmit.data;
import com.volmit.data.Codec;
import com.volmit.resource.ResourceId;
import java.util.Objects;
import java.util.function.Supplier;
/** The identity and form of one value a module keeps with a running world across restarts
* ({@link RunningWorld#saved}). The id is namespaced like a mantle key and should start with the owning module's id; the host
* names its storage after it, so each segment of the namespace and of the {@code /} separated path must be a name,
* never empty, {@code .} or {@code ..}.
* ({@link com.volmit.world.RunningWorld#saved}). The module registers it in its setup
* ({@link com.volmit.FeatureRegistry#registerSaved}), and the id is the module's id or starts with it and {@code /},
* as a mantle key's does. The host names its storage after the id, so each segment of the namespace and of the
* {@code /} separated path must be a name, never empty, {@code .} or {@code ..}.
*
* @param id the stable storage identity, such as {@code example:moss/patches}
* @param type the value class
* @param codec how the value is stored; its version is written with the value
* @param initial the value for a world that has none stored, or whose stored one the codec rejects
*/
public record SavedKey<T>(String id, Class<T> type, Codec<T> codec, Supplier<T> initial) {
public record SavedKey<T>(ResourceId id, Class<T> type, StorageCodec<T> codec, Supplier<T> initial) {
public SavedKey {
Objects.requireNonNull(id, "id");
Objects.requireNonNull(type, "type");
Objects.requireNonNull(codec, "codec");
Objects.requireNonNull(initial, "initial");
if (!id.matches("[a-z0-9_.-]+:[a-z0-9_./-]+")) {
throw new IllegalArgumentException("Expected a namespaced saved ID: " + id);
}
if (type.isPrimitive()) {
throw new IllegalArgumentException("Saved values require a reference type");
}
for (String segment : id.replace(':', '/').split("/", -1)) {
for (String segment : id.toString().replace(':', '/').split("/", -1)) {
if (segment.isEmpty() || segment.equals(".") || segment.equals("..")) {
throw new IllegalArgumentException("A saved ID names files, so it has no empty, . or .. segment: " + id);
}
@@ -5,11 +5,11 @@ import java.io.DataOutput;
import java.io.IOException;
/** Value serialization for data the host stores for a module: mantle layers ({@link com.volmit.mantle.MantleKey}) and
* saved world state ({@link com.volmit.world.SavedKey}). The host frames payloads and stores the key ID and version
* saved world state ({@link SavedKey}). The host frames payloads and stores the key ID and version
* separately. Implementations must support concurrent calls, must not close streams, and must reject malformed data or
* unsupported stored versions with IOException. Values cannot be null.
*/
public interface Codec<T> {
public interface StorageCodec<T> {
/** Nonnegative format version, stable for the registration lifetime. */
int version();
@@ -1,15 +1,13 @@
package com.volmit.mantle;
import com.volmit.resource.ResourceId;
import java.util.Objects;
/** Stable storage identity. Different IDs may use the same value class without sharing data. */
public record MantleKey<T>(String id, Class<T> type) {
public record MantleKey<T>(ResourceId id, Class<T> type) {
public MantleKey {
Objects.requireNonNull(id, "id");
Objects.requireNonNull(type, "type");
if (!id.matches("[a-z0-9_.-]+:[a-z0-9_./-]+")) {
throw new IllegalArgumentException("Expected a namespaced mantle ID: " + id);
}
if (type.isPrimitive()) {
throw new IllegalArgumentException("Mantle values require a reference type");
}
@@ -1,6 +1,7 @@
package com.volmit.world;
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;
@@ -50,12 +51,14 @@ public interface RunningWorld {
* {@link PackWorld#state}.
*
* <p>The module changes the value in place and owns its thread safety. The host writes it while module threads may
* still change it, so {@link com.volmit.data.Codec#write} must read the value safely. A stored payload the codec
* still change it, so {@link com.volmit.data.StorageCodec#write} must read the value safely. A stored payload the codec
* rejects is logged and replaced by {@code key.initial()}; the next save overwrites it. A pack reload or hotload
* stops the world and starts another over the same storage, so the new world reads what the old one wrote when it
* stopped.
*
* @throws IllegalArgumentException if another key with the same id but another type was used for this world
* @throws IllegalArgumentException if {@code key} is not the key a module registered with
* {@link com.volmit.FeatureRegistry#registerSaved}: an unregistered key, or another key with the id of a
* registered one, such as one that another module built in its namespace
*/
<T> T saved(SavedKey<T> key);
@@ -1,6 +1,6 @@
package com.volmit.world;
package com.volmit.data;
import com.volmit.data.Codec;
import com.volmit.resource.ResourceId;
import org.junit.jupiter.api.Test;
import java.io.DataInput;
@@ -11,7 +11,9 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
class SavedKeyTest {
private static final Codec<String> TEXT = new Codec<>() {
private static final ResourceId ID = ResourceId.parse("a:b");
private static final StorageCodec<String> TEXT = new StorageCodec<>() {
@Override
public int version() {
return 1;
@@ -51,10 +53,10 @@ class SavedKeyTest {
@Test
void rejectsMissingPartsAndPrimitiveTypes() {
assertThrows(NullPointerException.class, () -> new SavedKey<>(null, String.class, TEXT, () -> ""));
assertThrows(NullPointerException.class, () -> new SavedKey<>("a:b", null, TEXT, () -> ""));
assertThrows(NullPointerException.class, () -> new SavedKey<>("a:b", String.class, null, () -> ""));
assertThrows(NullPointerException.class, () -> new SavedKey<>("a:b", String.class, TEXT, null));
Codec<Integer> number = new Codec<>() {
assertThrows(NullPointerException.class, () -> new SavedKey<>(ID, null, TEXT, () -> ""));
assertThrows(NullPointerException.class, () -> new SavedKey<>(ID, String.class, null, () -> ""));
assertThrows(NullPointerException.class, () -> new SavedKey<>(ID, String.class, TEXT, null));
StorageCodec<Integer> number = new StorageCodec<>() {
@Override
public int version() {
return 1;
@@ -70,10 +72,10 @@ class SavedKeyTest {
return input.readInt();
}
};
assertThrows(IllegalArgumentException.class, () -> new SavedKey<>("a:b", int.class, number, () -> 0));
assertThrows(IllegalArgumentException.class, () -> new SavedKey<>(ID, int.class, number, () -> 0));
}
private static SavedKey<String> key(String id) {
return new SavedKey<>(id, String.class, TEXT, () -> "");
return new SavedKey<>(ResourceId.parse(id), String.class, TEXT, () -> "");
}
}
@@ -1,6 +1,7 @@
package com.volmit.mantle;
import com.volmit.geometry.BlockBox;
import com.volmit.resource.ResourceId;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@@ -33,20 +34,18 @@ class MantleValueTest {
@Test
void keysSeparateSameTypedLayersAndHaveValueEquality() {
var owner = new MantleKey<>("example:object-owner", String.class);
assertEquals(owner, new MantleKey<>("example:object-owner", String.class));
assertNotEquals(owner, new MantleKey<>("example:quest/location", String.class));
assertNotEquals(owner, new MantleKey<>("example:object-owner", Integer.class));
var owner = new MantleKey<>(ResourceId.parse("example:object-owner"), String.class);
assertEquals(owner, new MantleKey<>(ResourceId.parse("example:object-owner"), String.class));
assertNotEquals(owner, new MantleKey<>(ResourceId.parse("example:quest/location"), String.class));
assertNotEquals(owner, new MantleKey<>(ResourceId.parse("example:object-owner"), Integer.class));
}
@Test
void rejectsInvalidKeys() {
for (String id : new String[]{"", "owner", ":owner", "example:", "Example:owner", "a:b:c", "a: b"}) {
assertThrows(IllegalArgumentException.class, () -> new MantleKey<>(id, String.class));
}
void rejectsMissingPartsAndPrimitiveTypes() {
var id = ResourceId.parse("a:b");
assertThrows(NullPointerException.class, () -> new MantleKey<>(null, String.class));
assertThrows(NullPointerException.class, () -> new MantleKey<>("a:b", null));
assertThrows(IllegalArgumentException.class, () -> new MantleKey<>("a:b", int.class));
assertThrows(NullPointerException.class, () -> new MantleKey<>(id, null));
assertThrows(IllegalArgumentException.class, () -> new MantleKey<>(id, int.class));
}
@Test