Skip to content

Commit

Permalink
Fix ccl patching
Browse files Browse the repository at this point in the history
  • Loading branch information
kappa-maintainer committed Jul 13, 2024
1 parent 25520fa commit a97a4ee
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
13 changes: 11 additions & 2 deletions src/main/java/com/cleanroommc/fugue/helper/HookHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import net.minecraft.launchwrapper.Launch;
import net.minecraft.launchwrapper.LaunchClassLoader;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.Mixins;
import top.outlands.foundation.TransformerDelegate;
import top.outlands.foundation.boot.ActualClassLoader;

Expand All @@ -16,6 +14,7 @@
import java.nio.file.Paths;
import java.security.CodeSource;
import java.util.*;
import java.util.function.Function;

public class HookHelper {
public static boolean isInterface(int opcode) {
Expand Down Expand Up @@ -144,4 +143,14 @@ public static void TickCentralPreLoad() {
public static boolean isClassExist(String clazz) {
return Launch.classLoader.isClassExist(clazz);
}

public static Object computeIfAbsent(Map<Object, Object> map, Object key, Function<Object, Object> function) {
if (map.containsKey(key)) {
return map.get(key);
} else {
Object value = function.apply(key);
map.put(key, value);
return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,45 @@
import com.cleanroommc.fugue.common.Fugue;
import javassist.ClassPool;
import javassist.CtClass;
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 ClassHierarchyManagerTransformer implements IExplicitTransformer {
@Override
public byte[] transform(byte[] bytes) {
try {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.makeClass(new ByteArrayInputStream(bytes));
cc.getMethod("getOrCreateCache", "(Ljava/lang/String;)Lcodechicken/asm/ClassHierarchyManager$SuperCache;").setBody(
"""
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(bytes);
classReader.accept(classNode, 0);
if (classNode.methods != null)
{
for (MethodNode methodNode : classNode.methods)
{
if (methodNode.name.equals("getOrCreateCache")) {
InsnList instructions = methodNode.instructions;
if (instructions != null)
{
codechicken.asm.ClassHierarchyManager.SuperCache cache;
if (!superclasses.containsKey($1)) {
cache = new codechicken.asm.ClassHierarchyManager.SuperCache();
superclasses.put($1, cache);
} else {
cache = superclasses.get($1);
}
return (codechicken.asm.ClassHierarchyManager.SuperCache)cache;
for (AbstractInsnNode insnNode : instructions)
{
if (insnNode instanceof MethodInsnNode methodInsnNode)
{
if (methodInsnNode.getOpcode() == Opcodes.INVOKEVIRTUAL) {
instructions.insert(methodInsnNode, new MethodInsnNode(Opcodes.INVOKESTATIC, "com/cleanroommc/fugue/helper/HookHelper", "computeIfAbsent", "(Ljava/util/Map;Ljava/lang/Object;Ljava/util/function/Function;)Ljava/lang/Object;", false));
instructions.remove(methodInsnNode);
}
}
}
}
""");
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 a97a4ee

Please sign in to comment.