aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/modernjava/launch/transform/BasePatch.java
blob: 1ef494c7b49ba3d3e3d091895bea8b3042517ccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package moe.nea.modernjava.launch.transform;

import net.minecraft.launchwrapper.IClassTransformer;
import org.jetbrains.annotations.NotNull;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.util.Printer;
import org.objectweb.asm.util.Textifier;
import org.objectweb.asm.util.TraceMethodVisitor;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public abstract class BasePatch implements IClassTransformer {
	protected abstract String getTargetedName();

	protected Type getTargetedType() {
		return getClassType(getTargetedName());
	}

	protected abstract ClassNode transform(ClassNode classNode);

	@Override
	public byte[] transform(String name, String transformedName, byte[] basicClass) {
		if (basicClass == null) return null;
		if (!getTargetedName().equals(name)) return basicClass;
		ClassNode node = new ClassNode();
		ClassReader reader = new ClassReader(basicClass);
		reader.accept(node, 0);
		ClassNode processedNode = transform(node);
		ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
		processedNode.accept(writer);
		return writer.toByteArray();
	}

	protected Iterable<AbstractInsnNode> iterableInstructions(InsnList insns) {
		return new Iterable<AbstractInsnNode>() {
			@NotNull
			@Override
			public Iterator<AbstractInsnNode> iterator() {
				return insns.iterator();
			}
		};
	}

	private Printer printer = new Textifier();
	private TraceMethodVisitor traceMethodVisitor = new TraceMethodVisitor(printer);

	protected String debugInsn(AbstractInsnNode insnNode) {
		insnNode.accept(traceMethodVisitor);
		StringWriter sw = new StringWriter();
		printer.print(new PrintWriter(sw));
		printer.getText().clear();
		return sw.toString();
	}

	protected List<String> debugInsnList(InsnList list) {
		List<String> strings = new ArrayList<>();
		for (AbstractInsnNode node : iterableInstructions(list)) {
			strings.add(debugInsn(node));
		}
		return strings;
	}

	protected Type getClassType(String plainName) {
		return Type.getObjectType(plainName.replace('.', '/'));
	}

	protected static MethodNode findMethod(ClassNode node, String name, Type desc) {
		System.out.println("Searching for " + name + " " + desc.getDescriptor());
		for (MethodNode method : node.methods) {
			System.out.println(" - Candidate: " + method.name + " " + method.desc);
			if (name.equals(method.name) && desc.getDescriptor().equals(method.desc)) {
				System.out.println("Found method");
				return method;
			}
		}
		System.out.println("Could not find method.");
		return null;
	}

}