diff --git a/CONTRACTS.md b/CONTRACTS.md index 28a138202..8784b0dcf 100644 --- a/CONTRACTS.md +++ b/CONTRACTS.md @@ -23,7 +23,7 @@ types that carry them and lists what the Iris adapter still owes. | Work across chunks and mantle writes | `Stage.mantle`, `MantleStage`, `MantleStageContext`, `MantleGenerationContext`; chunk stages read the mantle only | | Annotated settings | `IrisFeature`, `@Feature` (id as JSON key), config annotations, `OptionsMetadata` | | Area a stage writes | `reach(blocks)`, `reach(PackReach)`, `writesMantle(...)` on stage definitions, `PackSettings`, host `StageReach`, `ResolvedStage.reachBlocks`, `PipelineReach` | -| Settings described for studio | `@Desc`, `@Required`, `@MinNumber`, `@MaxNumber`, `@MinItems`, `@Snippet`, `@Ref`/`Reference`, `FeatureRegistry.registerSettings`, host `LoadedPack.schema()` (`PackSchema`, `FeatureSchema`, `ObjectType`, `SchemaOption`, `ValueKind` with `HostKind`), `ModuleHost.describingHostTypesIn` | +| Settings described for studio | `@Desc`, `@Required`, `@MinNumber`, `@MaxNumber`, `@MinItems`, `@Snippet`, `@Ref`/`Reference`, `FeatureRegistry.registerSettings`, `FeatureRegistry.registerHeldType`, host `FeatureCatalog.heldTypes()`, host `LoadedPack.schema()` (`PackSchema`, `FeatureSchema`, `ObjectType`, `SchemaOption`, `ValueKind` with `HostKind`), `ModuleHost.describingHostTypesIn` | | Geometry and mantle registration | `FeatureRegistry`, host `ModuleRegistry`, `FeatureCatalog` | | Block states and palettes | `BlockState`, `BlockRegistry`, `BlockPalette`, `BlockSpec` (`BlockRegistry.resolve(BlockSpec)`, `blockEntity`), `PaletteSpec` and `PaletteSampler` (`BlockRegistry.palette`) | | Noise from pack-described styles | `NoiseStyle`, `StyledRange`, `PackResources.noise()`, `NoiseFactory`, `NoiseSampler` | diff --git a/DESIGN.md b/DESIGN.md index 0ab7f2a4f..3d921f33a 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -613,6 +613,9 @@ never equal a feature id. inlined copy, so a class used in many places is described once, and a class that contains itself, directly or through others, ends the walk at its own name. Two different classes with one name fail the load; only a built-in class and a pack class can collide that way. +- Built-in modules may also register a class that Iris's own classes hold (`registerHeldType`, `FeatureCatalog.heldTypes()`): + it is described like any settings class reachable from a feature, but no `FeatureSchema` names it. This is for + settings that live in a host class until their feature owns them, such as the clamp of an object placement. - A `SchemaOption` per field that `OptionsMetadata` selects, which means instance fields that are not transient, inherited ones included, in declaration order. Each has its name, `ValueKind`, `@Desc`, `@Required`, `@MinNumber`/`@MaxNumber`, `@MinItems` and the default value. diff --git a/README.md b/README.md index 930005d27..c05bc08f3 100644 --- a/README.md +++ b/README.md @@ -424,6 +424,9 @@ public final class FloraSettings implements IrisFeature { that no stage names, such as ones read only through `FeatureSettings.at(...)`, are registered with `registry.registerSettings(WindSettings.class)` in `register`. Their feature id follows the module id like any other, and once registered, other modules may name them in `when(...)`. +- A class of a built-in module that Iris's own classes hold, such as the clamp field of an object placement, is + registered with `registry.registerHeldType(Clamp.class)`, so studio describes it from the class (as an + `ObjectType`, with no feature). Only built-in modules may; a pack module's class can never be a field of an Iris class. A load error names the field, the settings and the module: diff --git a/src/main/java/com/volmit/FeatureRegistry.java b/src/main/java/com/volmit/FeatureRegistry.java index 01e2cdf06..4a2817e15 100644 --- a/src/main/java/com/volmit/FeatureRegistry.java +++ b/src/main/java/com/volmit/FeatureRegistry.java @@ -35,6 +35,14 @@ public interface FeatureRegistry { */ void registerSettings(Class settingsType); + /** Describes a class that Iris's own classes hold, such as the clamp of an object placement, so studio shows + * its fields as this class declares them. Only built-in modules may register one: the host classes that hold + * the class are Iris's, so a pack module's class could never be a field of one. The host reads the class + * with its fields in pack JSON, and the class follows the rules of settings classes. Registering a class twice + * changes nothing. + */ + void registerHeldType(Class type); + /** Declares a kind of pack resource this module reads: the JSON files under the kind's folder, served through * {@link com.volmit.resource.PackResources} by the kind's type and described for studio from its class like * settings. The pack load fails when another kind has the same id, type or folder, or the folder is one the diff --git a/src/main/java/com/volmit/host/FeatureCatalog.java b/src/main/java/com/volmit/host/FeatureCatalog.java index a6f2dd02c..551f95e28 100644 --- a/src/main/java/com/volmit/host/FeatureCatalog.java +++ b/src/main/java/com/volmit/host/FeatureCatalog.java @@ -21,6 +21,7 @@ public final class FeatureCatalog { private final Map, ObjectRegistration> objects; private final Map> mantle; private final List resourceKinds; + private final List heldTypes; private final List validators; private final List lifecycles; private final List services; @@ -31,6 +32,7 @@ public final class FeatureCatalog { Map, ObjectRegistration> objects, Map> mantle, Collection resourceKinds, + Collection heldTypes, List validators, List lifecycles, List services, @@ -39,6 +41,7 @@ public final class FeatureCatalog { this.objects = Map.copyOf(objects); this.mantle = Map.copyOf(mantle); this.resourceKinds = sorted(resourceKinds, registration -> registration.kind().id()); + this.heldTypes = sorted(heldTypes, registration -> registration.type().getName()); this.validators = inModuleOrder(validators, ValidatorRegistration::module); this.lifecycles = inModuleOrder(lifecycles, LifecycleRegistration::module); this.services = inModuleOrder(services, ServiceRegistration::module); @@ -63,6 +66,11 @@ public final class FeatureCatalog { return resourceKinds; } + /** The classes built-in modules described because Iris's own classes hold them, by class name. */ + public List heldTypes() { + return heldTypes; + } + /** The validators modules registered, by module id, each module's in the order it registered them. */ public List validators() { return validators; diff --git a/src/main/java/com/volmit/host/HeldTypeRegistration.java b/src/main/java/com/volmit/host/HeldTypeRegistration.java new file mode 100644 index 000000000..0cccdce06 --- /dev/null +++ b/src/main/java/com/volmit/host/HeldTypeRegistration.java @@ -0,0 +1,16 @@ +package com.volmit.host; + +import java.util.Objects; + +/** A class of a built-in module that Iris's own classes hold, and the module that registered it. */ +public record HeldTypeRegistration(Class type, DiscoveredModule module) { + public HeldTypeRegistration { + Objects.requireNonNull(type, "type"); + Objects.requireNonNull(module, "module"); + } + + @Override + public String toString() { + return "held type " + type.getName() + " of module " + module; + } +} diff --git a/src/main/java/com/volmit/host/ModuleRegistry.java b/src/main/java/com/volmit/host/ModuleRegistry.java index 65c348b17..072304ddd 100644 --- a/src/main/java/com/volmit/host/ModuleRegistry.java +++ b/src/main/java/com/volmit/host/ModuleRegistry.java @@ -39,6 +39,7 @@ public final class ModuleRegistry implements FeatureRegistry { private final Set> featureTypes = new HashSet<>(); private final Set> registeredByOwner = new HashSet<>(); private final Map resourceKinds = new HashMap<>(); + private final Map, HeldTypeRegistration> heldTypes = new HashMap<>(); private final List validators = new ArrayList<>(); private final List lifecycles = new ArrayList<>(); private final List services = new ArrayList<>(); @@ -105,6 +106,18 @@ public final class ModuleRegistry implements FeatureRegistry { registeredByOwner.add(settingsType); } + @Override + public void registerHeldType(Class type) { + checkOpen(); + Objects.requireNonNull(type, "type"); + requireRegistering("Held type " + type.getName()); + if (registering.source() != DiscoveredModule.Source.HOST) { + throw new IllegalArgumentException("Held type " + type.getName() + " of module " + registering + " is registered by a" + + " pack module; only built-in modules may describe a class that Iris's own classes hold"); + } + heldTypes.putIfAbsent(type, new HeldTypeRegistration(type, registering)); + } + @Override public void registerResourceKind(ResourceKind kind) { checkOpen(); @@ -179,8 +192,8 @@ public final class ModuleRegistry implements FeatureRegistry { public FeatureCatalog freeze() { if (catalog == null) { - catalog = new FeatureCatalog(settings, objects, mantle, resourceKinds.values(), validators, lifecycles, services, - vanillaStructureHooks); + catalog = new FeatureCatalog(settings, objects, mantle, resourceKinds.values(), heldTypes.values(), + validators, lifecycles, services, vanillaStructureHooks); } return catalog; } diff --git a/src/main/java/com/volmit/host/PackSchemaBuilder.java b/src/main/java/com/volmit/host/PackSchemaBuilder.java index 947525bb5..864e5917a 100644 --- a/src/main/java/com/volmit/host/PackSchemaBuilder.java +++ b/src/main/java/com/volmit/host/PackSchemaBuilder.java @@ -47,6 +47,7 @@ final class PackSchemaBuilder { private PackSchema describe(FeatureCatalog catalog) { var features = catalogued(catalog).map(this::draft).toList(); var resources = catalog.resourceKinds().stream().map(this::describe).toList(); + catalog.heldTypes().forEach(this::draft); requireUniqueSnippets(); var types = completeTypes(); return new PackSchema(features.stream().map(feature -> feature.complete(types)).toList(), resources, @@ -97,6 +98,10 @@ final class PackSchemaBuilder { return Stream.concat(settings, objects).sorted(Comparator.comparing(catalogued -> catalogued.descriptor().id())); } + private void draft(HeldTypeRegistration held) { + drafts.settings(SchemaSource.heldType(held)); + } + private FeatureDraft draft(Catalogued catalogued) { var owner = ownerOf(catalogued.descriptor()); drafts.settings(SchemaSource.settings(catalogued.descriptor(), owner)); diff --git a/src/main/java/com/volmit/host/SchemaSource.java b/src/main/java/com/volmit/host/SchemaSource.java index 6b825a4cc..59f000b00 100644 --- a/src/main/java/com/volmit/host/SchemaSource.java +++ b/src/main/java/com/volmit/host/SchemaSource.java @@ -16,6 +16,10 @@ record SchemaSource(String subject, Class type, DiscoveredModule module) { return new SchemaSource("resource kind " + kind.id(), kind.type(), module); } + static SchemaSource heldType(HeldTypeRegistration held) { + return new SchemaSource("held type " + held.type().getName(), held.type(), held.module()); + } + @Override public String toString() { return subject + " (module " + module + ")"; diff --git a/src/test/java/com/volmit/example/held/ClampSettings.java b/src/test/java/com/volmit/example/held/ClampSettings.java new file mode 100644 index 000000000..4e0cb53ea --- /dev/null +++ b/src/test/java/com/volmit/example/held/ClampSettings.java @@ -0,0 +1,19 @@ +package com.volmit.example.held; + +import com.volmit.config.Desc; +import com.volmit.config.MaxNumber; +import com.volmit.config.MinNumber; +import com.volmit.config.Snippet; + +@Snippet("clamp") +@Desc("Limits a placement by height") +public final class ClampSettings { + @MinNumber(-2048) + @MaxNumber(2048) + @Desc("The lowest height") + private int minimumHeight = -2048; + + public int minimumHeight() { + return minimumHeight; + } +} diff --git a/src/test/java/com/volmit/example/held/HeldModule.java b/src/test/java/com/volmit/example/held/HeldModule.java new file mode 100644 index 000000000..8e038a298 --- /dev/null +++ b/src/test/java/com/volmit/example/held/HeldModule.java @@ -0,0 +1,23 @@ +package com.volmit.example.held; + +import com.volmit.FeatureRegistry; +import com.volmit.IrisModule; +import com.volmit.ModuleInfo; +import com.volmit.stage.StageGraph; + +/** A built-in module whose settings class is held by a class of the host, not by a feature. */ +public final class HeldModule implements IrisModule { + @Override + public ModuleInfo info() { + return ModuleInfo.of("iris:held", "Held", "test"); + } + + @Override + public void install(StageGraph graph) {} + + @Override + public void register(FeatureRegistry registry) { + registry.registerHeldType(ClampSettings.class); + registry.registerHeldType(ClampSettings.class); + } +} diff --git a/src/test/java/com/volmit/host/PackSchemaTest.java b/src/test/java/com/volmit/host/PackSchemaTest.java index 00b6ab21f..fb2d8d971 100644 --- a/src/test/java/com/volmit/host/PackSchemaTest.java +++ b/src/test/java/com/volmit/host/PackSchemaTest.java @@ -4,6 +4,8 @@ import com.volmit.FeatureAttachment.Target; import com.volmit.config.Reference; import com.volmit.example.chunk.GrassModule; import com.volmit.example.chunk.TerrainModule; +import com.volmit.example.held.ClampSettings; +import com.volmit.example.held.HeldModule; import com.volmit.example.hosttypes.Palette; import com.volmit.example.placement.PlacementModules; import com.volmit.example.terrain.LayersModule; @@ -365,6 +367,30 @@ class PackSchemaTest { assertThrows(IllegalArgumentException.class, () -> NO_BUILT_INS.describingHostTypesIn("not a package")); } + @Test + void builtInModulesDescribeTheClassesTheHostsClassesHold() { + var schema = schemaOf(pack("held"), ModuleHost.of(List.of(new HeldModule()))); + + assertEquals(List.of(), schema.features()); + assertEquals(new ObjectType(ClampSettings.class.getName(), "Limits a placement by height", Optional.of("clamp"), + List.of(new SchemaOption("minimumHeight", new IntegerKind(), "The lowest height", false, OptionalDouble.of(-2048), + OptionalDouble.of(2048), OptionalInt.empty(), Optional.of(-2048L)))), schema.type(ClampSettings.class.getName())); + assertEquals(List.of(ClampSettings.class.getName()), schema.snippets().stream().map(ObjectType::name).toList()); + } + + @Test + void packModulesCannotRegisterHeldTypes() { + var jar = module("pack.flora.FloraModule", FLORA, + "public void register(com.volmit.FeatureRegistry registry) { registry.registerHeldType(pack.flora.Clamp.class); }", NO_STAGES) + .source("pack.flora.Clamp", "package pack.flora; public final class Clamp { private int minimum; }"); + var pack = pack("held-by-pack"); + jar.writeInto(pack, "flora.jar"); + + var failure = assertThrows(IllegalArgumentException.class, () -> LoadedPack.load(pack, NO_BUILT_INS)); + assertTrue(failure.getMessage().contains("Held type pack.flora.Clamp") && failure.getMessage().contains("only built-in modules"), + failure.getMessage()); + } + @Test void packModulesCannotUseHostTypes() { var host = NO_BUILT_INS.exposing(HOST_TYPES).describingHostTypesIn(HOST_TYPES);