diff --git a/api/src/main/java/com/volmit/ModuleSetup.java b/api/src/main/java/com/volmit/ModuleSetup.java index 3cad65821..efb917c89 100644 --- a/api/src/main/java/com/volmit/ModuleSetup.java +++ b/api/src/main/java/com/volmit/ModuleSetup.java @@ -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(); } diff --git a/api/src/main/java/com/volmit/tooling/SceneEntry.java b/api/src/main/java/com/volmit/tooling/SceneEntry.java new file mode 100644 index 000000000..91b62aca8 --- /dev/null +++ b/api/src/main/java/com/volmit/tooling/SceneEntry.java @@ -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"); + } + } +} diff --git a/api/src/main/java/com/volmit/tooling/SceneView.java b/api/src/main/java/com/volmit/tooling/SceneView.java new file mode 100644 index 000000000..25268f2c0 --- /dev/null +++ b/api/src/main/java/com/volmit/tooling/SceneView.java @@ -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 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 scene(PackWorld world, String key, int variant); +} diff --git a/api/src/main/java/com/volmit/tooling/ToolingRegistry.java b/api/src/main/java/com/volmit/tooling/ToolingRegistry.java new file mode 100644 index 000000000..be1a6d29d --- /dev/null +++ b/api/src/main/java/com/volmit/tooling/ToolingRegistry.java @@ -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); +} diff --git a/host/src/main/java/com/volmit/host/FeatureCatalog.java b/host/src/main/java/com/volmit/host/FeatureCatalog.java index 7434599e2..18733e8fb 100644 --- a/host/src/main/java/com/volmit/host/FeatureCatalog.java +++ b/host/src/main/java/com/volmit/host/FeatureCatalog.java @@ -41,6 +41,7 @@ public final class FeatureCatalog { private final Map, List> registrationsByService; private final Map, List> providersByService; private final Optional placementType; + private final List sceneViews; FeatureCatalog( Map, FeatureDescriptor> settings, @@ -50,8 +51,10 @@ public final class FeatureCatalog { List validators, List lifecycles, List services, - PlacementTypeRegistration placementType) { + PlacementTypeRegistration placementType, + Collection 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 sceneViews() { + return sceneViews; + } + /** The lifecycle hooks modules registered, by module id, each module's in the order it registered them. */ public List lifecycles() { return lifecycles; diff --git a/host/src/main/java/com/volmit/host/ModuleRegistry.java b/host/src/main/java/com/volmit/host/ModuleRegistry.java index d0ba59a87..a14d35198 100644 --- a/host/src/main/java/com/volmit/host/ModuleRegistry.java +++ b/host/src/main/java/com/volmit/host/ModuleRegistry.java @@ -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, FeatureDescriptor> settings = new HashMap<>(); private final Map, ObjectRegistration> objects = new HashMap<>(); private final Map> mantle = new HashMap<>(); @@ -45,6 +47,7 @@ public final class ModuleRegistry implements FeatureRegistry { private final List validators = new ArrayList<>(); private final List lifecycles = new ArrayList<>(); private final List services = new ArrayList<>(); + private final Map sceneViews = new HashMap<>(); private final Set reservedFolders; private final List 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; } diff --git a/host/src/main/java/com/volmit/host/ModuleSetups.java b/host/src/main/java/com/volmit/host/ModuleSetups.java index 6b3f90c0b..a37f6f4c7 100644 --- a/host/src/main/java/com/volmit/host/ModuleSetups.java +++ b/host/src/main/java/com/volmit/host/ModuleSetups.java @@ -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 {} } diff --git a/host/src/main/java/com/volmit/host/SceneViewRegistration.java b/host/src/main/java/com/volmit/host/SceneViewRegistration.java new file mode 100644 index 000000000..f83334dfd --- /dev/null +++ b/host/src/main/java/com/volmit/host/SceneViewRegistration.java @@ -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; + } +} diff --git a/host/src/test/java/com/volmit/host/ModuleRegistryTest.java b/host/src/test/java/com/volmit/host/ModuleRegistryTest.java index d4dadf0ed..498078df7 100644 --- a/host/src/test/java/com/volmit/host/ModuleRegistryTest.java +++ b/host/src/test/java/com/volmit/host/ModuleRegistryTest.java @@ -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 entries(com.volmit.world.PackWorld world) { return List.of(); } + public Optional scene(com.volmit.world.PackWorld world, String key, int variant) { + return Optional.empty(); + } + }; + } + @Test void moduleMayRegisterItsOwnIdAndIdsBelowIt() { var registry = new ModuleRegistry();