mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Skip unreached content in requireAllContent
- Follow file references from each flagged dimension - Fail only on removed content that dimension reaches - Add tests for an unreferenced biome and reach filtering
This commit is contained in:
4 files changed
+159
-19
No files matched your search
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2026 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package art.arcane.iris.pack;
|
||||
|
||||
import art.arcane.iris.pack.validation.CompatFinding;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Which pack files a dimension can reach, found by following every string in its file that names another pack file,
|
||||
* then every string in those files, and so on. It over-reaches on purpose: a string that matches a key in any folder
|
||||
* counts, so a finding is only treated as unused when nothing the dimension reaches mentions its subject.
|
||||
*/
|
||||
final class PackContentReach {
|
||||
private static final String SNIPPET_PREFIX = "snippet/";
|
||||
private static final String PLACEMENT_SUBJECT = "placement";
|
||||
private static final String PLACEMENT_KEY_SEPARATOR = ", ";
|
||||
private static final int KEY_LENGTH_LIMIT = 512;
|
||||
|
||||
private PackContentReach() {
|
||||
}
|
||||
|
||||
/** Every string reachable from {@code dimensionFile}, plus the dimension's own key. */
|
||||
static Set<String> referencedFrom(File packFolder, File dimensionFile, String dimensionKey, Collection<String> folders) {
|
||||
Set<String> reached = new HashSet<>();
|
||||
Set<File> visited = new HashSet<>();
|
||||
ArrayDeque<File> pending = new ArrayDeque<>();
|
||||
pending.add(dimensionFile);
|
||||
while (!pending.isEmpty()) {
|
||||
File file = pending.poll();
|
||||
if (!visited.add(file.getAbsoluteFile())) {
|
||||
continue;
|
||||
}
|
||||
String text;
|
||||
try {
|
||||
text = Files.readString(file.toPath(), StandardCharsets.UTF_8);
|
||||
} catch (IOException | RuntimeException unreadable) {
|
||||
continue;
|
||||
}
|
||||
for (String value : PackValidationIo.listReferencedKeysFromCorpus(text)) {
|
||||
if (value.isBlank() || value.length() > KEY_LENGTH_LIMIT || value.contains("..") || !reached.add(value)) {
|
||||
continue;
|
||||
}
|
||||
for (String folder : folders) {
|
||||
queueIfFile(pending, new File(packFolder, folder + "/" + value + ".json"));
|
||||
}
|
||||
if (value.startsWith(SNIPPET_PREFIX)) {
|
||||
queueIfFile(pending, new File(packFolder, value + ".json"));
|
||||
}
|
||||
}
|
||||
}
|
||||
reached.add(dimensionKey);
|
||||
return reached;
|
||||
}
|
||||
|
||||
/** Findings without a subject count as reached, so nothing is skipped without a reason. */
|
||||
static boolean reaches(Set<String> reached, CompatFinding finding) {
|
||||
String subjectKey = finding.subjectKey();
|
||||
if (subjectKey.isEmpty() || reached.contains(subjectKey)) {
|
||||
return true;
|
||||
}
|
||||
if (PLACEMENT_SUBJECT.equals(finding.subjectType())) {
|
||||
for (String objectKey : subjectKey.split(PLACEMENT_KEY_SEPARATOR)) {
|
||||
if (reached.contains(objectKey)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void queueIfFile(ArrayDeque<File> pending, File candidate) {
|
||||
if (candidate.isFile()) {
|
||||
pending.add(candidate);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,7 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
@@ -208,7 +209,7 @@ public final class PackValidator {
|
||||
GatedDimensions dimensions = loadEverything(packFolder, data, dimensionFiles);
|
||||
List<CompatFinding> findings = data.getCompatReport().findings();
|
||||
applyCompatFindings(minecraftVersion, dimensions.anyExcluded(), findings, blockingErrors, warnings);
|
||||
applyRequireAllContent(packFolder.getName(), dimensions.dimensionsRequiringAllContent(),
|
||||
applyRequireAllContent(packFolder.getName(), dimensions.reachedByRequiringDimension(),
|
||||
minecraftVersion, findings, blockingErrors);
|
||||
return findings;
|
||||
} catch (Throwable e) {
|
||||
@@ -232,12 +233,13 @@ public final class PackValidator {
|
||||
* anything else resolves) and evaluates every placement reachable from the dimensions, regions and biomes, plus
|
||||
* every static object on each dimension.
|
||||
*
|
||||
* @return whether any dimension in the pack cannot generate on this server, and which dimensions opted into
|
||||
* requiring all content
|
||||
* @return whether any dimension in the pack cannot generate on this server, and what each dimension that opted
|
||||
* into requiring all content can reach
|
||||
*/
|
||||
private static GatedDimensions loadEverything(File packFolder, IrisData data, File[] dimensionFiles) {
|
||||
boolean dimensionExcluded = false;
|
||||
List<String> dimensionsRequiringAllContent = new ArrayList<>();
|
||||
Map<String, Set<String>> reachedByRequiringDimension = new LinkedHashMap<>();
|
||||
List<String> folders = data.getLoaders().values().stream().map(ResourceLoader::getFolderName).toList();
|
||||
File dimensionsFolder = new File(packFolder, DIMENSIONS_FOLDER);
|
||||
for (File dimensionFile : dimensionFiles) {
|
||||
IrisDimension dimension = data.getDimensionLoader().load(
|
||||
@@ -249,7 +251,8 @@ public final class PackValidator {
|
||||
dimensionExcluded = true;
|
||||
}
|
||||
if (dimension.isRequireAllContent()) {
|
||||
dimensionsRequiringAllContent.add(dimension.getLoadKey());
|
||||
reachedByRequiringDimension.put(dimension.getLoadKey(), PackContentReach.referencedFrom(
|
||||
packFolder, dimensionFile, dimension.getLoadKey(), folders));
|
||||
}
|
||||
// Static objects gate themselves (the walker never descends into them), so ask each one here.
|
||||
dimension.evaluateStaticObjectsCompat(data);
|
||||
@@ -268,10 +271,10 @@ public final class PackValidator {
|
||||
for (IrisObjectPlacement placement : placements) {
|
||||
placement.evaluateCompat(data);
|
||||
}
|
||||
return new GatedDimensions(dimensionExcluded, List.copyOf(dimensionsRequiringAllContent));
|
||||
return new GatedDimensions(dimensionExcluded, reachedByRequiringDimension);
|
||||
}
|
||||
|
||||
private record GatedDimensions(boolean anyExcluded, List<String> dimensionsRequiringAllContent) {
|
||||
private record GatedDimensions(boolean anyExcluded, Map<String, Set<String>> reachedByRequiringDimension) {
|
||||
}
|
||||
|
||||
private static void collectPlacements(IrisRegistrant registrant, Set<IrisObjectPlacement> out) {
|
||||
@@ -346,25 +349,29 @@ public final class PackValidator {
|
||||
}
|
||||
|
||||
/**
|
||||
* A dimension with {@code requireAllContent} refuses to load when the gate excluded or dropped anything in its pack;
|
||||
* substitutions are declared fallbacks and never count.
|
||||
* A dimension with {@code requireAllContent} refuses to load when the gate excluded or dropped anything it can
|
||||
* reach; content no file of the dimension leads to is left out, and substitutions are declared fallbacks and never
|
||||
* count.
|
||||
*/
|
||||
static void applyRequireAllContent(
|
||||
String packName,
|
||||
List<String> requiringDimensionKeys,
|
||||
Map<String, Set<String>> reachedByRequiringDimension,
|
||||
String minecraftVersion,
|
||||
List<CompatFinding> findings,
|
||||
List<String> blockingErrors
|
||||
) {
|
||||
if (requiringDimensionKeys == null || requiringDimensionKeys.isEmpty()) {
|
||||
if (reachedByRequiringDimension == null || reachedByRequiringDimension.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
List<CompatFinding> removed = removedContent(findings);
|
||||
if (removed.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
for (String dimensionKey : requiringDimensionKeys) {
|
||||
addDistinct(blockingErrors, List.of(missingContentError(packName, dimensionKey, minecraftVersion, removed)));
|
||||
for (Map.Entry<String, Set<String>> dimension : reachedByRequiringDimension.entrySet()) {
|
||||
List<CompatFinding> reached = removed.stream()
|
||||
.filter(finding -> PackContentReach.reaches(dimension.getValue(), finding))
|
||||
.toList();
|
||||
if (!reached.isEmpty()) {
|
||||
addDistinct(blockingErrors,
|
||||
List.of(missingContentError(packName, dimension.getKey(), minecraftVersion, reached)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+21
-3
@@ -8,6 +8,8 @@ import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@@ -15,7 +17,8 @@ import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PackValidatorMissingContentFailureTest {
|
||||
private static final String PACK = "overworld";
|
||||
private static final List<String> REQUIRING_DIMENSION = List.of("overworld");
|
||||
private static final Map<String, Set<String>> REQUIRING_DIMENSION =
|
||||
Map.of("overworld", Set.of("overworld", "cave/sulfur", "desert", "desert/dunes", "a", "b", "c", "d", "e"));
|
||||
|
||||
private static CompatFinding block(CompatAction action, String subjectKey) {
|
||||
return new CompatFinding(
|
||||
@@ -27,7 +30,7 @@ public class PackValidatorMissingContentFailureTest {
|
||||
CompatRegistry.ENTITY, "minecraft:camel", action, "Spawner", subjectKey, "spawns[0]");
|
||||
}
|
||||
|
||||
private static List<String> blockingErrors(List<String> requiringDimensions, List<CompatFinding> findings) {
|
||||
private static List<String> blockingErrors(Map<String, Set<String>> requiringDimensions, List<CompatFinding> findings) {
|
||||
List<String> blockingErrors = new ArrayList<>();
|
||||
PackValidator.applyRequireAllContent(PACK, requiringDimensions, "26.1.2", findings, blockingErrors);
|
||||
return blockingErrors;
|
||||
@@ -37,7 +40,7 @@ public class PackValidatorMissingContentFailureTest {
|
||||
public void requirementIsOffByDefaultAndMissingContentStaysNonBlocking() {
|
||||
assertFalse(new IrisDimension().isRequireAllContent());
|
||||
|
||||
List<String> errors = blockingErrors(List.of(),
|
||||
List<String> errors = blockingErrors(Map.of(),
|
||||
List.of(block(CompatAction.EXCLUDED, "cave/sulfur"), entity(CompatAction.DROPPED, "desert")));
|
||||
|
||||
assertTrue(errors.toString(), errors.isEmpty());
|
||||
@@ -62,6 +65,21 @@ public class PackValidatorMissingContentFailureTest {
|
||||
+ " | minecraft:camel (entity): dropped Spawner desert at spawns[0]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findingsTheDimensionNeverReachesAreLeftOut() {
|
||||
List<String> errors = blockingErrors(Map.of("overworld", Set.of("overworld", "desert")), List.of(
|
||||
block(CompatAction.EXCLUDED, "cave/sulfur"), entity(CompatAction.DROPPED, "desert")));
|
||||
|
||||
assertEquals(List.of("Dimension 'overworld' in pack 'overworld' uses requireAllContent and content is unavailable"
|
||||
+ " on Minecraft 26.1.2: minecraft:camel (entity): dropped Spawner desert at spawns[0]"), errors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dimensionReachingNoRemovedContentIsLoadable() {
|
||||
assertTrue(blockingErrors(Map.of("overworld", Set.of("overworld")),
|
||||
List.of(block(CompatAction.EXCLUDED, "cave/sulfur"))).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void substitutionsAloneKeepTheDimensionLoadable() {
|
||||
List<String> errors = blockingErrors(REQUIRING_DIMENSION, List.of(block(CompatAction.SUBSTITUTED, "desert/dunes")));
|
||||
|
||||
+15
@@ -70,6 +70,21 @@ public class MissingContentFailurePackValidationTest {
|
||||
+ " dropped region main at landBiomes[1] sulfur"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requiringDimensionIgnoresContentItNeverReaches() throws Exception {
|
||||
File pack = temporaryFolder.newFolder(PACK);
|
||||
write(pack, "dimensions/main.json", "{\"requireAllContent\":true,\"regions\":[\"main\"]}");
|
||||
write(pack, "regions/main.json", "{\"landBiomes\":[\"plain\"]}");
|
||||
write(pack, "biomes/plain.json", "{\"name\":\"Plain\"}");
|
||||
write(pack, "biomes/stray.json",
|
||||
"{\"name\":\"Stray\",\"layers\":[{\"palette\":[{\"block\":\"minecraft:sulfur\"}]}]}");
|
||||
|
||||
PackValidationResult result = PackValidator.validate(pack);
|
||||
|
||||
assertFalse(result.getCompatFindings().isEmpty());
|
||||
assertFalse(result.getBlockingErrors().toString(), hasMissingContentError(result));
|
||||
}
|
||||
|
||||
private static void write(File root, String relative, String content) throws Exception {
|
||||
Path path = root.toPath().resolve(relative);
|
||||
Files.createDirectories(path.getParent());
|
||||
|
||||
Reference in new issue
Block a user