Skip to content

Commit 9464f07

Browse files
committed
Replace hacky capturing with MixinExtras
1 parent 28ac5ea commit 9464f07

File tree

2 files changed

+10
-59
lines changed

2 files changed

+10
-59
lines changed

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

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.embeddedt.modernfix.fabric.mixin.perf.dynamic_resources;
22

3+
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
4+
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
35
import net.fabricmc.loader.api.FabricLoader;
46
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
57
import net.minecraft.client.resources.model.*;
@@ -18,7 +20,6 @@
1820
import org.spongepowered.asm.mixin.Shadow;
1921
import org.spongepowered.asm.mixin.injection.At;
2022
import org.spongepowered.asm.mixin.injection.Inject;
21-
import org.spongepowered.asm.mixin.injection.ModifyVariable;
2223
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2324

2425
import java.lang.invoke.MethodHandle;
@@ -53,22 +54,13 @@ public abstract class ModelBakerImplMixin implements IExtendedModelBaker {
5354
}
5455
}
5556

56-
private ResourceLocation capturedLocation;
57-
private UnbakedModel capturedModel;
58-
private ModelState capturedState;
59-
6057
private boolean throwIfMissing;
6158

6259
@Override
6360
public void throwOnMissingModel() {
6461
throwIfMissing = true;
6562
}
6663

67-
@Inject(method = "bake", at = @At("HEAD"))
68-
private void captureState(ResourceLocation rl, ModelState state, CallbackInfoReturnable<BakedModel> cir) {
69-
capturedState = state;
70-
}
71-
7264
@Inject(method = "getModel", at = @At("HEAD"), cancellable = true)
7365
private void obtainModel(ResourceLocation arg, CallbackInfoReturnable<UnbakedModel> cir) {
7466
if(debugDynamicModelLoading)
@@ -109,10 +101,6 @@ private void obtainModel(ResourceLocation arg, CallbackInfoReturnable<UnbakedMod
109101
}
110102
cir.setReturnValue(toReplace);
111103
cir.getReturnValue().resolveParents(this.field_40571::getModel);
112-
if(capturedLocation == null) {
113-
capturedLocation = arg;
114-
capturedModel = cir.getReturnValue();
115-
}
116104
if(cir.getReturnValue() == extendedBakery.mfix$getUnbakedMissingModel()) {
117105
if(arg != ModelBakery.MISSING_MODEL_LOCATION) {
118106
if(debugDynamicModelLoading)
@@ -123,31 +111,17 @@ private void obtainModel(ResourceLocation arg, CallbackInfoReturnable<UnbakedMod
123111
}
124112
}
125113

126-
@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;"))
127-
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-
}
114+
@WrapOperation(method = "bake", 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;"))
115+
private BakedModel callBakedModelIntegration(UnbakedModel unbakedModel, ModelBaker baker, Function<Material, TextureAtlasSprite> spriteGetter, ModelState state, ResourceLocation location, Operation<BakedModel> operation) {
116+
BakedModel model = operation.call(unbakedModel, baker, spriteGetter, state, location);
137117

138118
for(ModernFixClientIntegration integration : ModernFixClient.CLIENT_INTEGRATIONS) {
139119
model = integration.onBakedModelLoad(location, unbakedModel, model, state, this.field_40571);
140120
}
141121

142-
// Allow more capturing
143-
this.capturedLocation = null;
144-
this.capturedModel = null;
145-
146122
return model;
147123
}
148124

149-
150-
151125
/**
152126
* @author embeddedt
153127
* @reason emulate old function, to allow injectors to work

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

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.embeddedt.modernfix.forge.mixin.perf.dynamic_resources;
22

33
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
4+
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
5+
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
46
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
57
import net.minecraft.client.resources.model.*;
68
import net.minecraft.resources.ResourceLocation;
@@ -34,22 +36,13 @@ public abstract class ModelBakerImplMixin implements IModelBakerImpl, IExtendedM
3436
mfix$ignoreCache = true;
3537
}
3638

37-
private ResourceLocation capturedLocation;
38-
private UnbakedModel capturedModel;
39-
private ModelState capturedState;
40-
4139
private boolean throwIfMissing;
4240

4341
@Override
4442
public void throwOnMissingModel() {
4543
throwIfMissing = true;
4644
}
4745

48-
@Inject(method = "bake(Lnet/minecraft/resources/ResourceLocation;Lnet/minecraft/client/resources/model/ModelState;Ljava/util/function/Function;)Lnet/minecraft/client/resources/model/BakedModel;", at = @At("HEAD"), remap = false)
49-
private void captureState(ResourceLocation arg, ModelState state, Function<Material, TextureAtlasSprite> sprites, CallbackInfoReturnable<BakedModel> cir) {
50-
capturedState = state;
51-
}
52-
5346
@Inject(method = "getModel", at = @At("HEAD"), cancellable = true)
5447
private void obtainModel(ResourceLocation arg, CallbackInfoReturnable<UnbakedModel> cir) {
5548
if(debugDynamicModelLoading)
@@ -77,10 +70,6 @@ private void obtainModel(ResourceLocation arg, CallbackInfoReturnable<UnbakedMod
7770
}
7871
cir.setReturnValue(toReplace);
7972
cir.getReturnValue().resolveParents(this.field_40571::getModel);
80-
if(capturedLocation == null) {
81-
capturedLocation = arg;
82-
capturedModel = cir.getReturnValue();
83-
}
8473
if(cir.getReturnValue() == extendedBakery.mfix$getUnbakedMissingModel()) {
8574
if(arg != ModelBakery.MISSING_MODEL_LOCATION) {
8675
if(debugDynamicModelLoading)
@@ -96,26 +85,14 @@ private Object ignoreCacheIfRequested(Object o) {
9685
return mfix$ignoreCache ? null : o;
9786
}
9887

99-
@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;"))
100-
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-
}
88+
@WrapOperation(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;"))
89+
private BakedModel callBakedModelIntegration(UnbakedModel unbakedModel, ModelBaker baker, Function<Material, TextureAtlasSprite> spriteGetter, ModelState state, ResourceLocation location, Operation<BakedModel> operation) {
90+
BakedModel model = operation.call(unbakedModel, baker, spriteGetter, state, location);
11091

11192
for(ModernFixClientIntegration integration : ModernFixClient.CLIENT_INTEGRATIONS) {
11293
model = integration.onBakedModelLoad(location, unbakedModel, model, state, this.field_40571);
11394
}
11495

115-
// Allow more capturing
116-
this.capturedLocation = null;
117-
this.capturedModel = null;
118-
11996
return model;
12097
}
12198
}

0 commit comments

Comments
 (0)