mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
Let modules declare pack resource kinds and validators: ResourceKind, PackResources.ids, @ResourceRef, the validation API and folders the host reserves
This commit is contained in:
1 parent
09afee8ae3
commit
ef4b640867
37 files changed
+990
-128
No files matched your search
@@ -6,6 +6,9 @@ import com.volmit.mantle.BlockBox;
|
||||
import com.volmit.object.*;
|
||||
import com.volmit.placement.PlacementRequest;
|
||||
import com.volmit.resource.*;
|
||||
import com.volmit.validation.Problem;
|
||||
import com.volmit.validation.Severity;
|
||||
import com.volmit.validation.Subject;
|
||||
import com.volmit.world.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.ArrayList;
|
||||
@@ -168,4 +171,38 @@ class ApiValuesTest {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> new PackRegion(arid.id(), "Arid", 0, land, List.of(), List.of(), List.of()));
|
||||
}
|
||||
|
||||
static final class Keep {}
|
||||
|
||||
@Test
|
||||
void resourceKindsReadOneFolderIntoAConcreteClass() {
|
||||
var kind = ResourceKind.of("test:castle/keep", "keeps", Keep.class);
|
||||
var id = ResourceId.parse("overworld:castle/main");
|
||||
|
||||
assertEquals(new ResourceKey<>(id, Keep.class), kind.key(id));
|
||||
assertTrue(ResourceKind.isFolderName("jigsaw-pools"));
|
||||
for (String folder : List.of("", ".", "..", "a/b", "Keeps", "a b")) {
|
||||
var failure = assertThrows(IllegalArgumentException.class, () -> ResourceKind.of("test:castle/keep", folder, Keep.class));
|
||||
assertTrue(failure.getMessage().startsWith("Folder '" + folder + "' of resource kind test:castle/keep is not a folder name"),
|
||||
failure.getMessage());
|
||||
}
|
||||
assertThrows(IllegalArgumentException.class, () -> ResourceKind.of("test:castle/keep", "keeps", Runnable.class));
|
||||
assertThrows(IllegalArgumentException.class, () -> ResourceKind.of("test:castle/keep", "keeps", int.class));
|
||||
assertThrows(IllegalArgumentException.class, () -> ResourceKind.of("keep", "keeps", Keep.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void problemsConcernAResourceOrAFileBelowThePackRoot() {
|
||||
var error = Problem.error(Subject.file("structures/castle.json"), "Castle never assembles.");
|
||||
var warning = Problem.warning(Subject.resource(ResourceId.parse("overworld:castle")), "Castle is large.");
|
||||
|
||||
assertEquals(Severity.ERROR, error.severity());
|
||||
assertEquals("structures/castle.json", error.subject().toString());
|
||||
assertEquals(Severity.WARNING, warning.severity());
|
||||
assertEquals("overworld:castle", warning.subject().toString());
|
||||
for (String path : List.of("", "/structures/castle.json", "../castle.json", "structures/../castle.json", "a//b", "a\\b", "./a")) {
|
||||
assertThrows(IllegalArgumentException.class, () -> Subject.file(path), path);
|
||||
}
|
||||
assertThrows(IllegalArgumentException.class, () -> Problem.error(Subject.file("a.json"), " "));
|
||||
}
|
||||
}
|
||||
@@ -135,7 +135,7 @@ class ChunkPipelineTest {
|
||||
|
||||
@Test
|
||||
void mantleStagesWriteBeyondTheirChunkAndChunkStagesReadTheirOwnPart() {
|
||||
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(new VeinModule())), Map.of(), Set.of(), Set.of());
|
||||
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(new VeinModule())), Map.of(), Set.of(), Set.of(), Set.of());
|
||||
var vein = new Entry<>(Target.BIOME, PLAINS, new VeinSettings(12, 4));
|
||||
int reach = loaded.mantle().stages().getFirst().reachBlocks(new DeclaredSettings(vein));
|
||||
assertEquals(3, reach, "a 12 block vein from the centre column runs 3 blocks into the next chunk");
|
||||
|
||||
@@ -202,7 +202,7 @@ class PlacementLifecyclePortTest {
|
||||
|
||||
@Test
|
||||
void loadingThePortCatalogsEveryConditioningSettingsType() {
|
||||
var loaded = LoadedModules.load(PlacementModules.builtIns(), Map.of(), Set.of(), Set.of());
|
||||
var loaded = LoadedModules.load(PlacementModules.builtIns(), Map.of(), Set.of(), Set.of(), Set.of());
|
||||
|
||||
assertEquals(List.of("example:moss", "iris:snow", "iris:stilt"),
|
||||
loaded.catalog().settings().stream().map(settings -> settings.id().toString()).toList());
|
||||
|
||||
@@ -30,7 +30,7 @@ class DiscoveredModuleTest {
|
||||
var module = new ShiftingIdentity();
|
||||
var discovered = DiscoveredModule.builtIn(module);
|
||||
|
||||
var loaded = LoadedModules.load(List.of(discovered), Map.of(), Set.of(), Set.of());
|
||||
var loaded = LoadedModules.load(List.of(discovered), Map.of(), Set.of(), Set.of(), Set.of());
|
||||
|
||||
assertEquals("first:core", discovered.info().id().toString());
|
||||
assertEquals(List.of("first:core/mark"),
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.volmit.host;
|
||||
import com.volmit.*;
|
||||
import com.volmit.config.Feature;
|
||||
import com.volmit.mantle.*;
|
||||
import com.volmit.resource.ResourceKind;
|
||||
import com.volmit.validation.Validator;
|
||||
import com.volmit.stage.PlacementStage;
|
||||
import com.volmit.stage.Stage;
|
||||
import com.volmit.stage.StageGraph;
|
||||
@@ -128,14 +130,14 @@ class ModuleRegistryTest {
|
||||
public void install(StageGraph graph) { graph.define(WORK).when(One.class).run((context, one) -> {}); }
|
||||
public void register(FeatureRegistry registry) { registry.registerObject(Two.class, ignored -> context -> null); }
|
||||
};
|
||||
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(conditioned)), Map.of(), Set.of(), Set.of());
|
||||
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(conditioned)), Map.of(), Set.of(), Set.of(), Set.of());
|
||||
assertEquals(List.of("test:core/one"), loaded.catalog().settings().stream().map(value -> value.id().toString()).toList());
|
||||
assertEquals(1, loaded.catalog().objects().size());
|
||||
assertEquals(1, loaded.placement().stages().size());
|
||||
assertTrue(loaded.chunk().stages().isEmpty());
|
||||
|
||||
var reserved = Map.of(FeatureAttachment.Target.BIOME, Set.of("shared"));
|
||||
assertThrows(IllegalArgumentException.class, () -> LoadedModules.load(List.of(DiscoveredModule.builtIn(conditioned)), reserved, Set.of(), Set.of()));
|
||||
assertThrows(IllegalArgumentException.class, () -> LoadedModules.load(List.of(DiscoveredModule.builtIn(conditioned)), reserved, Set.of(), Set.of(), Set.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -180,14 +182,72 @@ class ModuleRegistryTest {
|
||||
public void install(StageGraph graph) { graph.define(WORK).when(One.class).run((context, one) -> {}); }
|
||||
};
|
||||
|
||||
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(owner), DiscoveredModule.builtIn(user)), Map.of(), Set.of(), Set.of());
|
||||
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(owner), DiscoveredModule.builtIn(user)), Map.of(), Set.of(), Set.of(), Set.of());
|
||||
assertEquals(List.of("test:core/one"), loaded.catalog().settings().stream().map(value -> value.id().toString()).toList());
|
||||
|
||||
var unowned = assertThrows(IllegalArgumentException.class,
|
||||
() -> LoadedModules.load(List.of(DiscoveredModule.builtIn(user)), Map.of(), Set.of(), Set.of()));
|
||||
() -> LoadedModules.load(List.of(DiscoveredModule.builtIn(user)), Map.of(), Set.of(), Set.of(), Set.of()));
|
||||
assertTrue(unowned.getMessage().contains("which none of the modules naming it in when(...) owns"), unowned.getMessage());
|
||||
}
|
||||
|
||||
static class Castle {}
|
||||
static class Pool {}
|
||||
|
||||
@Test
|
||||
void resourceKindsHaveOwnedIdsAndOneIdTypeAndFolderEach() {
|
||||
var registry = new ModuleRegistry(Map.of(), Set.of("biomes"));
|
||||
var castles = ResourceKind.of("test:core/castle", "castles", Castle.class);
|
||||
registerAs(registry, "test:core", kinds -> {
|
||||
kinds.registerResourceKind(castles);
|
||||
var duplicate = assertThrows(IllegalArgumentException.class, () -> kinds.registerResourceKind(castles));
|
||||
assertEquals("Duplicate resource kind: test:core/castle of module test:core from host classpath", duplicate.getMessage());
|
||||
});
|
||||
|
||||
var foreign = assertThrows(IllegalArgumentException.class, () -> registerAs(registry, "test:other",
|
||||
kinds -> kinds.registerResourceKind(ResourceKind.of("test:core/pool", "pools", Pool.class))));
|
||||
assertEquals("Resource kind test:core/pool must be test:other or start with test:other/, the id of its registering"
|
||||
+ " module test:other from host classpath", foreign.getMessage());
|
||||
|
||||
var sameType = assertThrows(IllegalArgumentException.class, () -> registerAs(registry, "test:other",
|
||||
kinds -> kinds.registerResourceKind(ResourceKind.of("test:other/castle", "keeps", Castle.class))));
|
||||
assertEquals("resource kind test:other/castle of module test:other from host classpath reads into " + Castle.class.getName()
|
||||
+ ", which resource kind test:core/castle of module test:core from host classpath reads into already; resources"
|
||||
+ " are found by their type, so each kind needs a class of its own", sameType.getMessage());
|
||||
|
||||
var sameFolder = assertThrows(IllegalArgumentException.class, () -> registerAs(registry, "test:other",
|
||||
kinds -> kinds.registerResourceKind(ResourceKind.of("test:other/pool", "castles", Pool.class))));
|
||||
assertEquals("resource kind test:other/pool of module test:other from host classpath reads folder 'castles', which"
|
||||
+ " resource kind test:core/castle of module test:core from host classpath reads already; a folder holds one kind,"
|
||||
+ " so give one of them a folder of its own", sameFolder.getMessage());
|
||||
|
||||
var hostFolder = assertThrows(IllegalArgumentException.class, () -> registerAs(registry, "test:other",
|
||||
kinds -> kinds.registerResourceKind(ResourceKind.of("test:other/pool", "biomes", Pool.class))));
|
||||
assertEquals("Resource kind test:other/pool of module test:other from host classpath reads folder 'biomes', which the"
|
||||
+ " host reads itself; give the kind a folder of its own", hostFolder.getMessage());
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> registry.registerResourceKind(ResourceKind.of("test:core/pool", "pools", Pool.class)));
|
||||
assertEquals(List.of(castles), registry.freeze().resourceKinds().stream().map(ResourceKindRegistration::kind).toList());
|
||||
assertEquals("test:core", registry.freeze().resourceKinds().getFirst().module().info().id().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatorsAreCataloguedByModuleIdInTheOrderEachModuleRegisteredThem() {
|
||||
var registry = new ModuleRegistry();
|
||||
Validator first = pack -> {};
|
||||
Validator second = pack -> {};
|
||||
Validator other = pack -> {};
|
||||
registerAs(registry, "test:zeta", validators -> validators.registerValidator(other));
|
||||
registerAs(registry, "test:alpha", validators -> {
|
||||
validators.registerValidator(first);
|
||||
validators.registerValidator(second);
|
||||
});
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> registry.registerValidator(pack -> {}));
|
||||
assertEquals(List.of(first, second, other), registry.freeze().validators().stream().map(ValidatorRegistration::validator).toList());
|
||||
assertEquals("test:alpha", registry.freeze().validators().getFirst().module().info().id().toString());
|
||||
assertThrows(IllegalStateException.class, () -> registry.registerValidator(pack -> {}), "registration has ended");
|
||||
}
|
||||
|
||||
private static MantleCodec<String> codec(int version) {
|
||||
return new MantleCodec<>() {
|
||||
public int version() { return version; }
|
||||
|
||||
@@ -44,7 +44,7 @@ class PackModulesTest {
|
||||
private static LoadedModules loadWithBuiltIns(PackModules packModules) {
|
||||
var modules = new ArrayList<>(PlacementModules.builtIns());
|
||||
modules.addAll(packModules.modules());
|
||||
return LoadedModules.load(modules, Map.of(), Set.of(), Set.of());
|
||||
return LoadedModules.load(modules, Map.of(), Set.of(), Set.of(), Set.of());
|
||||
}
|
||||
|
||||
/** Versioned entries of a multi-release jar are read like any class, so they must be real class files. */
|
||||
@@ -343,7 +343,7 @@ class PackModulesTest {
|
||||
|
||||
try (var packModules = PackModules.load(pack)) {
|
||||
assertEquals(List.of(new MissingClasses(origin("user.jar"), List.of(OPTIONAL_CLASS))), packModules.missingClasses());
|
||||
assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of()));
|
||||
assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of(), Set.of()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -477,7 +477,7 @@ class PackModulesTest {
|
||||
.writeInto(pack, "beta.jar");
|
||||
|
||||
try (var packModules = PackModules.load(pack)) {
|
||||
var loaded = LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of());
|
||||
var loaded = LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of(), Set.of());
|
||||
assertTrue(loaded.placement().stages().stream().anyMatch(stage -> stage.stage().id().toString().equals("beta:core/size")));
|
||||
var beta = packModules.modules().getLast().module().getClass();
|
||||
assertEquals(0, beta.getDeclaredField("SHARED").getAnnotations().length,
|
||||
@@ -494,7 +494,7 @@ class PackModulesTest {
|
||||
try (var packModules = PackModules.load(pack, List.of(BUILT_IN_PACKAGE))) {
|
||||
var modules = new ArrayList<>(List.of(DiscoveredModule.builtIn(new CarveModule())));
|
||||
modules.addAll(packModules.modules());
|
||||
var order = LoadedModules.load(modules, Map.of(), Set.of(), Set.of()).placement().stages().stream()
|
||||
var order = LoadedModules.load(modules, Map.of(), Set.of(), Set.of(), Set.of()).placement().stages().stream()
|
||||
.map(stage -> stage.stage().id().toString()).toList();
|
||||
assertEquals(List.of("iris:carve/clear", "user:core/carved"), order);
|
||||
}
|
||||
@@ -572,7 +572,7 @@ class PackModulesTest {
|
||||
.writeInto(pack, "once.jar");
|
||||
|
||||
try (var packModules = PackModules.load(pack)) {
|
||||
var loaded = assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of()));
|
||||
var loaded = assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of(), Set.of()));
|
||||
assertEquals("once:core", packModules.modules().getFirst().info().id().toString());
|
||||
assertEquals(1, loaded.placement().stages().size());
|
||||
}
|
||||
@@ -591,7 +591,7 @@ class PackModulesTest {
|
||||
.writeInto(pack, "beta.jar");
|
||||
|
||||
try (var packModules = PackModules.load(pack)) {
|
||||
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of())),
|
||||
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of(), Set.of())),
|
||||
"Module beta:core from " + origin("beta.jar") + " failed to link or initialize a class while installing",
|
||||
"java.lang.NoSuchMethodError", "pack.lib.Lib.added()");
|
||||
}
|
||||
@@ -603,7 +603,7 @@ class PackModulesTest {
|
||||
module("pack.two.Duplicate", "dup:core", NO_MEMBERS, NO_STAGES).writeInto(pack, "two.jar");
|
||||
|
||||
try (var packModules = PackModules.load(pack)) {
|
||||
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of())),
|
||||
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of(), Set.of())),
|
||||
"Duplicate module dup:core from " + origin("two.jar") + "; already loaded from " + origin("one.jar"));
|
||||
}
|
||||
}
|
||||
@@ -625,7 +625,7 @@ class PackModulesTest {
|
||||
.writeInto(pack, "foreign.jar");
|
||||
|
||||
try (var packModules = PackModules.load(pack)) {
|
||||
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of())),
|
||||
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of(), Set.of())),
|
||||
"Stage mine:other/work must start with mine:core/, the id of its defining module mine:core from " + origin("foreign.jar"));
|
||||
}
|
||||
}
|
||||
@@ -640,8 +640,8 @@ class PackModulesTest {
|
||||
assertEquals("Module iris:stilt from " + origin("stilt.jar") + " uses reserved namespace 'iris', which only "
|
||||
+ "modules that ship with the host may use; give the module a namespace of its own, which its stages, "
|
||||
+ "settings ids and mantle keys then use too",
|
||||
failure(() -> LoadedModules.load(packModules.modules(), Map.of(), RESERVED_NAMESPACES, Set.of())));
|
||||
assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of()));
|
||||
failure(() -> LoadedModules.load(packModules.modules(), Map.of(), RESERVED_NAMESPACES, Set.of(), Set.of())));
|
||||
assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of(), Set.of()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -654,13 +654,13 @@ class PackModulesTest {
|
||||
try (var packModules = PackModules.load(pack, List.of(BUILT_IN_PACKAGE))) {
|
||||
var modules = new ArrayList<>(List.of(DiscoveredModule.builtIn(new CarveModule())));
|
||||
modules.addAll(packModules.modules());
|
||||
assertDoesNotThrow(() -> LoadedModules.load(modules, Map.of(), RESERVED_NAMESPACES, Set.of()));
|
||||
assertDoesNotThrow(() -> LoadedModules.load(modules, Map.of(), RESERVED_NAMESPACES, Set.of(), Set.of()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidReservedNamespaceFails() {
|
||||
assertEquals("Reserved namespace 'iris:core' is not a namespace; name one such as iris, without ':' or a path",
|
||||
failure(() -> LoadedModules.load(List.of(), Map.of(), Set.of("iris:core"), Set.of())));
|
||||
failure(() -> LoadedModules.load(List.of(), Map.of(), Set.of("iris:core"), Set.of(), Set.of())));
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import com.volmit.host.schema.FeatureRole;
|
||||
import com.volmit.host.schema.FeatureSchema;
|
||||
import com.volmit.host.schema.ObjectType;
|
||||
import com.volmit.host.schema.PackSchema;
|
||||
import com.volmit.host.schema.ResourceKindSchema;
|
||||
import com.volmit.host.schema.SchemaOption;
|
||||
import com.volmit.host.schema.TextFormat;
|
||||
import com.volmit.host.schema.ValueKind.BooleanKind;
|
||||
@@ -23,7 +24,10 @@ import com.volmit.host.schema.ValueKind.ListKind;
|
||||
import com.volmit.host.schema.ValueKind.MapKind;
|
||||
import com.volmit.host.schema.ValueKind.ObjectKind;
|
||||
import com.volmit.host.schema.ValueKind.ReferenceKind;
|
||||
import com.volmit.host.schema.ValueKind.ResourceReferenceKind;
|
||||
import com.volmit.host.schema.ValueKind.TextKind;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.resource.ResourceKind;
|
||||
import com.volmit.support.TestModuleJar;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
@@ -581,4 +585,133 @@ class PackSchemaTest {
|
||||
|
||||
assertEquals(List.of("zeta", "alpha", "omega", "beta"), settings.options().stream().map(SchemaOption::name).toList());
|
||||
}
|
||||
|
||||
private static final String CASTLE = "example:castle";
|
||||
private static final String CASTLE_MEMBERS = """
|
||||
public void register(com.volmit.FeatureRegistry registry) {
|
||||
registry.registerResourceKind(com.volmit.resource.ResourceKind.of("example:castle/keep", "keeps", Keep.class));
|
||||
registry.registerSettings(CastleSettings.class);
|
||||
}
|
||||
""";
|
||||
|
||||
private static TestModuleJar castle() {
|
||||
return module("pack.castle.CastleModule", CASTLE, CASTLE_MEMBERS, NO_STAGES)
|
||||
.source("pack.castle.Keep", """
|
||||
package pack.castle;
|
||||
|
||||
import com.volmit.config.*;
|
||||
|
||||
@Desc("A keep of a castle")
|
||||
public final class Keep {
|
||||
@Desc("Towers on the walls") @MinNumber(1)
|
||||
private int towers = 4;
|
||||
@ResourceRef(Keep.class)
|
||||
private String next = "";
|
||||
}
|
||||
""")
|
||||
.source("pack.castle.CastleSettings", """
|
||||
package pack.castle;
|
||||
|
||||
import com.volmit.FeatureAttachment.Target;
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.config.*;
|
||||
import java.util.List;
|
||||
|
||||
@Feature(id = "example:castle", name = "Castle", targets = Target.BIOME)
|
||||
public final class CastleSettings implements IrisFeature {
|
||||
@ResourceRef(Keep.class)
|
||||
private List<String> keeps = List.of("main");
|
||||
}
|
||||
""");
|
||||
}
|
||||
|
||||
private PackSchema castleSchema(ModuleHost host) {
|
||||
var pack = pack("castle-" + ++brokenPacks);
|
||||
castle().writeInto(pack, "castle.jar");
|
||||
return schemaOf(pack, host);
|
||||
}
|
||||
|
||||
@Test
|
||||
void describesResourceKindsLikeSettingsAndReferencesToThem() {
|
||||
var schema = castleSchema(NO_BUILT_INS);
|
||||
var keepKind = ResourceId.parse("example:castle/keep");
|
||||
|
||||
var kind = schema.resources().getFirst();
|
||||
assertEquals(new ResourceKindSchema(keepKind, kind.module(), "keeps", new ObjectKind("pack.castle.Keep")), kind);
|
||||
assertEquals(CASTLE, kind.module().id().toString());
|
||||
assertSame(kind, schema.resource(keepKind));
|
||||
var keep = schema.type("pack.castle.Keep");
|
||||
assertEquals("A keep of a castle", keep.description());
|
||||
assertEquals(new SchemaOption("towers", new IntegerKind(), "Towers on the walls", false, OptionalDouble.of(1),
|
||||
OptionalDouble.empty(), Optional.of(4L)), keep.options().getFirst());
|
||||
assertEquals(new ResourceReferenceKind(keepKind), keep.options().get(1).kind(), "a resource may name others of its kind");
|
||||
var keeps = schema.fields(Target.BIOME).getFirst().settings().options().getFirst();
|
||||
assertEquals(new ListKind(new ResourceReferenceKind(keepKind), false), keeps.kind());
|
||||
assertEquals(Optional.of(List.of("main")), keeps.defaultValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void builtInKindsMayReadIntoHostTypesWhichTheHostDescribesItself() {
|
||||
var builtIn = registering("iris:palettes", ResourceKind.of("iris:palettes/palette", "palettes", Palette.class));
|
||||
var schema = schemaOf(pack("palettes"), ModuleHost.of(List.of(builtIn)).describingHostTypesIn(HOST_TYPES));
|
||||
|
||||
assertEquals(new HostKind(Palette.class.getName()), schema.resources().getFirst().type());
|
||||
assertTrue(schema.types().isEmpty(), "the host describes its own types");
|
||||
}
|
||||
|
||||
@Test
|
||||
void kindFoldersBelongToOneKindAndNeverToTheHost() {
|
||||
var builtIn = registering("iris:keeps", ResourceKind.of("iris:keeps/keep", "keeps", Palette.class));
|
||||
var shared = assertThrows(IllegalArgumentException.class, () -> castleSchema(ModuleHost.of(List.of(builtIn))));
|
||||
assertEquals("resource kind example:castle/keep of module example:castle from modules/castle.jar reads folder 'keeps',"
|
||||
+ " which resource kind iris:keeps/keep of module iris:keeps from host classpath reads already; a folder holds"
|
||||
+ " one kind, so give one of them a folder of its own", shared.getMessage());
|
||||
|
||||
var reserved = assertThrows(IllegalArgumentException.class, () -> castleSchema(NO_BUILT_INS.reservingFolders("keeps")));
|
||||
assertEquals("Resource kind example:castle/keep of module example:castle from modules/castle.jar reads folder 'keeps',"
|
||||
+ " which the host reads itself; give the kind a folder of its own", reserved.getMessage());
|
||||
assertThrows(IllegalArgumentException.class, () -> NO_BUILT_INS.reservingFolders("not/a/folder"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void packKindsCannotReadIntoHostTypes() {
|
||||
var jar = module("pack.castle.CastleModule", CASTLE, """
|
||||
public void register(com.volmit.FeatureRegistry registry) {
|
||||
registry.registerResourceKind(com.volmit.resource.ResourceKind.of("example:castle/palette", "palettes",
|
||||
%s.class));
|
||||
}
|
||||
""".formatted(Palette.class.getName()), NO_STAGES).compileAgainstHostOf(Palette.class);
|
||||
var pack = pack("castle-host-type");
|
||||
jar.writeInto(pack, "castle.jar");
|
||||
var host = NO_BUILT_INS.exposing(HOST_TYPES).describingHostTypesIn(HOST_TYPES);
|
||||
|
||||
var failure = assertThrows(IllegalArgumentException.class, () -> LoadedPack.load(pack, host));
|
||||
assertEquals("Resource kind example:castle/palette of module example:castle from modules/castle.jar reads into "
|
||||
+ Palette.class.getName() + ", a class the host describes itself, which only modules that ship with the host"
|
||||
+ " may use; read the files into a class of the module", failure.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceReferencesNameARegisteredKindOnPlainText() {
|
||||
assertLoadFails("Field pack.flora.FloraSettings.keep of " + FLORA_SOURCE + " has @ResourceRef(java.lang.Integer), but no"
|
||||
+ " module of the pack registers a resource kind read into that class; name the class of a kind registered with"
|
||||
+ " FeatureRegistry.registerResourceKind", "@ResourceRef(Integer.class) private String keep;");
|
||||
assertLoadFails("Field pack.flora.FloraSettings.count of " + FLORA_SOURCE + " has @ResourceRef(java.lang.Integer) on int,"
|
||||
+ " but @ResourceRef fits only String and lists, sets and arrays of it", "@ResourceRef(Integer.class) private int count;");
|
||||
assertLoadFails("Field pack.flora.FloraSettings.keep of " + FLORA_SOURCE + " has @ResourceRef(java.lang.Integer) on"
|
||||
+ " ResourceId, but packs name a kind's resources by key without a namespace, such as castle/start; use String",
|
||||
"@ResourceRef(Integer.class) private ResourceId keep;");
|
||||
assertLoadFails("Field pack.flora.FloraSettings.keep of " + FLORA_SOURCE + " has both @Ref(BLOCK) and"
|
||||
+ " @ResourceRef(java.lang.Integer); an option names one kind of thing, so keep one",
|
||||
"@Ref(Reference.BLOCK) @ResourceRef(Integer.class) private String keep;");
|
||||
}
|
||||
|
||||
/** A built-in module {@code id} that registers {@code kind}. */
|
||||
private static com.volmit.IrisModule registering(String id, ResourceKind<?> kind) {
|
||||
return new com.volmit.IrisModule() {
|
||||
public com.volmit.ModuleInfo info() { return com.volmit.ModuleInfo.of(id, "Kinds", "test"); }
|
||||
public void install(com.volmit.stage.StageGraph graph) {}
|
||||
public void register(com.volmit.FeatureRegistry registry) { registry.registerResourceKind(kind); }
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -478,7 +478,7 @@ class StageGraphTest {
|
||||
stages.define(plain).run(context -> {});
|
||||
stages.define(paint).reach(2).run(context -> {});
|
||||
});
|
||||
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(withMantle(reach, depth, marks))), Map.of(), Set.of(), Set.of());
|
||||
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(withMantle(reach, depth, marks))), Map.of(), Set.of(), Set.of(), Set.of());
|
||||
|
||||
assertEquals(new StageReach(24, List.of(depth)), loaded.mantle().stages().getFirst().reach());
|
||||
assertEquals(StageReach.NONE, loaded.mantle().stages().get(2).reach(), "an undeclared reach is 0 with no layers");
|
||||
@@ -496,7 +496,7 @@ class StageGraphTest {
|
||||
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(module("test:reach", stages -> {
|
||||
stages.define(carve).reach(settings -> settings.declared(Toggle.class).isEmpty() ? 4 : 40).run(context -> {});
|
||||
stages.define(mark).reach(2).run(context -> {});
|
||||
}))), Map.of(), Set.of(), Set.of());
|
||||
}))), Map.of(), Set.of(), Set.of(), Set.of());
|
||||
var computed = loaded.mantle().stages().getFirst();
|
||||
var toggled = new DeclaredSettings(new Entry<>(FeatureAttachment.Target.DIMENSION, ResourceId.parse("pack:dimension/main"), new Toggle()));
|
||||
|
||||
@@ -512,7 +512,7 @@ class StageGraphTest {
|
||||
@Test
|
||||
void onlyMantleStagesWorkOutTheirReachAndNeverANegativeOne() {
|
||||
var negative = LoadedModules.load(List.of(DiscoveredModule.builtIn(module("test:reach", stages ->
|
||||
stages.define(Stage.mantle("test:reach/carve")).reach(settings -> -1).run(context -> {})))), Map.of(), Set.of(), Set.of());
|
||||
stages.define(Stage.mantle("test:reach/carve")).reach(settings -> -1).run(context -> {})))), Map.of(), Set.of(), Set.of(), Set.of());
|
||||
var failure = assertThrows(IllegalStateException.class, () -> negative.mantle().stages().getFirst().reachBlocks(DeclaredSettings.NONE));
|
||||
assertEquals("Stage test:reach/carve of module test:reach worked out a negative reach of -1 blocks for this pack", failure.getMessage());
|
||||
|
||||
@@ -567,13 +567,13 @@ class StageGraphTest {
|
||||
module("test:reach", stages -> stages.define(carve).writesMantle(depth, depth).run(context -> {}))));
|
||||
|
||||
var unregistered = assertThrows(IllegalArgumentException.class, () -> LoadedModules.load(List.of(DiscoveredModule.builtIn(
|
||||
module("test:reach", stages -> stages.define(carve).writesMantle(depth).run(context -> {})))), Map.of(), Set.of(), Set.of()));
|
||||
module("test:reach", stages -> stages.define(carve).writesMantle(depth).run(context -> {})))), Map.of(), Set.of(), Set.of(), Set.of()));
|
||||
assertEquals("Stage test:reach/carve of module test:reach writes mantle layer test:reach/depth, which no module registers;"
|
||||
+ " its owner registers it with FeatureRegistry.registerMantle", unregistered.getMessage());
|
||||
|
||||
var asText = new MantleKey<>("test:reach/depth", String.class);
|
||||
var mismatch = assertThrows(IllegalArgumentException.class, () -> LoadedModules.load(List.of(DiscoveredModule.builtIn(withMantle(
|
||||
module("test:reach", stages -> stages.define(carve).writesMantle(asText).run(context -> {})), depth))), Map.of(), Set.of(), Set.of()));
|
||||
module("test:reach", stages -> stages.define(carve).writesMantle(asText).run(context -> {})), depth))), Map.of(), Set.of(), Set.of(), Set.of()));
|
||||
assertEquals("Stage test:reach/carve of module test:reach writes mantle layer test:reach/depth as java.lang.String,"
|
||||
+ " but it is registered as java.lang.Integer", mismatch.getMessage());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user