diff --git a/src/main/java/com/volmit/FeatureRegistry.java b/src/main/java/com/volmit/FeatureRegistry.java index 507cdf1ce..aa923f97e 100644 --- a/src/main/java/com/volmit/FeatureRegistry.java +++ b/src/main/java/com/volmit/FeatureRegistry.java @@ -8,7 +8,7 @@ import com.volmit.object.ObjectGeneratorFactory; /** Host implementation collects non-stage registrations before loading pack configuration. * Feature IDs must be unique. JSON fields must be unique per attachment target, including the * settings types stages name in {@code when(...)}. Feature IDs and mantle keys a module registers - * use the namespace of its module id. + * are its module id or start with it and {@code /}, such as {@code iris:snow} or {@code iris:snow/depth}. */ public interface FeatureRegistry { /** Registers a layer before pack loading. Null arguments throw NullPointerException; diff --git a/src/main/java/com/volmit/ModuleInfo.java b/src/main/java/com/volmit/ModuleInfo.java index 05029fae2..12356ffc9 100644 --- a/src/main/java/com/volmit/ModuleInfo.java +++ b/src/main/java/com/volmit/ModuleInfo.java @@ -8,7 +8,8 @@ import java.util.Objects; /** Module identity. Every stage a module defines has an id that starts with the module id and * {@code /}, such as {@code iris:stilt/fill} for module {@code iris:stilt}, so a module id has no * {@code /} of its own: {@code iris:stilt/extra} would own stages under {@code iris:stilt}'s path. - * Settings ids and mantle keys the module registers use its namespace. The name is for + * Settings ids, object feature ids and mantle keys the module owns are its id or start with it and + * {@code /}, such as {@code iris:stilt} or {@code iris:stilt/depth}. The name is for * people, for example in load logs; the id is what the host and other modules match on. Name and * version must fit on one log line, so they reject control characters such as line breaks. * @@ -51,15 +52,22 @@ public record ModuleInfo(ResourceId id, String name, String version, List factory) { checkOpen(); var descriptor = FeatureDescriptor.of(featureType); - requireModuleNamespace("Object generator settings " + featureType.getName() + " with feature id", descriptor.id()); + requireOwnedId("Object generator settings " + featureType.getName() + " with feature id", descriptor.id()); var registration = new ObjectRegistration<>(descriptor, factory); claim(descriptor); objects.put(featureType, registration); @@ -73,7 +73,7 @@ public final class ModuleRegistry implements FeatureRegistry { checkOpen(); Objects.requireNonNull(key, "key"); Objects.requireNonNull(codec, "codec"); - requireModuleNamespace("Mantle layer", ResourceId.parse(key.id())); + requireOwnedId("Mantle layer", ResourceId.parse(key.id())); var registration = new MantleRegistration<>(key, lifetime, codec, codec.version()); if (mantle.putIfAbsent(key.id(), registration) != null) { @@ -99,14 +99,14 @@ public final class ModuleRegistry implements FeatureRegistry { return catalog; } - private void requireModuleNamespace(String registration, ResourceId id) { + private void requireOwnedId(String registration, ResourceId id) { if (registering == null) { throw new IllegalStateException(registration + " " + id + " registered outside IrisModule.register"); } - var namespace = registering.info().id().namespace(); - if (id.namespace().equals(namespace)) return; - throw new IllegalArgumentException(registration + " " + id + " must use namespace '" + namespace - + "' of its registering module " + registering); + var info = registering.info(); + if (info.ownsId(id)) return; + throw new IllegalArgumentException(registration + " " + id + " must be " + info.id() + " or start with " + + info.idPrefix() + ", the id of its registering module " + registering); } private void reserveHostFields(Map> reservedFields) { diff --git a/src/main/java/com/volmit/host/StageGraphBuilder.java b/src/main/java/com/volmit/host/StageGraphBuilder.java index 1d396516e..831002195 100644 --- a/src/main/java/com/volmit/host/StageGraphBuilder.java +++ b/src/main/java/com/volmit/host/StageGraphBuilder.java @@ -26,7 +26,8 @@ import java.util.stream.Collectors; * discard the whole builder: it may hold a partial module. Errors about one module name its origin. * *

A module defines only stages whose ids start with its own id and '/', and a settings type - * named in {@code when(...)} must have a feature id in the namespace of a module that names it. + * named in {@code when(...)} must have a feature id that at least one module naming it owns: that + * module's id, or one starting with it and '/'. */ public final class StageGraphBuilder implements StageGraph { private final Map declarations = new HashMap<>(); @@ -65,8 +66,8 @@ public final class StageGraphBuilder implements StageGraph { return GraphResolver.resolve(pipeline, declarations.values()); } - /** Settings types named by when(...), ordered by feature id. Fails if a type's feature id is not - * in the namespace of any module that names it: some module of that namespace must own it. + /** Settings types named by when(...), ordered by feature id. Fails if no module that names a type + * owns its feature id ({@link com.volmit.ModuleInfo#ownsId}). */ public List> settingsTypes() { var uses = settingsUses(); @@ -104,17 +105,22 @@ public final class StageGraphBuilder implements StageGraph { private void requireSettingsOwner(SettingsUse use) { var settings = use.settings(); - var namers = use.namers(); - var namespace = settings.id().namespace(); - if (namers.stream().anyMatch(module -> module.namespace().equals(namespace))) return; + var namers = use.namers().stream().map(modules::get).toList(); + if (namers.stream().anyMatch(module -> module.info().ownsId(settings.id()))) return; + var example = namers.getFirst().info(); throw new IllegalArgumentException("Settings type " + settings.type().getName() + " has feature id " - + settings.id() + ", but no module of namespace '" + namespace + "' names it in when(...); it is named by " - + describeModules(namers) + ". A feature id uses the namespace of the module that owns the settings"); + + settings.id() + ", which none of the modules naming it in when(...) owns: " + describeModules(namers) + + ". A feature id is the id of the module that owns the settings, such as " + example.id() + + ", or starts with it and '/', such as " + example.idPrefix() + lastSegment(settings.id())); } - private String describeModules(SortedSet moduleIds) { - return moduleIds.stream() - .map(modules::get) + private static String lastSegment(ResourceId id) { + var path = id.path(); + return path.substring(path.lastIndexOf('/') + 1); + } + + private static String describeModules(List modules) { + return modules.stream() .map(DiscoveredModule::toString) .collect(Collectors.joining(", ")); } @@ -144,7 +150,7 @@ public final class StageGraphBuilder implements StageGraph { private static void requireOwnedStageId(Stage stage, DiscoveredModule discovered) { var info = discovered.info(); if (info.ownsStage(stage.id())) return; - throw new IllegalArgumentException("Stage " + stage + " must start with " + info.stageIdPrefix() + throw new IllegalArgumentException("Stage " + stage + " must start with " + info.idPrefix() + ", the id of its defining module " + discovered); } diff --git a/src/main/java/com/volmit/stage/Stage.java b/src/main/java/com/volmit/stage/Stage.java index ce32b191f..ca82c894b 100644 --- a/src/main/java/com/volmit/stage/Stage.java +++ b/src/main/java/com/volmit/stage/Stage.java @@ -4,7 +4,7 @@ import com.volmit.resource.ResourceId; import java.util.Objects; /** A unit of pipeline work, declared as a constant by the module that defines it. - * The id is namespaced and its namespace must match the defining module. Identity is this + * The id starts with the defining module's id and '/', such as {@code iris:stilt/fill}. Identity is this * declaration object: other modules require or read the owner's constant, never a copy. * The output type is Void for stages that only act; otherwise dependents read it through * {@link StageContext#output(Stage)}. Block stages accumulate their output across the pass. diff --git a/src/main/java/com/volmit/stage/StageDefinition.java b/src/main/java/com/volmit/stage/StageDefinition.java index effb151ed..bf2e317f9 100644 --- a/src/main/java/com/volmit/stage/StageDefinition.java +++ b/src/main/java/com/volmit/stage/StageDefinition.java @@ -14,7 +14,10 @@ public interface StageDefinition { StageDefinition softBefore(StageRef... stages); - /** Activates the stage only where the given feature settings are configured. */ + /** Activates the stage only where the given feature settings are configured. At least one module + * naming the type must own its feature id: the id is that module's id or starts with it and '/'. + * Other modules may name it too, such as moss conditioning on {@code iris:snow}. + */ ConditionalStageDefinition when(Class settingsType); /** Body for a stage without output. */ diff --git a/src/main/java/com/volmit/stage/StageGraph.java b/src/main/java/com/volmit/stage/StageGraph.java index 8beaf19c0..7f3b909b2 100644 --- a/src/main/java/com/volmit/stage/StageGraph.java +++ b/src/main/java/com/volmit/stage/StageGraph.java @@ -2,8 +2,8 @@ package com.volmit.stage; /** Collects the stages a module defines during {@code IrisModule.install}. Each define call must * end with exactly one body (run, produce or accumulate). The graph is resolved once at pack load: - * missing requirements, cycles, duplicate definitions, foreign namespaces and cross-pipeline - * edges are load errors. Bodies are shared by concurrent invocations and must not capture + * missing requirements, cycles, duplicate definitions, stage ids outside the module's id and + * cross-pipeline edges are load errors. Bodies are shared by concurrent invocations and must not capture * invocation state; everything per-invocation arrives through the context and outputs. */ public interface StageGraph { diff --git a/src/test/java/com/volmit/ApiValuesTest.java b/src/test/java/com/volmit/ApiValuesTest.java index b15d6c2dd..02bbd1822 100644 --- a/src/test/java/com/volmit/ApiValuesTest.java +++ b/src/test/java/com/volmit/ApiValuesTest.java @@ -69,19 +69,26 @@ class ApiValuesTest { } @Test - void moduleIdsAreOneSegmentAndOwnTheStageIdsBelowThem() { + void moduleIdsAreOneSegmentAndOwnTheIdsBelowThem() { var nested = assertThrows(IllegalArgumentException.class, () -> ModuleInfo.of("iris:stilt/extra", "Extra", "1")); assertEquals("Module id iris:stilt/extra must not contain '/': stage ids are a module id, '/' and a name, " + "so iris:stilt/extra would name stages under another module's id", nested.getMessage()); var stilt = ModuleInfo.of("iris:stilt", "Stilt", "1"); - assertEquals("iris:stilt/", stilt.stageIdPrefix()); + assertEquals("iris:stilt/", stilt.idPrefix()); assertTrue(stilt.ownsStage(ResourceId.parse("iris:stilt/fill"))); assertTrue(stilt.ownsStage(ResourceId.parse("iris:stilt/fill/deep"))); assertFalse(stilt.ownsStage(ResourceId.parse("iris:stilt"))); assertFalse(stilt.ownsStage(ResourceId.parse("iris:stilts/fill"))); assertFalse(stilt.ownsStage(ResourceId.parse("iris:anchor/height"))); assertFalse(stilt.ownsStage(ResourceId.parse("other:stilt/fill"))); + + assertTrue(stilt.ownsId(ResourceId.parse("iris:stilt"))); + assertTrue(stilt.ownsId(ResourceId.parse("iris:stilt/depth"))); + assertFalse(stilt.ownsId(ResourceId.parse("iris:stilts"))); + assertFalse(stilt.ownsId(ResourceId.parse("iris:stilt-depth"))); + assertFalse(stilt.ownsId(ResourceId.parse("iris:anchor"))); + assertFalse(stilt.ownsId(ResourceId.parse("other:stilt"))); } @Test diff --git a/src/test/java/com/volmit/host/ModuleRegistryTest.java b/src/test/java/com/volmit/host/ModuleRegistryTest.java index 76b8137f9..e12010551 100644 --- a/src/test/java/com/volmit/host/ModuleRegistryTest.java +++ b/src/test/java/com/volmit/host/ModuleRegistryTest.java @@ -16,16 +16,16 @@ import java.util.function.Consumer; import static org.junit.jupiter.api.Assertions.*; class ModuleRegistryTest { - @Feature(id = "test:one", name = "One", field = "shared", targets = FeatureAttachment.Target.BIOME) + @Feature(id = "test:core/one", name = "One", field = "shared", targets = FeatureAttachment.Target.BIOME) static class One implements IrisFeature {} - @Feature(id = "test:two", name = "Two", field = "shared", targets = FeatureAttachment.Target.DIMENSION) + @Feature(id = "test:core/two", name = "Two", field = "shared", targets = FeatureAttachment.Target.DIMENSION) static class Two implements IrisFeature {} - @Feature(id = "test:collision", name = "Collision", field = "shared", targets = {FeatureAttachment.Target.DIMENSION, FeatureAttachment.Target.BIOME}) + @Feature(id = "test:core/collision", name = "Collision", field = "shared", targets = {FeatureAttachment.Target.DIMENSION, FeatureAttachment.Target.BIOME}) static class Collision implements IrisFeature {} - @Feature(id = "test:one", name = "Duplicate", field = "different", targets = FeatureAttachment.Target.REGION) + @Feature(id = "test:core/one", name = "Duplicate", field = "different", targets = FeatureAttachment.Target.REGION) static class Duplicate implements IrisFeature {} static class MissingAnnotation implements IrisFeature {} - @Feature(id = "test:abstract", name = "Abstract", field = "abstract", targets = FeatureAttachment.Target.BIOME) + @Feature(id = "test:core/abstract", name = "Abstract", field = "abstract", targets = FeatureAttachment.Target.BIOME) abstract static class AbstractFeature implements IrisFeature {} /** Runs {@code registrations} as the register(...) of a built-in module with the given id. */ @@ -42,15 +42,15 @@ class ModuleRegistryTest { var registry = new ModuleRegistry(); registry.registerSettings(One.class); registry.registerSettings(One.class); - registerAs(registry, "test:objects", objects -> { + registerAs(registry, "test:core", objects -> { assertThrows(IllegalArgumentException.class, () -> objects.registerObject(Collision.class, ignored -> context -> null)); objects.registerObject(Two.class, ignored -> context -> null); assertThrows(IllegalArgumentException.class, () -> objects.registerObject(Duplicate.class, ignored -> context -> null)); assertThrows(IllegalArgumentException.class, () -> objects.registerObject(One.class, ignored -> context -> null)); }); var catalog = registry.freeze(); - assertEquals(List.of("test:one"), catalog.settings().stream().map(value -> value.id().toString()).toList()); - assertEquals(List.of("test:two"), catalog.objects().stream().map(value -> value.descriptor().id().toString()).toList()); + assertEquals(List.of("test:core/one"), catalog.settings().stream().map(value -> value.id().toString()).toList()); + assertEquals(List.of("test:core/two"), catalog.objects().stream().map(value -> value.descriptor().id().toString()).toList()); } @Test @@ -59,7 +59,7 @@ class ModuleRegistryTest { assertThrows(IllegalArgumentException.class, () -> registry.registerSettings(One.class)); assertThrows(IllegalArgumentException.class, () -> registry.registerSettings(MissingAnnotation.class)); assertThrows(IllegalArgumentException.class, () -> registry.registerSettings(AbstractFeature.class)); - registerAs(registry, "test:objects", objects -> { + registerAs(registry, "test:core", objects -> { assertThrows(NullPointerException.class, () -> objects.registerObject(Two.class, null)); objects.registerObject(Two.class, ignored -> context -> null); }); @@ -70,29 +70,34 @@ class ModuleRegistryTest { @Test void mantleIdsAreUniqueAndCodecVersionIsSnapshotted() { var registry = new ModuleRegistry(); - var key = new MantleKey<>("test:layer", String.class); + var key = new MantleKey<>("test:core/layer", String.class); var codec = codec(3); - registerAs(registry, "test:layers", layers -> { + 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:bad", String.class), MantleLifetime.GENERATION, codec(-1))); + assertThrows(IllegalArgumentException.class, () -> layers.registerMantle(new MantleKey<>("test:core/bad", String.class), MantleLifetime.GENERATION, codec(-1))); }); assertEquals(3, registry.freeze().mantle().getFirst().version()); assertEquals(1, registry.freeze().mantle().size()); } @Test - void objectFeatureIdsAndMantleKeysUseTheRegisteringModulesNamespace() { + void objectFeatureIdsAndMantleKeysAreTheRegisteringModulesIdOrBelowIt() { var registry = new ModuleRegistry(); var object = assertThrows(IllegalArgumentException.class, () -> registerAs(registry, "other:objects", objects -> objects.registerObject(Two.class, ignored -> context -> null))); - assertEquals("Object generator settings " + Two.class.getName() + " with feature id test:two must use namespace " - + "'other' of its registering module other:objects from host classpath", object.getMessage()); + assertEquals("Object generator settings " + Two.class.getName() + " with feature id test:core/two must be " + + "other:objects or start with other:objects/, the id of its registering module other:objects from host classpath", + object.getMessage()); - var mantle = assertThrows(IllegalArgumentException.class, () -> registerAs(registry, "other:layers", - layers -> layers.registerMantle(new MantleKey<>("iris:depth", String.class), MantleLifetime.GENERATION, codec(1)))); - assertEquals("Mantle layer iris:depth must use namespace 'other' of its registering module other:layers from host classpath", - mantle.getMessage()); + var sameNamespace = assertThrows(IllegalArgumentException.class, () -> registerAs(registry, "test:other", + objects -> objects.registerObject(Two.class, ignored -> context -> null))); + 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)))); + 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), MantleLifetime.GENERATION, codec(1))); @@ -100,16 +105,28 @@ class ModuleRegistryTest { assertTrue(registry.freeze().mantle().isEmpty()); } + @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)); + }); + + assertEquals(List.of("iris:snow", "iris:snow/depth/deep"), + registry.freeze().mantle().stream().map(layer -> layer.key().id()).sorted().toList()); + } + @Test void loadingModulesCatalogsStageSettingsAndRegistrationsTogether() { var conditioned = new IrisModule() { - static final PlacementStage WORK = Stage.placement("test:conditioned/work"); - public ModuleInfo info() { return ModuleInfo.of("test:conditioned", "Conditioned", "1"); } + static final PlacementStage WORK = Stage.placement("test:core/work"); + public ModuleInfo info() { return ModuleInfo.of("test:core", "Conditioned", "1"); } public void install(StageGraph graph) { graph.define(WORK).when(One.class).run((context, one) -> {}); } public void register(FeatureRegistry registry) { registry.registerObject(Two.class, ignored -> context -> null); } }; var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(conditioned)), Map.of(), Set.of()); - assertEquals(List.of("test:one"), loaded.catalog().settings().stream().map(value -> value.id().toString()).toList()); + assertEquals(List.of("test:core/one"), loaded.catalog().settings().stream().map(value -> value.id().toString()).toList()); assertEquals(1, loaded.catalog().objects().size()); assertEquals(1, loaded.placement().stages().size()); assertTrue(loaded.chunk().stages().isEmpty()); diff --git a/src/test/java/com/volmit/host/StageGraphTest.java b/src/test/java/com/volmit/host/StageGraphTest.java index b4f085ddf..bb9da6943 100644 --- a/src/test/java/com/volmit/host/StageGraphTest.java +++ b/src/test/java/com/volmit/host/StageGraphTest.java @@ -24,6 +24,8 @@ import com.volmit.support.MemoryEdit; import com.volmit.support.TestContexts; import com.volmit.support.TestTerrain; import com.volmit.world.BlockPosition; +import com.volmit.example.placement.SnowModule; +import com.volmit.example.placement.SnowSettings; import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; @@ -34,7 +36,7 @@ import static org.junit.jupiter.api.Assertions.*; class StageGraphTest { private static final BlockState STONE = new BlockState("minecraft:stone"); - @Feature(id = "test:toggle", name = "Toggle", field = "toggle", targets = FeatureAttachment.Target.OBJECT_PLACEMENT) + @Feature(id = "test:owner/toggle", name = "Toggle", field = "toggle", targets = FeatureAttachment.Target.OBJECT_PLACEMENT) static final class Toggle implements IrisFeature {} static final class Unannotated implements IrisFeature {} @@ -227,17 +229,26 @@ class StageGraphTest { } @Test - void settingsMustBeOwnedByAModuleOfTheirNamespace() { + void settingsMustBeOwnedByAModuleThatNamesThem() { var foreign = assertThrows(IllegalArgumentException.class, () -> resolveSettings( module("other:user", stages -> stages.define(Stage.placement("other:user/work")).when(Toggle.class).run((context, toggle) -> {})))); - assertEquals("Settings type " + Toggle.class.getName() + " has feature id test:toggle, but no module of namespace 'test' " - + "names it in when(...); it is named by other:user from host classpath. A feature id uses the namespace " - + "of the module that owns the settings", foreign.getMessage()); + assertEquals("Settings type " + Toggle.class.getName() + " has feature id test:owner/toggle, which none of the modules " + + "naming it in when(...) owns: other:user from host classpath. A feature id is the id of the module that owns " + + "the settings, such as other:user, or starts with it and '/', such as other:user/toggle", foreign.getMessage()); + + var sameNamespace = assertThrows(IllegalArgumentException.class, () -> resolveSettings( + module("test:user", stages -> stages.define(Stage.placement("test:user/work")).when(Toggle.class).run((context, toggle) -> {})))); + assertTrue(sameNamespace.getMessage().contains("none of the modules naming it in when(...) owns: test:user from host classpath"), + sameNamespace.getMessage()); var shared = resolveSettings( module("test:owner", stages -> stages.define(Stage.placement("test:owner/work")).when(Toggle.class).run((context, toggle) -> {})), module("other:user", stages -> stages.define(Stage.placement("other:user/work")).when(Toggle.class).run((context, toggle) -> {}))); assertEquals(List.of(Toggle.class), shared); + + var snowOnMoss = resolveSettings(new SnowModule(), module("example:moss", stages -> + stages.define(Stage.placement("example:moss/on-snow")).when(SnowSettings.class).run((context, snow) -> {}))); + assertTrue(snowOnMoss.contains(SnowSettings.class), "moss may condition on iris:snow, which iris:snow owns"); } @Test