Suppress model bakery errors if there are too many

This commit is contained in:
embeddedt 2023-04-28 15:53:39 -04:00
parent 10672b0214
commit 469c564c1b
No known key found for this signature in database
GPG Key ID: A69433EC199B5613

View File

@ -11,6 +11,7 @@ import com.google.common.collect.Sets;
import com.google.gson.*; import com.google.gson.*;
import com.mojang.datafixers.util.Pair; import com.mojang.datafixers.util.Pair;
import com.mojang.math.Transformation; import com.mojang.math.Transformation;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.Util; import net.minecraft.Util;
import net.minecraft.client.color.block.BlockColors; import net.minecraft.client.color.block.BlockColors;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
@ -56,6 +57,7 @@ import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -194,12 +196,24 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery {
return ImmutableList.of(); return ImmutableList.of();
} }
private static final int ERROR_THRESHOLD = 200;
private void logOrSuppressError(Object2IntOpenHashMap<String> suppressionMap, String type, ResourceLocation location, Throwable e) {
int numErrors;
synchronized (suppressionMap) {
numErrors = suppressionMap.computeInt(location.getNamespace(), (k, oldVal) -> (oldVal == null ? 1 : oldVal + 1));
}
if(numErrors <= ERROR_THRESHOLD)
ModernFix.LOGGER.error("Error reading {} {}: {}", type, location, e);
}
/** /**
* Load all blockstate JSONs and model files, collect textures. * Load all blockstate JSONs and model files, collect textures.
*/ */
private void gatherModelMaterials(Set<Material> materialSet) { private void gatherModelMaterials(Set<Material> materialSet) {
Stopwatch stopwatch = Stopwatch.createStarted(); Stopwatch stopwatch = Stopwatch.createStarted();
List<CompletableFuture<Pair<ResourceLocation, JsonElement>>> blockStateData = new ArrayList<>(); List<CompletableFuture<Pair<ResourceLocation, JsonElement>>> blockStateData = new ArrayList<>();
final Object2IntOpenHashMap<String> blockstateErrors = new Object2IntOpenHashMap<>();
for(ResourceLocation blockstate : blockStateFiles) { for(ResourceLocation blockstate : blockStateFiles) {
blockStateData.add(CompletableFuture.supplyAsync(() -> { blockStateData.add(CompletableFuture.supplyAsync(() -> {
ResourceLocation fileLocation = new ResourceLocation(blockstate.getNamespace(), "blockstates/" + blockstate.getPath() + ".json"); ResourceLocation fileLocation = new ResourceLocation(blockstate.getNamespace(), "blockstates/" + blockstate.getPath() + ".json");
@ -207,7 +221,7 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery {
JsonParser parser = new JsonParser(); JsonParser parser = new JsonParser();
return Pair.of(blockstate, parser.parse(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))); return Pair.of(blockstate, parser.parse(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)));
} catch(IOException | JsonParseException e) { } catch(IOException | JsonParseException e) {
ModernFix.LOGGER.error("Error reading blockstate {}: {}", blockstate, e); logOrSuppressError(blockstateErrors, "blockstate", blockstate, e);
} }
return Pair.of(blockstate, null); return Pair.of(blockstate, null);
}, ModernFix.resourceReloadExecutor())); }, ModernFix.resourceReloadExecutor()));
@ -256,11 +270,17 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery {
} }
} catch(RuntimeException e) { } catch(RuntimeException e) {
ModernFix.LOGGER.error("Error with blockstate {}: {}", pair.getFirst(), e); logOrSuppressError(blockstateErrors, "blockstate", pair.getFirst(), e);
} }
} }
} }
blockstateErrors.object2IntEntrySet().forEach(entry -> {
if(entry.getIntValue() > ERROR_THRESHOLD) {
ModernFix.LOGGER.error("Suppressed additional {} blockstate errors for domain {}", entry.getIntValue(), entry.getKey());
}
});
blockstateErrors.clear();
blockStateData = null; blockStateData = null;
Map<ResourceLocation, BlockModel> basicModels = new HashMap<>(); Map<ResourceLocation, BlockModel> basicModels = new HashMap<>();
basicModels.put(MISSING_MODEL_LOCATION, (BlockModel)missingModel); basicModels.put(MISSING_MODEL_LOCATION, (BlockModel)missingModel);
@ -278,7 +298,7 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery {
JsonParser parser = new JsonParser(); JsonParser parser = new JsonParser();
return Pair.of(model, parser.parse(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))); return Pair.of(model, parser.parse(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)));
} catch(IOException | JsonParseException e) { } catch(IOException | JsonParseException e) {
ModernFix.LOGGER.error("Error reading model {}: {}", fileLocation, e); logOrSuppressError(blockstateErrors, "model", fileLocation, e);
return Pair.of(fileLocation, null); return Pair.of(fileLocation, null);
} }
}, ModernFix.resourceReloadExecutor())); }, ModernFix.resourceReloadExecutor()));
@ -298,12 +318,17 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery {
continue; continue;
} }
} catch(Throwable e) { } catch(Throwable e) {
ModernFix.LOGGER.warn("Unable to parse {}: {}", pair.getFirst(), e); logOrSuppressError(blockstateErrors, "model", pair.getFirst(), e);
} }
basicModels.put(pair.getFirst(), (BlockModel)missingModel); basicModels.put(pair.getFirst(), (BlockModel)missingModel);
} }
UVController.useDummyUv.set(Boolean.FALSE); UVController.useDummyUv.set(Boolean.FALSE);
} }
blockstateErrors.object2IntEntrySet().forEach(entry -> {
if(entry.getIntValue() > ERROR_THRESHOLD) {
ModernFix.LOGGER.error("Suppressed additional {} model errors for domain {}", entry.getIntValue(), entry.getKey());
}
});
modelFiles = null; modelFiles = null;
Function<ResourceLocation, UnbakedModel> modelGetter = loc -> { Function<ResourceLocation, UnbakedModel> modelGetter = loc -> {
UnbakedModel m = basicModels.get(loc); UnbakedModel m = basicModels.get(loc);