2012-08-09 01:53:41 +00:00
|
|
|
package cpw.mods.fml.common.asm.transformers;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.objectweb.asm.ClassReader;
|
|
|
|
import org.objectweb.asm.ClassWriter;
|
|
|
|
import org.objectweb.asm.Type;
|
|
|
|
import org.objectweb.asm.tree.AnnotationNode;
|
|
|
|
import org.objectweb.asm.tree.ClassNode;
|
|
|
|
import org.objectweb.asm.tree.FieldNode;
|
|
|
|
import org.objectweb.asm.tree.MethodNode;
|
|
|
|
|
|
|
|
import cpw.mods.fml.common.asm.SideOnly;
|
2012-08-09 05:58:14 +00:00
|
|
|
import cpw.mods.fml.relauncher.FMLRelauncher;
|
2012-08-09 01:53:41 +00:00
|
|
|
import cpw.mods.fml.relauncher.IClassTransformer;
|
|
|
|
|
|
|
|
public class SideTransformer implements IClassTransformer
|
|
|
|
{
|
2012-08-09 05:58:14 +00:00
|
|
|
private static String SIDE = FMLRelauncher.side();
|
2012-08-12 08:23:10 +00:00
|
|
|
private static final boolean DEBUG = false;
|
2012-08-09 01:53:41 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
|
|
|
public byte[] transform(String name, byte[] bytes)
|
|
|
|
{
|
|
|
|
ClassNode classNode = new ClassNode();
|
|
|
|
ClassReader classReader = new ClassReader(bytes);
|
|
|
|
classReader.accept(classNode, 0);
|
|
|
|
|
2012-08-20 05:26:52 +00:00
|
|
|
if (remove((List<AnnotationNode>)classNode.visibleAnnotations, SIDE))
|
|
|
|
{
|
|
|
|
if (DEBUG)
|
|
|
|
{
|
|
|
|
System.out.println(String.format("Attempted to load class %s for invalid side %s", classNode.name, SIDE));
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-08-09 01:53:41 +00:00
|
|
|
Iterator<FieldNode> fields = classNode.fields.iterator();
|
|
|
|
while(fields.hasNext())
|
|
|
|
{
|
|
|
|
FieldNode field = fields.next();
|
|
|
|
if (remove((List<AnnotationNode>)field.visibleAnnotations, SIDE))
|
|
|
|
{
|
2012-08-12 08:23:10 +00:00
|
|
|
if (DEBUG)
|
|
|
|
{
|
|
|
|
System.out.println(String.format("Removing Field: %s.%s", classNode.name, field.name));
|
|
|
|
}
|
2012-08-09 01:53:41 +00:00
|
|
|
fields.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Iterator<MethodNode> methods = classNode.methods.iterator();
|
|
|
|
while(methods.hasNext())
|
|
|
|
{
|
|
|
|
MethodNode method = methods.next();
|
|
|
|
if (remove((List<AnnotationNode>)method.visibleAnnotations, SIDE))
|
|
|
|
{
|
2012-08-12 08:23:10 +00:00
|
|
|
if (DEBUG)
|
|
|
|
{
|
|
|
|
System.out.println(String.format("Removing Method: %s.%s%s", classNode.name, method.name, method.desc));
|
|
|
|
}
|
2012-08-09 01:53:41 +00:00
|
|
|
methods.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
|
|
|
|
classNode.accept(writer);
|
|
|
|
return writer.toByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean remove(List<AnnotationNode> anns, String side)
|
|
|
|
{
|
|
|
|
if (anns == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (AnnotationNode ann : anns)
|
|
|
|
{
|
|
|
|
if (ann.desc.equals(Type.getDescriptor(SideOnly.class)))
|
|
|
|
{
|
|
|
|
if (ann.values != null)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < ann.values.size() - 1; x += 2)
|
|
|
|
{
|
|
|
|
Object key = ann.values.get(x);
|
|
|
|
Object value = ann.values.get(x+1);
|
|
|
|
if (key instanceof String && key.equals("value"))
|
|
|
|
{
|
|
|
|
if (value instanceof String[] )
|
|
|
|
{
|
|
|
|
if (!((String[])value)[1].equals(side))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|