mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-29 01:04:29 +00:00
Enforce declared bounds and make importedStructures a typed check
The settings decoder now enforces @MinNumber/@MaxNumber on every option a pack writes, nested ones included, naming the full path. Module resource files get the same check. Pack validation reports every dimension, region and biome whose settings fail to decode as a blocking error; before, the failure only logged and the pack passed. With that in place, batch 9 S15 replaces 120 lines of hand-written JSON checks of importedStructures with a typed check beside StructureValidator for what types cannot say: blank keys and a structure set without a namespace. @MinItems stays unenforced for now (OPEN_QUESTIONS 57).
This commit is contained in:
1 parent
d1a9c2c68b
commit
1bcd846fb5
11 files changed
+404
-303
No files matched your search
+61
-157
@@ -1,187 +1,91 @@
|
||||
package art.arcane.iris.core.pack;
|
||||
|
||||
import art.arcane.volmlib.util.json.JSONArray;
|
||||
import art.arcane.volmlib.util.json.JSONObject;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/** A dimension's {@code importedStructures} as pack validation sees it: decoded into its typed settings, then checked. */
|
||||
public class PackValidatorImportedStructurePolicyTest {
|
||||
@Test
|
||||
public void denyOnlyPolicyAcceptsDisabledKeysAndAdjustments() {
|
||||
JSONObject policy = new JSONObject()
|
||||
.put("disabled", new JSONArray().put("minecraft:stronghold"))
|
||||
.put("disabledExact", new JSONArray().put("minecraft:ruined_portal"))
|
||||
.put("frequencyOverrides", new JSONArray().put(new JSONObject()
|
||||
.put("structureSet", "minecraft:nether_complexes")
|
||||
.put("multiplier", 1.1D)))
|
||||
.put("adjustments", new JSONArray().put(new JSONObject()
|
||||
.put("match", new JSONArray().put("minecraft:village"))));
|
||||
List<String> errors = validate(policy);
|
||||
private static final String CANNOT_LOAD = "Dimension 'main' cannot be loaded: Cannot read settings iris:structure"
|
||||
+ " under \"importedStructures\" in Dimension main: ";
|
||||
|
||||
assertTrue(errors.isEmpty());
|
||||
}
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Test
|
||||
public void malformedFrequencyOverridesAreRejected() {
|
||||
JSONObject policy = new JSONObject()
|
||||
.put("frequencyOverrides", new JSONArray()
|
||||
.put("minecraft:nether_complexes")
|
||||
.put(new JSONObject().put("structureSet", "nether_complexes"))
|
||||
.put(new JSONObject()
|
||||
.put("structureSet", "minecraft:ruined_portals")
|
||||
.put("multiplier", 0D))
|
||||
.put(new JSONObject()
|
||||
.put("structureSet", "minecraft:nether_fossils")
|
||||
.put("multiplier", "often")));
|
||||
List<String> errors = validate(policy);
|
||||
|
||||
assertEquals(4, errors.size());
|
||||
assertTrue(errors.stream().anyMatch(error -> error.contains("frequencyOverrides[0] must be an object")));
|
||||
assertTrue(errors.stream().anyMatch(error -> error.contains("frequencyOverrides[1].structureSet")));
|
||||
assertTrue(errors.stream().anyMatch(error -> error.contains("frequencyOverrides[2].multiplier must be at least")));
|
||||
assertTrue(errors.stream().anyMatch(error -> error.contains("frequencyOverrides[3].multiplier must be a number")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encaseTerrainAndYBandAdjustmentsAreAccepted() {
|
||||
JSONObject policy = new JSONObject()
|
||||
.put("adjustments", new JSONArray().put(new JSONObject()
|
||||
.put("match", new JSONArray().put("minecraft:stronghold"))
|
||||
.put("yBand", new JSONObject().put("min", -120).put("max", -20))
|
||||
.put("terrain", new JSONObject()
|
||||
.put("mode", "ENCASE")
|
||||
.put("horizontalPadding", 4)
|
||||
.put("ceilingPadding", 4)
|
||||
.put("floorPadding", 4)
|
||||
.put("encasePalette", new JSONObject()
|
||||
.put("palette", new JSONArray()
|
||||
.put(new JSONObject()
|
||||
.put("block", "minecraft:stone_bricks")
|
||||
.put("weight", 6))
|
||||
.put(new JSONObject()
|
||||
.put("block", "minecraft:cobblestone")
|
||||
.put("weight", 1)))))));
|
||||
List<String> errors = validate(policy);
|
||||
public void theShippingPolicyShapesAreAccepted() throws Exception {
|
||||
List<String> errors = validate("""
|
||||
{"disabled": ["minecraft:stronghold"], "disabledExact": ["minecraft:ruined_portal"],
|
||||
"frequencyOverrides": [{"structureSet": "minecraft:nether_complexes", "multiplier": 1.1}],
|
||||
"adjustments": [
|
||||
{"match": ["minecraft:stronghold"], "yBand": {"min": -120, "max": -20},
|
||||
"terrain": {"mode": "ENCASE", "horizontalPadding": 4, "ceilingPadding": 4, "floorPadding": 4,
|
||||
"encasePalette": {"palette": [{"block": "minecraft:stone_bricks", "weight": 6}]}}},
|
||||
{"match": ["towns_and_towers:"], "terrain": {"mode": "VACUUM"}}
|
||||
]}
|
||||
""");
|
||||
|
||||
assertTrue(errors.toString(), errors.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonObjectEncasePaletteIsRejected() {
|
||||
JSONObject policy = new JSONObject()
|
||||
.put("adjustments", new JSONArray().put(new JSONObject()
|
||||
.put("match", new JSONArray().put("minecraft:stronghold"))
|
||||
.put("terrain", new JSONObject()
|
||||
.put("mode", "ENCASE")
|
||||
.put("encasePalette", "minecraft:stone_bricks"))));
|
||||
List<String> errors = validate(policy);
|
||||
public void anOptionAboveItsBoundBlocksThePackNamingItsPath() throws Exception {
|
||||
List<String> errors = validate("{\"frequencyOverrides\": [{\"structureSet\": \"minecraft:villages\", \"multiplier\": 99}]}");
|
||||
|
||||
assertEquals(List.of(CANNOT_LOAD + "importedStructures.frequencyOverrides[0].multiplier is 99.0; it must be at most 16"), errors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aBoundInsideNestedSettingsNamesTheWholePath() throws Exception {
|
||||
List<String> errors = validate("{\"adjustments\": [{\"match\": [\"minecraft:stronghold\"], \"yBand\": {\"min\": -5000}}]}");
|
||||
|
||||
assertEquals(List.of(CANNOT_LOAD + "importedStructures.adjustments[0].yBand.min is -5000; it must be at least -4064"), errors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anUnknownTerrainModeBlocksThePack() throws Exception {
|
||||
List<String> errors = validate("{\"adjustments\": [{\"match\": [\"minecraft:stronghold\"], \"terrain\": {\"mode\": \"SINK\"}}]}");
|
||||
|
||||
assertEquals(1, errors.size());
|
||||
assertTrue(errors.get(0), errors.get(0)
|
||||
.contains("adjustments[0].terrain.encasePalette must be an object"));
|
||||
assertTrue(errors.get(0), errors.get(0).startsWith(CANNOT_LOAD) && errors.get(0).contains("\"SINK\" is not one of"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void malformedYBandAndTerrainAdjustmentsAreRejected() {
|
||||
JSONObject policy = new JSONObject()
|
||||
.put("adjustments", new JSONArray()
|
||||
.put(new JSONObject()
|
||||
.put("match", new JSONArray().put("minecraft:stronghold"))
|
||||
.put("yBand", -120))
|
||||
.put(new JSONObject()
|
||||
.put("match", new JSONArray().put("minecraft:trial_chambers"))
|
||||
.put("yBand", new JSONObject().put("min", -9000))
|
||||
.put("terrain", new JSONObject().put("mode", "SOLIDIFY"))));
|
||||
List<String> errors = validate(policy);
|
||||
public void blankKeysAndAStructureSetWithoutNamespaceAreErrors() throws Exception {
|
||||
List<String> errors = validate("""
|
||||
{"disabled": ["minecraft:village", " "],
|
||||
"frequencyOverrides": [{"structureSet": "villages"}],
|
||||
"adjustments": [{"match": [""]}]}
|
||||
""");
|
||||
|
||||
assertEquals(3, errors.size());
|
||||
assertTrue(errors.stream().anyMatch(error ->
|
||||
error.contains("adjustments[0].yBand must be an object")));
|
||||
assertTrue(errors.stream().anyMatch(error ->
|
||||
error.contains("adjustments[1].yBand.min")));
|
||||
assertTrue(errors.stream().anyMatch(error ->
|
||||
error.contains("adjustments[1].terrain.mode")));
|
||||
assertEquals(List.of(
|
||||
"pack:dimension/main: importedStructures.disabled[1] is blank; write a structure key",
|
||||
"pack:dimension/main: importedStructures.frequencyOverrides[0].structureSet is \"villages\"; it must be a"
|
||||
+ " namespaced key such as minecraft:villages",
|
||||
"pack:dimension/main: importedStructures.adjustments[0].match[0] is blank; write a structure key"), errors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void legacyModeAndEnabledWhitelistAreRejected() {
|
||||
JSONObject policy = new JSONObject()
|
||||
.put("mode", "ALL_OFF")
|
||||
.put("enabled", new JSONArray().put("minecraft:monument"));
|
||||
List<String> errors = validate(policy);
|
||||
|
||||
assertEquals(2, errors.size());
|
||||
assertTrue(errors.get(0).contains("importedStructures.mode is not supported"));
|
||||
assertTrue(errors.get(1).contains("importedStructures.enabled is not supported"));
|
||||
/** The blocking errors of a pack whose one dimension declares {@code policy} as its {@code importedStructures}. */
|
||||
private List<String> validate(String policy) throws Exception {
|
||||
File pack = temporaryFolder.newFolder("pack");
|
||||
write(pack, "dimensions/main.json", "{\"regions\": [\"region\"], \"importedStructures\": " + policy + "}");
|
||||
write(pack, "regions/region.json", "{\"landBiomes\": [\"biome\"]}");
|
||||
write(pack, "biomes/biome.json", "{\"name\": \"Biome\"}");
|
||||
return PackValidator.validate(pack).getBlockingErrors();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void explicitNullsAndWrongShapesAreRejected() {
|
||||
JSONObject policy = new JSONObject()
|
||||
.put("disabled", JSONObject.NULL)
|
||||
.put("disabledExact", new JSONObject())
|
||||
.put("frequencyOverrides", JSONObject.NULL)
|
||||
.put("adjustments", new JSONObject());
|
||||
List<String> errors = validate(policy);
|
||||
|
||||
assertEquals(4, errors.size());
|
||||
assertTrue(errors.stream().anyMatch(error -> error.contains("'disabled' must be an array")));
|
||||
assertTrue(errors.stream().anyMatch(error -> error.contains("'disabledExact' must be an array")));
|
||||
assertTrue(errors.stream().anyMatch(error -> error.contains("frequencyOverrides must be an array")));
|
||||
assertTrue(errors.stream().anyMatch(error -> error.contains("adjustments must be an array")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void blankOrNonStringExactKeysAreRejected() {
|
||||
JSONObject policy = new JSONObject()
|
||||
.put("disabledExact", new JSONArray().put(" ").put(4));
|
||||
List<String> errors = validate(policy);
|
||||
|
||||
assertEquals(2, errors.size());
|
||||
assertTrue(errors.get(0).contains("'disabledExact' has a blank or non-string entry at index 0"));
|
||||
assertTrue(errors.get(1).contains("'disabledExact' has a blank or non-string entry at index 1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void explicitNullPolicyIsRejectedWhileOmissionUsesDefaults() {
|
||||
List<String> missingErrors = new ArrayList<>();
|
||||
PackDimensionValidator.validateImportedStructurePolicy("overworld", new JSONObject(),
|
||||
missingErrors, new ArrayList<>());
|
||||
assertTrue(missingErrors.isEmpty());
|
||||
|
||||
List<String> nullErrors = new ArrayList<>();
|
||||
PackDimensionValidator.validateImportedStructurePolicy("overworld",
|
||||
new JSONObject().put("importedStructures", JSONObject.NULL), nullErrors, new ArrayList<>());
|
||||
assertEquals(List.of("Dimension 'overworld' importedStructures must be an object."), nullErrors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removedClearVegetationAdjustmentWarnsWithoutBlockingThePack() {
|
||||
JSONObject policy = new JSONObject()
|
||||
.put("adjustments", new JSONArray().put(new JSONObject()
|
||||
.put("match", new JSONArray().put("minecraft:village"))
|
||||
.put("clearVegetation", true)));
|
||||
List<String> errors = new ArrayList<>();
|
||||
List<String> warnings = new ArrayList<>();
|
||||
|
||||
PackDimensionValidator.validateImportedStructurePolicy("overworld",
|
||||
new JSONObject().put("importedStructures", policy), errors, warnings);
|
||||
|
||||
assertTrue(errors.toString(), errors.isEmpty());
|
||||
assertEquals(1, warnings.size());
|
||||
assertTrue(warnings.get(0), warnings.get(0)
|
||||
.contains("adjustments[0].clearVegetation was removed and is ignored"));
|
||||
}
|
||||
|
||||
private List<String> validate(JSONObject policy) {
|
||||
List<String> errors = new ArrayList<>();
|
||||
PackDimensionValidator.validateImportedStructurePolicy("overworld",
|
||||
new JSONObject().put("importedStructures", policy), errors, new ArrayList<>());
|
||||
return errors;
|
||||
private void write(File root, String relative, String content) throws Exception {
|
||||
Path target = new File(root, relative).toPath();
|
||||
Files.createDirectories(target.getParent());
|
||||
Files.writeString(target, content, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
-19
@@ -1,7 +1,5 @@
|
||||
package art.arcane.iris.core.pack;
|
||||
|
||||
import art.arcane.volmlib.util.json.JSONArray;
|
||||
import art.arcane.volmlib.util.json.JSONObject;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
@@ -77,23 +75,6 @@ public class PackValidatorStructureTerrainBackendTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void acceptsNativeOnlyTerrainModesForImportedStructureAdjustments() {
|
||||
for (String mode : List.of("VACUUM", "ENCASE")) {
|
||||
JSONObject policy = new JSONObject().put("adjustments", new JSONArray().put(
|
||||
new JSONObject()
|
||||
.put("match", new JSONArray().put("towns_and_towers:"))
|
||||
.put("terrain", new JSONObject().put("mode", mode))));
|
||||
List<String> errors = new ArrayList<>();
|
||||
|
||||
PackDimensionValidator.validateImportedStructurePolicy(
|
||||
"overworld", new JSONObject().put("importedStructures", policy),
|
||||
errors, new ArrayList<>());
|
||||
|
||||
assertTrue(mode + ": " + errors, errors.isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
private void writePlacement(File pack, String relativePath, String placementId,
|
||||
String structureKey, boolean nativeStructure,
|
||||
String terrainMode) throws Exception {
|
||||
|
||||
Reference in new issue
Block a user