|
1 | 1 | package org.embeddedt.modernfix.common.mixin.perf.dynamic_resources; |
2 | 2 |
|
3 | | -import it.unimi.dsi.fastutil.ints.Int2ObjectMap; |
| 3 | +import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap; |
4 | 4 | import net.minecraft.client.renderer.ItemModelShaper; |
5 | 5 | import net.minecraft.client.resources.model.BakedModel; |
6 | | -import net.minecraft.client.resources.model.ModelManager; |
7 | | -import net.minecraft.client.resources.model.ModelResourceLocation; |
8 | 6 | import net.minecraft.resources.ResourceLocation; |
9 | | -import net.minecraft.world.item.Item; |
10 | 7 | 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; |
14 | 8 | import org.spongepowered.asm.mixin.*; |
15 | 9 | import org.spongepowered.asm.mixin.injection.At; |
16 | 10 | import org.spongepowered.asm.mixin.injection.Inject; |
17 | 11 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 12 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
18 | 13 |
|
19 | | -import java.util.HashMap; |
20 | 14 | import java.util.Map; |
21 | 15 |
|
22 | 16 | @Mixin(ItemModelShaper.class) |
23 | 17 | @ClientOnlyMixin |
24 | 18 | 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; |
39 | 20 |
|
40 | 21 | @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); |
70 | 24 | } |
71 | 25 |
|
72 | 26 | /** |
73 | 27 | * @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 | + } |
90 | 37 | } |
91 | 38 | } |
0 commit comments