Let modules show block scenes in the studio

ModuleSetup.tooling() gives a ToolingRegistry, where a module registers
SceneViews: a catalogue of entries, each with variants the studio draws
in 3D, built from the pack for a world. View ids follow the module's id
and are unique; the frozen catalogue lists them by id.
This commit is contained in:
piexel committed 2026-11-06 01:18:45 +01:00
1 parent f57c2ce92c
commit a658cd2446
9 files changed
+164 -10

No files matched your search

@@ -1,16 +1,19 @@
package com.volmit;
import com.volmit.stage.StageGraph;
import com.volmit.tooling.ToolingRegistry;
/** What a module gets in {@link IrisModule#setup}: the stage graph it defines its stages in, the registry for
* everything else, and its own log. Stages and registrations are accepted only while {@code setup} runs; the log
* may be kept and used later, such as from lifecycle hooks.
* everything else it adds to generation, the registry for its studio tools, and its own log. Stages and registrations
* are accepted only while {@code setup} runs; the log may be kept and used later, such as from lifecycle hooks.
*/
public interface ModuleSetup {
StageGraph stages();
FeatureRegistry registry();
ToolingRegistry tooling();
/** This module's log, the same one its stages and lifecycle hooks see through their world. */
ModuleLog log();
}
@@ -0,0 +1,25 @@
package com.volmit.tooling;
import java.util.Objects;
/**
* One entry of a {@link SceneView}'s catalogue.
*
* @param key what the view names the entry by in {@link SceneView#scene}, such as an object's {@code trees/oak}
* @param name what the studio lists it as
* @param group the folder the studio lists it under, such as {@code trees}; blank for none
* @param variants how many scenes the entry has, at least 1
*/
public record SceneEntry(String key, String name, String group, int variants) {
public SceneEntry {
Objects.requireNonNull(key, "key");
Objects.requireNonNull(name, "name");
Objects.requireNonNull(group, "group");
if (key.isBlank()) {
throw new IllegalArgumentException("A scene entry needs a key");
}
if (variants < 1) {
throw new IllegalArgumentException("Scene entry " + key + " has " + variants + " variants; an entry has at least 1");
}
}
}
@@ -0,0 +1,35 @@
package com.volmit.tooling;
import com.volmit.object.GeneratedObject;
import com.volmit.resource.ResourceId;
import com.volmit.world.PackWorld;
import java.util.List;
import java.util.Optional;
/**
* A studio view of the block scenes a module builds from the pack, such as its objects: a catalogue of entries, each
* with variants the studio draws in 3D. The studio asks on its own threads, several at once, for a world of the pack
* it shows; a view keeps nothing between calls that the world's state ({@link PackWorld#state}) should keep.
*/
public interface SceneView {
/** The module's id, or one starting with it and {@code /}, such as {@code iris:objects}. */
ResourceId id();
/** The view's name in the studio's list of views, such as "Objects". */
String label();
/** One line on what the view shows. */
String description();
/** The view's entries in the world's pack, in the order the studio lists them. */
List<SceneEntry> entries(PackWorld world);
/**
* The blocks of one variant of the entry named {@code key}, its anchor where the studio centres the scene; empty
* when the pack has no such entry, or the entry has fewer variants.
*
* @param variant from 0 to the entry's {@link SceneEntry#variants()}, exclusive
*/
Optional<GeneratedObject> scene(PackWorld world, String key, int variant);
}
@@ -0,0 +1,11 @@
package com.volmit.tooling;
/**
* Where a module adds its tools to the studio, given in {@link com.volmit.ModuleSetup#tooling()} and accepted only while
* {@link com.volmit.IrisModule#setup} runs. A view's id is the module's id or starts with it and {@code /}; a second
* view with an id already registered fails the pack load, naming both modules.
*/
public interface ToolingRegistry {
/** Adds a view of block scenes the module builds from the pack, such as its objects. */
void registerSceneView(SceneView view);
}
@@ -41,6 +41,7 @@ public final class FeatureCatalog {
private final Map<Class<?>, List<ServiceRegistration>> registrationsByService;
private final Map<Class<?>, List<?>> providersByService;
private final Optional<PlacementTypeRegistration> placementType;
private final List<SceneViewRegistration> sceneViews;
FeatureCatalog(
Map<Class<?>, FeatureDescriptor<?>> settings,
@@ -50,8 +51,10 @@ public final class FeatureCatalog {
List<ValidatorRegistration> validators,
List<LifecycleRegistration> lifecycles,
List<ServiceRegistration> services,
PlacementTypeRegistration placementType) {
PlacementTypeRegistration placementType,
Collection<SceneViewRegistration> sceneViews) {
this.placementType = Optional.ofNullable(placementType);
this.sceneViews = sorted(sceneViews, registration -> registration.view().id());
this.settings = Map.copyOf(settings);
this.objects = Map.copyOf(objects);
this.mantle = Map.copyOf(mantle);
@@ -90,6 +93,11 @@ public final class FeatureCatalog {
return validators;
}
/** The studio scene views modules registered, by view id. */
public List<SceneViewRegistration> sceneViews() {
return sceneViews;
}
/** The lifecycle hooks modules registered, by module id, each module's in the order it registered them. */
public List<LifecycleRegistration> lifecycles() {
return lifecycles;
@@ -11,6 +11,8 @@ import com.volmit.object.ObjectGeneratorFactory;
import com.volmit.resource.ResourceId;
import com.volmit.resource.ResourceKind;
import com.volmit.service.Providers;
import com.volmit.tooling.SceneView;
import com.volmit.tooling.ToolingRegistry;
import com.volmit.validation.Validator;
import java.util.ArrayList;
import java.util.HashMap;
@@ -29,11 +31,11 @@ import java.util.Set;
* settings ids must be the registering module's id or start with it and '/' ({@link com.volmit.ModuleInfo#ownsId}).
* Resource kinds have unique ids, types and folders, and no kind may read a folder the host reserves for its own
* files. Lifecycle hooks and service providers are registered by the module that owns them; a service whose providers
* are {@link Providers#ONE} has at most one. 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.
* are {@link Providers#ONE} has at most one. Studio scene views have unique ids the registering module owns. 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 {
public final class ModuleRegistry implements FeatureRegistry, ToolingRegistry {
private final Map<Class<?>, FeatureDescriptor<?>> settings = new HashMap<>();
private final Map<Class<?>, ObjectRegistration<?>> objects = new HashMap<>();
private final Map<String, MantleRegistration<?>> mantle = new HashMap<>();
@@ -45,6 +47,7 @@ public final class ModuleRegistry implements FeatureRegistry {
private final List<ValidatorRegistration> validators = new ArrayList<>();
private final List<LifecycleRegistration> lifecycles = new ArrayList<>();
private final List<ServiceRegistration> services = new ArrayList<>();
private final Map<ResourceId, SceneViewRegistration> sceneViews = new HashMap<>();
private final Set<String> reservedFolders;
private final List<DiscoveredModule> registered = new ArrayList<>();
private PlacementTypeRegistration placementType;
@@ -195,6 +198,21 @@ public final class ModuleRegistry implements FeatureRegistry {
services.add(added);
}
@Override
public void registerSceneView(SceneView view) {
checkOpen();
Objects.requireNonNull(view, "view");
ResourceId id = Objects.requireNonNull(view.id(), "view id");
requireOwnedId("Scene view", id);
var added = new SceneViewRegistration(view, registering);
var existing = sceneViews.get(id);
if (existing != null) {
throw new IllegalArgumentException("Scene view " + id + " is registered by module " + existing.module()
+ " already, and module " + added.module() + " registers it too; give one of them another id");
}
sceneViews.put(id, added);
}
/** Catalogs a settings type named by a stage's when(...), whose owner {@link StageGraphBuilder}
* has checked. Several stages, and the owner's {@link #registerSettings}, may name one type. Call
* it after every module has registered, so the owner is known.
@@ -216,7 +234,7 @@ public final class ModuleRegistry implements FeatureRegistry {
public FeatureCatalog freeze() {
if (catalog == null) {
catalog = new FeatureCatalog(settings, objects, mantle, resourceKinds.values(), validators, lifecycles, services,
placementType);
placementType, sceneViews.values());
}
return catalog;
}
@@ -4,6 +4,7 @@ import com.volmit.FeatureRegistry;
import com.volmit.ModuleLog;
import com.volmit.ModuleSetup;
import com.volmit.stage.StageGraph;
import com.volmit.tooling.ToolingRegistry;
import java.util.Objects;
/** Runs a module's {@link com.volmit.IrisModule#setup} against one pack load's stage graph and registry. While it
@@ -21,7 +22,7 @@ public final class ModuleSetups {
registry.beginSetup(discovered);
boolean completed = false;
try {
discovered.module().setup(new Setup(stages, registry, discovered.log()));
discovered.module().setup(new Setup(stages, registry, registry, discovered.log()));
completed = true;
} finally {
registry.endSetup();
@@ -29,5 +30,6 @@ public final class ModuleSetups {
}
}
private record Setup(StageGraph stages, FeatureRegistry registry, ModuleLog log) implements ModuleSetup {}
private record Setup(StageGraph stages, FeatureRegistry registry, ToolingRegistry tooling, ModuleLog log)
implements ModuleSetup {}
}
@@ -0,0 +1,18 @@
package com.volmit.host;
import com.volmit.tooling.SceneView;
import java.util.Objects;
/** A studio scene view and the module that registered it. */
public record SceneViewRegistration(SceneView view, DiscoveredModule module) {
public SceneViewRegistration {
Objects.requireNonNull(view, "view");
Objects.requireNonNull(module, "module");
}
@Override
public String toString() {
return "scene view " + view.id() + " of module " + module;
}
}
@@ -118,6 +118,40 @@ class ModuleRegistryTest {
assertTrue(registry.freeze().mantle().isEmpty());
}
@Test
void sceneViewsHaveUniqueIdsTheirModuleOwnsAndAreCataloguedById() {
var registry = new ModuleRegistry();
ModuleSetups.run(DiscoveredModule.builtIn(new IrisModule() {
public ModuleInfo info() { return ModuleInfo.of("iris:objects", "Objects", "1"); }
public void setup(ModuleSetup setup) {
setup.tooling().registerSceneView(sceneView("iris:objects/procedural"));
setup.tooling().registerSceneView(sceneView("iris:objects"));
var duplicate = assertThrows(IllegalArgumentException.class,
() -> setup.tooling().registerSceneView(sceneView("iris:objects")));
assertTrue(duplicate.getMessage().contains("Scene view iris:objects is registered by module"), duplicate.getMessage());
var foreign = assertThrows(IllegalArgumentException.class,
() -> setup.tooling().registerSceneView(sceneView("iris:terrain/palettes")));
assertTrue(foreign.getMessage().contains("must be iris:objects or start with iris:objects/"), foreign.getMessage());
}
}), new StageGraphBuilder(), registry);
assertThrows(IllegalStateException.class, () -> registry.registerSceneView(sceneView("iris:objects/loose")));
assertEquals(List.of("iris:objects", "iris:objects/procedural"),
registry.freeze().sceneViews().stream().map(view -> view.view().id().toString()).toList());
}
private static com.volmit.tooling.SceneView sceneView(String id) {
return new com.volmit.tooling.SceneView() {
public com.volmit.resource.ResourceId id() { return com.volmit.resource.ResourceId.parse(id); }
public String label() { return id; }
public String description() { return id; }
public List<com.volmit.tooling.SceneEntry> entries(com.volmit.world.PackWorld world) { return List.of(); }
public Optional<com.volmit.object.GeneratedObject> scene(com.volmit.world.PackWorld world, String key, int variant) {
return Optional.empty();
}
};
}
@Test
void moduleMayRegisterItsOwnIdAndIdsBelowIt() {
var registry = new ModuleRegistry();