|
| 1 | +/* |
| 2 | + * This file is part of FluidLogged. |
| 3 | + * |
| 4 | + * Copyright (C) 2025 The MEGA Team, FalsePattern |
| 5 | + * All Rights Reserved |
| 6 | + * |
| 7 | + * The above copyright notice, this permission notice and the word "MEGA" |
| 8 | + * shall be included in all copies or substantial portions of the Software. |
| 9 | + * |
| 10 | + * FluidLogged is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU Lesser General Public License as published by |
| 12 | + * the Free Software Foundation, only version 3 of the License. |
| 13 | + * |
| 14 | + * FluidLogged is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public License |
| 20 | + * along with FluidLogged. If not, see <https://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | + |
| 23 | +package mega.fluidlogged.internal.core; |
| 24 | + |
| 25 | +import com.falsepattern.lib.asm.ASMUtil; |
| 26 | +import com.falsepattern.lib.mapping.MappingManager; |
| 27 | +import com.falsepattern.lib.mapping.types.UniversalMethod; |
| 28 | +import com.falsepattern.lib.turboasm.ClassNodeHandle; |
| 29 | +import com.falsepattern.lib.turboasm.TurboClassTransformer; |
| 30 | +import lombok.val; |
| 31 | +import mega.fluidlogged.Tags; |
| 32 | +import org.jetbrains.annotations.NotNull; |
| 33 | +import org.objectweb.asm.Opcodes; |
| 34 | +import org.objectweb.asm.Type; |
| 35 | +import org.objectweb.asm.tree.InsnNode; |
| 36 | +import org.objectweb.asm.tree.MethodInsnNode; |
| 37 | +import org.objectweb.asm.tree.VarInsnNode; |
| 38 | + |
| 39 | +import net.minecraft.client.renderer.RenderBlocks; |
| 40 | + |
| 41 | +import java.lang.reflect.Method; |
| 42 | + |
| 43 | +/** |
| 44 | + * Waterlogging renderer hook |
| 45 | + * |
| 46 | + * Injection point: |
| 47 | + * <pre>{@code |
| 48 | + * <------------ here |
| 49 | + * int k3 = block.getRenderBlockPass(); |
| 50 | + * |
| 51 | + * if (k3 > k2) |
| 52 | + * { |
| 53 | + * flag = true; |
| 54 | + * } |
| 55 | + * |
| 56 | + * if (!block.canRenderInPass(k2)) continue; |
| 57 | + * }</pre> |
| 58 | + * |
| 59 | + * Injected code snippet: |
| 60 | + * <pre>{@code |
| 61 | + * int tmp = ASMHooks.drawFluidLogged(renderblocks, x, y, z, pass); |
| 62 | + * nextPass |= tmp & 1; |
| 63 | + * renderedAnything |= (tmp >>> 1) & 1; |
| 64 | + * }</pre> |
| 65 | + */ |
| 66 | +public class FluidLogRendererInjector implements TurboClassTransformer { |
| 67 | + @Override |
| 68 | + public String owner() { |
| 69 | + return Tags.MOD_ID; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public String name() { |
| 74 | + return "FluidLogRendererInjector"; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public boolean shouldTransformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) { |
| 79 | + return "net.minecraft.client.renderer.WorldRenderer".equals(className); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public boolean transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) { |
| 84 | + val cn = classNode.getNode(); |
| 85 | + if (cn == null) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + val method = ASMUtil.findMethodFromMCP(cn, "updateRenderer", "(Lnet/minecraft/entity/EntityLivingBase;)V", false); |
| 89 | + val iter = method.instructions.iterator(); |
| 90 | + while (iter.hasNext()) { |
| 91 | + val insn = iter.next(); |
| 92 | + if (!(insn instanceof MethodInsnNode)) |
| 93 | + continue; |
| 94 | + val mInsn = (MethodInsnNode) insn; |
| 95 | + if (!"net/minecraft/block/Block".equals(mInsn.owner)) |
| 96 | + continue; |
| 97 | + UniversalMethod uMethod; |
| 98 | + try { |
| 99 | + uMethod = MappingManager.getMethod(mInsn); |
| 100 | + } catch (ClassNotFoundException | NoSuchMethodException ignored) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + if (!"getRenderBlockPass".equals(uMethod.name.mcp())) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + iter.previous(); |
| 107 | + iter.add(new VarInsnNode(Opcodes.ALOAD, 16)); |
| 108 | + iter.add(new VarInsnNode(Opcodes.ILOAD, 23)); |
| 109 | + iter.add(new VarInsnNode(Opcodes.ILOAD, 21)); |
| 110 | + iter.add(new VarInsnNode(Opcodes.ILOAD, 22)); |
| 111 | + iter.add(new VarInsnNode(Opcodes.ILOAD, 17)); |
| 112 | + Method target; |
| 113 | + try { |
| 114 | + target = ASMHooks.class.getDeclaredMethod("drawFluidLogged", RenderBlocks.class, int.class, int.class, int.class, int.class); |
| 115 | + } catch (NoSuchMethodException e) { |
| 116 | + throw new RuntimeException(e); |
| 117 | + } |
| 118 | + iter.add(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(target.getDeclaringClass()), target.getName(), Type.getMethodDescriptor(target), false)); |
| 119 | + iter.add(new InsnNode(Opcodes.DUP)); |
| 120 | + iter.add(new InsnNode(Opcodes.ICONST_1)); |
| 121 | + iter.add(new InsnNode(Opcodes.IAND)); |
| 122 | + iter.add(new VarInsnNode(Opcodes.ILOAD, 18)); |
| 123 | + iter.add(new InsnNode(Opcodes.IOR)); |
| 124 | + iter.add(new VarInsnNode(Opcodes.ISTORE, 18)); |
| 125 | + iter.add(new InsnNode(Opcodes.ICONST_1)); |
| 126 | + iter.add(new InsnNode(Opcodes.IUSHR)); |
| 127 | + iter.add(new InsnNode(Opcodes.ICONST_1)); |
| 128 | + iter.add(new InsnNode(Opcodes.IAND)); |
| 129 | + iter.add(new VarInsnNode(Opcodes.ILOAD, 19)); |
| 130 | + iter.add(new InsnNode(Opcodes.IOR)); |
| 131 | + iter.add(new VarInsnNode(Opcodes.ISTORE, 19)); |
| 132 | + return true; |
| 133 | + } |
| 134 | + return false; |
| 135 | + } |
| 136 | +} |
0 commit comments