| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.common.collect.HashMultimap;
|
| 4 | import com.google.common.collect.Multimap;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.LinkedHashMap;
|
| 7 | import java.util.LinkedHashSet;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.Set;
|
| 11 | import java.util.function.BiConsumer;
|
| 12 | import java.util.stream.Collectors;
|
| 13 | import net.minecraft.resources.ResourceKey;
|
| 14 | import net.minecraft.tags.TagKey;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 | import org.slf4j.Logger;
|
| 17 |
|
| 18 | public interface ProblemReporter {
|
| 19 | ProblemReporter DISCARDING = new ProblemReporter() {
|
| 20 | @Override
|
| 21 | public ProblemReporter forChild(final ProblemReporter.PathElement path) {
|
| 22 | return this;
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | public void report(final ProblemReporter.Problem problem) {
|
| 27 | }
|
| 28 | };
|
| 29 |
|
| 30 | ProblemReporter forChild(ProblemReporter.PathElement path);
|
| 31 |
|
| 32 | void report(ProblemReporter.Problem problem);
|
| 33 |
|
| 34 | record CollectionReferencePathElement(TagKey<?> id) implements ProblemReporter.PathElement {
|
| 35 | @Override
|
| 36 | public String get() {
|
| 37 | return "->{#" + this.id.location() + "@" + this.id.registry() + "}";
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | class Collector implements ProblemReporter {
|
| 42 | public static final ProblemReporter.PathElement EMPTY_ROOT = () -> "";
|
| 43 | private final ProblemReporter.@Nullable Collector parent;
|
| 44 | private final ProblemReporter.PathElement element;
|
| 45 | private final Set<ProblemReporter.Collector.Entry> problems;
|
| 46 |
|
| 47 | public Collector() {
|
| 48 | this(EMPTY_ROOT);
|
| 49 | }
|
| 50 |
|
| 51 | public Collector(final ProblemReporter.PathElement root) {
|
| 52 | this.parent = null;
|
| 53 | this.problems = new LinkedHashSet<>();
|
| 54 | this.element = root;
|
| 55 | }
|
| 56 |
|
| 57 | private Collector(final ProblemReporter.Collector parent, final ProblemReporter.PathElement path) {
|
| 58 | this.problems = parent.problems;
|
| 59 | this.parent = parent;
|
| 60 | this.element = path;
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public ProblemReporter forChild(final ProblemReporter.PathElement path) {
|
| 65 | return new ProblemReporter.Collector(this, path);
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public void report(final ProblemReporter.Problem problem) {
|
| 70 | this.problems.add(new ProblemReporter.Collector.Entry(this, problem));
|
| 71 | }
|
| 72 |
|
| 73 | public boolean isEmpty() {
|
| 74 | return this.problems.isEmpty();
|
| 75 | }
|
| 76 |
|
| 77 | public boolean hasFatalProblems() {
|
| 78 | for (ProblemReporter.Collector.Entry entry : this.problems) {
|
| 79 | if (entry.problem.isFatal()) {
|
| 80 | return true;
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | return false;
|
| 85 | }
|
| 86 |
|
| 87 | public void forEach(final BiConsumer<String, ProblemReporter.Problem> output) {
|
| 88 | List<ProblemReporter.PathElement> pathElements = new ArrayList<>();
|
| 89 | StringBuilder pathString = new StringBuilder();
|
| 90 |
|
| 91 | for (ProblemReporter.Collector.Entry entry : this.problems) {
|
| 92 | for (ProblemReporter.Collector current = entry.source; current != null; current = current.parent) {
|
| 93 | pathElements.add(current.element);
|
| 94 | }
|
| 95 |
|
| 96 | for (int i = pathElements.size() - 1; i >= 0; i--) {
|
| 97 | pathString.append(pathElements.get(i).get());
|
| 98 | }
|
| 99 |
|
| 100 | output.accept(pathString.toString(), entry.problem());
|
| 101 | pathString.setLength(0);
|
| 102 | pathElements.clear();
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | public String getReport() {
|
| 107 | Multimap<String, ProblemReporter.Problem> groupedProblems = HashMultimap.create();
|
| 108 | this.forEach(groupedProblems::put);
|
| 109 | return groupedProblems.asMap()
|
| 110 | .entrySet()
|
| 111 | .stream()
|
| 112 | .map(entry -> " at " + entry.getKey() + ": " + entry.getValue().stream().map(ProblemReporter.Problem::description).collect(Collectors.joining("; ")))
|
| 113 | .collect(Collectors.joining("\n"));
|
| 114 | }
|
| 115 |
|
| 116 | public String getTreeReport() {
|
| 117 | List<ProblemReporter.PathElement> pathElements = new ArrayList<>();
|
| 118 | ProblemReporter.Collector.ProblemTreeNode root = new ProblemReporter.Collector.ProblemTreeNode(this.element);
|
| 119 |
|
| 120 | for (ProblemReporter.Collector.Entry entry : this.problems) {
|
| 121 | for (ProblemReporter.Collector current = entry.source; current != this; current = current.parent) {
|
| 122 | pathElements.add(current.element);
|
| 123 | }
|
| 124 |
|
| 125 | ProblemReporter.Collector.ProblemTreeNode node = root;
|
| 126 |
|
| 127 | for (int i = pathElements.size() - 1; i >= 0; i--) {
|
| 128 | node = node.child(pathElements.get(i));
|
| 129 | }
|
| 130 |
|
| 131 | pathElements.clear();
|
| 132 | node.problems.add(entry.problem);
|
| 133 | }
|
| 134 |
|
| 135 | return String.join("\n", root.getLines());
|
| 136 | }
|
| 137 |
|
| 138 | private record Entry(ProblemReporter.Collector source, ProblemReporter.Problem problem) {
|
| 139 | }
|
| 140 |
|
| 141 | private record ProblemTreeNode(
|
| 142 | ProblemReporter.PathElement element,
|
| 143 | List<ProblemReporter.Problem> problems,
|
| 144 | Map<ProblemReporter.PathElement, ProblemReporter.Collector.ProblemTreeNode> children
|
| 145 | ) {
|
| 146 | public ProblemTreeNode(final ProblemReporter.PathElement pathElement) {
|
| 147 | this(pathElement, new ArrayList<>(), new LinkedHashMap<>());
|
| 148 | }
|
| 149 |
|
| 150 | public ProblemReporter.Collector.ProblemTreeNode child(final ProblemReporter.PathElement id) {
|
| 151 | return this.children.computeIfAbsent(id, ProblemReporter.Collector.ProblemTreeNode::new);
|
| 152 | }
|
| 153 |
|
| 154 | public List<String> getLines() {
|
| 155 | int problemCount = this.problems.size();
|
| 156 | int childrenCount = this.children.size();
|
| 157 | if (problemCount == 0 && childrenCount == 0) {
|
| 158 | return List.of();
|
| 159 | }
|
| 160 |
|
| 161 | if (problemCount == 0 && childrenCount == 1) {
|
| 162 | List<String> lines = new ArrayList<>();
|
| 163 | this.children.forEach((element, child) -> lines.addAll(child.getLines()));
|
| 164 | lines.set(0, this.element.get() + lines.get(0));
|
| 165 | return lines;
|
| 166 | }
|
| 167 |
|
| 168 | if (problemCount == 1 && childrenCount == 0) {
|
| 169 | return List.of(this.element.get() + ": " + this.problems.getFirst().description());
|
| 170 | }
|
| 171 |
|
| 172 | List<String> lines = new ArrayList<>();
|
| 173 | this.children.forEach((element, child) -> lines.addAll(child.getLines()));
|
| 174 | lines.replaceAll(s -> " " + s);
|
| 175 |
|
| 176 | for (ProblemReporter.Problem problem : this.problems) {
|
| 177 | lines.add(" " + problem.description());
|
| 178 | }
|
| 179 |
|
| 180 | lines.addFirst(this.element.get() + ":");
|
| 181 | return lines;
|
| 182 | }
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | record ElementReferencePathElement(ResourceKey<?> id) implements ProblemReporter.PathElement {
|
| 187 | @Override
|
| 188 | public String get() {
|
| 189 | return "->{" + this.id.identifier() + "@" + this.id.registry() + "}";
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | record FieldPathElement(String name) implements ProblemReporter.PathElement {
|
| 194 | @Override
|
| 195 | public String get() {
|
| 196 | return "." + this.name;
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | record IndexedFieldPathElement(String name, int index) implements ProblemReporter.PathElement {
|
| 201 | @Override
|
| 202 | public String get() {
|
| 203 | return "." + this.name + "[" + this.index + "]";
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | record IndexedPathElement(int index) implements ProblemReporter.PathElement {
|
| 208 | @Override
|
| 209 | public String get() {
|
| 210 | return "[" + this.index + "]";
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | record MapEntryPathElement(String name, String key) implements ProblemReporter.PathElement {
|
| 215 | @Override
|
| 216 | public String get() {
|
| 217 | return "." + this.name + "[" + this.key + "]";
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | @FunctionalInterface
|
| 222 | interface PathElement {
|
| 223 | String get();
|
| 224 | }
|
| 225 |
|
| 226 | interface Problem {
|
| 227 | String description();
|
| 228 |
|
| 229 | default boolean isFatal() {
|
| 230 | return false;
|
| 231 | }
|
| 232 | }
|
| 233 |
|
| 234 | record RootElementPathElement(ResourceKey<?> id) implements ProblemReporter.PathElement {
|
| 235 | @Override
|
| 236 | public String get() {
|
| 237 | return "{" + this.id.identifier() + "@" + this.id.registry() + "}";
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | record RootFieldPathElement(String name) implements ProblemReporter.PathElement {
|
| 242 | @Override
|
| 243 | public String get() {
|
| 244 | return this.name;
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | class ScopedCollector extends ProblemReporter.Collector implements AutoCloseable {
|
| 249 | private final Logger logger;
|
| 250 |
|
| 251 | public ScopedCollector(final Logger logger) {
|
| 252 | this.logger = logger;
|
| 253 | }
|
| 254 |
|
| 255 | public ScopedCollector(final ProblemReporter.PathElement root, final Logger logger) {
|
| 256 | super(root);
|
| 257 | this.logger = logger;
|
| 258 | }
|
| 259 |
|
| 260 | @Override
|
| 261 | public void close() {
|
| 262 | if (!this.isEmpty()) {
|
| 263 | this.logger.warn("[{}] Serialization errors:\n{}", this.logger.getName(), this.getTreeReport());
|
| 264 | }
|
| 265 | }
|
| 266 | }
|
| 267 | }
|
| 268 |
|