Skip to content

Commit 11f313f

Browse files
committed
Avoid capturedLocation being replaced unexpectedly
1 parent 46127b5 commit 11f313f

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

fabric/src/main/java/org/embeddedt/modernfix/fabric/mixin/perf/dynamic_resources/ModelBakerImplMixin.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ private void captureState(ResourceLocation rl, ModelState state, CallbackInfoRet
7171

7272
@Inject(method = "getModel", at = @At("HEAD"), cancellable = true)
7373
private void obtainModel(ResourceLocation arg, CallbackInfoReturnable<UnbakedModel> cir) {
74-
capturedLocation = arg;
7574
if(debugDynamicModelLoading)
7675
ModernFix.LOGGER.info("Baking {}", arg);
7776
IExtendedModelBakery extendedBakery = (IExtendedModelBakery)this.field_40571;
@@ -110,7 +109,10 @@ private void obtainModel(ResourceLocation arg, CallbackInfoReturnable<UnbakedMod
110109
}
111110
cir.setReturnValue(toReplace);
112111
cir.getReturnValue().resolveParents(this.field_40571::getModel);
113-
capturedModel = cir.getReturnValue();
112+
if(capturedLocation == null) {
113+
capturedLocation = arg;
114+
capturedModel = cir.getReturnValue();
115+
}
114116
if(cir.getReturnValue() == extendedBakery.mfix$getUnbakedMissingModel()) {
115117
if(arg != ModelBakery.MISSING_MODEL_LOCATION) {
116118
if(debugDynamicModelLoading)
@@ -123,9 +125,24 @@ private void obtainModel(ResourceLocation arg, CallbackInfoReturnable<UnbakedMod
123125

124126
@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;"))
125127
private BakedModel unifyMissingBakedModel(BakedModel model) {
128+
// Save these variables in case the nested model calls getModel somehow
129+
ResourceLocation location = this.capturedLocation;
130+
UnbakedModel unbakedModel = this.capturedModel;
131+
ModelState state = this.capturedState;
132+
133+
// Safety check
134+
if(location == null) {
135+
return model;
136+
}
137+
126138
for(ModernFixClientIntegration integration : ModernFixClient.CLIENT_INTEGRATIONS) {
127-
model = integration.onBakedModelLoad(capturedLocation, capturedModel, model, capturedState, this.field_40571);
139+
model = integration.onBakedModelLoad(location, unbakedModel, model, state, this.field_40571);
128140
}
141+
142+
// Allow more capturing
143+
this.capturedLocation = null;
144+
this.capturedModel = null;
145+
129146
return model;
130147
}
131148

forge/src/main/java/org/embeddedt/modernfix/forge/mixin/perf/dynamic_resources/ModelBakerImplMixin.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ private void captureState(ResourceLocation arg, ModelState state, Function<Mater
5252

5353
@Inject(method = "getModel", at = @At("HEAD"), cancellable = true)
5454
private void obtainModel(ResourceLocation arg, CallbackInfoReturnable<UnbakedModel> cir) {
55-
capturedLocation = arg;
5655
if(debugDynamicModelLoading)
5756
ModernFix.LOGGER.info("Baking {}", arg);
5857
IExtendedModelBakery extendedBakery = (IExtendedModelBakery)this.field_40571;
@@ -78,7 +77,10 @@ private void obtainModel(ResourceLocation arg, CallbackInfoReturnable<UnbakedMod
7877
}
7978
cir.setReturnValue(toReplace);
8079
cir.getReturnValue().resolveParents(this.field_40571::getModel);
81-
capturedModel = cir.getReturnValue();
80+
if(capturedLocation == null) {
81+
capturedLocation = arg;
82+
capturedModel = cir.getReturnValue();
83+
}
8284
if(cir.getReturnValue() == extendedBakery.mfix$getUnbakedMissingModel()) {
8385
if(arg != ModelBakery.MISSING_MODEL_LOCATION) {
8486
if(debugDynamicModelLoading)
@@ -96,9 +98,24 @@ private Object ignoreCacheIfRequested(Object o) {
9698

9799
@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;"))
98100
private BakedModel unifyMissingBakedModel(BakedModel model) {
101+
// Save these variables in case the nested model calls getModel somehow
102+
ResourceLocation location = this.capturedLocation;
103+
UnbakedModel unbakedModel = this.capturedModel;
104+
ModelState state = this.capturedState;
105+
106+
// Safety check
107+
if(location == null) {
108+
return model;
109+
}
110+
99111
for(ModernFixClientIntegration integration : ModernFixClient.CLIENT_INTEGRATIONS) {
100-
model = integration.onBakedModelLoad(capturedLocation, capturedModel, model, capturedState, this.field_40571);
112+
model = integration.onBakedModelLoad(location, unbakedModel, model, state, this.field_40571);
101113
}
114+
115+
// Allow more capturing
116+
this.capturedLocation = null;
117+
this.capturedModel = null;
118+
102119
return model;
103120
}
104121
}

0 commit comments

Comments
 (0)