Skip to content

Commit 545fb38

Browse files
committed
Start rewriting dynamic resources
1 parent 09ea5e1 commit 545fb38

File tree

7 files changed

+43
-467
lines changed

7 files changed

+43
-467
lines changed

common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/BlockStateModelLoaderMixin.java

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 14 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,38 @@
11
package org.embeddedt.modernfix.common.mixin.perf.dynamic_resources;
22

3-
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
3+
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
44
import net.minecraft.client.renderer.ItemModelShaper;
55
import net.minecraft.client.resources.model.BakedModel;
6-
import net.minecraft.client.resources.model.ModelManager;
7-
import net.minecraft.client.resources.model.ModelResourceLocation;
86
import net.minecraft.resources.ResourceLocation;
9-
import net.minecraft.world.item.Item;
107
import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
11-
import org.embeddedt.modernfix.dynamicresources.DynamicModelCache;
12-
import org.embeddedt.modernfix.dynamicresources.ModelLocationCache;
13-
import org.embeddedt.modernfix.util.DynamicInt2ObjectMap;
148
import org.spongepowered.asm.mixin.*;
159
import org.spongepowered.asm.mixin.injection.At;
1610
import org.spongepowered.asm.mixin.injection.Inject;
1711
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
12+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
1813

19-
import java.util.HashMap;
2014
import java.util.Map;
2115

2216
@Mixin(ItemModelShaper.class)
2317
@ClientOnlyMixin
2418
public abstract class ItemModelShaperMixin {
25-
26-
@Shadow public abstract ModelManager getModelManager();
27-
28-
@Shadow @Final @Mutable private Int2ObjectMap<BakedModel> shapesCache;
29-
30-
private Map<Item, ModelResourceLocation> overrideLocationsVanilla;
31-
32-
public ItemModelShaperMixin() {
33-
super();
34-
}
35-
36-
private static final ModelResourceLocation SENTINEL_VANILLA = new ModelResourceLocation(ResourceLocation.fromNamespaceAndPath("modernfix", "sentinel"), "sentinel");
37-
38-
private final DynamicModelCache<Item> mfix$itemModelCache = new DynamicModelCache<>(k -> this.mfix$getModelForItem((Item)k), true);
19+
@Shadow @Final @Mutable private Map<ResourceLocation, BakedModel> modelToBakedModel;
3920

4021
@Inject(method = "<init>", at = @At("RETURN"))
41-
private void replaceLocationMap(CallbackInfo ci) {
42-
overrideLocationsVanilla = new HashMap<>();
43-
this.shapesCache = new DynamicInt2ObjectMap<>(index -> getModelManager().getModel(ModelLocationCache.get(Item.byId(index))));
44-
}
45-
46-
@Unique
47-
private ModelResourceLocation mfix$getLocation(Item item) {
48-
ModelResourceLocation map = overrideLocationsVanilla.getOrDefault(item, SENTINEL_VANILLA);
49-
if(map == SENTINEL_VANILLA) {
50-
/* generate the appropriate location from our cache */
51-
map = ModelLocationCache.get(item);
52-
}
53-
return map;
54-
}
55-
56-
57-
private BakedModel mfix$getModelForItem(Item item) {
58-
ModelResourceLocation map = mfix$getLocation(item);
59-
return map == null ? null : getModelManager().getModel(map);
60-
}
61-
62-
/**
63-
* @author embeddedt
64-
* @reason Get the stored location for that item and meta, and get the model
65-
* from that location from the model manager.
66-
**/
67-
@Overwrite
68-
public BakedModel getItemModel(Item item) {
69-
return this.mfix$itemModelCache.get(item);
22+
private void initializeLazyCache(CallbackInfo ci) {
23+
this.modelToBakedModel = new Object2ObjectLinkedOpenHashMap<>(this.modelToBakedModel);
7024
}
7125

7226
/**
7327
* @author embeddedt
74-
* @reason Don't get all models during init (with dynamic loading, that would
75-
* generate them all). Just store location instead.
76-
**/
77-
@Overwrite
78-
public void register(Item item, ModelResourceLocation location) {
79-
overrideLocationsVanilla.put(item, location);
80-
}
81-
82-
/**
83-
* @author embeddedt
84-
* @reason Disable cache rebuilding (with dynamic loading, that would generate
85-
* all models).
86-
**/
87-
@Overwrite
88-
public void rebuildCache() {
89-
this.mfix$itemModelCache.clear();
28+
* @reason Prevent all baked item models from being cached forever. We can safely mutate the map here as vanilla
29+
* also uses computeIfAbsent, which means multithreaded access is not safe in vanilla either.
30+
*/
31+
@Inject(method = "getItemModel(Lnet/minecraft/resources/ResourceLocation;)Lnet/minecraft/client/resources/model/BakedModel;", at = @At(value = "RETURN"))
32+
private void limitCacheSize(ResourceLocation resourceLocation, CallbackInfoReturnable<BakedModel> cir) {
33+
var map = modelToBakedModel;
34+
if (map instanceof Object2ObjectLinkedOpenHashMap<ResourceLocation, BakedModel> linkedMap && linkedMap.size() > 1000) {
35+
linkedMap.removeFirst();
36+
}
9037
}
9138
}

common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/ItemRendererMixin.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)