mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-29 17:23:59 +00:00
Group unknown pack field warnings
- Report each field once per pack, keyed by pack-relative path - Print one block per pack after loading goes quiet - Drop the snippet and Gson note
This commit is contained in:
1 parent
10463d58cc
commit
38ec1752c1
4 files changed
+249
-20
No files matched your search
@@ -22,7 +22,7 @@ final class JsonSchemaValidator {
|
||||
private JsonSchemaValidator() {
|
||||
}
|
||||
|
||||
static void validateTopLevelKeys(JSONObject parsed, String rawText, File file, String resourceTypeName, Class<?> objectClass) {
|
||||
static void validateTopLevelKeys(JSONObject parsed, String rawText, File file, File dataFolder, String resourceTypeName, Class<?> objectClass) {
|
||||
if (parsed == null || objectClass == null) {
|
||||
return;
|
||||
}
|
||||
@@ -31,7 +31,7 @@ final class JsonSchemaValidator {
|
||||
if (known.contains(key)) {
|
||||
continue;
|
||||
}
|
||||
reportUnknownKey(key, rawText, file, resourceTypeName, known);
|
||||
reportUnknownKey(key, rawText, file, dataFolder, resourceTypeName, known);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,25 +56,10 @@ final class JsonSchemaValidator {
|
||||
IrisLogging.warn(out.toString());
|
||||
}
|
||||
|
||||
private static void reportUnknownKey(String key, String rawText, File file, String resourceTypeName, Set<String> known) {
|
||||
private static void reportUnknownKey(String key, String rawText, File file, File dataFolder, String resourceTypeName, Set<String> known) {
|
||||
int line = findLineForKey(rawText, key);
|
||||
String suggestion = closestMatch(key, known);
|
||||
StringBuilder out = new StringBuilder();
|
||||
out.append("Unknown ").append(resourceTypeName).append(" field ")
|
||||
.append(C.WHITE).append('"').append(key).append('"').append(C.YELLOW)
|
||||
.append(" in ").append(C.WHITE).append(file.getPath());
|
||||
if (line > 0) {
|
||||
out.append(":").append(line);
|
||||
}
|
||||
out.append(C.YELLOW).append(" (Gson will silently ignore this)");
|
||||
if (suggestion != null) {
|
||||
out.append(". Did you mean ").append(C.WHITE).append('"').append(suggestion).append('"').append(C.YELLOW).append("?");
|
||||
}
|
||||
String snippet = buildSnippet(rawText, line);
|
||||
if (snippet != null) {
|
||||
out.append('\n').append(snippet);
|
||||
}
|
||||
IrisLogging.warn(out.toString());
|
||||
UnknownFieldReport.record(dataFolder, file, resourceTypeName, key, line, suggestion);
|
||||
}
|
||||
|
||||
private static Set<String> collectFieldNames(Class<?> cls) {
|
||||
|
||||
@@ -417,7 +417,7 @@ public class ResourceLoader<T extends IrisRegistrant> implements MeteredCache {
|
||||
PrecisionStopwatch p = PrecisionStopwatch.start();
|
||||
rawText = IO.readAll(j);
|
||||
JSONObject parsed = new JSONObject(rawText);
|
||||
JsonSchemaValidator.validateTopLevelKeys(parsed, rawText, j, resourceTypeName, objectClass);
|
||||
JsonSchemaValidator.validateTopLevelKeys(parsed, rawText, j, root, resourceTypeName, objectClass);
|
||||
if (objectClass == IrisDimension.class) {
|
||||
PackValidator.requireValidObjectSettings(manager.getDataFolder(), name, parsed);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
package art.arcane.iris.pack.loading;
|
||||
|
||||
import art.arcane.iris.localization.C;
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
import art.arcane.iris.world.history.GenerationHistoryPaths;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
final class UnknownFieldReport {
|
||||
private static final long QUIET_PERIOD_BEFORE_FLUSH_MS = 1_500L;
|
||||
private static final String FLUSH_THREAD_NAME = "Iris Unknown Field Report";
|
||||
private static final Set<String> REPORTED = ConcurrentHashMap.newKeySet();
|
||||
private static final List<UnknownField> PENDING = new ArrayList<>();
|
||||
private static final ScheduledExecutorService FLUSHER = Executors.newSingleThreadScheduledExecutor(runnable -> {
|
||||
Thread thread = new Thread(runnable, FLUSH_THREAD_NAME);
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
});
|
||||
private static ScheduledFuture<?> scheduledFlush;
|
||||
|
||||
private UnknownFieldReport() {
|
||||
}
|
||||
|
||||
static void record(File dataFolder, File file, String resourceTypeName, String field, int line, String suggestion) {
|
||||
String pack = packLabel(dataFolder);
|
||||
String location = relativeLocation(dataFolder, file, line);
|
||||
UnknownField unknown = new UnknownField(pack, resourceTypeName, field, location, suggestion);
|
||||
if (!REPORTED.add(unknown.identity())) {
|
||||
return;
|
||||
}
|
||||
synchronized (PENDING) {
|
||||
PENDING.add(unknown);
|
||||
rescheduleFlush();
|
||||
}
|
||||
}
|
||||
|
||||
static void flush() {
|
||||
drainReportLines().forEach(line -> IrisLogging.warn("%s", line));
|
||||
}
|
||||
|
||||
static List<String> drainReportLines() {
|
||||
List<UnknownField> batch;
|
||||
synchronized (PENDING) {
|
||||
batch = new ArrayList<>(PENDING);
|
||||
PENDING.clear();
|
||||
}
|
||||
List<String> lines = new ArrayList<>();
|
||||
groupByPack(batch).forEach((pack, fields) -> appendPackLines(lines, pack, fields));
|
||||
return lines;
|
||||
}
|
||||
|
||||
static void resetForTests() {
|
||||
synchronized (PENDING) {
|
||||
PENDING.clear();
|
||||
}
|
||||
REPORTED.clear();
|
||||
}
|
||||
|
||||
private static void rescheduleFlush() {
|
||||
if (scheduledFlush != null) {
|
||||
scheduledFlush.cancel(false);
|
||||
}
|
||||
scheduledFlush = FLUSHER.schedule(UnknownFieldReport::flush, QUIET_PERIOD_BEFORE_FLUSH_MS, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
private static Map<String, Map<String, List<UnknownField>>> groupByPack(List<UnknownField> batch) {
|
||||
Map<String, Map<String, List<UnknownField>>> packs = new TreeMap<>();
|
||||
for (UnknownField unknown : batch) {
|
||||
packs.computeIfAbsent(unknown.pack(), key -> new TreeMap<>())
|
||||
.computeIfAbsent(unknown.typeAndField(), key -> new ArrayList<>())
|
||||
.add(unknown);
|
||||
}
|
||||
return packs;
|
||||
}
|
||||
|
||||
private static void appendPackLines(List<String> lines, String pack, Map<String, List<UnknownField>> fields) {
|
||||
lines.add(pack + ": unknown fields, ignored");
|
||||
int labelWidth = fields.keySet().stream().mapToInt(String::length).max().orElse(0);
|
||||
fields.forEach((typeAndField, occurrences) -> lines.add(describeField(typeAndField, occurrences, labelWidth)));
|
||||
}
|
||||
|
||||
private static String describeField(String typeAndField, List<UnknownField> occurrences, int labelWidth) {
|
||||
UnknownField first = occurrences.getFirst();
|
||||
int count = occurrences.size();
|
||||
StringBuilder out = new StringBuilder(" ")
|
||||
.append(C.WHITE).append(padRight(typeAndField, labelWidth)).append(C.YELLOW)
|
||||
.append(" ").append(count).append(count == 1 ? " file " : " files")
|
||||
.append(" ").append(count == 1 ? "" : "e.g. ").append(first.location());
|
||||
if (first.suggestion() != null) {
|
||||
out.append(" (did you mean ").append(C.WHITE).append('"').append(first.suggestion()).append('"').append(C.YELLOW).append("?)");
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private static String padRight(String text, int width) {
|
||||
return text + " ".repeat(Math.max(0, width - text.length()));
|
||||
}
|
||||
|
||||
private static String packLabel(File dataFolder) {
|
||||
if (dataFolder == null) {
|
||||
return "pack";
|
||||
}
|
||||
Path epochDimension = epochDimensionRoot(dataFolder.toPath().toAbsolutePath().normalize());
|
||||
return epochDimension != null ? epochDimension.getFileName().toString() : dataFolder.getName();
|
||||
}
|
||||
|
||||
private static Path epochDimensionRoot(Path dataFolder) {
|
||||
Path epochs = ancestor(dataFolder, 2);
|
||||
if (!GenerationHistoryPaths.LEGACY_PACK_DIRECTORY_NAME.equals(name(dataFolder))
|
||||
|| !GenerationHistoryPaths.EPOCHS_DIRECTORY_NAME.equals(name(epochs))) {
|
||||
return null;
|
||||
}
|
||||
return ancestor(epochs, 3);
|
||||
}
|
||||
|
||||
private static Path ancestor(Path path, int levels) {
|
||||
Path current = path;
|
||||
for (int i = 0; i < levels && current != null; i++) {
|
||||
current = current.getParent();
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
private static String name(Path path) {
|
||||
return path == null || path.getFileName() == null ? null : path.getFileName().toString();
|
||||
}
|
||||
|
||||
private static String relativeLocation(File dataFolder, File file, int line) {
|
||||
String path = file.getPath();
|
||||
if (dataFolder != null) {
|
||||
Path root = dataFolder.toPath().toAbsolutePath().normalize();
|
||||
Path target = file.toPath().toAbsolutePath().normalize();
|
||||
if (target.startsWith(root)) {
|
||||
path = root.relativize(target).toString().replace(File.separatorChar, '/');
|
||||
}
|
||||
}
|
||||
return line > 0 ? path + ":" + line : path;
|
||||
}
|
||||
|
||||
private record UnknownField(String pack, String resourceTypeName, String field, String location, String suggestion) {
|
||||
String typeAndField() {
|
||||
return resourceTypeName + " \"" + field + "\"";
|
||||
}
|
||||
|
||||
String identity() {
|
||||
return pack + '\u0000' + typeAndField() + '\u0000' + location;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package art.arcane.iris.pack.loading;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class UnknownFieldReportTest {
|
||||
private static final File SOURCE_PACK = new File("/server/plugins/Iris/packs/cavetest");
|
||||
private static final File EPOCH_PACK = new File("/server/world/dimensions/iris/cavetest/iris/generation/epochs/"
|
||||
+ "ba9f3038756c38bc3f3a3174f3e7c575b766f76f422a129d6e8eb98380c33b8b/pack");
|
||||
|
||||
@Before
|
||||
public void reset() {
|
||||
UnknownFieldReport.resetForTests();
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
UnknownFieldReport.resetForTests();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void epochCopyAndRepeatedLoadsCollapseIntoOneEntry() {
|
||||
recordBiomeCaves(SOURCE_PACK, "biomes/carving/volcanic.json", 144);
|
||||
recordBiomeCaves(EPOCH_PACK, "biomes/carving/volcanic.json", 144);
|
||||
recordBiomeCaves(SOURCE_PACK, "biomes/carving/volcanic.json", 144);
|
||||
|
||||
List<String> lines = plain(UnknownFieldReport.drainReportLines());
|
||||
|
||||
assertEquals(List.of(
|
||||
"cavetest: unknown fields, ignored",
|
||||
" Biome \"caves\" 1 file biomes/carving/volcanic.json:144"
|
||||
), lines);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void manyFilesWithTheSameFieldShowOneExampleAndACount() {
|
||||
recordBiomeCaves(SOURCE_PACK, "biomes/carving/deep-ravine.json", 157);
|
||||
recordBiomeCaves(SOURCE_PACK, "biomes/carving/ice-ravine.json", 149);
|
||||
recordBiomeCaves(SOURCE_PACK, "biomes/carving/volcanic.json", 144);
|
||||
UnknownFieldReport.record(SOURCE_PACK, new File(SOURCE_PACK, "dimensions/cavetest.json"), "Dimension", "caves", 856, null);
|
||||
|
||||
List<String> lines = plain(UnknownFieldReport.drainReportLines());
|
||||
|
||||
assertEquals(List.of(
|
||||
"cavetest: unknown fields, ignored",
|
||||
" Biome \"caves\" 3 files e.g. biomes/carving/deep-ravine.json:157",
|
||||
" Dimension \"caves\" 1 file dimensions/cavetest.json:856"
|
||||
), lines);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typoKeepsItsSuggestion() {
|
||||
UnknownFieldReport.record(SOURCE_PACK, new File(SOURCE_PACK, "biomes/x.json"), "Biome", "carvng", 12, "carving");
|
||||
|
||||
List<String> lines = plain(UnknownFieldReport.drainReportLines());
|
||||
|
||||
assertTrue(lines.get(1).endsWith("biomes/x.json:12 (did you mean \"carving\"?)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void alreadyReportedFieldsStayQuietInLaterBatches() {
|
||||
recordBiomeCaves(SOURCE_PACK, "biomes/carving/volcanic.json", 144);
|
||||
UnknownFieldReport.drainReportLines();
|
||||
|
||||
recordBiomeCaves(EPOCH_PACK, "biomes/carving/volcanic.json", 144);
|
||||
|
||||
assertEquals(List.of(), UnknownFieldReport.drainReportLines());
|
||||
}
|
||||
|
||||
private static void recordBiomeCaves(File pack, String relativePath, int line) {
|
||||
UnknownFieldReport.record(pack, new File(pack, relativePath), "Biome", "caves", line, null);
|
||||
}
|
||||
|
||||
private static List<String> plain(List<String> lines) {
|
||||
return lines.stream().map(line -> line.replaceAll("(?i)\u00a7[0-9a-fk-orx]|<[^>]+>", "")).toList();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user