Skip to content

Commit

Permalink
Fix transformers not working
Browse files Browse the repository at this point in the history
  • Loading branch information
kappa-maintainer committed Jul 13, 2024
1 parent a97a4ee commit 517c573
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
7 changes: 6 additions & 1 deletion src/main/java/com/cleanroommc/fugue/helper/HookHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ private static String byGetResource() {
}

public static URI toURI(URL url) throws IOException, URISyntaxException {
return ((JarURLConnection) url.openConnection()).getJarFileURL().toURI();
URLConnection connection = url.openConnection();
if (connection instanceof JarURLConnection jarURLConnection) {
return jarURLConnection.getJarFileURL().toURI();
} else {
return url.toURI();
}
}

public static List<IClassTransformer> getTransformers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public byte[] transform(byte[] bytes) {
try {
var cp = ClassPool.getDefault();
CtClass cc = cp.makeClass(new ByteArrayInputStream(bytes));
cc.defrost();
var ifc = cc.getMethod("identifyFromClass", "(Ljava/lang/String;Ljava/util/Map;)Ljava/util/Set;");
ifc.insertAt(64, """
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public FinalStripperTransformer(Map<String, String> targets) {
public byte[] transform(byte[] bytes) {
try {
CtClass cc = ClassPool.getDefault().makeClass(new ByteArrayInputStream(bytes));
cc.defrost();
String[] fields = targets.get(cc.getName()).split("\\|");
Arrays.stream(cc.getFields()).filter(ctField ->
Arrays.stream(fields).anyMatch(f ->
Expand All @@ -29,6 +30,7 @@ public byte[] transform(byte[] bytes) {
ctField2.setModifiers(ctField2.getModifiers() & ~Modifier.FINAL);
Fugue.LOGGER.debug("Stripping final modifier of {} from {}", ctField2.getName(), ctField2.getDeclaringClass().getName());
});
cc.defrost();
bytes = cc.toBytecode();
} catch (Throwable t) {
Fugue.LOGGER.error("Exception {} on {}", t, this.getClass().getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,44 @@
import javassist.*;
import javassist.expr.ExprEditor;
import javassist.expr.MethodCall;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.*;
import top.outlands.foundation.IExplicitTransformer;

import java.io.ByteArrayInputStream;

public class ITweakerTransformer implements IExplicitTransformer {
@Override
public byte[] transform(byte[] bytes) {
try {
CtClass cc = ClassPool.getDefault().makeClass(new ByteArrayInputStream(bytes));
ExprEditor editor = new ExprEditor() {
@Override
public void edit(MethodCall m) throws CannotCompileException {
if (m.getMethodName().equals("toURI")) {
m.replace(
"""
{
$_ = com.cleanroommc.fugue.helper.HookHelper#toURI($0);
}
"""
);
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(bytes);
classReader.accept(classNode, 0);
if (classNode.methods != null)
{
for (MethodNode methodNode : classNode.methods)
{
InsnList instructions = methodNode.instructions;
if (instructions != null)
{
for (AbstractInsnNode insnNode : instructions)
{
if (insnNode.getOpcode() == Opcodes.INVOKEVIRTUAL && insnNode instanceof MethodInsnNode methodInsnNode)
{
if ("java/net/URL".equals(methodInsnNode.owner) && "toURI".equalsIgnoreCase(methodInsnNode.name))
{
instructions.insertBefore(methodInsnNode, new MethodInsnNode(Opcodes.INVOKESTATIC, "com/cleanroommc/fugue/helper/HookHelper", "toURI", "(Ljava/net/URL;)Ljava/net/URI;", false));
instructions.remove(methodInsnNode);
}
}
}
}
};
cc.instrument(editor);
bytes = cc.toBytecode();
} catch (Throwable t) {
Fugue.LOGGER.error("Exception {} on {}", t, this.getClass().getSimpleName());
}
}
return bytes;
ClassWriter classWriter = new ClassWriter(0);

classNode.accept(classWriter);
return classWriter.toByteArray();
}
}

0 comments on commit 517c573

Please sign in to comment.