mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Key settings by feature id, order options as declared and declare stage reach
- Packs write settings under the feature id. @Feature(field) becomes an optional alias that only built-in modules may set, so existing pack JSON keeps loading; a pack module setting one fails the load. Reserved host fields now only constrain aliases. FeatureSchema exposes key and alias. - Pack snippet names live under their module's folder (snippet/<namespace>/<path>/...); snippet names are folder paths. - OptionsMetadata lists options in declaration order, read from the class file, a superclass's first. - @Ref(PACK_BIOME|PACK_REGION|PACK_OBJECT) fits String only. - Stage definitions declare reach(blocks) and writesMantle(layers); ResolvedGraph.reach() collects the pipeline and per-layer maximum, and the load fails on an unregistered or mistyped layer.
This commit is contained in:
1 parent
b12e7a8c49
commit
a325640d5d
38 files changed
+761
-138
No files matched your search
@@ -0,0 +1,55 @@
|
||||
package com.volmit.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.lang.classfile.ClassFile;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/** Orders a class's own fields as its source declares them. {@link Class#getDeclaredFields()} promises
|
||||
* no order, but the class file lists fields in declaration order, so this reads it.
|
||||
*/
|
||||
final class DeclaredFieldOrder {
|
||||
private static final String CLASS_FILE_SUFFIX = ".class";
|
||||
|
||||
private DeclaredFieldOrder() {}
|
||||
|
||||
static List<Field> of(Class<?> type) {
|
||||
var names = fieldNames(type);
|
||||
return Arrays.stream(type.getDeclaredFields())
|
||||
.sorted(Comparator.comparingInt((Field field) -> position(names, field, type)))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private static int position(List<String> names, Field field, Class<?> type) {
|
||||
int position = names.indexOf(field.getName());
|
||||
if (position < 0) {
|
||||
throw new IllegalArgumentException("Field " + field.getName() + " of " + type.getName()
|
||||
+ " is not in its class file, so its declaration order is unknown");
|
||||
}
|
||||
return position;
|
||||
}
|
||||
|
||||
private static List<String> fieldNames(Class<?> type) {
|
||||
try (var classFile = openClassFile(type)) {
|
||||
return ClassFile.of().parse(classFile.readAllBytes()).fields().stream()
|
||||
.map(field -> field.fieldName().stringValue())
|
||||
.toList();
|
||||
} catch (IOException failure) {
|
||||
throw new UncheckedIOException("Cannot read the class file of " + type.getName(), failure);
|
||||
}
|
||||
}
|
||||
|
||||
private static InputStream openClassFile(Class<?> type) {
|
||||
var resource = "/" + type.getName().replace('.', '/') + CLASS_FILE_SUFFIX;
|
||||
var stream = type.getResourceAsStream(resource);
|
||||
if (stream == null) {
|
||||
throw new IllegalArgumentException("Cannot read the class file of " + type.getName()
|
||||
+ " to order its options as declared; settings classes must come from a jar or a class directory");
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,18 @@ package com.volmit.config;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/** Identity and JSON attachment for an IrisFeature configuration class. */
|
||||
/** 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.
|
||||
*
|
||||
* <p>{@code alias} is an optional short JSON key that only modules shipping with the host may set, so
|
||||
* existing pack JSON such as {@code "stilt": {...}} keeps loading. A pack module that sets one fails
|
||||
* the load. An alias contains no ':', so it never equals a feature id.
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface Feature {
|
||||
String id();
|
||||
String name();
|
||||
String field();
|
||||
String alias() default "";
|
||||
com.volmit.FeatureAttachment.Target[] targets();
|
||||
}
|
||||
@@ -25,15 +25,15 @@ public record OptionsMetadata(String description, String snippet, List<Option> o
|
||||
}
|
||||
}
|
||||
|
||||
/** Reads private and inherited fields without constructing settings or accessing values.
|
||||
* Nested object types are preserved for the schema builder to inspect recursively.
|
||||
/** Reads private and inherited fields without constructing settings or accessing values. Options are
|
||||
* in declaration order, a superclass's before its subclass's. Nested object types are preserved for
|
||||
* the schema builder to inspect recursively.
|
||||
*/
|
||||
public static OptionsMetadata inspect(Class<?> type) {
|
||||
var options = new ArrayList<Option>();
|
||||
var names = new HashSet<String>();
|
||||
for (Class<?> current = type; current != null && current != Object.class;
|
||||
current = current.getSuperclass()) {
|
||||
for (var field : current.getDeclaredFields()) {
|
||||
for (var current : hierarchy(type)) {
|
||||
for (var field : DeclaredFieldOrder.of(current)) {
|
||||
int modifiers = field.getModifiers();
|
||||
if (field.isSynthetic() || Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers)) {
|
||||
continue;
|
||||
@@ -64,6 +64,15 @@ public record OptionsMetadata(String description, String snippet, List<Option> o
|
||||
snippet == null ? "" : snippet.value(), options);
|
||||
}
|
||||
|
||||
/** {@code type} and its superclasses below Object, the topmost first. */
|
||||
private static List<Class<?>> hierarchy(Class<?> type) {
|
||||
var classes = new ArrayList<Class<?>>();
|
||||
for (Class<?> current = type; current != null && current != Object.class; current = current.getSuperclass()) {
|
||||
classes.addFirst(current);
|
||||
}
|
||||
return classes;
|
||||
}
|
||||
|
||||
private static boolean isNumeric(Class<?> type) {
|
||||
return Number.class.isAssignableFrom(type) || type == byte.class || type == short.class
|
||||
|| type == int.class || type == long.class || type == float.class || type == double.class;
|
||||
|
||||
Reference in new issue
Block a user