Reject unknown hydrology keys

- Report keys under hydrology and hydrologyPolicy that the config classes do not declare, with the full JSON path, as blocking errors
- Derive known keys by reflection over the fields Gson reads, and treat map keys as data while checking the objects inside
- Check a disabled hydrology block too, so a misspelt enabled flag is caught
- Follow snippet references from hydrology into their files
- Replace the tests that pinned unknown keys as ignored
- Pin the no-sea warning on both sides of its boundary, fluidHeight -64 and -63
This commit is contained in:
Brian Fopiano committed 2026-11-02 18:06:17 -05:00
1 parent 90fdd980e0
commit d6a3456024
2 files changed
+317 -30

No files matched your search

@@ -15,17 +15,27 @@ import art.arcane.iris.generation.terrain.IrisMaterialPalette;
import art.arcane.iris.generation.terrain.IrisRegion;
import art.arcane.iris.generation.terrain.ShoreBiomeLineages;
import art.arcane.iris.pack.loading.IrisData;
import art.arcane.iris.pack.loading.IrisRegistrant;
import art.arcane.iris.pack.schema.annotation.Snippet;
import art.arcane.iris.spi.IrisLogging;
import art.arcane.iris.spi.IrisPlatforms;
import art.arcane.volmlib.util.json.JSONArray;
import art.arcane.volmlib.util.json.JSONObject;
import art.arcane.volmlib.util.mantle.flag.ReservedFlag;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@@ -39,6 +49,7 @@ final class PackHydrologyValidator {
private static final String CONFIG = "hydrology";
private static final String POLICY = "hydrologyPolicy";
private static final String DEFAULT_FLUID = "water";
private static final String SNIPPET_ROOT = "snippet/";
private static final List<RangeField> RANGES = List.of(
new RangeField("ponds.freeboard", Floor.NON_NEGATIVE, true),
new RangeField("ponds.seeded.radius", Floor.POSITIVE, true),
@@ -93,6 +104,7 @@ final class PackHydrologyValidator {
errors.add(path + " must be an object.");
return data;
}
rejectUnknownKeys(path, hydrology, IrisHydrology.class, packFolder, errors);
if (!hydrology.optBoolean("enabled", false)) {
return data;
}
@@ -117,6 +129,96 @@ final class PackHydrologyValidator {
return loaded;
}
static void rejectUnknownKeys(String field, Object value, Type type, File packFolder, List<String> errors) {
rejectUnknownKeys(field, value, type, new KeyScan(packFolder, errors, new HashSet<>()));
}
private static void rejectUnknownKeys(String field, Object value, Type type, KeyScan scan) {
Class<?> raw = TypeToken.get(type).getRawType();
if (value instanceof JSONArray array) {
Type element = elementType(type, raw);
for (int index = 0; index < array.length(); index++) {
rejectUnknownKeys(field + "[" + index + "]", array.opt(index), element, scan);
}
} else if (value instanceof JSONObject object) {
if (Map.class.isAssignableFrom(raw)) {
Type entry = typeArgument(type, 1);
for (String key : new TreeSet<>(object.keySet())) {
rejectUnknownKeys(field + "." + key, object.opt(key), entry, scan);
}
} else if (hasDeclaredKeys(raw)) {
rejectUnknownFields(field, object, raw, scan);
}
} else if (value instanceof String reference && reference.startsWith(SNIPPET_ROOT) && raw.isAnnotationPresent(Snippet.class)) {
rejectUnknownSnippetKeys(field, reference, raw, scan);
}
}
private static void rejectUnknownFields(String field, JSONObject object, Class<?> type, KeyScan scan) {
Map<String, Type> declared = declaredKeys(type);
for (String key : new TreeSet<>(object.keySet())) {
String child = field + "." + key;
Type declaredType = declared.get(key);
if (declaredType == null) {
scan.errors().add(child + " is not a known key, so its value would be ignored. Known keys there: " + declared.keySet() + ".");
} else {
rejectUnknownKeys(child, object.opt(key), declaredType, scan);
}
}
}
private static void rejectUnknownSnippetKeys(String field, String reference, Class<?> type, KeyScan scan) {
String base = SNIPPET_ROOT + type.getDeclaredAnnotation(Snippet.class).value() + "/";
String snippet = reference.startsWith(base) ? reference : base + reference.substring(SNIPPET_ROOT.length());
if (!scan.activeSnippets().add(snippet)) {
return;
}
File file = new File(scan.packFolder(), snippet + ".json");
JSONObject object = file.isFile() ? PackValidationIo.readJson(file) : null;
if (object != null) {
rejectUnknownFields(field + " (" + snippet + ")", object, type, scan);
}
scan.activeSnippets().remove(snippet);
}
private static Type elementType(Type type, Class<?> raw) {
if (raw.isArray()) {
return raw.getComponentType();
}
return Collection.class.isAssignableFrom(raw) ? typeArgument(type, 0) : Object.class;
}
private static Type typeArgument(Type type, int index) {
if (type instanceof ParameterizedType parameterized && index < parameterized.getActualTypeArguments().length) {
return parameterized.getActualTypeArguments()[index];
}
return Object.class;
}
private static boolean hasDeclaredKeys(Class<?> type) {
return !type.isPrimitive() && !type.isEnum() && !type.isArray() && !type.getPackageName().startsWith("java.");
}
private static Map<String, Type> declaredKeys(Class<?> type) {
Map<String, Type> keys = new TreeMap<>();
for (Class<?> current = type; current != null && current != Object.class; current = current.getSuperclass()) {
for (Field field : current.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers) || field.isSynthetic()) {
continue;
}
SerializedName serialized = field.getAnnotation(SerializedName.class);
keys.putIfAbsent(serialized == null ? field.getName() : serialized.value(), field.getGenericType());
if (serialized != null) {
for (String alternate : serialized.alternate()) {
keys.putIfAbsent(alternate, field.getGenericType());
}
}
}
}
return keys;
}
private static boolean validateCompile(String path, IrisDimension dimension, List<String> errors) {
try {
HydrologySettings.compile(dimension.getHydrology(), 0L, dimension.getFluidHeight(), 0,
@@ -282,11 +384,13 @@ final class PackHydrologyValidator {
private static void validatePolicies(String path, String dimensionKey, IrisData data, IrisDimension dimension, PolicyFluids fluids,
List<String> errors) {
rejectUnknownPolicyKeys("Dimension '" + dimensionKey + "'." + POLICY, dimension, data, errors);
validatePolicy("Dimension '" + dimensionKey + "'." + POLICY, dimension.getHydrologyPolicy(), data, fluids, path, errors);
List<IrisRegion> regions = new ArrayList<>(dimension.getAllRegions(() -> data));
regions.sort(Comparator.comparing(IrisRegion::getLoadKey, Comparator.nullsFirst(Comparator.naturalOrder())));
for (IrisRegion region : regions) {
if (region != null) {
rejectUnknownPolicyKeys("Region '" + region.getLoadKey() + "'." + POLICY, region, data, errors);
validatePolicy("Region '" + region.getLoadKey() + "'." + POLICY, region.getHydrologyPolicy(), data, fluids, path, errors);
}
}
@@ -294,11 +398,20 @@ final class PackHydrologyValidator {
biomes.sort(Comparator.comparing(IrisBiome::getLoadKey, Comparator.nullsFirst(Comparator.naturalOrder())));
for (IrisBiome biome : biomes) {
if (biome != null) {
rejectUnknownPolicyKeys("Biome '" + biome.getLoadKey() + "'." + POLICY, biome, data, errors);
validatePolicy("Biome '" + biome.getLoadKey() + "'." + POLICY, biome.getHydrologyPolicy(), data, fluids, path, errors);
}
}
}
private static void rejectUnknownPolicyKeys(String field, IrisRegistrant owner, IrisData data, List<String> errors) {
File file = owner.getLoadFile();
JSONObject json = file == null ? null : PackValidationIo.readJson(file);
if (json != null) {
rejectUnknownKeys(field, json.opt(POLICY), IrisHydrologyPolicy.class, data.getDataFolder(), errors);
}
}
private static void validatePolicy(String field, IrisHydrologyPolicy policy, IrisData data, PolicyFluids fluids, String configPath,
List<String> errors) {
if (policy == null) {
@@ -501,4 +614,7 @@ final class PackHydrologyValidator {
private record Findings(List<String> errors, List<String> warnings) {
}
private record KeyScan(File packFolder, List<String> errors, Set<String> activeSnippets) {
}
}