|
| 1 | +package org.embeddedt.modernfix.forge.mixin.perf.resourcefullib_highlight_deduplication; |
| 2 | + |
| 3 | +import com.teamresourceful.resourcefullib.client.highlights.HighlightHandler; |
| 4 | +import com.teamresourceful.resourcefullib.client.highlights.base.Highlight; |
| 5 | +import com.teamresourceful.resourcefullib.client.highlights.base.HighlightLine; |
| 6 | +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; |
| 7 | +import net.minecraft.world.level.block.state.BlockState; |
| 8 | +import org.embeddedt.modernfix.ModernFix; |
| 9 | +import org.embeddedt.modernfix.annotation.ClientOnlyMixin; |
| 10 | +import org.embeddedt.modernfix.annotation.RequiresMod; |
| 11 | +import org.joml.Vector3f; |
| 12 | +import org.spongepowered.asm.mixin.Mixin; |
| 13 | +import org.spongepowered.asm.mixin.injection.At; |
| 14 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 15 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 16 | + |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.function.Function; |
| 21 | + |
| 22 | +@Mixin(HighlightHandler.class) |
| 23 | +@RequiresMod("resourcefullib") |
| 24 | +@ClientOnlyMixin |
| 25 | +public class HighlightHandlerMixin { |
| 26 | + @Inject(method = "apply(Ljava/util/Map;Lnet/minecraft/server/packs/resources/ResourceManager;Lnet/minecraft/util/profiling/ProfilerFiller;)V", at = @At("RETURN"), remap = false) |
| 27 | + private void deduplicateHighlights(CallbackInfo ci) { |
| 28 | + Map<BlockState, Highlight> stateCache; |
| 29 | + |
| 30 | + try { |
| 31 | + var stateCacheMap = this.getClass().getDeclaredField("STATE_CACHE"); |
| 32 | + if (stateCacheMap.get(null) instanceof HashMap<?,?> hashMap) { |
| 33 | + stateCache = (Map<BlockState, Highlight>)hashMap; |
| 34 | + } else { |
| 35 | + throw new ReflectiveOperationException("Unexpected map type"); |
| 36 | + } |
| 37 | + } catch (ReflectiveOperationException e) { |
| 38 | + ModernFix.LOGGER.error("Not applying Resourceful Lib patch due to reflection error", e); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + ObjectOpenHashSet<Vector3f> pointCache = new ObjectOpenHashSet<>(); |
| 43 | + ObjectOpenHashSet<HighlightLine> lineCache = new ObjectOpenHashSet<>(); |
| 44 | + ObjectOpenHashSet<List<HighlightLine>> listCache = new ObjectOpenHashSet<>(); |
| 45 | + Function<HighlightLine, HighlightLine> deduplicator = l -> { |
| 46 | + if (!lineCache.contains(l)) { |
| 47 | + l = new HighlightLine( |
| 48 | + pointCache.addOrGet(l.start()), |
| 49 | + pointCache.addOrGet(l.end()), |
| 50 | + pointCache.addOrGet(l.normal()) |
| 51 | + ); |
| 52 | + } |
| 53 | + return lineCache.addOrGet(l); |
| 54 | + }; |
| 55 | + |
| 56 | + stateCache.replaceAll((rl, highlight) -> { |
| 57 | + if (highlight == null) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + List<HighlightLine> newList = highlight.lines(); |
| 61 | + if (!listCache.contains(newList)) { |
| 62 | + newList = newList.stream().map(deduplicator).toList(); |
| 63 | + } |
| 64 | + return new Highlight(highlight.id(), listCache.addOrGet(newList)); |
| 65 | + }); |
| 66 | + |
| 67 | + ModernFix.LOGGER.info("Deduplicated ResourcefulLib highlights ({} points, {} lines)", pointCache.size(), lineCache.size()); |
| 68 | + } |
| 69 | +} |
0 commit comments