mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Give tools a platform-neutral world api and let modules ask each other's services
PackWorld, ValidationContext and ToolPack answer services(type) with the pack's providers. com.volmit.tooling.world lets a tool open a painter-backed world and drive its players, menus, items and block changes; ToolPack.worlds() and Sender.player() reach it. The Bukkit adapter implements it over the studio world creation with a painter slot keyed by world, one protection listener, VolmLib menus, persistent-data items and BoardSVC sidebars. Other platforms answer that tool worlds are not supported yet.
This commit is contained in:
1 parent
a81a3abb9f
commit
3691611c6c
76 files changed
+4533
-23
No files matched your search
@@ -13,7 +13,8 @@ import java.util.TreeSet;
|
||||
* {@code com.volmitx}. The host side of IrisApi, {@code com.volmit.host}, lies inside the API's
|
||||
* root package but is never exposed: it loads and runs modules, and no module may reach it. The
|
||||
* platform packages of the tooling api, {@code com.volmit.tooling.<platform>}, are never exposed either:
|
||||
* only a tooling part may use them. The neutral {@code com.volmit.tooling} stays exposed.
|
||||
* only a tooling part may use them. The neutral {@code com.volmit.tooling} stays exposed, and so does the world api for
|
||||
* tools, {@code com.volmit.tooling.world}, which every platform implements.
|
||||
*/
|
||||
final class ExposedPackages {
|
||||
/** The root package of the Iris API. Module classes must not use it. */
|
||||
@@ -22,6 +23,9 @@ final class ExposedPackages {
|
||||
static final String HOST_PACKAGE = ExposedPackages.class.getPackageName();
|
||||
/** The neutral tooling api. Each platform's own api is a subpackage of it, such as {@code com.volmit.tooling.bukkit}. */
|
||||
static final String TOOLING_PACKAGE = API_PACKAGE + ".tooling";
|
||||
/** The world api for tools, which every platform implements; it is neutral, so modules see it. */
|
||||
static final String TOOL_WORLD_PACKAGE = TOOLING_PACKAGE + ".world";
|
||||
private static final PackageName TOOL_WORLD = new PackageName(TOOL_WORLD_PACKAGE);
|
||||
private static final PackageName API = new PackageName(API_PACKAGE);
|
||||
private static final PackageName HOST = new PackageName(HOST_PACKAGE);
|
||||
private static final PackageName TOOLING = new PackageName(TOOLING_PACKAGE);
|
||||
@@ -77,9 +81,12 @@ final class ExposedPackages {
|
||||
return !HOST.contains(dottedName) && packages.stream().anyMatch(name -> name.contains(dottedName));
|
||||
}
|
||||
|
||||
/** Whether the package lies below {@code com.volmit.tooling}, where each platform's api has its package. */
|
||||
/** Whether the package lies below {@code com.volmit.tooling}, where each platform's api has its package. The
|
||||
* platform-neutral world api for tools, {@code com.volmit.tooling.world}, lies there too but is not a platform's.
|
||||
*/
|
||||
private static boolean isToolingPlatformPackage(String packageName) {
|
||||
return TOOLING.contains(packageName);
|
||||
boolean isToolWorldApi = packageName.equals(TOOL_WORLD_PACKAGE) || TOOL_WORLD.contains(packageName);
|
||||
return TOOLING.contains(packageName) && !isToolWorldApi;
|
||||
}
|
||||
|
||||
/** What precedes the last separator: the package of a class name, or the directory of a resource path. */
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.volmit.placement.Placements;
|
||||
import com.volmit.resource.PackResources;
|
||||
import com.volmit.world.PackWorld;
|
||||
import com.volmit.world.RunningWorld;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
@@ -72,6 +73,11 @@ final class ModuleWorld implements PackWorld {
|
||||
return world.extension(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> List<S> services(Class<S> service) {
|
||||
return world.services(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModuleLog log() {
|
||||
return log;
|
||||
|
||||
@@ -42,6 +42,8 @@ class ExposedPackagesTest {
|
||||
assertTrue(exposed.exposes("com.volmit.tooling.Command$Builder"));
|
||||
assertFalse(exposed.exposes("com.volmit.tooling.bukkit.BukkitToolPart"));
|
||||
assertFalse(exposed.exposes("com.volmit.tooling.modded.deep.ModdedToolPart"));
|
||||
assertTrue(exposed.exposes("com.volmit.tooling.world.ToolWorld"), "the world api for tools is neutral");
|
||||
assertFalse(exposed.exposes("com.volmit.tooling.worldly.BukkitToolPart"));
|
||||
assertTrue(exposed.exposes("com.volmit.toolingx.Other"));
|
||||
assertTrue(exposed.exposes("com.volmit.tool.bukkit.Other"));
|
||||
}
|
||||
|
||||
@@ -88,6 +88,7 @@ class ExtensionsTest {
|
||||
public Optional<RunningWorld> running() { return Optional.empty(); }
|
||||
public Placements placements() { throw new AssertionError("Unused placements"); }
|
||||
public <T> T state(Class<T> type, Function<? super PackWorld, ? extends T> create) { return states.state(type, () -> create.apply(this)); }
|
||||
public <S> List<S> services(Class<S> service) { return List.of(); }
|
||||
public ModuleLog log() { return ModuleLogs.discarding(); }
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.FeatureRegistry;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.ModuleSetup;
|
||||
import com.volmit.service.Providers;
|
||||
import com.volmit.service.Service;
|
||||
import com.volmit.support.TestWorld;
|
||||
import com.volmit.world.PackWorld;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** A module asks another module's service through the world: {@code PackWorld.services}, which the validation context and
|
||||
* the tool pack answer the same way.
|
||||
*/
|
||||
class PackWorldServicesTest {
|
||||
@Service(Providers.ALL)
|
||||
interface Greeting { String greet(); }
|
||||
@Service(Providers.ONE)
|
||||
interface Clock { long now(); }
|
||||
interface NotAService { void run(); }
|
||||
|
||||
private static void registerAs(ModuleRegistry registry, String moduleId, Consumer<FeatureRegistry> registrations) {
|
||||
ModuleSetups.run(DiscoveredModule.builtIn(new IrisModule() {
|
||||
public ModuleInfo info() { return ModuleInfo.of(moduleId, "Test module " + moduleId, "1"); }
|
||||
public void setup(ModuleSetup setup) { registrations.accept(setup.registry()); }
|
||||
}), new StageGraphBuilder(), registry);
|
||||
}
|
||||
|
||||
private static FeatureCatalog catalogOf(Greeting zeta, Greeting alphaFirst, Greeting alphaSecond) {
|
||||
var registry = new ModuleRegistry();
|
||||
registerAs(registry, "test:zeta", services -> services.registerService(Greeting.class, zeta));
|
||||
registerAs(registry, "test:alpha", services -> {
|
||||
services.registerService(Greeting.class, alphaFirst);
|
||||
services.registerService(Greeting.class, alphaSecond);
|
||||
});
|
||||
return registry.freeze();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aWorldAnswersWithTheProvidersOfItsPacksModulesInModuleIdOrder() {
|
||||
Greeting zeta = () -> "zeta";
|
||||
Greeting alphaFirst = () -> "alpha 1";
|
||||
Greeting alphaSecond = () -> "alpha 2";
|
||||
try (var world = new TestWorld(catalogOf(zeta, alphaFirst, alphaSecond))) {
|
||||
assertEquals(List.of(alphaFirst, alphaSecond, zeta), world.services(Greeting.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aServiceNobodyProvidesHasNoProviders() {
|
||||
try (var world = new TestWorld(catalogOf(() -> "zeta", () -> "alpha 1", () -> "alpha 2"))) {
|
||||
assertEquals(List.of(), world.services(Clock.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInterfaceThatIsNotAServiceIsRefused() {
|
||||
try (var world = new TestWorld(catalogOf(() -> "zeta", () -> "alpha 1", () -> "alpha 2"))) {
|
||||
assertThrows(IllegalArgumentException.class, () -> world.services(NotAService.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void theViewAModuleGetsOfAWorldAnswersLikeTheWorld() {
|
||||
Greeting zeta = () -> "zeta";
|
||||
Greeting alphaFirst = () -> "alpha 1";
|
||||
Greeting alphaSecond = () -> "alpha 2";
|
||||
var catalog = catalogOf(zeta, alphaFirst, alphaSecond);
|
||||
try (var world = new TestWorld(catalog)) {
|
||||
PackWorld view = catalog.registrations(Greeting.class).getFirst().scope(world);
|
||||
|
||||
assertNotSame(world, view);
|
||||
assertEquals(world.services(Greeting.class), view.services(Greeting.class));
|
||||
assertEquals(List.of(alphaFirst, alphaSecond, zeta), view.services(Greeting.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,27 +4,38 @@ import com.volmit.IrisFeature;
|
||||
import com.volmit.ModuleLog;
|
||||
import com.volmit.config.PackSettings;
|
||||
import com.volmit.config.SettingsStack;
|
||||
import com.volmit.host.FeatureCatalog;
|
||||
import com.volmit.host.ModuleLogs;
|
||||
import com.volmit.host.WorldStates;
|
||||
import com.volmit.placement.Placements;
|
||||
import com.volmit.resource.PackResources;
|
||||
import com.volmit.world.PackWorld;
|
||||
import com.volmit.world.RunningWorld;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
/** A world for tests: the test seed, no resources, no declared settings, and a real state store. */
|
||||
/** A world for tests: the test seed, no resources, no declared settings, a real state store, and the service providers of
|
||||
* a catalog when it is given one.
|
||||
*/
|
||||
public final class TestWorld implements PackWorld, AutoCloseable {
|
||||
private final WorldStates states;
|
||||
private final ModuleLog log;
|
||||
private final FeatureCatalog catalog;
|
||||
|
||||
public TestWorld() {
|
||||
this(new WorldStates(), ModuleLogs.discarding());
|
||||
this(null);
|
||||
}
|
||||
|
||||
private TestWorld(WorldStates states, ModuleLog log) {
|
||||
/** A world whose services are those {@code catalog} holds. */
|
||||
public TestWorld(FeatureCatalog catalog) {
|
||||
this(new WorldStates(), ModuleLogs.discarding(), catalog);
|
||||
}
|
||||
|
||||
private TestWorld(WorldStates states, ModuleLog log, FeatureCatalog catalog) {
|
||||
this.states = states;
|
||||
this.log = log;
|
||||
this.catalog = catalog;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,6 +81,11 @@ public final class TestWorld implements PackWorld, AutoCloseable {
|
||||
return states.state(type, () -> create.apply(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> List<S> services(Class<S> service) {
|
||||
return catalog == null ? List.of() : catalog.services(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModuleLog log() {
|
||||
return log;
|
||||
|
||||
Reference in new issue
Block a user