diff --git a/forge/build.gradle b/forge/build.gradle index d304ab3d..5344f33d 100644 --- a/forge/build.gradle +++ b/forge/build.gradle @@ -61,6 +61,7 @@ dependencies { modCompileOnly("curse.maven:supermartijncore-454372:4455391") modCompileOnly("vazkii.patchouli:Patchouli:1.19.2-77") modCompileOnly("curse.maven:cofhcore-69162:5374122") + modCompileOnly("curse.maven:resourcefullib-570073:5659871") // runtime remapping at home for (extraModJar in fileTree(dir: extraModsDir, include: '*.jar')) { diff --git a/forge/src/main/java/org/embeddedt/modernfix/forge/mixin/perf/resourcefullib_highlight_deduplication/HighlightHandlerMixin.java b/forge/src/main/java/org/embeddedt/modernfix/forge/mixin/perf/resourcefullib_highlight_deduplication/HighlightHandlerMixin.java new file mode 100644 index 00000000..157e694d --- /dev/null +++ b/forge/src/main/java/org/embeddedt/modernfix/forge/mixin/perf/resourcefullib_highlight_deduplication/HighlightHandlerMixin.java @@ -0,0 +1,69 @@ +package org.embeddedt.modernfix.forge.mixin.perf.resourcefullib_highlight_deduplication; + +import com.teamresourceful.resourcefullib.client.highlights.HighlightHandler; +import com.teamresourceful.resourcefullib.client.highlights.base.Highlight; +import com.teamresourceful.resourcefullib.client.highlights.base.HighlightLine; +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; +import net.minecraft.world.level.block.state.BlockState; +import org.embeddedt.modernfix.ModernFix; +import org.embeddedt.modernfix.annotation.ClientOnlyMixin; +import org.embeddedt.modernfix.annotation.RequiresMod; +import org.joml.Vector3f; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; + +@Mixin(HighlightHandler.class) +@RequiresMod("resourcefullib") +@ClientOnlyMixin +public class HighlightHandlerMixin { + @Inject(method = "apply(Ljava/util/Map;Lnet/minecraft/server/packs/resources/ResourceManager;Lnet/minecraft/util/profiling/ProfilerFiller;)V", at = @At("RETURN"), remap = false) + private void deduplicateHighlights(CallbackInfo ci) { + Map stateCache; + + try { + var stateCacheMap = this.getClass().getDeclaredField("STATE_CACHE"); + if (stateCacheMap.get(null) instanceof HashMap hashMap) { + stateCache = (Map)hashMap; + } else { + throw new ReflectiveOperationException("Unexpected map type"); + } + } catch (ReflectiveOperationException e) { + ModernFix.LOGGER.error("Not applying Resourceful Lib patch due to reflection error", e); + return; + } + + ObjectOpenHashSet pointCache = new ObjectOpenHashSet<>(); + ObjectOpenHashSet lineCache = new ObjectOpenHashSet<>(); + ObjectOpenHashSet> listCache = new ObjectOpenHashSet<>(); + Function deduplicator = l -> { + if (!lineCache.contains(l)) { + l = new HighlightLine( + pointCache.addOrGet(l.start()), + pointCache.addOrGet(l.end()), + pointCache.addOrGet(l.normal()) + ); + } + return lineCache.addOrGet(l); + }; + + stateCache.replaceAll((rl, highlight) -> { + if (highlight == null) { + return null; + } + List newList = highlight.lines(); + if (!listCache.contains(newList)) { + newList = newList.stream().map(deduplicator).toList(); + } + return new Highlight(highlight.id(), listCache.addOrGet(newList)); + }); + + ModernFix.LOGGER.info("Deduplicated ResourcefulLib highlights ({} points, {} lines)", pointCache.size(), lineCache.size()); + } +}