mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
Baseline IrisApi draft before stage-graph redesign
This commit is contained in:
commit
9043d93fbd
91 files changed
+4045
No files matched your search
@@ -0,0 +1,88 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.hunk.Hunk;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Reference dense volume with checked local coordinates and non-null values.
|
||||
* Each invocation owns its volume; concurrent writes to the same volume are unsupported.
|
||||
*/
|
||||
public final class ArrayHunk<T> implements Hunk<T> {
|
||||
private final Class<T> type;
|
||||
private final int width;
|
||||
private final int height;
|
||||
private final int depth;
|
||||
private final Object[] values;
|
||||
|
||||
public ArrayHunk(Class<T> type, int width, int height, int depth, T initial) {
|
||||
this.type = Objects.requireNonNull(type, "type");
|
||||
if (type.isPrimitive()) {
|
||||
throw new IllegalArgumentException("Hunk requires a reference type");
|
||||
}
|
||||
validateDimensions(width, height, depth);
|
||||
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.depth = depth;
|
||||
values = new Object[valueCount(width, height, depth)];
|
||||
Arrays.fill(values, type.cast(Objects.requireNonNull(initial, "initial")));
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public int getDepth() {
|
||||
return depth;
|
||||
}
|
||||
|
||||
public T get(int x, int y, int z) {
|
||||
return type.cast(values[index(x, y, z)]);
|
||||
}
|
||||
|
||||
public void set(int x, int y, int z, T value) {
|
||||
int valueIndex = index(x, y, z);
|
||||
values[valueIndex] = type.cast(Objects.requireNonNull(value, "value"));
|
||||
}
|
||||
|
||||
private static void validateDimensions(int width, int height, int depth) {
|
||||
if (width < 0) {
|
||||
throw negativeDimension();
|
||||
}
|
||||
if (height < 0) {
|
||||
throw negativeDimension();
|
||||
}
|
||||
if (depth < 0) {
|
||||
throw negativeDimension();
|
||||
}
|
||||
}
|
||||
|
||||
private static IllegalArgumentException negativeDimension() {
|
||||
return new IllegalArgumentException("Negative hunk dimension");
|
||||
}
|
||||
|
||||
private static int valueCount(int width, int height, int depth) {
|
||||
return Math.multiplyExact(Math.multiplyExact(width, height), depth);
|
||||
}
|
||||
|
||||
private int index(int x, int y, int z) {
|
||||
if (x < 0 || x >= width) {
|
||||
throw outOfBounds(x, y, z);
|
||||
}
|
||||
if (y < 0 || y >= height) {
|
||||
throw outOfBounds(x, y, z);
|
||||
}
|
||||
if (z < 0 || z >= depth) {
|
||||
throw outOfBounds(x, y, z);
|
||||
}
|
||||
return (y * depth + z) * width + x;
|
||||
}
|
||||
|
||||
private static IndexOutOfBoundsException outOfBounds(int x, int y, int z) {
|
||||
return new IndexOutOfBoundsException("Local hunk position: " + x + "," + y + "," + z);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.generation.Dependencies;
|
||||
import com.volmit.generation.GenerationContext;
|
||||
import com.volmit.generation.GenerationModifier;
|
||||
import com.volmit.hunk.Hunk;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Pack-load snapshot of modifier identity, requirements, type and prepared reach.
|
||||
* The occurrence identifies one configuration, independent of its shared ordering identity.
|
||||
*/
|
||||
public final class ConfiguredModifier<T> {
|
||||
private final ResourceId occurrence;
|
||||
private final Class<T> outputType;
|
||||
private final GenerationModifier<T> modifier;
|
||||
private final ResourceId id;
|
||||
private final Dependencies dependencies;
|
||||
private final int mantleRadius;
|
||||
|
||||
public ConfiguredModifier(
|
||||
ResourceId occurrence,
|
||||
Class<T> outputType,
|
||||
GenerationModifier<T> modifier) {
|
||||
this.occurrence = Objects.requireNonNull(occurrence, "occurrence");
|
||||
this.outputType = Objects.requireNonNull(outputType, "outputType");
|
||||
if (outputType.isPrimitive()) {
|
||||
throw new IllegalArgumentException("Output requires a reference type");
|
||||
}
|
||||
this.modifier = Objects.requireNonNull(modifier, "modifier");
|
||||
id = ResourceId.parse(modifier.id());
|
||||
dependencies = Objects.requireNonNull(modifier.depends(), "dependencies");
|
||||
mantleRadius = modifier.mantleRadius();
|
||||
if (mantleRadius < 0) {
|
||||
throw new IllegalArgumentException("Negative mantle radius: " + id);
|
||||
}
|
||||
}
|
||||
|
||||
public ResourceId occurrence() {
|
||||
return occurrence;
|
||||
}
|
||||
|
||||
public Class<T> outputType() {
|
||||
return outputType;
|
||||
}
|
||||
|
||||
public ResourceId id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Dependencies dependencies() {
|
||||
return dependencies;
|
||||
}
|
||||
|
||||
public int mantleRadius() {
|
||||
return mantleRadius;
|
||||
}
|
||||
|
||||
public Class<?> implementationType() {
|
||||
return modifier.getClass();
|
||||
}
|
||||
|
||||
/** Host must prepare views/predecessor work and abort publication if this throws. */
|
||||
public void modify(GenerationContext context, Hunk<T> output) {
|
||||
modifier.modify(
|
||||
Objects.requireNonNull(context, "context"),
|
||||
Objects.requireNonNull(output, "output"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.generation.Dependencies;
|
||||
import com.volmit.placement.PlacementModifier;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Pack-load placement declaration snapshot. The configured modifier must be immutable. */
|
||||
public final class ConfiguredPlacementModifier {
|
||||
private final ResourceId occurrence;
|
||||
private final PlacementModifier modifier;
|
||||
private final ResourceId id;
|
||||
private final Dependencies dependencies;
|
||||
private final int writeRadius;
|
||||
|
||||
public ConfiguredPlacementModifier(ResourceId occurrence, PlacementModifier modifier) {
|
||||
this.occurrence = Objects.requireNonNull(occurrence, "occurrence");
|
||||
this.modifier = Objects.requireNonNull(modifier, "modifier");
|
||||
id = ResourceId.parse(modifier.id());
|
||||
dependencies = Objects.requireNonNull(modifier.depends(), "dependencies");
|
||||
writeRadius = modifier.writeRadius();
|
||||
if (writeRadius < 0) {
|
||||
throw new IllegalArgumentException("Negative placement radius: " + id);
|
||||
}
|
||||
}
|
||||
|
||||
public ResourceId occurrence() {
|
||||
return occurrence;
|
||||
}
|
||||
|
||||
public PlacementModifier modifier() {
|
||||
return modifier;
|
||||
}
|
||||
|
||||
public ResourceId id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Dependencies dependencies() {
|
||||
return dependencies;
|
||||
}
|
||||
|
||||
public int writeRadius() {
|
||||
return writeRadius;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.generation.Dependencies;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/** Host-independent ordering of one complete modifier graph. Ready IDs use lexical order,
|
||||
* independent of discovery. Required IDs must exist. Optional edges apply only when present.
|
||||
* Self-dependencies and cycles fail. This graph never activates features or permits parallel writes.
|
||||
*/
|
||||
public final class DependencyOrder {
|
||||
private DependencyOrder() {}
|
||||
|
||||
public static List<ResourceId> resolve(Map<ResourceId, Dependencies> declarations) {
|
||||
var graph = Map.copyOf(declarations);
|
||||
var incomingEdges = initializeIncomingEdges(graph.keySet());
|
||||
var outgoingEdges = new HashMap<ResourceId, Set<ResourceId>>();
|
||||
|
||||
addDeclaredEdges(graph, incomingEdges, outgoingEdges);
|
||||
return topologicalOrder(graph, incomingEdges, outgoingEdges);
|
||||
}
|
||||
|
||||
private static Map<ResourceId, Integer> initializeIncomingEdges(Set<ResourceId> ids) {
|
||||
var incomingEdges = new HashMap<ResourceId, Integer>();
|
||||
ids.forEach(id -> incomingEdges.put(id, 0));
|
||||
return incomingEdges;
|
||||
}
|
||||
|
||||
private static void addDeclaredEdges(
|
||||
Map<ResourceId, Dependencies> graph,
|
||||
Map<ResourceId, Integer> incomingEdges,
|
||||
Map<ResourceId, Set<ResourceId>> outgoingEdges) {
|
||||
graph.forEach((id, dependencies) -> {
|
||||
Objects.requireNonNull(dependencies, "dependencies");
|
||||
addRequiredEdges(id, dependencies, graph, incomingEdges, outgoingEdges);
|
||||
addOptionalEdges(id, dependencies, graph, incomingEdges, outgoingEdges);
|
||||
});
|
||||
}
|
||||
|
||||
private static void addRequiredEdges(
|
||||
ResourceId id,
|
||||
Dependencies dependencies,
|
||||
Map<ResourceId, Dependencies> graph,
|
||||
Map<ResourceId, Integer> incomingEdges,
|
||||
Map<ResourceId, Set<ResourceId>> outgoingEdges) {
|
||||
for (var name : dependencies.required()) {
|
||||
var predecessor = ResourceId.parse(name);
|
||||
if (!graph.containsKey(predecessor)) {
|
||||
throw new IllegalArgumentException(id + " requires missing modifier " + predecessor);
|
||||
}
|
||||
addEdge(predecessor, id, incomingEdges, outgoingEdges);
|
||||
}
|
||||
}
|
||||
|
||||
private static void addOptionalEdges(
|
||||
ResourceId id,
|
||||
Dependencies dependencies,
|
||||
Map<ResourceId, Dependencies> graph,
|
||||
Map<ResourceId, Integer> incomingEdges,
|
||||
Map<ResourceId, Set<ResourceId>> outgoingEdges) {
|
||||
for (var name : dependencies.after()) {
|
||||
var predecessor = ResourceId.parse(name);
|
||||
rejectSelf(id, predecessor);
|
||||
if (graph.containsKey(predecessor)) {
|
||||
addEdge(predecessor, id, incomingEdges, outgoingEdges);
|
||||
}
|
||||
}
|
||||
for (var name : dependencies.before()) {
|
||||
var successor = ResourceId.parse(name);
|
||||
rejectSelf(id, successor);
|
||||
if (graph.containsKey(successor)) {
|
||||
addEdge(id, successor, incomingEdges, outgoingEdges);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static List<ResourceId> topologicalOrder(
|
||||
Map<ResourceId, Dependencies> graph,
|
||||
Map<ResourceId, Integer> incomingEdges,
|
||||
Map<ResourceId, Set<ResourceId>> outgoingEdges) {
|
||||
var ready = new PriorityQueue<ResourceId>();
|
||||
incomingEdges.forEach((id, count) -> {
|
||||
if (count == 0) {
|
||||
ready.add(id);
|
||||
}
|
||||
});
|
||||
|
||||
var ordered = new ArrayList<ResourceId>(graph.size());
|
||||
while (!ready.isEmpty()) {
|
||||
var id = ready.remove();
|
||||
ordered.add(id);
|
||||
releaseSuccessors(id, outgoingEdges, incomingEdges, ready);
|
||||
}
|
||||
|
||||
if (ordered.size() != graph.size()) {
|
||||
throw cycleException(graph.keySet(), ordered);
|
||||
}
|
||||
return List.copyOf(ordered);
|
||||
}
|
||||
|
||||
private static void releaseSuccessors(
|
||||
ResourceId id,
|
||||
Map<ResourceId, Set<ResourceId>> outgoingEdges,
|
||||
Map<ResourceId, Integer> incomingEdges,
|
||||
PriorityQueue<ResourceId> ready) {
|
||||
for (var successor : outgoingEdges.getOrDefault(id, Set.of())) {
|
||||
int remainingEdges = incomingEdges.compute(successor, (ignored, count) -> count - 1);
|
||||
if (remainingEdges == 0) {
|
||||
ready.add(successor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static IllegalArgumentException cycleException(Set<ResourceId> ids, List<ResourceId> ordered) {
|
||||
var unresolved = new TreeSet<>(ids);
|
||||
unresolved.removeAll(ordered);
|
||||
return new IllegalArgumentException("Modifier dependency cycle: " + unresolved);
|
||||
}
|
||||
|
||||
private static void rejectSelf(ResourceId id, ResourceId other) {
|
||||
if (id.equals(other)) {
|
||||
throw new IllegalArgumentException("Self-dependency: " + id);
|
||||
}
|
||||
}
|
||||
|
||||
private static void addEdge(
|
||||
ResourceId from,
|
||||
ResourceId to,
|
||||
Map<ResourceId, Integer> incomingEdges,
|
||||
Map<ResourceId, Set<ResourceId>> outgoingEdges) {
|
||||
rejectSelf(from, to);
|
||||
if (outgoingEdges.computeIfAbsent(from, ignored -> new HashSet<>()).add(to)) {
|
||||
incomingEdges.compute(to, (ignored, count) -> count + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.IrisFeature;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.function.Function;
|
||||
|
||||
/** Frozen registrations. Lists use lexical feature/layer ID order, independent of discovery.
|
||||
* Factory and codec implementations must themselves obey their concurrency contracts.
|
||||
*/
|
||||
public final class FeatureCatalog {
|
||||
private final Map<Class<?>, GenerationRegistration<?, ?>> generation;
|
||||
private final Map<Class<?>, ObjectRegistration<?>> objects;
|
||||
private final Map<Class<?>, PlacementRegistration<?>> placement;
|
||||
private final Map<String, MantleRegistration<?>> mantle;
|
||||
|
||||
FeatureCatalog(
|
||||
Map<Class<?>, GenerationRegistration<?, ?>> generation,
|
||||
Map<Class<?>, ObjectRegistration<?>> objects,
|
||||
Map<Class<?>, PlacementRegistration<?>> placement,
|
||||
Map<String, MantleRegistration<?>> mantle) {
|
||||
this.generation = Map.copyOf(generation);
|
||||
this.objects = Map.copyOf(objects);
|
||||
this.placement = Map.copyOf(placement);
|
||||
this.mantle = Map.copyOf(mantle);
|
||||
}
|
||||
|
||||
public List<GenerationRegistration<?, ?>> generation() {
|
||||
return sorted(generation.values(), registration -> registration.descriptor().id());
|
||||
}
|
||||
|
||||
public List<ObjectRegistration<?>> objects() {
|
||||
return sorted(objects.values(), registration -> registration.descriptor().id());
|
||||
}
|
||||
|
||||
public List<PlacementRegistration<?>> placement() {
|
||||
return sorted(placement.values(), registration -> registration.descriptor().id());
|
||||
}
|
||||
|
||||
public List<MantleRegistration<?>> mantle() {
|
||||
return sorted(mantle.values(), registration -> registration.key().id());
|
||||
}
|
||||
|
||||
private static <T, K extends Comparable<? super K>> List<T> sorted(
|
||||
Collection<T> registrations,
|
||||
Function<? super T, ? extends K> key) {
|
||||
return registrations.stream()
|
||||
.sorted(Comparator.comparing(key))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked") // The registry key and output type establish the generic pairing.
|
||||
public <F extends IrisFeature, T> GenerationRegistration<F, T> generation(
|
||||
Class<F> type,
|
||||
Class<T> outputType) {
|
||||
var registration = generation.get(type);
|
||||
if (registration == null) {
|
||||
throw new NoSuchElementException("No generation feature: " + type.getName());
|
||||
}
|
||||
if (!registration.outputType().equals(outputType)) {
|
||||
throw new IllegalArgumentException("Output type mismatch: " + type.getName());
|
||||
}
|
||||
return (GenerationRegistration<F, T>) registration;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked") // The registry key is the factory's feature type.
|
||||
public <F extends IrisFeature> ObjectRegistration<F> object(Class<F> type) {
|
||||
var registration = objects.get(type);
|
||||
if (registration == null) {
|
||||
throw new NoSuchElementException("No object feature: " + type.getName());
|
||||
}
|
||||
return (ObjectRegistration<F>) registration;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked") // The registry key is the factory's feature type.
|
||||
public <F extends IrisFeature> PlacementRegistration<F> placement(Class<F> type) {
|
||||
var registration = placement.get(type);
|
||||
if (registration == null) {
|
||||
throw new NoSuchElementException("No placement feature: " + type.getName());
|
||||
}
|
||||
return (PlacementRegistration<F>) registration;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.FeatureAttachment;
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.config.OptionsMetadata;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Validated registration metadata. It describes configuration, not executable ordering. */
|
||||
public record FeatureDescriptor<F extends IrisFeature>(
|
||||
ResourceId id,
|
||||
String name,
|
||||
Class<F> type,
|
||||
FeatureAttachment attachment,
|
||||
OptionsMetadata options) {
|
||||
public FeatureDescriptor {
|
||||
Objects.requireNonNull(id, "id");
|
||||
Objects.requireNonNull(name, "name");
|
||||
if (name.isBlank()) {
|
||||
throw new IllegalArgumentException("Blank feature name");
|
||||
}
|
||||
Objects.requireNonNull(type, "type");
|
||||
Objects.requireNonNull(attachment, "attachment");
|
||||
Objects.requireNonNull(options, "options");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.generation.Dependencies;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/** Immutable pack-load ordering across output types. Instances sharing a modifier ID must
|
||||
* have the same implementation, output type and dependencies. Occurrence IDs must be unique.
|
||||
* Instances within one identity use lexical occurrence order. Resolve once, dispatch many times.
|
||||
* The host includes its built-in lifecycle steps in this graph and rejects incompatible edges.
|
||||
*/
|
||||
public final class GenerationPlan {
|
||||
private final List<ConfiguredModifier<?>> ordered;
|
||||
|
||||
private GenerationPlan(List<ConfiguredModifier<?>> ordered) {
|
||||
this.ordered = List.copyOf(ordered);
|
||||
}
|
||||
|
||||
public static GenerationPlan resolve(List<? extends ConfiguredModifier<?>> modifiers) {
|
||||
List<ConfiguredModifier<?>> instances = new ArrayList<>(modifiers);
|
||||
var declarations = collectDeclarations(instances);
|
||||
var modifierIds = DependencyOrder.resolve(declarations);
|
||||
var orderedOccurrences = OccurrenceOrder.order(
|
||||
modifierIds,
|
||||
instances,
|
||||
ConfiguredModifier::id,
|
||||
ConfiguredModifier::occurrence);
|
||||
return new GenerationPlan(orderedOccurrences);
|
||||
}
|
||||
|
||||
private static Map<ResourceId, Dependencies> collectDeclarations(List<ConfiguredModifier<?>> instances) {
|
||||
var representatives = new HashMap<ResourceId, ConfiguredModifier<?>>();
|
||||
var occurrences = new HashSet<ResourceId>();
|
||||
var declarations = new HashMap<ResourceId, Dependencies>();
|
||||
|
||||
for (var modifier : instances) {
|
||||
checkUniqueOccurrence(modifier, occurrences);
|
||||
checkCompatibleIdentity(modifier, representatives);
|
||||
declarations.put(modifier.id(), modifier.dependencies());
|
||||
}
|
||||
return declarations;
|
||||
}
|
||||
|
||||
private static void checkUniqueOccurrence(
|
||||
ConfiguredModifier<?> modifier,
|
||||
Set<ResourceId> occurrences) {
|
||||
if (!occurrences.add(modifier.occurrence())) {
|
||||
throw new IllegalArgumentException("Duplicate occurrence: " + modifier.occurrence());
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkCompatibleIdentity(
|
||||
ConfiguredModifier<?> modifier,
|
||||
Map<ResourceId, ConfiguredModifier<?>> representatives) {
|
||||
var previous = representatives.putIfAbsent(modifier.id(), modifier);
|
||||
if (previous == null || isCompatible(previous, modifier)) {
|
||||
return;
|
||||
}
|
||||
throw new IllegalArgumentException("Conflicting modifier identity: " + modifier.id());
|
||||
}
|
||||
|
||||
private static boolean isCompatible(ConfiguredModifier<?> first, ConfiguredModifier<?> second) {
|
||||
return first.implementationType().equals(second.implementationType())
|
||||
&& first.outputType().equals(second.outputType())
|
||||
&& first.dependencies().equals(second.dependencies());
|
||||
}
|
||||
|
||||
public List<ConfiguredModifier<?>> ordered() {
|
||||
return ordered;
|
||||
}
|
||||
|
||||
/** Sequential dispatch only. Executor prepares/releases a dedicated context and typed output
|
||||
* for each applicable occurrence; applicability is host-owned. Failures stop dispatch.
|
||||
* This reference planner does not implement chunk scheduling, rollback or lifecycle bridging.
|
||||
*/
|
||||
public void execute(Executor executor) {
|
||||
java.util.Objects.requireNonNull(executor, "executor");
|
||||
for (var modifier : ordered) {
|
||||
executor.execute(modifier);
|
||||
}
|
||||
}
|
||||
|
||||
public interface Executor {
|
||||
<T> void execute(ConfiguredModifier<T> modifier);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.generation.ModifierFactory;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.Objects;
|
||||
|
||||
public record GenerationRegistration<F extends IrisFeature, T>(
|
||||
FeatureDescriptor<F> descriptor,
|
||||
Class<T> outputType,
|
||||
ModifierFactory<F, T> factory) {
|
||||
public GenerationRegistration {
|
||||
Objects.requireNonNull(descriptor, "descriptor");
|
||||
Objects.requireNonNull(outputType, "outputType");
|
||||
if (outputType.isPrimitive()) {
|
||||
throw new IllegalArgumentException("Output requires a reference type");
|
||||
}
|
||||
Objects.requireNonNull(factory, "factory");
|
||||
}
|
||||
|
||||
public ConfiguredModifier<T> configure(ResourceId occurrence, F feature) {
|
||||
var configuredFeature = descriptor.type().cast(Objects.requireNonNull(feature, "feature"));
|
||||
return new ConfiguredModifier<>(occurrence, outputType, factory.create(configuredFeature));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.mantle.MantleCodec;
|
||||
import com.volmit.mantle.MantleKey;
|
||||
import com.volmit.mantle.MantleLifetime;
|
||||
import java.util.Objects;
|
||||
|
||||
public record MantleRegistration<T>(
|
||||
MantleKey<T> key,
|
||||
MantleLifetime lifetime,
|
||||
MantleCodec<T> codec,
|
||||
int version) {
|
||||
public MantleRegistration {
|
||||
Objects.requireNonNull(key, "key");
|
||||
Objects.requireNonNull(lifetime, "lifetime");
|
||||
Objects.requireNonNull(codec, "codec");
|
||||
if (version < 0) {
|
||||
throw new IllegalArgumentException("Negative mantle codec version");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.FeatureAttachment;
|
||||
import com.volmit.FeatureRegistry;
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.config.Feature;
|
||||
import com.volmit.config.OptionsMetadata;
|
||||
import com.volmit.generation.ModifierFactory;
|
||||
import com.volmit.mantle.MantleCodec;
|
||||
import com.volmit.mantle.MantleKey;
|
||||
import com.volmit.mantle.MantleLifetime;
|
||||
import com.volmit.object.ObjectGeneratorFactory;
|
||||
import com.volmit.placement.PlacementModifierFactory;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/** Reference host registry. Single-threaded registration ends with freeze(); the catalog is
|
||||
* immutable. IDs are unique across feature kinds; JSON fields are unique per attachment target.
|
||||
* Pass existing host JSON fields to reserve them before modules register. A failed registration
|
||||
* leaves the registry unchanged. A failed module callback invalidates the host's whole build.
|
||||
* This does not decode JSON, discover modules or instantiate settings.
|
||||
*/
|
||||
public final class ModuleRegistry implements FeatureRegistry {
|
||||
private final Map<Class<?>, GenerationRegistration<?, ?>> generation = new HashMap<>();
|
||||
private final Map<Class<?>, ObjectRegistration<?>> objects = new HashMap<>();
|
||||
private final Map<Class<?>, PlacementRegistration<?>> placement = new HashMap<>();
|
||||
private final Map<String, MantleRegistration<?>> mantle = new HashMap<>();
|
||||
private final Set<ResourceId> featureIds = new HashSet<>();
|
||||
private final Set<Class<?>> featureTypes = new HashSet<>();
|
||||
private final Map<Field, String> fields = new HashMap<>();
|
||||
private FeatureCatalog catalog;
|
||||
|
||||
public ModuleRegistry() {
|
||||
this(Map.of());
|
||||
}
|
||||
|
||||
public ModuleRegistry(Map<FeatureAttachment.Target, Set<String>> reservedFields) {
|
||||
reserveHostFields(reservedFields);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <F extends IrisFeature, T> void register(
|
||||
Class<F> featureType,
|
||||
Class<T> outputType,
|
||||
ModifierFactory<F, T> factory) {
|
||||
var descriptor = describe(featureType);
|
||||
var registration = new GenerationRegistration<>(descriptor, outputType, factory);
|
||||
claim(descriptor);
|
||||
generation.put(featureType, registration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <F extends IrisFeature> void registerObject(
|
||||
Class<F> featureType,
|
||||
ObjectGeneratorFactory<F> factory) {
|
||||
var descriptor = describe(featureType);
|
||||
var registration = new ObjectRegistration<>(descriptor, factory);
|
||||
claim(descriptor);
|
||||
objects.put(featureType, registration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <F extends IrisFeature> void registerPlacement(
|
||||
Class<F> featureType,
|
||||
PlacementModifierFactory<F> factory) {
|
||||
var descriptor = describe(featureType);
|
||||
if (!isPlacementFeature(descriptor)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Placement modifiers must attach only to OBJECT_PLACEMENT: " + descriptor.id());
|
||||
}
|
||||
|
||||
var registration = new PlacementRegistration<>(descriptor, factory);
|
||||
claim(descriptor);
|
||||
placement.put(featureType, registration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void registerMantle(MantleKey<T> key, MantleLifetime lifetime, MantleCodec<T> codec) {
|
||||
checkOpen();
|
||||
Objects.requireNonNull(key, "key");
|
||||
Objects.requireNonNull(codec, "codec");
|
||||
|
||||
var registration = new MantleRegistration<>(key, lifetime, codec, codec.version());
|
||||
if (mantle.putIfAbsent(key.id(), registration) != null) {
|
||||
throw new IllegalArgumentException("Duplicate mantle layer: " + key.id());
|
||||
}
|
||||
}
|
||||
|
||||
public FeatureCatalog freeze() {
|
||||
if (catalog == null) {
|
||||
catalog = new FeatureCatalog(generation, objects, placement, mantle);
|
||||
}
|
||||
return catalog;
|
||||
}
|
||||
|
||||
private void reserveHostFields(Map<FeatureAttachment.Target, Set<String>> reservedFields) {
|
||||
for (var entry : reservedFields.entrySet()) {
|
||||
for (var name : entry.getValue()) {
|
||||
reserveHostField(entry.getKey(), name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void reserveHostField(FeatureAttachment.Target target, String name) {
|
||||
new FeatureAttachment(name, Set.of(target));
|
||||
fields.put(new Field(target, name), "host");
|
||||
}
|
||||
|
||||
private <F extends IrisFeature> FeatureDescriptor<F> describe(Class<F> type) {
|
||||
checkOpen();
|
||||
Objects.requireNonNull(type, "featureType");
|
||||
validateFeatureType(type);
|
||||
|
||||
var annotation = type.getDeclaredAnnotation(Feature.class);
|
||||
if (annotation == null) {
|
||||
throw new IllegalArgumentException("Missing @Feature: " + type.getName());
|
||||
}
|
||||
|
||||
return new FeatureDescriptor<>(
|
||||
ResourceId.parse(annotation.id()),
|
||||
annotation.name(),
|
||||
type,
|
||||
new FeatureAttachment(annotation.field(), Set.of(annotation.targets())),
|
||||
OptionsMetadata.inspect(type));
|
||||
}
|
||||
|
||||
private static void validateFeatureType(Class<? extends IrisFeature> type) {
|
||||
if (!IrisFeature.class.isAssignableFrom(type)
|
||||
|| type.isInterface()
|
||||
|| Modifier.isAbstract(type.getModifiers())) {
|
||||
throw new IllegalArgumentException(
|
||||
"Feature type must be a concrete IrisFeature: " + type.getName());
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isPlacementFeature(FeatureDescriptor<?> descriptor) {
|
||||
return descriptor.attachment().targets().equals(Set.of(FeatureAttachment.Target.OBJECT_PLACEMENT));
|
||||
}
|
||||
|
||||
private void claim(FeatureDescriptor<?> descriptor) {
|
||||
validateClaim(descriptor);
|
||||
recordClaim(descriptor);
|
||||
}
|
||||
|
||||
private void validateClaim(FeatureDescriptor<?> descriptor) {
|
||||
if (featureTypes.contains(descriptor.type()) || featureIds.contains(descriptor.id())) {
|
||||
throw new IllegalArgumentException("Duplicate feature: " + descriptor.id());
|
||||
}
|
||||
|
||||
for (var target : descriptor.attachment().targets()) {
|
||||
var field = new Field(target, descriptor.attachment().field());
|
||||
if (fields.containsKey(field)) {
|
||||
throw new IllegalArgumentException(
|
||||
"JSON field collision at " + field + ": " + fields.get(field));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void recordClaim(FeatureDescriptor<?> descriptor) {
|
||||
featureTypes.add(descriptor.type());
|
||||
featureIds.add(descriptor.id());
|
||||
for (var target : descriptor.attachment().targets()) {
|
||||
var field = new Field(target, descriptor.attachment().field());
|
||||
fields.put(field, descriptor.id().toString());
|
||||
}
|
||||
}
|
||||
|
||||
private void checkOpen() {
|
||||
if (catalog != null) {
|
||||
throw new IllegalStateException("Registration has ended");
|
||||
}
|
||||
}
|
||||
|
||||
private record Field(FeatureAttachment.Target target, String name) {}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.object.ObjectGenerator;
|
||||
import com.volmit.object.ObjectGeneratorFactory;
|
||||
import java.util.Objects;
|
||||
|
||||
public record ObjectRegistration<F extends IrisFeature>(
|
||||
FeatureDescriptor<F> descriptor,
|
||||
ObjectGeneratorFactory<F> factory) {
|
||||
public ObjectRegistration {
|
||||
Objects.requireNonNull(descriptor, "descriptor");
|
||||
Objects.requireNonNull(factory, "factory");
|
||||
}
|
||||
|
||||
public ObjectGenerator configure(F feature) {
|
||||
var configuredFeature = descriptor.type().cast(Objects.requireNonNull(feature, "feature"));
|
||||
return Objects.requireNonNull(factory.create(configuredFeature), "generator");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
/** Orders configured occurrences by dependency identity, then by occurrence ID. */
|
||||
final class OccurrenceOrder {
|
||||
private OccurrenceOrder() {}
|
||||
|
||||
static <T> List<T> order(
|
||||
List<ResourceId> orderedIds,
|
||||
List<T> occurrences,
|
||||
Function<? super T, ResourceId> identity,
|
||||
Function<? super T, ResourceId> occurrenceId) {
|
||||
Map<ResourceId, List<T>> occurrencesByIdentity = groupByIdentity(occurrences, identity);
|
||||
var ordered = new ArrayList<T>(occurrences.size());
|
||||
|
||||
for (var id : orderedIds) {
|
||||
var group = occurrencesByIdentity.get(id);
|
||||
if (group == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
group.sort((left, right) -> occurrenceId.apply(left).compareTo(occurrenceId.apply(right)));
|
||||
ordered.addAll(group);
|
||||
}
|
||||
|
||||
return List.copyOf(ordered);
|
||||
}
|
||||
|
||||
private static <T> Map<ResourceId, List<T>> groupByIdentity(
|
||||
List<T> occurrences,
|
||||
Function<? super T, ResourceId> identity) {
|
||||
var occurrencesByIdentity = new HashMap<ResourceId, List<T>>();
|
||||
for (var occurrence : occurrences) {
|
||||
occurrencesByIdentity.computeIfAbsent(identity.apply(occurrence), ignored -> new ArrayList<>())
|
||||
.add(occurrence);
|
||||
}
|
||||
return occurrencesByIdentity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,374 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.generation.Dependencies;
|
||||
import com.volmit.mantle.BlockBox;
|
||||
import com.volmit.object.ObjectBlock;
|
||||
import com.volmit.placement.PlacedBlock;
|
||||
import com.volmit.placement.PlacedMarker;
|
||||
import com.volmit.placement.PlacementAccess;
|
||||
import com.volmit.placement.PlacementContext;
|
||||
import com.volmit.placement.PlacementEdit;
|
||||
import com.volmit.placement.PlacementModifier;
|
||||
import com.volmit.placement.PlacementPlan;
|
||||
import com.volmit.placement.PlacementRequest;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.world.BlockPosition;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/** Reference placement orchestration, resolved at pack load. Same-ID instances must agree on
|
||||
* implementation and dependencies; occurrences are unique and sorted lexically within an ID.
|
||||
* The host supplies prepared, serialized storage and enforces platform/placement policies.
|
||||
* This runner buffers all mapped candidates; host validation must reject collisions or unsupported
|
||||
* writes before execution. It does not silently skip writes, transform geometry or place live blocks.
|
||||
*/
|
||||
public final class PlacementPipeline {
|
||||
private final List<ConfiguredPlacementModifier> ordered;
|
||||
|
||||
private PlacementPipeline(List<ConfiguredPlacementModifier> ordered) {
|
||||
this.ordered = List.copyOf(ordered);
|
||||
}
|
||||
|
||||
public static PlacementPipeline resolve(List<ConfiguredPlacementModifier> modifiers) {
|
||||
var instances = List.copyOf(modifiers);
|
||||
var declarations = collectDeclarations(instances);
|
||||
var modifierIds = DependencyOrder.resolve(declarations);
|
||||
var orderedOccurrences = OccurrenceOrder.order(
|
||||
modifierIds,
|
||||
instances,
|
||||
ConfiguredPlacementModifier::id,
|
||||
ConfiguredPlacementModifier::occurrence);
|
||||
return new PlacementPipeline(orderedOccurrences);
|
||||
}
|
||||
|
||||
private static Map<ResourceId, Dependencies> collectDeclarations(
|
||||
List<ConfiguredPlacementModifier> instances) {
|
||||
var representatives = new HashMap<ResourceId, ConfiguredPlacementModifier>();
|
||||
var occurrences = new HashSet<ResourceId>();
|
||||
var declarations = new HashMap<ResourceId, Dependencies>();
|
||||
|
||||
for (var modifier : instances) {
|
||||
checkUniqueOccurrence(modifier, occurrences);
|
||||
checkCompatibleIdentity(modifier, representatives);
|
||||
declarations.put(modifier.id(), modifier.dependencies());
|
||||
}
|
||||
return declarations;
|
||||
}
|
||||
|
||||
private static void checkUniqueOccurrence(
|
||||
ConfiguredPlacementModifier modifier,
|
||||
Set<ResourceId> occurrences) {
|
||||
if (!occurrences.add(modifier.occurrence())) {
|
||||
throw new IllegalArgumentException("Duplicate placement occurrence: " + modifier.occurrence());
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkCompatibleIdentity(
|
||||
ConfiguredPlacementModifier modifier,
|
||||
Map<ResourceId, ConfiguredPlacementModifier> representatives) {
|
||||
var previous = representatives.putIfAbsent(modifier.id(), modifier);
|
||||
if (previous == null || isCompatible(previous, modifier)) {
|
||||
return;
|
||||
}
|
||||
throw new IllegalArgumentException("Conflicting placement identity: " + modifier.id());
|
||||
}
|
||||
|
||||
private static boolean isCompatible(
|
||||
ConfiguredPlacementModifier first,
|
||||
ConfiguredPlacementModifier second) {
|
||||
return first.modifier().getClass().equals(second.modifier().getClass())
|
||||
&& first.dependencies().equals(second.dependencies());
|
||||
}
|
||||
|
||||
public List<ConfiguredPlacementModifier> ordered() {
|
||||
return ordered;
|
||||
}
|
||||
|
||||
public int writeRadius() {
|
||||
return ordered.stream()
|
||||
.mapToInt(ConfiguredPlacementModifier::writeRadius)
|
||||
.max()
|
||||
.orElse(0);
|
||||
}
|
||||
|
||||
/** Takes ownership of a fresh edit, closing it on success, rejection or exception.
|
||||
* Contexts are opened once per occurrence and must use dedicated RNGs from one pack snapshot.
|
||||
* The caller releases those invocation views after this call. The host must prepare the full
|
||||
* footprint plus declared reach; a prepare hook that moves outside it fails without clipping.
|
||||
* hostValidation must be read-only and covers collisions, support, protection, state/payload
|
||||
* validity and any policies beyond modifier validation. Callbacks must not commit the edit.
|
||||
*/
|
||||
public Optional<PlacementPlan> place(
|
||||
PlacementRequest request,
|
||||
PlacementEdit edit,
|
||||
Function<ConfiguredPlacementModifier, PlacementContext> contexts,
|
||||
Predicate<PlacementPlan> hostValidation) {
|
||||
Objects.requireNonNull(edit, "edit");
|
||||
try (edit) {
|
||||
requireInputs(request, contexts, hostValidation);
|
||||
var invocations = openInvocations(contexts);
|
||||
var preparedRequest = prepareAll(request, invocations);
|
||||
if (preparedRequest.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
var prepared = preparedRequest.orElseThrow();
|
||||
checkPreparedArea(prepared, edit);
|
||||
var plan = createPlan(prepared, invocations, edit);
|
||||
if (!validatePlan(plan, invocations, hostValidation)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
var hooks = openHookInvocations(prepared, invocations, edit);
|
||||
runBeforeHooks(plan, hooks);
|
||||
writePlan(plan, edit);
|
||||
runAfterHooks(plan, hooks);
|
||||
edit.commit();
|
||||
return Optional.of(plan);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireInputs(
|
||||
PlacementRequest request,
|
||||
Function<ConfiguredPlacementModifier, PlacementContext> contexts,
|
||||
Predicate<PlacementPlan> hostValidation) {
|
||||
Objects.requireNonNull(request, "request");
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
Objects.requireNonNull(hostValidation, "hostValidation");
|
||||
}
|
||||
|
||||
private List<PlacementInvocation> openInvocations(
|
||||
Function<ConfiguredPlacementModifier, PlacementContext> contexts) {
|
||||
var invocations = new ArrayList<PlacementInvocation>(ordered.size());
|
||||
for (var modifier : ordered) {
|
||||
var context = Objects.requireNonNull(contexts.apply(modifier), "context");
|
||||
invocations.add(new PlacementInvocation(modifier, context));
|
||||
}
|
||||
return List.copyOf(invocations);
|
||||
}
|
||||
|
||||
private static Optional<PlacementRequest> prepareAll(
|
||||
PlacementRequest initialRequest,
|
||||
List<PlacementInvocation> invocations) {
|
||||
var attemptId = initialRequest.id();
|
||||
var request = initialRequest;
|
||||
|
||||
for (var invocation : invocations) {
|
||||
var prepared = Objects.requireNonNull(
|
||||
invocation.modifier().modifier().prepare(invocation.context(), request),
|
||||
"prepared request");
|
||||
if (prepared.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
request = prepared.orElseThrow();
|
||||
if (!request.id().equals(attemptId)) {
|
||||
throw new IllegalArgumentException("Prepare must preserve placement attempt ID");
|
||||
}
|
||||
}
|
||||
return Optional.of(request);
|
||||
}
|
||||
|
||||
private PlacementPlan createPlan(
|
||||
PlacementRequest request,
|
||||
List<PlacementInvocation> invocations,
|
||||
PlacementEdit edit) {
|
||||
var blocks = mapBlocks(request, invocations);
|
||||
var markers = mapMarkers(request);
|
||||
var plan = new PlacementPlan(request, blocks, markers);
|
||||
|
||||
// Compute checked half-open bounds before any mutation, including integer extremes.
|
||||
plan.writtenBounds();
|
||||
validateMappedPositions(plan, request, edit, writeRadius());
|
||||
return plan;
|
||||
}
|
||||
|
||||
private static List<PlacedBlock> mapBlocks(
|
||||
PlacementRequest request,
|
||||
List<PlacementInvocation> invocations) {
|
||||
var mappedBlocks = new ArrayList<PlacedBlock>();
|
||||
for (var block : request.object().blocks()) {
|
||||
var mapped = mapBlock(request, block, invocations);
|
||||
mapped.ifPresent(mappedBlocks::add);
|
||||
}
|
||||
return mappedBlocks;
|
||||
}
|
||||
|
||||
private static Optional<PlacedBlock> mapBlock(
|
||||
PlacementRequest request,
|
||||
ObjectBlock block,
|
||||
List<PlacementInvocation> invocations) {
|
||||
Optional<PlacedBlock> mapped = Optional.of(new PlacedBlock(
|
||||
request.worldPosition(block.position()),
|
||||
block.state(),
|
||||
block.blockEntity()));
|
||||
|
||||
for (var invocation : invocations) {
|
||||
var modifier = invocation.modifier().modifier();
|
||||
var next = Objects.requireNonNull(
|
||||
modifier.mapBlock(invocation.context(), mapped.orElseThrow()),
|
||||
"mapped block");
|
||||
if (next.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
mapped = next;
|
||||
}
|
||||
return mapped;
|
||||
}
|
||||
|
||||
private static List<PlacedMarker> mapMarkers(PlacementRequest request) {
|
||||
var markers = new ArrayList<PlacedMarker>();
|
||||
for (var marker : request.object().markers()) {
|
||||
markers.add(new PlacedMarker(
|
||||
request.worldPosition(marker.position()),
|
||||
marker.type(),
|
||||
marker.attributes()));
|
||||
}
|
||||
return markers;
|
||||
}
|
||||
|
||||
private static void validateMappedPositions(
|
||||
PlacementPlan plan,
|
||||
PlacementRequest request,
|
||||
PlacementEdit edit,
|
||||
int radius) {
|
||||
for (var block : plan.blocks()) {
|
||||
checkPosition(block.position(), request.footprint(), edit.bounds(), radius);
|
||||
}
|
||||
for (var marker : plan.markers()) {
|
||||
checkPosition(marker.position(), request.footprint(), edit.bounds(), radius);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean validatePlan(
|
||||
PlacementPlan plan,
|
||||
List<PlacementInvocation> invocations,
|
||||
Predicate<PlacementPlan> hostValidation) {
|
||||
if (!hostValidation.test(plan)) {
|
||||
return false;
|
||||
}
|
||||
for (var invocation : invocations) {
|
||||
if (!invocation.modifier().modifier().validate(invocation.context(), plan)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private List<HookInvocation> openHookInvocations(
|
||||
PlacementRequest request,
|
||||
List<PlacementInvocation> invocations,
|
||||
PlacementEdit edit) {
|
||||
var hooks = new ArrayList<HookInvocation>(invocations.size());
|
||||
for (var invocation : invocations) {
|
||||
var access = hookAccess(invocation.modifier(), request, edit);
|
||||
hooks.add(new HookInvocation(invocation, access));
|
||||
}
|
||||
return List.copyOf(hooks);
|
||||
}
|
||||
|
||||
private static void runBeforeHooks(PlacementPlan plan, List<HookInvocation> hooks) {
|
||||
for (var hook : hooks) {
|
||||
hook.invocation().modifier().modifier().beforePlace(
|
||||
hook.invocation().context(),
|
||||
hook.access(),
|
||||
plan);
|
||||
}
|
||||
}
|
||||
|
||||
private static void writePlan(PlacementPlan plan, PlacementEdit edit) {
|
||||
for (var block : plan.blocks()) {
|
||||
edit.set(block);
|
||||
}
|
||||
for (var marker : plan.markers()) {
|
||||
edit.marker(marker);
|
||||
}
|
||||
}
|
||||
|
||||
private static void runAfterHooks(PlacementPlan plan, List<HookInvocation> hooks) {
|
||||
for (var hook : hooks) {
|
||||
hook.invocation().modifier().modifier().afterPlace(
|
||||
hook.invocation().context(),
|
||||
hook.access(),
|
||||
plan);
|
||||
}
|
||||
}
|
||||
|
||||
private static PlacementAccess hookAccess(
|
||||
ConfiguredPlacementModifier modifier,
|
||||
PlacementRequest request,
|
||||
PlacementEdit edit) {
|
||||
var footprint = request.footprint();
|
||||
int radius = modifier.writeRadius();
|
||||
var bounds = new BlockBox(
|
||||
Math.subtractExact(footprint.minX(), radius),
|
||||
edit.bounds().minY(),
|
||||
Math.subtractExact(footprint.minZ(), radius),
|
||||
Math.addExact(footprint.maxX(), radius),
|
||||
edit.bounds().maxY(),
|
||||
Math.addExact(footprint.maxZ(), radius));
|
||||
return PlacementViews.restrict(edit, bounds);
|
||||
}
|
||||
|
||||
private void checkPreparedArea(PlacementRequest request, PlacementEdit edit) {
|
||||
var footprint = request.footprint();
|
||||
var preparedBounds = edit.bounds();
|
||||
if (!preparedBounds.contains(footprint)
|
||||
|| !containsHorizontalReach(preparedBounds, footprint, writeRadius())) {
|
||||
throw new IndexOutOfBoundsException("Placement footprint/reach outside prepared bounds");
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkPosition(
|
||||
BlockPosition position,
|
||||
BlockBox footprint,
|
||||
BlockBox preparedBounds,
|
||||
int radius) {
|
||||
if (!preparedBounds.contains(position.x(), position.y(), position.z())
|
||||
|| !isWithinHorizontalReach(position.x(), position.z(), footprint, radius)) {
|
||||
throw new IndexOutOfBoundsException("Mapped placement write outside declared reach: " + position);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean containsHorizontalReach(BlockBox outer, BlockBox footprint, int radius) {
|
||||
return containsReachOnAxis(outer.minX(), outer.maxX(), footprint.minX(), footprint.maxX(), radius)
|
||||
&& containsReachOnAxis(outer.minZ(), outer.maxZ(), footprint.minZ(), footprint.maxZ(), radius);
|
||||
}
|
||||
|
||||
private static boolean isWithinHorizontalReach(
|
||||
int x,
|
||||
int z,
|
||||
BlockBox footprint,
|
||||
int radius) {
|
||||
return isWithinReachOnAxis(x, footprint.minX(), footprint.maxX(), radius)
|
||||
&& isWithinReachOnAxis(z, footprint.minZ(), footprint.maxZ(), radius);
|
||||
}
|
||||
|
||||
private static boolean containsReachOnAxis(
|
||||
int outerMin,
|
||||
int outerMax,
|
||||
int footprintMin,
|
||||
int footprintMax,
|
||||
int radius) {
|
||||
return (long) footprintMin - radius >= outerMin
|
||||
&& (long) footprintMax + radius <= outerMax;
|
||||
}
|
||||
|
||||
private static boolean isWithinReachOnAxis(int position, int min, int max, int radius) {
|
||||
return position >= (long) min - radius && position < (long) max + radius;
|
||||
}
|
||||
|
||||
private record PlacementInvocation(
|
||||
ConfiguredPlacementModifier modifier,
|
||||
PlacementContext context) {}
|
||||
|
||||
private record HookInvocation(PlacementInvocation invocation, PlacementAccess access) {}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.placement.PlacementModifierFactory;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PlacementRegistration<F extends IrisFeature>(
|
||||
FeatureDescriptor<F> descriptor,
|
||||
PlacementModifierFactory<F> factory) {
|
||||
public PlacementRegistration {
|
||||
Objects.requireNonNull(descriptor, "descriptor");
|
||||
Objects.requireNonNull(factory, "factory");
|
||||
}
|
||||
|
||||
public ConfiguredPlacementModifier configure(ResourceId occurrence, F feature) {
|
||||
var configuredFeature = descriptor.type().cast(Objects.requireNonNull(feature, "feature"));
|
||||
return new ConfiguredPlacementModifier(occurrence, factory.create(configuredFeature));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.block.BlockState;
|
||||
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;
|
||||
import com.volmit.placement.PlacedBlock;
|
||||
import com.volmit.placement.PlacedMarker;
|
||||
import com.volmit.placement.PlacementAccess;
|
||||
import com.volmit.placement.PlacementEdit;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Internal hook views: limit horizontal reach and hide transaction ownership. */
|
||||
final class PlacementViews {
|
||||
private PlacementViews() {}
|
||||
|
||||
static PlacementAccess restrict(PlacementEdit edit, BlockBox bounds) {
|
||||
return new RestrictedPlacementAccess(edit, bounds);
|
||||
}
|
||||
|
||||
private static Mantle restrictMantle(Mantle parent, BlockBox bounds) {
|
||||
checkArea(parent.bounds(), bounds);
|
||||
return new RestrictedMantle(parent, bounds);
|
||||
}
|
||||
|
||||
private static void checkPosition(BlockBox bounds, int x, int y, int z) {
|
||||
if (!bounds.contains(x, y, z)) {
|
||||
throw new IndexOutOfBoundsException("Placement access outside declared reach");
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkArea(BlockBox bounds, BlockBox area) {
|
||||
if (!bounds.contains(area)) {
|
||||
throw new IndexOutOfBoundsException("Placement area outside declared reach");
|
||||
}
|
||||
}
|
||||
|
||||
private static final class RestrictedPlacementAccess implements PlacementAccess {
|
||||
private final PlacementEdit edit;
|
||||
private final BlockBox allowedBounds;
|
||||
|
||||
private RestrictedPlacementAccess(PlacementEdit edit, BlockBox allowedBounds) {
|
||||
this.edit = Objects.requireNonNull(edit, "edit");
|
||||
this.allowedBounds = Objects.requireNonNull(allowedBounds, "bounds");
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockBox bounds() {
|
||||
edit.bounds();
|
||||
return allowedBounds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockState get(int x, int y, int z) {
|
||||
checkPosition(bounds(), x, y, z);
|
||||
return edit.get(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(PlacedBlock block) {
|
||||
var position = block.position();
|
||||
checkPosition(bounds(), position.x(), position.y(), position.z());
|
||||
edit.set(block);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void marker(PlacedMarker marker) {
|
||||
var position = marker.position();
|
||||
checkPosition(bounds(), position.x(), position.y(), position.z());
|
||||
edit.marker(marker);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mantle mantle() {
|
||||
return restrictMantle(edit.mantle(), bounds());
|
||||
}
|
||||
}
|
||||
|
||||
private static final class RestrictedMantle implements Mantle {
|
||||
private final Mantle parent;
|
||||
private final BlockBox allowedBounds;
|
||||
|
||||
private RestrictedMantle(Mantle parent, BlockBox allowedBounds) {
|
||||
this.parent = Objects.requireNonNull(parent, "parent");
|
||||
this.allowedBounds = Objects.requireNonNull(allowedBounds, "bounds");
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockBox bounds() {
|
||||
parent.bounds();
|
||||
return allowedBounds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MantleEdit edit() {
|
||||
bounds();
|
||||
throw new UnsupportedOperationException("Placement owns the mantle edit");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> MantleLayer<T> layer(MantleKey<T> key) {
|
||||
bounds();
|
||||
return new RestrictedMantleLayer<>(this, parent.layer(key));
|
||||
}
|
||||
}
|
||||
|
||||
private static final class RestrictedMantleLayer<T> implements MantleLayer<T> {
|
||||
private final RestrictedMantle owner;
|
||||
private final MantleLayer<T> layer;
|
||||
|
||||
private RestrictedMantleLayer(RestrictedMantle owner, MantleLayer<T> layer) {
|
||||
this.owner = Objects.requireNonNull(owner, "owner");
|
||||
this.layer = Objects.requireNonNull(layer, "layer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(int x, int y, int z) {
|
||||
checkPosition(owner.bounds(), x, y, z);
|
||||
return layer.get(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(int x, int y, int z, T value) {
|
||||
checkPosition(owner.bounds(), x, y, z);
|
||||
layer.set(x, y, z, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(int x, int y, int z) {
|
||||
checkPosition(owner.bounds(), x, y, z);
|
||||
layer.remove(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(BlockBox area, MantleVisitor<T> visitor) {
|
||||
checkArea(owner.bounds(), area);
|
||||
layer.visit(area, Objects.requireNonNull(visitor, "visitor"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear(BlockBox area) {
|
||||
checkArea(owner.bounds(), area);
|
||||
layer.clear(area);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user