diff --git a/src/main/java/com/warmthdawn/mod/gugu_utils/asm/GuGuUtilsCore.java b/src/main/java/com/warmthdawn/mod/gugu_utils/asm/GuGuUtilsCore.java index afcfd0c..82b5f06 100644 --- a/src/main/java/com/warmthdawn/mod/gugu_utils/asm/GuGuUtilsCore.java +++ b/src/main/java/com/warmthdawn/mod/gugu_utils/asm/GuGuUtilsCore.java @@ -1,6 +1,8 @@ package com.warmthdawn.mod.gugu_utils.asm; import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; import java.util.Map; @@ -9,6 +11,8 @@ @IFMLLoadingPlugin.Name("gugu-utils-core") @IFMLLoadingPlugin.TransformerExclusions({"com.warmthdawn.mod.gugu_utils.asm"}) public class GuGuUtilsCore implements IFMLLoadingPlugin { + + public static final Logger logger = LogManager.getLogger("GuGu Utils Core"); @Override public String[] getASMTransformerClass() { return new String[] { diff --git a/src/main/java/com/warmthdawn/mod/gugu_utils/asm/common/GuGuAsmTransformer.java b/src/main/java/com/warmthdawn/mod/gugu_utils/asm/common/GuGuAsmTransformer.java index a9298b3..7a3c3b8 100644 --- a/src/main/java/com/warmthdawn/mod/gugu_utils/asm/common/GuGuAsmTransformer.java +++ b/src/main/java/com/warmthdawn/mod/gugu_utils/asm/common/GuGuAsmTransformer.java @@ -1,9 +1,11 @@ package com.warmthdawn.mod.gugu_utils.asm.common; +import com.warmthdawn.mod.gugu_utils.asm.GuGuUtilsCore; import com.warmthdawn.mod.gugu_utils.asm.transformers.ActiveMachineRecipeTransformer; import com.warmthdawn.mod.gugu_utils.asm.transformers.DynamicMachineDeserializerTransformer; import com.warmthdawn.mod.gugu_utils.asm.transformers.TileMachineControllerTransformer; +import com.warmthdawn.mod.gugu_utils.asm.utils.SafeClassWriter; import net.minecraft.launchwrapper.IClassTransformer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -23,7 +25,6 @@ public class GuGuAsmTransformer implements IClassTransformer { tweakedClasses.put("hellfirepvp.modularmachinery.common.machine.DynamicMachine$MachineDeserializer", new DynamicMachineDeserializerTransformer()); } - private final Logger logger = LogManager.getLogger("GuGu Utils Core"); @Override @@ -32,7 +33,7 @@ public byte[] transform(String name, String transformedName, byte[] basicClass) return basicClass; } - logger.info("Transforming: " + transformedName); + GuGuUtilsCore.logger.info("Transforming: " + transformedName); try { ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(basicClass); @@ -40,7 +41,7 @@ public byte[] transform(String name, String transformedName, byte[] basicClass) tweakedClasses.getOrDefault(transformedName, MyTransformer.EMPTY_TRANSFORMER).transform(classNode); - ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); + ClassWriter classWriter = new SafeClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); classNode.accept(classWriter); return classWriter.toByteArray(); } catch (Exception e) { diff --git a/src/main/java/com/warmthdawn/mod/gugu_utils/asm/mixin/MixinModularMachinery.java b/src/main/java/com/warmthdawn/mod/gugu_utils/asm/mixin/MixinModularMachinery.java index 8f4ae67..cf403de 100644 --- a/src/main/java/com/warmthdawn/mod/gugu_utils/asm/mixin/MixinModularMachinery.java +++ b/src/main/java/com/warmthdawn/mod/gugu_utils/asm/mixin/MixinModularMachinery.java @@ -4,6 +4,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; +import com.warmthdawn.mod.gugu_utils.asm.GuGuUtilsCore; import com.warmthdawn.mod.gugu_utils.modularmachenary.IColorableTileEntity; import com.warmthdawn.mod.gugu_utils.modularmachenary.tweak.MMRecipeFailureActions; import hellfirepvp.modularmachinery.ModularMachinery; diff --git a/src/main/java/com/warmthdawn/mod/gugu_utils/asm/transformers/TileMachineControllerTransformer.java b/src/main/java/com/warmthdawn/mod/gugu_utils/asm/transformers/TileMachineControllerTransformer.java index c9e7c85..971aaff 100644 --- a/src/main/java/com/warmthdawn/mod/gugu_utils/asm/transformers/TileMachineControllerTransformer.java +++ b/src/main/java/com/warmthdawn/mod/gugu_utils/asm/transformers/TileMachineControllerTransformer.java @@ -20,7 +20,7 @@ public void transform(ClassNode classNode) { while (inserator.hasNext()) { AbstractInsnNode in = inserator.next(); - if (!AsmUtils.matchMethodInsn(in, Opcodes.INVOKEVIRTUAL, "getTileEntity", null, null, null)) { + if (!AsmUtils.matchMethodInsn(in, Opcodes.INVOKEVIRTUAL, "getTileEntity", "func_175625_s", null, null, null)) { continue; } InsnList hook = new InsnList(); diff --git a/src/main/java/com/warmthdawn/mod/gugu_utils/asm/utils/AsmUtils.java b/src/main/java/com/warmthdawn/mod/gugu_utils/asm/utils/AsmUtils.java index a815f82..d32c66d 100644 --- a/src/main/java/com/warmthdawn/mod/gugu_utils/asm/utils/AsmUtils.java +++ b/src/main/java/com/warmthdawn/mod/gugu_utils/asm/utils/AsmUtils.java @@ -7,8 +7,9 @@ import java.util.Optional; public final class AsmUtils { + public static boolean matchMethod(@NotNull MethodNode method, @NotNull String name, @Nullable String desc) { - return name.equals(method.name) && + return (name.equals(method.name)) && (desc == null || desc.equals(method.desc)); } @@ -21,21 +22,38 @@ public static Optional findMethod(@NotNull ClassNode classNode, @Not return Optional.empty(); } - public static boolean matchMethodInsn(@NotNull AbstractInsnNode node, int opCode, @NotNull String name, @Nullable String owner, @Nullable String desc, @Nullable Boolean itf) { + public static boolean matchMethodInsn(@NotNull AbstractInsnNode node, int opCode, @NotNull String name, + @Nullable String owner, @Nullable String desc, @Nullable Boolean itf) { + + return matchMethodInsn(node, opCode, name, null, owner, desc, itf); + } + + public static boolean matchMethodInsn(@NotNull AbstractInsnNode node, int opCode, @NotNull String name, + @Nullable String srgName, @Nullable String owner, @Nullable String desc, @Nullable Boolean itf) { if (!(node instanceof MethodInsnNode)) return false; MethodInsnNode methodInsnNode = (MethodInsnNode) node; - return name.equals(methodInsnNode.name) && + return name.equals(methodInsnNode.name) || + (srgName != null && srgName.equals(methodInsnNode.name)) && + (opCode == methodInsnNode.getOpcode()) && (owner == null || owner.equals(methodInsnNode.owner)) && (desc == null || desc.equals(methodInsnNode.desc)) && (itf == null || itf.equals(methodInsnNode.itf)); } - public static boolean matchFieldInsn(@NotNull AbstractInsnNode node, int opCode, @NotNull String name, @Nullable String owner, @Nullable String desc) { + public static boolean matchFieldInsn(@NotNull AbstractInsnNode node, int opCode, @NotNull String name, + @Nullable String owner, @Nullable String desc) { + return matchFieldInsn(node, opCode, name, null, owner, desc); + } + + public static boolean matchFieldInsn(@NotNull AbstractInsnNode node, int opCode, @NotNull String name, + @Nullable String srgName, @Nullable String owner, @Nullable String desc) { if (!(node instanceof FieldInsnNode)) return false; FieldInsnNode fieldInsnNode = (FieldInsnNode) node; - return name.equals(fieldInsnNode.name) && + return name.equals(fieldInsnNode.name) || + (srgName != null && srgName.equals(fieldInsnNode.name)) && + (opCode == fieldInsnNode.getOpcode()) && (owner == null || owner.equals(fieldInsnNode.owner)) && (desc == null || desc.equals(fieldInsnNode.desc)); } diff --git a/src/main/java/com/warmthdawn/mod/gugu_utils/asm/utils/SafeClassWriter.java b/src/main/java/com/warmthdawn/mod/gugu_utils/asm/utils/SafeClassWriter.java new file mode 100644 index 0000000..e171502 --- /dev/null +++ b/src/main/java/com/warmthdawn/mod/gugu_utils/asm/utils/SafeClassWriter.java @@ -0,0 +1,43 @@ +package com.warmthdawn.mod.gugu_utils.asm.utils; + +import net.minecraft.launchwrapper.Launch; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; + +/** + * Created by Thiakil on 16/11/2017. + */ +public class SafeClassWriter extends ClassWriter { + public SafeClassWriter(int flags) { + super(flags); + } + + public SafeClassWriter(ClassReader classReader, int flags) { + super(classReader, flags); + } + + protected String getCommonSuperClass(final String type1, final String type2) { + Class c, d; + ClassLoader classLoader = Launch.classLoader; + try { + c = Class.forName(type1.replace('/', '.'), false, classLoader); + d = Class.forName(type2.replace('/', '.'), false, classLoader); + } catch (Exception e) { + throw new RuntimeException(e.toString()); + } + if (c.isAssignableFrom(d)) { + return type1; + } + if (d.isAssignableFrom(c)) { + return type2; + } + if (c.isInterface() || d.isInterface()) { + return "java/lang/Object"; + } else { + do { + c = c.getSuperclass(); + } while (!c.isAssignableFrom(d)); + return c.getName().replace('.', '/'); + } + } +} \ No newline at end of file