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,5 +1,7 @@
package com.volmit.host;
import com.volmit.data.SavedKey;
import com.volmit.resource.ResourceId;
import com.volmit.service.Providers;
import com.volmit.service.Service;
import java.util.Collection;
@@ -31,7 +33,8 @@ public final class FeatureCatalog {
};
private final Map<Class<?>, FeatureDescriptor<?>> settings;
private final Map<String, MantleRegistration<?>> mantle;
private final Map<ResourceId, MantleRegistration<?>> mantle;
private final Map<ResourceId, SavedKey<?>> saved;
private final List<ResourceKindRegistration> resourceKinds;
private final List<ValidatorRegistration> validators;
private final List<LifecycleRegistration> lifecycles;
@@ -43,7 +46,8 @@ public final class FeatureCatalog {
FeatureCatalog(
Map<Class<?>, FeatureDescriptor<?>> settings,
Map<String, MantleRegistration<?>> mantle,
Map<ResourceId, MantleRegistration<?>> mantle,
Map<ResourceId, SavedKey<?>> saved,
Collection<ResourceKindRegistration> resourceKinds,
List<ValidatorRegistration> validators,
List<LifecycleRegistration> lifecycles,
@@ -56,6 +60,7 @@ public final class FeatureCatalog {
this.commands = sorted(commands, registration -> registration.command().name());
this.settings = Map.copyOf(settings);
this.mantle = Map.copyOf(mantle);
this.saved = Map.copyOf(saved);
this.resourceKinds = sorted(resourceKinds, registration -> registration.kind().id());
this.validators = inModuleOrder(validators, ValidatorRegistration::module);
this.lifecycles = inModuleOrder(lifecycles, LifecycleRegistration::module);
@@ -72,6 +77,11 @@ public final class FeatureCatalog {
return sorted(mantle.values(), registration -> registration.key().id());
}
/** The values modules keep with each running world ({@code FeatureRegistry.registerSaved}), by id. */
public List<SavedKey<?>> saved() {
return sorted(saved.values(), SavedKey::id);
}
/** The type packs write their object placements as, if a module declared it ({@code FeatureRegistry.registerPlacementType}). */
public Optional<PlacementTypeRegistration> placementType() {
return placementType;
@@ -1,6 +1,6 @@
package com.volmit.host;
import com.volmit.data.Codec;
import com.volmit.data.StorageCodec;
import com.volmit.mantle.MantleKey;
import com.volmit.mantle.MantleLifetime;
import java.util.Objects;
@@ -8,7 +8,7 @@ import java.util.Objects;
public record MantleRegistration<T>(
MantleKey<T> key,
MantleLifetime lifetime,
Codec<T> codec,
StorageCodec<T> codec,
int version) {
public MantleRegistration {
Objects.requireNonNull(key, "key");
@@ -4,7 +4,8 @@ import com.volmit.FeatureRegistry;
import com.volmit.IrisFeature;
import com.volmit.ModuleLifecycle;
import com.volmit.config.Level;
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.ResourceId;
@@ -27,7 +28,7 @@ import java.util.Set;
* immutable. IDs are unique across feature kinds. A feature's JSON key (see {@link com.volmit.config.Feature#key})
* is unique per attachment target, and may not equal a host JSON field passed in to reserve it; a clash fails the
* registration naming the key, the target and both claimants. Modules register during their setup
* ({@link ModuleSetups#run}); mantle keys, resource kind ids and explicitly registered
* ({@link ModuleSetups#run}); mantle keys, saved keys, resource kind ids and explicitly registered
* settings ids must be the registering module's id or start with it and '/' ({@link com.volmit.ModuleInfo#ownsId}).
* Resource kinds have unique ids, types and folders, and no kind may read a folder the host reserves for its own
* files. Lifecycle hooks and service providers are registered by the module that owns them; a service whose providers
@@ -38,7 +39,8 @@ import java.util.Set;
*/
public final class ModuleRegistry implements FeatureRegistry, ToolingRegistry {
private final Map<Class<?>, FeatureDescriptor<?>> settings = new HashMap<>();
private final Map<String, MantleRegistration<?>> mantle = new HashMap<>();
private final Map<ResourceId, MantleRegistration<?>> mantle = new HashMap<>();
private final Map<ResourceId, SavedKey<?>> saved = new HashMap<>();
private final Set<ResourceId> featureIds = new HashSet<>();
private final Set<Class<?>> featureTypes = new HashSet<>();
private final Set<Class<?>> registeredByOwner = new HashSet<>();
@@ -84,11 +86,11 @@ public final class ModuleRegistry implements FeatureRegistry, ToolingRegistry {
}
@Override
public <T> void registerMantle(MantleKey<T> key, MantleLifetime lifetime, Codec<T> codec) {
public <T> void registerMantle(MantleKey<T> key, MantleLifetime lifetime, StorageCodec<T> codec) {
checkOpen();
Objects.requireNonNull(key, "key");
Objects.requireNonNull(codec, "codec");
requireOwnedId("Mantle layer", ResourceId.parse(key.id()));
requireOwnedId("Mantle layer", key.id());
var registration = new MantleRegistration<>(key, lifetime, codec, codec.version());
if (mantle.putIfAbsent(key.id(), registration) != null) {
@@ -96,6 +98,19 @@ public final class ModuleRegistry implements FeatureRegistry, ToolingRegistry {
}
}
@Override
public void registerSaved(SavedKey<?> key) {
checkOpen();
Objects.requireNonNull(key, "key");
requireOwnedId("Saved value", key.id());
if (key.codec().version() < 0) {
throw new IllegalArgumentException("Negative saved codec version");
}
if (saved.putIfAbsent(key.id(), key) != null) {
throw new IllegalArgumentException("Duplicate saved value: " + key.id());
}
}
@Override
public void registerSettings(Class<? extends IrisFeature> settingsType) {
checkOpen();
@@ -237,7 +252,7 @@ public final class ModuleRegistry implements FeatureRegistry, ToolingRegistry {
public FeatureCatalog freeze() {
if (catalog == null) {
catalog = new FeatureCatalog(settings, mantle, resourceKinds.values(), validators, lifecycles, services,
catalog = new FeatureCatalog(settings, mantle, saved, resourceKinds.values(), validators, lifecycles, services,
placementType, studioViews.values(), commands);
}
return catalog;
@@ -1,5 +1,6 @@
package com.volmit.host;
import com.volmit.resource.ResourceId;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
@@ -12,7 +13,7 @@ import java.util.TreeMap;
* @param mantleLayers for each mantle layer some stage writes, by layer id in id order, the largest
* reach of the stages that write it
*/
public record PipelineReach(int blocks, Map<String, Integer> mantleLayers) {
public record PipelineReach(int blocks, Map<ResourceId, Integer> mantleLayers) {
public PipelineReach {
if (blocks < 0) throw new IllegalArgumentException("Negative reach: " + blocks);
mantleLayers = Collections.unmodifiableMap(new TreeMap<>(mantleLayers));
@@ -20,7 +21,7 @@ public record PipelineReach(int blocks, Map<String, Integer> mantleLayers) {
static PipelineReach of(Iterable<ResolvedStage> stages) {
int blocks = 0;
var layers = new TreeMap<String, Integer>();
var layers = new TreeMap<ResourceId, Integer>();
for (var stage : stages) {
int stageBlocks = stage.fixedReachBlocks();
blocks = Math.max(blocks, stageBlocks);
@@ -1,6 +1,7 @@
package com.volmit.host;
import com.volmit.mantle.MantleKey;
import com.volmit.resource.ResourceId;
import com.volmit.stage.PackReach;
import java.util.HashSet;
import java.util.List;
@@ -22,7 +23,7 @@ public record StageReach(PackReach blocks, List<MantleKey<?>> mantleLayers) {
public StageReach {
Objects.requireNonNull(blocks, "blocks");
mantleLayers = List.copyOf(mantleLayers);
var ids = new HashSet<String>();
var ids = new HashSet<ResourceId>();
for (var layer : mantleLayers) {
if (!ids.add(Objects.requireNonNull(layer, "mantle layer").id())) {
throw new IllegalArgumentException("Mantle layer " + layer.id() + " declared twice");
@@ -7,9 +7,10 @@ import com.volmit.block.BlockState;
import com.volmit.config.PackSettings;
import com.volmit.generation.ChunkContext;
import com.volmit.geometry.BlockBox;
import com.volmit.data.Codec;
import com.volmit.data.StorageCodec;
import com.volmit.mantle.MantleKey;
import com.volmit.mantle.MantleLifetime;
import com.volmit.resource.ResourceId;
import com.volmit.stage.ChunkStage;
import com.volmit.stage.ChunkStageContext;
import com.volmit.stage.MantleStage;
@@ -25,7 +26,7 @@ import java.io.IOException;
* its reach out when the pack loads.
*/
public final class VeinModule implements IrisModule {
public static final MantleKey<Integer> ORE = new MantleKey<>("example:veins/ore", Integer.class);
public static final MantleKey<Integer> ORE = new MantleKey<>(ResourceId.parse("example:veins/ore"),Integer.class);
public static final MantleStage PLAN = Stage.mantle("example:veins/plan");
public static final ChunkStage<Void> PLACE = Stage.chunk("example:veins/place");
public static final BlockState ORE_BLOCK = new BlockState("minecraft:iron_ore");
@@ -69,7 +70,7 @@ public final class VeinModule implements IrisModule {
blocks.set(x - context.originX(), y, z - context.originZ(), ORE_BLOCK));
}
private static final class IntegerCodec implements Codec<Integer> {
private static final class IntegerCodec implements StorageCodec<Integer> {
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(); }
@@ -3,8 +3,10 @@ package com.volmit.host;
import com.volmit.*;
import com.volmit.config.Feature;
import com.volmit.config.Level;
import com.volmit.data.Codec;
import com.volmit.data.SavedKey;
import com.volmit.data.StorageCodec;
import com.volmit.mantle.*;
import com.volmit.resource.ResourceId;
import com.volmit.resource.ResourceKind;
import com.volmit.service.Providers;
import com.volmit.service.Service;
@@ -79,12 +81,12 @@ class ModuleRegistryTest {
@Test
void mantleIdsAreUniqueAndCodecVersionIsSnapshotted() {
var registry = new ModuleRegistry();
var key = new MantleKey<>("test:core/layer", String.class);
var key = new MantleKey<>(ResourceId.parse("test:core/layer"), String.class);
var codec = codec(3);
registerAs(registry, "test:core", layers -> {
layers.registerMantle(key, MantleLifetime.GENERATION, codec);
assertThrows(IllegalArgumentException.class, () -> layers.registerMantle(key, MantleLifetime.PERSISTENT, codec));
assertThrows(IllegalArgumentException.class, () -> layers.registerMantle(new MantleKey<>("test:core/bad", String.class), MantleLifetime.GENERATION, codec(-1)));
assertThrows(IllegalArgumentException.class, () -> layers.registerMantle(new MantleKey<>(ResourceId.parse("test:core/bad"), String.class), MantleLifetime.GENERATION, codec(-1)));
});
assertEquals(3, registry.freeze().mantle().getFirst().version());
assertEquals(1, registry.freeze().mantle().size());
@@ -98,11 +100,11 @@ class ModuleRegistryTest {
assertTrue(sameNamespace.getMessage().contains("must be test:other or start with test:other/"), sameNamespace.getMessage());
var mantle = assertThrows(IllegalArgumentException.class, () -> registerAs(registry, "iris:snow",
layers -> layers.registerMantle(new MantleKey<>("iris:snowdepth", String.class), MantleLifetime.GENERATION, codec(1))));
layers -> layers.registerMantle(new MantleKey<>(ResourceId.parse("iris:snowdepth"), String.class), MantleLifetime.GENERATION, codec(1))));
assertEquals("Mantle layer iris:snowdepth must be iris:snow or start with iris:snow/, the id of its registering module "
+ "iris:snow from host classpath", mantle.getMessage());
assertThrows(IllegalStateException.class, () -> registry.registerMantle(new MantleKey<>("test:loose", String.class),
assertThrows(IllegalStateException.class, () -> registry.registerMantle(new MantleKey<>(ResourceId.parse("test:loose"), String.class),
MantleLifetime.GENERATION, codec(1)));
assertTrue(registry.freeze().settings().isEmpty());
assertTrue(registry.freeze().mantle().isEmpty());
@@ -208,16 +210,37 @@ class ModuleRegistryTest {
};
}
private static SavedKey<String> saved(String id) {
return new SavedKey<>(ResourceId.parse(id), String.class, codec(1), () -> "");
}
@Test
void savedKeysAreOwnedByTheRegisteringModuleAndUnique() {
var registry = new ModuleRegistry();
var key = saved("test:core/book");
registerAs(registry, "test:core", saves -> {
saves.registerSaved(key);
assertThrows(IllegalArgumentException.class, () -> saves.registerSaved(saved("test:core/book")));
assertThrows(NullPointerException.class, () -> saves.registerSaved(null));
});
assertEquals(List.of(key), registry.freeze().saved());
var foreign = assertThrows(IllegalArgumentException.class, () -> registerAs(new ModuleRegistry(), "test:other",
saves -> saves.registerSaved(key)));
assertTrue(foreign.getMessage().contains("must be test:other or start with test:other/"), foreign.getMessage());
assertThrows(IllegalStateException.class, () -> new ModuleRegistry().registerSaved(key));
}
@Test
void moduleMayRegisterItsOwnIdAndIdsBelowIt() {
var registry = new ModuleRegistry();
registerAs(registry, "iris:snow", layers -> {
layers.registerMantle(new MantleKey<>("iris:snow", String.class), MantleLifetime.GENERATION, codec(1));
layers.registerMantle(new MantleKey<>("iris:snow/depth/deep", String.class), MantleLifetime.GENERATION, codec(1));
layers.registerMantle(new MantleKey<>(ResourceId.parse("iris:snow"), String.class), MantleLifetime.GENERATION, codec(1));
layers.registerMantle(new MantleKey<>(ResourceId.parse("iris:snow/depth/deep"), String.class), MantleLifetime.GENERATION, codec(1));
});
assertEquals(List.of("iris:snow", "iris:snow/depth/deep"),
registry.freeze().mantle().stream().map(layer -> layer.key().id()).sorted().toList());
registry.freeze().mantle().stream().map(layer -> layer.key().id().toString()).sorted().toList());
}
@Test
@@ -592,8 +615,8 @@ class ModuleRegistryTest {
assertEquals("test:alpha", registrations.getFirst().module().info().id().toString());
}
private static Codec<String> codec(int version) {
return new Codec<>() {
private static StorageCodec<String> codec(int version) {
return new StorageCodec<>() {
public int version() { return version; }
public void write(DataOutput output, String value) {}
public String read(DataInput input, int storedVersion) { return ""; }
@@ -11,7 +11,7 @@ import com.volmit.config.Feature;
import com.volmit.config.FeatureSettings.Entry;
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.mantle.MantleLifetime;
import com.volmit.object.GeneratedObject;
@@ -519,7 +519,7 @@ class StageGraphTest {
@Test
void onePlacementInvocationMakesItsMantleViewsOnce() {
var key = new MantleKey<>("test:views/depth", Integer.class);
var key = new MantleKey<>(ResourceId.parse("test:views/depth"),Integer.class);
var views = new ArrayList<Object>();
var graph = resolve(Pipeline.PLACEMENT, module("test:views", stages ->
stages.define(Stage.placement("test:views/read")).run(context -> {
@@ -569,8 +569,8 @@ class StageGraphTest {
@Test
void reachIsDeclaredPerStageAndCollectedPerPipelineAndLayer() {
var depth = new MantleKey<>("test:reach/depth", Integer.class);
var marks = new MantleKey<>("test:reach/marks", String.class);
var depth = new MantleKey<>(ResourceId.parse("test:reach/depth"), Integer.class);
var marks = new MantleKey<>(ResourceId.parse("test:reach/marks"), String.class);
var carve = Stage.mantle("test:reach/carve");
var mark = Stage.mantle("test:reach/mark");
var plain = Stage.mantle("test:reach/plain");
@@ -585,7 +585,7 @@ class StageGraphTest {
assertEquals(new StageReach(24, List.of(depth)), loaded.mantle().stages().getFirst().reach());
assertEquals(StageReach.NONE, loaded.mantle().stages().get(2).reach(), "an undeclared reach is 0 with no layers");
assertEquals(new PipelineReach(24, Map.of("test:reach/depth", 24, "test:reach/marks", 8)), loaded.mantle().reach());
assertEquals(new PipelineReach(24, Map.of(depth.id(), 24, marks.id(), 8)), loaded.mantle().reach());
assertEquals(new PipelineReach(2, Map.of()), loaded.placement().reach());
assertEquals(new PipelineReach(0, Map.of()), loaded.chunk().reach());
assertTrue(loaded.mantle().describe().contains(" reach 24 blocks, writes mantle test:reach/depth\n"), loaded.mantle().describe());
@@ -655,7 +655,7 @@ class StageGraphTest {
@Test
void reachMustBeValidAndNameRegisteredLayers() {
var depth = new MantleKey<>("test:reach/depth", Integer.class);
var depth = new MantleKey<>(ResourceId.parse("test:reach/depth"), Integer.class);
var carve = Stage.mantle("test:reach/carve");
var negative = assertThrows(IllegalArgumentException.class, () -> resolve(Pipeline.MANTLE,
@@ -672,7 +672,7 @@ class StageGraphTest {
assertEquals("Stage test:reach/carve of module test:reach writes mantle layer test:reach/depth, which no module registers;"
+ " its owner registers it with FeatureRegistry.registerMantle", unregistered.getMessage());
var asText = new MantleKey<>("test:reach/depth", String.class);
var asText = new MantleKey<>(ResourceId.parse("test:reach/depth"), String.class);
var mismatch = assertThrows(ModuleLoadException.class, () -> LoadedModules.load(List.of(DiscoveredModule.builtIn(withMantle(
module("test:reach", stages -> stages.define(carve).writesMantle(asText).run(context -> {})), depth))), Map.of(), Set.of(), Set.of()));
assertEquals("Stage test:reach/carve of module test:reach writes mantle layer test:reach/depth as java.lang.String,"
@@ -694,8 +694,8 @@ class StageGraphTest {
registry.registerMantle(layer, MantleLifetime.GENERATION, codec(layer));
}
private static <T> Codec<T> codec(MantleKey<T> layer) {
return new Codec<>() {
private static <T> StorageCodec<T> codec(MantleKey<T> layer) {
return new StorageCodec<>() {
public int version() { return 0; }
public void write(java.io.DataOutput output, T value) {}
public T read(java.io.DataInput input, int storedVersion) { throw new UnsupportedOperationException(); }
@@ -21,15 +21,15 @@ public final class MemoryMantle {
.thenComparingInt(BlockPosition::y).thenComparingInt(BlockPosition::z);
/** Where the planned blocks are kept; the host owns them, so no module registers this layer. */
private static final MantleKey<BlockState> PLANNED_BLOCKS = new MantleKey<>("host:planned-blocks", BlockState.class);
private static final MantleKey<BlockState> PLANNED_BLOCKS = new MantleKey<>(ResourceId.parse("host:planned-blocks"),BlockState.class);
/** Where the carved voids are kept; the host owns them, so no module registers this layer. */
private static final MantleKey<CarvedVoid> CAVERNS = new MantleKey<>("host:caverns", CarvedVoid.class);
private static final MantleKey<CarvedVoid> CAVERNS = new MantleKey<>(ResourceId.parse("host:caverns"),CarvedVoid.class);
/** Where the platform biomes stages set are kept; the host owns them, so no module registers this layer. */
private static final MantleKey<ResourceId> PLATFORM_BIOMES = new MantleKey<>("host:platform-biomes", ResourceId.class);
private static final MantleKey<ResourceId> PLATFORM_BIOMES = new MantleKey<>(ResourceId.parse("host:platform-biomes"),ResourceId.class);
private final Map<String, TreeMap<BlockPosition, Object>> layers = new HashMap<>();
private final Map<ResourceId, TreeMap<BlockPosition, Object>> layers = new HashMap<>();
/** A view that accepts reads and writes inside {@code bounds} only. */
public Mantle within(BlockBox bounds) {