diff --git a/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java b/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java index 9e5f9299..d019b83c 100644 --- a/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java +++ b/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java @@ -48,6 +48,8 @@ public class ModernFixEarlyConfig { this.addMixinRule("feature.direct_stack_trace", false); this.addMixinRule("perf.fast_registry_validation", true); this.addMixinRule("perf.skip_first_datapack_reload", true); + // not stable yet + this.addMixinRule("perf.rewrite_registry", false); this.addMixinRule("perf.remove_biome_temperature_cache", true); this.addMixinRule("perf.reduce_blockstate_cache_rebuilds", true); this.addMixinRule("perf.model_optimizations", true); diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ItemModelShaperMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ItemModelShaperMixin.java index 3d372b1d..e3904026 100644 --- a/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ItemModelShaperMixin.java +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ItemModelShaperMixin.java @@ -9,16 +9,23 @@ import net.minecraft.world.item.Item; import net.minecraftforge.client.model.ForgeItemModelShaper; import net.minecraftforge.registries.ForgeRegistries; import org.embeddedt.modernfix.dynamicresources.ModelLocationCache; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Overwrite; -import org.spongepowered.asm.mixin.Shadow; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.*; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import java.util.Collection; +import java.util.HashMap; import java.util.Map; +import java.util.Set; @Mixin(ForgeItemModelShaper.class) public abstract class ItemModelShaperMixin extends ItemModelShaper { - @Shadow @Final private Map, ModelResourceLocation> locations; + @Shadow @Final @Mutable private Map, ModelResourceLocation> locations; + + private Map, ModelResourceLocation> overrideLocations; public ItemModelShaperMixin(ModelManager arg) { super(arg); @@ -26,6 +33,86 @@ public abstract class ItemModelShaperMixin extends ItemModelShaper { private static final ModelResourceLocation SENTINEL = new ModelResourceLocation("modernfix", "sentinel"); + @Inject(method = "", at = @At("RETURN")) + private void replaceLocationMap(CallbackInfo ci) { + overrideLocations = new HashMap<>(); + // need to replace this map because mods query locations through it + locations = new Map, ModelResourceLocation>() { + @Override + public int size() { + return ForgeRegistries.ITEMS.getValues().size(); + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public boolean containsKey(Object key) { + return true; + } + + @Override + public boolean containsValue(Object value) { + return false; + } + + @Override + public ModelResourceLocation get(Object key) { + return getLocation(((Holder.Reference)key).get()); + } + + @Nullable + @Override + public ModelResourceLocation put(Holder.Reference key, ModelResourceLocation value) { + throw new UnsupportedOperationException(); + } + + @Override + public ModelResourceLocation remove(Object key) { + throw new UnsupportedOperationException(); + } + + @Override + public void putAll(@NotNull Map, ? extends ModelResourceLocation> m) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } + + @NotNull + @Override + public Set> keySet() { + throw new UnsupportedOperationException(); + } + + @NotNull + @Override + public Collection values() { + throw new UnsupportedOperationException(); + } + + @NotNull + @Override + public Set, ModelResourceLocation>> entrySet() { + throw new UnsupportedOperationException(); + } + }; + } + + private ModelResourceLocation getLocation(Item item) { + ModelResourceLocation map = overrideLocations.getOrDefault(ForgeRegistries.ITEMS.getDelegateOrThrow(item), SENTINEL); + if(map == SENTINEL) { + /* generate the appropriate location from our cache */ + map = ModelLocationCache.get(item); + } + return map; + } + /** * @reason Get the stored location for that item and meta, and get the model * from that location from the model manager. @@ -33,11 +120,7 @@ public abstract class ItemModelShaperMixin extends ItemModelShaper { @Overwrite @Override public BakedModel getItemModel(Item item) { - ModelResourceLocation map = locations.getOrDefault(ForgeRegistries.ITEMS.getDelegateOrThrow(item), SENTINEL); - if(map == SENTINEL) { - /* generate the appropriate location from our cache */ - map = ModelLocationCache.get(item); - } + ModelResourceLocation map = getLocation(item); return map == null ? null : getModelManager().getModel(map); } @@ -48,7 +131,7 @@ public abstract class ItemModelShaperMixin extends ItemModelShaper { @Overwrite @Override public void register(Item item, ModelResourceLocation location) { - locations.put(ForgeRegistries.ITEMS.getDelegateOrThrow(item), location); + overrideLocations.put(ForgeRegistries.ITEMS.getDelegateOrThrow(item), location); } /** diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ModelBakeryMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ModelBakeryMixin.java index 50f8c9c1..6c6f5c34 100644 --- a/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ModelBakeryMixin.java +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ModelBakeryMixin.java @@ -517,8 +517,10 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery { } @Redirect(method = "loadModel", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/state/StateDefinition;getPossibleStates()Lcom/google/common/collect/ImmutableList;")) private ImmutableList loadOnlyRelevantBlockState(StateDefinition stateDefinition, ResourceLocation location) { - Set> fixedProperties = new HashSet<>(); ModelResourceLocation mrl = (ModelResourceLocation)location; + if(Objects.equals(mrl.getVariant(), "inventory")) + return ImmutableList.of(); + Set> fixedProperties = new HashSet<>(); BlockState fixedState = stateDefinition.any(); for(String s : COMMA_SPLITTER.split(mrl.getVariant())) { Iterator iterator = EQUAL_SPLITTER.split(s).iterator(); @@ -576,6 +578,8 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery { LOGGER.info("Baking {}", arg); UnbakedModel iunbakedmodel = this.getModel(arg); iunbakedmodel.getMaterials(this::getModel, new HashSet<>()); + if(iunbakedmodel == missingModel && debugDynamicModelLoading) + LOGGER.warn("Model {} not present", arg); BakedModel ibakedmodel = null; if (iunbakedmodel instanceof BlockModel) { BlockModel blockmodel = (BlockModel)iunbakedmodel; diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/fast_registry_validation/ForgeRegistryMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/fast_registry_validation/ForgeRegistryMixin.java index 90df21e0..81398380 100644 --- a/src/main/java/org/embeddedt/modernfix/mixin/perf/fast_registry_validation/ForgeRegistryMixin.java +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/fast_registry_validation/ForgeRegistryMixin.java @@ -1,18 +1,10 @@ package org.embeddedt.modernfix.mixin.perf.fast_registry_validation; import net.minecraftforge.fml.util.ObfuscationReflectionHelper; -import com.google.common.collect.BiMap; -import net.minecraft.core.Registry; -import net.minecraft.resources.ResourceKey; -import net.minecraft.resources.ResourceLocation; import net.minecraftforge.registries.ForgeRegistry; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; -import org.embeddedt.modernfix.registry.FastForgeRegistry; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Mutable; -import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; @@ -66,44 +58,4 @@ public class ForgeRegistryMixin { private void skipTrace(Logger logger, Marker marker, String s, Object o, Object o1, Object o2, Object o3, Object o4) { } - - @Shadow @Final @Mutable private BiMap ids; - - @Shadow @Final @Mutable private BiMap, V> keys; - - @Shadow @Final private ResourceKey> key; - - @Shadow @Final @Mutable private BiMap names; - - @Shadow @Final @Mutable private BiMap owners; - - private FastForgeRegistry fastRegistry; - - /** - * The following code replaces the Forge HashBiMaps with a more efficient data structure based around - * an array list for IDs and one HashMap going from value -> information. - */ - @Inject(method = "", at = @At("RETURN")) - private void replaceBackingMaps(CallbackInfo ci) { - this.fastRegistry = new FastForgeRegistry<>(this.key); - this.ids = fastRegistry.getIds(); - this.keys = fastRegistry.getKeys(); - this.names = fastRegistry.getNames(); - this.owners = fastRegistry.getOwners(); - } - - @Inject(method = "freeze", at = @At("RETURN")) - private void optimizeRegistry(CallbackInfo ci) { - this.fastRegistry.optimize(); - } - - @Redirect(method = "sync", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/BiMap;clear()V")) - private void clearBiMap(BiMap map) { - if(map == this.owners) { - this.fastRegistry.clear(); - } else if(map == this.keys || map == this.names || map == this.ids) { - // do nothing, the registry is faster at clearing everything at once - } else - map.clear(); - } } diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/rewrite_registry/ForgeRegistryMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/rewrite_registry/ForgeRegistryMixin.java new file mode 100644 index 00000000..a47b7c5f --- /dev/null +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/rewrite_registry/ForgeRegistryMixin.java @@ -0,0 +1,62 @@ +package org.embeddedt.modernfix.mixin.perf.rewrite_registry; + +import com.google.common.collect.BiMap; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; +import net.minecraftforge.registries.ForgeRegistry; +import org.embeddedt.modernfix.registry.FastForgeRegistry; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(value = ForgeRegistry.class, remap = false) +public class ForgeRegistryMixin { + @Shadow + @Final + @Mutable + private BiMap ids; + + @Shadow @Final @Mutable private BiMap, V> keys; + + @Shadow @Final private ResourceKey> key; + + @Shadow @Final @Mutable private BiMap names; + + @Shadow @Final @Mutable private BiMap owners; + + private FastForgeRegistry fastRegistry; + + /** + * The following code replaces the Forge HashBiMaps with a more efficient data structure based around + * an array list for IDs and one HashMap going from value -> information. + */ + @Inject(method = "", at = @At("RETURN")) + private void replaceBackingMaps(CallbackInfo ci) { + this.fastRegistry = new FastForgeRegistry<>(this.key); + this.ids = fastRegistry.getIds(); + this.keys = fastRegistry.getKeys(); + this.names = fastRegistry.getNames(); + this.owners = fastRegistry.getOwners(); + } + + @Inject(method = "freeze", at = @At("RETURN")) + private void optimizeRegistry(CallbackInfo ci) { + this.fastRegistry.optimize(); + } + + @Redirect(method = "sync", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/BiMap;clear()V")) + private void clearBiMap(BiMap map) { + if(map == this.owners) { + this.fastRegistry.clear(); + } else if(map == this.keys || map == this.names || map == this.ids) { + // do nothing, the registry is faster at clearing everything at once + } else + map.clear(); + } +} diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/fast_registry_validation/ForgeRegistrySnapshotMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/rewrite_registry/ForgeRegistrySnapshotMixin.java similarity index 94% rename from src/main/java/org/embeddedt/modernfix/mixin/perf/fast_registry_validation/ForgeRegistrySnapshotMixin.java rename to src/main/java/org/embeddedt/modernfix/mixin/perf/rewrite_registry/ForgeRegistrySnapshotMixin.java index ad941ab0..87fb4de7 100644 --- a/src/main/java/org/embeddedt/modernfix/mixin/perf/fast_registry_validation/ForgeRegistrySnapshotMixin.java +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/rewrite_registry/ForgeRegistrySnapshotMixin.java @@ -1,4 +1,4 @@ -package org.embeddedt.modernfix.mixin.perf.fast_registry_validation; +package org.embeddedt.modernfix.mixin.perf.rewrite_registry; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; diff --git a/src/main/resources/modernfix.mixins.json b/src/main/resources/modernfix.mixins.json index 73c384e9..3bef7e06 100644 --- a/src/main/resources/modernfix.mixins.json +++ b/src/main/resources/modernfix.mixins.json @@ -39,7 +39,8 @@ "perf.tag_id_caching.TagEntryMixin", "perf.tag_id_caching.TagOrElementLocationMixin", "perf.fast_registry_validation.ForgeRegistryMixin", - "perf.fast_registry_validation.ForgeRegistrySnapshotMixin", + "perf.rewrite_registry.ForgeRegistryMixin", + "perf.rewrite_registry.ForgeRegistrySnapshotMixin", "perf.fast_registry_validation.ResourceKeyMixin", "perf.cache_strongholds.ChunkGeneratorMixin", "perf.cache_upgraded_structures.StructureManagerMixin",