|
| 1 | +package org.embeddedt.modernfix.forge.mixin.perf.dynamic_resources.ldlib; |
| 2 | + |
| 3 | +import com.lowdragmc.lowdraglib.LDLib; |
| 4 | +import com.lowdragmc.lowdraglib.client.ClientProxy; |
| 5 | +import com.lowdragmc.lowdraglib.client.forge.ClientProxyImpl; |
| 6 | +import com.lowdragmc.lowdraglib.client.model.custommodel.LDLMetadataSection; |
| 7 | +import com.lowdragmc.lowdraglib.client.model.forge.CustomBakedModelImpl; |
| 8 | +import com.lowdragmc.lowdraglib.client.model.forge.LDLRendererModel; |
| 9 | +import net.minecraft.client.renderer.texture.TextureAtlasSprite; |
| 10 | +import net.minecraft.client.resources.model.BakedModel; |
| 11 | +import net.minecraft.client.resources.model.Material; |
| 12 | +import net.minecraft.client.resources.model.ModelBakery; |
| 13 | +import net.minecraft.client.resources.model.ModelResourceLocation; |
| 14 | +import net.minecraft.client.resources.model.ModelState; |
| 15 | +import net.minecraft.client.resources.model.UnbakedModel; |
| 16 | +import net.minecraft.resources.ResourceLocation; |
| 17 | +import org.embeddedt.modernfix.ModernFixClient; |
| 18 | +import org.embeddedt.modernfix.annotation.ClientOnlyMixin; |
| 19 | +import org.embeddedt.modernfix.annotation.RequiresMod; |
| 20 | +import org.embeddedt.modernfix.api.entrypoint.ModernFixClientIntegration; |
| 21 | +import org.spongepowered.asm.mixin.Mixin; |
| 22 | +import org.spongepowered.asm.mixin.injection.At; |
| 23 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 24 | +import org.spongepowered.asm.mixin.injection.Redirect; |
| 25 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 26 | + |
| 27 | +import java.util.ArrayDeque; |
| 28 | +import java.util.Deque; |
| 29 | +import java.util.HashSet; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.function.Function; |
| 33 | + |
| 34 | +@Mixin(ClientProxyImpl.class) |
| 35 | +@ClientOnlyMixin |
| 36 | +@RequiresMod("ldlib") |
| 37 | +public abstract class ClientProxyImplMixin implements ModernFixClientIntegration { |
| 38 | + @Inject(method = "<init>", at = @At("RETURN")) |
| 39 | + private void registerIntegration(CallbackInfo ci) { |
| 40 | + ModernFixClient.CLIENT_INTEGRATIONS.add(this); |
| 41 | + } |
| 42 | + |
| 43 | + @Redirect(method = "modelBake", at = @At(value = "INVOKE", target = "Ljava/util/Map;entrySet()Ljava/util/Set;", ordinal = 0), remap = false) |
| 44 | + private Set<?> disableLoop(Map<?, ?> map) { |
| 45 | + return Set.of(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public BakedModel onBakedModelLoad(ResourceLocation rl, UnbakedModel rootModel, BakedModel baked, ModelState state, ModelBakery bakery, Function<Material, TextureAtlasSprite> textureGetter) { |
| 50 | + if (baked == null) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + if (rl instanceof ModelResourceLocation && rootModel != null) { |
| 54 | + if (baked instanceof LDLRendererModel) { |
| 55 | + return baked; |
| 56 | + } |
| 57 | + if (baked.isCustomRenderer()) { // Nothing we can add to builtin models |
| 58 | + return baked; |
| 59 | + } |
| 60 | + Deque<ResourceLocation> dependencies = new ArrayDeque<>(); |
| 61 | + Set<ResourceLocation> seenModels = new HashSet<>(); |
| 62 | + dependencies.push(rl); |
| 63 | + seenModels.add(rl); |
| 64 | + boolean shouldWrap = ClientProxy.WRAPPED_MODELS.getOrDefault(rl, false); |
| 65 | + // Breadth-first loop through dependencies, exiting as soon as a CTM texture is found, and skipping duplicates/cycles |
| 66 | + while (!shouldWrap && !dependencies.isEmpty()) { |
| 67 | + ResourceLocation dep = dependencies.pop(); |
| 68 | + UnbakedModel model; |
| 69 | + try { |
| 70 | + model = dep == rl ? rootModel : bakery.getModel(dep); |
| 71 | + } catch (Exception e) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + try { |
| 75 | + Set<Material> textures = new HashSet<>(ClientProxy.SCRAPED_TEXTURES.get(dep)); |
| 76 | + for (Material tex : textures) { |
| 77 | + // Cache all dependent texture metadata |
| 78 | + // At least one texture has CTM metadata, so we should wrap this baked |
| 79 | + if (!LDLMetadataSection.getMetadata(LDLMetadataSection.spriteToAbsolute(tex.texture())).isMissing()) { // TODO lazy |
| 80 | + shouldWrap = true; |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + if (!shouldWrap) { |
| 85 | + for (ResourceLocation newDep : model.getDependencies()) { |
| 86 | + if (seenModels.add(newDep)) { |
| 87 | + dependencies.push(newDep); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } catch (Exception e) { |
| 92 | + LDLib.LOGGER.error("Error loading baked dependency {} for baked {}. Skipping...", dep, rl, e); |
| 93 | + } |
| 94 | + } |
| 95 | + ClientProxy.WRAPPED_MODELS.put(rl, shouldWrap); |
| 96 | + if (shouldWrap) { |
| 97 | + return new CustomBakedModelImpl(baked); |
| 98 | + } |
| 99 | + } |
| 100 | + return baked; |
| 101 | + } |
| 102 | +} |
0 commit comments