mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
Settings features declare a short JSON key and may declare a root option
@Feature(key) is required: a plain field name, unique per target across all modules and the host's own JSON fields (ModuleHost.reservingFields, back for keys). A clash refuses the pack load naming the key, the target and both claimants. @Feature(root) declares that the JSON under the key is one option's value, so a settings class has exactly one shape; FeatureSchema carries it.
This commit is contained in:
1 parent
1707bb95f6
commit
b82c292dcb
30 files changed
+536
-119
No files matched your search
@@ -1,13 +1,28 @@
|
||||
package com.volmit;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/** Where settings appear in pack JSON: the targets. The JSON key is always the feature id. */
|
||||
/** Where settings appear in pack JSON: the targets. The JSON key is declared by {@link com.volmit.config.Feature#key}. */
|
||||
public record FeatureAttachment(Set<Target> targets) {
|
||||
public enum Target { DIMENSION, REGION, BIOME, OBJECT_PLACEMENT }
|
||||
|
||||
private static final char NAMESPACE_SEPARATOR = ':';
|
||||
|
||||
public FeatureAttachment {
|
||||
targets = Set.copyOf(targets);
|
||||
if (targets.isEmpty()) throw new IllegalArgumentException("At least one target is required");
|
||||
}
|
||||
|
||||
/** Checks a JSON field name that is not a feature id: a settings key or a field the host itself uses.
|
||||
* It is not blank and has no ':', which every feature id has.
|
||||
*/
|
||||
public static String requireFieldName(String name) {
|
||||
Objects.requireNonNull(name, "name");
|
||||
if (name.isBlank()) throw new IllegalArgumentException("Field must not be blank");
|
||||
if (name.indexOf(NAMESPACE_SEPARATOR) >= 0) {
|
||||
throw new IllegalArgumentException("Field " + name + " contains ':', which only feature ids may contain");
|
||||
}
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,25 @@ package com.volmit.config;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/** Identity and JSON attachment for an IrisFeature configuration class. Packs write the settings under
|
||||
* their feature id, such as {@code "example:moss": {...}}, at each of the targets.
|
||||
/** Identity and JSON attachment for an IrisFeature configuration class. The {@code id}, such as
|
||||
* {@code "example:moss"}, is the feature's identity in the stage graph, logs and lookups by class. Packs write
|
||||
* the settings under the short {@code key}, such as {@code "moss": {...}}, at each of the targets.
|
||||
*
|
||||
* <p>The key is a plain JSON field name: not blank, no ':'. It is unique per target across all loaded modules
|
||||
* and the JSON fields the host itself uses at that target, so it must not be a field of a biome, region or
|
||||
* dimension; a clash refuses the pack load, naming both claimants.
|
||||
*
|
||||
* <p>The JSON under the key is an object of the class's options, unless {@code root} names an option. Then the
|
||||
* key holds exactly that option's value, such as a list, and an object is not accepted: with
|
||||
* {@code @Feature(id = "iris:deposit", key = "deposits", root = "generators", ...)} a pack writes
|
||||
* {@code "deposits": [...]}. The class has no other option then, since nothing could set it.
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface Feature {
|
||||
String id();
|
||||
String name();
|
||||
String key();
|
||||
String root() default "";
|
||||
com.volmit.FeatureAttachment.Target[] targets();
|
||||
}
|
||||
@@ -6,13 +6,22 @@ import com.volmit.config.Feature;
|
||||
import com.volmit.config.OptionsMetadata;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/** Validated settings metadata. It describes configuration, not executable ordering. */
|
||||
/** Validated settings metadata. It describes configuration, not executable ordering.
|
||||
*
|
||||
* @param key the JSON key packs write the settings under at each target; see {@link Feature#key}
|
||||
* @param root the option whose value the key holds, when the JSON is one option's value instead of an object
|
||||
* of options; see {@link Feature#root}
|
||||
*/
|
||||
public record FeatureDescriptor<F extends IrisFeature>(
|
||||
ResourceId id,
|
||||
String name,
|
||||
String key,
|
||||
Optional<String> root,
|
||||
Class<F> type,
|
||||
FeatureAttachment attachment,
|
||||
OptionsMetadata options) {
|
||||
@@ -22,9 +31,12 @@ public record FeatureDescriptor<F extends IrisFeature>(
|
||||
if (name.isBlank()) {
|
||||
throw new IllegalArgumentException("Blank feature name");
|
||||
}
|
||||
requireKey(id, key);
|
||||
Objects.requireNonNull(root, "root");
|
||||
Objects.requireNonNull(type, "type");
|
||||
Objects.requireNonNull(attachment, "attachment");
|
||||
Objects.requireNonNull(options, "options");
|
||||
root.ifPresent(option -> requireOnlyOption(type, option, options));
|
||||
}
|
||||
|
||||
/** Reads the @Feature declaration of a concrete settings class. */
|
||||
@@ -40,14 +52,29 @@ public record FeatureDescriptor<F extends IrisFeature>(
|
||||
return new FeatureDescriptor<>(
|
||||
ResourceId.parse(annotation.id()),
|
||||
annotation.name(),
|
||||
annotation.key(),
|
||||
annotation.root().isEmpty() ? Optional.empty() : Optional.of(annotation.root()),
|
||||
type,
|
||||
new FeatureAttachment(Set.of(annotation.targets())),
|
||||
OptionsMetadata.inspect(type));
|
||||
}
|
||||
|
||||
/** The JSON key packs write these settings under: the feature id. */
|
||||
public String key() {
|
||||
return id.toString();
|
||||
private static void requireKey(ResourceId id, String key) {
|
||||
try {
|
||||
FeatureAttachment.requireFieldName(key);
|
||||
} catch (IllegalArgumentException invalid) {
|
||||
throw new IllegalArgumentException("Invalid key of feature " + id + ": " + invalid.getMessage()
|
||||
+ "; a key is a plain JSON field name such as \"deposits\"", invalid);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireOnlyOption(Class<?> type, String root, OptionsMetadata options) {
|
||||
var names = options.options().stream().map(OptionsMetadata.Option::name).toList();
|
||||
if (names.equals(List.of(root))) {
|
||||
return;
|
||||
}
|
||||
throw new IllegalArgumentException("Feature root '" + root + "' of " + type.getName() + " must name its only option,"
|
||||
+ " since the JSON under its key is that option's value; its options are " + names);
|
||||
}
|
||||
|
||||
private static void requireConcreteFeature(Class<?> type) {
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.FeatureAttachment;
|
||||
import com.volmit.host.schema.PackSchema;
|
||||
import com.volmit.stage.Pipeline;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -23,6 +25,7 @@ public record LoadedModules(
|
||||
|
||||
/** Pass the built-in modules together with {@link PackModules#modules()} of the pack.
|
||||
*
|
||||
* @param reservedHostFields JSON fields the host itself uses, per attachment target, which no settings key may take
|
||||
* @param hostTypePackages packages whose classes the host describes itself, each with its
|
||||
* subpackages; only built-in modules' settings may use them (see {@link ModuleHost#describingHostTypesIn})
|
||||
* @param reservedFolders pack folders the host reads itself, which no resource kind may read (see
|
||||
@@ -30,9 +33,10 @@ public record LoadedModules(
|
||||
*/
|
||||
public static LoadedModules load(
|
||||
List<DiscoveredModule> modules,
|
||||
Map<FeatureAttachment.Target, Set<String>> reservedHostFields,
|
||||
Collection<String> hostTypePackages,
|
||||
Collection<String> reservedFolders) {
|
||||
var registry = new ModuleRegistry(Set.copyOf(reservedFolders));
|
||||
var registry = new ModuleRegistry(reservedHostFields, Set.copyOf(reservedFolders));
|
||||
var stages = new StageGraphBuilder();
|
||||
for (var discovered : modules) {
|
||||
installModule(discovered, stages, registry);
|
||||
|
||||
@@ -37,7 +37,7 @@ public final class LoadedPack implements AutoCloseable {
|
||||
var packModules = PackModules.load(packRoot, host.exposedHostPackages(), host.jarCopyRoot(), host.logger());
|
||||
try {
|
||||
var loaded = LoadedModules.load(host.withPackModules(packModules.modules()),
|
||||
host.hostTypePackages(), host.reservedFolders());
|
||||
host.reservedHostFields(), host.hostTypePackages(), host.reservedFolders());
|
||||
return new LoadedPack(packModules, loaded);
|
||||
} catch (RuntimeException | Error failure) {
|
||||
closeAfterFailure(packModules, failure);
|
||||
|
||||
@@ -1,34 +1,40 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.FeatureAttachment;
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.resource.ResourceKind;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/** The host's side of every pack load, the same for all packs: its built-in modules, the host
|
||||
* packages pack modules may use besides the Iris API, the packages of the classes the host describes itself
|
||||
* and the pack folders it reads itself (which no resource kind may read). Build it once and pass it to every
|
||||
* packages pack modules may use besides the Iris API, the JSON fields the host itself uses (which no
|
||||
* settings key may take), the packages of the classes the host describes itself and the pack folders it
|
||||
* reads itself (which no resource kind may read). Build it once and pass it to every
|
||||
* {@link LoadedPack#load(java.nio.file.Path, ModuleHost)}.
|
||||
*
|
||||
* <pre>{@code
|
||||
* var host = ModuleHost.of(builtInModules)
|
||||
* .exposing("art.arcane.iris.engine.modules")
|
||||
* .reservingFields(reservedHostFields)
|
||||
* .reservingFolders("biomes", "regions");
|
||||
* }</pre>
|
||||
*
|
||||
* Every method adds to what earlier calls configured, so a host can build the configuration in steps.
|
||||
* Built-in modules' {@code info()} is read here, once. Exposed package names, host type packages
|
||||
* and reserved folders are checked here, so a mistake fails when the host starts rather than at the
|
||||
* Built-in modules' {@code info()} is read here, once. Exposed package names, host type packages,
|
||||
* reserved folders and reserved field names are checked here, so a mistake fails when the host starts rather than at the
|
||||
* first pack load.
|
||||
*/
|
||||
public record ModuleHost(
|
||||
List<DiscoveredModule> builtIns,
|
||||
Set<String> exposedHostPackages,
|
||||
Map<FeatureAttachment.Target, Set<String>> reservedHostFields,
|
||||
Set<String> hostTypePackages,
|
||||
Set<String> reservedFolders,
|
||||
Path jarCopyRoot,
|
||||
@@ -38,12 +44,14 @@ public record ModuleHost(
|
||||
public ModuleHost {
|
||||
builtIns = List.copyOf(builtIns);
|
||||
exposedHostPackages = Set.copyOf(exposedHostPackages);
|
||||
reservedHostFields = copyOf(reservedHostFields);
|
||||
hostTypePackages = Set.copyOf(hostTypePackages);
|
||||
reservedFolders = Set.copyOf(reservedFolders);
|
||||
Objects.requireNonNull(jarCopyRoot, "jarCopyRoot");
|
||||
Objects.requireNonNull(logger, "logger");
|
||||
ExposedPackages.apiAnd(exposedHostPackages);
|
||||
HostTypes.of(hostTypePackages);
|
||||
requireFieldNames(reservedHostFields);
|
||||
requireFolderNames(reservedFolders);
|
||||
}
|
||||
|
||||
@@ -52,7 +60,7 @@ public record ModuleHost(
|
||||
*/
|
||||
public static ModuleHost of(List<? extends IrisModule> builtInModules) {
|
||||
var builtIns = builtInModules.stream().map(DiscoveredModule::builtIn).toList();
|
||||
return new ModuleHost(builtIns, Set.of(), Set.of(), Set.of(), systemTemporaryDirectory(),
|
||||
return new ModuleHost(builtIns, Set.of(), Map.of(), Set.of(), Set.of(), systemTemporaryDirectory(),
|
||||
System.getLogger(PackModules.class.getName()));
|
||||
}
|
||||
|
||||
@@ -60,7 +68,16 @@ public record ModuleHost(
|
||||
* modules, such as the packages that hold built-in modules' stage constants.
|
||||
*/
|
||||
public ModuleHost exposing(String... packages) {
|
||||
return new ModuleHost(builtIns, union(exposedHostPackages, packages), hostTypePackages, reservedFolders, jarCopyRoot, logger);
|
||||
return new ModuleHost(builtIns, union(exposedHostPackages, packages), reservedHostFields,
|
||||
hostTypePackages, reservedFolders, jarCopyRoot, logger);
|
||||
}
|
||||
|
||||
/** This configuration with {@code fields} also reserved as JSON fields the host itself uses,
|
||||
* merged per attachment target with the fields reserved before.
|
||||
*/
|
||||
public ModuleHost reservingFields(Map<FeatureAttachment.Target, Set<String>> fields) {
|
||||
return new ModuleHost(builtIns, exposedHostPackages, merge(reservedHostFields, fields),
|
||||
hostTypePackages, reservedFolders, jarCopyRoot, logger);
|
||||
}
|
||||
|
||||
/** This configuration with the classes in {@code packages}, each with its subpackages, described by
|
||||
@@ -69,7 +86,7 @@ public record ModuleHost(
|
||||
* instead of describing their fields. Pack modules' settings cannot use them.
|
||||
*/
|
||||
public ModuleHost describingHostTypesIn(String... packages) {
|
||||
return new ModuleHost(builtIns, exposedHostPackages,
|
||||
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields,
|
||||
union(hostTypePackages, packages), reservedFolders, jarCopyRoot, logger);
|
||||
}
|
||||
|
||||
@@ -77,7 +94,7 @@ public record ModuleHost(
|
||||
* itself, such as its biome and object folders. A module resource kind that reads one fails the pack load.
|
||||
*/
|
||||
public ModuleHost reservingFolders(String... folders) {
|
||||
return new ModuleHost(builtIns, exposedHostPackages, hostTypePackages,
|
||||
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, hostTypePackages,
|
||||
union(reservedFolders, folders), jarCopyRoot, logger);
|
||||
}
|
||||
|
||||
@@ -85,7 +102,7 @@ public record ModuleHost(
|
||||
* cache directory of the host, which is created when missing. The last call wins.
|
||||
*/
|
||||
public ModuleHost copyingJarsTo(Path directory) {
|
||||
return new ModuleHost(builtIns, exposedHostPackages, hostTypePackages,
|
||||
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, hostTypePackages,
|
||||
reservedFolders, directory, logger);
|
||||
}
|
||||
|
||||
@@ -93,7 +110,7 @@ public record ModuleHost(
|
||||
* {@code logger}, such as an adapter to the host's own log. The last call wins.
|
||||
*/
|
||||
public ModuleHost loggingTo(System.Logger logger) {
|
||||
return new ModuleHost(builtIns, exposedHostPackages, hostTypePackages,
|
||||
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, hostTypePackages,
|
||||
reservedFolders, jarCopyRoot, logger);
|
||||
}
|
||||
|
||||
@@ -108,6 +125,17 @@ public record ModuleHost(
|
||||
return Path.of(System.getProperty(TEMPORARY_DIRECTORY_PROPERTY));
|
||||
}
|
||||
|
||||
private static Map<FeatureAttachment.Target, Set<String>> copyOf(Map<FeatureAttachment.Target, Set<String>> fields) {
|
||||
var copy = new EnumMap<FeatureAttachment.Target, Set<String>>(FeatureAttachment.Target.class);
|
||||
fields.forEach((target, names) -> copy.put(target, Set.copyOf(names)));
|
||||
return Map.copyOf(copy);
|
||||
}
|
||||
|
||||
/** A blank field name, or one with ':' like a feature id, fails here, as it would as a settings key. */
|
||||
private static void requireFieldNames(Map<FeatureAttachment.Target, Set<String>> fields) {
|
||||
fields.values().forEach(names -> names.forEach(FeatureAttachment::requireFieldName));
|
||||
}
|
||||
|
||||
private static void requireFolderNames(Set<String> folders) {
|
||||
for (var folder : folders) {
|
||||
if (!ResourceKind.isFolderName(folder)) {
|
||||
@@ -117,6 +145,15 @@ public record ModuleHost(
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<FeatureAttachment.Target, Set<String>> merge(
|
||||
Map<FeatureAttachment.Target, Set<String>> existing,
|
||||
Map<FeatureAttachment.Target, Set<String>> added) {
|
||||
var merged = new EnumMap<FeatureAttachment.Target, Set<String>>(FeatureAttachment.Target.class);
|
||||
existing.forEach((target, names) -> merged.put(target, new TreeSet<>(names)));
|
||||
added.forEach((target, names) -> merged.computeIfAbsent(target, ignored -> new TreeSet<>()).addAll(names));
|
||||
return merged;
|
||||
}
|
||||
|
||||
private static Set<String> union(Set<String> existing, String... added) {
|
||||
var names = new TreeSet<>(existing);
|
||||
names.addAll(Arrays.asList(added));
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.FeatureAttachment;
|
||||
import com.volmit.FeatureRegistry;
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.IrisModule;
|
||||
@@ -17,12 +18,15 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/** Reference host registry. Single-threaded registration ends with freeze(); the catalog is
|
||||
* immutable. IDs are unique across feature kinds, and every feature id is its JSON key. Modules register through
|
||||
* immutable. IDs are unique across feature kinds. A feature's JSON key (see {@link com.volmit.config.Feature#key})
|
||||
* is unique per attachment target, and may not equal a host JSON field passed in to reserve it; a clash fails the
|
||||
* registration naming the key, the target and both claimants. Modules register through
|
||||
* {@link #register(DiscoveredModule)}; object feature ids, mantle keys, resource kind ids and explicitly registered
|
||||
* 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
|
||||
@@ -38,6 +42,7 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
private final Set<ResourceId> featureIds = new HashSet<>();
|
||||
private final Set<Class<?>> featureTypes = new HashSet<>();
|
||||
private final Set<Class<?>> registeredByOwner = new HashSet<>();
|
||||
private final Map<KeyAtTarget, KeyClaimant> keyClaimants = new HashMap<>();
|
||||
private final Map<ResourceId, ResourceKindRegistration> resourceKinds = new HashMap<>();
|
||||
private final Map<Class<?>, HeldTypeRegistration> heldTypes = new HashMap<>();
|
||||
private final List<ValidatorRegistration> validators = new ArrayList<>();
|
||||
@@ -45,15 +50,20 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
private final List<ServiceRegistration> services = new ArrayList<>();
|
||||
private final List<VanillaStructureHookRegistration> vanillaStructureHooks = new ArrayList<>();
|
||||
private final Set<String> reservedFolders;
|
||||
private final List<DiscoveredModule> registered = new ArrayList<>();
|
||||
private FeatureCatalog catalog;
|
||||
private DiscoveredModule registering;
|
||||
|
||||
public ModuleRegistry() {
|
||||
this(Set.of());
|
||||
this(Map.of(), Set.of());
|
||||
}
|
||||
|
||||
/** @param reservedFolders folders below the pack root that the host reads itself, which no resource kind may read */
|
||||
public ModuleRegistry(Set<String> reservedFolders) {
|
||||
/**
|
||||
* @param reservedHostFields JSON fields the host itself uses, per attachment target, which no settings key may take
|
||||
* @param reservedFolders folders below the pack root that the host reads itself, which no resource kind may read
|
||||
*/
|
||||
public ModuleRegistry(Map<FeatureAttachment.Target, Set<String>> reservedHostFields, Set<String> reservedFolders) {
|
||||
reservedHostFields.forEach((target, names) -> names.forEach(name -> reserveHostField(target, name)));
|
||||
this.reservedFolders = Set.copyOf(reservedFolders);
|
||||
}
|
||||
|
||||
@@ -65,6 +75,7 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
throw new IllegalStateException("Module " + discovered.info().id() + " registered during " + registering.info().id());
|
||||
}
|
||||
registering = discovered;
|
||||
registered.add(discovered);
|
||||
try {
|
||||
discovered.module().register(this);
|
||||
} finally {
|
||||
@@ -80,7 +91,7 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
var descriptor = FeatureDescriptor.of(featureType);
|
||||
requireOwnedId("Object generator settings " + featureType.getName() + " with feature id", descriptor.id());
|
||||
var registration = new ObjectRegistration<>(descriptor, factory);
|
||||
claim(descriptor);
|
||||
claim(descriptor, registering);
|
||||
objects.put(featureType, registration);
|
||||
}
|
||||
|
||||
@@ -102,7 +113,7 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
checkOpen();
|
||||
var descriptor = FeatureDescriptor.of(settingsType);
|
||||
requireOwnedId("Settings " + settingsType.getName() + " with feature id", descriptor.id());
|
||||
catalogSettings(descriptor);
|
||||
catalogSettings(descriptor, registering);
|
||||
registeredByOwner.add(settingsType);
|
||||
}
|
||||
|
||||
@@ -182,7 +193,7 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
return;
|
||||
}
|
||||
var descriptor = FeatureDescriptor.of(settingsType);
|
||||
catalogSettings(descriptor);
|
||||
catalogSettings(descriptor, ownerOf(descriptor));
|
||||
}
|
||||
|
||||
/** Whether the module owning {@code settingsType} registered it through {@link #registerSettings}. */
|
||||
@@ -198,14 +209,21 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
return catalog;
|
||||
}
|
||||
|
||||
private void catalogSettings(FeatureDescriptor<?> descriptor) {
|
||||
private void catalogSettings(FeatureDescriptor<?> descriptor, DiscoveredModule owner) {
|
||||
if (settings.containsKey(descriptor.type())) {
|
||||
return;
|
||||
}
|
||||
claim(descriptor);
|
||||
claim(descriptor, owner);
|
||||
settings.put(descriptor.type(), descriptor);
|
||||
}
|
||||
|
||||
private DiscoveredModule ownerOf(FeatureDescriptor<?> descriptor) {
|
||||
return registered.stream()
|
||||
.filter(module -> module.info().ownsId(descriptor.id()))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new IllegalStateException("No registered module owns feature id " + descriptor.id()));
|
||||
}
|
||||
|
||||
private void requireOwnedId(String registration, ResourceId id) {
|
||||
if (registering == null) {
|
||||
throw new IllegalStateException(registration + " " + id + " registered outside IrisModule.register");
|
||||
@@ -255,12 +273,42 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
private void claim(FeatureDescriptor<?> descriptor) {
|
||||
private void reserveHostField(FeatureAttachment.Target target, String name) {
|
||||
keyClaimants.put(new KeyAtTarget(target, FeatureAttachment.requireFieldName(name)), KeyClaimant.hostField());
|
||||
}
|
||||
|
||||
private void claim(FeatureDescriptor<?> descriptor, DiscoveredModule owner) {
|
||||
requireNewFeature(descriptor);
|
||||
requireUnclaimedKeys(descriptor, owner);
|
||||
featureTypes.add(descriptor.type());
|
||||
featureIds.add(descriptor.id());
|
||||
descriptor.attachment().targets().forEach(target ->
|
||||
keyClaimants.put(new KeyAtTarget(target, descriptor.key()), KeyClaimant.of(descriptor, owner)));
|
||||
}
|
||||
|
||||
private void requireNewFeature(FeatureDescriptor<?> descriptor) {
|
||||
if (featureTypes.contains(descriptor.type()) || featureIds.contains(descriptor.id())) {
|
||||
throw new IllegalArgumentException("Duplicate feature: " + descriptor.id());
|
||||
}
|
||||
featureTypes.add(descriptor.type());
|
||||
featureIds.add(descriptor.id());
|
||||
}
|
||||
|
||||
/** One JSON key means one setting at a target: the pack's JSON could not say which one it meant. The message
|
||||
* names the claimants in a fixed order, so it does not depend on which module registered first.
|
||||
*/
|
||||
private void requireUnclaimedKeys(FeatureDescriptor<?> descriptor, DiscoveredModule owner) {
|
||||
for (var target : descriptor.attachment().targets()) {
|
||||
var earlier = keyClaimants.get(new KeyAtTarget(target, descriptor.key()));
|
||||
if (earlier == null) continue;
|
||||
var later = KeyClaimant.of(descriptor, owner);
|
||||
var first = earlier.compareTo(later) <= 0 ? earlier : later;
|
||||
var second = first == earlier ? later : earlier;
|
||||
throw new IllegalArgumentException(targetName(target) + " key \"" + descriptor.key() + "\" is claimed by " + first
|
||||
+ " and " + second + "; give one of the settings another key in its @Feature");
|
||||
}
|
||||
}
|
||||
|
||||
private static String targetName(FeatureAttachment.Target target) {
|
||||
return target.name().toLowerCase(Locale.ROOT).replace('_', ' ');
|
||||
}
|
||||
|
||||
private void checkOpen() {
|
||||
@@ -268,4 +316,31 @@ public final class ModuleRegistry implements FeatureRegistry {
|
||||
throw new IllegalStateException("Registration has ended");
|
||||
}
|
||||
}
|
||||
|
||||
private record KeyAtTarget(FeatureAttachment.Target target, String key) {}
|
||||
|
||||
/** Who holds a key: a field of the host's own JSON, a built-in module's feature or a pack module's, in that order. */
|
||||
private record KeyClaimant(Source source, String feature, String text) implements Comparable<KeyClaimant> {
|
||||
private enum Source { HOST_FIELD, BUILT_IN, PACK }
|
||||
|
||||
static KeyClaimant hostField() {
|
||||
return new KeyClaimant(Source.HOST_FIELD, "", "Iris");
|
||||
}
|
||||
|
||||
static KeyClaimant of(FeatureDescriptor<?> descriptor, DiscoveredModule owner) {
|
||||
var source = owner.source() == DiscoveredModule.Source.HOST ? Source.BUILT_IN : Source.PACK;
|
||||
return new KeyClaimant(source, descriptor.id().toString(), descriptor.id() + " (" + owner.origin() + ")");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(KeyClaimant other) {
|
||||
var bySource = source.compareTo(other.source);
|
||||
return bySource != 0 ? bySource : feature.compareTo(other.feature);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,7 +85,7 @@ final class PackSchemaBuilder {
|
||||
private record FeatureDraft(Catalogued catalogued, DiscoveredModule owner) {
|
||||
FeatureSchema complete(Map<String, ObjectType> types) {
|
||||
var descriptor = catalogued.descriptor();
|
||||
return new FeatureSchema(descriptor.id(), descriptor.name(), catalogued.role(), owner.info(), descriptor.key(),
|
||||
return new FeatureSchema(descriptor.id(), descriptor.name(), catalogued.role(), owner.info(), descriptor.key(), descriptor.root(),
|
||||
descriptor.attachment().targets(), types.get(descriptor.type().getName()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,14 +6,16 @@ import com.volmit.resource.ResourceId;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/** A module's settings type and the JSON keys it is written under at each of its targets.
|
||||
/** A module's settings type and the JSON key it is written under at each of its targets.
|
||||
*
|
||||
* @param id the feature id from {@code @Feature}
|
||||
* @param name the feature's display name from {@code @Feature}
|
||||
* @param module the module that owns the feature id
|
||||
* @param key the JSON key the settings are written under, the feature id, the same at every target
|
||||
* @param key the JSON key the settings are written under, declared by the feature, the same at every target
|
||||
* @param root the option whose value the key holds, when the JSON is that value and not an object of options
|
||||
* @param targets where the keys appear, in {@link FeatureAttachment.Target} order
|
||||
* @param settings the settings class; its description describes the value
|
||||
*/
|
||||
@@ -23,6 +25,7 @@ public record FeatureSchema(
|
||||
FeatureRole role,
|
||||
ModuleInfo module,
|
||||
String key,
|
||||
Optional<String> root,
|
||||
Set<FeatureAttachment.Target> targets,
|
||||
ObjectType settings) {
|
||||
public FeatureSchema {
|
||||
@@ -31,7 +34,23 @@ public record FeatureSchema(
|
||||
Objects.requireNonNull(role, "role");
|
||||
Objects.requireNonNull(module, "module");
|
||||
Objects.requireNonNull(key, "key");
|
||||
Objects.requireNonNull(root, "root");
|
||||
Objects.requireNonNull(settings, "settings");
|
||||
targets = Collections.unmodifiableSet(EnumSet.copyOf(targets));
|
||||
root.ifPresent(option -> requireOption(settings, option));
|
||||
}
|
||||
|
||||
/** The option the key holds the value of, when the JSON under the key is one option's value. Then the
|
||||
* key takes exactly that value, such as a list, and not an object of options.
|
||||
*/
|
||||
public Optional<SchemaOption> rootOption() {
|
||||
return root.map(option -> requireOption(settings, option));
|
||||
}
|
||||
|
||||
private static SchemaOption requireOption(ObjectType settings, String option) {
|
||||
return settings.options().stream()
|
||||
.filter(candidate -> candidate.name().equals(option))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new IllegalArgumentException("Root " + option + " is no option of " + settings.name()));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user