Read settings per column with every level as its own entry, add the mantle pipeline, host types and the one-option shorthand

Settings no longer resolve to one value per type. FeatureSettings.at(type, x, z) returns one entry per
level that declares the type at that column (dimension, region, surface biome, placement), each with
its level, source and value; levels stack and a module decides what several mean. Conditional stage
bodies receive every configured entry. Chunk and mantle runners collect a chunk's settings themselves,
so a stage runs when any column of the chunk configures it.

Work that writes the mantle or reaches beyond its chunk moves to a new MANTLE pipeline (Stage.mantle,
MantleRunner) that the host runs around a chunk before it generates. Chunk stages may no longer declare
a reach or mantle writes and see the mantle read-only.

Built-in settings may hold host types the host describes itself (ModuleHost.describingHostTypesIn,
ValueKind.HostKind); pack modules cannot. FeatureSchema.shorthand() names the only option of settings
packs may write as a bare value.
This commit is contained in:
piexel committed 2026-10-08 11:31:03 +02:00
1 parent 09df1af88e
commit 97ac2a2982
61 files changed
+1316 -184

No files matched your search

@@ -4,23 +4,35 @@ import com.volmit.FeatureAttachment;
import com.volmit.IrisFeature;
import com.volmit.resource.ResourceId;
import java.util.List;
import java.util.Objects;
/** Invocation-scoped lookup of applicable settings by attachment and world position.
* Results are immutable lists in pack declaration order, after the host applies inheritance
* rules. Targets remain separate: this interface never silently merges dimension/region/biome
* values. OBJECT_PLACEMENT is read from the current placement, or returns an empty list.
* Values must be treated as read-only. Bounds and lifetime match the invocation's sampler.
/** Invocation-scoped lookup of the settings that apply at one world column.
*
* <p>Every level that declares the settings contributes its own entry: the dimension, the region and
* the biome of the column, and in a placement the placement itself. Levels stack: nothing is merged or
* overridden, and a module decides what several entries mean, such as running a deposit once per
* entry. Entries are immutable lists in level order ({@link FeatureAttachment.Target} order). The
* biome is the column's surface biome; cave biomes are not a level. Values must be treated as
* read-only. Bounds and lifetime match the invocation's sampler.
*
* <p>Something that spans several columns, such as a chunk-wide pass or a placed object, reads the
* settings at one column it treats as its origin; per-chunk behaviour reads the chunk's centre column
* (local 7, 7).
*/
public interface FeatureSettings {
<F extends IrisFeature> List<Entry<F>> at(Class<F> featureType, FeatureAttachment.Target target,
int worldX, int worldY, int worldZ);
<F extends IrisFeature> List<Entry<F>> at(Class<F> featureType, int worldX, int worldZ);
/** source identifies the configuration owner; occurrence is unique within the pack snapshot. */
record Entry<F extends IrisFeature>(ResourceId source, ResourceId occurrence, F value) {
/** One level's declaration of the settings.
*
* @param level where the settings are declared: the dimension, a region, a biome or the placement
* @param source the resource that declares them, such as {@code overworld:biome/desert/dunes}
* @param value the decoded settings
*/
record Entry<F extends IrisFeature>(FeatureAttachment.Target level, ResourceId source, F value) {
public Entry {
java.util.Objects.requireNonNull(source, "source");
java.util.Objects.requireNonNull(occurrence, "occurrence");
java.util.Objects.requireNonNull(value, "value");
Objects.requireNonNull(level, "level");
Objects.requireNonNull(source, "source");
Objects.requireNonNull(value, "value");
}
}
}
@@ -15,7 +15,9 @@ public interface GenerationContext extends InvocationContext {
TerrainSampler terrain();
FeatureSettings settings();
/** Non-null mantle access scoped to this invocation; the host owns its lifetime. */
/** Non-null mantle access scoped to this invocation; the host owns its lifetime. Chunk stages read it
* only: the mantle pipeline has finished around the chunk, and writes throw.
*/
Mantle mantle();
/** The host-owned local volume of the given value type, shared by all stages of this
@@ -0,0 +1,21 @@
package com.volmit.generation;
import com.volmit.config.FeatureSettings;
import com.volmit.mantle.Mantle;
import com.volmit.world.TerrainSampler;
/** Per-invocation inputs of the mantle pipeline for one chunk. Must not be retained after execution. */
public interface MantleGenerationContext extends InvocationContext {
/** The chunk's first block along X, in world coordinates. */
int originX();
/** The chunk's first block along Z, in world coordinates. */
int originZ();
TerrainSampler terrain();
FeatureSettings settings();
/** The mantle of the chunk widened by the pipeline's reach, full height. Writes outside it throw. */
Mantle mantle();
}
@@ -11,7 +11,7 @@ import com.volmit.stage.Stage;
import com.volmit.world.TerrainSampler;
import java.util.Objects;
/** The context every stage of one chunk invocation receives. */
/** The context every stage of one chunk invocation receives. The mantle is read-only. */
final class ChunkInvocation implements ChunkStageContext {
private final StageInvocation stages;
private final GenerationContext inputs;
@@ -57,7 +57,7 @@ final class ChunkInvocation implements ChunkStageContext {
@Override
public Mantle mantle() {
return inputs.mantle();
return new ReadOnlyMantle(inputs.mantle());
}
@Override
+10 -4
View File
@@ -6,7 +6,7 @@ import java.util.Objects;
/** Reference executor of a resolved chunk graph: active stages run once, in resolved order.
* It does not schedule neighbors or roll back hunks; a failed invocation must prevent the host
* from publishing the chunk.
* from publishing the chunk. Stages see the mantle read-only.
*/
public final class ChunkRunner {
private final ResolvedGraph graph;
@@ -18,10 +18,16 @@ public final class ChunkRunner {
}
}
/** Runs one chunk. The host derives {@code invocationSeed} from the world seed and the chunk
* position; each stage draws from its own stream of that seed ({@link StageStreams}).
/** Runs one chunk. A conditional stage is active when any of the chunk's 16 by 16 columns has an
* entry of its settings in {@code inputs.settings()}, and receives every distinct entry of those
* columns. The host derives {@code invocationSeed} from the world seed and the chunk position; each
* stage draws from its own stream of that seed ({@link StageStreams}).
*/
public void generate(GenerationContext inputs, long invocationSeed, ConfiguredSettings settings) {
public void generate(GenerationContext inputs, long invocationSeed) {
if (graph.size() == 0) {
return;
}
var settings = ChunkSettings.collect(graph, inputs::settings, inputs.originX(), inputs.originZ());
var chunk = new ChunkInvocation(new StageInvocation(graph, invocationSeed, settings), inputs);
for (int position = 0; position < graph.size(); position++) {
chunk.stages().runInvocationStage(position, chunk);
@@ -0,0 +1,51 @@
package com.volmit.host;
import com.volmit.FeatureAttachment;
import com.volmit.IrisFeature;
import com.volmit.config.FeatureSettings;
import com.volmit.config.FeatureSettings.Entry;
import com.volmit.resource.ResourceId;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.function.Supplier;
/** The settings one chunk's stages see: for every settings type the graph conditions on, each distinct
* entry of the chunk's 16 by 16 columns. A stage is therefore active when any column of the chunk
* configures its settings, also where a biome border crosses the chunk. Entries are distinct by level
* and source and keep the order their first column was read in, columns by X and then Z.
*/
final class ChunkSettings {
/** The chunk's width along X and Z, in blocks: the footprint of chunk and mantle stages. */
static final int CHUNK_WIDTH = 16;
private ChunkSettings() {}
/** {@code settings} is asked only when the graph conditions a stage on settings. */
static ConfiguredSettings collect(ResolvedGraph graph, Supplier<FeatureSettings> settings, int originX, int originZ) {
if (graph.settingsTypes().isEmpty()) {
return ConfiguredSettings.none();
}
var view = settings.get();
var entries = new ArrayList<Entry<?>>();
for (var type : graph.settingsTypes()) {
entries.addAll(distinctEntries(type.asSubclass(IrisFeature.class), view, originX, originZ));
}
return ConfiguredSettings.of(entries);
}
private static Collection<Entry<?>> distinctEntries(
Class<? extends IrisFeature> type, FeatureSettings view, int originX, int originZ) {
var distinct = new LinkedHashMap<Declaration, Entry<?>>();
for (int x = 0; x < CHUNK_WIDTH; x++) {
for (int z = 0; z < CHUNK_WIDTH; z++) {
for (var entry : view.at(type, originX + x, originZ + z)) {
distinct.putIfAbsent(new Declaration(entry.level(), entry.source()), entry);
}
}
}
return distinct.values();
}
private record Declaration(FeatureAttachment.Target level, ResourceId source) {}
}
@@ -1,41 +1,66 @@
package com.volmit.host;
import com.volmit.FeatureAttachment;
import com.volmit.IrisFeature;
import com.volmit.config.FeatureSettings.Entry;
import com.volmit.resource.ResourceId;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/** Feature settings configured for one invocation: the placement's settings, or the settings
* the host resolved for a chunk. At most one value per type; merging and precedence between
* dimension/region/biome/placement declarations stay host policy. Build once and reuse it for
* every invocation with the same configuration.
/** The settings entries configured for one invocation, per settings type, in level order. A stage
* conditioned on a type is active when the type has at least one entry, and its body receives them
* all. For a placement these are the placement's own declarations; for a chunk the runners collect
* every distinct entry of the chunk's columns themselves. Build once and reuse it for every invocation
* with the same configuration.
*/
public final class ConfiguredSettings {
private static final ConfiguredSettings NONE = new ConfiguredSettings(Map.of());
private static final Comparator<Entry<?>> LEVEL_ORDER = Comparator.comparing(Entry::level);
private final Map<Class<?>, IrisFeature> values;
private final Map<Class<?>, List<Entry<?>>> entries;
private ConfiguredSettings(Map<Class<?>, IrisFeature> values) {
this.values = Map.copyOf(values);
private ConfiguredSettings(Map<Class<?>, List<Entry<?>>> entries) {
this.entries = Map.copyOf(entries);
}
public static ConfiguredSettings none() {
return NONE;
}
public static ConfiguredSettings of(IrisFeature... settings) {
var values = new HashMap<Class<?>, IrisFeature>();
for (var value : settings) {
Objects.requireNonNull(value, "settings");
if (values.putIfAbsent(value.getClass(), value) != null) {
throw new IllegalArgumentException("Settings configured twice: " + value.getClass().getName());
}
/** These entries, grouped by settings type, each group in level order and otherwise as given. */
public static ConfiguredSettings of(List<? extends Entry<?>> entries) {
var grouped = new HashMap<Class<?>, List<Entry<?>>>();
for (var entry : entries) {
Objects.requireNonNull(entry, "entry");
grouped.computeIfAbsent(entry.value().getClass(), ignored -> new ArrayList<>()).add(entry);
}
return new ConfiguredSettings(values);
grouped.replaceAll((type, group) -> group.stream().sorted(LEVEL_ORDER).toList());
return grouped.isEmpty() ? NONE : new ConfiguredSettings(grouped);
}
public <S extends IrisFeature> Optional<S> find(Class<S> type) {
return Optional.ofNullable(type.cast(values.get(type)));
/** One placement's own settings, at most one value per type, declared by {@code source}. */
public static ConfiguredSettings placement(ResourceId source, IrisFeature... settings) {
Objects.requireNonNull(source, "source");
var entries = new ArrayList<Entry<?>>();
var types = new HashMap<Class<?>, IrisFeature>();
for (var value : settings) {
Objects.requireNonNull(value, "settings");
if (types.putIfAbsent(value.getClass(), value) != null) {
throw new IllegalArgumentException("Settings configured twice: " + value.getClass().getName());
}
entries.add(new Entry<>(FeatureAttachment.Target.OBJECT_PLACEMENT, source, value));
}
return of(entries);
}
/** The entries of {@code type}, in level order; empty when nothing configures it. */
@SuppressWarnings("unchecked") // Entries are grouped by the class of their value.
public <S extends IrisFeature> List<Entry<S>> find(Class<S> type) {
var found = entries.get(type);
return found == null ? List.of() : (List<Entry<S>>) (List<?>) found;
}
}
@@ -5,6 +5,7 @@ import com.volmit.host.schema.ValueKind;
import com.volmit.host.schema.ValueKind.BooleanKind;
import com.volmit.host.schema.ValueKind.DecimalKind;
import com.volmit.host.schema.ValueKind.EnumKind;
import com.volmit.host.schema.ValueKind.HostKind;
import com.volmit.host.schema.ValueKind.IntegerKind;
import com.volmit.host.schema.ValueKind.ListKind;
import com.volmit.host.schema.ValueKind.MapKind;
@@ -45,6 +46,7 @@ final class DefaultValues {
case ListKind list -> list(elements(value), list);
case MapKind map -> map((Map<?, ?>) value, map.value());
case ObjectKind object -> object(value, types.get(object.type()));
case HostKind _ -> Optional.empty();
};
}
@@ -0,0 +1,32 @@
package com.volmit.host;
import com.volmit.PackageName;
import java.util.Collection;
import java.util.List;
/** Classes the host describes in studio itself, such as Iris's palettes and noise styles, which built-in
* settings may hold. Given as packages, each with its subpackages. Pack modules cannot use them.
*/
final class HostTypes {
static final HostTypes NONE = new HostTypes(List.of());
private final List<PackageName> packages;
private HostTypes(List<PackageName> packages) {
this.packages = packages;
}
static HostTypes of(Collection<String> packages) {
return new HostTypes(packages.stream().map(HostTypes::parse).toList());
}
boolean contains(Class<?> type) {
var name = type.getName();
return packages.stream().anyMatch(packageName -> packageName.contains(name));
}
private static PackageName parse(String name) {
return PackageName.tryParse(name).orElseThrow(() -> new IllegalArgumentException(
"Host type package '" + name + "' is not a package name, such as art.arcane.iris.engine.object"));
}
}
@@ -13,12 +13,14 @@ import java.util.Set;
* settings types stages are conditioned on), its description for studio and one resolved graph per
* pipeline.
*/
public record LoadedModules(FeatureCatalog catalog, PackSchema schema, ResolvedGraph placement, ResolvedGraph chunk) {
public record LoadedModules(
FeatureCatalog catalog, PackSchema schema, ResolvedGraph placement, ResolvedGraph chunk, ResolvedGraph mantle) {
public LoadedModules {
Objects.requireNonNull(catalog, "catalog");
Objects.requireNonNull(schema, "schema");
Objects.requireNonNull(placement, "placement");
Objects.requireNonNull(chunk, "chunk");
Objects.requireNonNull(mantle, "mantle");
}
/** Pass the built-in modules together with {@link PackModules#modules()} of the pack.
@@ -26,11 +28,14 @@ public record LoadedModules(FeatureCatalog catalog, PackSchema schema, ResolvedG
* @param reservedHostFields JSON fields the host itself uses, per attachment target
* @param reservedNamespaces namespaces only built-in modules may use, such as {@code iris}; a
* pack module whose id uses one fails the load
* @param hostTypePackages packages whose classes the host describes itself, each with its
* subpackages; only built-in modules' settings may use them (see {@link ModuleHost#describingHostTypesIn})
*/
public static LoadedModules load(
List<DiscoveredModule> modules,
Map<FeatureAttachment.Target, Set<String>> reservedHostFields,
Collection<String> reservedNamespaces) {
Collection<String> reservedNamespaces,
Collection<String> hostTypePackages) {
var reserved = ReservedNamespaces.of(reservedNamespaces);
modules.forEach(reserved::requireAllowed);
var registry = new ModuleRegistry(reservedHostFields);
@@ -40,12 +45,13 @@ public record LoadedModules(FeatureCatalog catalog, PackSchema schema, ResolvedG
}
stages.settingsTypes(registry::registeredByOwner).forEach(registry::registerStageSettings);
var catalog = registry.freeze();
var schema = PackSchemaBuilder.describe(catalog, modules);
var schema = PackSchemaBuilder.describe(catalog, modules, HostTypes.of(hostTypePackages));
var placement = stages.resolve(Pipeline.PLACEMENT);
var chunk = stages.resolve(Pipeline.CHUNK);
var mantle = stages.resolve(Pipeline.MANTLE);
requireRegisteredMantleLayers(catalog, placement);
requireRegisteredMantleLayers(catalog, chunk);
return new LoadedModules(catalog, schema, placement, chunk);
requireRegisteredMantleLayers(catalog, mantle);
return new LoadedModules(catalog, schema, placement, chunk, mantle);
}
/** A stage's reach names the layers it writes; a layer nobody registered has no codec or lifetime. */
@@ -29,7 +29,7 @@ public final class LoadedPack implements AutoCloseable {
this.loaded = loaded;
}
/** Loads the pack's module jars, installs them with the host's built-in modules and resolves both
/** Loads the pack's module jars, installs them with the host's built-in modules and resolves the
* pipelines. Fails with the first load error, naming the jar or module at fault.
*/
public static LoadedPack load(Path packRoot, ModuleHost host) {
@@ -37,7 +37,7 @@ public final class LoadedPack implements AutoCloseable {
var packModules = PackModules.load(packRoot, host.exposedHostPackages(), host.jarCopyRoot(), host.logger());
try {
var loaded = LoadedModules.load(host.withPackModules(packModules.modules()),
host.reservedHostFields(), host.reservedNamespaces());
host.reservedHostFields(), host.reservedNamespaces(), host.hostTypePackages());
return new LoadedPack(packModules, loaded);
} catch (RuntimeException | Error failure) {
closeAfterFailure(packModules, failure);
@@ -74,6 +74,10 @@ public final class LoadedPack implements AutoCloseable {
return loaded.chunk();
}
public ResolvedGraph mantle() {
return loaded.mantle();
}
@Override
public void close() {
packModules.close();
@@ -0,0 +1,71 @@
package com.volmit.host;
import com.volmit.config.FeatureSettings;
import com.volmit.generation.MantleGenerationContext;
import com.volmit.mantle.Mantle;
import com.volmit.random.WorldRandom;
import com.volmit.resource.PackResources;
import com.volmit.stage.MantleStageContext;
import com.volmit.stage.Stage;
import com.volmit.world.TerrainSampler;
import java.util.Objects;
/** The context every stage of one mantle invocation receives. */
final class MantleInvocation implements MantleStageContext {
private final StageInvocation stages;
private final MantleGenerationContext inputs;
MantleInvocation(StageInvocation stages, MantleGenerationContext inputs) {
this.stages = stages;
this.inputs = Objects.requireNonNull(inputs, "inputs");
}
StageInvocation stages() {
return stages;
}
@Override
public <O> O output(Stage<O> stage) {
return stages.output(stage);
}
@Override
public int originX() {
return inputs.originX();
}
@Override
public int originZ() {
return inputs.originZ();
}
@Override
public TerrainSampler terrain() {
return inputs.terrain();
}
@Override
public FeatureSettings settings() {
return inputs.settings();
}
@Override
public Mantle mantle() {
return inputs.mantle();
}
@Override
public long worldSeed() {
return inputs.worldSeed();
}
@Override
public PackResources resources() {
return inputs.resources();
}
@Override
public WorldRandom random() {
return stages.random();
}
}
@@ -0,0 +1,36 @@
package com.volmit.host;
import com.volmit.generation.MantleGenerationContext;
import com.volmit.stage.Pipeline;
import java.util.Objects;
/** Reference executor of a resolved mantle graph: active stages run once per chunk, in resolved order.
* The host runs it for every chunk within the pipeline's reach of a chunk before that chunk generates,
* each chunk once, and gives it the mantle around the chunk as far as that reach. It does not roll back
* mantle writes; a failed invocation must stop the host from generating the chunks it reaches.
*/
public final class MantleRunner {
private final ResolvedGraph graph;
public MantleRunner(ResolvedGraph graph) {
this.graph = Objects.requireNonNull(graph, "graph");
if (graph.pipeline() != Pipeline.MANTLE) {
throw new IllegalArgumentException("Expected a mantle graph, got " + graph.pipeline());
}
}
/** Runs the mantle stages of one chunk. A conditional stage is active when any of the chunk's
* 16 by 16 columns has an entry of its settings, and receives every distinct entry of those columns.
* The host derives {@code invocationSeed} from the world seed and the chunk position.
*/
public void generate(MantleGenerationContext inputs, long invocationSeed) {
if (graph.size() == 0) {
return;
}
var settings = ChunkSettings.collect(graph, inputs::settings, inputs.originX(), inputs.originZ());
var mantle = new MantleInvocation(new StageInvocation(graph, invocationSeed, settings), inputs);
for (int position = 0; position < graph.size(); position++) {
mantle.stages().runInvocationStage(position, mantle);
}
}
}
+25 -9
View File
@@ -14,7 +14,8 @@ import java.util.TreeSet;
/** The host's side of every pack load, the same for all packs: its built-in modules, the host
* packages pack modules may use besides the Iris API, the JSON fields the host itself uses (which no
* built-in alias may take), and the namespaces only built-in modules may use. Build it once and pass it to every
* built-in alias may take), the namespaces only built-in modules may use and the packages of the
* classes the host describes itself. Build it once and pass it to every
* {@link LoadedPack#load(java.nio.file.Path, ModuleHost)}.
*
* <pre>{@code
@@ -25,8 +26,8 @@ import java.util.TreeSet;
* }</pre>
*
* Every method adds to what earlier calls configured, so a host can build the configuration in steps.
* Built-in modules' {@code info()} is read here, once. Exposed package names, reserved namespaces and
* reserved field names are checked here, so a mistake fails when the host starts rather than at the
* Built-in modules' {@code info()} is read here, once. Exposed package names, reserved namespaces,
* host type packages and reserved field names are checked here, so a mistake fails when the host starts rather than at the
* first pack load.
*/
public record ModuleHost(
@@ -34,6 +35,7 @@ public record ModuleHost(
Set<String> exposedHostPackages,
Map<FeatureAttachment.Target, Set<String>> reservedHostFields,
Set<String> reservedNamespaces,
Set<String> hostTypePackages,
Path jarCopyRoot,
System.Logger logger) {
private static final String TEMPORARY_DIRECTORY_PROPERTY = "java.io.tmpdir";
@@ -43,10 +45,12 @@ public record ModuleHost(
exposedHostPackages = Set.copyOf(exposedHostPackages);
reservedHostFields = copyOf(reservedHostFields);
reservedNamespaces = Set.copyOf(reservedNamespaces);
hostTypePackages = Set.copyOf(hostTypePackages);
Objects.requireNonNull(jarCopyRoot, "jarCopyRoot");
Objects.requireNonNull(logger, "logger");
ExposedPackages.apiAnd(exposedHostPackages);
ReservedNamespaces.of(reservedNamespaces);
HostTypes.of(hostTypePackages);
requireFieldNames(reservedHostFields);
}
@@ -55,7 +59,7 @@ public record ModuleHost(
*/
public static ModuleHost of(List<? extends IrisModule> builtInModules) {
var builtIns = builtInModules.stream().map(DiscoveredModule::builtIn).toList();
return new ModuleHost(builtIns, Set.of(), Map.of(), Set.of(), systemTemporaryDirectory(),
return new ModuleHost(builtIns, Set.of(), Map.of(), Set.of(), Set.of(), systemTemporaryDirectory(),
System.getLogger(PackModules.class.getName()));
}
@@ -64,13 +68,13 @@ public record ModuleHost(
*/
public ModuleHost exposing(String... packages) {
return new ModuleHost(builtIns, union(exposedHostPackages, packages), reservedHostFields, reservedNamespaces,
jarCopyRoot, logger);
hostTypePackages, jarCopyRoot, logger);
}
/** This configuration with {@code namespaces} reserved for built-in modules. */
public ModuleHost reservingNamespaces(String... namespaces) {
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, union(reservedNamespaces, namespaces),
jarCopyRoot, logger);
hostTypePackages, jarCopyRoot, logger);
}
/** This configuration with {@code fields} also reserved as JSON fields the host itself uses,
@@ -78,21 +82,33 @@ public record ModuleHost(
*/
public ModuleHost reservingFields(Map<FeatureAttachment.Target, Set<String>> fields) {
return new ModuleHost(builtIns, exposedHostPackages, merge(reservedHostFields, fields), reservedNamespaces,
jarCopyRoot, logger);
hostTypePackages, jarCopyRoot, logger);
}
/** This configuration with the classes in {@code packages}, each with its subpackages, described by
* the host itself: built-in settings may hold them, such as Iris's palettes, and the pack's
* {@link com.volmit.host.schema.PackSchema} names them as {@link com.volmit.host.schema.ValueKind.HostKind}
* instead of describing their fields. Pack modules' settings cannot use them.
*/
public ModuleHost describingHostTypesIn(String... packages) {
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, reservedNamespaces,
union(hostTypePackages, packages), jarCopyRoot, logger);
}
/** This configuration copying module jars into new directories below {@code directory}, such as a
* cache directory of the host, which is created when missing. The last call wins.
*/
public ModuleHost copyingJarsTo(Path directory) {
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, reservedNamespaces, directory, logger);
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, reservedNamespaces, hostTypePackages,
directory, logger);
}
/** This configuration logging each pack load's module summary and missing-classes warning to
* {@code logger}, such as an adapter to the host's own log. The last call wins.
*/
public ModuleHost loggingTo(System.Logger logger) {
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, reservedNamespaces, jarCopyRoot, logger);
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, reservedNamespaces, hostTypePackages,
jarCopyRoot, logger);
}
/** The built-in modules followed by a pack's modules, in the order {@link LoadedModules} installs them. */
@@ -26,14 +26,15 @@ final class PackSchemaBuilder {
private static final char FOLDER_SEPARATOR = '/';
private final List<DiscoveredModule> modules;
private final TypeDrafts drafts = new TypeDrafts();
private final TypeDrafts drafts;
private PackSchemaBuilder(List<DiscoveredModule> modules) {
private PackSchemaBuilder(List<DiscoveredModule> modules, HostTypes hostTypes) {
this.modules = modules;
this.drafts = new TypeDrafts(hostTypes);
}
static PackSchema describe(FeatureCatalog catalog, List<DiscoveredModule> modules) {
return new PackSchemaBuilder(modules).describe(catalog);
static PackSchema describe(FeatureCatalog catalog, List<DiscoveredModule> modules, HostTypes hostTypes) {
return new PackSchemaBuilder(modules, hostTypes).describe(catalog);
}
private PackSchema describe(FeatureCatalog catalog) {
@@ -0,0 +1,64 @@
package com.volmit.host;
import com.volmit.mantle.BlockBox;
import com.volmit.mantle.Mantle;
import com.volmit.mantle.MantleEdit;
import com.volmit.mantle.MantleKey;
import com.volmit.mantle.MantleLayer;
import com.volmit.mantle.MantleVisitor;
/** The mantle as chunk stages see it: reads pass through, every write throws. The mantle pipeline has
* finished around the chunk by then, and its neighbors may already be generated.
*/
final class ReadOnlyMantle implements Mantle {
private static final String READ_ONLY = "Chunk stages read the mantle but do not write it;"
+ " write it from a mantle stage (Stage.mantle), which runs before the chunks it reaches";
private final Mantle mantle;
ReadOnlyMantle(Mantle mantle) {
this.mantle = mantle;
}
@Override
public BlockBox bounds() {
return mantle.bounds();
}
@Override
public <T> MantleLayer<T> layer(MantleKey<T> key) {
return new ReadOnlyLayer<>(mantle.layer(key));
}
@Override
public MantleEdit edit() {
throw new UnsupportedOperationException(READ_ONLY);
}
private record ReadOnlyLayer<T>(MantleLayer<T> layer) implements MantleLayer<T> {
@Override
public T get(int x, int y, int z) {
return layer.get(x, y, z);
}
@Override
public void set(int x, int y, int z, T value) {
throw new UnsupportedOperationException(READ_ONLY);
}
@Override
public void remove(int x, int y, int z) {
throw new UnsupportedOperationException(READ_ONLY);
}
@Override
public void visit(BlockBox area, MantleVisitor<T> visitor) {
layer.visit(area, visitor);
}
@Override
public void clear(BlockBox area) {
throw new UnsupportedOperationException(READ_ONLY);
}
}
}
@@ -3,6 +3,7 @@ package com.volmit.host;
import com.volmit.IrisFeature;
import com.volmit.mantle.MantleKey;
import com.volmit.resource.ResourceId;
import com.volmit.stage.Pipeline;
import com.volmit.stage.Stage;
import com.volmit.stage.StageRef;
import java.util.ArrayList;
@@ -54,6 +55,7 @@ final class StageDeclaration {
void reach(int blocks) {
checkOpen();
if (blocks > 0) requireOutsideChunkPipeline("a reach of " + blocks + " blocks");
if (reachBlocks != null) {
throw new IllegalStateException("Stage " + this + " already declares a reach of " + reachBlocks + " blocks");
}
@@ -65,6 +67,7 @@ final class StageDeclaration {
void writesMantle(MantleKey<?>... layers) {
checkOpen();
requireOutsideChunkPipeline("mantle writes");
for (var layer : layers) {
Objects.requireNonNull(layer, "mantle layer");
if (mantleLayers.stream().anyMatch(declared -> declared.id().equals(layer.id()))) {
@@ -147,6 +150,14 @@ final class StageDeclaration {
return "Stage " + this + " " + declared + "; define it with " + alternativeMethod + "(...)";
}
/** Chunk stages write only their own chunk: the host may already have generated its neighbors. */
private void requireOutsideChunkPipeline(String declared) {
if (stage.pipeline() != Pipeline.CHUNK) return;
throw new IllegalArgumentException("Stage " + this + " is a chunk stage and declares " + declared
+ ", but chunk stages write only their own chunk and read the mantle; define work that writes the"
+ " mantle or reaches beyond its chunk as a mantle stage (Stage.mantle), which runs before the chunks it reaches");
}
private void checkOpen() {
if (body != null) {
throw new IllegalStateException("Stage " + this + " is already complete");
@@ -6,6 +6,8 @@ import com.volmit.stage.BlockStage;
import com.volmit.stage.BlockStageDefinition;
import com.volmit.stage.ChunkStage;
import com.volmit.stage.ChunkStageContext;
import com.volmit.stage.MantleStage;
import com.volmit.stage.MantleStageContext;
import com.volmit.stage.Pipeline;
import com.volmit.stage.PlacementStage;
import com.volmit.stage.PlacementStageContext;
@@ -63,6 +65,11 @@ public final class StageGraphBuilder implements StageGraph {
return new InvocationStageDraft<>(declare(stage));
}
@Override
public <O> StageDefinition<MantleStageContext, O> define(MantleStage<O> stage) {
return new InvocationStageDraft<>(declare(stage));
}
public ResolvedGraph resolve(Pipeline pipeline) {
Objects.requireNonNull(pipeline, "pipeline");
return GraphResolver.resolve(pipeline, declarations.values());
@@ -10,7 +10,7 @@ import java.util.stream.IntStream;
/** Pipeline-independent state of one invocation: activation, settings, stage outputs and stage
* random streams, all indexed by resolved position. Activation is computed once up front in resolved
* order: a stage is active when its settings are configured (or it has no condition) and every
* order: a stage is active when its settings have an entry (or it has no condition) and every
* stage it requires is active, so inactivity cascades to dependents. Cost is one pass over stages
* and requirement edges plus one settings lookup per distinct settings type. A stage's stream is
* created when it first draws and depends only on the invocation seed and the stage id.
@@ -131,7 +131,8 @@ final class StageInvocation {
var types = graph.settingsTypes();
var values = new Object[types.size()];
for (int slot = 0; slot < values.length; slot++) {
values[slot] = settings.find((Class<? extends IrisFeature>) types.get(slot)).orElse(null);
var entries = settings.find((Class<? extends IrisFeature>) types.get(slot));
values[slot] = entries.isEmpty() ? null : entries;
}
return values;
}
@@ -8,8 +8,8 @@ import java.util.Objects;
/** What one stage declares it writes outside its footprint.
*
* @param blocks how far, in blocks along X and Z, the stage writes blocks or mantle values beyond its
* footprint: the chunk's 16 by 16 columns for a chunk stage, the X/Z bounds of the placed object
* for a placement stage. 0 means only inside it. Height is not limited: the footprint is whole
* footprint: the chunk's 16 by 16 columns for a mantle stage, the X/Z bounds of the placed object
* for a placement stage. 0 means only inside it, and is all a chunk stage may declare. Height is not limited: the footprint is whole
* columns.
* @param mantleLayers the mantle layers the stage writes, each once, in declaration order
*/
@@ -17,7 +17,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
/** Describes every class reachable from the settings of a pack, each once. A class is recorded before
/** Describes every class reachable from the settings of a pack, each once, except host types, which the
* host describes itself. A class is recorded before
* its fields are described, so a class that contains itself, directly or through others, ends the
* walk at its own name. Each class is instantiated once with its no-argument constructor, the way the
* host creates settings before decoding JSON into them, and its fields' values there are the defaults.
@@ -26,6 +27,11 @@ final class TypeDrafts {
private final Map<Class<?>, TypeDraft> drafts = new LinkedHashMap<>();
private final Set<Class<?>> describing = new HashSet<>();
private final Map<String, Class<?>> names = new HashMap<>();
private final HostTypes hostTypes;
TypeDrafts(HostTypes hostTypes) {
this.hostTypes = hostTypes;
}
/** One described class: its metadata and its options with their kinds and raw default values. */
record TypeDraft(Class<?> type, SchemaSource source, OptionsMetadata metadata, List<OptionDraft> options) {}
@@ -63,7 +69,8 @@ final class TypeDrafts {
private TypeDraft draft(Class<?> type, SchemaSource source) {
var metadata = inspect(type, source);
var instance = instantiate(type, source);
var kinds = new ValueKinds(nested -> object(nested, source));
var kinds = new ValueKinds(nested -> object(nested, source), hostTypes,
source.module().source() == DiscoveredModule.Source.HOST);
var options = metadata.options().stream()
.map(option -> new OptionDraft(option, kind(option, kinds, source), defaultValue(option.field(), instance, source)))
.toList();
+20 -4
View File
@@ -10,6 +10,7 @@ import com.volmit.host.schema.ValueKind.BooleanKind;
import com.volmit.host.schema.ValueKind.DecimalKind;
import com.volmit.host.schema.ValueKind.EnumConstant;
import com.volmit.host.schema.ValueKind.EnumKind;
import com.volmit.host.schema.ValueKind.HostKind;
import com.volmit.host.schema.ValueKind.IntegerKind;
import com.volmit.host.schema.ValueKind.ListKind;
import com.volmit.host.schema.ValueKind.MapKind;
@@ -29,9 +30,10 @@ import java.util.Map;
import java.util.Set;
import java.util.function.Function;
/** Maps a settings field's declared type and {@code @Ref} to the JSON it takes. Classes that are none
* of the built-in kinds become object kinds through the given function. Throws
* {@link SettingsTypeProblem} for a type or reference that does not fit.
/** Maps a settings field's declared type and {@code @Ref} to the JSON it takes. Host types become host
* kinds where the settings may use them; other classes that are none of the built-in kinds become object
* kinds through the given function. Throws {@link SettingsTypeProblem} for a type or reference that does
* not fit.
*/
final class ValueKinds {
static final String SUPPORTED_TYPES = "settings fields can be booleans, numbers, String, ResourceId, BlockState, enums, "
@@ -44,9 +46,14 @@ final class ValueKinds {
private static final Set<Reference> PACK_REFERENCES = EnumSet.of(Reference.PACK_BIOME, Reference.PACK_REGION, Reference.PACK_OBJECT);
private final Function<Class<?>, ObjectKind> objects;
private final HostTypes hostTypes;
private final boolean hostTypesAllowed;
ValueKinds(Function<Class<?>, ObjectKind> objects) {
/** {@code hostTypesAllowed} is whether the settings belong to a module that ships with the host. */
ValueKinds(Function<Class<?>, ObjectKind> objects, HostTypes hostTypes, boolean hostTypesAllowed) {
this.objects = objects;
this.hostTypes = hostTypes;
this.hostTypesAllowed = hostTypesAllowed;
}
ValueKind of(OptionsMetadata.Option option) {
@@ -64,6 +71,7 @@ final class ValueKinds {
}
private ValueKind classKind(Class<?> type, Reference reference) {
if (hostTypes.contains(type)) return hostKind(type, reference);
if (type == String.class) return text(TextFormat.PLAIN, reference);
if (type == ResourceId.class) return text(TextFormat.RESOURCE_ID, reference);
if (type == BlockState.class) return blockState(reference);
@@ -80,6 +88,7 @@ final class ValueKinds {
private ValueKind genericKind(ParameterizedType generic, Reference reference) {
var raw = (Class<?>) generic.getRawType();
if (hostTypes.contains(raw)) return hostKind(raw, reference);
var arguments = generic.getActualTypeArguments();
if (raw == List.class || raw == Set.class) return new ListKind(kind(arguments[0], reference), raw == Set.class);
if (raw == Map.class) return mapKind(generic, arguments, reference);
@@ -94,6 +103,13 @@ final class ValueKinds {
return new MapKind(kind(arguments[1], null));
}
private HostKind hostKind(Class<?> type, Reference reference) {
requireNoReference(type, reference);
if (hostTypesAllowed) return new HostKind(type.getName());
throw new SettingsTypeProblem("uses " + type.getName() + ", a class the host describes itself, which only settings of"
+ " modules that ship with the host may use; " + SUPPORTED_TYPES);
}
private static ValueKind text(TextFormat format, Reference reference) {
if (reference == null) return new TextKind(format);
if (format == TextFormat.RESOURCE_ID && PACK_REFERENCES.contains(reference)) throw packReferenceOnResourceId(reference);
@@ -39,4 +39,12 @@ public record FeatureSchema(
Objects.requireNonNull(settings, "settings");
targets = Collections.unmodifiableSet(EnumSet.copyOf(targets));
}
/** The settings' only option, when they have exactly one. Packs may then write that option's value
* directly instead of an object holding it, such as {@code "snow": 0.5} for {@code "snow": {"amount": 0.5}},
* and the host decodes a value that is not a JSON object into it.
*/
public Optional<SchemaOption> shorthand() {
return settings.options().size() == 1 ? Optional.of(settings.options().getFirst()) : Optional.empty();
}
}
@@ -63,6 +63,16 @@ public sealed interface ValueKind {
}
}
/** A value of the class named {@code type}, which the host describes itself, such as one of Iris's
* palettes or noise styles. Only built-in modules' settings use these (see
* {@link com.volmit.host.ModuleHost#describingHostTypesIn}); the host renders its own description.
*/
record HostKind(String type) implements ValueKind {
public HostKind {
Objects.requireNonNull(type, "type");
}
}
/** An enum constant and its {@code @Desc}, or an empty description. */
record EnumConstant(String name, String description) {
public EnumConstant {
@@ -15,9 +15,8 @@ public interface BlockStageDefinition<O> {
BlockStageDefinition<O> softBefore(StageRef... stages);
/** Declares how far, in blocks along X and Z, this stage writes blocks or mantle values beyond its
* footprint: the chunk for a chunk stage, the placed object's X/Z bounds for a placement stage. The
* host prepares that much around every chunk or placement of the pipeline. Leaving it out means 0.
/** Declares how far, in blocks along X and Z, this stage writes blocks or mantle values beyond the
* placed object's X/Z bounds. The host prepares that much around every placement. Leaving it out means 0.
*/
BlockStageDefinition<O> reach(int blocks);
@@ -1,6 +1,8 @@
package com.volmit.stage;
/** Runs once per chunk generation invocation. */
/** Runs once per chunk generation invocation and writes only its own chunk. It may read the mantle but
* not write it, and it declares no reach: such work belongs in a {@link MantleStage}.
*/
public final class ChunkStage<O> extends Stage<O> {
ChunkStage(String id, Class<O> outputType) {
super(id, outputType);
@@ -1,11 +1,12 @@
package com.volmit.stage;
import com.volmit.IrisFeature;
import com.volmit.mantle.MantleKey;
import java.util.function.Supplier;
/** A block stage active only where its settings are configured; bodies receive them. */
public interface ConditionalBlockStageDefinition<S, O> {
/** A block stage active only where its settings are configured; bodies receive every configured entry. */
public interface ConditionalBlockStageDefinition<S extends IrisFeature, O> {
ConditionalBlockStageDefinition<S, O> requires(Stage<?>... stages);
ConditionalBlockStageDefinition<S, O> softAfter(StageRef... stages);
@@ -1,9 +1,10 @@
package com.volmit.stage;
import com.volmit.IrisFeature;
import com.volmit.mantle.MantleKey;
/** An invocation stage active only where its settings are configured; bodies receive them. */
public interface ConditionalStageDefinition<C extends StageContext, S, O> {
/** An invocation stage active only where its settings are configured; bodies receive every configured entry. */
public interface ConditionalStageDefinition<C extends StageContext, S extends IrisFeature, O> {
ConditionalStageDefinition<C, S, O> requires(Stage<?>... stages);
ConditionalStageDefinition<C, S, O> softAfter(StageRef... stages);
@@ -0,0 +1,18 @@
package com.volmit.stage;
/** Runs once per chunk in the mantle pipeline, before the chunks it reaches generate. */
public final class MantleStage<O> extends Stage<O> {
MantleStage(String id, Class<O> outputType) {
super(id, outputType);
}
@Override
public Pipeline pipeline() {
return Pipeline.MANTLE;
}
@Override
public StageKind kind() {
return StageKind.INVOCATION;
}
}
@@ -0,0 +1,6 @@
package com.volmit.stage;
import com.volmit.generation.MantleGenerationContext;
/** One mantle invocation for one chunk: the mantle around it, without a block hunk. */
public interface MantleStageContext extends MantleGenerationContext, StageContext {}
+9 -2
View File
@@ -4,6 +4,13 @@ package com.volmit.stage;
public enum Pipeline {
/** Runs once per object placement attempt, against one buffered placement edit. */
PLACEMENT,
/** Runs once per chunk generation invocation, against host-owned local hunks. */
CHUNK
/** Runs once per chunk generation invocation, against host-owned local hunks. Its stages write only
* their own chunk and read the mantle.
*/
CHUNK,
/** Runs once per chunk before any chunk near it generates, against the mantle only: work that writes
* the mantle or reaches beyond its chunk. The host runs it around a chunk as far as the pipeline's
* reach, so a chunk's mantle is complete when the chunk pipeline reads it.
*/
MANTLE
}
@@ -1,6 +1,13 @@
package com.volmit.stage;
import com.volmit.IrisFeature;
import com.volmit.config.FeatureSettings.Entry;
import java.util.List;
/** Body of a conditional stage without output. {@code settings} holds every entry configured for the
* invocation, in level order, and is never empty.
*/
@FunctionalInterface
public interface SettingsAction<C, S> {
void run(C context, S settings);
public interface SettingsAction<C, S extends IrisFeature> {
void run(C context, List<Entry<S>> settings);
}
@@ -1,6 +1,13 @@
package com.volmit.stage;
import com.volmit.IrisFeature;
import com.volmit.config.FeatureSettings.Entry;
import java.util.List;
/** Body of a conditional accumulating block stage. {@code settings} holds every entry configured for
* the placement, in level order, and is never empty.
*/
@FunctionalInterface
public interface SettingsBlockCollector<S, O> {
void collect(BlockStageContext context, S settings, O accumulator);
public interface SettingsBlockCollector<S extends IrisFeature, O> {
void collect(BlockStageContext context, List<Entry<S>> settings, O accumulator);
}
@@ -1,6 +1,13 @@
package com.volmit.stage;
import com.volmit.IrisFeature;
import com.volmit.config.FeatureSettings.Entry;
import java.util.List;
/** Body of a conditional stage with output. {@code settings} holds every entry configured for the
* invocation, in level order, and is never empty.
*/
@FunctionalInterface
public interface SettingsFunction<C, S, O> {
O run(C context, S settings);
public interface SettingsFunction<C, S extends IrisFeature, O> {
O run(C context, List<Entry<S>> settings);
}
+9 -1
View File
@@ -9,7 +9,7 @@ import java.util.Objects;
* 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.
*/
public abstract sealed class Stage<O> permits PlacementStage, BlockStage, ChunkStage {
public abstract sealed class Stage<O> permits PlacementStage, BlockStage, ChunkStage, MantleStage {
private final ResourceId id;
private final Class<O> outputType;
@@ -46,6 +46,14 @@ public abstract sealed class Stage<O> permits PlacementStage, BlockStage, ChunkS
return new ChunkStage<>(id, outputType);
}
public static MantleStage<Void> mantle(String id) {
return new MantleStage<>(id, Void.class);
}
public static <O> MantleStage<O> mantle(String id, Class<O> outputType) {
return new MantleStage<>(id, outputType);
}
public final ResourceId id() {
return id;
}
@@ -17,17 +17,22 @@ public interface StageDefinition<C extends StageContext, O> {
StageDefinition<C, O> softBefore(StageRef... stages);
/** Declares how far, in blocks along X and Z, this stage writes blocks or mantle values beyond its
* footprint: the chunk for a chunk stage, the placed object's X/Z bounds for a placement stage. The
* footprint: the chunk for a mantle stage, the placed object's X/Z bounds for a placement stage. The
* host prepares that much around every chunk or placement of the pipeline. Leaving it out means 0.
* Chunk stages write only their own chunk, so a reach above 0 on one fails the install.
*/
StageDefinition<C, O> reach(int blocks);
/** Declares the mantle layers this stage writes; each must be registered by some module. */
/** Declares the mantle layers this stage writes; each must be registered by some module. Chunk stages
* only read the mantle, so declaring layers on one fails the install.
*/
StageDefinition<C, O> writesMantle(MantleKey<?>... layers);
/** 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}.
/** Activates the stage only where the given feature settings are configured: in a placement where the
* placement declares them, in a chunk or mantle invocation where any column of the chunk has an entry.
* The body receives every configured entry, in level order. 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}.
*/
<S extends IrisFeature> ConditionalStageDefinition<C, S, O> when(Class<S> settingsType);
@@ -12,4 +12,6 @@ public interface StageGraph {
<O> BlockStageDefinition<O> define(BlockStage<O> stage);
<O> StageDefinition<ChunkStageContext, O> define(ChunkStage<O> stage);
<O> StageDefinition<MantleStageContext, O> define(MantleStage<O> stage);
}