mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
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:
1 parent
f57c2ce92c
commit
a658cd2446
9 files changed
+164
-10
No files matched your search
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user