codechicken.lib.asm.ASMHelper Java Examples

The following examples show how to use codechicken.lib.asm.ASMHelper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MCStripTransformer.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static byte[] transform(byte[] bytes) {
    ClassNode cnode = ASMHelper.createClassNode(bytes, ClassReader.EXPAND_FRAMES);

    boolean changed = false;
    Iterator<MethodNode> it = cnode.methods.iterator();
    while (it.hasNext()) {
        MethodNode mnode = it.next();
        ReferenceDetector r = new ReferenceDetector();
        mnode.accept(new RemappingMethodAdapter(mnode.access, mnode.desc, new MethodVisitor(Opcodes.ASM4) {
        }, r));
        if (r.found) {
            it.remove();
            changed = true;
        }
    }
    if (changed) {
        bytes = ASMHelper.createBytes(cnode, 0);
    }
    return bytes;
}
 
Example #2
Source File: DefaultImplementationTransformer.java    From CodeChickenCore with MIT License 6 votes vote down vote up
public boolean patch(ClassNode cnode) {
	LinkedList<String> names = new LinkedList<String>();
	for(MethodNode method : cnode.methods) {
		ObfMapping m = new ObfMapping(cnode.name, method.name, method.desc).toRuntime();
		names.add(m.s_name+m.s_desc);
	}

	boolean changed = false;
	for(MethodNode impl : impls) {
		if(names.contains(impl.name+impl.desc))
			continue;
		
		MethodNode copy = new MethodNode(impl.access, impl.name, impl.desc, 
				impl.signature, impl.exceptions == null ? null : impl.exceptions.toArray(new String[0]));
		ASMHelper.copy(impl, copy);
		cnode.methods.add(impl);
		changed = true;
	}
	return changed;
}
 
Example #3
Source File: ClassDiscoverer.java    From CodeChickenCore with MIT License 6 votes vote down vote up
private void checkAddClass(String resource) {
    try {
        String classname = resource.replace(".class", "").replace("\\", ".").replace("/", ".");
        byte[] bytes = Launch.classLoader.getClassBytes(classname);
        if (bytes == null)
            return;

        ClassNode cnode = ASMHelper.createClassNode(bytes);
        for (String superclass : superclasses)
            if (!cnode.interfaces.contains(superclass) && !cnode.superName.equals(superclass))
                return;

        addClass(classname);
    } catch (IOException e) {
        CodeChickenCorePlugin.logger.error("Unable to load class: " + resource, e);
    }
}
 
Example #4
Source File: MCStripTransformer.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static byte[] transform(byte[] bytes) {
    ClassNode cnode = ASMHelper.createClassNode(bytes, ClassReader.EXPAND_FRAMES);

    boolean changed = false;
    Iterator<MethodNode> it = cnode.methods.iterator();
    while(it.hasNext()) {
        MethodNode mnode = it.next();
        ReferenceDetector r = new ReferenceDetector();
        mnode.accept(new RemappingMethodAdapter(mnode.access, mnode.desc, new MethodVisitor(Opcodes.ASM4) {}, r));
        if(r.found) {
            it.remove();
            changed = true;
        }
    }
    if(changed)
        bytes = ASMHelper.createBytes(cnode, 0);
    return bytes;
}
 
Example #5
Source File: InterfaceDependancyTransformer.java    From CodeChickenCore with MIT License 5 votes vote down vote up
@Override
public byte[] transform(String name, String tname, byte[] bytes) {
    if (bytes == null) return null;
    ClassNode cnode = ASMHelper.createClassNode(bytes);

    boolean hasDependancyInterfaces = false;
    if (cnode.visibleAnnotations != null)
        for (AnnotationNode ann : cnode.visibleAnnotations)
            if (ann.desc.equals(Type.getDescriptor(InterfaceDependancies.class))) {
                hasDependancyInterfaces = true;
                break;
            }

    if (!hasDependancyInterfaces)
        return bytes;

    hasDependancyInterfaces = false;
    for (Iterator<String> iterator = cnode.interfaces.iterator(); iterator.hasNext(); ) {
        try {
            Launch.classLoader.findClass(new ObfMapping(iterator.next()).toRuntime().javaClass());
        } catch (ClassNotFoundException cnfe) {
            iterator.remove();
            hasDependancyInterfaces = true;
        }
    }

    if (!hasDependancyInterfaces)
        return bytes;

    return ASMHelper.createBytes(cnode, 0);
}
 
Example #6
Source File: DefaultImplementationTransformer.java    From CodeChickenCore with MIT License 5 votes vote down vote up
private static ClassNode getClassNode(String name) {
  	try {
	return ASMHelper.createClassNode(cl.getClassBytes(name.replace('/', '.')));
} catch (IOException e) {
	throw new RuntimeException(e);
}
  }
 
Example #7
Source File: DefaultImplementationTransformer.java    From CodeChickenCore with MIT License 5 votes vote down vote up
@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
	if(transformedName.startsWith("net.minecraft") || impls.isEmpty())
		return bytes;
	
	ClassNode cnode = ASMHelper.createClassNode(bytes);
	boolean changed = false;
	for(String iname : cnode.interfaces) {
		InterfaceImpl impl = impls.get(iname);
		if(impl != null)
			changed |= impl.patch(cnode);
	}
	
	return changed ? ASMHelper.createBytes(cnode, 0) : bytes;
}
 
Example #8
Source File: MCPDeobfuscationTransformer.java    From CodeChickenCore with MIT License 5 votes vote down vote up
@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (name.equals("net.minecraftforge.fml.common.Loader")) {
        bytes = injectCallback(bytes);
        activated = true;
    }

    if (!activated || bytes == null)
        return bytes;

    ClassNode cnode = ASMHelper.createClassNode(bytes, ClassReader.EXPAND_FRAMES);
    ClassWriter cw = new CC_ClassWriter(0, true);
    run.remap(cnode, cw);
    return cw.toByteArray();
}
 
Example #9
Source File: MCPDeobfuscationTransformer.java    From CodeChickenCore with MIT License 5 votes vote down vote up
@Override
public List<String> getParents(ObfuscationEntry desc) {
    try {
        String name = ObfMapping.obfuscated ? desc.obf.s_owner : desc.mcp.s_owner;
        name = name.replace('/', '.');
        byte[] bytes = Launch.classLoader.getClassBytes(name);
        if (bytes != null)
            return ObfuscationRun.getParents(ASMHelper.createClassNode(bytes));
    } catch (IOException e) {
    }
    return null;
}
 
Example #10
Source File: MCPDeobfuscationTransformer.java    From CodeChickenCore with MIT License 4 votes vote down vote up
private byte[] injectCallback(byte[] bytes) {
    ClassNode cnode = ASMHelper.createClassNode(bytes);
    MethodNode mnode = ASMHelper.findMethod(new ObfMapping(cnode.name, "<clinit>", "()V"), cnode);
    mnode.instructions.insert(new MethodInsnNode(INVOKESTATIC, "codechicken/core/asm/MCPDeobfuscationTransformer", "loadCallback", "()V"));
    return ASMHelper.createBytes(cnode, 0);
}