diff --git a/CONTRACTS.md b/CONTRACTS.md index ebfe7092e..53d6ddb79 100644 --- a/CONTRACTS.md +++ b/CONTRACTS.md @@ -20,6 +20,7 @@ types that carry them and lists what the Iris adapter still owes. | Pack-load validation and diagnostics | host `StageGraphBuilder`, `ResolvedGraph`, `LoadedModules` | | Reference execution | host `PlacementRunner`, `ChunkRunner` | | Annotated settings | `IrisFeature`, `@Feature`, config annotations, `OptionsMetadata` | +| Settings described for studio | `@Desc`, `@Required`, `@MinNumber`, `@MaxNumber`, `@Snippet`, `@Ref`/`Reference`, `FeatureRegistry.registerSettings`, host `LoadedPack.schema()` (`PackSchema`, `FeatureSchema`, `ObjectType`, `SchemaOption`, `ValueKind`) | | Geometry and mantle registration | `FeatureRegistry`, host `ModuleRegistry`, `FeatureCatalog` | | Block states and palettes | `BlockState`, `BlockRegistry`, `BlockPalette` | | Pack-scoped resolution | `ResourceId`, `ResourceKey`, `PackResources` | @@ -41,6 +42,12 @@ namespaces the host reserves. A pack load works on copies of the module jars, so the originals are never held open. +Modules describe their settings only through their settings classes; jars ship no schema files. The +pack load turns every catalogued settings type into a `PackSchema` and fails, naming field, settings +and module, on a field type it cannot describe, a `@Ref` on a field that is not text, a class without +a working no-argument constructor or a bad or repeated snippet name. Defaults come from an instance +made by that constructor. The description is in a fixed order and holds module entries only. + Modules compile against the API `compileOnly` and never ship it. At runtime they can use the API (`com.volmit.*`), the host packages the host chooses to expose, the JDK and the classes in their own pack's jars. They may use any library they ship in the pack, never Iris's copy. JDK modules @@ -86,6 +93,16 @@ running Iris is the pack publisher's responsibility. packs and is part of what packs can break on. Stage output types and settings types that pack code uses through those stages must be in exposed packages too. The API is always exposed. - Consider showing `PackModules.missingClasses()` in studio. +- Render `pack.schema()` into studio's files: merge each target's `fields(target)` into the schema + of the matching Iris file type (dimension, region, biome, object placement), write `types()` as + definitions, a `/snippet/` schema per entry of `snippets()`, and completion lists per + `Reference` from the server registries and the pack's biome, region and object folders. Fail, do + not drop, when a module snippet name equals one of Iris's own. +- Refuse to open a pack in studio when its load fails, showing the error, rather than writing a + schema without the modules. +- Load the pack again and rebuild the schema files after a module jar in `/modules` changes. +- Create settings with their no-argument constructor and decode JSON into that instance, so an + omitted field keeps the default studio shows. ## Open diff --git a/DESIGN.md b/DESIGN.md index e7db1a287..94ab7e724 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -1,7 +1,8 @@ # Iris API design Status: pack module loading (`LoadedPack`), the stage graph, per-stage random streams, both -pipelines' reference executors, settings catalog, geometry, resources and typed storage contracts +pipelines' reference executors, settings catalog and its description for studio, geometry, +resources and typed storage contracts exist and are tested without the Iris engine. Provider and runtime families at the end are design notes, not Java interfaces. @@ -22,6 +23,8 @@ flowchart TD Register --> Catalog Resolve --> Placement[Placement graph] Resolve --> Chunk[Chunk graph] + Catalog --> Schema[PackSchema for studio] + Schema --> Render[Host JSON Schema and workspace] Catalog --> Decode[Host JSON decoding] Decode --> Configured[ConfiguredSettings per invocation] Configured --> Placement @@ -458,7 +461,10 @@ the host from publishing the chunk. Settings types implement `IrisFeature` and carry `@Feature` (namespaced id, display name, JSON field, attachment targets). `when(type)` validates the annotation while the module installs. The id -must be owned by a module naming the type (see Namespaces). +must be owned by a module naming the type (see Namespaces), or the owner registers the type itself +with `FeatureRegistry.registerSettings`. That is for settings no stage names, such as settings read +only through `FeatureSettings.at(...)`; registering a type a stage also names changes nothing, and a +type its owner registered may be named in `when(...)` by any module. `LoadedModules.load` catalogs those types with geometry generator settings and rejects duplicate ids and JSON field collisions per attachment target, including reserved host fields. `OptionsMetadata` reads field metadata; it does not decode JSON. @@ -471,10 +477,123 @@ The mantle contracts are unchanged: namespaced typed keys, versioned codecs, gen persistent lifetimes, ordered sparse visits and buffered edits. Only owners register a layer. Unknown persisted layers must be preserved or fail explicitly. +## Settings description for studio + +Studio needs a JSON Schema per pack file type and completion lists, and Iris writes both +(`.iris/schema/*.json`, the `.code-workspace` and IntelliJ's `jsonSchemas.xml`). Module settings +must appear there too. Module jars ship no schema: a schema written by the module's author would +describe the jar's build, not the pack that loads it, and would have to be kept in sync by hand. +IrisApi also writes no JSON Schema: it has no JSON library, and the host already renders its own +classes. The pack load instead collects what modules declare in their settings classes into a +`PackSchema` (`com.volmit.host.schema`), plain immutable Java values, and the host renders it. + +`LoadedModules.load` builds it right after freezing the catalog (`PackSchemaBuilder`), so a pack +that loads has a complete description and a settings class the host could not render fails the +load. It contains every catalogued settings type of every module, built-in ones too, because the +core engine is modules as well. The host's own JSON fields are not in it; reserved host fields +already keep module fields from colliding with them. + +### Shape + +- A `FeatureSchema` per catalogued type: feature id and name, role (`SETTINGS` for types named in + `when(...)` or registered with `registerSettings`, `OBJECT_GENERATOR` for `registerObject`), the + owning module's `ModuleInfo`, the JSON field, its targets and the settings `ObjectType`. + `fields(target)` lists a target's features; a feature attached to several targets appears under + each. The owner is the one module whose id owns the feature id; module ids are unique and contain + no `/`, so there is never more than one. +- An `ObjectType` per class reachable from module settings, keyed by binary class name: description, + `@Snippet` name and options. A field of a class type is an `ObjectKind` naming the type, never an + 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. +- A `SchemaOption` per field that `OptionsMetadata` selects, which means instance fields that are + not transient, inherited ones included. Each has its name, `ValueKind`, `@Desc`, `@Required`, + `@MinNumber`/`@MaxNumber` and the default value. + +### Value kinds + +`ValueKind` is sealed, so a host renderer that switches over it covers every case: + +| Field type | Kind | +| --- | --- | +| `boolean`, `Boolean` | `BooleanKind` | +| `byte`, `short`, `int`, `long` and boxes | `IntegerKind` | +| `float`, `double` and boxes | `DecimalKind` | +| `String` | `TextKind(PLAIN)`, or `ReferenceKind(reference, PLAIN)` with `@Ref` | +| `ResourceId` | `TextKind(RESOURCE_ID)`, or `ReferenceKind(reference, RESOURCE_ID)` with `@Ref` | +| `BlockState` | `ReferenceKind(BLOCK, BLOCK_STATE)` | +| an enum | `EnumKind`: class name and constants in declaration order, each with its `@Desc` | +| `List`, `E[]` | `ListKind(E, false)` | +| `Set` | `ListKind(E, true)` | +| `Map` | `MapKind(V)` | +| any other concrete class | `ObjectKind(class name)` | + +Everything else fails the load with the field, its settings and its module named, and what fits +instead: + +- `char` and `Character`: no settings use them, and JSON has no character type; +- JDK classes such as `UUID`, `Optional`, `BigDecimal` or `Object`, meaning classes of named modules + in the boot layer: the host would need a format for each; +- `ResourceKey`: its type argument is the Java class a resource loads as, which tells studio + nothing it could list; `ResourceId` with `@Ref` says what a value names; +- interfaces and abstract classes: their fields depend on the subclass a pack would pick; +- inner (non-static), local and anonymous classes, which cannot be created on their own; +- raw `List`, `Set` and `Map`, concrete collections such as `ArrayList`, wildcards and type + variables, other generic classes, and maps whose keys are not `String`. + +Only `List`, `Set` and `Map` are accepted as collections. They are what the host can decode without +choosing an implementation class, and accepting `ArrayList` or `Collection` would add cases without +adding anything a pack can write. + +### References + +`@Ref(Reference.X)` says what a text value names. The kinds are a closed enum of what the Iris host +can list today through its `RegistryList*` annotations: blocks, items, entities, platform biomes, +structures, potion effects and enchantments from the server registries, and biomes, regions and +objects of the pack, the resource folders Iris's own settings refer to most. More kinds come when a +module needs one and the host can serve it. `@Ref` fits `String` and `ResourceId` and lists, sets and +arrays of them, where it applies to the elements. On any other field, a map included, it fails the +load. `BlockState` always names a block, so its fields are block references without `@Ref`; +`@Ref(BLOCK)` there is accepted, any other kind fails. + +### Defaults + +Every described class needs a no-argument constructor, of any visibility. The builder creates one +instance of each class with it and reads the fields of that instance as the defaults. The +alternatives were worse: reading no defaults leaves studio without the values a pack gets when it +leaves a field out, and parsing initializers from class files cannot see values a constructor sets. +Running module code is not new at this point: the load has already run the module's constructor, +`info()`, `install` and `register`. The host creates settings this way anyway before decoding JSON +into them, so a constructor that would fail here would fail on every decode: the load fails with the +constructor's exception, and a class without the constructor fails too, rather than describing +settings the host cannot create. + +Defaults are JSON-shaped: `Boolean`, `Long`, `Double`, `String` (text, references as written, +`BlockState.key()`, enum constant names), `List` and `Map` with sorted keys. +An object default is the map of its options that have defaults, read through the declared class's +options. A float keeps its own shortest decimal form (`0.1f` is `0.1`). There is no default for a +null field, a NaN or infinite number, a list or map with an element without one, or a set of +objects, lists or maps, which has no order to write; sets of scalars are sorted. An object inside +itself, such as a field holding `this`, is left out of the outer object. + +### Snippets + +`@Snippet("name")` on a class lets a pack write that object in its own file under `snippet/name/`. +`snippets()` lists those types. A snippet name must be a folder name of lowercase letters, digits, `-` +and `_`, and it must belong to one class; otherwise the load fails naming both classes. + +### Stable order + +The host writes schema files from this, so the order is fixed: features by id, a target's features +by JSON field, types by class name, snippets by snippet name, options by field name and enum +constants in declaration order. Options are sorted by name because `Class.getDeclaredFields()` +promises no order. Which settings an error names first follows feature id order too, so the same +pack always fails with the same message. + ## Not implemented -- Iris host wiring: JSON decoding and validation, schema generation, resource loaders, platform - block adapters, storage and codecs. +- Iris host wiring: JSON decoding and validation, rendering `PackSchema` into Iris's schema and + workspace files, resource loaders, platform block adapters, storage and codecs. - Reach. The old per-modifier `writeRadius` and `mantleRadius` are gone with the modifiers. The host must prepare the whole area a pipeline may touch. Per-stage reach is an open question. - Legacy random compatibility. Existing Iris features draw from shared, positional generators in a diff --git a/README.md b/README.md index 32849bdf1..cfb0d8a98 100644 --- a/README.md +++ b/README.md @@ -46,11 +46,12 @@ public final class MossModule implements IrisModule { draws. A block stage has one stream per placement, drawn across the blocks of its pass. The stream is a `WorldRandom` (see Random streams below). -`IrisModule.register(FeatureRegistry)` stays for non-stage contributions: geometry generators -and mantle layers. Their feature ids and mantle keys are the module id or start with it and `/`, -like stage ids: `iris:snow` or `iris:snow/depth` for module `iris:snow`. A settings type named in -`when(...)` needs a feature id that at least one module naming it owns in that sense, so another -module may still condition its stages on it, as moss does on `iris:snow`. +`IrisModule.register(FeatureRegistry)` stays for non-stage contributions: geometry generators, +mantle layers and settings no stage names (see Settings for studio). Their feature ids and mantle +keys are the module id or start with it and `/`, like stage ids: `iris:snow` or `iris:snow/depth` +for module `iris:snow`. A settings type named in `when(...)` needs a feature id that at least one +module naming it owns in that sense, or its owner must have registered it, so another module may +still condition its stages on it, as moss does on `iris:snow`. ## Random streams @@ -127,6 +128,58 @@ Iris never downloads modules and does not check which API version a jar was buil ships its module jars, and keeping them compatible with the Iris version it targets is the pack publisher's job. +## Settings for studio + +Modules describe their settings in the settings classes themselves. Module jars ship no JSON Schema +or workspace files: the pack load collects the description, and Iris writes the studio schema and +completion from it. + +```java +@Desc("Plants flowers and bushes") +@Feature(id = "example:flora", name = "Flora", field = "flora", targets = Target.BIOME) +public final class FloraSettings implements IrisFeature { + @Desc("Plants per chunk") @Required @MinNumber(0) @MaxNumber(64) + private int density = 4; // default 4 + + @Ref(Reference.PACK_BIOME) + private List biomes = List.of(); // completes the pack's biomes + + private BlockState flower = new BlockState("minecraft:poppy"); // completes blocks without @Ref + private Season season = Season.SPRING; // completes the constant names + private Patch patch = new Patch(); // a class of the module, described once +} +``` + +- `@Desc` on a class, field or enum constant is its description. `@Required`, `@MinNumber` and + `@MaxNumber` are checks for the host to render. +- Every settings class, nested ones included, needs a no-argument constructor, which may be private. + The host creates settings with it, and the field values of that instance are the defaults studio + shows. +- Field types: `boolean`, primitive or boxed numbers, `String`, `ResourceId`, `BlockState`, enums, + `List`, `Set` and arrays of these, `Map`, and concrete classes, which are described the + same way. `@Snippet("name")` on a class lets packs write it as a file under `snippet/name/`. + Anything else fails the pack load: `char`, JDK classes such as `UUID` or `Optional`, `ResourceKey`, + interfaces and abstract classes, inner (non-static) classes, collections without type arguments, + concrete collections such as `ArrayList`, and maps with other keys. +- `@Ref(Reference.X)` says what a `String` or `ResourceId` names, also on lists, sets and arrays of + them, so studio offers the right list: `BLOCK`, `ITEM`, `ENTITY`, `BIOME`, `STRUCTURE`, + `POTION_EFFECT` or `ENCHANTMENT` from the server's registries, or `PACK_BIOME`, `PACK_REGION` or + `PACK_OBJECT` of the pack. `BlockState` fields name blocks without it. `@Ref` on any other field + fails the load. +- Settings named in `when(...)` and object generator settings are described automatically. Settings + 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 load error names the field, the settings and the module: + +``` +Field com.example.flora.FloraSettings.id of settings example:flora (module example:flora from modules/flora.jar) uses java.util.UUID, a JDK class settings cannot use; settings fields can be booleans, numbers, String, ResourceId, BlockState, enums, lists, sets and arrays of these, Map with String keys and classes with a no-argument constructor +Field com.example.flora.FloraSettings.count of settings example:flora (module example:flora from modules/flora.jar) has @Ref(BLOCK) on int, but @Ref fits only String and ResourceId and lists, sets and arrays of them +Class com.example.flora.Shape of settings example:flora (module example:flora from modules/flora.jar) has no no-argument constructor; the host creates settings with it and reads their defaults from it, so declare one, private if nothing else should use it +Snippet name 'patch' is used by com.example.flora.Patch of settings example:flora (module example:flora from modules/flora.jar) and by com.example.flora.Plot of settings example:flora (module example:flora from modules/flora.jar); studio describes snippet/patch/ by one class, so give each its own name +``` + ## Host side ```java @@ -151,8 +204,8 @@ try (var pack = LoadedPack.load(packRoot, host)) { once, here), the host packages pack modules may use, the JSON fields the host itself uses and the reserved namespaces. Each method adds to what earlier calls configured. Package names, namespaces and reserved field names are checked when it is built. `LoadedPack.load` -loads the pack's jars, installs them after the built-ins, catalogs settings and registrations and -resolves both pipelines. It returns a complete pack or throws with nothing left open. `close()` +loads the pack's jars, installs them after the built-ins, catalogs settings and registrations, +describes them for studio and resolves both pipelines. It returns a complete pack or throws with nothing left open. `close()` releases the pack's class loader and its jar copies. `PackModules` and `LoadedModules`, which it uses, stay available for tests and special cases. @@ -222,7 +275,8 @@ Pack overworld uses 2 modules: A pack without jars logs `Pack overworld uses no modules: no jars in its modules directory`. `LoadedModules.load` installs every module, catalogs the settings types named in `when(...)` -together with geometry and mantle registrations, and resolves both pipelines. Graph errors +together with explicitly registered settings and geometry and mantle registrations, describes them +and resolves both pipelines. Graph errors (missing requirement, cycle, a stage outside its module's id, duplicate, cross-pipeline edge) fail here, at pack load, and so do reserved namespaces and feature ids or mantle keys outside the module's id. Errors about one module name its jar or "host classpath", and so does a module @@ -236,6 +290,36 @@ Mantle layer iris:depth must be example:moss or start with example:moss/, the id `describe()` prints the order, owners, edge kinds, fused block passes and the stage pairs that have no path between them. +`pack.schema()` is the `PackSchema` of the pack's modules, built-in ones included: plain Java values, +no JSON. The host renders it and merges it with its own fields: + +- `fields(target)`: the module features attached to a target, by JSON field. Each `FeatureSchema` has + the feature id and name, its role (`SETTINGS` or `OBJECT_GENERATOR`), the owning module's + `ModuleInfo`, the field, its targets and the settings `ObjectType`. +- `types()`: every class reachable from module settings, once each, by name. An `ObjectType` has the + class name, description, snippet name and options. A `SchemaOption` has name, `ValueKind`, + description, required, minimum, maximum and default value. +- `ValueKind` is sealed: `BooleanKind`, `IntegerKind`, `DecimalKind`, `TextKind(format)`, + `ReferenceKind(reference, format)`, `EnumKind(type, constants)`, `ListKind(element, unique)`, + `MapKind(value)` and `ObjectKind(type)`, which names an entry of `types()`. `TextFormat` is `PLAIN`, + `RESOURCE_ID` or `BLOCK_STATE`. +- `snippets()` and `objectGenerators()` pick the types with a snippet name and the object generator + features. + +For the stilt example the host sees one `OBJECT_PLACEMENT` field. The records are abridged: + +``` +FeatureSchema[id=iris:stilt, name=Stilt, role=SETTINGS, module=Stilts (iris:stilt), field=stilt, targets=[OBJECT_PLACEMENT], + settings=ObjectType[name=com.volmit.example.placement.StiltSettings, description=Fills the space below an object down to the ground, + snippet=Optional.empty, options=[SchemaOption[name=maxDepth, kind=IntegerKind[], description=Maximum blocks filled below each + lowest object block, required=false, minimum=OptionalDouble[1.0], maximum=OptionalDouble.empty, defaultValue=Optional[16]]]]] +``` + +Everything is in a fixed order, so the same modules write the same schema files on every run. The +host refuses to open a pack in studio when its load fails, and loads the pack again, rebuilding the +schema, after a module jar changes. + + `PlacementRunner` owns the edit for one attempt. It commits once if no stage rejects or throws and closes the edit in every case. Stages get buffered `PlacementAccess` without commit rights. @@ -249,7 +333,8 @@ and closes the edit in every case. Stages get buffered `PlacementAccess` without with a world position using checked arithmetic. - `Hunk` is a dense local volume; `ArrayHunk` is the bounds-checked reference. - The `mantle` package keeps namespaced typed layers, codecs, lifetimes and buffered edits. -- `IrisFeature`, `@Feature`, the config annotations and `OptionsMetadata` describe settings. +- `IrisFeature`, `@Feature`, the config annotations (`@Desc`, `@Required`, `@MinNumber`, + `@MaxNumber`, `@Snippet`, `@Ref`) and `OptionsMetadata` describe settings. Invocation views and random streams expire when the invocation ends. Stage bodies are shared by concurrent invocations and must not keep views, streams or mutable state. @@ -268,6 +353,11 @@ and the derivation is pinned to values computed from its specification (`StageSt `WorldRandomTest` pins every `WorldRandom` method, bounds and edge cases to values computed by an independent Python reference (`python3 -B src/test/reference/world_random.py`; the build does not need Python) and fails if the type gains an unpinned method or implements `RandomGenerator`. +`PackSchemaTest` loads module jars and checks their description: fields under their targets, +descriptions, required, bounds, defaults (floats, nulls, sets, maps, objects, a field holding `this`), +enums, collections, references, nested classes with a cycle, snippets, object generator settings, +explicit registration, built-in modules, the same result whatever the jar order, and the load error +for each unsupported field type, misplaced `@Ref`, unusable class and bad or repeated snippet name. `DiscoveredModuleTest` checks that `info()` is read once. `LoadedPackTest` loads a pack with built-ins and checks that a failed load and `close()` leave nothing open. `PackJarSnapshotTest` checks that classes come from the copies, that the original can be replaced while loaded, that copies go