mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 16:54:04 +00:00
Describe module settings for studio as LoadedPack.schema()
PackSchema (com.volmit.host.schema) is a JSON-free description of every catalogued settings type: features with their module, JSON field and targets, every reachable class once by name (cycles end at the name), options with value kind, description, required, bounds, defaults read from a no-argument instance and @Ref references, plus object generator settings and snippets. Everything is in a fixed order. The pack load fails, naming class, field and module, on unsupported field types, @Ref on fields that are not text, classes without a working no-argument constructor and snippet names that are not folder names or are used twice. GrassSettings.block becomes a BlockState, a block reference without @Ref.
This commit is contained in:
1 parent
93c755ffe4
commit
3465f8d390
19 files changed
+1302
-6
No files matched your search
@@ -0,0 +1,9 @@
|
||||
package com.volmit.host.schema;
|
||||
|
||||
/** Why a module catalogued a settings type. */
|
||||
public enum FeatureRole {
|
||||
/** Settings that stages read: named in {@code when(...)} or registered with {@code registerSettings}. */
|
||||
SETTINGS,
|
||||
/** Settings of an object generator, registered with {@code registerObject}. */
|
||||
OBJECT_GENERATOR
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.volmit.host.schema;
|
||||
|
||||
import com.volmit.FeatureAttachment;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/** A module's settings type and the JSON field it is attached to at each of its targets.
|
||||
*
|
||||
* @param id the feature id from {@code @Feature}
|
||||
* @param name the feature's display name from {@code @Feature}
|
||||
* @param module the module that owns the feature id
|
||||
* @param field the JSON field the settings are written under, the same at every target
|
||||
* @param targets where the field appears, in {@link FeatureAttachment.Target} order
|
||||
* @param settings the settings class; its description describes the field
|
||||
*/
|
||||
public record FeatureSchema(
|
||||
ResourceId id,
|
||||
String name,
|
||||
FeatureRole role,
|
||||
ModuleInfo module,
|
||||
String field,
|
||||
Set<FeatureAttachment.Target> targets,
|
||||
ObjectType settings) {
|
||||
public FeatureSchema {
|
||||
Objects.requireNonNull(id, "id");
|
||||
Objects.requireNonNull(name, "name");
|
||||
Objects.requireNonNull(role, "role");
|
||||
Objects.requireNonNull(module, "module");
|
||||
Objects.requireNonNull(field, "field");
|
||||
Objects.requireNonNull(settings, "settings");
|
||||
targets = Collections.unmodifiableSet(EnumSet.copyOf(targets));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.volmit.host.schema;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/** A settings class, described once per pack however many options use it.
|
||||
*
|
||||
* @param name the class's binary name, such as {@code com.example.moss.MossSettings}; unique in a pack
|
||||
* @param description the class's {@code @Desc}, or empty
|
||||
* @param snippet the class's {@code @Snippet} name: a pack may then write the object in its own file
|
||||
* under {@code snippet/<name>/} and refer to it by path
|
||||
* @param options the class's fields, including inherited ones, in name order
|
||||
*/
|
||||
public record ObjectType(String name, String description, Optional<String> snippet, List<SchemaOption> options) {
|
||||
public ObjectType {
|
||||
Objects.requireNonNull(name, "name");
|
||||
Objects.requireNonNull(description, "description");
|
||||
Objects.requireNonNull(snippet, "snippet");
|
||||
options = List.copyOf(options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.volmit.host.schema;
|
||||
|
||||
import com.volmit.FeatureAttachment;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Objects;
|
||||
|
||||
/** What the modules of a loaded pack add to the pack's JSON, for the host to render as JSON Schema and
|
||||
* studio completion. It holds module-provided entries only: the host describes its own fields.
|
||||
*
|
||||
* <p>Everything is in a fixed order, so the same modules always give the same description: features
|
||||
* by id, the features of a target by JSON field, types by name, snippets by snippet name, options by
|
||||
* name and enum constants in declaration order.
|
||||
*
|
||||
* @param features every catalogued settings type of the pack's modules, built-in ones included
|
||||
* @param types every class reachable from those settings, each once
|
||||
*/
|
||||
public record PackSchema(List<FeatureSchema> features, List<ObjectType> types) {
|
||||
public PackSchema {
|
||||
features = features.stream().sorted(Comparator.comparing(FeatureSchema::id)).toList();
|
||||
types = types.stream().sorted(Comparator.comparing(ObjectType::name)).toList();
|
||||
}
|
||||
|
||||
/** The module fields of one attachment target, by JSON field name. */
|
||||
public List<FeatureSchema> fields(FeatureAttachment.Target target) {
|
||||
Objects.requireNonNull(target, "target");
|
||||
return features.stream()
|
||||
.filter(feature -> feature.targets().contains(target))
|
||||
.sorted(Comparator.comparing(FeatureSchema::field))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/** The settings of object generators, by feature id. */
|
||||
public List<FeatureSchema> objectGenerators() {
|
||||
return features.stream().filter(feature -> feature.role() == FeatureRole.OBJECT_GENERATOR).toList();
|
||||
}
|
||||
|
||||
/** The type an {@link ValueKind.ObjectKind} names. */
|
||||
public ObjectType type(String name) {
|
||||
return types.stream()
|
||||
.filter(type -> type.name().equals(name))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new NoSuchElementException("No settings type: " + name));
|
||||
}
|
||||
|
||||
/** The types a pack may write as snippet files, by snippet name; each name belongs to one type. */
|
||||
public List<ObjectType> snippets() {
|
||||
return types.stream()
|
||||
.filter(type -> type.snippet().isPresent())
|
||||
.sorted(Comparator.comparing(type -> type.snippet().orElseThrow()))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.volmit.host.schema;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalDouble;
|
||||
|
||||
/** One option of an {@link ObjectType}: a field of a settings class.
|
||||
*
|
||||
* <p>{@code defaultValue} is the field's value in an instance made by the class's no-argument
|
||||
* constructor, as plain JSON-shaped Java values: {@code Boolean}, {@code Long} for integer kinds,
|
||||
* {@code Double} for decimal kinds, {@code String} for text, references and enum constants,
|
||||
* {@code List<Object>} for list kinds (sets sorted) and {@code Map<String, Object>} with sorted keys
|
||||
* for maps and objects, where an object holds its options that have defaults. It is empty when the
|
||||
* field is null, when a float or double is not finite, when a list or map holds a value without a
|
||||
* default and when a set holds objects, lists or maps. An object inside itself, such as a field
|
||||
* holding {@code this}, is left out of the outer object.
|
||||
*/
|
||||
public record SchemaOption(
|
||||
String name,
|
||||
ValueKind kind,
|
||||
String description,
|
||||
boolean required,
|
||||
OptionalDouble minimum,
|
||||
OptionalDouble maximum,
|
||||
Optional<Object> defaultValue) {
|
||||
public SchemaOption {
|
||||
Objects.requireNonNull(name, "name");
|
||||
Objects.requireNonNull(kind, "kind");
|
||||
Objects.requireNonNull(description, "description");
|
||||
Objects.requireNonNull(minimum, "minimum");
|
||||
Objects.requireNonNull(maximum, "maximum");
|
||||
Objects.requireNonNull(defaultValue, "defaultValue");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.volmit.host.schema;
|
||||
|
||||
/** How a string option is written. */
|
||||
public enum TextFormat {
|
||||
/** Any string: {@code String} fields. */
|
||||
PLAIN,
|
||||
/** {@code namespace:path}, as {@link com.volmit.resource.ResourceId} parses it. */
|
||||
RESOURCE_ID,
|
||||
/** {@code namespace:path}, optionally followed by {@code [name=value,...]}, as {@link com.volmit.block.BlockState#key()} writes it. */
|
||||
BLOCK_STATE
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.volmit.host.schema;
|
||||
|
||||
import com.volmit.config.Reference;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/** The JSON an option takes. Every settings field maps to exactly one kind; a field of any other type
|
||||
* fails the pack load, so the host never sees an untyped option.
|
||||
*/
|
||||
public sealed interface ValueKind {
|
||||
/** {@code true} or {@code false}: {@code boolean} and {@code Boolean} fields. */
|
||||
record BooleanKind() implements ValueKind {}
|
||||
|
||||
/** A whole number: {@code byte}, {@code short}, {@code int} and {@code long} fields and their boxes. */
|
||||
record IntegerKind() implements ValueKind {}
|
||||
|
||||
/** Any number: {@code float} and {@code double} fields and their boxes. */
|
||||
record DecimalKind() implements ValueKind {}
|
||||
|
||||
/** A string in {@code format} that names nothing in particular. */
|
||||
record TextKind(TextFormat format) implements ValueKind {
|
||||
public TextKind {
|
||||
Objects.requireNonNull(format, "format");
|
||||
}
|
||||
}
|
||||
|
||||
/** A string in {@code format} that names an entry of {@code reference}, such as a block. */
|
||||
record ReferenceKind(Reference reference, TextFormat format) implements ValueKind {
|
||||
public ReferenceKind {
|
||||
Objects.requireNonNull(reference, "reference");
|
||||
Objects.requireNonNull(format, "format");
|
||||
}
|
||||
}
|
||||
|
||||
/** One of the constant names of the enum class named {@code type}, in declaration order. */
|
||||
record EnumKind(String type, List<EnumConstant> constants) implements ValueKind {
|
||||
public EnumKind {
|
||||
Objects.requireNonNull(type, "type");
|
||||
constants = List.copyOf(constants);
|
||||
}
|
||||
}
|
||||
|
||||
/** A JSON array of {@code element} values: lists, arrays and, with {@code unique}, sets. */
|
||||
record ListKind(ValueKind element, boolean unique) implements ValueKind {
|
||||
public ListKind {
|
||||
Objects.requireNonNull(element, "element");
|
||||
}
|
||||
}
|
||||
|
||||
/** A JSON object with any string keys and {@code value} values: {@code Map<String, V>} fields. */
|
||||
record MapKind(ValueKind value) implements ValueKind {
|
||||
public MapKind {
|
||||
Objects.requireNonNull(value, "value");
|
||||
}
|
||||
}
|
||||
|
||||
/** A JSON object described by the {@link ObjectType} named {@code type} in {@link PackSchema#types()}.
|
||||
* Types are referenced by name, so a type that contains itself is described once.
|
||||
*/
|
||||
record ObjectKind(String type) implements ValueKind {
|
||||
public ObjectKind {
|
||||
Objects.requireNonNull(type, "type");
|
||||
}
|
||||
}
|
||||
|
||||
/** An enum constant and its {@code @Desc}, or an empty description. */
|
||||
record EnumConstant(String name, String description) {
|
||||
public EnumConstant {
|
||||
Objects.requireNonNull(name, "name");
|
||||
Objects.requireNonNull(description, "description");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user