Skip to content

Commit

Permalink
🐛 修复无法变色的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
warmthdawn committed Sep 29, 2021
1 parent 4c470b1 commit 9b00a6b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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[] {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -32,15 +33,15 @@ 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);
classReader.accept(classNode, 0);

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -21,21 +22,38 @@ public static Optional<MethodNode> 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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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('.', '/');
}
}
}

0 comments on commit 9b00a6b

Please sign in to comment.