From b4cef0b5c92d837359ef0cbbc280994eedec8013 Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Thu, 13 Apr 2023 10:16:33 -0700 Subject: [PATCH] Add Rubidium compatibility - Add proper Rubidium compatibility on our side - Overwrite methods in Rubidium's FlywheelCompat class with stubs to prevent crashes --- build.gradle | 4 +- .../flywheel/compat/SodiumMixinPlugin.java | 48 +++++++++++++++++++ .../VisualizedRenderDispatcher.java | 17 +++++++ .../sodium/ChunkRenderRebuildTaskMixin.java | 23 +++++++++ .../mixin/sodium/FlywheelCompatMixin.java | 24 ++++++++++ .../visualmanage/ChunkRebuildHooksMixin.java | 10 +--- .../resources/flywheel.sodium.mixins.json | 15 ++++++ 7 files changed, 132 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/compat/SodiumMixinPlugin.java create mode 100644 src/main/java/com/jozufozu/flywheel/mixin/sodium/ChunkRenderRebuildTaskMixin.java create mode 100644 src/main/java/com/jozufozu/flywheel/mixin/sodium/FlywheelCompatMixin.java create mode 100644 src/main/resources/flywheel.sodium.mixins.json diff --git a/build.gradle b/build.gradle index 60cd5e602..923f49b27 100644 --- a/build.gradle +++ b/build.gradle @@ -51,6 +51,7 @@ minecraft { property 'flw.debugMemorySafety', 'true' arg '-mixin.config=flywheel.mixins.json' + arg '-mixin.config=flywheel.sodium.mixins.json' mods { flywheel { @@ -66,6 +67,7 @@ minecraft { property 'forge.logging.console.level', 'debug' arg '-mixin.config=flywheel.mixins.json' + arg '-mixin.config=flywheel.sodium.mixins.json' mods { flywheel { @@ -188,7 +190,7 @@ jar { 'Implementation-Version' : project.jar.archiveVersion, //'Implementation-Vendor': 'flywheel authors', 'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), - 'MixinConfigs' : 'flywheel.mixins.json' + 'MixinConfigs' : 'flywheel.mixins.json,flywheel.sodium.mixins.json' ]) } } diff --git a/src/main/java/com/jozufozu/flywheel/compat/SodiumMixinPlugin.java b/src/main/java/com/jozufozu/flywheel/compat/SodiumMixinPlugin.java new file mode 100644 index 000000000..26da383c4 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/compat/SodiumMixinPlugin.java @@ -0,0 +1,48 @@ +package com.jozufozu.flywheel.compat; + +import java.util.List; +import java.util.Set; +import java.util.function.Supplier; + +import org.objectweb.asm.tree.ClassNode; +import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; +import org.spongepowered.asm.mixin.extensibility.IMixinInfo; + +import com.google.common.base.Suppliers; + +import net.minecraftforge.fml.loading.LoadingModList; + +public class SodiumMixinPlugin implements IMixinConfigPlugin { + private static final Supplier IS_SODIUM_LOADED = Suppliers.memoize(() -> LoadingModList.get().getModFileById("rubidium") != null); + + @Override + public void onLoad(String mixinPackage) { + } + + @Override + public String getRefMapperConfig() { + return null; + } + + @Override + public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { + return IS_SODIUM_LOADED.get(); + } + + @Override + public void acceptTargets(Set myTargets, Set otherTargets) { + } + + @Override + public List getMixins() { + return null; + } + + @Override + public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { + } + + @Override + public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { + } +} diff --git a/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizedRenderDispatcher.java b/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizedRenderDispatcher.java index 208f15811..781425a14 100644 --- a/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizedRenderDispatcher.java +++ b/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizedRenderDispatcher.java @@ -6,6 +6,7 @@ import com.jozufozu.flywheel.api.event.BeginFrameEvent; import com.jozufozu.flywheel.api.event.RenderStageEvent; import com.jozufozu.flywheel.api.visual.Effect; import com.jozufozu.flywheel.api.visual.Visual; +import com.jozufozu.flywheel.api.visualization.BlockEntityVisualizer; import com.jozufozu.flywheel.extension.ClientLevelExtension; import com.jozufozu.flywheel.impl.visualization.manager.VisualManager; import com.jozufozu.flywheel.lib.util.AnimationTickHolder; @@ -163,6 +164,22 @@ public class VisualizedRenderDispatcher { .forEach(world.getEntities()::add); } + public static boolean tryAddBlockEntity(T blockEntity) { + Level level = blockEntity.getLevel(); + if (FlwUtil.canUseVisualization(level)) { + return false; + } + + BlockEntityVisualizer visualizer = VisualizationHelper.getVisualizer(blockEntity); + if (visualizer == null) { + return false; + } + + getBlockEntities(level).queueAdd(blockEntity); + + return visualizer.shouldSkipRender(blockEntity); + } + public static void addDebugInfo(List info) { ClientLevel level = Minecraft.getInstance().level; if (FlwUtil.canUseVisualization(level)) { diff --git a/src/main/java/com/jozufozu/flywheel/mixin/sodium/ChunkRenderRebuildTaskMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/sodium/ChunkRenderRebuildTaskMixin.java new file mode 100644 index 000000000..c0aea1f06 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/mixin/sodium/ChunkRenderRebuildTaskMixin.java @@ -0,0 +1,23 @@ +package com.jozufozu.flywheel.mixin.sodium; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +import com.jozufozu.flywheel.impl.visualization.VisualizedRenderDispatcher; + +import me.jellysquid.mods.sodium.client.render.chunk.tasks.ChunkRenderRebuildTask; +import net.minecraft.client.renderer.blockentity.BlockEntityRenderDispatcher; +import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; +import net.minecraft.world.level.block.entity.BlockEntity; + +@Mixin(value = ChunkRenderRebuildTask.class, remap = false) +public class ChunkRenderRebuildTaskMixin { + @Redirect(method = "performBuild", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/blockentity/BlockEntityRenderDispatcher;getRenderer(Lnet/minecraft/world/level/block/entity/BlockEntity;)Lnet/minecraft/client/renderer/blockentity/BlockEntityRenderer;")) + private BlockEntityRenderer flywheel$redirectGetRenderer(BlockEntityRenderDispatcher dispatcher, BlockEntity blockEntity) { + if (VisualizedRenderDispatcher.tryAddBlockEntity(blockEntity)) { + return null; + } + return dispatcher.getRenderer(blockEntity); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/mixin/sodium/FlywheelCompatMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/sodium/FlywheelCompatMixin.java new file mode 100644 index 000000000..674a144cb --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/mixin/sodium/FlywheelCompatMixin.java @@ -0,0 +1,24 @@ +package com.jozufozu.flywheel.mixin.sodium; + +import java.util.Collection; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; + +import me.jellysquid.mods.sodium.client.compat.FlywheelCompat; +import net.minecraft.world.level.block.entity.BlockEntity; + +/** + * Overwrite all methods in this class with stubs. These methods use Flywheel classes that no longer exist and would cause a NoClassDefFoundError if invoked. + */ +@Mixin(value = FlywheelCompat.class, remap = false) +public class FlywheelCompatMixin { + @Overwrite + public static boolean addAndFilterBEs(BlockEntity blockEntity) { + return true; + } + + @Overwrite + public static void filterBlockEntityList(Collection blockEntities) { + } +} diff --git a/src/main/java/com/jozufozu/flywheel/mixin/visualmanage/ChunkRebuildHooksMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/visualmanage/ChunkRebuildHooksMixin.java index fddc991cc..43ab75e0d 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/visualmanage/ChunkRebuildHooksMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/visualmanage/ChunkRebuildHooksMixin.java @@ -7,9 +7,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import com.jozufozu.flywheel.impl.visualization.VisualizationHelper; import com.jozufozu.flywheel.impl.visualization.VisualizedRenderDispatcher; -import com.jozufozu.flywheel.util.FlwUtil; import net.minecraft.client.renderer.chunk.ChunkRenderDispatcher; import net.minecraft.world.level.block.entity.BlockEntity; @@ -18,12 +16,8 @@ import net.minecraft.world.level.block.entity.BlockEntity; public class ChunkRebuildHooksMixin { @Inject(method = "handleBlockEntity", at = @At("HEAD"), cancellable = true) private void flywheel$addAndFilterBEs(ChunkRenderDispatcher.CompiledChunk compiledChunk, Set set, E be, CallbackInfo ci) { - if (FlwUtil.canUseVisualization(be.getLevel())) { - if (VisualizationHelper.canVisualize(be)) - VisualizedRenderDispatcher.getBlockEntities(be.getLevel()).queueAdd(be); - - if (VisualizationHelper.shouldSkipRender(be)) - ci.cancel(); + if (VisualizedRenderDispatcher.tryAddBlockEntity(be)) { + ci.cancel(); } } } diff --git a/src/main/resources/flywheel.sodium.mixins.json b/src/main/resources/flywheel.sodium.mixins.json new file mode 100644 index 000000000..f97f130ca --- /dev/null +++ b/src/main/resources/flywheel.sodium.mixins.json @@ -0,0 +1,15 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "com.jozufozu.flywheel.mixin.sodium", + "compatibilityLevel": "JAVA_17", + "refmap": "flywheel.refmap.json", + "plugin": "com.jozufozu.flywheel.compat.SodiumMixinConfigPlugin", + "client": [ + "ChunkRenderRebuildTaskMixin", + "FlywheelCompatMixin" + ], + "injectors": { + "defaultRequire": 1 + } +}