From 11f313f6e7604841a57c8831d90210911e4b3b1c Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Sat, 9 Dec 2023 13:50:04 -0500 Subject: [PATCH] Avoid capturedLocation being replaced unexpectedly --- .../ModelBakerImplMixin.java | 25 ++++++++++++++++--- .../ModelBakerImplMixin.java | 25 ++++++++++++++++--- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/fabric/src/main/java/org/embeddedt/modernfix/fabric/mixin/perf/dynamic_resources/ModelBakerImplMixin.java b/fabric/src/main/java/org/embeddedt/modernfix/fabric/mixin/perf/dynamic_resources/ModelBakerImplMixin.java index 54eadc7e..03995c6f 100644 --- a/fabric/src/main/java/org/embeddedt/modernfix/fabric/mixin/perf/dynamic_resources/ModelBakerImplMixin.java +++ b/fabric/src/main/java/org/embeddedt/modernfix/fabric/mixin/perf/dynamic_resources/ModelBakerImplMixin.java @@ -71,7 +71,6 @@ public abstract class ModelBakerImplMixin implements IExtendedModelBaker { @Inject(method = "getModel", at = @At("HEAD"), cancellable = true) private void obtainModel(ResourceLocation arg, CallbackInfoReturnable cir) { - capturedLocation = arg; if(debugDynamicModelLoading) ModernFix.LOGGER.info("Baking {}", arg); IExtendedModelBakery extendedBakery = (IExtendedModelBakery)this.field_40571; @@ -110,7 +109,10 @@ public abstract class ModelBakerImplMixin implements IExtendedModelBaker { } cir.setReturnValue(toReplace); cir.getReturnValue().resolveParents(this.field_40571::getModel); - capturedModel = cir.getReturnValue(); + if(capturedLocation == null) { + capturedLocation = arg; + capturedModel = cir.getReturnValue(); + } if(cir.getReturnValue() == extendedBakery.mfix$getUnbakedMissingModel()) { if(arg != ModelBakery.MISSING_MODEL_LOCATION) { if(debugDynamicModelLoading) @@ -123,9 +125,24 @@ public abstract class ModelBakerImplMixin implements IExtendedModelBaker { @ModifyVariable(method = "bake", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/client/resources/model/UnbakedModel;bake(Lnet/minecraft/client/resources/model/ModelBaker;Ljava/util/function/Function;Lnet/minecraft/client/resources/model/ModelState;Lnet/minecraft/resources/ResourceLocation;)Lnet/minecraft/client/resources/model/BakedModel;")) private BakedModel unifyMissingBakedModel(BakedModel model) { - for(ModernFixClientIntegration integration : ModernFixClient.CLIENT_INTEGRATIONS) { - model = integration.onBakedModelLoad(capturedLocation, capturedModel, model, capturedState, this.field_40571); + // Save these variables in case the nested model calls getModel somehow + ResourceLocation location = this.capturedLocation; + UnbakedModel unbakedModel = this.capturedModel; + ModelState state = this.capturedState; + + // Safety check + if(location == null) { + return model; } + + for(ModernFixClientIntegration integration : ModernFixClient.CLIENT_INTEGRATIONS) { + model = integration.onBakedModelLoad(location, unbakedModel, model, state, this.field_40571); + } + + // Allow more capturing + this.capturedLocation = null; + this.capturedModel = null; + return model; } diff --git a/forge/src/main/java/org/embeddedt/modernfix/forge/mixin/perf/dynamic_resources/ModelBakerImplMixin.java b/forge/src/main/java/org/embeddedt/modernfix/forge/mixin/perf/dynamic_resources/ModelBakerImplMixin.java index 7b68311a..c3527eb5 100644 --- a/forge/src/main/java/org/embeddedt/modernfix/forge/mixin/perf/dynamic_resources/ModelBakerImplMixin.java +++ b/forge/src/main/java/org/embeddedt/modernfix/forge/mixin/perf/dynamic_resources/ModelBakerImplMixin.java @@ -52,7 +52,6 @@ public abstract class ModelBakerImplMixin implements IModelBakerImpl, IExtendedM @Inject(method = "getModel", at = @At("HEAD"), cancellable = true) private void obtainModel(ResourceLocation arg, CallbackInfoReturnable cir) { - capturedLocation = arg; if(debugDynamicModelLoading) ModernFix.LOGGER.info("Baking {}", arg); IExtendedModelBakery extendedBakery = (IExtendedModelBakery)this.field_40571; @@ -78,7 +77,10 @@ public abstract class ModelBakerImplMixin implements IModelBakerImpl, IExtendedM } cir.setReturnValue(toReplace); cir.getReturnValue().resolveParents(this.field_40571::getModel); - capturedModel = cir.getReturnValue(); + if(capturedLocation == null) { + capturedLocation = arg; + capturedModel = cir.getReturnValue(); + } if(cir.getReturnValue() == extendedBakery.mfix$getUnbakedMissingModel()) { if(arg != ModelBakery.MISSING_MODEL_LOCATION) { if(debugDynamicModelLoading) @@ -96,9 +98,24 @@ public abstract class ModelBakerImplMixin implements IModelBakerImpl, IExtendedM @ModifyExpressionValue(method = "bake(Lnet/minecraft/resources/ResourceLocation;Lnet/minecraft/client/resources/model/ModelState;Ljava/util/function/Function;)Lnet/minecraft/client/resources/model/BakedModel;", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/resources/model/UnbakedModel;bake(Lnet/minecraft/client/resources/model/ModelBaker;Ljava/util/function/Function;Lnet/minecraft/client/resources/model/ModelState;Lnet/minecraft/resources/ResourceLocation;)Lnet/minecraft/client/resources/model/BakedModel;")) private BakedModel unifyMissingBakedModel(BakedModel model) { - for(ModernFixClientIntegration integration : ModernFixClient.CLIENT_INTEGRATIONS) { - model = integration.onBakedModelLoad(capturedLocation, capturedModel, model, capturedState, this.field_40571); + // Save these variables in case the nested model calls getModel somehow + ResourceLocation location = this.capturedLocation; + UnbakedModel unbakedModel = this.capturedModel; + ModelState state = this.capturedState; + + // Safety check + if(location == null) { + return model; } + + for(ModernFixClientIntegration integration : ModernFixClient.CLIENT_INTEGRATIONS) { + model = integration.onBakedModelLoad(location, unbakedModel, model, state, this.field_40571); + } + + // Allow more capturing + this.capturedLocation = null; + this.capturedModel = null; + return model; } }