Search in sources :

Example 1 with MultipartBakedModel

use of net.minecraft.client.renderer.block.model.MultipartBakedModel in project FoamFix by asiekierka.

the class FoamFixModelDeduplicate method onModelBake.

@SubscribeEvent(priority = EventPriority.LOW)
public void onModelBake(ModelBakeEvent event) {
    if (FoamFixShared.config.dbgCountModels) {
        debugCountModels(event);
    }
    Map<ResourceLocation, IModel> cache;
    try {
        cache = (Map<ResourceLocation, IModel>) MethodHandleHelper.findFieldGetter(ModelLoaderRegistry.class, "cache").invoke();
    } catch (Throwable t) {
        t.printStackTrace();
        cache = Collections.emptyMap();
    }
    if (FoamFixShared.config.clWipeModelCache) {
        int itemsCleared = 0;
        FoamFix.logger.info("Clearing ModelLoaderRegistry cache (" + cache.size() + " items)...");
        int cacheSize = cache.size();
        cache.entrySet().removeIf((e) -> {
            ResourceLocation r = e.getKey();
            if ("minecraft".equals(r.getResourceDomain()) || "fml".equals(r.getResourceDomain()) || "forge".equals(r.getResourceDomain())) {
                if (r.getResourcePath().endsWith("/generated")) {
                    return false;
                }
                if (r.getResourcePath().startsWith("builtin/")) {
                    return false;
                }
            }
            return true;
        });
        itemsCleared += cacheSize - cache.size();
        FoamFix.logger.info("Cleared " + itemsCleared + " objects.");
        cache = Collections.emptyMap();
    }
    if (FoamFixShared.config.geDeduplicate) {
        FoamFix.logger.info("Deduplicating models...");
        try {
            if (cache != null) {
                ProgressManager.ProgressBar bakeBar = ProgressManager.push("FoamFix: deduplicating", cache.size() + 2);
                if (ProxyClient.deduplicator == null) {
                    ProxyClient.deduplicator = new Deduplicator();
                }
                ProxyClient.deduplicator.maxRecursion = FoamFixShared.config.clDeduplicateRecursionLevel;
                ProxyClient.deduplicator.addObjects(ForgeRegistries.BLOCKS.getKeys());
                ProxyClient.deduplicator.addObjects(ForgeRegistries.ITEMS.getKeys());
                try {
                    bakeBar.step("Vertex formats");
                    for (Field f : DefaultVertexFormats.class.getDeclaredFields()) {
                        if (f.getType() == VertexFormat.class) {
                            f.setAccessible(true);
                            ProxyClient.deduplicator.deduplicateObject(f.get(null), 0);
                        }
                    }
                } catch (Exception e) {
                }
                if (FoamFixShared.config.clDeduplicateIModels) {
                    for (ResourceLocation loc : cache.keySet()) {
                        IModel model = cache.get(loc);
                        String modelName = loc.toString();
                        bakeBar.step(String.format("[%s]", modelName));
                        try {
                            ProxyClient.deduplicator.addObject(loc);
                            ProxyClient.deduplicator.deduplicateObject(model, 0);
                        } catch (Exception e) {
                        }
                    }
                }
                try {
                    bakeBar.step("Stats");
                    for (Field f : StatList.class.getDeclaredFields()) {
                        if (f.getType() == StatBase[].class) {
                            f.setAccessible(true);
                            for (StatBase statBase : (StatBase[]) f.get(null)) {
                                ProxyClient.deduplicator.deduplicateObject(statBase, 0);
                            }
                        }
                    }
                    for (StatBase statBase : StatList.ALL_STATS) {
                        ProxyClient.deduplicator.deduplicateObject(statBase, 0);
                    }
                } catch (Exception e) {
                }
                ProgressManager.pop(bakeBar);
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
        ProgressManager.ProgressBar bakeBar = ProgressManager.push("FoamFix: deduplicating", event.getModelRegistry().getKeys().size());
        if (ProxyClient.deduplicator == null) {
            ProxyClient.deduplicator = new Deduplicator();
        }
        ProxyClient.deduplicator.maxRecursion = FoamFixShared.config.clDeduplicateRecursionLevel;
        FoamFix.logger.info("Deduplicating models...");
        for (ModelResourceLocation loc : event.getModelRegistry().getKeys()) {
            IBakedModel model = event.getModelRegistry().getObject(loc);
            String modelName = loc.toString();
            bakeBar.step(String.format("[%s]", modelName));
            if (model instanceof MultipartBakedModel) {
                ProxyClient.deduplicator.successfuls++;
                model = new FoamyMultipartBakedModel((MultipartBakedModel) model);
            }
            try {
                ProxyClient.deduplicator.addObject(loc);
                event.getModelRegistry().putObject(loc, (IBakedModel) ProxyClient.deduplicator.deduplicateObject(model, 0));
            } catch (Exception e) {
            }
        }
        ProgressManager.pop(bakeBar);
        FoamFix.logger.info("Deduplicated " + ProxyClient.deduplicator.successfuls + " (+ " + ProxyClient.deduplicator.successfulTrims + ") objects.");
    /* List<Class> map = Lists.newArrayList(ProxyClient.deduplicator.dedupObjDataMap.keySet());
            map.sort(Comparator.comparingInt(a -> ProxyClient.deduplicator.dedupObjDataMap.get(a)));
            for (Class c : map) {
                FoamFix.logger.info(c.getSimpleName() + " = " + ProxyClient.deduplicator.dedupObjDataMap.get(c));
            } */
    }
    // release deduplicator to save memory
    ProxyClient.deduplicator = null;
    FoamFix.updateRamSaved();
}
Also used : IModel(net.minecraftforge.client.model.IModel) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) Field(java.lang.reflect.Field) ModelLoaderRegistry(net.minecraftforge.client.model.ModelLoaderRegistry) ProgressManager(net.minecraftforge.fml.common.ProgressManager) MultipartBakedModel(net.minecraft.client.renderer.block.model.MultipartBakedModel) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) ResourceLocation(net.minecraft.util.ResourceLocation) StatBase(net.minecraft.stats.StatBase) IBakedModel(net.minecraft.client.renderer.block.model.IBakedModel) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Aggregations

Field (java.lang.reflect.Field)1 IBakedModel (net.minecraft.client.renderer.block.model.IBakedModel)1 ModelResourceLocation (net.minecraft.client.renderer.block.model.ModelResourceLocation)1 MultipartBakedModel (net.minecraft.client.renderer.block.model.MultipartBakedModel)1 StatBase (net.minecraft.stats.StatBase)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 IModel (net.minecraftforge.client.model.IModel)1 ModelLoaderRegistry (net.minecraftforge.client.model.ModelLoaderRegistry)1 ProgressManager (net.minecraftforge.fml.common.ProgressManager)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1